commit 69bc11c0336c314b64477315db4cad848639f97b
parent c7cc51563ad5632ab840bf182345a8e72cc22237
Author: rpa <rpa@laika>
Date: Sun, 19 Feb 2023 22:56:21 +0000
keyval: add keyreg
Diffstat:
3 files changed, 60 insertions(+), 19 deletions(-)
diff --git a/src/keyval/cli.c b/src/keyval/cli.c
@@ -11,11 +11,13 @@ int i, fd;
char *opstr, *keystr;
Keyval kv;
+KVHeader *kh = (KVHeader *)(&kv);;
struct Dict { char *s; int op; };
struct Dict dict[] = {
{"get", KVGet},
{"set", KVSet},
+ {"keyreg", KVKeyreg},
{nil, 0},
};
@@ -49,7 +51,8 @@ main(int argc, char **argv)
if (argc == 0) usage();
keystr = argv[0];
-// argc--, argv++;
+ argc--;// argv++;
+ if (argc != 0) usage();
memcpy(kv.key, keystr, strlen(keystr));
@@ -58,12 +61,28 @@ main(int argc, char **argv)
kv.count = MetaSize;
write(fd, &kv, MetaSize);
read(fd, &kv, BufSize);
+ if (kv.count == 0) return;
write(1, kv.data, kv.count - MetaSize);
break;
case KVSet:
kv.count = MetaSize + read(0, kv.data, DataSize);
kv.count = write(fd, &kv, kv.count);
break;
+ case KVKeyreg:
+ kv.count = MetaSize + strlen(keystr);
+ memcpy(kv.data, keystr, kv.count - MetaSize);
+ write(fd, &kv, kv.count);
+ read(fd, &kv, BufSize);
+ keystr = mallocz(KeySize + 1, 1);
+ for (i = 0; i < 32; i++) {
+ if (kh[i].count == 0) break;
+ memcpy(keystr, kh[i].key, KeySize);
+ print("%s\n", keystr);
+ }
+ free(keystr);
+ break;
+ case KVValreg:
+ break;
default:
fprint(2, "bad op\n");
exits("bad op");
diff --git a/src/keyval/mkfile b/src/keyval/mkfile
@@ -4,3 +4,4 @@ TARG=srv cli
OFILES=
HFILES=keyval.h
</sys/src/cmd/mkmany
+
diff --git a/src/keyval/srv.c b/src/keyval/srv.c
@@ -37,7 +37,6 @@ Key *keyp;
char *serv = "keyval";
char *mnt = "/mnt/keyval";
-Reprog *reg;
Srv fs = {
.open = ctl_open,
.read = ctl_read,
@@ -101,39 +100,55 @@ setkey(Key **root, Keyval *kv)
return k;
}
-/*
-int
-keyreg(char *buf, Key *kp, Reprog *reg)
+void
+keyreg(Key *kp, Keyval *rv, Keyval *kv)
{
- 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);
+ int i;
+ KVHeader *kh = (KVHeader *) kv;
+ char *key = mallocz(KeySize + 1, 1);
+ char *buf = mallocz(rv->count - MetaSize + 1, 1);
+ memcpy(buf, rv->data, rv->count - MetaSize);
+ Reprog *reg = regcomp(buf);
+ free(buf);
+
+ for(i = 0; kp != nil; kp = kp->next) {
+ memcpy(key, kp->key, KeySize);
+ if (regexec(reg, key, nil, 0) == 1) {
+ if (i >= BufSize / MetaSize) break;
+ if (kp->count < MetaSize) continue;
+ memcpy(kh + i, kp, MetaSize);
+ i++;
}
}
- bp[-1] = '\0';
- return n;
+ kv->count = i * MetaSize;
+
+ free(reg);
+ free(key);
}
+/*********
int
-valreg(char *buf, Key *kp, Reprog *reg)
+valreg(Key *kp, Keyval *rv, Keyval *kv)
{
+ Reprog *reg;
+ char *buf = mallocz(rv->count - MetaSize + 1, 1);
+ memcpy(buf, rv->data, rv->count - MetaSize);
+ reg = regcomp(buf);
+ free(buf);
+
int n;
- char *bp = buf;
- bp += sprint(bp, "ok ");
+ char *bp = kv->data;
for(n = 0; kp != nil; kp = kp->next) {
- if (regexec(reg, kp->val.p, nil, 0) == 1) {
+ if (regexec(reg, kp->data, nil, 0) == 1) {
n++;
bp += snprint(bp, BufSize - (bp - buf), "%s ", kp->key);
}
}
bp[-1] = '\0';
+ free(reg);
return n;
}
-*/
+**********/
void ctl_read(Req *r)
{
@@ -172,6 +187,12 @@ void ctl_write(Req *r)
memcpy(faux, kv, kv->count);
}
break;
+ case KVKeyreg:
+ if (r->ifcall.count < MetaSize) goto badwrite;
+ keyreg(keyp, kv, faux);
+ break;
+ case KVValreg:
+ break;
default:
goto badwrite;
}