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