Blob


1 /*
2 * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "compat.h"
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <inttypes.h>
28 #include <netdb.h>
29 #include <libgen.h>
30 #include <limits.h>
31 #include <signal.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <syslog.h>
36 #include <tls.h>
37 #include <unistd.h>
39 #ifdef HAVE_READLINE
40 #include <readline/readline.h>
41 #include <readline/history.h>
42 #endif
44 #ifndef nitems
45 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 #endif
48 #include "9pclib.h"
49 #include "kami.h"
50 #include "utils.h"
51 #include "log.h"
53 /* flags */
54 int tls;
55 const char *crtpath;
56 const char *keypath;
58 /* state */
59 struct tls_config *tlsconf;
60 struct tls *ctx;
61 int sock;
62 struct evbuffer *buf;
63 struct evbuffer *dirbuf;
64 uint32_t msize;
65 int bell;
67 volatile sig_atomic_t resized;
68 int tty_p;
69 int tty_width;
71 struct np_stat {
72 uint16_t type;
73 uint32_t dev;
74 struct qid qid;
75 uint32_t mode;
76 uint32_t atime;
77 uint32_t mtime;
78 uint64_t length;
79 char *name;
80 char *uid;
81 char *gid;
82 char *muid;
83 };
85 struct progress {
86 uint64_t max;
87 uint64_t done;
88 };
90 int pwdfid;
92 #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
94 #if !HAVE_READLINE
95 char *
96 readline(const char *prompt)
97 {
98 char *ch, *line = NULL;
99 size_t linesize = 0;
100 ssize_t linelen;
102 printf("%s", prompt);
103 fflush(stdout);
105 linelen = getline(&line, &linesize, stdin);
106 if (linelen == -1)
107 return NULL;
109 if ((ch = strchr(line, '\n')) != NULL)
110 *ch = '\0';
111 return line;
114 void
115 add_history(const char *line)
117 return;
119 #endif
121 static char *
122 read_line(const char *prompt)
124 char *line;
126 again:
127 if ((line = readline(prompt)) == NULL)
128 return NULL;
129 /* XXX: trim spaces? */
130 if (*line == '\0') {
131 free(line);
132 goto again;
135 add_history(line);
136 return line;
139 static void
140 spawn(const char *argv0, ...)
142 pid_t pid;
143 size_t i;
144 int status;
145 const char *argv[16], *last;
146 va_list ap;
148 memset(argv, 0, sizeof(argv));
150 va_start(ap, argv0);
151 argv[0] = argv0;
152 for (i = 1; i < nitems(argv); ++i) {
153 last = va_arg(ap, const char *);
154 if (last == NULL)
155 break;
156 argv[i] = last;
158 va_end(ap);
160 assert(last == NULL);
162 switch (pid = fork()) {
163 case -1:
164 err(1, "fork");
165 case 0: /* child */
166 execvp(argv[0], (char *const *)argv);
167 err(1, "execvp");
168 default:
169 waitpid(pid, &status, 0);
173 static void
174 tty_resized(int signo)
176 resized = 1;
179 static void __dead
180 usage(int ret)
182 fprintf(stderr, "usage: %s [-c] [-C cert] [-K key] "
183 "host[:port] [path]\n", getprogname());
184 fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
185 exit(ret);
188 static void
189 do_send(void)
191 const void *buf;
192 size_t nbytes;
193 ssize_t r;
195 while (EVBUFFER_LENGTH(evb) != 0) {
196 buf = EVBUFFER_DATA(evb);
197 nbytes = EVBUFFER_LENGTH(evb);
199 if (ctx == NULL) {
200 r = write(sock, buf, nbytes);
201 if (r == 0 || r == -1)
202 errx(1, "EOF");
203 } else {
204 r = tls_write(ctx, buf, nbytes);
205 if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
206 continue;
207 if (r == -1)
208 errx(1, "tls: %s", tls_error(ctx));
211 evbuffer_drain(evb, r);
215 static void
216 mustread(void *d, size_t len)
218 ssize_t r;
220 while (len != 0) {
221 if (ctx == NULL) {
222 r = read(sock, d, len);
223 if (r == 0 || r == -1)
224 errx(1, "EOF");
225 } else {
226 r = tls_read(ctx, d, len);
227 if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
228 continue;
229 if (r == -1)
230 errx(1, "tls: %s", tls_error(ctx));
233 d += r;
234 len -= r;
238 static void
239 recv_msg(void)
241 uint32_t len, l;
242 char tmp[BUFSIZ];
244 mustread(&len, sizeof(len));
245 len = le32toh(len);
246 if (len < HEADERSIZE)
247 errx(1, "read message of invalid length %d", len);
249 len -= 4; /* skip the length just read */
251 while (len != 0) {
252 l = MIN(len, sizeof(tmp));
253 mustread(tmp, l);
254 len -= l;
255 evbuffer_add(buf, tmp, l);
259 static uint64_t
260 np_read64(struct evbuffer *buf)
262 uint64_t n;
264 evbuffer_remove(buf, &n, sizeof(n));
265 return le64toh(n);
268 static uint32_t
269 np_read32(struct evbuffer *buf)
271 uint32_t n;
273 evbuffer_remove(buf, &n, sizeof(n));
274 return le32toh(n);
277 static uint16_t
278 np_read16(struct evbuffer *buf)
280 uint16_t n;
282 evbuffer_remove(buf, &n, sizeof(n));
283 return le16toh(n);
286 static uint16_t
287 np_read8(struct evbuffer *buf)
289 uint8_t n;
291 evbuffer_remove(buf, &n, sizeof(n));
292 return n;
295 static char *
296 np_readstr(struct evbuffer *buf)
298 uint16_t len;
299 char *str;
301 len = np_read16(buf);
302 assert(EVBUFFER_LENGTH(buf) >= len);
304 if ((str = calloc(1, len+1)) == NULL)
305 err(1, "calloc");
306 evbuffer_remove(buf, str, len);
307 return str;
310 static void
311 np_read_qid(struct evbuffer *buf, struct qid *qid)
313 assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
315 qid->type = np_read8(buf);
316 qid->vers = np_read32(buf);
317 qid->path = np_read64(buf);
320 static int
321 np_read_stat(struct evbuffer *buf, struct np_stat *st)
323 uint16_t size;
325 memset(st, 0, sizeof(*st));
327 size = np_read16(buf);
328 if (size > EVBUFFER_LENGTH(buf))
329 return -1;
331 st->type = np_read16(buf);
332 st->dev = np_read32(buf);
333 np_read_qid(buf, &st->qid);
334 st->mode = np_read32(buf);
335 st->atime = np_read32(buf);
336 st->mtime = np_read32(buf);
337 st->length = np_read64(buf);
338 st->name = np_readstr(buf);
339 st->uid = np_readstr(buf);
340 st->gid = np_readstr(buf);
341 st->muid = np_readstr(buf);
343 return 0;
346 static void
347 expect(uint8_t type)
349 uint8_t t;
351 t = np_read8(buf);
352 if (t == type)
353 return;
355 if (t == Rerror) {
356 char *err;
358 /* skip tag */
359 np_read16(buf);
361 err = np_readstr(buf);
362 errx(1, "expected %s, got error %s",
363 pp_msg_type(type), err);
366 errx(1, "expected %s, got msg type %s",
367 pp_msg_type(type), pp_msg_type(t));
370 static void
371 expect2(uint8_t type, uint16_t tag)
373 uint16_t t;
375 expect(type);
377 t = np_read16(buf);
378 if (t == tag)
379 return;
381 errx(1, "expected tag 0x%x, got 0x%x", tag, t);
384 static void
385 do_version(void)
387 char *version;
389 tversion(VERSION9P, MSIZE9P);
390 do_send();
391 recv_msg();
392 expect2(Rversion, NOTAG);
394 msize = np_read32(buf);
395 version = np_readstr(buf);
397 if (msize > MSIZE9P)
398 errx(1, "got unexpected msize: %d", msize);
399 if (strcmp(version, VERSION9P))
400 errx(1, "unexpected 9p version: %s", version);
402 free(version);
403 ASSERT_EMPTYBUF();
406 static void
407 do_attach(const char *path)
409 const char *user;
410 struct qid qid;
412 if (path == NULL)
413 path = "/";
414 if ((user = getenv("USER")) == NULL)
415 user = "flan";
417 tattach(pwdfid, NOFID, user, path);
418 do_send();
419 recv_msg();
420 expect2(Rattach, iota_tag);
421 np_read_qid(buf, &qid);
423 ASSERT_EMPTYBUF();
426 static uint32_t
427 do_open(uint32_t fid, uint8_t mode)
429 struct qid qid;
430 uint32_t iounit;
432 topen(fid, mode);
433 do_send();
434 recv_msg();
435 expect2(Ropen, iota_tag);
437 np_read_qid(buf, &qid);
438 iounit = np_read32(buf);
440 ASSERT_EMPTYBUF();
442 return iounit;
445 static void
446 do_clunk(uint32_t fid)
448 tclunk(fid);
449 do_send();
450 recv_msg();
451 expect2(Rclunk, iota_tag);
453 ASSERT_EMPTYBUF();
456 static void
457 dup_fid(int fid, int nfid)
459 uint16_t nwqid;
461 twalk(fid, nfid, NULL, 0);
462 do_send();
463 recv_msg();
464 expect2(Rwalk, iota_tag);
466 nwqid = np_read16(buf);
467 assert(nwqid == 0);
469 ASSERT_EMPTYBUF();
472 static int
473 walk_path(int fid, int newfid, const char *path, struct qid *qid)
475 char *wnames[MAXWELEM], *p, *t;
476 size_t nwname, i;
477 uint16_t nwqid;
479 if ((p = strdup(path)) == NULL)
480 err(1, "strdup");
481 t = p;
483 /* strip initial ./ */
484 if (t[0] == '.' && t[1] == '/')
485 t += 2;
487 for (nwname = 0; nwname < nitems(wnames) &&
488 (wnames[nwname] = strsep(&t, "/")) != NULL;) {
489 if (*wnames[nwname] != '\0')
490 nwname++;
493 twalk(fid, newfid, (const char **)wnames, nwname);
494 do_send();
495 recv_msg();
496 expect2(Rwalk, iota_tag);
498 nwqid = np_read16(buf);
499 assert(nwqid <= nwname);
501 /* consume all qids */
502 for (i = 0; i < nwname; ++i)
503 np_read_qid(buf, qid);
505 free(p);
507 return nwqid == nwname;
510 static void
511 do_stat(int fid, struct np_stat *st)
513 tstat(fid);
514 do_send();
515 recv_msg();
516 expect2(Rstat, iota_tag);
518 if (np_read_stat(buf, st) == -1)
519 errx(1, "invalid stat struct read");
521 ASSERT_EMPTYBUF();
524 static size_t
525 do_read(int fid, uint64_t off, uint32_t count, void *data)
527 uint32_t r;
529 tread(fid, off, count);
530 do_send();
531 recv_msg();
532 expect2(Rread, iota_tag);
534 r = np_read32(buf);
535 assert(r == EVBUFFER_LENGTH(buf));
536 assert(r <= count);
537 evbuffer_remove(buf, data, r);
539 ASSERT_EMPTYBUF();
541 return r;
544 static void
545 draw_progress(const char *pre, const struct progress *p)
547 struct winsize ws;
548 int i, l, w;
549 double perc;
551 perc = 100.0 * p->done / p->max;
552 if (!tty_p) {
553 fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
554 return;
557 if (resized) {
558 resized = 0;
560 if (ioctl(0, TIOCGWINSZ, &ws) == -1)
561 return;
562 tty_width = ws.ws_col;
564 w = tty_width;
566 if (pre == NULL ||
567 ((l = printf("\r%s ", pre)) == -1 || l >= w))
568 return;
570 w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
571 if (w < 0) {
572 printf("%4d%%\n", (int)perc);
573 return;
576 printf("|");
578 l = w * MIN(100.0, perc) / 100.0;
579 for (i = 0; i < l; i++)
580 printf("*");
581 for (; i < w; i++)
582 printf(" ");
583 printf("|%4d%%", (int)perc);
585 fflush(stdout);
588 static void
589 fetch_fid(int fid, int fd, const char *name)
591 struct progress p = {0};
592 struct np_stat st;
593 size_t r;
594 char buf[BUFSIZ];
596 do_stat(fid, &st);
597 do_open(fid, KOREAD);
599 p.max = st.length;
600 for (;;) {
601 size_t siz, off;
602 ssize_t nw;
604 r = do_read(fid, p.done, sizeof(buf), buf);
605 if (r == 0)
606 break;
608 siz = sizeof(buf);
609 for (off = 0; off < siz; off += nw)
610 if ((nw = write(fd, buf + off, siz - off)) == 0 ||
611 nw == -1)
612 err(1, "write");
614 p.done += r;
615 draw_progress(name, &p);
617 #if 0
618 /* throttle, for debugging purpose */
620 struct timespec ts = { 0, 500000000 };
621 nanosleep(&ts, NULL);
623 #endif
626 putchar('\n');
628 do_clunk(fid);
631 static void
632 do_tls_connect(const char *host, const char *port)
634 int handshake;
636 if ((tlsconf = tls_config_new()) == NULL)
637 fatalx("tls_config_new");
638 tls_config_insecure_noverifycert(tlsconf);
639 tls_config_insecure_noverifyname(tlsconf);
640 if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
641 fatalx("can't load certs (%s, %s)", crtpath, keypath);
643 if ((ctx = tls_client()) == NULL)
644 fatal("tls_client");
645 if (tls_configure(ctx, tlsconf) == -1)
646 fatalx("tls_configure: %s", tls_error(ctx));
648 if (tls_connect(ctx, host, port) == -1)
649 fatalx("can't connect to %s:%s: %s", host, port,
650 tls_error(ctx));
652 for (handshake = 0; !handshake;) {
653 switch (tls_handshake(ctx)) {
654 case -1:
655 fatalx("tls_handshake: %s", tls_error(ctx));
656 case 0:
657 handshake = 1;
658 break;
663 static void
664 do_ctxt_connect(const char *host, const char *port)
666 struct addrinfo hints, *res, *res0;
667 int error, saved_errno;
668 const char *cause = NULL;
670 memset(&hints, 0, sizeof(hints));
671 hints.ai_family = AF_UNSPEC;
672 hints.ai_socktype = SOCK_STREAM;
673 error = getaddrinfo(host, port, &hints, &res0);
674 if (error)
675 errx(1, "%s", gai_strerror(error));
677 sock = -1;
678 for (res = res0; res != NULL; res = res->ai_next) {
679 sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
680 res->ai_protocol);
681 if (sock == -1) {
682 cause = "socket";
683 continue;
686 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
687 cause = "connect";
688 saved_errno = errno;
689 close(sock);
690 errno = saved_errno;
691 sock = -1;
692 continue;
695 break;
698 if (sock == -1)
699 err(1, "%s", cause);
700 freeaddrinfo(res0);
703 static void
704 do_connect(const char *connspec, const char *path)
706 char *host, *colon;
707 const char *port;
709 host = xstrdup(connspec);
710 if ((colon = strchr(host, ':')) != NULL) {
711 *colon = '\0';
712 port = ++colon;
713 } else
714 port = "1337";
716 printf("connecting to %s:%s...", host, port);
717 fflush(stdout);
719 if (tls)
720 do_tls_connect(host, port);
721 else
722 do_ctxt_connect(host, port);
724 printf(" done!\n");
726 do_version();
727 do_attach(path);
729 free(host);
732 static void
733 cmd_bell(int argc, const char **argv)
735 if (argc == 0) {
736 bell = !bell;
737 if (bell)
738 puts("bell mode enabled");
739 else
740 puts("bell mode disabled");
741 return;
744 if (argc != 1)
745 goto usage;
747 if (!strcmp(*argv, "on")) {
748 bell = 1;
749 puts("bell mode enabled");
750 return;
753 if (!strcmp(*argv, "off")) {
754 bell = 0;
755 puts("bell mode disabled");
756 return;
759 usage:
760 printf("bell [on | off]\n");
763 static void
764 cmd_bye(int argc, const char **argv)
766 log_warnx("bye\n");
767 exit(0);
770 static void
771 cmd_cd(int argc, const char **argv)
773 struct qid qid;
774 int nfid;
776 if (argc != 1) {
777 printf("usage: cd remote-path\n");
778 return;
781 nfid = pwdfid+1;
782 if (walk_path(pwdfid, nfid, argv[0], &qid) == -1 ||
783 !(qid.type & QTDIR)) {
784 printf("can't cd %s\n", argv[0]);
785 do_clunk(nfid);
786 } else {
787 do_clunk(pwdfid);
788 pwdfid = nfid;
792 static void
793 cmd_get(int argc, const char **argv)
795 struct qid qid;
796 const char *l;
797 int nfid;
798 int fd;
800 if (argc != 1 && argc != 2) {
801 printf("usage: get remote-file [local-file]\n");
802 return;
805 if (argc == 2)
806 l = argv[1];
807 else if ((l = strrchr(argv[0], '/')) != NULL)
808 l++; /* skip / */
809 else
810 l = argv[0];
812 nfid = pwdfid+1;
813 if (walk_path(pwdfid, nfid, argv[0], &qid) == -1) {
814 printf("can't fetch %s\n", argv[0]);
815 return;
818 if (qid.type != 0) {
819 printf("can't fetch %s\n", argv[0]);
820 do_clunk(nfid);
821 return;
824 if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
825 warn("can't open %s", l);
826 do_clunk(nfid);
827 return;
830 fetch_fid(nfid, fd, l);
831 close(fd);
834 static void
835 cmd_lcd(int argc, const char **argv)
837 const char *dir;
839 if (argc > 1) {
840 printf("lcd takes only one argument\n");
841 return;
844 if (argc == 1)
845 dir = *argv;
847 if (argc == 0 && (dir = getenv("HOME")) == NULL) {
848 printf("HOME is not defined\n");
849 return;
852 if (chdir(dir) == -1)
853 printf("cd: %s: %s\n", dir, strerror(errno));
856 static void
857 cmd_lpwd(int argc, const char **argv)
859 char path[PATH_MAX];
861 if (getcwd(path, sizeof(path)) == NULL) {
862 printf("lpwd: %s\n", strerror(errno));
863 return;
866 printf("%s\n", path);
869 static void
870 cmd_ls(int argc, const char **argv)
872 struct np_stat st;
873 uint64_t off = 0;
874 uint32_t len;
875 char fmt[FMT_SCALED_STRSIZE];
877 if (argc != 0) {
878 printf("ls don't take arguments (yet)\n");
879 return;
882 dup_fid(pwdfid, 1);
883 do_open(1, KOREAD);
885 evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
887 for (;;) {
888 tread(1, off, BUFSIZ);
889 do_send();
890 recv_msg();
891 expect2(Rread, iota_tag);
893 len = np_read32(buf);
894 if (len == 0)
895 break;
897 evbuffer_add_buffer(dirbuf, buf);
898 off += len;
900 ASSERT_EMPTYBUF();
903 while (EVBUFFER_LENGTH(dirbuf) != 0) {
904 if (np_read_stat(dirbuf, &st) == -1)
905 errx(1, "invalid stat struct read");
907 if (fmt_scaled(st.length, fmt) == -1)
908 strlcpy(fmt, "xxx", sizeof(fmt));
910 printf("%4s %8s %s\n", pp_qid_type(st.qid.type), fmt, st.name);
912 free(st.name);
913 free(st.uid);
914 free(st.gid);
915 free(st.muid);
918 do_clunk(1);
921 static void
922 cmd_page(int argc, const char **argv)
924 struct qid qid;
925 int nfid, tmpfd;
926 char sfn[24], p[PATH_MAX], *name;
928 if (argc != 1) {
929 puts("usage: page file");
930 return;
933 nfid = pwdfid+1;
934 if (walk_path(pwdfid, nfid, *argv, &qid) == -1) {
935 printf("can't fetch %s\n", *argv);
936 return;
939 if (qid.type != 0) {
940 printf("can't page file type %s\n", pp_qid_type(qid.type));
941 do_clunk(nfid);
942 return;
945 strlcpy(sfn, "/tmp/kamiftp.XXXXXXXXXX", sizeof(sfn));
946 if ((tmpfd = mkstemp(sfn)) == -1) {
947 warn("mkstemp %s", sfn);
948 do_clunk(nfid);
949 return;
952 strlcpy(p, *argv, sizeof(p));
953 name = basename(p);
954 fetch_fid(nfid, tmpfd, name);
955 close(tmpfd);
956 spawn("less", sfn, NULL);
957 unlink(sfn);
960 static void
961 cmd_verbose(int argc, const char **argv)
963 if (argc == 0) {
964 log_setverbose(!log_getverbose());
965 if (log_getverbose())
966 puts("verbose mode enabled");
967 else
968 puts("verbose mode disabled");
969 return;
972 if (argc != 1)
973 goto usage;
975 if (!strcmp(*argv, "on")) {
976 log_setverbose(1);
977 puts("verbose mode enabled");
978 return;
981 if (!strcmp(*argv, "off")) {
982 log_setverbose(0);
983 puts("verbose mode disabled");
984 return;
987 usage:
988 printf("verbose [on | off]\n");
991 static void
992 excmd(int argc, const char **argv)
994 struct cmd {
995 const char *name;
996 void (*fn)(int, const char **);
997 } cmds[] = {
998 {"bell", cmd_bell},
999 {"bye", cmd_bye},
1000 {"cd", cmd_cd},
1001 {"get", cmd_get},
1002 {"lcd", cmd_lcd},
1003 {"lpwd", cmd_lpwd},
1004 {"ls", cmd_ls},
1005 {"page", cmd_page},
1006 {"quit", cmd_bye},
1007 {"verbose", cmd_verbose},
1009 size_t i;
1011 if (argc == 0)
1012 return;
1013 for (i = 0; i < nitems(cmds); ++i) {
1014 if (!strcmp(cmds[i].name, *argv)) {
1015 cmds[i].fn(argc-1, argv+1);
1016 return;
1020 log_warnx("unknown command %s", *argv);
1023 int
1024 main(int argc, char **argv)
1026 int ch;
1028 log_init(1, LOG_DAEMON);
1029 log_setverbose(0);
1030 log_procinit(getprogname());
1032 while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
1033 switch (ch) {
1034 case 'C':
1035 crtpath = optarg;
1036 break;
1037 case 'c':
1038 tls = 1;
1039 break;
1040 case 'K':
1041 keypath = optarg;
1042 break;
1043 default:
1044 usage(1);
1047 argc -= optind;
1048 argv += optind;
1050 if (argc == 0)
1051 usage(1);
1053 if (isatty(1)) {
1054 tty_p = 1;
1055 resized = 1;
1056 signal(SIGWINCH, tty_resized);
1059 if ((evb = evbuffer_new()) == NULL)
1060 fatal("evbuffer_new");
1062 if ((buf = evbuffer_new()) == NULL)
1063 fatal("evbuffer_new");
1065 if ((dirbuf = evbuffer_new()) == NULL)
1066 fatal("evbuferr_new");
1068 do_connect(argv[0], argv[1]);
1070 for (;;) {
1071 int argc = 0;
1072 char *line, *argv[16] = {0}, **ap;
1074 if ((line = read_line("kamiftp> ")) == NULL)
1075 break;
1077 for (argc = 0, ap = argv; ap < &argv[15] &&
1078 (*ap = strsep(&line, " \t")) != NULL;) {
1079 if (**ap != '\0')
1080 ap++, argc++;
1082 excmd(argc, (const char **)argv);
1084 if (bell) {
1085 printf("\a");
1086 fflush(stdout);
1089 free(line);
1092 printf("\n");