xref: /dflybsd-src/contrib/less/linenum.c (revision e0f238eda64c20d98364903e0c003825fd0b66dd)
11133e27eSPeter Avalos /*
2*e433da38SAaron LI  * Copyright (C) 1984-2024  Mark Nudelman
31133e27eSPeter Avalos  *
41133e27eSPeter Avalos  * You may distribute under the terms of either the GNU General Public
51133e27eSPeter Avalos  * License or the Less License, as specified in the README file.
61133e27eSPeter Avalos  *
7e639dc31SJohn Marino  * For more information, see the README file.
81133e27eSPeter Avalos  */
91133e27eSPeter Avalos 
101133e27eSPeter Avalos 
111133e27eSPeter Avalos /*
121133e27eSPeter Avalos  * Code to handle displaying line numbers.
131133e27eSPeter Avalos  *
141133e27eSPeter Avalos  * Finding the line number of a given file position is rather tricky.
151133e27eSPeter Avalos  * We don't want to just start at the beginning of the file and
161133e27eSPeter Avalos  * count newlines, because that is slow for large files (and also
171133e27eSPeter Avalos  * wouldn't work if we couldn't get to the start of the file; e.g.
181133e27eSPeter Avalos  * if input is a long pipe).
191133e27eSPeter Avalos  *
201133e27eSPeter Avalos  * So we use the function add_lnum to cache line numbers.
211133e27eSPeter Avalos  * We try to be very clever and keep only the more interesting
221133e27eSPeter Avalos  * line numbers when we run out of space in our table.  A line
231133e27eSPeter Avalos  * number is more interesting than another when it is far from
241133e27eSPeter Avalos  * other line numbers.   For example, we'd rather keep lines
251133e27eSPeter Avalos  * 100,200,300 than 100,101,300.  200 is more interesting than
261133e27eSPeter Avalos  * 101 because 101 can be derived very cheaply from 100, while
271133e27eSPeter Avalos  * 200 is more expensive to derive from 100.
281133e27eSPeter Avalos  *
291133e27eSPeter Avalos  * The function currline() returns the line number of a given
301133e27eSPeter Avalos  * position in the file.  As a side effect, it calls add_lnum
311133e27eSPeter Avalos  * to cache the line number.  Therefore currline is occasionally
321133e27eSPeter Avalos  * called to make sure we cache line numbers often enough.
331133e27eSPeter Avalos  */
341133e27eSPeter Avalos 
351133e27eSPeter Avalos #include "less.h"
361133e27eSPeter Avalos 
371133e27eSPeter Avalos /*
381133e27eSPeter Avalos  * Structure to keep track of a line number and the associated file position.
391133e27eSPeter Avalos  * A doubly-linked circular list of line numbers is kept ordered by line number.
401133e27eSPeter Avalos  */
411133e27eSPeter Avalos struct linenum_info
421133e27eSPeter Avalos {
431133e27eSPeter Avalos 	struct linenum_info *next;      /* Link to next in the list */
441133e27eSPeter Avalos 	struct linenum_info *prev;      /* Line to previous in the list */
451133e27eSPeter Avalos 	POSITION pos;                   /* File position */
461133e27eSPeter Avalos 	POSITION gap;                   /* Gap between prev and next */
471133e27eSPeter Avalos 	LINENUM line;                   /* Line number */
481133e27eSPeter Avalos };
491133e27eSPeter Avalos /*
501133e27eSPeter Avalos  * "gap" needs some explanation: the gap of any particular line number
511133e27eSPeter Avalos  * is the distance between the previous one and the next one in the list.
521133e27eSPeter Avalos  * ("Distance" means difference in file position.)  In other words, the
531133e27eSPeter Avalos  * gap of a line number is the gap which would be introduced if this
541133e27eSPeter Avalos  * line number were deleted.  It is used to decide which one to replace
551133e27eSPeter Avalos  * when we have a new one to insert and the table is full.
561133e27eSPeter Avalos  */
571133e27eSPeter Avalos 
588be36e5bSPeter Avalos #define NPOOL   200                     /* Size of line number pool */
591133e27eSPeter Avalos 
601133e27eSPeter Avalos #define LONGTIME        (2)             /* In seconds */
611133e27eSPeter Avalos 
621133e27eSPeter Avalos static struct linenum_info anchor;      /* Anchor of the list */
631133e27eSPeter Avalos static struct linenum_info *freelist;   /* Anchor of the unused entries */
641133e27eSPeter Avalos static struct linenum_info pool[NPOOL]; /* The pool itself */
651133e27eSPeter Avalos static struct linenum_info *spare;      /* We always keep one spare entry */
66*e433da38SAaron LI public lbool scanning_eof = FALSE;
671133e27eSPeter Avalos 
681133e27eSPeter Avalos extern int linenums;
691133e27eSPeter Avalos extern int sigs;
701133e27eSPeter Avalos extern int sc_height;
710c7ad07eSAntonio Huete Jimenez extern int header_lines;
720c7ad07eSAntonio Huete Jimenez extern int nonum_headers;
731133e27eSPeter Avalos 
741133e27eSPeter Avalos /*
751133e27eSPeter Avalos  * Initialize the line number structures.
761133e27eSPeter Avalos  */
clr_linenum(void)77320d7c8aSAaron LI public void clr_linenum(void)
781133e27eSPeter Avalos {
7902d62a0fSDaniel Fojt 	struct linenum_info *p;
801133e27eSPeter Avalos 
811133e27eSPeter Avalos 	/*
821133e27eSPeter Avalos 	 * Put all the entries on the free list.
831133e27eSPeter Avalos 	 * Leave one for the "spare".
841133e27eSPeter Avalos 	 */
851133e27eSPeter Avalos 	for (p = pool;  p < &pool[NPOOL-2];  p++)
861133e27eSPeter Avalos 		p->next = p+1;
871133e27eSPeter Avalos 	pool[NPOOL-2].next = NULL;
881133e27eSPeter Avalos 	freelist = pool;
891133e27eSPeter Avalos 
901133e27eSPeter Avalos 	spare = &pool[NPOOL-1];
911133e27eSPeter Avalos 
921133e27eSPeter Avalos 	/*
931133e27eSPeter Avalos 	 * Initialize the anchor.
941133e27eSPeter Avalos 	 */
951133e27eSPeter Avalos 	anchor.next = anchor.prev = &anchor;
961133e27eSPeter Avalos 	anchor.gap = 0;
971133e27eSPeter Avalos 	anchor.pos = (POSITION)0;
981133e27eSPeter Avalos 	anchor.line = 1;
991133e27eSPeter Avalos }
1001133e27eSPeter Avalos 
1011133e27eSPeter Avalos /*
1021133e27eSPeter Avalos  * Calculate the gap for an entry.
1031133e27eSPeter Avalos  */
calcgap(struct linenum_info * p)104320d7c8aSAaron LI static void calcgap(struct linenum_info *p)
1051133e27eSPeter Avalos {
1061133e27eSPeter Avalos 	/*
1071133e27eSPeter Avalos 	 * Don't bother to compute a gap for the anchor.
1081133e27eSPeter Avalos 	 * Also don't compute a gap for the last one in the list.
1091133e27eSPeter Avalos 	 * The gap for that last one should be considered infinite,
1101133e27eSPeter Avalos 	 * but we never look at it anyway.
1111133e27eSPeter Avalos 	 */
1121133e27eSPeter Avalos 	if (p == &anchor || p->next == &anchor)
1131133e27eSPeter Avalos 		return;
1141133e27eSPeter Avalos 	p->gap = p->next->pos - p->prev->pos;
1151133e27eSPeter Avalos }
1161133e27eSPeter Avalos 
1171133e27eSPeter Avalos /*
1181133e27eSPeter Avalos  * Add a new line number to the cache.
1191133e27eSPeter Avalos  * The specified position (pos) should be the file position of the
1201133e27eSPeter Avalos  * FIRST character in the specified line.
1211133e27eSPeter Avalos  */
add_lnum(LINENUM linenum,POSITION pos)122320d7c8aSAaron LI public void add_lnum(LINENUM linenum, POSITION pos)
1231133e27eSPeter Avalos {
12402d62a0fSDaniel Fojt 	struct linenum_info *p;
12502d62a0fSDaniel Fojt 	struct linenum_info *new;
12602d62a0fSDaniel Fojt 	struct linenum_info *nextp;
12702d62a0fSDaniel Fojt 	struct linenum_info *prevp;
12802d62a0fSDaniel Fojt 	POSITION mingap;
1291133e27eSPeter Avalos 
1301133e27eSPeter Avalos 	/*
1311133e27eSPeter Avalos 	 * Find the proper place in the list for the new one.
1321133e27eSPeter Avalos 	 * The entries are sorted by position.
1331133e27eSPeter Avalos 	 */
1341133e27eSPeter Avalos 	for (p = anchor.next;  p != &anchor && p->pos < pos;  p = p->next)
1351133e27eSPeter Avalos 		if (p->line == linenum)
1361133e27eSPeter Avalos 			/* We already have this one. */
1371133e27eSPeter Avalos 			return;
1381133e27eSPeter Avalos 	nextp = p;
1391133e27eSPeter Avalos 	prevp = p->prev;
1401133e27eSPeter Avalos 
1411133e27eSPeter Avalos 	if (freelist != NULL)
1421133e27eSPeter Avalos 	{
1431133e27eSPeter Avalos 		/*
1441133e27eSPeter Avalos 		 * We still have free (unused) entries.
1451133e27eSPeter Avalos 		 * Use one of them.
1461133e27eSPeter Avalos 		 */
1471133e27eSPeter Avalos 		new = freelist;
1481133e27eSPeter Avalos 		freelist = freelist->next;
1491133e27eSPeter Avalos 	} else
1501133e27eSPeter Avalos 	{
1511133e27eSPeter Avalos 		/*
1521133e27eSPeter Avalos 		 * No free entries.
1531133e27eSPeter Avalos 		 * Use the "spare" entry.
1541133e27eSPeter Avalos 		 */
1551133e27eSPeter Avalos 		new = spare;
1561133e27eSPeter Avalos 		spare = NULL;
1571133e27eSPeter Avalos 	}
1581133e27eSPeter Avalos 
1591133e27eSPeter Avalos 	/*
1601133e27eSPeter Avalos 	 * Fill in the fields of the new entry,
1611133e27eSPeter Avalos 	 * and insert it into the proper place in the list.
1621133e27eSPeter Avalos 	 */
1631133e27eSPeter Avalos 	new->next = nextp;
1641133e27eSPeter Avalos 	new->prev = prevp;
1651133e27eSPeter Avalos 	new->pos = pos;
1661133e27eSPeter Avalos 	new->line = linenum;
1671133e27eSPeter Avalos 
1681133e27eSPeter Avalos 	nextp->prev = new;
1691133e27eSPeter Avalos 	prevp->next = new;
1701133e27eSPeter Avalos 
1711133e27eSPeter Avalos 	/*
1721133e27eSPeter Avalos 	 * Recalculate gaps for the new entry and the neighboring entries.
1731133e27eSPeter Avalos 	 */
1741133e27eSPeter Avalos 	calcgap(new);
1751133e27eSPeter Avalos 	calcgap(nextp);
1761133e27eSPeter Avalos 	calcgap(prevp);
1771133e27eSPeter Avalos 
1781133e27eSPeter Avalos 	if (spare == NULL)
1791133e27eSPeter Avalos 	{
1801133e27eSPeter Avalos 		/*
1811133e27eSPeter Avalos 		 * We have used the spare entry.
1821133e27eSPeter Avalos 		 * Scan the list to find the one with the smallest
1831133e27eSPeter Avalos 		 * gap, take it out and make it the spare.
1841133e27eSPeter Avalos 		 * We should never remove the last one, so stop when
1851133e27eSPeter Avalos 		 * we get to p->next == &anchor.  This also avoids
1861133e27eSPeter Avalos 		 * looking at the gap of the last one, which is
1871133e27eSPeter Avalos 		 * not computed by calcgap.
1881133e27eSPeter Avalos 		 */
1891133e27eSPeter Avalos 		mingap = anchor.next->gap;
1901133e27eSPeter Avalos 		for (p = anchor.next;  p->next != &anchor;  p = p->next)
1911133e27eSPeter Avalos 		{
1921133e27eSPeter Avalos 			if (p->gap <= mingap)
1931133e27eSPeter Avalos 			{
1941133e27eSPeter Avalos 				spare = p;
1951133e27eSPeter Avalos 				mingap = p->gap;
1961133e27eSPeter Avalos 			}
1971133e27eSPeter Avalos 		}
1981133e27eSPeter Avalos 		spare->next->prev = spare->prev;
1991133e27eSPeter Avalos 		spare->prev->next = spare->next;
2001133e27eSPeter Avalos 	}
2011133e27eSPeter Avalos }
2021133e27eSPeter Avalos 
2031133e27eSPeter Avalos /*
2041133e27eSPeter Avalos  * If we get stuck in a long loop trying to figure out the
2051133e27eSPeter Avalos  * line number, print a message to tell the user what we're doing.
2061133e27eSPeter Avalos  */
longloopmessage(void)207320d7c8aSAaron LI static void longloopmessage(void)
2081133e27eSPeter Avalos {
2091133e27eSPeter Avalos 	ierror("Calculating line numbers", NULL_PARG);
2101133e27eSPeter Avalos }
2111133e27eSPeter Avalos 
212*e433da38SAaron LI struct delayed_msg
213*e433da38SAaron LI {
214*e433da38SAaron LI 	void (*message)(void);
215*e433da38SAaron LI 	int loopcount;
2161133e27eSPeter Avalos #if HAVE_TIME
217*e433da38SAaron LI 	time_type startime;
2181133e27eSPeter Avalos #endif
219*e433da38SAaron LI };
2201133e27eSPeter Avalos 
start_delayed_msg(struct delayed_msg * dmsg,void (* message)(void))221*e433da38SAaron LI static void start_delayed_msg(struct delayed_msg *dmsg, void (*message)(void))
222*e433da38SAaron LI {
223*e433da38SAaron LI 	dmsg->loopcount = 0;
224*e433da38SAaron LI 	dmsg->message = message;
225*e433da38SAaron LI #if HAVE_TIME
226*e433da38SAaron LI 	dmsg->startime = get_time();
227*e433da38SAaron LI #endif
228*e433da38SAaron LI }
229*e433da38SAaron LI 
delayed_msg(struct delayed_msg * dmsg)230*e433da38SAaron LI static void delayed_msg(struct delayed_msg *dmsg)
2311133e27eSPeter Avalos {
2321133e27eSPeter Avalos #if HAVE_TIME
233*e433da38SAaron LI 	if (dmsg->loopcount >= 0 && ++(dmsg->loopcount) > 100)
2341133e27eSPeter Avalos 	{
235*e433da38SAaron LI 		dmsg->loopcount = 0;
236*e433da38SAaron LI 		if (get_time() >= dmsg->startime + LONGTIME)
2371133e27eSPeter Avalos 		{
238*e433da38SAaron LI 			dmsg->message();
239*e433da38SAaron LI 			dmsg->loopcount = -1;
2401133e27eSPeter Avalos 		}
2411133e27eSPeter Avalos 	}
2421133e27eSPeter Avalos #else
243*e433da38SAaron LI 	if (dmsg->loopcount >= 0 && ++(dmsg->loopcount) > LONGLOOP)
2441133e27eSPeter Avalos 	{
245*e433da38SAaron LI 		dmsg->message();
246*e433da38SAaron LI 		dmsg->loopcount = -1;
2471133e27eSPeter Avalos 	}
2481133e27eSPeter Avalos #endif
2491133e27eSPeter Avalos }
2501133e27eSPeter Avalos 
2511133e27eSPeter Avalos /*
2528be36e5bSPeter Avalos  * Turn off line numbers because the user has interrupted
2538be36e5bSPeter Avalos  * a lengthy line number calculation.
2548be36e5bSPeter Avalos  */
abort_delayed_msg(struct delayed_msg * dmsg)255*e433da38SAaron LI static void abort_delayed_msg(struct delayed_msg *dmsg)
2568be36e5bSPeter Avalos {
257*e433da38SAaron LI 	if (dmsg->loopcount >= 0)
2580c7ad07eSAntonio Huete Jimenez 		return;
2598be36e5bSPeter Avalos 	if (linenums == OPT_ONPLUS)
2608be36e5bSPeter Avalos 		/*
2618be36e5bSPeter Avalos 		 * We were displaying line numbers, so need to repaint.
2628be36e5bSPeter Avalos 		 */
263*e433da38SAaron LI 		screen_trashed();
2648be36e5bSPeter Avalos 	linenums = 0;
2658be36e5bSPeter Avalos 	error("Line numbers turned off", NULL_PARG);
2668be36e5bSPeter Avalos }
2678be36e5bSPeter Avalos 
2688be36e5bSPeter Avalos /*
2691133e27eSPeter Avalos  * Find the line number associated with a given position.
2701133e27eSPeter Avalos  * Return 0 if we can't figure it out.
2711133e27eSPeter Avalos  */
find_linenum(POSITION pos)272320d7c8aSAaron LI public LINENUM find_linenum(POSITION pos)
2731133e27eSPeter Avalos {
27402d62a0fSDaniel Fojt 	struct linenum_info *p;
27502d62a0fSDaniel Fojt 	LINENUM linenum;
2761133e27eSPeter Avalos 	POSITION cpos;
277*e433da38SAaron LI 	struct delayed_msg dmsg;
2781133e27eSPeter Avalos 
2791133e27eSPeter Avalos 	if (!linenums)
2801133e27eSPeter Avalos 		/*
2811133e27eSPeter Avalos 		 * We're not using line numbers.
2821133e27eSPeter Avalos 		 */
2831133e27eSPeter Avalos 		return (0);
2841133e27eSPeter Avalos 	if (pos == NULL_POSITION)
2851133e27eSPeter Avalos 		/*
2861133e27eSPeter Avalos 		 * Caller doesn't know what he's talking about.
2871133e27eSPeter Avalos 		 */
2881133e27eSPeter Avalos 		return (0);
2891133e27eSPeter Avalos 	if (pos <= ch_zero())
2901133e27eSPeter Avalos 		/*
2911133e27eSPeter Avalos 		 * Beginning of file is always line number 1.
2921133e27eSPeter Avalos 		 */
2931133e27eSPeter Avalos 		return (1);
2941133e27eSPeter Avalos 
2951133e27eSPeter Avalos 	/*
2961133e27eSPeter Avalos 	 * Find the entry nearest to the position we want.
2971133e27eSPeter Avalos 	 */
2981133e27eSPeter Avalos 	for (p = anchor.next;  p != &anchor && p->pos < pos;  p = p->next)
2991133e27eSPeter Avalos 		continue;
3001133e27eSPeter Avalos 	if (p->pos == pos)
3011133e27eSPeter Avalos 		/* Found it exactly. */
3021133e27eSPeter Avalos 		return (p->line);
3031133e27eSPeter Avalos 
3041133e27eSPeter Avalos 	/*
3051133e27eSPeter Avalos 	 * This is the (possibly) time-consuming part.
3061133e27eSPeter Avalos 	 * We start at the line we just found and start
3071133e27eSPeter Avalos 	 * reading the file forward or backward till we
3081133e27eSPeter Avalos 	 * get to the place we want.
3091133e27eSPeter Avalos 	 *
3101133e27eSPeter Avalos 	 * First decide whether we should go forward from the
3111133e27eSPeter Avalos 	 * previous one or backwards from the next one.
3121133e27eSPeter Avalos 	 * The decision is based on which way involves
3131133e27eSPeter Avalos 	 * traversing fewer bytes in the file.
3141133e27eSPeter Avalos 	 */
315*e433da38SAaron LI 	start_delayed_msg(&dmsg, longloopmessage);
3161133e27eSPeter Avalos 	if (p == &anchor || pos - p->prev->pos < p->pos - pos)
3171133e27eSPeter Avalos 	{
3181133e27eSPeter Avalos 		/*
3191133e27eSPeter Avalos 		 * Go forward.
3201133e27eSPeter Avalos 		 */
3211133e27eSPeter Avalos 		p = p->prev;
3221133e27eSPeter Avalos 		if (ch_seek(p->pos))
3231133e27eSPeter Avalos 			return (0);
3241133e27eSPeter Avalos 		for (linenum = p->line, cpos = p->pos;  cpos < pos;  linenum++)
3251133e27eSPeter Avalos 		{
3261133e27eSPeter Avalos 			/*
3271133e27eSPeter Avalos 			 * Allow a signal to abort this loop.
3281133e27eSPeter Avalos 			 */
329*e433da38SAaron LI 			cpos = forw_raw_line(cpos, NULL, NULL);
3308be36e5bSPeter Avalos 			if (ABORT_SIGS()) {
331*e433da38SAaron LI 				abort_delayed_msg(&dmsg);
3328be36e5bSPeter Avalos 				return (0);
3338be36e5bSPeter Avalos 			}
3348be36e5bSPeter Avalos 			if (cpos == NULL_POSITION)
3351133e27eSPeter Avalos 				return (0);
336*e433da38SAaron LI 			delayed_msg(&dmsg);
3371133e27eSPeter Avalos 		}
3381133e27eSPeter Avalos 		/*
3391133e27eSPeter Avalos 		 * We might as well cache it.
3401133e27eSPeter Avalos 		 */
3411133e27eSPeter Avalos 		add_lnum(linenum, cpos);
3421133e27eSPeter Avalos 		/*
3431133e27eSPeter Avalos 		 * If the given position is not at the start of a line,
3441133e27eSPeter Avalos 		 * make sure we return the correct line number.
3451133e27eSPeter Avalos 		 */
3461133e27eSPeter Avalos 		if (cpos > pos)
3471133e27eSPeter Avalos 			linenum--;
3481133e27eSPeter Avalos 	} else
3491133e27eSPeter Avalos 	{
3501133e27eSPeter Avalos 		/*
3511133e27eSPeter Avalos 		 * Go backward.
3521133e27eSPeter Avalos 		 */
3531133e27eSPeter Avalos 		if (ch_seek(p->pos))
3541133e27eSPeter Avalos 			return (0);
3551133e27eSPeter Avalos 		for (linenum = p->line, cpos = p->pos;  cpos > pos;  linenum--)
3561133e27eSPeter Avalos 		{
3571133e27eSPeter Avalos 			/*
3581133e27eSPeter Avalos 			 * Allow a signal to abort this loop.
3591133e27eSPeter Avalos 			 */
360*e433da38SAaron LI 			cpos = back_raw_line(cpos, NULL, NULL);
3618be36e5bSPeter Avalos 			if (ABORT_SIGS()) {
362*e433da38SAaron LI 				abort_delayed_msg(&dmsg);
3638be36e5bSPeter Avalos 				return (0);
3648be36e5bSPeter Avalos 			}
3658be36e5bSPeter Avalos 			if (cpos == NULL_POSITION)
3661133e27eSPeter Avalos 				return (0);
367*e433da38SAaron LI 			delayed_msg(&dmsg);
3681133e27eSPeter Avalos 		}
3691133e27eSPeter Avalos 		/*
3701133e27eSPeter Avalos 		 * We might as well cache it.
3711133e27eSPeter Avalos 		 */
3721133e27eSPeter Avalos 		add_lnum(linenum, cpos);
3731133e27eSPeter Avalos 	}
3741133e27eSPeter Avalos 	return (linenum);
3751133e27eSPeter Avalos }
3761133e27eSPeter Avalos 
3771133e27eSPeter Avalos /*
3781133e27eSPeter Avalos  * Find the position of a given line number.
3791133e27eSPeter Avalos  * Return NULL_POSITION if we can't figure it out.
3801133e27eSPeter Avalos  */
find_pos(LINENUM linenum)381320d7c8aSAaron LI public POSITION find_pos(LINENUM linenum)
3821133e27eSPeter Avalos {
38302d62a0fSDaniel Fojt 	struct linenum_info *p;
3841133e27eSPeter Avalos 	POSITION cpos;
3851133e27eSPeter Avalos 	LINENUM clinenum;
3861133e27eSPeter Avalos 
3871133e27eSPeter Avalos 	if (linenum <= 1)
3881133e27eSPeter Avalos 		/*
3891133e27eSPeter Avalos 		 * Line number 1 is beginning of file.
3901133e27eSPeter Avalos 		 */
3911133e27eSPeter Avalos 		return (ch_zero());
3921133e27eSPeter Avalos 
3931133e27eSPeter Avalos 	/*
3941133e27eSPeter Avalos 	 * Find the entry nearest to the line number we want.
3951133e27eSPeter Avalos 	 */
3961133e27eSPeter Avalos 	for (p = anchor.next;  p != &anchor && p->line < linenum;  p = p->next)
3971133e27eSPeter Avalos 		continue;
3981133e27eSPeter Avalos 	if (p->line == linenum)
3991133e27eSPeter Avalos 		/* Found it exactly. */
4001133e27eSPeter Avalos 		return (p->pos);
4011133e27eSPeter Avalos 
4021133e27eSPeter Avalos 	if (p == &anchor || linenum - p->prev->line < p->line - linenum)
4031133e27eSPeter Avalos 	{
4041133e27eSPeter Avalos 		/*
4051133e27eSPeter Avalos 		 * Go forward.
4061133e27eSPeter Avalos 		 */
4071133e27eSPeter Avalos 		p = p->prev;
4081133e27eSPeter Avalos 		if (ch_seek(p->pos))
4091133e27eSPeter Avalos 			return (NULL_POSITION);
4101133e27eSPeter Avalos 		for (clinenum = p->line, cpos = p->pos;  clinenum < linenum;  clinenum++)
4111133e27eSPeter Avalos 		{
4121133e27eSPeter Avalos 			/*
4131133e27eSPeter Avalos 			 * Allow a signal to abort this loop.
4141133e27eSPeter Avalos 			 */
415*e433da38SAaron LI 			cpos = forw_raw_line(cpos, NULL, NULL);
4168be36e5bSPeter Avalos 			if (ABORT_SIGS())
4178be36e5bSPeter Avalos 				return (NULL_POSITION);
4188be36e5bSPeter Avalos 			if (cpos == NULL_POSITION)
4191133e27eSPeter Avalos 				return (NULL_POSITION);
4201133e27eSPeter Avalos 		}
4211133e27eSPeter Avalos 	} else
4221133e27eSPeter Avalos 	{
4231133e27eSPeter Avalos 		/*
4241133e27eSPeter Avalos 		 * Go backward.
4251133e27eSPeter Avalos 		 */
4261133e27eSPeter Avalos 		if (ch_seek(p->pos))
4271133e27eSPeter Avalos 			return (NULL_POSITION);
4281133e27eSPeter Avalos 		for (clinenum = p->line, cpos = p->pos;  clinenum > linenum;  clinenum--)
4291133e27eSPeter Avalos 		{
4301133e27eSPeter Avalos 			/*
4311133e27eSPeter Avalos 			 * Allow a signal to abort this loop.
4321133e27eSPeter Avalos 			 */
433*e433da38SAaron LI 			cpos = back_raw_line(cpos, NULL, NULL);
4348be36e5bSPeter Avalos 			if (ABORT_SIGS())
4358be36e5bSPeter Avalos 				return (NULL_POSITION);
4368be36e5bSPeter Avalos 			if (cpos == NULL_POSITION)
4371133e27eSPeter Avalos 				return (NULL_POSITION);
4381133e27eSPeter Avalos 		}
4391133e27eSPeter Avalos 	}
4401133e27eSPeter Avalos 	/*
4411133e27eSPeter Avalos 	 * We might as well cache it.
4421133e27eSPeter Avalos 	 */
4431133e27eSPeter Avalos 	add_lnum(clinenum, cpos);
4441133e27eSPeter Avalos 	return (cpos);
4451133e27eSPeter Avalos }
4461133e27eSPeter Avalos 
4471133e27eSPeter Avalos /*
4481133e27eSPeter Avalos  * Return the line number of the "current" line.
4491133e27eSPeter Avalos  * The argument "where" tells which line is to be considered
4501133e27eSPeter Avalos  * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc).
4511133e27eSPeter Avalos  */
currline(int where)452320d7c8aSAaron LI public LINENUM currline(int where)
4531133e27eSPeter Avalos {
4541133e27eSPeter Avalos 	POSITION pos;
4551133e27eSPeter Avalos 	POSITION len;
4561133e27eSPeter Avalos 	LINENUM linenum;
4571133e27eSPeter Avalos 
4581133e27eSPeter Avalos 	pos = position(where);
4591133e27eSPeter Avalos 	len = ch_length();
4601133e27eSPeter Avalos 	while (pos == NULL_POSITION && where >= 0 && where < sc_height)
4611133e27eSPeter Avalos 		pos = position(++where);
4621133e27eSPeter Avalos 	if (pos == NULL_POSITION)
4631133e27eSPeter Avalos 		pos = len;
4641133e27eSPeter Avalos 	linenum = find_linenum(pos);
4651133e27eSPeter Avalos 	if (pos == len)
4661133e27eSPeter Avalos 		linenum--;
4671133e27eSPeter Avalos 	return (linenum);
4681133e27eSPeter Avalos }
4690c7ad07eSAntonio Huete Jimenez 
detlenmessage(void)470*e433da38SAaron LI static void detlenmessage(void)
471*e433da38SAaron LI {
472*e433da38SAaron LI 	ierror("Determining length of file", NULL_PARG);
473*e433da38SAaron LI }
474*e433da38SAaron LI 
4750c7ad07eSAntonio Huete Jimenez /*
4760c7ad07eSAntonio Huete Jimenez  * Scan entire file, counting line numbers.
4770c7ad07eSAntonio Huete Jimenez  */
scan_eof(void)478320d7c8aSAaron LI public void scan_eof(void)
4790c7ad07eSAntonio Huete Jimenez {
480320d7c8aSAaron LI 	POSITION pos = ch_zero();
4810c7ad07eSAntonio Huete Jimenez 	LINENUM linenum = 0;
482*e433da38SAaron LI 	struct delayed_msg dmsg;
4830c7ad07eSAntonio Huete Jimenez 
4840c7ad07eSAntonio Huete Jimenez 	if (ch_seek(0))
4850c7ad07eSAntonio Huete Jimenez 		return;
486320d7c8aSAaron LI 	/*
487320d7c8aSAaron LI 	 * scanning_eof prevents the "Waiting for data" message from
488320d7c8aSAaron LI 	 * overwriting "Determining length of file".
489320d7c8aSAaron LI 	 */
490*e433da38SAaron LI 	start_delayed_msg(&dmsg, detlenmessage);
491320d7c8aSAaron LI 	scanning_eof = TRUE;
4920c7ad07eSAntonio Huete Jimenez 	while (pos != NULL_POSITION)
4930c7ad07eSAntonio Huete Jimenez 	{
4940c7ad07eSAntonio Huete Jimenez 		/* For efficiency, only add one every 256 line numbers. */
4950c7ad07eSAntonio Huete Jimenez 		if ((linenum++ % 256) == 0)
4960c7ad07eSAntonio Huete Jimenez 			add_lnum(linenum, pos);
497*e433da38SAaron LI 		pos = forw_raw_line(pos, NULL, NULL);
4980c7ad07eSAntonio Huete Jimenez 		if (ABORT_SIGS())
499*e433da38SAaron LI 		{
500*e433da38SAaron LI 			abort_delayed_msg(&dmsg);
5010c7ad07eSAntonio Huete Jimenez 			break;
5020c7ad07eSAntonio Huete Jimenez 		}
503*e433da38SAaron LI 		delayed_msg(&dmsg);
504*e433da38SAaron LI 	}
505320d7c8aSAaron LI 	scanning_eof = FALSE;
5060c7ad07eSAntonio Huete Jimenez }
5070c7ad07eSAntonio Huete Jimenez 
5080c7ad07eSAntonio Huete Jimenez /*
5090c7ad07eSAntonio Huete Jimenez  * Return a line number adjusted for display
5100c7ad07eSAntonio Huete Jimenez  * (handles the --no-number-headers option).
5110c7ad07eSAntonio Huete Jimenez  */
vlinenum(LINENUM linenum)512320d7c8aSAaron LI public LINENUM vlinenum(LINENUM linenum)
5130c7ad07eSAntonio Huete Jimenez {
5140c7ad07eSAntonio Huete Jimenez 	if (nonum_headers)
5150c7ad07eSAntonio Huete Jimenez 		linenum = (linenum < header_lines) ? 0 : linenum - header_lines;
5160c7ad07eSAntonio Huete Jimenez 	return linenum;
5170c7ad07eSAntonio Huete Jimenez }
518