stew

a monorepo of some sort
Log | Files | Refs

commit a16b7b759d4b0ef34d40e73bb335f9b12576a294
parent d139e0cb9b2997316ddd0d29417d7b8e9e1f95d7
Author: rpa <rpa@grass>
Date:   Sun, 20 Aug 2023 17:12:58 +0000

src/tablist: extract tool

Diffstat:
Asrc/tablist/extract.c | 112+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/tablist/mkfile | 4++++
Msrc/tablist/tablist.c | 11+++++++++--
3 files changed, 125 insertions(+), 2 deletions(-)

diff --git a/src/tablist/extract.c b/src/tablist/extract.c @@ -0,0 +1,112 @@ +#include <u.h> +#include <libc.h> +#include <regexp.h> + +#include "../util/util.h" +#include "../tablist/tablist.h" + +Reprog **query; +int qcount; + +void +usage(void) +{ + fprint(2, "usage: %s query\n", argv0); + exits("usage"); +} + +int +fnoop(TLnode *, void *) +{ + return 1; +} + +int +fregex(TLnode *n, void *v) +{ + int ret; + char *buf; + Reprog *r = v; + Slice *data = n->name; + if (data == nil) data = n->value; + buf = malloc(data->len + 1); + memcpy(buf, data->p, data->len); + buf[data->len] = 0; + + ret = regexec(r, buf, nil, 0); + + free(buf); + + return ret; +} + +int +frecur(TLnode *n, void *v) +{ + int i, x = (int)v; + TLnode **c; + if (x >= qcount) { + return 1; + } + if (fregex(n, query[x]) == 0) { + return 0; + } + if (x + 1 == qcount) return 1; + for (i = 0; i < n->children->len; i++) { + c = slicegetp(n->children, i); + if (frecur(*c, (void *)(x + 1)) == 1) { + return 1; + } + } + return 0; +} + +Slice * +filter(Slice *in, int (*ffunc)(TLnode *, void *)) +{ + int i; + Slice *out; + TLnode **v, *n; + out = allocslice(&TLnodeType, 0, in->len); + for (i = 0; i < in->len; i++) { + v = slicegetp(in, i); + n = *v; + if (frecur(n, nil)) { + sliceappendp(out, 1, v); + } + } + return out; +} + +void +main(int argc, char **argv) +{ + char buf[8096]; + int i, n; + TLdecoder *dec; + TLencoder *enc; + Slice *out; + + ARGBEGIN { + default: + usage(); + } ARGEND + + qcount = argc; + query = malloc(sizeof(Reprog *) * qcount); + for (i = 0; i < qcount; i++) { + query[i] = regcompnl(argv[i]); + } + + dec = initTLdecoder(nil); + while ((n = read(0, buf, sizeof(buf))) > 0) { + TLdecode(dec, buf, n); + } + + out = filter(dec->nodes, fregex); + + enc = initTLencoder(out); + while ((n = TLencode(enc, buf, sizeof(buf))) > 0) { + write(1, buf, n); + } +} diff --git a/src/tablist/mkfile b/src/tablist/mkfile @@ -9,3 +9,7 @@ UPDATE= mktest:V: mk -f mktest + +extract: extract.c + $CC extract.c + $LD -o extract extract.$O diff --git a/src/tablist/tablist.c b/src/tablist/tablist.c @@ -161,7 +161,7 @@ TLdecode(TLdecoder *r, char *buf, usize count) /******* ENCODER *******/ typedef void * (TLencstate)(TLencoder *, char *); -TLencstate _encnode, _encname, _encvalue, _encnext, +TLencstate _encstart, _encnode, _encname, _encvalue, _encnext, _encnl, _encsp, _encind, _encend; @@ -172,11 +172,18 @@ initTLencoder(Slice *nodes) { TLencoder *w = mallocz(sizeof(TLencoder), 1); w->ns[0] = nodes; - w->state = _encnode; + w->state = _encstart; return w; } void * +_encstart(TLencoder *w, char *c) +{ + if (w->ns[0]->len == 0) return _encend(w, c); + return _encnode(w, c); +} + +void * _encnode(TLencoder *w, char *c) { TLnode *n = ENCTOS(w);