term.c

Go to the documentation of this file.
00001 /* Copyright (c) 1985 Ceriel J.H. Jacobs */
00002 
00003 /*
00004  * Terminal handling routines, mostly initializing.
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];   /* Holds terminal capability strings */
00034 static char *   ptc;            /* Pointer in it */
00035 static char     tcbuf[1024];    /* Another termcap buffer */
00036 short   ospeed;         /* Needed for tputs() */
00037 char    PC;             /* Needed for tputs() */
00038 char *  UP;             /* Needed for tgoto() */
00039 static char     *ll;
00040 
00041 struct linelist _X[100];        /* 100 is enough ? */
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; {    /* if character *c is used, set it to undefined */
00063 
00064         if (isused(*c)) *c = 0377;
00065 }
00066 
00067 /*
00068  * Set terminal in cbreak mode.
00069  * Also check if tabs need expanding.
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                  * We do tab expansion ourselves
00082                  */
00083                 expandtabs = 1;
00084         }
00085         p->c_oflag &= ~(TAB3|OCRNL|ONLRET|ONLCR);
00086         p->c_oflag |= (/*ONOCR|*/OPOST);        /* ONOCR does not seem to work
00087                                                    very well in combination with
00088                                                    ~ONLCR
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]));  /* INTR and QUIT (mnemonics not defined ??) */
00093         handle(&(p->c_cc[1]));
00094         erasech = p->c_cc[VERASE];
00095         killch = p->c_cc[VKILL];
00096         p->c_cc[VMIN] = 1;      /* Just wait for one character */
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   /* Should be XTABS */
00106         if (p->c_oflag & XTABS) {
00107                 /*
00108                  * We do tab expansion ourselves
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;      /* Just wait for one character */
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          * If we can, we put yap in another process group, and the terminal
00132          * with it. This is done, so that interrupts given by the user
00133          * will only affect yap and not it's children (processes writing
00134          * on a pipe to yap)
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                  * We do tab expansion ourselves
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 /* Bloody Sun ... */
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  * Reset the terminal to its original state
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  * Get terminal capability "cap".
00227  * If not present, return an empty string.
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  * Initialize some terminal-dependent stuff.
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;     /* attributes */
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");      /* Hard copy terminal?*/
00271         PC = *(getcap("pc"));
00272         if (*(s = getcap("bc"))) {
00273                 /*
00274                  * Backspace if not ^H
00275                  */
00276                 BC = s;
00277         }
00278         UP = getcap("up");              /* move up a line */
00279         CE = getcap("ce");              /* clear to end of line */
00280         CL = getcap("cl");              /* clear screen */
00281         if (!*CL) cflag = 1;
00282         TI = getcap("ti");              /* Initialization for CM */
00283         TE = getcap("te");              /* end for CM */
00284         CM = getcap("cm");              /* cursor addressing */
00285         SR = getcap("sr");              /* scroll reverse */
00286         AL = getcap("al");              /* Insert line */
00287         SO = getcap("so");              /* standout */
00288         SE = getcap("se");              /* standend */
00289         SG = tgetnum("sg");             /* blanks left by SO, SE */
00290         if (SG < 0) SG = 0;
00291         US = getcap("us");              /* underline */
00292         UE = getcap("ue");              /* end underline */
00293         UG = tgetnum("ug");             /* blanks left by US, UE */
00294         if (UG < 0) UG = 0;
00295         UC = getcap("uc");              /* underline a character */
00296         mb = getcap("mb");              /* blinking attribute */
00297         MD = getcap("md");              /* bold attribute */
00298         ME = getcap("me");              /* turn off attributes */
00299         mh = getcap("mh");              /* half bright attribute */
00300         mr = getcap("mr");              /* reversed video attribute */
00301         if (!nflag) {
00302                 /*
00303                  * Recognize special strings
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");           /* columns on page */
00326         i = tgetnum("li");              /* Lines on page */
00327         AM = tgetflag("am");            /* terminal wraps automatically? */
00328         XN = tgetflag("xn");            /* and then ignores next newline? */
00329         DB = tgetflag("db");            /* terminal retains lines below */
00330         if (!*(s = getcap("ho")) && *CM) {
00331                 s = tgoto(CM,0,0);      /* Another way of getting home */
00332         }
00333         if ((!*CE && !*AL) || !*s || hardcopy) {
00334                 cflag = stupid = 1;
00335         }
00336         (VOID) strcpy(HO,s);
00337         if (*(s = getcap("ta"))) {
00338                 /*
00339                  * Tab (other than ^I or padding)
00340                  */
00341                 TA = s;
00342         }
00343        if (!*(ll = getcap("ll")) && *CM && i > 0) {
00344                 /*
00345                  * Lower left hand corner
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                         /* SUN bug */
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          * The next part does not really belong here, but there it is ...
00372          * Initialize a circular list for the screenlines.
00373          */
00374 
00375        scr_info.tail = lp = _X;
00376        lp1 = lp + (100 - 1);
00377         for (; lp <= lp1; lp++) {
00378                 /*
00379                  * Circular doubly linked list
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  * Place cursor at start of line n.
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                  * Cursor addressing
00405                  */
00406                 tputs(tgoto(CM,0,n),1,fputch);
00407         }
00408         else if (*BO && *UP && n >= (maxpagesize >> 1)) {
00409                 /*
00410                  * Bottom and then up
00411                  */
00412                 bottom();
00413                 while (n++ < maxpagesize) putline(UP);
00414         }
00415         else {  /* Home, and then down */
00416                 home();
00417                 while (n--) putline("\r\n");
00418         }
00419 }
00420 
00421 /*
00422  * Clear bottom line
00423  */
00424 
00425 VOID
00426 clrbline() {
00427 
00428         if (stupid) {
00429                 putline("\r\n");
00430                 return;
00431         }
00432         bottom();
00433         if (*CE) {
00434                 /*
00435                  * We can clear to end of line
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 }

Generated on Fri Apr 14 22:57:12 2006 for minix by  doxygen 1.4.6