commit a81b3524bf756fdd5d00976abaa45c13363e7df8
parent d7f78577327e439fe62e595fd500fa49854f6554
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Thu, 22 Aug 2024 23:16:34 +0000
many things
Diffstat:
9 files changed, 186 insertions(+), 125 deletions(-)
diff --git a/src/fltp/fltp.c b/src/fltp/fltp.c
@@ -98,7 +98,6 @@ fltpendlist(FLTP *fltp){
void
fltpgetline(FLTP *fltp){
- FLTNode *new;
int c;
fltp->ti = 0;
for(;;){
@@ -114,8 +113,6 @@ fltpgetline(FLTP *fltp){
sysfatal("fltpgetline: input buffer overflow (unterminated token)");
}
}
- if (fltp->ti > 0) {
- new = createfltnode(fltp->np, fltp->np, fltp->tbuf, fltp->ti);
- fltp->np = new;
- }
+ // unlike fltpgettoken, we create new node even if the line is empty
+ fltp->np = createfltnode(fltp->np, fltp->np, fltp->tbuf, fltp->ti);
}
diff --git a/src/fltp/fltp.h b/src/fltp/fltp.h
@@ -1,4 +1,4 @@
-#pragma lib "libfltp.a$O"
+//#pragma lib "libfltp.a$O"
enum{
diff --git a/src/mkfile b/src/mkfile
@@ -7,17 +7,15 @@ TARG=\
HFILES=
OFILES=
+LIB=\
+ tablist/libtablist.a$O \
+ util/libutil.a$O \
+ fltp/libfltp.a$O \
</sys/src/cmd/mkmany
$O.list: list.$O binlist/liblist.a$O
-$O.wdb2fltp: wdb2fltp.$O
- bind -b tablist /$objtype/lib
- bind -b util /$objtype/lib
- lc /$objtype/lib/*a$O
- $LD $LDFLAGS -o $target $prereq
-
binlist/liblist.a$O:
cd binlist
mk
diff --git a/src/tablist/mkfile b/src/tablist/mkfile
@@ -13,3 +13,5 @@ mktest:V:
extract: extract.c
$CC extract.c
$LD -o extract extract.$O
+
+libtablist.a$O: ../util/libutil.a$O
diff --git a/src/tablist/tablist.h b/src/tablist/tablist.h
@@ -1,4 +1,4 @@
-#pragma lib "libtablist.a$O"
+//#pragma lib "libtablist.a$O"
enum {
TLstackdepth = 256,
diff --git a/src/util/util.h b/src/util/util.h
@@ -1,4 +1,4 @@
-#pragma lib "libutil.a$O"
+// #pragma lib "libutil.a$O"
typedef struct Type Type;
@@ -36,4 +36,4 @@ void *slicegetp(Slice *, int);
void freeslice(Slice *);
long voidlen(void **); /* strlen, but for array of pointers */
-char ** strfind(char **, long n, char *); /* find string in array of strings */
-\ No newline at end of file
+char ** strfind(char **, long n, char *); /* find string in array of strings */
diff --git a/src/wdb/mkfile b/src/wdb/mkfile
@@ -11,9 +11,13 @@ RC=\
ui
TARG=search update search-fltp
-</sys/src/cmd/mkmany
-# LIB=../util/libutil.a$O ../tablist/libtablist.a$O
+LIB=\
+ ../util/libutil.a$O\
+ ../tablist/libtablist.a$O\
+ ../fltp/libfltp.a$O
+
+</sys/src/cmd/mkmany
# $O.update: update.$O $LIB
@@ -24,7 +28,3 @@ installdir:V:
installrc:V: installdir
cp $RC $BIN
-
-$O.search-fltp:
- bind -b ../fltp /$objtype/lib
- $LD $LDFLAGS -o $target $prereq
diff --git a/src/wdb/search-fltp.c b/src/wdb/search-fltp.c
@@ -7,82 +7,176 @@ Biobuf *bout;
#include "../fltp/fltp.h"
+#define AFTER(X) ( (char *)X + sizeof(*X) )
+
enum{
MaxTags = 256,
BufSize = 4096,
ListSize = 256,
};
-char *buf;
-char *tagsbuf[MaxTags], *typebuf[MaxTags];
-FLTNode *listbuf[ListSize], *uuid, *name, *tags, *type;
+int tagsqn, typeqn;
+char *buf, *tagsquery[MaxTags + 1], *typequery[MaxTags + 1], *tagslist[MaxTags];
+FLTNode *uuid, *name, *tags, *type;
+Reprog *re;
FLTP *wdb;
void
-runtests(FLTP *fltp){
-
+node2buf(FLTNode *p)
+{
+ snprint(buf, BufSize, "%.*s", (int)p->nbytes, AFTER(p));
}
-void
-fltpnewendlist(FLTP *fltp){
- int i;
- char *s;
- FLTNode *np, *ls;
- fltpendlist(fltp);
- ls = fltp->np->parent;
- for(i = 0, np = ls->next; np != fltp->np; np = np->next){
- if(np->parent != ls) continue;
- if(i >= ListSize - 1)
- sysfatal("list buffer overflown");
- listbuf[i] = np;
- i++;
- }
- listbuf[i] = nil;
- if(i == 0) return;
- s = (char *)(listbuf[0]) + sizeof(FLTNode);
- snprint(buf, BufSize, "%.*s", (long)listbuf[0]->nbytes, s);
- if(listbuf[0]->prev->parent == nil){
- runtests(fltp);
- return;
- }
- if(strcmp(buf, "name") == 0){
- name = listbuf[0];
- return;
- }
- if(strcmp(buf, "tags") == 0){
- tags = listbuf[0];
- return;
+int
+_testtag(char **tags, int n, char *test){
+ char **p;
+ for(p = tags; p < tags + n; p++){
+ if (strcmp(test, *p) == 0) return 1;
}
- if(strcmp(buf, "type") == 0){
- type = listbuf[0];
- return;
+ return 0;
+}
+
+int
+_testtags(FLTNode *p){
+ int n, i, t;
+ node2buf(p);
+ t = tokenize(buf, tagslist, MaxTags);
+ n = 0;
+ for(i = 0; i < t; i++){
+ if (_testtag(tagsquery, tagsqn, tagslist[i]) != 0) n++;
}
+ return n;
}
-/*
+
int
-_testtag(char **tags, char *test)
-{
- for (; *tags != nil; tags++) {
- if (strcmp(test, *tags) == 0) return 1;
+_testtype(FLTNode *p){
+ int n, i, t;
+ node2buf(p);
+ t = tokenize(buf, tagslist, MaxTags);
+ n = 0;
+ for(i = 0; i < t; i++){
+ if (_testtag(typequery, typeqn, tagslist[i]) != 0) n++;
}
+ return n;
+}
+
+int
+_iselem(FLTNode *p){
+ fprint(2, "elem %.*s\n", (int)p->nbytes, AFTER(p));
+ return 0;
+}
+
+int
+_islist(FLTNode *p){
+ fprint(2, "list %.*s\n", (int)p->nbytes, AFTER(p));
return 0;
}
int
-_testtags(char **tags, char **test)
+_testnameelem(FLTNode *p)
{
- for (; *test != nil; test++) {
- if (_testtag(tags, *test) == 0) return 0;
+ node2buf(p);
+ return regexec(re, buf, nil, 0);
+}
+
+int
+_testnamelist(FLTNode *)
+{
+ node2buf(uuid);
+ fprint(2, "sublist in name field: %s\n", buf);
+ return 0;
+}
+
+int
+testlist(FLTNode *lp, int (*elemfp)(FLTNode *), int (*listfp)(FLTNode *)){
+ FLTNode *p, *root;
+ int r;
+ r = 0;
+ root = lp->parent;
+ for(p = lp->prev; p->prev != root; p = p->prev){
+ if(p->parent != root) continue;
+ if(p->next->parent == p) r += (listfp == nil) ? 0 : listfp(p);
+ else r += (elemfp == nil) ? 0 : elemfp(p);
}
- return 1;
+ return r;
+}
+
+int
+testname(FLTP *){
+ if (re == nil) return 1;
+ if(name == nil) sysfatal("name == nil");
+ return testlist(name, _testnameelem, _testnamelist);
+}
+
+int
+testtags(FLTP *){
+ int n;
+ if (tagsquery[0] == nil) return 1;
+ if(tags == nil) sysfatal("tags == nil");
+ n = testlist(tags, _testtags, nil);
+ return n >= tagsqn;
+}
+
+int
+testtype(FLTP *){
+ if (typequery[0] == nil) return 1;
+ if(type == nil) sysfatal("type == nil");
+ return testlist(type, _testtype, nil);
+}
+
+void
+runtests(FLTP *fltp){
+ if (testname(fltp) == 0) return;
+ if (testtags(fltp) == 0) return;
+ if (testtype(fltp) == 0) return;
+ print("%.*s\n", (int)(uuid->parent->next->nbytes), AFTER(uuid->parent->next));
}
-*/
+
+void
+fltpnewendlist(FLTP *fltp){
+ char *s;
+ FLTNode *ls;
+ fltpendlist(fltp);
+ ls = fltp->np->parent->next;
+ s = (char *)(ls) + sizeof(FLTNode);
+ snprint(buf, BufSize, "%.*s", (int)ls->nbytes, s);
+ if(ls->prev->parent == nil){
+ uuid = fltp->np;
+ runtests(fltp);
+ name = nil;
+ tags = nil;
+ type = nil;
+ return;
+ } else if(strcmp(buf, "name") == 0) name = fltp->np;
+ else if(strcmp(buf, "tags") == 0) tags = fltp->np;
+ else if(strcmp(buf, "type") == 0)type = fltp->np;
+}
+
+void
+fltpnewgetline(FLTP *fltp){
+ FLTNode *old, *new;
+ old = fltp->np;
+ fltpgetline(fltp);
+ new = fltp->np;
+
+ new->prev = old->prev;
+ new->parent = old->parent;
+ if(old->prev != nil) old->prev->next = new;
+
+ free(old);
+}
+
void
loadindex(void){
Biobuf *bfd;
- char *bp, *path = "/usr/rpa/lib/wdb/index/all";
+ char *bp, *path = "/usr/rpa/lib/wdb/index/fltp";
wdb = initfltp();
+ FLTWord nw[] = {
+ { &nw[1], "}", fltpnewendlist },
+ { wdb->wp, ".n", fltpnewgetline },
+ };
+ wdb->wp = nw;
bfd = Bopen(path, OREAD);
while((bp = Brdline(bfd, '\n')) != nil){
evalfltp(wdb, bp, Blinelen(bfd));
@@ -90,49 +184,6 @@ loadindex(void){
Bterm(bfd);
}
-int
-testname(FLTNode *n, Reprog *re){
- /* char *sp, *np = "name";
- FLTNode *vp;
- if (re == nil) return 1;
- vp = n->next;
- while(vp->parent !=
- sp = (char *)n + sizeof(FLTNode);
- if (n->nbytes != strlen(np)) return 1;
- if (strncmp(sp, np, n->nbytes) != 0) return 1;
- snprint(buf, "%.*s", n->nbytes;
- int r = regexec(re, str, nil, 0);
- free(str);
- return r; */
- return 1;
-}
-
-int
-testtags(FLTNode *n, char *field, char **tags){
- /* int i;
- char uuid[64], *str;
- if (tags[0] == nil) return 1;
- i = snprint(uuid, 64, "%.*s\n", n->name->len, n->name->p);
- uuid[i-1] = 0;
- TLnode *nfield = TLgetnode(n->children, field, nil);
- if (nfield == nil) {
- fprint(2, "can't get node %s→%s\n", uuid, field);
- return 0;
- }
- str = TLval2str(nfield);
- if (str == nil) {
- fprint(2, "can't get value %s→%s\n", uuid, field);
- return 0;
- }
- char *test[MaxTags];
- int N = tokenize(str, test, MaxTags);
- test[N] = nil;
- int r = _testtags(test, tags);
- free(str);
- return r; */
- return 1;
-}
-
void
usage(void){
fprint(2, "usage: %s [-t tags] [-T type] [query]\n", argv0);
@@ -141,18 +192,15 @@ usage(void){
void
main(int argc, char *argv[]){
- long i;
- Reprog *re;
char *t;
- FLTNode *np;
ARGBEGIN{
case 't':
t = EARGF(usage());
- tokenize(t, tagsbuf, MaxTags);
+ tagsqn = tokenize(t, tagsquery, MaxTags);
break;
case 'T':
t = EARGF(usage());
- tokenize(t, typebuf, MaxTags);
+ typeqn = tokenize(t, typequery, MaxTags);
break;
default:
usage();
@@ -160,10 +208,7 @@ main(int argc, char *argv[]){
if(argc > 1) usage();
if(argc == 1) re = regcomp(argv[0]);
else re = nil;
-
buf = malloc(BufSize);
-
bout = Bfdopen(1, OWRITE);
loadindex();
-
}
diff --git a/src/wdb2fltp.c b/src/wdb2fltp.c
@@ -7,7 +7,6 @@
#include <u.h>
#include <libc.h>
-// #include "fltp/fltp.h"
#include <util/util.h>
#include <tablist/tablist.h>
@@ -17,14 +16,35 @@ void printslice(Slice *);
void
printself(TLnode *node){
- if(node->name != nil) print("%.*s ", node->name->len, node->name->p);
- else print(".n %.*s\n", node->value->len, node->value->p);
+ int i;
+ char *buf;
+ if(node->name != nil){
+ buf = malloc(node->name->len + 1);
+ for (i = 0; i < node->name->len; i++) {
+ char *s = slicegetp(node->name, i);
+ buf[i] = *s;
+ }
+ buf[node->name->len] = '\0';
+ }
+ else{
+ buf = malloc(node->value->len + 1 + 3);
+ buf[0] = '.';
+ buf[1] = 'n';
+ buf[2] = ' ';
+ for (i = 0; i < node->value->len; i++) {
+ char *s = slicegetp(node->value, i);
+ buf[i + 3] = *s;
+ }
+ buf[node->value->len + 3] = '\0';
+ }
+ print("%s\n", buf);
+ free(buf);
}
void
printnode(TLnode *node){
if (node->children->len > 0){
- print("{ ");
+ print("{\n");
printself(node);
printslice(node->children);
print("}\n");