ref: ecdf17034b03e4a866651dba39ac4b5206f4a460
parent: 35f93cec31d91dc9c68911fc0ac658eb67363af4
author: halfwit <michaelmisch1985@gmail.com>
date: Wed Feb 28 17:05:51 PST 2024
Update a bit more userland code
--- a/TODO
+++ b/TODO
@@ -1,8 +1,11 @@
BUILTINS:
- - [x]' read' ?
- - [x] 'write' ?
+ - [x]' read'
+ - [ ] 'write' ?
+ - [x] 'ls'
- [x] 'mount'
- - [x] 'bind', might need a device driver or header-exposed syscall See how bind (2) is coded
+ - [x] 'bind'
+ - [ ] 'mkdir'
+ - [ ] 'rm'
- [ ] 'unmount'
- [ ] 'ns'
@@ -13,9 +16,7 @@
- [x] Move rc code to use our libc.h, u.h everywhere so our defs of open + such are correct
- [ ] gui-cocoa try to capture the windows we create so we can send mouse/keyboard to it, plist for launchd
- [ ] gui-wl cannibalize the wayland shims
- - [ ] create namespaces with our bind/mount and allow report via ns(1)
- [ ] define the gui interface
- - [ ] make sure we chroot programs so the stdio and opens are correctly sent to our kernel devices
- [x] Makefile librc --> librc.a
- [x] libc.h getwd plan9port/src/lib9/getwd.c
- [x] libc.h dirread /sys/src/libc/9sys/dirread.c
@@ -32,7 +33,7 @@
- devkbd - #b: !needed, We use the client exported /dev/kbd instead
- devmouse - #m: !needed, We use the client exported /dev/mouse instead
- devaudio - #A: !needed, We use the client exported /dev/audio instead
- - devcmd - #C: needed OS(1) commands *outside* namespace
+ - devcmd - #C: !needed OS(1) commands, we already run on host
- devcons - #c: needed, /dev/cons
- devenv - #e: needed, /dev/env
- devfs - #U: needed, local files
@@ -39,9 +40,8 @@
- devip - #I: !needed, networking
- devlfd - #L: !needed
- devmnt - #M: needed, client shares
- - devpipe - #|: !needed for pipe in rc
+ - devpipe - #|: ?needed for pipe in rc
- devroot - #/: needed, base of stack
- devssl - #D: !needed, ssl
- devtls - #a: !needed, tls
- - devfd - #f: needed, host fds in our kernelspace
- devtab - meta, needed - add/remove any devices that we add/remove, from here as well
--- a/include/user.h
+++ b/include/user.h
@@ -62,6 +62,7 @@
extern int dirwstat(char*, Dir*);
extern int dirfwstat(int, Dir*);
extern long dirread(int, Dir**);
+extern long dirreadall(int, Dir**);
extern ulong iounit(int);
extern int lfdfd(int);
--- a/rc/drawcpu.c
+++ b/rc/drawcpu.c
@@ -29,6 +29,7 @@
"bind", execbind,
"mount", execmount,
"unmount", execunmount,
+ "ls", execls,
0
};
--- a/rc/exec.h
+++ b/rc/exec.h
@@ -74,7 +74,7 @@
};
extern void (*builtinfunc(char *name))(void);
-void execread(void), execns(void);
+void execread(void), execns(void), execls(void);
void execbind(void), execmount(void), execunmount(void);
void execcd(void), execwhatis(void), execeval(void), execexec(void);
int execforkexec(void);
--- a/rc/simple.c
+++ b/rc/simple.c
@@ -169,7 +169,7 @@
return argv;
}
-void
+int
chars(int fd, int multi, vlong count, char *file)
{
char buf[8*1024];
@@ -181,8 +181,7 @@
if(n > (count - m))
n = count - m;
if((n = read(fd, buf, n)) < 0){
- fprint(2, "read: error reading %s: %r\n", file);
- exits("read error");
+ return -1;
}
if(n == 0){
if(m == 0)
@@ -191,6 +190,7 @@
}
write(1, buf, n);
}
+ return 0;
}
int
@@ -205,8 +205,7 @@
for(m=0; ; ){
n = read(fd, &c, 1);
if(n < 0){
- fprint(2, "read: error reading %s: %r\n", file);
- exits("read error");
+ return -1;
}
if(n == 0){
if(m == 0)
@@ -217,8 +216,7 @@
nalloc += 1024;
buf = realloc(buf, nalloc);
if(buf == nil){
- fprint(2, "read: malloc error: %r\n");
- exits("malloc");
+ return -1;
}
}
buf[m++] = c;
@@ -231,13 +229,15 @@
return m;
}
-void
+int
lines(int fd, int multi, vlong count, char *file)
{
+ int n;
do{
- if(line(fd, file) == 0)
+ if((n = line(fd, file)) <= 0)
break;
}while(multi || --count > 0);
+ return n;
}
void
@@ -244,7 +244,7 @@
execread(void)
{
// print("Execread\n");
- void (*proc)(int, int, vlong, char*);
+ int (*proc)(int, int, vlong, char*);
word *a;
int fd, multi = 0;
vlong num = 0;
@@ -270,13 +270,10 @@
multi = 1;
break;
default:
- pfmt(err, "Usage: read [ -m | -n nlines | -c nbytes | -r nrunes ] [ file ... ]\n");
- setstatus("read usage");
- poplist();
- return;
+ goto Usage;
}
+ popword();
}
- popword();
}
if(count(runq->argv->words)==0){
(*proc)(0, multi, num, "<stdin>");
@@ -285,13 +282,23 @@
for(;a;a = a->next){
fd = open(a->word, OREAD);
if(fd < 0){
- fprint(2, "read: can't open %s: %r\n", a->word);
- exits("open");
+ goto Error;
}
- (*proc)(fd, multi, num, a->word);
+ if((*proc)(fd, multi, num, a->word) < 0)
+ goto Error;
close(fd);
}
}
+ return;
+Error:
+ pfmt(err, "read: %s\n", strerror(errno));
+ setstatus("read error");
+ poplist();
+ return;
+Usage:
+ pfmt(err, "Usage: read [ -m | -n nlines | -c nbytes | -r nrunes ] [ file ... ]\n");
+ setstatus("read usage");
+ poplist();
}
void
@@ -304,7 +311,7 @@
execbind(void)
{
//print("Execbind\n");
- ulong flag;
+ ulong flag = 0;
int qflag = 0;
popword(); /* "bind" */
while(runq->argv->words && runq->argv->words->word[0]=='-'){
@@ -330,30 +337,29 @@
default:
goto Usage;
}
+ popword();
}
- popword();
}
- if(count(runq->argv->words)!=2 || (flag&MAFTER)&&(flag&MBEFORE)){
+ if(count(runq->argv->words)!=2 || (flag&MAFTER)&&(flag&MBEFORE))
goto Usage;
- }
- if(bind(runq->argv->words->word, runq->argv->words->next->word, flag) == -1){
- if(qflag)
- return;
- Xerror1("bind error");
- }
+ if(bind(runq->argv->words->word, runq->argv->words->next->word, flag) == -1)
+ goto Error;
return;
+Error:
+ setstatus("bind error");
+ poplist();
+ if(qflag)
+ return;
+ pfmt(err, "bind: %s\n", strerror(errno));
+ return;
Usage:
- Xerror1("usage: bind [-b|-a|-c|-bc|-ac] new old");
+ pfmt(err, "usage: bind [-b|-a|-c|-bc|-ac] new old\n");
+ setstatus("bind usage");
+ poplist();
return;
}
void
-catch(void *, char *m)
-{
- pfmt(err, "%s: %s\n", argv0, m);
-}
-
-void
execmount(void)
{
//print("Execmount\n");
@@ -392,8 +398,8 @@
default:
goto Usage;
}
+ popword();
}
- popword();
}
if(count(runq->argv->words)==3)
spec = runq->argv->words->next->next->word;
@@ -402,23 +408,21 @@
if((flag&MAFTER)&&(flag&MBEFORE))
goto Usage;
fd = Open(runq->argv->words->word, ORDWR);
- if(fd < 0){
- if(qflag)
- return;
- pfmt(err, "mount: can't open %s\n", runq->argv->words->word);
+ if(fd < 0)
+ goto Error;
+ if(sysmount(fd, -1, runq->argv->words->next->word, flag, spec) < 0)
+ goto Error;
+ return;
+Error:
+ setstatus("mount error");
+ poplist();
+ if(qflag)
return;
- }
- notify(catch);
- if(sysmount(fd, -1, runq->argv->words->next->word, flag, spec) < 0){
- if(qflag)
- return;
- pfmt(err, "mount: %r\n");
- setstatus("mount error");
- poplist();
- }
+ pfmt(err, "mount: %s\n", strerror(errno));
return;
Usage:
- Xerror1("usage: mount [-a|-b] [-cCnNq] [-k keypattern] /srv/service dir [spec]");
+ pfmt(err, "usage: mount [-a|-b] [-cCnNq] [-k keypattern] /srv/service dir [spec]\n");
+ setstatus("mount usage");
return;
}
@@ -426,6 +430,55 @@
execunmount(void)
{
//unmount
+}
+
+void
+execls(void)
+{
+ Dir *db;
+ int fd, n, i;
+ char *path;
+
+ /* Read in our dir and just output name in a row */
+ popword(); /* "ls" */
+ switch(count(runq->argv->words)){
+ case 0:
+ path = ".";
+ break;
+ case 1:
+ path = runq->argv->words->word;
+ break;
+ default:
+ pfmt(err, "ls: listing multiple files not supported\n");
+ return;
+ }
+ db = dirstat(path);
+ if(db == nil)
+ goto Error;
+ if((db->qid.type&QTDIR)) {
+ free(db);
+ fd = open(path, OREAD);
+ if(fd == -1)
+ goto Error;
+ n = dirreadall(fd, &db);
+ if(n < 0)
+ goto Error;
+ for(i = 0; i < n; i++){
+ write(1, db->name, strlen(db->name));
+ write(1, "\n", 1);
+ db++;
+ }
+ close(fd);
+ } else {
+ write(1, db->name, strlen(db->name));
+ write(1, "\n", 1);
+ }
+ return;
+Error:
+ pfmt(err, "ls: %sr\n", strerror(errno));
+ setstatus("ls error");
+ poplist();
+ return;
}
void