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