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
26 extern dev_t curr_dev
;
27 extern ino_t curr_ino
;
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".
42 struct buf
*next
, *prev
;
43 struct buf
*hnext
, *hprev
;
45 unsigned int datasize
;
46 unsigned char data
[LBUFSIZE
];
50 /* -- Following members must match struct buf */
51 struct buf
*buf_next
, *buf_prev
;
52 struct buf
*buf_hnext
, *buf_hprev
;
56 * The file state is maintained in a filestate structure.
57 * A pointer to the filestate is kept in the ifile structure.
59 #define BUFHASH_SIZE 64
61 struct buf
*buf_next
, *buf_prev
;
62 struct buflist hashtbl
[BUFHASH_SIZE
];
72 #define ch_bufhead thisfile->buf_next
73 #define ch_buftail thisfile->buf_prev
74 #define ch_nbufs thisfile->nbufs
75 #define ch_block thisfile->block
76 #define ch_offset thisfile->offset
77 #define ch_fpos thisfile->fpos
78 #define ch_fsize thisfile->fsize
79 #define ch_flags thisfile->flags
80 #define ch_file thisfile->file
82 #define END_OF_CHAIN ((struct buf *)&thisfile->buf_next)
83 #define END_OF_HCHAIN(h) ((struct buf *)&thisfile->hashtbl[h])
84 #define BUFHASH(blk) ((blk) & (BUFHASH_SIZE-1))
86 #define FOR_BUFS_IN_CHAIN(h,bp) \
87 for (bp = thisfile->hashtbl[h].buf_hnext; \
88 bp != END_OF_HCHAIN(h); bp = bp->hnext)
91 (bp)->hnext->hprev = (bp)->hprev; \
92 (bp)->hprev->hnext = (bp)->hnext;
94 #define HASH_INS(bp,h) \
95 (bp)->hnext = thisfile->hashtbl[h].buf_hnext; \
96 (bp)->hprev = END_OF_HCHAIN(h); \
97 thisfile->hashtbl[h].buf_hnext->hprev = (bp); \
98 thisfile->hashtbl[h].buf_hnext = (bp);
100 static struct filestate
*thisfile
;
101 static int ch_ungotchar
= -1;
102 static int maxbufs
= -1;
107 extern int screen_trashed
;
108 extern int follow_mode
;
109 extern constant
char helpdata
[];
110 extern constant
int size_helpdata
;
111 extern IFILE curr_ifile
;
114 extern char *namelogfile
;
117 static int ch_addbuf();
121 * Get the character pointed to by the read pointer.
122 * ch_get() is a macro which is more efficient to call
123 * than fch_get (the function), in the usual case
124 * that the block desired is at the head of the chain.
126 #define ch_get() ((ch_block == ch_bufhead->block && \
127 ch_offset < ch_bufhead->datasize) ? \
128 ch_bufhead->data[ch_offset] : fch_get())
132 register struct buf
*bp
;
139 if (thisfile
== NULL
)
145 * Look for a buffer holding the desired block.
147 h
= BUFHASH(ch_block
);
148 FOR_BUFS_IN_CHAIN(h
, bp
)
150 if (bp
->block
== ch_block
)
152 if (ch_offset
>= bp
->datasize
)
154 * Need more data in this buffer.
161 * Block is not in a buffer.
162 * Take the least recently used buffer
163 * and read the desired block into it.
164 * If the LRU buffer has data in it,
165 * then maybe allocate a new buffer.
167 if (ch_buftail
== END_OF_CHAIN
|| ch_buftail
->block
!= -1)
170 * There is no empty buffer to use.
171 * Allocate a new buffer if:
172 * 1. We can't seek on this file and -b is not in effect; or
173 * 2. We haven't allocated the max buffers for this file yet.
175 if ((autobuf
&& !(ch_flags
& CH_CANSEEK
)) ||
176 (maxbufs
< 0 || ch_nbufs
< maxbufs
))
179 * Allocation failed: turn off autobuf.
184 HASH_RM(bp
); /* Remove from old hash chain. */
185 bp
->block
= ch_block
;
187 HASH_INS(bp
, h
); /* Insert into new hash chain. */
190 pos
= (ch_block
* LBUFSIZE
) + bp
->datasize
;
191 if ((len
= ch_length()) != NULL_POSITION
&& pos
>= len
)
200 * Not at the correct position: must seek.
201 * If input is a pipe, we're in trouble (can't seek on a pipe).
202 * Some data has been lost: just return "?".
204 if (!(ch_flags
& CH_CANSEEK
))
206 if (lseek(ch_file
, (off_t
)pos
, SEEK_SET
) == BAD_LSEEK
)
208 error("seek error", NULL_PARG
);
217 * If we read less than a full block, that's ok.
218 * We use partial block and pick up the rest next time.
220 if (ch_ungotchar
!= -1)
222 bp
->data
[bp
->datasize
] = ch_ungotchar
;
225 } else if (ch_flags
& CH_HELPFILE
)
227 bp
->data
[bp
->datasize
] = helpdata
[ch_fpos
];
231 n
= iread(ch_file
, &bp
->data
[bp
->datasize
],
232 (unsigned int)(LBUFSIZE
- bp
->datasize
));
239 #if MSDOS_COMPILER==WIN32C
243 error("read error", NULL_PARG
);
251 * If we have a log file, write the new data to it.
253 if (!secure
&& logfile
>= 0 && n
> 0)
254 write(logfile
, (char *) &bp
->data
[bp
->datasize
], n
);
261 * If we have read to end of file, set ch_fsize to indicate
262 * the position of the end of file.
270 * We are ignoring EOF.
271 * Wait a while, then try again.
276 parg
.p_string
= wait_message();
282 #if MSDOS_COMPILER==WIN32C
289 if (follow_mode
== FOLLOW_NAME
)
291 /* See whether the file's i-number has changed.
292 * If so, force the file to be closed and
295 int r
= stat(get_filename(curr_ifile
), &st
);
296 if (r
== 0 && (st
.st_ino
!= curr_ino
||
297 st
.st_dev
!= curr_dev
))
299 /* screen_trashed=2 causes
300 * make_display to reopen the file. */
312 if (ch_bufhead
!= bp
)
315 * Move the buffer to the head of the buffer chain.
316 * This orders the buffer chain, most- to least-recently used.
318 bp
->next
->prev
= bp
->prev
;
319 bp
->prev
->next
= bp
->next
;
320 bp
->next
= ch_bufhead
;
321 bp
->prev
= END_OF_CHAIN
;
322 ch_bufhead
->prev
= bp
;
326 * Move to head of hash chain too.
332 if (ch_offset
>= bp
->datasize
)
334 * After all that, we still don't have enough data.
335 * Go back and try again.
339 return (bp
->data
[ch_offset
]);
343 * ch_ungetchar is a rather kludgy and limited way to push
344 * a single char onto an input file descriptor.
350 if (c
!= -1 && ch_ungotchar
!= -1)
351 error("ch_ungetchar overrun", NULL_PARG
);
358 * If we haven't read all of standard input into it, do that now.
363 static int tried
= FALSE
;
367 if (!tried
&& ch_fsize
== NULL_POSITION
)
370 ierror("Finishing logfile", NULL_PARG
);
371 while (ch_forw_get() != EOI
)
381 * Start a log file AFTER less has already been running.
382 * Invoked from the - command; see toggle_option().
383 * Write all the existing buffered data to the log file.
388 register struct buf
*bp
;
393 nblocks
= (ch_fpos
+ LBUFSIZE
- 1) / LBUFSIZE
;
394 for (block
= 0; block
< nblocks
; block
++)
396 for (bp
= ch_bufhead
; ; bp
= bp
->next
)
398 if (bp
== END_OF_CHAIN
)
402 error("Warning: log file is incomplete",
408 if (bp
->block
== block
)
410 write(logfile
, (char *) bp
->data
, bp
->datasize
);
420 * Determine if a specific block is currently in one of the buffers.
426 register struct buf
*bp
;
430 FOR_BUFS_IN_CHAIN(h
, bp
)
432 if (bp
->block
== block
)
439 * Seek to a specified position in the file.
440 * Return 0 if successful, non-zero if can't seek there.
444 register POSITION pos
;
449 if (thisfile
== NULL
)
453 if (pos
< ch_zero() || (len
!= NULL_POSITION
&& pos
> len
))
456 new_block
= pos
/ LBUFSIZE
;
457 if (!(ch_flags
& CH_CANSEEK
) && pos
!= ch_fpos
&& !buffered(new_block
))
461 while (ch_fpos
< pos
)
463 if (ch_forw_get() == EOI
)
473 ch_block
= new_block
;
474 ch_offset
= pos
% LBUFSIZE
;
479 * Seek to the end of the file.
486 if (thisfile
== NULL
)
489 if (ch_flags
& CH_CANSEEK
)
490 ch_fsize
= filesize(ch_file
);
493 if (len
!= NULL_POSITION
)
494 return (ch_seek(len
));
497 * Do it the slow way: read till end of data.
499 while (ch_forw_get() != EOI
)
506 * Seek to the beginning of the file, or as close to it as we can get.
507 * We may not be able to seek there if input is a pipe and the
508 * beginning of the pipe is no longer buffered.
513 register struct buf
*bp
, *firstbp
;
516 * Try a plain ch_seek first.
518 if (ch_seek(ch_zero()) == 0)
522 * Can't get to position 0.
523 * Look thru the buffers for the one closest to position 0.
525 firstbp
= bp
= ch_bufhead
;
526 if (bp
== END_OF_CHAIN
)
528 while ((bp
= bp
->next
) != END_OF_CHAIN
)
529 if (bp
->block
< firstbp
->block
)
531 ch_block
= firstbp
->block
;
537 * Return the length of the file, if known.
542 if (thisfile
== NULL
)
543 return (NULL_POSITION
);
545 return (NULL_POSITION
);
546 if (ch_flags
& CH_HELPFILE
)
547 return (size_helpdata
);
552 * Return the current position in the file.
557 if (thisfile
== NULL
)
558 return (NULL_POSITION
);
559 return (ch_block
* LBUFSIZE
) + ch_offset
;
563 * Get the current char and post-increment the read pointer.
570 if (thisfile
== NULL
)
575 if (ch_offset
< LBUFSIZE
-1)
586 * Pre-decrement the read pointer and get the new current char.
591 if (thisfile
== NULL
)
599 if (!(ch_flags
& CH_CANSEEK
) && !buffered(ch_block
-1))
602 ch_offset
= LBUFSIZE
-1;
608 * Set max amount of buffer space.
609 * bufspace is in units of 1024 bytes. -1 mean no limit.
612 ch_setbufspace(bufspace
)
619 maxbufs
= ((bufspace
* 1024) + LBUFSIZE
-1) / LBUFSIZE
;
626 * Flush (discard) any saved file state, including buffer contents.
631 register struct buf
*bp
;
633 if (thisfile
== NULL
)
636 if (!(ch_flags
& CH_CANSEEK
))
639 * If input is a pipe, we don't flush buffer contents,
640 * since the contents can't be recovered.
642 ch_fsize
= NULL_POSITION
;
647 * Initialize all the buffers.
649 for (bp
= ch_bufhead
; bp
!= END_OF_CHAIN
; bp
= bp
->next
)
653 * Figure out the size of the file, if we can.
655 ch_fsize
= filesize(ch_file
);
658 * Seek to a known position: the beginning of the file.
661 ch_block
= 0; /* ch_fpos / LBUFSIZE; */
662 ch_offset
= 0; /* ch_fpos % LBUFSIZE; */
666 * This is a kludge to workaround a Linux kernel bug: files in
667 * /proc have a size of 0 according to fstat() but have readable
668 * data. They are sometimes, but not always, seekable.
669 * Force them to be non-seekable here.
673 ch_fsize
= NULL_POSITION
;
674 ch_flags
&= ~CH_CANSEEK
;
678 if (lseek(ch_file
, (off_t
)0, SEEK_SET
) == BAD_LSEEK
)
681 * Warning only; even if the seek fails for some reason,
682 * there's a good chance we're at the beginning anyway.
683 * {{ I think this is bogus reasoning. }}
685 error("seek error to 0", NULL_PARG
);
690 * Allocate a new buffer.
691 * The buffer is added to the tail of the buffer chain.
696 register struct buf
*bp
;
699 * Allocate and initialize a new buffer and link it
700 * onto the tail of the buffer list.
702 bp
= (struct buf
*) calloc(1, sizeof(struct buf
));
707 bp
->next
= END_OF_CHAIN
;
708 bp
->prev
= ch_buftail
;
709 ch_buftail
->next
= bp
;
723 for (h
= 0; h
< BUFHASH_SIZE
; h
++)
725 thisfile
->hashtbl
[h
].buf_hnext
= END_OF_HCHAIN(h
);
726 thisfile
->hashtbl
[h
].buf_hprev
= END_OF_HCHAIN(h
);
731 * Delete all buffers for this file.
736 register struct buf
*bp
;
738 while (ch_bufhead
!= END_OF_CHAIN
)
741 bp
->next
->prev
= bp
->prev
;
742 bp
->prev
->next
= bp
->next
;
750 * Is it possible to seek on a file descriptor?
758 if (f
== fd0
&& !isatty(fd0
))
761 * In MS-DOS, pipes are seekable. Check for
762 * standard input, and pretend it is not seekable.
767 return (lseek(f
, (off_t
)1, SEEK_SET
) != BAD_LSEEK
);
771 * Initialize file state for a new file.
779 * See if we already have a filestate for this file.
781 thisfile
= (struct filestate
*) get_filestate(curr_ifile
);
782 if (thisfile
== NULL
)
785 * Allocate and initialize a new filestate.
787 thisfile
= (struct filestate
*)
788 calloc(1, sizeof(struct filestate
));
789 thisfile
->buf_next
= thisfile
->buf_prev
= END_OF_CHAIN
;
794 thisfile
->offset
= 0;
796 thisfile
->fsize
= NULL_POSITION
;
800 * Try to seek; set CH_CANSEEK if it works.
802 if ((flags
& CH_CANSEEK
) && !seekable(f
))
803 ch_flags
&= ~CH_CANSEEK
;
804 set_filestate(curr_ifile
, (void *) thisfile
);
806 if (thisfile
->file
== -1)
817 int keepstate
= FALSE
;
819 if (thisfile
== NULL
)
822 if (ch_flags
& (CH_CANSEEK
|CH_POPENED
|CH_HELPFILE
))
825 * We can seek or re-open, so we don't need to keep buffers.
830 if (!(ch_flags
& CH_KEEPOPEN
))
833 * We don't need to keep the file descriptor open
834 * (because we can re-open it.)
835 * But don't really close it if it was opened via popen(),
836 * because pclose() wants to close it.
838 if (!(ch_flags
& (CH_POPENED
|CH_HELPFILE
)))
846 * We don't even need to keep the filestate structure.
850 set_filestate(curr_ifile
, (void *) NULL
);
855 * Return ch_flags for the current file.
860 if (thisfile
== NULL
)
867 ch_dump(struct filestate
*fs
)
874 printf(" --no filestate\n");
877 printf(" file %d, flags %x, fpos %x, fsize %x, blk/off %x/%x\n",
878 fs
->file
, fs
->flags
, fs
->fpos
,
879 fs
->fsize
, fs
->block
, fs
->offset
);
880 printf(" %d bufs:\n", fs
->nbufs
);
881 for (bp
= fs
->buf_next
; bp
!= (struct buf
*)fs
; bp
= bp
->next
)
883 printf("%x: blk %x, size %x \"",
884 bp
, bp
->block
, bp
->datasize
);
885 for (s
= bp
->data
; s
< bp
->data
+ 30; s
++)
886 if (*s
>= ' ' && *s
< 0x7F)