Home | History | Annotate | Line # | Download | only in more
      1  1.6       agc /*	$NetBSD: ch.c,v 1.6 2003/10/13 14:34:25 agc Exp $	*/
      2  1.2     perry 
      3  1.1       cjs /*
      4  1.6       agc  * Copyright (c) 1988 Mark Nudelman
      5  1.1       cjs  * Copyright (c) 1988, 1993
      6  1.1       cjs  *	The Regents of the University of California.  All rights reserved.
      7  1.1       cjs  *
      8  1.1       cjs  * Redistribution and use in source and binary forms, with or without
      9  1.1       cjs  * modification, are permitted provided that the following conditions
     10  1.1       cjs  * are met:
     11  1.1       cjs  * 1. Redistributions of source code must retain the above copyright
     12  1.1       cjs  *    notice, this list of conditions and the following disclaimer.
     13  1.1       cjs  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1       cjs  *    notice, this list of conditions and the following disclaimer in the
     15  1.1       cjs  *    documentation and/or other materials provided with the distribution.
     16  1.5       agc  * 3. Neither the name of the University nor the names of its contributors
     17  1.5       agc  *    may be used to endorse or promote products derived from this software
     18  1.5       agc  *    without specific prior written permission.
     19  1.5       agc  *
     20  1.5       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21  1.5       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  1.5       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  1.5       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24  1.5       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  1.5       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  1.5       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  1.5       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  1.5       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  1.5       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  1.5       agc  * SUCH DAMAGE.
     31  1.5       agc  */
     32  1.5       agc 
     33  1.3  christos #include <sys/cdefs.h>
     34  1.1       cjs #ifndef lint
     35  1.3  christos #if 0
     36  1.1       cjs static char sccsid[] = "@(#)ch.c	8.1 (Berkeley) 6/6/93";
     37  1.3  christos #else
     38  1.6       agc __RCSID("$NetBSD: ch.c,v 1.6 2003/10/13 14:34:25 agc Exp $");
     39  1.3  christos #endif
     40  1.1       cjs #endif /* not lint */
     41  1.1       cjs 
     42  1.1       cjs /*
     43  1.1       cjs  * Low level character input from the input file.
     44  1.1       cjs  * We use these special purpose routines which optimize moving
     45  1.1       cjs  * both forward and backward from the current read pointer.
     46  1.1       cjs  */
     47  1.1       cjs 
     48  1.1       cjs #include <sys/types.h>
     49  1.1       cjs #include <sys/file.h>
     50  1.1       cjs #include <unistd.h>
     51  1.3  christos #include <stdlib.h>
     52  1.1       cjs #include <stdio.h>
     53  1.3  christos #include <err.h>
     54  1.3  christos 
     55  1.3  christos #include "less.h"
     56  1.3  christos #include "extern.h"
     57  1.1       cjs 
     58  1.1       cjs int file = -1;		/* File descriptor of the input file */
     59  1.1       cjs 
     60  1.1       cjs /*
     61  1.1       cjs  * Pool of buffers holding the most recently used blocks of the input file.
     62  1.1       cjs  */
     63  1.1       cjs struct buf {
     64  1.1       cjs 	struct buf *next, *prev;
     65  1.1       cjs 	long block;
     66  1.1       cjs 	int datasize;
     67  1.1       cjs 	char data[BUFSIZ];
     68  1.1       cjs };
     69  1.1       cjs int nbufs;
     70  1.1       cjs 
     71  1.1       cjs /*
     72  1.1       cjs  * The buffer pool is kept as a doubly-linked circular list, in order from
     73  1.1       cjs  * most- to least-recently used.  The circular list is anchored by buf_anchor.
     74  1.1       cjs  */
     75  1.1       cjs #define	END_OF_CHAIN	((struct buf *)&buf_anchor)
     76  1.1       cjs #define	buf_head	buf_anchor.next
     77  1.1       cjs #define	buf_tail	buf_anchor.prev
     78  1.1       cjs 
     79  1.1       cjs static struct {
     80  1.1       cjs 	struct buf *next, *prev;
     81  1.1       cjs } buf_anchor = { END_OF_CHAIN, END_OF_CHAIN };
     82  1.1       cjs 
     83  1.1       cjs /*
     84  1.1       cjs  * Current position in file.
     85  1.1       cjs  * Stored as a block number and an offset into the block.
     86  1.1       cjs  */
     87  1.1       cjs static long ch_block;
     88  1.1       cjs static int ch_offset;
     89  1.1       cjs 
     90  1.1       cjs /* Length of file, needed if input is a pipe. */
     91  1.1       cjs static off_t ch_fsize;
     92  1.1       cjs 
     93  1.1       cjs /* Number of bytes read, if input is standard input (a pipe). */
     94  1.1       cjs static off_t last_piped_pos;
     95  1.1       cjs 
     96  1.1       cjs /*
     97  1.1       cjs  * Get the character pointed to by the read pointer.  ch_get() is a macro
     98  1.1       cjs  * which is more efficient to call than fch_get (the function), in the usual
     99  1.1       cjs  * case that the block desired is at the head of the chain.
    100  1.1       cjs  */
    101  1.1       cjs #define	ch_get() \
    102  1.1       cjs 	((buf_head->block == ch_block && \
    103  1.1       cjs 	    ch_offset < buf_head->datasize) ? \
    104  1.1       cjs 	    buf_head->data[ch_offset] : fch_get())
    105  1.1       cjs 
    106  1.3  christos static int fch_get __P((void));
    107  1.3  christos static int buffered __P((long));
    108  1.3  christos 
    109  1.3  christos static int
    110  1.1       cjs fch_get()
    111  1.1       cjs {
    112  1.3  christos 	struct buf *bp;
    113  1.3  christos 	int n, ch;
    114  1.3  christos 	char *p, *t;
    115  1.3  christos 	off_t pos;
    116  1.1       cjs 
    117  1.1       cjs 	/* look for a buffer holding the desired block. */
    118  1.1       cjs 	for (bp = buf_head;  bp != END_OF_CHAIN;  bp = bp->next)
    119  1.1       cjs 		if (bp->block == ch_block) {
    120  1.1       cjs 			if (ch_offset >= bp->datasize)
    121  1.1       cjs 				/*
    122  1.1       cjs 				 * Need more data in this buffer.
    123  1.1       cjs 				 */
    124  1.1       cjs 				goto read_more;
    125  1.1       cjs 			/*
    126  1.1       cjs 			 * On a pipe, we don't sort the buffers LRU
    127  1.1       cjs 			 * because this can cause gaps in the buffers.
    128  1.1       cjs 			 * For example, suppose we've got 12 1K buffers,
    129  1.1       cjs 			 * and a 15K input stream.  If we read the first 12K
    130  1.1       cjs 			 * sequentially, then jump to line 1, then jump to
    131  1.1       cjs 			 * the end, the buffers have blocks 0,4,5,6,..,14.
    132  1.1       cjs 			 * If we then jump to line 1 again and try to
    133  1.1       cjs 			 * read sequentially, we're out of luck when we
    134  1.1       cjs 			 * get to block 1 (we'd get the "pipe error" below).
    135  1.1       cjs 			 * To avoid this, we only sort buffers on a pipe
    136  1.1       cjs 			 * when we actually READ the data, not when we
    137  1.1       cjs 			 * find it already buffered.
    138  1.1       cjs 			 */
    139  1.1       cjs 			if (ispipe)
    140  1.1       cjs 				return(bp->data[ch_offset]);
    141  1.1       cjs 			goto found;
    142  1.1       cjs 		}
    143  1.1       cjs 	/*
    144  1.1       cjs 	 * Block is not in a buffer.  Take the least recently used buffer
    145  1.1       cjs 	 * and read the desired block into it.  If the LRU buffer has data
    146  1.1       cjs 	 * in it, and input is a pipe, then try to allocate a new buffer first.
    147  1.1       cjs 	 */
    148  1.1       cjs 	if (ispipe && buf_tail->block != (long)(-1))
    149  1.1       cjs 		(void)ch_addbuf(1);
    150  1.1       cjs 	bp = buf_tail;
    151  1.1       cjs 	bp->block = ch_block;
    152  1.1       cjs 	bp->datasize = 0;
    153  1.1       cjs 
    154  1.1       cjs read_more:
    155  1.1       cjs 	pos = (ch_block * BUFSIZ) + bp->datasize;
    156  1.1       cjs 	if (ispipe) {
    157  1.1       cjs 		/*
    158  1.1       cjs 		 * The data requested should be immediately after
    159  1.1       cjs 		 * the last data read from the pipe.
    160  1.1       cjs 		 */
    161  1.1       cjs 		if (pos != last_piped_pos) {
    162  1.1       cjs 			error("pipe error");
    163  1.1       cjs 			quit();
    164  1.1       cjs 		}
    165  1.1       cjs 	} else
    166  1.1       cjs 		(void)lseek(file, pos, L_SET);
    167  1.1       cjs 
    168  1.1       cjs 	/*
    169  1.1       cjs 	 * Read the block.
    170  1.1       cjs 	 * If we read less than a full block, we just return the
    171  1.1       cjs 	 * partial block and pick up the rest next time.
    172  1.1       cjs 	 */
    173  1.1       cjs 	n = iread(file, &bp->data[bp->datasize], BUFSIZ - bp->datasize);
    174  1.1       cjs 	if (n == READ_INTR)
    175  1.1       cjs 		return (EOI);
    176  1.1       cjs 	if (n < 0) {
    177  1.1       cjs 		error("read error");
    178  1.1       cjs 		quit();
    179  1.1       cjs 	}
    180  1.1       cjs 	if (ispipe)
    181  1.1       cjs 		last_piped_pos += n;
    182  1.1       cjs 
    183  1.1       cjs 	p = &bp->data[bp->datasize];
    184  1.1       cjs 	bp->datasize += n;
    185  1.1       cjs 
    186  1.1       cjs 	/*
    187  1.1       cjs 	 * Set an EOI marker in the buffered data itself.  Then ensure the
    188  1.1       cjs 	 * data is "clean": there are no extra EOI chars in the data and
    189  1.1       cjs 	 * that the "meta" bit (the 0200 bit) is reset in each char;
    190  1.1       cjs 	 * also translate \r\n sequences to \n if -u flag not set.
    191  1.1       cjs 	 */
    192  1.1       cjs 	if (n == 0) {
    193  1.1       cjs 		ch_fsize = pos;
    194  1.1       cjs 		bp->data[bp->datasize++] = EOI;
    195  1.1       cjs 	}
    196  1.1       cjs 
    197  1.1       cjs 	if (bs_mode) {
    198  1.1       cjs 		for (p = &bp->data[bp->datasize]; --n >= 0;) {
    199  1.1       cjs 			*--p &= 0177;
    200  1.1       cjs 			if (*p == EOI)
    201  1.1       cjs 				*p = 0200;
    202  1.1       cjs 		}
    203  1.1       cjs 	}
    204  1.1       cjs 	else {
    205  1.1       cjs 		for (t = p; --n >= 0; ++p) {
    206  1.1       cjs 			ch = *p & 0177;
    207  1.1       cjs 			if (ch == '\r' && n && (p[1] & 0177) == '\n') {
    208  1.1       cjs 				++p;
    209  1.1       cjs 				*t++ = '\n';
    210  1.1       cjs 			}
    211  1.1       cjs 			else
    212  1.1       cjs 				*t++ = (ch == EOI) ? 0200 : ch;
    213  1.1       cjs 		}
    214  1.1       cjs 		if (p != t) {
    215  1.1       cjs 			bp->datasize -= p - t;
    216  1.1       cjs 			if (ispipe)
    217  1.1       cjs 				last_piped_pos -= p - t;
    218  1.1       cjs 		}
    219  1.1       cjs 	}
    220  1.1       cjs 
    221  1.1       cjs found:
    222  1.1       cjs 	if (buf_head != bp) {
    223  1.1       cjs 		/*
    224  1.1       cjs 		 * Move the buffer to the head of the buffer chain.
    225  1.1       cjs 		 * This orders the buffer chain, most- to least-recently used.
    226  1.1       cjs 		 */
    227  1.1       cjs 		bp->next->prev = bp->prev;
    228  1.1       cjs 		bp->prev->next = bp->next;
    229  1.1       cjs 
    230  1.1       cjs 		bp->next = buf_head;
    231  1.1       cjs 		bp->prev = END_OF_CHAIN;
    232  1.1       cjs 		buf_head->prev = bp;
    233  1.1       cjs 		buf_head = bp;
    234  1.1       cjs 	}
    235  1.1       cjs 
    236  1.1       cjs 	if (ch_offset >= bp->datasize)
    237  1.1       cjs 		/*
    238  1.1       cjs 		 * After all that, we still don't have enough data.
    239  1.1       cjs 		 * Go back and try again.
    240  1.1       cjs 		 */
    241  1.1       cjs 		goto read_more;
    242  1.1       cjs 
    243  1.1       cjs 	return(bp->data[ch_offset]);
    244  1.1       cjs }
    245  1.1       cjs 
    246  1.1       cjs /*
    247  1.1       cjs  * Determine if a specific block is currently in one of the buffers.
    248  1.1       cjs  */
    249  1.3  christos static int
    250  1.1       cjs buffered(block)
    251  1.1       cjs 	long block;
    252  1.1       cjs {
    253  1.3  christos 	struct buf *bp;
    254  1.1       cjs 
    255  1.1       cjs 	for (bp = buf_head; bp != END_OF_CHAIN; bp = bp->next)
    256  1.1       cjs 		if (bp->block == block)
    257  1.1       cjs 			return(1);
    258  1.1       cjs 	return(0);
    259  1.1       cjs }
    260  1.1       cjs 
    261  1.1       cjs /*
    262  1.1       cjs  * Seek to a specified position in the file.
    263  1.1       cjs  * Return 0 if successful, non-zero if can't seek there.
    264  1.1       cjs  */
    265  1.3  christos int
    266  1.1       cjs ch_seek(pos)
    267  1.3  christos 	off_t pos;
    268  1.1       cjs {
    269  1.1       cjs 	long new_block;
    270  1.1       cjs 
    271  1.1       cjs 	new_block = pos / BUFSIZ;
    272  1.1       cjs 	if (!ispipe || pos == last_piped_pos || buffered(new_block)) {
    273  1.1       cjs 		/*
    274  1.1       cjs 		 * Set read pointer.
    275  1.1       cjs 		 */
    276  1.1       cjs 		ch_block = new_block;
    277  1.1       cjs 		ch_offset = pos % BUFSIZ;
    278  1.1       cjs 		return(0);
    279  1.1       cjs 	}
    280  1.1       cjs 	return(1);
    281  1.1       cjs }
    282  1.1       cjs 
    283  1.1       cjs /*
    284  1.1       cjs  * Seek to the end of the file.
    285  1.1       cjs  */
    286  1.3  christos int
    287  1.1       cjs ch_end_seek()
    288  1.1       cjs {
    289  1.1       cjs 	if (!ispipe)
    290  1.1       cjs 		return(ch_seek(ch_length()));
    291  1.1       cjs 
    292  1.1       cjs 	/*
    293  1.1       cjs 	 * Do it the slow way: read till end of data.
    294  1.1       cjs 	 */
    295  1.1       cjs 	while (ch_forw_get() != EOI)
    296  1.1       cjs 		if (sigs)
    297  1.1       cjs 			return(1);
    298  1.1       cjs 	return(0);
    299  1.1       cjs }
    300  1.1       cjs 
    301  1.1       cjs /*
    302  1.1       cjs  * Seek to the beginning of the file, or as close to it as we can get.
    303  1.1       cjs  * We may not be able to seek there if input is a pipe and the
    304  1.1       cjs  * beginning of the pipe is no longer buffered.
    305  1.1       cjs  */
    306  1.3  christos int
    307  1.1       cjs ch_beg_seek()
    308  1.1       cjs {
    309  1.3  christos 	struct buf *bp, *firstbp;
    310  1.1       cjs 
    311  1.1       cjs 	/*
    312  1.1       cjs 	 * Try a plain ch_seek first.
    313  1.1       cjs 	 */
    314  1.1       cjs 	if (ch_seek((off_t)0) == 0)
    315  1.1       cjs 		return(0);
    316  1.1       cjs 
    317  1.1       cjs 	/*
    318  1.1       cjs 	 * Can't get to position 0.
    319  1.1       cjs 	 * Look thru the buffers for the one closest to position 0.
    320  1.1       cjs 	 */
    321  1.1       cjs 	firstbp = bp = buf_head;
    322  1.1       cjs 	if (bp == END_OF_CHAIN)
    323  1.1       cjs 		return(1);
    324  1.1       cjs 	while ((bp = bp->next) != END_OF_CHAIN)
    325  1.1       cjs 		if (bp->block < firstbp->block)
    326  1.1       cjs 			firstbp = bp;
    327  1.1       cjs 	ch_block = firstbp->block;
    328  1.1       cjs 	ch_offset = 0;
    329  1.1       cjs 	return(0);
    330  1.1       cjs }
    331  1.1       cjs 
    332  1.1       cjs /*
    333  1.1       cjs  * Return the length of the file, if known.
    334  1.1       cjs  */
    335  1.1       cjs off_t
    336  1.1       cjs ch_length()
    337  1.1       cjs {
    338  1.1       cjs 	if (ispipe)
    339  1.1       cjs 		return(ch_fsize);
    340  1.1       cjs 	return((off_t)(lseek(file, (off_t)0, L_XTND)));
    341  1.1       cjs }
    342  1.1       cjs 
    343  1.1       cjs /*
    344  1.1       cjs  * Return the current position in the file.
    345  1.1       cjs  */
    346  1.1       cjs off_t
    347  1.1       cjs ch_tell()
    348  1.1       cjs {
    349  1.1       cjs 	return(ch_block * BUFSIZ + ch_offset);
    350  1.1       cjs }
    351  1.1       cjs 
    352  1.1       cjs /*
    353  1.1       cjs  * Get the current char and post-increment the read pointer.
    354  1.1       cjs  */
    355  1.3  christos int
    356  1.1       cjs ch_forw_get()
    357  1.1       cjs {
    358  1.3  christos 	int c;
    359  1.1       cjs 
    360  1.1       cjs 	c = ch_get();
    361  1.1       cjs 	if (c != EOI && ++ch_offset >= BUFSIZ) {
    362  1.1       cjs 		ch_offset = 0;
    363  1.1       cjs 		++ch_block;
    364  1.1       cjs 	}
    365  1.1       cjs 	return(c);
    366  1.1       cjs }
    367  1.1       cjs 
    368  1.1       cjs /*
    369  1.1       cjs  * Pre-decrement the read pointer and get the new current char.
    370  1.1       cjs  */
    371  1.3  christos int
    372  1.1       cjs ch_back_get()
    373  1.1       cjs {
    374  1.1       cjs 	if (--ch_offset < 0) {
    375  1.1       cjs 		if (ch_block <= 0 || (ispipe && !buffered(ch_block-1))) {
    376  1.1       cjs 			ch_offset = 0;
    377  1.1       cjs 			return(EOI);
    378  1.1       cjs 		}
    379  1.1       cjs 		ch_offset = BUFSIZ - 1;
    380  1.1       cjs 		ch_block--;
    381  1.1       cjs 	}
    382  1.1       cjs 	return(ch_get());
    383  1.1       cjs }
    384  1.1       cjs 
    385  1.1       cjs /*
    386  1.1       cjs  * Allocate buffers.
    387  1.1       cjs  * Caller wants us to have a total of at least want_nbufs buffers.
    388  1.1       cjs  * keep==1 means keep the data in the current buffers;
    389  1.1       cjs  * otherwise discard the old data.
    390  1.1       cjs  */
    391  1.3  christos void
    392  1.1       cjs ch_init(want_nbufs, keep)
    393  1.1       cjs 	int want_nbufs;
    394  1.1       cjs 	int keep;
    395  1.1       cjs {
    396  1.3  christos 	struct buf *bp;
    397  1.1       cjs 	char message[80];
    398  1.1       cjs 
    399  1.1       cjs 	cbufs = nbufs;
    400  1.1       cjs 	if (nbufs < want_nbufs && ch_addbuf(want_nbufs - nbufs)) {
    401  1.1       cjs 		/*
    402  1.1       cjs 		 * Cannot allocate enough buffers.
    403  1.1       cjs 		 * If we don't have ANY, then quit.
    404  1.1       cjs 		 * Otherwise, just report the error and return.
    405  1.1       cjs 		 */
    406  1.4    itojun 		(void)snprintf(message, sizeof(message),
    407  1.4    itojun 		    "cannot allocate %d buffers", want_nbufs - nbufs);
    408  1.1       cjs 		error(message);
    409  1.1       cjs 		if (nbufs == 0)
    410  1.1       cjs 			quit();
    411  1.1       cjs 		return;
    412  1.1       cjs 	}
    413  1.1       cjs 
    414  1.1       cjs 	if (keep)
    415  1.1       cjs 		return;
    416  1.1       cjs 
    417  1.1       cjs 	/*
    418  1.1       cjs 	 * We don't want to keep the old data,
    419  1.1       cjs 	 * so initialize all the buffers now.
    420  1.1       cjs 	 */
    421  1.1       cjs 	for (bp = buf_head;  bp != END_OF_CHAIN;  bp = bp->next)
    422  1.1       cjs 		bp->block = (long)(-1);
    423  1.1       cjs 	last_piped_pos = (off_t)0;
    424  1.1       cjs 	ch_fsize = NULL_POSITION;
    425  1.1       cjs 	(void)ch_seek((off_t)0);
    426  1.1       cjs }
    427  1.1       cjs 
    428  1.1       cjs /*
    429  1.1       cjs  * Allocate some new buffers.
    430  1.1       cjs  * The buffers are added to the tail of the buffer chain.
    431  1.1       cjs  */
    432  1.3  christos int
    433  1.1       cjs ch_addbuf(nnew)
    434  1.1       cjs 	int nnew;
    435  1.1       cjs {
    436  1.3  christos 	struct buf *bp;
    437  1.3  christos 	struct buf *newbufs;
    438  1.1       cjs 
    439  1.1       cjs 	/*
    440  1.1       cjs 	 * We don't have enough buffers.
    441  1.1       cjs 	 * Allocate some new ones.
    442  1.1       cjs 	 */
    443  1.1       cjs 	newbufs = (struct buf *)calloc((u_int)nnew, sizeof(struct buf));
    444  1.1       cjs 	if (newbufs == NULL)
    445  1.1       cjs 		return(1);
    446  1.1       cjs 
    447  1.1       cjs 	/*
    448  1.1       cjs 	 * Initialize the new buffers and link them together.
    449  1.1       cjs 	 * Link them all onto the tail of the buffer list.
    450  1.1       cjs 	 */
    451  1.1       cjs 	nbufs += nnew;
    452  1.1       cjs 	cbufs = nbufs;
    453  1.1       cjs 	for (bp = &newbufs[0];  bp < &newbufs[nnew];  bp++) {
    454  1.1       cjs 		bp->next = bp + 1;
    455  1.1       cjs 		bp->prev = bp - 1;
    456  1.1       cjs 		bp->block = (long)(-1);
    457  1.1       cjs 	}
    458  1.1       cjs 	newbufs[nnew-1].next = END_OF_CHAIN;
    459  1.1       cjs 	newbufs[0].prev = buf_tail;
    460  1.1       cjs 	buf_tail->next = &newbufs[0];
    461  1.1       cjs 	buf_tail = &newbufs[nnew-1];
    462  1.1       cjs 	return(0);
    463  1.1       cjs }
    464