1 /* $NetBSD: ch.c,v 1.5 2003/08/07 09:27:58 agc Exp $ */
4 * Copyright (c) 1988 Mark Nudelman
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
36 static char sccsid
[] = "@(#)ch.c 8.1 (Berkeley) 6/6/93";
38 __RCSID("$NetBSD: ch.c,v 1.5 2003/08/07 09:27:58 agc Exp $");
43 * Low level character input from the input file.
44 * We use these special purpose routines which optimize moving
45 * both forward and backward from the current read pointer.
48 #include <sys/types.h>
58 int file
= -1; /* File descriptor of the input file */
61 * Pool of buffers holding the most recently used blocks of the input file.
64 struct buf
*next
, *prev
;
72 * The buffer pool is kept as a doubly-linked circular list, in order from
73 * most- to least-recently used. The circular list is anchored by buf_anchor.
75 #define END_OF_CHAIN ((struct buf *)&buf_anchor)
76 #define buf_head buf_anchor.next
77 #define buf_tail buf_anchor.prev
80 struct buf
*next
, *prev
;
81 } buf_anchor
= { END_OF_CHAIN
, END_OF_CHAIN
};
84 * Current position in file.
85 * Stored as a block number and an offset into the block.
90 /* Length of file, needed if input is a pipe. */
91 static off_t ch_fsize
;
93 /* Number of bytes read, if input is standard input (a pipe). */
94 static off_t last_piped_pos
;
97 * Get the character pointed to by the read pointer. ch_get() is a macro
98 * which is more efficient to call than fch_get (the function), in the usual
99 * case that the block desired is at the head of the chain.
102 ((buf_head->block == ch_block && \
103 ch_offset < buf_head->datasize) ? \
104 buf_head->data[ch_offset] : fch_get())
106 static int fch_get
__P((void));
107 static int buffered
__P((long));
117 /* look for a buffer holding the desired block. */
118 for (bp
= buf_head
; bp
!= END_OF_CHAIN
; bp
= bp
->next
)
119 if (bp
->block
== ch_block
) {
120 if (ch_offset
>= bp
->datasize
)
122 * Need more data in this buffer.
126 * On a pipe, we don't sort the buffers LRU
127 * because this can cause gaps in the buffers.
128 * For example, suppose we've got 12 1K buffers,
129 * and a 15K input stream. If we read the first 12K
130 * sequentially, then jump to line 1, then jump to
131 * the end, the buffers have blocks 0,4,5,6,..,14.
132 * If we then jump to line 1 again and try to
133 * read sequentially, we're out of luck when we
134 * get to block 1 (we'd get the "pipe error" below).
135 * To avoid this, we only sort buffers on a pipe
136 * when we actually READ the data, not when we
137 * find it already buffered.
140 return(bp
->data
[ch_offset
]);
144 * Block is not in a buffer. Take the least recently used buffer
145 * and read the desired block into it. If the LRU buffer has data
146 * in it, and input is a pipe, then try to allocate a new buffer first.
148 if (ispipe
&& buf_tail
->block
!= (long)(-1))
151 bp
->block
= ch_block
;
155 pos
= (ch_block
* BUFSIZ
) + bp
->datasize
;
158 * The data requested should be immediately after
159 * the last data read from the pipe.
161 if (pos
!= last_piped_pos
) {
166 (void)lseek(file
, pos
, L_SET
);
170 * If we read less than a full block, we just return the
171 * partial block and pick up the rest next time.
173 n
= iread(file
, &bp
->data
[bp
->datasize
], BUFSIZ
- bp
->datasize
);
183 p
= &bp
->data
[bp
->datasize
];
187 * Set an EOI marker in the buffered data itself. Then ensure the
188 * data is "clean": there are no extra EOI chars in the data and
189 * that the "meta" bit (the 0200 bit) is reset in each char;
190 * also translate \r\n sequences to \n if -u flag not set.
194 bp
->data
[bp
->datasize
++] = EOI
;
198 for (p
= &bp
->data
[bp
->datasize
]; --n
>= 0;) {
205 for (t
= p
; --n
>= 0; ++p
) {
207 if (ch
== '\r' && n
&& (p
[1] & 0177) == '\n') {
212 *t
++ = (ch
== EOI
) ? 0200 : ch
;
215 bp
->datasize
-= p
- t
;
217 last_piped_pos
-= p
- t
;
222 if (buf_head
!= bp
) {
224 * Move the buffer to the head of the buffer chain.
225 * This orders the buffer chain, most- to least-recently used.
227 bp
->next
->prev
= bp
->prev
;
228 bp
->prev
->next
= bp
->next
;
231 bp
->prev
= END_OF_CHAIN
;
236 if (ch_offset
>= bp
->datasize
)
238 * After all that, we still don't have enough data.
239 * Go back and try again.
243 return(bp
->data
[ch_offset
]);
247 * Determine if a specific block is currently in one of the buffers.
255 for (bp
= buf_head
; bp
!= END_OF_CHAIN
; bp
= bp
->next
)
256 if (bp
->block
== block
)
262 * Seek to a specified position in the file.
263 * Return 0 if successful, non-zero if can't seek there.
271 new_block
= pos
/ BUFSIZ
;
272 if (!ispipe
|| pos
== last_piped_pos
|| buffered(new_block
)) {
276 ch_block
= new_block
;
277 ch_offset
= pos
% BUFSIZ
;
284 * Seek to the end of the file.
290 return(ch_seek(ch_length()));
293 * Do it the slow way: read till end of data.
295 while (ch_forw_get() != EOI
)
302 * Seek to the beginning of the file, or as close to it as we can get.
303 * We may not be able to seek there if input is a pipe and the
304 * beginning of the pipe is no longer buffered.
309 struct buf
*bp
, *firstbp
;
312 * Try a plain ch_seek first.
314 if (ch_seek((off_t
)0) == 0)
318 * Can't get to position 0.
319 * Look thru the buffers for the one closest to position 0.
321 firstbp
= bp
= buf_head
;
322 if (bp
== END_OF_CHAIN
)
324 while ((bp
= bp
->next
) != END_OF_CHAIN
)
325 if (bp
->block
< firstbp
->block
)
327 ch_block
= firstbp
->block
;
333 * Return the length of the file, if known.
340 return((off_t
)(lseek(file
, (off_t
)0, L_XTND
)));
344 * Return the current position in the file.
349 return(ch_block
* BUFSIZ
+ ch_offset
);
353 * Get the current char and post-increment the read pointer.
361 if (c
!= EOI
&& ++ch_offset
>= BUFSIZ
) {
369 * Pre-decrement the read pointer and get the new current char.
374 if (--ch_offset
< 0) {
375 if (ch_block
<= 0 || (ispipe
&& !buffered(ch_block
-1))) {
379 ch_offset
= BUFSIZ
- 1;
387 * Caller wants us to have a total of at least want_nbufs buffers.
388 * keep==1 means keep the data in the current buffers;
389 * otherwise discard the old data.
392 ch_init(want_nbufs
, keep
)
400 if (nbufs
< want_nbufs
&& ch_addbuf(want_nbufs
- nbufs
)) {
402 * Cannot allocate enough buffers.
403 * If we don't have ANY, then quit.
404 * Otherwise, just report the error and return.
406 (void)snprintf(message
, sizeof(message
),
407 "cannot allocate %d buffers", want_nbufs
- nbufs
);
418 * We don't want to keep the old data,
419 * so initialize all the buffers now.
421 for (bp
= buf_head
; bp
!= END_OF_CHAIN
; bp
= bp
->next
)
422 bp
->block
= (long)(-1);
423 last_piped_pos
= (off_t
)0;
424 ch_fsize
= NULL_POSITION
;
425 (void)ch_seek((off_t
)0);
429 * Allocate some new buffers.
430 * The buffers are added to the tail of the buffer chain.
440 * We don't have enough buffers.
441 * Allocate some new ones.
443 newbufs
= (struct buf
*)calloc((u_int
)nnew
, sizeof(struct buf
));
448 * Initialize the new buffers and link them together.
449 * Link them all onto the tail of the buffer list.
453 for (bp
= &newbufs
[0]; bp
< &newbufs
[nnew
]; bp
++) {
456 bp
->block
= (long)(-1);
458 newbufs
[nnew
-1].next
= END_OF_CHAIN
;
459 newbufs
[0].prev
= buf_tail
;
460 buf_tail
->next
= &newbufs
[0];
461 buf_tail
= &newbufs
[nnew
-1];