Blame


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