00001
00002
00003
00004
00005
00006
00007 # ifndef lint
00008 static char rcsid[] = "$Header: /opt/proj/minix/cvsroot/src/commands/yap/term.c,v 1.1.1.1 2005/04/21 14:55:41 beng Exp $";
00009 # endif
00010
00011 # define _TERM_
00012
00013 #include "in_all.h"
00014 #include "term.h"
00015 #include "machine.h"
00016 #include "output.h"
00017 #include "display.h"
00018 #include "options.h"
00019 #include "getline.h"
00020 #include "keys.h"
00021 #include "main.h"
00022
00023 #ifdef TIOCGWINSZ
00024 static struct winsize w;
00025 #endif
00026
00027 char *strcpy(),
00028 *strcat(),
00029 *tgoto(),
00030 *tgetstr(),
00031 *getenv();
00032
00033 static char tcbuf1[1024];
00034 static char * ptc;
00035 static char tcbuf[1024];
00036 short ospeed;
00037 char PC;
00038 char * UP;
00039 static char *ll;
00040
00041 struct linelist _X[100];
00042
00043 # if USG_TTY
00044 static struct termio _tty,_svtty;
00045 # elif POSIX_TTY
00046 static struct termios _tty, _svtty;
00047 # else
00048 # ifdef TIOCSPGRP
00049 static int proc_id, saved_pgrpid;
00050 # endif
00051 static struct sgttyb _tty,_svtty;
00052 # ifdef TIOCGETC
00053 static struct tchars _ttyc, _svttyc;
00054 # endif
00055 # ifdef TIOCGLTC
00056 static int line_discipline;
00057 static struct ltchars _lttyc, _svlttyc;
00058 # endif
00059 # endif
00060
00061 static VOID
00062 handle(c) char *c; {
00063
00064 if (isused(*c)) *c = 0377;
00065 }
00066
00067
00068
00069
00070
00071
00072 VOID
00073 inittty() {
00074 # if USG_TTY
00075 register struct termio *p = &_tty;
00076
00077 ioctl(0,TCGETA,(char *) p);
00078 _svtty = *p;
00079 if (p->c_oflag & TAB3) {
00080
00081
00082
00083 expandtabs = 1;
00084 }
00085 p->c_oflag &= ~(TAB3|OCRNL|ONLRET|ONLCR);
00086 p->c_oflag |= (OPOST);
00087
00088
00089
00090 p->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON);
00091 if (isused('S'&037) || isused('Q'&037)) p->c_iflag &= ~IXON;
00092 handle(&(p->c_cc[0]));
00093 handle(&(p->c_cc[1]));
00094 erasech = p->c_cc[VERASE];
00095 killch = p->c_cc[VKILL];
00096 p->c_cc[VMIN] = 1;
00097 p->c_cc[VTIME] = 0;
00098 ospeed = p->c_cflag & CBAUD;
00099 ioctl(0,TCSETAW,(char *) p);
00100 #elif POSIX_TTY
00101 register struct termios *p = &_tty;
00102
00103 tcgetattr(0, p);
00104 _svtty = *p;
00105 #ifdef _MINIX
00106 if (p->c_oflag & XTABS) {
00107
00108
00109
00110 expandtabs = 1;
00111 }
00112 p->c_oflag &= (OPOST|XTABS);
00113 #else
00114 p->c_oflag &= ~OPOST;
00115 #endif
00116 p->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON);
00117 if (isused('S'&037) || isused('Q'&037)) p->c_iflag &= ~IXON;
00118 handle(&(p->c_cc[VINTR]));
00119 handle(&(p->c_cc[VQUIT]));
00120 erasech = p->c_cc[VERASE];
00121 killch = p->c_cc[VKILL];
00122 p->c_cc[VMIN] = 1;
00123 p->c_cc[VTIME] = 0;
00124 ospeed = cfgetospeed(p);
00125 tcsetattr(0, TCSANOW, p);
00126 # else
00127 register struct sgttyb *p = &_tty;
00128
00129 # ifdef TIOCSPGRP
00130
00131
00132
00133
00134
00135
00136 if (ioctl(0, TIOCSPGRP, (char *) &proc_id) != -1) {
00137 setpgrp(0, proc_id);
00138 }
00139 # endif
00140 ioctl(0,TIOCGETP,(char *) p);
00141 _svtty = *p;
00142 erasech = p->sg_erase;
00143 killch = p->sg_kill;
00144 ospeed = p->sg_ospeed;
00145 if (p->sg_flags & XTABS) {
00146
00147
00148
00149 expandtabs = 1;
00150 }
00151 p->sg_flags |= (CBREAK);
00152 p->sg_flags &= ~(ECHO|XTABS|RAW|LCASE|CRMOD);
00153 #ifdef TIOCSETN
00154 ioctl(0, TIOCSETN, (char *) p);
00155 #else
00156 ioctl(0,TIOCSETP,(char *) p);
00157 #endif
00158
00159 #undef t_startc
00160 #undef t_stopc
00161 #undef t_intrc
00162 #undef t_quitc
00163 #undef t_suspc
00164 #undef t_dsuspc
00165 #undef t_flushc
00166 #undef t_lnextc
00167 # ifdef TIOCGETC
00168 { register struct tchars *q = &_ttyc;
00169
00170 ioctl(0,TIOCGETC,(char *) q);
00171 _svttyc = *q;
00172 handle(&(q->t_intrc));
00173 handle(&(q->t_quitc));
00174 if (isused(q->t_startc) || isused(q->t_stopc)) {
00175 q->t_startc = q->t_stopc = 0377;
00176 }
00177 ioctl(0,TIOCSETC, (char *) q);
00178 }
00179 # endif
00180 # ifdef TIOCGLTC
00181 { register struct ltchars *q = &_lttyc;
00182
00183 ioctl(0,TIOCGETD,(char *) &line_discipline);
00184 if (line_discipline == NTTYDISC) {
00185 ioctl(0, TIOCGLTC,(char *) q);
00186 _svlttyc = *q;
00187 handle(&(q->t_suspc));
00188 handle(&(q->t_dsuspc));
00189 q->t_flushc = q->t_lnextc = 0377;
00190 ioctl(0,TIOCSLTC, (char *) q);
00191 }
00192 }
00193 # endif
00194 # endif
00195 }
00196
00197
00198
00199
00200
00201 VOID
00202 resettty() {
00203
00204 # if USG_TTY
00205 ioctl(0,TCSETAW,(char *) &_svtty);
00206 # elif POSIX_TTY
00207 tcsetattr(0, TCSANOW, &_svtty);
00208 # else
00209 # ifdef TIOCSPGRP
00210 ioctl(0, TIOCSPGRP, (char *) &saved_pgrpid);
00211 setpgrp(0, saved_pgrpid);
00212 # endif
00213 ioctl(0,TIOCSETP,(char *) &_svtty);
00214 # ifdef TIOCGETC
00215 ioctl(0,TIOCSETC, (char *) &_svttyc);
00216 # endif
00217 # ifdef TIOCGLTC
00218 if (line_discipline == NTTYDISC) ioctl(0,TIOCSLTC, (char *) &_svlttyc);
00219 # endif
00220 # endif
00221 putline(TE);
00222 flush();
00223 }
00224
00225
00226
00227
00228
00229
00230 STATIC char *
00231 getcap(cap) char *cap; {
00232 register char *s;
00233
00234 s = tgetstr(cap, &ptc);
00235 if (!s) return "";
00236 return s;
00237 }
00238
00239
00240
00241
00242
00243 VOID
00244 ini_terminal() {
00245
00246 register char * s;
00247 register struct linelist *lp, *lp1;
00248 register i;
00249 register UG, SG;
00250 char tempbuf[20];
00251 char *mb, *mh, *mr;
00252
00253 initkeys();
00254 #if !_MINIX
00255 # ifdef TIOCSPGRP
00256 proc_id = getpid();
00257 ioctl(0,TIOCGPGRP, (char *) &saved_pgrpid);
00258 # endif
00259 #endif
00260 inittty();
00261 stupid = 1;
00262 ptc = tcbuf1;
00263 BC = "\b";
00264 TA = "\t";
00265 if (!(s = getenv("TERM"))) s = "dumb";
00266 if (tgetent(tcbuf, s) <= 0) {
00267 panic("No termcap entry");
00268 }
00269 stupid = 0;
00270 hardcopy = tgetflag("hc");
00271 PC = *(getcap("pc"));
00272 if (*(s = getcap("bc"))) {
00273
00274
00275
00276 BC = s;
00277 }
00278 UP = getcap("up");
00279 CE = getcap("ce");
00280 CL = getcap("cl");
00281 if (!*CL) cflag = 1;
00282 TI = getcap("ti");
00283 TE = getcap("te");
00284 CM = getcap("cm");
00285 SR = getcap("sr");
00286 AL = getcap("al");
00287 SO = getcap("so");
00288 SE = getcap("se");
00289 SG = tgetnum("sg");
00290 if (SG < 0) SG = 0;
00291 US = getcap("us");
00292 UE = getcap("ue");
00293 UG = tgetnum("ug");
00294 if (UG < 0) UG = 0;
00295 UC = getcap("uc");
00296 mb = getcap("mb");
00297 MD = getcap("md");
00298 ME = getcap("me");
00299 mh = getcap("mh");
00300 mr = getcap("mr");
00301 if (!nflag) {
00302
00303
00304
00305 (VOID) addstring(SO,SG,&sppat);
00306 (VOID) addstring(SE,SG,&sppat);
00307 (VOID) addstring(US,UG,&sppat);
00308 (VOID) addstring(UE,UG,&sppat);
00309 (VOID) addstring(mb,0,&sppat);
00310 (VOID) addstring(MD,0,&sppat);
00311 (VOID) addstring(ME,0,&sppat);
00312 (VOID) addstring(mh,0,&sppat);
00313 (VOID) addstring(mr,0,&sppat);
00314 if (*UC) {
00315 (VOID) strcpy(tempbuf,BC);
00316 (VOID) strcat(tempbuf,UC);
00317 (VOID) addstring(tempbuf,0,&sppat);
00318 }
00319 }
00320 if (UG > 0 || uflag) {
00321 US = "";
00322 UE = "";
00323 }
00324 if (*US || uflag) UC = "";
00325 COLS = tgetnum("co");
00326 i = tgetnum("li");
00327 AM = tgetflag("am");
00328 XN = tgetflag("xn");
00329 DB = tgetflag("db");
00330 if (!*(s = getcap("ho")) && *CM) {
00331 s = tgoto(CM,0,0);
00332 }
00333 if ((!*CE && !*AL) || !*s || hardcopy) {
00334 cflag = stupid = 1;
00335 }
00336 (VOID) strcpy(HO,s);
00337 if (*(s = getcap("ta"))) {
00338
00339
00340
00341 TA = s;
00342 }
00343 if (!*(ll = getcap("ll")) && *CM && i > 0) {
00344
00345
00346
00347 (VOID) strcpy(BO, tgoto(CM,0,i-1));
00348 }
00349 else (VOID) strcpy(BO, ll);
00350 if (COLS <= 0 || COLS > 256) {
00351 if ((unsigned) COLS >= 65409) {
00352
00353 COLS &= 0xffff;
00354 COLS -= (65409 - 128);
00355 }
00356 if (COLS <= 0 || COLS > 256) COLS = 80;
00357 }
00358 if (i <= 0) {
00359 i = 24;
00360 cflag = stupid = 1;
00361 }
00362 LINES = i;
00363 maxpagesize = i - 1;
00364 scrollsize = maxpagesize / 2;
00365 if (scrollsize <= 0) scrollsize = 1;
00366 if (!pagesize || pagesize >= i) {
00367 pagesize = maxpagesize;
00368 }
00369
00370
00371
00372
00373
00374
00375 scr_info.tail = lp = _X;
00376 lp1 = lp + (100 - 1);
00377 for (; lp <= lp1; lp++) {
00378
00379
00380
00381 lp->next = lp + 1;
00382 lp->prev = lp - 1;
00383 }
00384 lp1->next = scr_info.tail;
00385 lp1->next->prev = lp1;
00386 if (stupid) {
00387 (VOID) strcpy(BO,"\r\n");
00388 }
00389 putline(TI);
00390 window();
00391 }
00392
00393
00394
00395
00396
00397 VOID
00398 mgoto(n) register n; {
00399
00400 if (n == 0) home();
00401 else if (n == maxpagesize && *BO) bottom();
00402 else if (*CM) {
00403
00404
00405
00406 tputs(tgoto(CM,0,n),1,fputch);
00407 }
00408 else if (*BO && *UP && n >= (maxpagesize >> 1)) {
00409
00410
00411
00412 bottom();
00413 while (n++ < maxpagesize) putline(UP);
00414 }
00415 else {
00416 home();
00417 while (n--) putline("\r\n");
00418 }
00419 }
00420
00421
00422
00423
00424
00425 VOID
00426 clrbline() {
00427
00428 if (stupid) {
00429 putline("\r\n");
00430 return;
00431 }
00432 bottom();
00433 if (*CE) {
00434
00435
00436
00437 clrtoeol();
00438 return;
00439 }
00440 # ifdef VT100_PATCH
00441 insert_line(maxpagesize);
00442 # else
00443 insert_line();
00444 # endif
00445 }
00446
00447 # ifdef VT100_PATCH
00448 ins_line(l) {
00449 tputs(tgoto(AL, l, 0), maxpagesize - l, fputch);
00450 }
00451 # endif
00452
00453 VOID
00454 home() {
00455
00456 tputs(HO,1,fputch);
00457 }
00458
00459 VOID
00460 bottom() {
00461
00462 tputs(BO,1,fputch);
00463 if (!*BO) mgoto(maxpagesize);
00464 }
00465
00466 int
00467 window()
00468 {
00469 #ifdef TIOCGWINSZ
00470 if (ioctl(1, TIOCGWINSZ, &w) < 0) return 0;
00471
00472 if (w.ws_col == 0) w.ws_col = COLS;
00473 if (w.ws_row == 0) w.ws_row = LINES;
00474 if (w.ws_col != COLS || w.ws_row != LINES) {
00475 COLS = w.ws_col;
00476 LINES = w.ws_row;
00477 maxpagesize = LINES - 1;
00478 pagesize = maxpagesize;
00479 if (! *ll) (VOID) strcpy(BO, tgoto(CM,0,maxpagesize));
00480 scr_info.currentpos = 0;
00481 scrollsize = maxpagesize / 2;
00482 if (scrollsize <= 0) scrollsize = 1;
00483 return 1;
00484 }
00485 #endif
00486 return 0;
00487 }