break out special local mail processing (e.g., mapping to the
[unix-history] / usr / src / usr.bin / window / win.c
index 6e3585f..18cbaaa 100644 (file)
@@ -1,8 +1,19 @@
+/*
+ * 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.
+ *
+ * %sccs.include.redist.c%
+ */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)win.c       3.5 84/04/05";
-#endif
+static char sccsid[] = "@(#)win.c      8.1 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "defs.h"
 
 #include "defs.h"
+#include "char.h"
 
 /*
  * Higher level routines for dealing with windows.
 
 /*
  * Higher level routines for dealing with windows.
@@ -24,8 +35,10 @@ static       char *sccsid = "@(#)win.c       3.5 84/04/05";
  * Open a user window.
  */
 struct ww *
  * Open a user window.
  */
 struct ww *
-openwin(id, row, col, nrow, ncol, nline, label)
+openwin(id, row, col, nrow, ncol, nline, label, haspty, hasframe, shf, sh)
 char *label;
 char *label;
+char haspty, hasframe;
+char *shf, **sh;
 {
        register struct ww *w;
 
 {
        register struct ww *w;
 
@@ -36,26 +49,29 @@ char *label;
                error("Illegal window position.");
                return 0;
        }
                error("Illegal window position.");
                return 0;
        }
-       if ((w = wwopen(WWO_PTY, nrow, ncol, row, col, nline)) == 0) {
-               error("%s.", wwerror());
+       w = wwopen(haspty ? WWO_PTY : WWO_SOCKET, nrow, ncol, row, col, nline);
+       if (w == 0) {
+               error("Can't open window: %s.", wwerror());
                return 0;
        }
        w->ww_id = id;
        window[id] = w;
                return 0;
        }
        w->ww_id = id;
        window[id] = w;
-       w->ww_hasframe = 1;
-       w->ww_altpos.r = 1;
-       w->ww_altpos.c = 0;
+       w->ww_hasframe = hasframe;
+       w->ww_alt = w->ww_w;
        if (label != 0 && setlabel(w, label) < 0)
                error("No memory for label.");
        wwcursor(w, 1);
        if (label != 0 && setlabel(w, label) < 0)
                error("No memory for label.");
        wwcursor(w, 1);
+       /*
+        * We have to do this little maneuver to make sure
+        * addwin() puts w at the top, so we don't waste an
+        * insert and delete operation.
+        */
+       setselwin((struct ww *)0);
        addwin(w, 0);
        addwin(w, 0);
-       selwin = w;
-       reframe();
-       wwupdate();
-       wwflush();
-       if (wwspawn(w, shell, shellname, (char *)0) < 0) {
-               c_close(w);
-               error("%s: %s.", shell, wwerror());
+       setselwin(w);
+       if (wwspawn(w, shf, sh) < 0) {
+               error("Can't execute %s: %s.", shf, wwerror());
+               closewin(w);
                return 0;
        }
        return w;
                return 0;
        }
        return w;
@@ -74,23 +90,51 @@ findid()
        return i;
 }
 
        return i;
 }
 
+struct ww *
+findselwin()
+{
+       register struct ww *w, *s = 0;
+       register i;
+
+       for (i = 0; i < NWINDOW; i++)
+               if ((w = window[i]) != 0 && w != selwin &&
+                   (s == 0 ||
+                    !isfg(w) && (w->ww_order < s->ww_order || isfg(s))))
+                       s = w;
+       return s;
+}
+
 /*
 /*
- * Close a user window.
- * May leave selwin == 0.
+ * Close a user window.  Close all if w == 0.
  */
 closewin(w)
 register struct ww *w;
 {
  */
 closewin(w)
 register struct ww *w;
 {
-       if (w == selwin)
-               selwin = 0;
-       if (w == lastselwin)
-               lastselwin = 0;
-       if (w->ww_id >= 0 && w->ww_id < NWINDOW)
-               window[w->ww_id] = 0;
-       if (w->ww_label)
-               str_free(w->ww_label);
-       deletewin(w);
-       wwclose(w);
+       char didit = 0;
+       register i;
+
+       if (w != 0) {
+               closewin1(w);
+               didit++;
+       } else
+               for (i = 0; i < NWINDOW; i++) {
+                       if ((w = window[i]) == 0)
+                               continue;
+                       closewin1(w);
+                       didit++;
+               }
+       if (didit) {
+               if (selwin == 0)
+                       if (lastselwin != 0) {
+                               setselwin(lastselwin);
+                               lastselwin = 0;
+                       } else if (w = findselwin())
+                               setselwin(w);
+               if (lastselwin == 0 && selwin)
+                       if (w = findselwin())
+                               lastselwin = w;
+               reframe();
+       }
 }
 
 /*
 }
 
 /*
@@ -107,12 +151,13 @@ char *label;
        w->ww_mapnl = 1;
        w->ww_hasframe = 1;
        w->ww_nointr = 1;
        w->ww_mapnl = 1;
        w->ww_hasframe = 1;
        w->ww_nointr = 1;
+       w->ww_noupdate = 1;
+       w->ww_unctrl = 1;
        w->ww_id = -1;
        w->ww_center = 1;
        (void) setlabel(w, label);
        addwin(w, 1);
        reframe();
        w->ww_id = -1;
        w->ww_center = 1;
        (void) setlabel(w, label);
        addwin(w, 1);
        reframe();
-       wwupdate();
        return w;
 }
 
        return w;
 }
 
@@ -122,10 +167,25 @@ char *label;
 closeiwin(w)
 struct ww *w;
 {
 closeiwin(w)
 struct ww *w;
 {
-       closewin(w);
+       closewin1(w);
        reframe();
 }
 
        reframe();
 }
 
+closewin1(w)
+register struct ww *w;
+{
+       if (w == selwin)
+               selwin = 0;
+       if (w == lastselwin)
+               lastselwin = 0;
+       if (w->ww_id >= 0 && w->ww_id < NWINDOW)
+               window[w->ww_id] = 0;
+       if (w->ww_label)
+               str_free(w->ww_label);
+       deletewin(w);
+       wwclose(w);
+}
+
 /*
  * Move the window to the top of its group.
  * Don't do it if already fully visible.
 /*
  * Move the window to the top of its group.
  * Don't do it if already fully visible.
@@ -148,6 +208,7 @@ char doreframe;
 
 /*
  * Add a window at the top of normal windows or foreground windows.
 
 /*
  * Add a window at the top of normal windows or foreground windows.
+ * For normal windows, we put it behind the current window.
  */
 addwin(w, fg)
 register struct ww *w;
  */
 addwin(w, fg)
 register struct ww *w;
@@ -158,7 +219,8 @@ char fg;
                if (fgwin == framewin)
                        fgwin = w;
        } else
                if (fgwin == framewin)
                        fgwin = w;
        } else
-               wwadd(w, fgwin);
+               wwadd(w, selwin != 0 && selwin != w && !isfg(selwin)
+                               ? selwin : fgwin);
 }
 
 /*
 }
 
 /*
@@ -210,6 +272,38 @@ register struct ww *w;
        }
 }
 
        }
 }
 
+stopwin(w)
+       register struct ww *w;
+{
+       if (w->ww_pty >= 0 && w->ww_ispty && wwstoptty(w->ww_pty) < 0)
+               error("Can't stop output: %s.", wwerror());
+       else
+               w->ww_stopped = 1;
+}
+
+startwin(w)
+       register struct ww *w;
+{
+       if (w->ww_pty >= 0 && w->ww_ispty && wwstarttty(w->ww_pty) < 0)
+               error("Can't start output: %s.", wwerror());
+       else
+               w->ww_stopped = 0;
+}
+
+sizewin(w, nrow, ncol)
+register struct ww *w;
+{
+       struct ww *back = w->ww_back;
+
+       w->ww_alt.nr = w->ww_w.nr;
+       w->ww_alt.nc = w->ww_w.nc;
+       wwdelete(w);
+       if (wwsize(w, nrow, ncol) < 0)
+               error("Can't resize window: %s.", wwerror());
+       wwadd(w, back);
+       reframe();
+}
+
 waitnl(w)
 struct ww *w;
 {
 waitnl(w)
 struct ww *w;
 {
@@ -221,23 +315,30 @@ register struct ww *w;
 char always;
 {
        int c;
 char always;
 {
        int c;
+       char uc = w->ww_unctrl;
 
        if (!always && w->ww_cur.r < w->ww_w.b - 2)
                return 0;
        c = waitnl1(w, "[Type escape to abort, any other key to continue]");
 
        if (!always && w->ww_cur.r < w->ww_w.b - 2)
                return 0;
        c = waitnl1(w, "[Type escape to abort, any other key to continue]");
+       w->ww_unctrl = 0;
        wwputs("\033E", w);
        wwputs("\033E", w);
-       return c == CTRL([) ? 2 : 1;
+       w->ww_unctrl = uc;
+       return c == ctrl('[') ? 2 : 1;
 }
 
 waitnl1(w, prompt)
 register struct ww *w;
 char *prompt;
 {
 }
 
 waitnl1(w, prompt)
 register struct ww *w;
 char *prompt;
 {
+       char uc = w->ww_unctrl;
+
+       w->ww_unctrl = 0;
        front(w, 0);
        front(w, 0);
-       wwprintf(w, "\033Y%c%c\033p%s\033q ",
+       wwprintf(w, "\033Y%c%c\033sA%s\033rA ",
                w->ww_w.nr - 1 + ' ', ' ', prompt);     /* print on last line */
        wwcurtowin(w);
        while (wwpeekc() < 0)
                wwiomux();
                w->ww_w.nr - 1 + ' ', ' ', prompt);     /* print on last line */
        wwcurtowin(w);
        while (wwpeekc() < 0)
                wwiomux();
+       w->ww_unctrl = uc;
        return wwgetc();
 }
        return wwgetc();
 }