=================================================================== 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 -c -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 **** /* ! * Copyright (c) 1984,1985,1989,1994,1995 Mark Nudelman ! * All rights reserved. * ! * 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. * ! * 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. */ --- 1,11 ---- /* ! * Copyright (C) 1984-2002 Mark Nudelman * ! * You may distribute under the terms of either the GNU General Public ! * License or the Less License, as specified in the README file. * ! * For more information about less, or for information on how to ! * contact the author, see the README file. */ *************** *** 50,68 **** */ #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 *next; /* Link to next in the list */ ! struct linenum *prev; /* Line to previous in the list */ POSITION pos; /* File position */ POSITION gap; /* Gap between prev and next */ ! int line; /* Line number */ }; /* * "gap" needs some explanation: the gap of any particular line number --- 34,51 ---- */ #include "less.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_info { ! 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 */ ! LINENUM line; /* Line number */ }; /* * "gap" needs some explanation: the gap of any particular line number *************** *** 79,88 **** 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 */ extern int linenums; extern int sigs; --- 62,71 ---- public int lnloop = 0; /* Are we in the line num loop? */ ! 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,100 **** public void clr_linenum() { ! register struct linenum *p; /* * Put all the entries on the free list. --- 77,83 ---- public void clr_linenum() { ! register struct linenum_info *p; /* * Put all the entries on the free list. *************** *** 121,127 **** */ static void calcgap(p) ! register struct linenum *p; { /* * Don't bother to compute a gap for the anchor. --- 104,110 ---- */ static void calcgap(p) ! register struct linenum_info *p; { /* * Don't bother to compute a gap for the anchor. *************** *** 140,153 **** * FIRST character in the specified line. */ public void ! add_lnum(lno, pos) ! int lno; POSITION pos; { ! register struct linenum *p; ! register struct linenum *new; ! register struct linenum *nextp; ! register struct linenum *prevp; register POSITION mingap; /* --- 123,136 ---- * FIRST character in the specified line. */ public void ! add_lnum(linenum, pos) ! LINENUM linenum; POSITION pos; { ! register struct linenum_info *p; ! register struct linenum_info *new; ! register struct linenum_info *nextp; ! register struct linenum_info *prevp; register POSITION mingap; /* *************** *** 155,161 **** * The entries are sorted by position. */ for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next) ! if (p->line == lno) /* We already have this one. */ return; nextp = p; --- 138,144 ---- * The entries are sorted by position. */ for (p = anchor.next; p != &anchor && p->pos < pos; p = p->next) ! if (p->line == linenum) /* We already have this one. */ return; nextp = p; *************** *** 186,192 **** new->next = nextp; new->prev = prevp; new->pos = pos; ! new->line = lno; nextp->prev = new; prevp->next = new; --- 169,175 ---- new->next = nextp; new->prev = prevp; new->pos = pos; ! new->line = linenum; nextp->prev = new; prevp->next = new; *************** *** 270,281 **** * Find the line number associated with a given position. * Return 0 if we can't figure it out. */ ! public int find_linenum(pos) POSITION pos; { ! register struct linenum *p; ! register int lno; POSITION cpos; if (!linenums) --- 253,264 ---- * Find the line number associated with a given position. * Return 0 if we can't figure it out. */ ! public LINENUM find_linenum(pos) POSITION pos; { ! register struct linenum_info *p; ! register LINENUM linenum; POSITION cpos; if (!linenums) *************** *** 314,320 **** * The decision is based on which way involves * traversing fewer bytes in the file. */ - flush(); #if HAVE_TIME startime = get_time(); #endif --- 297,302 ---- *************** *** 327,333 **** if (ch_seek(p->pos)) return (0); loopcount = 0; ! for (lno = p->line, cpos = p->pos; cpos < pos; lno++) { /* * Allow a signal to abort this loop. --- 309,315 ---- if (ch_seek(p->pos)) return (0); loopcount = 0; ! for (linenum = p->line, cpos = p->pos; cpos < pos; linenum++) { /* * Allow a signal to abort this loop. *************** *** 341,353 **** /* * We might as well cache it. */ ! add_lnum(lno, 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--; } else { /* --- 323,335 ---- /* * We might as well cache it. */ ! 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) ! linenum--; } else { /* *************** *** 356,362 **** if (ch_seek(p->pos)) return (0); loopcount = 0; ! for (lno = p->line, cpos = p->pos; cpos > pos; lno--) { /* * Allow a signal to abort this loop. --- 338,344 ---- if (ch_seek(p->pos)) return (0); loopcount = 0; ! for (linenum = p->line, cpos = p->pos; cpos > pos; linenum--) { /* * Allow a signal to abort this loop. *************** *** 370,379 **** /* * We might as well cache it. */ ! add_lnum(lno, cpos); } ! return (lno); } /* --- 352,361 ---- /* * We might as well cache it. */ ! add_lnum(linenum, cpos); } ! return (linenum); } /* *************** *** 381,394 **** * Return NULL_POSITION if we can't figure it out. */ public POSITION ! find_pos(lno) ! int lno; { ! register struct linenum *p; POSITION cpos; ! int clno; ! if (lno <= 1) /* * Line number 1 is beginning of file. */ --- 363,376 ---- * Return NULL_POSITION if we can't figure it out. */ public POSITION ! find_pos(linenum) ! LINENUM linenum; { ! register struct linenum_info *p; POSITION cpos; ! LINENUM clinenum; ! if (linenum <= 1) /* * Line number 1 is beginning of file. */ *************** *** 397,410 **** /* * Find the entry nearest to the line number we want. */ ! for (p = anchor.next; p != &anchor && p->line < lno; p = p->next) continue; ! if (p->line == lno) /* Found it exactly. */ return (p->pos); ! flush(); ! if (p == &anchor || lno - p->prev->line < p->line - lno) { /* * Go forward. --- 379,391 ---- /* * Find the entry nearest to the line number we want. */ ! for (p = anchor.next; p != &anchor && p->line < linenum; p = p->next) continue; ! if (p->line == linenum) /* Found it exactly. */ return (p->pos); ! if (p == &anchor || linenum - p->prev->line < p->line - linenum) { /* * Go forward. *************** *** 412,418 **** p = p->prev; if (ch_seek(p->pos)) return (NULL_POSITION); ! for (clno = p->line, cpos = p->pos; clno < lno; clno++) { /* * Allow a signal to abort this loop. --- 393,399 ---- p = p->prev; if (ch_seek(p->pos)) return (NULL_POSITION); ! for (clinenum = p->line, cpos = p->pos; clinenum < linenum; clinenum++) { /* * Allow a signal to abort this loop. *************** *** 428,434 **** */ if (ch_seek(p->pos)) return (NULL_POSITION); ! for (clno = p->line, cpos = p->pos; clno > lno; clno--) { /* * Allow a signal to abort this loop. --- 409,415 ---- */ if (ch_seek(p->pos)) return (NULL_POSITION); ! for (clinenum = p->line, cpos = p->pos; clinenum > linenum; clinenum--) { /* * Allow a signal to abort this loop. *************** *** 441,447 **** /* * We might as well cache it. */ ! add_lnum(clno, cpos); return (cpos); } --- 422,428 ---- /* * We might as well cache it. */ ! add_lnum(clinenum, cpos); return (cpos); } *************** *** 450,462 **** * The argument "where" tells which line is to be considered * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc). */ ! public int currline(where) int where; { POSITION pos; POSITION len; ! int lnum; pos = position(where); len = ch_length(); --- 431,443 ---- * The argument "where" tells which line is to be considered * the "current" line (e.g. TOP, BOTTOM, MIDDLE, etc). */ ! public LINENUM currline(where) int where; { POSITION pos; POSITION len; ! LINENUM linenum; pos = position(where); len = ch_length(); *************** *** 464,471 **** pos = position(++where); if (pos == NULL_POSITION) pos = len; ! lnum = find_linenum(pos); if (pos == len) ! lnum--; ! return (lnum); } --- 445,452 ---- pos = position(++where); if (pos == NULL_POSITION) pos = len; ! linenum = find_linenum(pos); if (pos == len) ! linenum--; ! return (linenum); }