chiark / gitweb /
Support schedule-* commands from command-line client. Exiguously tested.
[disorder] / clients / disorder.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004-2008 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 #include "version.h"
57
58 static disorder_client *client;
59
60 static const struct option options[] = {
61   { "help", no_argument, 0, 'h' },
62   { "version", no_argument, 0, 'V' },
63   { "config", required_argument, 0, 'c' },
64   { "debug", no_argument, 0, 'd' },
65   { "local", no_argument, 0, 'l' },
66   { "no-per-user-config", no_argument, 0, 'N' },
67   { "help-commands", no_argument, 0, 'H' },
68   { "user", required_argument, 0, 'u' },
69   { "password", required_argument, 0, 'p' },
70   { 0, 0, 0, 0 }
71 };
72
73 /* display usage message and terminate */
74 static void help(void) {
75   xprintf("Usage:\n"
76           "  disorder [OPTIONS] COMMAND ...\n"
77           "Options:\n"
78           "  --help, -h              Display usage message\n"
79           "  --help-commands, -H     List commands\n"
80           "  --version, -V           Display version number\n"
81           "  --config PATH, -c PATH  Set configuration file\n"
82           "  --local, -l             Force connection to local server\n"
83           "  --debug, -d             Turn on debugging\n");
84   xfclose(stdout);
85   exit(0);
86 }
87
88 static disorder_client *getclient(void) {
89   if(!client) {
90     if(!(client = disorder_new(1))) exit(EXIT_FAILURE);
91     if(disorder_connect(client)) exit(EXIT_FAILURE);
92   }
93   return client;
94 }
95
96 static void cf_version(char attribute((unused)) **argv) {
97   char *v;
98
99   if(disorder_version(getclient(), &v)) exit(EXIT_FAILURE);
100   xprintf("%s\n", nullcheck(utf82mb(v)));
101 }
102
103 static void print_queue_entry(const struct queue_entry *q) {
104   if(q->track) xprintf("track %s\n", nullcheck(utf82mb(q->track)));
105   if(q->id) xprintf("  id %s\n", nullcheck(utf82mb(q->id)));
106   if(q->submitter) xprintf("  submitted by %s at %s",
107                            nullcheck(utf82mb(q->submitter)), ctime(&q->when));
108   if(q->played) xprintf("  played at %s", ctime(&q->played));
109   if(q->state == playing_started
110      || q->state == playing_paused) xprintf("  %lds so far",  q->sofar);
111   else if(q->expected) xprintf("  might start at %s", ctime(&q->expected));
112   if(q->scratched) xprintf("  scratched by %s\n",
113                            nullcheck(utf82mb(q->scratched)));
114   else xprintf("  %s\n", playing_states[q->state]);
115   if(q->wstat) xprintf("  %s\n", wstat(q->wstat));
116 }
117
118 static void cf_playing(char attribute((unused)) **argv) {
119   struct queue_entry *q;
120
121   if(disorder_playing(getclient(), &q)) exit(EXIT_FAILURE);
122   if(q)
123     print_queue_entry(q);
124   else
125     xprintf("nothing\n");
126 }
127
128 static void cf_play(char **argv) {
129   while(*argv)
130     if(disorder_play(getclient(), *argv++)) exit(EXIT_FAILURE);
131 }
132
133 static void cf_remove(char **argv) {
134   if(disorder_remove(getclient(), argv[0])) exit(EXIT_FAILURE);
135 }
136
137 static void cf_disable(char attribute((unused)) **argv) {
138   if(disorder_disable(getclient())) exit(EXIT_FAILURE);
139 }
140
141 static void cf_enable(char attribute((unused)) **argv) {
142   if(disorder_enable(getclient())) exit(EXIT_FAILURE);
143 }
144
145 static void cf_scratch(char **argv) {
146   if(disorder_scratch(getclient(), argv[0])) exit(EXIT_FAILURE);
147 }
148
149 static void cf_shutdown(char attribute((unused)) **argv) {
150   if(disorder_shutdown(getclient())) exit(EXIT_FAILURE);
151 }
152
153 static void cf_reconfigure(char attribute((unused)) **argv) {
154   /* Re-check configuration for server */
155   if(config_read(1)) fatal(0, "cannot read configuration");
156   if(disorder_reconfigure(getclient())) exit(EXIT_FAILURE);
157 }
158
159 static void cf_rescan(char attribute((unused)) **argv) {
160   if(disorder_rescan(getclient())) exit(EXIT_FAILURE);
161 }
162
163 static void cf_somequeue(int (*fn)(disorder_client *c,
164                                    struct queue_entry **qp)) {
165   struct queue_entry *q;
166
167   if(fn(getclient(), &q)) exit(EXIT_FAILURE);
168   while(q) {
169     print_queue_entry(q);
170     q = q->next;
171   }
172 }
173
174 static void cf_recent(char attribute((unused)) **argv) {
175   cf_somequeue(disorder_recent);
176 }
177
178 static void cf_queue(char attribute((unused)) **argv) {
179   cf_somequeue(disorder_queue);
180 }
181
182 static void cf_quack(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(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(getclient(), 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(char **argv) {
215   cf_somelist(argv, disorder_directories);
216 }
217
218 static void cf_files(char **argv) {
219   cf_somelist(argv, disorder_files);
220 }
221
222 static void cf_allfiles(char **argv) {
223   cf_somelist(argv, disorder_allfiles);
224 }
225
226 static void cf_get(char **argv) {
227   char *value;
228
229   if(disorder_get(getclient(), argv[0], argv[1], &value)) exit(EXIT_FAILURE);
230   xprintf("%s\n", nullcheck(utf82mb(value)));
231 }
232
233 static void cf_length(char **argv) {
234   long length;
235
236   if(disorder_length(getclient(), argv[0], &length)) exit(EXIT_FAILURE);
237   xprintf("%ld\n", length);
238 }
239
240 static void cf_set(char **argv) {
241   if(disorder_set(getclient(), argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
242 }
243
244 static void cf_unset(char **argv) {
245   if(disorder_unset(getclient(), argv[0], argv[1])) exit(EXIT_FAILURE);
246 }
247
248 static void cf_prefs(char **argv) {
249   struct kvp *k;
250
251   if(disorder_prefs(getclient(), argv[0], &k)) exit(EXIT_FAILURE);
252   for(; k; k = k->next)
253     xprintf("%s = %s\n",
254             nullcheck(utf82mb(k->name)), nullcheck(utf82mb(k->value)));
255 }
256
257 static void cf_search(char **argv) {
258   char **results;
259   int nresults, n;
260
261   if(disorder_search(getclient(), *argv, &results, &nresults)) exit(EXIT_FAILURE);
262   for(n = 0; n < nresults; ++n)
263     xprintf("%s\n", nullcheck(utf82mb(results[n])));
264 }
265
266 static void cf_random_disable(char attribute((unused)) **argv) {
267   if(disorder_random_disable(getclient())) exit(EXIT_FAILURE);
268 }
269
270 static void cf_random_enable(char attribute((unused)) **argv) {
271   if(disorder_random_enable(getclient())) exit(EXIT_FAILURE);
272 }
273
274 static void cf_stats(char attribute((unused)) **argv) {
275   char **vec;
276
277   if(disorder_stats(getclient(), &vec, 0)) exit(EXIT_FAILURE);
278   while(*vec)
279       xprintf("%s\n", nullcheck(utf82mb(*vec++)));
280 }
281
282 static void cf_get_volume(char attribute((unused)) **argv) {
283   int l, r;
284
285   if(disorder_get_volume(getclient(), &l, &r)) exit(EXIT_FAILURE);
286   xprintf("%d %d\n", l, r);
287 }
288
289 static void cf_set_volume(char **argv) {
290   if(disorder_set_volume(getclient(), atoi(argv[0]), atoi(argv[1]))) exit(EXIT_FAILURE);
291 }
292
293 static void cf_log(char attribute((unused)) **argv) {
294   if(disorder_log(getclient(), sink_stdio("stdout", stdout))) exit(EXIT_FAILURE);
295 }
296
297 static void cf_move(char **argv) {
298   long n;
299   int e;
300   
301   if((e = xstrtol(&n, argv[1], 0, 10)))
302     fatal(e, "cannot convert '%s'", argv[1]);
303   if(n > INT_MAX || n < INT_MIN)
304     fatal(e, "%ld out of range", n);
305   if(disorder_move(getclient(), argv[0], (int)n)) exit(EXIT_FAILURE);
306 }
307
308 static void cf_part(char **argv) {
309   char *s;
310
311   if(disorder_part(getclient(), &s, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
312   xprintf("%s\n", nullcheck(utf82mb(s)));
313 }
314
315 static int isarg_filename(const char *s) {
316   return s[0] == '/';
317 }
318
319 static void cf_authorize(char **argv) {
320   authorize(getclient(), argv[0], argv[1]);
321 }
322
323 static void cf_resolve(char **argv) {
324   char *track;
325
326   if(disorder_resolve(getclient(), &track, argv[0])) exit(EXIT_FAILURE);
327   xprintf("%s\n", nullcheck(utf82mb(track)));
328 }
329
330 static void cf_pause(char attribute((unused)) **argv) {
331   if(disorder_pause(getclient())) exit(EXIT_FAILURE);
332 }
333
334 static void cf_resume(char attribute((unused)) **argv) {
335   if(disorder_resume(getclient())) exit(EXIT_FAILURE);
336 }
337
338 static void cf_tags(char attribute((unused)) **argv) {
339   char **vec;
340
341   if(disorder_tags(getclient(), &vec, 0)) exit(EXIT_FAILURE);
342   while(*vec)
343       xprintf("%s\n", nullcheck(utf82mb(*vec++)));
344 }
345
346 static void cf_users(char attribute((unused)) **argv) {
347   char **vec;
348
349   if(disorder_users(getclient(), &vec, 0)) exit(EXIT_FAILURE);
350   while(*vec)
351     xprintf("%s\n", nullcheck(utf82mb(*vec++)));
352 }
353
354 static void cf_get_global(char **argv) {
355   char *value;
356
357   if(disorder_get_global(getclient(), argv[0], &value)) exit(EXIT_FAILURE);
358   xprintf("%s\n", nullcheck(utf82mb(value)));
359 }
360
361 static void cf_set_global(char **argv) {
362   if(disorder_set_global(getclient(), argv[0], argv[1])) exit(EXIT_FAILURE);
363 }
364
365 static void cf_unset_global(char **argv) {
366   if(disorder_unset_global(getclient(), argv[0])) exit(EXIT_FAILURE);
367 }
368
369 static int isarg_integer(const char *s) {
370   if(!*s) return 0;
371   while(*s) {
372     if(!isdigit((unsigned char)*s))
373       return 0;
374     ++s;
375   }
376   return 1;
377 }
378
379 static void cf_new(char **argv) {
380   char **vec;
381
382   if(disorder_new_tracks(getclient(), &vec, 0, argv[0] ? atoi(argv[0]) : 0))
383     exit(EXIT_FAILURE);
384   while(*vec)
385     xprintf("%s\n", nullcheck(utf82mb(*vec++)));
386 }
387
388 static void cf_rtp_address(char attribute((unused)) **argv) {
389   char *address, *port;
390
391   if(disorder_rtp_address(getclient(), &address, &port)) exit(EXIT_FAILURE);
392   xprintf("address: %s\nport: %s\n", address, port);
393 }
394
395 static int isarg_rights(const char *arg) {
396   return strchr(arg, ',') || !parse_rights(arg, 0, 0);
397 }
398
399 static void cf_adduser(char **argv) {
400   if(disorder_adduser(getclient(), argv[0], argv[1], argv[2]))
401     exit(EXIT_FAILURE);
402 }
403
404 static void cf_deluser(char **argv) {
405   if(disorder_deluser(getclient(), argv[0]))
406     exit(EXIT_FAILURE);
407 }
408
409 static void cf_edituser(char **argv) {
410   if(disorder_edituser(getclient(), argv[0], argv[1], argv[2]))
411     exit(EXIT_FAILURE);
412 }
413
414 static void cf_userinfo(char **argv) {
415   char *s;
416
417   if(disorder_userinfo(getclient(), argv[0], argv[1], &s))
418     exit(EXIT_FAILURE);
419   xprintf("%s\n", nullcheck(utf82mb(s)));
420 }
421
422 static int isarg_option(const char *arg) {
423   return arg[0] == '-';
424 }
425
426 static int argvlen(char **argv) {
427   char **start = argv;
428   
429   while(*argv)
430     ++argv;
431   return argv - start;
432 }
433
434 static const struct option setup_guest_options[] = {
435   { "help", no_argument, 0, 'h' },
436   { "online-registration", no_argument, 0, 'r' },
437   { "no-online-registration", no_argument, 0, 'R' },
438   { 0, 0, 0, 0 }
439 };
440
441 static void help_setup_guest(void) {
442   xprintf("Usage:\n"
443           "  disorder setup-guest [OPTIONS]\n"
444           "Options:\n"
445           "  --help, -h                Display usage message\n"
446           "  --online-registration     Enable online registration (default)\n"
447           "  --no-online-registration  Disable online registration\n");
448   xfclose(stdout);
449   exit(0);
450 }
451
452 static void cf_setup_guest(char **argv) {
453   int n, online_registration = 1;
454   
455   while((n = getopt_long(argvlen(argv) + 1, argv - 1,
456                          "hrR", setup_guest_options, 0)) >= 0) {
457     switch(n) {
458     case 'h': help_setup_guest();
459     case 'r': online_registration = 1; break;
460     case 'R': online_registration = 0; break;
461     default: fatal(0, "invalid option");
462     }
463   }
464   if(online_registration && !config->mail_sender)
465     fatal(0, "you MUST set mail_sender if you want online registration");
466   if(disorder_adduser(getclient(), "guest", "",
467                       online_registration ? "read,register" : "read"))
468     exit(EXIT_FAILURE);
469 }
470
471 struct scheduled_event {
472   time_t when;
473   struct kvp *actiondata;
474   char *id;
475 };
476
477 static int compare_event(const void *av, const void *bv) {
478   struct scheduled_event *a = (void *)av, *b = (void *)bv;
479
480   /* Primary sort key is the trigger time */
481   if(a->when < b->when)
482     return -1;
483   else if(a->when > b->when)
484     return 1;
485   /* For events that go off at the same time just sort by ID */
486   return strcmp(a->id, b->id);
487 }
488
489 static void cf_schedule_list(char attribute((unused)) **argv) {
490   char **ids;
491   int nids, n;
492   struct scheduled_event *events;
493   char tb[128];
494   const char *action, *key, *value, *priority;
495   int prichar;
496
497   /* Get all known events */
498   if(disorder_schedule_list(getclient(), &ids, &nids))
499     exit(EXIT_FAILURE);
500   events = xcalloc(nids, sizeof *events);
501   for(n = 0; n < nids; ++n) {
502     events[n].id = ids[n];
503     if(disorder_schedule_get(getclient(), ids[n], &events[n].actiondata))
504       exit(EXIT_FAILURE);
505     events[n].when = atoll(kvp_get(events[n].actiondata, "when"));
506   }
507   /* Sort by trigger time */
508   qsort(events, nids, sizeof *events, compare_event);
509   /* Display them */
510   for(n = 0; n < nids; ++n) {
511     strftime(tb, sizeof tb, "%Y-%m-%d %H:%M:%S %Z", localtime(&events[n].when));
512     action = kvp_get(events[n].actiondata, "action");
513     priority = kvp_get(events[n].actiondata, "priority");
514     if(!strcmp(priority, "junk"))
515       prichar = 'J';
516     else if(!strcmp(priority, "normal"))
517       prichar = 'N';
518     else
519       prichar = '?';
520     xprintf("%11s %-25s %c %-8s %s",
521             events[n].id, tb, prichar, kvp_get(events[n].actiondata, "who"),
522             action);
523     if(!strcmp(action, "play"))
524       xprintf(" %s",
525               nullcheck(utf82mb(kvp_get(events[n].actiondata, "track"))));
526     else if(!strcmp(action, "set-global")) {
527       key = kvp_get(events[n].actiondata, "key");
528       value = kvp_get(events[n].actiondata, "value");
529       if(value)
530         xprintf(" %s=%s",
531                 nullcheck(utf82mb(key)),
532                 nullcheck(utf82mb(value)));
533       else
534         xprintf(" %s unset",
535                 nullcheck(utf82mb(key)));
536     }
537     xprintf("\n");
538   }
539 }
540
541 static void cf_schedule_del(char **argv) {
542   if(disorder_schedule_del(getclient(), argv[0]))
543     exit(EXIT_FAILURE);
544 }
545
546 static void cf_schedule_play(char **argv) {
547   if(disorder_schedule_add(getclient(),
548                            atoll(argv[0]),
549                            argv[1],
550                            "play",
551                            argv[2]))
552     exit(EXIT_FAILURE);
553 }
554
555 static void cf_schedule_set_global(char **argv) {
556   if(disorder_schedule_add(getclient(),
557                            atoll(argv[0]),
558                            argv[1],
559                            "set-global",
560                            argv[2],
561                            argv[3]))
562     exit(EXIT_FAILURE);
563 }
564
565 static void cf_schedule_unset_global(char **argv) {
566   if(disorder_schedule_add(getclient(),
567                            atoll(argv[0]),
568                            argv[1],
569                            "set-global",
570                            argv[2],
571                            (char *)0))
572     exit(EXIT_FAILURE);
573 }
574
575 static const struct command {
576   const char *name;
577   int min, max;
578   void (*fn)(char **);
579   int (*isarg)(const char *);
580   const char *argstr, *desc;
581 } commands[] = {
582   { "adduser",        2, 3, cf_adduser, isarg_rights, "USERNAME PASSWORD [RIGHTS]",
583                       "Create a new user" },
584   { "allfiles",       1, 2, cf_allfiles, isarg_regexp, "DIR [~REGEXP]",
585                       "List all files and directories in DIR" },
586   { "authorize",      1, 2, cf_authorize, isarg_rights, "USERNAME [RIGHTS]",
587                       "Authorize user USERNAME to connect" },
588   { "deluser",        1, 1, cf_deluser, 0, "USERNAME",
589                       "Delete user USERNAME" },
590   { "dirs",           1, 2, cf_dirs, isarg_regexp, "DIR [~REGEXP]",
591                       "List directories in DIR" },
592   { "disable",        0, 0, cf_disable, 0, "",
593                       "Disable play" },
594   { "disable-random", 0, 0, cf_random_disable, 0, "",
595                       "Disable random play" },
596   { "edituser",       3, 3, cf_edituser, 0, "USERNAME PROPERTY VALUE",
597                       "Set a property of user USERNAME" },
598   { "enable",         0, 0, cf_enable, 0, "",
599                       "Enable play" },
600   { "enable-random",  0, 0, cf_random_enable, 0, "",
601                       "Enable random play" },
602   { "files",          1, 2, cf_files, isarg_regexp, "DIR [~REGEXP]",
603                       "List files in DIR" },
604   { "get",            2, 2, cf_get, 0, "TRACK NAME",
605                       "Get a preference value" },
606   { "get-global",     1, 1, cf_get_global, 0, "NAME",
607                       "Get a global preference value" },
608   { "get-volume",     0, 0, cf_get_volume, 0, "",
609                       "Get the current volume" },
610   { "length",         1, 1, cf_length, 0, "TRACK",
611                       "Get the length of TRACK in seconds" },
612   { "log",            0, 0, cf_log, 0, "",
613                       "Copy event log to stdout" },
614   { "move",           2, 2, cf_move, 0, "TRACK DELTA",
615                       "Move a track in the queue" },
616   { "new",            0, 1, cf_new, isarg_integer, "[MAX]",
617                       "Get the most recently added MAX tracks" },
618   { "part",           3, 3, cf_part, 0, "TRACK CONTEXT PART",
619                       "Find a track name part" },
620   { "pause",          0, 0, cf_pause, 0, "",
621                       "Pause the currently playing track" },
622   { "play",           1, INT_MAX, cf_play, isarg_filename, "TRACKS...",
623                       "Add TRACKS to the end of the queue" },
624   { "playing",        0, 0, cf_playing, 0, "",
625                       "Report the playing track" },
626   { "prefs",          1, 1, cf_prefs, 0, "TRACK",
627                       "Display all the preferences for TRACK" },
628   { "quack",          0, 0, cf_quack, 0, 0, 0 },
629   { "queue",          0, 0, cf_queue, 0, "",
630                       "Display the current queue" },
631   { "random-disable", 0, 0, cf_random_disable, 0, "",
632                       "Disable random play" },
633   { "random-enable",  0, 0, cf_random_enable, 0, "",
634                       "Enable random play" },
635   { "recent",         0, 0, cf_recent, 0, "",
636                       "Display recently played track" },
637   { "reconfigure",    0, 0, cf_reconfigure, 0, "",
638                       "Reconfigure the daemon" },
639   { "remove",         1, 1, cf_remove, 0, "TRACK",
640                       "Remove a track from the queue" },
641   { "rescan",         0, 0, cf_rescan, 0, "",
642                       "Rescan for new tracks" },
643   { "resolve",        1, 1, cf_resolve, 0, "TRACK",
644                       "Resolve alias for TRACK" },
645   { "resume",         0, 0, cf_resume, 0, "",
646                       "Resume after a pause" },
647   { "rtp-address",    0, 0, cf_rtp_address, 0, "",
648                       "Report server's broadcast address" },
649   { "schedule-del",   1, 1, cf_schedule_del, 0, "EVENT",
650                       "Delete a scheduled event" },
651   { "schedule-list",  0, 0, cf_schedule_list, 0, "",
652                       "List scheduled events" },
653   { "schedule-play",  3, 3, cf_schedule_play, 0, "WHEN PRI TRACK",
654                       "Play TRACK later" },
655   { "schedule-set-global", 4, 4, cf_schedule_set_global, 0, "WHEN PRI NAME VAL",
656                       "Set a global preference later" },
657   { "schedule-unset-global", 3, 3, cf_schedule_unset_global, 0, "WHEN PRI NAME",
658                       "Unset a global preference later" },
659   { "scratch",        0, 0, cf_scratch, 0, "",
660                       "Scratch the currently playing track" },
661   { "scratch-id",     1, 1, cf_scratch, 0, "ID",
662                       "Scratch the currently playing track" },
663   { "search",         1, 1, cf_search, 0, "WORDS",
664                       "Display tracks matching all the words" },
665   { "set",            3, 3, cf_set, 0, "TRACK NAME VALUE",
666                       "Set a preference value" },
667   { "set-global",     2, 2, cf_set_global, 0, "NAME VALUE",
668                       "Set a global preference value" },
669   { "set-volume",     2, 2, cf_set_volume, 0, "LEFT RIGHT",
670                       "Set the volume" },
671   { "setup-guest",    0, INT_MAX, cf_setup_guest, isarg_option, "[OPTIONS]",
672                       "Create the guest login" },
673   { "shutdown",       0, 0, cf_shutdown, 0, "",
674                       "Shut down the daemon" },
675   { "stats",          0, 0, cf_stats, 0, "",
676                       "Display server statistics" },
677   { "tags",           0, 0, cf_tags, 0, "",
678                       "List known tags" },
679   { "unset",          2, 2, cf_unset, 0, "TRACK NAME",
680                       "Unset a preference" },
681   { "unset-global",   1, 1, cf_unset_global, 0, "NAME",
682                       "Unset a global preference" },
683   { "userinfo",       2, 2, cf_userinfo, 0, "USERNAME PROPERTY",
684                       "Get a property of a user" },
685   { "users",          0, 0, cf_users, 0, "",
686                       "List all users" },
687   { "version",        0, 0, cf_version, 0, "",
688                       "Display the server version" },
689 };
690
691 static void help_commands(void) {
692   unsigned n, max = 0, l;
693
694   xprintf("Command summary:\n");
695   for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
696     if(!commands[n].desc) continue;
697     l = strlen(commands[n].name);
698     if(*commands[n].argstr)
699       l += strlen(commands[n].argstr) + 1;
700     if(l > max)
701       max = l;
702   }
703   for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
704     if(!commands[n].desc) continue;
705     l = strlen(commands[n].name);
706     if(*commands[n].argstr)
707       l += strlen(commands[n].argstr) + 1;
708     xprintf("  %s%s%s%*s  %s\n", commands[n].name,
709             *commands[n].argstr ? " " : "",
710             commands[n].argstr,
711             max - l, "",
712             commands[n].desc);
713   }
714   xfclose(stdout);
715   exit(0);
716 }
717
718 int main(int argc, char **argv) {
719   int n, i, j, local = 0;
720   int status = 0;
721   struct vector args;
722   const char *user = 0, *password = 0;
723
724   mem_init();
725   /* garbage-collect PCRE's memory */
726   pcre_malloc = xmalloc;
727   pcre_free = xfree;
728   if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
729   while((n = getopt_long(argc, argv, "+hVc:dHlNu:p:", options, 0)) >= 0) {
730     switch(n) {
731     case 'h': help();
732     case 'H': help_commands();
733     case 'V': version("disorder");
734     case 'c': configfile = optarg; break;
735     case 'd': debugging = 1; break;
736     case 'l': local = 1; break;
737     case 'N': config_per_user = 0; break;
738     case 'u': user = optarg; break;
739     case 'p': password = optarg; break;
740     default: fatal(0, "invalid option");
741     }
742   }
743   if(config_read(0)) fatal(0, "cannot read configuration");
744   if(user) {
745     config->username = user;
746     config->password = 0;
747   }
748   if(password)
749     config->password = password;
750   if(local)
751     config->connect.n = 0;
752   n = optind;
753   optind = 1;                           /* for subsequent getopt calls */
754   /* accumulate command args */
755   while(n < argc) {
756     if((i = TABLE_FIND(commands, struct command, name, argv[n])) < 0)
757       fatal(0, "unknown command '%s'", argv[n]);
758     if(n + commands[i].min >= argc)
759       fatal(0, "missing arguments to '%s'", argv[n]);
760     vector_init(&args);
761     /* Include the command name in the args, but at element -1, for
762      * the benefit of subcommand getopt calls */
763     vector_append(&args, argv[n]);
764     n++;
765     for(j = 0; j < commands[i].min; ++j)
766       vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
767     for(; j < commands[i].max
768           && n + j < argc
769           && commands[i].isarg(argv[n + j]); ++j)
770       vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
771     vector_terminate(&args);
772     commands[i].fn(args.vec + 1);
773     n += j;
774   }
775   if(client && disorder_close(client)) exit(EXIT_FAILURE);
776   if(fclose(stdout) < 0) fatal(errno, "error closing stdout");
777   return status;
778 }
779
780 /*
781 Local Variables:
782 c-basic-offset:2
783 comment-column:40
784 End:
785 */