chiark / gitweb /
Further ALSA underrun fiddling.
[disorder] / clients / disorder.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
8ae47ac8 3 * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
460b9539 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>
fe8f8518 37#include <pcre.h>
2a10b70b 38#include <ctype.h>
460b9539 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"
3fbdc96d 56#include "version.h"
460b9539 57
0f55e905 58static disorder_client *client;
460b9539 59
60static 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' },
af66d051 65 { "local", no_argument, 0, 'l' },
63ad732f 66 { "no-per-user-config", no_argument, 0, 'N' },
460b9539 67 { "help-commands", no_argument, 0, 'H' },
f0feb22e
RK
68 { "user", required_argument, 0, 'u' },
69 { "password", required_argument, 0, 'p' },
460b9539 70 { 0, 0, 0, 0 }
71};
72
73/* display usage message and terminate */
74static 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"
af66d051 82 " --local, -l Force connection to local server\n"
460b9539 83 " --debug, -d Turn on debugging\n");
84 xfclose(stdout);
85 exit(0);
86}
87
0f55e905
RK
88static 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
96static void cf_version(char attribute((unused)) **argv) {
460b9539 97 char *v;
98
0f55e905 99 if(disorder_version(getclient(), &v)) exit(EXIT_FAILURE);
460b9539 100 xprintf("%s\n", nullcheck(utf82mb(v)));
101}
102
103static 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
0f55e905 118static void cf_playing(char attribute((unused)) **argv) {
460b9539 119 struct queue_entry *q;
120
0f55e905 121 if(disorder_playing(getclient(), &q)) exit(EXIT_FAILURE);
460b9539 122 if(q)
123 print_queue_entry(q);
124 else
125 xprintf("nothing\n");
126}
127
0f55e905 128static void cf_play(char **argv) {
460b9539 129 while(*argv)
0f55e905 130 if(disorder_play(getclient(), *argv++)) exit(EXIT_FAILURE);
460b9539 131}
132
0f55e905
RK
133static void cf_remove(char **argv) {
134 if(disorder_remove(getclient(), argv[0])) exit(EXIT_FAILURE);
460b9539 135}
136
0f55e905
RK
137static void cf_disable(char attribute((unused)) **argv) {
138 if(disorder_disable(getclient())) exit(EXIT_FAILURE);
460b9539 139}
140
0f55e905
RK
141static void cf_enable(char attribute((unused)) **argv) {
142 if(disorder_enable(getclient())) exit(EXIT_FAILURE);
460b9539 143}
144
0f55e905
RK
145static void cf_scratch(char **argv) {
146 if(disorder_scratch(getclient(), argv[0])) exit(EXIT_FAILURE);
460b9539 147}
148
0f55e905
RK
149static void cf_shutdown(char attribute((unused)) **argv) {
150 if(disorder_shutdown(getclient())) exit(EXIT_FAILURE);
460b9539 151}
152
0f55e905 153static void cf_reconfigure(char attribute((unused)) **argv) {
c00fce3a
RK
154 /* Re-check configuration for server */
155 if(config_read(1)) fatal(0, "cannot read configuration");
0f55e905 156 if(disorder_reconfigure(getclient())) exit(EXIT_FAILURE);
460b9539 157}
158
0f55e905
RK
159static void cf_rescan(char attribute((unused)) **argv) {
160 if(disorder_rescan(getclient())) exit(EXIT_FAILURE);
460b9539 161}
162
0f55e905 163static void cf_somequeue(int (*fn)(disorder_client *c,
460b9539 164 struct queue_entry **qp)) {
165 struct queue_entry *q;
166
0f55e905 167 if(fn(getclient(), &q)) exit(EXIT_FAILURE);
460b9539 168 while(q) {
169 print_queue_entry(q);
170 q = q->next;
171 }
172}
173
0f55e905
RK
174static void cf_recent(char attribute((unused)) **argv) {
175 cf_somequeue(disorder_recent);
460b9539 176}
177
0f55e905
RK
178static void cf_queue(char attribute((unused)) **argv) {
179 cf_somequeue(disorder_queue);
460b9539 180}
181
0f55e905 182static void cf_quack(char attribute((unused)) **argv) {
460b9539 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
0f55e905 194static void cf_somelist(char **argv,
460b9539 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;
0f55e905 205 if(fn(getclient(), argv[0], re, &vec, 0)) exit(EXIT_FAILURE);
460b9539 206 while(*vec)
207 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
208}
209
210static int isarg_regexp(const char *s) {
211 return s[0] == '~';
212}
213
0f55e905
RK
214static void cf_dirs(char **argv) {
215 cf_somelist(argv, disorder_directories);
460b9539 216}
217
0f55e905
RK
218static void cf_files(char **argv) {
219 cf_somelist(argv, disorder_files);
460b9539 220}
221
0f55e905
RK
222static void cf_allfiles(char **argv) {
223 cf_somelist(argv, disorder_allfiles);
460b9539 224}
225
0f55e905 226static void cf_get(char **argv) {
460b9539 227 char *value;
228
0f55e905 229 if(disorder_get(getclient(), argv[0], argv[1], &value)) exit(EXIT_FAILURE);
460b9539 230 xprintf("%s\n", nullcheck(utf82mb(value)));
231}
232
0f55e905 233static void cf_length(char **argv) {
460b9539 234 long length;
235
0f55e905 236 if(disorder_length(getclient(), argv[0], &length)) exit(EXIT_FAILURE);
460b9539 237 xprintf("%ld\n", length);
238}
239
0f55e905
RK
240static void cf_set(char **argv) {
241 if(disorder_set(getclient(), argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
460b9539 242}
243
0f55e905
RK
244static void cf_unset(char **argv) {
245 if(disorder_unset(getclient(), argv[0], argv[1])) exit(EXIT_FAILURE);
460b9539 246}
247
0f55e905 248static void cf_prefs(char **argv) {
460b9539 249 struct kvp *k;
250
0f55e905 251 if(disorder_prefs(getclient(), argv[0], &k)) exit(EXIT_FAILURE);
460b9539 252 for(; k; k = k->next)
253 xprintf("%s = %s\n",
254 nullcheck(utf82mb(k->name)), nullcheck(utf82mb(k->value)));
255}
256
0f55e905 257static void cf_search(char **argv) {
460b9539 258 char **results;
259 int nresults, n;
260
0f55e905 261 if(disorder_search(getclient(), *argv, &results, &nresults)) exit(EXIT_FAILURE);
460b9539 262 for(n = 0; n < nresults; ++n)
263 xprintf("%s\n", nullcheck(utf82mb(results[n])));
264}
265
0f55e905
RK
266static void cf_random_disable(char attribute((unused)) **argv) {
267 if(disorder_random_disable(getclient())) exit(EXIT_FAILURE);
460b9539 268}
269
0f55e905
RK
270static void cf_random_enable(char attribute((unused)) **argv) {
271 if(disorder_random_enable(getclient())) exit(EXIT_FAILURE);
460b9539 272}
273
0f55e905 274static void cf_stats(char attribute((unused)) **argv) {
460b9539 275 char **vec;
276
0f55e905 277 if(disorder_stats(getclient(), &vec, 0)) exit(EXIT_FAILURE);
460b9539 278 while(*vec)
279 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
280}
281
0f55e905 282static void cf_get_volume(char attribute((unused)) **argv) {
460b9539 283 int l, r;
284
0f55e905 285 if(disorder_get_volume(getclient(), &l, &r)) exit(EXIT_FAILURE);
460b9539 286 xprintf("%d %d\n", l, r);
287}
288
0f55e905
RK
289static void cf_set_volume(char **argv) {
290 if(disorder_set_volume(getclient(), atoi(argv[0]), atoi(argv[1]))) exit(EXIT_FAILURE);
460b9539 291}
292
0f55e905
RK
293static void cf_log(char attribute((unused)) **argv) {
294 if(disorder_log(getclient(), sink_stdio("stdout", stdout))) exit(EXIT_FAILURE);
460b9539 295}
296
0f55e905 297static void cf_move(char **argv) {
460b9539 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);
0f55e905 305 if(disorder_move(getclient(), argv[0], (int)n)) exit(EXIT_FAILURE);
460b9539 306}
307
0f55e905 308static void cf_part(char **argv) {
460b9539 309 char *s;
310
0f55e905 311 if(disorder_part(getclient(), &s, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
460b9539 312 xprintf("%s\n", nullcheck(utf82mb(s)));
313}
314
315static int isarg_filename(const char *s) {
316 return s[0] == '/';
317}
318
0f55e905
RK
319static void cf_authorize(char **argv) {
320 authorize(getclient(), argv[0], argv[1]);
460b9539 321}
322
0f55e905 323static void cf_resolve(char **argv) {
460b9539 324 char *track;
325
0f55e905 326 if(disorder_resolve(getclient(), &track, argv[0])) exit(EXIT_FAILURE);
460b9539 327 xprintf("%s\n", nullcheck(utf82mb(track)));
328}
329
0f55e905
RK
330static void cf_pause(char attribute((unused)) **argv) {
331 if(disorder_pause(getclient())) exit(EXIT_FAILURE);
460b9539 332}
333
0f55e905
RK
334static void cf_resume(char attribute((unused)) **argv) {
335 if(disorder_resume(getclient())) exit(EXIT_FAILURE);
460b9539 336}
337
0f55e905 338static void cf_tags(char attribute((unused)) **argv) {
460b9539 339 char **vec;
340
0f55e905 341 if(disorder_tags(getclient(), &vec, 0)) exit(EXIT_FAILURE);
460b9539 342 while(*vec)
343 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
344}
345
0f55e905 346static void cf_users(char attribute((unused)) **argv) {
c3be4f19
RK
347 char **vec;
348
0f55e905 349 if(disorder_users(getclient(), &vec, 0)) exit(EXIT_FAILURE);
c3be4f19
RK
350 while(*vec)
351 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
352}
353
0f55e905 354static void cf_get_global(char **argv) {
460b9539 355 char *value;
356
0f55e905 357 if(disorder_get_global(getclient(), argv[0], &value)) exit(EXIT_FAILURE);
460b9539 358 xprintf("%s\n", nullcheck(utf82mb(value)));
359}
360
0f55e905
RK
361static void cf_set_global(char **argv) {
362 if(disorder_set_global(getclient(), argv[0], argv[1])) exit(EXIT_FAILURE);
460b9539 363}
364
0f55e905
RK
365static void cf_unset_global(char **argv) {
366 if(disorder_unset_global(getclient(), argv[0])) exit(EXIT_FAILURE);
460b9539 367}
368
2a10b70b
RK
369static 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
0f55e905 379static void cf_new(char **argv) {
2a10b70b
RK
380 char **vec;
381
0f55e905 382 if(disorder_new_tracks(getclient(), &vec, 0, argv[0] ? atoi(argv[0]) : 0))
2a10b70b
RK
383 exit(EXIT_FAILURE);
384 while(*vec)
385 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
386}
387
0f55e905 388static void cf_rtp_address(char attribute((unused)) **argv) {
ca831831
RK
389 char *address, *port;
390
0f55e905 391 if(disorder_rtp_address(getclient(), &address, &port)) exit(EXIT_FAILURE);
ca831831
RK
392 xprintf("address: %s\nport: %s\n", address, port);
393}
394
0f55e905
RK
395static int isarg_rights(const char *arg) {
396 return strchr(arg, ',') || !parse_rights(arg, 0, 0);
397}
398
399static void cf_adduser(char **argv) {
400 if(disorder_adduser(getclient(), argv[0], argv[1], argv[2]))
f0feb22e
RK
401 exit(EXIT_FAILURE);
402}
403
0f55e905
RK
404static void cf_deluser(char **argv) {
405 if(disorder_deluser(getclient(), argv[0]))
a55c70c7
RK
406 exit(EXIT_FAILURE);
407}
408
0f55e905
RK
409static void cf_edituser(char **argv) {
410 if(disorder_edituser(getclient(), argv[0], argv[1], argv[2]))
eb5dc014
RK
411 exit(EXIT_FAILURE);
412}
413
0f55e905 414static void cf_userinfo(char **argv) {
a55c70c7
RK
415 char *s;
416
0f55e905 417 if(disorder_userinfo(getclient(), argv[0], argv[1], &s))
a55c70c7
RK
418 exit(EXIT_FAILURE);
419 xprintf("%s\n", nullcheck(utf82mb(s)));
420}
eb5dc014 421
0f55e905
RK
422static int isarg_option(const char *arg) {
423 return arg[0] == '-';
424}
425
426static int argvlen(char **argv) {
427 char **start = argv;
428
429 while(*argv)
430 ++argv;
431 return argv - start;
432}
433
434static 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
441static 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
452static 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 }
f1592969 464 if(online_registration && !config->mail_sender)
465 fatal(0, "you MUST set mail_sender if you want online registration");
0f55e905
RK
466 if(disorder_adduser(getclient(), "guest", "",
467 online_registration ? "read,register" : "read"))
468 exit(EXIT_FAILURE);
469}
470
460b9539 471static const struct command {
472 const char *name;
473 int min, max;
0f55e905 474 void (*fn)(char **);
460b9539 475 int (*isarg)(const char *);
476 const char *argstr, *desc;
477} commands[] = {
0f55e905 478 { "adduser", 2, 3, cf_adduser, isarg_rights, "USER PASSWORD [RIGHTS]",
f0feb22e 479 "Create a new user" },
460b9539 480 { "allfiles", 1, 2, cf_allfiles, isarg_regexp, "DIR [~REGEXP]",
481 "List all files and directories in DIR" },
0f55e905 482 { "authorize", 1, 2, cf_authorize, isarg_rights, "USER [RIGHTS]",
460b9539 483 "Authorize USER to connect to the server" },
a55c70c7
RK
484 { "deluser", 1, 1, cf_deluser, 0, "USER",
485 "Delete a user" },
460b9539 486 { "dirs", 1, 2, cf_dirs, isarg_regexp, "DIR [~REGEXP]",
487 "List directories in DIR" },
488 { "disable", 0, 0, cf_disable, 0, "",
489 "Disable play" },
490 { "disable-random", 0, 0, cf_random_disable, 0, "",
491 "Disable random play" },
a55c70c7 492 { "edituser", 3, 3, cf_edituser, 0, "USER PROPERTY VALUE",
eb5dc014 493 "Set a property of a user" },
460b9539 494 { "enable", 0, 0, cf_enable, 0, "",
495 "Enable play" },
496 { "enable-random", 0, 0, cf_random_enable, 0, "",
497 "Enable random play" },
498 { "files", 1, 2, cf_files, isarg_regexp, "DIR [~REGEXP]",
499 "List files in DIR" },
500 { "get", 2, 2, cf_get, 0, "TRACK NAME",
501 "Get a preference value" },
502 { "get-global", 1, 1, cf_get_global, 0, "NAME",
503 "Get a global preference value" },
504 { "get-volume", 0, 0, cf_get_volume, 0, "",
505 "Get the current volume" },
506 { "length", 1, 1, cf_length, 0, "TRACK",
507 "Get the length of TRACK in seconds" },
508 { "log", 0, 0, cf_log, 0, "",
509 "Copy event log to stdout" },
510 { "move", 2, 2, cf_move, 0, "TRACK DELTA",
511 "Move a track in the queue" },
2a10b70b
RK
512 { "new", 0, 1, cf_new, isarg_integer, "[MAX]",
513 "Get the most recently added MAX tracks" },
460b9539 514 { "part", 3, 3, cf_part, 0, "TRACK CONTEXT PART",
515 "Find a track name part" },
516 { "pause", 0, 0, cf_pause, 0, "",
517 "Pause the currently playing track" },
518 { "play", 1, INT_MAX, cf_play, isarg_filename, "TRACKS...",
519 "Add TRACKS to the end of the queue" },
520 { "playing", 0, 0, cf_playing, 0, "",
521 "Report the playing track" },
522 { "prefs", 1, 1, cf_prefs, 0, "TRACK",
523 "Display all the preferences for TRACK" },
524 { "quack", 0, 0, cf_quack, 0, 0, 0 },
525 { "queue", 0, 0, cf_queue, 0, "",
526 "Display the current queue" },
527 { "random-disable", 0, 0, cf_random_disable, 0, "",
528 "Disable random play" },
529 { "random-enable", 0, 0, cf_random_enable, 0, "",
530 "Enable random play" },
531 { "recent", 0, 0, cf_recent, 0, "",
532 "Display recently played track" },
533 { "reconfigure", 0, 0, cf_reconfigure, 0, "",
534 "Reconfigure the daemon" },
535 { "remove", 1, 1, cf_remove, 0, "TRACK",
536 "Remove a track from the queue" },
537 { "rescan", 0, 0, cf_rescan, 0, "",
538 "Rescan for new tracks" },
539 { "resolve", 1, 1, cf_resolve, 0, "TRACK",
540 "Resolve alias for TRACK" },
541 { "resume", 0, 0, cf_resume, 0, "",
542 "Resume after a pause" },
ca831831
RK
543 { "rtp-address", 0, 0, cf_rtp_address, 0, "",
544 "Report server's broadcast address" },
460b9539 545 { "scratch", 0, 0, cf_scratch, 0, "",
546 "Scratch the currently playing track" },
547 { "scratch-id", 1, 1, cf_scratch, 0, "ID",
548 "Scratch the currently playing track" },
549 { "search", 1, 1, cf_search, 0, "WORDS",
550 "Display tracks matching all the words" },
551 { "set", 3, 3, cf_set, 0, "TRACK NAME VALUE",
552 "Set a preference value" },
553 { "set-global", 2, 2, cf_set_global, 0, "NAME VALUE",
554 "Set a global preference value" },
555 { "set-volume", 2, 2, cf_set_volume, 0, "LEFT RIGHT",
556 "Set the volume" },
0f55e905
RK
557 { "setup-guest", 0, INT_MAX, cf_setup_guest, isarg_option, "[OPTIONS]",
558 "Create the guest login" },
460b9539 559 { "shutdown", 0, 0, cf_shutdown, 0, "",
560 "Shut down the daemon" },
561 { "stats", 0, 0, cf_stats, 0, "",
562 "Display server statistics" },
563 { "tags", 0, 0, cf_tags, 0, "",
564 "List known tags" },
565 { "unset", 2, 2, cf_unset, 0, "TRACK NAME",
566 "Unset a preference" },
567 { "unset-global", 1, 1, cf_unset_global, 0, "NAME",
568 "Unset a global preference" },
a55c70c7
RK
569 { "userinfo", 2, 2, cf_userinfo, 0, "USER PROPERTY",
570 "Get a property of as user" },
c3be4f19
RK
571 { "users", 0, 0, cf_users, 0, "",
572 "List all users" },
460b9539 573 { "version", 0, 0, cf_version, 0, "",
574 "Display the server version" },
575};
576
577static void help_commands(void) {
578 unsigned n, max = 0, l;
579
580 xprintf("Command summary:\n");
581 for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
582 if(!commands[n].desc) continue;
583 l = strlen(commands[n].name);
584 if(*commands[n].argstr)
585 l += strlen(commands[n].argstr) + 1;
586 if(l > max)
587 max = l;
588 }
589 for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
590 if(!commands[n].desc) continue;
591 l = strlen(commands[n].name);
592 if(*commands[n].argstr)
593 l += strlen(commands[n].argstr) + 1;
594 xprintf(" %s%s%s%*s %s\n", commands[n].name,
595 *commands[n].argstr ? " " : "",
596 commands[n].argstr,
597 max - l, "",
598 commands[n].desc);
599 }
600 xfclose(stdout);
601 exit(0);
602}
603
604int main(int argc, char **argv) {
af66d051 605 int n, i, j, local = 0;
460b9539 606 int status = 0;
607 struct vector args;
f0feb22e 608 const char *user = 0, *password = 0;
460b9539 609
320598d4 610 mem_init();
fe8f8518
RK
611 /* garbage-collect PCRE's memory */
612 pcre_malloc = xmalloc;
613 pcre_free = xfree;
460b9539 614 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
218b3b38 615 while((n = getopt_long(argc, argv, "+hVc:dHlNu:p:", options, 0)) >= 0) {
460b9539 616 switch(n) {
617 case 'h': help();
618 case 'H': help_commands();
3fbdc96d 619 case 'V': version("disorder");
460b9539 620 case 'c': configfile = optarg; break;
621 case 'd': debugging = 1; break;
af66d051 622 case 'l': local = 1; break;
63ad732f 623 case 'N': config_per_user = 0; break;
f0feb22e
RK
624 case 'u': user = optarg; break;
625 case 'p': password = optarg; break;
460b9539 626 default: fatal(0, "invalid option");
627 }
628 }
c00fce3a 629 if(config_read(0)) fatal(0, "cannot read configuration");
f0feb22e
RK
630 if(user) {
631 config->username = user;
632 config->password = 0;
633 }
634 if(password)
635 config->password = password;
af66d051 636 if(local)
637 config->connect.n = 0;
460b9539 638 n = optind;
0f55e905 639 optind = 1; /* for subsequent getopt calls */
460b9539 640 /* accumulate command args */
641 while(n < argc) {
642 if((i = TABLE_FIND(commands, struct command, name, argv[n])) < 0)
643 fatal(0, "unknown command '%s'", argv[n]);
644 if(n + commands[i].min >= argc)
645 fatal(0, "missing arguments to '%s'", argv[n]);
460b9539 646 vector_init(&args);
0f55e905
RK
647 /* Include the command name in the args, but at element -1, for
648 * the benefit of subcommand getopt calls */
649 vector_append(&args, argv[n]);
650 n++;
460b9539 651 for(j = 0; j < commands[i].min; ++j)
652 vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
653 for(; j < commands[i].max
654 && n + j < argc
655 && commands[i].isarg(argv[n + j]); ++j)
656 vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
657 vector_terminate(&args);
0f55e905 658 commands[i].fn(args.vec + 1);
460b9539 659 n += j;
660 }
0f55e905 661 if(client && disorder_close(client)) exit(EXIT_FAILURE);
460b9539 662 if(fclose(stdout) < 0) fatal(errno, "error closing stdout");
663 return status;
664}
665
666/*
667Local Variables:
668c-basic-offset:2
669comment-column:40
670End:
671*/