new options to "window": shell, pty, frame
[unix-history] / usr / src / usr.bin / window / lcmd1.c
index 969bbee..1550dfd 100644 (file)
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)lcmd1.c     3.4 83/08/22";
+static char *sccsid = "@(#)lcmd1.c     3.22 84/04/08";
 #endif
 
 #include "defs.h"
 #endif
 
 #include "defs.h"
+#include "string.h"
+#include "value.h"
+#include "lcmd.h"
 
 
-struct ww *openwin();
-struct ww *idtowin();
+struct lcmd_arg arg_window[] = {
+       { "row",        1,      ARG_ANY },
+       { "column",     1,      ARG_ANY },
+       { "nrows",      2,      ARG_ANY },
+       { "ncols",      2,      ARG_ANY },
+       { "nlines",     2,      ARG_NUM },
+       { "label",      1,      ARG_STR },
+       { "shell",      1,      ARG_STR },
+       { "pty",        1,      ARG_ANY },
+       { "frame",      1,      ARG_ANY },
+       0
+};
 
 
-l_window()
+l_window(v, a)
+register struct value *v, *a;
 {
 {
-       register char **pp = argv;
-       register struct ww *w;
-       int col, row, ncol, nrow, id;
+       int col, row, ncol, nrow, id, nline;
+       char *label;
+       char haspty, hasframe;
+       char *shf, **sh;
+       char *argv[sizeof shell / sizeof *shell];
 
 
-       if ((id = findid()) < 0) {
-               error("Too many windows.");
+       if ((id = findid()) < 0)
                return;
                return;
+       row = a[0].v_type != V_NUM ? 1 : a[0].v_num;
+       col = a[1].v_type != V_NUM ? 0 : a[1].v_num;
+       nrow = a[2].v_type != V_NUM ? wwnrow - row : a[2].v_num;
+       ncol = a[3].v_type != V_NUM ? wwncol - col : a[3].v_num;
+       nline = a[4].v_type == V_ERR ? nbufline : a[4].v_num;
+       label =  a[5].v_type == V_ERR ? 0 : a[5].v_str;
+       if (a[6].v_type == V_STR) {
+               if (mkargv(a[6].v_str, argv, sizeof argv / sizeof *argv) < 0)
+                       return;
+               sh = argv;
+               shf = *argv;
+       } else {
+               sh = shell;
+               shf = shellfile;
        }
        }
-       if (**++pp == '*')
-               row = 1;
-       else
-               row = atoi(*pp);
-       if (**++pp == '*')
-               col = 0;
-       else
-               col = atoi(*pp);
-       if (**++pp == '*')
-               nrow = wwnrow - row;
-       else
-               nrow = atoi(*pp);
-       if (**++pp == '*')
-               ncol = wwncol - col;
-       else
-               ncol = atoi(*pp);
-       w = openwin(id, nrow, ncol, row, col);
-       if (w == 0)
-               error("Can't open window: row %d col %d, %d rows %d cols.",
-                       row, col, nrow, ncol);
+       if ((haspty = vtobool(a + 7, 1, -1)) < 0)
+               return;
+       if ((hasframe = vtobool(a + 8, 1, -1)) < 0)
+               return;
+       if (openwin(id, row, col, nrow, ncol, nline, label,
+                       haspty, hasframe, shf, sh) == 0)
+               return;
+       v->v_type = V_NUM;
+       v->v_num = id + 1;
+}
+
+struct lcmd_arg arg_nline[] = {
+       { "nlines",     1,      ARG_NUM },
+       0
+};
+
+l_nline(v, a)
+register struct value *v, *a;
+{
+       v->v_num = nbufline;
+       v->v_type = V_NUM;
+       if (a->v_type != V_ERR)
+               nbufline = a->v_num;
 }
 
 }
 
-l_select()
+struct lcmd_arg arg_select[] = {
+       { "window",     1,      ARG_NUM },
+       0
+};
+
+l_select(v, a)
+register struct value *v, *a;
 {
        struct ww *w;
 
 {
        struct ww *w;
 
-       if ((w = idtowin(*argv + 1)) == 0)
+       v->v_type = V_NUM;
+       v->v_num = selwin ? selwin->ww_id + 1 : -1;
+       if (a->v_type == V_ERR)
+               return;
+       if ((w = vtowin(a)) == 0)
                return;
        setselwin(w);
 }
 
                return;
        setselwin(w);
 }
 
-l_escape()
+struct lcmd_arg arg_debug[] = {
+       { "flag",       1,      ARG_ANY },
+       0
+};
+
+l_debug(v, a)
+register struct value *v, *a;
+{
+       v->v_type = V_NUM;
+       v->v_num = debug;
+       debug = vtobool(a, 1, debug);
+}
+
+struct lcmd_arg arg_escape[] = {
+       { "escapec",    1,      ARG_STR },
+       0
+};
+
+l_escape(v, a)
+register struct value *v, *a;
 {
 {
-       setescape(argv[1]);
+       char buf[2];
+
+       buf[0] = escapec;
+       buf[1] = 0;
+       if ((v->v_str = str_cpy(buf)) == 0) {
+               error("Out of memory.");
+               return;
+       }
+       v->v_type = V_STR;
+       if (a->v_type != V_ERR)
+               setescape(a->v_str);
 }
 
 }
 
-l_label()
+struct lcmd_arg arg_label[] = {
+       { "window",     1,      ARG_NUM },
+       { "label",      1,      ARG_STR },
+       0
+};
+
+/*ARGSUSED*/
+l_label(v, a)
+struct value *v;
+register struct value *a;
 {
        struct ww *w;
 
 {
        struct ww *w;
 
-       if ((w = idtowin(argv[1])) == 0)
+       if ((w = vtowin(a)) == 0)
                return;
                return;
-       if (setlabel(w, argv[2]) < 0)
+       if ((++a)->v_type != V_ERR && setlabel(w, a->v_str) < 0)
                error("Out of memory.");
        reframe();
 }
 
                error("Out of memory.");
        reframe();
 }
 
-l_terse()
+struct lcmd_arg arg_foreground[] = {
+       { "window",     1,      ARG_NUM },
+       { "flag",       1,      ARG_ANY },
+       0
+};
+
+l_foreground(v, a)
+register struct value *v, *a;
 {
 {
-       char oldterse = terse;
+       struct ww *w;
+       char flag;
 
 
-       if (argc < 2)
-               terse = 1;
-       else if (strcmp(argv[1], "off") == 0)
-               terse = 0;
-       else
-               terse = 1;
-       if (terse && !oldterse)
-               wwdelete(cmdwin);
-       else if (!terse && oldterse)
-               wwadd(cmdwin, &wwhead);
+       if ((w = vtowin(a)) == 0)
+               return;
+       v->v_type = V_NUM;
+       v->v_num = isfg(w);
+       flag = vtobool(++a, 1, v->v_num);
+       if (flag == v->v_num)
+               return;
+       deletewin(w);
+       addwin(w, flag);
        reframe();
 }
 
        reframe();
 }
 
-l_source()
+struct lcmd_arg arg_terse[] = {
+       { "flag",       1,      ARG_ANY },
+       0
+};
+
+l_terse(v, a)
+register struct value *v, *a;
 {
 {
-       if (insource) {
-               error("Recursive source.");
-               return;
-       }
-       if (dosource(argv[1]) < 0)
-               error("Can't open %s.", argv[1]);
+       v->v_type = V_NUM;
+       v->v_num = terse;
+       setterse(vtobool(a, 1, terse));
+}
+
+struct lcmd_arg arg_source[] = {
+       { "filename",   1,      ARG_STR },
+       0
+};
+
+l_source(v, a)
+register struct value *v, *a;
+{
+       v->v_type = V_NUM;
+       if (a->v_type != V_ERR && dosource(a->v_str) < 0) {
+               error("Can't open %s.", a->v_str);
+               v->v_num = -1;
+       } else
+               v->v_num = 0;
 }
 
 }
 
-l_write()
+/*ARGSUSED*/
+l_write(v, a)
+struct value *v;
+register struct value *a;
 {
 {
+       char buf[20];
        struct ww *w;
 
        struct ww *w;
 
-       if ((w = idtowin(argv[1])) == 0)
+       if ((w = vtowin(a++)) == 0)
                return;
                return;
-       (void) write(w->ww_pty, argv[2], strlen(argv[2]));
+       while (a->v_type != V_ERR) {
+               if (a->v_type == V_NUM) {
+                       (void) sprintf(buf, "%d", a->v_num);
+                       (void) write(w->ww_pty, buf, strlen(buf));
+               } else
+                       (void) write(w->ww_pty, a->v_str, strlen(a->v_str));
+               if ((++a)->v_type != V_ERR)
+                       (void) write(w->ww_pty, " ", 1);
+       }
 }
 
 }
 
-l_close()
+/*ARGSUSED*/
+l_close(v, a)
+struct value *v;
+register struct value *a;
 {
 {
-       register i;
-       register struct ww *w;
-       char didit = 0;
+       struct ww *w;
 
 
-       if (argc < 2) {
+       if (a->v_type == V_STR && str_match(a->v_str, "all", 1))
                c_close((struct ww *)0);
                c_close((struct ww *)0);
+       else
+               for (; a->v_type != V_ERR; a++)
+                       if ((w = vtowin(a)) != 0)
+                               c_close(w);
+}
+
+struct lcmd_arg arg_cursormodes[] = {
+       { "modes",      1,      ARG_NUM },
+       0
+};
+
+l_cursormodes(v, a)
+register struct value *v, *a;
+{
+
+       v->v_type = V_NUM;
+       v->v_num = wwcursormodes;
+       if (a->v_type != V_ERR)
+               wwsetcursormodes(a->v_num);
+}
+
+struct lcmd_arg arg_unset[] = {
+       { "variable",   1,      ARG_ANY },
+       0
+};
+
+l_unset(v, a)
+register struct value *v, *a;
+{
+       v->v_type = V_NUM;
+       switch (a->v_type) {
+       case V_ERR:
+               v->v_num = -1;
                return;
                return;
+       case V_NUM:
+               if ((a->v_str = str_itoa(a->v_num)) == 0) {
+                       error("Out of memory.");
+                       v->v_num = -1;
+                       return;
+               }
+               a->v_type = V_STR;
+               break;
        }
        }
-       for (i = 1; i < argc; i++) {
-               if ((w = idtowin(argv[i])) == 0)
-                       continue;
-               closewin(w);
-               didit++;
-       }
-       if (selwin == 0) {
-               for (i = 0; i < NWINDOW && window[i] != 0; i++)
-                       ;
-               if (i < NWINDOW)
-                       setselwin(window[i]);
-       }
-       if (didit)
-               reframe();
+       v->v_num = var_unset(a->v_str);
 }
 
 struct ww *
 }
 
 struct ww *
-idtowin(idstr)
-char *idstr;
+vtowin(v)
+register struct value *v;
 {
 {
-       register id;
        struct ww *w;
 
        struct ww *w;
 
-       id = atoi(idstr) - 1;
-       if (id < 0 || id >= NWINDOW || (w = window[id]) == 0) {
-               error("%d: No such window.", id + 1);
+       switch (v->v_type) {
+       case V_ERR:
+               error("No window specified.");
+               return 0;
+       case V_STR:
+               error("%s: No such window.", v->v_str);
+               return 0;
+       }
+       if (v->v_num < 1 || v->v_num > NWINDOW
+           || (w = window[v->v_num - 1]) == 0) {
+               error("%d: No such window.", v->v_num);
                return 0;
        }
        return w;
 }
                return 0;
        }
        return w;
 }
+
+vtobool(v, def, err)
+register struct value *v;
+char def, err;
+{
+       switch (v->v_type) {
+       case V_NUM:
+               return v->v_num != 0;
+       case V_STR:
+               if (str_match(v->v_str, "true", 1)
+                   || str_match(v->v_str, "on", 2)
+                   || str_match(v->v_str, "yes", 1))
+                       return 1;
+               else if (str_match(v->v_str, "false", 1)
+                   || str_match(v->v_str, "off", 2)
+                   || str_match(v->v_str, "no", 1))
+                       return 0;
+               else {
+                       error("%s: Illegal boolean value.", v->v_str);
+                       return err;
+               }
+               /*NOTREACHED*/
+       case V_ERR:
+               return def;
+       }
+       /*NOTREACHED*/
+}
+
+mkargv(p, argv, n)
+register char *p;
+register char **argv;
+register n;
+{
+       while (--n > 0) {
+               for (; *p && (*p == ' ' || *p == '\t'); p++)
+                       ;
+               if (!*p)
+                       break;
+               *argv++ = p;
+               for (; *p && *p != ' ' && *p != '\t'; p++)
+                       ;
+               if (*p)
+                       *p++ = 0;
+       }
+       if (n == 0) {
+               error("Too many shell arguments.");
+               return -1;
+       }
+       *argv = 0;
+       return 0;
+}