xref: /dragonfly/contrib/less/linenum.c (revision f9993810)
1 /*
2  * Copyright (C) 1984-2022  Mark Nudelman
3  *
4  * You may distribute under the terms of either the GNU General Public
5  * License or the Less License, as specified in the README file.
6  *
7  * For more information, see the README file.
8  */
9 
10 
11 /*
12  * Code to handle displaying line numbers.
13  *
14  * Finding the line number of a given file position is rather tricky.
15  * We don't want to just start at the beginning of the file and
16  * count newlines, because that is slow for large files (and also
17  * wouldn't work if we couldn't get to the start of the file; e.g.
18  * if input is a long pipe).
19  *
20  * So we use the function add_lnum to cache line numbers.
21  * We try to be very clever and keep only the more interesting
22  * line numbers when we run out of space in our table.  A line
23  * number is more interesting than another when it is far from
24  * other line numbers.   For example, we'd rather keep lines
25  * 100,200,300 than 100,101,300.  200 is more interesting than
26  * 101 because 101 can be derived very cheaply from 100, while
27  * 200 is more expensive to derive from 100.
28  *
29  * The function currline() returns the line number of a given
30  * position in the file.  As a side effect, it calls add_lnum
31  * to cache the line number.  Therefore currline is occasionally
32  * called to make sure we cache line numbers often enough.
33  */
34 
35 #include "less.h"
36 
37 /*
38  * Structure to keep track of a line number and the associated file position.
39  * A doubly-linked circular list of line numbers is kept ordered by line number.
40  */
41 struct linenum_info
42 {
43 	struct linenum_info *next;      /* Link to next in the list */
44 	struct linenum_info *prev;      /* Line to previous in the list */
45 	POSITION pos;                   /* File position */
46 	POSITION gap;                   /* Gap between prev and next */
47 	LINENUM line;                   /* Line number */
48 };
49 /*
50  * "gap" needs some explanation: the gap of any particular line number
51  * is the distance between the previous one and the next one in the list.
52  * ("Distance" means difference in file position.)  In other words, the
53  * gap of a line number is the gap which would be introduced if this
54  * line number were deleted.  It is used to decide which one to replace
55  * when we have a new one to insert and the table is full.
56  */
57 
58 #define NPOOL   200                     /* Size of line number pool */
59 
60 #define LONGTIME        (2)             /* In seconds */
61 
62 static struct linenum_info anchor;      /* Anchor of the list */
63 static struct linenum_info *freelist;   /* Anchor of the unused entries */
64 static struct linenum_info pool[NPOOL]; /* The pool itself */
65 static struct linenum_info *spare;              /* We always keep one spare entry */
66 
67 extern int linenums;
68 extern int sigs;
69 extern int sc_height;
70 extern int screen_trashed;
71 extern int header_lines;
72 extern int nonum_headers;
73 
74 /*
75  * Initialize the line number structures.
76  */
77 	public void
78 clr_linenum(VOID_PARAM)
79 {
80 	struct linenum_info *p;
81 
82 	/*
83 	 * Put all the entries on the free list.
84 	 * Leave one for the "spare".
85 	 */
86 	for (p = pool;  p < &pool[NPOOL-2];  p++)
87 		p->next = p+1;
88 	pool[NPOOL-2].next = NULL;
89 	freelist = pool;
90 
91 	spare = &pool[NPOOL-1];
92 
93 	/*
94 	 * Initialize the anchor.
95 	 */
96 	anchor.next = anchor.prev = &anchor;
97 	anchor.gap = 0;
98 	anchor.pos = (POSITION)0;
99 	anchor.line = 1;
100 }
101 
102 /*
103  * Calculate the gap for an entry.
104  */
105 	static void
106 calcgap(p)
107 	struct linenum_info *p;
108 {
109 	/*
110 	 * Don't bother to compute a gap for the anchor.
111 	 * Also don't compute a gap for the last one in the list.
112 	 * The gap for that last one should be considered infinite,
113 	 * but we never look at it anyway.
114 	 */
115 	if (p == &anchor || p->next == &anchor)
116 		return;
117 	p->gap = p->next->pos - p->prev->pos;
118 }
119 
120 /*
121  * Add a new line number to the cache.
122  * The specified position (pos) should be the file position of the
123  * FIRST character in the specified line.
124  */
125 	public void
126 add_lnum(linenum, pos)
127 	LINENUM linenum;
128 	POSITION pos;
129 {
130 	struct linenum_info *p;
131 	struct linenum_info *new;
132 	struct linenum_info *nextp;
133 	struct linenum_info *prevp;
134 	POSITION mingap;
135 
136 	/*
137 	 * Find the proper place in the list for the new one.
138 	 * The entries are sorted by position.
139 	 */
140 	for (p = anchor.next;  p != &anchor && p->pos < pos;  p = p->next)
141 		if (p->line == linenum)
142 			/* We already have this one. */
143 			return;
144 	nextp = p;
145 	prevp = p->prev;
146 
147 	if (freelist != NULL)
148 	{
149 		/*
150 		 * We still have free (unused) entries.
151 		 * Use one of them.
152 		 */
153 		new = freelist;
154 		freelist = freelist->next;
155 	} else
156 	{
157 		/*
158 		 * No free entries.
159 		 * Use the "spare" entry.
160 		 */
161 		new = spare;
162 		spare = NULL;
163 	}
164 
165 	/*
166 	 * Fill in the fields of the new entry,
167 	 * and insert it into the proper place in the list.
168 	 */
169 	new->next = nextp;
170 	new->prev = prevp;
171 	new->pos = pos;
172 	new->line = linenum;
173 
174 	nextp->prev = new;
175 	prevp->next = new;
176 
177 	/*
178 	 * Recalculate gaps for the new entry and the neighboring entries.
179 	 */
180 	calcgap(new);
181 	calcgap(nextp);
182 	calcgap(prevp);
183 
184 	if (spare == NULL)
185 	{
186 		/*
187 		 * We have used the spare entry.
188 		 * Scan the list to find the one with the smallest
189 		 * gap, take it out and make it the spare.
190 		 * We should never remove the last one, so stop when
191 		 * we get to p->next == &anchor.  This also avoids
192 		 * looking at the gap of the last one, which is
193 		 * 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
214 longloopmessage(VOID_PARAM)
215 {
216 	ierror("Calculating line numbers", NULL_PARG);
217 }
218 
219 static int loopcount;
220 #if HAVE_TIME
221 static time_type startime;
222 #endif
223 
224 	static void
225 longish(VOID_PARAM)
226 {
227 #if HAVE_TIME
228 	if (loopcount >= 0 && ++loopcount > 100)
229 	{
230 		loopcount = 0;
231 		if (get_time() >= startime + LONGTIME)
232 		{
233 			longloopmessage();
234 			loopcount = -1;
235 		}
236 	}
237 #else
238 	if (loopcount >= 0 && ++loopcount > LONGLOOP)
239 	{
240 		longloopmessage();
241 		loopcount = -1;
242 	}
243 #endif
244 }
245 
246 /*
247  * Turn off line numbers because the user has interrupted
248  * a lengthy line number calculation.
249  */
250 	static void
251 abort_long(VOID_PARAM)
252 {
253 	if (loopcount >= 0)
254 		return;
255 	if (linenums == OPT_ONPLUS)
256 		/*
257 		 * We were displaying line numbers, so need to repaint.
258 		 */
259 		screen_trashed = 1;
260 	linenums = 0;
261 	error("Line numbers turned off", NULL_PARG);
262 }
263 
264 /*
265  * Find the line number associated with a given position.
266  * Return 0 if we can't figure it out.
267  */
268 	public LINENUM
269 find_linenum(pos)
270 	POSITION pos;
271 {
272 	struct linenum_info *p;
273 	LINENUM linenum;
274 	POSITION cpos;
275 
276 	if (!linenums)
277 		/*
278 		 * We're not using line numbers.
279 		 */
280 		return (0);
281 	if (pos == NULL_POSITION)
282 		/*
283 		 * Caller doesn't know what he's talking about.
284 		 */
285 		return (0);
286 	if (pos <= ch_zero())
287 		/*
288 		 * Beginning of file is always line number 1.
289 		 */
290 		return (1);
291 
292 	/*
293 	 * Find the entry nearest to the position we want.
294 	 */
295 	for (p = anchor.next;  p != &anchor && p->pos < pos;  p = p->next)
296 		continue;
297 	if (p->pos == pos)
298 		/* Found it exactly. */
299 		return (p->line);
300 
301 	/*
302 	 * This is the (possibly) time-consuming part.
303 	 * We start at the line we just found and start
304 	 * reading the file forward or backward till we
305 	 * get to the place we want.
306 	 *
307 	 * First decide whether we should go forward from the
308 	 * previous one or backwards from the next one.
309 	 * The decision is based on which way involves
310 	 * traversing fewer bytes in the file.
311 	 */
312 #if HAVE_TIME
313 	startime = get_time();
314 #endif
315 	loopcount = 0;
316 	if (p == &anchor || pos - p->prev->pos < p->pos - pos)
317 	{
318 		/*
319 		 * Go forward.
320 		 */
321 		p = p->prev;
322 		if (ch_seek(p->pos))
323 			return (0);
324 		for (linenum = p->line, cpos = p->pos;  cpos < pos;  linenum++)
325 		{
326 			/*
327 			 * Allow a signal to abort this loop.
328 			 */
329 			cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL);
330 			if (ABORT_SIGS()) {
331 				abort_long();
332 				return (0);
333 			}
334 			if (cpos == NULL_POSITION)
335 				return (0);
336 			longish();
337 		}
338 		/*
339 		 * We might as well cache it.
340 		 */
341 		add_lnum(linenum, cpos);
342 		/*
343 		 * If the given position is not at the start of a line,
344 		 * make sure we return the correct line number.
345 		 */
346 		if (cpos > pos)
347 			linenum--;
348 	} else
349 	{
350 		/*
351 		 * Go backward.
352 		 */
353 		if (ch_seek(p->pos))
354 			return (0);
355 		for (linenum = p->line, cpos = p->pos;  cpos > pos;  linenum--)
356 		{
357 			/*
358 			 * Allow a signal to abort this loop.
359 			 */
360 			cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
361 			if (ABORT_SIGS()) {
362 				abort_long();
363 				return (0);
364 			}
365 			if (cpos == NULL_POSITION)
366 				return (0);
367 			longish();
368 		}
369 		/*
370 		 * We might as well cache it.
371 		 */
372 		add_lnum(linenum, cpos);
373 	}
374 	loopcount = 0;
375 	return (linenum);
376 }
377 
378 /*
379  * Find the position of a given line number.
380  * Return NULL_POSITION if we can't figure it out.
381  */
382 	public POSITION
383 find_pos(linenum)
384 	LINENUM linenum;
385 {
386 	struct linenum_info *p;
387 	POSITION cpos;
388 	LINENUM clinenum;
389 
390 	if (linenum <= 1)
391 		/*
392 		 * Line number 1 is beginning of file.
393 		 */
394 		return (ch_zero());
395 
396 	/*
397 	 * Find the entry nearest to the line number we want.
398 	 */
399 	for (p = anchor.next;  p != &anchor && p->line < linenum;  p = p->next)
400 		continue;
401 	if (p->line == linenum)
402 		/* Found it exactly. */
403 		return (p->pos);
404 
405 	if (p == &anchor || linenum - p->prev->line < p->line - linenum)
406 	{
407 		/*
408 		 * Go forward.
409 		 */
410 		p = p->prev;
411 		if (ch_seek(p->pos))
412 			return (NULL_POSITION);
413 		for (clinenum = p->line, cpos = p->pos;  clinenum < linenum;  clinenum++)
414 		{
415 			/*
416 			 * Allow a signal to abort this loop.
417 			 */
418 			cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL);
419 			if (ABORT_SIGS())
420 				return (NULL_POSITION);
421 			if (cpos == NULL_POSITION)
422 				return (NULL_POSITION);
423 		}
424 	} else
425 	{
426 		/*
427 		 * Go backward.
428 		 */
429 		if (ch_seek(p->pos))
430 			return (NULL_POSITION);
431 		for (clinenum = p->line, cpos = p->pos;  clinenum > linenum;  clinenum--)
432 		{
433 			/*
434 			 * Allow a signal to abort this loop.
435 			 */
436 			cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
437 			if (ABORT_SIGS())
438 				return (NULL_POSITION);
439 			if (cpos == NULL_POSITION)
440 				return (NULL_POSITION);
441 		}
442 	}
443 	/*
444 	 * We might as well cache it.
445 	 */
446 	add_lnum(clinenum, cpos);
447 	return (cpos);
448 }
449 
450 /*
451  * Return the line number of the "current" line.
452  * The argument "where" tells which line is to be considered
453  * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc).
454  */
455 	public LINENUM
456 currline(where)
457 	int where;
458 {
459 	POSITION pos;
460 	POSITION len;
461 	LINENUM linenum;
462 
463 	pos = position(where);
464 	len = ch_length();
465 	while (pos == NULL_POSITION && where >= 0 && where < sc_height)
466 		pos = position(++where);
467 	if (pos == NULL_POSITION)
468 		pos = len;
469 	linenum = find_linenum(pos);
470 	if (pos == len)
471 		linenum--;
472 	return (linenum);
473 }
474 
475 /*
476  * Scan entire file, counting line numbers.
477  */
478 	public void
479 scan_eof(VOID_PARAM)
480 {
481 	POSITION pos = 0;
482 	LINENUM linenum = 0;
483 
484 	if (ch_seek(0))
485 		return;
486 	ierror("Determining length of file", NULL_PARG);
487 	while (pos != NULL_POSITION)
488 	{
489 		/* For efficiency, only add one every 256 line numbers. */
490 		if ((linenum++ % 256) == 0)
491 			add_lnum(linenum, pos);
492 		pos = forw_raw_line(pos, (char **)NULL, (int *)NULL);
493 		if (ABORT_SIGS())
494 			break;
495 	}
496 }
497 
498 /*
499  * Return a line number adjusted for display
500  * (handles the --no-number-headers option).
501  */
502 	public LINENUM
503 vlinenum(linenum)
504 	LINENUM linenum;
505 {
506 	if (nonum_headers)
507 		linenum = (linenum < header_lines) ? 0 : linenum - header_lines;
508 	return linenum;
509 }
510