forget difference between big and small commands - obsolete with vm.
[minix.git] / commands / yap / term.c
blob35b8e020a477c9803fb87933c09d43f4d47a3ce5
1 /* Copyright (c) 1985 Ceriel J.H. Jacobs */
3 /*
4 * Terminal handling routines, mostly initializing.
5 */
7 # ifndef lint
8 static char rcsid[] = "$Header$";
9 # endif
11 # define _TERM_
13 #include "in_all.h"
14 #include "term.h"
15 #include "machine.h"
16 #include "output.h"
17 #include "display.h"
18 #include "options.h"
19 #include "getline.h"
20 #include "keys.h"
21 #include "main.h"
23 #ifdef TIOCGWINSZ
24 static struct winsize w;
25 #endif
27 char *strcpy(),
28 *strcat(),
29 *tgoto(),
30 *tgetstr(),
31 *getenv();
33 static char tcbuf1[1024]; /* Holds terminal capability strings */
34 static char * ptc; /* Pointer in it */
35 static char tcbuf[1024]; /* Another termcap buffer */
36 short ospeed; /* Needed for tputs() */
37 char PC; /* Needed for tputs() */
38 char * UP; /* Needed for tgoto() */
39 static char *ll;
41 struct linelist _X[100]; /* 100 is enough ? */
43 # if USG_TTY
44 static struct termio _tty,_svtty;
45 # elif POSIX_TTY
46 static struct termios _tty, _svtty;
47 # else
48 # ifdef TIOCSPGRP
49 static int proc_id, saved_pgrpid;
50 # endif
51 static struct sgttyb _tty,_svtty;
52 # ifdef TIOCGETC
53 static struct tchars _ttyc, _svttyc;
54 # endif
55 # ifdef TIOCGLTC
56 static int line_discipline;
57 static struct ltchars _lttyc, _svlttyc;
58 # endif
59 # endif
61 static VOID
62 handle(c) char *c; { /* if character *c is used, set it to undefined */
64 if (isused(*c)) *c = 0377;
68 * Set terminal in cbreak mode.
69 * Also check if tabs need expanding.
72 VOID
73 inittty() {
74 # if USG_TTY
75 register struct termio *p = &_tty;
77 ioctl(0,TCGETA,(char *) p);
78 _svtty = *p;
79 if (p->c_oflag & TAB3) {
81 * We do tab expansion ourselves
83 expandtabs = 1;
85 p->c_oflag &= ~(TAB3|OCRNL|ONLRET|ONLCR);
86 p->c_oflag |= (/*ONOCR|*/OPOST); /* ONOCR does not seem to work
87 very well in combination with
88 ~ONLCR
90 p->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON);
91 if (isused('S'&037) || isused('Q'&037)) p->c_iflag &= ~IXON;
92 handle(&(p->c_cc[0])); /* INTR and QUIT (mnemonics not defined ??) */
93 handle(&(p->c_cc[1]));
94 erasech = p->c_cc[VERASE];
95 killch = p->c_cc[VKILL];
96 p->c_cc[VMIN] = 1; /* Just wait for one character */
97 p->c_cc[VTIME] = 0;
98 ospeed = p->c_cflag & CBAUD;
99 ioctl(0,TCSETAW,(char *) p);
100 #elif POSIX_TTY
101 register struct termios *p = &_tty;
103 tcgetattr(0, p);
104 _svtty = *p;
105 #ifdef _MINIX /* Should be XTABS */
106 if (p->c_oflag & XTABS) {
108 * We do tab expansion ourselves
110 expandtabs = 1;
112 p->c_oflag &= (OPOST|XTABS);
113 #else
114 p->c_oflag &= ~OPOST;
115 #endif
116 p->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON);
117 if (isused('S'&037) || isused('Q'&037)) p->c_iflag &= ~IXON;
118 handle(&(p->c_cc[VINTR]));
119 handle(&(p->c_cc[VQUIT]));
120 erasech = p->c_cc[VERASE];
121 killch = p->c_cc[VKILL];
122 p->c_cc[VMIN] = 1; /* Just wait for one character */
123 p->c_cc[VTIME] = 0;
124 ospeed = cfgetospeed(p);
125 tcsetattr(0, TCSANOW, p);
126 # else
127 register struct sgttyb *p = &_tty;
129 # ifdef TIOCSPGRP
131 * If we can, we put yap in another process group, and the terminal
132 * with it. This is done, so that interrupts given by the user
133 * will only affect yap and not it's children (processes writing
134 * on a pipe to yap)
136 if (ioctl(0, TIOCSPGRP, (char *) &proc_id) != -1) {
137 setpgrp(0, proc_id);
139 # endif
140 ioctl(0,TIOCGETP,(char *) p);
141 _svtty = *p;
142 erasech = p->sg_erase;
143 killch = p->sg_kill;
144 ospeed = p->sg_ospeed;
145 if (p->sg_flags & XTABS) {
147 * We do tab expansion ourselves
149 expandtabs = 1;
151 p->sg_flags |= (CBREAK);
152 p->sg_flags &= ~(ECHO|XTABS|RAW|LCASE|CRMOD);
153 #ifdef TIOCSETN
154 ioctl(0, TIOCSETN, (char *) p);
155 #else
156 ioctl(0,TIOCSETP,(char *) p);
157 #endif
158 /* Bloody Sun ... */
159 #undef t_startc
160 #undef t_stopc
161 #undef t_intrc
162 #undef t_quitc
163 #undef t_suspc
164 #undef t_dsuspc
165 #undef t_flushc
166 #undef t_lnextc
167 # ifdef TIOCGETC
168 { register struct tchars *q = &_ttyc;
170 ioctl(0,TIOCGETC,(char *) q);
171 _svttyc = *q;
172 handle(&(q->t_intrc));
173 handle(&(q->t_quitc));
174 if (isused(q->t_startc) || isused(q->t_stopc)) {
175 q->t_startc = q->t_stopc = 0377;
177 ioctl(0,TIOCSETC, (char *) q);
179 # endif
180 # ifdef TIOCGLTC
181 { register struct ltchars *q = &_lttyc;
183 ioctl(0,TIOCGETD,(char *) &line_discipline);
184 if (line_discipline == NTTYDISC) {
185 ioctl(0, TIOCGLTC,(char *) q);
186 _svlttyc = *q;
187 handle(&(q->t_suspc));
188 handle(&(q->t_dsuspc));
189 q->t_flushc = q->t_lnextc = 0377;
190 ioctl(0,TIOCSLTC, (char *) q);
193 # endif
194 # endif
198 * Reset the terminal to its original state
201 VOID
202 resettty() {
204 # if USG_TTY
205 ioctl(0,TCSETAW,(char *) &_svtty);
206 # elif POSIX_TTY
207 tcsetattr(0, TCSANOW, &_svtty);
208 # else
209 # ifdef TIOCSPGRP
210 ioctl(0, TIOCSPGRP, (char *) &saved_pgrpid);
211 setpgrp(0, saved_pgrpid);
212 # endif
213 ioctl(0,TIOCSETP,(char *) &_svtty);
214 # ifdef TIOCGETC
215 ioctl(0,TIOCSETC, (char *) &_svttyc);
216 # endif
217 # ifdef TIOCGLTC
218 if (line_discipline == NTTYDISC) ioctl(0,TIOCSLTC, (char *) &_svlttyc);
219 # endif
220 # endif
221 putline(TE);
222 flush();
226 * Get terminal capability "cap".
227 * If not present, return an empty string.
230 STATIC char *
231 getcap(cap) char *cap; {
232 register char *s;
234 s = tgetstr(cap, &ptc);
235 if (!s) return "";
236 return s;
240 * Initialize some terminal-dependent stuff.
243 VOID
244 ini_terminal() {
246 register char * s;
247 register struct linelist *lp, *lp1;
248 register i;
249 register UG, SG;
250 char tempbuf[20];
251 char *mb, *mh, *mr; /* attributes */
253 initkeys();
254 #if !_MINIX
255 # ifdef TIOCSPGRP
256 proc_id = getpid();
257 ioctl(0,TIOCGPGRP, (char *) &saved_pgrpid);
258 # endif
259 #endif
260 inittty();
261 stupid = 1;
262 ptc = tcbuf1;
263 BC = "\b";
264 TA = "\t";
265 if (!(s = getenv("TERM"))) s = "dumb";
266 if (tgetent(tcbuf, s) <= 0) {
267 panic("No termcap entry");
269 stupid = 0;
270 hardcopy = tgetflag("hc"); /* Hard copy terminal?*/
271 PC = *(getcap("pc"));
272 if (*(s = getcap("bc"))) {
274 * Backspace if not ^H
276 BC = s;
278 UP = getcap("up"); /* move up a line */
279 CE = getcap("ce"); /* clear to end of line */
280 CL = getcap("cl"); /* clear screen */
281 if (!*CL) cflag = 1;
282 TI = getcap("ti"); /* Initialization for CM */
283 TE = getcap("te"); /* end for CM */
284 CM = getcap("cm"); /* cursor addressing */
285 SR = getcap("sr"); /* scroll reverse */
286 AL = getcap("al"); /* Insert line */
287 SO = getcap("so"); /* standout */
288 SE = getcap("se"); /* standend */
289 SG = tgetnum("sg"); /* blanks left by SO, SE */
290 if (SG < 0) SG = 0;
291 US = getcap("us"); /* underline */
292 UE = getcap("ue"); /* end underline */
293 UG = tgetnum("ug"); /* blanks left by US, UE */
294 if (UG < 0) UG = 0;
295 UC = getcap("uc"); /* underline a character */
296 mb = getcap("mb"); /* blinking attribute */
297 MD = getcap("md"); /* bold attribute */
298 ME = getcap("me"); /* turn off attributes */
299 mh = getcap("mh"); /* half bright attribute */
300 mr = getcap("mr"); /* reversed video attribute */
301 if (!nflag) {
303 * Recognize special strings
305 (VOID) addstring(SO,SG,&sppat);
306 (VOID) addstring(SE,SG,&sppat);
307 (VOID) addstring(US,UG,&sppat);
308 (VOID) addstring(UE,UG,&sppat);
309 (VOID) addstring(mb,0,&sppat);
310 (VOID) addstring(MD,0,&sppat);
311 (VOID) addstring(ME,0,&sppat);
312 (VOID) addstring(mh,0,&sppat);
313 (VOID) addstring(mr,0,&sppat);
314 if (*UC) {
315 (VOID) strcpy(tempbuf,BC);
316 (VOID) strcat(tempbuf,UC);
317 (VOID) addstring(tempbuf,0,&sppat);
320 if (UG > 0 || uflag) {
321 US = "";
322 UE = "";
324 if (*US || uflag) UC = "";
325 COLS = tgetnum("co"); /* columns on page */
326 i = tgetnum("li"); /* Lines on page */
327 AM = tgetflag("am"); /* terminal wraps automatically? */
328 XN = tgetflag("xn"); /* and then ignores next newline? */
329 DB = tgetflag("db"); /* terminal retains lines below */
330 if (!*(s = getcap("ho")) && *CM) {
331 s = tgoto(CM,0,0); /* Another way of getting home */
333 if ((!*CE && !*AL) || !*s || hardcopy) {
334 cflag = stupid = 1;
336 (VOID) strcpy(HO,s);
337 if (*(s = getcap("ta"))) {
339 * Tab (other than ^I or padding)
341 TA = s;
343 if (!*(ll = getcap("ll")) && *CM && i > 0) {
345 * Lower left hand corner
347 (VOID) strcpy(BO, tgoto(CM,0,i-1));
349 else (VOID) strcpy(BO, ll);
350 if (COLS <= 0 || COLS > 256) {
351 if ((unsigned) COLS >= 65409) {
352 /* SUN bug */
353 COLS &= 0xffff;
354 COLS -= (65409 - 128);
356 if (COLS <= 0 || COLS > 256) COLS = 80;
358 if (i <= 0) {
359 i = 24;
360 cflag = stupid = 1;
362 LINES = i;
363 maxpagesize = i - 1;
364 scrollsize = maxpagesize / 2;
365 if (scrollsize <= 0) scrollsize = 1;
366 if (!pagesize || pagesize >= i) {
367 pagesize = maxpagesize;
371 * The next part does not really belong here, but there it is ...
372 * Initialize a circular list for the screenlines.
375 scr_info.tail = lp = _X;
376 lp1 = lp + (100 - 1);
377 for (; lp <= lp1; lp++) {
379 * Circular doubly linked list
381 lp->next = lp + 1;
382 lp->prev = lp - 1;
384 lp1->next = scr_info.tail;
385 lp1->next->prev = lp1;
386 if (stupid) {
387 (VOID) strcpy(BO,"\r\n");
389 putline(TI);
390 window();
394 * Place cursor at start of line n.
397 VOID
398 mgoto(n) register n; {
400 if (n == 0) home();
401 else if (n == maxpagesize && *BO) bottom();
402 else if (*CM) {
404 * Cursor addressing
406 tputs(tgoto(CM,0,n),1,fputch);
408 else if (*BO && *UP && n >= (maxpagesize >> 1)) {
410 * Bottom and then up
412 bottom();
413 while (n++ < maxpagesize) putline(UP);
415 else { /* Home, and then down */
416 home();
417 while (n--) putline("\r\n");
422 * Clear bottom line
425 VOID
426 clrbline() {
428 if (stupid) {
429 putline("\r\n");
430 return;
432 bottom();
433 if (*CE) {
435 * We can clear to end of line
437 clrtoeol();
438 return;
440 # ifdef VT100_PATCH
441 insert_line(maxpagesize);
442 # else
443 insert_line();
444 # endif
447 # ifdef VT100_PATCH
448 ins_line(l) {
449 tputs(tgoto(AL, l, 0), maxpagesize - l, fputch);
451 # endif
453 VOID
454 home() {
456 tputs(HO,1,fputch);
459 VOID
460 bottom() {
462 tputs(BO,1,fputch);
463 if (!*BO) mgoto(maxpagesize);
467 window()
469 #ifdef TIOCGWINSZ
470 if (ioctl(1, TIOCGWINSZ, &w) < 0) return 0;
472 if (w.ws_col == 0) w.ws_col = COLS;
473 if (w.ws_row == 0) w.ws_row = LINES;
474 if (w.ws_col != COLS || w.ws_row != LINES) {
475 COLS = w.ws_col;
476 LINES = w.ws_row;
477 maxpagesize = LINES - 1;
478 pagesize = maxpagesize;
479 if (! *ll) (VOID) strcpy(BO, tgoto(CM,0,maxpagesize));
480 scr_info.currentpos = 0;
481 scrollsize = maxpagesize / 2;
482 if (scrollsize <= 0) scrollsize = 1;
483 return 1;
485 #endif
486 return 0;