new parser
authorEdward Wang <edward@ucbvax.Berkeley.EDU>
Wed, 23 Nov 1983 11:57:48 +0000 (03:57 -0800)
committerEdward Wang <edward@ucbvax.Berkeley.EDU>
Wed, 23 Nov 1983 11:57:48 +0000 (03:57 -0800)
SCCS-vsn: usr.bin/window/defs.h 3.6
SCCS-vsn: usr.bin/window/error.c 3.3
SCCS-vsn: usr.bin/window/lcmd.c 3.7
SCCS-vsn: usr.bin/window/lcmd1.c 3.7

usr/src/usr.bin/window/defs.h
usr/src/usr.bin/window/error.c
usr/src/usr.bin/window/lcmd.c
usr/src/usr.bin/window/lcmd1.c

index 3f19992..79e9d59 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- *     @(#)defs.h      3.5 83/11/02    
+ *     @(#)defs.h      3.6 83/11/22    
  */
 
 #include "ww.h"
  */
 
 #include "ww.h"
@@ -47,11 +47,5 @@ char escapec;                        /* escape character */
 
 struct ww *getwin();
 struct ww *openwin();
 
 struct ww *getwin();
 struct ww *openwin();
-struct ww *idtowin();
+struct ww *vtowin();
 struct ww *openiwin();
 struct ww *openiwin();
-
-       /* stuff for long commands */
-int argc;
-char *argv[100];
-int lineno;                    /* line number in the source file */
-char insource;                 /* we're in a source */
index d3784e9..5c38b85 100644 (file)
@@ -1,13 +1,10 @@
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)error.c     3.2 83/08/16";
+static char *sccsid = "@(#)error.c     3.3 83/11/22";
 #endif
 
 #include "defs.h"
 #endif
 
 #include "defs.h"
-
-static char *filename;                 /* source file name */
-static struct ww *errwin;              /* window for error reporting */
-static int errlineno;                  /* lineno in errwin */
-static char baderror;                  /* can't open the error window */
+#include "value.h"
+#include "context.h"
 
 #define ERRLINES 10                    /* number of lines for errwin */
 
 
 #define ERRLINES 10                    /* number of lines for errwin */
 
@@ -15,7 +12,7 @@ static char baderror;                 /* can't open the error window */
 error(fmt, a, b, c, d, e, f, g, h)
 char *fmt;
 {
 error(fmt, a, b, c, d, e, f, g, h)
 char *fmt;
 {
-       if (filename == 0) {
+       if (cx.x_type != X_FILE) {
                if (terse)
                        wwbell();
                else {
                if (terse)
                        wwbell();
                else {
@@ -24,44 +21,34 @@ char *fmt;
                }
                return;
        }
                }
                return;
        }
-       if (baderror)
+       if (cx.x_baderr)
                return;
                return;
-       if (errwin == 0) {
+       if (cx.x_errwin == 0) {
                char buf[512];
 
                char buf[512];
 
-               (void) sprintf(buf, "Errors from %s", filename);
-               if ((errwin = openiwin(ERRLINES, buf)) == 0) {
+               (void) sprintf(buf, "Errors from %s", cx.x_filename);
+               if ((cx.x_errwin = openiwin(ERRLINES, buf)) == 0) {
                        (void) wwprintf(cmdwin, "Can't open error window.  ");
                        (void) wwprintf(cmdwin, "Can't open error window.  ");
-                       baderror++;
+                       cx.x_baderr = 1;
                        return;
                }
                        return;
                }
-               errlineno = 0;
+               cx.x_errlineno = 0;
        }
        }
-       if (errlineno++ > ERRLINES - 4) {
-               waitnl(errwin);
-               errlineno = 0;
+       if (cx.x_errlineno++ > ERRLINES - 4) {
+               waitnl(cx.x_errwin);
+               cx.x_errlineno = 0;
        }
        }
-       if (lineno != 0)
-               (void) wwprintf(errwin, "line %d: ", lineno);
-       (void) wwprintf(errwin, fmt, a, b, c, d, e, f, g, h);
-       (void) wwprintf(errwin, "\n");
-}
-
-beginerror(fn)
-char *fn;
-{
-       filename = malloc((unsigned) strlen(fn) + 1);
-       (void) strcpy(filename, fn);
+       if (cx.x_lineno != 0)
+               (void) wwprintf(cx.x_errwin, "line %d: ", cx.x_lineno);
+       (void) wwprintf(cx.x_errwin, fmt, a, b, c, d, e, f, g, h);
+       (void) wwprintf(cx.x_errwin, "\n");
 }
 
 }
 
-enderror()
+err_end()
 {
 {
-       if (errwin != 0) {
-               waitnl(errwin);
-               closeiwin(errwin);
-               errwin = 0;
+       if (cx.x_errwin != 0) {
+               waitnl(cx.x_errwin);
+               closeiwin(cx.x_errwin);
+               cx.x_errwin = 0;
        }
        }
-       baderror = 0;
-       free(filename);
-       filename = 0;
 }
 }
index 1a246ae..af8517f 100644 (file)
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)lcmd.c      3.6 83/08/25";
+static char *sccsid = "@(#)lcmd.c      3.7 83/11/22";
 #endif
 
 #include "defs.h"
 #endif
 
 #include "defs.h"
+#include "value.h"
+#include "lcmd.h"
 
 int l_buffer();
 
 int l_buffer();
-int l_window();
-int l_select();
+int l_close();
 int l_escape();
 int l_label();
 int l_escape();
 int l_label();
-int l_terse();
+int l_select();
 int l_source();
 int l_source();
+int l_terse();
+int l_window();
 int l_write();
 int l_write();
-int l_close();
 
 
-struct lcmd {
-       char *l_name;                   /* name of command */
-       int l_lmin;                     /* minimum length to check */
-       int l_lmax;                     /* maximum length to check */
-       int l_amin;                     /* minimum argument */
-       int l_amax;                     /* maximum argument */
-       int (*l_func)();                /* the function */
-};
-static struct lcmd lcmd[] = {
-       "%",            1, 1, 0,  0, l_select,
-       "buffer",       1, 0, 1,  1, l_buffer,
-       "close",        1, 0, 0, -1, l_close,
-       "escape",       1, 0, 1,  1, l_escape,
-       "label",        1, 0, 2,  2, l_label,
-       "source",       1, 0, 1,  1, l_source,
-       "terse",        1, 0, 0,  1, l_terse,
-       "window",       1, 0, 4,  5, l_window,
-       "write",        2, 0, 2,  2, l_write,
-       0
+struct lcmd_arg arg_buffer[];
+struct lcmd_arg arg_close[];
+struct lcmd_arg arg_escape[];
+struct lcmd_arg arg_label[];
+struct lcmd_arg arg_select[];
+struct lcmd_arg arg_source[];
+struct lcmd_arg arg_terse[];
+struct lcmd_arg arg_window[];
+struct lcmd_arg arg_write[];
+
+struct lcmd_tab lcmd_tab[] = {
+       "%",            1,      l_select,       arg_select,
+       "buffer",       1,      l_buffer,       arg_buffer,
+       "close",        1,      l_close,        arg_close,
+       "escape",       1,      l_escape,       arg_escape,
+       "label",        1,      l_label,        arg_label,
+       "select",       2,      l_select,       arg_select,
+       "source",       2,      l_source,       arg_source,
+       "terse",        1,      l_terse,        arg_terse,
+       "window",       2,      l_window,       arg_window,
+       "write",        2,      l_write,        arg_write,
+       0,              0,      0,              0,
 };
 
 };
 
-dosource(filename)
-char *filename;
+struct lcmd_tab *
+lcmd_lookup(name)
+char *name;
 {
 {
-       register FILE *f;
-       char buf[BUFSIZ];
+       register struct lcmd_tab *p;
 
 
-       if ((f = fopen(filename, "r")) == 0)
-               return -1;
-       insource++;
-       beginerror(filename);
-       for (lineno = 1; fgets(buf, sizeof buf, f) != 0; lineno++)
-               dolongcmd(buf);
-       enderror();
-       insource = 0;
+       for (p = lcmd_tab; p->lc_name != 0; p++)
+               if (str_match(name, p->lc_name, p->lc_minlen))
+                       return p;
        return 0;
 }
 
        return 0;
 }
 
-dolongcmd(line)
-char *line;
+dosource(filename)
+char *filename;
 {
 {
-       register struct lcmd *lp;
-       register len;
-
-       makeargv(line);
-       if (argc == 0)
-               return;
-       for (lp = lcmd; lp->l_name; lp++) {
-               len = strlen(*argv);
-               if (len < lp->l_lmin)
-                       continue;
-               if (!strncmp(*argv, lp->l_name, lp->l_lmax ? lp->l_lmax : len))
-                       break;
-       }
-       if (lp->l_name) {
-               if (lp->l_amin > argc - 1)
-                       error("Too few arguments.");
-               else if (lp->l_amax >= 0 && lp->l_amax < argc - 1)
-                       error("Too many arguments.");
-               else
-                       (*lp->l_func)();
-       } else
-               error("%s: Unknown command.", *argv);
+       if (cx_setfile(filename) < 0)
+               return -1;
+       p_start();
+       err_end();
+       cx_end();
+       return 0;
 }
 
 }
 
-makeargv(p)
-register char *p;
+dolongcmd(buffer)
+char *buffer;
 {
 {
-       static char buf[BUFSIZ];
-       register char *q = buf, **pp = argv;
-       char quote = 0, escape = 0;
-       int i;
-
-       for (; *p == ' ' || *p == '\t'; p++)
-               ;
-       while (*p && *p != '\n' && (*p != '#' || escape || quote)
-              && pp < &argv[sizeof argv/sizeof *argv - 1]) {
-               *pp++ = q;
-               while (*p && *p != '\n') {
-                       if (escape) {
-                               switch (*p) {
-                               case 'n':
-                                       *q++ = '\n';
-                                       p++;
-                                       break;
-                               case 'r':
-                                       *q++ = '\r';
-                                       p++;
-                                       break;
-                               case '0': case '1': case '2': case '3':
-                               case '4': case '5': case '6': case '7':
-                                       *q = 0;
-                                       for (i = 3; --i >= 0
-                                            && *p >= '0' && *p <= '9';)
-                                               *q = *q << 3 | *p++ - '0';
-                                       q++;
-                                       break;
-                               default:
-                                       *q++ = *p++;
-                                       break;
-                               }
-                               escape = 0;
-                       } else if (*p == '\\') {
-                               escape = 1;
-                               p++;
-                       } else if (quote) {
-                               if (*p == quote) {
-                                       quote = 0;
-                                       p++;
-                               } else
-                                       *q++ = *p++;
-                       } else {
-                               if (*p == '"' || *p == '\'')
-                                       quote = *p++;
-                               else if (*p == ' ' || *p == '\t')
-                                       break;
-                               else
-                                       *q++ = *p++;
-                       }
-               }
-               *q++ = 0;
-               for (; *p == ' ' || *p == '\t'; p++)
-                       ;
-       }
-       *pp = 0;
-       argc = pp - argv;
+       if (cx_setbuf(buffer) < 0)
+               return -1;
+       p_start();
+       err_end();
+       cx_end();
+       return 0;
 }
 }
index c1e052f..373d4d4 100644 (file)
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)lcmd1.c     3.6 83/08/26";
+static char *sccsid = "@(#)lcmd1.c     3.7 83/11/22";
 #endif
 
 #include "defs.h"
 #endif
 
 #include "defs.h"
-
-l_window()
+#include "string.h"
+#include "value.h"
+#include "lcmd.h"
+
+struct lcmd_arg arg_window[] = {
+       { "row",        1,      ARG_NUM },
+       { "column",     1,      ARG_NUM },
+       { "nrows",      2,      ARG_NUM },
+       { "ncols",      2,      ARG_NUM },
+       { "nlines",     2,      ARG_NUM },
+       { "label",      1,      ARG_STR },
+       { 0,            0,      0 }
+};
+
+l_window(v)
+register struct value *v;
 {
 {
-       register char **pp = argv;
+       register struct lcmd_arg *a = arg_window;
        int col, row, ncol, nrow, id, nline;
        int col, row, ncol, nrow, id, nline;
+       char *label;
 
        if ((id = findid()) < 0)
                return;
 
        if ((id = findid()) < 0)
                return;
-       row = **++pp == '*' ? 1 : atoi(*pp);
-       col = **++pp == '*' ? 0 : atoi(*pp);
-       nrow = **++pp == '*' ? wwnrow - row : atoi(*pp);
-       ncol = **++pp == '*' ? wwncol - col : atoi(*pp);
-       nline = *++pp == 0 ? nbufline : atoi(*pp);
-       (void) openwin(id, row, col, nrow, ncol, nline);
+       row = a->arg_vtype == V_ERR ? 1 : a->arg_num;
+       col = (++a)->arg_vtype == V_ERR ? 0 : a->arg_num;
+       nrow = (++a)->arg_vtype == V_ERR ? wwnrow - row : a->arg_num;
+       ncol = (++a)->arg_vtype == V_ERR ? wwncol - col : a->arg_num;
+       nline = (++a)->arg_vtype == V_ERR ? nbufline : a->arg_num;
+       label =  (++a)->arg_vtype == V_ERR ? 0 : a->arg_str;
+       if (openwin(id, row, col, nrow, ncol, nline, label) == 0)
+               return;
+       v->v_type = V_NUM;
+       v->v_num = id;
 }
 
 }
 
-l_buffer()
+struct lcmd_arg arg_buffer[] = {
+       { "nlines",     1,      ARG_NUM },
+       { 0,            0,      0 }
+};
+
+l_buffer(v)
+struct value *v;
 {
 {
-       nbufline = atoi(argv[1]);
+       v->v_num = nbufline;
+       v->v_type = V_NUM;
+       if (arg_buffer[0].arg_vtype != V_ERR)
+               nbufline = arg_buffer[0].arg_num;
 }
 
 }
 
-l_select()
+struct lcmd_arg arg_select[] = {
+       { "window",     1,      ARG_NUM },
+       { 0,            0,      0 }
+};
+
+l_select(v)
+struct value *v;
 {
        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;
+       if (arg_select[0].arg_vtype == V_ERR)
                return;
                return;
+       if ((w = window[arg_select[0].arg_num]) == 0) {
+               error("%d: No such window.", arg_select[0].arg_num);
+               return;
+       }
        setselwin(w);
 }
 
        setselwin(w);
 }
 
-l_escape()
+struct lcmd_arg arg_escape[] = {
+       { "escapec",    1,      ARG_NUM },
+       { 0,            0,      0 }
+};
+
+l_escape(v)
+struct value *v;
 {
 {
-       setescape(argv[1]);
+       if ((v->v_str = str_cpy(unctrl(escapec))) == 0) {
+               error("Out of memory.");
+               return;
+       }
+       v->v_type = V_STR;
+       if (arg_escape[0].arg_type != V_ERR)
+               setescape(arg_escape[0].arg_str);
 }
 
 }
 
-l_label()
+struct lcmd_arg arg_label[] = {
+       { "window",     1,      ARG_NUM },
+       { "label",      1,      ARG_STR },
+       { 0,            0,      0 }
+};
+
+/*ARGSUSED*/
+l_label(v)
+struct value *v;
 {
        struct ww *w;
 {
        struct ww *w;
+       register struct lcmd_arg *a = arg_label;
 
 
-       if ((w = idtowin(argv[1])) == 0)
+       if ((w = vtowin(&a->arg_val)) == 0)
                return;
                return;
-       if (setlabel(w, argv[2]) < 0)
+       if ((++a)->arg_vtype != V_ERR && setlabel(w, a->arg_str) < 0)
                error("Out of memory.");
        reframe();
 }
 
                error("Out of memory.");
        reframe();
 }
 
-l_terse()
+struct lcmd_arg arg_terse[] = {
+       { "flag",       1,      ARG_NUM },
+       { 0,            0,      0 }
+};
+
+l_terse(v)
+struct value *v;
 {
 {
-       char oldterse = terse;
-
-       if (argc < 2)
-               terse = 1;
-       else if (strcmp(argv[1], "off") == 0)
-               terse = 0;
-       else
-               terse = 1;
-       if (terse && !oldterse)
+       v->v_type = V_NUM;
+       v->v_num = terse;
+       terse = vtobool(&arg_terse[0].arg_val, 1, terse);
+       if (!terse && v->v_num)
                wwdelete(cmdwin);
                wwdelete(cmdwin);
-       else if (!terse && oldterse)
+       else if (!v->v_num && terse)
                wwadd(cmdwin, &wwhead);
        reframe();
 }
 
                wwadd(cmdwin, &wwhead);
        reframe();
 }
 
-l_source()
+struct lcmd_arg arg_source[] = {
+       { "filename",   1,      ARG_STR },
+       { 0,            0,      0 }
+};
+
+/*ARGSUSED*/
+l_source(v)
+struct value *v;
 {
 {
-       if (insource) {
-               error("Recursive source.");
-               return;
-       }
-       if (dosource(argv[1]) < 0)
-               error("Can't open %s.", argv[1]);
+       if (arg_source[0].arg_vtype != V_ERR
+           && dosource(arg_source[0].arg_str) < 0) {
+               error("Can't open %s.", arg_source[0].arg_str);
+               v->v_num = -1;
+       } else
+               v->v_num = 0;
+       v->v_type = V_NUM;
 }
 
 }
 
-l_write()
+struct lcmd_arg arg_write[] = {
+       { "window",     1,      ARG_NUM },
+       { "string",     1,      ARG_STR },
+       { 0,            0,      0 }
+};
+
+/*ARGSUSED*/
+l_write(v)
+struct value *v;
 {
 {
+       register struct lcmd_arg *a = arg_write;
        struct ww *w;
 
        struct ww *w;
 
-       if ((w = idtowin(argv[1])) == 0)
+       if ((w = vtowin(&a->arg_val)) == 0)
                return;
                return;
-       (void) write(w->ww_pty, argv[2], strlen(argv[2]));
+       a++;
+       (void) write(w->ww_pty, a->arg_str, strlen(a->arg_str));
 }
 
 }
 
-l_close()
+struct lcmd_arg arg_close[] = {
+       { "window",     1,      ARG_NUM },
+       { 0,            0,      0 }
+};
+
+/*ARGSUSED*/
+l_close(v)
+struct value *v;
 {
 {
+       register struct lcmd_arg *a = arg_close;
        register i;
        register i;
-       register struct ww *w;
-       char didit = 0;
+       struct ww *w;
 
 
-       if (argc < 2) {
+       if (a->arg_vtype == V_ERR) {
                c_close((struct ww *)0);
                return;
        }
                c_close((struct ww *)0);
                return;
        }
-       for (i = 1; i < argc; i++) {
-               if ((w = idtowin(argv[i])) == 0)
-                       continue;
-               closewin(w);
-               didit++;
-       }
+       if ((w = vtowin(&a->arg_val)) == 0)
+               return;
+       closewin(w);
        if (selwin == 0) {
                for (i = 0; i < NWINDOW && window[i] != 0; i++)
                        ;
                if (i < NWINDOW)
                        setselwin(window[i]);
        }
        if (selwin == 0) {
                for (i = 0; i < NWINDOW && window[i] != 0; i++)
                        ;
                if (i < NWINDOW)
                        setselwin(window[i]);
        }
-       if (didit)
-               reframe();
+       reframe();
 }
 
 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("Window identifier required.");
+               return 0;
+       case V_STR:
+               error("Number required for window identifier.");
+               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*/
+}