commit b8639014820b2d93ed06319f304d16b522385bc6
parent fd9efb21290105057ee7a26134070ae0130cdc7a
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Thu, 26 Aug 2021 18:32:15 +0000
absorb devfs into fs and bind fs onto /dev the way acme and rio do it
Diffstat:
D | devfs.c | | | 75 | --------------------------------------------------------------------------- |
M | fs.c | | | 104 | +++++++++++++++++++++++++++++++++++++++++++++++-------------------------------- |
M | mkfile | | | 2 | +- |
M | richterm.c | | | 41 | +++++++++++++++++------------------------ |
M | richterm.h | | | 50 | ++++++++++++++------------------------------------ |
5 files changed, 94 insertions(+), 178 deletions(-)
diff --git a/devfs.c b/devfs.c
@@ -1,75 +0,0 @@
-#include <u.h>
-#include <libc.h>
-#include <fcall.h>
-#include <thread.h>
-#include <draw.h>
-#include <9p.h>
-
-#include "array.h"
-#include "richterm.h"
-
-File *cons, *consctl, *text;
-
-void
-devfs_read(Req *r)
-{
- File *f;
- Devfsctl *dctl;
- Array *dv;
- f = r->fid->file;
- dctl = f->aux;
- if (f == cons) {
- recv(dctl->rc, &dv);
- r->ofcall.count = dv->count;
- memcpy(r->ofcall.data, dv->p, dv->count);
- arrayfree(dv);
- respond(r, nil);
- } else if (f == consctl) {
- respond(r, "not implemented");
- }else if (f == text) {
- arrayread(r, rich.text);
- respond(r, nil);
- } else respond(r, "what");
-}
-
-void
-devfs_write(Req *r)
-{
- File *f;
- f = r->fid->file;
- if (f == cons){
- Array *a;
- a = arraycreate(sizeof(char), r->ifcall.count, nil);
- arraygrow(a, r->ifcall.count, r->ifcall.data);
- send(insertc, &a);
-
- r->ofcall.count = r->ifcall.count;
-
- respond(r, nil);
- } else if (f == consctl) {
- respond(r, "not implemented");
- } else respond(r, "what");
-}
-
-Devfsctl *
-initdevfs(void)
-{
- Devfsctl *dctl;
- static Srv srv = {
- .read = devfs_read,
- .write = devfs_write,
- };
- dctl = mallocz(sizeof(Devfsctl), 1);
-
- dctl->wc = chancreate(sizeof(char *), 0);
- dctl->rc = chancreate(sizeof(Array *), 1024);
- srv.tree = alloctree("richterm", "richterm", DMDIR|0555, nil);
- if (srv.tree == nil) return nil;
- cons = createfile(srv.tree->root, "cons", "richterm", 0666, dctl);
- if (cons == nil) return nil;
- consctl = createfile(srv.tree->root, "consctl", "richterm", 0666, dctl);
- if (consctl == nil) return nil;
- text = createfile(srv.tree->root, "text", "richterm", 0444, dctl);
- threadpostmountsrv(&srv, nil, "/dev", MBEFORE);
- return dctl;
-}
diff --git a/fs.c b/fs.c
@@ -8,35 +8,38 @@
#include "array.h"
#include "richterm.h"
-File *new, *ctl;
+File *new, *ctl, *text, *cons, *consctl;
Object *newobj;
+File *fsroot;
+Channel *consc;
void fs_open(Req *);
void fs_read(Req *);
void fs_write(Req *);
void ftree_destroy(File *);
-Fsctl *
+int
initfs(char *srvname)
{
- Fsctl *fsctl;
static Srv srv = {
.open = fs_open,
.read = fs_read,
.write = fs_write,
};
newobj = nil;
- fsctl = mallocz(sizeof(Fsctl), 1);
- fsctl->c = chancreate(sizeof(int), 0);
+ consc = chancreate(sizeof(Array *), 1024);
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;
+ fsroot = srv.tree->root;
+ new = createfile(fsroot, "new", "richterm", 0666, nil);
+ ctl = createfile(fsroot, "ctl", "richterm", 0666, nil);
+ text = createfile(fsroot, "text", "richterm", 0444,
+ fauxalloc(nil, rich.text, arrayread, nil));
+ cons = createfile(fsroot, "cons", "richterm", 0666,
+ fauxalloc(nil, nil, consread, conswrite));
+ consctl = createfile(fsroot, "consctl", "richterm", 0666,
+ fauxalloc(nil, nil, nil, nil));
threadpostmountsrv(&srv, srvname, "/mnt/richterm", MREPL);
- return fsctl;
+ return 0;
}
char *
@@ -97,23 +100,15 @@ ftree_destroy(File *f)
void
fs_open(Req *r)
{
- Fsctl *fsctl;
- File *f;
-
- fsctl = new->aux;
- f = r->fid->file;
-
- if (f == new) {
+ if (r->fid->file == new) {
newobj = objectcreate();
- mkobjectftree(newobj, fsctl->tree->root);
+ mkobjectftree(newobj, fsroot);
objinsertbeforelast(newobj);
/* Because our newobj is created empty, there's no need
to move text from olast around. */
}
-
respond(r, nil);
-
}
void
@@ -130,39 +125,45 @@ fs_read(Req *r)
readstr(r, newobj->id);
respond(r, nil);
} else if (aux != nil) {
- aux->read(r, aux->data);
- respond(r, nil);
+ char *s;
+ s = nil;
+ if (aux->read != nil) aux->read(r);
+ else s = "no read";
+ respond(r, s);
} else respond(r, "fs_read: f->aux is nil");
}
void
fs_write(Req *r)
{
- File *f;
Faux *aux;
- f = r->fid->file;
- aux = f->aux;
- if (f == ctl) {
+ aux = r->fid->file->aux;
+ if (r->fid->file == 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) {
+ } else if (r->fid->file == new) {
respond(r, "not allowed");
} else if (aux != nil) {
- aux->write(r, aux->data);
- respond(r, nil);
- nbsend(redrawc, &aux->obj);
+ char *s;
+ s = nil;
+ if (aux->write != nil) {
+ aux->write(r);
+ nbsend(redrawc, &aux->obj);
+ }
+ else s = "no write";
+ respond(r, s);
} else respond(r, "fs_write: f->aux is nil");
}
void
-arrayread(Req *r, void *v)
+arrayread(Req *r)
{
Array *data;
- data = v;
+ data = ((Faux *)r->fid->file->aux)->data;
qlock(rich.l);
qlock(data->l);
readbuf(r, data->p, data->count);
@@ -171,10 +172,10 @@ arrayread(Req *r, void *v)
}
void
-arraywrite(Req *r, void *v)
+arraywrite(Req *r)
{
Array *data;
- data = v;
+ data = ((Faux *)r->fid->file->aux)->data;
qlock(rich.l);
data->count = 0;
arraygrow(data, r->ifcall.count, r->ifcall.data);
@@ -183,7 +184,7 @@ arraywrite(Req *r, void *v)
}
void
-textread(Req *r, void *)
+textread(Req *r)
{
Faux *aux;
Object *obj, *oe;
@@ -209,7 +210,7 @@ textread(Req *r, void *)
}
void
-textwrite(Req *r, void *)
+textwrite(Req *r)
{
/* TODO: this is not exactly finished */
/* in particular TRUNK/APPEND handling is needed */
@@ -237,7 +238,7 @@ textwrite(Req *r, void *)
}
void
-fontread(Req *r, void *)
+fontread(Req *r)
{
Faux *aux;
qlock(rich.l);
@@ -247,7 +248,7 @@ fontread(Req *r, void *)
}
void
-fontwrite(Req *r, void *)
+fontwrite(Req *r)
{
char buf[4096], *bp;
Faux *aux;
@@ -264,4 +265,24 @@ fontwrite(Req *r, void *)
aux->obj->font = getfont(fonts, bp);
qunlock(rich.l);
-}
-\ No newline at end of file
+}
+
+void
+consread(Req *r)
+{
+ Array *dv;
+ recv(consc, &dv);
+ r->ofcall.count = dv->count;
+ memcpy(r->ofcall.data, dv->p, dv->count);
+ arrayfree(dv);
+}
+
+void
+conswrite(Req *r)
+{
+ Array *a;
+ a = arraycreate(sizeof(char), r->ifcall.count, nil);
+ arraygrow(a, r->ifcall.count, r->ifcall.data);
+ send(insertc, &a);
+ r->ofcall.count = r->ifcall.count;
+}
diff --git a/mkfile b/mkfile
@@ -1,7 +1,7 @@
</$objtype/mkfile
TARG=richterm
-OFILES=richterm.$O devfs.$O fs.$O array.$O
+OFILES=richterm.$O fs.$O array.$O
HFILES=richterm.h array.h
</sys/src/cmd/mkone
diff --git a/richterm.c b/richterm.c
@@ -26,8 +26,6 @@ int hostpid = -1;
Channel *pidchan, *redrawc, *insertc;
Mousectl *mctl;
Keyboardctl *kctl;
-Devfsctl *dctl;
-Fsctl *fsctl;
Array *fonts;
Image *Iscrollbar, *Ilink, *Inormbg, *Iselbg, *Itext;
@@ -184,7 +182,7 @@ threadmain(int argc, char **argv)
break;
case INSERT:
obj = objectcreate();
- mkobjectftree(obj, fsctl->tree->root);
+ mkobjectftree(obj, fsroot);
objinsertbeforelast(obj);
objsettext(obj, arrayget(av, 0, nil), av->count);
arrayfree(av);
@@ -246,14 +244,14 @@ threadmain(int argc, char **argv)
arraygrow(dv, rich.text->count - olast->offset,
arrayget(rich.text, olast->offset, nil));
- nbsend(dctl->rc, &dv);
+ nbsend(consc, &dv);
/* dv is freed on recv end */
qunlock(rich.l);
obj = objectcreate();
- mkobjectftree(obj, fsctl->tree->root);
+ mkobjectftree(obj, fsroot);
objinsertbeforelast(obj);
olast->offset = rich.text->count;
}
@@ -393,11 +391,11 @@ scroll(Point p, Rich *r)
}
Faux *
-fauxalloc(Object *obj, Array *data, int type)
+fauxalloc(Object *obj, Array *data, void (*read)(Req *), void (*write)(Req *))
{
Faux *aux;
aux = mallocz(sizeof(Faux), 1);
- *aux = (Faux) {type, obj, data, arrayread, arraywrite};
+ *aux = (Faux) {obj, data, read, write};
return aux;
}
@@ -446,27 +444,22 @@ rmobjectftree(Object *obj)
Object *
mkobjectftree(Object *obj, File *root)
{
- Faux *auxtext, *auxfont, *auxlink, *auximage;
-
obj->id = smprint("%ulld", ++rich.idcount);
obj->dir = createfile(root, obj->id, "richterm", DMDIR|0555, nil);
- auxtext = fauxalloc(obj, nil, FT_TEXT);
- auxfont = fauxalloc(obj, nil, FT_FONT);
- auxlink = fauxalloc(obj, obj->dlink, FT_LINK);
- auximage = fauxalloc(obj, obj->dimage, FT_IMAGE);
- auxtext->read = textread;
- auxtext->write = textwrite;
+ obj->ftext = createfile(obj->dir, "text", "richterm", 0666,
+ fauxalloc(obj, nil, textread, textwrite));
+
+ obj->ffont = createfile(obj->dir, "font", "richterm", 0666,
+ fauxalloc(obj, nil, fontread, fontwrite));
- auxfont->read = fontread;
- auxfont->write = fontwrite;
+ obj->flink = createfile(obj->dir, "link", "richterm", 0666,
+ fauxalloc(obj, obj->dlink, arrayread, arraywrite));
- obj->ftext = createfile(obj->dir, "text", "richterm", 0666, auxtext);
- obj->ffont = createfile(obj->dir, "font", "richterm", 0666, auxfont);
- obj->flink = createfile(obj->dir, "link", "richterm", 0666, auxlink);
- obj->fimage = createfile(obj->dir, "image", "richterm", 0666, auximage);
+ obj->fimage = createfile(obj->dir, "image", "richterm", 0666,
+ fauxalloc(obj, obj->dimage, arrayread, arraywrite));
return obj;
}
@@ -498,11 +491,11 @@ runcmd(void *args)
rfork(RFNAMEG);
- if ((dctl = initdevfs()) == nil)
- sysfatal("initdevfs failed: %r");
- if ((fsctl = initfs(srvname)) == nil)
+ if ((initfs(srvname)) != 0)
sysfatal("initfs failed: %r");
+ bind("/mnt/richterm", "/dev/", MBEFORE);
+
rfork(RFFDG);
close(0);
open("/dev/cons", OREAD);
diff --git a/richterm.h b/richterm.h
@@ -1,5 +1,7 @@
extern Channel *redrawc;
extern Channel *insertc;
+extern Channel *consc;
+extern File *fsroot;
void drawscrollbar(void);
@@ -70,48 +72,24 @@ extern Rich rich;
void drawpage(Image *, Rich *);
void generatepage(Rich *, long);
-typedef struct Devfsctl Devfsctl;
-
-struct Devfsctl {
- Channel *rc;
- Channel *wc;
-};
-
-Devfsctl * initdevfs(void);
-
-typedef struct Fsctl Fsctl;
-
-struct Fsctl {
- Channel *c;
- Tree *tree;
-};
-
-extern Fsctl *fsctl;
-
-Fsctl * initfs(char *);
+int initfs(char *);
typedef struct Faux Faux;
struct Faux {
- int type;
Object *obj;
Array *data;
- void (*read)(Req *, void *);
- void (*write)(Req *, void *);
-};
-
-enum {
- FT_TEXT,
- FT_FONT,
- FT_LINK,
- FT_IMAGE
+ void (*read)(Req *);
+ void (*write)(Req *);
};
-Faux * fauxalloc(Object *, Array *, int);
+Faux * fauxalloc(Object *, Array *, void (*)(Req *), void (*)(Req *));
-void textread(Req *, void *);
-void textwrite(Req *, void *);
-void arrayread(Req *, void *);
-void arraywrite(Req *, void *);
-void fontread(Req *, void *);
-void fontwrite(Req *, void *);
+void textread(Req *);
+void textwrite(Req *);
+void arrayread(Req *);
+void arraywrite(Req *);
+void fontread(Req *);
+void fontwrite(Req *);
+void consread(Req *);
+void conswrite(Req *);