2 * Low level character input from the input file.
3 * We use these special purpose routines which optimize moving
4 * both forward and backward from the current read pointer.
9 public int file = -1; /* File descriptor of the input file */
10 public int ignore_eoi;
13 * Pool of buffers holding the most recently used blocks of the input file.
17 struct buf *next, *prev; /* Must be first to match struct filestate */
19 unsigned int datasize;
20 unsigned char data[BUFSIZ];
24 * The buffer pool is kept as a doubly-linked circular list,
25 * in order from most- to least-recently used.
26 * The circular list is anchored by the file state "thisfile".
28 * The file state is maintained in a filestate structure.
29 * There are two such structures, one used when input is a pipe
30 * and the other when input is an ordinary file.
31 * This is so that we can leave a pipe, look and other files,
32 * and return to the pipe without losing buffered data.
33 * Buffered data can be reconstructed for a non-pipe file by
34 * simply re-reading the file, but a pipe cannot be re-read.
38 struct buf *next, *prev; /* Must be first to match struct buf */
46 #define END_OF_CHAIN ((struct buf *)thisfile)
47 #define buf_head thisfile->next
48 #define buf_tail thisfile->prev
49 #define ch_nbufs thisfile->nbufs
50 #define ch_block thisfile->block
51 #define ch_offset thisfile->offset
52 #define ch_fpos thisfile->fpos
53 #define ch_fsize thisfile->fsize
55 static struct filestate pipefile =
56 { (struct buf *)&pipefile, (struct buf *)&pipefile };
58 static struct filestate nonpipefile =
59 { (struct buf *)&nonpipefile, (struct buf *)&nonpipefile };
61 static struct filestate *thisfile;
68 extern char *namelogfile;
71 static int ch_addbuf();
75 * Get the character pointed to by the read pointer.
76 * ch_get() is a macro which is more efficient to call
77 * than fch_get (the function), in the usual case
78 * that the block desired is at the head of the chain.
80 #define ch_get() ((ch_block == buf_head->block && \
81 ch_offset < buf_head->datasize) ? \
82 buf_head->data[ch_offset] : fch_get())
86 register struct buf *bp;
95 * Look for a buffer holding the desired block.
97 for (bp = buf_head; bp != END_OF_CHAIN; bp = bp->next)
98 if (bp->block == ch_block)
100 if (ch_offset >= bp->datasize)
102 * Need more data in this buffer.
108 * Block is not in a buffer.
109 * Take the least recently used buffer
110 * and read the desired block into it.
111 * If the LRU buffer has data in it,
112 * and autobuf is true, and input is a pipe,
113 * then try to allocate a new buffer first.
115 if (autobuf && ispipe && buf_tail->block != (long)(-1))
118 * Allocation failed: turn off autobuf.
122 bp->block = ch_block;
126 pos = (ch_block * BUFSIZ) + bp->datasize;
127 if ((len = ch_length()) != NULL_POSITION && pos >= len)
136 * Not at the correct position: must seek.
137 * If input is a pipe, we're in trouble (can't seek on a pipe).
138 * Some data has been lost: just return "?".
142 if (lseek(file, (offset_t)pos, 0) == BAD_LSEEK)
144 error("seek error", NULL_PARG);
152 * If we read less than a full block, that's ok.
153 * We use partial block and pick up the rest next time.
155 n = iread(file, &bp->data[bp->datasize],
156 (unsigned int)(BUFSIZ - bp->datasize));
161 error("read error", NULL_PARG);
168 * If we have a log file, write the new data to it.
170 if (logfile >= 0 && n > 0)
171 write(logfile, (char *) &bp->data[bp->datasize], n);
177 * If we have read to end of file, set ch_fsize to indicate
178 * the position of the end of file.
186 * We are ignoring EOF.
187 * Wait a while, then try again.
190 ierror("Waiting for data", NULL_PARG);
202 * Move the buffer to the head of the buffer chain.
203 * This orders the buffer chain, most- to least-recently used.
205 bp->next->prev = bp->prev;
206 bp->prev->next = bp->next;
209 bp->prev = END_OF_CHAIN;
214 if (ch_offset >= bp->datasize)
216 * After all that, we still don't have enough data.
217 * Go back and try again.
221 return (bp->data[ch_offset]);
227 * If we haven't read all of standard input into it, do that now.
232 static int tried = 0;
236 if (!tried && ch_fsize == NULL_POSITION)
239 ierror("Finishing logfile", NULL_PARG);
240 while (ch_forw_get() != EOI)
250 * Start a log file AFTER less has already been running.
251 * Invoked from the - command; see toggle_option().
252 * Write all the existing buffered data to the log file.
257 register struct buf *bp;
261 last_block = (ch_fpos + BUFSIZ - 1) / BUFSIZ;
262 for (block = 0; block <= last_block; block++)
263 for (bp = buf_head; bp != END_OF_CHAIN; bp = bp->next)
264 if (bp->block == block)
266 write(logfile, (char *) bp->data, bp->datasize);
274 * Determine if a specific block is currently in one of the buffers.
280 register struct buf *bp;
282 for (bp = buf_head; bp != END_OF_CHAIN; bp = bp->next)
283 if (bp->block == block)
289 * Seek to a specified position in the file.
290 * Return 0 if successful, non-zero if can't seek there.
294 register POSITION pos;
300 if (pos < ch_zero() || (len != NULL_POSITION && pos > len))
303 new_block = pos / BUFSIZ;
304 if (ispipe && pos != ch_fpos && !buffered(new_block))
309 ch_block = new_block;
310 ch_offset = pos % BUFSIZ;
315 * Seek to the end of the file.
323 ch_fsize = filesize(file);
326 if (len != NULL_POSITION)
327 return (ch_seek(len));
330 * Do it the slow way: read till end of data.
332 while (ch_forw_get() != EOI)
339 * Seek to the beginning of the file, or as close to it as we can get.
340 * We may not be able to seek there if input is a pipe and the
341 * beginning of the pipe is no longer buffered.
346 register struct buf *bp, *firstbp;
349 * Try a plain ch_seek first.
351 if (ch_seek(ch_zero()) == 0)
355 * Can't get to position 0.
356 * Look thru the buffers for the one closest to position 0.
358 firstbp = bp = buf_head;
359 if (bp == END_OF_CHAIN)
361 while ((bp = bp->next) != END_OF_CHAIN)
362 if (bp->block < firstbp->block)
364 ch_block = firstbp->block;
370 * Return the length of the file, if known.
376 return (NULL_POSITION);
381 * Return the current position in the file.
383 #define tellpos(blk,off) ((POSITION)((((long)(blk)) * BUFSIZ) + (off)))
388 return (tellpos(ch_block, ch_offset));
392 * Get the current char and post-increment the read pointer.
402 if (ch_offset < BUFSIZ-1)
406 #if __ZOFFSET /* NOT WORKING */
407 if (ch_fsize != NULL_POSITION &&
408 tellpos(ch_block+1, 0) >= ch_fsize)
418 * Pre-decrement the read pointer and get the new current char.
427 #if __ZOFFSET /* NOT WORKING */
428 if (tellpos(ch_block-1, BUFSIZ-1) < ch_zero())
434 if (ispipe && !buffered(ch_block-1))
437 ch_offset = BUFSIZ-1;
444 * Caller wants us to have a total of at least want_nbufs buffers.
452 if (ch_nbufs < want_nbufs && ch_addbuf(want_nbufs - ch_nbufs))
455 * Cannot allocate enough buffers.
456 * If we don't have ANY, then quit.
457 * Otherwise, just report the error and return.
459 parg.p_int = want_nbufs - ch_nbufs;
460 error("Cannot allocate %d buffers", &parg);
468 * Flush any saved file state, including buffer contents.
473 register struct buf *bp;
478 * If input is a pipe, we don't flush buffer contents,
479 * since the contents can't be recovered.
481 ch_fsize = NULL_POSITION;
486 * Initialize all the buffers.
488 for (bp = buf_head; bp != END_OF_CHAIN; bp = bp->next)
489 bp->block = (long)(-1);
492 * Figure out the size of the file, if we can.
494 ch_fsize = filesize(file);
497 * Seek to a known position: the beginning of the file.
500 ch_block = ch_fpos / BUFSIZ;
501 ch_offset = ch_fpos % BUFSIZ;
503 if (lseek(file, (offset_t)0, 0) == BAD_LSEEK)
506 * Warning only; even if the seek fails for some reason,
507 * there's a good chance we're at the beginning anyway.
508 * {{ I think this is bogus reasoning. }}
510 error("seek error to 0", NULL_PARG);
515 * Allocate some new buffers.
516 * The buffers are added to the tail of the buffer chain.
522 register struct buf *bp;
523 register struct buf *newbufs;
526 * We don't have enough buffers.
527 * Allocate some new ones.
529 newbufs = (struct buf *) calloc(nnew, sizeof(struct buf));
534 * Initialize the new buffers and link them together.
535 * Link them all onto the tail of the buffer list.
538 for (bp = &newbufs[0]; bp < &newbufs[nnew]; bp++)
542 bp->block = (long)(-1);
544 newbufs[nnew-1].next = END_OF_CHAIN;
545 newbufs[0].prev = buf_tail;
546 buf_tail->next = &newbufs[0];
547 buf_tail = &newbufs[nnew-1];
552 * Use the pipe file state.
557 thisfile = &pipefile;
561 * Use the non-pipe file state.
566 thisfile = &nonpipefile;