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 { "no-per-user-config", no_argument
, 0, 'N' },
66 { "help-commands", no_argument
, 0, 'H' },
70 /* display usage message and terminate */
71 static void help(void) {
73 " disorder [OPTIONS] COMMAND ...\n"
75 " --help, -h Display usage message\n"
76 " --help-commands, -H List commands\n"
77 " --version, -V Display version number\n"
78 " --config PATH, -c PATH Set configuration file\n"
79 " --local, -l Force connection to local server\n"
80 " --debug, -d Turn on debugging\n");
85 /* display version number and terminate */
86 static void version(void) {
87 xprintf("disorder version %s\n", disorder_version_string
);
92 static void cf_version(disorder_client
*c
,
93 char attribute((unused
)) **argv
) {
96 if(disorder_version(c
, &v
)) exit(EXIT_FAILURE
);
97 xprintf("%s\n", nullcheck(utf82mb(v
)));
100 static void print_queue_entry(const struct queue_entry
*q
) {
101 if(q
->track
) xprintf("track %s\n", nullcheck(utf82mb(q
->track
)));
102 if(q
->id
) xprintf(" id %s\n", nullcheck(utf82mb(q
->id
)));
103 if(q
->submitter
) xprintf(" submitted by %s at %s",
104 nullcheck(utf82mb(q
->submitter
)), ctime(&q
->when
));
105 if(q
->played
) xprintf(" played at %s", ctime(&q
->played
));
106 if(q
->state
== playing_started
107 || q
->state
== playing_paused
) xprintf(" %lds so far", q
->sofar
);
108 else if(q
->expected
) xprintf(" might start at %s", ctime(&q
->expected
));
109 if(q
->scratched
) xprintf(" scratched by %s\n",
110 nullcheck(utf82mb(q
->scratched
)));
111 else xprintf(" %s\n", playing_states
[q
->state
]);
112 if(q
->wstat
) xprintf(" %s\n", wstat(q
->wstat
));
115 static void cf_playing(disorder_client
*c
,
116 char attribute((unused
)) **argv
) {
117 struct queue_entry
*q
;
119 if(disorder_playing(c
, &q
)) exit(EXIT_FAILURE
);
121 print_queue_entry(q
);
123 xprintf("nothing\n");
126 static void cf_play(disorder_client
*c
, char **argv
) {
128 if(disorder_play(c
, *argv
++)) exit(EXIT_FAILURE
);
131 static void cf_remove(disorder_client
*c
, char **argv
) {
132 if(disorder_remove(c
, argv
[0])) exit(EXIT_FAILURE
);
135 static void cf_disable(disorder_client
*c
,
136 char attribute((unused
)) **argv
) {
137 if(disorder_disable(c
)) exit(EXIT_FAILURE
);
140 static void cf_enable(disorder_client
*c
, char attribute((unused
)) **argv
) {
141 if(disorder_enable(c
)) exit(EXIT_FAILURE
);
144 static void cf_scratch(disorder_client
*c
,
146 if(disorder_scratch(c
, argv
[0])) exit(EXIT_FAILURE
);
149 static void cf_shutdown(disorder_client
*c
,
150 char attribute((unused
)) **argv
) {
151 if(disorder_shutdown(c
)) exit(EXIT_FAILURE
);
154 static void cf_reconfigure(disorder_client
*c
,
155 char attribute((unused
)) **argv
) {
156 /* Re-check configuration for server */
157 if(config_read(1)) fatal(0, "cannot read configuration");
158 if(disorder_reconfigure(c
)) exit(EXIT_FAILURE
);
161 static void cf_rescan(disorder_client
*c
, char attribute((unused
)) **argv
) {
162 if(disorder_rescan(c
)) exit(EXIT_FAILURE
);
165 static void cf_somequeue(disorder_client
*c
,
166 int (*fn
)(disorder_client
*c
,
167 struct queue_entry
**qp
)) {
168 struct queue_entry
*q
;
170 if(fn(c
, &q
)) exit(EXIT_FAILURE
);
172 print_queue_entry(q
);
177 static void cf_recent(disorder_client
*c
, char attribute((unused
)) **argv
) {
178 cf_somequeue(c
, disorder_recent
);
181 static void cf_queue(disorder_client
*c
, char attribute((unused
)) **argv
) {
182 cf_somequeue(c
, disorder_queue
);
185 static void cf_quack(disorder_client
attribute((unused
)) *c
,
186 char attribute((unused
)) **argv
) {
188 " .------------------.\n"
189 " | Naath is a babe! |\n"
190 " `---------+--------'\n"
194 "~~~~~~~~~~~~~~~~~~~~~~\n"
198 static void cf_somelist(disorder_client
*c
, char **argv
,
199 int (*fn
)(disorder_client
*c
,
200 const char *arg
, const char *re
,
201 char ***vecp
, int *nvecp
)) {
206 re
= xstrdup(argv
[1] + 1);
209 if(fn(c
, argv
[0], re
, &vec
, 0)) exit(EXIT_FAILURE
);
211 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
214 static int isarg_regexp(const char *s
) {
218 static void cf_dirs(disorder_client
*c
,
220 cf_somelist(c
, argv
, disorder_directories
);
223 static void cf_files(disorder_client
*c
, char **argv
) {
224 cf_somelist(c
, argv
, disorder_files
);
227 static void cf_allfiles(disorder_client
*c
, char **argv
) {
228 cf_somelist(c
, argv
, disorder_allfiles
);
231 static void cf_get(disorder_client
*c
, char **argv
) {
234 if(disorder_get(c
, argv
[0], argv
[1], &value
)) exit(EXIT_FAILURE
);
235 xprintf("%s\n", nullcheck(utf82mb(value
)));
238 static void cf_length(disorder_client
*c
, char **argv
) {
241 if(disorder_length(c
, argv
[0], &length
)) exit(EXIT_FAILURE
);
242 xprintf("%ld\n", length
);
245 static void cf_set(disorder_client
*c
, char **argv
) {
246 if(disorder_set(c
, argv
[0], argv
[1], argv
[2])) exit(EXIT_FAILURE
);
249 static void cf_unset(disorder_client
*c
, char **argv
) {
250 if(disorder_unset(c
, argv
[0], argv
[1])) exit(EXIT_FAILURE
);
253 static void cf_prefs(disorder_client
*c
, char **argv
) {
256 if(disorder_prefs(c
, argv
[0], &k
)) exit(EXIT_FAILURE
);
257 for(; k
; k
= k
->next
)
259 nullcheck(utf82mb(k
->name
)), nullcheck(utf82mb(k
->value
)));
262 static void cf_search(disorder_client
*c
, char **argv
) {
266 if(disorder_search(c
, *argv
, &results
, &nresults
)) exit(EXIT_FAILURE
);
267 for(n
= 0; n
< nresults
; ++n
)
268 xprintf("%s\n", nullcheck(utf82mb(results
[n
])));
271 static void cf_random_disable(disorder_client
*c
,
272 char attribute((unused
)) **argv
) {
273 if(disorder_random_disable(c
)) exit(EXIT_FAILURE
);
276 static void cf_random_enable(disorder_client
*c
,
277 char attribute((unused
)) **argv
) {
278 if(disorder_random_enable(c
)) exit(EXIT_FAILURE
);
281 static void cf_stats(disorder_client
*c
,
282 char attribute((unused
)) **argv
) {
285 if(disorder_stats(c
, &vec
, 0)) exit(EXIT_FAILURE
);
287 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
290 static void cf_get_volume(disorder_client
*c
,
291 char attribute((unused
)) **argv
) {
294 if(disorder_get_volume(c
, &l
, &r
)) exit(EXIT_FAILURE
);
295 xprintf("%d %d\n", l
, r
);
298 static void cf_set_volume(disorder_client
*c
,
300 if(disorder_set_volume(c
, atoi(argv
[0]), atoi(argv
[1]))) exit(EXIT_FAILURE
);
303 static void cf_become(disorder_client
*c
,
305 if(disorder_become(c
, argv
[0])) exit(EXIT_FAILURE
);
308 static void cf_log(disorder_client
*c
,
309 char attribute((unused
)) **argv
) {
310 if(disorder_log(c
, sink_stdio("stdout", stdout
))) exit(EXIT_FAILURE
);
313 static void cf_move(disorder_client
*c
,
318 if((e
= xstrtol(&n
, argv
[1], 0, 10)))
319 fatal(e
, "cannot convert '%s'", argv
[1]);
320 if(n
> INT_MAX
|| n
< INT_MIN
)
321 fatal(e
, "%ld out of range", n
);
322 if(disorder_move(c
, argv
[0], (int)n
)) exit(EXIT_FAILURE
);
325 static void cf_part(disorder_client
*c
,
329 if(disorder_part(c
, &s
, argv
[0], argv
[1], argv
[2])) exit(EXIT_FAILURE
);
330 xprintf("%s\n", nullcheck(utf82mb(s
)));
333 static int isarg_filename(const char *s
) {
337 static void cf_authorize(disorder_client
attribute((unused
)) *c
,
339 if(!authorize(argv
[0]))
340 auto_reconfigure
= 1;
343 static void cf_resolve(disorder_client
*c
,
347 if(disorder_resolve(c
, &track
, argv
[0])) exit(EXIT_FAILURE
);
348 xprintf("%s\n", nullcheck(utf82mb(track
)));
351 static void cf_pause(disorder_client
*c
,
352 char attribute((unused
)) **argv
) {
353 if(disorder_pause(c
)) exit(EXIT_FAILURE
);
356 static void cf_resume(disorder_client
*c
,
357 char attribute((unused
)) **argv
) {
358 if(disorder_resume(c
)) exit(EXIT_FAILURE
);
361 static void cf_tags(disorder_client
*c
,
362 char attribute((unused
)) **argv
) {
365 if(disorder_tags(c
, &vec
, 0)) exit(EXIT_FAILURE
);
367 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
370 static void cf_get_global(disorder_client
*c
, char **argv
) {
373 if(disorder_get_global(c
, argv
[0], &value
)) exit(EXIT_FAILURE
);
374 xprintf("%s\n", nullcheck(utf82mb(value
)));
377 static void cf_set_global(disorder_client
*c
, char **argv
) {
378 if(disorder_set_global(c
, argv
[0], argv
[1])) exit(EXIT_FAILURE
);
381 static void cf_unset_global(disorder_client
*c
, char **argv
) {
382 if(disorder_unset_global(c
, argv
[0])) exit(EXIT_FAILURE
);
385 static int isarg_integer(const char *s
) {
388 if(!isdigit((unsigned char)*s
))
395 static void cf_new(disorder_client
*c
,
399 if(disorder_new_tracks(c
, &vec
, 0, argv
[0] ?
atoi(argv
[0]) : 0))
402 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
405 static void cf_rtp_address(disorder_client
*c
,
406 char attribute((unused
)) **argv
) {
407 char *address
, *port
;
409 if(disorder_rtp_address(c
, &address
, &port
)) exit(EXIT_FAILURE
);
410 xprintf("address: %s\nport: %s\n", address
, port
);
413 static const struct command
{
416 void (*fn
)(disorder_client
*c
, char **);
417 int (*isarg
)(const char *);
418 const char *argstr
, *desc
;
420 { "allfiles", 1, 2, cf_allfiles
, isarg_regexp
, "DIR [~REGEXP]",
421 "List all files and directories in DIR" },
422 { "authorize", 1, 1, cf_authorize
, 0, "USER",
423 "Authorize USER to connect to the server" },
424 { "become", 1, 1, cf_become
, 0, "USER",
425 "Become user USER" },
426 { "dirs", 1, 2, cf_dirs
, isarg_regexp
, "DIR [~REGEXP]",
427 "List directories in DIR" },
428 { "disable", 0, 0, cf_disable
, 0, "",
430 { "disable-random", 0, 0, cf_random_disable
, 0, "",
431 "Disable random play" },
432 { "enable", 0, 0, cf_enable
, 0, "",
434 { "enable-random", 0, 0, cf_random_enable
, 0, "",
435 "Enable random play" },
436 { "files", 1, 2, cf_files
, isarg_regexp
, "DIR [~REGEXP]",
437 "List files in DIR" },
438 { "get", 2, 2, cf_get
, 0, "TRACK NAME",
439 "Get a preference value" },
440 { "get-global", 1, 1, cf_get_global
, 0, "NAME",
441 "Get a global preference value" },
442 { "get-volume", 0, 0, cf_get_volume
, 0, "",
443 "Get the current volume" },
444 { "length", 1, 1, cf_length
, 0, "TRACK",
445 "Get the length of TRACK in seconds" },
446 { "log", 0, 0, cf_log
, 0, "",
447 "Copy event log to stdout" },
448 { "move", 2, 2, cf_move
, 0, "TRACK DELTA",
449 "Move a track in the queue" },
450 { "new", 0, 1, cf_new
, isarg_integer
, "[MAX]",
451 "Get the most recently added MAX tracks" },
452 { "part", 3, 3, cf_part
, 0, "TRACK CONTEXT PART",
453 "Find a track name part" },
454 { "pause", 0, 0, cf_pause
, 0, "",
455 "Pause the currently playing track" },
456 { "play", 1, INT_MAX
, cf_play
, isarg_filename
, "TRACKS...",
457 "Add TRACKS to the end of the queue" },
458 { "playing", 0, 0, cf_playing
, 0, "",
459 "Report the playing track" },
460 { "prefs", 1, 1, cf_prefs
, 0, "TRACK",
461 "Display all the preferences for TRACK" },
462 { "quack", 0, 0, cf_quack
, 0, 0, 0 },
463 { "queue", 0, 0, cf_queue
, 0, "",
464 "Display the current queue" },
465 { "random-disable", 0, 0, cf_random_disable
, 0, "",
466 "Disable random play" },
467 { "random-enable", 0, 0, cf_random_enable
, 0, "",
468 "Enable random play" },
469 { "recent", 0, 0, cf_recent
, 0, "",
470 "Display recently played track" },
471 { "reconfigure", 0, 0, cf_reconfigure
, 0, "",
472 "Reconfigure the daemon" },
473 { "remove", 1, 1, cf_remove
, 0, "TRACK",
474 "Remove a track from the queue" },
475 { "rescan", 0, 0, cf_rescan
, 0, "",
476 "Rescan for new tracks" },
477 { "resolve", 1, 1, cf_resolve
, 0, "TRACK",
478 "Resolve alias for TRACK" },
479 { "resume", 0, 0, cf_resume
, 0, "",
480 "Resume after a pause" },
481 { "rtp-address", 0, 0, cf_rtp_address
, 0, "",
482 "Report server's broadcast address" },
483 { "scratch", 0, 0, cf_scratch
, 0, "",
484 "Scratch the currently playing track" },
485 { "scratch-id", 1, 1, cf_scratch
, 0, "ID",
486 "Scratch the currently playing track" },
487 { "search", 1, 1, cf_search
, 0, "WORDS",
488 "Display tracks matching all the words" },
489 { "set", 3, 3, cf_set
, 0, "TRACK NAME VALUE",
490 "Set a preference value" },
491 { "set-global", 2, 2, cf_set_global
, 0, "NAME VALUE",
492 "Set a global preference value" },
493 { "set-volume", 2, 2, cf_set_volume
, 0, "LEFT RIGHT",
495 { "shutdown", 0, 0, cf_shutdown
, 0, "",
496 "Shut down the daemon" },
497 { "stats", 0, 0, cf_stats
, 0, "",
498 "Display server statistics" },
499 { "tags", 0, 0, cf_tags
, 0, "",
501 { "unset", 2, 2, cf_unset
, 0, "TRACK NAME",
502 "Unset a preference" },
503 { "unset-global", 1, 1, cf_unset_global
, 0, "NAME",
504 "Unset a global preference" },
505 { "version", 0, 0, cf_version
, 0, "",
506 "Display the server version" },
509 static void help_commands(void) {
510 unsigned n
, max
= 0, l
;
512 xprintf("Command summary:\n");
513 for(n
= 0; n
< sizeof commands
/ sizeof *commands
; ++n
) {
514 if(!commands
[n
].desc
) continue;
515 l
= strlen(commands
[n
].name
);
516 if(*commands
[n
].argstr
)
517 l
+= strlen(commands
[n
].argstr
) + 1;
521 for(n
= 0; n
< sizeof commands
/ sizeof *commands
; ++n
) {
522 if(!commands
[n
].desc
) continue;
523 l
= strlen(commands
[n
].name
);
524 if(*commands
[n
].argstr
)
525 l
+= strlen(commands
[n
].argstr
) + 1;
526 xprintf(" %s%s%s%*s %s\n", commands
[n
].name
,
527 *commands
[n
].argstr ?
" " : "",
536 int main(int argc
, char **argv
) {
537 int n
, i
, j
, local
= 0;
538 disorder_client
*c
= 0;
543 /* garbage-collect PCRE's memory */
544 pcre_malloc
= xmalloc
;
546 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
547 while((n
= getopt_long(argc
, argv
, "hVc:dHlN", options
, 0)) >= 0) {
550 case 'H': help_commands();
552 case 'c': configfile
= optarg
; break;
553 case 'd': debugging
= 1; break;
554 case 'l': local
= 1; break;
555 case 'N': config_per_user
= 0; break;
556 default: fatal(0, "invalid option");
559 if(config_read(0)) fatal(0, "cannot read configuration");
561 config
->connect
.n
= 0;
562 if(!(c
= disorder_new(1))) exit(EXIT_FAILURE
);
563 if(disorder_connect(c
)) exit(EXIT_FAILURE
);
565 /* accumulate command args */
567 if((i
= TABLE_FIND(commands
, struct command
, name
, argv
[n
])) < 0)
568 fatal(0, "unknown command '%s'", argv
[n
]);
569 if(n
+ commands
[i
].min
>= argc
)
570 fatal(0, "missing arguments to '%s'", argv
[n
]);
573 for(j
= 0; j
< commands
[i
].min
; ++j
)
574 vector_append(&args
, nullcheck(mb2utf8(argv
[n
+ j
])));
575 for(; j
< commands
[i
].max
577 && commands
[i
].isarg(argv
[n
+ j
]); ++j
)
578 vector_append(&args
, nullcheck(mb2utf8(argv
[n
+ j
])));
579 vector_terminate(&args
);
580 commands
[i
].fn(c
, args
.vec
);
583 if(auto_reconfigure
) {
585 if(disorder_reconfigure(c
)) exit(EXIT_FAILURE
);
587 if(c
&& disorder_close(c
)) exit(EXIT_FAILURE
);
588 if(fclose(stdout
) < 0) fatal(errno
, "error closing stdout");