less verbose...
[unix-history] / usr / src / usr.bin / window / main.c
index c69814b..1e9c188 100644 (file)
@@ -1,25 +1,45 @@
+/*
+ * 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 = "@(#)main.c      2.1 83/07/30";
-#endif
+static char sccsid[] = "@(#)main.c     3.34 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "defs.h"
 
 #include "defs.h"
-
-char escapec = CTRL(p);
+#include <sys/signal.h>
+#include <stdio.h>
+#include "string.h"
+#include "char.h"
+#include "local.h"
 
 #define next(a) (*++*(a) ? *(a) : (*++(a) ? *(a) : (char *)usage()))
 
 
 #define next(a) (*++*(a) ? *(a) : (*++(a) ? *(a) : (char *)usage()))
 
-/*ARGUSED*/
+/*ARGSUSED*/
 main(argc, argv)
 char **argv;
 {
 main(argc, argv)
 char **argv;
 {
-       register n;
        register char *p;
        register char *p;
-       char fast = 0;
-       int wwchild();
-       int imask;
-       char *rindex();
-       char *getenv();
+       char fflag = 0;
+       char dflag = 0;
+       char xflag = 0;
+       char *cmd = 0;
+       char tflag = 0;
 
 
+       escapec = ESCAPEC;      
        if (p = rindex(*argv, '/'))
                p++;
        else
        if (p = rindex(*argv, '/'))
                p++;
        else
@@ -29,110 +49,104 @@ char **argv;
                if (**argv == '-') {
                        switch (*++*argv) {
                        case 'f':
                if (**argv == '-') {
                        switch (*++*argv) {
                        case 'f':
-                               fast++;
+                               fflag++;
+                               break;
+                       case 'c':
+                               if (cmd != 0) {
+                                       (void) fprintf(stderr,
+                                               "Only one -c allowed.\n");
+                                       (void) usage();
+                               }
+                               cmd = next(argv);
                                break;
                        case 'e':
                                setescape(next(argv));
                                break;
                        case 't':
                                break;
                        case 'e':
                                setescape(next(argv));
                                break;
                        case 't':
-                               terse++;
+                               tflag++;
                                break;
                        case 'd':
                                break;
                        case 'd':
-                               debug++;
+                               dflag++;
+                               break;
+                       case 'D':
+                               debug = !debug;
+                               break;
+                       case 'x':
+                               xflag++;
                                break;
                        default:
                                break;
                        default:
-                               usage();
+                               (void) usage();
                        }
                } else
                        }
                } else
-                       usage();
+                       (void) usage();
        }
        }
-       if ((shell = getenv("SHELL")) == 0)
-               shell = "/bin/csh";
-       if (shellname = rindex(shell, '/'))
-               shellname++;
+       if ((p = getenv("SHELL")) == 0)
+               p = SHELL;
+       if ((default_shellfile = str_cpy(p)) == 0)
+               nomem();
+       if (p = rindex(default_shellfile, '/'))
+               p++;
        else
        else
-               shellname = shell;
-       gettimeofday(&starttime, &timezone);
+               p = default_shellfile;
+       default_shell[0] = p;
+       default_shell[1] = 0;
+       default_nline = NLINE;
+       default_smooth = 1;
+       (void) gettimeofday(&starttime, (struct timezone *)0);
        if (wwinit() < 0) {
        if (wwinit() < 0) {
-               fflush(stdout);
-               fprintf("Can't do windows on this terminal.\n");
+               (void) fprintf(stderr, "%s.\n", wwerror());
                exit(1);
        }
                exit(1);
        }
-       if (debug) {
+       if (debug)
                wwnewtty.ww_tchars.t_quitc = wwoldtty.ww_tchars.t_quitc;
                wwnewtty.ww_tchars.t_quitc = wwoldtty.ww_tchars.t_quitc;
-               wwsettty(0, &wwnewtty);
+       if (xflag) {
+               wwnewtty.ww_tchars.t_stopc = wwoldtty.ww_tchars.t_stopc;
+               wwnewtty.ww_tchars.t_startc = wwoldtty.ww_tchars.t_startc;
        }
        }
-       if ((cmdwin = wwopen(WW_NONE, 0, 1, wwncol, 0, 0)) == 0) {
-               fflush(stdout);
-               fprintf(stderr, "Can't open command window.\r\n");
+       if (debug || xflag)
+               (void) wwsettty(0, &wwnewtty, &wwoldtty);
+
+       if ((cmdwin = wwopen(wwbaud > 2400 ? WWO_REVERSE : 0, 1, wwncol,
+                            0, 0, 0)) == 0) {
+               (void) wwflush();
+               (void) fprintf(stderr, "%s.\r\n", wwerror());
+               goto bad;
+       }
+       cmdwin->ww_mapnl = 1;
+       cmdwin->ww_nointr = 1;
+       cmdwin->ww_noupdate = 1;
+       cmdwin->ww_unctrl = 1;
+       if ((framewin = wwopen(WWO_GLASS|WWO_FRAME, wwnrow, wwncol, 0, 0, 0))
+           == 0) {
+               (void) wwflush();
+               (void) fprintf(stderr, "%s.\r\n", wwerror());
                goto bad;
        }
                goto bad;
        }
-       if (terse)
-               Whide(cmdwin->ww_win);
-       wwsetcurwin(cmdwin);
-       for (n = 0; n < wwncol; n++)                    /* XXX */
-               Waputc(0, WINVERSE|WBUF, cmdwin->ww_win);
+       wwadd(framewin, &wwhead);
+       if ((boxwin = wwopen(WWO_GLASS, wwnrow, wwncol, 0, 0, 0)) == 0) {
+               (void) wwflush();
+               (void) fprintf(stderr, "%s.\r\n", wwerror());
+               goto bad;
+       }
+       fgwin = framewin;
+
+       wwupdate();
        wwflush();
        (void) signal(SIGCHLD, wwchild);
        wwflush();
        (void) signal(SIGCHLD, wwchild);
-       if (!fast) {
-               if (doconfig() < 0)
+       setvars();
+
+       setterse(tflag);
+       setcmd(1);
+       if (cmd != 0)
+               (void) dolongcmd(cmd, (struct value *)0, 0);
+       if (!fflag)
+               if (dflag || doconfig() < 0)
                        dodefault();
                        dodefault();
-               if (selwin != 0) {
-                       wwsetcurwin(selwin);
-                       Woncursor(selwin->ww_win, 0);
-               }
-       }
-       while (!quit) {
-               if (curwin == cmdwin) {
-                       docmd();
-                       continue;
-               }
-               /*
-                * Loop until we get some keyboard input.
-                */
-               while (ibufc == 0) {
-                       wwsetcursor(WCurRow(curwin->ww_win),
-                               WCurCol(curwin->ww_win));
-                       wwflush();
-                       imask = 1 << 0;
-                       while (wwforce(&imask) < 0)
-                               ;
-                       if ((imask & 1 << 0) == 0)
-                               continue;
-                       /* NOTE: ibufc == 0 */
-                       ibufp = ibuf;
-                       if ((ibufc = read(0, ibuf, sizeof ibuf)) < 0) {
-                               ibufc = 0;
-                               nreade++;
-                       } else if (ibufc == 0)
-                               nreadz++;
-                       else
-                               nreadc += ibufc;
-                       nread++;
-               }
-               /*
-                * Weird loop.  Copy the buffer to the pty stopping
-                * on the escape character in a hopefully efficient
-                * way.
-                * Probably a good thing to make ibufc == 1 a special
-                * case.
-                */
-               for (p = ibufp, n = ibufc;;) {
-                       if (--n < 0) {
-                               write(curwin->ww_pty, ibufp, ibufc);
-                               ibufp = ibuf;
-                               ibufc = 0;
-                               break;
-                       } else if (*p++ == escapec) {
-                               if ((n = p - ibufp) > 1)
-                                       write(curwin->ww_pty, ibufp, n - 1);
-                               ibufp = p;
-                               ibufc -= n;
-                               wwsetcurwin(cmdwin);
-                               break;
-                       }
-               }
-       }
+       if (selwin != 0)
+               setcmd(0);
+
+       mloop();
+
 bad:
        wwend();
        return 0;
 bad:
        wwend();
        return 0;
@@ -140,6 +154,13 @@ bad:
 
 usage()
 {
 
 usage()
 {
-       fprintf(stderr, "window: [-e escape] [-t]\n");
+       (void) fprintf(stderr, "Usage: window [-e escape-char] [-c command] [-t] [-f] [-d]\n");
+       exit(1);
+       return 0;                       /* for lint */
+}
+
+nomem()
+{
+       (void) fprintf(stderr, "Out of memory.\n");
        exit(1);
 }
        exit(1);
 }