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