strace: Fix issues with a few syscalls
[akaros.git] / tests / query.c
1 /*
2  *  search the network database for matches
3  */
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <parlib/parlib.h>
7 #include <unistd.h>
8 #include <signal.h>
9 #include <fcntl.h>
10 #include <error.h>
11 #include <iplib/iplib.h>
12 #include <ndblib/ndb.h>
13
14 static int all, multiple;
15
16 char *argv0;
17 #define ARGBEGIN        for((argv0||(argv0=*argv)),argv++,argc--;\
18                             argv[0] && argv[0][0]=='-' && argv[0][1];\
19                             argc--, argv++) {\
20                                 char *_args, *_argt;\
21                                 char _argc;             \
22                                 _args = &argv[0][1];\
23                                 if(_args[0]=='-' && _args[1]==0){\
24                                         argc--; argv++; break;\
25                                 }\
26                                 _argc = _args[0];\
27                                 while(*_args && _args++)\
28                                         switch(_argc)
29 #define ARGEND          /*SET(_argt);USED(_argt,_argc,_args);}USED(argv, argc);*/}
30 #define ARGF()          (_argt=_args, _args="",\
31                                 (*_argt? _argt: argv[1]? (argc--, *++argv): 0))
32 #define EARGF(x)        (_argt=_args, _args="",\
33                                 (*_argt? _argt: argv[1]? (argc--, *++argv): ((x), abort(), (char*)0)))
34
35 #define ARGC()          _argc
36
37 void
38 usage(void)
39 {
40         fprintf(stderr, "usage: query [-am] [-f ndbfile] attr value "
41                 "[returned-attr [reps]]\n");
42         fprintf(stderr, "usage");
43         exit(1);
44 }
45
46 /* print values of nt's attributes matching rattr */
47 static void
48 prmatch(struct ndbtuple *nt, char *rattr)
49 {
50         for(; nt; nt = nt->entry)
51                 if (strcmp(nt->attr, rattr) == 0)
52                         printf("%s\n", nt->val);
53 }
54
55 void
56 search(struct ndb *db, char *attr, char *val, char *rattr)
57 {
58         char *p;
59         struct ndbs s;
60         struct ndbtuple *t, *nt;
61
62         /* first entry with a matching rattr */
63         if(rattr && !all){
64                 p = ndbgetvalue(db, &s, attr, val, rattr, &t);
65                 if (multiple)
66                         prmatch(t, rattr);
67                 else if(p)
68                         printf("%s\n", p);
69                 ndbfree(t);
70                 free(p);
71                 return;
72         }
73
74         /* all entries with matching rattrs */
75         if(rattr) {
76                 for(t = ndbsearch(db, &s, attr, val); t != NULL;
77                     t = ndbsnext(&s, attr, val)){
78                         prmatch(t, rattr);
79                         ndbfree(t);
80                 }
81                 return;
82         }
83
84         /* all entries */
85         for(t = ndbsearch(db, &s, attr, val); t; t = ndbsnext(&s, attr, val)){
86                 for(nt = t; nt; nt = nt->entry)
87                         printf("%s=%s ", nt->attr, nt->val);
88                 printf("\n");
89                 ndbfree(t);
90         }
91 }
92
93 void
94 main(int argc, char **argv)
95 {
96         int reps = 1;
97         char *rattr = NULL, *dbfile = NULL;
98         struct ndb *db;
99         
100         ARGBEGIN{
101         case 'a':
102                 all++;
103                 break;
104         case 'm':
105                 multiple++;
106                 break;
107         case 'f':
108                 dbfile = EARGF(usage());
109                 break;
110         default:
111                 usage();
112         }ARGEND;
113
114         switch(argc){
115         case 4:
116                 reps = atoi(argv[3]);   /* wtf use is this? */
117                 /* fall through */
118         case 3:
119                 rattr = argv[2];
120                 break;
121         case 2:
122                 rattr = NULL;
123                 break;
124         default:
125                 usage();
126         }
127
128         db = ndbopen(dbfile);
129         if(db == NULL){
130                 error(1, 0, "no db: %r");
131         }
132         while(reps--)
133                 search(db, argv[0], argv[1], rattr);
134         ndbclose(db);
135 }
136