reality check from unix; simplify it!
This commit is contained in:
parent
6fa747864e
commit
5040da40fc
7 changed files with 79 additions and 78 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,2 +1,5 @@
|
||||||
kuro
|
kuro
|
||||||
kurosrvr
|
kurosrvr
|
||||||
|
o.kuro
|
||||||
|
6.out
|
||||||
|
*.o
|
22
9port.mkfile
Normal file
22
9port.mkfile
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
<$PLAN9/src/mkhdr
|
||||||
|
<|sh $PLAN9/src/cmd/devdraw/mkwsysrules.sh # for X11
|
||||||
|
TARG=kuro
|
||||||
|
|
||||||
|
OFILES=\
|
||||||
|
kuro.$O\
|
||||||
|
srv.$O\
|
||||||
|
client.$O\
|
||||||
|
util.$O\
|
||||||
|
node.$O\
|
||||||
|
nodetable.$O\
|
||||||
|
|
||||||
|
HFILES=dat.h\
|
||||||
|
fns.h\
|
||||||
|
|
||||||
|
UPDATE=\
|
||||||
|
mkfile\
|
||||||
|
$HFILES\
|
||||||
|
${OFILES:%.$O=%.c}\
|
||||||
|
|
||||||
|
<${PLAN9}/src/mkone
|
||||||
|
|
7
fns.h
7
fns.h
|
@ -1,8 +1,9 @@
|
||||||
int strequ(char* s1, char* s2);
|
int strequ(char* s1, char* s2);
|
||||||
char* strcsw(char* s, char a, char b);
|
char* strcsw(char* s, char a, char b);
|
||||||
char* uvlong_to_hex(uvlong input, char* buf);
|
char* uvlong_to_hex(uvlong input, char* buf);
|
||||||
|
int ensure(char* path);
|
||||||
|
|
||||||
void start_9p(char* mtpt);
|
void start_9p();
|
||||||
|
|
||||||
void kuro9p_set_mtpt(char* mtpt);
|
void kuro9p_set_mtpt(char* mtpt);
|
||||||
void kuro9p_write(char* path, char* data, int len);
|
void kuro9p_write(char* path, char* data, int len);
|
||||||
|
@ -11,10 +12,10 @@ char* kuro9p_read(char* path, char* buf, int len);
|
||||||
void nodetbl_add(NodeTable* self, NodeRef* node);
|
void nodetbl_add(NodeTable* self, NodeRef* node);
|
||||||
void nodetbl_del(NodeTable* self, uvlong id);
|
void nodetbl_del(NodeTable* self, uvlong id);
|
||||||
|
|
||||||
void node_setup(Node* self, uvlong id, int fd, Handler* handlers, KuroMemory* memory);
|
void node_setup(Node* self, Handler* handlers, KuroMemory* memory);
|
||||||
void node_execute(Node* self);
|
void node_execute(Node* self);
|
||||||
void node_loop(void* data);
|
void node_loop(void* data);
|
||||||
Node* create_node(uvlong id, int fd, int new, char* filename);
|
Node* create_node(char* filename);
|
||||||
void supervise_node(Node* self);
|
void supervise_node(Node* self);
|
||||||
void node_cleanup(Node* self);
|
void node_cleanup(Node* self);
|
||||||
void memory_cleanup(KuroMemory* self);
|
void memory_cleanup(KuroMemory* self);
|
56
kuro.c
56
kuro.c
|
@ -9,33 +9,6 @@ int threadmaybackground(void) {
|
||||||
|
|
||||||
void threadmain(int argc, char **argv)
|
void threadmain(int argc, char **argv)
|
||||||
{
|
{
|
||||||
/* we want to strip any path elements off the executable name */
|
|
||||||
char exe_name[256] = {0};
|
|
||||||
char* exe_base;
|
|
||||||
|
|
||||||
uvlong node_id = 0;
|
|
||||||
int client_fd = -1;
|
|
||||||
int newwin = 0;
|
|
||||||
|
|
||||||
char client_9p_str[256] = {0};
|
|
||||||
|
|
||||||
strcpy(exe_name, argv[0]);
|
|
||||||
exe_base = utfrrune(exe_name, '/');
|
|
||||||
if (exe_base) {
|
|
||||||
exe_base++;
|
|
||||||
} else {
|
|
||||||
exe_base = exe_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* if we are the server, serve the filetree on 9p */
|
|
||||||
|
|
||||||
if (strequ(exe_base, "kurosrvr")) {
|
|
||||||
if (argc > 1) {
|
|
||||||
start_9p(argv[1]);
|
|
||||||
} else {
|
|
||||||
sysfatal("usage: kurosrvr mtpt");
|
|
||||||
}
|
|
||||||
} else if (strequ(exe_base, "kuro")) {
|
|
||||||
mtpt = getenv("KURO_MTPT");
|
mtpt = getenv("KURO_MTPT");
|
||||||
if (mtpt == nil) {
|
if (mtpt == nil) {
|
||||||
sysfatal("KURO_MTPT not set");
|
sysfatal("KURO_MTPT not set");
|
||||||
|
@ -45,31 +18,10 @@ void threadmain(int argc, char **argv)
|
||||||
print("KURO_MTPT=%s\n", mtpt);
|
print("KURO_MTPT=%s\n", mtpt);
|
||||||
|
|
||||||
ARGBEGIN{
|
ARGBEGIN{
|
||||||
case 'n':
|
|
||||||
newwin=1;
|
|
||||||
break;
|
|
||||||
case 'i':
|
|
||||||
sscanf(ARGF(), "%d", &node_id);
|
|
||||||
break;
|
|
||||||
case 'p':
|
|
||||||
sscanf(ARGF(), "%d", &client_fd);
|
|
||||||
break;
|
|
||||||
}ARGEND
|
}ARGEND
|
||||||
|
|
||||||
strcpy(client_9p_str, "new");
|
threadcreate(start_9p, nil, 4096);
|
||||||
if (argv[0]) {
|
supervise_node(create_node(argc > 1 ? argv[0] : nil));
|
||||||
strcat(client_9p_str, " ");
|
threadexitsall(0);
|
||||||
strcat(client_9p_str, argv[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node_id > 0 && client_fd >= 0) {
|
|
||||||
supervise_node(create_node(node_id, client_fd, newwin, argv[0]));
|
|
||||||
} else {
|
|
||||||
/* if node_id or client_id are missing, ask the server to fork us with them, and forward the filename */
|
|
||||||
print("9pstr=%s\n", client_9p_str);
|
|
||||||
kuro9p_write("/ctl", client_9p_str, strlen(client_9p_str));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
sysfatal("invoke as kurosrvr to start the background service or kuro to start an application window");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
23
node.c
23
node.c
|
@ -2,10 +2,8 @@
|
||||||
#include "fns.h"
|
#include "fns.h"
|
||||||
|
|
||||||
|
|
||||||
void node_setup(Node* self, uvlong id, int fd, Handler* handlers, KuroMemory* memory) {
|
void node_setup(Node* self, Handler* handlers, KuroMemory* memory) {
|
||||||
if (self) {
|
if (self) {
|
||||||
self->id = id;
|
|
||||||
self->fd = fd;
|
|
||||||
self->handlers = handlers;
|
self->handlers = handlers;
|
||||||
self->memory = memory;
|
self->memory = memory;
|
||||||
self->cpu = chancreate(sizeof(Instruction), 0);
|
self->cpu = chancreate(sizeof(Instruction), 0);
|
||||||
|
@ -36,23 +34,22 @@ void node_execute(Node* self) {
|
||||||
threadcreate(node_loop, self, 1024);
|
threadcreate(node_loop, self, 1024);
|
||||||
}
|
}
|
||||||
|
|
||||||
Node* create_node(uvlong id, int fd, int new, char* filename) {
|
Node* create_node(char* filename) {
|
||||||
|
|
||||||
if (new) {
|
|
||||||
newwindow("-dx 600 -dy 600");
|
|
||||||
}
|
|
||||||
initdraw(nil, nil, "kuro");
|
initdraw(nil, nil, "kuro");
|
||||||
|
unlockdisplay(display);
|
||||||
|
|
||||||
KuroMemory* self = (KuroMemory*)malloc(sizeof(KuroMemory));
|
KuroMemory* self = (KuroMemory*)malloc(sizeof(KuroMemory));
|
||||||
Node* node = (Node*)malloc(sizeof(Node));
|
Node* node = (Node*)malloc(sizeof(Node));
|
||||||
Handler* handlers = (Handler*)malloc(TOTAL_OPCODES * sizeof(Handler*));
|
Handler* handlers = (Handler*)malloc(TOTAL_OPCODES * sizeof(Handler*));
|
||||||
|
|
||||||
node_setup(node, id, fd, handlers, self);
|
node_setup(node, handlers, self);
|
||||||
|
|
||||||
self->img = screen;
|
self->img = screen;
|
||||||
self->screen = _screen;
|
self->screen = _screen;
|
||||||
|
if (filename) {
|
||||||
strcpy(self->filepath, filename);
|
strcpy(self->filepath, filename);
|
||||||
|
}
|
||||||
|
|
||||||
/* node_execute runs the node on a separate thread */
|
/* node_execute runs the node on a separate thread */
|
||||||
node_execute(node);
|
node_execute(node);
|
||||||
|
@ -90,10 +87,8 @@ void supervise_node(Node* self) {
|
||||||
print("got a mouse event");
|
print("got a mouse event");
|
||||||
break;
|
break;
|
||||||
case PORT_RESIZE:
|
case PORT_RESIZE:
|
||||||
lockdisplay(display);
|
/*if (getwindow(display, Refnone) < 0)
|
||||||
if (getwindow(display, Refnone) < 0)
|
sysfatal("couldn't resize");*/
|
||||||
sysfatal("couldn't resize");
|
|
||||||
unlockdisplay(display);
|
|
||||||
print("got a resize event");
|
print("got a resize event");
|
||||||
break;
|
break;
|
||||||
case PORT_KBD:
|
case PORT_KBD:
|
||||||
|
|
28
srv.c
28
srv.c
|
@ -41,6 +41,7 @@ void kuro_read(Req* r) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void new_window(char* filename, int new) {
|
void new_window(char* filename, int new) {
|
||||||
int rw_fd[2];
|
int rw_fd[2];
|
||||||
char client_fd[32] = {0};
|
char client_fd[32] = {0};
|
||||||
|
@ -65,6 +66,11 @@ void new_window(char* filename, int new) {
|
||||||
exec("./kuro", a);
|
exec("./kuro", a);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void newwindowthread(void* arg) {
|
||||||
|
char* filename = (char*)arg;
|
||||||
|
new_window(filename, 1);
|
||||||
|
}
|
||||||
|
|
||||||
void write_ctl(Req* r) {
|
void write_ctl(Req* r) {
|
||||||
char cmd[16] = {0};
|
char cmd[16] = {0};
|
||||||
char* c = r->ifcall.data;
|
char* c = r->ifcall.data;
|
||||||
|
@ -87,7 +93,7 @@ void write_ctl(Req* r) {
|
||||||
print("arg: %s\n", c);
|
print("arg: %s\n", c);
|
||||||
|
|
||||||
if (strequ(cmd, "new")) {
|
if (strequ(cmd, "new")) {
|
||||||
new_window(c, 1);
|
threadcreate(newwindowthread, c, 256);
|
||||||
respond(r, nil);
|
respond(r, nil);
|
||||||
} else {
|
} else {
|
||||||
print("unknown command...\n");
|
print("unknown command...\n");
|
||||||
|
@ -107,16 +113,28 @@ void kuro_write(Req* r) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void start_9p(char* mtpt) {
|
char* mk_mtpt(char* parent, char* buf) {
|
||||||
if (mtpt) {
|
int pid = getpid();
|
||||||
putenv("KURO_MTPT", mtpt);
|
sprintf(buf, "%s/%d", parent, pid);
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
void start_9p() {
|
||||||
|
char* mtpt_base = getenv("KURO_MTPT");
|
||||||
|
if (mtpt_base) {
|
||||||
|
char mtpt[256] = "TESTING LIKE THIS SHOULD NOT BE NECESSARY; AYEYEYEYEYE";
|
||||||
|
|
||||||
|
mk_mtpt(mtpt_base, mtpt);
|
||||||
|
int i = ensure(mtpt);
|
||||||
|
print("%d result of ensure\n", i);
|
||||||
|
print("%s\n", mtpt);
|
||||||
Srv srv = { .read = kuro_read, .write = kuro_write };
|
Srv srv = { .read = kuro_read, .write = kuro_write };
|
||||||
Tree* tree = alloctree(nil, nil, DMDIR | 0777, fs_destroy_file);
|
Tree* tree = alloctree(nil, nil, DMDIR | 0777, fs_destroy_file);
|
||||||
srv.tree = tree;
|
srv.tree = tree;
|
||||||
closefile(createfile(tree->root, "ctl", nil, DMAPPEND | 0600, create_aux(CTL)));
|
closefile(createfile(tree->root, "ctl", nil, DMAPPEND | 0600, create_aux(CTL)));
|
||||||
closefile(createfile(tree->root, "nodes", nil, DMDIR | 0600, nil));
|
|
||||||
closefile(tree->root);
|
closefile(tree->root);
|
||||||
/* TODO: figure out how to kill the server cleanly so we don't need MREPL */
|
/* TODO: figure out how to kill the server cleanly so we don't need MREPL */
|
||||||
threadpostmountsrv(&srv, nil, mtpt, MREPL | MCREATE);
|
threadpostmountsrv(&srv, nil, mtpt, MREPL | MCREATE);
|
||||||
|
threadexits(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
12
util.c
12
util.c
|
@ -15,6 +15,16 @@ char* strcsw(char* s, char a, char b) {
|
||||||
}
|
}
|
||||||
|
|
||||||
char* uvlong_to_hex(uvlong input, char* buf) {
|
char* uvlong_to_hex(uvlong input, char* buf) {
|
||||||
sprintf(buf, "%08%x\0", input);
|
sprintf(buf, "%08x\0", input);
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ensure(char* path) {
|
||||||
|
int f = create(path, OREAD, DMDIR | 0777);
|
||||||
|
if (f < 0) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
close(f);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue