ndbparse.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 data base entry. Entries may span multiple
  17. * lines. An entry starts on a left margin. All subsequent
  18. * lines must be indented by white space. An entry consists
  19. * of tuples of the forms:
  20. * attribute-name
  21. * attribute-name=value
  22. * attribute-name="value with white space"
  23. *
  24. * The parsing returns a 2-dimensional structure. The first
  25. * dimension joins all tuples. All tuples on the same line
  26. * form a ring along the second dimension.
  27. */
  28. /*
  29. * parse the next entry in the file
  30. */
  31. Ndbtuple*
  32. ndbparse(Ndb *db)
  33. {
  34. char *line;
  35. Ndbtuple *t;
  36. Ndbtuple *first, *last;
  37. int len;
  38. first = last = 0;
  39. for(;;){
  40. if((line = Brdline(&db->b, '\n')) == 0)
  41. break;
  42. len = Blinelen(&db->b);
  43. if(line[len-1] != '\n')
  44. break;
  45. if(first && !ISWHITE(*line) && *line != '#'){
  46. Bseek(&db->b, -len, 1);
  47. break;
  48. }
  49. t = _ndbparseline(line);
  50. if(t == 0)
  51. continue;
  52. setmalloctag(t, getcallerpc(&db));
  53. if(first)
  54. last->entry = t;
  55. else
  56. first = t;
  57. last = t;
  58. while(last->entry)
  59. last = last->entry;
  60. }
  61. ndbsetmalloctag(first, getcallerpc(&db));
  62. return first;
  63. }