2 * Copyright (C) 1984-2023 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
24 #include <sys/statfs.h>
25 #if HAVE_LINUX_MAGIC_H
26 #include <linux/magic.h>
30 typedef POSITION BLOCKNUM;
32 public int ignore_eoi;
35 * Pool of buffers holding the most recently used blocks of the input file.
36 * The buffer pool is kept as a doubly-linked circular list,
37 * in order from most- to least-recently used.
38 * The circular list is anchored by the file state "thisfile".
41 struct bufnode *next, *prev;
42 struct bufnode *hnext, *hprev;
49 unsigned int datasize;
50 unsigned char data[LBUFSIZE];
52 #define bufnode_buf(bn) ((struct buf *) bn)
55 * The file state is maintained in a filestate structure.
56 * A pointer to the filestate is kept in the ifile structure.
58 #define BUFHASH_SIZE 1024
60 struct bufnode buflist;
61 struct bufnode hashtbl[BUFHASH_SIZE];
71 #define ch_bufhead thisfile->buflist.next
72 #define ch_buftail thisfile->buflist.prev
73 #define ch_nbufs thisfile->nbufs
74 #define ch_block thisfile->block
75 #define ch_offset thisfile->offset
76 #define ch_fpos thisfile->fpos
77 #define ch_fsize thisfile->fsize
78 #define ch_flags thisfile->flags
79 #define ch_file thisfile->file
81 #define END_OF_CHAIN (&thisfile->buflist)
82 #define END_OF_HCHAIN(h) (&thisfile->hashtbl[h])
83 #define BUFHASH(blk) ((blk) & (BUFHASH_SIZE-1))
86 * Macros to manipulate the list of buffers in thisfile->buflist.
88 #define FOR_BUFS(bn) \
89 for (bn = ch_bufhead; bn != END_OF_CHAIN; bn = bn->next)
92 (bn)->next->prev = (bn)->prev; \
93 (bn)->prev->next = (bn)->next;
95 #define BUF_INS_HEAD(bn) \
96 (bn)->next = ch_bufhead; \
97 (bn)->prev = END_OF_CHAIN; \
98 ch_bufhead->prev = (bn); \
101 #define BUF_INS_TAIL(bn) \
102 (bn)->next = END_OF_CHAIN; \
103 (bn)->prev = ch_buftail; \
104 ch_buftail->next = (bn); \
108 * Macros to manipulate the list of buffers in thisfile->hashtbl[n].
110 #define FOR_BUFS_IN_CHAIN(h,bn) \
111 for (bn = thisfile->hashtbl[h].hnext; \
112 bn != END_OF_HCHAIN(h); bn = bn->hnext)
114 #define BUF_HASH_RM(bn) \
115 (bn)->hnext->hprev = (bn)->hprev; \
116 (bn)->hprev->hnext = (bn)->hnext;
118 #define BUF_HASH_INS(bn,h) \
119 (bn)->hnext = thisfile->hashtbl[h].hnext; \
120 (bn)->hprev = END_OF_HCHAIN(h); \
121 thisfile->hashtbl[h].hnext->hprev = (bn); \
122 thisfile->hashtbl[h].hnext = (bn);
124 static struct filestate *thisfile;
125 static int ch_ungotchar = -1;
126 static int maxbufs = -1;
131 extern int screen_trashed;
132 extern int follow_mode;
133 extern int waiting_for_data;
134 extern constant char helpdata[];
135 extern constant int size_helpdata;
136 extern IFILE curr_ifile;
139 extern char *namelogfile;
142 static int ch_addbuf();
146 * Get the character pointed to by the read pointer.
148 static int ch_get(void)
158 if (thisfile == NULL)
162 * Quick check for the common case where
163 * the desired char is in the head buffer.
165 if (ch_bufhead != END_OF_CHAIN)
167 bp = bufnode_buf(ch_bufhead);
168 if (ch_block == bp->block && ch_offset < bp->datasize)
169 return bp->data[ch_offset];
173 * Look for a buffer holding the desired block.
175 waiting_for_data = FALSE;
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. */
226 pos = (ch_block * LBUFSIZE) + bp->datasize;
227 if ((len = ch_length()) != NULL_POSITION && pos >= len)
236 * Not at the correct position: must seek.
237 * If input is a pipe, we're in trouble (can't seek on a pipe).
238 * Some data has been lost: just return "?".
240 if (!(ch_flags & CH_CANSEEK))
242 if (lseek(ch_file, (off_t)pos, SEEK_SET) == BAD_LSEEK)
244 error("seek error", NULL_PARG);
253 * If we read less than a full block, that's ok.
254 * We use partial block and pick up the rest next time.
256 if (ch_ungotchar != -1)
258 bp->data[bp->datasize] = ch_ungotchar;
261 } else if (ch_flags & CH_HELPFILE)
263 bp->data[bp->datasize] = helpdata[ch_fpos];
267 n = iread(ch_file, &bp->data[bp->datasize],
268 (unsigned int)(LBUFSIZE - bp->datasize));
284 #if MSDOS_COMPILER==WIN32C
288 error("read error", NULL_PARG);
296 * If we have a log file, write the new data to it.
298 if (!secure && logfile >= 0 && n > 0)
299 write(logfile, (char *) &bp->data[bp->datasize], n);
307 /* Either end of file or no data available.
308 * read_again indicates the latter. */
311 if (ignore_eoi || read_again)
313 /* Wait a while, then try again. */
314 if (!waiting_for_data)
317 parg.p_string = wait_message();
318 ixerror("%s", &parg);
319 waiting_for_data = TRUE;
321 sleep_ms(50); /* Reduce system load */
323 if (ignore_eoi && follow_mode == FOLLOW_NAME && curr_ifile_changed())
325 /* screen_trashed=2 causes make_display to reopen the file. */
334 if (ch_bufhead != bn)
337 * Move the buffer to the head of the buffer chain.
338 * This orders the buffer chain, most- to least-recently used.
344 * Move to head of hash chain too.
350 if (ch_offset < bp->datasize)
353 * After all that, we still don't have enough data.
354 * Go back and try again.
357 return (bp->data[ch_offset]);
361 * ch_ungetchar is a rather kludgy and limited way to push
362 * a single char onto an input file descriptor.
364 public void ch_ungetchar(int c)
366 if (c != -1 && ch_ungotchar != -1)
367 error("ch_ungetchar overrun", NULL_PARG);
374 * If we haven't read all of standard input into it, do that now.
376 public void end_logfile(void)
378 static int tried = FALSE;
382 if (!tried && ch_fsize == NULL_POSITION)
385 ierror("Finishing logfile", NULL_PARG);
386 while (ch_forw_get() != EOI)
397 * Start a log file AFTER less has already been running.
398 * Invoked from the - command; see toggle_option().
399 * Write all the existing buffered data to the log file.
401 public void sync_logfile(void)
411 nblocks = (ch_fpos + LBUFSIZE - 1) / LBUFSIZE;
412 for (block = 0; block < nblocks; block++)
417 bp = bufnode_buf(bn);
418 if (bp->block == block)
420 write(logfile, (char *) bp->data, bp->datasize);
425 if (!wrote && !warned)
427 error("Warning: log file is incomplete",
437 * Determine if a specific block is currently in one of the buffers.
439 static int buffered(BLOCKNUM block)
446 FOR_BUFS_IN_CHAIN(h, bn)
448 bp = bufnode_buf(bn);
449 if (bp->block == block)
456 * Seek to a specified position in the file.
457 * Return 0 if successful, non-zero if can't seek there.
459 public int ch_seek(POSITION pos)
464 if (thisfile == NULL)
468 if (pos < ch_zero() || (len != NULL_POSITION && pos > len))
471 new_block = pos / LBUFSIZE;
472 if (!(ch_flags & CH_CANSEEK) && pos != ch_fpos && !buffered(new_block))
476 while (ch_fpos < pos)
478 if (ch_forw_get() == EOI)
488 ch_block = new_block;
489 ch_offset = pos % LBUFSIZE;
494 * Seek to the end of the file.
496 public int ch_end_seek(void)
500 if (thisfile == NULL)
503 if (ch_flags & CH_CANSEEK)
504 ch_fsize = filesize(ch_file);
507 if (len != NULL_POSITION)
508 return (ch_seek(len));
511 * Do it the slow way: read till end of data.
513 while (ch_forw_get() != EOI)
520 * Seek to the last position in the file that is currently buffered.
522 public int ch_end_buffer_seek(void)
529 if (thisfile == NULL || (ch_flags & CH_CANSEEK))
530 return (ch_end_seek());
535 bp = bufnode_buf(bn);
536 buf_pos = (bp->block * LBUFSIZE) + bp->datasize;
537 if (buf_pos > end_pos)
541 return (ch_seek(end_pos));
545 * Seek to the beginning of the file, or as close to it as we can get.
546 * We may not be able to seek there if input is a pipe and the
547 * beginning of the pipe is no longer buffered.
549 public int ch_beg_seek(void)
552 struct bufnode *firstbn;
555 * Try a plain ch_seek first.
557 if (ch_seek(ch_zero()) == 0)
561 * Can't get to position 0.
562 * Look thru the buffers for the one closest to position 0.
564 firstbn = ch_bufhead;
565 if (firstbn == END_OF_CHAIN)
569 if (bufnode_buf(bn)->block < bufnode_buf(firstbn)->block)
572 ch_block = bufnode_buf(firstbn)->block;
578 * Return the length of the file, if known.
580 public POSITION ch_length(void)
582 if (thisfile == NULL)
583 return (NULL_POSITION);
585 return (NULL_POSITION);
586 if (ch_flags & CH_HELPFILE)
587 return (size_helpdata);
588 if (ch_flags & CH_NODATA)
594 * Return the current position in the file.
596 public POSITION ch_tell(void)
598 if (thisfile == NULL)
599 return (NULL_POSITION);
600 return (ch_block * LBUFSIZE) + ch_offset;
604 * Get the current char and post-increment the read pointer.
606 public int ch_forw_get(void)
610 if (thisfile == NULL)
615 if (ch_offset < LBUFSIZE-1)
626 * Pre-decrement the read pointer and get the new current char.
628 public int ch_back_get(void)
630 if (thisfile == NULL)
638 if (!(ch_flags & CH_CANSEEK) && !buffered(ch_block-1))
641 ch_offset = LBUFSIZE-1;
647 * Set max amount of buffer space.
648 * bufspace is in units of 1024 bytes. -1 mean no limit.
650 public void ch_setbufspace(int bufspace)
656 int lbufk = LBUFSIZE / 1024;
657 maxbufs = bufspace / lbufk + (bufspace % lbufk != 0);
664 * Flush (discard) any saved file state, including buffer contents.
666 public void ch_flush(void)
670 if (thisfile == NULL)
673 if (!(ch_flags & CH_CANSEEK))
676 * If input is a pipe, we don't flush buffer contents,
677 * since the contents can't be recovered.
679 ch_fsize = NULL_POSITION;
684 * Initialize all the buffers.
688 bufnode_buf(bn)->block = -1;
692 * Figure out the size of the file, if we can.
694 ch_fsize = filesize(ch_file);
697 * Seek to a known position: the beginning of the file.
700 ch_block = 0; /* ch_fpos / LBUFSIZE; */
701 ch_offset = 0; /* ch_fpos % LBUFSIZE; */
705 * This is a kludge to workaround a Linux kernel bug: files in
706 * /proc have a size of 0 according to fstat() but have readable
707 * data. They are sometimes, but not always, seekable.
708 * Force them to be non-seekable here.
713 if (fstatfs(ch_file, &st) == 0)
715 if (st.f_type == PROC_SUPER_MAGIC)
717 ch_fsize = NULL_POSITION;
718 ch_flags &= ~CH_CANSEEK;
724 if (lseek(ch_file, (off_t)0, SEEK_SET) == BAD_LSEEK)
727 * Warning only; even if the seek fails for some reason,
728 * there's a good chance we're at the beginning anyway.
729 * {{ I think this is bogus reasoning. }}
731 error("seek error to 0", NULL_PARG);
736 * Allocate a new buffer.
737 * The buffer is added to the tail of the buffer chain.
739 static int ch_addbuf(void)
745 * Allocate and initialize a new buffer and link it
746 * onto the tail of the buffer list.
748 bp = (struct buf *) calloc(1, sizeof(struct buf));
763 static void init_hashtbl(void)
767 for (h = 0; h < BUFHASH_SIZE; h++)
769 thisfile->hashtbl[h].hnext = END_OF_HCHAIN(h);
770 thisfile->hashtbl[h].hprev = END_OF_HCHAIN(h);
775 * Delete all buffers for this file.
777 static void ch_delbufs(void)
781 while (ch_bufhead != END_OF_CHAIN)
785 free(bufnode_buf(bn));
792 * Is it possible to seek on a file descriptor?
794 public int seekable(int f)
798 if (f == fd0 && !isatty(fd0))
801 * In MS-DOS, pipes are seekable. Check for
802 * standard input, and pretend it is not seekable.
807 return (lseek(f, (off_t)1, SEEK_SET) != BAD_LSEEK);
811 * Force EOF to be at the current read position.
812 * This is used after an ignore_eof read, during which the EOF may change.
814 public void ch_set_eof(void)
816 if (ch_fsize != NULL_POSITION && ch_fsize < ch_fpos)
822 * Initialize file state for a new file.
824 public void ch_init(int f, int flags)
827 * See if we already have a filestate for this file.
829 thisfile = (struct filestate *) get_filestate(curr_ifile);
830 if (thisfile == NULL)
833 * Allocate and initialize a new filestate.
835 thisfile = (struct filestate *)
836 ecalloc(1, sizeof(struct filestate));
837 thisfile->buflist.next = thisfile->buflist.prev = END_OF_CHAIN;
839 thisfile->flags = flags;
842 thisfile->offset = 0;
844 thisfile->fsize = NULL_POSITION;
847 * Try to seek; set CH_CANSEEK if it works.
849 if ((flags & CH_CANSEEK) && !seekable(f))
850 ch_flags &= ~CH_CANSEEK;
851 set_filestate(curr_ifile, (void *) thisfile);
853 if (thisfile->file == -1)
861 public void ch_close(void)
863 int keepstate = FALSE;
865 if (thisfile == NULL)
868 if ((ch_flags & (CH_CANSEEK|CH_POPENED|CH_HELPFILE)) && !(ch_flags & CH_KEEPOPEN))
871 * We can seek or re-open, so we don't need to keep buffers.
876 if (!(ch_flags & CH_KEEPOPEN))
879 * We don't need to keep the file descriptor open
880 * (because we can re-open it.)
881 * But don't really close it if it was opened via popen(),
882 * because pclose() wants to close it.
884 if (!(ch_flags & (CH_POPENED|CH_HELPFILE)))
892 * We don't even need to keep the filestate structure.
896 set_filestate(curr_ifile, (void *) NULL);
901 * Return ch_flags for the current file.
903 public int ch_getflags(void)
905 if (thisfile == NULL)
911 static void ch_dump(struct filestate *fs)
919 printf(" --no filestate\n");
922 printf(" file %d, flags %x, fpos %x, fsize %x, blk/off %x/%x\n",
923 fs->file, fs->flags, fs->fpos,
924 fs->fsize, fs->block, fs->offset);
925 printf(" %d bufs:\n", fs->nbufs);
926 for (bn = fs->next; bn != &fs->buflist; bn = bn->next)
928 bp = bufnode_buf(bn);
929 printf("%x: blk %x, size %x \"",
930 bp, bp->block, bp->datasize);
931 for (s = bp->data; s < bp->data + 30; s++)
932 if (*s >= ' ' && *s < 0x7F)