/*
* Generous estimate of number of fields, including terminal nil pointer
*/
static int
ncmdfield(char *p, int n)
{
int white, nwhite;
char *ep;
int nf;
if(p == nil)
return 1;
nf = 0;
ep = p+n;
white = 1; /* first text will start field */
while(p < ep){
nwhite = (strchr(" \t\r\n", *p++ & 0xFF) != 0); /* UTF is irrelevant */
if(white && !nwhite) /* beginning of field */
nf++;
white = nwhite;
}
return nf+1; /* +1 for nil */
}
/*
* parse a command written to a device
*/
Cmdbuf*
parsecmd(char *p, int n)
{
Cmdbuf *volatile cb;
int nf;
char *sp;
nf = ncmdfield(p, n);
/* allocate Cmdbuf plus string pointers plus copy of string including \0 */
sp = smalloc(sizeof(*cb) + nf * sizeof(char*) + n + 1);
cb = (Cmdbuf*)sp;
cb->f = (char**)(&cb[1]);
cb->buf = (char*)(&cb->f[nf]);
/*
* Reconstruct original message, for error diagnostic
*/
void
cmderror(Cmdbuf *cb, char *s)
{
int i;
char *p, *e;
p = up->genbuf;
e = p+ERRMAX-10;
p = seprint(p, e, "%s \"", s);
for(i=0; i<cb->nf; i++){
if(i > 0)
p = seprint(p, e, " ");
p = seprint(p, e, "%q", cb->f[i]);
}
strcpy(p, "\"");
error(up->genbuf);
}
/*
* Look up entry in table
*/
Cmdtab*
lookupcmd(Cmdbuf *cb, Cmdtab *ctab, int nctab)
{
int i;
Cmdtab *ct;