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