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