2 * Copyright (C) 1984-2007 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
13 * Low level character input from the input file.
14 * We use these special purpose routines which optimize moving
15 * both forward and backward from the current read pointer.
19 #if MSDOS_COMPILER==WIN32C
24 typedef POSITION BLOCKNUM;
26 public int ignore_eoi;
29 * Pool of buffers holding the most recently used blocks of the input file.
30 * The buffer pool is kept as a doubly-linked circular list,
31 * in order from most- to least-recently used.
32 * The circular list is anchored by the file state "thisfile".
36 struct buf *next, *prev;
37 struct buf *hnext, *hprev;
39 unsigned int datasize;
40 unsigned char data[LBUFSIZE];
44 /* -- Following members must match struct buf */
45 struct buf *buf_next, *buf_prev;
46 struct buf *buf_hnext, *buf_hprev;
50 * The file state is maintained in a filestate structure.
51 * A pointer to the filestate is kept in the ifile structure.
53 #define BUFHASH_SIZE 64
55 struct buf *buf_next, *buf_prev;
56 struct buflist hashtbl[BUFHASH_SIZE];
66 #define ch_bufhead thisfile->buf_next
67 #define ch_buftail thisfile->buf_prev
68 #define ch_nbufs thisfile->nbufs
69 #define ch_block thisfile->block
70 #define ch_offset thisfile->offset
71 #define ch_fpos thisfile->fpos
72 #define ch_fsize thisfile->fsize
73 #define ch_flags thisfile->flags
74 #define ch_file thisfile->file
76 #define END_OF_CHAIN ((struct buf *)&thisfile->buf_next)
77 #define END_OF_HCHAIN(h) ((struct buf *)&thisfile->hashtbl[h])
78 #define BUFHASH(blk) ((blk) & (BUFHASH_SIZE-1))
80 #define FOR_BUFS_IN_CHAIN(h,bp) \
81 for (bp = thisfile->hashtbl[h].buf_hnext; \
82 bp != END_OF_HCHAIN(h); bp = bp->hnext)
85 (bp)->hnext->hprev = (bp)->hprev; \
86 (bp)->hprev->hnext = (bp)->hnext;
88 #define HASH_INS(bp,h) \
89 (bp)->hnext = thisfile->hashtbl[h].buf_hnext; \
90 (bp)->hprev = END_OF_HCHAIN(h); \
91 thisfile->hashtbl[h].buf_hnext->hprev = (bp); \
92 thisfile->hashtbl[h].buf_hnext = (bp);
94 static struct filestate *thisfile;
95 static int ch_ungotchar = -1;
96 static int maxbufs = -1;
101 extern constant char helpdata[];
102 extern constant int size_helpdata;
103 extern IFILE curr_ifile;
106 extern char *namelogfile;
109 static int ch_addbuf();
113 * Get the character pointed to by the read pointer.
114 * ch_get() is a macro which is more efficient to call
115 * than fch_get (the function), in the usual case
116 * that the block desired is at the head of the chain.
118 #define ch_get() ((ch_block == ch_bufhead->block && \
119 ch_offset < ch_bufhead->datasize) ? \
120 ch_bufhead->data[ch_offset] : fch_get())
124 register struct buf *bp;
131 if (thisfile == NULL)
137 * Look for a buffer holding the desired block.
139 h = BUFHASH(ch_block);
140 FOR_BUFS_IN_CHAIN(h, bp)
142 if (bp->block == ch_block)
144 if (ch_offset >= bp->datasize)
146 * Need more data in this buffer.
153 * Block is not in a buffer.
154 * Take the least recently used buffer
155 * and read the desired block into it.
156 * If the LRU buffer has data in it,
157 * then maybe allocate a new buffer.
159 if (ch_buftail == END_OF_CHAIN || ch_buftail->block != -1)
162 * There is no empty buffer to use.
163 * Allocate a new buffer if:
164 * 1. We can't seek on this file and -b is not in effect; or
165 * 2. We haven't allocated the max buffers for this file yet.
167 if ((autobuf && !(ch_flags & CH_CANSEEK)) ||
168 (maxbufs < 0 || ch_nbufs < maxbufs))
171 * Allocation failed: turn off autobuf.
176 HASH_RM(bp); /* Remove from old hash chain. */
177 bp->block = ch_block;
179 HASH_INS(bp, h); /* Insert into new hash chain. */
182 pos = (ch_block * LBUFSIZE) + bp->datasize;
183 if ((len = ch_length()) != NULL_POSITION && pos >= len)
192 * Not at the correct position: must seek.
193 * If input is a pipe, we're in trouble (can't seek on a pipe).
194 * Some data has been lost: just return "?".
196 if (!(ch_flags & CH_CANSEEK))
198 if (lseek(ch_file, (off_t)pos, 0) == BAD_LSEEK)
200 error("seek error", NULL_PARG);
209 * If we read less than a full block, that's ok.
210 * We use partial block and pick up the rest next time.
212 if (ch_ungotchar != -1)
214 bp->data[bp->datasize] = ch_ungotchar;
217 } else if (ch_flags & CH_HELPFILE)
219 bp->data[bp->datasize] = helpdata[ch_fpos];
223 n = iread(ch_file, &bp->data[bp->datasize],
224 (unsigned int)(LBUFSIZE - bp->datasize));
231 #if MSDOS_COMPILER==WIN32C
235 error("read error", NULL_PARG);
243 * If we have a log file, write the new data to it.
245 if (!secure && logfile >= 0 && n > 0)
246 write(logfile, (char *) &bp->data[bp->datasize], n);
253 * If we have read to end of file, set ch_fsize to indicate
254 * the position of the end of file.
262 * We are ignoring EOF.
263 * Wait a while, then try again.
268 parg.p_string = wait_message();
274 #if MSDOS_COMPILER==WIN32C
285 if (ch_bufhead != bp)
288 * Move the buffer to the head of the buffer chain.
289 * This orders the buffer chain, most- to least-recently used.
291 bp->next->prev = bp->prev;
292 bp->prev->next = bp->next;
293 bp->next = ch_bufhead;
294 bp->prev = END_OF_CHAIN;
295 ch_bufhead->prev = bp;
299 * Move to head of hash chain too.
305 if (ch_offset >= bp->datasize)
307 * After all that, we still don't have enough data.
308 * Go back and try again.
312 return (bp->data[ch_offset]);
316 * ch_ungetchar is a rather kludgy and limited way to push
317 * a single char onto an input file descriptor.
323 if (c != -1 && ch_ungotchar != -1)
324 error("ch_ungetchar overrun", NULL_PARG);
331 * If we haven't read all of standard input into it, do that now.
336 static int tried = FALSE;
340 if (!tried && ch_fsize == NULL_POSITION)
343 ierror("Finishing logfile", NULL_PARG);
344 while (ch_forw_get() != EOI)
354 * Start a log file AFTER less has already been running.
355 * Invoked from the - command; see toggle_option().
356 * Write all the existing buffered data to the log file.
361 register struct buf *bp;
366 nblocks = (ch_fpos + LBUFSIZE - 1) / LBUFSIZE;
367 for (block = 0; block < nblocks; block++)
369 for (bp = ch_bufhead; ; bp = bp->next)
371 if (bp == END_OF_CHAIN)
375 error("Warning: log file is incomplete",
381 if (bp->block == block)
383 write(logfile, (char *) bp->data, bp->datasize);
393 * Determine if a specific block is currently in one of the buffers.
399 register struct buf *bp;
403 FOR_BUFS_IN_CHAIN(h, bp)
405 if (bp->block == block)
412 * Seek to a specified position in the file.
413 * Return 0 if successful, non-zero if can't seek there.
417 register POSITION pos;
422 if (thisfile == NULL)
426 if (pos < ch_zero() || (len != NULL_POSITION && pos > len))
429 new_block = pos / LBUFSIZE;
430 if (!(ch_flags & CH_CANSEEK) && pos != ch_fpos && !buffered(new_block))
434 while (ch_fpos < pos)
436 if (ch_forw_get() == EOI)
446 ch_block = new_block;
447 ch_offset = pos % LBUFSIZE;
452 * Seek to the end of the file.
459 if (thisfile == NULL)
462 if (ch_flags & CH_CANSEEK)
463 ch_fsize = filesize(ch_file);
466 if (len != NULL_POSITION)
467 return (ch_seek(len));
470 * Do it the slow way: read till end of data.
472 while (ch_forw_get() != EOI)
479 * Seek to the beginning of the file, or as close to it as we can get.
480 * We may not be able to seek there if input is a pipe and the
481 * beginning of the pipe is no longer buffered.
486 register struct buf *bp, *firstbp;
489 * Try a plain ch_seek first.
491 if (ch_seek(ch_zero()) == 0)
495 * Can't get to position 0.
496 * Look thru the buffers for the one closest to position 0.
498 firstbp = bp = ch_bufhead;
499 if (bp == END_OF_CHAIN)
501 while ((bp = bp->next) != END_OF_CHAIN)
502 if (bp->block < firstbp->block)
504 ch_block = firstbp->block;
510 * Return the length of the file, if known.
515 if (thisfile == NULL)
516 return (NULL_POSITION);
518 return (NULL_POSITION);
519 if (ch_flags & CH_HELPFILE)
520 return (size_helpdata);
525 * Return the current position in the file.
530 if (thisfile == NULL)
531 return (NULL_POSITION);
532 return (ch_block * LBUFSIZE) + ch_offset;
536 * Get the current char and post-increment the read pointer.
543 if (thisfile == NULL)
548 if (ch_offset < LBUFSIZE-1)
559 * Pre-decrement the read pointer and get the new current char.
564 if (thisfile == NULL)
572 if (!(ch_flags & CH_CANSEEK) && !buffered(ch_block-1))
575 ch_offset = LBUFSIZE-1;
581 * Set max amount of buffer space.
582 * bufspace is in units of 1024 bytes. -1 mean no limit.
585 ch_setbufspace(bufspace)
592 maxbufs = ((bufspace * 1024) + LBUFSIZE-1) / LBUFSIZE;
599 * Flush (discard) any saved file state, including buffer contents.
604 register struct buf *bp;
606 if (thisfile == NULL)
609 if (!(ch_flags & CH_CANSEEK))
612 * If input is a pipe, we don't flush buffer contents,
613 * since the contents can't be recovered.
615 ch_fsize = NULL_POSITION;
620 * Initialize all the buffers.
622 for (bp = ch_bufhead; bp != END_OF_CHAIN; bp = bp->next)
626 * Figure out the size of the file, if we can.
628 ch_fsize = filesize(ch_file);
631 * Seek to a known position: the beginning of the file.
634 ch_block = 0; /* ch_fpos / LBUFSIZE; */
635 ch_offset = 0; /* ch_fpos % LBUFSIZE; */
639 * This is a kludge to workaround a Linux kernel bug: files in
640 * /proc have a size of 0 according to fstat() but have readable
641 * data. They are sometimes, but not always, seekable.
642 * Force them to be non-seekable here.
646 ch_fsize = NULL_POSITION;
647 ch_flags &= ~CH_CANSEEK;
651 if (lseek(ch_file, (off_t)0, 0) == BAD_LSEEK)
654 * Warning only; even if the seek fails for some reason,
655 * there's a good chance we're at the beginning anyway.
656 * {{ I think this is bogus reasoning. }}
658 error("seek error to 0", NULL_PARG);
663 * Allocate a new buffer.
664 * The buffer is added to the tail of the buffer chain.
669 register struct buf *bp;
672 * Allocate and initialize a new buffer and link it
673 * onto the tail of the buffer list.
675 bp = (struct buf *) calloc(1, sizeof(struct buf));
680 bp->next = END_OF_CHAIN;
681 bp->prev = ch_buftail;
682 ch_buftail->next = bp;
696 for (h = 0; h < BUFHASH_SIZE; h++)
698 thisfile->hashtbl[h].buf_hnext = END_OF_HCHAIN(h);
699 thisfile->hashtbl[h].buf_hprev = END_OF_HCHAIN(h);
704 * Delete all buffers for this file.
709 register struct buf *bp;
711 while (ch_bufhead != END_OF_CHAIN)
714 bp->next->prev = bp->prev;;
715 bp->prev->next = bp->next;
723 * Is it possible to seek on a file descriptor?
731 if (f == fd0 && !isatty(fd0))
734 * In MS-DOS, pipes are seekable. Check for
735 * standard input, and pretend it is not seekable.
740 return (lseek(f, (off_t)1, 0) != BAD_LSEEK);
744 * Initialize file state for a new file.
752 * See if we already have a filestate for this file.
754 thisfile = (struct filestate *) get_filestate(curr_ifile);
755 if (thisfile == NULL)
758 * Allocate and initialize a new filestate.
760 thisfile = (struct filestate *)
761 calloc(1, sizeof(struct filestate));
762 thisfile->buf_next = thisfile->buf_prev = END_OF_CHAIN;
767 thisfile->offset = 0;
769 thisfile->fsize = NULL_POSITION;
773 * Try to seek; set CH_CANSEEK if it works.
775 if ((flags & CH_CANSEEK) && !seekable(f))
776 ch_flags &= ~CH_CANSEEK;
777 set_filestate(curr_ifile, (void *) thisfile);
779 if (thisfile->file == -1)
790 int keepstate = FALSE;
792 if (thisfile == NULL)
795 if (ch_flags & (CH_CANSEEK|CH_POPENED|CH_HELPFILE))
798 * We can seek or re-open, so we don't need to keep buffers.
803 if (!(ch_flags & CH_KEEPOPEN))
806 * We don't need to keep the file descriptor open
807 * (because we can re-open it.)
808 * But don't really close it if it was opened via popen(),
809 * because pclose() wants to close it.
811 if (!(ch_flags & (CH_POPENED|CH_HELPFILE)))
819 * We don't even need to keep the filestate structure.
823 set_filestate(curr_ifile, (void *) NULL);
828 * Return ch_flags for the current file.
833 if (thisfile == NULL)
840 ch_dump(struct filestate *fs)
847 printf(" --no filestate\n");
850 printf(" file %d, flags %x, fpos %x, fsize %x, blk/off %x/%x\n",
851 fs->file, fs->flags, fs->fpos,
852 fs->fsize, fs->block, fs->offset);
853 printf(" %d bufs:\n", fs->nbufs);
854 for (bp = fs->buf_next; bp != (struct buf *)fs; bp = bp->next)
856 printf("%x: blk %x, size %x \"",
857 bp, bp->block, bp->datasize);
858 for (s = bp->data; s < bp->data + 30; s++)
859 if (*s >= ' ' && *s < 0x7F)