Home | History | Annotate | Line # | Download | only in more
position.c revision 1.3
      1 /*	$NetBSD: position.c,v 1.3 1998/02/04 11:09:05 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988 Mark Nudleman
      5  * Copyright (c) 1988, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)position.c	8.1 (Berkeley) 6/6/93";
     41 #else
     42 __RCSID("$NetBSD: position.c,v 1.3 1998/02/04 11:09:05 christos Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 /*
     47  * Routines dealing with the "position" table.
     48  * This is a table which tells the position (in the input file) of the
     49  * first char on each currently displayed line.
     50  *
     51  * {{ The position table is scrolled by moving all the entries.
     52  *    Would be better to have a circular table
     53  *    and just change a couple of pointers. }}
     54  */
     55 
     56 #include <sys/types.h>
     57 #include <stdlib.h>
     58 
     59 #include "less.h"
     60 #include "extern.h"
     61 
     62 static off_t *table;		/* The position table */
     63 static int tablesize;
     64 
     65 /*
     66  * Return the starting file position of a line displayed on the screen.
     67  * The line may be specified as a line number relative to the top
     68  * of the screen, but is usually one of these special cases:
     69  *	the top (first) line on the screen
     70  *	the second line on the screen
     71  *	the bottom line on the screen
     72  *	the line after the bottom line on the screen
     73  */
     74 off_t
     75 position(where)
     76 	int where;
     77 {
     78 	switch (where)
     79 	{
     80 	case BOTTOM:
     81 		where = sc_height - 2;
     82 		break;
     83 	case BOTTOM_PLUS_ONE:
     84 		where = sc_height - 1;
     85 		break;
     86 	case MIDDLE:
     87 		where = sc_height / 2;
     88 	}
     89 	return (table[where]);
     90 }
     91 
     92 /*
     93  * Add a new file position to the bottom of the position table.
     94  */
     95 void
     96 add_forw_pos(pos)
     97 	off_t pos;
     98 {
     99 	int i;
    100 
    101 	/*
    102 	 * Scroll the position table up.
    103 	 */
    104 	for (i = 1;  i < sc_height;  i++)
    105 		table[i-1] = table[i];
    106 	table[sc_height - 1] = pos;
    107 }
    108 
    109 /*
    110  * Add a new file position to the top of the position table.
    111  */
    112 void
    113 add_back_pos(pos)
    114 	off_t pos;
    115 {
    116 	int i;
    117 
    118 	/*
    119 	 * Scroll the position table down.
    120 	 */
    121 	for (i = sc_height - 1;  i > 0;  i--)
    122 		table[i] = table[i-1];
    123 	table[0] = pos;
    124 }
    125 
    126 void
    127 copytable()
    128 {
    129 	int a, b;
    130 
    131 	for (a = 0; a < sc_height && table[a] == NULL_POSITION; a++);
    132 	for (b = 0; a < sc_height; a++, b++) {
    133 		table[b] = table[a];
    134 		table[a] = NULL_POSITION;
    135 	}
    136 }
    137 
    138 /*
    139  * Initialize the position table, done whenever we clear the screen.
    140  */
    141 void
    142 pos_clear()
    143 {
    144 	int i;
    145 
    146 	if (table == 0) {
    147 		tablesize = sc_height > 25 ? sc_height : 25;
    148 		table = (off_t *)malloc(tablesize * sizeof *table);
    149 	} else if (sc_height >= tablesize) {
    150 		tablesize = sc_height;
    151 		table = (off_t *)realloc(table, tablesize * sizeof *table);
    152 	}
    153 
    154 	for (i = 0;  i < sc_height;  i++)
    155 		table[i] = NULL_POSITION;
    156 }
    157 
    158 /*
    159  * See if the byte at a specified position is currently on the screen.
    160  * Check the position table to see if the position falls within its range.
    161  * Return the position table entry if found, -1 if not.
    162  */
    163 int
    164 onscreen(pos)
    165 	off_t pos;
    166 {
    167 	int i;
    168 
    169 	if (pos < table[0])
    170 		return (-1);
    171 	for (i = 1;  i < sc_height;  i++)
    172 		if (pos < table[i])
    173 			return (i-1);
    174 	return (-1);
    175 }
    176