priv: Define and use PRIV_VARSYM_SYS
[dragonfly.git] / contrib / less / ifile.c
blob00b935027fd3135dce66405b372f183f53691170
1 /*
2 * Copyright (C) 1984-2008 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 about less, or for information on how to
8 * contact the author, see the README file.
9 */
13 * An IFILE represents an input file.
15 * It is actually a pointer to an ifile structure,
16 * but is opaque outside this module.
17 * Ifile structures are kept in a linked list in the order they
18 * appear on the command line.
19 * Any new file which does not already appear in the list is
20 * inserted after the current file.
23 #include "less.h"
25 extern IFILE curr_ifile;
27 struct ifile {
28 struct ifile *h_next; /* Links for command line list */
29 struct ifile *h_prev;
30 char *h_filename; /* 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 */
39 * Convert an IFILE (external representation)
40 * to a struct file (internal representation), and vice versa.
42 #define int_ifile(h) ((struct ifile *)(h))
43 #define ext_ifile(h) ((IFILE)(h))
46 * Anchor for linked list.
48 static struct ifile anchor = { &anchor, &anchor, NULL, NULL, 0, 0, '\0',
49 { NULL_POSITION, 0 } };
50 static int ifiles = 0;
52 static void
53 incr_index(p, incr)
54 register struct ifile *p;
55 int incr;
57 for (; p != &anchor; p = p->h_next)
58 p->h_index += incr;
62 * Link an ifile into the ifile list.
64 static void
65 link_ifile(p, prev)
66 struct ifile *p;
67 struct ifile *prev;
70 * Link into list.
72 if (prev == NULL)
73 prev = &anchor;
74 p->h_next = prev->h_next;
75 p->h_prev = prev;
76 prev->h_next->h_prev = p;
77 prev->h_next = p;
79 * Calculate index for the new one,
80 * and adjust the indexes for subsequent ifiles in the list.
82 p->h_index = prev->h_index + 1;
83 incr_index(p->h_next, 1);
84 ifiles++;
88 * Unlink an ifile from the ifile list.
90 static void
91 unlink_ifile(p)
92 struct ifile *p;
94 p->h_next->h_prev = p->h_prev;
95 p->h_prev->h_next = p->h_next;
96 incr_index(p->h_next, -1);
97 ifiles--;
101 * Allocate a new ifile structure and stick a filename in it.
102 * It should go after "prev" in the list
103 * (or at the beginning of the list if "prev" is NULL).
104 * Return a pointer to the new ifile structure.
106 static struct ifile *
107 new_ifile(filename, prev)
108 char *filename;
109 struct ifile *prev;
111 register struct ifile *p;
114 * Allocate and initialize structure.
116 p = (struct ifile *) ecalloc(1, sizeof(struct ifile));
117 p->h_filename = save(filename);
118 p->h_scrpos.pos = NULL_POSITION;
119 p->h_opened = 0;
120 p->h_hold = 0;
121 p->h_filestate = NULL;
122 link_ifile(p, prev);
123 return (p);
127 * Delete an existing ifile structure.
129 public void
130 del_ifile(h)
131 IFILE h;
133 register struct ifile *p;
135 if (h == NULL_IFILE)
136 return;
138 * If the ifile we're deleting is the currently open ifile,
139 * move off it.
141 unmark(h);
142 if (h == curr_ifile)
143 curr_ifile = getoff_ifile(curr_ifile);
144 p = int_ifile(h);
145 unlink_ifile(p);
146 free(p->h_filename);
147 free(p);
151 * Get the ifile after a given one in the list.
153 public IFILE
154 next_ifile(h)
155 IFILE h;
157 register struct ifile *p;
159 p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
160 if (p->h_next == &anchor)
161 return (NULL_IFILE);
162 return (ext_ifile(p->h_next));
166 * Get the ifile before a given one in the list.
168 public IFILE
169 prev_ifile(h)
170 IFILE h;
172 register struct ifile *p;
174 p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
175 if (p->h_prev == &anchor)
176 return (NULL_IFILE);
177 return (ext_ifile(p->h_prev));
181 * Return a different ifile from the given one.
183 public IFILE
184 getoff_ifile(ifile)
185 IFILE ifile;
187 IFILE newifile;
189 if ((newifile = prev_ifile(ifile)) != NULL_IFILE)
190 return (newifile);
191 if ((newifile = next_ifile(ifile)) != NULL_IFILE)
192 return (newifile);
193 return (NULL_IFILE);
197 * Return the number of ifiles.
199 public int
200 nifile()
202 return (ifiles);
206 * Find an ifile structure, given a filename.
208 static struct ifile *
209 find_ifile(filename)
210 char *filename;
212 register struct ifile *p;
214 for (p = anchor.h_next; p != &anchor; p = p->h_next)
215 if (strcmp(filename, p->h_filename) == 0)
216 return (p);
217 return (NULL);
221 * Get the ifile associated with a filename.
222 * If the filename has not been seen before,
223 * insert the new ifile after "prev" in the list.
225 public IFILE
226 get_ifile(filename, prev)
227 char *filename;
228 IFILE prev;
230 register struct ifile *p;
232 if ((p = find_ifile(filename)) == NULL)
233 p = new_ifile(filename, int_ifile(prev));
234 return (ext_ifile(p));
238 * Get the filename associated with a ifile.
240 public char *
241 get_filename(ifile)
242 IFILE ifile;
244 if (ifile == NULL)
245 return (NULL);
246 return (int_ifile(ifile)->h_filename);
250 * Get the index of the file associated with a ifile.
252 public int
253 get_index(ifile)
254 IFILE ifile;
256 return (int_ifile(ifile)->h_index);
260 * Save the file position to be associated with a given file.
262 public void
263 store_pos(ifile, scrpos)
264 IFILE ifile;
265 struct scrpos *scrpos;
267 int_ifile(ifile)->h_scrpos = *scrpos;
271 * Recall the file position associated with a file.
272 * If no position has been associated with the file, return NULL_POSITION.
274 public void
275 get_pos(ifile, scrpos)
276 IFILE ifile;
277 struct scrpos *scrpos;
279 *scrpos = int_ifile(ifile)->h_scrpos;
283 * Mark the ifile as "opened".
285 public void
286 set_open(ifile)
287 IFILE ifile;
289 int_ifile(ifile)->h_opened = 1;
293 * Return whether the ifile has been opened previously.
295 public int
296 opened(ifile)
297 IFILE ifile;
299 return (int_ifile(ifile)->h_opened);
302 public void
303 hold_ifile(ifile, incr)
304 IFILE ifile;
305 int incr;
307 int_ifile(ifile)->h_hold += incr;
310 public int
311 held_ifile(ifile)
312 IFILE ifile;
314 return (int_ifile(ifile)->h_hold);
317 public void *
318 get_filestate(ifile)
319 IFILE ifile;
321 return (int_ifile(ifile)->h_filestate);
324 public void
325 set_filestate(ifile, filestate)
326 IFILE ifile;
327 void *filestate;
329 int_ifile(ifile)->h_filestate = filestate;
332 #if 0
333 public void
334 if_dump()
336 register struct ifile *p;
338 for (p = anchor.h_next; p != &anchor; p = p->h_next)
340 printf("%x: %d. <%s> pos %d,%x\n",
341 p, p->h_index, p->h_filename,
342 p->h_scrpos.ln, p->h_scrpos.pos);
343 ch_dump(p->h_filestate);
346 #endif