2 * Copyright (C) 1984-2022 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;
30 #include <sys/statfs.h>
33 typedef POSITION BLOCKNUM;
35 public int ignore_eoi;
38 * Pool of buffers holding the most recently used blocks of the input file.
39 * The buffer pool is kept as a doubly-linked circular list,
40 * in order from most- to least-recently used.
41 * The circular list is anchored by the file state "thisfile".
44 struct bufnode *next, *prev;
45 struct bufnode *hnext, *hprev;
52 unsigned int datasize;
53 unsigned char data[LBUFSIZE];
55 #define bufnode_buf(bn) ((struct buf *) bn)
58 * The file state is maintained in a filestate structure.
59 * A pointer to the filestate is kept in the ifile structure.
61 #define BUFHASH_SIZE 1024
63 struct bufnode buflist;
64 struct bufnode hashtbl[BUFHASH_SIZE];
74 #define ch_bufhead thisfile->buflist.next
75 #define ch_buftail thisfile->buflist.prev
76 #define ch_nbufs thisfile->nbufs
77 #define ch_block thisfile->block
78 #define ch_offset thisfile->offset
79 #define ch_fpos thisfile->fpos
80 #define ch_fsize thisfile->fsize
81 #define ch_flags thisfile->flags
82 #define ch_file thisfile->file
84 #define END_OF_CHAIN (&thisfile->buflist)
85 #define END_OF_HCHAIN(h) (&thisfile->hashtbl[h])
86 #define BUFHASH(blk) ((blk) & (BUFHASH_SIZE-1))
89 * Macros to manipulate the list of buffers in thisfile->buflist.
91 #define FOR_BUFS(bn) \
92 for (bn = ch_bufhead; bn != END_OF_CHAIN; bn = bn->next)
95 (bn)->next->prev = (bn)->prev; \
96 (bn)->prev->next = (bn)->next;
98 #define BUF_INS_HEAD(bn) \
99 (bn)->next = ch_bufhead; \
100 (bn)->prev = END_OF_CHAIN; \
101 ch_bufhead->prev = (bn); \
104 #define BUF_INS_TAIL(bn) \
105 (bn)->next = END_OF_CHAIN; \
106 (bn)->prev = ch_buftail; \
107 ch_buftail->next = (bn); \
111 * Macros to manipulate the list of buffers in thisfile->hashtbl[n].
113 #define FOR_BUFS_IN_CHAIN(h,bn) \
114 for (bn = thisfile->hashtbl[h].hnext; \
115 bn != END_OF_HCHAIN(h); bn = bn->hnext)
117 #define BUF_HASH_RM(bn) \
118 (bn)->hnext->hprev = (bn)->hprev; \
119 (bn)->hprev->hnext = (bn)->hnext;
121 #define BUF_HASH_INS(bn,h) \
122 (bn)->hnext = thisfile->hashtbl[h].hnext; \
123 (bn)->hprev = END_OF_HCHAIN(h); \
124 thisfile->hashtbl[h].hnext->hprev = (bn); \
125 thisfile->hashtbl[h].hnext = (bn);
127 static struct filestate *thisfile;
128 static int ch_ungotchar = -1;
129 static int maxbufs = -1;
134 extern int screen_trashed;
135 extern int follow_mode;
136 extern constant char helpdata[];
137 extern constant int size_helpdata;
138 extern IFILE curr_ifile;
141 extern char *namelogfile;
144 static int ch_addbuf();
148 * Get the character pointed to by the read pointer.
161 if (thisfile == NULL)
165 * Quick check for the common case where
166 * the desired char is in the head buffer.
168 if (ch_bufhead != END_OF_CHAIN)
170 bp = bufnode_buf(ch_bufhead);
171 if (ch_block == bp->block && ch_offset < bp->datasize)
172 return bp->data[ch_offset];
178 * Look for a buffer holding the desired block.
180 h = BUFHASH(ch_block);
181 FOR_BUFS_IN_CHAIN(h, bn)
183 bp = bufnode_buf(bn);
184 if (bp->block == ch_block)
186 if (ch_offset >= bp->datasize)
188 * Need more data in this buffer.
194 if (bn == END_OF_HCHAIN(h))
197 * Block is not in a buffer.
198 * Take the least recently used buffer
199 * and read the desired block into it.
200 * If the LRU buffer has data in it,
201 * then maybe allocate a new buffer.
203 if (ch_buftail == END_OF_CHAIN ||
204 bufnode_buf(ch_buftail)->block != -1)
207 * There is no empty buffer to use.
208 * Allocate a new buffer if:
209 * 1. We can't seek on this file and -b is not in effect; or
210 * 2. We haven't allocated the max buffers for this file yet.
212 if ((autobuf && !(ch_flags & CH_CANSEEK)) ||
213 (maxbufs < 0 || ch_nbufs < maxbufs))
216 * Allocation failed: turn off autobuf.
221 bp = bufnode_buf(bn);
222 BUF_HASH_RM(bn); /* Remove from old hash chain. */
223 bp->block = ch_block;
225 BUF_HASH_INS(bn, h); /* Insert into new hash chain. */
229 pos = (ch_block * LBUFSIZE) + bp->datasize;
230 if ((len = ch_length()) != NULL_POSITION && pos >= len)
239 * Not at the correct position: must seek.
240 * If input is a pipe, we're in trouble (can't seek on a pipe).
241 * Some data has been lost: just return "?".
243 if (!(ch_flags & CH_CANSEEK))
245 if (lseek(ch_file, (off_t)pos, SEEK_SET) == BAD_LSEEK)
247 error("seek error", NULL_PARG);
256 * If we read less than a full block, that's ok.
257 * We use partial block and pick up the rest next time.
259 if (ch_ungotchar != -1)
261 bp->data[bp->datasize] = ch_ungotchar;
264 } else if (ch_flags & CH_HELPFILE)
266 bp->data[bp->datasize] = helpdata[ch_fpos];
270 n = iread(ch_file, &bp->data[bp->datasize],
271 (unsigned int)(LBUFSIZE - bp->datasize));
278 #if MSDOS_COMPILER==WIN32C
282 error("read error", NULL_PARG);
290 * If we have a log file, write the new data to it.
292 if (!secure && logfile >= 0 && n > 0)
293 write(logfile, (char *) &bp->data[bp->datasize], n);
300 * If we have read to end of file, set ch_fsize to indicate
301 * the position of the end of file.
309 * We are ignoring EOF.
310 * Wait a while, then try again.
315 parg.p_string = wait_message();
318 sleep_ms(2); /* Reduce system load */
322 if (follow_mode == FOLLOW_NAME)
324 /* See whether the file's i-number has changed,
325 * or the file has shrunk.
326 * If so, force the file to be closed and
329 POSITION curr_pos = ch_tell();
330 int r = stat(get_filename(curr_ifile), &st);
331 if (r == 0 && (st.st_ino != curr_ino ||
332 st.st_dev != curr_dev ||
333 (curr_pos != NULL_POSITION && st.st_size < curr_pos)))
335 /* screen_trashed=2 causes
336 * make_display to reopen the file. */
348 if (ch_bufhead != bn)
351 * Move the buffer to the head of the buffer chain.
352 * This orders the buffer chain, most- to least-recently used.
358 * Move to head of hash chain too.
364 if (ch_offset >= bp->datasize)
366 * After all that, we still don't have enough data.
367 * Go back and try again.
371 return (bp->data[ch_offset]);
375 * ch_ungetchar is a rather kludgy and limited way to push
376 * a single char onto an input file descriptor.
382 if (c != -1 && ch_ungotchar != -1)
383 error("ch_ungetchar overrun", NULL_PARG);
390 * If we haven't read all of standard input into it, do that now.
393 end_logfile(VOID_PARAM)
395 static int tried = FALSE;
399 if (!tried && ch_fsize == NULL_POSITION)
402 ierror("Finishing logfile", NULL_PARG);
403 while (ch_forw_get() != EOI)
414 * Start a log file AFTER less has already been running.
415 * Invoked from the - command; see toggle_option().
416 * Write all the existing buffered data to the log file.
419 sync_logfile(VOID_PARAM)
427 nblocks = (ch_fpos + LBUFSIZE - 1) / LBUFSIZE;
428 for (block = 0; block < nblocks; block++)
433 bp = bufnode_buf(bn);
434 if (bp->block == block)
436 write(logfile, (char *) bp->data, bp->datasize);
441 if (!wrote && !warned)
443 error("Warning: log file is incomplete",
453 * Determine if a specific block is currently in one of the buffers.
464 FOR_BUFS_IN_CHAIN(h, bn)
466 bp = bufnode_buf(bn);
467 if (bp->block == block)
474 * Seek to a specified position in the file.
475 * Return 0 if successful, non-zero if can't seek there.
484 if (thisfile == NULL)
488 if (pos < ch_zero() || (len != NULL_POSITION && pos > len))
491 new_block = pos / LBUFSIZE;
492 if (!(ch_flags & CH_CANSEEK) && pos != ch_fpos && !buffered(new_block))
496 while (ch_fpos < pos)
498 if (ch_forw_get() == EOI)
508 ch_block = new_block;
509 ch_offset = pos % LBUFSIZE;
514 * Seek to the end of the file.
517 ch_end_seek(VOID_PARAM)
521 if (thisfile == NULL)
524 if (ch_flags & CH_CANSEEK)
525 ch_fsize = filesize(ch_file);
528 if (len != NULL_POSITION)
529 return (ch_seek(len));
532 * Do it the slow way: read till end of data.
534 while (ch_forw_get() != EOI)
541 * Seek to the last position in the file that is currently buffered.
544 ch_end_buffer_seek(VOID_PARAM)
551 if (thisfile == NULL || (ch_flags & CH_CANSEEK))
552 return (ch_end_seek());
557 bp = bufnode_buf(bn);
558 buf_pos = (bp->block * LBUFSIZE) + bp->datasize;
559 if (buf_pos > end_pos)
563 return (ch_seek(end_pos));
567 * Seek to the beginning of the file, or as close to it as we can get.
568 * We may not be able to seek there if input is a pipe and the
569 * beginning of the pipe is no longer buffered.
572 ch_beg_seek(VOID_PARAM)
575 struct bufnode *firstbn;
578 * Try a plain ch_seek first.
580 if (ch_seek(ch_zero()) == 0)
584 * Can't get to position 0.
585 * Look thru the buffers for the one closest to position 0.
587 firstbn = ch_bufhead;
588 if (firstbn == END_OF_CHAIN)
592 if (bufnode_buf(bn)->block < bufnode_buf(firstbn)->block)
595 ch_block = bufnode_buf(firstbn)->block;
601 * Return the length of the file, if known.
604 ch_length(VOID_PARAM)
606 if (thisfile == NULL)
607 return (NULL_POSITION);
609 return (NULL_POSITION);
610 if (ch_flags & CH_HELPFILE)
611 return (size_helpdata);
612 if (ch_flags & CH_NODATA)
618 * Return the current position in the file.
623 if (thisfile == NULL)
624 return (NULL_POSITION);
625 return (ch_block * LBUFSIZE) + ch_offset;
629 * Get the current char and post-increment the read pointer.
632 ch_forw_get(VOID_PARAM)
636 if (thisfile == NULL)
641 if (ch_offset < LBUFSIZE-1)
652 * Pre-decrement the read pointer and get the new current char.
655 ch_back_get(VOID_PARAM)
657 if (thisfile == NULL)
665 if (!(ch_flags & CH_CANSEEK) && !buffered(ch_block-1))
668 ch_offset = LBUFSIZE-1;
674 * Set max amount of buffer space.
675 * bufspace is in units of 1024 bytes. -1 mean no limit.
678 ch_setbufspace(bufspace)
685 maxbufs = ((bufspace * 1024) + LBUFSIZE-1) / LBUFSIZE;
692 * Flush (discard) any saved file state, including buffer contents.
699 if (thisfile == NULL)
702 if (!(ch_flags & CH_CANSEEK))
705 * If input is a pipe, we don't flush buffer contents,
706 * since the contents can't be recovered.
708 ch_fsize = NULL_POSITION;
713 * Initialize all the buffers.
717 bufnode_buf(bn)->block = -1;
721 * Figure out the size of the file, if we can.
723 ch_fsize = filesize(ch_file);
726 * Seek to a known position: the beginning of the file.
729 ch_block = 0; /* ch_fpos / LBUFSIZE; */
730 ch_offset = 0; /* ch_fpos % LBUFSIZE; */
734 * This is a kludge to workaround a Linux kernel bug: files in
735 * /proc have a size of 0 according to fstat() but have readable
736 * data. They are sometimes, but not always, seekable.
737 * Force them to be non-seekable here.
742 if (fstatfs(ch_file, &st) == 0)
744 if (st.f_type == PROC_SUPER_MAGIC)
746 ch_fsize = NULL_POSITION;
747 ch_flags &= ~CH_CANSEEK;
753 if (lseek(ch_file, (off_t)0, SEEK_SET) == BAD_LSEEK)
756 * Warning only; even if the seek fails for some reason,
757 * there's a good chance we're at the beginning anyway.
758 * {{ I think this is bogus reasoning. }}
760 error("seek error to 0", NULL_PARG);
765 * Allocate a new buffer.
766 * The buffer is added to the tail of the buffer chain.
769 ch_addbuf(VOID_PARAM)
775 * Allocate and initialize a new buffer and link it
776 * onto the tail of the buffer list.
778 bp = (struct buf *) calloc(1, sizeof(struct buf));
794 init_hashtbl(VOID_PARAM)
798 for (h = 0; h < BUFHASH_SIZE; h++)
800 thisfile->hashtbl[h].hnext = END_OF_HCHAIN(h);
801 thisfile->hashtbl[h].hprev = END_OF_HCHAIN(h);
806 * Delete all buffers for this file.
809 ch_delbufs(VOID_PARAM)
813 while (ch_bufhead != END_OF_CHAIN)
817 free(bufnode_buf(bn));
824 * Is it possible to seek on a file descriptor?
832 if (f == fd0 && !isatty(fd0))
835 * In MS-DOS, pipes are seekable. Check for
836 * standard input, and pretend it is not seekable.
841 return (lseek(f, (off_t)1, SEEK_SET) != BAD_LSEEK);
845 * Force EOF to be at the current read position.
846 * This is used after an ignore_eof read, during which the EOF may change.
849 ch_set_eof(VOID_PARAM)
851 if (ch_fsize != NULL_POSITION && ch_fsize < ch_fpos)
857 * Initialize file state for a new file.
865 * See if we already have a filestate for this file.
867 thisfile = (struct filestate *) get_filestate(curr_ifile);
868 if (thisfile == NULL)
871 * Allocate and initialize a new filestate.
873 thisfile = (struct filestate *)
874 ecalloc(1, sizeof(struct filestate));
875 thisfile->buflist.next = thisfile->buflist.prev = END_OF_CHAIN;
877 thisfile->flags = flags;
880 thisfile->offset = 0;
882 thisfile->fsize = NULL_POSITION;
885 * Try to seek; set CH_CANSEEK if it works.
887 if ((flags & CH_CANSEEK) && !seekable(f))
888 ch_flags &= ~CH_CANSEEK;
889 set_filestate(curr_ifile, (void *) thisfile);
891 if (thisfile->file == -1)
902 int keepstate = FALSE;
904 if (thisfile == NULL)
907 if ((ch_flags & (CH_CANSEEK|CH_POPENED|CH_HELPFILE)) && !(ch_flags & CH_KEEPOPEN))
910 * We can seek or re-open, so we don't need to keep buffers.
915 if (!(ch_flags & CH_KEEPOPEN))
918 * We don't need to keep the file descriptor open
919 * (because we can re-open it.)
920 * But don't really close it if it was opened via popen(),
921 * because pclose() wants to close it.
923 if (!(ch_flags & (CH_POPENED|CH_HELPFILE)))
931 * We don't even need to keep the filestate structure.
935 set_filestate(curr_ifile, (void *) NULL);
940 * Return ch_flags for the current file.
943 ch_getflags(VOID_PARAM)
945 if (thisfile == NULL)
952 ch_dump(struct filestate *fs)
960 printf(" --no filestate\n");
963 printf(" file %d, flags %x, fpos %x, fsize %x, blk/off %x/%x\n",
964 fs->file, fs->flags, fs->fpos,
965 fs->fsize, fs->block, fs->offset);
966 printf(" %d bufs:\n", fs->nbufs);
967 for (bn = fs->next; bn != &fs->buflist; bn = bn->next)
969 bp = bufnode_buf(bn);
970 printf("%x: blk %x, size %x \"",
971 bp, bp->block, bp->datasize);
972 for (s = bp->data; s < bp->data + 30; s++)
973 if (*s >= ' ' && *s < 0x7F)