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 "../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. Proc *up = externup();
  44. Cmdbuf *volatile cb;
  45. int nf;
  46. char *sp;
  47. nf = ncmdfield(p, n);
  48. /* allocate Cmdbuf plus string pointers plus copy of string including \0 */
  49. sp = smalloc(sizeof(*cb) + nf * sizeof(char*) + n + 1);
  50. cb = (Cmdbuf*)sp;
  51. cb->f = (char**)(&cb[1]);
  52. cb->buf = (char*)(&cb->f[nf]);
  53. if(up!=nil && waserror()){
  54. free(cb);
  55. nexterror();
  56. }
  57. memmove(cb->buf, p, n);
  58. if(up != nil)
  59. poperror();
  60. /* dump new line and null terminate */
  61. if(n > 0 && cb->buf[n-1] == '\n')
  62. n--;
  63. cb->buf[n] = '\0';
  64. cb->nf = tokenize(cb->buf, cb->f, nf-1);
  65. cb->f[cb->nf] = nil;
  66. return cb;
  67. }
  68. /*
  69. * Reconstruct original message, for error diagnostic
  70. */
  71. void
  72. cmderror(Cmdbuf *cb, char *s)
  73. {
  74. Proc *up = externup();
  75. int i;
  76. char *p, *e;
  77. p = up->genbuf;
  78. e = p+ERRMAX-10;
  79. p = seprint(p, e, "%s \"", s);
  80. for(i=0; i<cb->nf; i++){
  81. if(i > 0)
  82. p = seprint(p, e, " ");
  83. p = seprint(p, e, "%q", cb->f[i]);
  84. }
  85. strcpy(p, "\"");
  86. error(up->genbuf);
  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. if(cb->nf == 0)
  97. error("empty control message");
  98. for(ct = ctab, i=0; i<nctab; i++, ct++){
  99. if(strcmp(ct->cmd, "*") !=0) /* wildcard always matches */
  100. if(strcmp(ct->cmd, cb->f[0]) != 0)
  101. continue;
  102. if(ct->narg != 0 && ct->narg != cb->nf)
  103. cmderror(cb, Ecmdargs);
  104. return ct;
  105. }
  106. cmderror(cb, "unknown control message");
  107. return nil;
  108. }