2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006 Richard Kettlewell
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.
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.
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
25 #include <sys/types.h>
26 #include <sys/socket.h>
39 #include "configuration.h"
53 #include "authorize.h"
56 static int auto_reconfigure;
58 static const struct option options[] = {
59 { "help", no_argument, 0, 'h' },
60 { "version", no_argument, 0, 'V' },
61 { "config", required_argument, 0, 'c' },
62 { "debug", no_argument, 0, 'd' },
63 { "help-commands", no_argument, 0, 'H' },
67 /* display usage message and terminate */
68 static void help(void) {
70 " disorder [OPTIONS] COMMAND ...\n"
72 " --help, -h Display usage message\n"
73 " --help-commands, -H List commands\n"
74 " --version, -V Display version number\n"
75 " --config PATH, -c PATH Set configuration file\n"
76 " --debug, -d Turn on debugging\n");
81 /* display version number and terminate */
82 static void version(void) {
83 xprintf("disorder version %s\n", disorder_version_string);
88 static void cf_version(disorder_client *c,
89 char attribute((unused)) **argv) {
92 if(disorder_version(c, &v)) exit(EXIT_FAILURE);
93 xprintf("%s\n", nullcheck(utf82mb(v)));
96 static void print_queue_entry(const struct queue_entry *q) {
97 if(q->track) xprintf("track %s\n", nullcheck(utf82mb(q->track)));
98 if(q->id) xprintf(" id %s\n", nullcheck(utf82mb(q->id)));
99 if(q->submitter) xprintf(" submitted by %s at %s",
100 nullcheck(utf82mb(q->submitter)), ctime(&q->when));
101 if(q->played) xprintf(" played at %s", ctime(&q->played));
102 if(q->state == playing_started
103 || q->state == playing_paused) xprintf(" %lds so far", q->sofar);
104 else if(q->expected) xprintf(" might start at %s", ctime(&q->expected));
105 if(q->scratched) xprintf(" scratched by %s\n",
106 nullcheck(utf82mb(q->scratched)));
107 else xprintf(" %s\n", playing_states[q->state]);
108 if(q->wstat) xprintf(" %s\n", wstat(q->wstat));
111 static void cf_playing(disorder_client *c,
112 char attribute((unused)) **argv) {
113 struct queue_entry *q;
115 if(disorder_playing(c, &q)) exit(EXIT_FAILURE);
117 print_queue_entry(q);
119 xprintf("nothing\n");
122 static void cf_play(disorder_client *c, char **argv) {
124 if(disorder_play(c, *argv++)) exit(EXIT_FAILURE);
127 static void cf_remove(disorder_client *c, char **argv) {
128 if(disorder_remove(c, argv[0])) exit(EXIT_FAILURE);
131 static void cf_disable(disorder_client *c,
132 char attribute((unused)) **argv) {
133 if(disorder_disable(c)) exit(EXIT_FAILURE);
136 static void cf_enable(disorder_client *c, char attribute((unused)) **argv) {
137 if(disorder_enable(c)) exit(EXIT_FAILURE);
140 static void cf_scratch(disorder_client *c,
142 if(disorder_scratch(c, argv[0])) exit(EXIT_FAILURE);
145 static void cf_shutdown(disorder_client *c,
146 char attribute((unused)) **argv) {
147 if(disorder_shutdown(c)) exit(EXIT_FAILURE);
150 static void cf_reconfigure(disorder_client *c,
151 char attribute((unused)) **argv) {
152 if(disorder_reconfigure(c)) exit(EXIT_FAILURE);
155 static void cf_rescan(disorder_client *c, char attribute((unused)) **argv) {
156 if(disorder_rescan(c)) exit(EXIT_FAILURE);
159 static void cf_somequeue(disorder_client *c,
160 int (*fn)(disorder_client *c,
161 struct queue_entry **qp)) {
162 struct queue_entry *q;
164 if(fn(c, &q)) exit(EXIT_FAILURE);
166 print_queue_entry(q);
171 static void cf_recent(disorder_client *c, char attribute((unused)) **argv) {
172 cf_somequeue(c, disorder_recent);
175 static void cf_queue(disorder_client *c, char attribute((unused)) **argv) {
176 cf_somequeue(c, disorder_queue);
179 static void cf_quack(disorder_client attribute((unused)) *c,
180 char attribute((unused)) **argv) {
182 " .------------------.\n"
183 " | Naath is a babe! |\n"
184 " `---------+--------'\n"
188 "~~~~~~~~~~~~~~~~~~~~~~\n"
192 static void cf_somelist(disorder_client *c, char **argv,
193 int (*fn)(disorder_client *c,
194 const char *arg, const char *re,
195 char ***vecp, int *nvecp)) {
200 re = xstrdup(argv[1] + 1);
203 if(fn(c, argv[0], re, &vec, 0)) exit(EXIT_FAILURE);
205 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
208 static int isarg_regexp(const char *s) {
212 static void cf_dirs(disorder_client *c,
214 cf_somelist(c, argv, disorder_directories);
217 static void cf_files(disorder_client *c, char **argv) {
218 cf_somelist(c, argv, disorder_files);
221 static void cf_allfiles(disorder_client *c, char **argv) {
222 cf_somelist(c, argv, disorder_allfiles);
225 static void cf_get(disorder_client *c, char **argv) {
228 if(disorder_get(c, argv[0], argv[1], &value)) exit(EXIT_FAILURE);
229 xprintf("%s\n", nullcheck(utf82mb(value)));
232 static void cf_length(disorder_client *c, char **argv) {
235 if(disorder_length(c, argv[0], &length)) exit(EXIT_FAILURE);
236 xprintf("%ld\n", length);
239 static void cf_set(disorder_client *c, char **argv) {
240 if(disorder_set(c, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
243 static void cf_unset(disorder_client *c, char **argv) {
244 if(disorder_unset(c, argv[0], argv[1])) exit(EXIT_FAILURE);
247 static void cf_prefs(disorder_client *c, char **argv) {
250 if(disorder_prefs(c, argv[0], &k)) exit(EXIT_FAILURE);
251 for(; k; k = k->next)
253 nullcheck(utf82mb(k->name)), nullcheck(utf82mb(k->value)));
256 static void cf_search(disorder_client *c, char **argv) {
260 if(disorder_search(c, *argv, &results, &nresults)) exit(EXIT_FAILURE);
261 for(n = 0; n < nresults; ++n)
262 xprintf("%s\n", nullcheck(utf82mb(results[n])));
265 static void cf_random_disable(disorder_client *c,
266 char attribute((unused)) **argv) {
267 if(disorder_random_disable(c)) exit(EXIT_FAILURE);
270 static void cf_random_enable(disorder_client *c,
271 char attribute((unused)) **argv) {
272 if(disorder_random_enable(c)) exit(EXIT_FAILURE);
275 static void cf_stats(disorder_client *c,
276 char attribute((unused)) **argv) {
279 if(disorder_stats(c, &vec, 0)) exit(EXIT_FAILURE);
281 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
284 static void cf_get_volume(disorder_client *c,
285 char attribute((unused)) **argv) {
288 if(disorder_get_volume(c, &l, &r)) exit(EXIT_FAILURE);
289 xprintf("%d %d\n", l, r);
292 static void cf_set_volume(disorder_client *c,
294 if(disorder_set_volume(c, atoi(argv[0]), atoi(argv[1]))) exit(EXIT_FAILURE);
297 static void cf_become(disorder_client *c,
299 if(disorder_become(c, argv[0])) exit(EXIT_FAILURE);
302 static void cf_log(disorder_client *c,
303 char attribute((unused)) **argv) {
304 if(disorder_log(c, sink_stdio("stdout", stdout))) exit(EXIT_FAILURE);
307 static void cf_move(disorder_client *c,
312 if((e = xstrtol(&n, argv[1], 0, 10)))
313 fatal(e, "cannot convert '%s'", argv[1]);
314 if(n > INT_MAX || n < INT_MIN)
315 fatal(e, "%ld out of range", n);
316 if(disorder_move(c, argv[0], (int)n)) exit(EXIT_FAILURE);
319 static void cf_part(disorder_client *c,
323 if(disorder_part(c, &s, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
324 xprintf("%s\n", nullcheck(utf82mb(s)));
327 static int isarg_filename(const char *s) {
331 static void cf_authorize(disorder_client attribute((unused)) *c,
333 if(!authorize(argv[0]))
334 auto_reconfigure = 1;
337 static void cf_resolve(disorder_client *c,
341 if(disorder_resolve(c, &track, argv[0])) exit(EXIT_FAILURE);
342 xprintf("%s\n", nullcheck(utf82mb(track)));
345 static void cf_pause(disorder_client *c,
346 char attribute((unused)) **argv) {
347 if(disorder_pause(c)) exit(EXIT_FAILURE);
350 static void cf_resume(disorder_client *c,
351 char attribute((unused)) **argv) {
352 if(disorder_resume(c)) exit(EXIT_FAILURE);
355 static void cf_tags(disorder_client *c,
356 char attribute((unused)) **argv) {
359 if(disorder_tags(c, &vec, 0)) exit(EXIT_FAILURE);
361 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
364 static void cf_get_global(disorder_client *c, char **argv) {
367 if(disorder_get_global(c, argv[0], &value)) exit(EXIT_FAILURE);
368 xprintf("%s\n", nullcheck(utf82mb(value)));
371 static void cf_set_global(disorder_client *c, char **argv) {
372 if(disorder_set_global(c, argv[0], argv[1])) exit(EXIT_FAILURE);
375 static void cf_unset_global(disorder_client *c, char **argv) {
376 if(disorder_unset_global(c, argv[0])) exit(EXIT_FAILURE);
379 static const struct command {
382 void (*fn)(disorder_client *c, char **);
383 int (*isarg)(const char *);
384 const char *argstr, *desc;
386 { "allfiles", 1, 2, cf_allfiles, isarg_regexp, "DIR [~REGEXP]",
387 "List all files and directories in DIR" },
388 { "authorize", 1, 1, cf_authorize, 0, "USER",
389 "Authorize USER to connect to the server" },
390 { "become", 1, 1, cf_become, 0, "USER",
391 "Become user USER" },
392 { "dirs", 1, 2, cf_dirs, isarg_regexp, "DIR [~REGEXP]",
393 "List directories in DIR" },
394 { "disable", 0, 0, cf_disable, 0, "",
396 { "disable-random", 0, 0, cf_random_disable, 0, "",
397 "Disable random play" },
398 { "enable", 0, 0, cf_enable, 0, "",
400 { "enable-random", 0, 0, cf_random_enable, 0, "",
401 "Enable random play" },
402 { "files", 1, 2, cf_files, isarg_regexp, "DIR [~REGEXP]",
403 "List files in DIR" },
404 { "get", 2, 2, cf_get, 0, "TRACK NAME",
405 "Get a preference value" },
406 { "get-global", 1, 1, cf_get_global, 0, "NAME",
407 "Get a global preference value" },
408 { "get-volume", 0, 0, cf_get_volume, 0, "",
409 "Get the current volume" },
410 { "length", 1, 1, cf_length, 0, "TRACK",
411 "Get the length of TRACK in seconds" },
412 { "log", 0, 0, cf_log, 0, "",
413 "Copy event log to stdout" },
414 { "move", 2, 2, cf_move, 0, "TRACK DELTA",
415 "Move a track in the queue" },
416 { "part", 3, 3, cf_part, 0, "TRACK CONTEXT PART",
417 "Find a track name part" },
418 { "pause", 0, 0, cf_pause, 0, "",
419 "Pause the currently playing track" },
420 { "play", 1, INT_MAX, cf_play, isarg_filename, "TRACKS...",
421 "Add TRACKS to the end of the queue" },
422 { "playing", 0, 0, cf_playing, 0, "",
423 "Report the playing track" },
424 { "prefs", 1, 1, cf_prefs, 0, "TRACK",
425 "Display all the preferences for TRACK" },
426 { "quack", 0, 0, cf_quack, 0, 0, 0 },
427 { "queue", 0, 0, cf_queue, 0, "",
428 "Display the current queue" },
429 { "random-disable", 0, 0, cf_random_disable, 0, "",
430 "Disable random play" },
431 { "random-enable", 0, 0, cf_random_enable, 0, "",
432 "Enable random play" },
433 { "recent", 0, 0, cf_recent, 0, "",
434 "Display recently played track" },
435 { "reconfigure", 0, 0, cf_reconfigure, 0, "",
436 "Reconfigure the daemon" },
437 { "remove", 1, 1, cf_remove, 0, "TRACK",
438 "Remove a track from the queue" },
439 { "rescan", 0, 0, cf_rescan, 0, "",
440 "Rescan for new tracks" },
441 { "resolve", 1, 1, cf_resolve, 0, "TRACK",
442 "Resolve alias for TRACK" },
443 { "resume", 0, 0, cf_resume, 0, "",
444 "Resume after a pause" },
445 { "scratch", 0, 0, cf_scratch, 0, "",
446 "Scratch the currently playing track" },
447 { "scratch-id", 1, 1, cf_scratch, 0, "ID",
448 "Scratch the currently playing track" },
449 { "search", 1, 1, cf_search, 0, "WORDS",
450 "Display tracks matching all the words" },
451 { "set", 3, 3, cf_set, 0, "TRACK NAME VALUE",
452 "Set a preference value" },
453 { "set-global", 2, 2, cf_set_global, 0, "NAME VALUE",
454 "Set a global preference value" },
455 { "set-volume", 2, 2, cf_set_volume, 0, "LEFT RIGHT",
457 { "shutdown", 0, 0, cf_shutdown, 0, "",
458 "Shut down the daemon" },
459 { "stats", 0, 0, cf_stats, 0, "",
460 "Display server statistics" },
461 { "tags", 0, 0, cf_tags, 0, "",
463 { "unset", 2, 2, cf_unset, 0, "TRACK NAME",
464 "Unset a preference" },
465 { "unset-global", 1, 1, cf_unset_global, 0, "NAME",
466 "Unset a global preference" },
467 { "version", 0, 0, cf_version, 0, "",
468 "Display the server version" },
471 static void help_commands(void) {
472 unsigned n, max = 0, l;
474 xprintf("Command summary:\n");
475 for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
476 if(!commands[n].desc) continue;
477 l = strlen(commands[n].name);
478 if(*commands[n].argstr)
479 l += strlen(commands[n].argstr) + 1;
483 for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
484 if(!commands[n].desc) continue;
485 l = strlen(commands[n].name);
486 if(*commands[n].argstr)
487 l += strlen(commands[n].argstr) + 1;
488 xprintf(" %s%s%s%*s %s\n", commands[n].name,
489 *commands[n].argstr ? " " : "",
498 int main(int argc, char **argv) {
500 disorder_client *c = 0;
506 /* garbage-collect PCRE's memory */
507 pcre_malloc = xmalloc;
509 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
510 while((n = getopt_long(argc, argv, "hVc:dHL", options, 0)) >= 0) {
513 case 'H': help_commands();
515 case 'c': configfile = optarg; break;
516 case 'd': debugging = 1; break;
517 default: fatal(0, "invalid option");
520 if(config_read()) fatal(0, "cannot read configuration");
521 if(!(c = disorder_new(1))) exit(EXIT_FAILURE);
522 s = config_get_file("socket");
523 if(disorder_connect(c)) exit(EXIT_FAILURE);
525 /* accumulate command args */
527 if((i = TABLE_FIND(commands, struct command, name, argv[n])) < 0)
528 fatal(0, "unknown command '%s'", argv[n]);
529 if(n + commands[i].min >= argc)
530 fatal(0, "missing arguments to '%s'", argv[n]);
533 for(j = 0; j < commands[i].min; ++j)
534 vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
535 for(; j < commands[i].max
537 && commands[i].isarg(argv[n + j]); ++j)
538 vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
539 vector_terminate(&args);
540 commands[i].fn(c, args.vec);
543 if(auto_reconfigure) {
545 if(disorder_reconfigure(c)) exit(EXIT_FAILURE);
547 if(c && disorder_close(c)) exit(EXIT_FAILURE);
548 if(fclose(stdout) < 0) fatal(errno, "error closing stdout");