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 /* Re-check configuration for server */
153 if(config_read(1)) fatal(0, "cannot read configuration");
154 if(disorder_reconfigure(c
)) exit(EXIT_FAILURE
);
157 static void cf_rescan(disorder_client
*c
, char attribute((unused
)) **argv
) {
158 if(disorder_rescan(c
)) exit(EXIT_FAILURE
);
161 static void cf_somequeue(disorder_client
*c
,
162 int (*fn
)(disorder_client
*c
,
163 struct queue_entry
**qp
)) {
164 struct queue_entry
*q
;
166 if(fn(c
, &q
)) exit(EXIT_FAILURE
);
168 print_queue_entry(q
);
173 static void cf_recent(disorder_client
*c
, char attribute((unused
)) **argv
) {
174 cf_somequeue(c
, disorder_recent
);
177 static void cf_queue(disorder_client
*c
, char attribute((unused
)) **argv
) {
178 cf_somequeue(c
, disorder_queue
);
181 static void cf_quack(disorder_client
attribute((unused
)) *c
,
182 char attribute((unused
)) **argv
) {
184 " .------------------.\n"
185 " | Naath is a babe! |\n"
186 " `---------+--------'\n"
190 "~~~~~~~~~~~~~~~~~~~~~~\n"
194 static void cf_somelist(disorder_client
*c
, char **argv
,
195 int (*fn
)(disorder_client
*c
,
196 const char *arg
, const char *re
,
197 char ***vecp
, int *nvecp
)) {
202 re
= xstrdup(argv
[1] + 1);
205 if(fn(c
, argv
[0], re
, &vec
, 0)) exit(EXIT_FAILURE
);
207 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
210 static int isarg_regexp(const char *s
) {
214 static void cf_dirs(disorder_client
*c
,
216 cf_somelist(c
, argv
, disorder_directories
);
219 static void cf_files(disorder_client
*c
, char **argv
) {
220 cf_somelist(c
, argv
, disorder_files
);
223 static void cf_allfiles(disorder_client
*c
, char **argv
) {
224 cf_somelist(c
, argv
, disorder_allfiles
);
227 static void cf_get(disorder_client
*c
, char **argv
) {
230 if(disorder_get(c
, argv
[0], argv
[1], &value
)) exit(EXIT_FAILURE
);
231 xprintf("%s\n", nullcheck(utf82mb(value
)));
234 static void cf_length(disorder_client
*c
, char **argv
) {
237 if(disorder_length(c
, argv
[0], &length
)) exit(EXIT_FAILURE
);
238 xprintf("%ld\n", length
);
241 static void cf_set(disorder_client
*c
, char **argv
) {
242 if(disorder_set(c
, argv
[0], argv
[1], argv
[2])) exit(EXIT_FAILURE
);
245 static void cf_unset(disorder_client
*c
, char **argv
) {
246 if(disorder_unset(c
, argv
[0], argv
[1])) exit(EXIT_FAILURE
);
249 static void cf_prefs(disorder_client
*c
, char **argv
) {
252 if(disorder_prefs(c
, argv
[0], &k
)) exit(EXIT_FAILURE
);
253 for(; k
; k
= k
->next
)
255 nullcheck(utf82mb(k
->name
)), nullcheck(utf82mb(k
->value
)));
258 static void cf_search(disorder_client
*c
, char **argv
) {
262 if(disorder_search(c
, *argv
, &results
, &nresults
)) exit(EXIT_FAILURE
);
263 for(n
= 0; n
< nresults
; ++n
)
264 xprintf("%s\n", nullcheck(utf82mb(results
[n
])));
267 static void cf_random_disable(disorder_client
*c
,
268 char attribute((unused
)) **argv
) {
269 if(disorder_random_disable(c
)) exit(EXIT_FAILURE
);
272 static void cf_random_enable(disorder_client
*c
,
273 char attribute((unused
)) **argv
) {
274 if(disorder_random_enable(c
)) exit(EXIT_FAILURE
);
277 static void cf_stats(disorder_client
*c
,
278 char attribute((unused
)) **argv
) {
281 if(disorder_stats(c
, &vec
, 0)) exit(EXIT_FAILURE
);
283 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
286 static void cf_get_volume(disorder_client
*c
,
287 char attribute((unused
)) **argv
) {
290 if(disorder_get_volume(c
, &l
, &r
)) exit(EXIT_FAILURE
);
291 xprintf("%d %d\n", l
, r
);
294 static void cf_set_volume(disorder_client
*c
,
296 if(disorder_set_volume(c
, atoi(argv
[0]), atoi(argv
[1]))) exit(EXIT_FAILURE
);
299 static void cf_become(disorder_client
*c
,
301 if(disorder_become(c
, argv
[0])) exit(EXIT_FAILURE
);
304 static void cf_log(disorder_client
*c
,
305 char attribute((unused
)) **argv
) {
306 if(disorder_log(c
, sink_stdio("stdout", stdout
))) exit(EXIT_FAILURE
);
309 static void cf_move(disorder_client
*c
,
314 if((e
= xstrtol(&n
, argv
[1], 0, 10)))
315 fatal(e
, "cannot convert '%s'", argv
[1]);
316 if(n
> INT_MAX
|| n
< INT_MIN
)
317 fatal(e
, "%ld out of range", n
);
318 if(disorder_move(c
, argv
[0], (int)n
)) exit(EXIT_FAILURE
);
321 static void cf_part(disorder_client
*c
,
325 if(disorder_part(c
, &s
, argv
[0], argv
[1], argv
[2])) exit(EXIT_FAILURE
);
326 xprintf("%s\n", nullcheck(utf82mb(s
)));
329 static int isarg_filename(const char *s
) {
333 static void cf_authorize(disorder_client
attribute((unused
)) *c
,
335 if(!authorize(argv
[0]))
336 auto_reconfigure
= 1;
339 static void cf_resolve(disorder_client
*c
,
343 if(disorder_resolve(c
, &track
, argv
[0])) exit(EXIT_FAILURE
);
344 xprintf("%s\n", nullcheck(utf82mb(track
)));
347 static void cf_pause(disorder_client
*c
,
348 char attribute((unused
)) **argv
) {
349 if(disorder_pause(c
)) exit(EXIT_FAILURE
);
352 static void cf_resume(disorder_client
*c
,
353 char attribute((unused
)) **argv
) {
354 if(disorder_resume(c
)) exit(EXIT_FAILURE
);
357 static void cf_tags(disorder_client
*c
,
358 char attribute((unused
)) **argv
) {
361 if(disorder_tags(c
, &vec
, 0)) exit(EXIT_FAILURE
);
363 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
366 static void cf_get_global(disorder_client
*c
, char **argv
) {
369 if(disorder_get_global(c
, argv
[0], &value
)) exit(EXIT_FAILURE
);
370 xprintf("%s\n", nullcheck(utf82mb(value
)));
373 static void cf_set_global(disorder_client
*c
, char **argv
) {
374 if(disorder_set_global(c
, argv
[0], argv
[1])) exit(EXIT_FAILURE
);
377 static void cf_unset_global(disorder_client
*c
, char **argv
) {
378 if(disorder_unset_global(c
, argv
[0])) exit(EXIT_FAILURE
);
381 static const struct command
{
384 void (*fn
)(disorder_client
*c
, char **);
385 int (*isarg
)(const char *);
386 const char *argstr
, *desc
;
388 { "allfiles", 1, 2, cf_allfiles
, isarg_regexp
, "DIR [~REGEXP]",
389 "List all files and directories in DIR" },
390 { "authorize", 1, 1, cf_authorize
, 0, "USER",
391 "Authorize USER to connect to the server" },
392 { "become", 1, 1, cf_become
, 0, "USER",
393 "Become user USER" },
394 { "dirs", 1, 2, cf_dirs
, isarg_regexp
, "DIR [~REGEXP]",
395 "List directories in DIR" },
396 { "disable", 0, 0, cf_disable
, 0, "",
398 { "disable-random", 0, 0, cf_random_disable
, 0, "",
399 "Disable random play" },
400 { "enable", 0, 0, cf_enable
, 0, "",
402 { "enable-random", 0, 0, cf_random_enable
, 0, "",
403 "Enable random play" },
404 { "files", 1, 2, cf_files
, isarg_regexp
, "DIR [~REGEXP]",
405 "List files in DIR" },
406 { "get", 2, 2, cf_get
, 0, "TRACK NAME",
407 "Get a preference value" },
408 { "get-global", 1, 1, cf_get_global
, 0, "NAME",
409 "Get a global preference value" },
410 { "get-volume", 0, 0, cf_get_volume
, 0, "",
411 "Get the current volume" },
412 { "length", 1, 1, cf_length
, 0, "TRACK",
413 "Get the length of TRACK in seconds" },
414 { "log", 0, 0, cf_log
, 0, "",
415 "Copy event log to stdout" },
416 { "move", 2, 2, cf_move
, 0, "TRACK DELTA",
417 "Move a track in the queue" },
418 { "part", 3, 3, cf_part
, 0, "TRACK CONTEXT PART",
419 "Find a track name part" },
420 { "pause", 0, 0, cf_pause
, 0, "",
421 "Pause the currently playing track" },
422 { "play", 1, INT_MAX
, cf_play
, isarg_filename
, "TRACKS...",
423 "Add TRACKS to the end of the queue" },
424 { "playing", 0, 0, cf_playing
, 0, "",
425 "Report the playing track" },
426 { "prefs", 1, 1, cf_prefs
, 0, "TRACK",
427 "Display all the preferences for TRACK" },
428 { "quack", 0, 0, cf_quack
, 0, 0, 0 },
429 { "queue", 0, 0, cf_queue
, 0, "",
430 "Display the current queue" },
431 { "random-disable", 0, 0, cf_random_disable
, 0, "",
432 "Disable random play" },
433 { "random-enable", 0, 0, cf_random_enable
, 0, "",
434 "Enable random play" },
435 { "recent", 0, 0, cf_recent
, 0, "",
436 "Display recently played track" },
437 { "reconfigure", 0, 0, cf_reconfigure
, 0, "",
438 "Reconfigure the daemon" },
439 { "remove", 1, 1, cf_remove
, 0, "TRACK",
440 "Remove a track from the queue" },
441 { "rescan", 0, 0, cf_rescan
, 0, "",
442 "Rescan for new tracks" },
443 { "resolve", 1, 1, cf_resolve
, 0, "TRACK",
444 "Resolve alias for TRACK" },
445 { "resume", 0, 0, cf_resume
, 0, "",
446 "Resume after a pause" },
447 { "scratch", 0, 0, cf_scratch
, 0, "",
448 "Scratch the currently playing track" },
449 { "scratch-id", 1, 1, cf_scratch
, 0, "ID",
450 "Scratch the currently playing track" },
451 { "search", 1, 1, cf_search
, 0, "WORDS",
452 "Display tracks matching all the words" },
453 { "set", 3, 3, cf_set
, 0, "TRACK NAME VALUE",
454 "Set a preference value" },
455 { "set-global", 2, 2, cf_set_global
, 0, "NAME VALUE",
456 "Set a global preference value" },
457 { "set-volume", 2, 2, cf_set_volume
, 0, "LEFT RIGHT",
459 { "shutdown", 0, 0, cf_shutdown
, 0, "",
460 "Shut down the daemon" },
461 { "stats", 0, 0, cf_stats
, 0, "",
462 "Display server statistics" },
463 { "tags", 0, 0, cf_tags
, 0, "",
465 { "unset", 2, 2, cf_unset
, 0, "TRACK NAME",
466 "Unset a preference" },
467 { "unset-global", 1, 1, cf_unset_global
, 0, "NAME",
468 "Unset a global preference" },
469 { "version", 0, 0, cf_version
, 0, "",
470 "Display the server version" },
473 static void help_commands(void) {
474 unsigned n
, max
= 0, l
;
476 xprintf("Command summary:\n");
477 for(n
= 0; n
< sizeof commands
/ sizeof *commands
; ++n
) {
478 if(!commands
[n
].desc
) continue;
479 l
= strlen(commands
[n
].name
);
480 if(*commands
[n
].argstr
)
481 l
+= strlen(commands
[n
].argstr
) + 1;
485 for(n
= 0; n
< sizeof commands
/ sizeof *commands
; ++n
) {
486 if(!commands
[n
].desc
) continue;
487 l
= strlen(commands
[n
].name
);
488 if(*commands
[n
].argstr
)
489 l
+= strlen(commands
[n
].argstr
) + 1;
490 xprintf(" %s%s%s%*s %s\n", commands
[n
].name
,
491 *commands
[n
].argstr ?
" " : "",
500 int main(int argc
, char **argv
) {
502 disorder_client
*c
= 0;
508 /* garbage-collect PCRE's memory */
509 pcre_malloc
= xmalloc
;
511 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
512 while((n
= getopt_long(argc
, argv
, "hVc:dHL", options
, 0)) >= 0) {
515 case 'H': help_commands();
517 case 'c': configfile
= optarg
; break;
518 case 'd': debugging
= 1; break;
519 default: fatal(0, "invalid option");
522 if(config_read(0)) fatal(0, "cannot read configuration");
523 if(!(c
= disorder_new(1))) exit(EXIT_FAILURE
);
524 s
= config_get_file("socket");
525 if(disorder_connect(c
)) exit(EXIT_FAILURE
);
527 /* accumulate command args */
529 if((i
= TABLE_FIND(commands
, struct command
, name
, argv
[n
])) < 0)
530 fatal(0, "unknown command '%s'", argv
[n
]);
531 if(n
+ commands
[i
].min
>= argc
)
532 fatal(0, "missing arguments to '%s'", argv
[n
]);
535 for(j
= 0; j
< commands
[i
].min
; ++j
)
536 vector_append(&args
, nullcheck(mb2utf8(argv
[n
+ j
])));
537 for(; j
< commands
[i
].max
539 && commands
[i
].isarg(argv
[n
+ j
]); ++j
)
540 vector_append(&args
, nullcheck(mb2utf8(argv
[n
+ j
])));
541 vector_terminate(&args
);
542 commands
[i
].fn(c
, args
.vec
);
545 if(auto_reconfigure
) {
547 if(disorder_reconfigure(c
)) exit(EXIT_FAILURE
);
549 if(c
&& disorder_close(c
)) exit(EXIT_FAILURE
);
550 if(fclose(stdout
) < 0) fatal(errno
, "error closing stdout");