1 #include <u.h> 2 #include <libc.h> 3 #include <bio.h> 4 #include <ctype.h> 5 #include <ndb.h> 6 #include "ndbhf.h" 7 8 9 /* 10 * parse a single tuple 11 */ 12 char* 13 _ndbparsetuple(char *cp, Ndbtuple **tp) 14 { 15 char *p; 16 int len; 17 Ndbtuple *t; 18 19 /* a '#' starts a comment lasting till new line */ 20 EATWHITE(cp); 21 if(*cp == '#' || *cp == '\n') 22 return 0; 23 24 /* we keep our own free list to reduce mallocs */ 25 t = malloc(sizeof(Ndbtuple)); 26 if(t == 0) 27 return 0; 28 memset(t, 0, sizeof(*t)); 29 *tp = t; 30 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; 38 strncpy(t->attr, p, len); 39 40 /* parse value */ 41 EATWHITE(cp); 42 if(*cp == '='){ 43 cp++; 44 EATWHITE(cp); 45 if(*cp == '"'){ 46 p = ++cp; 47 while(*cp != '\n' && *cp != '"') 48 cp++; 49 len = cp - p; 50 if(*cp == '"') 51 cp++; 52 } else { 53 p = cp; 54 while(!ISWHITE(*cp) && *cp != '\n') 55 cp++; 56 len = cp - p; 57 } 58 if(len >= Ndbvlen) 59 len = Ndbvlen; 60 strncpy(t->val, p, len); 61 } 62 63 return cp; 64 } 65 66 /* 67 * parse all tuples in a line. we assume that the 68 * line ends in a '\n'. 69 * 70 * the tuples are linked as a list using ->entry and 71 * as a ring using ->line. 72 */ 73 Ndbtuple* 74 _ndbparseline(char *cp) 75 { 76 Ndbtuple *t; 77 Ndbtuple *first, *last; 78 79 first = last = 0; 80 while(*cp != '#' && *cp != '\n'){ 81 t = 0; 82 cp = _ndbparsetuple(cp, &t); 83 if(cp == 0) 84 break; 85 if(first){ 86 last->line = t; 87 last->entry = t; 88 } else 89 first = t; 90 last = t; 91 t->line = 0; 92 t->entry = 0; 93 } 94 if(first) 95 last->line = first; 96 return first; 97 } 98