stew

a monorepo of some sort
Log | Files | Refs

commit df30786a6cc4c3dc986fac071cebc27ddd89ebd4
parent ce8510fcd42ff72dddc14bc3530fb7e2d7b27f02
Author: rpa <rpa@laika>
Date:   Sat, 11 Feb 2023 19:49:53 +0000

wdb/srv: add regexp ops

Diffstat:
Msrc/wdb/srv.c | 54+++++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 51 insertions(+), 3 deletions(-)

diff --git a/src/wdb/srv.c b/src/wdb/srv.c @@ -1,5 +1,8 @@ #include <u.h> #include <libc.h> +#include <regexp.h> +// #include <fcall.h> +// #include <9p.h> enum { BufSize = 8192, @@ -23,8 +26,9 @@ struct Key { int fd, pd[2]; Key *keyp; -void srv(void); +void keyval(void); int eval(char *buf, int n); +Reprog *reg; void main(int, char **) @@ -42,13 +46,13 @@ main(int, char **) exits("fork"); break; case 0: - srv(); + keyval(); break; } } void -srv(void) +keyval(void) { int n; char buf[BufSize]; @@ -100,6 +104,38 @@ getkey(Key *root, char *key) } int +keyreg(char *buf, Key *kp, Reprog *reg) +{ + int n; + char *bp = buf; + bp += sprint(bp, "ok "); + for(n = 0; kp != nil; kp = kp->next) { + if (regexec(reg, kp->key, nil, 0) == 1) { + n++; + bp += snprint(bp, BufSize - (bp - buf), "%s ", kp->key); + } + } + bp[-1] = '\0'; + return n; +} + +int +valreg(char *buf, Key *kp, Reprog *reg) +{ + int n; + char *bp = buf; + bp += sprint(bp, "ok "); + for(n = 0; kp != nil; kp = kp->next) { + if (regexec(reg, kp->val.p, nil, 0) == 1) { + n++; + bp += snprint(bp, BufSize - (bp - buf), "%s ", kp->key); + } + } + bp[-1] = '\0'; + return n; +} + +int eval(char *buf, int n) { char *args[3], *act, *key, *data; @@ -125,5 +161,17 @@ eval(char *buf, int n) return sprint(buf, "ok %s %s", key, v->p); } } + if (strncmp(act, "valreg", 8) == 0) { + reg = regcompnl(args[1]); + if (reg == nil) return sprint(buf, "error bad regex"); + if (valreg(buf, keyp, reg) == 0) return sprint(buf, "nil"); + return strlen(buf); + } + if (strncmp(act, "keyreg", 8) == 0) { + reg = regcompnl(args[1]); + if (reg == nil) return sprint(buf, "error bad regex"); + if (keyreg(buf, keyp, reg) == 0) return sprint(buf, "nil"); + return strlen(buf); + } return sprint(buf, "error unknown command"); }