2 * Copyright (C) 1984-2023 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;
54 static void incr_index(struct ifile *p, int incr)
56 for (; p != &anchor; p = p->h_next)
61 * Link an ifile into the ifile list.
63 static void link_ifile(struct ifile *p, struct ifile *prev)
70 p->h_next = prev->h_next;
72 prev->h_next->h_prev = p;
75 * Calculate index for the new one,
76 * and adjust the indexes for subsequent ifiles in the list.
78 p->h_index = prev->h_index + 1;
79 incr_index(p->h_next, 1);
84 * Unlink an ifile from the ifile list.
86 static void 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 * new_ifile(char *filename, struct ifile *prev)
105 * Allocate and initialize structure.
107 p = (struct ifile *) ecalloc(1, sizeof(struct ifile));
108 p->h_filename = save(filename);
109 p->h_rfilename = lrealpath(filename);
110 p->h_scrpos.pos = NULL_POSITION;
113 p->h_filestate = NULL;
114 p->h_altfilename = NULL;
118 * {{ It's dodgy to call mark.c functions from here;
119 * there is potentially dangerous recursion.
120 * Probably need to revisit this design. }}
122 mark_check_ifile(ext_ifile(p));
127 * Delete an existing ifile structure.
129 public void del_ifile(IFILE h)
136 * If the ifile we're deleting is the currently open ifile,
141 curr_ifile = getoff_ifile(curr_ifile);
144 free(p->h_rfilename);
150 * Get the ifile after a given one in the list.
152 public IFILE next_ifile(IFILE h)
156 p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
157 if (p->h_next == &anchor)
159 return (ext_ifile(p->h_next));
163 * Get the ifile before a given one in the list.
165 public IFILE prev_ifile(IFILE h)
169 p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
170 if (p->h_prev == &anchor)
172 return (ext_ifile(p->h_prev));
176 * Return a different ifile from the given one.
178 public IFILE getoff_ifile(IFILE ifile)
182 if ((newifile = prev_ifile(ifile)) != NULL_IFILE)
184 if ((newifile = next_ifile(ifile)) != NULL_IFILE)
190 * Return the number of ifiles.
192 public int nifile(void)
198 * Find an ifile structure, given a filename.
200 static struct ifile * find_ifile(char *filename)
203 char *rfilename = lrealpath(filename);
205 for (p = anchor.h_next; p != &anchor; p = p->h_next)
207 if (strcmp(rfilename, p->h_rfilename) == 0)
210 * If given name is shorter than the name we were
211 * previously using for this file, adopt shorter name.
213 if (strlen(filename) < strlen(p->h_filename))
216 p->h_filename = save(filename);
228 * Get the ifile associated with a filename.
229 * If the filename has not been seen before,
230 * insert the new ifile after "prev" in the list.
232 public IFILE get_ifile(char *filename, IFILE prev)
236 if ((p = find_ifile(filename)) == NULL)
237 p = new_ifile(filename, int_ifile(prev));
238 return (ext_ifile(p));
242 * Get the display filename associated with a ifile.
244 public char * get_filename(IFILE ifile)
248 return (int_ifile(ifile)->h_filename);
252 * Get the canonical filename associated with a ifile.
254 public char * get_real_filename(IFILE ifile)
258 return (int_ifile(ifile)->h_rfilename);
262 * Get the index of the file associated with a ifile.
264 public int get_index(IFILE ifile)
266 return (int_ifile(ifile)->h_index);
270 * Save the file position to be associated with a given file.
272 public void store_pos(IFILE ifile, struct scrpos *scrpos)
274 int_ifile(ifile)->h_scrpos = *scrpos;
278 * Recall the file position associated with a file.
279 * If no position has been associated with the file, return NULL_POSITION.
281 public void get_pos(IFILE ifile, struct scrpos *scrpos)
283 *scrpos = int_ifile(ifile)->h_scrpos;
287 * Mark the ifile as "opened".
289 public void set_open(IFILE ifile)
291 int_ifile(ifile)->h_opened = 1;
295 * Return whether the ifile has been opened previously.
297 public int opened(IFILE ifile)
299 return (int_ifile(ifile)->h_opened);
302 public void hold_ifile(IFILE ifile, int incr)
304 int_ifile(ifile)->h_hold += incr;
307 public int held_ifile(IFILE ifile)
309 return (int_ifile(ifile)->h_hold);
312 public void * get_filestate(IFILE ifile)
314 return (int_ifile(ifile)->h_filestate);
317 public void set_filestate(IFILE ifile, void *filestate)
319 int_ifile(ifile)->h_filestate = filestate;
322 public void set_altpipe(IFILE ifile, void *p)
324 int_ifile(ifile)->h_altpipe = p;
327 public void *get_altpipe(IFILE ifile)
329 return (int_ifile(ifile)->h_altpipe);
332 public void set_altfilename(IFILE ifile, char *altfilename)
334 struct ifile *p = int_ifile(ifile);
335 if (p->h_altfilename != NULL && p->h_altfilename != altfilename)
336 free(p->h_altfilename);
337 p->h_altfilename = altfilename;
340 public char * get_altfilename(IFILE ifile)
342 return (int_ifile(ifile)->h_altfilename);
346 public void if_dump(void)
350 for (p = anchor.h_next; p != &anchor; p = p->h_next)
352 printf("%x: %d. <%s> pos %d,%x\n",
353 p, p->h_index, p->h_filename,
354 p->h_scrpos.ln, p->h_scrpos.pos);
355 ch_dump(p->h_filestate);