linenum.c revision 170257
1189251Ssam/*
2189251Ssam * Copyright (C) 1984-2007  Mark Nudelman
3189251Ssam *
4189251Ssam * You may distribute under the terms of either the GNU General Public
5189251Ssam * License or the Less License, as specified in the README file.
6189251Ssam *
7189251Ssam * For more information about less, or for information on how to
8189251Ssam * contact the author, see the README file.
9189251Ssam */
10189251Ssam
11189251Ssam
12189251Ssam/*
13189251Ssam * Code to handle displaying line numbers.
14214734Srpaulo *
15214734Srpaulo * Finding the line number of a given file position is rather tricky.
16214734Srpaulo * We don't want to just start at the beginning of the file and
17189251Ssam * count newlines, because that is slow for large files (and also
18189251Ssam * wouldn't work if we couldn't get to the start of the file; e.g.
19189251Ssam * if input is a long pipe).
20189251Ssam *
21189251Ssam * So we use the function add_lnum to cache line numbers.
22189251Ssam * We try to be very clever and keep only the more interesting
23189251Ssam * line numbers when we run out of space in our table.  A line
24189251Ssam * number is more interesting than another when it is far from
25189251Ssam * other line numbers.   For example, we'd rather keep lines
26189251Ssam * 100,200,300 than 100,101,300.  200 is more interesting than
27189251Ssam * 101 because 101 can be derived very cheaply from 100, while
28189251Ssam * 200 is more expensive to derive from 100.
29189251Ssam *
30189251Ssam * The function currline() returns the line number of a given
31214734Srpaulo * position in the file.  As a side effect, it calls add_lnum
32189251Ssam * to cache the line number.  Therefore currline is occasionally
33214734Srpaulo * called to make sure we cache line numbers often enough.
34189251Ssam */
35214734Srpaulo
36189251Ssam#include "less.h"
37189251Ssam
38189251Ssam/*
39189251Ssam * Structure to keep track of a line number and the associated file position.
40189251Ssam * A doubly-linked circular list of line numbers is kept ordered by line number.
41189251Ssam */
42189251Ssamstruct linenum_info
43189251Ssam{
44189251Ssam	struct linenum_info *next;	/* Link to next in the list */
45189251Ssam	struct linenum_info *prev;	/* Line to previous in the list */
46189251Ssam	POSITION pos;			/* File position */
47189251Ssam	POSITION gap;			/* Gap between prev and next */
48189251Ssam	LINENUM line;			/* Line number */
49189251Ssam};
50189251Ssam/*
51189251Ssam * "gap" needs some explanation: the gap of any particular line number
52214734Srpaulo * is the distance between the previous one and the next one in the list.
53189251Ssam * ("Distance" means difference in file position.)  In other words, the
54189251Ssam * gap of a line number is the gap which would be introduced if this
55189251Ssam * line number were deleted.  It is used to decide which one to replace
56189251Ssam * when we have a new one to insert and the table is full.
57189251Ssam */
58189251Ssam
59189251Ssam#define	NPOOL	50			/* Size of line number pool */
60189251Ssam
61189251Ssam#define	LONGTIME	(2)		/* In seconds */
62189251Ssam
63189251Ssampublic int lnloop = 0;			/* Are we in the line num loop? */
64189251Ssam
65189251Ssamstatic struct linenum_info anchor;	/* Anchor of the list */
66214734Srpaulostatic struct linenum_info *freelist;	/* Anchor of the unused entries */
67189251Ssamstatic struct linenum_info pool[NPOOL];	/* The pool itself */
68189251Ssamstatic struct linenum_info *spare;		/* We always keep one spare entry */
69189251Ssam
70252726Srpauloextern int linenums;
71189251Ssamextern int sigs;
72189251Ssamextern int sc_height;
73189251Ssam
74189251Ssam/*
75189251Ssam * Initialize the line number structures.
76189251Ssam */
77189251Ssam	public void
78189251Ssamclr_linenum()
79189251Ssam{
80214734Srpaulo	register struct linenum_info *p;
81189251Ssam
82189251Ssam	/*
83189251Ssam	 * Put all the entries on the free list.
84189251Ssam	 * Leave one for the "spare".
85189251Ssam	 */
86189251Ssam	for (p = pool;  p < &pool[NPOOL-2];  p++)
87189251Ssam		p->next = p+1;
88189251Ssam	pool[NPOOL-2].next = NULL;
89189251Ssam	freelist = pool;
90189251Ssam
91214734Srpaulo	spare = &pool[NPOOL-1];
92214734Srpaulo
93189251Ssam	/*
94189251Ssam	 * Initialize the anchor.
95252726Srpaulo	 */
96214734Srpaulo	anchor.next = anchor.prev = &anchor;
97214734Srpaulo	anchor.gap = 0;
98214734Srpaulo	anchor.pos = (POSITION)0;
99214734Srpaulo	anchor.line = 1;
100214734Srpaulo}
101252726Srpaulo
102214734Srpaulo/*
103189251Ssam * Calculate the gap for an entry.
104189251Ssam */
105189251Ssam	static void
106252726Srpaulocalcgap(p)
107252726Srpaulo	register struct linenum_info *p;
108252726Srpaulo{
109252726Srpaulo	/*
110252726Srpaulo	 * Don't bother to compute a gap for the anchor.
111252726Srpaulo	 * Also don't compute a gap for the last one in the list.
112252726Srpaulo	 * The gap for that last one should be considered infinite,
113252726Srpaulo	 * but we never look at it anyway.
114252726Srpaulo	 */
115252726Srpaulo	if (p == &anchor || p->next == &anchor)
116189251Ssam		return;
117252726Srpaulo	p->gap = p->next->pos - p->prev->pos;
118252726Srpaulo}
119252726Srpaulo
120252726Srpaulo/*
121189251Ssam * Add a new line number to the cache.
122189251Ssam * The specified position (pos) should be the file position of the
123189251Ssam * FIRST character in the specified line.
124189251Ssam */
125252726Srpaulo	public void
126189251Ssamadd_lnum(linenum, pos)
127189251Ssam	LINENUM linenum;
128189251Ssam	POSITION pos;
129189251Ssam{
130189251Ssam	register struct linenum_info *p;
131189251Ssam	register struct linenum_info *new;
132189251Ssam	register struct linenum_info *nextp;
133189251Ssam	register struct linenum_info *prevp;
134189251Ssam	register POSITION mingap;
135189251Ssam
136189251Ssam	/*
137214734Srpaulo	 * Find the proper place in the list for the new one.
138189251Ssam	 * The entries are sorted by position.
139214734Srpaulo	 */
140189251Ssam	for (p = anchor.next;  p != &anchor && p->pos < pos;  p = p->next)
141214734Srpaulo		if (p->line == linenum)
142189251Ssam			/* We already have this one. */
143189251Ssam			return;
144189251Ssam	nextp = p;
145189251Ssam	prevp = p->prev;
146252726Srpaulo
147252726Srpaulo	if (freelist != NULL)
148252726Srpaulo	{
149189251Ssam		/*
150189251Ssam		 * We still have free (unused) entries.
151189251Ssam		 * Use one of them.
152189251Ssam		 */
153189251Ssam		new = freelist;
154209158Srpaulo		freelist = freelist->next;
155189251Ssam	} else
156189251Ssam	{
157189251Ssam		/*
158189251Ssam		 * No free entries.
159189251Ssam		 * Use the "spare" entry.
160252726Srpaulo		 */
161214734Srpaulo		new = spare;
162214734Srpaulo		spare = NULL;
163189251Ssam	}
164189251Ssam
165189251Ssam	/*
166189251Ssam	 * Fill in the fields of the new entry,
167209158Srpaulo	 * and insert it into the proper place in the list.
168209158Srpaulo	 */
169189251Ssam	new->next = nextp;
170189251Ssam	new->prev = prevp;
171214734Srpaulo	new->pos = pos;
172214734Srpaulo	new->line = linenum;
173281806Srpaulo
174189251Ssam	nextp->prev = new;
175189251Ssam	prevp->next = new;
176189251Ssam
177189251Ssam	/*
178189251Ssam	 * Recalculate gaps for the new entry and the neighboring entries.
179189251Ssam	 */
180189251Ssam	calcgap(new);
181252726Srpaulo	calcgap(nextp);
182189251Ssam	calcgap(prevp);
183189251Ssam
184189251Ssam	if (spare == NULL)
185189251Ssam	{
186214734Srpaulo		/*
187214734Srpaulo		 * We have used the spare entry.
188214734Srpaulo		 * Scan the list to find the one with the smallest
189214734Srpaulo		 * gap, take it out and make it the spare.
190252726Srpaulo		 * We should never remove the last one, so stop when
191252726Srpaulo		 * we get to p->next == &anchor.  This also avoids
192214734Srpaulo		 * looking at the gap of the last one, which is
193189251Ssam		 * not computed by calcgap.
194		 */
195		mingap = anchor.next->gap;
196		for (p = anchor.next;  p->next != &anchor;  p = p->next)
197		{
198			if (p->gap <= mingap)
199			{
200				spare = p;
201				mingap = p->gap;
202			}
203		}
204		spare->next->prev = spare->prev;
205		spare->prev->next = spare->next;
206	}
207}
208
209/*
210 * If we get stuck in a long loop trying to figure out the
211 * line number, print a message to tell the user what we're doing.
212 */
213	static void
214longloopmessage()
215{
216	ierror("Calculating line numbers", NULL_PARG);
217	/*
218	 * Set the lnloop flag here, so if the user interrupts while
219	 * we are calculating line numbers, the signal handler will
220	 * turn off line numbers (linenums=0).
221	 */
222	lnloop = 1;
223}
224
225static int loopcount;
226#if HAVE_TIME
227static long startime;
228#endif
229
230	static void
231longish()
232{
233#if HAVE_TIME
234	if (loopcount >= 0 && ++loopcount > 100)
235	{
236		loopcount = 0;
237		if (get_time() >= startime + LONGTIME)
238		{
239			longloopmessage();
240			loopcount = -1;
241		}
242	}
243#else
244	if (loopcount >= 0 && ++loopcount > LONGLOOP)
245	{
246		longloopmessage();
247		loopcount = -1;
248	}
249#endif
250}
251
252/*
253 * Find the line number associated with a given position.
254 * Return 0 if we can't figure it out.
255 */
256	public LINENUM
257find_linenum(pos)
258	POSITION pos;
259{
260	register struct linenum_info *p;
261	register LINENUM linenum;
262	POSITION cpos;
263
264	if (!linenums)
265		/*
266		 * We're not using line numbers.
267		 */
268		return (0);
269	if (pos == NULL_POSITION)
270		/*
271		 * Caller doesn't know what he's talking about.
272		 */
273		return (0);
274	if (pos <= ch_zero())
275		/*
276		 * Beginning of file is always line number 1.
277		 */
278		return (1);
279
280	/*
281	 * Find the entry nearest to the position we want.
282	 */
283	for (p = anchor.next;  p != &anchor && p->pos < pos;  p = p->next)
284		continue;
285	if (p->pos == pos)
286		/* Found it exactly. */
287		return (p->line);
288
289	/*
290	 * This is the (possibly) time-consuming part.
291	 * We start at the line we just found and start
292	 * reading the file forward or backward till we
293	 * get to the place we want.
294	 *
295	 * First decide whether we should go forward from the
296	 * previous one or backwards from the next one.
297	 * The decision is based on which way involves
298	 * traversing fewer bytes in the file.
299	 */
300#if HAVE_TIME
301	startime = get_time();
302#endif
303	if (p == &anchor || pos - p->prev->pos < p->pos - pos)
304	{
305		/*
306		 * Go forward.
307		 */
308		p = p->prev;
309		if (ch_seek(p->pos))
310			return (0);
311		loopcount = 0;
312		for (linenum = p->line, cpos = p->pos;  cpos < pos;  linenum++)
313		{
314			/*
315			 * Allow a signal to abort this loop.
316			 */
317			cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL);
318			if (ABORT_SIGS() || cpos == NULL_POSITION)
319				return (0);
320			longish();
321		}
322		lnloop = 0;
323		/*
324		 * We might as well cache it.
325		 */
326		add_lnum(linenum, cpos);
327		/*
328		 * If the given position is not at the start of a line,
329		 * make sure we return the correct line number.
330		 */
331		if (cpos > pos)
332			linenum--;
333	} else
334	{
335		/*
336		 * Go backward.
337		 */
338		if (ch_seek(p->pos))
339			return (0);
340		loopcount = 0;
341		for (linenum = p->line, cpos = p->pos;  cpos > pos;  linenum--)
342		{
343			/*
344			 * Allow a signal to abort this loop.
345			 */
346			cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
347			if (ABORT_SIGS() || cpos == NULL_POSITION)
348				return (0);
349			longish();
350		}
351		lnloop = 0;
352		/*
353		 * We might as well cache it.
354		 */
355		add_lnum(linenum, cpos);
356	}
357
358	return (linenum);
359}
360
361/*
362 * Find the position of a given line number.
363 * Return NULL_POSITION if we can't figure it out.
364 */
365	public POSITION
366find_pos(linenum)
367	LINENUM linenum;
368{
369	register struct linenum_info *p;
370	POSITION cpos;
371	LINENUM clinenum;
372
373	if (linenum <= 1)
374		/*
375		 * Line number 1 is beginning of file.
376		 */
377		return (ch_zero());
378
379	/*
380	 * Find the entry nearest to the line number we want.
381	 */
382	for (p = anchor.next;  p != &anchor && p->line < linenum;  p = p->next)
383		continue;
384	if (p->line == linenum)
385		/* Found it exactly. */
386		return (p->pos);
387
388	if (p == &anchor || linenum - p->prev->line < p->line - linenum)
389	{
390		/*
391		 * Go forward.
392		 */
393		p = p->prev;
394		if (ch_seek(p->pos))
395			return (NULL_POSITION);
396		for (clinenum = p->line, cpos = p->pos;  clinenum < linenum;  clinenum++)
397		{
398			/*
399			 * Allow a signal to abort this loop.
400			 */
401			cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL);
402			if (ABORT_SIGS() || cpos == NULL_POSITION)
403				return (NULL_POSITION);
404		}
405	} else
406	{
407		/*
408		 * Go backward.
409		 */
410		if (ch_seek(p->pos))
411			return (NULL_POSITION);
412		for (clinenum = p->line, cpos = p->pos;  clinenum > linenum;  clinenum--)
413		{
414			/*
415			 * Allow a signal to abort this loop.
416			 */
417			cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
418			if (ABORT_SIGS() || cpos == NULL_POSITION)
419				return (NULL_POSITION);
420		}
421	}
422	/*
423	 * We might as well cache it.
424	 */
425	add_lnum(clinenum, cpos);
426	return (cpos);
427}
428
429/*
430 * Return the line number of the "current" line.
431 * The argument "where" tells which line is to be considered
432 * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc).
433 */
434	public LINENUM
435currline(where)
436	int where;
437{
438	POSITION pos;
439	POSITION len;
440	LINENUM linenum;
441
442	pos = position(where);
443	len = ch_length();
444	while (pos == NULL_POSITION && where >= 0 && where < sc_height)
445		pos = position(++where);
446	if (pos == NULL_POSITION)
447		pos = len;
448	linenum = find_linenum(pos);
449	if (pos == len)
450		linenum--;
451	return (linenum);
452}
453