2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 Richard Kettlewell
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.
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.
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
24 #include <sys/types.h>
25 #include <sys/socket.h>
36 #include "configuration.h"
49 #include "authorize.h"
52 #include "dateparse.h"
53 #include "inputline.h"
55 static disorder_client
*client
;
57 static const struct option options
[] = {
58 { "help", no_argument
, 0, 'h' },
59 { "version", no_argument
, 0, 'V' },
60 { "config", required_argument
, 0, 'c' },
61 { "debug", no_argument
, 0, 'd' },
62 { "local", no_argument
, 0, 'l' },
63 { "no-per-user-config", no_argument
, 0, 'N' },
64 { "help-commands", no_argument
, 0, 'H' },
65 { "user", required_argument
, 0, 'u' },
66 { "password", required_argument
, 0, 'p' },
70 /* display usage message and terminate */
71 static void help(void) {
73 " disorder [OPTIONS] COMMAND ...\n"
75 " --help, -h Display usage message\n"
76 " --help-commands, -H List commands\n"
77 " --version, -V Display version number\n"
78 " --config PATH, -c PATH Set configuration file\n"
79 " --local, -l Force connection to local server\n"
80 " --debug, -d Turn on debugging\n");
85 static disorder_client
*getclient(void) {
87 if(!(client
= disorder_new(1))) exit(EXIT_FAILURE
);
88 if(disorder_connect(client
)) exit(EXIT_FAILURE
);
93 static void cf_version(char attribute((unused
)) **argv
) {
96 if(disorder_version(getclient(), &v
)) exit(EXIT_FAILURE
);
97 xprintf("%s\n", nullcheck(utf82mb(v
)));
100 static void print_queue_entry(const struct queue_entry
*q
) {
101 if(q
->track
) xprintf("track %s\n", nullcheck(utf82mb(q
->track
)));
102 if(q
->id
) xprintf(" id %s\n", nullcheck(utf82mb(q
->id
)));
103 if(q
->submitter
) xprintf(" submitted by %s at %s",
104 nullcheck(utf82mb(q
->submitter
)), ctime(&q
->when
));
105 if(q
->played
) xprintf(" played at %s", ctime(&q
->played
));
106 if(q
->state
== playing_started
107 || q
->state
== playing_paused
) xprintf(" %lds so far", q
->sofar
);
108 else if(q
->expected
) xprintf(" might start at %s", ctime(&q
->expected
));
109 if(q
->scratched
) xprintf(" scratched by %s\n",
110 nullcheck(utf82mb(q
->scratched
)));
111 else xprintf(" %s\n", playing_states
[q
->state
]);
112 if(q
->wstat
) xprintf(" %s\n", wstat(q
->wstat
));
115 static void cf_playing(char attribute((unused
)) **argv
) {
116 struct queue_entry
*q
;
118 if(disorder_playing(getclient(), &q
)) exit(EXIT_FAILURE
);
120 print_queue_entry(q
);
122 xprintf("nothing\n");
125 static void cf_play(char **argv
) {
127 if(disorder_play(getclient(), *argv
++)) exit(EXIT_FAILURE
);
130 static void cf_remove(char **argv
) {
131 if(disorder_remove(getclient(), argv
[0])) exit(EXIT_FAILURE
);
134 static void cf_disable(char attribute((unused
)) **argv
) {
135 if(disorder_disable(getclient())) exit(EXIT_FAILURE
);
138 static void cf_enable(char attribute((unused
)) **argv
) {
139 if(disorder_enable(getclient())) exit(EXIT_FAILURE
);
142 static void cf_scratch(char **argv
) {
143 if(disorder_scratch(getclient(), argv
[0])) exit(EXIT_FAILURE
);
146 static void cf_shutdown(char attribute((unused
)) **argv
) {
147 if(disorder_shutdown(getclient())) exit(EXIT_FAILURE
);
150 static void cf_reconfigure(char attribute((unused
)) **argv
) {
151 /* Re-check configuration for server */
152 if(config_read(1)) fatal(0, "cannot read configuration");
153 if(disorder_reconfigure(getclient())) exit(EXIT_FAILURE
);
156 static void cf_rescan(char attribute((unused
)) **argv
) {
157 if(disorder_rescan(getclient())) exit(EXIT_FAILURE
);
160 static void cf_somequeue(int (*fn
)(disorder_client
*c
,
161 struct queue_entry
**qp
)) {
162 struct queue_entry
*q
;
164 if(fn(getclient(), &q
)) exit(EXIT_FAILURE
);
166 print_queue_entry(q
);
171 static void cf_recent(char attribute((unused
)) **argv
) {
172 cf_somequeue(disorder_recent
);
175 static void cf_queue(char attribute((unused
)) **argv
) {
176 cf_somequeue(disorder_queue
);
179 static void cf_quack(char attribute((unused
)) **argv
) {
180 if(!strcasecmp(nl_langinfo(CODESET
), "utf-8")) {
181 #define TL "\xE2\x95\xAD"
182 #define TR "\xE2\x95\xAE"
183 #define BR "\xE2\x95\xAF"
184 #define BL "\xE2\x95\xB0"
185 #define H "\xE2\x94\x80"
186 #define V "\xE2\x94\x82"
187 #define T "\xE2\x94\xAC"
189 " "TL H H H H H H H H H H H H H H H H H H TR
"\n"
190 " "V
" Naath is a babe! "V
"\n"
191 " "BL H H H H H H H H H T H H H H H H H H BR
"\n"
195 "~~~~~~~~~~~~~~~~~~~~~~\n"
199 " .------------------.\n"
200 " | Naath is a babe! |\n"
201 " `---------+--------'\n"
205 "~~~~~~~~~~~~~~~~~~~~~~\n"
210 static void cf_somelist(char **argv
,
211 int (*fn
)(disorder_client
*c
,
212 const char *arg
, const char *re
,
213 char ***vecp
, int *nvecp
)) {
218 re
= xstrdup(argv
[1] + 1);
221 if(fn(getclient(), argv
[0], re
, &vec
, 0)) exit(EXIT_FAILURE
);
223 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
226 static int isarg_regexp(const char *s
) {
230 static void cf_dirs(char **argv
) {
231 cf_somelist(argv
, disorder_directories
);
234 static void cf_files(char **argv
) {
235 cf_somelist(argv
, disorder_files
);
238 static void cf_allfiles(char **argv
) {
239 cf_somelist(argv
, disorder_allfiles
);
242 static void cf_get(char **argv
) {
245 if(disorder_get(getclient(), argv
[0], argv
[1], &value
)) exit(EXIT_FAILURE
);
246 xprintf("%s\n", nullcheck(utf82mb(value
)));
249 static void cf_length(char **argv
) {
252 if(disorder_length(getclient(), argv
[0], &length
)) exit(EXIT_FAILURE
);
253 xprintf("%ld\n", length
);
256 static void cf_set(char **argv
) {
257 if(disorder_set(getclient(), argv
[0], argv
[1], argv
[2])) exit(EXIT_FAILURE
);
260 static void cf_unset(char **argv
) {
261 if(disorder_unset(getclient(), argv
[0], argv
[1])) exit(EXIT_FAILURE
);
264 static void cf_prefs(char **argv
) {
267 if(disorder_prefs(getclient(), argv
[0], &k
)) exit(EXIT_FAILURE
);
268 for(; k
; k
= k
->next
)
270 nullcheck(utf82mb(k
->name
)), nullcheck(utf82mb(k
->value
)));
273 static void cf_search(char **argv
) {
277 if(disorder_search(getclient(), *argv
, &results
, &nresults
)) exit(EXIT_FAILURE
);
278 for(n
= 0; n
< nresults
; ++n
)
279 xprintf("%s\n", nullcheck(utf82mb(results
[n
])));
282 static void cf_random_disable(char attribute((unused
)) **argv
) {
283 if(disorder_random_disable(getclient())) exit(EXIT_FAILURE
);
286 static void cf_random_enable(char attribute((unused
)) **argv
) {
287 if(disorder_random_enable(getclient())) exit(EXIT_FAILURE
);
290 static void cf_stats(char attribute((unused
)) **argv
) {
293 if(disorder_stats(getclient(), &vec
, 0)) exit(EXIT_FAILURE
);
295 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
298 static void cf_get_volume(char attribute((unused
)) **argv
) {
301 if(disorder_get_volume(getclient(), &l
, &r
)) exit(EXIT_FAILURE
);
302 xprintf("%d %d\n", l
, r
);
305 static void cf_set_volume(char **argv
) {
306 if(disorder_set_volume(getclient(), atoi(argv
[0]), atoi(argv
[1]))) exit(EXIT_FAILURE
);
309 static void cf_log(char attribute((unused
)) **argv
) {
310 if(disorder_log(getclient(), sink_stdio("stdout", stdout
))) exit(EXIT_FAILURE
);
313 static void cf_move(char **argv
) {
317 if((e
= xstrtol(&n
, argv
[1], 0, 10)))
318 fatal(e
, "cannot convert '%s'", argv
[1]);
319 if(n
> INT_MAX
|| n
< INT_MIN
)
320 fatal(e
, "%ld out of range", n
);
321 if(disorder_move(getclient(), argv
[0], (int)n
)) exit(EXIT_FAILURE
);
324 static void cf_part(char **argv
) {
327 if(disorder_part(getclient(), &s
, argv
[0], argv
[1], argv
[2])) exit(EXIT_FAILURE
);
328 xprintf("%s\n", nullcheck(utf82mb(s
)));
331 static int isarg_filename(const char *s
) {
335 static void cf_authorize(char **argv
) {
336 authorize(getclient(), argv
[0], argv
[1]);
339 static void cf_resolve(char **argv
) {
342 if(disorder_resolve(getclient(), &track
, argv
[0])) exit(EXIT_FAILURE
);
343 xprintf("%s\n", nullcheck(utf82mb(track
)));
346 static void cf_pause(char attribute((unused
)) **argv
) {
347 if(disorder_pause(getclient())) exit(EXIT_FAILURE
);
350 static void cf_resume(char attribute((unused
)) **argv
) {
351 if(disorder_resume(getclient())) exit(EXIT_FAILURE
);
354 static void cf_tags(char attribute((unused
)) **argv
) {
357 if(disorder_tags(getclient(), &vec
, 0)) exit(EXIT_FAILURE
);
359 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
362 static void cf_users(char attribute((unused
)) **argv
) {
365 if(disorder_users(getclient(), &vec
, 0)) exit(EXIT_FAILURE
);
367 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
370 static void cf_get_global(char **argv
) {
373 if(disorder_get_global(getclient(), argv
[0], &value
)) exit(EXIT_FAILURE
);
374 xprintf("%s\n", nullcheck(utf82mb(value
)));
377 static void cf_set_global(char **argv
) {
378 if(disorder_set_global(getclient(), argv
[0], argv
[1])) exit(EXIT_FAILURE
);
381 static void cf_unset_global(char **argv
) {
382 if(disorder_unset_global(getclient(), argv
[0])) exit(EXIT_FAILURE
);
385 static int isarg_integer(const char *s
) {
388 if(!isdigit((unsigned char)*s
))
395 static void cf_new(char **argv
) {
398 if(disorder_new_tracks(getclient(), &vec
, 0, argv
[0] ?
atoi(argv
[0]) : 0))
401 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
404 static void cf_rtp_address(char attribute((unused
)) **argv
) {
405 char *address
, *port
;
407 if(disorder_rtp_address(getclient(), &address
, &port
)) exit(EXIT_FAILURE
);
408 xprintf("address: %s\nport: %s\n", address
, port
);
411 static int isarg_rights(const char *arg
) {
412 return strchr(arg
, ',') || !parse_rights(arg
, 0, 0);
415 static void cf_adduser(char **argv
) {
416 if(disorder_adduser(getclient(), argv
[0], argv
[1], argv
[2]))
420 static void cf_deluser(char **argv
) {
421 if(disorder_deluser(getclient(), argv
[0]))
425 static void cf_edituser(char **argv
) {
426 if(disorder_edituser(getclient(), argv
[0], argv
[1], argv
[2]))
430 static void cf_userinfo(char **argv
) {
433 if(disorder_userinfo(getclient(), argv
[0], argv
[1], &s
))
435 xprintf("%s\n", nullcheck(utf82mb(s
)));
438 static int isarg_option(const char *arg
) {
439 return arg
[0] == '-';
442 static int argvlen(char **argv
) {
450 static const struct option setup_guest_options
[] = {
451 { "help", no_argument
, 0, 'h' },
452 { "online-registration", no_argument
, 0, 'r' },
453 { "no-online-registration", no_argument
, 0, 'R' },
457 static void help_setup_guest(void) {
459 " disorder setup-guest [OPTIONS]\n"
461 " --help, -h Display usage message\n"
462 " --online-registration Enable online registration (default)\n"
463 " --no-online-registration Disable online registration\n");
468 static void cf_setup_guest(char **argv
) {
469 int n
, online_registration
= 1;
471 while((n
= getopt_long(argvlen(argv
) + 1, argv
- 1,
472 "hrR", setup_guest_options
, 0)) >= 0) {
474 case 'h': help_setup_guest();
475 case 'r': online_registration
= 1; break;
476 case 'R': online_registration
= 0; break;
477 default: fatal(0, "invalid option");
480 if(online_registration
&& !config
->mail_sender
)
481 fatal(0, "you MUST set mail_sender if you want online registration");
482 if(disorder_adduser(getclient(), "guest", "",
483 online_registration ?
"read,register" : "read"))
487 struct scheduled_event
{
489 struct kvp
*actiondata
;
493 static int compare_event(const void *av
, const void *bv
) {
494 struct scheduled_event
*a
= (void *)av
, *b
= (void *)bv
;
496 /* Primary sort key is the trigger time */
497 if(a
->when
< b
->when
)
499 else if(a
->when
> b
->when
)
501 /* For events that go off at the same time just sort by ID */
502 return strcmp(a
->id
, b
->id
);
505 static void cf_schedule_list(char attribute((unused
)) **argv
) {
508 struct scheduled_event
*events
;
510 const char *action
, *key
, *value
, *priority
;
513 /* Get all known events */
514 if(disorder_schedule_list(getclient(), &ids
, &nids
))
516 events
= xcalloc(nids
, sizeof *events
);
517 for(n
= 0; n
< nids
; ++n
) {
518 events
[n
].id
= ids
[n
];
519 if(disorder_schedule_get(getclient(), ids
[n
], &events
[n
].actiondata
))
521 events
[n
].when
= atoll(kvp_get(events
[n
].actiondata
, "when"));
523 /* Sort by trigger time */
524 qsort(events
, nids
, sizeof *events
, compare_event
);
526 for(n
= 0; n
< nids
; ++n
) {
527 strftime(tb
, sizeof tb
, "%Y-%m-%d %H:%M:%S %Z", localtime(&events
[n
].when
));
528 action
= kvp_get(events
[n
].actiondata
, "action");
529 priority
= kvp_get(events
[n
].actiondata
, "priority");
530 if(!strcmp(priority
, "junk"))
532 else if(!strcmp(priority
, "normal"))
536 xprintf("%11s %-25s %c %-8s %s",
537 events
[n
].id
, tb
, prichar
, kvp_get(events
[n
].actiondata
, "who"),
539 if(!strcmp(action
, "play"))
541 nullcheck(utf82mb(kvp_get(events
[n
].actiondata
, "track"))));
542 else if(!strcmp(action
, "set-global")) {
543 key
= kvp_get(events
[n
].actiondata
, "key");
544 value
= kvp_get(events
[n
].actiondata
, "value");
547 nullcheck(utf82mb(key
)),
548 nullcheck(utf82mb(value
)));
551 nullcheck(utf82mb(key
)));
557 static void cf_schedule_del(char **argv
) {
558 if(disorder_schedule_del(getclient(), argv
[0]))
562 static void cf_schedule_play(char **argv
) {
563 if(disorder_schedule_add(getclient(),
571 static void cf_schedule_set_global(char **argv
) {
572 if(disorder_schedule_add(getclient(),
581 static void cf_schedule_unset_global(char **argv
) {
582 if(disorder_schedule_add(getclient(),
591 static void cf_playlists(char attribute((unused
)) **argv
) {
594 if(disorder_playlists(getclient(), &vec
, 0))
597 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
600 static void cf_playlist_del(char **argv
) {
601 if(disorder_playlist_delete(getclient(), argv
[0]))
605 static void cf_playlist_get(char **argv
) {
608 if(disorder_playlist_get(getclient(), argv
[0], &vec
, 0))
611 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
614 static void cf_playlist_set(char **argv
) {
621 // Read track list from file
622 if(!(input
= fopen(argv
[1], "r")))
623 fatal(errno
, "opening %s", argv
[1]);
626 // Read track list from standard input
631 while(inputline(tag
, input
, &l
, '\n')) {
637 fatal(errno
, "reading %s", tag
);
640 if(disorder_playlist_lock(getclient(), argv
[0])
641 || disorder_playlist_set(getclient(), argv
[0], v
->vec
, v
->nvec
)
642 || disorder_playlist_unlock(getclient()))
646 static const struct command
{
650 int (*isarg
)(const char *);
651 const char *argstr
, *desc
;
653 { "adduser", 2, 3, cf_adduser
, isarg_rights
, "USERNAME PASSWORD [RIGHTS]",
654 "Create a new user" },
655 { "allfiles", 1, 2, cf_allfiles
, isarg_regexp
, "DIR [~REGEXP]",
656 "List all files and directories in DIR" },
657 { "authorize", 1, 2, cf_authorize
, isarg_rights
, "USERNAME [RIGHTS]",
658 "Authorize user USERNAME to connect" },
659 { "deluser", 1, 1, cf_deluser
, 0, "USERNAME",
660 "Delete user USERNAME" },
661 { "dirs", 1, 2, cf_dirs
, isarg_regexp
, "DIR [~REGEXP]",
662 "List directories in DIR" },
663 { "disable", 0, 0, cf_disable
, 0, "",
665 { "disable-random", 0, 0, cf_random_disable
, 0, "",
666 "Disable random play" },
667 { "edituser", 3, 3, cf_edituser
, 0, "USERNAME PROPERTY VALUE",
668 "Set a property of user USERNAME" },
669 { "enable", 0, 0, cf_enable
, 0, "",
671 { "enable-random", 0, 0, cf_random_enable
, 0, "",
672 "Enable random play" },
673 { "files", 1, 2, cf_files
, isarg_regexp
, "DIR [~REGEXP]",
674 "List files in DIR" },
675 { "get", 2, 2, cf_get
, 0, "TRACK NAME",
676 "Get a preference value" },
677 { "get-global", 1, 1, cf_get_global
, 0, "NAME",
678 "Get a global preference value" },
679 { "get-volume", 0, 0, cf_get_volume
, 0, "",
680 "Get the current volume" },
681 { "length", 1, 1, cf_length
, 0, "TRACK",
682 "Get the length of TRACK in seconds" },
683 { "log", 0, 0, cf_log
, 0, "",
684 "Copy event log to stdout" },
685 { "move", 2, 2, cf_move
, 0, "TRACK DELTA",
686 "Move a track in the queue" },
687 { "new", 0, 1, cf_new
, isarg_integer
, "[MAX]",
688 "Get the most recently added MAX tracks" },
689 { "part", 3, 3, cf_part
, 0, "TRACK CONTEXT PART",
690 "Find a track name part" },
691 { "pause", 0, 0, cf_pause
, 0, "",
692 "Pause the currently playing track" },
693 { "play", 1, INT_MAX
, cf_play
, isarg_filename
, "TRACKS...",
694 "Add TRACKS to the end of the queue" },
695 { "playing", 0, 0, cf_playing
, 0, "",
696 "Report the playing track" },
697 { "playlist-del", 1, 1, cf_playlist_del
, 0, "PLAYLIST",
698 "Delete a playlist" },
699 { "playlist-get", 1, 1, cf_playlist_get
, 0, "PLAYLIST",
700 "Get the contents of a playlist" },
701 { "playlist-set", 1, 2, cf_playlist_set
, isarg_filename
, "PLAYLIST [PATH]",
702 "Set the contents of a playlist" },
703 { "playlists", 0, 0, cf_playlists
, 0, "",
705 { "prefs", 1, 1, cf_prefs
, 0, "TRACK",
706 "Display all the preferences for TRACK" },
707 { "quack", 0, 0, cf_quack
, 0, 0, 0 },
708 { "queue", 0, 0, cf_queue
, 0, "",
709 "Display the current queue" },
710 { "random-disable", 0, 0, cf_random_disable
, 0, "",
711 "Disable random play" },
712 { "random-enable", 0, 0, cf_random_enable
, 0, "",
713 "Enable random play" },
714 { "recent", 0, 0, cf_recent
, 0, "",
715 "Display recently played track" },
716 { "reconfigure", 0, 0, cf_reconfigure
, 0, "",
717 "Reconfigure the daemon" },
718 { "remove", 1, 1, cf_remove
, 0, "TRACK",
719 "Remove a track from the queue" },
720 { "rescan", 0, 0, cf_rescan
, 0, "",
721 "Rescan for new tracks" },
722 { "resolve", 1, 1, cf_resolve
, 0, "TRACK",
723 "Resolve alias for TRACK" },
724 { "resume", 0, 0, cf_resume
, 0, "",
725 "Resume after a pause" },
726 { "rtp-address", 0, 0, cf_rtp_address
, 0, "",
727 "Report server's broadcast address" },
728 { "schedule-del", 1, 1, cf_schedule_del
, 0, "EVENT",
729 "Delete a scheduled event" },
730 { "schedule-list", 0, 0, cf_schedule_list
, 0, "",
731 "List scheduled events" },
732 { "schedule-play", 3, 3, cf_schedule_play
, 0, "WHEN PRI TRACK",
733 "Play TRACK later" },
734 { "schedule-set-global", 4, 4, cf_schedule_set_global
, 0, "WHEN PRI NAME VAL",
735 "Set a global preference later" },
736 { "schedule-unset-global", 3, 3, cf_schedule_unset_global
, 0, "WHEN PRI NAME",
737 "Unset a global preference later" },
738 { "scratch", 0, 0, cf_scratch
, 0, "",
739 "Scratch the currently playing track" },
740 { "scratch-id", 1, 1, cf_scratch
, 0, "ID",
741 "Scratch the currently playing track" },
742 { "search", 1, 1, cf_search
, 0, "WORDS",
743 "Display tracks matching all the words" },
744 { "set", 3, 3, cf_set
, 0, "TRACK NAME VALUE",
745 "Set a preference value" },
746 { "set-global", 2, 2, cf_set_global
, 0, "NAME VALUE",
747 "Set a global preference value" },
748 { "set-volume", 2, 2, cf_set_volume
, 0, "LEFT RIGHT",
750 { "setup-guest", 0, INT_MAX
, cf_setup_guest
, isarg_option
, "[OPTIONS]",
751 "Create the guest login" },
752 { "shutdown", 0, 0, cf_shutdown
, 0, "",
753 "Shut down the daemon" },
754 { "stats", 0, 0, cf_stats
, 0, "",
755 "Display server statistics" },
756 { "tags", 0, 0, cf_tags
, 0, "",
758 { "unset", 2, 2, cf_unset
, 0, "TRACK NAME",
759 "Unset a preference" },
760 { "unset-global", 1, 1, cf_unset_global
, 0, "NAME",
761 "Unset a global preference" },
762 { "userinfo", 2, 2, cf_userinfo
, 0, "USERNAME PROPERTY",
763 "Get a property of a user" },
764 { "users", 0, 0, cf_users
, 0, "",
766 { "version", 0, 0, cf_version
, 0, "",
767 "Display the server version" },
770 static void help_commands(void) {
771 unsigned n
, max
= 0, l
;
773 xprintf("Command summary:\n");
774 for(n
= 0; n
< sizeof commands
/ sizeof *commands
; ++n
) {
775 if(!commands
[n
].desc
) continue;
776 l
= strlen(commands
[n
].name
);
777 if(*commands
[n
].argstr
)
778 l
+= strlen(commands
[n
].argstr
) + 1;
782 for(n
= 0; n
< sizeof commands
/ sizeof *commands
; ++n
) {
783 if(!commands
[n
].desc
) continue;
784 l
= strlen(commands
[n
].name
);
785 if(*commands
[n
].argstr
)
786 l
+= strlen(commands
[n
].argstr
) + 1;
787 xprintf(" %s%s%s%*s %s\n", commands
[n
].name
,
788 *commands
[n
].argstr ?
" " : "",
797 int main(int argc
, char **argv
) {
798 int n
, i
, j
, local
= 0;
801 const char *user
= 0, *password
= 0;
804 /* garbage-collect PCRE's memory */
805 pcre_malloc
= xmalloc
;
807 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
808 if(!setlocale(LC_TIME
, "")) fatal(errno
, "error calling setlocale");
809 while((n
= getopt_long(argc
, argv
, "+hVc:dHlNu:p:", options
, 0)) >= 0) {
812 case 'H': help_commands();
813 case 'V': version("disorder");
814 case 'c': configfile
= optarg
; break;
815 case 'd': debugging
= 1; break;
816 case 'l': local
= 1; break;
817 case 'N': config_per_user
= 0; break;
818 case 'u': user
= optarg
; break;
819 case 'p': password
= optarg
; break;
820 default: fatal(0, "invalid option");
823 if(config_read(0)) fatal(0, "cannot read configuration");
825 config
->username
= user
;
826 config
->password
= 0;
829 config
->password
= password
;
831 config
->connect
.n
= 0;
833 optind
= 1; /* for subsequent getopt calls */
834 /* accumulate command args */
836 if((i
= TABLE_FIND(commands
, name
, argv
[n
])) < 0)
837 fatal(0, "unknown command '%s'", argv
[n
]);
838 if(n
+ commands
[i
].min
>= argc
)
839 fatal(0, "missing arguments to '%s'", argv
[n
]);
841 /* Include the command name in the args, but at element -1, for
842 * the benefit of subcommand getopt calls */
843 vector_append(&args
, argv
[n
]);
845 for(j
= 0; j
< commands
[i
].min
; ++j
)
846 vector_append(&args
, nullcheck(mb2utf8(argv
[n
+ j
])));
847 for(; j
< commands
[i
].max
849 && commands
[i
].isarg(argv
[n
+ j
]); ++j
)
850 vector_append(&args
, nullcheck(mb2utf8(argv
[n
+ j
])));
851 vector_terminate(&args
);
852 commands
[i
].fn(args
.vec
+ 1);
855 if(client
&& disorder_close(client
)) exit(EXIT_FAILURE
);
856 if(fclose(stdout
) < 0) fatal(errno
, "error closing stdout");