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