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 char *
385 check(uint8_t type, uint16_t tag)
387 uint16_t rtag;
388 uint8_t rtype;
390 rtype = np_read8(buf);
391 rtag = np_read16(buf);
392 if (rtype == type) {
393 if (rtag != tag)
394 errx(1, "expected tag 0x%x, got 0x%x", tag, rtag);
395 return NULL;
398 if (rtype == Rerror)
399 return np_readstr(buf);
401 errx(1, "expected %s, got msg type %s",
402 pp_msg_type(type), pp_msg_type(rtype));
405 static void
406 do_version(void)
408 char *version;
410 tversion(VERSION9P, MSIZE9P);
411 do_send();
412 recv_msg();
413 expect2(Rversion, NOTAG);
415 msize = np_read32(buf);
416 version = np_readstr(buf);
418 if (msize > MSIZE9P)
419 errx(1, "got unexpected msize: %d", msize);
420 if (strcmp(version, VERSION9P))
421 errx(1, "unexpected 9p version: %s", version);
423 free(version);
424 ASSERT_EMPTYBUF();
427 static void
428 do_attach(const char *path)
430 const char *user;
431 struct qid qid;
433 if (path == NULL)
434 path = "/";
435 if ((user = getenv("USER")) == NULL)
436 user = "flan";
438 tattach(pwdfid, NOFID, user, path);
439 do_send();
440 recv_msg();
441 expect2(Rattach, iota_tag);
442 np_read_qid(buf, &qid);
444 ASSERT_EMPTYBUF();
447 static uint32_t
448 do_open(uint32_t fid, uint8_t mode)
450 struct qid qid;
451 uint32_t iounit;
453 topen(fid, mode);
454 do_send();
455 recv_msg();
456 expect2(Ropen, iota_tag);
458 np_read_qid(buf, &qid);
459 iounit = np_read32(buf);
461 ASSERT_EMPTYBUF();
463 return iounit;
466 static void
467 do_clunk(uint32_t fid)
469 tclunk(fid);
470 do_send();
471 recv_msg();
472 expect2(Rclunk, iota_tag);
474 ASSERT_EMPTYBUF();
477 static void
478 dup_fid(int fid, int nfid)
480 uint16_t nwqid;
482 twalk(fid, nfid, NULL, 0);
483 do_send();
484 recv_msg();
485 expect2(Rwalk, iota_tag);
487 nwqid = np_read16(buf);
488 assert(nwqid == 0);
490 ASSERT_EMPTYBUF();
493 static char *
494 walk_path(int fid, int newfid, const char *path, int *missing,
495 struct qid *qid)
497 char *wnames[MAXWELEM], *p, *t, *errstr;
498 size_t nwname, i;
499 uint16_t nwqid;
501 if ((p = strdup(path)) == NULL)
502 err(1, "strdup");
503 t = p;
505 /* strip initial ./ */
506 if (t[0] == '.' && t[1] == '/')
507 t += 2;
509 for (nwname = 0; nwname < nitems(wnames) &&
510 (wnames[nwname] = strsep(&t, "/")) != NULL;) {
511 if (*wnames[nwname] != '\0')
512 nwname++;
515 twalk(fid, newfid, (const char **)wnames, nwname);
516 do_send();
517 recv_msg();
519 *missing = nwname;
520 if ((errstr = check(Rwalk, iota_tag)) != NULL)
521 return errstr;
523 nwqid = np_read16(buf);
524 assert(nwqid <= nwname);
526 /* consume all qids */
527 for (i = 0; i < nwname; ++i)
528 np_read_qid(buf, qid);
530 free(p);
532 *missing = nwname - nwqid;
533 return NULL;
536 static void
537 do_stat(int fid, struct np_stat *st)
539 tstat(fid);
540 do_send();
541 recv_msg();
542 expect2(Rstat, iota_tag);
544 if (np_read_stat(buf, st) == -1)
545 errx(1, "invalid stat struct read");
547 ASSERT_EMPTYBUF();
550 static size_t
551 do_read(int fid, uint64_t off, uint32_t count, void *data)
553 uint32_t r;
555 tread(fid, off, count);
556 do_send();
557 recv_msg();
558 expect2(Rread, iota_tag);
560 r = np_read32(buf);
561 assert(r == EVBUFFER_LENGTH(buf));
562 assert(r <= count);
563 evbuffer_remove(buf, data, r);
565 ASSERT_EMPTYBUF();
567 return r;
570 static void
571 draw_progress(const char *pre, const struct progress *p)
573 struct winsize ws;
574 int i, l, w;
575 double perc;
577 perc = 100.0 * p->done / p->max;
578 if (!tty_p) {
579 fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
580 return;
583 if (resized) {
584 resized = 0;
586 if (ioctl(0, TIOCGWINSZ, &ws) == -1)
587 return;
588 tty_width = ws.ws_col;
590 w = tty_width;
592 if (pre == NULL ||
593 ((l = printf("\r%s ", pre)) == -1 || l >= w))
594 return;
596 w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
597 if (w < 0) {
598 printf("%4d%%\n", (int)perc);
599 return;
602 printf("|");
604 l = w * MIN(100.0, perc) / 100.0;
605 for (i = 0; i < l; i++)
606 printf("*");
607 for (; i < w; i++)
608 printf(" ");
609 printf("|%4d%%", (int)perc);
611 fflush(stdout);
614 static void
615 fetch_fid(int fid, int fd, const char *name)
617 struct progress p = {0};
618 struct np_stat st;
619 size_t r;
620 char buf[BUFSIZ];
622 do_stat(fid, &st);
623 do_open(fid, KOREAD);
625 p.max = st.length;
626 for (;;) {
627 size_t off;
628 ssize_t nw;
630 r = do_read(fid, p.done, sizeof(buf), buf);
631 if (r == 0)
632 break;
634 for (off = 0; off < r; off += nw)
635 if ((nw = write(fd, buf + off, r - off)) == 0 ||
636 nw == -1)
637 err(1, "write");
639 p.done += r;
640 draw_progress(name, &p);
642 #if 0
643 /* throttle, for debugging purpose */
645 struct timespec ts = { 0, 500000000 };
646 nanosleep(&ts, NULL);
648 #endif
651 putchar('\n');
653 do_clunk(fid);
656 static void
657 do_tls_connect(const char *host, const char *port)
659 int handshake;
661 if ((tlsconf = tls_config_new()) == NULL)
662 fatalx("tls_config_new");
663 tls_config_insecure_noverifycert(tlsconf);
664 tls_config_insecure_noverifyname(tlsconf);
665 if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
666 fatalx("can't load certs (%s, %s)", crtpath, keypath);
668 if ((ctx = tls_client()) == NULL)
669 fatal("tls_client");
670 if (tls_configure(ctx, tlsconf) == -1)
671 fatalx("tls_configure: %s", tls_error(ctx));
673 if (tls_connect(ctx, host, port) == -1)
674 fatalx("can't connect to %s:%s: %s", host, port,
675 tls_error(ctx));
677 for (handshake = 0; !handshake;) {
678 switch (tls_handshake(ctx)) {
679 case -1:
680 fatalx("tls_handshake: %s", tls_error(ctx));
681 case 0:
682 handshake = 1;
683 break;
688 static void
689 do_ctxt_connect(const char *host, const char *port)
691 struct addrinfo hints, *res, *res0;
692 int error, saved_errno;
693 const char *cause = NULL;
695 memset(&hints, 0, sizeof(hints));
696 hints.ai_family = AF_UNSPEC;
697 hints.ai_socktype = SOCK_STREAM;
698 error = getaddrinfo(host, port, &hints, &res0);
699 if (error)
700 errx(1, "%s", gai_strerror(error));
702 sock = -1;
703 for (res = res0; res != NULL; res = res->ai_next) {
704 sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
705 res->ai_protocol);
706 if (sock == -1) {
707 cause = "socket";
708 continue;
711 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
712 cause = "connect";
713 saved_errno = errno;
714 close(sock);
715 errno = saved_errno;
716 sock = -1;
717 continue;
720 break;
723 if (sock == -1)
724 err(1, "%s", cause);
725 freeaddrinfo(res0);
728 static void
729 do_connect(const char *connspec, const char *path)
731 char *host, *colon;
732 const char *port;
734 host = xstrdup(connspec);
735 if ((colon = strchr(host, ':')) != NULL) {
736 *colon = '\0';
737 port = ++colon;
738 } else
739 port = "1337";
741 printf("connecting to %s:%s...", host, port);
742 fflush(stdout);
744 if (tls)
745 do_tls_connect(host, port);
746 else
747 do_ctxt_connect(host, port);
749 printf(" done!\n");
751 do_version();
752 do_attach(path);
754 free(host);
757 static void
758 cmd_bell(int argc, const char **argv)
760 if (argc == 0) {
761 bell = !bell;
762 if (bell)
763 puts("bell mode enabled");
764 else
765 puts("bell mode disabled");
766 return;
769 if (argc != 1)
770 goto usage;
772 if (!strcmp(*argv, "on")) {
773 bell = 1;
774 puts("bell mode enabled");
775 return;
778 if (!strcmp(*argv, "off")) {
779 bell = 0;
780 puts("bell mode disabled");
781 return;
784 usage:
785 printf("bell [on | off]\n");
788 static void
789 cmd_bye(int argc, const char **argv)
791 log_warnx("bye\n");
792 exit(0);
795 static void
796 cmd_cd(int argc, const char **argv)
798 struct qid qid;
799 int nfid, miss;
800 char *errstr;
802 if (argc != 1) {
803 printf("usage: cd remote-path\n");
804 return;
807 nfid = pwdfid+1;
808 errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
809 if (errstr != NULL) {
810 printf("%s: %s\n", argv[0], errstr);
811 free(errstr);
812 return;
815 if (miss != 0 || !(qid.type & QTDIR)) {
816 printf("%s: not a directory\n", argv[0]);
817 do_clunk(nfid);
818 return;
821 do_clunk(pwdfid);
822 pwdfid = nfid;
825 static void
826 cmd_get(int argc, const char **argv)
828 struct qid qid;
829 const char *l;
830 char *errstr;
831 int nfid, fd, miss;
833 if (argc != 1 && argc != 2) {
834 printf("usage: get remote-file [local-file]\n");
835 return;
838 if (argc == 2)
839 l = argv[1];
840 else if ((l = strrchr(argv[0], '/')) != NULL)
841 l++; /* skip / */
842 else
843 l = argv[0];
845 nfid = pwdfid+1;
846 errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
847 if (errstr != NULL) {
848 printf("%s: %s\n", argv[0], errstr);
849 free(errstr);
850 return;
853 if (miss != 0 || qid.type != 0) {
854 printf("%s: not a file\n", argv[0]);
855 do_clunk(nfid);
856 return;
859 if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
860 warn("can't open %s", l);
861 do_clunk(nfid);
862 return;
865 fetch_fid(nfid, fd, l);
866 close(fd);
869 static void
870 cmd_lcd(int argc, const char **argv)
872 const char *dir;
874 if (argc > 1) {
875 printf("lcd takes only one argument\n");
876 return;
879 if (argc == 1)
880 dir = *argv;
882 if (argc == 0 && (dir = getenv("HOME")) == NULL) {
883 printf("HOME is not defined\n");
884 return;
887 if (chdir(dir) == -1)
888 printf("cd: %s: %s\n", dir, strerror(errno));
891 static void
892 cmd_lpwd(int argc, const char **argv)
894 char path[PATH_MAX];
896 if (getcwd(path, sizeof(path)) == NULL) {
897 printf("lpwd: %s\n", strerror(errno));
898 return;
901 printf("%s\n", path);
904 static void
905 cmd_ls(int argc, const char **argv)
907 struct np_stat st;
908 uint64_t off = 0;
909 uint32_t len;
910 char fmt[FMT_SCALED_STRSIZE];
912 if (argc != 0) {
913 printf("ls don't take arguments (yet)\n");
914 return;
917 dup_fid(pwdfid, 1);
918 do_open(1, KOREAD);
920 evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
922 for (;;) {
923 tread(1, off, BUFSIZ);
924 do_send();
925 recv_msg();
926 expect2(Rread, iota_tag);
928 len = np_read32(buf);
929 if (len == 0)
930 break;
932 evbuffer_add_buffer(dirbuf, buf);
933 off += len;
935 ASSERT_EMPTYBUF();
938 while (EVBUFFER_LENGTH(dirbuf) != 0) {
939 if (np_read_stat(dirbuf, &st) == -1)
940 errx(1, "invalid stat struct read");
942 if (fmt_scaled(st.length, fmt) == -1)
943 strlcpy(fmt, "xxx", sizeof(fmt));
945 printf("%4s %8s %s\n", pp_qid_type(st.qid.type), fmt, st.name);
947 free(st.name);
948 free(st.uid);
949 free(st.gid);
950 free(st.muid);
953 do_clunk(1);
956 static void
957 cmd_page(int argc, const char **argv)
959 struct qid qid;
960 int nfid, tmpfd, miss;
961 char sfn[24], p[PATH_MAX], *name, *errstr;
963 if (argc != 1) {
964 puts("usage: page file");
965 return;
968 nfid = pwdfid+1;
969 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
970 if (errstr != NULL) {
971 printf("%s: %s\n", *argv, errstr);
972 free(errstr);
973 return;
976 if (miss != 0 || qid.type != 0) {
977 printf("%s: not a file\n", *argv);
978 do_clunk(nfid);
979 return;
982 strlcpy(sfn, "/tmp/kamiftp.XXXXXXXXXX", sizeof(sfn));
983 if ((tmpfd = mkstemp(sfn)) == -1) {
984 warn("mkstemp %s", sfn);
985 do_clunk(nfid);
986 return;
989 strlcpy(p, *argv, sizeof(p));
990 name = basename(p);
991 fetch_fid(nfid, tmpfd, name);
992 close(tmpfd);
993 spawn("less", sfn, NULL);
994 unlink(sfn);
997 static void
998 cmd_verbose(int argc, const char **argv)
1000 if (argc == 0) {
1001 log_setverbose(!log_getverbose());
1002 if (log_getverbose())
1003 puts("verbose mode enabled");
1004 else
1005 puts("verbose mode disabled");
1006 return;
1009 if (argc != 1)
1010 goto usage;
1012 if (!strcmp(*argv, "on")) {
1013 log_setverbose(1);
1014 puts("verbose mode enabled");
1015 return;
1018 if (!strcmp(*argv, "off")) {
1019 log_setverbose(0);
1020 puts("verbose mode disabled");
1021 return;
1024 usage:
1025 printf("verbose [on | off]\n");
1028 static void
1029 excmd(int argc, const char **argv)
1031 struct cmd {
1032 const char *name;
1033 void (*fn)(int, const char **);
1034 } cmds[] = {
1035 {"bell", cmd_bell},
1036 {"bye", cmd_bye},
1037 {"cd", cmd_cd},
1038 {"get", cmd_get},
1039 {"lcd", cmd_lcd},
1040 {"lpwd", cmd_lpwd},
1041 {"ls", cmd_ls},
1042 {"page", cmd_page},
1043 {"quit", cmd_bye},
1044 {"verbose", cmd_verbose},
1046 size_t i;
1048 if (argc == 0)
1049 return;
1050 for (i = 0; i < nitems(cmds); ++i) {
1051 if (!strcmp(cmds[i].name, *argv)) {
1052 cmds[i].fn(argc-1, argv+1);
1053 return;
1057 log_warnx("unknown command %s", *argv);
1060 int
1061 main(int argc, char **argv)
1063 int ch;
1065 log_init(1, LOG_DAEMON);
1066 log_setverbose(0);
1067 log_procinit(getprogname());
1069 while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
1070 switch (ch) {
1071 case 'C':
1072 crtpath = optarg;
1073 break;
1074 case 'c':
1075 tls = 1;
1076 break;
1077 case 'K':
1078 keypath = optarg;
1079 break;
1080 default:
1081 usage(1);
1084 argc -= optind;
1085 argv += optind;
1087 if (argc == 0)
1088 usage(1);
1090 if (isatty(1)) {
1091 tty_p = 1;
1092 resized = 1;
1093 signal(SIGWINCH, tty_resized);
1096 if ((evb = evbuffer_new()) == NULL)
1097 fatal("evbuffer_new");
1099 if ((buf = evbuffer_new()) == NULL)
1100 fatal("evbuffer_new");
1102 if ((dirbuf = evbuffer_new()) == NULL)
1103 fatal("evbuferr_new");
1105 do_connect(argv[0], argv[1]);
1107 for (;;) {
1108 int argc = 0;
1109 char *line, *argv[16] = {0}, **ap;
1111 if ((line = read_line("kamiftp> ")) == NULL)
1112 break;
1114 for (argc = 0, ap = argv; ap < &argv[15] &&
1115 (*ap = strsep(&line, " \t")) != NULL;) {
1116 if (**ap != '\0')
1117 ap++, argc++;
1119 excmd(argc, (const char **)argv);
1121 if (bell) {
1122 printf("\a");
1123 fflush(stdout);
1126 free(line);
1129 printf("\n");