BSD 4_4_Lite1 release
[unix-history] / usr / src / usr.bin / window / main.c
index 7943103..15de840 100644 (file)
+/*
+ * Copyright (c) 1983, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Edward Wang at The University of California, Berkeley.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)main.c      1.2 83/07/17";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1983, 1993\n\
+       The Regents of the University of California.  All rights reserved.\n";
+#endif /* not lint */
 
 
-#include "ww.h"
-#include <signal.h>
-#include <sys/time.h>
-#include <sys/resource.h>
+#ifndef lint
+static char sccsid[] = "@(#)main.c     8.2 (Berkeley) 4/2/94";
+#endif /* not lint */
 
 
-#define ESCAPE CTRL(p)
+#include "defs.h"
+#include <paths.h>
+#include <stdio.h>
+#include "string.h"
+#include "char.h"
+#include "local.h"
 
 
-int nread = 0;
-struct timeval starttime;
-struct timezone timezone;
+#define next(a) (*++*(a) ? *(a) : (*++(a) ? *(a) : (char *)usage()))
 
 
-main()
+/*ARGSUSED*/
+main(argc, argv)
+char **argv;
 {
 {
-       register n;
        register char *p;
        register char *p;
-       struct ww *w1, *w2;
-       int wwchild();
-       int imask;
-       char buf[512];
-       char escape = 0;
-       char c;
-       struct rusage rusage;
+       char fflag = 0;
+       char dflag = 0;
+       char xflag = 0;
+       char *cmd = 0;
+       char tflag = 0;
 
 
-       if (wwinit() < 0) {
-               fflush(stdout);
-               fprintf("Can't do windows on this terminal.\n");
-               exit(1);
-       }
-       n = WRows / 2;
-       if ((w1 = wwopen(1, n, WCols, 0, 0)) == 0
-           || (w2 = wwopen(2, WRows - n, WCols, n, 0)) == 0) {
-               fflush(stdout);
-               fprintf(stderr, "Can't open windows.\n");
-               goto bad;
-       }
-       wwsetcurrent(w1);
-       (void) signal(SIGCHLD, wwchild);
-       switch (wwfork(w1)) {
-       case -1:
-               perror("wwfork");
-               goto bad;
-       case 0:
-               execl("/bin/csh", "csh", 0);
-               perror("execl(/bin/csh)");
-               exit(1);
-       }
-       switch (wwfork(w2)) {
-       case -1:
-               perror("wwfork");
-               goto bad;
-       case 0:
-               execl("/bin/csh", "csh", 0);
-               perror("execl(/bin/csh)");
-               exit(1);
-       }
-       gettimeofday(&starttime, &timezone);
-       wwputstr("Type ^P? for help.\r\n");
-       for (;;) {
-               wwflush();
-               if (!wwhaschildren())
-                       break;
-               while (imask = 1<<0, wwforce(&imask) < 0)
-                       ;
-               if ((imask & 1<<0) == 0)
-                       continue;
-               n = read(0, buf, sizeof buf);
-               for (p = buf; n-- > 0; p++) {
-                       nread++;
-                       *p &= 0x7f;
-                       if (!escape) {
-                               if (*p == ESCAPE)
-                                       escape++;
-                               else
-                                       write(curwin->ww_pty, p, 1);
-                               continue;
-                       }
-                       escape = 0;
-                       switch (*p) {
-                       case ESCAPE:
-                               write(curwin->ww_pty, p, 1);
-                               break;
-                       case '1':
-                               wwsetcurrent(w1);
-                               break;
-                       case '2':
-                               wwsetcurrent(w2);
-                               break;
-                       case CTRL(z):
-                               wwsuspend();
-                               break;
-                       case '.':
-                               goto out;
-                       /*
-                       case CTRL(\\):
-                               wwend();
-                               kill(getpid(), SIGILL);
-                               break;
-                       */
-                       case CTRL(l):
-                               ScreenGarbaged = 1;
+       escapec = ESCAPEC;      
+       if (p = rindex(*argv, '/'))
+               p++;
+       else
+               p = *argv;
+       debug = strcmp(p, "a.out") == 0;
+       while (*++argv) {
+               if (**argv == '-') {
+                       switch (*++*argv) {
+                       case 'f':
+                               fflag++;
                                break;
                                break;
-                       case 'r':
-                               curwin->ww_refresh = 0;
+                       case 'c':
+                               if (cmd != 0) {
+                                       (void) fprintf(stderr,
+                                               "Only one -c allowed.\n");
+                                       (void) usage();
+                               }
+                               cmd = next(argv);
                                break;
                                break;
-                       case 'R':
-                               curwin->ww_refresh = 1;
-                               break;
-                       case 's':
-                               wwprintf(curwin, "\r\nread: %d, write: %d\r\n",
-                                       nread, wwnwrite);
+                       case 'e':
+                               setescape(next(argv));
                                break;
                        case 't':
                                break;
                        case 't':
-                               printrusage(curwin, RUSAGE_SELF);
+                               tflag++;
+                               break;
+                       case 'd':
+                               dflag++;
                                break;
                                break;
-                       case 'T':
-                               printrusage(curwin, RUSAGE_CHILDREN);
+                       case 'D':
+                               debug = !debug;
                                break;
                                break;
-                       case '?':
-                               help();
+                       case 'x':
+                               xflag++;
                                break;
                        default:
                                break;
                        default:
-                               c = ESCAPE;
-                               write(curwin->ww_pty, &c, 1);
-                               write(curwin->ww_pty, p, 1);
+                               (void) usage();
                        }
                        }
-               }
+               } else
+                       (void) usage();
+       }
+       if ((p = getenv("SHELL")) == 0)
+               p = _PATH_BSHELL;
+       if ((default_shellfile = str_cpy(p)) == 0) {
+               (void) fprintf(stderr, "Out of memory.\n");
+               exit(1);
+       }
+       if (p = rindex(default_shellfile, '/'))
+               p++;
+       else
+               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) {
+               (void) fprintf(stderr, "%s.\n", wwerror());
+               exit(1);
        }
        }
-out:
-bad:
-       wwend();
-       return 0;
-}
-
-help()
-{
-       wwputstr("\r\n");
-       wwputstr("^P1   goto top window\r\n");
-       wwputstr("^P2   goto bottom window\r\n");
-       wwputstr("^PR   force refresh after every newline\r\n");
-       wwputstr("^Pr   don't refresh every line\r\n");
-       wwputstr("^Ps   print io statistics\r\n");
-       wwputstr("^Pt   print resource usage of this program\r\n");
-       wwputstr("^PT   print resource usage of children\r\n");
-       wwputstr("^P^L  redraw screen\r\n");
-       wwputstr("^P^Z  suspend\r\n");
-       wwputstr("^P.   quit\r\n");
-}
 
 
-printrusage(w, flag)
-register struct ww *w;
-{
-       struct rusage rusage;
-       struct timeval timeval;
-       char *strtime();
+#ifdef OLD_TTY
+       if (debug)
+               wwnewtty.ww_tchars.t_quitc = wwoldtty.ww_tchars.t_quitc;
+       if (xflag) {
+               wwnewtty.ww_tchars.t_stopc = wwoldtty.ww_tchars.t_stopc;
+               wwnewtty.ww_tchars.t_startc = wwoldtty.ww_tchars.t_startc;
+       }
+#else
+       if (debug) {
+               wwnewtty.ww_termios.c_cc[VQUIT] =
+                       wwoldtty.ww_termios.c_cc[VQUIT];
+               wwnewtty.ww_termios.c_lflag |= ISIG;
+       }
+       if (xflag) {
+               wwnewtty.ww_termios.c_cc[VSTOP] =
+                       wwoldtty.ww_termios.c_cc[VSTOP];
+               wwnewtty.ww_termios.c_cc[VSTART] =
+                       wwoldtty.ww_termios.c_cc[VSTART];
+               wwnewtty.ww_termios.c_iflag |= IXON;
+       }
+#endif
+       if (debug || xflag)
+               (void) wwsettty(0, &wwnewtty);
 
 
-       gettimeofday(&timeval, &timezone);
-       timeval.tv_sec -= starttime.tv_sec;
-       if ((timeval.tv_usec -= starttime.tv_usec) < 0) {
-               timeval.tv_sec--;
-               timeval.tv_usec += 1000000;
+       if ((cmdwin = wwopen(wwbaud > 2400 ? WWO_REVERSE : 0, 1, wwncol,
+                            0, 0, 0)) == 0) {
+               wwflush();
+               (void) fprintf(stderr, "%s.\r\n", wwerror());
+               goto bad;
        }
        }
-       getrusage(flag, &rusage);
+       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) {
+               wwflush();
+               (void) fprintf(stderr, "%s.\r\n", wwerror());
+               goto bad;
+       }
+       wwadd(framewin, &wwhead);
+       if ((boxwin = wwopen(WWO_GLASS, wwnrow, wwncol, 0, 0, 0)) == 0) {
+               wwflush();
+               (void) fprintf(stderr, "%s.\r\n", wwerror());
+               goto bad;
+       }
+       fgwin = framewin;
+
+       wwupdate();
+       wwflush();
+       setvars();
+
+       setterse(tflag);
+       setcmd(1);
+       if (cmd != 0)
+               (void) dolongcmd(cmd, (struct value *)0, 0);
+       if (!fflag)
+               if (dflag || doconfig() < 0)
+                       dodefault();
+       if (selwin != 0)
+               setcmd(0);
 
 
-       wwprintf(w, "\r\ntime\t\tutime\tstime\tmaxrss\tixrss\tidrss\tisrss\r\n");
-       wwprintf(w, "%-16s", strtime(&timeval));
-       wwprintf(w, "%-8s", strtime(&rusage.ru_utime));
-       wwprintf(w, "%-8s", strtime(&rusage.ru_stime));
-       wwprintf(w, "%D\t%D\t%D\t%D\r\n",
-               rusage.ru_maxrss, rusage.ru_ixrss,
-               rusage.ru_idrss, rusage.ru_isrss);
-       wwprintf(w, "minflt\tmajflt\tnswap\tinblk\toublk\tmsgsnd\tmsgrcv\tnsigs\tnvcsw\tnivcsw\r\n");
-       wwprintf(w, "%D\%D\t%D\t%D\t%D\t%D\t%D\t%D\t%D\t%D\t%D\r\n",
-               rusage.ru_minflt, rusage.ru_majflt, rusage.ru_nswap,
-               rusage.ru_inblock, rusage.ru_oublock,
-               rusage.ru_msgsnd, rusage.ru_msgrcv, rusage.ru_nsignals,
-               rusage.ru_nvcsw, rusage.ru_nivcsw);
+       mloop();
+
+bad:
+       wwend(1);
+       return 0;
 }
 
 }
 
-char *
-strtime(t)
-register struct timeval *t;
+usage()
 {
 {
-       char fill = 0;
-       static char buf[20];
-       register char *p = buf;
-
-       if (t->tv_sec > 60*60) {
-               sprintf(p, "%D:", t->tv_sec / 60*60);
-               while (*p++)
-                       ;
-               p--;
-               t->tv_sec %= 60*60;
-               fill++;
-       }
-       if (t->tv_sec > 60) {
-               sprintf(p, fill ? "%02D:" : "%D:", t->tv_sec / 60, ':');
-               while (*p++)
-                       ;
-               p--;
-               t->tv_sec %= 60;
-               fill++;
-       }
-       sprintf(p, fill ? "%02D.%02d" : "%D.%02D",
-               t->tv_sec, t->tv_usec / 10000);
-       return buf;
+       (void) fprintf(stderr, "Usage: window [-e escape-char] [-c command] [-t] [-f] [-d]\n");
+       exit(1);
+       return 0;                       /* for lint */
 }
 }