xref: /original-bsd/usr.bin/more/position.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1988 Mark Nudleman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)position.c	8.1 (Berkeley) 06/06/93";
11 #endif /* not lint */
12 
13 /*
14  * Routines dealing with the "position" table.
15  * This is a table which tells the position (in the input file) of the
16  * first char on each currently displayed line.
17  *
18  * {{ The position table is scrolled by moving all the entries.
19  *    Would be better to have a circular table
20  *    and just change a couple of pointers. }}
21  */
22 
23 #include <sys/types.h>
24 #include <less.h>
25 
26 static off_t *table;		/* The position table */
27 static int tablesize;
28 
29 extern int sc_height;
30 
31 /*
32  * Return the starting file position of a line displayed on the screen.
33  * The line may be specified as a line number relative to the top
34  * of the screen, but is usually one of these special cases:
35  *	the top (first) line on the screen
36  *	the second line on the screen
37  *	the bottom line on the screen
38  *	the line after the bottom line on the screen
39  */
40 off_t
41 position(where)
42 	int where;
43 {
44 	switch (where)
45 	{
46 	case BOTTOM:
47 		where = sc_height - 2;
48 		break;
49 	case BOTTOM_PLUS_ONE:
50 		where = sc_height - 1;
51 		break;
52 	case MIDDLE:
53 		where = sc_height / 2;
54 	}
55 	return (table[where]);
56 }
57 
58 /*
59  * Add a new file position to the bottom of the position table.
60  */
61 add_forw_pos(pos)
62 	off_t pos;
63 {
64 	register int i;
65 
66 	/*
67 	 * Scroll the position table up.
68 	 */
69 	for (i = 1;  i < sc_height;  i++)
70 		table[i-1] = table[i];
71 	table[sc_height - 1] = pos;
72 }
73 
74 /*
75  * Add a new file position to the top of the position table.
76  */
77 add_back_pos(pos)
78 	off_t pos;
79 {
80 	register int i;
81 
82 	/*
83 	 * Scroll the position table down.
84 	 */
85 	for (i = sc_height - 1;  i > 0;  i--)
86 		table[i] = table[i-1];
87 	table[0] = pos;
88 }
89 
90 copytable()
91 {
92 	register int a, b;
93 
94 	for (a = 0; a < sc_height && table[a] == NULL_POSITION; a++);
95 	for (b = 0; a < sc_height; a++, b++) {
96 		table[b] = table[a];
97 		table[a] = NULL_POSITION;
98 	}
99 }
100 
101 /*
102  * Initialize the position table, done whenever we clear the screen.
103  */
104 pos_clear()
105 {
106 	register int i;
107 	extern char *malloc(), *realloc();
108 
109 	if (table == 0) {
110 		tablesize = sc_height > 25 ? sc_height : 25;
111 		table = (off_t *)malloc(tablesize * sizeof *table);
112 	} else if (sc_height >= tablesize) {
113 		tablesize = sc_height;
114 		table = (off_t *)realloc(table, tablesize * sizeof *table);
115 	}
116 
117 	for (i = 0;  i < sc_height;  i++)
118 		table[i] = NULL_POSITION;
119 }
120 
121 /*
122  * See if the byte at a specified position is currently on the screen.
123  * Check the position table to see if the position falls within its range.
124  * Return the position table entry if found, -1 if not.
125  */
126 onscreen(pos)
127 	off_t pos;
128 {
129 	register int i;
130 
131 	if (pos < table[0])
132 		return (-1);
133 	for (i = 1;  i < sc_height;  i++)
134 		if (pos < table[i])
135 			return (i-1);
136 	return (-1);
137 }
138