source: trunk/minix/commands/yap/term.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 10.0 KB
Line 
1/* Copyright (c) 1985 Ceriel J.H. Jacobs */
2
3/*
4 * Terminal handling routines, mostly initializing.
5 */
6
7# ifndef lint
8static char rcsid[] = "$Header: /cvsup/minix/src/commands/yap/term.c,v 1.1.1.1 2005/04/21 14:55:41 beng Exp $";
9# endif
10
11# define _TERM_
12
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"
22
23#ifdef TIOCGWINSZ
24static struct winsize w;
25#endif
26
27char *strcpy(),
28 *strcat(),
29 *tgoto(),
30 *tgetstr(),
31 *getenv();
32
33static char tcbuf1[1024]; /* Holds terminal capability strings */
34static char * ptc; /* Pointer in it */
35static char tcbuf[1024]; /* Another termcap buffer */
36short ospeed; /* Needed for tputs() */
37char PC; /* Needed for tputs() */
38char * UP; /* Needed for tgoto() */
39static char *ll;
40
41struct linelist _X[100]; /* 100 is enough ? */
42
43# if USG_TTY
44static struct termio _tty,_svtty;
45# elif POSIX_TTY
46static struct termios _tty, _svtty;
47# else
48# ifdef TIOCSPGRP
49static int proc_id, saved_pgrpid;
50# endif
51static struct sgttyb _tty,_svtty;
52# ifdef TIOCGETC
53static struct tchars _ttyc, _svttyc;
54# endif
55# ifdef TIOCGLTC
56static int line_discipline;
57static struct ltchars _lttyc, _svlttyc;
58# endif
59# endif
60
61static VOID
62handle(c) char *c; { /* if character *c is used, set it to undefined */
63
64 if (isused(*c)) *c = 0377;
65}
66
67/*
68 * Set terminal in cbreak mode.
69 * Also check if tabs need expanding.
70 */
71
72VOID
73inittty() {
74# if USG_TTY
75 register struct termio *p = &_tty;
76
77 ioctl(0,TCGETA,(char *) p);
78 _svtty = *p;
79 if (p->c_oflag & TAB3) {
80 /*
81 * We do tab expansion ourselves
82 */
83 expandtabs = 1;
84 }
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
89 */
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;
102
103 tcgetattr(0, p);
104 _svtty = *p;
105#ifdef _MINIX /* Should be XTABS */
106 if (p->c_oflag & XTABS) {
107 /*
108 * We do tab expansion ourselves
109 */
110 expandtabs = 1;
111 }
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;
128
129# ifdef TIOCSPGRP
130 /*
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)
135 */
136 if (ioctl(0, TIOCSPGRP, (char *) &proc_id) != -1) {
137 setpgrp(0, proc_id);
138 }
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) {
146 /*
147 * We do tab expansion ourselves
148 */
149 expandtabs = 1;
150 }
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;
169
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;
176 }
177 ioctl(0,TIOCSETC, (char *) q);
178 }
179# endif
180# ifdef TIOCGLTC
181 { register struct ltchars *q = &_lttyc;
182
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);
191 }
192 }
193# endif
194# endif
195}
196
197/*
198 * Reset the terminal to its original state
199 */
200
201VOID
202resettty() {
203
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();
223}
224
225/*
226 * Get terminal capability "cap".
227 * If not present, return an empty string.
228 */
229
230STATIC char *
231getcap(cap) char *cap; {
232 register char *s;
233
234 s = tgetstr(cap, &ptc);
235 if (!s) return "";
236 return s;
237}
238
239/*
240 * Initialize some terminal-dependent stuff.
241 */
242
243VOID
244ini_terminal() {
245
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 */
252
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");
268 }
269 stupid = 0;
270 hardcopy = tgetflag("hc"); /* Hard copy terminal?*/
271 PC = *(getcap("pc"));
272 if (*(s = getcap("bc"))) {
273 /*
274 * Backspace if not ^H
275 */
276 BC = s;
277 }
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) {
302 /*
303 * Recognize special strings
304 */
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);
318 }
319 }
320 if (UG > 0 || uflag) {
321 US = "";
322 UE = "";
323 }
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 */
332 }
333 if ((!*CE && !*AL) || !*s || hardcopy) {
334 cflag = stupid = 1;
335 }
336 (VOID) strcpy(HO,s);
337 if (*(s = getcap("ta"))) {
338 /*
339 * Tab (other than ^I or padding)
340 */
341 TA = s;
342 }
343 if (!*(ll = getcap("ll")) && *CM && i > 0) {
344 /*
345 * Lower left hand corner
346 */
347 (VOID) strcpy(BO, tgoto(CM,0,i-1));
348 }
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);
355 }
356 if (COLS <= 0 || COLS > 256) COLS = 80;
357 }
358 if (i <= 0) {
359 i = 24;
360 cflag = stupid = 1;
361 }
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;
368 }
369
370 /*
371 * The next part does not really belong here, but there it is ...
372 * Initialize a circular list for the screenlines.
373 */
374
375 scr_info.tail = lp = _X;
376 lp1 = lp + (100 - 1);
377 for (; lp <= lp1; lp++) {
378 /*
379 * Circular doubly linked list
380 */
381 lp->next = lp + 1;
382 lp->prev = lp - 1;
383 }
384 lp1->next = scr_info.tail;
385 lp1->next->prev = lp1;
386 if (stupid) {
387 (VOID) strcpy(BO,"\r\n");
388 }
389 putline(TI);
390 window();
391}
392
393/*
394 * Place cursor at start of line n.
395 */
396
397VOID
398mgoto(n) register n; {
399
400 if (n == 0) home();
401 else if (n == maxpagesize && *BO) bottom();
402 else if (*CM) {
403 /*
404 * Cursor addressing
405 */
406 tputs(tgoto(CM,0,n),1,fputch);
407 }
408 else if (*BO && *UP && n >= (maxpagesize >> 1)) {
409 /*
410 * Bottom and then up
411 */
412 bottom();
413 while (n++ < maxpagesize) putline(UP);
414 }
415 else { /* Home, and then down */
416 home();
417 while (n--) putline("\r\n");
418 }
419}
420
421/*
422 * Clear bottom line
423 */
424
425VOID
426clrbline() {
427
428 if (stupid) {
429 putline("\r\n");
430 return;
431 }
432 bottom();
433 if (*CE) {
434 /*
435 * We can clear to end of line
436 */
437 clrtoeol();
438 return;
439 }
440# ifdef VT100_PATCH
441 insert_line(maxpagesize);
442# else
443 insert_line();
444# endif
445}
446
447# ifdef VT100_PATCH
448ins_line(l) {
449 tputs(tgoto(AL, l, 0), maxpagesize - l, fputch);
450}
451# endif
452
453VOID
454home() {
455
456 tputs(HO,1,fputch);
457}
458
459VOID
460bottom() {
461
462 tputs(BO,1,fputch);
463 if (!*BO) mgoto(maxpagesize);
464}
465
466int
467window()
468{
469#ifdef TIOCGWINSZ
470 if (ioctl(1, TIOCGWINSZ, &w) < 0) return 0;
471
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;
484 }
485#endif
486 return 0;
487}
Note: See TracBrowser for help on using the repository browser.