parse.c (2049B)
1 #include <u.h> 2 #include <libc.h> 3 #include <fcall.h> 4 #include <thread.h> 5 #include <9p.h> 6 7 /* 8 * Generous estimate of number of fields, including terminal nil pointer 9 */ 10 static int 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* 36 parsecmd(char *p, int n) 37 { 38 Cmdbuf *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 = emalloc9p(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 memmove(cb->buf, p, n); 51 52 /* dump new line and null terminate */ 53 if(n > 0 && cb->buf[n-1] == '\n') 54 n--; 55 cb->buf[n] = '\0'; 56 57 cb->nf = tokenize(cb->buf, cb->f, nf-1); 58 cb->f[cb->nf] = nil; 59 60 return cb; 61 } 62 63 /* 64 * Reconstruct original message, for error diagnostic 65 */ 66 void 67 respondcmderror(Req *r, Cmdbuf *cb, char *fmt, ...) 68 { 69 int i; 70 va_list arg; 71 char *p, *e; 72 char err[ERRMAX]; 73 74 e = err+ERRMAX-10; 75 va_start(arg, fmt); 76 p = vseprint(err, e, fmt, arg); 77 va_end(arg); 78 p = seprint(p, e, ": \""); 79 for(i=0; i<cb->nf; i++){ 80 if(i > 0) 81 p = seprint(p, e, " "); 82 p = seprint(p, e, "%q", cb->f[i]); 83 } 84 strcpy(p, "\""); 85 respond(r, err); 86 } 87 88 /* 89 * Look up entry in table 90 */ 91 Cmdtab* 92 lookupcmd(Cmdbuf *cb, Cmdtab *ctab, int nctab) 93 { 94 int i; 95 Cmdtab *ct; 96 97 if(cb->nf == 0){ 98 werrstr("empty control message"); 99 return nil; 100 } 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 werrstr("bad # args to command"); 108 return nil; 109 } 110 return ct; 111 } 112 113 werrstr("unknown control message"); 114 return nil; 115 }