ndbquery: compatibility with ndb/query

Added the -m and -a flags to ndbquery as in Plan 9's ndb/query.
This commit is contained in:
gornorn 2024-08-06 14:36:25 +02:00 committed by GitHub
parent a2567fcac9
commit 89137059ed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,16 +3,26 @@
#include <bio.h> #include <bio.h>
#include <ndb.h> #include <ndb.h>
static int all, multiple;
/* /*
* search the database for matches * search the database for matches
*/ */
void void
usage(void) usage(void)
{ {
fprint(2, "usage: query attr value [returned attribute]\n"); fprint(2, "usage: ndbquery [-am] [-f ndbfile] attr value [returned attribute]\n");
exits("usage"); exits("usage");
} }
static void
prmatch(Ndbtuple *nt, char *rattr)
{
for(; nt; nt = nt->entry)
if(strcmp(nt->attr, rattr) == 0)
print("%s\n", nt->val);
}
void void
search(Ndb *db, char *attr, char *val, char *rattr) search(Ndb *db, char *attr, char *val, char *rattr)
{ {
@ -21,15 +31,30 @@ search(Ndb *db, char *attr, char *val, char *rattr)
Ndbtuple *nt; Ndbtuple *nt;
char *p; char *p;
if(rattr){ if(rattr && !all){
p = ndbgetvalue(db, &s, attr, val, rattr, nil); p = ndbgetvalue(db, &s, attr, val, rattr, &t);
if(p){ if(multiple)
prmatch(t, rattr);
else if(p){
print("%s\n", p); print("%s\n", p);
}
ndbfree(t);
free(p); free(p);
return;
}
/* all entries with matching rattrs */
if(rattr){
t = ndbsearch(db, &s, attr, val);
while(t != nil){
prmatch(t, rattr);
ndbfree(t);
t = ndbsnext(&s, attr, val);
} }
return; return;
} }
/* all entries */
t = ndbsearch(db, &s, attr, val); t = ndbsearch(db, &s, attr, val);
while(t){ while(t){
for(nt = t; nt; nt = nt->entry) for(nt = t; nt; nt = nt->entry)
@ -49,6 +74,12 @@ main(int argc, char **argv)
int reps = 1; int reps = 1;
ARGBEGIN{ ARGBEGIN{
case 'a':
all++;
break;
case 'm':
multiple++;
break;
case 'f': case 'f':
dbfile = ARGF(); dbfile = ARGF();
break; break;