Drop main() prototype. Syncs with NetBSD-8
[minix.git] / external / bsd / less / dist / ifile.c
blobe98cf561fc8d993bb3f4ebdb29cffbd52c1ef766
1 /* $NetBSD: ifile.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
3 /*
4 * Copyright (C) 1984-2012 Mark Nudelman
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, see the README file.
14 * An IFILE represents an input file.
16 * It is actually a pointer to an ifile structure,
17 * but is opaque outside this module.
18 * Ifile structures are kept in a linked list in the order they
19 * appear on the command line.
20 * Any new file which does not already appear in the list is
21 * inserted after the current file.
24 #include "less.h"
26 extern IFILE curr_ifile;
28 struct ifile {
29 struct ifile *h_next; /* Links for command line list */
30 struct ifile *h_prev;
31 char *h_filename; /* Name of the file */
32 void *h_filestate; /* File state (used in ch.c) */
33 int h_index; /* Index within command line list */
34 int h_hold; /* Hold count */
35 char h_opened; /* Has this ifile been opened? */
36 struct scrpos h_scrpos; /* Saved position within the file */
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;
53 static void incr_index __P((struct ifile *, int));
54 static void link_ifile __P((struct ifile *, struct ifile *));
55 static void unlink_ifile __P((struct ifile *));
56 static struct ifile *new_ifile __P((char *, struct ifile *));
57 static struct ifile *find_ifile __P((char *));
59 static void
60 incr_index(p, incr)
61 register struct ifile *p;
62 int incr;
64 for (; p != &anchor; p = p->h_next)
65 p->h_index += incr;
69 * Link an ifile into the ifile list.
71 static void
72 link_ifile(p, prev)
73 struct ifile *p;
74 struct ifile *prev;
77 * Link into list.
79 if (prev == NULL)
80 prev = &anchor;
81 p->h_next = prev->h_next;
82 p->h_prev = prev;
83 prev->h_next->h_prev = p;
84 prev->h_next = p;
86 * Calculate index for the new one,
87 * and adjust the indexes for subsequent ifiles in the list.
89 p->h_index = prev->h_index + 1;
90 incr_index(p->h_next, 1);
91 ifiles++;
95 * Unlink an ifile from the ifile list.
97 static void
98 unlink_ifile(p)
99 struct ifile *p;
101 p->h_next->h_prev = p->h_prev;
102 p->h_prev->h_next = p->h_next;
103 incr_index(p->h_next, -1);
104 ifiles--;
108 * Allocate a new ifile structure and stick a filename in it.
109 * It should go after "prev" in the list
110 * (or at the beginning of the list if "prev" is NULL).
111 * Return a pointer to the new ifile structure.
113 static struct ifile *
114 new_ifile(filename, prev)
115 char *filename;
116 struct ifile *prev;
118 register struct ifile *p;
121 * Allocate and initialize structure.
123 p = (struct ifile *) ecalloc(1, sizeof(struct ifile));
124 p->h_filename = save(filename);
125 p->h_scrpos.pos = NULL_POSITION;
126 p->h_opened = 0;
127 p->h_hold = 0;
128 p->h_filestate = NULL;
129 link_ifile(p, prev);
130 return (p);
134 * Delete an existing ifile structure.
136 public void
137 del_ifile(h)
138 IFILE h;
140 register struct ifile *p;
142 if (h == NULL_IFILE)
143 return;
145 * If the ifile we're deleting is the currently open ifile,
146 * move off it.
148 unmark(h);
149 if (h == curr_ifile)
150 curr_ifile = getoff_ifile(curr_ifile);
151 p = int_ifile(h);
152 unlink_ifile(p);
153 free(p->h_filename);
154 free(p);
158 * Get the ifile after a given one in the list.
160 public IFILE
161 next_ifile(h)
162 IFILE h;
164 register struct ifile *p;
166 p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
167 if (p->h_next == &anchor)
168 return (NULL_IFILE);
169 return (ext_ifile(p->h_next));
173 * Get the ifile before a given one in the list.
175 public IFILE
176 prev_ifile(h)
177 IFILE h;
179 register struct ifile *p;
181 p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
182 if (p->h_prev == &anchor)
183 return (NULL_IFILE);
184 return (ext_ifile(p->h_prev));
188 * Return a different ifile from the given one.
190 public IFILE
191 getoff_ifile(ifile)
192 IFILE ifile;
194 IFILE newifile;
196 if ((newifile = prev_ifile(ifile)) != NULL_IFILE)
197 return (newifile);
198 if ((newifile = next_ifile(ifile)) != NULL_IFILE)
199 return (newifile);
200 return (NULL_IFILE);
204 * Return the number of ifiles.
206 public int
207 nifile()
209 return (ifiles);
213 * Find an ifile structure, given a filename.
215 static struct ifile *
216 find_ifile(filename)
217 char *filename;
219 register struct ifile *p;
221 for (p = anchor.h_next; p != &anchor; p = p->h_next)
222 if (strcmp(filename, p->h_filename) == 0)
223 return (p);
224 return (NULL);
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
233 get_ifile(filename, prev)
234 char *filename;
235 IFILE prev;
237 register struct ifile *p;
239 if ((p = find_ifile(filename)) == NULL)
240 p = new_ifile(filename, int_ifile(prev));
241 return (ext_ifile(p));
245 * Get the filename associated with a ifile.
247 public char *
248 get_filename(ifile)
249 IFILE ifile;
251 if (ifile == NULL)
252 return (NULL);
253 return (int_ifile(ifile)->h_filename);
257 * Get the index of the file associated with a ifile.
259 public int
260 get_index(ifile)
261 IFILE ifile;
263 return (int_ifile(ifile)->h_index);
267 * Save the file position to be associated with a given file.
269 public void
270 store_pos(ifile, scrpos)
271 IFILE ifile;
272 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
282 get_pos(ifile, scrpos)
283 IFILE ifile;
284 struct scrpos *scrpos;
286 *scrpos = int_ifile(ifile)->h_scrpos;
290 * Mark the ifile as "opened".
292 public void
293 set_open(ifile)
294 IFILE ifile;
296 int_ifile(ifile)->h_opened = 1;
300 * Return whether the ifile has been opened previously.
302 public int
303 opened(ifile)
304 IFILE ifile;
306 return (int_ifile(ifile)->h_opened);
309 public void
310 hold_ifile(ifile, incr)
311 IFILE ifile;
312 int incr;
314 int_ifile(ifile)->h_hold += incr;
317 public int
318 held_ifile(ifile)
319 IFILE ifile;
321 return (int_ifile(ifile)->h_hold);
324 public void *
325 get_filestate(ifile)
326 IFILE ifile;
328 return (int_ifile(ifile)->h_filestate);
331 public void
332 set_filestate(ifile, filestate)
333 IFILE ifile;
334 void *filestate;
336 int_ifile(ifile)->h_filestate = filestate;
339 #if 0
340 public void
341 if_dump()
343 register struct ifile *p;
345 for (p = anchor.h_next; p != &anchor; p = p->h_next)
347 printf("%x: %d. <%s> pos %d,%x\n",
348 p, p->h_index, p->h_filename,
349 p->h_scrpos.ln, p->h_scrpos.pos);
350 ch_dump(p->h_filestate);
353 #endif