Home | History | Annotate | Line # | Download | only in dist
ch.c revision 1.1.1.2
      1 /*	$NetBSD: ch.c,v 1.1.1.2 2013/09/04 19:35:03 tron Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1984-2012  Mark Nudelman
      5  *
      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.
      8  *
      9  * For more information, see the README file.
     10  */
     11 
     12 
     13 /*
     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.
     17  */
     18 
     19 #include "less.h"
     20 #if MSDOS_COMPILER==WIN32C
     21 #include <errno.h>
     22 #include <windows.h>
     23 #endif
     24 
     25 #if HAVE_STAT_INO
     26 #include <sys/stat.h>
     27 extern dev_t curr_dev;
     28 extern ino_t curr_ino;
     29 #endif
     30 
     31 typedef POSITION BLOCKNUM;
     32 
     33 public int ignore_eoi;
     34 
     35 /*
     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".
     40  */
     41 struct bufnode {
     42 	struct bufnode *next, *prev;
     43 	struct bufnode *hnext, *hprev;
     44 };
     45 
     46 #define	LBUFSIZE	8192
     47 struct buf {
     48 	struct bufnode node;
     49 	BLOCKNUM block;
     50 	unsigned int datasize;
     51 	unsigned char data[LBUFSIZE];
     52 };
     53 #define bufnode_buf(bn)  ((struct buf *) bn)
     54 
     55 /*
     56  * The file state is maintained in a filestate structure.
     57  * A pointer to the filestate is kept in the ifile structure.
     58  */
     59 #define	BUFHASH_SIZE	64
     60 struct filestate {
     61 	struct bufnode buflist;
     62 	struct bufnode hashtbl[BUFHASH_SIZE];
     63 	int file;
     64 	int flags;
     65 	POSITION fpos;
     66 	int nbufs;
     67 	BLOCKNUM block;
     68 	unsigned int offset;
     69 	POSITION fsize;
     70 };
     71 
     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
     81 
     82 #define	END_OF_CHAIN	(&thisfile->buflist)
     83 #define	END_OF_HCHAIN(h) (&thisfile->hashtbl[h])
     84 #define BUFHASH(blk)	((blk) & (BUFHASH_SIZE-1))
     85 
     86 /*
     87  * Macros to manipulate the list of buffers in thisfile->buflist.
     88  */
     89 #define	FOR_BUFS(bn) \
     90 	for (bn = ch_bufhead;  bn != END_OF_CHAIN;  bn = bn->next)
     91 
     92 #define BUF_RM(bn) \
     93 	(bn)->next->prev = (bn)->prev; \
     94 	(bn)->prev->next = (bn)->next;
     95 
     96 #define BUF_INS_HEAD(bn) \
     97 	(bn)->next = ch_bufhead; \
     98 	(bn)->prev = END_OF_CHAIN; \
     99 	ch_bufhead->prev = (bn); \
    100 	ch_bufhead = (bn);
    101 
    102 #define BUF_INS_TAIL(bn) \
    103 	(bn)->next = END_OF_CHAIN; \
    104 	(bn)->prev = ch_buftail; \
    105 	ch_buftail->next = (bn); \
    106 	ch_buftail = (bn);
    107 
    108 /*
    109  * Macros to manipulate the list of buffers in thisfile->hashtbl[n].
    110  */
    111 #define	FOR_BUFS_IN_CHAIN(h,bn) \
    112 	for (bn = thisfile->hashtbl[h].hnext;  \
    113 	     bn != END_OF_HCHAIN(h);  bn = bn->hnext)
    114 
    115 #define	BUF_HASH_RM(bn) \
    116 	(bn)->hnext->hprev = (bn)->hprev; \
    117 	(bn)->hprev->hnext = (bn)->hnext;
    118 
    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);
    124 
    125 static struct filestate *thisfile;
    126 static int ch_ungotchar = -1;
    127 static int maxbufs = -1;
    128 
    129 extern int autobuf;
    130 extern int sigs;
    131 extern int secure;
    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;
    137 #if LOGFILE
    138 extern int logfile;
    139 extern char *namelogfile;
    140 #endif
    141 
    142 static int ch_addbuf();
    143 
    144 
    145 /*
    146  * Get the character pointed to by the read pointer.
    147  */
    148 	int
    149 ch_get()
    150 {
    151 	register struct buf *bp;
    152 	register struct bufnode *bn;
    153 	register int n;
    154 	register int slept;
    155 	register int h;
    156 	POSITION pos;
    157 	POSITION len;
    158 
    159 	if (thisfile == NULL)
    160 		return (EOI);
    161 
    162 	/*
    163 	 * Quick check for the common case where
    164 	 * the desired char is in the head buffer.
    165 	 */
    166 	if (ch_bufhead != END_OF_CHAIN)
    167 	{
    168 		bp = bufnode_buf(ch_bufhead);
    169 		if (ch_block == bp->block && ch_offset < bp->datasize)
    170 			return bp->data[ch_offset];
    171 	}
    172 
    173 	slept = FALSE;
    174 
    175 	/*
    176 	 * Look for a buffer holding the desired block.
    177 	 */
    178 	h = BUFHASH(ch_block);
    179 	FOR_BUFS_IN_CHAIN(h, bn)
    180 	{
    181 		bp = bufnode_buf(bn);
    182 		if (bp->block == ch_block)
    183 		{
    184 			if (ch_offset >= bp->datasize)
    185 				/*
    186 				 * Need more data in this buffer.
    187 				 */
    188 				break;
    189 			goto found;
    190 		}
    191 	}
    192 	if (bn == END_OF_HCHAIN(h))
    193 	{
    194 		/*
    195 		 * Block is not in a buffer.
    196 		 * Take the least recently used buffer
    197 		 * and read the desired block into it.
    198 		 * If the LRU buffer has data in it,
    199 		 * then maybe allocate a new buffer.
    200 		 */
    201 		if (ch_buftail == END_OF_CHAIN ||
    202 			bufnode_buf(ch_buftail)->block != -1)
    203 		{
    204 			/*
    205 			 * There is no empty buffer to use.
    206 			 * Allocate a new buffer if:
    207 			 * 1. We can't seek on this file and -b is not in effect; or
    208 			 * 2. We haven't allocated the max buffers for this file yet.
    209 			 */
    210 			if ((autobuf && !(ch_flags & CH_CANSEEK)) ||
    211 				(maxbufs < 0 || ch_nbufs < maxbufs))
    212 				if (ch_addbuf())
    213 					/*
    214 					 * Allocation failed: turn off autobuf.
    215 					 */
    216 					autobuf = OPT_OFF;
    217 		}
    218 		bn = ch_buftail;
    219 		bp = bufnode_buf(bn);
    220 		BUF_HASH_RM(bn); /* Remove from old hash chain. */
    221 		bp->block = ch_block;
    222 		bp->datasize = 0;
    223 		BUF_HASH_INS(bn, h); /* Insert into new hash chain. */
    224 	}
    225 
    226     read_more:
    227 	pos = (ch_block * LBUFSIZE) + bp->datasize;
    228 	if ((len = ch_length()) != NULL_POSITION && pos >= len)
    229 		/*
    230 		 * At end of file.
    231 		 */
    232 		return (EOI);
    233 
    234 	if (pos != ch_fpos)
    235 	{
    236 		/*
    237 		 * Not at the correct position: must seek.
    238 		 * If input is a pipe, we're in trouble (can't seek on a pipe).
    239 		 * Some data has been lost: just return "?".
    240 		 */
    241 		if (!(ch_flags & CH_CANSEEK))
    242 			return ('?');
    243 		if (lseek(ch_file, (off_t)pos, SEEK_SET) == BAD_LSEEK)
    244 		{
    245  			error("seek error", NULL_PARG);
    246 			clear_eol();
    247 			return (EOI);
    248  		}
    249  		ch_fpos = pos;
    250  	}
    251 
    252 	/*
    253 	 * Read the block.
    254 	 * If we read less than a full block, that's ok.
    255 	 * We use partial block and pick up the rest next time.
    256 	 */
    257 	if (ch_ungotchar != -1)
    258 	{
    259 		bp->data[bp->datasize] = ch_ungotchar;
    260 		n = 1;
    261 		ch_ungotchar = -1;
    262 	} else if (ch_flags & CH_HELPFILE)
    263 	{
    264 		bp->data[bp->datasize] = helpdata[ch_fpos];
    265 		n = 1;
    266 	} else
    267 	{
    268 		n = iread(ch_file, &bp->data[bp->datasize],
    269 			(unsigned int)(LBUFSIZE - bp->datasize));
    270 	}
    271 
    272 	if (n == READ_INTR)
    273 		return (EOI);
    274 	if (n < 0)
    275 	{
    276 #if MSDOS_COMPILER==WIN32C
    277 		if (errno != EPIPE)
    278 #endif
    279 		{
    280 			error("read error", NULL_PARG);
    281 			clear_eol();
    282 		}
    283 		n = 0;
    284 	}
    285 
    286 #if LOGFILE
    287 	/*
    288 	 * If we have a log file, write the new data to it.
    289 	 */
    290 	if (!secure && logfile >= 0 && n > 0)
    291 		write(logfile, (char *) &bp->data[bp->datasize], n);
    292 #endif
    293 
    294 	ch_fpos += n;
    295 	bp->datasize += n;
    296 
    297 	/*
    298 	 * If we have read to end of file, set ch_fsize to indicate
    299 	 * the position of the end of file.
    300 	 */
    301 	if (n == 0)
    302 	{
    303 		ch_fsize = pos;
    304 		if (ignore_eoi)
    305 		{
    306 			/*
    307 			 * We are ignoring EOF.
    308 			 * Wait a while, then try again.
    309 			 */
    310 			if (!slept)
    311 			{
    312 				PARG parg;
    313 				parg.p_string = wait_message();
    314 				ierror("%s", &parg);
    315 			}
    316 #if !MSDOS_COMPILER
    317 	 		sleep(1);
    318 #else
    319 #if MSDOS_COMPILER==WIN32C
    320 			Sleep(1000);
    321 #endif
    322 #endif
    323 			slept = TRUE;
    324 
    325 #if HAVE_STAT_INO
    326 			if (follow_mode == FOLLOW_NAME)
    327 			{
    328 				/* See whether the file's i-number has changed.
    329 				 * If so, force the file to be closed and
    330 				 * reopened. */
    331 				struct stat st;
    332 				int r = stat(get_filename(curr_ifile), &st);
    333 				if (r == 0 && (st.st_ino != curr_ino ||
    334 					st.st_dev != curr_dev))
    335 				{
    336 					/* screen_trashed=2 causes
    337 					 * make_display to reopen the file. */
    338 					screen_trashed = 2;
    339 					return (EOI);
    340 				}
    341 			}
    342 #endif
    343 		}
    344 		if (sigs)
    345 			return (EOI);
    346 	}
    347 
    348     found:
    349 	if (ch_bufhead != bn)
    350 	{
    351 		/*
    352 		 * Move the buffer to the head of the buffer chain.
    353 		 * This orders the buffer chain, most- to least-recently used.
    354 		 */
    355 		BUF_RM(bn);
    356 		BUF_INS_HEAD(bn);
    357 
    358 		/*
    359 		 * Move to head of hash chain too.
    360 		 */
    361 		BUF_HASH_RM(bn);
    362 		BUF_HASH_INS(bn, h);
    363 	}
    364 
    365 	if (ch_offset >= bp->datasize)
    366 		/*
    367 		 * After all that, we still don't have enough data.
    368 		 * Go back and try again.
    369 		 */
    370 		goto read_more;
    371 
    372 	return (bp->data[ch_offset]);
    373 }
    374 
    375 /*
    376  * ch_ungetchar is a rather kludgy and limited way to push
    377  * a single char onto an input file descriptor.
    378  */
    379 	public void
    380 ch_ungetchar(c)
    381 	int c;
    382 {
    383 	if (c != -1 && ch_ungotchar != -1)
    384 		error("ch_ungetchar overrun", NULL_PARG);
    385 	ch_ungotchar = c;
    386 }
    387 
    388 #if LOGFILE
    389 /*
    390  * Close the logfile.
    391  * If we haven't read all of standard input into it, do that now.
    392  */
    393 	public void
    394 end_logfile()
    395 {
    396 	static int tried = FALSE;
    397 
    398 	if (logfile < 0)
    399 		return;
    400 	if (!tried && ch_fsize == NULL_POSITION)
    401 	{
    402 		tried = TRUE;
    403 		ierror("Finishing logfile", NULL_PARG);
    404 		while (ch_forw_get() != EOI)
    405 			if (ABORT_SIGS())
    406 				break;
    407 	}
    408 	close(logfile);
    409 	logfile = -1;
    410 	namelogfile = NULL;
    411 }
    412 
    413 /*
    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.
    417  */
    418 	public void
    419 sync_logfile()
    420 {
    421 	register struct buf *bp;
    422 	register struct bufnode *bn;
    423 	int warned = FALSE;
    424 	BLOCKNUM block;
    425 	BLOCKNUM nblocks;
    426 
    427 	nblocks = (ch_fpos + LBUFSIZE - 1) / LBUFSIZE;
    428 	for (block = 0;  block < nblocks;  block++)
    429 	{
    430 		int wrote = FALSE;
    431 		FOR_BUFS(bn)
    432 		{
    433 			bp = bufnode_buf(bn);
    434 			if (bp->block == block)
    435 			{
    436 				write(logfile, (char *) bp->data, bp->datasize);
    437 				wrote = TRUE;
    438 				break;
    439 			}
    440 		}
    441 		if (!wrote && !warned)
    442 		{
    443 			error("Warning: log file is incomplete",
    444 				NULL_PARG);
    445 			warned = TRUE;
    446 		}
    447 	}
    448 }
    449 
    450 #endif
    451 
    452 /*
    453  * Determine if a specific block is currently in one of the buffers.
    454  */
    455 	static int
    456 buffered(block)
    457 	BLOCKNUM block;
    458 {
    459 	register struct buf *bp;
    460 	register struct bufnode *bn;
    461 	register int h;
    462 
    463 	h = BUFHASH(block);
    464 	FOR_BUFS_IN_CHAIN(h, bn)
    465 	{
    466 		bp = bufnode_buf(bn);
    467 		if (bp->block == block)
    468 			return (TRUE);
    469 	}
    470 	return (FALSE);
    471 }
    472 
    473 /*
    474  * Seek to a specified position in the file.
    475  * Return 0 if successful, non-zero if can't seek there.
    476  */
    477 	public int
    478 ch_seek(pos)
    479 	register POSITION pos;
    480 {
    481 	BLOCKNUM new_block;
    482 	POSITION len;
    483 
    484 	if (thisfile == NULL)
    485 		return (0);
    486 
    487 	len = ch_length();
    488 	if (pos < ch_zero() || (len != NULL_POSITION && pos > len))
    489 		return (1);
    490 
    491 	new_block = pos / LBUFSIZE;
    492 	if (!(ch_flags & CH_CANSEEK) && pos != ch_fpos && !buffered(new_block))
    493 	{
    494 		if (ch_fpos > pos)
    495 			return (1);
    496 		while (ch_fpos < pos)
    497 		{
    498 			if (ch_forw_get() == EOI)
    499 				return (1);
    500 			if (ABORT_SIGS())
    501 				return (1);
    502 		}
    503 		return (0);
    504 	}
    505 	/*
    506 	 * Set read pointer.
    507 	 */
    508 	ch_block = new_block;
    509 	ch_offset = pos % LBUFSIZE;
    510 	return (0);
    511 }
    512 
    513 /*
    514  * Seek to the end of the file.
    515  */
    516 	public int
    517 ch_end_seek()
    518 {
    519 	POSITION len;
    520 
    521 	if (thisfile == NULL)
    522 		return (0);
    523 
    524 	if (ch_flags & CH_CANSEEK)
    525 		ch_fsize = filesize(ch_file);
    526 
    527 	len = ch_length();
    528 	if (len != NULL_POSITION)
    529 		return (ch_seek(len));
    530 
    531 	/*
    532 	 * Do it the slow way: read till end of data.
    533 	 */
    534 	while (ch_forw_get() != EOI)
    535 		if (ABORT_SIGS())
    536 			return (1);
    537 	return (0);
    538 }
    539 
    540 /*
    541  * Seek to the beginning of the file, or as close to it as we can get.
    542  * We may not be able to seek there if input is a pipe and the
    543  * beginning of the pipe is no longer buffered.
    544  */
    545 	public int
    546 ch_beg_seek()
    547 {
    548 	register struct bufnode *bn;
    549 	register struct bufnode *firstbn;
    550 
    551 	/*
    552 	 * Try a plain ch_seek first.
    553 	 */
    554 	if (ch_seek(ch_zero()) == 0)
    555 		return (0);
    556 
    557 	/*
    558 	 * Can't get to position 0.
    559 	 * Look thru the buffers for the one closest to position 0.
    560 	 */
    561 	firstbn = ch_bufhead;
    562 	if (firstbn == END_OF_CHAIN)
    563 		return (1);
    564 	FOR_BUFS(bn)
    565 	{
    566 		if (bufnode_buf(bn)->block < bufnode_buf(firstbn)->block)
    567 			firstbn = bn;
    568 	}
    569 	ch_block = bufnode_buf(firstbn)->block;
    570 	ch_offset = 0;
    571 	return (0);
    572 }
    573 
    574 /*
    575  * Return the length of the file, if known.
    576  */
    577 	public POSITION
    578 ch_length()
    579 {
    580 	if (thisfile == NULL)
    581 		return (NULL_POSITION);
    582 	if (ignore_eoi)
    583 		return (NULL_POSITION);
    584 	if (ch_flags & CH_HELPFILE)
    585 		return (size_helpdata);
    586 	if (ch_flags & CH_NODATA)
    587 		return (0);
    588 	return (ch_fsize);
    589 }
    590 
    591 /*
    592  * Return the current position in the file.
    593  */
    594 	public POSITION
    595 ch_tell()
    596 {
    597 	if (thisfile == NULL)
    598 		return (NULL_POSITION);
    599 	return (ch_block * LBUFSIZE) + ch_offset;
    600 }
    601 
    602 /*
    603  * Get the current char and post-increment the read pointer.
    604  */
    605 	public int
    606 ch_forw_get()
    607 {
    608 	register int c;
    609 
    610 	if (thisfile == NULL)
    611 		return (EOI);
    612 	c = ch_get();
    613 	if (c == EOI)
    614 		return (EOI);
    615 	if (ch_offset < LBUFSIZE-1)
    616 		ch_offset++;
    617 	else
    618 	{
    619 		ch_block ++;
    620 		ch_offset = 0;
    621 	}
    622 	return (c);
    623 }
    624 
    625 /*
    626  * Pre-decrement the read pointer and get the new current char.
    627  */
    628 	public int
    629 ch_back_get()
    630 {
    631 	if (thisfile == NULL)
    632 		return (EOI);
    633 	if (ch_offset > 0)
    634 		ch_offset --;
    635 	else
    636 	{
    637 		if (ch_block <= 0)
    638 			return (EOI);
    639 		if (!(ch_flags & CH_CANSEEK) && !buffered(ch_block-1))
    640 			return (EOI);
    641 		ch_block--;
    642 		ch_offset = LBUFSIZE-1;
    643 	}
    644 	return (ch_get());
    645 }
    646 
    647 /*
    648  * Set max amount of buffer space.
    649  * bufspace is in units of 1024 bytes.  -1 mean no limit.
    650  */
    651 	public void
    652 ch_setbufspace(bufspace)
    653 	int bufspace;
    654 {
    655 	if (bufspace < 0)
    656 		maxbufs = -1;
    657 	else
    658 	{
    659 		maxbufs = ((bufspace * 1024) + LBUFSIZE-1) / LBUFSIZE;
    660 		if (maxbufs < 1)
    661 			maxbufs = 1;
    662 	}
    663 }
    664 
    665 /*
    666  * Flush (discard) any saved file state, including buffer contents.
    667  */
    668 	public void
    669 ch_flush()
    670 {
    671 	register struct bufnode *bn;
    672 
    673 	if (thisfile == NULL)
    674 		return;
    675 
    676 	if (!(ch_flags & CH_CANSEEK))
    677 	{
    678 		/*
    679 		 * If input is a pipe, we don't flush buffer contents,
    680 		 * since the contents can't be recovered.
    681 		 */
    682 		ch_fsize = NULL_POSITION;
    683 		return;
    684 	}
    685 
    686 	/*
    687 	 * Initialize all the buffers.
    688 	 */
    689 	FOR_BUFS(bn)
    690 	{
    691 		bufnode_buf(bn)->block = -1;
    692 	}
    693 
    694 	/*
    695 	 * Figure out the size of the file, if we can.
    696 	 */
    697 	ch_fsize = filesize(ch_file);
    698 
    699 	/*
    700 	 * Seek to a known position: the beginning of the file.
    701 	 */
    702 	ch_fpos = 0;
    703 	ch_block = 0; /* ch_fpos / LBUFSIZE; */
    704 	ch_offset = 0; /* ch_fpos % LBUFSIZE; */
    705 
    706 #if 1
    707 	/*
    708 	 * This is a kludge to workaround a Linux kernel bug: files in
    709 	 * /proc have a size of 0 according to fstat() but have readable
    710 	 * data.  They are sometimes, but not always, seekable.
    711 	 * Force them to be non-seekable here.
    712 	 */
    713 	if (ch_fsize == 0)
    714 	{
    715 		ch_fsize = NULL_POSITION;
    716 		ch_flags &= ~CH_CANSEEK;
    717 	}
    718 #endif
    719 
    720 	if (lseek(ch_file, (off_t)0, SEEK_SET) == BAD_LSEEK)
    721 	{
    722 		/*
    723 		 * Warning only; even if the seek fails for some reason,
    724 		 * there's a good chance we're at the beginning anyway.
    725 		 * {{ I think this is bogus reasoning. }}
    726 		 */
    727 		error("seek error to 0", NULL_PARG);
    728 	}
    729 }
    730 
    731 /*
    732  * Allocate a new buffer.
    733  * The buffer is added to the tail of the buffer chain.
    734  */
    735 	static int
    736 ch_addbuf()
    737 {
    738 	register struct buf *bp;
    739 	register struct bufnode *bn;
    740 
    741 	/*
    742 	 * Allocate and initialize a new buffer and link it
    743 	 * onto the tail of the buffer list.
    744 	 */
    745 	bp = (struct buf *) calloc(1, sizeof(struct buf));
    746 	if (bp == NULL)
    747 		return (1);
    748 	ch_nbufs++;
    749 	bp->block = -1;
    750 	bn = &bp->node;
    751 
    752 	BUF_INS_TAIL(bn);
    753 	BUF_HASH_INS(bn, 0);
    754 	return (0);
    755 }
    756 
    757 /*
    758  *
    759  */
    760 	static void
    761 init_hashtbl()
    762 {
    763 	register int h;
    764 
    765 	for (h = 0;  h < BUFHASH_SIZE;  h++)
    766 	{
    767 		thisfile->hashtbl[h].hnext = END_OF_HCHAIN(h);
    768 		thisfile->hashtbl[h].hprev = END_OF_HCHAIN(h);
    769 	}
    770 }
    771 
    772 /*
    773  * Delete all buffers for this file.
    774  */
    775 	static void
    776 ch_delbufs()
    777 {
    778 	register struct bufnode *bn;
    779 
    780 	while (ch_bufhead != END_OF_CHAIN)
    781 	{
    782 		bn = ch_bufhead;
    783 		BUF_RM(bn);
    784 		free(bufnode_buf(bn));
    785 	}
    786 	ch_nbufs = 0;
    787 	init_hashtbl();
    788 }
    789 
    790 /*
    791  * Is it possible to seek on a file descriptor?
    792  */
    793 	public int
    794 seekable(f)
    795 	int f;
    796 {
    797 #if MSDOS_COMPILER
    798 	extern int fd0;
    799 	if (f == fd0 && !isatty(fd0))
    800 	{
    801 		/*
    802 		 * In MS-DOS, pipes are seekable.  Check for
    803 		 * standard input, and pretend it is not seekable.
    804 		 */
    805 		return (0);
    806 	}
    807 #endif
    808 	return (lseek(f, (off_t)1, SEEK_SET) != BAD_LSEEK);
    809 }
    810 
    811 /*
    812  * Force EOF to be at the current read position.
    813  * This is used after an ignore_eof read, during which the EOF may change.
    814  */
    815 	public void
    816 ch_set_eof()
    817 {
    818 	ch_fsize = ch_fpos;
    819 }
    820 
    821 
    822 /*
    823  * Initialize file state for a new file.
    824  */
    825 	public void
    826 ch_init(f, flags)
    827 	int f;
    828 	int flags;
    829 {
    830 	/*
    831 	 * See if we already have a filestate for this file.
    832 	 */
    833 	thisfile = (struct filestate *) get_filestate(curr_ifile);
    834 	if (thisfile == NULL)
    835 	{
    836 		/*
    837 		 * Allocate and initialize a new filestate.
    838 		 */
    839 		thisfile = (struct filestate *)
    840 				calloc(1, sizeof(struct filestate));
    841 		thisfile->buflist.next = thisfile->buflist.prev = END_OF_CHAIN;
    842 		thisfile->nbufs = 0;
    843 		thisfile->flags = 0;
    844 		thisfile->fpos = 0;
    845 		thisfile->block = 0;
    846 		thisfile->offset = 0;
    847 		thisfile->file = -1;
    848 		thisfile->fsize = NULL_POSITION;
    849 		ch_flags = flags;
    850 		init_hashtbl();
    851 		/*
    852 		 * Try to seek; set CH_CANSEEK if it works.
    853 		 */
    854 		if ((flags & CH_CANSEEK) && !seekable(f))
    855 			ch_flags &= ~CH_CANSEEK;
    856 		set_filestate(curr_ifile, (void *) thisfile);
    857 	}
    858 	if (thisfile->file == -1)
    859 		thisfile->file = f;
    860 	ch_flush();
    861 }
    862 
    863 /*
    864  * Close a filestate.
    865  */
    866 	public void
    867 ch_close()
    868 {
    869 	int keepstate = FALSE;
    870 
    871 	if (thisfile == NULL)
    872 		return;
    873 
    874 	if (ch_flags & (CH_CANSEEK|CH_POPENED|CH_HELPFILE))
    875 	{
    876 		/*
    877 		 * We can seek or re-open, so we don't need to keep buffers.
    878 		 */
    879 		ch_delbufs();
    880 	} else
    881 		keepstate = TRUE;
    882 	if (!(ch_flags & CH_KEEPOPEN))
    883 	{
    884 		/*
    885 		 * We don't need to keep the file descriptor open
    886 		 * (because we can re-open it.)
    887 		 * But don't really close it if it was opened via popen(),
    888 		 * because pclose() wants to close it.
    889 		 */
    890 		if (!(ch_flags & (CH_POPENED|CH_HELPFILE)))
    891 			close(ch_file);
    892 		ch_file = -1;
    893 	} else
    894 		keepstate = TRUE;
    895 	if (!keepstate)
    896 	{
    897 		/*
    898 		 * We don't even need to keep the filestate structure.
    899 		 */
    900 		free(thisfile);
    901 		thisfile = NULL;
    902 		set_filestate(curr_ifile, (void *) NULL);
    903 	}
    904 }
    905 
    906 /*
    907  * Return ch_flags for the current file.
    908  */
    909 	public int
    910 ch_getflags()
    911 {
    912 	if (thisfile == NULL)
    913 		return (0);
    914 	return (ch_flags);
    915 }
    916 
    917 #if 0
    918 	public void
    919 ch_dump(struct filestate *fs)
    920 {
    921 	struct buf *bp;
    922 	struct bufnode *bn;
    923 	unsigned char *s;
    924 
    925 	if (fs == NULL)
    926 	{
    927 		printf(" --no filestate\n");
    928 		return;
    929 	}
    930 	printf(" file %d, flags %x, fpos %x, fsize %x, blk/off %x/%x\n",
    931 		fs->file, fs->flags, fs->fpos,
    932 		fs->fsize, fs->block, fs->offset);
    933 	printf(" %d bufs:\n", fs->nbufs);
    934 	for (bn = fs->next; bn != &fs->buflist;  bn = bn->next)
    935 	{
    936 		bp = bufnode_buf(bn);
    937 		printf("%x: blk %x, size %x \"",
    938 			bp, bp->block, bp->datasize);
    939 		for (s = bp->data;  s < bp->data + 30;  s++)
    940 			if (*s >= ' ' && *s < 0x7F)
    941 				printf("%c", *s);
    942 			else
    943 				printf(".");
    944 		printf("\"\n");
    945 	}
    946 }
    947 #endif
    948