Commit | Line | Data |
---|---|---|
bfe13c81 KB |
1 | /* |
2 | * Copyright (c) 1988 Mark Nudleman | |
3 | * Copyright (c) 1988 Regents of the University of California. | |
4 | * All rights reserved. | |
5 | * | |
6 | * This code is derived from software contributed to Berkeley by | |
7 | * Mark Nudleman. | |
8 | * | |
9 | * Redistribution and use in source and binary forms are permitted | |
10 | * provided that the above copyright notice and this paragraph are | |
11 | * duplicated in all such forms and that any documentation, | |
12 | * advertising materials, and other materials related to such | |
13 | * distribution and use acknowledge that the software was developed | |
14 | * by the University of California, Berkeley. The name of the | |
15 | * University may not be used to endorse or promote products derived | |
16 | * from this software without specific prior written permission. | |
17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
18 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
19 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
20 | */ | |
21 | ||
22 | #ifndef lint | |
083e784b | 23 | static char sccsid[] = "@(#)ch.c 5.4 (Berkeley) %G%"; |
bfe13c81 KB |
24 | #endif /* not lint */ |
25 | ||
26 | /* | |
27 | * Low level character input from the input file. | |
28 | * We use these special purpose routines which optimize moving | |
29 | * both forward and backward from the current read pointer. | |
30 | */ | |
31 | ||
32 | #include "less.h" | |
33 | ||
34 | public int file = -1; /* File descriptor of the input file */ | |
35 | ||
36 | /* | |
37 | * Pool of buffers holding the most recently used blocks of the input file. | |
38 | */ | |
39 | #define BUFSIZ 1024 | |
40 | struct buf { | |
41 | struct buf *next, *prev; | |
42 | long block; | |
43 | int datasize; | |
44 | char data[BUFSIZ]; | |
45 | }; | |
46 | public int nbufs; | |
47 | ||
48 | /* | |
49 | * The buffer pool is kept as a doubly-linked circular list, | |
50 | * in order from most- to least-recently used. | |
51 | * The circular list is anchored by buf_anchor. | |
52 | */ | |
53 | #define END_OF_CHAIN ((struct buf *)&buf_anchor) | |
54 | #define buf_head buf_anchor.next | |
55 | #define buf_tail buf_anchor.prev | |
56 | ||
57 | static struct { | |
58 | struct buf *next, *prev; | |
59 | } buf_anchor = { END_OF_CHAIN, END_OF_CHAIN }; | |
60 | ||
61 | extern int clean_data; | |
62 | extern int ispipe; | |
63 | extern int autobuf; | |
64 | extern int cbufs; | |
65 | extern int sigs; | |
bfe13c81 KB |
66 | |
67 | /* | |
68 | * Current position in file. | |
69 | * Stored as a block number and an offset into the block. | |
70 | */ | |
71 | static long ch_block; | |
72 | static int ch_offset; | |
73 | ||
74 | /* | |
75 | * Length of file, needed if input is a pipe. | |
76 | */ | |
77 | static POSITION ch_fsize; | |
78 | ||
79 | /* | |
80 | * Number of bytes read, if input is standard input (a pipe). | |
81 | */ | |
82 | static POSITION last_piped_pos; | |
83 | ||
84 | /* | |
85 | * Get the character pointed to by the read pointer. | |
86 | * ch_get() is a macro which is more efficient to call | |
87 | * than fch_get (the function), in the usual case | |
88 | * that the block desired is at the head of the chain. | |
89 | */ | |
90 | #define ch_get() ((buf_head->block == ch_block && \ | |
91 | ch_offset < buf_head->datasize) ? \ | |
92 | buf_head->data[ch_offset] : fch_get()) | |
93 | static int | |
94 | fch_get() | |
95 | { | |
96 | register struct buf *bp; | |
97 | register int n; | |
98 | register char *p; | |
99 | POSITION pos; | |
100 | ||
101 | /* | |
102 | * Look for a buffer holding the desired block. | |
103 | */ | |
104 | for (bp = buf_head; bp != END_OF_CHAIN; bp = bp->next) | |
105 | if (bp->block == ch_block) | |
106 | { | |
107 | if (ch_offset >= bp->datasize) | |
108 | /* | |
109 | * Need more data in this buffer. | |
110 | */ | |
111 | goto read_more; | |
112 | /* | |
113 | * On a pipe, we don't sort the buffers LRU | |
114 | * because this can cause gaps in the buffers. | |
115 | * For example, suppose we've got 12 1K buffers, | |
116 | * and a 15K input stream. If we read the first 12K | |
117 | * sequentially, then jump to line 1, then jump to | |
118 | * the end, the buffers have blocks 0,4,5,6,..,14. | |
119 | * If we then jump to line 1 again and try to | |
120 | * read sequentially, we're out of luck when we | |
121 | * get to block 1 (we'd get the "pipe error" below). | |
122 | * To avoid this, we only sort buffers on a pipe | |
123 | * when we actually READ the data, not when we | |
124 | * find it already buffered. | |
125 | */ | |
126 | if (ispipe) | |
127 | return (bp->data[ch_offset]); | |
128 | goto found; | |
129 | } | |
130 | /* | |
131 | * Block is not in a buffer. | |
132 | * Take the least recently used buffer | |
133 | * and read the desired block into it. | |
134 | * If the LRU buffer has data in it, | |
135 | * and autobuf is true, and input is a pipe, | |
136 | * then try to allocate a new buffer first. | |
137 | */ | |
138 | if (autobuf && ispipe && buf_tail->block != (long)(-1)) | |
139 | (void) ch_addbuf(1); | |
140 | bp = buf_tail; | |
141 | bp->block = ch_block; | |
142 | bp->datasize = 0; | |
143 | ||
144 | read_more: | |
145 | pos = (ch_block * BUFSIZ) + bp->datasize; | |
146 | if (ispipe) | |
147 | { | |
148 | /* | |
149 | * The data requested should be immediately after | |
150 | * the last data read from the pipe. | |
151 | */ | |
152 | if (pos != last_piped_pos) | |
153 | { | |
154 | error("pipe error"); | |
155 | quit(); | |
156 | } | |
157 | } else | |
083e784b | 158 | (void)lseek(file, pos, L_SET); |
bfe13c81 KB |
159 | |
160 | /* | |
161 | * Read the block. | |
162 | * If we read less than a full block, we just return the | |
163 | * partial block and pick up the rest next time. | |
164 | */ | |
165 | n = iread(file, &bp->data[bp->datasize], BUFSIZ - bp->datasize); | |
166 | if (n == READ_INTR) | |
167 | return (EOI); | |
168 | if (n < 0) | |
169 | { | |
170 | error("read error"); | |
171 | quit(); | |
172 | } | |
173 | if (ispipe) | |
174 | last_piped_pos += n; | |
175 | ||
bfe13c81 KB |
176 | bp->datasize += n; |
177 | ||
178 | /* | |
179 | * Set an EOI marker in the buffered data itself. | |
180 | * Then ensure the data is "clean": there are no | |
181 | * extra EOI chars in the data and that the "meta" | |
182 | * bit (the 0200 bit) is reset in each char. | |
183 | */ | |
184 | if (n == 0) | |
185 | { | |
186 | ch_fsize = pos; | |
187 | bp->data[bp->datasize++] = EOI; | |
188 | } | |
189 | ||
190 | if (!clean_data) | |
191 | { | |
192 | p = &bp->data[bp->datasize]; | |
193 | while (--n >= 0) | |
194 | { | |
195 | *--p &= 0177; | |
196 | if (*p == EOI) | |
197 | *p = '@'; | |
198 | } | |
199 | } | |
200 | ||
201 | found: | |
202 | if (buf_head != bp) | |
203 | { | |
204 | /* | |
205 | * Move the buffer to the head of the buffer chain. | |
206 | * This orders the buffer chain, most- to least-recently used. | |
207 | */ | |
208 | bp->next->prev = bp->prev; | |
209 | bp->prev->next = bp->next; | |
210 | ||
211 | bp->next = buf_head; | |
212 | bp->prev = END_OF_CHAIN; | |
213 | buf_head->prev = bp; | |
214 | buf_head = bp; | |
215 | } | |
216 | ||
217 | if (ch_offset >= bp->datasize) | |
218 | /* | |
219 | * After all that, we still don't have enough data. | |
220 | * Go back and try again. | |
221 | */ | |
222 | goto read_more; | |
223 | ||
224 | return (bp->data[ch_offset]); | |
225 | } | |
226 | ||
bfe13c81 KB |
227 | /* |
228 | * Determine if a specific block is currently in one of the buffers. | |
229 | */ | |
230 | static int | |
231 | buffered(block) | |
232 | long block; | |
233 | { | |
234 | register struct buf *bp; | |
235 | ||
236 | for (bp = buf_head; bp != END_OF_CHAIN; bp = bp->next) | |
237 | if (bp->block == block) | |
238 | return (1); | |
239 | return (0); | |
240 | } | |
241 | ||
242 | /* | |
243 | * Seek to a specified position in the file. | |
244 | * Return 0 if successful, non-zero if can't seek there. | |
245 | */ | |
246 | public int | |
247 | ch_seek(pos) | |
248 | register POSITION pos; | |
249 | { | |
250 | long new_block; | |
251 | ||
252 | new_block = pos / BUFSIZ; | |
253 | if (!ispipe || pos == last_piped_pos || buffered(new_block)) | |
254 | { | |
255 | /* | |
256 | * Set read pointer. | |
257 | */ | |
258 | ch_block = new_block; | |
259 | ch_offset = pos % BUFSIZ; | |
260 | return (0); | |
261 | } | |
262 | return (1); | |
263 | } | |
264 | ||
265 | /* | |
266 | * Seek to the end of the file. | |
267 | */ | |
268 | public int | |
269 | ch_end_seek() | |
270 | { | |
271 | if (!ispipe) | |
272 | return (ch_seek(ch_length())); | |
273 | ||
274 | /* | |
275 | * Do it the slow way: read till end of data. | |
276 | */ | |
277 | while (ch_forw_get() != EOI) | |
278 | if (sigs) | |
279 | return (1); | |
280 | return (0); | |
281 | } | |
282 | ||
283 | /* | |
284 | * Seek to the beginning of the file, or as close to it as we can get. | |
285 | * We may not be able to seek there if input is a pipe and the | |
286 | * beginning of the pipe is no longer buffered. | |
287 | */ | |
288 | public int | |
289 | ch_beg_seek() | |
290 | { | |
291 | register struct buf *bp, *firstbp; | |
292 | ||
293 | /* | |
294 | * Try a plain ch_seek first. | |
295 | */ | |
296 | if (ch_seek((POSITION)0) == 0) | |
297 | return (0); | |
298 | ||
299 | /* | |
300 | * Can't get to position 0. | |
301 | * Look thru the buffers for the one closest to position 0. | |
302 | */ | |
303 | firstbp = bp = buf_head; | |
304 | if (bp == END_OF_CHAIN) | |
305 | return (1); | |
306 | while ((bp = bp->next) != END_OF_CHAIN) | |
307 | if (bp->block < firstbp->block) | |
308 | firstbp = bp; | |
309 | ch_block = firstbp->block; | |
310 | ch_offset = 0; | |
311 | return (0); | |
312 | } | |
313 | ||
314 | /* | |
315 | * Return the length of the file, if known. | |
316 | */ | |
317 | public POSITION | |
318 | ch_length() | |
319 | { | |
320 | if (ispipe) | |
321 | return (ch_fsize); | |
51eb338d | 322 | return ((POSITION)(lseek(file, (off_t)0, L_XTND))); |
bfe13c81 KB |
323 | } |
324 | ||
325 | /* | |
326 | * Return the current position in the file. | |
327 | */ | |
328 | public POSITION | |
329 | ch_tell() | |
330 | { | |
331 | return (ch_block * BUFSIZ + ch_offset); | |
332 | } | |
333 | ||
334 | /* | |
335 | * Get the current char and post-increment the read pointer. | |
336 | */ | |
337 | public int | |
338 | ch_forw_get() | |
339 | { | |
340 | register int c; | |
341 | ||
342 | c = ch_get(); | |
343 | if (c != EOI && ++ch_offset >= BUFSIZ) | |
344 | { | |
345 | ch_offset = 0; | |
346 | ch_block ++; | |
347 | } | |
348 | return (c); | |
349 | } | |
350 | ||
351 | /* | |
352 | * Pre-decrement the read pointer and get the new current char. | |
353 | */ | |
354 | public int | |
355 | ch_back_get() | |
356 | { | |
357 | if (--ch_offset < 0) | |
358 | { | |
359 | if (ch_block <= 0 || (ispipe && !buffered(ch_block-1))) | |
360 | { | |
361 | ch_offset = 0; | |
362 | return (EOI); | |
363 | } | |
364 | ch_offset = BUFSIZ - 1; | |
365 | ch_block--; | |
366 | } | |
367 | return (ch_get()); | |
368 | } | |
369 | ||
370 | /* | |
371 | * Allocate buffers. | |
372 | * Caller wants us to have a total of at least want_nbufs buffers. | |
373 | * keep==1 means keep the data in the current buffers; | |
374 | * otherwise discard the old data. | |
375 | */ | |
376 | public void | |
377 | ch_init(want_nbufs, keep) | |
378 | int want_nbufs; | |
379 | int keep; | |
380 | { | |
381 | register struct buf *bp; | |
382 | char message[80]; | |
383 | ||
384 | cbufs = nbufs; | |
385 | if (nbufs < want_nbufs && ch_addbuf(want_nbufs - nbufs)) | |
386 | { | |
387 | /* | |
388 | * Cannot allocate enough buffers. | |
389 | * If we don't have ANY, then quit. | |
390 | * Otherwise, just report the error and return. | |
391 | */ | |
083e784b | 392 | (void)sprintf(message, "cannot allocate %d buffers", |
bfe13c81 KB |
393 | want_nbufs - nbufs); |
394 | error(message); | |
395 | if (nbufs == 0) | |
396 | quit(); | |
397 | return; | |
398 | } | |
399 | ||
400 | if (keep) | |
401 | return; | |
402 | ||
403 | /* | |
404 | * We don't want to keep the old data, | |
405 | * so initialize all the buffers now. | |
406 | */ | |
407 | for (bp = buf_head; bp != END_OF_CHAIN; bp = bp->next) | |
408 | bp->block = (long)(-1); | |
409 | last_piped_pos = (POSITION)0; | |
410 | ch_fsize = NULL_POSITION; | |
411 | (void) ch_seek((POSITION)0); | |
412 | } | |
413 | ||
414 | /* | |
415 | * Allocate some new buffers. | |
416 | * The buffers are added to the tail of the buffer chain. | |
417 | */ | |
418 | static int | |
419 | ch_addbuf(nnew) | |
420 | int nnew; | |
421 | { | |
422 | register struct buf *bp; | |
423 | register struct buf *newbufs; | |
424 | ||
425 | /* | |
426 | * We don't have enough buffers. | |
427 | * Allocate some new ones. | |
428 | */ | |
083e784b | 429 | newbufs = (struct buf *) calloc((u_int)nnew, sizeof(struct buf)); |
bfe13c81 KB |
430 | if (newbufs == NULL) |
431 | return (1); | |
432 | ||
433 | /* | |
434 | * Initialize the new buffers and link them together. | |
435 | * Link them all onto the tail of the buffer list. | |
436 | */ | |
437 | nbufs += nnew; | |
438 | cbufs = nbufs; | |
439 | for (bp = &newbufs[0]; bp < &newbufs[nnew]; bp++) | |
440 | { | |
441 | bp->next = bp + 1; | |
442 | bp->prev = bp - 1; | |
443 | bp->block = (long)(-1); | |
444 | } | |
445 | newbufs[nnew-1].next = END_OF_CHAIN; | |
446 | newbufs[0].prev = buf_tail; | |
447 | buf_tail->next = &newbufs[0]; | |
448 | buf_tail = &newbufs[nnew-1]; | |
449 | return (0); | |
450 | } |