linenum.c revision 1.4 1 1.4 agc /* $NetBSD: linenum.c,v 1.4 2003/08/07 09:28:00 agc Exp $ */
2 1.2 perry
3 1.1 cjs /*
4 1.1 cjs * Copyright (c) 1988, 1993
5 1.1 cjs * The Regents of the University of California. All rights reserved.
6 1.1 cjs *
7 1.1 cjs * Redistribution and use in source and binary forms, with or without
8 1.1 cjs * modification, are permitted provided that the following conditions
9 1.1 cjs * are met:
10 1.1 cjs * 1. Redistributions of source code must retain the above copyright
11 1.1 cjs * notice, this list of conditions and the following disclaimer.
12 1.1 cjs * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cjs * notice, this list of conditions and the following disclaimer in the
14 1.1 cjs * documentation and/or other materials provided with the distribution.
15 1.4 agc * 3. Neither the name of the University nor the names of its contributors
16 1.4 agc * may be used to endorse or promote products derived from this software
17 1.4 agc * without specific prior written permission.
18 1.4 agc *
19 1.4 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.4 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.4 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.4 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.4 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.4 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.4 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.4 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.4 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.4 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.4 agc * SUCH DAMAGE.
30 1.4 agc */
31 1.4 agc
32 1.4 agc /*
33 1.4 agc * Copyright (c) 1988 Mark Nudleman
34 1.4 agc *
35 1.4 agc * Redistribution and use in source and binary forms, with or without
36 1.4 agc * modification, are permitted provided that the following conditions
37 1.4 agc * are met:
38 1.4 agc * 1. Redistributions of source code must retain the above copyright
39 1.4 agc * notice, this list of conditions and the following disclaimer.
40 1.4 agc * 2. Redistributions in binary form must reproduce the above copyright
41 1.4 agc * notice, this list of conditions and the following disclaimer in the
42 1.4 agc * documentation and/or other materials provided with the distribution.
43 1.1 cjs * 3. All advertising materials mentioning features or use of this software
44 1.1 cjs * must display the following acknowledgement:
45 1.1 cjs * This product includes software developed by the University of
46 1.1 cjs * California, Berkeley and its contributors.
47 1.1 cjs * 4. Neither the name of the University nor the names of its contributors
48 1.1 cjs * may be used to endorse or promote products derived from this software
49 1.1 cjs * without specific prior written permission.
50 1.1 cjs *
51 1.1 cjs * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 1.1 cjs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 1.1 cjs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 1.1 cjs * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 1.1 cjs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 1.1 cjs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 1.1 cjs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 1.1 cjs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 1.1 cjs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 1.1 cjs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 1.1 cjs * SUCH DAMAGE.
62 1.1 cjs */
63 1.1 cjs
64 1.3 christos #include <sys/cdefs.h>
65 1.1 cjs #ifndef lint
66 1.3 christos #if 0
67 1.1 cjs static char sccsid[] = "@(#)linenum.c 8.1 (Berkeley) 6/6/93";
68 1.3 christos #else
69 1.4 agc __RCSID("$NetBSD: linenum.c,v 1.4 2003/08/07 09:28:00 agc Exp $");
70 1.3 christos #endif
71 1.1 cjs #endif /* not lint */
72 1.1 cjs
73 1.1 cjs /*
74 1.1 cjs * Code to handle displaying line numbers.
75 1.1 cjs *
76 1.1 cjs * Finding the line number of a given file position is rather tricky.
77 1.1 cjs * We don't want to just start at the beginning of the file and
78 1.1 cjs * count newlines, because that is slow for large files (and also
79 1.1 cjs * wouldn't work if we couldn't get to the start of the file; e.g.
80 1.1 cjs * if input is a long pipe).
81 1.1 cjs *
82 1.1 cjs * So we use the function add_lnum to cache line numbers.
83 1.1 cjs * We try to be very clever and keep only the more interesting
84 1.1 cjs * line numbers when we run out of space in our table. A line
85 1.1 cjs * number is more interesting than another when it is far from
86 1.1 cjs * other line numbers. For example, we'd rather keep lines
87 1.1 cjs * 100,200,300 than 100,101,300. 200 is more interesting than
88 1.1 cjs * 101 because 101 can be derived very cheaply from 100, while
89 1.1 cjs * 200 is more expensive to derive from 100.
90 1.1 cjs *
91 1.1 cjs * The function currline() returns the line number of a given
92 1.1 cjs * position in the file. As a side effect, it calls add_lnum
93 1.1 cjs * to cache the line number. Therefore currline is occasionally
94 1.1 cjs * called to make sure we cache line numbers often enough.
95 1.1 cjs */
96 1.1 cjs
97 1.1 cjs #include <sys/types.h>
98 1.1 cjs #include <stdio.h>
99 1.3 christos #include <time.h>
100 1.3 christos
101 1.3 christos #include "less.h"
102 1.3 christos #include "extern.h"
103 1.1 cjs
104 1.1 cjs /*
105 1.1 cjs * Structure to keep track of a line number and the associated file position.
106 1.1 cjs * A doubly-linked circular list of line numbers is kept ordered by line number.
107 1.1 cjs */
108 1.1 cjs struct linenum
109 1.1 cjs {
110 1.1 cjs struct linenum *next; /* Link to next in the list */
111 1.1 cjs struct linenum *prev; /* Line to previous in the list */
112 1.1 cjs off_t pos; /* File position */
113 1.1 cjs off_t gap; /* Gap between prev and next */
114 1.1 cjs int line; /* Line number */
115 1.1 cjs };
116 1.1 cjs /*
117 1.1 cjs * "gap" needs some explanation: the gap of any particular line number
118 1.1 cjs * is the distance between the previous one and the next one in the list.
119 1.1 cjs * ("Distance" means difference in file position.) In other words, the
120 1.1 cjs * gap of a line number is the gap which would be introduced if this
121 1.1 cjs * line number were deleted. It is used to decide which one to replace
122 1.1 cjs * when we have a new one to insert and the table is full.
123 1.1 cjs */
124 1.1 cjs
125 1.1 cjs #define NPOOL 50 /* Size of line number pool */
126 1.1 cjs
127 1.1 cjs #define LONGTIME (2) /* In seconds */
128 1.1 cjs
129 1.1 cjs int lnloop = 0; /* Are we in the line num loop? */
130 1.1 cjs
131 1.1 cjs static struct linenum anchor; /* Anchor of the list */
132 1.1 cjs static struct linenum *freelist; /* Anchor of the unused entries */
133 1.1 cjs static struct linenum pool[NPOOL]; /* The pool itself */
134 1.1 cjs static struct linenum *spare; /* We always keep one spare entry */
135 1.1 cjs
136 1.3 christos static void calcgap __P((struct linenum *));
137 1.3 christos static void longloopmessage __P((void));
138 1.1 cjs /*
139 1.1 cjs * Initialize the line number structures.
140 1.1 cjs */
141 1.3 christos void
142 1.1 cjs clr_linenum()
143 1.1 cjs {
144 1.3 christos struct linenum *p;
145 1.1 cjs
146 1.1 cjs /*
147 1.1 cjs * Put all the entries on the free list.
148 1.1 cjs * Leave one for the "spare".
149 1.1 cjs */
150 1.1 cjs for (p = pool; p < &pool[NPOOL-2]; p++)
151 1.1 cjs p->next = p+1;
152 1.1 cjs pool[NPOOL-2].next = NULL;
153 1.1 cjs freelist = pool;
154 1.1 cjs
155 1.1 cjs spare = &pool[NPOOL-1];
156 1.1 cjs
157 1.1 cjs /*
158 1.1 cjs * Initialize the anchor.
159 1.1 cjs */
160 1.1 cjs anchor.next = anchor.prev = &anchor;
161 1.1 cjs anchor.gap = 0;
162 1.1 cjs anchor.pos = (off_t)0;
163 1.1 cjs anchor.line = 1;
164 1.1 cjs }
165 1.1 cjs
166 1.1 cjs /*
167 1.1 cjs * Calculate the gap for an entry.
168 1.1 cjs */
169 1.3 christos static void
170 1.1 cjs calcgap(p)
171 1.3 christos struct linenum *p;
172 1.1 cjs {
173 1.1 cjs /*
174 1.1 cjs * Don't bother to compute a gap for the anchor.
175 1.1 cjs * Also don't compute a gap for the last one in the list.
176 1.1 cjs * The gap for that last one should be considered infinite,
177 1.1 cjs * but we never look at it anyway.
178 1.1 cjs */
179 1.1 cjs if (p == &anchor || p->next == &anchor)
180 1.1 cjs return;
181 1.1 cjs p->gap = p->next->pos - p->prev->pos;
182 1.1 cjs }
183 1.1 cjs
184 1.1 cjs /*
185 1.1 cjs * Add a new line number to the cache.
186 1.1 cjs * The specified position (pos) should be the file position of the
187 1.1 cjs * FIRST character in the specified line.
188 1.1 cjs */
189 1.3 christos void
190 1.1 cjs add_lnum(line, pos)
191 1.1 cjs int line;
192 1.1 cjs off_t pos;
193 1.1 cjs {
194 1.3 christos struct linenum *p;
195 1.3 christos struct linenum *new;
196 1.3 christos struct linenum *nextp;
197 1.3 christos struct linenum *prevp;
198 1.3 christos off_t mingap;
199 1.1 cjs
200 1.1 cjs /*
201 1.1 cjs * Find the proper place in the list for the new one.
202 1.1 cjs * The entries are sorted by position.
203 1.1 cjs */
204 1.1 cjs for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next)
205 1.1 cjs if (p->line == line)
206 1.1 cjs /* We already have this one. */
207 1.1 cjs return;
208 1.1 cjs nextp = p;
209 1.1 cjs prevp = p->prev;
210 1.1 cjs
211 1.1 cjs if (freelist != NULL)
212 1.1 cjs {
213 1.1 cjs /*
214 1.1 cjs * We still have free (unused) entries.
215 1.1 cjs * Use one of them.
216 1.1 cjs */
217 1.1 cjs new = freelist;
218 1.1 cjs freelist = freelist->next;
219 1.1 cjs } else
220 1.1 cjs {
221 1.1 cjs /*
222 1.1 cjs * No free entries.
223 1.1 cjs * Use the "spare" entry.
224 1.1 cjs */
225 1.1 cjs new = spare;
226 1.1 cjs spare = NULL;
227 1.1 cjs }
228 1.1 cjs
229 1.1 cjs /*
230 1.1 cjs * Fill in the fields of the new entry,
231 1.1 cjs * and insert it into the proper place in the list.
232 1.1 cjs */
233 1.1 cjs new->next = nextp;
234 1.1 cjs new->prev = prevp;
235 1.1 cjs new->pos = pos;
236 1.1 cjs new->line = line;
237 1.1 cjs
238 1.1 cjs nextp->prev = new;
239 1.1 cjs prevp->next = new;
240 1.1 cjs
241 1.1 cjs /*
242 1.1 cjs * Recalculate gaps for the new entry and the neighboring entries.
243 1.1 cjs */
244 1.1 cjs calcgap(new);
245 1.1 cjs calcgap(nextp);
246 1.1 cjs calcgap(prevp);
247 1.1 cjs
248 1.1 cjs if (spare == NULL)
249 1.1 cjs {
250 1.1 cjs /*
251 1.1 cjs * We have used the spare entry.
252 1.1 cjs * Scan the list to find the one with the smallest
253 1.1 cjs * gap, take it out and make it the spare.
254 1.1 cjs * We should never remove the last one, so stop when
255 1.1 cjs * we get to p->next == &anchor. This also avoids
256 1.1 cjs * looking at the gap of the last one, which is
257 1.1 cjs * not computed by calcgap.
258 1.1 cjs */
259 1.1 cjs mingap = anchor.next->gap;
260 1.1 cjs for (p = anchor.next; p->next != &anchor; p = p->next)
261 1.1 cjs {
262 1.1 cjs if (p->gap <= mingap)
263 1.1 cjs {
264 1.1 cjs spare = p;
265 1.1 cjs mingap = p->gap;
266 1.1 cjs }
267 1.1 cjs }
268 1.1 cjs spare->next->prev = spare->prev;
269 1.1 cjs spare->prev->next = spare->next;
270 1.1 cjs }
271 1.1 cjs }
272 1.1 cjs
273 1.1 cjs /*
274 1.1 cjs * If we get stuck in a long loop trying to figure out the
275 1.1 cjs * line number, print a message to tell the user what we're doing.
276 1.1 cjs */
277 1.3 christos static void
278 1.1 cjs longloopmessage()
279 1.1 cjs {
280 1.1 cjs ierror("Calculating line numbers");
281 1.1 cjs /*
282 1.1 cjs * Set the lnloop flag here, so if the user interrupts while
283 1.1 cjs * we are calculating line numbers, the signal handler will
284 1.1 cjs * turn off line numbers (linenums=0).
285 1.1 cjs */
286 1.1 cjs lnloop = 1;
287 1.1 cjs }
288 1.1 cjs
289 1.1 cjs /*
290 1.1 cjs * Find the line number associated with a given position.
291 1.1 cjs * Return 0 if we can't figure it out.
292 1.1 cjs */
293 1.3 christos int
294 1.1 cjs find_linenum(pos)
295 1.1 cjs off_t pos;
296 1.1 cjs {
297 1.3 christos struct linenum *p;
298 1.3 christos int lno;
299 1.3 christos int loopcount;
300 1.3 christos off_t cpos;
301 1.3 christos time_t startime;
302 1.1 cjs
303 1.1 cjs if (!linenums)
304 1.1 cjs /*
305 1.1 cjs * We're not using line numbers.
306 1.1 cjs */
307 1.1 cjs return (0);
308 1.1 cjs if (pos == NULL_POSITION)
309 1.1 cjs /*
310 1.1 cjs * Caller doesn't know what he's talking about.
311 1.1 cjs */
312 1.1 cjs return (0);
313 1.1 cjs if (pos == (off_t)0)
314 1.1 cjs /*
315 1.1 cjs * Beginning of file is always line number 1.
316 1.1 cjs */
317 1.1 cjs return (1);
318 1.1 cjs
319 1.1 cjs /*
320 1.1 cjs * Find the entry nearest to the position we want.
321 1.1 cjs */
322 1.1 cjs for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next)
323 1.1 cjs continue;
324 1.1 cjs if (p->pos == pos)
325 1.1 cjs /* Found it exactly. */
326 1.1 cjs return (p->line);
327 1.1 cjs
328 1.1 cjs /*
329 1.1 cjs * This is the (possibly) time-consuming part.
330 1.1 cjs * We start at the line we just found and start
331 1.1 cjs * reading the file forward or backward till we
332 1.1 cjs * get to the place we want.
333 1.1 cjs *
334 1.1 cjs * First decide whether we should go forward from the
335 1.1 cjs * previous one or backwards from the next one.
336 1.1 cjs * The decision is based on which way involves
337 1.1 cjs * traversing fewer bytes in the file.
338 1.1 cjs */
339 1.1 cjs flush();
340 1.1 cjs (void)time(&startime);
341 1.1 cjs if (p == &anchor || pos - p->prev->pos < p->pos - pos)
342 1.1 cjs {
343 1.1 cjs /*
344 1.1 cjs * Go forward.
345 1.1 cjs */
346 1.1 cjs p = p->prev;
347 1.1 cjs if (ch_seek(p->pos))
348 1.1 cjs return (0);
349 1.1 cjs loopcount = 0;
350 1.1 cjs for (lno = p->line, cpos = p->pos; cpos < pos; lno++)
351 1.1 cjs {
352 1.1 cjs /*
353 1.1 cjs * Allow a signal to abort this loop.
354 1.1 cjs */
355 1.1 cjs cpos = forw_raw_line(cpos);
356 1.1 cjs if (sigs || cpos == NULL_POSITION)
357 1.1 cjs return (0);
358 1.1 cjs if (loopcount >= 0 && ++loopcount > 100) {
359 1.1 cjs loopcount = 0;
360 1.1 cjs if (time((time_t *)NULL)
361 1.1 cjs >= startime + LONGTIME) {
362 1.1 cjs longloopmessage();
363 1.1 cjs loopcount = -1;
364 1.1 cjs }
365 1.1 cjs }
366 1.1 cjs }
367 1.1 cjs lnloop = 0;
368 1.1 cjs /*
369 1.1 cjs * If the given position is not at the start of a line,
370 1.1 cjs * make sure we return the correct line number.
371 1.1 cjs */
372 1.1 cjs if (cpos > pos)
373 1.1 cjs lno--;
374 1.1 cjs } else
375 1.1 cjs {
376 1.1 cjs /*
377 1.1 cjs * Go backward.
378 1.1 cjs */
379 1.1 cjs if (ch_seek(p->pos))
380 1.1 cjs return (0);
381 1.1 cjs loopcount = 0;
382 1.1 cjs for (lno = p->line, cpos = p->pos; cpos > pos; lno--)
383 1.1 cjs {
384 1.1 cjs /*
385 1.1 cjs * Allow a signal to abort this loop.
386 1.1 cjs */
387 1.1 cjs cpos = back_raw_line(cpos);
388 1.1 cjs if (sigs || cpos == NULL_POSITION)
389 1.1 cjs return (0);
390 1.1 cjs if (loopcount >= 0 && ++loopcount > 100) {
391 1.1 cjs loopcount = 0;
392 1.1 cjs if (time((time_t *)NULL)
393 1.1 cjs >= startime + LONGTIME) {
394 1.1 cjs longloopmessage();
395 1.1 cjs loopcount = -1;
396 1.1 cjs }
397 1.1 cjs }
398 1.1 cjs }
399 1.1 cjs lnloop = 0;
400 1.1 cjs }
401 1.1 cjs
402 1.1 cjs /*
403 1.1 cjs * We might as well cache it.
404 1.1 cjs */
405 1.1 cjs add_lnum(lno, cpos);
406 1.1 cjs return (lno);
407 1.1 cjs }
408 1.1 cjs
409 1.1 cjs /*
410 1.1 cjs * Return the line number of the "current" line.
411 1.1 cjs * The argument "where" tells which line is to be considered
412 1.1 cjs * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc).
413 1.1 cjs */
414 1.3 christos int
415 1.1 cjs currline(where)
416 1.1 cjs int where;
417 1.1 cjs {
418 1.3 christos off_t pos;
419 1.1 cjs
420 1.1 cjs if ((pos = position(where)) == NULL_POSITION)
421 1.1 cjs pos = ch_length();
422 1.1 cjs return(find_linenum(pos));
423 1.1 cjs }
424