chiark / gitweb /
a little more testing
[disorder] / clients / disorder.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
20
21#include <config.h>
22#include "types.h"
23
24#include <getopt.h>
25#include <sys/types.h>
26#include <sys/socket.h>
27#include <sys/un.h>
28#include <stdio.h>
29#include <errno.h>
30#include <stdlib.h>
31#include <string.h>
32#include <locale.h>
33#include <time.h>
34#include <stddef.h>
35#include <unistd.h>
36#include <assert.h>
37#include <pcre.h>
38#include <ctype.h>
39
40#include "configuration.h"
41#include "syscalls.h"
42#include "log.h"
43#include "queue.h"
44#include "client.h"
45#include "wstat.h"
46#include "table.h"
47#include "charset.h"
48#include "kvp.h"
49#include "split.h"
50#include "sink.h"
51#include "plugin.h"
52#include "mem.h"
53#include "defs.h"
54#include "authorize.h"
55#include "vector.h"
56
57static int auto_reconfigure;
58
59static 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' },
67 { 0, 0, 0, 0 }
68};
69
70/* display usage message and terminate */
71static void help(void) {
72 xprintf("Usage:\n"
73 " disorder [OPTIONS] COMMAND ...\n"
74 "Options:\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");
81 xfclose(stdout);
82 exit(0);
83}
84
85/* display version number and terminate */
86static void version(void) {
87 xprintf("disorder version %s\n", disorder_version_string);
88 xfclose(stdout);
89 exit(0);
90}
91
92static void cf_version(disorder_client *c,
93 char attribute((unused)) **argv) {
94 char *v;
95
96 if(disorder_version(c, &v)) exit(EXIT_FAILURE);
97 xprintf("%s\n", nullcheck(utf82mb(v)));
98}
99
100static 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));
113}
114
115static void cf_playing(disorder_client *c,
116 char attribute((unused)) **argv) {
117 struct queue_entry *q;
118
119 if(disorder_playing(c, &q)) exit(EXIT_FAILURE);
120 if(q)
121 print_queue_entry(q);
122 else
123 xprintf("nothing\n");
124}
125
126static void cf_play(disorder_client *c, char **argv) {
127 while(*argv)
128 if(disorder_play(c, *argv++)) exit(EXIT_FAILURE);
129}
130
131static void cf_remove(disorder_client *c, char **argv) {
132 if(disorder_remove(c, argv[0])) exit(EXIT_FAILURE);
133}
134
135static void cf_disable(disorder_client *c,
136 char attribute((unused)) **argv) {
137 if(disorder_disable(c)) exit(EXIT_FAILURE);
138}
139
140static void cf_enable(disorder_client *c, char attribute((unused)) **argv) {
141 if(disorder_enable(c)) exit(EXIT_FAILURE);
142}
143
144static void cf_scratch(disorder_client *c,
145 char **argv) {
146 if(disorder_scratch(c, argv[0])) exit(EXIT_FAILURE);
147}
148
149static void cf_shutdown(disorder_client *c,
150 char attribute((unused)) **argv) {
151 if(disorder_shutdown(c)) exit(EXIT_FAILURE);
152}
153
154static 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);
159}
160
161static void cf_rescan(disorder_client *c, char attribute((unused)) **argv) {
162 if(disorder_rescan(c)) exit(EXIT_FAILURE);
163}
164
165static void cf_somequeue(disorder_client *c,
166 int (*fn)(disorder_client *c,
167 struct queue_entry **qp)) {
168 struct queue_entry *q;
169
170 if(fn(c, &q)) exit(EXIT_FAILURE);
171 while(q) {
172 print_queue_entry(q);
173 q = q->next;
174 }
175}
176
177static void cf_recent(disorder_client *c, char attribute((unused)) **argv) {
178 cf_somequeue(c, disorder_recent);
179}
180
181static void cf_queue(disorder_client *c, char attribute((unused)) **argv) {
182 cf_somequeue(c, disorder_queue);
183}
184
185static void cf_quack(disorder_client attribute((unused)) *c,
186 char attribute((unused)) **argv) {
187 xprintf("\n"
188 " .------------------.\n"
189 " | Naath is a babe! |\n"
190 " `---------+--------'\n"
191 " \\\n"
192 " >0\n"
193 " (<)'\n"
194 "~~~~~~~~~~~~~~~~~~~~~~\n"
195 "\n");
196}
197
198static 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)) {
202 char **vec;
203 const char *re;
204
205 if(argv[1])
206 re = xstrdup(argv[1] + 1);
207 else
208 re = 0;
209 if(fn(c, argv[0], re, &vec, 0)) exit(EXIT_FAILURE);
210 while(*vec)
211 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
212}
213
214static int isarg_regexp(const char *s) {
215 return s[0] == '~';
216}
217
218static void cf_dirs(disorder_client *c,
219 char **argv) {
220 cf_somelist(c, argv, disorder_directories);
221}
222
223static void cf_files(disorder_client *c, char **argv) {
224 cf_somelist(c, argv, disorder_files);
225}
226
227static void cf_allfiles(disorder_client *c, char **argv) {
228 cf_somelist(c, argv, disorder_allfiles);
229}
230
231static void cf_get(disorder_client *c, char **argv) {
232 char *value;
233
234 if(disorder_get(c, argv[0], argv[1], &value)) exit(EXIT_FAILURE);
235 xprintf("%s\n", nullcheck(utf82mb(value)));
236}
237
238static void cf_length(disorder_client *c, char **argv) {
239 long length;
240
241 if(disorder_length(c, argv[0], &length)) exit(EXIT_FAILURE);
242 xprintf("%ld\n", length);
243}
244
245static void cf_set(disorder_client *c, char **argv) {
246 if(disorder_set(c, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
247}
248
249static void cf_unset(disorder_client *c, char **argv) {
250 if(disorder_unset(c, argv[0], argv[1])) exit(EXIT_FAILURE);
251}
252
253static void cf_prefs(disorder_client *c, char **argv) {
254 struct kvp *k;
255
256 if(disorder_prefs(c, argv[0], &k)) exit(EXIT_FAILURE);
257 for(; k; k = k->next)
258 xprintf("%s = %s\n",
259 nullcheck(utf82mb(k->name)), nullcheck(utf82mb(k->value)));
260}
261
262static void cf_search(disorder_client *c, char **argv) {
263 char **results;
264 int nresults, n;
265
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])));
269}
270
271static void cf_random_disable(disorder_client *c,
272 char attribute((unused)) **argv) {
273 if(disorder_random_disable(c)) exit(EXIT_FAILURE);
274}
275
276static void cf_random_enable(disorder_client *c,
277 char attribute((unused)) **argv) {
278 if(disorder_random_enable(c)) exit(EXIT_FAILURE);
279}
280
281static void cf_stats(disorder_client *c,
282 char attribute((unused)) **argv) {
283 char **vec;
284
285 if(disorder_stats(c, &vec, 0)) exit(EXIT_FAILURE);
286 while(*vec)
287 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
288}
289
290static void cf_get_volume(disorder_client *c,
291 char attribute((unused)) **argv) {
292 int l, r;
293
294 if(disorder_get_volume(c, &l, &r)) exit(EXIT_FAILURE);
295 xprintf("%d %d\n", l, r);
296}
297
298static void cf_set_volume(disorder_client *c,
299 char **argv) {
300 if(disorder_set_volume(c, atoi(argv[0]), atoi(argv[1]))) exit(EXIT_FAILURE);
301}
302
303static void cf_become(disorder_client *c,
304 char **argv) {
305 if(disorder_become(c, argv[0])) exit(EXIT_FAILURE);
306}
307
308static void cf_log(disorder_client *c,
309 char attribute((unused)) **argv) {
310 if(disorder_log(c, sink_stdio("stdout", stdout))) exit(EXIT_FAILURE);
311}
312
313static void cf_move(disorder_client *c,
314 char **argv) {
315 long n;
316 int e;
317
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);
323}
324
325static void cf_part(disorder_client *c,
326 char **argv) {
327 char *s;
328
329 if(disorder_part(c, &s, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
330 xprintf("%s\n", nullcheck(utf82mb(s)));
331}
332
333static int isarg_filename(const char *s) {
334 return s[0] == '/';
335}
336
337static void cf_authorize(disorder_client attribute((unused)) *c,
338 char **argv) {
339 if(!authorize(argv[0]))
340 auto_reconfigure = 1;
341}
342
343static void cf_resolve(disorder_client *c,
344 char **argv) {
345 char *track;
346
347 if(disorder_resolve(c, &track, argv[0])) exit(EXIT_FAILURE);
348 xprintf("%s\n", nullcheck(utf82mb(track)));
349}
350
351static void cf_pause(disorder_client *c,
352 char attribute((unused)) **argv) {
353 if(disorder_pause(c)) exit(EXIT_FAILURE);
354}
355
356static void cf_resume(disorder_client *c,
357 char attribute((unused)) **argv) {
358 if(disorder_resume(c)) exit(EXIT_FAILURE);
359}
360
361static void cf_tags(disorder_client *c,
362 char attribute((unused)) **argv) {
363 char **vec;
364
365 if(disorder_tags(c, &vec, 0)) exit(EXIT_FAILURE);
366 while(*vec)
367 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
368}
369
370static void cf_get_global(disorder_client *c, char **argv) {
371 char *value;
372
373 if(disorder_get_global(c, argv[0], &value)) exit(EXIT_FAILURE);
374 xprintf("%s\n", nullcheck(utf82mb(value)));
375}
376
377static void cf_set_global(disorder_client *c, char **argv) {
378 if(disorder_set_global(c, argv[0], argv[1])) exit(EXIT_FAILURE);
379}
380
381static void cf_unset_global(disorder_client *c, char **argv) {
382 if(disorder_unset_global(c, argv[0])) exit(EXIT_FAILURE);
383}
384
385static int isarg_integer(const char *s) {
386 if(!*s) return 0;
387 while(*s) {
388 if(!isdigit((unsigned char)*s))
389 return 0;
390 ++s;
391 }
392 return 1;
393}
394
395static void cf_new(disorder_client *c,
396 char **argv) {
397 char **vec;
398
399 if(disorder_new_tracks(c, &vec, 0, argv[0] ? atoi(argv[0]) : 0))
400 exit(EXIT_FAILURE);
401 while(*vec)
402 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
403}
404
405static void cf_rtp_address(disorder_client *c,
406 char attribute((unused)) **argv) {
407 char *address, *port;
408
409 if(disorder_rtp_address(c, &address, &port)) exit(EXIT_FAILURE);
410 xprintf("address: %s\nport: %s\n", address, port);
411}
412
413static const struct command {
414 const char *name;
415 int min, max;
416 void (*fn)(disorder_client *c, char **);
417 int (*isarg)(const char *);
418 const char *argstr, *desc;
419} commands[] = {
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, "",
429 "Disable play" },
430 { "disable-random", 0, 0, cf_random_disable, 0, "",
431 "Disable random play" },
432 { "enable", 0, 0, cf_enable, 0, "",
433 "Enable play" },
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",
494 "Set the volume" },
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, "",
500 "List known tags" },
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" },
507};
508
509static void help_commands(void) {
510 unsigned n, max = 0, l;
511
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;
518 if(l > max)
519 max = l;
520 }
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 ? " " : "",
528 commands[n].argstr,
529 max - l, "",
530 commands[n].desc);
531 }
532 xfclose(stdout);
533 exit(0);
534}
535
536int main(int argc, char **argv) {
537 int n, i, j, local = 0;
538 disorder_client *c = 0;
539 int status = 0;
540 struct vector args;
541
542 mem_init();
543 /* garbage-collect PCRE's memory */
544 pcre_malloc = xmalloc;
545 pcre_free = xfree;
546 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
547 while((n = getopt_long(argc, argv, "hVc:dHlN", options, 0)) >= 0) {
548 switch(n) {
549 case 'h': help();
550 case 'H': help_commands();
551 case 'V': version();
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");
557 }
558 }
559 if(config_read(0)) fatal(0, "cannot read configuration");
560 if(local)
561 config->connect.n = 0;
562 if(!(c = disorder_new(1))) exit(EXIT_FAILURE);
563 if(disorder_connect(c)) exit(EXIT_FAILURE);
564 n = optind;
565 /* accumulate command args */
566 while(n < argc) {
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]);
571 n++;
572 vector_init(&args);
573 for(j = 0; j < commands[i].min; ++j)
574 vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
575 for(; j < commands[i].max
576 && n + j < argc
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);
581 n += j;
582 }
583 if(auto_reconfigure) {
584 assert(c != 0);
585 if(disorder_reconfigure(c)) exit(EXIT_FAILURE);
586 }
587 if(c && disorder_close(c)) exit(EXIT_FAILURE);
588 if(fclose(stdout) < 0) fatal(errno, "error closing stdout");
589 return status;
590}
591
592/*
593Local Variables:
594c-basic-offset:2
595comment-column:40
596End:
597*/