parse.c 2.4 KB

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