Annotation of src/usr.bin/less/linenum.c, Revision 1.12
1.1 etheisen 1: /*
1.7 shadchin 2: * Copyright (C) 1984-2012 Mark Nudelman
1.10 nicm 3: * Modified for use with illumos by Garrett D'Amore.
4: * Copyright 2014 Garrett D'Amore <garrett@damore.org>
1.1 etheisen 5: *
1.4 millert 6: * You may distribute under the terms of either the GNU General Public
7: * License or the Less License, as specified in the README file.
1.1 etheisen 8: *
1.7 shadchin 9: * For more information, see the README file.
1.8 nicm 10: */
1.1 etheisen 11:
12: /*
13: * Code to handle displaying line numbers.
14: *
15: * Finding the line number of a given file position is rather tricky.
16: * We don't want to just start at the beginning of the file and
17: * count newlines, because that is slow for large files (and also
18: * wouldn't work if we couldn't get to the start of the file; e.g.
19: * if input is a long pipe).
20: *
21: * So we use the function add_lnum to cache line numbers.
22: * We try to be very clever and keep only the more interesting
23: * line numbers when we run out of space in our table. A line
24: * number is more interesting than another when it is far from
25: * other line numbers. For example, we'd rather keep lines
26: * 100,200,300 than 100,101,300. 200 is more interesting than
27: * 101 because 101 can be derived very cheaply from 100, while
28: * 200 is more expensive to derive from 100.
29: *
30: * The function currline() returns the line number of a given
31: * position in the file. As a side effect, it calls add_lnum
32: * to cache the line number. Therefore currline is occasionally
33: * called to make sure we cache line numbers often enough.
34: */
35:
36: #include "less.h"
37:
38: /*
39: * Structure to keep track of a line number and the associated file position.
40: * A doubly-linked circular list of line numbers is kept ordered by line number.
41: */
1.11 deraadt 42: struct linenum_info {
1.4 millert 43: struct linenum_info *next; /* Link to next in the list */
44: struct linenum_info *prev; /* Line to previous in the list */
1.8 nicm 45: off_t pos; /* File position */
46: off_t gap; /* Gap between prev and next */
1.4 millert 47: LINENUM line; /* Line number */
1.1 etheisen 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:
1.5 shadchin 58: #define NPOOL 200 /* Size of line number pool */
1.1 etheisen 59:
60: #define LONGTIME (2) /* In seconds */
61:
1.4 millert 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 */
1.8 nicm 65: static struct linenum_info *spare; /* We always keep one spare entry */
1.1 etheisen 66:
67: extern int linenums;
1.6 millert 68: extern volatile sig_atomic_t sigs;
1.1 etheisen 69: extern int sc_height;
1.5 shadchin 70: extern int screen_trashed;
1.1 etheisen 71:
72: /*
73: * Initialize the line number structures.
74: */
1.8 nicm 75: void
76: clr_linenum(void)
1.1 etheisen 77: {
1.8 nicm 78: struct linenum_info *p;
1.1 etheisen 79:
80: /*
81: * Put all the entries on the free list.
82: * Leave one for the "spare".
83: */
84: for (p = pool; p < &pool[NPOOL-2]; p++)
85: p->next = p+1;
86: pool[NPOOL-2].next = NULL;
87: freelist = pool;
88:
89: spare = &pool[NPOOL-1];
90:
91: /*
92: * Initialize the anchor.
93: */
94: anchor.next = anchor.prev = &anchor;
95: anchor.gap = 0;
1.8 nicm 96: anchor.pos = 0;
1.1 etheisen 97: anchor.line = 1;
98: }
99:
100: /*
101: * Calculate the gap for an entry.
102: */
1.8 nicm 103: static void
104: calcgap(struct linenum_info *p)
1.1 etheisen 105: {
106: /*
107: * Don't bother to compute a gap for the anchor.
108: * Also don't compute a gap for the last one in the list.
109: * The gap for that last one should be considered infinite,
110: * but we never look at it anyway.
111: */
112: if (p == &anchor || p->next == &anchor)
113: return;
114: p->gap = p->next->pos - p->prev->pos;
115: }
116:
117: /*
118: * Add a new line number to the cache.
119: * The specified position (pos) should be the file position of the
120: * FIRST character in the specified line.
121: */
1.8 nicm 122: void
123: add_lnum(LINENUM linenum, off_t pos)
1.1 etheisen 124: {
1.8 nicm 125: struct linenum_info *p;
126: struct linenum_info *new;
127: struct linenum_info *nextp;
128: struct linenum_info *prevp;
129: off_t mingap;
1.1 etheisen 130:
131: /*
132: * Find the proper place in the list for the new one.
133: * The entries are sorted by position.
134: */
135: for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next)
1.4 millert 136: if (p->line == linenum)
1.1 etheisen 137: /* We already have this one. */
138: return;
139: nextp = p;
140: prevp = p->prev;
141:
1.8 nicm 142: if (freelist != NULL) {
1.1 etheisen 143: /*
144: * We still have free (unused) entries.
145: * Use one of them.
146: */
147: new = freelist;
148: freelist = freelist->next;
1.8 nicm 149: } else {
1.1 etheisen 150: /*
151: * No free entries.
152: * Use the "spare" entry.
153: */
154: new = spare;
155: spare = NULL;
156: }
157:
158: /*
159: * Fill in the fields of the new entry,
160: * and insert it into the proper place in the list.
161: */
162: new->next = nextp;
163: new->prev = prevp;
164: new->pos = pos;
1.4 millert 165: new->line = linenum;
1.1 etheisen 166:
167: nextp->prev = new;
168: prevp->next = new;
169:
170: /*
171: * Recalculate gaps for the new entry and the neighboring entries.
172: */
173: calcgap(new);
174: calcgap(nextp);
175: calcgap(prevp);
176:
1.8 nicm 177: if (spare == NULL) {
1.1 etheisen 178: /*
179: * We have used the spare entry.
180: * Scan the list to find the one with the smallest
181: * gap, take it out and make it the spare.
182: * We should never remove the last one, so stop when
183: * we get to p->next == &anchor. This also avoids
184: * looking at the gap of the last one, which is
185: * not computed by calcgap.
186: */
187: mingap = anchor.next->gap;
1.8 nicm 188: for (p = anchor.next; p->next != &anchor; p = p->next) {
189: if (p->gap <= mingap) {
1.1 etheisen 190: spare = p;
191: mingap = p->gap;
192: }
193: }
194: spare->next->prev = spare->prev;
195: spare->prev->next = spare->next;
196: }
197: }
198:
199: /*
200: * If we get stuck in a long loop trying to figure out the
201: * line number, print a message to tell the user what we're doing.
202: */
1.8 nicm 203: static void
204: longloopmessage(void)
1.1 etheisen 205: {
1.12 ! deraadt 206: ierror("Calculating line numbers", NULL);
1.1 etheisen 207: }
208:
209: static int loopcount;
1.9 tedu 210: static time_t startime;
1.1 etheisen 211:
1.8 nicm 212: static void
213: longish(void)
1.1 etheisen 214: {
1.8 nicm 215: if (loopcount >= 0 && ++loopcount > 100) {
1.1 etheisen 216: loopcount = 0;
1.9 tedu 217: if (time(NULL) >= startime + LONGTIME) {
1.1 etheisen 218: longloopmessage();
219: loopcount = -1;
220: }
221: }
222: }
223:
224: /*
1.5 shadchin 225: * Turn off line numbers because the user has interrupted
226: * a lengthy line number calculation.
227: */
1.8 nicm 228: static void
229: abort_long(void)
1.5 shadchin 230: {
231: if (linenums == OPT_ONPLUS)
232: /*
233: * We were displaying line numbers, so need to repaint.
234: */
235: screen_trashed = 1;
236: linenums = 0;
1.12 ! deraadt 237: error("Line numbers turned off", NULL);
1.5 shadchin 238: }
239:
240: /*
1.1 etheisen 241: * Find the line number associated with a given position.
242: * Return 0 if we can't figure it out.
243: */
1.8 nicm 244: LINENUM
245: find_linenum(off_t pos)
1.1 etheisen 246: {
1.8 nicm 247: struct linenum_info *p;
248: LINENUM linenum;
249: off_t cpos;
1.1 etheisen 250:
251: if (!linenums)
252: /*
253: * We're not using line numbers.
254: */
255: return (0);
1.8 nicm 256: if (pos == -1)
1.1 etheisen 257: /*
258: * Caller doesn't know what he's talking about.
259: */
260: return (0);
261: if (pos <= ch_zero())
262: /*
263: * Beginning of file is always line number 1.
264: */
265: return (1);
266:
267: /*
268: * Find the entry nearest to the position we want.
269: */
270: for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next)
271: continue;
272: if (p->pos == pos)
273: /* Found it exactly. */
274: return (p->line);
275:
276: /*
277: * This is the (possibly) time-consuming part.
278: * We start at the line we just found and start
279: * reading the file forward or backward till we
280: * get to the place we want.
281: *
1.8 nicm 282: * First decide whether we should go forward from the
1.1 etheisen 283: * previous one or backwards from the next one.
1.8 nicm 284: * The decision is based on which way involves
1.1 etheisen 285: * traversing fewer bytes in the file.
286: */
1.9 tedu 287: startime = time(NULL);
1.8 nicm 288: if (p == &anchor || pos - p->prev->pos < p->pos - pos) {
1.1 etheisen 289: /*
290: * Go forward.
291: */
292: p = p->prev;
293: if (ch_seek(p->pos))
294: return (0);
295: loopcount = 0;
1.8 nicm 296: for (linenum = p->line, cpos = p->pos; cpos < pos; linenum++) {
1.1 etheisen 297: /*
298: * Allow a signal to abort this loop.
299: */
1.8 nicm 300: cpos = forw_raw_line(cpos, NULL, NULL);
1.5 shadchin 301: if (ABORT_SIGS()) {
302: abort_long();
303: return (0);
304: }
1.8 nicm 305: if (cpos == -1)
1.1 etheisen 306: return (0);
307: longish();
308: }
309: /*
310: * We might as well cache it.
311: */
1.4 millert 312: add_lnum(linenum, cpos);
1.1 etheisen 313: /*
314: * If the given position is not at the start of a line,
315: * make sure we return the correct line number.
316: */
317: if (cpos > pos)
1.4 millert 318: linenum--;
1.8 nicm 319: } else {
1.1 etheisen 320: /*
321: * Go backward.
322: */
323: if (ch_seek(p->pos))
324: return (0);
325: loopcount = 0;
1.8 nicm 326: for (linenum = p->line, cpos = p->pos; cpos > pos; linenum--) {
1.1 etheisen 327: /*
328: * Allow a signal to abort this loop.
329: */
1.8 nicm 330: cpos = back_raw_line(cpos, NULL, NULL);
1.5 shadchin 331: if (ABORT_SIGS()) {
332: abort_long();
333: return (0);
334: }
1.8 nicm 335: if (cpos == -1)
1.1 etheisen 336: return (0);
337: longish();
338: }
339: /*
340: * We might as well cache it.
341: */
1.4 millert 342: add_lnum(linenum, cpos);
1.1 etheisen 343: }
344:
1.4 millert 345: return (linenum);
1.1 etheisen 346: }
347:
348: /*
349: * Find the position of a given line number.
1.8 nicm 350: * Return -1 if we can't figure it out.
1.1 etheisen 351: */
1.8 nicm 352: off_t
353: find_pos(LINENUM linenum)
1.1 etheisen 354: {
1.8 nicm 355: struct linenum_info *p;
356: off_t cpos;
1.4 millert 357: LINENUM clinenum;
1.1 etheisen 358:
1.4 millert 359: if (linenum <= 1)
1.1 etheisen 360: /*
361: * Line number 1 is beginning of file.
362: */
363: return (ch_zero());
364:
365: /*
366: * Find the entry nearest to the line number we want.
367: */
1.4 millert 368: for (p = anchor.next; p != &anchor && p->line < linenum; p = p->next)
1.1 etheisen 369: continue;
1.4 millert 370: if (p->line == linenum)
1.1 etheisen 371: /* Found it exactly. */
372: return (p->pos);
373:
1.8 nicm 374: if (p == &anchor || linenum - p->prev->line < p->line - linenum) {
1.1 etheisen 375: /*
376: * Go forward.
377: */
378: p = p->prev;
379: if (ch_seek(p->pos))
1.8 nicm 380: return (-1);
381: for (clinenum = p->line, cpos = p->pos;
382: clinenum < linenum;
383: clinenum++) {
1.1 etheisen 384: /*
385: * Allow a signal to abort this loop.
386: */
1.8 nicm 387: cpos = forw_raw_line(cpos, NULL, NULL);
1.5 shadchin 388: if (ABORT_SIGS())
1.8 nicm 389: return (-1);
390: if (cpos == -1)
391: return (-1);
1.1 etheisen 392: }
1.8 nicm 393: } else {
1.1 etheisen 394: /*
395: * Go backward.
396: */
397: if (ch_seek(p->pos))
1.8 nicm 398: return (-1);
399: for (clinenum = p->line, cpos = p->pos;
400: clinenum > linenum;
401: clinenum--) {
1.1 etheisen 402: /*
403: * Allow a signal to abort this loop.
404: */
1.5 shadchin 405: cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
406: if (ABORT_SIGS())
1.8 nicm 407: return (-1);
408: if (cpos == -1)
409: return (-1);
1.1 etheisen 410: }
411: }
412: /*
413: * We might as well cache it.
414: */
1.4 millert 415: add_lnum(clinenum, cpos);
1.1 etheisen 416: return (cpos);
417: }
418:
419: /*
420: * Return the line number of the "current" line.
421: * The argument "where" tells which line is to be considered
422: * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc).
423: */
1.8 nicm 424: LINENUM
425: currline(int where)
1.1 etheisen 426: {
1.8 nicm 427: off_t pos;
428: off_t len;
1.4 millert 429: LINENUM linenum;
1.1 etheisen 430:
431: pos = position(where);
432: len = ch_length();
1.8 nicm 433: while (pos == -1 && where >= 0 && where < sc_height)
1.1 etheisen 434: pos = position(++where);
1.8 nicm 435: if (pos == -1)
1.1 etheisen 436: pos = len;
1.4 millert 437: linenum = find_linenum(pos);
1.1 etheisen 438: if (pos == len)
1.4 millert 439: linenum--;
440: return (linenum);
1.1 etheisen 441: }