insert mode bug fix and cleanup
[unix-history] / usr / src / usr.bin / window / wwinit.c
index 454e863..be9bfcb 100644 (file)
@@ -1,6 +1,23 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)wwinit.c   3.22 %G%";
-#endif
+static char sccsid[] = "@(#)wwinit.c   3.35 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "ww.h"
 #include "tt.h"
 
 #include "ww.h"
 #include "tt.h"
@@ -12,9 +29,6 @@ wwinit()
 {
        register i, j;
        char *kp;
 {
        register i, j;
        char *kp;
-       register char **p, **q;
-       char **env, **termcap = 0;
-       extern char **environ;
        int s;
 
        wwdtablesize = getdtablesize();
        int s;
 
        wwdtablesize = getdtablesize();
@@ -22,8 +36,12 @@ wwinit()
        wwhead.ww_back = &wwhead;
 
        s = sigblock(sigmask(SIGIO));
        wwhead.ww_back = &wwhead;
 
        s = sigblock(sigmask(SIGIO));
-       if (signal(SIGIO, wwrint) == BADSIG)
+       if (signal(SIGIO, wwrint) == BADSIG ||
+           signal(SIGCHLD, wwchild) == BADSIG ||
+           signal(SIGPIPE, SIG_IGN) == BADSIG) {
+               wwerrno = WWE_SYS;
                return -1;
                return -1;
+       }
 
        if (wwgettty(0, &wwoldtty) < 0)
                return -1;
 
        if (wwgettty(0, &wwoldtty) < 0)
                return -1;
@@ -62,13 +80,12 @@ wwinit()
        }
        wwbaud = wwbaudmap[wwoldtty.ww_sgttyb.sg_ospeed];
 
        }
        wwbaud = wwbaudmap[wwoldtty.ww_sgttyb.sg_ospeed];
 
-       if (ttinit() < 0)
+       if (xxinit() < 0)
                goto bad;
        wwnrow = tt.tt_nrow;
        wwncol = tt.tt_ncol;
        wwavailmodes = tt.tt_availmodes;
        wwwrap = tt.tt_wrap;
                goto bad;
        wwnrow = tt.tt_nrow;
        wwncol = tt.tt_ncol;
        wwavailmodes = tt.tt_availmodes;
        wwwrap = tt.tt_wrap;
-       (*tt.tt_init)();
 
        if (wwavailmodes & WWM_REV)
                wwcursormodes = WWM_REV | wwavailmodes & WWM_BLK;
 
        if (wwavailmodes & WWM_REV)
                wwcursormodes = WWM_REV | wwavailmodes & WWM_BLK;
@@ -109,10 +126,34 @@ wwinit()
        for (i = 0; i < wwnrow; i++)
                wwtouched[i] = 0;
 
        for (i = 0; i < wwnrow; i++)
                wwtouched[i] = 0;
 
+       wwupd = (struct ww_update *) malloc((unsigned) wwnrow * sizeof *wwupd);
+       if (wwupd == 0) {
+               wwerrno = WWE_NOMEM;
+               goto bad;
+       }
+
        wwindex[WWX_NOBODY] = &wwnobody;
        wwnobody.ww_order = NWW;
 
        wwindex[WWX_NOBODY] = &wwnobody;
        wwnobody.ww_order = NWW;
 
-       kp = wwkeys;
+       kp = wwwintermcap;
+       if (wwavailmodes & WWM_REV)
+               wwaddcap1(WWT_REV, &kp);
+       if (wwavailmodes & WWM_BLK)
+               wwaddcap1(WWT_BLK, &kp);
+       if (wwavailmodes & WWM_UL)
+               wwaddcap1(WWT_UL, &kp);
+       if (wwavailmodes & WWM_GRP)
+               wwaddcap1(WWT_GRP, &kp);
+       if (wwavailmodes & WWM_DIM)
+               wwaddcap1(WWT_DIM, &kp);
+       if (wwavailmodes & WWM_USR)
+               wwaddcap1(WWT_USR, &kp);
+       if (tt.tt_insline && tt.tt_delline || tt.tt_setscroll)
+               wwaddcap1(WWT_ALDL, &kp);
+       if (tt.tt_inschar || tt.tt_insspace)
+               wwaddcap1(WWT_IMEI, &kp);
+       if (tt.tt_delchar)
+               wwaddcap1(WWT_DC, &kp);
        wwaddcap("kb", &kp);
        wwaddcap("ku", &kp);
        wwaddcap("kd", &kp);
        wwaddcap("kb", &kp);
        wwaddcap("ku", &kp);
        wwaddcap("kd", &kp);
@@ -121,7 +162,6 @@ wwinit()
        wwaddcap("kh", &kp);
        if ((j = tgetnum("kn")) >= 0) {
                char cap[32];
        wwaddcap("kh", &kp);
        if ((j = tgetnum("kn")) >= 0) {
                char cap[32];
-               int i;
 
                (void) sprintf(kp, "kn#%d:", j);
                for (; *kp; kp++)
 
                (void) sprintf(kp, "kn#%d:", j);
                for (; *kp; kp++)
@@ -133,24 +173,17 @@ wwinit()
                        wwaddcap(cap, &kp);
                }
        }
                        wwaddcap(cap, &kp);
                }
        }
-       for (i = 0, p = environ; *p++; i++)
-               ;
-       if ((env = (char **)malloc((unsigned)(i + 3) * sizeof (char *))) == 0)
-               goto bad;
-       for (p = environ, q = env; *p; p++, q++) {
-               if (strncmp(*p, "TERM=", 5) == 0)
-                       *q = WWT_TERM;
-               else if (strncmp(*p, "TERMCAP=", 8) == 0)
-                       termcap = q;
-               else
-                       *q = *p;
-       }
-       *(termcap ? termcap : q++) = wwwintermcap;
-       *q = 0;
-       environ = env;
+       /*
+        * It's ok to do this here even if setenv() is destructive
+        * since tt_init() has already made its own copy of it and
+        * wwterm now points to the copy.
+        */
+       (void) setenv("TERM", WWT_TERM, 1);
 
 
-       (void) signal(SIGPIPE, SIG_IGN);
        (void) sigsetmask(s);
        (void) sigsetmask(s);
+       /* catch typeahead before ASYNC was set */
+       (void) kill(getpid(), SIGIO);
+       xxstart();
        return 0;
 bad:
        /*
        return 0;
 bad:
        /*
@@ -164,8 +197,8 @@ bad:
 }
 
 wwaddcap(cap, kp)
 }
 
 wwaddcap(cap, kp)
-register char *cap;
-register char **kp;
+       register char *cap;
+       register char **kp;
 {
        char tbuf[512];
        char *tp = tbuf;
 {
        char tbuf[512];
        char *tp = tbuf;
@@ -184,3 +217,12 @@ register char **kp;
                **kp = 0;
        }
 }
                **kp = 0;
        }
 }
+
+wwaddcap1(cap, kp)
+       register char *cap;
+       register char **kp;
+{
+       while (*(*kp)++ = *cap++)
+               ;
+       (*kp)--;
+}