commit 693d6207f0e9d6629848a6a954ab11e89f524e0a
parent 13d753250ee5139e0e3f05fc004ad6966ae6c4bd
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Thu, 5 Aug 2021 17:03:21 +0000
imlement fs/ctl, implement clear command
Diffstat:
3 files changed, 58 insertions(+), 8 deletions(-)
diff --git a/fs.c b/fs.c
@@ -7,9 +7,48 @@
#include "richterm.h"
-File *new;
+File *new, *ctl;
Object *newobj;
+char *
+ctlcmd(char *buf)
+{
+ int n;
+ char *args[256];
+ n = tokenize(buf, args, 256);
+ if (n <= 0) return "expected a command";
+ if (strcmp(args[0], "test") == 0) return "test!";
+ if (strcmp(args[0], "clear") == 0) {
+ int i;
+ Object *obj;
+ for (i = 0; i < rich.count; i++) {
+ obj = rich.obj + i;
+ removefile(obj->ftext);
+ removefile(obj->ffont);
+ removefile(obj->flink);
+ removefile(obj->fimage);
+ removefile(obj->dir);
+ }
+
+ rich.count = 0;
+ rich.obj = realloc(rich.obj, 0);
+
+ return nil;
+ }
+ return "not implemented";
+}
+
+void
+ftree_destroy(File *f)
+{
+ Faux *aux;
+ if (f->aux == nil) return;
+ aux = f->aux;
+ free(aux->data->p);
+ free(aux->data);
+ free(aux);
+}
+
void
fs_open(Req *r)
{
@@ -26,7 +65,9 @@ fs_read(Req *r)
Faux *aux;
f = r->fid->file;
aux = f->aux;
- if (f == new) {
+ if (f == ctl) {
+ respond(r, "not implemented");
+ } else if (f == new) {
if (newobj != nil)
readstr(r, newobj->id);
respond(r, nil);
@@ -47,7 +88,14 @@ fs_write(Req *r)
Faux *aux;
f = r->fid->file;
aux = f->aux;
- if (f == new) {
+ if (f == ctl) {
+ char *ret, *buf;
+ buf = mallocz(r->ifcall.count + 1, 1);
+ memcpy(buf, r->ifcall.data, r->ifcall.count);
+ ret = ctlcmd(buf);
+ free(buf);
+ respond(r, ret);
+ } else if (f == new) {
respond(r, "not allowed");
} else if (aux != nil) {
qlock(rich.l);
@@ -63,9 +111,7 @@ fs_write(Req *r)
r->ofcall.count = r->ifcall.count;
respond(r, nil);
qunlock(rich.l);
- //generatepage(&rich);
redraw(1);
- /* there should be a call to redraw(), probably */
} else respond(r, "fs_write: f->aux is nil");
}
@@ -81,11 +127,13 @@ initfs(void)
newobj = nil;
fsctl = mallocz(sizeof(Fsctl), 1);
fsctl->c = chancreate(sizeof(int), 0);
- srv.tree = alloctree("richterm", "richterm", DMDIR|0555, nil);
+ srv.tree = alloctree("richterm", "richterm", DMDIR|0555, ftree_destroy);
if (srv.tree == nil) return nil;
fsctl->tree = srv.tree;
new = createfile(srv.tree->root, "new", "richterm", 0666, fsctl);
if (new == nil) return nil;
+ ctl = createfile(srv.tree->root, "ctl", "richterm", 0666, fsctl);
+ if (ctl == nil) return nil;
threadpostmountsrv(&srv, "richterm", "/mnt/richterm", MREPL);
return fsctl;
}
diff --git a/richterm.c b/richterm.c
@@ -188,7 +188,7 @@ mouse(Mouse mv, int mmode)
linkaux = nil;
if (view != nil) obj = view->obj;
if (obj != nil) linkaux = obj->flink->aux;
- if (linkaux->data->n > 0) {
+ if ((linkaux != nil) && (linkaux->data->n > 0)) {
Data dv;
dv.n = linkaux->data->n;
dv.p = malloc(linkaux->data->n);
@@ -405,9 +405,10 @@ newobject(Rich *rich)
Object *obj;
qlock(rich->l);
rich->count++;
+ rich->idcount++;
rich->obj = realloc(rich->obj, rich->count * sizeof(Object));
obj = &(rich->obj[rich->count - 1]);
- obj->id = smprint("%ld", rich->count);
+ obj->id = smprint("%ld", rich->idcount);
qunlock(rich->l);
return obj;
}
diff --git a/richterm.h b/richterm.h
@@ -65,6 +65,7 @@ struct Rich {
QLock *l;
Object *obj;
long count;
+ long idcount;
Page page;
};