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