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