1 #include "u.h"
2 #include "lib.h"
3 #include "dat.h"
4 #include "fns.h"
5 #include "error.h"
6
7 /*
8 * Generous estimate of number of fields, including terminal nil pointer
9 */
10 static int
ncmdfield(char * p,int n)11 ncmdfield(char *p, int n)
12 {
13 int white, nwhite;
14 char *ep;
15 int nf;
16
17 if(p == nil)
18 return 1;
19
20 nf = 0;
21 ep = p+n;
22 white = 1; /* first text will start field */
23 while(p < ep){
24 nwhite = (strchr(" \t\r\n", *p++ & 0xFF) != 0); /* UTF is irrelevant */
25 if(white && !nwhite) /* beginning of field */
26 nf++;
27 white = nwhite;
28 }
29 return nf+1; /* +1 for nil */
30 }
31
32 /*
33 * parse a command written to a device
34 */
35 Cmdbuf*
parsecmd(char * p,int n)36 parsecmd(char *p, int n)
37 {
38 Cmdbuf *volatile cb;
39 int nf;
40 char *sp;
41
42 nf = ncmdfield(p, n);
43
44 /* allocate Cmdbuf plus string pointers plus copy of string including \0 */
45 sp = smalloc(sizeof(*cb) + nf * sizeof(char*) + n + 1);
46 cb = (Cmdbuf*)sp;
47 cb->f = (char**)(&cb[1]);
48 cb->buf = (char*)(&cb->f[nf]);
49
50 if(up!=nil && waserror()){
51 free(cb);
52 nexterror();
53 }
54 memmove(cb->buf, p, n);
55 if(up != nil)
56 poperror();
57
58 /* dump new line and null terminate */
59 if(n > 0 && cb->buf[n-1] == '\n')
60 n--;
61 cb->buf[n] = '\0';
62
63 cb->nf = tokenize(cb->buf, cb->f, nf-1);
64 cb->f[cb->nf] = nil;
65
66 return cb;
67 }
68
69 /*
70 * Reconstruct original message, for error diagnostic
71 */
72 void
cmderror(Cmdbuf * cb,char * s)73 cmderror(Cmdbuf *cb, char *s)
74 {
75 int i;
76 char *p, *e;
77
78 p = up->genbuf;
79 e = p+ERRMAX-10;
80 p = seprint(p, e, "%s \"", s);
81 for(i=0; i<cb->nf; i++){
82 if(i > 0)
83 p = seprint(p, e, " ");
84 p = seprint(p, e, "%q", cb->f[i]);
85 }
86 strcpy(p, "\"");
87 error(up->genbuf);
88 }
89
90 /*
91 * Look up entry in table
92 */
93 Cmdtab*
lookupcmd(Cmdbuf * cb,Cmdtab * ctab,int nctab)94 lookupcmd(Cmdbuf *cb, Cmdtab *ctab, int nctab)
95 {
96 int i;
97 Cmdtab *ct;
98
99 if(cb->nf == 0)
100 error("empty control message");
101
102 for(ct = ctab, i=0; i<nctab; i++, ct++){
103 if(strcmp(ct->cmd, "*") !=0) /* wildcard always matches */
104 if(strcmp(ct->cmd, cb->f[0]) != 0)
105 continue;
106 if(ct->narg != 0 && ct->narg != cb->nf)
107 cmderror(cb, Ecmdargs);
108 return ct;
109 }
110
111 cmderror(cb, "unknown control message");
112 return nil;
113 }
114