Home | History | Annotate | Line # | Download | only in more
linenum.c revision 1.3
      1  1.3  christos /*	$NetBSD: linenum.c,v 1.3 1998/02/04 11:08:55 christos Exp $	*/
      2  1.2     perry 
      3  1.1       cjs /*
      4  1.1       cjs  * Copyright (c) 1988 Mark Nudleman
      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.1       cjs  * 3. All advertising materials mentioning features or use of this software
     17  1.1       cjs  *    must display the following acknowledgement:
     18  1.1       cjs  *	This product includes software developed by the University of
     19  1.1       cjs  *	California, Berkeley and its contributors.
     20  1.1       cjs  * 4. Neither the name of the University nor the names of its contributors
     21  1.1       cjs  *    may be used to endorse or promote products derived from this software
     22  1.1       cjs  *    without specific prior written permission.
     23  1.1       cjs  *
     24  1.1       cjs  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1       cjs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1       cjs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1       cjs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1       cjs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1       cjs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1       cjs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1       cjs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1       cjs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1       cjs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1       cjs  * SUCH DAMAGE.
     35  1.1       cjs  */
     36  1.1       cjs 
     37  1.3  christos #include <sys/cdefs.h>
     38  1.1       cjs #ifndef lint
     39  1.3  christos #if 0
     40  1.1       cjs static char sccsid[] = "@(#)linenum.c	8.1 (Berkeley) 6/6/93";
     41  1.3  christos #else
     42  1.3  christos __RCSID("$NetBSD: linenum.c,v 1.3 1998/02/04 11:08:55 christos Exp $");
     43  1.3  christos #endif
     44  1.1       cjs #endif /* not lint */
     45  1.1       cjs 
     46  1.1       cjs /*
     47  1.1       cjs  * Code to handle displaying line numbers.
     48  1.1       cjs  *
     49  1.1       cjs  * Finding the line number of a given file position is rather tricky.
     50  1.1       cjs  * We don't want to just start at the beginning of the file and
     51  1.1       cjs  * count newlines, because that is slow for large files (and also
     52  1.1       cjs  * wouldn't work if we couldn't get to the start of the file; e.g.
     53  1.1       cjs  * if input is a long pipe).
     54  1.1       cjs  *
     55  1.1       cjs  * So we use the function add_lnum to cache line numbers.
     56  1.1       cjs  * We try to be very clever and keep only the more interesting
     57  1.1       cjs  * line numbers when we run out of space in our table.  A line
     58  1.1       cjs  * number is more interesting than another when it is far from
     59  1.1       cjs  * other line numbers.   For example, we'd rather keep lines
     60  1.1       cjs  * 100,200,300 than 100,101,300.  200 is more interesting than
     61  1.1       cjs  * 101 because 101 can be derived very cheaply from 100, while
     62  1.1       cjs  * 200 is more expensive to derive from 100.
     63  1.1       cjs  *
     64  1.1       cjs  * The function currline() returns the line number of a given
     65  1.1       cjs  * position in the file.  As a side effect, it calls add_lnum
     66  1.1       cjs  * to cache the line number.  Therefore currline is occasionally
     67  1.1       cjs  * called to make sure we cache line numbers often enough.
     68  1.1       cjs  */
     69  1.1       cjs 
     70  1.1       cjs #include <sys/types.h>
     71  1.1       cjs #include <stdio.h>
     72  1.3  christos #include <time.h>
     73  1.3  christos 
     74  1.3  christos #include "less.h"
     75  1.3  christos #include "extern.h"
     76  1.1       cjs 
     77  1.1       cjs /*
     78  1.1       cjs  * Structure to keep track of a line number and the associated file position.
     79  1.1       cjs  * A doubly-linked circular list of line numbers is kept ordered by line number.
     80  1.1       cjs  */
     81  1.1       cjs struct linenum
     82  1.1       cjs {
     83  1.1       cjs 	struct linenum *next;		/* Link to next in the list */
     84  1.1       cjs 	struct linenum *prev;		/* Line to previous in the list */
     85  1.1       cjs 	off_t pos;			/* File position */
     86  1.1       cjs 	off_t gap;			/* Gap between prev and next */
     87  1.1       cjs 	int line;			/* Line number */
     88  1.1       cjs };
     89  1.1       cjs /*
     90  1.1       cjs  * "gap" needs some explanation: the gap of any particular line number
     91  1.1       cjs  * is the distance between the previous one and the next one in the list.
     92  1.1       cjs  * ("Distance" means difference in file position.)  In other words, the
     93  1.1       cjs  * gap of a line number is the gap which would be introduced if this
     94  1.1       cjs  * line number were deleted.  It is used to decide which one to replace
     95  1.1       cjs  * when we have a new one to insert and the table is full.
     96  1.1       cjs  */
     97  1.1       cjs 
     98  1.1       cjs #define	NPOOL	50			/* Size of line number pool */
     99  1.1       cjs 
    100  1.1       cjs #define	LONGTIME	(2)		/* In seconds */
    101  1.1       cjs 
    102  1.1       cjs int lnloop = 0;				/* Are we in the line num loop? */
    103  1.1       cjs 
    104  1.1       cjs static struct linenum anchor;		/* Anchor of the list */
    105  1.1       cjs static struct linenum *freelist;	/* Anchor of the unused entries */
    106  1.1       cjs static struct linenum pool[NPOOL];	/* The pool itself */
    107  1.1       cjs static struct linenum *spare;		/* We always keep one spare entry */
    108  1.1       cjs 
    109  1.3  christos static void calcgap __P((struct linenum *));
    110  1.3  christos static void longloopmessage __P((void));
    111  1.1       cjs /*
    112  1.1       cjs  * Initialize the line number structures.
    113  1.1       cjs  */
    114  1.3  christos void
    115  1.1       cjs clr_linenum()
    116  1.1       cjs {
    117  1.3  christos 	struct linenum *p;
    118  1.1       cjs 
    119  1.1       cjs 	/*
    120  1.1       cjs 	 * Put all the entries on the free list.
    121  1.1       cjs 	 * Leave one for the "spare".
    122  1.1       cjs 	 */
    123  1.1       cjs 	for (p = pool;  p < &pool[NPOOL-2];  p++)
    124  1.1       cjs 		p->next = p+1;
    125  1.1       cjs 	pool[NPOOL-2].next = NULL;
    126  1.1       cjs 	freelist = pool;
    127  1.1       cjs 
    128  1.1       cjs 	spare = &pool[NPOOL-1];
    129  1.1       cjs 
    130  1.1       cjs 	/*
    131  1.1       cjs 	 * Initialize the anchor.
    132  1.1       cjs 	 */
    133  1.1       cjs 	anchor.next = anchor.prev = &anchor;
    134  1.1       cjs 	anchor.gap = 0;
    135  1.1       cjs 	anchor.pos = (off_t)0;
    136  1.1       cjs 	anchor.line = 1;
    137  1.1       cjs }
    138  1.1       cjs 
    139  1.1       cjs /*
    140  1.1       cjs  * Calculate the gap for an entry.
    141  1.1       cjs  */
    142  1.3  christos static void
    143  1.1       cjs calcgap(p)
    144  1.3  christos 	struct linenum *p;
    145  1.1       cjs {
    146  1.1       cjs 	/*
    147  1.1       cjs 	 * Don't bother to compute a gap for the anchor.
    148  1.1       cjs 	 * Also don't compute a gap for the last one in the list.
    149  1.1       cjs 	 * The gap for that last one should be considered infinite,
    150  1.1       cjs 	 * but we never look at it anyway.
    151  1.1       cjs 	 */
    152  1.1       cjs 	if (p == &anchor || p->next == &anchor)
    153  1.1       cjs 		return;
    154  1.1       cjs 	p->gap = p->next->pos - p->prev->pos;
    155  1.1       cjs }
    156  1.1       cjs 
    157  1.1       cjs /*
    158  1.1       cjs  * Add a new line number to the cache.
    159  1.1       cjs  * The specified position (pos) should be the file position of the
    160  1.1       cjs  * FIRST character in the specified line.
    161  1.1       cjs  */
    162  1.3  christos void
    163  1.1       cjs add_lnum(line, pos)
    164  1.1       cjs 	int line;
    165  1.1       cjs 	off_t pos;
    166  1.1       cjs {
    167  1.3  christos 	struct linenum *p;
    168  1.3  christos 	struct linenum *new;
    169  1.3  christos 	struct linenum *nextp;
    170  1.3  christos 	struct linenum *prevp;
    171  1.3  christos 	off_t mingap;
    172  1.1       cjs 
    173  1.1       cjs 	/*
    174  1.1       cjs 	 * Find the proper place in the list for the new one.
    175  1.1       cjs 	 * The entries are sorted by position.
    176  1.1       cjs 	 */
    177  1.1       cjs 	for (p = anchor.next;  p != &anchor && p->pos < pos;  p = p->next)
    178  1.1       cjs 		if (p->line == line)
    179  1.1       cjs 			/* We already have this one. */
    180  1.1       cjs 			return;
    181  1.1       cjs 	nextp = p;
    182  1.1       cjs 	prevp = p->prev;
    183  1.1       cjs 
    184  1.1       cjs 	if (freelist != NULL)
    185  1.1       cjs 	{
    186  1.1       cjs 		/*
    187  1.1       cjs 		 * We still have free (unused) entries.
    188  1.1       cjs 		 * Use one of them.
    189  1.1       cjs 		 */
    190  1.1       cjs 		new = freelist;
    191  1.1       cjs 		freelist = freelist->next;
    192  1.1       cjs 	} else
    193  1.1       cjs 	{
    194  1.1       cjs 		/*
    195  1.1       cjs 		 * No free entries.
    196  1.1       cjs 		 * Use the "spare" entry.
    197  1.1       cjs 		 */
    198  1.1       cjs 		new = spare;
    199  1.1       cjs 		spare = NULL;
    200  1.1       cjs 	}
    201  1.1       cjs 
    202  1.1       cjs 	/*
    203  1.1       cjs 	 * Fill in the fields of the new entry,
    204  1.1       cjs 	 * and insert it into the proper place in the list.
    205  1.1       cjs 	 */
    206  1.1       cjs 	new->next = nextp;
    207  1.1       cjs 	new->prev = prevp;
    208  1.1       cjs 	new->pos = pos;
    209  1.1       cjs 	new->line = line;
    210  1.1       cjs 
    211  1.1       cjs 	nextp->prev = new;
    212  1.1       cjs 	prevp->next = new;
    213  1.1       cjs 
    214  1.1       cjs 	/*
    215  1.1       cjs 	 * Recalculate gaps for the new entry and the neighboring entries.
    216  1.1       cjs 	 */
    217  1.1       cjs 	calcgap(new);
    218  1.1       cjs 	calcgap(nextp);
    219  1.1       cjs 	calcgap(prevp);
    220  1.1       cjs 
    221  1.1       cjs 	if (spare == NULL)
    222  1.1       cjs 	{
    223  1.1       cjs 		/*
    224  1.1       cjs 		 * We have used the spare entry.
    225  1.1       cjs 		 * Scan the list to find the one with the smallest
    226  1.1       cjs 		 * gap, take it out and make it the spare.
    227  1.1       cjs 		 * We should never remove the last one, so stop when
    228  1.1       cjs 		 * we get to p->next == &anchor.  This also avoids
    229  1.1       cjs 		 * looking at the gap of the last one, which is
    230  1.1       cjs 		 * not computed by calcgap.
    231  1.1       cjs 		 */
    232  1.1       cjs 		mingap = anchor.next->gap;
    233  1.1       cjs 		for (p = anchor.next;  p->next != &anchor;  p = p->next)
    234  1.1       cjs 		{
    235  1.1       cjs 			if (p->gap <= mingap)
    236  1.1       cjs 			{
    237  1.1       cjs 				spare = p;
    238  1.1       cjs 				mingap = p->gap;
    239  1.1       cjs 			}
    240  1.1       cjs 		}
    241  1.1       cjs 		spare->next->prev = spare->prev;
    242  1.1       cjs 		spare->prev->next = spare->next;
    243  1.1       cjs 	}
    244  1.1       cjs }
    245  1.1       cjs 
    246  1.1       cjs /*
    247  1.1       cjs  * If we get stuck in a long loop trying to figure out the
    248  1.1       cjs  * line number, print a message to tell the user what we're doing.
    249  1.1       cjs  */
    250  1.3  christos static void
    251  1.1       cjs longloopmessage()
    252  1.1       cjs {
    253  1.1       cjs 	ierror("Calculating line numbers");
    254  1.1       cjs 	/*
    255  1.1       cjs 	 * Set the lnloop flag here, so if the user interrupts while
    256  1.1       cjs 	 * we are calculating line numbers, the signal handler will
    257  1.1       cjs 	 * turn off line numbers (linenums=0).
    258  1.1       cjs 	 */
    259  1.1       cjs 	lnloop = 1;
    260  1.1       cjs }
    261  1.1       cjs 
    262  1.1       cjs /*
    263  1.1       cjs  * Find the line number associated with a given position.
    264  1.1       cjs  * Return 0 if we can't figure it out.
    265  1.1       cjs  */
    266  1.3  christos int
    267  1.1       cjs find_linenum(pos)
    268  1.1       cjs 	off_t pos;
    269  1.1       cjs {
    270  1.3  christos 	struct linenum *p;
    271  1.3  christos 	int lno;
    272  1.3  christos 	int loopcount;
    273  1.3  christos 	off_t cpos;
    274  1.3  christos 	time_t startime;
    275  1.1       cjs 
    276  1.1       cjs 	if (!linenums)
    277  1.1       cjs 		/*
    278  1.1       cjs 		 * We're not using line numbers.
    279  1.1       cjs 		 */
    280  1.1       cjs 		return (0);
    281  1.1       cjs 	if (pos == NULL_POSITION)
    282  1.1       cjs 		/*
    283  1.1       cjs 		 * Caller doesn't know what he's talking about.
    284  1.1       cjs 		 */
    285  1.1       cjs 		return (0);
    286  1.1       cjs 	if (pos == (off_t)0)
    287  1.1       cjs 		/*
    288  1.1       cjs 		 * Beginning of file is always line number 1.
    289  1.1       cjs 		 */
    290  1.1       cjs 		return (1);
    291  1.1       cjs 
    292  1.1       cjs 	/*
    293  1.1       cjs 	 * Find the entry nearest to the position we want.
    294  1.1       cjs 	 */
    295  1.1       cjs 	for (p = anchor.next;  p != &anchor && p->pos < pos;  p = p->next)
    296  1.1       cjs 		continue;
    297  1.1       cjs 	if (p->pos == pos)
    298  1.1       cjs 		/* Found it exactly. */
    299  1.1       cjs 		return (p->line);
    300  1.1       cjs 
    301  1.1       cjs 	/*
    302  1.1       cjs 	 * This is the (possibly) time-consuming part.
    303  1.1       cjs 	 * We start at the line we just found and start
    304  1.1       cjs 	 * reading the file forward or backward till we
    305  1.1       cjs 	 * get to the place we want.
    306  1.1       cjs 	 *
    307  1.1       cjs 	 * First decide whether we should go forward from the
    308  1.1       cjs 	 * previous one or backwards from the next one.
    309  1.1       cjs 	 * The decision is based on which way involves
    310  1.1       cjs 	 * traversing fewer bytes in the file.
    311  1.1       cjs 	 */
    312  1.1       cjs 	flush();
    313  1.1       cjs 	(void)time(&startime);
    314  1.1       cjs 	if (p == &anchor || pos - p->prev->pos < p->pos - pos)
    315  1.1       cjs 	{
    316  1.1       cjs 		/*
    317  1.1       cjs 		 * Go forward.
    318  1.1       cjs 		 */
    319  1.1       cjs 		p = p->prev;
    320  1.1       cjs 		if (ch_seek(p->pos))
    321  1.1       cjs 			return (0);
    322  1.1       cjs 		loopcount = 0;
    323  1.1       cjs 		for (lno = p->line, cpos = p->pos;  cpos < pos;  lno++)
    324  1.1       cjs 		{
    325  1.1       cjs 			/*
    326  1.1       cjs 			 * Allow a signal to abort this loop.
    327  1.1       cjs 			 */
    328  1.1       cjs 			cpos = forw_raw_line(cpos);
    329  1.1       cjs 			if (sigs || cpos == NULL_POSITION)
    330  1.1       cjs 				return (0);
    331  1.1       cjs 			if (loopcount >= 0 && ++loopcount > 100) {
    332  1.1       cjs 				loopcount = 0;
    333  1.1       cjs 				if (time((time_t *)NULL)
    334  1.1       cjs 				    >= startime + LONGTIME) {
    335  1.1       cjs 					longloopmessage();
    336  1.1       cjs 					loopcount = -1;
    337  1.1       cjs 				}
    338  1.1       cjs 			}
    339  1.1       cjs 		}
    340  1.1       cjs 		lnloop = 0;
    341  1.1       cjs 		/*
    342  1.1       cjs 		 * If the given position is not at the start of a line,
    343  1.1       cjs 		 * make sure we return the correct line number.
    344  1.1       cjs 		 */
    345  1.1       cjs 		if (cpos > pos)
    346  1.1       cjs 			lno--;
    347  1.1       cjs 	} else
    348  1.1       cjs 	{
    349  1.1       cjs 		/*
    350  1.1       cjs 		 * Go backward.
    351  1.1       cjs 		 */
    352  1.1       cjs 		if (ch_seek(p->pos))
    353  1.1       cjs 			return (0);
    354  1.1       cjs 		loopcount = 0;
    355  1.1       cjs 		for (lno = p->line, cpos = p->pos;  cpos > pos;  lno--)
    356  1.1       cjs 		{
    357  1.1       cjs 			/*
    358  1.1       cjs 			 * Allow a signal to abort this loop.
    359  1.1       cjs 			 */
    360  1.1       cjs 			cpos = back_raw_line(cpos);
    361  1.1       cjs 			if (sigs || cpos == NULL_POSITION)
    362  1.1       cjs 				return (0);
    363  1.1       cjs 			if (loopcount >= 0 && ++loopcount > 100) {
    364  1.1       cjs 				loopcount = 0;
    365  1.1       cjs 				if (time((time_t *)NULL)
    366  1.1       cjs 				    >= startime + LONGTIME) {
    367  1.1       cjs 					longloopmessage();
    368  1.1       cjs 					loopcount = -1;
    369  1.1       cjs 				}
    370  1.1       cjs 			}
    371  1.1       cjs 		}
    372  1.1       cjs 		lnloop = 0;
    373  1.1       cjs 	}
    374  1.1       cjs 
    375  1.1       cjs 	/*
    376  1.1       cjs 	 * We might as well cache it.
    377  1.1       cjs 	 */
    378  1.1       cjs 	add_lnum(lno, cpos);
    379  1.1       cjs 	return (lno);
    380  1.1       cjs }
    381  1.1       cjs 
    382  1.1       cjs /*
    383  1.1       cjs  * Return the line number of the "current" line.
    384  1.1       cjs  * The argument "where" tells which line is to be considered
    385  1.1       cjs  * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc).
    386  1.1       cjs  */
    387  1.3  christos int
    388  1.1       cjs currline(where)
    389  1.1       cjs 	int where;
    390  1.1       cjs {
    391  1.3  christos 	off_t pos;
    392  1.1       cjs 
    393  1.1       cjs 	if ((pos = position(where)) == NULL_POSITION)
    394  1.1       cjs 		pos = ch_length();
    395  1.1       cjs 	return(find_linenum(pos));
    396  1.1       cjs }
    397