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