2 #include "../port/lib.h"
6 #include "../port/error.h"
9 * Generous estimate of number of fields, including terminal nil pointer
12 ncmdfield(char *p, int n)
23 white = 1; /* first text will start field */
25 nwhite = (strchr(" \t\r\n", *p++ & 0xFF) != 0); /* UTF is irrelevant */
26 if(white && !nwhite) /* beginning of field */
30 return nf+1; /* +1 for nil */
34 * parse a command written to a device
37 parsecmd(char *p, int n)
45 /* allocate Cmdbuf plus string pointers plus copy of string including \0 */
46 sp = smalloc(sizeof(*cb) + nf * sizeof(char*) + n + 1);
48 cb->f = (char**)(&cb[1]);
49 cb->buf = (char*)(&cb->f[nf]);
51 if(up!=nil && waserror()){
55 memmove(cb->buf, p, n);
59 /* dump new line and null terminate */
60 if(n > 0 && cb->buf[n-1] == '\n')
64 cb->nf = tokenize(cb->buf, cb->f, nf-1);
71 * Reconstruct original message, for error diagnostic
74 cmderror(Cmdbuf *cb, char *s)
81 p = seprint(p, e, "%s \"", s);
82 for(i=0; i<cb->nf; i++){
84 p = seprint(p, e, " ");
85 p = seprint(p, e, "%q", cb->f[i]);
92 * Look up entry in table
95 lookupcmd(Cmdbuf *cb, Cmdtab *ctab, int nctab)
101 error("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 cmderror(cb, Ecmdargs);
112 cmderror(cb, "unknown control message");