change RV8, LRV8, and IND8 to use `struct' instead of `double' assignment
[unix-history] / usr / src / usr.bin / ex / ex_tty.c
index 62763af..d84bbc0 100644 (file)
@@ -1,4 +1,5 @@
-/* Copyright (c) 1979 Regents of the University of California */
+/* Copyright (c) 1980 Regents of the University of California */
+static char *sccsid = "@(#)ex_tty.c    6.2 %G%";
 #include "ex.h"
 #include "ex_tty.h"
 
 #include "ex.h"
 #include "ex_tty.h"
 
@@ -12,6 +13,7 @@ short ospeed = -1;
 gettmode()
 {
 
 gettmode()
 {
 
+#ifndef USG3TTY
        if (gtty(1, &tty) < 0)
                return;
        if (ospeed != tty.sg_ospeed)
        if (gtty(1, &tty) < 0)
                return;
        if (ospeed != tty.sg_ospeed)
@@ -21,17 +23,29 @@ gettmode()
        UPPERCASE = (tty.sg_flags & LCASE) != 0;
        GT = (tty.sg_flags & XTABS) != XTABS && !XT;
        NONL = (tty.sg_flags & CRMOD) == 0;
        UPPERCASE = (tty.sg_flags & LCASE) != 0;
        GT = (tty.sg_flags & XTABS) != XTABS && !XT;
        NONL = (tty.sg_flags & CRMOD) == 0;
+#else
+       if (ioctl(1, TCGETA, &tty) < 0)
+               return;
+       if (ospeed != tty.c_cflag & CBAUD)
+               value(SLOWOPEN) = (tty.c_cflag & CBAUD) < B1200;
+       ospeed = tty.c_cflag & CBAUD;
+       normf = tty;
+       UPPERCASE = (tty.c_iflag & IUCLC) != 0;
+       GT = (tty.c_oflag & TABDLY) != TAB3 && !XT;
+       NONL = (tty.c_oflag & OCRNL) == 0;
+#endif
 }
 
 char *xPC;
 char **sstrs[] = {
 }
 
 char *xPC;
 char **sstrs[] = {
-       &AL, &BC, &BT, &CD, &CE, &CL, &CM, &DC, &DL, &DM, &DO, &ED, &EI,
+       &AL, &BC, &BT, &CD, &CE, &CL, &CM, &xCR, &DC, &DL, &DM, &DO, &ED, &EI,
        &F0, &F1, &F2, &F3, &F4, &F5, &F6, &F7, &F8, &F9,
        &HO, &IC, &IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU, &LL,
        &F0, &F1, &F2, &F3, &F4, &F5, &F6, &F7, &F8, &F9,
        &HO, &IC, &IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU, &LL,
-       &ND, &xPC, &SE, &SF, &SO, &SR, &TA, &TE, &TI, &UP, &VB, &VS, &VE
+       &ND, &xNL, &xPC, &SE, &SF, &SO, &SR, &TA, &TE, &TI, &UP, &VB, &VS, &VE
 };
 bool *sflags[] = {
 };
 bool *sflags[] = {
-       &AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI, &NC, &OS, &UL, &XN, &XT
+       &AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI, &NC, &NS, &OS, &UL,
+       &XB, &XN, &XT, &XX
 };
 char **fkeys[10] = {
        &F0, &F1, &F2, &F3, &F4, &F5, &F6, &F7, &F8, &F9
 };
 char **fkeys[10] = {
        &F0, &F1, &F2, &F3, &F4, &F5, &F6, &F7, &F8, &F9
@@ -39,7 +53,7 @@ char **fkeys[10] = {
 setterm(type)
        char *type;
 {
 setterm(type)
        char *type;
 {
-       char *cgoto();
+       char *tgoto();
        register int unknown, i;
        register int l;
        char ltcbuf[TCBUFSIZE];
        register int unknown, i;
        register int l;
        char ltcbuf[TCBUFSIZE];
@@ -50,18 +64,20 @@ setterm(type)
        putpad(TE);
        if (tgetent(ltcbuf, type) != 1) {
                unknown++;
        putpad(TE);
        if (tgetent(ltcbuf, type) != 1) {
                unknown++;
-               CP(genbuf, "xx|dumb:");
+               CP(ltcbuf, "xx|dumb:");
        }
        i = LINES = tgetnum("li");
        if (LINES <= 5)
                LINES = 24;
        }
        i = LINES = tgetnum("li");
        if (LINES <= 5)
                LINES = 24;
-       if (LINES > 48)
-               LINES = 48;
+       if (LINES > TUBELINES)
+               LINES = TUBELINES;
        l = LINES;
        if (ospeed < B1200)
        l = LINES;
        if (ospeed < B1200)
-               l /= 2;
+               l = 9;  /* including the message line at the bottom */
        else if (ospeed < B2400)
        else if (ospeed < B2400)
-               l = (l * 2) / 3;
+               l = 17;
+       if (l > LINES)
+               l = LINES;
        aoftspace = tspace;
        zap();
        /*
        aoftspace = tspace;
        zap();
        /*
@@ -73,31 +89,53 @@ setterm(type)
        arrows[3].cap = KR; arrows[3].mapto = "l"; arrows[3].descr = "right";
        arrows[4].cap = KH; arrows[4].mapto = "H"; arrows[4].descr = "home";
 
        arrows[3].cap = KR; arrows[3].mapto = "l"; arrows[3].descr = "right";
        arrows[4].cap = KH; arrows[4].mapto = "H"; arrows[4].descr = "home";
 
+#ifdef TIOCLGET
+       /*
+        * Now map users susp char to ^Z, being careful that the susp
+        * overrides any arrow key, but only for hackers (=new tty driver).
+        */
+       {
+               static char sc[2];
+               int i, fnd;
+
+               ioctl(0, TIOCGETD, &ldisc);
+               if (ldisc == NTTYDISC) {
+                       sc[0] = olttyc.t_suspc;
+                       sc[1] = 0;
+                       if (olttyc.t_suspc == CTRL(z)) {
+                               for (i=0; i<=4; i++)
+                                       if (arrows[i].cap[0] == CTRL(z))
+                                               addmac(sc, NULL, NULL, arrows);
+                       } else
+                               addmac(sc, "\32", "susp", arrows);
+               }
+       }
+#endif
+
        options[WINDOW].ovalue = options[WINDOW].odefault = l - 1;
        if (defwind) options[WINDOW].ovalue = defwind;
        options[SCROLL].ovalue = options[SCROLL].odefault = HC ? 11 : ((l-1) / 2);
        COLUMNS = tgetnum("co");
        options[WINDOW].ovalue = options[WINDOW].odefault = l - 1;
        if (defwind) options[WINDOW].ovalue = defwind;
        options[SCROLL].ovalue = options[SCROLL].odefault = HC ? 11 : ((l-1) / 2);
        COLUMNS = tgetnum("co");
-       if (COLUMNS <= 20)
+       if (COLUMNS <= 4)
                COLUMNS = 1000;
                COLUMNS = 1000;
-       if (cgoto()[0] == 'O')  /* OOPS */
+       if (tgoto(CM, 2, 2)[0] == 'O')  /* OOPS */
                CA = 0, CM = 0;
        else
                CA = 0, CM = 0;
        else
-               CA = 1, costCM = strlen(tgoto(CM, 8, 10));
+               CA = 1, costCM = cost(tgoto(CM, 8, 10));
+       costSR = cost(SR);
+       costAL = cost(AL);
        PC = xPC ? xPC[0] : 0;
        aoftspace = tspace;
        PC = xPC ? xPC[0] : 0;
        aoftspace = tspace;
-       CP(ttytype, longname(genbuf, type));
+       CP(ttytype, longname(ltcbuf, type));
        if (i <= 0)
                LINES = 2;
        /* proper strings to change tty type */
        if (i <= 0)
                LINES = 2;
        /* proper strings to change tty type */
-#ifdef notdef
-       /* Taken out because we don't allow it. See ex_set.c for reasons. */
-       if (inopen)
-               putpad(VE);
-#endif
        termreset();
        gettmode();
        value(REDRAW) = AL && DL;
        value(OPTIMIZE) = !CA && !GT;
        termreset();
        gettmode();
        value(REDRAW) = AL && DL;
        value(OPTIMIZE) = !CA && !GT;
+       if (ospeed == B1200 && !value(REDRAW))
+               value(SLOWOPEN) = 1;    /* see also gettmode above */
        if (unknown)
                serror("%s: Unknown terminal type", type);
 }
        if (unknown)
                serror("%s: Unknown terminal type", type);
 }
@@ -108,13 +146,13 @@ zap()
        register bool **fp;
        register char ***sp;
 
        register bool **fp;
        register char ***sp;
 
-       namp = "ambsdadbeohchzinmincosulxnxt";
+       namp = "ambsdadbeohchzinmincnsosulxbxnxtxx";
        fp = sflags;
        do {
                *(*fp++) = tgetflag(namp);
                namp += 2;
        } while (*namp);
        fp = sflags;
        do {
                *(*fp++) = tgetflag(namp);
                namp += 2;
        } while (*namp);
-       namp = "albcbtcdceclcmdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullndpcsesfsosrtatetiupvbvsve";
+       namp = "albcbtcdceclcmcrdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullndnlpcsesfsosrtatetiupvbvsve";
        sp = sstrs;
        do {
                *(*sp++) = tgetstr(namp, &aoftspace);
        sp = sstrs;
        do {
                *(*sp++) = tgetstr(namp, &aoftspace);
@@ -151,3 +189,33 @@ fkey(i)
        else
                return(NOSTR);
 }
        else
                return(NOSTR);
 }
+
+/*
+ * cost figures out how much (in characters) it costs to send the string
+ * str to the terminal.  It takes into account padding information, as
+ * much as it can, for a typical case.  (Right now the typical case assumes
+ * the number of lines affected is the size of the screen, since this is
+ * mainly used to decide if AL or SR is better, and this always happens
+ * at the top of the screen.  We assume cursor motion (CM) has little
+ * padding, if any, required, so that case, which is really more important
+ * than AL vs SR, won't be really affected.)
+ */
+static int costnum;
+cost(str)
+char *str;
+{
+       int countnum();
+
+       if (str == NULL)
+               return 10000;   /* infinity */
+       costnum = 0;
+       tputs(str, LINES, countnum);
+       return costnum;
+}
+
+/* ARGSUSED */
+countnum(ch)
+char ch;
+{
+       costnum++;
+}