chiark / gitweb /
relax config file checking for non-server programs
[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   /* Re-check configuration for server */
153   if(config_read(1)) fatal(0, "cannot read configuration");
154   if(disorder_reconfigure(c)) exit(EXIT_FAILURE);
155 }
156
157 static void cf_rescan(disorder_client *c, char attribute((unused)) **argv) {
158   if(disorder_rescan(c)) exit(EXIT_FAILURE);
159 }
160
161 static void cf_somequeue(disorder_client *c,
162                          int (*fn)(disorder_client *c,
163                                    struct queue_entry **qp)) {
164   struct queue_entry *q;
165
166   if(fn(c, &q)) exit(EXIT_FAILURE);
167   while(q) {
168     print_queue_entry(q);
169     q = q->next;
170   }
171 }
172
173 static void cf_recent(disorder_client *c, char attribute((unused)) **argv) {
174   cf_somequeue(c, disorder_recent);
175 }
176
177 static void cf_queue(disorder_client *c, char attribute((unused)) **argv) {
178   cf_somequeue(c, disorder_queue);
179 }
180
181 static void cf_quack(disorder_client attribute((unused)) *c,
182                      char attribute((unused)) **argv) {
183   xprintf("\n"
184           " .------------------.\n"
185           " | Naath is a babe! |\n"
186           " `---------+--------'\n"
187           "            \\\n"
188           "              >0\n"
189           "               (<)'\n"
190           "~~~~~~~~~~~~~~~~~~~~~~\n"
191           "\n");
192 }
193
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)) {
198   char **vec;
199   const char *re;
200
201   if(argv[1])
202     re = xstrdup(argv[1] + 1);
203   else
204     re = 0;
205   if(fn(c, argv[0], re, &vec, 0)) exit(EXIT_FAILURE);
206   while(*vec)
207     xprintf("%s\n", nullcheck(utf82mb(*vec++)));
208 }
209
210 static int isarg_regexp(const char *s) {
211   return s[0] == '~';
212 }
213
214 static void cf_dirs(disorder_client *c,
215                     char **argv) {
216   cf_somelist(c, argv, disorder_directories);
217 }
218
219 static void cf_files(disorder_client *c, char **argv) {
220   cf_somelist(c, argv, disorder_files);
221 }
222
223 static void cf_allfiles(disorder_client *c, char **argv) {
224   cf_somelist(c, argv, disorder_allfiles);
225 }
226
227 static void cf_get(disorder_client *c, char **argv) {
228   char *value;
229
230   if(disorder_get(c, argv[0], argv[1], &value)) exit(EXIT_FAILURE);
231   xprintf("%s\n", nullcheck(utf82mb(value)));
232 }
233
234 static void cf_length(disorder_client *c, char **argv) {
235   long length;
236
237   if(disorder_length(c, argv[0], &length)) exit(EXIT_FAILURE);
238   xprintf("%ld\n", length);
239 }
240
241 static void cf_set(disorder_client *c, char **argv) {
242   if(disorder_set(c, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
243 }
244
245 static void cf_unset(disorder_client *c, char **argv) {
246   if(disorder_unset(c, argv[0], argv[1])) exit(EXIT_FAILURE);
247 }
248
249 static void cf_prefs(disorder_client *c, char **argv) {
250   struct kvp *k;
251
252   if(disorder_prefs(c, argv[0], &k)) exit(EXIT_FAILURE);
253   for(; k; k = k->next)
254     xprintf("%s = %s\n",
255             nullcheck(utf82mb(k->name)), nullcheck(utf82mb(k->value)));
256 }
257
258 static void cf_search(disorder_client *c, char **argv) {
259   char **results;
260   int nresults, n;
261
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])));
265 }
266
267 static void cf_random_disable(disorder_client *c,
268                               char attribute((unused)) **argv) {
269   if(disorder_random_disable(c)) exit(EXIT_FAILURE);
270 }
271
272 static void cf_random_enable(disorder_client *c,
273                              char attribute((unused)) **argv) {
274   if(disorder_random_enable(c)) exit(EXIT_FAILURE);
275 }
276
277 static void cf_stats(disorder_client *c,
278                      char attribute((unused)) **argv) {
279   char **vec;
280
281   if(disorder_stats(c, &vec, 0)) exit(EXIT_FAILURE);
282   while(*vec)
283       xprintf("%s\n", nullcheck(utf82mb(*vec++)));
284 }
285
286 static void cf_get_volume(disorder_client *c,
287                           char attribute((unused)) **argv) {
288   int l, r;
289
290   if(disorder_get_volume(c, &l, &r)) exit(EXIT_FAILURE);
291   xprintf("%d %d\n", l, r);
292 }
293
294 static void cf_set_volume(disorder_client *c,
295                           char **argv) {
296   if(disorder_set_volume(c, atoi(argv[0]), atoi(argv[1]))) exit(EXIT_FAILURE);
297 }
298
299 static void cf_become(disorder_client *c,
300                       char **argv) {
301   if(disorder_become(c, argv[0])) exit(EXIT_FAILURE);
302 }
303
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);
307 }
308
309 static void cf_move(disorder_client *c,
310                    char **argv) {
311   long n;
312   int e;
313   
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);
319 }
320
321 static void cf_part(disorder_client *c,
322                     char **argv) {
323   char *s;
324
325   if(disorder_part(c, &s, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
326   xprintf("%s\n", nullcheck(utf82mb(s)));
327 }
328
329 static int isarg_filename(const char *s) {
330   return s[0] == '/';
331 }
332
333 static void cf_authorize(disorder_client attribute((unused)) *c,
334                          char **argv) {
335   if(!authorize(argv[0]))
336     auto_reconfigure = 1;
337 }
338
339 static void cf_resolve(disorder_client *c,
340                        char **argv) {
341   char *track;
342
343   if(disorder_resolve(c, &track, argv[0])) exit(EXIT_FAILURE);
344   xprintf("%s\n", nullcheck(utf82mb(track)));
345 }
346
347 static void cf_pause(disorder_client *c,
348                         char attribute((unused)) **argv) {
349   if(disorder_pause(c)) exit(EXIT_FAILURE);
350 }
351
352 static void cf_resume(disorder_client *c,
353                         char attribute((unused)) **argv) {
354   if(disorder_resume(c)) exit(EXIT_FAILURE);
355 }
356
357 static void cf_tags(disorder_client *c,
358                      char attribute((unused)) **argv) {
359   char **vec;
360
361   if(disorder_tags(c, &vec, 0)) exit(EXIT_FAILURE);
362   while(*vec)
363       xprintf("%s\n", nullcheck(utf82mb(*vec++)));
364 }
365
366 static void cf_get_global(disorder_client *c, char **argv) {
367   char *value;
368
369   if(disorder_get_global(c, argv[0], &value)) exit(EXIT_FAILURE);
370   xprintf("%s\n", nullcheck(utf82mb(value)));
371 }
372
373 static void cf_set_global(disorder_client *c, char **argv) {
374   if(disorder_set_global(c, argv[0], argv[1])) exit(EXIT_FAILURE);
375 }
376
377 static void cf_unset_global(disorder_client *c, char **argv) {
378   if(disorder_unset_global(c, argv[0])) exit(EXIT_FAILURE);
379 }
380
381 static const struct command {
382   const char *name;
383   int min, max;
384   void (*fn)(disorder_client *c, char **);
385   int (*isarg)(const char *);
386   const char *argstr, *desc;
387 } commands[] = {
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, "",
397                       "Disable play" },
398   { "disable-random", 0, 0, cf_random_disable, 0, "",
399                       "Disable random play" },
400   { "enable",         0, 0, cf_enable, 0, "",
401                       "Enable play" },
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",
458                       "Set the volume" },
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, "",
464                       "List known tags" },
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" },
471 };
472
473 static void help_commands(void) {
474   unsigned n, max = 0, l;
475
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;
482     if(l > max)
483       max = l;
484   }
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 ? " " : "",
492             commands[n].argstr,
493             max - l, "",
494             commands[n].desc);
495   }
496   xfclose(stdout);
497   exit(0);
498 }
499
500 int main(int argc, char **argv) {
501   int n, i, j;
502   disorder_client *c = 0;
503   const char *s;
504   int status = 0;
505   struct vector args;
506
507   mem_init();
508   /* garbage-collect PCRE's memory */
509   pcre_malloc = xmalloc;
510   pcre_free = xfree;
511   if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
512   while((n = getopt_long(argc, argv, "hVc:dHL", options, 0)) >= 0) {
513     switch(n) {
514     case 'h': help();
515     case 'H': help_commands();
516     case 'V': version();
517     case 'c': configfile = optarg; break;
518     case 'd': debugging = 1; break;
519     default: fatal(0, "invalid option");
520     }
521   }
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);
526   n = optind;
527   /* accumulate command args */
528   while(n < argc) {
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]);
533     n++;
534     vector_init(&args);
535     for(j = 0; j < commands[i].min; ++j)
536       vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
537     for(; j < commands[i].max
538           && n + j < argc
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);
543     n += j;
544   }
545   if(auto_reconfigure) {
546     assert(c != 0);
547     if(disorder_reconfigure(c)) exit(EXIT_FAILURE);
548   }
549   if(c && disorder_close(c)) exit(EXIT_FAILURE);
550   if(fclose(stdout) < 0) fatal(errno, "error closing stdout");
551   return status;
552 }
553
554 /*
555 Local Variables:
556 c-basic-offset:2
557 comment-column:40
558 End:
559 */