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