Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/less/ifile.c
39476 views
1
/*
2
* Copyright (C) 1984-2025 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
24
extern IFILE curr_ifile;
25
26
struct 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
*/
50
static struct ifile anchor = { &anchor, &anchor, NULL, NULL, NULL, 0, 0, '\0',
51
{ NULL_POSITION, 0 }, NULL, NULL };
52
static int ifiles = 0;
53
54
static 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
*/
63
static 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
*/
86
static 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
*/
100
static struct ifile * new_ifile(constant 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
*/
129
public 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
*/
152
public 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
*/
165
public 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
*/
178
public 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
*/
192
public int nifile(void)
193
{
194
return (ifiles);
195
}
196
197
/*
198
* Find an ifile structure, given a filename.
199
*/
200
static struct ifile * find_ifile(constant 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
*/
232
public IFILE get_ifile(constant 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
*/
244
public constant 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
*/
254
public constant 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
*/
264
public 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
*/
272
public 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
*/
281
public 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
*/
289
public 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
*/
297
public int opened(IFILE ifile)
298
{
299
return (int_ifile(ifile)->h_opened);
300
}
301
302
public void hold_ifile(IFILE ifile, int incr)
303
{
304
int_ifile(ifile)->h_hold += incr;
305
}
306
307
public int held_ifile(IFILE ifile)
308
{
309
return (int_ifile(ifile)->h_hold);
310
}
311
312
public void * get_filestate(IFILE ifile)
313
{
314
return (int_ifile(ifile)->h_filestate);
315
}
316
317
public void set_filestate(IFILE ifile, void *filestate)
318
{
319
int_ifile(ifile)->h_filestate = filestate;
320
}
321
322
public void set_altpipe(IFILE ifile, void *p)
323
{
324
int_ifile(ifile)->h_altpipe = p;
325
}
326
327
public void *get_altpipe(IFILE ifile)
328
{
329
return (int_ifile(ifile)->h_altpipe);
330
}
331
332
public 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
340
/*
341
* Not constant; caller can free altfilename.
342
* {{ This is poor design and should be changed. }}
343
*/
344
public char * get_altfilename(IFILE ifile)
345
{
346
return (int_ifile(ifile)->h_altfilename);
347
}
348
349
#if 0
350
public void if_dump(void)
351
{
352
struct ifile *p;
353
354
for (p = anchor.h_next; p != &anchor; p = p->h_next)
355
{
356
printf("%x: %d. <%s> pos %d,%x\n",
357
p, p->h_index, p->h_filename,
358
p->h_scrpos.ln, p->h_scrpos.pos);
359
ch_dump(p->h_filestate);
360
}
361
}
362
#endif
363
364