From 311a05f6138cdbbbb2208ca79e0736f0fec030a8 Mon Sep 17 00:00:00 2001 From: Richard Nyberg Date: Sun, 17 Sep 2006 08:19:31 +0000 Subject: [PATCH] btcli add now starts the torrent if not told otherwise. Soem cleanup and improved error output. --- cli/add.c | 19 ++++++++++++++++--- cli/btcli.c | 12 ++++++------ cli/btcli.h | 4 ++-- cli/del.c | 2 +- cli/kill.c | 4 +++- cli/list.c | 20 ++++---------------- cli/start.c | 2 +- cli/stat.c | 4 ++-- cli/stop.c | 2 +- 9 files changed, 36 insertions(+), 33 deletions(-) diff --git a/cli/add.c b/cli/add.c index f0793d7..cc7e383 100644 --- a/cli/add.c +++ b/cli/add.c @@ -27,6 +27,7 @@ usage_add(void) static struct option add_opts [] = { { "help", no_argument, NULL, 'H' }, + { "nostart", no_argument, NULL, 'N'}, { "topdir", no_argument, NULL, 'T'}, {NULL, 0, NULL, 0} }; @@ -34,12 +35,15 @@ static struct option add_opts [] = { void cmd_add(int argc, char **argv) { - int ch, topdir = 0; + int ch, topdir = 0, start = 1; size_t dirlen = 0; char *dir = NULL, *name = NULL; - while ((ch = getopt_long(argc, argv, "d:n:", add_opts, NULL)) != -1) { + while ((ch = getopt_long(argc, argv, "Nd:n:", add_opts, NULL)) != -1) { switch (ch) { + case 'N': + start = 0; + break; case 'T': topdir = 1; break; @@ -64,6 +68,7 @@ cmd_add(int argc, char **argv) btpd_connect(); char *mi; size_t mi_size; + enum ipc_err code; char dpath[PATH_MAX]; struct io_buffer iob; @@ -82,6 +87,14 @@ cmd_add(int argc, char **argv) buf_swrite(&iob, ""); if (realpath(iob.buf, dpath) == NULL) err(1, "realpath '%s'", dpath); - handle_ipc_res(btpd_add(ipc, mi, mi_size, dpath, name), argv[0]); + code = btpd_add(ipc, mi, mi_size, dpath, name); + if (code == 0 && start) { + struct ipc_torrent tspec; + tspec.by_hash = 1; + mi_info_hash(mi, tspec.u.hash); + code = btpd_start(ipc, &tspec); + } + if (code != IPC_OK) + errx(1, "%s", ipc_strerror(code)); return; } diff --git a/cli/btcli.c b/cli/btcli.c index f8b6a81..78a3313 100644 --- a/cli/btcli.c +++ b/cli/btcli.c @@ -11,15 +11,15 @@ btpd_connect(void) } enum ipc_err -handle_ipc_res(enum ipc_err code, const char *target) +handle_ipc_res(enum ipc_err code, const char *cmd, const char *target) { switch (code) { case IPC_OK: break; case IPC_COMMERR: - errx(1, "fatal error in communication with btpd"); + errx(1, "%s", ipc_strerror(code)); default: - warnx("btpd response for '%s': %s", target, ipc_strerror(code)); + warnx("%s '%s': %s", cmd, target, ipc_strerror(code)); } return code; } @@ -61,7 +61,7 @@ torrent_spec(char *arg, struct ipc_torrent *tp) return 1; } -struct { +static struct { const char *name; void (*fun)(int, char **); void (*help)(void); @@ -77,7 +77,7 @@ struct { int ncmds = sizeof(cmd_table) / sizeof(cmd_table[0]); -void +static void usage(void) { printf( @@ -104,7 +104,7 @@ usage(void) exit(1); } -struct option base_opts [] = { +static struct option base_opts [] = { { "help", no_argument, NULL, 'H' }, {NULL, 0, NULL, 0} }; diff --git a/cli/btcli.h b/cli/btcli.h index 5b64662..96c3f4b 100644 --- a/cli/btcli.h +++ b/cli/btcli.h @@ -21,8 +21,8 @@ extern const char *btpd_dir; extern struct ipc *ipc; void btpd_connect(void); -enum ipc_err handle_ipc_res(enum ipc_err err, const char *target); - +enum ipc_err handle_ipc_res(enum ipc_err err, const char *cmd, + const char *target); char tstate_char(enum ipc_tstate ts); int torrent_spec(char *arg, struct ipc_torrent *tp); diff --git a/cli/del.c b/cli/del.c index eaa0a1e..91e53b6 100644 --- a/cli/del.c +++ b/cli/del.c @@ -26,5 +26,5 @@ cmd_del(int argc, char **argv) btpd_connect(); for (int i = 1; i < argc; i++) if (torrent_spec(argv[i], &t)) - handle_ipc_res(btpd_del(ipc, &t), argv[i]); + handle_ipc_res(btpd_del(ipc, &t), "del", argv[i]); } diff --git a/cli/kill.c b/cli/kill.c index b2c6862..e7a6fa7 100644 --- a/cli/kill.c +++ b/cli/kill.c @@ -21,6 +21,7 @@ void cmd_kill(int argc, char **argv) { int seconds = -1; + enum ipc_err code; char *endptr; if (argc == 2) { @@ -31,5 +32,6 @@ cmd_kill(int argc, char **argv) usage_kill(); btpd_connect(); - handle_ipc_res(btpd_die(ipc, seconds), "kill"); + if ((code = btpd_die(ipc, seconds)) != 0) + errx(1, "%s", ipc_strerror(code)); } diff --git a/cli/list.c b/cli/list.c index 8bdf851..c0888c2 100644 --- a/cli/list.c +++ b/cli/list.c @@ -30,9 +30,6 @@ itm_insert(struct items *itms, struct item *itm) struct item *p; BTPDQ_FOREACH(p, &itms->hd, entry) if (itm->num < p->num) -#if 0 - if (strcmp(itm->name, p->name) < 0) -#endif break; if (p != NULL) BTPDQ_INSERT_BEFORE(p, itm, entry); @@ -54,17 +51,6 @@ list_cb(int obji, enum ipc_err objerr, struct ipc_get_res *res, void *arg) asprintf(&itm->name, "%.*s", (int)res[IPC_TVAL_NAME].v.str.l, res[IPC_TVAL_NAME].v.str.p); itm_insert(itms, itm); -#if 0 - int *count = arg; - (*count)++; - printf("%4u %c.", (unsigned)res[IPC_TVAL_NUM].v.num, - tstate_char(res[IPC_TVAL_STATE].v.num)); - if (res[IPC_TVAL_NAME].type == IPC_TYPE_ERR) - printf(" %s\n", ipc_strerror(res[IPC_TVAL_NAME].v.num)); - else - printf(" %.*s\n", (int)res[IPC_TVAL_NAME].v.str.l, - res[IPC_TVAL_NAME].v.str.p); -#endif } void @@ -90,7 +76,8 @@ static struct option list_opts [] = { void cmd_list(int argc, char **argv) { - int ch, /*count = 0,*/ inactive = 0, active = 0; + int ch, inactive = 0, active = 0; + enum ipc_err code; enum ipc_twc twc; enum ipc_tval keys[] = { IPC_TVAL_NUM, IPC_TVAL_STATE, IPC_TVAL_NAME }; struct items itms; @@ -118,7 +105,8 @@ cmd_list(int argc, char **argv) printf("NUM ST NAME\n"); itms.count = 0; BTPDQ_INIT(&itms.hd); - handle_ipc_res(btpd_tget_wc(ipc, twc, keys, 3, list_cb, &itms), "tget"); + if ((code = btpd_tget_wc(ipc, twc, keys, 3, list_cb, &itms)) != IPC_OK) + errx(1, "%s", ipc_strerror(code)); print_items(&itms); printf("Listed %d torrent%s.\n", itms.count, itms.count == 1 ? "" : "s"); } diff --git a/cli/start.c b/cli/start.c index 4772bb6..28d3ce1 100644 --- a/cli/start.c +++ b/cli/start.c @@ -23,5 +23,5 @@ cmd_start(int argc, char **argv) btpd_connect(); for (int i = 1; i < argc; i++) if (torrent_spec(argv[i], &t)) - handle_ipc_res(btpd_start(ipc, &t), argv[i]); + handle_ipc_res(btpd_start(ipc, &t), "start", argv[i]); } diff --git a/cli/stat.c b/cli/stat.c index 0ed75bf..1d1b781 100644 --- a/cli/stat.c +++ b/cli/stat.c @@ -162,8 +162,8 @@ again: stat_cb, &cba); else err = btpd_tget(ipc, tps, ntps, stkeys, nstkeys, stat_cb, &cba); - if (handle_ipc_res(err, "stat") != IPC_OK) - exit(1); + if (err != IPC_OK) + errx(1, ipc_strerror(err)); if (names) printf("-----\n"); if (individual) diff --git a/cli/stop.c b/cli/stop.c index caf68f4..4d8258d 100644 --- a/cli/stop.c +++ b/cli/stop.c @@ -23,5 +23,5 @@ cmd_stop(int argc, char **argv) btpd_connect(); for (int i = 1; i < argc; i++) if (torrent_spec(argv[i], &t)) - handle_ipc_res(btpd_stop(ipc, &t), argv[i]); + handle_ipc_res(btpd_stop(ipc, &t), "stop", argv[i]); }