1 /* $NetBSD: ch.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
4 * Copyright (C) 1984-2012 Mark Nudelman
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.
14 * Low level character input from the input file.
15 * We use these special purpose routines which optimize moving
16 * both forward and backward from the current read pointer.
20 #if MSDOS_COMPILER==WIN32C
27 extern dev_t curr_dev
;
28 extern ino_t curr_ino
;
31 typedef POSITION BLOCKNUM
;
33 public int ignore_eoi
;
36 * Pool of buffers holding the most recently used blocks of the input file.
37 * The buffer pool is kept as a doubly-linked circular list,
38 * in order from most- to least-recently used.
39 * The circular list is anchored by the file state "thisfile".
42 struct bufnode
*next
, *prev
;
43 struct bufnode
*hnext
, *hprev
;
50 unsigned int datasize
;
51 unsigned char data
[LBUFSIZE
];
53 #define bufnode_buf(bn) ((struct buf *) bn)
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 bufnode buflist
;
62 struct bufnode hashtbl
[BUFHASH_SIZE
];
72 #define ch_bufhead thisfile->buflist.next
73 #define ch_buftail thisfile->buflist.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 (&thisfile->buflist)
83 #define END_OF_HCHAIN(h) (&thisfile->hashtbl[h])
84 #define BUFHASH(blk) ((blk) & (BUFHASH_SIZE-1))
87 * Macros to manipulate the list of buffers in thisfile->buflist.
89 #define FOR_BUFS(bn) \
90 for (bn = ch_bufhead; bn != END_OF_CHAIN; bn = bn->next)
93 (bn)->next->prev = (bn)->prev; \
94 (bn)->prev->next = (bn)->next;
96 #define BUF_INS_HEAD(bn) \
97 (bn)->next = ch_bufhead; \
98 (bn)->prev = END_OF_CHAIN; \
99 ch_bufhead->prev = (bn); \
102 #define BUF_INS_TAIL(bn) \
103 (bn)->next = END_OF_CHAIN; \
104 (bn)->prev = ch_buftail; \
105 ch_buftail->next = (bn); \
109 * Macros to manipulate the list of buffers in thisfile->hashtbl[n].
111 #define FOR_BUFS_IN_CHAIN(h,bn) \
112 for (bn = thisfile->hashtbl[h].hnext; \
113 bn != END_OF_HCHAIN(h); bn = bn->hnext)
115 #define BUF_HASH_RM(bn) \
116 (bn)->hnext->hprev = (bn)->hprev; \
117 (bn)->hprev->hnext = (bn)->hnext;
119 #define BUF_HASH_INS(bn,h) \
120 (bn)->hnext = thisfile->hashtbl[h].hnext; \
121 (bn)->hprev = END_OF_HCHAIN(h); \
122 thisfile->hashtbl[h].hnext->hprev = (bn); \
123 thisfile->hashtbl[h].hnext = (bn);
125 static struct filestate
*thisfile
;
126 static int ch_ungotchar
= -1;
127 static int maxbufs
= -1;
132 extern int screen_trashed
;
133 extern int follow_mode
;
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
__P((void));
143 static int buffered
__P((BLOCKNUM
));
144 static void ch_delbufs
__P((void));
148 * Get the character pointed to by the read pointer.
153 register struct buf
*bp
;
154 register struct bufnode
*bn
;
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();
321 #if MSDOS_COMPILER==WIN32C
328 if (follow_mode
== FOLLOW_NAME
)
330 /* See whether the file's i-number has changed.
331 * If so, force the file to be closed and
334 int r
= stat(get_filename(curr_ifile
), &st
);
335 if (r
== 0 && (st
.st_ino
!= curr_ino
||
336 st
.st_dev
!= curr_dev
))
338 /* screen_trashed=2 causes
339 * make_display to reopen the file. */
351 if (ch_bufhead
!= bn
)
354 * Move the buffer to the head of the buffer chain.
355 * This orders the buffer chain, most- to least-recently used.
361 * Move to head of hash chain too.
367 if (ch_offset
>= bp
->datasize
)
369 * After all that, we still don't have enough data.
370 * Go back and try again.
374 return (bp
->data
[ch_offset
]);
378 * ch_ungetchar is a rather kludgy and limited way to push
379 * a single char onto an input file descriptor.
385 if (c
!= -1 && ch_ungotchar
!= -1)
386 error("ch_ungetchar overrun", NULL_PARG
);
393 * If we haven't read all of standard input into it, do that now.
398 static int tried
= FALSE
;
402 if (!tried
&& ch_fsize
== NULL_POSITION
)
405 ierror("Finishing logfile", NULL_PARG
);
406 while (ch_forw_get() != EOI
)
416 * Start a log file AFTER less has already been running.
417 * Invoked from the - command; see toggle_option().
418 * Write all the existing buffered data to the log file.
423 register struct buf
*bp
;
424 register struct bufnode
*bn
;
429 nblocks
= (ch_fpos
+ LBUFSIZE
- 1) / LBUFSIZE
;
430 for (block
= 0; block
< nblocks
; block
++)
435 bp
= bufnode_buf(bn
);
436 if (bp
->block
== block
)
438 write(logfile
, (char *) bp
->data
, bp
->datasize
);
443 if (!wrote
&& !warned
)
445 error("Warning: log file is incomplete",
455 * Determine if a specific block is currently in one of the buffers.
461 register struct buf
*bp
;
462 register struct bufnode
*bn
;
466 FOR_BUFS_IN_CHAIN(h
, bn
)
468 bp
= bufnode_buf(bn
);
469 if (bp
->block
== block
)
476 * Seek to a specified position in the file.
477 * Return 0 if successful, non-zero if can't seek there.
481 register POSITION pos
;
486 if (thisfile
== NULL
)
490 if (pos
< ch_zero() || (len
!= NULL_POSITION
&& pos
> len
))
493 new_block
= pos
/ LBUFSIZE
;
494 if (!(ch_flags
& CH_CANSEEK
) && pos
!= ch_fpos
&& !buffered(new_block
))
498 while (ch_fpos
< pos
)
500 if (ch_forw_get() == EOI
)
510 ch_block
= new_block
;
511 ch_offset
= pos
% LBUFSIZE
;
516 * Seek to the end of the file.
523 if (thisfile
== NULL
)
526 if (ch_flags
& CH_CANSEEK
)
527 ch_fsize
= filesize(ch_file
);
530 if (len
!= NULL_POSITION
)
531 return (ch_seek(len
));
534 * Do it the slow way: read till end of data.
536 while (ch_forw_get() != EOI
)
543 * Seek to the beginning of the file, or as close to it as we can get.
544 * We may not be able to seek there if input is a pipe and the
545 * beginning of the pipe is no longer buffered.
550 register struct bufnode
*bn
;
551 register struct bufnode
*firstbn
;
554 * Try a plain ch_seek first.
556 if (ch_seek(ch_zero()) == 0)
560 * Can't get to position 0.
561 * Look thru the buffers for the one closest to position 0.
563 firstbn
= ch_bufhead
;
564 if (firstbn
== END_OF_CHAIN
)
568 if (bufnode_buf(bn
)->block
< bufnode_buf(firstbn
)->block
)
571 ch_block
= bufnode_buf(firstbn
)->block
;
577 * Return the length of the file, if known.
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.
599 if (thisfile
== NULL
)
600 return (NULL_POSITION
);
601 return (ch_block
* LBUFSIZE
) + ch_offset
;
605 * Get the current char and post-increment the read pointer.
612 if (thisfile
== NULL
)
617 if (ch_offset
< LBUFSIZE
-1)
628 * Pre-decrement the read pointer and get the new current char.
633 if (thisfile
== NULL
)
641 if (!(ch_flags
& CH_CANSEEK
) && !buffered(ch_block
-1))
644 ch_offset
= LBUFSIZE
-1;
650 * Set max amount of buffer space.
651 * bufspace is in units of 1024 bytes. -1 mean no limit.
654 ch_setbufspace(bufspace
)
661 maxbufs
= ((bufspace
* 1024) + LBUFSIZE
-1) / LBUFSIZE
;
668 * Flush (discard) any saved file state, including buffer contents.
673 register struct bufnode
*bn
;
675 if (thisfile
== NULL
)
678 if (!(ch_flags
& CH_CANSEEK
))
681 * If input is a pipe, we don't flush buffer contents,
682 * since the contents can't be recovered.
684 ch_fsize
= NULL_POSITION
;
689 * Initialize all the buffers.
693 bufnode_buf(bn
)->block
= -1;
697 * Figure out the size of the file, if we can.
699 ch_fsize
= filesize(ch_file
);
702 * Seek to a known position: the beginning of the file.
705 ch_block
= 0; /* ch_fpos / LBUFSIZE; */
706 ch_offset
= 0; /* ch_fpos % LBUFSIZE; */
710 * This is a kludge to workaround a Linux kernel bug: files in
711 * /proc have a size of 0 according to fstat() but have readable
712 * data. They are sometimes, but not always, seekable.
713 * Force them to be non-seekable here.
717 ch_fsize
= NULL_POSITION
;
718 ch_flags
&= ~CH_CANSEEK
;
722 if (lseek(ch_file
, (off_t
)0, SEEK_SET
) == BAD_LSEEK
)
725 * Warning only; even if the seek fails for some reason,
726 * there's a good chance we're at the beginning anyway.
727 * {{ I think this is bogus reasoning. }}
729 error("seek error to 0", NULL_PARG
);
734 * Allocate a new buffer.
735 * The buffer is added to the tail of the buffer chain.
740 register struct buf
*bp
;
741 register struct bufnode
*bn
;
744 * Allocate and initialize a new buffer and link it
745 * onto the tail of the buffer list.
747 bp
= (struct buf
*) calloc(1, sizeof(struct buf
));
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.
780 register struct bufnode
*bn
;
782 while (ch_bufhead
!= END_OF_CHAIN
)
786 free(bufnode_buf(bn
));
793 * Is it possible to seek on a file descriptor?
801 if (f
== fd0
&& !isatty(fd0
))
804 * In MS-DOS, pipes are seekable. Check for
805 * standard input, and pretend it is not seekable.
810 return (lseek(f
, (off_t
)1, SEEK_SET
) != BAD_LSEEK
);
814 * Force EOF to be at the current read position.
815 * This is used after an ignore_eof read, during which the EOF may change.
825 * Initialize file state for a new file.
833 * See if we already have a filestate for this file.
835 thisfile
= (struct filestate
*) get_filestate(curr_ifile
);
836 if (thisfile
== NULL
)
839 * Allocate and initialize a new filestate.
841 thisfile
= (struct filestate
*)
842 calloc(1, sizeof(struct filestate
));
843 thisfile
->buflist
.next
= thisfile
->buflist
.prev
= END_OF_CHAIN
;
848 thisfile
->offset
= 0;
850 thisfile
->fsize
= NULL_POSITION
;
854 * Try to seek; set CH_CANSEEK if it works.
856 if ((flags
& CH_CANSEEK
) && !seekable(f
))
857 ch_flags
&= ~CH_CANSEEK
;
858 set_filestate(curr_ifile
, (void *) thisfile
);
860 if (thisfile
->file
== -1)
871 int keepstate
= FALSE
;
873 if (thisfile
== NULL
)
876 if (ch_flags
& (CH_CANSEEK
|CH_POPENED
|CH_HELPFILE
))
879 * We can seek or re-open, so we don't need to keep buffers.
884 if (!(ch_flags
& CH_KEEPOPEN
))
887 * We don't need to keep the file descriptor open
888 * (because we can re-open it.)
889 * But don't really close it if it was opened via popen(),
890 * because pclose() wants to close it.
892 if (!(ch_flags
& (CH_POPENED
|CH_HELPFILE
)))
900 * We don't even need to keep the filestate structure.
904 set_filestate(curr_ifile
, (void *) NULL
);
909 * Return ch_flags for the current file.
914 if (thisfile
== NULL
)
921 ch_dump(struct filestate
*fs
)
929 printf(" --no filestate\n");
932 printf(" file %d, flags %x, fpos %x, fsize %x, blk/off %x/%x\n",
933 fs
->file
, fs
->flags
, fs
->fpos
,
934 fs
->fsize
, fs
->block
, fs
->offset
);
935 printf(" %d bufs:\n", fs
->nbufs
);
936 for (bn
= fs
->next
; bn
!= &fs
->buflist
; bn
= bn
->next
)
938 bp
= bufnode_buf(bn
);
939 printf("%x: blk %x, size %x \"",
940 bp
, bp
->block
, bp
->datasize
);
941 for (s
= bp
->data
; s
< bp
->data
+ 30; s
++)
942 if (*s
>= ' ' && *s
< 0x7F)