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