new commands s and S. also better way to stop window output
[unix-history] / usr / src / usr.bin / window / cmd.c
index fca374e..90adf2c 100644 (file)
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)cmd.c       1.1 83/07/18";
+static char sccsid[] = "@(#)cmd.c      3.31 %G%";
 #endif
 
 #include "defs.h"
 #endif
 
 #include "defs.h"
-
-struct ww *getwin();
-struct ww *openwin();
-char *strtime();
+#include "char.h"
 
 docmd()
 {
        register char c;
        register struct ww *w;
 
 docmd()
 {
        register char c;
        register struct ww *w;
-       char buf;
+       char out = 0;
 
 
-top:
-       Wunhide(cmdwin->ww_win);
-       while ((c = bgetc()) >= 0) {
-               wwputs("\r\n", cmdwin);
-               switch (c) {
-               case 'r':
-               case 'R':
-               case CTRL([):
-               case ESCAPE:
-                       if (selwin == 0) {
-                               wwputs("No window.  ", cmdwin);
-                               continue;
-                       }
-               }
-               switch (c) {
-               case '1': case '2': case '3': case '4': case '5':
-               case '6': case '7': case '8': case '9':
-                       if ((w = wwfind(c - '0')) == 0) {
-                               Ding();
+       for (;;) {
+               while ((c = wwgetc()) >= 0) {
+                       if (!terse)
+                               wwputc('\n', cmdwin);
+                       switch (c) {
+                       default:
+                               if (c == escapec)
+                                       goto foo;
                                break;
                                break;
+                       case 'h': case 'j': case 'k': case 'l':
+                       case ctrl(y):
+                       case ctrl(e):
+                       case ctrl(u):
+                       case ctrl(d):
+                       case ctrl(b):
+                       case ctrl(f):
+                       case ctrl(s):
+                       case ctrl(q):
+                       case ctrl([):
+                       foo:
+                               if (selwin == 0) {
+                                       error("No window.");
+                                       continue;
+                               }
                        }
                        }
-                       setselwin(w);
-                       goto out;
-               case '%':
-                       if ((w = getwin()) == 0)
+                       switch (c) {
+                       case '1': case '2': case '3': case '4': case '5':
+                       case '6': case '7': case '8': case '9':
+                               if ((w = window[c - '1']) == 0) {
+                                       error("%c: No such window.", c);
+                                       break;
+                               }
+                               setselwin(w);
+                               if (checkproc(selwin) >= 0)
+                                        out = 1;
                                break;
                                break;
-                       setselwin(w);
-                       break;
-               case 'c':
-               case 'C':
-               case 'Z':
-                       doclose(c);
-                       break;
-               case 'w':
-                       dowindow();
-                       break;
-               case 'r':
-                       selwin->ww_refresh = 0;
-                       break;
-               case 'R':
-                       selwin->ww_refresh = 1;
-                       break;
-               case 't':
-                       dotime(RUSAGE_SELF);
-                       break;
-               case 'T':
-                       dotime(RUSAGE_CHILDREN);
-                       break;
-               case CTRL(l):
-                       ScreenGarbaged = 1;
-                       break;
-               case '?':
-                       dohelp();
-                       break;
-               case ESCAPE:
-                       buf = ESCAPE;
-                       write(selwin->ww_pty, &buf, 1);
-               case CTRL([):
-                       goto out;
-               case CTRL(z):
-                       wwsuspend();
-                       break;
-               case '.':
-                       quit++;
-                       goto out;
-               default:
-                       Ding();
-                       wwputs("Type ? for help.  ", cmdwin);
+                       case '%':
+                               if ((w = getwin()) != 0)
+                                       setselwin(w);
+                               break;
+                       case ctrl(^):
+                               if (lastselwin != 0) {
+                                       setselwin(lastselwin);
+                                       if (checkproc(selwin) >= 0)
+                                               out = 1;
+                               } else
+                                       error("No previous window.");
+                               break;
+                       case 'c':
+                               if ((w = getwin()) != 0)
+                                       c_close(w);
+                               break;
+                       case 'w':
+                               c_window();
+                               break;
+                       case 'm':
+                               if ((w = getwin()) != 0)
+                                       c_move(w);
+                               break;
+                       case 'M':
+                               if ((w = getwin()) != 0)
+                                       movewin(w, w->ww_alt.t, w->ww_alt.l);
+                               break;
+                       case 's':
+                               if ((w = getwin()) != 0)
+                                       c_size(w);
+                               break;
+                       case 'S':
+                               if ((w = getwin()) != 0)
+                                       sizewin(w, w->ww_alt.nr, w->ww_alt.nc);
+                               break;
+                       case ':':
+                               c_colon();
+                               break;
+                       case 'h':
+                               (void) wwwrite(selwin, "\b", 1);
+                               break;
+                       case 'j':
+                               (void) wwwrite(selwin, "\n", 1);
+                               break;
+                       case 'k':
+                               (void) wwwrite(selwin, "\033A", 2);
+                               break;
+                       case 'l':
+                               (void) wwwrite(selwin, "\033C", 2);
+                               break;
+                       case ctrl(e):
+                               wwscroll(selwin, 1);
+                               break;
+                       case ctrl(y):
+                               wwscroll(selwin, -1);
+                               break;
+                       case ctrl(d):
+                               wwscroll(selwin, selwin->ww_w.nr / 2);
+                               break;
+                       case ctrl(u):
+                               wwscroll(selwin, - selwin->ww_w.nr / 2);
+                               break;
+                       case ctrl(f):
+                               wwscroll(selwin, selwin->ww_w.nr);
+                               break;
+                       case ctrl(b):
+                               wwscroll(selwin, - selwin->ww_w.nr);
+                               break;
+                       case ctrl(s):
+                               if (selwin->ww_pty < 0)
+                                       break;
+                               if (selwin->ww_ispty) {
+                                       (void) ioctl(selwin->ww_pty, TIOCSTOP,
+                                               (char *)0);
+                                       selwin->ww_stopped = 1;
+                               } else
+                                       (void) write(selwin->ww_pty,
+                                               &wwwintty.ww_tchars.t_stopc, 1);
+                               break;
+                       case ctrl(q):
+                               if (selwin->ww_pty < 0)
+                                       break;
+                               if (selwin->ww_ispty) {
+                                       (void) ioctl(selwin->ww_pty, TIOCSTART,
+                                               (char *)0);
+                                       selwin->ww_stopped = 0;
+                               } else
+                                       (void) write(selwin->ww_pty,
+                                               &wwwintty.ww_tchars.t_startc,
+                                               1);
+                               break;
+                       case ctrl(l):
+                               wwredraw();
+                               break;
+                       case '?':
+                               c_help();
+                               break;
+                       case ctrl([):
+                               if (checkproc(selwin) >= 0)
+                                       out = 1;
+                               break;
+                       case ctrl(z):
+                               wwsuspend();
+                               break;
+                       case 'q':
+                               c_quit();
+                               break;
+                       /* debugging stuff */
+                       case '&':
+                               if (debug) {
+                                       c_debug();
+                                       break;
+                               }
+                       default:
+                               if (c == escapec) {
+                                       if (checkproc(selwin) >= 0) {
+                                               (void) write(selwin->ww_pty,
+                                                       &escapec, 1);
+                                               out = 1;
+                                       }
+                               } else {
+                                       if (!terse)
+                                               wwbell();
+                                       error("Type ? for help.");
+                               }
+                       }
+               }
+               if (out || quit)
                        break;
                        break;
+               if (terse)
+                       wwsetcursor(0, 0);
+               else {
+                       wwputs("Command: ", cmdwin);
+                       wwcurtowin(cmdwin);
                }
                }
+               while (wwpeekc() < 0)
+                       wwiomux();
        }
        }
-       wwputs("Command: ", cmdwin);
-       wwsetcursor(WCurRow(cmdwin->ww_win), WCurCol(cmdwin->ww_win));
-       while (bpeekc() < 0) {
-               wwflush();
-               bread();
-       }
-       goto top;
-out:
        if (!quit)
        if (!quit)
-               wwsetcurrent(selwin);
-       Whide(cmdwin->ww_win);
+               setcmd(0);
 }
 
 struct ww *
 getwin()
 {
        register int c;
 }
 
 struct ww *
 getwin()
 {
        register int c;
-       struct ww *w;
+       struct ww *w = 0;
 
 
-       while ((c = bgetc()) < 0) {
-               wwflush();
-               bread();
-       }
-       if (c < '1' || c > '9') {
-               Ding();
-               return 0;
-       }
-       if ((w = wwfind(c - '0')) == 0)
-               Ding();
+       if (!terse)
+               wwputs("Which window? ", cmdwin);
+       wwcurtowin(cmdwin);
+       while ((c = wwgetc()) < 0)
+               wwiomux();
+       if (debug && c == 'c')
+               w = cmdwin;
+       else if (debug && c == 'f')
+               w = framewin;
+       else if (debug && c == 'b')
+               w = boxwin;
+       else if (c >= '1' && c < NWINDOW + '1')
+               w = window[c - '1'];
+       if (w == 0)
+               wwbell();
+       if (!terse)
+               wwputc('\n', cmdwin);
        return w;
 }
 
        return w;
 }
 
-setselwin(w)
-register struct ww *w;
+checkproc(w)
+struct ww *w;
 {
 {
-       if (selwin)
-               labelwin(selwin, 0);
-       selwin = w;
-       if (w) {
-               labelwin(w, WINVERSE);
-               /* bring it to the top just below cmdwin */
-               wwsetcurrent(w);
-               wwsetcurrent(cmdwin);
+       if (w->ww_state != WWS_HASPROC) {
+               error("No process in window.");
+               return -1;
        }
        }
+       return 0;
 }
 
 }
 
-labelwin(w, mode)
-register struct ww *w;
+setcmd(new)
+char new;
 {
 {
-       char buf[2];
+       if (new && !incmd) {
+               if (!terse)
+                       wwadd(cmdwin, &wwhead);
+               if (selwin != 0)
+                       wwcursor(selwin, 1);
+               wwcurwin = 0;
+       } else if (!new && incmd) {
+               if (!terse) {
+                       wwdelete(cmdwin);
+                       reframe();
+               }
+               if (selwin != 0)
+                       wwcursor(selwin, 0);
+               wwcurwin = selwin;
+       }
+       incmd = new;
+}
+
+setterse(new)
+char new;
+{
+       if (incmd)
+               if (new && !terse) {
+                       wwdelete(cmdwin);
+                       reframe();
+               } else if (!new && terse)
+                       wwadd(cmdwin, &wwhead);
+       terse = new;
+}
 
 
-       buf[0] = w->ww_ident + '0';
-       buf[1] = 0;
-       wwlabel(w, buf, mode);
+/*
+ * Set the current window.
+ */
+setselwin(w)
+struct ww *w;
+{
+       if (selwin == w)
+               return;
+       if (selwin != 0)
+               lastselwin = selwin;
+       if ((selwin = w) != 0)
+               front(selwin, 1);
 }
 }