commit 929b9460ccadc21e0b41ee5e787a11eb82319089
parent 64b6bd763ca22870b1abad0a6a2212d21768c530
Author: rpa <rpa@laika>
Date: Thu, 5 Jan 2023 15:20:13 +0000
wdb/search: rename from wdb and add tag filter
Diffstat:
M | src/wdb/mkfile | | | 4 | ++-- |
A | src/wdb/search.c | | | 147 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
D | src/wdb/wdb.c | | | 145 | ------------------------------------------------------------------------------- |
3 files changed, 149 insertions(+), 147 deletions(-)
diff --git a/src/wdb/mkfile b/src/wdb/mkfile
@@ -1,5 +1,5 @@
</$objtype/mkfile
-TARG=wdb
+TARG=search
HFILES=wdb.h util.h
-OFILES=wdb.$O util.$O
+OFILES=util.$O
</sys/src/cmd/mkmany
\ No newline at end of file
diff --git a/src/wdb/search.c b/src/wdb/search.c
@@ -0,0 +1,147 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include <regexp.h>
+
+Biobuf *bout;
+
+#include "wdb.h"
+#include "util.h"
+
+enum { MaxTags = 256, };
+
+WDB db;
+char *tags[MaxTags];
+
+int
+testtag(char **tags, char *test)
+{
+ for (; *tags != nil; tags++) {
+ if (strcmp(test, *tags) == 0) return 1;
+ }
+ return 0;
+}
+
+int
+testtags(char **tags, char **test)
+{
+ for (; *test != nil; test++) {
+ if (testtag(tags, *test) == 0) return 0;
+ }
+ return 1;
+}
+
+int
+testtagsstr(char **tags, char *str)
+{
+ char *test[MaxTags];
+ char *s;
+ int n, r;
+ if (tags[0] == nil) return 1;
+ if (str == nil) return 0;
+ s = strdup(str);
+ n = tokenize(s, test, MaxTags);
+ test[n] = nil;
+ r = testtags(test, tags);
+ free(s);
+ return r;
+}
+
+long
+wdbgrow(WDB *db)
+{
+ int i;
+ long j;
+ for (i = 0; i < WDBMax; i++) {
+ db->pp[i] = realloc(db->pp[i], sizeof(char *) * (db->n + 8));
+ for (j = db->n; j < db->n + 8; j++) db->pp[i][j] = nil;
+ }
+ db->n += 8;
+ return db->n - 8;
+}
+
+long
+wdbnewentry(WDB *db)
+{
+ long i;
+ for (i = 0; i < db->n; i++) if (db->pp[0][i] == 0) return i;
+ return wdbgrow(db);
+}
+
+void
+_loadindex(char *fname, int f)
+{
+ char *path = smprint("/usr/rpa/lib/wdb/index/%s", fname);
+ Biobuf *b = Bopen(path, OREAD);
+ free(path);
+ if (b == nil) sysfatal("%r");
+ char *s;
+ while((s = Brdstr(b, '\n', 1)) != nil) {
+ long n;
+ char *fields[2];
+ getfields(s, fields, 2, 1, "\t");
+ char **e = strfind(db.pp[WDBId], db.n, fields[0]);
+ if (e == nil) {
+ n = wdbnewentry(&db);
+ db.pp[WDBId][n] = strdup(fields[0]);
+ } else {
+ n = e - db.pp[WDBId];
+ }
+ if (db.pp[f][n] == nil) db.pp[f][n] = strdup(fields[1]);
+ else {
+ char *ss = smprint("%s\n%s", db.pp[f][n], fields[1]);
+ free(db.pp[f][n]);
+ db.pp[f][n] = ss;
+ }
+ free(s);
+ }
+ Bterm(b);
+ Bflush(bout);
+}
+
+void
+loadindex(void)
+{
+ _loadindex("name", WDBName);
+ _loadindex("tags", WDBTags);
+ _loadindex("type", WDBType);
+
+}
+
+void
+usage(void)
+{
+ fprint(2, "usage: %s [-t tags] [query]\n", argv0);
+ exits("usage");
+}
+
+void
+main(int argc, char *argv[])
+{
+ Reprog *re;
+ char *t;
+ ARGBEGIN {
+ case 't':
+ t = EARGF(usage());
+ tokenize(t, tags, MaxTags);
+ break;
+ default:
+ usage();
+ } ARGEND
+ if (argc > 1) usage();
+ if (argc == 1) re = regcomp(argv[0]);
+ else re = regcomp(".*");
+
+ bout = Bfdopen(1, OWRITE);
+ loadindex();
+ long i;
+ for (i = 0; i < db.n; i++) {
+ char *name = db.pp[WDBName][i];
+ if (name == nil) break;
+ if (
+ (regexec(re, name, nil, 0) != 0) &&
+ (testtagsstr(tags, db.pp[WDBTags][i]) != 0)) {
+ Bprint(bout, "%s\n", db.pp[WDBId][i]);
+ }
+ }
+}
diff --git a/src/wdb/wdb.c b/src/wdb/wdb.c
@@ -1,145 +0,0 @@
-#include <u.h>
-#include <libc.h>
-#include <bio.h>
-#include <regexp.h>
-
-Biobuf *bout;
-
-#include "wdb.h"
-#include "util.h"
-
-enum { MaxTags = 256, };
-
-WDB db;
-char *tags[MaxTags];
-
-int
-testtag(char **tags, char *test)
-{
- for (; *tags != nil; tags++) {
- if (strcmp(test, *tags) == 0) return 1;
- }
- return 0;
-}
-
-int
-testtags(char **tags, char **test)
-{
- for (; *test != nil; test++) {
- if (testtag(tags, *test) == 0) return 0;
- }
- return 1;
-}
-
-int
-testtagsstr(char **tags, char *str)
-{
- char *test[MaxTags];
- char *s;
- int n, r;
- s = strdup(str);
- n = tokenize(s, test, MaxTags);
- test[n] = nil;
- r = testtags(test, tags);
- free(s);
- return r;
-}
-
-long
-wdbgrow(WDB *db)
-{
- int i;
- long j;
- for (i = 0; i < WDBMax; i++) {
- db->pp[i] = realloc(db->pp[i], sizeof(char *) * (db->n + 8));
- for (j = db->n; j < db->n + 8; j++) db->pp[i][j] = nil;
- }
- db->n += 8;
- return db->n - 8;
-}
-
-long
-wdbnewentry(WDB *db)
-{
- long i;
- for (i = 0; i < db->n; i++) if (db->pp[0][i] == 0) return i;
- return wdbgrow(db);
-}
-
-void
-_loadindex(char *fname, int f)
-{
- char *path = smprint("/usr/rpa/lib/wdb/index/%s", fname);
- Biobuf *b = Bopen(path, OREAD);
- free(path);
- if (b == nil) sysfatal("%r");
- char *s;
- while((s = Brdstr(b, '\n', 1)) != nil) {
- long n;
- char *fields[2];
- getfields(s, fields, 2, 1, "\t");
- char **e = strfind(db.pp[WDBId], db.n, fields[0]);
- if (e == nil) {
- n = wdbnewentry(&db);
- db.pp[WDBId][n] = strdup(fields[0]);
- } else {
- n = e - db.pp[WDBId];
- }
- if (db.pp[f][n] == nil) db.pp[f][n] = strdup(fields[1]);
- else {
- char *ss = smprint("%s\n%s", db.pp[f][n], fields[1]);
- free(db.pp[f][n]);
- db.pp[f][n] = ss;
- }
- free(s);
- }
- Bterm(b);
- Bflush(bout);
-}
-
-void
-loadindex(void)
-{
- _loadindex("name", WDBName);
- _loadindex("tags", WDBTags);
- _loadindex("type", WDBType);
-
-}
-
-void
-usage(void)
-{
- fprint(2, "usage: %s [query]\n", argv0);
- exits("usage");
-}
-
-void
-main(int argc, char *argv[])
-{
- Reprog *re;
- char *t;
- ARGBEGIN {
- case 't':
- t = EARGF(usage());
- tokenize(t, tags, MaxTags);
- break;
- default:
- usage();
- } ARGEND
- if (argc > 1) usage();
- if (argc == 1) re = regcomp(argv[0]);
- else re = regcomp(".*");
-
- bout = Bfdopen(1, OWRITE);
- loadindex();
- long i;
- for (i = 0; i < db.n; i++) {
- int rn;
- char *name = db.pp[WDBName][i];
- if (name == nil) break;
- rn = regexec(re, name, nil, 0);
- if (rn != 0) {
- Bprint(bout, "%s\n", db.pp[WDBId][i]);
- }
- }
-}