2 * Copyright (C) 1984-2020 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;
125 * {{ It's dodgy to call mark.c functions from here;
126 * there is potentially dangerous recursion.
127 * Probably need to revisit this design. }}
129 mark_check_ifile(ext_ifile(p));
134 * Delete an existing ifile structure.
145 * If the ifile we're deleting is the currently open ifile,
150 curr_ifile = getoff_ifile(curr_ifile);
158 * Get the ifile after a given one in the list.
166 p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
167 if (p->h_next == &anchor)
169 return (ext_ifile(p->h_next));
173 * Get the ifile before a given one in the list.
181 p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
182 if (p->h_prev == &anchor)
184 return (ext_ifile(p->h_prev));
188 * Return a different ifile from the given one.
196 if ((newifile = prev_ifile(ifile)) != NULL_IFILE)
198 if ((newifile = next_ifile(ifile)) != NULL_IFILE)
204 * Return the number of ifiles.
213 * Find an ifile structure, given a filename.
215 static struct ifile *
220 char *rfilename = lrealpath(filename);
222 for (p = anchor.h_next; p != &anchor; p = p->h_next)
224 if (strcmp(filename, p->h_filename) == 0 ||
225 strcmp(rfilename, p->h_filename) == 0)
228 * If given name is shorter than the name we were
229 * previously using for this file, adopt shorter name.
231 if (strlen(filename) < strlen(p->h_filename))
232 strcpy(p->h_filename, filename);
243 * Get the ifile associated with a filename.
244 * If the filename has not been seen before,
245 * insert the new ifile after "prev" in the list.
248 get_ifile(filename, prev)
254 if ((p = find_ifile(filename)) == NULL)
255 p = new_ifile(filename, int_ifile(prev));
256 return (ext_ifile(p));
260 * Get the filename associated with a ifile.
268 return (int_ifile(ifile)->h_filename);
272 * Get the index of the file associated with a ifile.
278 return (int_ifile(ifile)->h_index);
282 * Save the file position to be associated with a given file.
285 store_pos(ifile, scrpos)
287 struct scrpos *scrpos;
289 int_ifile(ifile)->h_scrpos = *scrpos;
293 * Recall the file position associated with a file.
294 * If no position has been associated with the file, return NULL_POSITION.
297 get_pos(ifile, scrpos)
299 struct scrpos *scrpos;
301 *scrpos = int_ifile(ifile)->h_scrpos;
305 * Mark the ifile as "opened".
311 int_ifile(ifile)->h_opened = 1;
315 * Return whether the ifile has been opened previously.
321 return (int_ifile(ifile)->h_opened);
325 hold_ifile(ifile, incr)
329 int_ifile(ifile)->h_hold += incr;
336 return (int_ifile(ifile)->h_hold);
343 return (int_ifile(ifile)->h_filestate);
347 set_filestate(ifile, filestate)
351 int_ifile(ifile)->h_filestate = filestate;
355 set_altpipe(ifile, p)
359 int_ifile(ifile)->h_altpipe = p;
366 return (int_ifile(ifile)->h_altpipe);
370 set_altfilename(ifile, altfilename)
374 struct ifile *p = int_ifile(ifile);
375 if (p->h_altfilename != NULL)
376 free(p->h_altfilename);
377 p->h_altfilename = altfilename;
381 get_altfilename(ifile)
384 return (int_ifile(ifile)->h_altfilename);
393 for (p = anchor.h_next; p != &anchor; p = p->h_next)
395 printf("%x: %d. <%s> pos %d,%x\n",
396 p, p->h_index, p->h_filename,
397 p->h_scrpos.ln, p->h_scrpos.pos);
398 ch_dump(p->h_filestate);