2 * Copyright (C) 1984-2012 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, see the README file.
12 * Low level character input from the input file.
13 * We use these special purpose routines which optimize moving
14 * both forward and backward from the current read pointer.
18 #if MSDOS_COMPILER==WIN32C
25 extern dev_t curr_dev;
26 extern ino_t curr_ino;
29 typedef POSITION BLOCKNUM;
31 public int ignore_eoi;
34 * Pool of buffers holding the most recently used blocks of the input file.
35 * The buffer pool is kept as a doubly-linked circular list,
36 * in order from most- to least-recently used.
37 * The circular list is anchored by the file state "thisfile".
40 struct bufnode *next, *prev;
41 struct bufnode *hnext, *hprev;
48 unsigned int datasize;
49 unsigned char data[LBUFSIZE];
51 #define bufnode_buf(bn) ((struct buf *) bn)
54 * The file state is maintained in a filestate structure.
55 * A pointer to the filestate is kept in the ifile structure.
57 #define BUFHASH_SIZE 64
59 struct bufnode buflist;
60 struct bufnode hashtbl[BUFHASH_SIZE];
70 #define ch_bufhead thisfile->buflist.next
71 #define ch_buftail thisfile->buflist.prev
72 #define ch_nbufs thisfile->nbufs
73 #define ch_block thisfile->block
74 #define ch_offset thisfile->offset
75 #define ch_fpos thisfile->fpos
76 #define ch_fsize thisfile->fsize
77 #define ch_flags thisfile->flags
78 #define ch_file thisfile->file
80 #define END_OF_CHAIN (&thisfile->buflist)
81 #define END_OF_HCHAIN(h) (&thisfile->hashtbl[h])
82 #define BUFHASH(blk) ((blk) & (BUFHASH_SIZE-1))
85 * Macros to manipulate the list of buffers in thisfile->buflist.
87 #define FOR_BUFS(bn) \
88 for (bn = ch_bufhead; bn != END_OF_CHAIN; bn = bn->next)
91 (bn)->next->prev = (bn)->prev; \
92 (bn)->prev->next = (bn)->next;
94 #define BUF_INS_HEAD(bn) \
95 (bn)->next = ch_bufhead; \
96 (bn)->prev = END_OF_CHAIN; \
97 ch_bufhead->prev = (bn); \
100 #define BUF_INS_TAIL(bn) \
101 (bn)->next = END_OF_CHAIN; \
102 (bn)->prev = ch_buftail; \
103 ch_buftail->next = (bn); \
107 * Macros to manipulate the list of buffers in thisfile->hashtbl[n].
109 #define FOR_BUFS_IN_CHAIN(h,bn) \
110 for (bn = thisfile->hashtbl[h].hnext; \
111 bn != END_OF_HCHAIN(h); bn = bn->hnext)
113 #define BUF_HASH_RM(bn) \
114 (bn)->hnext->hprev = (bn)->hprev; \
115 (bn)->hprev->hnext = (bn)->hnext;
117 #define BUF_HASH_INS(bn,h) \
118 (bn)->hnext = thisfile->hashtbl[h].hnext; \
119 (bn)->hprev = END_OF_HCHAIN(h); \
120 thisfile->hashtbl[h].hnext->hprev = (bn); \
121 thisfile->hashtbl[h].hnext = (bn);
123 static struct filestate *thisfile;
124 static int ch_ungotchar = -1;
125 static int maxbufs = -1;
130 extern int screen_trashed;
131 extern int follow_mode;
132 extern constant char helpdata[];
133 extern constant int size_helpdata;
134 extern IFILE curr_ifile;
137 extern char *namelogfile;
140 static int ch_addbuf();
144 * Get the character pointed to by the read pointer.
149 register struct buf *bp;
150 register struct bufnode *bn;
157 if (thisfile == NULL)
161 * Quick check for the common case where
162 * the desired char is in the head buffer.
164 if (ch_bufhead != END_OF_CHAIN)
166 bp = bufnode_buf(ch_bufhead);
167 if (ch_block == bp->block && ch_offset < bp->datasize)
168 return bp->data[ch_offset];
174 * Look for a buffer holding the desired block.
176 h = BUFHASH(ch_block);
177 FOR_BUFS_IN_CHAIN(h, bn)
179 bp = bufnode_buf(bn);
180 if (bp->block == ch_block)
182 if (ch_offset >= bp->datasize)
184 * Need more data in this buffer.
190 if (bn == END_OF_HCHAIN(h))
193 * Block is not in a buffer.
194 * Take the least recently used buffer
195 * and read the desired block into it.
196 * If the LRU buffer has data in it,
197 * then maybe allocate a new buffer.
199 if (ch_buftail == END_OF_CHAIN ||
200 bufnode_buf(ch_buftail)->block != -1)
203 * There is no empty buffer to use.
204 * Allocate a new buffer if:
205 * 1. We can't seek on this file and -b is not in effect; or
206 * 2. We haven't allocated the max buffers for this file yet.
208 if ((autobuf && !(ch_flags & CH_CANSEEK)) ||
209 (maxbufs < 0 || ch_nbufs < maxbufs))
212 * Allocation failed: turn off autobuf.
217 bp = bufnode_buf(bn);
218 BUF_HASH_RM(bn); /* Remove from old hash chain. */
219 bp->block = ch_block;
221 BUF_HASH_INS(bn, h); /* Insert into new hash chain. */
225 pos = (ch_block * LBUFSIZE) + bp->datasize;
226 if ((len = ch_length()) != NULL_POSITION && pos >= len)
235 * Not at the correct position: must seek.
236 * If input is a pipe, we're in trouble (can't seek on a pipe).
237 * Some data has been lost: just return "?".
239 if (!(ch_flags & CH_CANSEEK))
241 if (lseek(ch_file, (off_t)pos, SEEK_SET) == BAD_LSEEK)
243 error("seek error", NULL_PARG);
252 * If we read less than a full block, that's ok.
253 * We use partial block and pick up the rest next time.
255 if (ch_ungotchar != -1)
257 bp->data[bp->datasize] = ch_ungotchar;
260 } else if (ch_flags & CH_HELPFILE)
262 bp->data[bp->datasize] = helpdata[ch_fpos];
266 n = iread(ch_file, &bp->data[bp->datasize],
267 (unsigned int)(LBUFSIZE - bp->datasize));
274 #if MSDOS_COMPILER==WIN32C
278 error("read error", NULL_PARG);
286 * If we have a log file, write the new data to it.
288 if (!secure && logfile >= 0 && n > 0)
289 write(logfile, (char *) &bp->data[bp->datasize], n);
296 * If we have read to end of file, set ch_fsize to indicate
297 * the position of the end of file.
305 * We are ignoring EOF.
306 * Wait a while, then try again.
311 parg.p_string = wait_message();
317 #if MSDOS_COMPILER==WIN32C
324 if (follow_mode == FOLLOW_NAME)
326 /* See whether the file's i-number has changed.
327 * If so, force the file to be closed and
330 int r = stat(get_filename(curr_ifile), &st);
331 if (r == 0 && (st.st_ino != curr_ino ||
332 st.st_dev != curr_dev))
334 /* screen_trashed=2 causes
335 * make_display to reopen the file. */
347 if (ch_bufhead != bn)
350 * Move the buffer to the head of the buffer chain.
351 * This orders the buffer chain, most- to least-recently used.
357 * Move to head of hash chain too.
363 if (ch_offset >= bp->datasize)
365 * After all that, we still don't have enough data.
366 * Go back and try again.
370 return (bp->data[ch_offset]);
374 * ch_ungetchar is a rather kludgy and limited way to push
375 * a single char onto an input file descriptor.
381 if (c != -1 && ch_ungotchar != -1)
382 error("ch_ungetchar overrun", NULL_PARG);
389 * If we haven't read all of standard input into it, do that now.
394 static int tried = FALSE;
398 if (!tried && ch_fsize == NULL_POSITION)
401 ierror("Finishing logfile", NULL_PARG);
402 while (ch_forw_get() != EOI)
412 * Start a log file AFTER less has already been running.
413 * Invoked from the - command; see toggle_option().
414 * Write all the existing buffered data to the log file.
419 register struct buf *bp;
420 register struct bufnode *bn;
425 nblocks = (ch_fpos + LBUFSIZE - 1) / LBUFSIZE;
426 for (block = 0; block < nblocks; block++)
431 bp = bufnode_buf(bn);
432 if (bp->block == block)
434 write(logfile, (char *) bp->data, bp->datasize);
439 if (!wrote && !warned)
441 error("Warning: log file is incomplete",
451 * Determine if a specific block is currently in one of the buffers.
457 register struct buf *bp;
458 register struct bufnode *bn;
462 FOR_BUFS_IN_CHAIN(h, bn)
464 bp = bufnode_buf(bn);
465 if (bp->block == block)
472 * Seek to a specified position in the file.
473 * Return 0 if successful, non-zero if can't seek there.
477 register POSITION pos;
482 if (thisfile == NULL)
486 if (pos < ch_zero() || (len != NULL_POSITION && pos > len))
489 new_block = pos / LBUFSIZE;
490 if (!(ch_flags & CH_CANSEEK) && pos != ch_fpos && !buffered(new_block))
494 while (ch_fpos < pos)
496 if (ch_forw_get() == EOI)
506 ch_block = new_block;
507 ch_offset = pos % LBUFSIZE;
512 * Seek to the end of the file.
519 if (thisfile == NULL)
522 if (ch_flags & CH_CANSEEK)
523 ch_fsize = filesize(ch_file);
526 if (len != NULL_POSITION)
527 return (ch_seek(len));
530 * Do it the slow way: read till end of data.
532 while (ch_forw_get() != EOI)
539 * Seek to the beginning of the file, or as close to it as we can get.
540 * We may not be able to seek there if input is a pipe and the
541 * beginning of the pipe is no longer buffered.
546 register struct bufnode *bn;
547 register struct bufnode *firstbn;
550 * Try a plain ch_seek first.
552 if (ch_seek(ch_zero()) == 0)
556 * Can't get to position 0.
557 * Look thru the buffers for the one closest to position 0.
559 firstbn = ch_bufhead;
560 if (firstbn == END_OF_CHAIN)
564 if (bufnode_buf(bn)->block < bufnode_buf(firstbn)->block)
567 ch_block = bufnode_buf(firstbn)->block;
573 * Return the length of the file, if known.
578 if (thisfile == NULL)
579 return (NULL_POSITION);
581 return (NULL_POSITION);
582 if (ch_flags & CH_HELPFILE)
583 return (size_helpdata);
584 if (ch_flags & CH_NODATA)
590 * Return the current position in the file.
595 if (thisfile == NULL)
596 return (NULL_POSITION);
597 return (ch_block * LBUFSIZE) + ch_offset;
601 * Get the current char and post-increment the read pointer.
608 if (thisfile == NULL)
613 if (ch_offset < LBUFSIZE-1)
624 * Pre-decrement the read pointer and get the new current char.
629 if (thisfile == NULL)
637 if (!(ch_flags & CH_CANSEEK) && !buffered(ch_block-1))
640 ch_offset = LBUFSIZE-1;
646 * Set max amount of buffer space.
647 * bufspace is in units of 1024 bytes. -1 mean no limit.
650 ch_setbufspace(bufspace)
657 maxbufs = ((bufspace * 1024) + LBUFSIZE-1) / LBUFSIZE;
664 * Flush (discard) any saved file state, including buffer contents.
669 register struct bufnode *bn;
671 if (thisfile == NULL)
674 if (!(ch_flags & CH_CANSEEK))
677 * If input is a pipe, we don't flush buffer contents,
678 * since the contents can't be recovered.
680 ch_fsize = NULL_POSITION;
685 * Initialize all the buffers.
689 bufnode_buf(bn)->block = -1;
693 * Figure out the size of the file, if we can.
695 ch_fsize = filesize(ch_file);
698 * Seek to a known position: the beginning of the file.
701 ch_block = 0; /* ch_fpos / LBUFSIZE; */
702 ch_offset = 0; /* ch_fpos % LBUFSIZE; */
706 * This is a kludge to workaround a Linux kernel bug: files in
707 * /proc have a size of 0 according to fstat() but have readable
708 * data. They are sometimes, but not always, seekable.
709 * Force them to be non-seekable here.
713 ch_fsize = NULL_POSITION;
714 ch_flags &= ~CH_CANSEEK;
718 if (lseek(ch_file, (off_t)0, SEEK_SET) == BAD_LSEEK)
721 * Warning only; even if the seek fails for some reason,
722 * there's a good chance we're at the beginning anyway.
723 * {{ I think this is bogus reasoning. }}
725 error("seek error to 0", NULL_PARG);
730 * Allocate a new buffer.
731 * The buffer is added to the tail of the buffer chain.
736 register struct buf *bp;
737 register struct bufnode *bn;
740 * Allocate and initialize a new buffer and link it
741 * onto the tail of the buffer list.
743 bp = (struct buf *) calloc(1, sizeof(struct buf));
763 for (h = 0; h < BUFHASH_SIZE; h++)
765 thisfile->hashtbl[h].hnext = END_OF_HCHAIN(h);
766 thisfile->hashtbl[h].hprev = END_OF_HCHAIN(h);
771 * Delete all buffers for this file.
776 register struct bufnode *bn;
778 while (ch_bufhead != END_OF_CHAIN)
782 free(bufnode_buf(bn));
789 * Is it possible to seek on a file descriptor?
797 if (f == fd0 && !isatty(fd0))
800 * In MS-DOS, pipes are seekable. Check for
801 * standard input, and pretend it is not seekable.
806 return (lseek(f, (off_t)1, SEEK_SET) != BAD_LSEEK);
810 * Force EOF to be at the current read position.
811 * This is used after an ignore_eof read, during which the EOF may change.
821 * Initialize file state for a new file.
829 * See if we already have a filestate for this file.
831 thisfile = (struct filestate *) get_filestate(curr_ifile);
832 if (thisfile == NULL)
835 * Allocate and initialize a new filestate.
837 thisfile = (struct filestate *)
838 calloc(1, sizeof(struct filestate));
839 thisfile->buflist.next = thisfile->buflist.prev = END_OF_CHAIN;
844 thisfile->offset = 0;
846 thisfile->fsize = NULL_POSITION;
850 * Try to seek; set CH_CANSEEK if it works.
852 if ((flags & CH_CANSEEK) && !seekable(f))
853 ch_flags &= ~CH_CANSEEK;
854 set_filestate(curr_ifile, (void *) thisfile);
856 if (thisfile->file == -1)
867 int keepstate = FALSE;
869 if (thisfile == NULL)
872 if (ch_flags & (CH_CANSEEK|CH_POPENED|CH_HELPFILE))
875 * We can seek or re-open, so we don't need to keep buffers.
880 if (!(ch_flags & CH_KEEPOPEN))
883 * We don't need to keep the file descriptor open
884 * (because we can re-open it.)
885 * But don't really close it if it was opened via popen(),
886 * because pclose() wants to close it.
888 if (!(ch_flags & (CH_POPENED|CH_HELPFILE)))
896 * We don't even need to keep the filestate structure.
900 set_filestate(curr_ifile, (void *) NULL);
905 * Return ch_flags for the current file.
910 if (thisfile == NULL)
917 ch_dump(struct filestate *fs)
925 printf(" --no filestate\n");
928 printf(" file %d, flags %x, fpos %x, fsize %x, blk/off %x/%x\n",
929 fs->file, fs->flags, fs->fpos,
930 fs->fsize, fs->block, fs->offset);
931 printf(" %d bufs:\n", fs->nbufs);
932 for (bn = fs->next; bn != &fs->buflist; bn = bn->next)
934 bp = bufnode_buf(bn);
935 printf("%x: blk %x, size %x \"",
936 bp, bp->block, bp->datasize);
937 for (s = bp->data; s < bp->data + 30; s++)
938 if (*s >= ' ' && *s < 0x7F)