7 uint messagesize = 65536; /* just a buffer size */
12 fprint(2, "usage: aux/9pcon [-m messagesize] /srv/service | -c command | -n networkaddress\n");
25 fprint(2, "fork failed: %r\n");
31 execl("/bin/rc", "rc", "-c", cmd, nil);
32 fprint(2, "exec failed: %r\n");
47 buf = malloc(messagesize);
49 sysfatal("out of memory");
51 while((n = read9pmsg(fd, buf, messagesize)) > 0){
52 if(convM2S(buf, n, &f) != n){
53 print("convM2S: %r\n");
56 print("\t<- %F\n", &f);
59 print("server eof\n");
61 print("read9pmsg from server: %r\n");
65 tversion(Fcall *f, int, char **argv)
67 f->msize = strtol(argv[0], 0, 0);
68 if(f->msize > messagesize)
69 return "message size too big; use -m option on command line";
75 tauth(Fcall *f, int, char **argv)
77 f->afid = strtol(argv[0], 0, 0);
84 tflush(Fcall *f, int, char **argv)
86 f->oldtag = strtol(argv[0], 0, 0);
91 tattach(Fcall *f, int, char **argv)
93 f->fid = strtol(argv[0], 0, 0);
94 f->afid = strtol(argv[1], 0, 0);
101 twalk(Fcall *f, int argc, char **argv)
106 return "usage: Twalk tag fid newfid [name...]";
107 f->fid = strtol(argv[0], 0, 0);
108 f->newfid = strtol(argv[1], 0, 0);
110 if(f->nwname > MAXWELEM)
111 return "too many names";
112 for(i=0; i<argc-2; i++)
113 f->wname[i] = argv[2+i];
118 topen(Fcall *f, int, char **argv)
120 f->fid = strtol(argv[0], 0, 0);
121 f->mode = strtol(argv[1], 0, 0);
126 tcreate(Fcall *f, int, char **argv)
128 f->fid = strtol(argv[0], 0, 0);
130 f->perm = strtoul(argv[2], 0, 8);
131 f->mode = strtol(argv[3], 0, 0);
136 tread(Fcall *f, int, char **argv)
138 f->fid = strtol(argv[0], 0, 0);
139 f->offset = strtoll(argv[1], 0, 0);
140 f->count = strtol(argv[2], 0, 0);
145 twrite(Fcall *f, int, char **argv)
147 f->fid = strtol(argv[0], 0, 0);
148 f->offset = strtoll(argv[1], 0, 0);
150 f->count = strlen(argv[2]);
155 tclunk(Fcall *f, int, char **argv)
157 f->fid = strtol(argv[0], 0, 0);
162 tremove(Fcall *f, int, char **argv)
164 f->fid = strtol(argv[0], 0, 0);
169 tstat(Fcall *f, int, char **argv)
171 f->fid = strtol(argv[0], 0, 0);
178 if(strcmp(s, "~0") == 0)
180 return strtoul(s, 0, 0);
186 if(strcmp(s, "~0") == 0)
188 return strtoull(s, 0, 0);
192 twstat(Fcall *f, int, char **argv)
194 static uchar buf[DIRMAX];
197 memset(&d, 0, sizeof d);
202 d.mode = xstrtoul(argv[4]);
203 d.mtime = xstrtoul(argv[5]);
204 d.length = xstrtoull(argv[6]);
206 f->fid = strtol(argv[0], 0, 0);
208 f->nstat = convD2M(&d, buf, sizeof buf);
209 if(f->nstat < BIT16SZ)
210 return "convD2M failed (internal error)";
218 settag(Fcall*, int, char **argv)
220 static char buf[120];
222 taggen = strtol(argv[0], 0, 0)-1;
223 snprint(buf, sizeof buf, "next tag is %d", taggen+1);
227 typedef struct Cmd Cmd;
233 char *(*fn)(Fcall *f, int, char**);
237 "Tversion", Tversion, 2, "messagesize version", tversion,
238 "Tauth", Tauth, 3, "afid uname aname", tauth,
239 "Tflush", Tflush, 1, "oldtag", tflush,
240 "Tattach", Tattach, 4, "fid afid uname aname", tattach,
241 "Twalk", Twalk, 0, "fid newfid [name...]", twalk,
242 "Topen", Topen, 2, "fid mode", topen,
243 "Tcreate", Tcreate, 4, "fid name perm mode", tcreate,
244 "Tread", Tread, 3, "fid offset count", tread,
245 "Twrite", Twrite, 3, "fid offset data", twrite,
246 "Tclunk", Tclunk, 1, "fid", tclunk,
247 "Tremove", Tremove, 1, "fid", tremove,
248 "Tstat", Tstat, 1, "fid", tstat,
249 "Twstat", Twstat, 7, "fid name uid gid mode mtime length", twstat,
250 "nexttag", 0, 0, "", settag,
262 buf = malloc(messagesize);
264 fprint(2, "out of memory\n");
270 while(p = Brdline(&b, '\n')){
271 p[Blinelen(&b)-1] = '\0';
274 if((nf = tokenize(p, f, nelem(f))) == 0)
276 for(i=0; i<nelem(msg9p); i++)
277 if(strcmp(f[0], msg9p[i].name) == 0)
279 if(i == nelem(msg9p)){
280 fprint(2, "?unknown message\n");
283 memset(&t, 0, sizeof t);
284 t.type = msg9p[i].type;
285 if(t.type == Tversion)
289 if(nf < 1 || (msg9p[i].argc && nf != 1+msg9p[i].argc)){
290 fprint(2, "?usage: %s %s\n", msg9p[i].name, msg9p[i].usage);
293 if(e = msg9p[i].fn(&t, nf-1, f+1)){
294 fprint(2, "?%s\n", e);
297 n = convS2M(&t, buf, messagesize);
299 fprint(2, "?message too large for buffer\n");
302 if(write(fd, buf, n) != n){
303 fprint(2, "?write fails: %r\n");
306 print("\t-> %F\n", &t);
311 main(int argc, char **argv)
313 int fd, pid, cmd, net;
322 messagesize = strtol(EARGF(usage()), 0, 0);
331 fmtinstall('F', fcallfmt);
332 fmtinstall('D', dirfmt);
333 fmtinstall('M', dirmodefmt);
342 fd = connectcmd(argv[0]);
344 fd = dial(netmkaddr(argv[0], "net", "9fs"), 0, 0, 0);
346 sysfatal("dial: %r");
348 fd = open(argv[0], ORDWR);
350 sysfatal("open: %r");
353 switch(pid = rfork(RFPROC|RFMEM)){
355 sysfatal("rfork: %r");
359 postnote(PNPROC, getppid(), "kill");
363 postnote(PNPROC, pid, "kill");