parse.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. /*
  16. * Generous estimate of number of fields, including terminal nil pointer
  17. */
  18. static int
  19. ncmdfield(char *p, int n)
  20. {
  21. int white, nwhite;
  22. char *ep;
  23. int nf;
  24. if(p == nil)
  25. return 1;
  26. nf = 0;
  27. ep = p+n;
  28. white = 1; /* first text will start field */
  29. while(p < ep){
  30. nwhite = (strchr(" \t\r\n", *p++ & 0xFF) != 0); /* UTF is irrelevant */
  31. if(white && !nwhite) /* beginning of field */
  32. nf++;
  33. white = nwhite;
  34. }
  35. return nf+1; /* +1 for nil */
  36. }
  37. /*
  38. * parse a command written to a device
  39. */
  40. Cmdbuf*
  41. parsecmd(char *p, int n)
  42. {
  43. Cmdbuf *volatile cb;
  44. int nf;
  45. char *sp;
  46. nf = ncmdfield(p, n);
  47. /* allocate Cmdbuf plus string pointers plus copy of string including \0 */
  48. sp = smalloc(sizeof(*cb) + nf * sizeof(char*) + n + 1);
  49. cb = (Cmdbuf*)sp;
  50. cb->f = (char**)(&cb[1]);
  51. cb->buf = (char*)(&cb->f[nf]);
  52. if(up!=nil && waserror()){
  53. free(cb);
  54. nexterror();
  55. }
  56. memmove(cb->buf, p, n);
  57. if(up != nil)
  58. poperror();
  59. /* dump new line and null terminate */
  60. if(n > 0 && cb->buf[n-1] == '\n')
  61. n--;
  62. cb->buf[n] = '\0';
  63. cb->nf = tokenize(cb->buf, cb->f, nf-1);
  64. cb->f[cb->nf] = nil;
  65. return cb;
  66. }
  67. /*
  68. * Reconstruct original message, for error diagnostic
  69. */
  70. void
  71. cmderror(Cmdbuf *cb, char *s)
  72. {
  73. int i;
  74. char *p, *e;
  75. p = up->genbuf;
  76. e = p+ERRMAX-10;
  77. p = seprint(p, e, "%s \"", s);
  78. for(i=0; i<cb->nf; i++){
  79. if(i > 0)
  80. p = seprint(p, e, " ");
  81. p = seprint(p, e, "%q", cb->f[i]);
  82. }
  83. strcpy(p, "\"");
  84. error(up->genbuf);
  85. }
  86. /*
  87. * Look up entry in table
  88. */
  89. Cmdtab*
  90. lookupcmd(Cmdbuf *cb, Cmdtab *ctab, int nctab)
  91. {
  92. int i;
  93. Cmdtab *ct;
  94. if(cb->nf == 0)
  95. error("empty control message");
  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. cmderror(cb, Ecmdargs);
  102. return ct;
  103. }
  104. cmderror(cb, "unknown control message");
  105. return nil;
  106. }