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