Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / distrib / utils / more / position.c
blobb40e1359effb2c47cf6f682cab42c5b05f64cb77
1 /* $NetBSD: position.c,v 1.4 2003/08/07 09:28:01 agc Exp $ */
3 /*
4 * Copyright (c) 1988 Mark Nudelman
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
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.
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.
33 #include <sys/cdefs.h>
34 #ifndef lint
35 #if 0
36 static 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 */
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.
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. }}
52 #include <sys/types.h>
53 #include <stdlib.h>
55 #include "less.h"
56 #include "extern.h"
58 static off_t *table; /* The position table */
59 static int tablesize;
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
70 off_t
71 position(where)
72 int where;
74 switch (where)
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;
85 return (table[where]);
89 * Add a new file position to the bottom of the position table.
91 void
92 add_forw_pos(pos)
93 off_t pos;
95 int i;
98 * Scroll the position table up.
100 for (i = 1; i < sc_height; i++)
101 table[i-1] = table[i];
102 table[sc_height - 1] = pos;
106 * Add a new file position to the top of the position table.
108 void
109 add_back_pos(pos)
110 off_t pos;
112 int i;
115 * Scroll the position table down.
117 for (i = sc_height - 1; i > 0; i--)
118 table[i] = table[i-1];
119 table[0] = pos;
122 void
123 copytable()
125 int a, b;
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;
135 * Initialize the position table, done whenever we clear the screen.
137 void
138 pos_clear()
140 int i;
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);
150 for (i = 0; i < sc_height; i++)
151 table[i] = NULL_POSITION;
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.
160 onscreen(pos)
161 off_t pos;
163 int i;
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);