2 * Copyright (C) 1984-2015 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
12 * An IFILE represents an input file.
14 * It is actually a pointer to an ifile structure,
15 * but is opaque outside this module.
16 * Ifile structures are kept in a linked list in the order they
17 * appear on the command line.
18 * Any new file which does not already appear in the list is
19 * inserted after the current file.
24 extern IFILE curr_ifile;
27 struct ifile *h_next; /* Links for command line list */
29 char *h_filename; /* Name of the file */
30 void *h_filestate; /* File state (used in ch.c) */
31 int h_index; /* Index within command line list */
32 int h_hold; /* Hold count */
33 char h_opened; /* Has this ifile been opened? */
34 struct scrpos h_scrpos; /* Saved position within the file */
38 * Convert an IFILE (external representation)
39 * to a struct file (internal representation), and vice versa.
41 #define int_ifile(h) ((struct ifile *)(h))
42 #define ext_ifile(h) ((IFILE)(h))
45 * Anchor for linked list.
47 static struct ifile anchor = { &anchor, &anchor, NULL, NULL, 0, 0, '\0',
48 { NULL_POSITION, 0 } };
49 static int ifiles = 0;
52 incr_index(struct ifile *p, int incr)
54 for (; p != &anchor; p = p->h_next)
59 * Link an ifile into the ifile list.
62 link_ifile(struct ifile *p, struct ifile *prev)
69 p->h_next = prev->h_next;
71 prev->h_next->h_prev = p;
74 * Calculate index for the new one,
75 * and adjust the indexes for subsequent ifiles in the list.
77 p->h_index = prev->h_index + 1;
78 incr_index(p->h_next, 1);
83 * Unlink an ifile from the ifile list.
86 unlink_ifile(struct ifile *p)
88 p->h_next->h_prev = p->h_prev;
89 p->h_prev->h_next = p->h_next;
90 incr_index(p->h_next, -1);
95 * Allocate a new ifile structure and stick a filename in it.
96 * It should go after "prev" in the list
97 * (or at the beginning of the list if "prev" is NULL).
98 * Return a pointer to the new ifile structure.
100 static struct ifile *
101 new_ifile(char *filename, struct ifile *prev)
106 * Allocate and initialize structure.
108 p = (struct ifile *) ecalloc(1, sizeof(struct ifile));
109 p->h_filename = save(filename);
110 p->h_scrpos.pos = NULL_POSITION;
113 p->h_filestate = NULL;
119 * Delete an existing ifile structure.
129 * If the ifile we're deleting is the currently open ifile,
134 curr_ifile = getoff_ifile(curr_ifile);
142 * Get the ifile after a given one in the list.
149 p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
150 if (p->h_next == &anchor)
152 return (ext_ifile(p->h_next));
156 * Get the ifile before a given one in the list.
163 p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
164 if (p->h_prev == &anchor)
166 return (ext_ifile(p->h_prev));
170 * Return a different ifile from the given one.
173 getoff_ifile(IFILE ifile)
177 if ((newifile = prev_ifile(ifile)) != NULL_IFILE)
179 if ((newifile = next_ifile(ifile)) != NULL_IFILE)
185 * Return the number of ifiles.
194 * Find an ifile structure, given a filename.
196 static struct ifile *
197 find_ifile(char *filename)
201 for (p = anchor.h_next; p != &anchor; p = p->h_next)
202 if (strcmp(filename, p->h_filename) == 0)
208 * Get the ifile associated with a filename.
209 * If the filename has not been seen before,
210 * insert the new ifile after "prev" in the list.
213 get_ifile(char *filename, IFILE prev)
217 if ((p = find_ifile(filename)) == NULL)
218 p = new_ifile(filename, int_ifile(prev));
219 return (ext_ifile(p));
223 * Get the filename associated with a ifile.
226 get_filename(IFILE ifile)
230 return (int_ifile(ifile)->h_filename);
234 * Get the index of the file associated with a ifile.
237 get_index(IFILE ifile)
239 return (int_ifile(ifile)->h_index);
243 * Save the file position to be associated with a given file.
246 store_pos(IFILE ifile, struct scrpos *scrpos)
248 int_ifile(ifile)->h_scrpos = *scrpos;
252 * Recall the file position associated with a file.
253 * If no position has been associated with the file, return NULL_POSITION.
256 get_pos(IFILE ifile, struct scrpos *scrpos)
258 *scrpos = int_ifile(ifile)->h_scrpos;
262 * Mark the ifile as "opened".
265 set_open(IFILE ifile)
267 int_ifile(ifile)->h_opened = 1;
271 * Return whether the ifile has been opened previously.
276 return (int_ifile(ifile)->h_opened);
280 hold_ifile(IFILE ifile, int incr)
282 int_ifile(ifile)->h_hold += incr;
286 held_ifile(IFILE ifile)
288 return (int_ifile(ifile)->h_hold);
292 get_filestate(IFILE ifile)
294 return (int_ifile(ifile)->h_filestate);
298 set_filestate(IFILE ifile, void *filestate)
300 int_ifile(ifile)->h_filestate = filestate;
309 for (p = anchor.h_next; p != &anchor; p = p->h_next)
311 printf("%x: %d. <%s> pos %d,%x\n",
312 p, p->h_index, p->h_filename,
313 p->h_scrpos.ln, p->h_scrpos.pos);
314 ch_dump(p->h_filestate);