2 * Copyright (C) 1984-2017 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 */
35 void *h_altpipe; /* Alt pipe */
36 char *h_altfilename; /* Alt filename */
40 * Convert an IFILE (external representation)
41 * to a struct file (internal representation), and vice versa.
43 #define int_ifile(h) ((struct ifile *)(h))
44 #define ext_ifile(h) ((IFILE)(h))
47 * Anchor for linked list.
49 static struct ifile anchor = { &anchor, &anchor, NULL, NULL, 0, 0, '\0',
50 { NULL_POSITION, 0 } };
51 static int ifiles = 0;
58 for (; p != &anchor; p = p->h_next)
63 * Link an ifile into the ifile list.
75 p->h_next = prev->h_next;
77 prev->h_next->h_prev = p;
80 * Calculate index for the new one,
81 * and adjust the indexes for subsequent ifiles in the list.
83 p->h_index = prev->h_index + 1;
84 incr_index(p->h_next, 1);
89 * Unlink an ifile from the ifile list.
95 p->h_next->h_prev = p->h_prev;
96 p->h_prev->h_next = p->h_next;
97 incr_index(p->h_next, -1);
102 * Allocate a new ifile structure and stick a filename in it.
103 * It should go after "prev" in the list
104 * (or at the beginning of the list if "prev" is NULL).
105 * Return a pointer to the new ifile structure.
107 static struct ifile *
108 new_ifile(filename, prev)
115 * Allocate and initialize structure.
117 p = (struct ifile *) ecalloc(1, sizeof(struct ifile));
118 p->h_filename = save(filename);
119 p->h_scrpos.pos = NULL_POSITION;
122 p->h_filestate = NULL;
128 * Delete an existing ifile structure.
139 * If the ifile we're deleting is the currently open ifile,
144 curr_ifile = getoff_ifile(curr_ifile);
152 * Get the ifile after a given one in the list.
160 p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
161 if (p->h_next == &anchor)
163 return (ext_ifile(p->h_next));
167 * Get the ifile before a given one in the list.
175 p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
176 if (p->h_prev == &anchor)
178 return (ext_ifile(p->h_prev));
182 * Return a different ifile from the given one.
190 if ((newifile = prev_ifile(ifile)) != NULL_IFILE)
192 if ((newifile = next_ifile(ifile)) != NULL_IFILE)
198 * Return the number of ifiles.
207 * Find an ifile structure, given a filename.
209 static struct ifile *
215 for (p = anchor.h_next; p != &anchor; p = p->h_next)
216 if (strcmp(filename, p->h_filename) == 0)
222 * Get the ifile associated with a filename.
223 * If the filename has not been seen before,
224 * insert the new ifile after "prev" in the list.
227 get_ifile(filename, prev)
233 if ((p = find_ifile(filename)) == NULL)
234 p = new_ifile(filename, int_ifile(prev));
235 return (ext_ifile(p));
239 * Get the filename associated with a ifile.
247 return (int_ifile(ifile)->h_filename);
251 * Get the index of the file associated with a ifile.
257 return (int_ifile(ifile)->h_index);
261 * Save the file position to be associated with a given file.
264 store_pos(ifile, scrpos)
266 struct scrpos *scrpos;
268 int_ifile(ifile)->h_scrpos = *scrpos;
272 * Recall the file position associated with a file.
273 * If no position has been associated with the file, return NULL_POSITION.
276 get_pos(ifile, scrpos)
278 struct scrpos *scrpos;
280 *scrpos = int_ifile(ifile)->h_scrpos;
284 * Mark the ifile as "opened".
290 int_ifile(ifile)->h_opened = 1;
294 * Return whether the ifile has been opened previously.
300 return (int_ifile(ifile)->h_opened);
304 hold_ifile(ifile, incr)
308 int_ifile(ifile)->h_hold += incr;
315 return (int_ifile(ifile)->h_hold);
322 return (int_ifile(ifile)->h_filestate);
326 set_filestate(ifile, filestate)
330 int_ifile(ifile)->h_filestate = filestate;
334 set_altpipe(ifile, p)
338 int_ifile(ifile)->h_altpipe = p;
345 return (int_ifile(ifile)->h_altpipe);
349 set_altfilename(ifile, altfilename)
353 struct ifile *p = int_ifile(ifile);
354 if (p->h_altfilename != NULL)
355 free(p->h_altfilename);
356 p->h_altfilename = altfilename;
360 get_altfilename(ifile)
363 return (int_ifile(ifile)->h_altfilename);
372 for (p = anchor.h_next; p != &anchor; p = p->h_next)
374 printf("%x: %d. <%s> pos %d,%x\n",
375 p, p->h_index, p->h_filename,
376 p->h_scrpos.ln, p->h_scrpos.pos);
377 ch_dump(p->h_filestate);