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