1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
#include "btcli.h"
const char *btpd_dir;
struct ipc *ipc;
void
btpd_connect(void)
{
if ((errno = ipc_open(btpd_dir, &ipc)) != 0)
err(1, "cannot open connection to btpd in %s", btpd_dir);
}
enum ipc_err
handle_ipc_res(enum ipc_err code, const char *cmd, const char *target)
{
switch (code) {
case IPC_OK:
break;
case IPC_COMMERR:
errx(1, "%s", ipc_strerror(code));
default:
warnx("%s '%s': %s", cmd, target, ipc_strerror(code));
}
return code;
}
char
tstate_char(enum ipc_tstate ts)
{
switch (ts) {
case IPC_TSTATE_INACTIVE:
return 'I';
case IPC_TSTATE_START:
return '+';
case IPC_TSTATE_STOP:
return '-';
case IPC_TSTATE_LEECH:
return 'L';
case IPC_TSTATE_SEED:
return 'S';
}
errx(1, "bad state");
}
int
torrent_spec(char *arg, struct ipc_torrent *tp)
{
char *p;
tp->u.num = strtoul(arg, &p, 10);
if (*p == '\0') {
tp->by_hash = 0;
return 1;
}
if ((p = mi_load(arg, NULL)) == NULL) {
warnx("bad torrent '%s' (%s)", arg, strerror(errno));
return 0;
}
tp->by_hash = 1;
mi_info_hash(p, tp->u.hash);
free(p);
return 1;
}
static struct {
const char *name;
void (*fun)(int, char **);
void (*help)(void);
} cmd_table[] = {
{ "add", cmd_add, usage_add },
{ "del", cmd_del, usage_del },
{ "kill", cmd_kill, usage_kill },
{ "list", cmd_list, usage_list },
{ "start", cmd_start, usage_start },
{ "stop", cmd_stop, usage_stop },
{ "stat", cmd_stat, usage_stat }
};
int ncmds = sizeof(cmd_table) / sizeof(cmd_table[0]);
static void
usage(void)
{
printf(
"btcli is the btpd command line interface.\n"
"\n"
"Usage: btcli [main options] command [command options]\n"
"\n"
"Main options:\n"
"-d dir\n"
"\tThe btpd directory.\n"
"\n"
"--help [command]\n"
"\tShow this text or help for the specified command.\n"
"\n"
"Commands:\n"
"add\n"
"del\n"
"kill\n"
"list\n"
"start\n"
"stat\n"
"stop\n"
"\n");
exit(1);
}
static struct option base_opts [] = {
{ "help", no_argument, NULL, 'H' },
{NULL, 0, NULL, 0}
};
int
main(int argc, char **argv)
{
int ch, help = 0;
if (argc < 2)
usage();
while ((ch = getopt_long(argc, argv, "+d:", base_opts, NULL)) != -1) {
switch (ch) {
case 'd':
btpd_dir = optarg;
break;
case 'H':
help = 1;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc == 0)
usage();
if (btpd_dir == NULL)
if ((btpd_dir = find_btpd_dir()) == NULL)
errx(1, "cannot find the btpd directory");
optind = 0;
int found = 0;
for (int i = 0; !found && i < ncmds; i++) {
if (strcmp(argv[0], cmd_table[i].name) == 0) {
found = 1;
if (help)
cmd_table[i].help();
else
cmd_table[i].fun(argc, argv);
}
}
if (!found)
usage();
return 0;
}
|