chiark / gitweb /
playrtp now builds on macos. untested.
[disorder] / clients / disorder.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004, 2005, 2006 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
39 #include "configuration.h"
40 #include "syscalls.h"
41 #include "log.h"
42 #include "queue.h"
43 #include "client.h"
44 #include "wstat.h"
45 #include "table.h"
46 #include "charset.h"
47 #include "kvp.h"
48 #include "split.h"
49 #include "sink.h"
50 #include "plugin.h"
51 #include "mem.h"
52 #include "defs.h"
53 #include "authorize.h"
54 #include "vector.h"
55
56 static int auto_reconfigure;
57
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' },
64   { 0, 0, 0, 0 }
65 };
66
67 /* display usage message and terminate */
68 static void help(void) {
69   xprintf("Usage:\n"
70           "  disorder [OPTIONS] COMMAND ...\n"
71           "Options:\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");
77   xfclose(stdout);
78   exit(0);
79 }
80
81 /* display version number and terminate */
82 static void version(void) {
83   xprintf("disorder version %s\n", disorder_version_string);
84   xfclose(stdout);
85   exit(0);
86 }
87
88 static void cf_version(disorder_client *c,
89                        char attribute((unused)) **argv) {
90   char *v;
91
92   if(disorder_version(c, &v)) exit(EXIT_FAILURE);
93   xprintf("%s\n", nullcheck(utf82mb(v)));
94 }
95
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));
109 }
110
111 static void cf_playing(disorder_client *c,
112                        char attribute((unused)) **argv) {
113   struct queue_entry *q;
114
115   if(disorder_playing(c, &q)) exit(EXIT_FAILURE);
116   if(q)
117     print_queue_entry(q);
118   else
119     xprintf("nothing\n");
120 }
121
122 static void cf_play(disorder_client *c, char **argv) {
123   while(*argv)
124     if(disorder_play(c, *argv++)) exit(EXIT_FAILURE);
125 }
126
127 static void cf_remove(disorder_client *c, char **argv) {
128   if(disorder_remove(c, argv[0])) exit(EXIT_FAILURE);
129 }
130
131 static void cf_disable(disorder_client *c,
132                        char attribute((unused)) **argv) {
133   if(disorder_disable(c)) exit(EXIT_FAILURE);
134 }
135
136 static void cf_enable(disorder_client *c, char attribute((unused)) **argv) {
137   if(disorder_enable(c)) exit(EXIT_FAILURE);
138 }
139
140 static void cf_scratch(disorder_client *c,
141                        char **argv) {
142   if(disorder_scratch(c, argv[0])) exit(EXIT_FAILURE);
143 }
144
145 static void cf_shutdown(disorder_client *c,
146                         char attribute((unused)) **argv) {
147   if(disorder_shutdown(c)) exit(EXIT_FAILURE);
148 }
149
150 static void cf_reconfigure(disorder_client *c,
151                            char attribute((unused)) **argv) {
152   if(disorder_reconfigure(c)) exit(EXIT_FAILURE);
153 }
154
155 static void cf_rescan(disorder_client *c, char attribute((unused)) **argv) {
156   if(disorder_rescan(c)) exit(EXIT_FAILURE);
157 }
158
159 static void cf_somequeue(disorder_client *c,
160                          int (*fn)(disorder_client *c,
161                                    struct queue_entry **qp)) {
162   struct queue_entry *q;
163
164   if(fn(c, &q)) exit(EXIT_FAILURE);
165   while(q) {
166     print_queue_entry(q);
167     q = q->next;
168   }
169 }
170
171 static void cf_recent(disorder_client *c, char attribute((unused)) **argv) {
172   cf_somequeue(c, disorder_recent);
173 }
174
175 static void cf_queue(disorder_client *c, char attribute((unused)) **argv) {
176   cf_somequeue(c, disorder_queue);
177 }
178
179 static void cf_quack(disorder_client attribute((unused)) *c,
180                      char attribute((unused)) **argv) {
181   xprintf("\n"
182           " .------------------.\n"
183           " | Naath is a babe! |\n"
184           " `---------+--------'\n"
185           "            \\\n"
186           "              >0\n"
187           "               (<)'\n"
188           "~~~~~~~~~~~~~~~~~~~~~~\n"
189           "\n");
190 }
191
192 static void cf_somelist(disorder_client *c, char **argv,
193                         int (*fn)(disorder_client *c,
194                                   const char *arg, const char *re,
195                                   char ***vecp, int *nvecp)) {
196   char **vec;
197   const char *re;
198
199   if(argv[1])
200     re = xstrdup(argv[1] + 1);
201   else
202     re = 0;
203   if(fn(c, argv[0], re, &vec, 0)) exit(EXIT_FAILURE);
204   while(*vec)
205     xprintf("%s\n", nullcheck(utf82mb(*vec++)));
206 }
207
208 static int isarg_regexp(const char *s) {
209   return s[0] == '~';
210 }
211
212 static void cf_dirs(disorder_client *c,
213                     char **argv) {
214   cf_somelist(c, argv, disorder_directories);
215 }
216
217 static void cf_files(disorder_client *c, char **argv) {
218   cf_somelist(c, argv, disorder_files);
219 }
220
221 static void cf_allfiles(disorder_client *c, char **argv) {
222   cf_somelist(c, argv, disorder_allfiles);
223 }
224
225 static void cf_get(disorder_client *c, char **argv) {
226   char *value;
227
228   if(disorder_get(c, argv[0], argv[1], &value)) exit(EXIT_FAILURE);
229   xprintf("%s\n", nullcheck(utf82mb(value)));
230 }
231
232 static void cf_length(disorder_client *c, char **argv) {
233   long length;
234
235   if(disorder_length(c, argv[0], &length)) exit(EXIT_FAILURE);
236   xprintf("%ld\n", length);
237 }
238
239 static void cf_set(disorder_client *c, char **argv) {
240   if(disorder_set(c, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
241 }
242
243 static void cf_unset(disorder_client *c, char **argv) {
244   if(disorder_unset(c, argv[0], argv[1])) exit(EXIT_FAILURE);
245 }
246
247 static void cf_prefs(disorder_client *c, char **argv) {
248   struct kvp *k;
249
250   if(disorder_prefs(c, argv[0], &k)) exit(EXIT_FAILURE);
251   for(; k; k = k->next)
252     xprintf("%s = %s\n",
253             nullcheck(utf82mb(k->name)), nullcheck(utf82mb(k->value)));
254 }
255
256 static void cf_search(disorder_client *c, char **argv) {
257   char **results;
258   int nresults, n;
259
260   if(disorder_search(c, *argv, &results, &nresults)) exit(EXIT_FAILURE);
261   for(n = 0; n < nresults; ++n)
262     xprintf("%s\n", nullcheck(utf82mb(results[n])));
263 }
264
265 static void cf_random_disable(disorder_client *c,
266                               char attribute((unused)) **argv) {
267   if(disorder_random_disable(c)) exit(EXIT_FAILURE);
268 }
269
270 static void cf_random_enable(disorder_client *c,
271                              char attribute((unused)) **argv) {
272   if(disorder_random_enable(c)) exit(EXIT_FAILURE);
273 }
274
275 static void cf_stats(disorder_client *c,
276                      char attribute((unused)) **argv) {
277   char **vec;
278
279   if(disorder_stats(c, &vec, 0)) exit(EXIT_FAILURE);
280   while(*vec)
281       xprintf("%s\n", nullcheck(utf82mb(*vec++)));
282 }
283
284 static void cf_get_volume(disorder_client *c,
285                           char attribute((unused)) **argv) {
286   int l, r;
287
288   if(disorder_get_volume(c, &l, &r)) exit(EXIT_FAILURE);
289   xprintf("%d %d\n", l, r);
290 }
291
292 static void cf_set_volume(disorder_client *c,
293                           char **argv) {
294   if(disorder_set_volume(c, atoi(argv[0]), atoi(argv[1]))) exit(EXIT_FAILURE);
295 }
296
297 static void cf_become(disorder_client *c,
298                       char **argv) {
299   if(disorder_become(c, argv[0])) exit(EXIT_FAILURE);
300 }
301
302 static void cf_log(disorder_client *c,
303                    char attribute((unused)) **argv) {
304   if(disorder_log(c, sink_stdio("stdout", stdout))) exit(EXIT_FAILURE);
305 }
306
307 static void cf_move(disorder_client *c,
308                    char **argv) {
309   long n;
310   int e;
311   
312   if((e = xstrtol(&n, argv[1], 0, 10)))
313     fatal(e, "cannot convert '%s'", argv[1]);
314   if(n > INT_MAX || n < INT_MIN)
315     fatal(e, "%ld out of range", n);
316   if(disorder_move(c, argv[0], (int)n)) exit(EXIT_FAILURE);
317 }
318
319 static void cf_part(disorder_client *c,
320                     char **argv) {
321   char *s;
322
323   if(disorder_part(c, &s, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
324   xprintf("%s\n", nullcheck(utf82mb(s)));
325 }
326
327 static int isarg_filename(const char *s) {
328   return s[0] == '/';
329 }
330
331 static void cf_authorize(disorder_client attribute((unused)) *c,
332                          char **argv) {
333   if(!authorize(argv[0]))
334     auto_reconfigure = 1;
335 }
336
337 static void cf_resolve(disorder_client *c,
338                        char **argv) {
339   char *track;
340
341   if(disorder_resolve(c, &track, argv[0])) exit(EXIT_FAILURE);
342   xprintf("%s\n", nullcheck(utf82mb(track)));
343 }
344
345 static void cf_pause(disorder_client *c,
346                         char attribute((unused)) **argv) {
347   if(disorder_pause(c)) exit(EXIT_FAILURE);
348 }
349
350 static void cf_resume(disorder_client *c,
351                         char attribute((unused)) **argv) {
352   if(disorder_resume(c)) exit(EXIT_FAILURE);
353 }
354
355 static void cf_tags(disorder_client *c,
356                      char attribute((unused)) **argv) {
357   char **vec;
358
359   if(disorder_tags(c, &vec, 0)) exit(EXIT_FAILURE);
360   while(*vec)
361       xprintf("%s\n", nullcheck(utf82mb(*vec++)));
362 }
363
364 static void cf_get_global(disorder_client *c, char **argv) {
365   char *value;
366
367   if(disorder_get_global(c, argv[0], &value)) exit(EXIT_FAILURE);
368   xprintf("%s\n", nullcheck(utf82mb(value)));
369 }
370
371 static void cf_set_global(disorder_client *c, char **argv) {
372   if(disorder_set_global(c, argv[0], argv[1])) exit(EXIT_FAILURE);
373 }
374
375 static void cf_unset_global(disorder_client *c, char **argv) {
376   if(disorder_unset_global(c, argv[0])) exit(EXIT_FAILURE);
377 }
378
379 static const struct command {
380   const char *name;
381   int min, max;
382   void (*fn)(disorder_client *c, char **);
383   int (*isarg)(const char *);
384   const char *argstr, *desc;
385 } commands[] = {
386   { "allfiles",       1, 2, cf_allfiles, isarg_regexp, "DIR [~REGEXP]",
387                       "List all files and directories in DIR" },
388   { "authorize",      1, 1, cf_authorize, 0, "USER",
389                       "Authorize USER to connect to the server" },
390   { "become",         1, 1, cf_become, 0, "USER",
391                       "Become user USER" },
392   { "dirs",           1, 2, cf_dirs, isarg_regexp, "DIR [~REGEXP]",
393                       "List directories in DIR" },
394   { "disable",        0, 0, cf_disable, 0, "",
395                       "Disable play" },
396   { "disable-random", 0, 0, cf_random_disable, 0, "",
397                       "Disable random play" },
398   { "enable",         0, 0, cf_enable, 0, "",
399                       "Enable play" },
400   { "enable-random",  0, 0, cf_random_enable, 0, "",
401                       "Enable random play" },
402   { "files",          1, 2, cf_files, isarg_regexp, "DIR [~REGEXP]",
403                       "List files in DIR" },
404   { "get",            2, 2, cf_get, 0, "TRACK NAME",
405                       "Get a preference value" },
406   { "get-global",     1, 1, cf_get_global, 0, "NAME",
407                       "Get a global preference value" },
408   { "get-volume",     0, 0, cf_get_volume, 0, "",
409                       "Get the current volume" },
410   { "length",         1, 1, cf_length, 0, "TRACK",
411                       "Get the length of TRACK in seconds" },
412   { "log",            0, 0, cf_log, 0, "",
413                       "Copy event log to stdout" },
414   { "move",           2, 2, cf_move, 0, "TRACK DELTA",
415                       "Move a track in the queue" },
416   { "part",           3, 3, cf_part, 0, "TRACK CONTEXT PART",
417                       "Find a track name part" },
418   { "pause",          0, 0, cf_pause, 0, "",
419                       "Pause the currently playing track" },
420   { "play",           1, INT_MAX, cf_play, isarg_filename, "TRACKS...",
421                       "Add TRACKS to the end of the queue" },
422   { "playing",        0, 0, cf_playing, 0, "",
423                       "Report the playing track" },
424   { "prefs",          1, 1, cf_prefs, 0, "TRACK",
425                       "Display all the preferences for TRACK" },
426   { "quack",          0, 0, cf_quack, 0, 0, 0 },
427   { "queue",          0, 0, cf_queue, 0, "",
428                       "Display the current queue" },
429   { "random-disable", 0, 0, cf_random_disable, 0, "",
430                       "Disable random play" },
431   { "random-enable",  0, 0, cf_random_enable, 0, "",
432                       "Enable random play" },
433   { "recent",         0, 0, cf_recent, 0, "",
434                       "Display recently played track" },
435   { "reconfigure",    0, 0, cf_reconfigure, 0, "",
436                       "Reconfigure the daemon" },
437   { "remove",         1, 1, cf_remove, 0, "TRACK",
438                       "Remove a track from the queue" },
439   { "rescan",         0, 0, cf_rescan, 0, "",
440                       "Rescan for new tracks" },
441   { "resolve",        1, 1, cf_resolve, 0, "TRACK",
442                       "Resolve alias for TRACK" },
443   { "resume",         0, 0, cf_resume, 0, "",
444                       "Resume after a pause" },
445   { "scratch",        0, 0, cf_scratch, 0, "",
446                       "Scratch the currently playing track" },
447   { "scratch-id",     1, 1, cf_scratch, 0, "ID",
448                       "Scratch the currently playing track" },
449   { "search",         1, 1, cf_search, 0, "WORDS",
450                       "Display tracks matching all the words" },
451   { "set",            3, 3, cf_set, 0, "TRACK NAME VALUE",
452                       "Set a preference value" },
453   { "set-global",     2, 2, cf_set_global, 0, "NAME VALUE",
454                       "Set a global preference value" },
455   { "set-volume",     2, 2, cf_set_volume, 0, "LEFT RIGHT",
456                       "Set the volume" },
457   { "shutdown",       0, 0, cf_shutdown, 0, "",
458                       "Shut down the daemon" },
459   { "stats",          0, 0, cf_stats, 0, "",
460                       "Display server statistics" },
461   { "tags",           0, 0, cf_tags, 0, "",
462                       "List known tags" },
463   { "unset",          2, 2, cf_unset, 0, "TRACK NAME",
464                       "Unset a preference" },
465   { "unset-global",   1, 1, cf_unset_global, 0, "NAME",
466                       "Unset a global preference" },
467   { "version",        0, 0, cf_version, 0, "",
468                       "Display the server version" },
469 };
470
471 static void help_commands(void) {
472   unsigned n, max = 0, l;
473
474   xprintf("Command summary:\n");
475   for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
476     if(!commands[n].desc) continue;
477     l = strlen(commands[n].name);
478     if(*commands[n].argstr)
479       l += strlen(commands[n].argstr) + 1;
480     if(l > max)
481       max = l;
482   }
483   for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
484     if(!commands[n].desc) continue;
485     l = strlen(commands[n].name);
486     if(*commands[n].argstr)
487       l += strlen(commands[n].argstr) + 1;
488     xprintf("  %s%s%s%*s  %s\n", commands[n].name,
489             *commands[n].argstr ? " " : "",
490             commands[n].argstr,
491             max - l, "",
492             commands[n].desc);
493   }
494   xfclose(stdout);
495   exit(0);
496 }
497
498 int main(int argc, char **argv) {
499   int n, i, j;
500   disorder_client *c = 0;
501   const char *s;
502   int status = 0;
503   struct vector args;
504
505   mem_init();
506   /* garbage-collect PCRE's memory */
507   pcre_malloc = xmalloc;
508   pcre_free = xfree;
509   if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
510   while((n = getopt_long(argc, argv, "hVc:dHL", options, 0)) >= 0) {
511     switch(n) {
512     case 'h': help();
513     case 'H': help_commands();
514     case 'V': version();
515     case 'c': configfile = optarg; break;
516     case 'd': debugging = 1; break;
517     default: fatal(0, "invalid option");
518     }
519   }
520   if(config_read()) fatal(0, "cannot read configuration");
521   if(!(c = disorder_new(1))) exit(EXIT_FAILURE);
522   s = config_get_file("socket");
523   if(disorder_connect(c)) exit(EXIT_FAILURE);
524   n = optind;
525   /* accumulate command args */
526   while(n < argc) {
527     if((i = TABLE_FIND(commands, struct command, name, argv[n])) < 0)
528       fatal(0, "unknown command '%s'", argv[n]);
529     if(n + commands[i].min >= argc)
530       fatal(0, "missing arguments to '%s'", argv[n]);
531     n++;
532     vector_init(&args);
533     for(j = 0; j < commands[i].min; ++j)
534       vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
535     for(; j < commands[i].max
536           && n + j < argc
537           && commands[i].isarg(argv[n + j]); ++j)
538       vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
539     vector_terminate(&args);
540     commands[i].fn(c, args.vec);
541     n += j;
542   }
543   if(auto_reconfigure) {
544     assert(c != 0);
545     if(disorder_reconfigure(c)) exit(EXIT_FAILURE);
546   }
547   if(c && disorder_close(c)) exit(EXIT_FAILURE);
548   if(fclose(stdout) < 0) fatal(errno, "error closing stdout");
549   return status;
550 }
551
552 /*
553 Local Variables:
554 c-basic-offset:2
555 comment-column:40
556 End:
557 */