etc/services - sync with NetBSD-8
[minix.git] / external / bsd / less / dist / linenum.c
blob976ff62b42810f0e14c098d913c4898e82c98184
1 /* $NetBSD: linenum.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
3 /*
4 * Copyright (C) 1984-2012 Mark Nudelman
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.
9 * For more information, see the README file.
14 * Code to handle displaying line numbers.
16 * Finding the line number of a given file position is rather tricky.
17 * We don't want to just start at the beginning of the file and
18 * count newlines, because that is slow for large files (and also
19 * wouldn't work if we couldn't get to the start of the file; e.g.
20 * if input is a long pipe).
22 * So we use the function add_lnum to cache line numbers.
23 * We try to be very clever and keep only the more interesting
24 * line numbers when we run out of space in our table. A line
25 * number is more interesting than another when it is far from
26 * other line numbers. For example, we'd rather keep lines
27 * 100,200,300 than 100,101,300. 200 is more interesting than
28 * 101 because 101 can be derived very cheaply from 100, while
29 * 200 is more expensive to derive from 100.
31 * The function currline() returns the line number of a given
32 * position in the file. As a side effect, it calls add_lnum
33 * to cache the line number. Therefore currline is occasionally
34 * called to make sure we cache line numbers often enough.
37 #include "less.h"
40 * Structure to keep track of a line number and the associated file position.
41 * A doubly-linked circular list of line numbers is kept ordered by line number.
43 struct linenum_info
45 struct linenum_info *next; /* Link to next in the list */
46 struct linenum_info *prev; /* Line to previous in the list */
47 POSITION pos; /* File position */
48 POSITION gap; /* Gap between prev and next */
49 LINENUM line; /* Line number */
52 * "gap" needs some explanation: the gap of any particular line number
53 * is the distance between the previous one and the next one in the list.
54 * ("Distance" means difference in file position.) In other words, the
55 * gap of a line number is the gap which would be introduced if this
56 * line number were deleted. It is used to decide which one to replace
57 * when we have a new one to insert and the table is full.
60 #define NPOOL 200 /* Size of line number pool */
62 #define LONGTIME (2) /* In seconds */
64 static struct linenum_info anchor; /* Anchor of the list */
65 static struct linenum_info *freelist; /* Anchor of the unused entries */
66 static struct linenum_info pool[NPOOL]; /* The pool itself */
67 static struct linenum_info *spare; /* We always keep one spare entry */
69 extern int linenums;
70 extern int sigs;
71 extern int sc_height;
72 extern int screen_trashed;
74 static void calcgap __P((struct linenum_info *));
75 static void longloopmessage __P((void));
76 static void longish __P((void));
79 * Initialize the line number structures.
81 public void
82 clr_linenum()
84 register struct linenum_info *p;
87 * Put all the entries on the free list.
88 * Leave one for the "spare".
90 for (p = pool; p < &pool[NPOOL-2]; p++)
91 p->next = p+1;
92 pool[NPOOL-2].next = NULL;
93 freelist = pool;
95 spare = &pool[NPOOL-1];
98 * Initialize the anchor.
100 anchor.next = anchor.prev = &anchor;
101 anchor.gap = 0;
102 anchor.pos = (POSITION)0;
103 anchor.line = 1;
107 * Calculate the gap for an entry.
109 static void
110 calcgap(p)
111 register struct linenum_info *p;
114 * Don't bother to compute a gap for the anchor.
115 * Also don't compute a gap for the last one in the list.
116 * The gap for that last one should be considered infinite,
117 * but we never look at it anyway.
119 if (p == &anchor || p->next == &anchor)
120 return;
121 p->gap = p->next->pos - p->prev->pos;
125 * Add a new line number to the cache.
126 * The specified position (pos) should be the file position of the
127 * FIRST character in the specified line.
129 public void
130 add_lnum(linenum, pos)
131 LINENUM linenum;
132 POSITION pos;
134 register struct linenum_info *p;
135 register struct linenum_info *new;
136 register struct linenum_info *nextp;
137 register struct linenum_info *prevp;
138 register POSITION mingap;
141 * Find the proper place in the list for the new one.
142 * The entries are sorted by position.
144 for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next)
145 if (p->line == linenum)
146 /* We already have this one. */
147 return;
148 nextp = p;
149 prevp = p->prev;
151 if (freelist != NULL)
154 * We still have free (unused) entries.
155 * Use one of them.
157 new = freelist;
158 freelist = freelist->next;
159 } else
162 * No free entries.
163 * Use the "spare" entry.
165 new = spare;
166 spare = NULL;
170 * Fill in the fields of the new entry,
171 * and insert it into the proper place in the list.
173 new->next = nextp;
174 new->prev = prevp;
175 new->pos = pos;
176 new->line = linenum;
178 nextp->prev = new;
179 prevp->next = new;
182 * Recalculate gaps for the new entry and the neighboring entries.
184 calcgap(new);
185 calcgap(nextp);
186 calcgap(prevp);
188 if (spare == NULL)
191 * We have used the spare entry.
192 * Scan the list to find the one with the smallest
193 * gap, take it out and make it the spare.
194 * We should never remove the last one, so stop when
195 * we get to p->next == &anchor. This also avoids
196 * looking at the gap of the last one, which is
197 * not computed by calcgap.
199 mingap = anchor.next->gap;
200 for (p = anchor.next; p->next != &anchor; p = p->next)
202 if (p->gap <= mingap)
204 spare = p;
205 mingap = p->gap;
208 spare->next->prev = spare->prev;
209 spare->prev->next = spare->next;
214 * If we get stuck in a long loop trying to figure out the
215 * line number, print a message to tell the user what we're doing.
217 static void
218 longloopmessage()
220 ierror("Calculating line numbers", NULL_PARG);
223 static int loopcount;
224 #if HAVE_TIME
225 static long startime;
226 #endif
228 static void
229 longish()
231 #if HAVE_TIME
232 if (loopcount >= 0 && ++loopcount > 100)
234 loopcount = 0;
235 if (get_time() >= startime + LONGTIME)
237 longloopmessage();
238 loopcount = -1;
241 #else
242 if (loopcount >= 0 && ++loopcount > LONGLOOP)
244 longloopmessage();
245 loopcount = -1;
247 #endif
251 * Turn off line numbers because the user has interrupted
252 * a lengthy line number calculation.
254 static void
255 abort_long()
257 if (linenums == OPT_ONPLUS)
259 * We were displaying line numbers, so need to repaint.
261 screen_trashed = 1;
262 linenums = 0;
263 error("Line numbers turned off", NULL_PARG);
267 * Find the line number associated with a given position.
268 * Return 0 if we can't figure it out.
270 public LINENUM
271 find_linenum(pos)
272 POSITION pos;
274 register struct linenum_info *p;
275 register LINENUM linenum;
276 POSITION cpos;
278 if (!linenums)
280 * We're not using line numbers.
282 return (0);
283 if (pos == NULL_POSITION)
285 * Caller doesn't know what he's talking about.
287 return (0);
288 if (pos <= ch_zero())
290 * Beginning of file is always line number 1.
292 return (1);
295 * Find the entry nearest to the position we want.
297 for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next)
298 continue;
299 if (p->pos == pos)
300 /* Found it exactly. */
301 return (p->line);
304 * This is the (possibly) time-consuming part.
305 * We start at the line we just found and start
306 * reading the file forward or backward till we
307 * get to the place we want.
309 * First decide whether we should go forward from the
310 * previous one or backwards from the next one.
311 * The decision is based on which way involves
312 * traversing fewer bytes in the file.
314 #if HAVE_TIME
315 startime = get_time();
316 #endif
317 if (p == &anchor || pos - p->prev->pos < p->pos - pos)
320 * Go forward.
322 p = p->prev;
323 if (ch_seek(p->pos))
324 return (0);
325 loopcount = 0;
326 for (linenum = p->line, cpos = p->pos; cpos < pos; linenum++)
329 * Allow a signal to abort this loop.
331 cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL);
332 if (ABORT_SIGS()) {
333 abort_long();
334 return (0);
336 if (cpos == NULL_POSITION)
337 return (0);
338 longish();
341 * We might as well cache it.
343 add_lnum(linenum, cpos);
345 * If the given position is not at the start of a line,
346 * make sure we return the correct line number.
348 if (cpos > pos)
349 linenum--;
350 } else
353 * Go backward.
355 if (ch_seek(p->pos))
356 return (0);
357 loopcount = 0;
358 for (linenum = p->line, cpos = p->pos; cpos > pos; linenum--)
361 * Allow a signal to abort this loop.
363 cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
364 if (ABORT_SIGS()) {
365 abort_long();
366 return (0);
368 if (cpos == NULL_POSITION)
369 return (0);
370 longish();
373 * We might as well cache it.
375 add_lnum(linenum, cpos);
378 return (linenum);
382 * Find the position of a given line number.
383 * Return NULL_POSITION if we can't figure it out.
385 public POSITION
386 find_pos(linenum)
387 LINENUM linenum;
389 register struct linenum_info *p;
390 POSITION cpos;
391 LINENUM clinenum;
393 if (linenum <= 1)
395 * Line number 1 is beginning of file.
397 return (ch_zero());
400 * Find the entry nearest to the line number we want.
402 for (p = anchor.next; p != &anchor && p->line < linenum; p = p->next)
403 continue;
404 if (p->line == linenum)
405 /* Found it exactly. */
406 return (p->pos);
408 if (p == &anchor || linenum - p->prev->line < p->line - linenum)
411 * Go forward.
413 p = p->prev;
414 if (ch_seek(p->pos))
415 return (NULL_POSITION);
416 for (clinenum = p->line, cpos = p->pos; clinenum < linenum; clinenum++)
419 * Allow a signal to abort this loop.
421 cpos = forw_raw_line(cpos, (char **)NULL, (int *)NULL);
422 if (ABORT_SIGS())
423 return (NULL_POSITION);
424 if (cpos == NULL_POSITION)
425 return (NULL_POSITION);
427 } else
430 * Go backward.
432 if (ch_seek(p->pos))
433 return (NULL_POSITION);
434 for (clinenum = p->line, cpos = p->pos; clinenum > linenum; clinenum--)
437 * Allow a signal to abort this loop.
439 cpos = back_raw_line(cpos, (char **)NULL, (int *)NULL);
440 if (ABORT_SIGS())
441 return (NULL_POSITION);
442 if (cpos == NULL_POSITION)
443 return (NULL_POSITION);
447 * We might as well cache it.
449 add_lnum(clinenum, cpos);
450 return (cpos);
454 * Return the line number of the "current" line.
455 * The argument "where" tells which line is to be considered
456 * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc).
458 public LINENUM
459 currline(where)
460 int where;
462 POSITION pos;
463 POSITION len;
464 LINENUM linenum;
466 pos = position(where);
467 len = ch_length();
468 while (pos == NULL_POSITION && where >= 0 && where < sc_height)
469 pos = position(++where);
470 if (pos == NULL_POSITION)
471 pos = len;
472 linenum = find_linenum(pos);
473 if (pos == len)
474 linenum--;
475 return (linenum);