2 * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
41 #include <readline/readline.h>
42 #include <readline/history.h>
52 #define TMPFSTR "/tmp/kamiftp.XXXXXXXXXX"
53 #define TMPFSTRLEN sizeof(TMPFSTR)
63 struct evbuffer *dirbuf;
68 volatile sig_atomic_t resized;
80 #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
83 read_line(const char *prompt)
88 if ((line = readline(prompt)) == NULL)
90 /* XXX: trim spaces? */
101 spawn(const char *argv0, ...)
106 const char *argv[16], *last;
109 memset(argv, 0, sizeof(argv));
113 for (i = 1; i < nitems(argv); ++i) {
114 last = va_arg(ap, const char *);
121 assert(last == NULL);
123 switch (pid = fork()) {
127 execvp(argv[0], (char *const *)argv);
130 waitpid(pid, &status, 0);
135 stdio_tls_write(void *arg, const char *buf, int len)
137 struct tls *ctx = arg;
141 ret = tls_write(ctx, buf, len);
142 } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
145 warn("tls_write: %s", tls_error(ctx));
151 stdio_tls_read(void *arg, char *buf, int len)
153 struct tls *ctx = arg;
157 ret = tls_read(ctx, buf, len);
158 } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
161 warn("tls_read: %s", tls_error(ctx));
167 stdio_tls_close(void *arg)
169 struct tls *ctx = arg;
173 ret = tls_close(ctx);
174 } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
180 tty_resized(int signo)
188 fprintf(stderr, "usage: %s [-C cert] [-K key] [-o output] "
189 "[9p://][user@]host[:port][/path]\n", getprogname());
190 fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
211 hexdump("outgoing message", EVBUFFER_DATA(evb),
212 EVBUFFER_LENGTH(evb));
214 while (EVBUFFER_LENGTH(evb) != 0) {
215 r = fwrite(EVBUFFER_DATA(evb), 1, EVBUFFER_LENGTH(evb), fp);
217 fatalx("unexpected EOF");
218 evbuffer_drain(evb, r);
223 mustread(void *d, size_t len)
227 r = fread(d, 1, len, fp);
229 errx(1, "unexpected EOF");
239 r = fread(&len, 1, sizeof(len), fp);
240 if (r != sizeof(len))
241 errx(1, "unexpected EOF");
244 if (len < HEADERSIZE)
245 errx(1, "read message of invalid length %d", len);
247 len -= 4; /* skip the length just read */
250 l = MIN(len, sizeof(tmp));
252 r = fread(tmp, 1, l, fp);
254 errx(1, "unexpected EOF");
256 evbuffer_add(buf, tmp, r);
260 hexdump("incoming packet", EVBUFFER_DATA(buf),
261 EVBUFFER_LENGTH(buf));
265 np_read64(struct evbuffer *buf)
269 evbuffer_remove(buf, &n, sizeof(n));
274 np_read32(struct evbuffer *buf)
278 evbuffer_remove(buf, &n, sizeof(n));
283 np_read16(struct evbuffer *buf)
287 evbuffer_remove(buf, &n, sizeof(n));
292 np_read8(struct evbuffer *buf)
296 evbuffer_remove(buf, &n, sizeof(n));
301 np_readstr(struct evbuffer *buf)
306 len = np_read16(buf);
307 assert(EVBUFFER_LENGTH(buf) >= len);
309 if ((str = calloc(1, len+1)) == NULL)
311 evbuffer_remove(buf, str, len);
316 np_read_qid(struct evbuffer *buf, struct qid *qid)
318 assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
320 qid->type = np_read8(buf);
321 qid->vers = np_read32(buf);
322 qid->path = np_read64(buf);
326 np_read_stat(struct evbuffer *buf, struct np_stat *st)
330 memset(st, 0, sizeof(*st));
332 size = np_read16(buf);
333 if (size > EVBUFFER_LENGTH(buf))
336 st->type = np_read16(buf);
337 st->dev = np_read32(buf);
338 np_read_qid(buf, &st->qid);
339 st->mode = np_read32(buf);
340 st->atime = np_read32(buf);
341 st->mtime = np_read32(buf);
342 st->length = np_read64(buf);
343 st->name = np_readstr(buf);
344 st->uid = np_readstr(buf);
345 st->gid = np_readstr(buf);
346 st->muid = np_readstr(buf);
366 err = np_readstr(buf);
367 errx(1, "expected %s, got error %s",
368 pp_msg_type(type), err);
371 errx(1, "expected %s, got msg type %s",
372 pp_msg_type(type), pp_msg_type(t));
376 expect2(uint8_t type, uint16_t tag)
386 errx(1, "expected tag 0x%x, got 0x%x", tag, t);
390 check(uint8_t type, uint16_t tag)
395 rtype = np_read8(buf);
396 rtag = np_read16(buf);
399 errx(1, "expected tag 0x%x, got 0x%x", tag, rtag);
404 return np_readstr(buf);
406 errx(1, "expected %s, got msg type %s",
407 pp_msg_type(type), pp_msg_type(rtype));
415 tversion(VERSION9P, MSIZE9P);
418 expect2(Rversion, NOTAG);
420 msize = np_read32(buf);
421 version = np_readstr(buf);
423 if (msize > MSIZE9P || msize < 256)
424 errx(1, "got unexpected msize: %d", msize);
425 if (strcmp(version, VERSION9P))
426 errx(1, "unexpected 9p version: %s", version);
433 do_attach(const char *user)
437 tattach(pwdfid, NOFID, user, "/");
440 expect2(Rattach, iota_tag);
441 np_read_qid(buf, &qid);
447 do_open(uint32_t fid, uint8_t mode)
455 expect2(Ropen, iota_tag);
457 np_read_qid(buf, &qid);
458 iounit = np_read32(buf);
466 do_create(uint32_t fid, const char *name, uint32_t perm, uint8_t mode)
471 tcreate(fid, name, perm, mode);
474 expect2(Rcreate, iota_tag);
476 np_read_qid(buf, &qid);
477 iounit = np_read32(buf);
485 do_clunk(uint32_t fid)
490 expect2(Rclunk, iota_tag);
496 dup_fid(int fid, int nfid)
501 twalk(fid, nfid, NULL, 0);
505 if ((errstr = check(Rwalk, iota_tag)) != NULL)
508 nwqid = np_read16(buf);
517 walk_path(int fid, int newfid, const char *path, int *missing,
520 char *wnames[MAXWELEM], *p, *t, *errstr;
524 if ((p = strdup(path)) == NULL)
528 /* strip initial ./ */
529 if (t[0] == '.' && t[1] == '/')
532 for (nwname = 0; nwname < nitems(wnames) &&
533 (wnames[nwname] = strsep(&t, "/")) != NULL;) {
534 if (*wnames[nwname] != '\0')
538 twalk(fid, newfid, (const char **)wnames, nwname);
543 if ((errstr = check(Rwalk, iota_tag)) != NULL) {
548 nwqid = np_read16(buf);
549 assert(nwqid <= nwname);
551 /* consume all qids */
552 for (i = 0; i < nwqid; ++i)
553 np_read_qid(buf, qid);
557 *missing = nwname - nwqid;
562 do_stat(int fid, struct np_stat *st)
567 expect2(Rstat, iota_tag);
569 /* eat up the first two byte length */
572 if (np_read_stat(buf, st) == -1)
573 errx(1, "invalid stat struct read");
579 do_wstat(int fid, const struct np_stat *st)
587 if ((errstr = check(Rwstat, iota_tag)) != NULL)
603 if ((errstr = check(Rremove, iota_tag)) != NULL)
612 do_read(int fid, uint64_t off, uint32_t count, void *data)
616 tread(fid, off, count);
619 expect2(Rread, iota_tag);
622 assert(r == EVBUFFER_LENGTH(buf));
624 evbuffer_remove(buf, data, r);
632 do_write(int fid, uint64_t off, uint32_t count, void *data)
636 twrite(fid, off, data, count);
639 expect2(Rwrite, iota_tag);
650 draw_progress(const char *pre, const struct progress *p)
659 perc = 100.0 * p->done / p->max;
661 fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
668 if (ioctl(0, TIOCGWINSZ, &ws) == -1)
670 tty_width = ws.ws_col;
675 ((l = fprintf(stderr, "\r%s ", pre)) == -1 || l >= w))
678 w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
680 fprintf(stderr, "%4d%%\n", (int)perc);
684 fprintf(stderr, "|");
686 l = w * MIN(100.0, perc) / 100.0;
687 for (i = 0; i < l; i++)
688 fprintf(stderr, "*");
690 fprintf(stderr, " ");
691 fprintf(stderr, "|%4d%%", (int)perc);
695 fetch_fid(int fid, int fd, const char *name)
697 static char buf[MSIZE9P];
698 struct progress p = {0};
704 do_open(fid, KOREAD);
711 len = MIN(sizeof(buf), msize);
712 len -= IOHDRSZ; /* for the request' fields */
714 r = do_read(fid, p.done, len, buf);
718 for (off = 0; off < r; off += nw)
719 if ((nw = write(fd, buf + off, r - off)) == 0 ||
726 draw_progress(name, &p);
729 /* throttle, for debugging purpose */
731 struct timespec ts = { 0, 500000000 };
732 nanosleep(&ts, NULL);
745 send_fid(int fid, const char *fnam, int open_flags, int fd, const char *name)
747 static char buf[MSIZE9P];
748 struct progress p = {0};
753 if (fstat(fd, &sb) == -1)
757 do_create(fid, fnam, 0644, KOWRITE);
759 do_open(fid, open_flags | KOWRITE);
763 len = MIN(sizeof(buf), msize);
764 len -= HEADERSIZE + 4 + 4 + 8; /* for the request' fields */
766 r = read(fd, buf, len);
772 w = do_write(fid, p.done, r, buf);
775 draw_progress(name, &p);
778 /* throttle, for debugging purpose */
780 struct timespec ts = { 0, 500000000 };
781 nanosleep(&ts, NULL);
791 woc_file(int fd, const char *prompt, const char *path)
794 const char *n = NULL;
799 errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
800 if (errstr != NULL && miss > 1) {
801 fprintf(stderr, "%s: %s\n", path, errstr);
806 if (errstr != NULL || miss == 1) {
807 char p[PATH_MAX], *dn;
810 * If it's only one component missing (the file name), walk
811 * to the parent directory and try to create the file.
814 if (strlcpy(p, path, sizeof(p)) >= sizeof(p)) {
815 fprintf(stderr, "path too long: %s\n", path);
820 if (!strcmp(dn, ".")) {
821 errstr = dup_fid(pwdfid, nfid);
824 errstr = walk_path(pwdfid, nfid, dn, &miss, &qid);
826 if (errstr != NULL) {
827 fprintf(stderr, "%s: %s\n", dn, errstr);
833 fprintf(stderr, "%s: not a directory\n", dn);
837 if ((n = strrchr(path, '/')) != NULL)
846 fprintf(stderr, "can't create %s: missing %d path"
847 " component(s)\n", path, miss);
851 send_fid(nfid, n, KOTRUNC, fd, prompt);
856 dial(const char *host, const char *port)
858 struct addrinfo hints, *res, *res0;
859 int sock, error, saved_errno;
860 const char *cause = NULL;
862 memset(&hints, 0, sizeof(hints));
863 hints.ai_family = AF_UNSPEC;
864 hints.ai_socktype = SOCK_STREAM;
865 error = getaddrinfo(host, port, &hints, &res0);
867 errx(1, "%s", gai_strerror(error));
870 for (res = res0; res != NULL; res = res->ai_next) {
871 sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
878 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
896 do_connect(const char *host, const char *port, const char *user)
902 fprintf(stderr, "connecting to %s:%s...", host, port);
905 struct tls_config *conf;
909 if ((conf = tls_config_new()) == NULL)
910 fatalx("failed to create TLS config");
911 tls_config_insecure_noverifycert(conf);
912 tls_config_insecure_noverifyname(conf);
917 if (tls_config_set_keypair_file(conf, crtpath, keypath) == -1)
918 fatalx("failed to load certs: (%s, %s)", crtpath,
921 if ((ctx = tls_client()) == NULL)
922 fatalx("failed to create TLS client");
923 if (tls_configure(ctx, conf) == -1)
924 fatalx("failed to configure TLS client");
925 tls_config_free(conf);
927 if (tls_connect(ctx, host, port) == -1)
928 fatalx("failed to connect to %s:%s: %s", host, port,
932 r = tls_handshake(ctx);
933 } while (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT);
934 fp = funopen(ctx, stdio_tls_read, stdio_tls_write, NULL,
941 fd = dial(host, port);
942 if ((fp = fdopen(fd, "r+")) == NULL)
946 fprintf(stderr, " done!\n");
953 tmp_file(char sfn[TMPFSTRLEN])
957 strlcpy(sfn, TMPFSTR, TMPFSTRLEN);
958 if ((tmpfd = mkstemp(sfn)) == -1) {
959 warn("mkstemp %s", sfn);
963 /* set the close-on-exec flag */
964 if (fcntl(tmpfd, F_SETFD, FD_CLOEXEC) == -1) {
973 static inline const char *
994 /* unreachable, just for the compiler' happiness */
1000 prepare_wstat(struct np_stat *st)
1002 memset(st, 0xFF, sizeof(*st));
1010 print_dirent(const struct np_stat *st)
1015 char fmt[FMT_SCALED_STRSIZE], tim[13];
1017 if (fmt_scaled(st->length, fmt) == -1)
1018 strlcpy(fmt, "xxx", sizeof(fmt));
1022 if (now > mtime && (now - mtime) < 365/2 * 24 * 12 * 60)
1023 timfmt = "%b %e %R";
1025 timfmt = "%b %e %Y";
1027 if ((tm = localtime(&mtime)) == NULL ||
1028 strftime(tim, sizeof(tim), timfmt, tm) == 0)
1029 strlcpy(tim, "unknown", sizeof(tim));
1031 if (st->qid.type & QTDIR)
1035 printf("%s", pp_perm(st->mode >> 6));
1036 printf("%s", pp_perm(st->mode >> 3));
1037 printf("%s", pp_perm(st->mode));
1038 printf(" %8s %12s %s%s\n", fmt, tim, st->name,
1039 st->qid.type & QTDIR ? "/" : "");
1045 dir_listing(const char *path, int (*fn)(const struct np_stat *),
1058 errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1059 if (errstr != NULL) {
1061 printf("%s: %s\n", path, errstr);
1067 printf("%s: No such file or directory\n", path);
1070 if (!(qid.type & QTDIR)) {
1072 printf("%s: not a directory\n", path);
1077 do_open(nfid, KOREAD);
1078 evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1081 tread(nfid, off, msize - IOHDRSZ);
1084 expect2(Rread, iota_tag);
1086 len = np_read32(buf);
1090 evbuffer_add_buffer(dirbuf, buf);
1096 while (EVBUFFER_LENGTH(dirbuf) != 0) {
1097 if (np_read_stat(dirbuf, &st) == -1)
1098 errx(1, "invalid stat struct read");
1111 evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1117 cmd_bell(int argc, const char **argv)
1122 puts("bell mode enabled");
1124 puts("bell mode disabled");
1131 if (!strcmp(*argv, "on")) {
1133 puts("bell mode enabled");
1137 if (!strcmp(*argv, "off")) {
1139 puts("bell mode disabled");
1144 printf("bell [on | off]\n");
1148 cmd_bye(int argc, const char **argv)
1156 cmd_cd(int argc, const char **argv)
1163 printf("usage: cd remote-path\n");
1168 errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1169 if (errstr != NULL) {
1170 printf("%s: %s\n", argv[0], errstr);
1175 if (miss != 0 || !(qid.type & QTDIR)) {
1176 printf("%s: not a directory\n", argv[0]);
1187 cmd_edit(int argc, const char **argv)
1190 int nfid, tmpfd, miss;
1191 char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1195 puts("usage: edit file");
1199 if ((ed = getenv("VISUAL")) == NULL &&
1200 (ed = getenv("EDITOR")) == NULL)
1204 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1205 if (errstr != NULL) {
1206 printf("%s: %s\n", *argv, errstr);
1211 if (miss != 0 || qid.type != 0) {
1212 printf("%s: not a file\n", *argv);
1218 if ((tmpfd = tmp_file(sfn)) == -1) {
1223 strlcpy(p, *argv, sizeof(p));
1226 if (fetch_fid(nfid, tmpfd, name)) {
1227 warn("failed fetch or can't write %s", sfn);
1232 spawn(ed, sfn, NULL);
1235 * Re-open the file because it's not guaranteed that the
1236 * file descriptor tmpfd is still associated with the file
1237 * pointed by sfn: it's not uncommon for editor to write
1238 * a backup file and then rename(2) it to the file name.
1240 if ((tmpfd = open(sfn, O_RDONLY)) == -1) {
1241 warn("can't open %s", sfn);
1245 woc_file(tmpfd, *argv, name);
1253 cmd_get(int argc, const char **argv)
1260 if (argc != 1 && argc != 2) {
1261 printf("usage: get remote-file [local-file]\n");
1267 else if ((l = strrchr(argv[0], '/')) != NULL)
1273 errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1274 if (errstr != NULL) {
1275 printf("%s: %s\n", argv[0], errstr);
1280 if (miss != 0 || qid.type != 0) {
1281 printf("%s: not a file\n", argv[0]);
1287 if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
1288 warn("can't open %s", l);
1293 if (fetch_fid(nfid, fd, l) == -1)
1294 warn("write %s", l);
1299 cmd_hexdump(int argc, const char **argv)
1304 puts("hexdump mode enabled");
1306 puts("hexdump mode disabled");
1313 if (!strcmp(*argv, "on")) {
1315 puts("hexdump mode enabled");
1319 if (!strcmp(*argv, "off")) {
1321 puts("hexdump mode disabled");
1326 puts("usage: hexdump [on | off]");
1330 cmd_lcd(int argc, const char **argv)
1335 printf("usage: lcd [local-directory]\n");
1342 if (argc == 0 && (dir = getenv("HOME")) == NULL) {
1343 printf("HOME is not defined\n");
1347 if (chdir(dir) == -1)
1348 printf("cd: %s: %s\n", dir, strerror(errno));
1352 cmd_lpwd(int argc, const char **argv)
1354 char path[PATH_MAX];
1357 printf("usage: lpwd\n");
1361 if (getcwd(path, sizeof(path)) == NULL) {
1362 printf("lpwd: %s\n", strerror(errno));
1366 printf("%s\n", path);
1370 cmd_ls(int argc, const char **argv)
1373 puts("usage: ls [path]");
1377 dir_listing(argc == 0 ? "." : argv[0], print_dirent, 1);
1381 cmd_page(int argc, const char **argv)
1384 int nfid, tmpfd, miss, r;
1385 char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1389 puts("usage: page file");
1393 if ((pager = getenv("PAGER")) == NULL)
1397 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1398 if (errstr != NULL) {
1399 printf("%s: %s\n", *argv, errstr);
1404 if (miss != 0 || qid.type != 0) {
1405 printf("%s: not a file\n", *argv);
1411 if ((tmpfd = tmp_file(sfn)) == -1) {
1416 strlcpy(p, *argv, sizeof(p));
1418 if ((r = fetch_fid(nfid, tmpfd, name)) == -1)
1419 warn("write %s", sfn);
1422 spawn(pager, sfn, NULL);
1427 cmd_pipe(int argc, const char **argv)
1431 int nfid, tmpfd, miss, status;
1432 int filedes[2]; /* read end, write end */
1436 puts("usage: pipe remote-file cmd [args...]");
1441 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1442 if (errstr != NULL) {
1443 printf("%s: %s\n", *argv, errstr);
1448 if (miss != 0 || qid.type != 0) {
1449 printf("%s: not a file\n", *argv);
1455 if (pipe(filedes) == -1)
1458 switch (pid = vfork()) {
1463 if (dup2(filedes[0], 0) == -1)
1465 execvp(argv[1], (char *const *)argv + 1);
1470 if (fetch_fid(nfid, filedes[1], *argv) == -1)
1471 warnx("failed to fetch all the file");
1474 waitpid(pid, &status, 0);
1478 cmd_put(int argc, const char **argv)
1484 if (argc != 1 && argc != 2) {
1485 printf("usage: put local-file [remote-file]\n");
1491 else if ((l = strrchr(argv[0], '/')) != NULL)
1496 if ((fd = open(argv[0], O_RDONLY)) == -1) {
1497 warn("%s", argv[0]);
1501 woc_file(fd, argv[0], l);
1506 cmd_rename(int argc, const char **argv)
1514 puts("usage: rename remote-file new-remote-name");
1519 errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1520 if (errstr != NULL) {
1521 printf("%s: %s\n", argv[0], errstr);
1527 printf("%s: not such file or directory\n", argv[0]);
1532 st.name = (char *)argv[1];
1533 if ((errstr = do_wstat(nfid, &st)) != NULL) {
1534 printf("rename: %s\n", errstr);
1542 cmd_rm(int argc, const char **argv)
1549 puts("usage: rm file ...");
1553 for (; *argv; ++argv, --argc) {
1555 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1556 if (errstr != NULL) {
1557 printf("%s: %s\n", *argv, errstr);
1562 printf("%s: not such file or directory\n", *argv);
1566 if ((errstr = do_remove(nfid)) != NULL) {
1567 printf("%s: %s\n", *argv, errstr);
1575 cmd_verbose(int argc, const char **argv)
1578 log_setverbose(!log_getverbose());
1579 if (log_getverbose())
1580 puts("verbose mode enabled");
1582 puts("verbose mode disabled");
1589 if (!strcmp(*argv, "on")) {
1591 puts("verbose mode enabled");
1595 if (!strcmp(*argv, "off")) {
1597 puts("verbose mode disabled");
1602 printf("verbose [on | off]\n");
1606 excmd(int argc, const char **argv)
1613 for (i = 0; i < nitems(cmds); ++i) {
1614 if (!strcmp(cmds[i].name, *argv)) {
1615 cmds[i].fn(argc-1, argv+1);
1620 log_warnx("unknown command %s", *argv);
1624 parsecmd(char *cmd, char **argv, size_t len)
1629 memset(argv, 0, sizeof(*argv) * len);
1631 while (argc < len) {
1632 while (isspace((unsigned char)*cmd))
1639 for (; *cmd != '\0'; ++cmd) {
1646 memmove(cmd, cmd + 1, strlen(cmd));
1650 if (*cmd == quote) {
1652 memmove(cmd, cmd + 1, strlen(cmd));
1656 if (*cmd == '\'' || *cmd == '"') {
1658 memmove(cmd, cmd + 1, strlen(cmd));
1665 if (isspace((unsigned char)*cmd))
1669 if (*cmd == '\0' && (escape || quote)) {
1670 fprintf(stderr, "unterminated %s\n",
1671 escape ? "escape" : "quote");
1681 fprintf(stderr, "too many arguments\n");
1688 cd_or_fetch(const char *path, const char *outfile)
1694 while (*path == '/')
1700 errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1702 errx(1, "walk %s: %s", path, errstr);
1704 errc(1, ENOENT, "walk %s", path);
1706 if (qid.type & QTDIR) {
1708 errx(1, "can't fetch directory %s", path);
1714 if (outfile == NULL) {
1715 if ((outfile = strrchr(path, '/')) == NULL)
1719 if (*outfile == '\0')
1720 errx(1, "invalid path: missing file name: %s",
1724 if (strcmp(outfile, "-") != 0) {
1725 fd = open(outfile, O_WRONLY|O_CREAT, 0644);
1727 err(1, "can't open for writing %s", outfile);
1731 if (fetch_fid(nfid, fd, outfile) == -1)
1732 err(1, "write %s", outfile);
1739 parse_addr(const char *url, const char **user,
1740 const char **port, const char **path)
1742 static char buf[PATH_MAX];
1745 *user = *port = *path = NULL;
1748 if (strlcpy(buf, url, sizeof(buf)) >= sizeof(buf))
1749 errx(1, "connection string too long");
1751 if (!strncmp(host, "9p://", 5))
1754 if ((t = strchr(host, '/')) != NULL) {
1756 errx(1, "invalid connection string: %s", url);
1762 if ((t = strchr(host, '@')) != NULL) {
1764 errx(1, "invalid connection string: %s", url);
1768 } else if ((*user = getenv("USER")) == NULL)
1769 errx(1, "USER not defined");
1771 if ((t = strchr(host, ':')) != NULL) {
1783 main(int argc, char **argv)
1785 const char *user, *host, *port, *path;
1786 const char *outfile = NULL;
1789 log_init(1, LOG_DAEMON);
1791 log_procinit(getprogname());
1793 while ((ch = getopt(argc, argv, "C:cK:o:")) != -1) {
1799 case 'c': /* deprecated, remove after 0.3 */
1816 if (argc == 0 || (tls && crtpath == NULL))
1819 host = parse_addr(argv[0], &user, &port, &path);
1820 if (path == NULL && argv[1] != NULL) /* drop argv[1] after 0.3 */
1822 if (outfile && path == NULL)
1825 signal(SIGPIPE, SIG_IGN);
1829 signal(SIGWINCH, tty_resized);
1832 if ((evb = evbuffer_new()) == NULL)
1833 fatal("evbuffer_new");
1835 if ((buf = evbuffer_new()) == NULL)
1836 fatal("evbuffer_new");
1838 if ((dirbuf = evbuffer_new()) == NULL)
1839 fatal("evbuferr_new");
1841 do_connect(host, port, user);
1843 cd_or_fetch(path, outfile);
1848 char *line, *argv[16] = {0}, **ap;
1850 if ((line = read_line("kamiftp> ")) == NULL)
1853 if ((argc = parsecmd(line, argv, nitems(argv) - 1)) == -1) {
1859 excmd(argc, (const char **)argv);
1862 fprintf(stderr, "\a");