Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
8ae47ac8 | 3 | * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell |
460b9539 | 4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include "types.h" | |
23 | ||
24 | #include <getopt.h> | |
25 | #include <sys/types.h> | |
26 | #include <sys/socket.h> | |
27 | #include <sys/un.h> | |
28 | #include <stdio.h> | |
29 | #include <errno.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
32 | #include <locale.h> | |
33 | #include <time.h> | |
34 | #include <stddef.h> | |
35 | #include <unistd.h> | |
36 | #include <assert.h> | |
fe8f8518 | 37 | #include <pcre.h> |
2a10b70b | 38 | #include <ctype.h> |
460b9539 | 39 | |
40 | #include "configuration.h" | |
41 | #include "syscalls.h" | |
42 | #include "log.h" | |
43 | #include "queue.h" | |
44 | #include "client.h" | |
45 | #include "wstat.h" | |
46 | #include "table.h" | |
47 | #include "charset.h" | |
48 | #include "kvp.h" | |
49 | #include "split.h" | |
50 | #include "sink.h" | |
51 | #include "plugin.h" | |
52 | #include "mem.h" | |
53 | #include "defs.h" | |
54 | #include "authorize.h" | |
55 | #include "vector.h" | |
56 | ||
57 | static int auto_reconfigure; | |
58 | ||
59 | static const struct option options[] = { | |
60 | { "help", no_argument, 0, 'h' }, | |
61 | { "version", no_argument, 0, 'V' }, | |
62 | { "config", required_argument, 0, 'c' }, | |
63 | { "debug", no_argument, 0, 'd' }, | |
af66d051 | 64 | { "local", no_argument, 0, 'l' }, |
63ad732f | 65 | { "no-per-user-config", no_argument, 0, 'N' }, |
460b9539 | 66 | { "help-commands", no_argument, 0, 'H' }, |
f0feb22e RK |
67 | { "user", required_argument, 0, 'u' }, |
68 | { "password", required_argument, 0, 'p' }, | |
460b9539 | 69 | { 0, 0, 0, 0 } |
70 | }; | |
71 | ||
72 | /* display usage message and terminate */ | |
73 | static void help(void) { | |
74 | xprintf("Usage:\n" | |
75 | " disorder [OPTIONS] COMMAND ...\n" | |
76 | "Options:\n" | |
77 | " --help, -h Display usage message\n" | |
78 | " --help-commands, -H List commands\n" | |
79 | " --version, -V Display version number\n" | |
80 | " --config PATH, -c PATH Set configuration file\n" | |
af66d051 | 81 | " --local, -l Force connection to local server\n" |
460b9539 | 82 | " --debug, -d Turn on debugging\n"); |
83 | xfclose(stdout); | |
84 | exit(0); | |
85 | } | |
86 | ||
87 | /* display version number and terminate */ | |
88 | static void version(void) { | |
a05e4467 | 89 | xprintf("%s", disorder_version_string); |
460b9539 | 90 | xfclose(stdout); |
91 | exit(0); | |
92 | } | |
93 | ||
94 | static void cf_version(disorder_client *c, | |
95 | char attribute((unused)) **argv) { | |
96 | char *v; | |
97 | ||
98 | if(disorder_version(c, &v)) exit(EXIT_FAILURE); | |
99 | xprintf("%s\n", nullcheck(utf82mb(v))); | |
100 | } | |
101 | ||
102 | static void print_queue_entry(const struct queue_entry *q) { | |
103 | if(q->track) xprintf("track %s\n", nullcheck(utf82mb(q->track))); | |
104 | if(q->id) xprintf(" id %s\n", nullcheck(utf82mb(q->id))); | |
105 | if(q->submitter) xprintf(" submitted by %s at %s", | |
106 | nullcheck(utf82mb(q->submitter)), ctime(&q->when)); | |
107 | if(q->played) xprintf(" played at %s", ctime(&q->played)); | |
108 | if(q->state == playing_started | |
109 | || q->state == playing_paused) xprintf(" %lds so far", q->sofar); | |
110 | else if(q->expected) xprintf(" might start at %s", ctime(&q->expected)); | |
111 | if(q->scratched) xprintf(" scratched by %s\n", | |
112 | nullcheck(utf82mb(q->scratched))); | |
113 | else xprintf(" %s\n", playing_states[q->state]); | |
114 | if(q->wstat) xprintf(" %s\n", wstat(q->wstat)); | |
115 | } | |
116 | ||
117 | static void cf_playing(disorder_client *c, | |
118 | char attribute((unused)) **argv) { | |
119 | struct queue_entry *q; | |
120 | ||
121 | if(disorder_playing(c, &q)) exit(EXIT_FAILURE); | |
122 | if(q) | |
123 | print_queue_entry(q); | |
124 | else | |
125 | xprintf("nothing\n"); | |
126 | } | |
127 | ||
128 | static void cf_play(disorder_client *c, char **argv) { | |
129 | while(*argv) | |
130 | if(disorder_play(c, *argv++)) exit(EXIT_FAILURE); | |
131 | } | |
132 | ||
133 | static void cf_remove(disorder_client *c, char **argv) { | |
134 | if(disorder_remove(c, argv[0])) exit(EXIT_FAILURE); | |
135 | } | |
136 | ||
137 | static void cf_disable(disorder_client *c, | |
138 | char attribute((unused)) **argv) { | |
139 | if(disorder_disable(c)) exit(EXIT_FAILURE); | |
140 | } | |
141 | ||
142 | static void cf_enable(disorder_client *c, char attribute((unused)) **argv) { | |
143 | if(disorder_enable(c)) exit(EXIT_FAILURE); | |
144 | } | |
145 | ||
146 | static void cf_scratch(disorder_client *c, | |
147 | char **argv) { | |
148 | if(disorder_scratch(c, argv[0])) exit(EXIT_FAILURE); | |
149 | } | |
150 | ||
151 | static void cf_shutdown(disorder_client *c, | |
152 | char attribute((unused)) **argv) { | |
153 | if(disorder_shutdown(c)) exit(EXIT_FAILURE); | |
154 | } | |
155 | ||
156 | static void cf_reconfigure(disorder_client *c, | |
157 | char attribute((unused)) **argv) { | |
c00fce3a RK |
158 | /* Re-check configuration for server */ |
159 | if(config_read(1)) fatal(0, "cannot read configuration"); | |
460b9539 | 160 | if(disorder_reconfigure(c)) exit(EXIT_FAILURE); |
161 | } | |
162 | ||
163 | static void cf_rescan(disorder_client *c, char attribute((unused)) **argv) { | |
164 | if(disorder_rescan(c)) exit(EXIT_FAILURE); | |
165 | } | |
166 | ||
167 | static void cf_somequeue(disorder_client *c, | |
168 | int (*fn)(disorder_client *c, | |
169 | struct queue_entry **qp)) { | |
170 | struct queue_entry *q; | |
171 | ||
172 | if(fn(c, &q)) exit(EXIT_FAILURE); | |
173 | while(q) { | |
174 | print_queue_entry(q); | |
175 | q = q->next; | |
176 | } | |
177 | } | |
178 | ||
179 | static void cf_recent(disorder_client *c, char attribute((unused)) **argv) { | |
180 | cf_somequeue(c, disorder_recent); | |
181 | } | |
182 | ||
183 | static void cf_queue(disorder_client *c, char attribute((unused)) **argv) { | |
184 | cf_somequeue(c, disorder_queue); | |
185 | } | |
186 | ||
187 | static void cf_quack(disorder_client attribute((unused)) *c, | |
188 | char attribute((unused)) **argv) { | |
189 | xprintf("\n" | |
190 | " .------------------.\n" | |
191 | " | Naath is a babe! |\n" | |
192 | " `---------+--------'\n" | |
193 | " \\\n" | |
194 | " >0\n" | |
195 | " (<)'\n" | |
196 | "~~~~~~~~~~~~~~~~~~~~~~\n" | |
197 | "\n"); | |
198 | } | |
199 | ||
200 | static void cf_somelist(disorder_client *c, char **argv, | |
201 | int (*fn)(disorder_client *c, | |
202 | const char *arg, const char *re, | |
203 | char ***vecp, int *nvecp)) { | |
204 | char **vec; | |
205 | const char *re; | |
206 | ||
207 | if(argv[1]) | |
208 | re = xstrdup(argv[1] + 1); | |
209 | else | |
210 | re = 0; | |
211 | if(fn(c, argv[0], re, &vec, 0)) exit(EXIT_FAILURE); | |
212 | while(*vec) | |
213 | xprintf("%s\n", nullcheck(utf82mb(*vec++))); | |
214 | } | |
215 | ||
216 | static int isarg_regexp(const char *s) { | |
217 | return s[0] == '~'; | |
218 | } | |
219 | ||
220 | static void cf_dirs(disorder_client *c, | |
221 | char **argv) { | |
222 | cf_somelist(c, argv, disorder_directories); | |
223 | } | |
224 | ||
225 | static void cf_files(disorder_client *c, char **argv) { | |
226 | cf_somelist(c, argv, disorder_files); | |
227 | } | |
228 | ||
229 | static void cf_allfiles(disorder_client *c, char **argv) { | |
230 | cf_somelist(c, argv, disorder_allfiles); | |
231 | } | |
232 | ||
233 | static void cf_get(disorder_client *c, char **argv) { | |
234 | char *value; | |
235 | ||
236 | if(disorder_get(c, argv[0], argv[1], &value)) exit(EXIT_FAILURE); | |
237 | xprintf("%s\n", nullcheck(utf82mb(value))); | |
238 | } | |
239 | ||
240 | static void cf_length(disorder_client *c, char **argv) { | |
241 | long length; | |
242 | ||
243 | if(disorder_length(c, argv[0], &length)) exit(EXIT_FAILURE); | |
244 | xprintf("%ld\n", length); | |
245 | } | |
246 | ||
247 | static void cf_set(disorder_client *c, char **argv) { | |
248 | if(disorder_set(c, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE); | |
249 | } | |
250 | ||
251 | static void cf_unset(disorder_client *c, char **argv) { | |
252 | if(disorder_unset(c, argv[0], argv[1])) exit(EXIT_FAILURE); | |
253 | } | |
254 | ||
255 | static void cf_prefs(disorder_client *c, char **argv) { | |
256 | struct kvp *k; | |
257 | ||
258 | if(disorder_prefs(c, argv[0], &k)) exit(EXIT_FAILURE); | |
259 | for(; k; k = k->next) | |
260 | xprintf("%s = %s\n", | |
261 | nullcheck(utf82mb(k->name)), nullcheck(utf82mb(k->value))); | |
262 | } | |
263 | ||
264 | static void cf_search(disorder_client *c, char **argv) { | |
265 | char **results; | |
266 | int nresults, n; | |
267 | ||
268 | if(disorder_search(c, *argv, &results, &nresults)) exit(EXIT_FAILURE); | |
269 | for(n = 0; n < nresults; ++n) | |
270 | xprintf("%s\n", nullcheck(utf82mb(results[n]))); | |
271 | } | |
272 | ||
273 | static void cf_random_disable(disorder_client *c, | |
274 | char attribute((unused)) **argv) { | |
275 | if(disorder_random_disable(c)) exit(EXIT_FAILURE); | |
276 | } | |
277 | ||
278 | static void cf_random_enable(disorder_client *c, | |
279 | char attribute((unused)) **argv) { | |
280 | if(disorder_random_enable(c)) exit(EXIT_FAILURE); | |
281 | } | |
282 | ||
283 | static void cf_stats(disorder_client *c, | |
284 | char attribute((unused)) **argv) { | |
285 | char **vec; | |
286 | ||
287 | if(disorder_stats(c, &vec, 0)) exit(EXIT_FAILURE); | |
288 | while(*vec) | |
289 | xprintf("%s\n", nullcheck(utf82mb(*vec++))); | |
290 | } | |
291 | ||
292 | static void cf_get_volume(disorder_client *c, | |
293 | char attribute((unused)) **argv) { | |
294 | int l, r; | |
295 | ||
296 | if(disorder_get_volume(c, &l, &r)) exit(EXIT_FAILURE); | |
297 | xprintf("%d %d\n", l, r); | |
298 | } | |
299 | ||
300 | static void cf_set_volume(disorder_client *c, | |
301 | char **argv) { | |
302 | if(disorder_set_volume(c, atoi(argv[0]), atoi(argv[1]))) exit(EXIT_FAILURE); | |
303 | } | |
304 | ||
305 | static void cf_become(disorder_client *c, | |
306 | char **argv) { | |
307 | if(disorder_become(c, argv[0])) exit(EXIT_FAILURE); | |
308 | } | |
309 | ||
310 | static void cf_log(disorder_client *c, | |
311 | char attribute((unused)) **argv) { | |
312 | if(disorder_log(c, sink_stdio("stdout", stdout))) exit(EXIT_FAILURE); | |
313 | } | |
314 | ||
315 | static void cf_move(disorder_client *c, | |
316 | char **argv) { | |
317 | long n; | |
318 | int e; | |
319 | ||
320 | if((e = xstrtol(&n, argv[1], 0, 10))) | |
321 | fatal(e, "cannot convert '%s'", argv[1]); | |
322 | if(n > INT_MAX || n < INT_MIN) | |
323 | fatal(e, "%ld out of range", n); | |
324 | if(disorder_move(c, argv[0], (int)n)) exit(EXIT_FAILURE); | |
325 | } | |
326 | ||
327 | static void cf_part(disorder_client *c, | |
328 | char **argv) { | |
329 | char *s; | |
330 | ||
331 | if(disorder_part(c, &s, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE); | |
332 | xprintf("%s\n", nullcheck(utf82mb(s))); | |
333 | } | |
334 | ||
335 | static int isarg_filename(const char *s) { | |
336 | return s[0] == '/'; | |
337 | } | |
338 | ||
f0feb22e | 339 | static void cf_authorize(disorder_client *c, |
460b9539 | 340 | char **argv) { |
f0feb22e | 341 | if(!authorize(c, argv[0])) |
460b9539 | 342 | auto_reconfigure = 1; |
343 | } | |
344 | ||
345 | static void cf_resolve(disorder_client *c, | |
346 | char **argv) { | |
347 | char *track; | |
348 | ||
349 | if(disorder_resolve(c, &track, argv[0])) exit(EXIT_FAILURE); | |
350 | xprintf("%s\n", nullcheck(utf82mb(track))); | |
351 | } | |
352 | ||
353 | static void cf_pause(disorder_client *c, | |
354 | char attribute((unused)) **argv) { | |
355 | if(disorder_pause(c)) exit(EXIT_FAILURE); | |
356 | } | |
357 | ||
358 | static void cf_resume(disorder_client *c, | |
359 | char attribute((unused)) **argv) { | |
360 | if(disorder_resume(c)) exit(EXIT_FAILURE); | |
361 | } | |
362 | ||
363 | static void cf_tags(disorder_client *c, | |
364 | char attribute((unused)) **argv) { | |
365 | char **vec; | |
366 | ||
367 | if(disorder_tags(c, &vec, 0)) exit(EXIT_FAILURE); | |
368 | while(*vec) | |
369 | xprintf("%s\n", nullcheck(utf82mb(*vec++))); | |
370 | } | |
371 | ||
372 | static void cf_get_global(disorder_client *c, char **argv) { | |
373 | char *value; | |
374 | ||
375 | if(disorder_get_global(c, argv[0], &value)) exit(EXIT_FAILURE); | |
376 | xprintf("%s\n", nullcheck(utf82mb(value))); | |
377 | } | |
378 | ||
379 | static void cf_set_global(disorder_client *c, char **argv) { | |
380 | if(disorder_set_global(c, argv[0], argv[1])) exit(EXIT_FAILURE); | |
381 | } | |
382 | ||
383 | static void cf_unset_global(disorder_client *c, char **argv) { | |
384 | if(disorder_unset_global(c, argv[0])) exit(EXIT_FAILURE); | |
385 | } | |
386 | ||
2a10b70b RK |
387 | static int isarg_integer(const char *s) { |
388 | if(!*s) return 0; | |
389 | while(*s) { | |
390 | if(!isdigit((unsigned char)*s)) | |
391 | return 0; | |
392 | ++s; | |
393 | } | |
394 | return 1; | |
395 | } | |
396 | ||
397 | static void cf_new(disorder_client *c, | |
398 | char **argv) { | |
399 | char **vec; | |
400 | ||
401 | if(disorder_new_tracks(c, &vec, 0, argv[0] ? atoi(argv[0]) : 0)) | |
402 | exit(EXIT_FAILURE); | |
403 | while(*vec) | |
404 | xprintf("%s\n", nullcheck(utf82mb(*vec++))); | |
405 | } | |
406 | ||
ca831831 RK |
407 | static void cf_rtp_address(disorder_client *c, |
408 | char attribute((unused)) **argv) { | |
409 | char *address, *port; | |
410 | ||
411 | if(disorder_rtp_address(c, &address, &port)) exit(EXIT_FAILURE); | |
412 | xprintf("address: %s\nport: %s\n", address, port); | |
413 | } | |
414 | ||
f0feb22e RK |
415 | static void cf_adduser(disorder_client *c, |
416 | char **argv) { | |
417 | if(disorder_adduser(c, argv[0], argv[1])) | |
418 | exit(EXIT_FAILURE); | |
419 | } | |
420 | ||
460b9539 | 421 | static const struct command { |
422 | const char *name; | |
423 | int min, max; | |
424 | void (*fn)(disorder_client *c, char **); | |
425 | int (*isarg)(const char *); | |
426 | const char *argstr, *desc; | |
427 | } commands[] = { | |
f0feb22e RK |
428 | { "adduser", 2, 2, cf_adduser, 0, "USER PASSWORD", |
429 | "Create a new user" }, | |
460b9539 | 430 | { "allfiles", 1, 2, cf_allfiles, isarg_regexp, "DIR [~REGEXP]", |
431 | "List all files and directories in DIR" }, | |
432 | { "authorize", 1, 1, cf_authorize, 0, "USER", | |
433 | "Authorize USER to connect to the server" }, | |
434 | { "become", 1, 1, cf_become, 0, "USER", | |
435 | "Become user USER" }, | |
436 | { "dirs", 1, 2, cf_dirs, isarg_regexp, "DIR [~REGEXP]", | |
437 | "List directories in DIR" }, | |
438 | { "disable", 0, 0, cf_disable, 0, "", | |
439 | "Disable play" }, | |
440 | { "disable-random", 0, 0, cf_random_disable, 0, "", | |
441 | "Disable random play" }, | |
442 | { "enable", 0, 0, cf_enable, 0, "", | |
443 | "Enable play" }, | |
444 | { "enable-random", 0, 0, cf_random_enable, 0, "", | |
445 | "Enable random play" }, | |
446 | { "files", 1, 2, cf_files, isarg_regexp, "DIR [~REGEXP]", | |
447 | "List files in DIR" }, | |
448 | { "get", 2, 2, cf_get, 0, "TRACK NAME", | |
449 | "Get a preference value" }, | |
450 | { "get-global", 1, 1, cf_get_global, 0, "NAME", | |
451 | "Get a global preference value" }, | |
452 | { "get-volume", 0, 0, cf_get_volume, 0, "", | |
453 | "Get the current volume" }, | |
454 | { "length", 1, 1, cf_length, 0, "TRACK", | |
455 | "Get the length of TRACK in seconds" }, | |
456 | { "log", 0, 0, cf_log, 0, "", | |
457 | "Copy event log to stdout" }, | |
458 | { "move", 2, 2, cf_move, 0, "TRACK DELTA", | |
459 | "Move a track in the queue" }, | |
2a10b70b RK |
460 | { "new", 0, 1, cf_new, isarg_integer, "[MAX]", |
461 | "Get the most recently added MAX tracks" }, | |
460b9539 | 462 | { "part", 3, 3, cf_part, 0, "TRACK CONTEXT PART", |
463 | "Find a track name part" }, | |
464 | { "pause", 0, 0, cf_pause, 0, "", | |
465 | "Pause the currently playing track" }, | |
466 | { "play", 1, INT_MAX, cf_play, isarg_filename, "TRACKS...", | |
467 | "Add TRACKS to the end of the queue" }, | |
468 | { "playing", 0, 0, cf_playing, 0, "", | |
469 | "Report the playing track" }, | |
470 | { "prefs", 1, 1, cf_prefs, 0, "TRACK", | |
471 | "Display all the preferences for TRACK" }, | |
472 | { "quack", 0, 0, cf_quack, 0, 0, 0 }, | |
473 | { "queue", 0, 0, cf_queue, 0, "", | |
474 | "Display the current queue" }, | |
475 | { "random-disable", 0, 0, cf_random_disable, 0, "", | |
476 | "Disable random play" }, | |
477 | { "random-enable", 0, 0, cf_random_enable, 0, "", | |
478 | "Enable random play" }, | |
479 | { "recent", 0, 0, cf_recent, 0, "", | |
480 | "Display recently played track" }, | |
481 | { "reconfigure", 0, 0, cf_reconfigure, 0, "", | |
482 | "Reconfigure the daemon" }, | |
483 | { "remove", 1, 1, cf_remove, 0, "TRACK", | |
484 | "Remove a track from the queue" }, | |
485 | { "rescan", 0, 0, cf_rescan, 0, "", | |
486 | "Rescan for new tracks" }, | |
487 | { "resolve", 1, 1, cf_resolve, 0, "TRACK", | |
488 | "Resolve alias for TRACK" }, | |
489 | { "resume", 0, 0, cf_resume, 0, "", | |
490 | "Resume after a pause" }, | |
ca831831 RK |
491 | { "rtp-address", 0, 0, cf_rtp_address, 0, "", |
492 | "Report server's broadcast address" }, | |
460b9539 | 493 | { "scratch", 0, 0, cf_scratch, 0, "", |
494 | "Scratch the currently playing track" }, | |
495 | { "scratch-id", 1, 1, cf_scratch, 0, "ID", | |
496 | "Scratch the currently playing track" }, | |
497 | { "search", 1, 1, cf_search, 0, "WORDS", | |
498 | "Display tracks matching all the words" }, | |
499 | { "set", 3, 3, cf_set, 0, "TRACK NAME VALUE", | |
500 | "Set a preference value" }, | |
501 | { "set-global", 2, 2, cf_set_global, 0, "NAME VALUE", | |
502 | "Set a global preference value" }, | |
503 | { "set-volume", 2, 2, cf_set_volume, 0, "LEFT RIGHT", | |
504 | "Set the volume" }, | |
505 | { "shutdown", 0, 0, cf_shutdown, 0, "", | |
506 | "Shut down the daemon" }, | |
507 | { "stats", 0, 0, cf_stats, 0, "", | |
508 | "Display server statistics" }, | |
509 | { "tags", 0, 0, cf_tags, 0, "", | |
510 | "List known tags" }, | |
511 | { "unset", 2, 2, cf_unset, 0, "TRACK NAME", | |
512 | "Unset a preference" }, | |
513 | { "unset-global", 1, 1, cf_unset_global, 0, "NAME", | |
514 | "Unset a global preference" }, | |
515 | { "version", 0, 0, cf_version, 0, "", | |
516 | "Display the server version" }, | |
517 | }; | |
518 | ||
519 | static void help_commands(void) { | |
520 | unsigned n, max = 0, l; | |
521 | ||
522 | xprintf("Command summary:\n"); | |
523 | for(n = 0; n < sizeof commands / sizeof *commands; ++n) { | |
524 | if(!commands[n].desc) continue; | |
525 | l = strlen(commands[n].name); | |
526 | if(*commands[n].argstr) | |
527 | l += strlen(commands[n].argstr) + 1; | |
528 | if(l > max) | |
529 | max = l; | |
530 | } | |
531 | for(n = 0; n < sizeof commands / sizeof *commands; ++n) { | |
532 | if(!commands[n].desc) continue; | |
533 | l = strlen(commands[n].name); | |
534 | if(*commands[n].argstr) | |
535 | l += strlen(commands[n].argstr) + 1; | |
536 | xprintf(" %s%s%s%*s %s\n", commands[n].name, | |
537 | *commands[n].argstr ? " " : "", | |
538 | commands[n].argstr, | |
539 | max - l, "", | |
540 | commands[n].desc); | |
541 | } | |
542 | xfclose(stdout); | |
543 | exit(0); | |
544 | } | |
545 | ||
546 | int main(int argc, char **argv) { | |
af66d051 | 547 | int n, i, j, local = 0; |
460b9539 | 548 | disorder_client *c = 0; |
460b9539 | 549 | int status = 0; |
550 | struct vector args; | |
f0feb22e | 551 | const char *user = 0, *password = 0; |
460b9539 | 552 | |
320598d4 | 553 | mem_init(); |
fe8f8518 RK |
554 | /* garbage-collect PCRE's memory */ |
555 | pcre_malloc = xmalloc; | |
556 | pcre_free = xfree; | |
460b9539 | 557 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); |
f0feb22e | 558 | while((n = getopt_long(argc, argv, "hVc:dHlNu:p:", options, 0)) >= 0) { |
460b9539 | 559 | switch(n) { |
560 | case 'h': help(); | |
561 | case 'H': help_commands(); | |
562 | case 'V': version(); | |
563 | case 'c': configfile = optarg; break; | |
564 | case 'd': debugging = 1; break; | |
af66d051 | 565 | case 'l': local = 1; break; |
63ad732f | 566 | case 'N': config_per_user = 0; break; |
f0feb22e RK |
567 | case 'u': user = optarg; break; |
568 | case 'p': password = optarg; break; | |
460b9539 | 569 | default: fatal(0, "invalid option"); |
570 | } | |
571 | } | |
c00fce3a | 572 | if(config_read(0)) fatal(0, "cannot read configuration"); |
f0feb22e RK |
573 | if(user) { |
574 | config->username = user; | |
575 | config->password = 0; | |
576 | } | |
577 | if(password) | |
578 | config->password = password; | |
af66d051 | 579 | if(local) |
580 | config->connect.n = 0; | |
460b9539 | 581 | if(!(c = disorder_new(1))) exit(EXIT_FAILURE); |
460b9539 | 582 | if(disorder_connect(c)) exit(EXIT_FAILURE); |
583 | n = optind; | |
584 | /* accumulate command args */ | |
585 | while(n < argc) { | |
586 | if((i = TABLE_FIND(commands, struct command, name, argv[n])) < 0) | |
587 | fatal(0, "unknown command '%s'", argv[n]); | |
588 | if(n + commands[i].min >= argc) | |
589 | fatal(0, "missing arguments to '%s'", argv[n]); | |
590 | n++; | |
591 | vector_init(&args); | |
592 | for(j = 0; j < commands[i].min; ++j) | |
593 | vector_append(&args, nullcheck(mb2utf8(argv[n + j]))); | |
594 | for(; j < commands[i].max | |
595 | && n + j < argc | |
596 | && commands[i].isarg(argv[n + j]); ++j) | |
597 | vector_append(&args, nullcheck(mb2utf8(argv[n + j]))); | |
598 | vector_terminate(&args); | |
599 | commands[i].fn(c, args.vec); | |
600 | n += j; | |
601 | } | |
602 | if(auto_reconfigure) { | |
603 | assert(c != 0); | |
604 | if(disorder_reconfigure(c)) exit(EXIT_FAILURE); | |
605 | } | |
606 | if(c && disorder_close(c)) exit(EXIT_FAILURE); | |
607 | if(fclose(stdout) < 0) fatal(errno, "error closing stdout"); | |
608 | return status; | |
609 | } | |
610 | ||
611 | /* | |
612 | Local Variables: | |
613 | c-basic-offset:2 | |
614 | comment-column:40 | |
615 | End: | |
616 | */ |