=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/less/linenum.c,v retrieving revision 1.1.1.1 retrieving revision 1.1.1.2 diff -u -r1.1.1.1 -r1.1.1.2 --- src/usr.bin/less/linenum.c 1996/09/21 05:39:42 1.1.1.1 +++ src/usr.bin/less/linenum.c 2003/04/13 18:21:21 1.1.1.2 @@ -1,27 +1,11 @@ /* - * 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. */ @@ -50,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 @@ -79,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; @@ -94,7 +77,7 @@ public void clr_linenum() { - register struct linenum *p; + register struct linenum_info *p; /* * Put all the entries on the free list. @@ -121,7 +104,7 @@ */ static void calcgap(p) - register struct linenum *p; + register struct linenum_info *p; { /* * Don't bother to compute a gap for the anchor. @@ -140,14 +123,14 @@ * FIRST character in the specified line. */ public void -add_lnum(lno, pos) - int lno; +add_lnum(linenum, pos) + LINENUM linenum; POSITION pos; { - register struct linenum *p; - register struct linenum *new; - register struct linenum *nextp; - register struct linenum *prevp; + register struct linenum_info *p; + register struct linenum_info *new; + register struct linenum_info *nextp; + register struct linenum_info *prevp; register POSITION mingap; /* @@ -155,7 +138,7 @@ * 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; @@ -186,7 +169,7 @@ new->next = nextp; new->prev = prevp; new->pos = pos; - new->line = lno; + new->line = linenum; nextp->prev = new; prevp->next = new; @@ -270,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; { - register struct linenum *p; - register int lno; + register struct linenum_info *p; + register LINENUM linenum; POSITION cpos; if (!linenums) @@ -314,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 @@ -327,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. @@ -341,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 { /* @@ -356,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. @@ -370,10 +352,10 @@ /* * We might as well cache it. */ - add_lnum(lno, cpos); + add_lnum(linenum, cpos); } - return (lno); + return (linenum); } /* @@ -381,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; { - register 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. */ @@ -397,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. @@ -412,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. @@ -428,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. @@ -441,7 +422,7 @@ /* * We might as well cache it. */ - add_lnum(clno, cpos); + add_lnum(clinenum, cpos); return (cpos); } @@ -450,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(); @@ -464,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); }