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