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>
38 #include "configuration.h"
52 #include "authorize.h"
55 static int auto_reconfigure;
57 static const struct option options[] = {
58 { "help", no_argument, 0, 'h' },
59 { "version", no_argument, 0, 'V' },
60 { "config", required_argument, 0, 'c' },
61 { "debug", no_argument, 0, 'd' },
62 { "help-commands", no_argument, 0, 'H' },
66 /* display usage message and terminate */
67 static void help(void) {
69 " disorder [OPTIONS] COMMAND ...\n"
71 " --help, -h Display usage message\n"
72 " --help-commands, -H List commands\n"
73 " --version, -V Display version number\n"
74 " --config PATH, -c PATH Set configuration file\n"
75 " --debug, -d Turn on debugging\n");
80 /* display version number and terminate */
81 static void version(void) {
82 xprintf("disorder version %s\n", disorder_version_string);
87 static void cf_version(disorder_client *c,
88 char attribute((unused)) **argv) {
91 if(disorder_version(c, &v)) exit(EXIT_FAILURE);
92 xprintf("%s\n", nullcheck(utf82mb(v)));
95 static void print_queue_entry(const struct queue_entry *q) {
96 if(q->track) xprintf("track %s\n", nullcheck(utf82mb(q->track)));
97 if(q->id) xprintf(" id %s\n", nullcheck(utf82mb(q->id)));
98 if(q->submitter) xprintf(" submitted by %s at %s",
99 nullcheck(utf82mb(q->submitter)), ctime(&q->when));
100 if(q->played) xprintf(" played at %s", ctime(&q->played));
101 if(q->state == playing_started
102 || q->state == playing_paused) xprintf(" %lds so far", q->sofar);
103 else if(q->expected) xprintf(" might start at %s", ctime(&q->expected));
104 if(q->scratched) xprintf(" scratched by %s\n",
105 nullcheck(utf82mb(q->scratched)));
106 else xprintf(" %s\n", playing_states[q->state]);
107 if(q->wstat) xprintf(" %s\n", wstat(q->wstat));
110 static void cf_playing(disorder_client *c,
111 char attribute((unused)) **argv) {
112 struct queue_entry *q;
114 if(disorder_playing(c, &q)) exit(EXIT_FAILURE);
116 print_queue_entry(q);
118 xprintf("nothing\n");
121 static void cf_play(disorder_client *c, char **argv) {
123 if(disorder_play(c, *argv++)) exit(EXIT_FAILURE);
126 static void cf_remove(disorder_client *c, char **argv) {
127 if(disorder_remove(c, argv[0])) exit(EXIT_FAILURE);
130 static void cf_disable(disorder_client *c,
131 char attribute((unused)) **argv) {
132 if(disorder_disable(c)) exit(EXIT_FAILURE);
135 static void cf_enable(disorder_client *c, char attribute((unused)) **argv) {
136 if(disorder_enable(c)) exit(EXIT_FAILURE);
139 static void cf_scratch(disorder_client *c,
141 if(disorder_scratch(c, argv[0])) exit(EXIT_FAILURE);
144 static void cf_shutdown(disorder_client *c,
145 char attribute((unused)) **argv) {
146 if(disorder_shutdown(c)) exit(EXIT_FAILURE);
149 static void cf_reconfigure(disorder_client *c,
150 char attribute((unused)) **argv) {
151 if(disorder_reconfigure(c)) exit(EXIT_FAILURE);
154 static void cf_rescan(disorder_client *c, char attribute((unused)) **argv) {
155 if(disorder_rescan(c)) exit(EXIT_FAILURE);
158 static void cf_somequeue(disorder_client *c,
159 int (*fn)(disorder_client *c,
160 struct queue_entry **qp)) {
161 struct queue_entry *q;
163 if(fn(c, &q)) exit(EXIT_FAILURE);
165 print_queue_entry(q);
170 static void cf_recent(disorder_client *c, char attribute((unused)) **argv) {
171 cf_somequeue(c, disorder_recent);
174 static void cf_queue(disorder_client *c, char attribute((unused)) **argv) {
175 cf_somequeue(c, disorder_queue);
178 static void cf_quack(disorder_client attribute((unused)) *c,
179 char attribute((unused)) **argv) {
181 " .------------------.\n"
182 " | Naath is a babe! |\n"
183 " `---------+--------'\n"
187 "~~~~~~~~~~~~~~~~~~~~~~\n"
191 static void cf_somelist(disorder_client *c, char **argv,
192 int (*fn)(disorder_client *c,
193 const char *arg, const char *re,
194 char ***vecp, int *nvecp)) {
199 re = xstrdup(argv[1] + 1);
202 if(fn(c, argv[0], re, &vec, 0)) exit(EXIT_FAILURE);
204 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
207 static int isarg_regexp(const char *s) {
211 static void cf_dirs(disorder_client *c,
213 cf_somelist(c, argv, disorder_directories);
216 static void cf_files(disorder_client *c, char **argv) {
217 cf_somelist(c, argv, disorder_files);
220 static void cf_allfiles(disorder_client *c, char **argv) {
221 cf_somelist(c, argv, disorder_allfiles);
224 static void cf_get(disorder_client *c, char **argv) {
227 if(disorder_get(c, argv[0], argv[1], &value)) exit(EXIT_FAILURE);
228 xprintf("%s\n", nullcheck(utf82mb(value)));
231 static void cf_length(disorder_client *c, char **argv) {
234 if(disorder_length(c, argv[0], &length)) exit(EXIT_FAILURE);
235 xprintf("%ld\n", length);
238 static void cf_set(disorder_client *c, char **argv) {
239 if(disorder_set(c, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
242 static void cf_unset(disorder_client *c, char **argv) {
243 if(disorder_unset(c, argv[0], argv[1])) exit(EXIT_FAILURE);
246 static void cf_prefs(disorder_client *c, char **argv) {
249 if(disorder_prefs(c, argv[0], &k)) exit(EXIT_FAILURE);
250 for(; k; k = k->next)
252 nullcheck(utf82mb(k->name)), nullcheck(utf82mb(k->value)));
255 static void cf_search(disorder_client *c, char **argv) {
259 if(disorder_search(c, *argv, &results, &nresults)) exit(EXIT_FAILURE);
260 for(n = 0; n < nresults; ++n)
261 xprintf("%s\n", nullcheck(utf82mb(results[n])));
264 static void cf_random_disable(disorder_client *c,
265 char attribute((unused)) **argv) {
266 if(disorder_random_disable(c)) exit(EXIT_FAILURE);
269 static void cf_random_enable(disorder_client *c,
270 char attribute((unused)) **argv) {
271 if(disorder_random_enable(c)) exit(EXIT_FAILURE);
274 static void cf_stats(disorder_client *c,
275 char attribute((unused)) **argv) {
278 if(disorder_stats(c, &vec, 0)) exit(EXIT_FAILURE);
280 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
283 static void cf_get_volume(disorder_client *c,
284 char attribute((unused)) **argv) {
287 if(disorder_get_volume(c, &l, &r)) exit(EXIT_FAILURE);
288 xprintf("%d %d\n", l, r);
291 static void cf_set_volume(disorder_client *c,
293 if(disorder_set_volume(c, atoi(argv[0]), atoi(argv[1]))) exit(EXIT_FAILURE);
296 static void cf_become(disorder_client *c,
298 if(disorder_become(c, argv[0])) exit(EXIT_FAILURE);
301 static void cf_log(disorder_client *c,
302 char attribute((unused)) **argv) {
303 if(disorder_log(c, sink_stdio("stdout", stdout))) exit(EXIT_FAILURE);
306 static void cf_move(disorder_client *c,
311 if((e = xstrtol(&n, argv[1], 0, 10)))
312 fatal(e, "cannot convert '%s'", argv[1]);
313 if(n > INT_MAX || n < INT_MIN)
314 fatal(e, "%ld out of range", n);
315 if(disorder_move(c, argv[0], (int)n)) exit(EXIT_FAILURE);
318 static void cf_part(disorder_client *c,
322 if(disorder_part(c, &s, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
323 xprintf("%s\n", nullcheck(utf82mb(s)));
326 static int isarg_filename(const char *s) {
330 static void cf_authorize(disorder_client attribute((unused)) *c,
332 if(!authorize(argv[0]))
333 auto_reconfigure = 1;
336 static void cf_resolve(disorder_client *c,
340 if(disorder_resolve(c, &track, argv[0])) exit(EXIT_FAILURE);
341 xprintf("%s\n", nullcheck(utf82mb(track)));
344 static void cf_pause(disorder_client *c,
345 char attribute((unused)) **argv) {
346 if(disorder_pause(c)) exit(EXIT_FAILURE);
349 static void cf_resume(disorder_client *c,
350 char attribute((unused)) **argv) {
351 if(disorder_resume(c)) exit(EXIT_FAILURE);
354 static void cf_tags(disorder_client *c,
355 char attribute((unused)) **argv) {
358 if(disorder_tags(c, &vec, 0)) exit(EXIT_FAILURE);
360 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
363 static void cf_get_global(disorder_client *c, char **argv) {
366 if(disorder_get_global(c, argv[0], &value)) exit(EXIT_FAILURE);
367 xprintf("%s\n", nullcheck(utf82mb(value)));
370 static void cf_set_global(disorder_client *c, char **argv) {
371 if(disorder_set_global(c, argv[0], argv[1])) exit(EXIT_FAILURE);
374 static void cf_unset_global(disorder_client *c, char **argv) {
375 if(disorder_unset_global(c, argv[0])) exit(EXIT_FAILURE);
378 static const struct command {
381 void (*fn)(disorder_client *c, char **);
382 int (*isarg)(const char *);
383 const char *argstr, *desc;
385 { "allfiles", 1, 2, cf_allfiles, isarg_regexp, "DIR [~REGEXP]",
386 "List all files and directories in DIR" },
387 { "authorize", 1, 1, cf_authorize, 0, "USER",
388 "Authorize USER to connect to the server" },
389 { "become", 1, 1, cf_become, 0, "USER",
390 "Become user USER" },
391 { "dirs", 1, 2, cf_dirs, isarg_regexp, "DIR [~REGEXP]",
392 "List directories in DIR" },
393 { "disable", 0, 0, cf_disable, 0, "",
395 { "disable-random", 0, 0, cf_random_disable, 0, "",
396 "Disable random play" },
397 { "enable", 0, 0, cf_enable, 0, "",
399 { "enable-random", 0, 0, cf_random_enable, 0, "",
400 "Enable random play" },
401 { "files", 1, 2, cf_files, isarg_regexp, "DIR [~REGEXP]",
402 "List files in DIR" },
403 { "get", 2, 2, cf_get, 0, "TRACK NAME",
404 "Get a preference value" },
405 { "get-global", 1, 1, cf_get_global, 0, "NAME",
406 "Get a global preference value" },
407 { "get-volume", 0, 0, cf_get_volume, 0, "",
408 "Get the current volume" },
409 { "length", 1, 1, cf_length, 0, "TRACK",
410 "Get the length of TRACK in seconds" },
411 { "log", 0, 0, cf_log, 0, "",
412 "Copy event log to stdout" },
413 { "move", 2, 2, cf_move, 0, "TRACK DELTA",
414 "Move a track in the queue" },
415 { "part", 3, 3, cf_part, 0, "TRACK CONTEXT PART",
416 "Find a track name part" },
417 { "pause", 0, 0, cf_pause, 0, "",
418 "Pause the currently playing track" },
419 { "play", 1, INT_MAX, cf_play, isarg_filename, "TRACKS...",
420 "Add TRACKS to the end of the queue" },
421 { "playing", 0, 0, cf_playing, 0, "",
422 "Report the playing track" },
423 { "prefs", 1, 1, cf_prefs, 0, "TRACK",
424 "Display all the preferences for TRACK" },
425 { "quack", 0, 0, cf_quack, 0, 0, 0 },
426 { "queue", 0, 0, cf_queue, 0, "",
427 "Display the current queue" },
428 { "random-disable", 0, 0, cf_random_disable, 0, "",
429 "Disable random play" },
430 { "random-enable", 0, 0, cf_random_enable, 0, "",
431 "Enable random play" },
432 { "recent", 0, 0, cf_recent, 0, "",
433 "Display recently played track" },
434 { "reconfigure", 0, 0, cf_reconfigure, 0, "",
435 "Reconfigure the daemon" },
436 { "remove", 1, 1, cf_remove, 0, "TRACK",
437 "Remove a track from the queue" },
438 { "rescan", 0, 0, cf_rescan, 0, "",
439 "Rescan for new tracks" },
440 { "resolve", 1, 1, cf_resolve, 0, "TRACK",
441 "Resolve alias for TRACK" },
442 { "resume", 0, 0, cf_resume, 0, "",
443 "Resume after a pause" },
444 { "scratch", 0, 0, cf_scratch, 0, "",
445 "Scratch the currently playing track" },
446 { "scratch-id", 1, 1, cf_scratch, 0, "ID",
447 "Scratch the currently playing track" },
448 { "search", 1, 1, cf_search, 0, "WORDS",
449 "Display tracks matching all the words" },
450 { "set", 3, 3, cf_set, 0, "TRACK NAME VALUE",
451 "Set a preference value" },
452 { "set-global", 2, 2, cf_set_global, 0, "NAME VALUE",
453 "Set a global preference value" },
454 { "set-volume", 2, 2, cf_set_volume, 0, "LEFT RIGHT",
456 { "shutdown", 0, 0, cf_shutdown, 0, "",
457 "Shut down the daemon" },
458 { "stats", 0, 0, cf_stats, 0, "",
459 "Display server statistics" },
460 { "tags", 0, 0, cf_tags, 0, "",
462 { "unset", 2, 2, cf_unset, 0, "TRACK NAME",
463 "Unset a preference" },
464 { "unset-global", 1, 1, cf_unset_global, 0, "NAME",
465 "Unset a global preference" },
466 { "version", 0, 0, cf_version, 0, "",
467 "Display the server version" },
470 static void help_commands(void) {
471 unsigned n, max = 0, l;
473 xprintf("Command summary:\n");
474 for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
475 if(!commands[n].desc) continue;
476 l = strlen(commands[n].name);
477 if(*commands[n].argstr)
478 l += strlen(commands[n].argstr) + 1;
482 for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
483 if(!commands[n].desc) continue;
484 l = strlen(commands[n].name);
485 if(*commands[n].argstr)
486 l += strlen(commands[n].argstr) + 1;
487 xprintf(" %s%s%s%*s %s\n", commands[n].name,
488 *commands[n].argstr ? " " : "",
497 int main(int argc, char **argv) {
499 disorder_client *c = 0;
505 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
506 while((n = getopt_long(argc, argv, "hVc:dHL", options, 0)) >= 0) {
509 case 'H': help_commands();
511 case 'c': configfile = optarg; break;
512 case 'd': debugging = 1; break;
513 default: fatal(0, "invalid option");
516 if(config_read()) fatal(0, "cannot read configuration");
517 if(!(c = disorder_new(1))) exit(EXIT_FAILURE);
518 s = config_get_file("socket");
519 if(disorder_connect(c)) exit(EXIT_FAILURE);
521 /* accumulate command args */
523 if((i = TABLE_FIND(commands, struct command, name, argv[n])) < 0)
524 fatal(0, "unknown command '%s'", argv[n]);
525 if(n + commands[i].min >= argc)
526 fatal(0, "missing arguments to '%s'", argv[n]);
529 for(j = 0; j < commands[i].min; ++j)
530 vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
531 for(; j < commands[i].max
533 && commands[i].isarg(argv[n + j]); ++j)
534 vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
535 vector_terminate(&args);
536 commands[i].fn(c, args.vec);
539 if(auto_reconfigure) {
541 if(disorder_reconfigure(c)) exit(EXIT_FAILURE);
543 if(c && disorder_close(c)) exit(EXIT_FAILURE);
544 if(fclose(stdout) < 0) fatal(errno, "error closing stdout");
554 /* arch-tag:0ff200f4c42e9b04dd781fa89c6129f6 */