2 * Copyright (C) 1984-2022 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 char *h_rfilename; /* Canonical name of the file */
31 void *h_filestate; /* File state (used in ch.c) */
32 int h_index; /* Index within command line list */
33 int h_hold; /* Hold count */
34 char h_opened; /* Has this ifile been opened? */
35 struct scrpos h_scrpos; /* Saved position within the file */
36 void *h_altpipe; /* Alt pipe */
37 char *h_altfilename; /* Alt filename */
41 * Convert an IFILE (external representation)
42 * to a struct file (internal representation), and vice versa.
44 #define int_ifile(h) ((struct ifile *)(h))
45 #define ext_ifile(h) ((IFILE)(h))
48 * Anchor for linked list.
50 static struct ifile anchor = { &anchor, &anchor, NULL, NULL, NULL, 0, 0, '\0',
51 { NULL_POSITION, 0 } };
52 static int ifiles = 0;
59 for (; p != &anchor; p = p->h_next)
64 * Link an ifile into the ifile list.
76 p->h_next = prev->h_next;
78 prev->h_next->h_prev = p;
81 * Calculate index for the new one,
82 * and adjust the indexes for subsequent ifiles in the list.
84 p->h_index = prev->h_index + 1;
85 incr_index(p->h_next, 1);
90 * Unlink an ifile from the ifile list.
96 p->h_next->h_prev = p->h_prev;
97 p->h_prev->h_next = p->h_next;
98 incr_index(p->h_next, -1);
103 * Allocate a new ifile structure and stick a filename in it.
104 * It should go after "prev" in the list
105 * (or at the beginning of the list if "prev" is NULL).
106 * Return a pointer to the new ifile structure.
108 static struct ifile *
109 new_ifile(filename, prev)
116 * Allocate and initialize structure.
118 p = (struct ifile *) ecalloc(1, sizeof(struct ifile));
119 p->h_filename = save(filename);
120 p->h_rfilename = lrealpath(filename);
121 p->h_scrpos.pos = NULL_POSITION;
124 p->h_filestate = NULL;
125 p->h_altfilename = NULL;
129 * {{ It's dodgy to call mark.c functions from here;
130 * there is potentially dangerous recursion.
131 * Probably need to revisit this design. }}
133 mark_check_ifile(ext_ifile(p));
138 * Delete an existing ifile structure.
149 * If the ifile we're deleting is the currently open ifile,
154 curr_ifile = getoff_ifile(curr_ifile);
157 free(p->h_rfilename);
163 * Get the ifile after a given one in the list.
171 p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
172 if (p->h_next == &anchor)
174 return (ext_ifile(p->h_next));
178 * Get the ifile before a given one in the list.
186 p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
187 if (p->h_prev == &anchor)
189 return (ext_ifile(p->h_prev));
193 * Return a different ifile from the given one.
201 if ((newifile = prev_ifile(ifile)) != NULL_IFILE)
203 if ((newifile = next_ifile(ifile)) != NULL_IFILE)
209 * Return the number of ifiles.
218 * Find an ifile structure, given a filename.
220 static struct ifile *
225 char *rfilename = lrealpath(filename);
227 for (p = anchor.h_next; p != &anchor; p = p->h_next)
229 if (strcmp(rfilename, p->h_rfilename) == 0)
232 * If given name is shorter than the name we were
233 * previously using for this file, adopt shorter name.
235 if (strlen(filename) < strlen(p->h_filename))
238 p->h_filename = save(filename);
250 * Get the ifile associated with a filename.
251 * If the filename has not been seen before,
252 * insert the new ifile after "prev" in the list.
255 get_ifile(filename, prev)
261 if ((p = find_ifile(filename)) == NULL)
262 p = new_ifile(filename, int_ifile(prev));
263 return (ext_ifile(p));
267 * Get the display filename associated with a ifile.
275 return (int_ifile(ifile)->h_filename);
279 * Get the canonical filename associated with a ifile.
282 get_real_filename(ifile)
287 return (int_ifile(ifile)->h_rfilename);
291 * Get the index of the file associated with a ifile.
297 return (int_ifile(ifile)->h_index);
301 * Save the file position to be associated with a given file.
304 store_pos(ifile, scrpos)
306 struct scrpos *scrpos;
308 int_ifile(ifile)->h_scrpos = *scrpos;
312 * Recall the file position associated with a file.
313 * If no position has been associated with the file, return NULL_POSITION.
316 get_pos(ifile, scrpos)
318 struct scrpos *scrpos;
320 *scrpos = int_ifile(ifile)->h_scrpos;
324 * Mark the ifile as "opened".
330 int_ifile(ifile)->h_opened = 1;
334 * Return whether the ifile has been opened previously.
340 return (int_ifile(ifile)->h_opened);
344 hold_ifile(ifile, incr)
348 int_ifile(ifile)->h_hold += incr;
355 return (int_ifile(ifile)->h_hold);
362 return (int_ifile(ifile)->h_filestate);
366 set_filestate(ifile, filestate)
370 int_ifile(ifile)->h_filestate = filestate;
374 set_altpipe(ifile, p)
378 int_ifile(ifile)->h_altpipe = p;
385 return (int_ifile(ifile)->h_altpipe);
389 set_altfilename(ifile, altfilename)
393 struct ifile *p = int_ifile(ifile);
394 if (p->h_altfilename != NULL && p->h_altfilename != altfilename)
395 free(p->h_altfilename);
396 p->h_altfilename = altfilename;
400 get_altfilename(ifile)
403 return (int_ifile(ifile)->h_altfilename);
412 for (p = anchor.h_next; p != &anchor; p = p->h_next)
414 printf("%x: %d. <%s> pos %d,%x\n",
415 p, p->h_index, p->h_filename,
416 p->h_scrpos.ln, p->h_scrpos.pos);
417 ch_dump(p->h_filestate);