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