ndbaux.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <bio.h>
  12. #include <ctype.h>
  13. #include <ndb.h>
  14. #include "ndbhf.h"
  15. /*
  16. * parse a single tuple
  17. */
  18. char*
  19. _ndbparsetuple(char *cp, Ndbtuple **tp)
  20. {
  21. char *p;
  22. int len;
  23. Ndbtuple *t;
  24. /* a '#' starts a comment lasting till new line */
  25. EATWHITE(cp);
  26. if(*cp == '#' || *cp == '\n')
  27. return 0;
  28. t = ndbnew(nil, nil);
  29. setmalloctag(t, getcallerpc());
  30. *tp = t;
  31. /* parse attribute */
  32. p = cp;
  33. while(*cp != '=' && !ISWHITE(*cp) && *cp != '\n')
  34. cp++;
  35. len = cp - p;
  36. if(len >= Ndbalen)
  37. len = Ndbalen-1;
  38. strncpy(t->attr, p, len);
  39. /* parse value */
  40. EATWHITE(cp);
  41. if(*cp == '='){
  42. cp++;
  43. if(*cp == '"'){
  44. p = ++cp;
  45. while(*cp != '\n' && *cp != '"')
  46. cp++;
  47. len = cp - p;
  48. if(*cp == '"')
  49. cp++;
  50. } else if(*cp == '#'){
  51. len = 0;
  52. } else {
  53. p = cp;
  54. while(!ISWHITE(*cp) && *cp != '\n')
  55. cp++;
  56. len = cp - p;
  57. }
  58. ndbsetval(t, p, len);
  59. }
  60. return cp;
  61. }
  62. /*
  63. * parse all tuples in a line. we assume that the
  64. * line ends in a '\n'.
  65. *
  66. * the tuples are linked as a list using ->entry and
  67. * as a ring using ->line.
  68. */
  69. Ndbtuple*
  70. _ndbparseline(char *cp)
  71. {
  72. Ndbtuple *t;
  73. Ndbtuple *first, *last;
  74. first = last = 0;
  75. while(*cp != '#' && *cp != '\n'){
  76. t = 0;
  77. cp = _ndbparsetuple(cp, &t);
  78. if(cp == 0)
  79. break;
  80. if(first){
  81. last->line = t;
  82. last->entry = t;
  83. } else
  84. first = t;
  85. last = t;
  86. t->line = 0;
  87. t->entry = 0;
  88. setmalloctag(t, getcallerpc());
  89. }
  90. if(first)
  91. last->line = first;
  92. ndbsetmalloctag(first, getcallerpc());
  93. return first;
  94. }