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