-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscreen.c
280 lines (232 loc) · 5.79 KB
/
screen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* Screen interface package
*
* Author: Bob Baldwin June 1983
* $Date: 86/01/15 16:04:16 $
* $Log: screen.c,v $
* Revision 1.1 86/01/15 16:04:16 baldwin
* Initial revision
*
* Much code moved to terminal.c baldwin 10/86
*
* Revision 1.2 85/01/10 02:35:10 simsong
* Changed to termcap support
*
* Revision 1.1 85/01/09 23:45:10 simsong
* Initial revision
*
*/
/* The screen consists of 24 lines and 80 columns. The basic screen
* operation is replacing the character that is already at a given
* position with a new character. That is, characters are placed,
* they are not inserted.
*
* Various commands are provided for positioning the cursor, placing
* strings on the screen, and removing characters.
*
* If a routine finds something wrong or inconsistent, it will print
* an error message on the screen.
*/
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <curses.h>
#include <term.h>
#include "window.h"
#include "terminal.h"
#include "specs.h"
/* These variables contain the current location of the cursor.
* The origin, or home, of the cursor is in the upper lefthand
* corner of the screen. That location is line 1, column 1.
* The lower righthand corner of the screen is the location
* identified by line 24, column 80.
*/
int cline;
int ccolumn;
/* These must be 24 and 80 since only that region of the screen
* is covered by the windows. The wl_driver() routine will
* exit if the cursor moves out of that area.
*/
int MXLINE = MAXHEIGHT; /* Max line number */
int MXCOL = MAXWIDTH; /* Max column number */
/* Clear Screen. This should be the first screen operation called in order
* to initialize the line and column locations.
*/
void clrscreen(void)
{
enter_mode(SMNORMAL);
Puts(erase_scr); /* clear the screen */
cline = 1;
ccolumn = 1;
}
/* Set Cursor Position. The next character places on the screen
* will appear in the given line and column.
*
* Note: Bob's code is broken in that it assumes the screen goes
* from 1..24 and 1..80, rather than 0..23 and 0..79. So this
* routine subtracs one before it calls the curses routine.
*/
void setcursor(line, column)
int line, column; /* ranges: 1..24 and 1..80 inclusive */
{
if (line < 1 || line > MXLINE || column < 1 || column > MXCOL) {
disperr("setcursor tried to move cursor off screen");
return;
}
cline = line;
ccolumn = column;
enter_mode(SMNORMAL);
Puts(tgoto(cm,column-1,line-1));
}
/* Return the row location of the cursor (1 is in upper-left corner).
*/
int rowcursor(void)
{
return(cline);
}
/* Return the row location of the cursor (1 is in upper-left corner).
*/
int colcursor(void)
{
return(ccolumn);
}
/* Get Cursor Position
* The value returned equals 256*LineNumber + ColumnNumber
*/
int getcursor(void)
{
return((cline<<8)+ccolumn);
}
/* Jog the Cursor one position
* The value of dir determines the direction of travel:
* 1 = up, 2 = down, 3 = left, 4 = right. A value other than one of those
* four will cause an error message to be printed.
*/
/* of course, there is a more ellegant way to implement this */
void jogcursor(dir)
int dir;
{
switch(dir) {
case 1: cline = (cline<=1) ? 1 : cline-1;
break;
case 2: cline = (cline >= MXLINE) ? MXLINE : cline+1;
break;
case 3: ccolumn = (ccolumn <= 1) ? 1 : ccolumn-1;
break;
case 4: ccolumn = (ccolumn >= MXCOL) ? MXCOL : ccolumn+1;
break;
default:
disperr("jogcursor arg out of range");
return;
}
setcursor(cline, ccolumn);
}
/* Place String on the current line. The cursor is advanced to the
* position after the last character in the string, unless we hit the
* edge of the screen in which case the cursor stays pinned to the
* edge and doesn't move beyond it.
*/
void plstring(s)
char *s;
{
for ( ; *s != 0; s++) {
putsym((*s) & 0377);
}
ccolumn += strlen(s);
if (ccolumn >= MXCOL)
ccolumn = MXCOL; /* Assumes no wrap-around. */
}
/* Place a number of Spaces
* This routine can also be used to erase characters on the screen by
* overwriting them with spaces.
*/
void plnspaces(n)
int n;
{ int i;
if (n < 0) {
disperr("plnspaces: negative arg");
return;
}
for (i = 0 ; i < n ; i++) {
putsym(' ');
}
ccolumn += n;
if (ccolumn >= MXCOL)
ccolumn = MXCOL;
}
/* Place a Number of a given Character
*/
void plnchars(n, c)
int n;
int c;
{
int i;
if (n < 0) {
disperr("plnchars: negative arg");
return;
}
for (i = 0 ; i < n ; i++) {
putsym(c);
}
ccolumn += n;
if (ccolumn >= MXCOL)
ccolumn = MXCOL;
}
/* Vertical place a Character a Number of times.
* This routine can be used to draw vertical lines using
* a given character. It correctly handles displaying
* in column 80.
* The cursor is moved to the position below the last character
* placed on the screen. However the cursor will not move below
* the last line on the screen.
*/
void vertnchars(n,c)
int n;
int c;
{
int i;
if (n < 0) {
disperr("vertnchars: negative arg");
return;
}
for (i = 0 ; i < n ; i++) {
putsym(c);
/* Assume cursor motion ok even in graphic mode. */
setcursor(++cline, ccolumn);
if (cline >= MXLINE) {
cline = MXLINE;
break;
}
}
}
/* Delete characters after the cursor up until the End Of the Line
*/
void deleol(void)
{
Puts(erase_eol);
}
/* Delete characters after the cursor up until the End Of the Screen
*/
void deleos(void)
{
Puts(erase_eos);
}
/* Display Error message. The message is a string that does not end
* with a \n.
*/
void disperr(s)
char *s;
{ int sline, scolumn; /* Saved line and column numbers. */
sline = cline;
scolumn = ccolumn;
/* setcursor(1, 1); avoid bug when screen size unknown. */
printf("\n%s\n", s);
/* setcursor(sline, scolumn); or position not set. */
}
/* Put a string to stdout without trailing newline.
*/
void Puts(s)
char *s;
{
while(*s)
putchar(*s++);
}