=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/less/linenum.c,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- src/usr.bin/less/linenum.c 2001/11/19 19:02:14 1.3 +++ src/usr.bin/less/linenum.c 2003/04/13 18:26:26 1.4 @@ -1,29 +1,11 @@ -/* $OpenBSD: linenum.c,v 1.3 2001/11/19 19:02:14 mpech Exp $ */ - /* - * Copyright (c) 1984,1985,1989,1994,1995 Mark Nudelman - * All rights reserved. + * Copyright (C) 1984-2002 Mark Nudelman * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice in the documentation and/or other materials provided with - * the distribution. + * You may distribute under the terms of either the GNU General Public + * License or the Less License, as specified in the README file. * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN - * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * For more information about less, or for information on how to + * contact the author, see the README file. */ @@ -52,19 +34,18 @@ */ #include "less.h" -#include "position.h" /* * Structure to keep track of a line number and the associated file position. * A doubly-linked circular list of line numbers is kept ordered by line number. */ -struct linenum +struct linenum_info { - struct linenum *next; /* Link to next in the list */ - struct linenum *prev; /* Line to previous in the list */ + struct linenum_info *next; /* Link to next in the list */ + struct linenum_info *prev; /* Line to previous in the list */ POSITION pos; /* File position */ POSITION gap; /* Gap between prev and next */ - int line; /* Line number */ + LINENUM line; /* Line number */ }; /* * "gap" needs some explanation: the gap of any particular line number @@ -81,10 +62,10 @@ public int lnloop = 0; /* Are we in the line num loop? */ -static struct linenum anchor; /* Anchor of the list */ -static struct linenum *freelist; /* Anchor of the unused entries */ -static struct linenum pool[NPOOL]; /* The pool itself */ -static struct linenum *spare; /* We always keep one spare entry */ +static struct linenum_info anchor; /* Anchor of the list */ +static struct linenum_info *freelist; /* Anchor of the unused entries */ +static struct linenum_info pool[NPOOL]; /* The pool itself */ +static struct linenum_info *spare; /* We always keep one spare entry */ extern int linenums; extern int sigs; @@ -96,7 +77,7 @@ public void clr_linenum() { - struct linenum *p; + register struct linenum_info *p; /* * Put all the entries on the free list. @@ -123,7 +104,7 @@ */ static void calcgap(p) - struct linenum *p; + register struct linenum_info *p; { /* * Don't bother to compute a gap for the anchor. @@ -142,22 +123,22 @@ * FIRST character in the specified line. */ public void -add_lnum(lno, pos) - int lno; +add_lnum(linenum, pos) + LINENUM linenum; POSITION pos; { - struct linenum *p; - struct linenum *new; - struct linenum *nextp; - struct linenum *prevp; - POSITION mingap; + register struct linenum_info *p; + register struct linenum_info *new; + register struct linenum_info *nextp; + register struct linenum_info *prevp; + register POSITION mingap; /* * Find the proper place in the list for the new one. * The entries are sorted by position. */ for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next) - if (p->line == lno) + if (p->line == linenum) /* We already have this one. */ return; nextp = p; @@ -188,7 +169,7 @@ new->next = nextp; new->prev = prevp; new->pos = pos; - new->line = lno; + new->line = linenum; nextp->prev = new; prevp->next = new; @@ -272,12 +253,12 @@ * Find the line number associated with a given position. * Return 0 if we can't figure it out. */ - public int + public LINENUM find_linenum(pos) POSITION pos; { - struct linenum *p; - int lno; + register struct linenum_info *p; + register LINENUM linenum; POSITION cpos; if (!linenums) @@ -316,7 +297,6 @@ * The decision is based on which way involves * traversing fewer bytes in the file. */ - flush(); #if HAVE_TIME startime = get_time(); #endif @@ -329,7 +309,7 @@ if (ch_seek(p->pos)) return (0); loopcount = 0; - for (lno = p->line, cpos = p->pos; cpos < pos; lno++) + for (linenum = p->line, cpos = p->pos; cpos < pos; linenum++) { /* * Allow a signal to abort this loop. @@ -343,13 +323,13 @@ /* * We might as well cache it. */ - add_lnum(lno, cpos); + add_lnum(linenum, cpos); /* * If the given position is not at the start of a line, * make sure we return the correct line number. */ if (cpos > pos) - lno--; + linenum--; } else { /* @@ -358,7 +338,7 @@ if (ch_seek(p->pos)) return (0); loopcount = 0; - for (lno = p->line, cpos = p->pos; cpos > pos; lno--) + for (linenum = p->line, cpos = p->pos; cpos > pos; linenum--) { /* * Allow a signal to abort this loop. @@ -372,10 +352,10 @@ /* * We might as well cache it. */ - add_lnum(lno, cpos); + add_lnum(linenum, cpos); } - return (lno); + return (linenum); } /* @@ -383,14 +363,14 @@ * Return NULL_POSITION if we can't figure it out. */ public POSITION -find_pos(lno) - int lno; +find_pos(linenum) + LINENUM linenum; { - struct linenum *p; + register struct linenum_info *p; POSITION cpos; - int clno; + LINENUM clinenum; - if (lno <= 1) + if (linenum <= 1) /* * Line number 1 is beginning of file. */ @@ -399,14 +379,13 @@ /* * Find the entry nearest to the line number we want. */ - for (p = anchor.next; p != &anchor && p->line < lno; p = p->next) + for (p = anchor.next; p != &anchor && p->line < linenum; p = p->next) continue; - if (p->line == lno) + if (p->line == linenum) /* Found it exactly. */ return (p->pos); - flush(); - if (p == &anchor || lno - p->prev->line < p->line - lno) + if (p == &anchor || linenum - p->prev->line < p->line - linenum) { /* * Go forward. @@ -414,7 +393,7 @@ p = p->prev; if (ch_seek(p->pos)) return (NULL_POSITION); - for (clno = p->line, cpos = p->pos; clno < lno; clno++) + for (clinenum = p->line, cpos = p->pos; clinenum < linenum; clinenum++) { /* * Allow a signal to abort this loop. @@ -430,7 +409,7 @@ */ if (ch_seek(p->pos)) return (NULL_POSITION); - for (clno = p->line, cpos = p->pos; clno > lno; clno--) + for (clinenum = p->line, cpos = p->pos; clinenum > linenum; clinenum--) { /* * Allow a signal to abort this loop. @@ -443,7 +422,7 @@ /* * We might as well cache it. */ - add_lnum(clno, cpos); + add_lnum(clinenum, cpos); return (cpos); } @@ -452,13 +431,13 @@ * The argument "where" tells which line is to be considered * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc). */ - public int + public LINENUM currline(where) int where; { POSITION pos; POSITION len; - int lnum; + LINENUM linenum; pos = position(where); len = ch_length(); @@ -466,8 +445,8 @@ pos = position(++where); if (pos == NULL_POSITION) pos = len; - lnum = find_linenum(pos); + linenum = find_linenum(pos); if (pos == len) - lnum--; - return (lnum); + linenum--; + return (linenum); }