@@ -54,7 +54,7 @@ cmd_add(int argc, char **argv) | |||||
case 'd': | case 'd': | ||||
dir = optarg; | dir = optarg; | ||||
if ((dirlen = strlen(dir)) == 0) | if ((dirlen = strlen(dir)) == 0) | ||||
errx(1, "bad option value for -d"); | diemsg("bad option value for -d.\n"); | ||||
break; | break; | ||||
case 'n': | case 'n': | ||||
name = optarg; | name = optarg; | ||||
@@ -77,7 +77,7 @@ cmd_add(int argc, char **argv) | |||||
struct iobuf iob; | struct iobuf iob; | ||||
if ((mi = mi_load(argv[0], &mi_size)) == NULL) | if ((mi = mi_load(argv[0], &mi_size)) == NULL) | ||||
err(1, "error loading '%s'", argv[0]); | diemsg("error loading '%s' (%s).\n", argv[0], strerror(errno)); | ||||
iob = iobuf_init(PATH_MAX); | iob = iobuf_init(PATH_MAX); | ||||
iobuf_write(&iob, dir, dirlen); | iobuf_write(&iob, dir, dirlen); | ||||
@@ -90,7 +90,7 @@ cmd_add(int argc, char **argv) | |||||
} | } | ||||
iobuf_swrite(&iob, "\0"); | iobuf_swrite(&iob, "\0"); | ||||
if ((errno = make_abs_path(iob.buf, dpath)) != 0) | if ((errno = make_abs_path(iob.buf, dpath)) != 0) | ||||
err(1, "make_abs_path '%s'", dpath); | diemsg("make_abs_path '%s' failed (%s).\n", dpath, strerror(errno)); | ||||
code = btpd_add(ipc, mi, mi_size, dpath, name); | code = btpd_add(ipc, mi, mi_size, dpath, name); | ||||
if (code == 0 && start) { | if (code == 0 && start) { | ||||
struct ipc_torrent tspec; | struct ipc_torrent tspec; | ||||
@@ -99,6 +99,6 @@ cmd_add(int argc, char **argv) | |||||
code = btpd_start(ipc, &tspec); | code = btpd_start(ipc, &tspec); | ||||
} | } | ||||
if (code != IPC_OK) | if (code != IPC_OK) | ||||
errx(1, "%s", ipc_strerror(code)); | diemsg("command failed (%s).\n", ipc_strerror(code)); | ||||
return; | return; | ||||
} | } |
@@ -1,13 +1,26 @@ | |||||
#include <stdarg.h> | |||||
#include "btcli.h" | #include "btcli.h" | ||||
const char *btpd_dir; | const char *btpd_dir; | ||||
struct ipc *ipc; | struct ipc *ipc; | ||||
void | |||||
diemsg(const char *fmt, ...) | |||||
{ | |||||
va_list ap; | |||||
va_start(ap, fmt); | |||||
vfprintf(stderr, fmt, ap); | |||||
va_end(ap); | |||||
exit(1); | |||||
} | |||||
void | void | ||||
btpd_connect(void) | btpd_connect(void) | ||||
{ | { | ||||
if ((errno = ipc_open(btpd_dir, &ipc)) != 0) | if ((errno = ipc_open(btpd_dir, &ipc)) != 0) | ||||
err(1, "cannot open connection to btpd in %s", btpd_dir); | diemsg("cannot open connection to btpd in %s (%s).\n", btpd_dir, | ||||
strerror(errno)); | |||||
} | } | ||||
enum ipc_err | enum ipc_err | ||||
@@ -17,9 +30,10 @@ handle_ipc_res(enum ipc_err code, const char *cmd, const char *target) | |||||
case IPC_OK: | case IPC_OK: | ||||
break; | break; | ||||
case IPC_COMMERR: | case IPC_COMMERR: | ||||
errx(1, "%s", ipc_strerror(code)); | diemsg("error in communication with btpd.\n"); | ||||
default: | default: | ||||
warnx("%s '%s': %s", cmd, target, ipc_strerror(code)); | fprintf(stderr, "btcli %s '%s': %s.\n", cmd, target, | ||||
ipc_strerror(code)); | |||||
} | } | ||||
return code; | return code; | ||||
} | } | ||||
@@ -68,7 +82,7 @@ tstate_char(enum ipc_tstate ts) | |||||
case IPC_TSTATE_SEED: | case IPC_TSTATE_SEED: | ||||
return 'S'; | return 'S'; | ||||
} | } | ||||
errx(1, "bad state"); | diemsg("unrecognized torrent state.\n"); | ||||
} | } | ||||
int | int | ||||
@@ -81,7 +95,8 @@ torrent_spec(char *arg, struct ipc_torrent *tp) | |||||
return 1; | return 1; | ||||
} | } | ||||
if ((p = mi_load(arg, NULL)) == NULL) { | if ((p = mi_load(arg, NULL)) == NULL) { | ||||
warnx("bad torrent '%s' (%s)", arg, strerror(errno)); | fprintf(stderr, "btcli: bad torrent '%s' (%s).'n", arg, | ||||
strerror(errno)); | |||||
return 0; | return 0; | ||||
} | } | ||||
tp->by_hash = 1; | tp->by_hash = 1; | ||||
@@ -170,7 +185,7 @@ main(int argc, char **argv) | |||||
if (btpd_dir == NULL) | if (btpd_dir == NULL) | ||||
if ((btpd_dir = find_btpd_dir()) == NULL) | if ((btpd_dir = find_btpd_dir()) == NULL) | ||||
errx(1, "cannot find the btpd directory"); | diemsg("cannot find the btpd directory.\n"); | ||||
optind = 0; | optind = 0; | ||||
int found = 0; | int found = 0; | ||||
@@ -1,7 +1,6 @@ | |||||
#ifndef BTCLI_H | #ifndef BTCLI_H | ||||
#define BTCLI_H | #define BTCLI_H | ||||
#include <err.h> | |||||
#include <errno.h> | #include <errno.h> | ||||
#include <getopt.h> | #include <getopt.h> | ||||
#include <inttypes.h> | #include <inttypes.h> | ||||
@@ -23,6 +22,8 @@ | |||||
extern const char *btpd_dir; | extern const char *btpd_dir; | ||||
extern struct ipc *ipc; | extern struct ipc *ipc; | ||||
__attribute__((noreturn)) | |||||
void diemsg(const char *fmt, ...); | |||||
void btpd_connect(void); | void btpd_connect(void); | ||||
enum ipc_err handle_ipc_res(enum ipc_err err, const char *cmd, | enum ipc_err handle_ipc_res(enum ipc_err err, const char *cmd, | ||||
const char *target); | const char *target); | ||||
@@ -1,11 +1,11 @@ | |||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#include <err.h> | |||||
#include <errno.h> | #include <errno.h> | ||||
#include <getopt.h> | #include <getopt.h> | ||||
#include <inttypes.h> | #include <inttypes.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | |||||
#include <time.h> | #include <time.h> | ||||
#include "metainfo.h" | #include "metainfo.h" | ||||
@@ -75,8 +75,11 @@ main(int argc, char **argv) | |||||
while (argc > 0) { | while (argc > 0) { | ||||
char *mi = NULL; | char *mi = NULL; | ||||
if ((mi = mi_load(*argv, NULL)) == NULL) | if ((mi = mi_load(*argv, NULL)) == NULL) { | ||||
err(1, "mi_load: %s", *argv); | fprintf(stderr, "failed to load torrent file '%s' (%s).\n", | ||||
*argv, strerror(errno)); | |||||
exit(1); | |||||
} | |||||
print_metainfo(mi); | print_metainfo(mi); | ||||
free(mi); | free(mi); | ||||
@@ -33,5 +33,5 @@ cmd_kill(int argc, char **argv) | |||||
btpd_connect(); | btpd_connect(); | ||||
if ((code = btpd_die(ipc, seconds)) != 0) | if ((code = btpd_die(ipc, seconds)) != 0) | ||||
errx(1, "%s", ipc_strerror(code)); | diemsg("command failed (%s).\n", ipc_strerror(code)); | ||||
} | } |
@@ -60,7 +60,7 @@ list_cb(int obji, enum ipc_err objerr, struct ipc_get_res *res, void *arg) | |||||
struct items *itms = arg; | struct items *itms = arg; | ||||
struct item *itm = calloc(1, sizeof(*itm)); | struct item *itm = calloc(1, sizeof(*itm)); | ||||
if (objerr != IPC_OK) | if (objerr != IPC_OK) | ||||
errx(1, "list failed for '%s' (%s)", itms->argv[obji], | diemsg("list failed for '%s' (%s).\n", itms->argv[obji], | ||||
ipc_strerror(objerr)); | ipc_strerror(objerr)); | ||||
itms->count++; | itms->count++; | ||||
itm->num = (unsigned)res[IPC_TVAL_NUM].v.num; | itm->num = (unsigned)res[IPC_TVAL_NUM].v.num; | ||||
@@ -148,7 +148,7 @@ cmd_list(int argc, char **argv) | |||||
else | else | ||||
code = btpd_tget(ipc, itms.tps, itms.ntps, keys, nkeys, list_cb, &itms); | code = btpd_tget(ipc, itms.tps, itms.ntps, keys, nkeys, list_cb, &itms); | ||||
if (code != IPC_OK) | if (code != IPC_OK) | ||||
errx(1, "%s", ipc_strerror(code)); | diemsg("command failed (%s).\n", ipc_strerror(code)); | ||||
printf("%-40.40s NUM ST HAVE SIZE RATIO\n", "NAME"); | printf("%-40.40s NUM ST HAVE SIZE RATIO\n", "NAME"); | ||||
print_items(&itms); | print_items(&itms); | ||||
} | } |
@@ -136,7 +136,7 @@ again: | |||||
else | else | ||||
err = btpd_tget(ipc, tps, ntps, stkeys, nstkeys, stat_cb, &cba); | err = btpd_tget(ipc, tps, ntps, stkeys, nstkeys, stat_cb, &cba); | ||||
if (err != IPC_OK) | if (err != IPC_OK) | ||||
errx(1, ipc_strerror(err)); | diemsg("command failed (%s).\n", ipc_strerror(err)); | ||||
if (names) | if (names) | ||||
printf("-------\n"); | printf("-------\n"); | ||||
if (individual) | if (individual) | ||||
@@ -51,7 +51,7 @@ cmd_stop(int argc, char **argv) | |||||
if (all) { | if (all) { | ||||
enum ipc_err code = btpd_stop_all(ipc); | enum ipc_err code = btpd_stop_all(ipc); | ||||
if (code != IPC_OK) | if (code != IPC_OK) | ||||
errx(1, "%s", ipc_strerror(code)); | diemsg("command failed (%s).\n", ipc_strerror(code)); | ||||
} else { | } else { | ||||
for (int i = 0; i < argc; i++) | for (int i = 0; i < argc; i++) | ||||
if (torrent_spec(argv[i], &t)) | if (torrent_spec(argv[i], &t)) | ||||