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