2 * Copyright (C) 1984-2012 Mark Nudelman
3 * Modified for use with illumos by Garrett D'Amore.
4 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, 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.
22 extern dev_t curr_dev
;
23 extern ino_t curr_ino
;
24 extern int less_is_more
;
26 typedef off_t BLOCKNUM
;
31 * Pool of buffers holding the most recently used blocks of the input file.
32 * The buffer pool is kept as a doubly-linked circular list,
33 * in order from most- to least-recently used.
34 * The circular list is anchored by the file state "thisfile".
37 struct bufnode
*next
, *prev
;
38 struct bufnode
*hnext
, *hprev
;
45 unsigned int datasize
;
46 unsigned char data
[LBUFSIZE
];
48 #define bufnode_buf(bn) ((struct buf *)bn)
51 * The file state is maintained in a filestate structure.
52 * A pointer to the filestate is kept in the ifile structure.
54 #define BUFHASH_SIZE 64
56 struct bufnode buflist
;
57 struct bufnode hashtbl
[BUFHASH_SIZE
];
67 #define ch_bufhead thisfile->buflist.next
68 #define ch_buftail thisfile->buflist.prev
69 #define ch_nbufs thisfile->nbufs
70 #define ch_block thisfile->block
71 #define ch_offset thisfile->offset
72 #define ch_fpos thisfile->fpos
73 #define ch_fsize thisfile->fsize
74 #define ch_flags thisfile->flags
75 #define ch_file thisfile->file
77 #define END_OF_CHAIN (&thisfile->buflist)
78 #define END_OF_HCHAIN(h) (&thisfile->hashtbl[h])
79 #define BUFHASH(blk) ((blk) & (BUFHASH_SIZE-1))
82 * Macros to manipulate the list of buffers in thisfile->buflist.
84 #define FOR_BUFS(bn) \
85 for ((bn) = ch_bufhead; (bn) != END_OF_CHAIN; (bn) = (bn)->next)
88 (bn)->next->prev = (bn)->prev; \
89 (bn)->prev->next = (bn)->next;
91 #define BUF_INS_HEAD(bn) \
92 (bn)->next = ch_bufhead; \
93 (bn)->prev = END_OF_CHAIN; \
94 ch_bufhead->prev = (bn); \
97 #define BUF_INS_TAIL(bn) \
98 (bn)->next = END_OF_CHAIN; \
99 (bn)->prev = ch_buftail; \
100 ch_buftail->next = (bn); \
104 * Macros to manipulate the list of buffers in thisfile->hashtbl[n].
106 #define FOR_BUFS_IN_CHAIN(h, bn) \
107 for ((bn) = thisfile->hashtbl[h].hnext; \
108 (bn) != END_OF_HCHAIN(h); (bn) = (bn)->hnext)
110 #define BUF_HASH_RM(bn) \
111 (bn)->hnext->hprev = (bn)->hprev; \
112 (bn)->hprev->hnext = (bn)->hnext;
114 #define BUF_HASH_INS(bn, h) \
115 (bn)->hnext = thisfile->hashtbl[h].hnext; \
116 (bn)->hprev = END_OF_HCHAIN(h); \
117 thisfile->hashtbl[h].hnext->hprev = (bn); \
118 thisfile->hashtbl[h].hnext = (bn);
120 static struct filestate
*thisfile
;
121 static int ch_ungotchar
= -1;
122 static int maxbufs
= -1;
125 extern volatile sig_atomic_t sigs
;
127 extern int screen_trashed
;
128 extern int follow_mode
;
129 extern IFILE curr_ifile
;
131 extern char *namelogfile
;
133 static int ch_addbuf(void);
137 * Get the character pointed to by the read pointer.
150 if (thisfile
== NULL
)
154 * Quick check for the common case where
155 * the desired char is in the head buffer.
157 if (ch_bufhead
!= END_OF_CHAIN
) {
158 bp
= bufnode_buf(ch_bufhead
);
159 if (ch_block
== bp
->block
&& ch_offset
< bp
->datasize
)
160 return (bp
->data
[ch_offset
]);
166 * Look for a buffer holding the desired block.
168 h
= BUFHASH(ch_block
);
169 FOR_BUFS_IN_CHAIN(h
, bn
) {
170 bp
= bufnode_buf(bn
);
171 if (bp
->block
== ch_block
) {
172 if (ch_offset
>= bp
->datasize
)
174 * Need more data in this buffer.
180 if (bn
== END_OF_HCHAIN(h
)) {
182 * Block is not in a buffer.
183 * Take the least recently used buffer
184 * and read the desired block into it.
185 * If the LRU buffer has data in it,
186 * then maybe allocate a new buffer.
188 if (ch_buftail
== END_OF_CHAIN
||
189 bufnode_buf(ch_buftail
)->block
!= -1) {
191 * There is no empty buffer to use.
192 * Allocate a new buffer if:
193 * 1. We can't seek on this file and -b is not in
195 * 2. We haven't allocated the max buffers for this
198 if ((autobuf
&& !(ch_flags
& CH_CANSEEK
)) ||
199 (maxbufs
< 0 || ch_nbufs
< maxbufs
))
202 * Allocation failed: turn off autobuf.
207 bp
= bufnode_buf(bn
);
208 BUF_HASH_RM(bn
); /* Remove from old hash chain. */
209 bp
->block
= ch_block
;
211 BUF_HASH_INS(bn
, h
); /* Insert into new hash chain. */
215 pos
= (ch_block
* LBUFSIZE
) + bp
->datasize
;
216 if ((len
= ch_length()) != -1 && pos
>= len
)
222 if (pos
!= ch_fpos
) {
224 * Not at the correct position: must seek.
225 * If input is a pipe, we're in trouble (can't seek on a pipe).
226 * Some data has been lost: just return "?".
228 if (!(ch_flags
& CH_CANSEEK
))
230 if (lseek(ch_file
, (off_t
)pos
, SEEK_SET
) == (off_t
)-1) {
231 error("seek error", NULL
);
240 * If we read less than a full block, that's ok.
241 * We use partial block and pick up the rest next time.
243 if (ch_ungotchar
!= -1) {
244 bp
->data
[bp
->datasize
] = (unsigned char)ch_ungotchar
;
248 n
= iread(ch_file
, &bp
->data
[bp
->datasize
],
249 (unsigned int)(LBUFSIZE
- bp
->datasize
));
255 error("read error", NULL
);
261 * If we have a log file, write the new data to it.
263 if (!secure
&& logfile
>= 0 && n
> 0)
264 (void) write(logfile
, (char *)&bp
->data
[bp
->datasize
], n
);
270 * If we have read to end of file, set ch_fsize to indicate
271 * the position of the end of file.
277 * We are ignoring EOF.
278 * Wait a while, then try again.
282 parg
.p_string
= wait_message();
288 if (follow_mode
== FOLLOW_NAME
) {
290 * See whether the file's i-number has changed.
291 * 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
!= bn
) {
314 * Move the buffer to the head of the buffer chain.
315 * This orders the buffer chain, most- to least-recently used.
321 * Move to head of hash chain too.
327 if (ch_offset
>= bp
->datasize
)
329 * After all that, we still don't have enough data.
330 * Go back and try again.
334 return (bp
->data
[ch_offset
]);
338 * ch_ungetchar is a rather kludgy and limited way to push
339 * a single char onto an input file descriptor.
344 if (c
!= -1 && ch_ungotchar
!= -1)
345 error("ch_ungetchar overrun", NULL
);
351 * If we haven't read all of standard input into it, do that now.
356 static int tried
= FALSE
;
360 if (!tried
&& ch_fsize
== -1) {
362 ierror("Finishing logfile", NULL
);
363 while (ch_forw_get() != EOI
)
373 * Start a log file AFTER less has already been running.
374 * Invoked from the - command; see toggle_option().
375 * Write all the existing buffered data to the log file.
386 nblocks
= (ch_fpos
+ LBUFSIZE
- 1) / LBUFSIZE
;
387 for (block
= 0; block
< nblocks
; block
++) {
390 bp
= bufnode_buf(bn
);
391 if (bp
->block
== block
) {
392 (void) write(logfile
, (char *)bp
->data
,
398 if (!wrote
&& !warned
) {
399 error("Warning: log file is incomplete", NULL
);
406 * Determine if a specific block is currently in one of the buffers.
409 buffered(BLOCKNUM block
)
416 FOR_BUFS_IN_CHAIN(h
, bn
) {
417 bp
= bufnode_buf(bn
);
418 if (bp
->block
== block
)
425 * Seek to a specified position in the file.
426 * Return 0 if successful, non-zero if can't seek there.
434 if (thisfile
== NULL
)
438 if (pos
< ch_zero() || (len
!= -1 && pos
> len
))
441 new_block
= pos
/ LBUFSIZE
;
442 if (!(ch_flags
& CH_CANSEEK
) && pos
!= ch_fpos
&&
443 !buffered(new_block
)) {
446 while (ch_fpos
< pos
) {
447 if (ch_forw_get() == EOI
)
457 ch_block
= new_block
;
458 ch_offset
= pos
% LBUFSIZE
;
463 * Seek to the end of the file.
470 if (thisfile
== NULL
)
473 if (ch_flags
& CH_CANSEEK
)
474 ch_fsize
= filesize(ch_file
);
478 return (ch_seek(len
));
481 * Do it the slow way: read till end of data.
483 while (ch_forw_get() != EOI
)
490 * Seek to the beginning of the file, or as close to it as we can get.
491 * We may not be able to seek there if input is a pipe and the
492 * beginning of the pipe is no longer buffered.
498 struct bufnode
*firstbn
;
501 * Try a plain ch_seek first.
503 if (ch_seek(ch_zero()) == 0)
507 * Can't get to position 0.
508 * Look thru the buffers for the one closest to position 0.
510 firstbn
= ch_bufhead
;
511 if (firstbn
== END_OF_CHAIN
)
514 if (bufnode_buf(bn
)->block
< bufnode_buf(firstbn
)->block
)
517 ch_block
= bufnode_buf(firstbn
)->block
;
523 * Return the length of the file, if known.
528 if (thisfile
== NULL
)
532 if (ch_flags
& CH_NODATA
)
538 * Return the current position in the file.
543 if (thisfile
== NULL
)
545 return ((ch_block
* LBUFSIZE
) + ch_offset
);
549 * Get the current char and post-increment the read pointer.
556 if (thisfile
== NULL
)
561 if (ch_offset
< LBUFSIZE
-1) {
571 * Pre-decrement the read pointer and get the new current char.
576 if (thisfile
== NULL
)
583 if (!(ch_flags
& CH_CANSEEK
) && !buffered(ch_block
-1))
586 ch_offset
= LBUFSIZE
-1;
592 * Set max amount of buffer space.
593 * bufspace is in units of 1024 bytes. -1 mean no limit.
596 ch_setbufspace(int bufspace
)
601 maxbufs
= ((bufspace
* 1024) + LBUFSIZE
-1) / LBUFSIZE
;
608 * Flush (discard) any saved file state, including buffer contents.
615 if (thisfile
== NULL
)
618 if (!(ch_flags
& CH_CANSEEK
)) {
620 * If input is a pipe, we don't flush buffer contents,
621 * since the contents can't be recovered.
628 * Initialize all the buffers.
631 bufnode_buf(bn
)->block
= -1;
635 * Figure out the size of the file, if we can.
637 ch_fsize
= filesize(ch_file
);
640 * Seek to a known position: the beginning of the file.
643 ch_block
= 0; /* ch_fpos / LBUFSIZE; */
644 ch_offset
= 0; /* ch_fpos % LBUFSIZE; */
648 * This is a kludge to workaround a Linux kernel bug: files in
649 * /proc have a size of 0 according to fstat() but have readable
650 * data. They are sometimes, but not always, seekable.
651 * Force them to be non-seekable here.
655 ch_flags
&= ~CH_CANSEEK
;
659 if (lseek(ch_file
, (off_t
)0, SEEK_SET
) == (off_t
)-1) {
661 * Warning only; even if the seek fails for some reason,
662 * there's a good chance we're at the beginning anyway.
663 * {{ I think this is bogus reasoning. }}
665 error("seek error to 0", NULL
);
670 * Allocate a new buffer.
671 * The buffer is added to the tail of the buffer chain.
680 * Allocate and initialize a new buffer and link it
681 * onto the tail of the buffer list.
683 bp
= calloc(1, sizeof (struct buf
));
703 for (h
= 0; h
< BUFHASH_SIZE
; h
++) {
704 thisfile
->hashtbl
[h
].hnext
= END_OF_HCHAIN(h
);
705 thisfile
->hashtbl
[h
].hprev
= END_OF_HCHAIN(h
);
710 * Delete all buffers for this file.
717 while (ch_bufhead
!= END_OF_CHAIN
) {
720 free(bufnode_buf(bn
));
727 * Is it possible to seek on a file descriptor?
732 return (lseek(f
, (off_t
)1, SEEK_SET
) != (off_t
)-1);
736 * Force EOF to be at the current read position.
737 * This is used after an ignore_eof read, during which the EOF may change.
747 * Initialize file state for a new file.
750 ch_init(int f
, int flags
)
753 * See if we already have a filestate for this file.
755 thisfile
= get_filestate(curr_ifile
);
756 if (thisfile
== NULL
) {
758 * Allocate and initialize a new filestate.
760 thisfile
= calloc(1, sizeof (struct filestate
));
761 thisfile
->buflist
.next
= thisfile
->buflist
.prev
= END_OF_CHAIN
;
766 thisfile
->offset
= 0;
768 thisfile
->fsize
= -1;
772 * Try to seek; set CH_CANSEEK if it works.
774 if ((flags
& CH_CANSEEK
) && !seekable(f
))
775 ch_flags
&= ~CH_CANSEEK
;
776 set_filestate(curr_ifile
, (void *) thisfile
);
778 if (thisfile
->file
== -1)
789 int keepstate
= FALSE
;
791 if (thisfile
== NULL
)
794 if (ch_flags
& (CH_CANSEEK
|CH_POPENED
|CH_HELPFILE
)) {
796 * We can seek or re-open, so we don't need to keep buffers.
802 if (!(ch_flags
& CH_KEEPOPEN
)) {
804 * We don't need to keep the file descriptor open
805 * (because we can re-open it.)
806 * But don't really close it if it was opened via popen(),
807 * because pclose() wants to close it.
809 if (!(ch_flags
& CH_POPENED
))
817 * We don't even need to keep the filestate structure.
821 set_filestate(curr_ifile
, NULL
);
826 * Return ch_flags for the current file.
831 if (thisfile
== NULL
)