1/*
2 * Copyright (C) 1984-2023  Mark Nudelman
3 *
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.
6 *
7 * For more information, see the README file.
8 */
9
10
11/*
12 * An IFILE represents an input file.
13 *
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.
20 */
21
22#include "less.h"
23
24extern IFILE    curr_ifile;
25
26struct ifile {
27	struct ifile *h_next;           /* Links for command line list */
28	struct ifile *h_prev;
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 */
38};
39
40/*
41 * Convert an IFILE (external representation)
42 * to a struct file (internal representation), and vice versa.
43 */
44#define int_ifile(h)    ((struct ifile *)(h))
45#define ext_ifile(h)    ((IFILE)(h))
46
47/*
48 * Anchor for linked list.
49 */
50static struct ifile anchor = { &anchor, &anchor, NULL, NULL, NULL, 0, 0, '\0',
51				{ NULL_POSITION, 0 } };
52static int ifiles = 0;
53
54static void incr_index(struct ifile *p, int incr)
55{
56	for (;  p != &anchor;  p = p->h_next)
57		p->h_index += incr;
58}
59
60/*
61 * Link an ifile into the ifile list.
62 */
63static void link_ifile(struct ifile *p, struct ifile *prev)
64{
65	/*
66	 * Link into list.
67	 */
68	if (prev == NULL)
69		prev = &anchor;
70	p->h_next = prev->h_next;
71	p->h_prev = prev;
72	prev->h_next->h_prev = p;
73	prev->h_next = p;
74	/*
75	 * Calculate index for the new one,
76	 * and adjust the indexes for subsequent ifiles in the list.
77	 */
78	p->h_index = prev->h_index + 1;
79	incr_index(p->h_next, 1);
80	ifiles++;
81}
82
83/*
84 * Unlink an ifile from the ifile list.
85 */
86static void unlink_ifile(struct ifile *p)
87{
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);
91	ifiles--;
92}
93
94/*
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.
99 */
100static struct ifile * new_ifile(char *filename, struct ifile *prev)
101{
102	struct ifile *p;
103
104	/*
105	 * Allocate and initialize structure.
106	 */
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;
111	p->h_opened = 0;
112	p->h_hold = 0;
113	p->h_filestate = NULL;
114	p->h_altfilename = NULL;
115	p->h_altpipe = NULL;
116	link_ifile(p, prev);
117	/*
118	 * {{ It's dodgy to call mark.c functions from here;
119	 *    there is potentially dangerous recursion.
120	 *    Probably need to revisit this design. }}
121	 */
122	mark_check_ifile(ext_ifile(p));
123	return (p);
124}
125
126/*
127 * Delete an existing ifile structure.
128 */
129public void del_ifile(IFILE h)
130{
131	struct ifile *p;
132
133	if (h == NULL_IFILE)
134		return;
135	/*
136	 * If the ifile we're deleting is the currently open ifile,
137	 * move off it.
138	 */
139	unmark(h);
140	if (h == curr_ifile)
141		curr_ifile = getoff_ifile(curr_ifile);
142	p = int_ifile(h);
143	unlink_ifile(p);
144	free(p->h_rfilename);
145	free(p->h_filename);
146	free(p);
147}
148
149/*
150 * Get the ifile after a given one in the list.
151 */
152public IFILE next_ifile(IFILE h)
153{
154	struct ifile *p;
155
156	p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
157	if (p->h_next == &anchor)
158		return (NULL_IFILE);
159	return (ext_ifile(p->h_next));
160}
161
162/*
163 * Get the ifile before a given one in the list.
164 */
165public IFILE prev_ifile(IFILE h)
166{
167	struct ifile *p;
168
169	p = (h == NULL_IFILE) ? &anchor : int_ifile(h);
170	if (p->h_prev == &anchor)
171		return (NULL_IFILE);
172	return (ext_ifile(p->h_prev));
173}
174
175/*
176 * Return a different ifile from the given one.
177 */
178public IFILE getoff_ifile(IFILE ifile)
179{
180	IFILE newifile;
181
182	if ((newifile = prev_ifile(ifile)) != NULL_IFILE)
183		return (newifile);
184	if ((newifile = next_ifile(ifile)) != NULL_IFILE)
185		return (newifile);
186	return (NULL_IFILE);
187}
188
189/*
190 * Return the number of ifiles.
191 */
192public int nifile(void)
193{
194	return (ifiles);
195}
196
197/*
198 * Find an ifile structure, given a filename.
199 */
200static struct ifile * find_ifile(char *filename)
201{
202	struct ifile *p;
203	char *rfilename = lrealpath(filename);
204
205	for (p = anchor.h_next;  p != &anchor;  p = p->h_next)
206	{
207		if (strcmp(rfilename, p->h_rfilename) == 0)
208		{
209			/*
210			 * If given name is shorter than the name we were
211			 * previously using for this file, adopt shorter name.
212			 */
213			if (strlen(filename) < strlen(p->h_filename))
214			{
215				free(p->h_filename);
216				p->h_filename = save(filename);
217			}
218			break;
219		}
220	}
221	free(rfilename);
222	if (p == &anchor)
223		p = NULL;
224	return (p);
225}
226
227/*
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.
231 */
232public IFILE get_ifile(char *filename, IFILE prev)
233{
234	struct ifile *p;
235
236	if ((p = find_ifile(filename)) == NULL)
237		p = new_ifile(filename, int_ifile(prev));
238	return (ext_ifile(p));
239}
240
241/*
242 * Get the display filename associated with a ifile.
243 */
244public char * get_filename(IFILE ifile)
245{
246	if (ifile == NULL)
247		return (NULL);
248	return (int_ifile(ifile)->h_filename);
249}
250
251/*
252 * Get the canonical filename associated with a ifile.
253 */
254public char * get_real_filename(IFILE ifile)
255{
256	if (ifile == NULL)
257		return (NULL);
258	return (int_ifile(ifile)->h_rfilename);
259}
260
261/*
262 * Get the index of the file associated with a ifile.
263 */
264public int get_index(IFILE ifile)
265{
266	return (int_ifile(ifile)->h_index);
267}
268
269/*
270 * Save the file position to be associated with a given file.
271 */
272public void store_pos(IFILE ifile, struct scrpos *scrpos)
273{
274	int_ifile(ifile)->h_scrpos = *scrpos;
275}
276
277/*
278 * Recall the file position associated with a file.
279 * If no position has been associated with the file, return NULL_POSITION.
280 */
281public void get_pos(IFILE ifile, struct scrpos *scrpos)
282{
283	*scrpos = int_ifile(ifile)->h_scrpos;
284}
285
286/*
287 * Mark the ifile as "opened".
288 */
289public void set_open(IFILE ifile)
290{
291	int_ifile(ifile)->h_opened = 1;
292}
293
294/*
295 * Return whether the ifile has been opened previously.
296 */
297public int opened(IFILE ifile)
298{
299	return (int_ifile(ifile)->h_opened);
300}
301
302public void hold_ifile(IFILE ifile, int incr)
303{
304	int_ifile(ifile)->h_hold += incr;
305}
306
307public int held_ifile(IFILE ifile)
308{
309	return (int_ifile(ifile)->h_hold);
310}
311
312public void * get_filestate(IFILE ifile)
313{
314	return (int_ifile(ifile)->h_filestate);
315}
316
317public void set_filestate(IFILE ifile, void *filestate)
318{
319	int_ifile(ifile)->h_filestate = filestate;
320}
321
322public void set_altpipe(IFILE ifile, void *p)
323{
324	int_ifile(ifile)->h_altpipe = p;
325}
326
327public void *get_altpipe(IFILE ifile)
328{
329	return (int_ifile(ifile)->h_altpipe);
330}
331
332public void set_altfilename(IFILE ifile, char *altfilename)
333{
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;
338}
339
340public char * get_altfilename(IFILE ifile)
341{
342	return (int_ifile(ifile)->h_altfilename);
343}
344
345#if 0
346public void if_dump(void)
347{
348	struct ifile *p;
349
350	for (p = anchor.h_next;  p != &anchor;  p = p->h_next)
351	{
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);
356	}
357}
358#endif
359