2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006, 2007 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>
40 #include "configuration.h"
54 #include "authorize.h"
57 static int auto_reconfigure;
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' },
64 { "local", no_argument, 0, 'l' },
65 { "help-commands", no_argument, 0, 'H' },
69 /* display usage message and terminate */
70 static void help(void) {
72 " disorder [OPTIONS] COMMAND ...\n"
74 " --help, -h Display usage message\n"
75 " --help-commands, -H List commands\n"
76 " --version, -V Display version number\n"
77 " --config PATH, -c PATH Set configuration file\n"
78 " --local, -l Force connection to local server\n"
79 " --debug, -d Turn on debugging\n");
84 /* display version number and terminate */
85 static void version(void) {
86 xprintf("disorder version %s\n", disorder_version_string);
91 static void cf_version(disorder_client *c,
92 char attribute((unused)) **argv) {
95 if(disorder_version(c, &v)) exit(EXIT_FAILURE);
96 xprintf("%s\n", nullcheck(utf82mb(v)));
99 static void print_queue_entry(const struct queue_entry *q) {
100 if(q->track) xprintf("track %s\n", nullcheck(utf82mb(q->track)));
101 if(q->id) xprintf(" id %s\n", nullcheck(utf82mb(q->id)));
102 if(q->submitter) xprintf(" submitted by %s at %s",
103 nullcheck(utf82mb(q->submitter)), ctime(&q->when));
104 if(q->played) xprintf(" played at %s", ctime(&q->played));
105 if(q->state == playing_started
106 || q->state == playing_paused) xprintf(" %lds so far", q->sofar);
107 else if(q->expected) xprintf(" might start at %s", ctime(&q->expected));
108 if(q->scratched) xprintf(" scratched by %s\n",
109 nullcheck(utf82mb(q->scratched)));
110 else xprintf(" %s\n", playing_states[q->state]);
111 if(q->wstat) xprintf(" %s\n", wstat(q->wstat));
114 static void cf_playing(disorder_client *c,
115 char attribute((unused)) **argv) {
116 struct queue_entry *q;
118 if(disorder_playing(c, &q)) exit(EXIT_FAILURE);
120 print_queue_entry(q);
122 xprintf("nothing\n");
125 static void cf_play(disorder_client *c, char **argv) {
127 if(disorder_play(c, *argv++)) exit(EXIT_FAILURE);
130 static void cf_remove(disorder_client *c, char **argv) {
131 if(disorder_remove(c, argv[0])) exit(EXIT_FAILURE);
134 static void cf_disable(disorder_client *c,
135 char attribute((unused)) **argv) {
136 if(disorder_disable(c)) exit(EXIT_FAILURE);
139 static void cf_enable(disorder_client *c, char attribute((unused)) **argv) {
140 if(disorder_enable(c)) exit(EXIT_FAILURE);
143 static void cf_scratch(disorder_client *c,
145 if(disorder_scratch(c, argv[0])) exit(EXIT_FAILURE);
148 static void cf_shutdown(disorder_client *c,
149 char attribute((unused)) **argv) {
150 if(disorder_shutdown(c)) exit(EXIT_FAILURE);
153 static void cf_reconfigure(disorder_client *c,
154 char attribute((unused)) **argv) {
155 /* Re-check configuration for server */
156 if(config_read(1)) fatal(0, "cannot read configuration");
157 if(disorder_reconfigure(c)) exit(EXIT_FAILURE);
160 static void cf_rescan(disorder_client *c, char attribute((unused)) **argv) {
161 if(disorder_rescan(c)) exit(EXIT_FAILURE);
164 static void cf_somequeue(disorder_client *c,
165 int (*fn)(disorder_client *c,
166 struct queue_entry **qp)) {
167 struct queue_entry *q;
169 if(fn(c, &q)) exit(EXIT_FAILURE);
171 print_queue_entry(q);
176 static void cf_recent(disorder_client *c, char attribute((unused)) **argv) {
177 cf_somequeue(c, disorder_recent);
180 static void cf_queue(disorder_client *c, char attribute((unused)) **argv) {
181 cf_somequeue(c, disorder_queue);
184 static void cf_quack(disorder_client attribute((unused)) *c,
185 char attribute((unused)) **argv) {
187 " .------------------.\n"
188 " | Naath is a babe! |\n"
189 " `---------+--------'\n"
193 "~~~~~~~~~~~~~~~~~~~~~~\n"
197 static void cf_somelist(disorder_client *c, char **argv,
198 int (*fn)(disorder_client *c,
199 const char *arg, const char *re,
200 char ***vecp, int *nvecp)) {
205 re = xstrdup(argv[1] + 1);
208 if(fn(c, argv[0], re, &vec, 0)) exit(EXIT_FAILURE);
210 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
213 static int isarg_regexp(const char *s) {
217 static void cf_dirs(disorder_client *c,
219 cf_somelist(c, argv, disorder_directories);
222 static void cf_files(disorder_client *c, char **argv) {
223 cf_somelist(c, argv, disorder_files);
226 static void cf_allfiles(disorder_client *c, char **argv) {
227 cf_somelist(c, argv, disorder_allfiles);
230 static void cf_get(disorder_client *c, char **argv) {
233 if(disorder_get(c, argv[0], argv[1], &value)) exit(EXIT_FAILURE);
234 xprintf("%s\n", nullcheck(utf82mb(value)));
237 static void cf_length(disorder_client *c, char **argv) {
240 if(disorder_length(c, argv[0], &length)) exit(EXIT_FAILURE);
241 xprintf("%ld\n", length);
244 static void cf_set(disorder_client *c, char **argv) {
245 if(disorder_set(c, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
248 static void cf_unset(disorder_client *c, char **argv) {
249 if(disorder_unset(c, argv[0], argv[1])) exit(EXIT_FAILURE);
252 static void cf_prefs(disorder_client *c, char **argv) {
255 if(disorder_prefs(c, argv[0], &k)) exit(EXIT_FAILURE);
256 for(; k; k = k->next)
258 nullcheck(utf82mb(k->name)), nullcheck(utf82mb(k->value)));
261 static void cf_search(disorder_client *c, char **argv) {
265 if(disorder_search(c, *argv, &results, &nresults)) exit(EXIT_FAILURE);
266 for(n = 0; n < nresults; ++n)
267 xprintf("%s\n", nullcheck(utf82mb(results[n])));
270 static void cf_random_disable(disorder_client *c,
271 char attribute((unused)) **argv) {
272 if(disorder_random_disable(c)) exit(EXIT_FAILURE);
275 static void cf_random_enable(disorder_client *c,
276 char attribute((unused)) **argv) {
277 if(disorder_random_enable(c)) exit(EXIT_FAILURE);
280 static void cf_stats(disorder_client *c,
281 char attribute((unused)) **argv) {
284 if(disorder_stats(c, &vec, 0)) exit(EXIT_FAILURE);
286 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
289 static void cf_get_volume(disorder_client *c,
290 char attribute((unused)) **argv) {
293 if(disorder_get_volume(c, &l, &r)) exit(EXIT_FAILURE);
294 xprintf("%d %d\n", l, r);
297 static void cf_set_volume(disorder_client *c,
299 if(disorder_set_volume(c, atoi(argv[0]), atoi(argv[1]))) exit(EXIT_FAILURE);
302 static void cf_become(disorder_client *c,
304 if(disorder_become(c, argv[0])) exit(EXIT_FAILURE);
307 static void cf_log(disorder_client *c,
308 char attribute((unused)) **argv) {
309 if(disorder_log(c, sink_stdio("stdout", stdout))) exit(EXIT_FAILURE);
312 static void cf_move(disorder_client *c,
317 if((e = xstrtol(&n, argv[1], 0, 10)))
318 fatal(e, "cannot convert '%s'", argv[1]);
319 if(n > INT_MAX || n < INT_MIN)
320 fatal(e, "%ld out of range", n);
321 if(disorder_move(c, argv[0], (int)n)) exit(EXIT_FAILURE);
324 static void cf_part(disorder_client *c,
328 if(disorder_part(c, &s, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
329 xprintf("%s\n", nullcheck(utf82mb(s)));
332 static int isarg_filename(const char *s) {
336 static void cf_authorize(disorder_client attribute((unused)) *c,
338 if(!authorize(argv[0]))
339 auto_reconfigure = 1;
342 static void cf_resolve(disorder_client *c,
346 if(disorder_resolve(c, &track, argv[0])) exit(EXIT_FAILURE);
347 xprintf("%s\n", nullcheck(utf82mb(track)));
350 static void cf_pause(disorder_client *c,
351 char attribute((unused)) **argv) {
352 if(disorder_pause(c)) exit(EXIT_FAILURE);
355 static void cf_resume(disorder_client *c,
356 char attribute((unused)) **argv) {
357 if(disorder_resume(c)) exit(EXIT_FAILURE);
360 static void cf_tags(disorder_client *c,
361 char attribute((unused)) **argv) {
364 if(disorder_tags(c, &vec, 0)) exit(EXIT_FAILURE);
366 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
369 static void cf_get_global(disorder_client *c, char **argv) {
372 if(disorder_get_global(c, argv[0], &value)) exit(EXIT_FAILURE);
373 xprintf("%s\n", nullcheck(utf82mb(value)));
376 static void cf_set_global(disorder_client *c, char **argv) {
377 if(disorder_set_global(c, argv[0], argv[1])) exit(EXIT_FAILURE);
380 static void cf_unset_global(disorder_client *c, char **argv) {
381 if(disorder_unset_global(c, argv[0])) exit(EXIT_FAILURE);
384 static int isarg_integer(const char *s) {
387 if(!isdigit((unsigned char)*s))
394 static void cf_new(disorder_client *c,
398 if(disorder_new_tracks(c, &vec, 0, argv[0] ? atoi(argv[0]) : 0))
401 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
404 static void cf_rtp_address(disorder_client *c,
405 char attribute((unused)) **argv) {
406 char *address, *port;
408 if(disorder_rtp_address(c, &address, &port)) exit(EXIT_FAILURE);
409 xprintf("address: %s\nport: %s\n", address, port);
412 static const struct command {
415 void (*fn)(disorder_client *c, char **);
416 int (*isarg)(const char *);
417 const char *argstr, *desc;
419 { "allfiles", 1, 2, cf_allfiles, isarg_regexp, "DIR [~REGEXP]",
420 "List all files and directories in DIR" },
421 { "authorize", 1, 1, cf_authorize, 0, "USER",
422 "Authorize USER to connect to the server" },
423 { "become", 1, 1, cf_become, 0, "USER",
424 "Become user USER" },
425 { "dirs", 1, 2, cf_dirs, isarg_regexp, "DIR [~REGEXP]",
426 "List directories in DIR" },
427 { "disable", 0, 0, cf_disable, 0, "",
429 { "disable-random", 0, 0, cf_random_disable, 0, "",
430 "Disable random play" },
431 { "enable", 0, 0, cf_enable, 0, "",
433 { "enable-random", 0, 0, cf_random_enable, 0, "",
434 "Enable random play" },
435 { "files", 1, 2, cf_files, isarg_regexp, "DIR [~REGEXP]",
436 "List files in DIR" },
437 { "get", 2, 2, cf_get, 0, "TRACK NAME",
438 "Get a preference value" },
439 { "get-global", 1, 1, cf_get_global, 0, "NAME",
440 "Get a global preference value" },
441 { "get-volume", 0, 0, cf_get_volume, 0, "",
442 "Get the current volume" },
443 { "length", 1, 1, cf_length, 0, "TRACK",
444 "Get the length of TRACK in seconds" },
445 { "log", 0, 0, cf_log, 0, "",
446 "Copy event log to stdout" },
447 { "move", 2, 2, cf_move, 0, "TRACK DELTA",
448 "Move a track in the queue" },
449 { "new", 0, 1, cf_new, isarg_integer, "[MAX]",
450 "Get the most recently added MAX tracks" },
451 { "part", 3, 3, cf_part, 0, "TRACK CONTEXT PART",
452 "Find a track name part" },
453 { "pause", 0, 0, cf_pause, 0, "",
454 "Pause the currently playing track" },
455 { "play", 1, INT_MAX, cf_play, isarg_filename, "TRACKS...",
456 "Add TRACKS to the end of the queue" },
457 { "playing", 0, 0, cf_playing, 0, "",
458 "Report the playing track" },
459 { "prefs", 1, 1, cf_prefs, 0, "TRACK",
460 "Display all the preferences for TRACK" },
461 { "quack", 0, 0, cf_quack, 0, 0, 0 },
462 { "queue", 0, 0, cf_queue, 0, "",
463 "Display the current queue" },
464 { "random-disable", 0, 0, cf_random_disable, 0, "",
465 "Disable random play" },
466 { "random-enable", 0, 0, cf_random_enable, 0, "",
467 "Enable random play" },
468 { "recent", 0, 0, cf_recent, 0, "",
469 "Display recently played track" },
470 { "reconfigure", 0, 0, cf_reconfigure, 0, "",
471 "Reconfigure the daemon" },
472 { "remove", 1, 1, cf_remove, 0, "TRACK",
473 "Remove a track from the queue" },
474 { "rescan", 0, 0, cf_rescan, 0, "",
475 "Rescan for new tracks" },
476 { "resolve", 1, 1, cf_resolve, 0, "TRACK",
477 "Resolve alias for TRACK" },
478 { "resume", 0, 0, cf_resume, 0, "",
479 "Resume after a pause" },
480 { "rtp-address", 0, 0, cf_rtp_address, 0, "",
481 "Report server's broadcast address" },
482 { "scratch", 0, 0, cf_scratch, 0, "",
483 "Scratch the currently playing track" },
484 { "scratch-id", 1, 1, cf_scratch, 0, "ID",
485 "Scratch the currently playing track" },
486 { "search", 1, 1, cf_search, 0, "WORDS",
487 "Display tracks matching all the words" },
488 { "set", 3, 3, cf_set, 0, "TRACK NAME VALUE",
489 "Set a preference value" },
490 { "set-global", 2, 2, cf_set_global, 0, "NAME VALUE",
491 "Set a global preference value" },
492 { "set-volume", 2, 2, cf_set_volume, 0, "LEFT RIGHT",
494 { "shutdown", 0, 0, cf_shutdown, 0, "",
495 "Shut down the daemon" },
496 { "stats", 0, 0, cf_stats, 0, "",
497 "Display server statistics" },
498 { "tags", 0, 0, cf_tags, 0, "",
500 { "unset", 2, 2, cf_unset, 0, "TRACK NAME",
501 "Unset a preference" },
502 { "unset-global", 1, 1, cf_unset_global, 0, "NAME",
503 "Unset a global preference" },
504 { "version", 0, 0, cf_version, 0, "",
505 "Display the server version" },
508 static void help_commands(void) {
509 unsigned n, max = 0, l;
511 xprintf("Command summary:\n");
512 for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
513 if(!commands[n].desc) continue;
514 l = strlen(commands[n].name);
515 if(*commands[n].argstr)
516 l += strlen(commands[n].argstr) + 1;
520 for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
521 if(!commands[n].desc) continue;
522 l = strlen(commands[n].name);
523 if(*commands[n].argstr)
524 l += strlen(commands[n].argstr) + 1;
525 xprintf(" %s%s%s%*s %s\n", commands[n].name,
526 *commands[n].argstr ? " " : "",
535 int main(int argc, char **argv) {
536 int n, i, j, local = 0;
537 disorder_client *c = 0;
542 /* garbage-collect PCRE's memory */
543 pcre_malloc = xmalloc;
545 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
546 while((n = getopt_long(argc, argv, "hVc:dHl", options, 0)) >= 0) {
549 case 'H': help_commands();
551 case 'c': configfile = optarg; break;
552 case 'd': debugging = 1; break;
553 case 'l': local = 1; break;
554 default: fatal(0, "invalid option");
557 if(config_read(0)) fatal(0, "cannot read configuration");
559 config->connect.n = 0;
560 if(!(c = disorder_new(1))) exit(EXIT_FAILURE);
561 if(disorder_connect(c)) exit(EXIT_FAILURE);
563 /* accumulate command args */
565 if((i = TABLE_FIND(commands, struct command, name, argv[n])) < 0)
566 fatal(0, "unknown command '%s'", argv[n]);
567 if(n + commands[i].min >= argc)
568 fatal(0, "missing arguments to '%s'", argv[n]);
571 for(j = 0; j < commands[i].min; ++j)
572 vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
573 for(; j < commands[i].max
575 && commands[i].isarg(argv[n + j]); ++j)
576 vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
577 vector_terminate(&args);
578 commands[i].fn(c, args.vec);
581 if(auto_reconfigure) {
583 if(disorder_reconfigure(c)) exit(EXIT_FAILURE);
585 if(c && disorder_close(c)) exit(EXIT_FAILURE);
586 if(fclose(stdout) < 0) fatal(errno, "error closing stdout");