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