8 * Generous estimate of number of fields, including terminal nil pointer
11 ncmdfield(char *p, int n)
22 white = 1; /* first text will start field */
24 nwhite = (strchr(" \t\r\n", *p++ & 0xFF) != 0); /* UTF is irrelevant */
25 if(white && !nwhite) /* beginning of field */
29 return nf+1; /* +1 for nil */
33 * parse a command written to a device
36 parsecmd(char *p, int n)
44 /* allocate Cmdbuf plus string pointers plus copy of string including \0 */
45 sp = emalloc9p(sizeof(*cb) + nf * sizeof(char*) + n + 1);
47 cb->f = (char**)(&cb[1]);
48 cb->buf = (char*)(&cb->f[nf]);
50 memmove(cb->buf, p, n);
52 /* dump new line and null terminate */
53 if(n > 0 && cb->buf[n-1] == '\n')
57 cb->nf = tokenize(cb->buf, cb->f, nf-1);
64 * Reconstruct original message, for error diagnostic
67 respondcmderror(Req *r, Cmdbuf *cb, char *fmt, ...)
76 p = vseprint(err, e, fmt, arg);
78 p = seprint(p, e, ": \"");
79 quotefmtinstall(); /* just in case */
80 for(i=0; i<cb->nf; i++){
82 p = seprint(p, e, " ");
83 p = seprint(p, e, "%q", cb->f[i]);
90 * Look up entry in table
93 lookupcmd(Cmdbuf *cb, Cmdtab *ctab, int nctab)
99 werrstr("empty control message");
103 for(ct = ctab, i=0; i<nctab; i++, ct++){
104 if(strcmp(ct->cmd, "*") !=0) /* wildcard always matches */
105 if(strcmp(ct->cmd, cb->f[0]) != 0)
107 if(ct->narg != 0 && ct->narg != cb->nf){
108 werrstr("bad # args to command");
114 werrstr("unknown control message");