plan9port/src/cmd/9p.c

273 lines
4 KiB
C
Raw Normal View History

2003-12-06 18:08:52 +00:00
#include <u.h>
#include <signal.h>
2003-12-06 18:08:52 +00:00
#include <libc.h>
2005-02-11 19:44:04 +00:00
#include <bio.h>
2003-12-06 18:08:52 +00:00
#include <fcall.h>
2005-01-04 22:09:51 +00:00
#include <9pclient.h>
2005-02-11 16:53:27 +00:00
#include <auth.h>
#include <thread.h>
2003-12-06 18:08:52 +00:00
char *addr;
void
usage(void)
{
fprint(2, "usage: 9p [-a address] cmd args...\n");
fprint(2, "possible cmds:\n");
fprint(2, " read name\n");
fprint(2, " readfd name\n");
2005-02-11 19:44:04 +00:00
fprint(2, " write [-l] name\n");
fprint(2, " writefd name\n");
2003-12-06 18:08:52 +00:00
fprint(2, " stat name\n");
// fprint(2, " ls name\n");
fprint(2, "without -a, name elem/path means /path on server unix!$ns/elem\n");
2005-01-17 21:29:00 +00:00
threadexitsall("usage");
2003-12-06 18:08:52 +00:00
}
void xread(int, char**);
void xwrite(int, char**);
void xreadfd(int, char**);
void xwritefd(int, char**);
2003-12-06 18:08:52 +00:00
void xstat(int, char**);
void xls(int, char**);
struct {
char *s;
void (*f)(int, char**);
} cmds[] = {
"read", xread,
"write", xwrite,
"readfd", xreadfd,
"writefd", xwritefd,
2003-12-06 18:08:52 +00:00
"stat", xstat,
// "ls", xls,
};
void
threadmain(int argc, char **argv)
2003-12-06 18:08:52 +00:00
{
char *cmd;
int i;
ARGBEGIN{
case 'a':
addr = EARGF(usage());
break;
2004-12-27 01:22:48 +00:00
case 'D':
2005-02-11 16:53:27 +00:00
chatty9pclient = 1;
break;
2003-12-06 18:08:52 +00:00
default:
usage();
}ARGEND
signal(SIGINT, SIG_DFL);
2003-12-06 18:08:52 +00:00
if(argc < 1)
usage();
cmd = argv[0];
for(i=0; i<nelem(cmds); i++){
if(strcmp(cmds[i].s, cmd) == 0){
cmds[i].f(argc, argv);
threadexitsall(0);
2003-12-06 18:08:52 +00:00
}
}
usage();
}
2005-01-04 22:09:51 +00:00
CFsys*
2003-12-06 18:08:52 +00:00
xparse(char *name, char **path)
{
int fd;
char *p;
2005-01-04 22:09:51 +00:00
CFsys *fs;
2003-12-06 18:08:52 +00:00
if(addr == nil){
p = strchr(name, '/');
if(p == nil)
p = name+strlen(name);
else
*p++ = 0;
*path = p;
2005-02-11 16:53:27 +00:00
fs = nsamount(name, "");
if(fs == nil)
sysfatal("mount: %r");
}else{
2003-12-06 18:08:52 +00:00
*path = name;
if((fd = dial(addr, nil, nil, nil)) < 0)
sysfatal("dial: %r");
2005-02-11 16:53:27 +00:00
if((fs = fsamount(fd, "")) == nil)
sysfatal("fsmount: %r");
}
2003-12-06 18:08:52 +00:00
return fs;
}
2005-01-04 22:09:51 +00:00
CFid*
2003-12-06 18:08:52 +00:00
xopen(char *name, int mode)
{
2005-01-04 22:09:51 +00:00
CFid *fid;
CFsys *fs;
2003-12-06 18:08:52 +00:00
fs = xparse(name, &name);
fid = fsopen(fs, name, mode);
if(fid == nil)
sysfatal("fsopen %s: %r", name);
return fid;
}
int
xopenfd(char *name, int mode)
{
2005-01-04 22:09:51 +00:00
CFsys *fs;
fs = xparse(name, &name);
return fsopenfd(fs, name, mode);
}
2003-12-06 18:08:52 +00:00
void
xread(int argc, char **argv)
{
char buf[1024];
int n;
2005-01-04 22:09:51 +00:00
CFid *fid;
2003-12-06 18:08:52 +00:00
ARGBEGIN{
default:
usage();
}ARGEND
if(argc != 1)
usage();
fid = xopen(argv[0], OREAD);
while((n = fsread(fid, buf, sizeof buf)) > 0)
write(1, buf, n);
if(n < 0)
sysfatal("read error: %r");
2005-01-17 21:29:00 +00:00
threadexitsall(0);
2003-12-06 18:08:52 +00:00
}
void
xreadfd(int argc, char **argv)
{
char buf[1024];
int n;
int fd;
ARGBEGIN{
default:
usage();
}ARGEND
if(argc != 1)
usage();
fd = xopenfd(argv[0], OREAD);
while((n = read(fd, buf, sizeof buf)) > 0)
write(1, buf, n);
if(n < 0)
sysfatal("read error: %r");
2005-01-17 21:29:00 +00:00
threadexitsall(0);
}
2003-12-06 18:08:52 +00:00
void
xwrite(int argc, char **argv)
{
char buf[1024];
int n, did;
2005-01-04 22:09:51 +00:00
CFid *fid;
2005-02-11 19:44:04 +00:00
Biobuf *b;
char *p;
int byline;
2003-12-06 18:08:52 +00:00
2005-02-11 19:44:04 +00:00
byline = 0;
2003-12-06 18:08:52 +00:00
ARGBEGIN{
2005-02-11 19:44:04 +00:00
case 'l':
byline = 1;
2003-12-06 18:08:52 +00:00
default:
usage();
}ARGEND
if(argc != 1)
usage();
did = 0;
2003-12-06 18:08:52 +00:00
fid = xopen(argv[0], OWRITE|OTRUNC);
2005-02-11 19:44:04 +00:00
if(byline){
n = 0;
b = malloc(sizeof *b);
if(b == nil)
sysfatal("out of memory");
Binit(b, 0, OREAD);
while((p = Brdstr(b, '\n', 0)) != nil){
n = strlen(p);
did = 1;
if(fswrite(fid, p, n) != n)
sysfatal("write error: %r");
}
free(b);
}else{
while((n = read(0, buf, sizeof buf)) > 0){
did = 1;
if(fswrite(fid, buf, n) != n)
sysfatal("write error: %r");
}
}
if(n == 0 && !did){
if(fswrite(fid, buf, 0) != 0)
sysfatal("write error: %r");
}
2003-12-06 18:08:52 +00:00
if(n < 0)
sysfatal("read error: %r");
2005-01-17 21:29:00 +00:00
threadexitsall(0);
2003-12-06 18:08:52 +00:00
}
void
xwritefd(int argc, char **argv)
{
char buf[1024];
int n;
int fd;
ARGBEGIN{
default:
usage();
}ARGEND
if(argc != 1)
usage();
fd = xopenfd(argv[0], OWRITE|OTRUNC);
while((n = read(0, buf, sizeof buf)) > 0)
if(write(fd, buf, n) != n)
sysfatal("write error: %r");
if(n < 0)
sysfatal("read error: %r");
2005-01-17 21:29:00 +00:00
threadexitsall(0);
}
2003-12-06 18:08:52 +00:00
void
xstat(int argc, char **argv)
{
Dir *d;
2005-01-04 22:09:51 +00:00
CFsys *fs;
char *name;
2003-12-06 18:08:52 +00:00
ARGBEGIN{
default:
usage();
}ARGEND
if(argc != 1)
usage();
2005-01-04 22:09:51 +00:00
name = argv[0];
fs = xparse(name, &name);
if((d = fsdirstat(fs, name)) == 0)
sysfatal("dirstat: %r");
2003-12-06 18:08:52 +00:00
fmtinstall('D', dirfmt);
fmtinstall('M', dirmodefmt);
print("%D\n", d);
2005-01-17 21:29:00 +00:00
threadexitsall(0);
2003-12-06 18:08:52 +00:00
}