]>
diplodocus.org Git - nmh/blob - docs/historical/mh-6.8.5/miscellany/less-177/ifile.c
2 * An IFILE represents an input file.
4 * It is actually a pointer to an ifile structure,
5 * but is opaque outside this module.
6 * Ifile structures are kept in a linked list in the order they
7 * appear on the command line.
8 * Any new file which does not already appear in the list is
9 * inserted after the current file.
15 struct ifile
*h_next
; /* Links for command line list */
17 int h_index
; /* Index within command line list */
18 char *h_filename
; /* Name of the file */
19 struct scrpos h_scrpos
; /* Saved position within the file */
23 * Convert an IFILE (external representation)
24 * to a struct file (internal representation), and vice versa.
26 #define int_ifile(h) ((struct ifile *)(h))
27 #define ext_ifile(h) ((IFILE)(h))
30 * Anchor for linked list.
32 static struct ifile anchor
= { &anchor
, &anchor
, 0 };
33 static int ifiles
= 0;
36 * Allocate a new ifile structure and stick a filename in it.
37 * It should go after "prev" in the list
38 * (or at the beginning of the list if "prev" is NULL).
39 * Return a pointer to the new ifile structure.
42 new_ifile(filename
, prev
)
46 register struct ifile
*p
;
47 register struct ifile
*np
;
50 * Allocate and initialize structure.
52 p
= (struct ifile
*) ecalloc(1, sizeof(struct ifile
));
53 p
->h_filename
= filename
;
54 p
->h_scrpos
.pos
= NULL_POSITION
;
61 p
->h_next
= prev
->h_next
;
63 prev
->h_next
->h_prev
= p
;
67 * Calculate index for the new one,
68 * and adjust the indexes for subsequent ifiles in the list.
70 p
->h_index
= prev
->h_index
+ 1;
71 for (np
= p
->h_next
; np
!= &anchor
; np
= np
->h_next
)
79 * Get the ifile after a given one in the list.
85 register struct ifile
*p
;
87 p
= (h
== NULL_IFILE
) ? &anchor
: int_ifile(h
);
88 if (p
->h_next
== &anchor
)
90 return (ext_ifile(p
->h_next
));
94 * Get the ifile before a given one in the list.
100 register struct ifile
*p
;
102 p
= (h
== NULL_IFILE
) ? &anchor
: int_ifile(h
);
103 if (p
->h_prev
== &anchor
)
105 return (ext_ifile(p
->h_prev
));
109 * Return the number of ifiles.
118 * Find an ifile structure, given a filename.
120 static struct ifile
*
124 register struct ifile
*p
;
126 for (p
= anchor
.h_next
; p
!= &anchor
; p
= p
->h_next
)
127 if (strcmp(filename
, p
->h_filename
) == 0)
133 * Get the ifile associated with a filename.
134 * If the filename has not been seen before,
135 * insert the new ifile after "prev" in the list.
138 get_ifile(filename
, prev
)
142 register struct ifile
*p
;
144 if ((p
= find_ifile(filename
)) == NULL
)
145 p
= new_ifile(save(filename
), int_ifile(prev
));
146 return (ext_ifile(p
));
150 * Get the filename associated with a ifile.
158 return (int_ifile(ifile
)->h_filename
);
162 * Get the index of the file associated with a ifile.
168 return (int_ifile(ifile
)->h_index
);
172 * Save the file position to be associated with a given file.
175 store_pos(ifile
, scrpos
)
177 struct scrpos
*scrpos
;
179 int_ifile(ifile
)->h_scrpos
= *scrpos
;
183 * Recall the file position associated with a file.
184 * If no position has been associated with the file, return NULL_POSITION.
187 get_pos(ifile
, scrpos
)
189 struct scrpos
*scrpos
;
191 *scrpos
= int_ifile(ifile
)->h_scrpos
;