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 3 of the License, or
8 * (at your option) any later version.
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.
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/>.
18 /** @file clients/disorder.c
19 * @brief Command-line client
25 #include <sys/types.h>
26 #include <sys/socket.h>
38 #include "configuration.h"
51 #include "authorize.h"
54 #include "dateparse.h"
56 #include "inputline.h"
58 static disorder_client
*client
;
60 static const struct option options
[] = {
61 { "help", no_argument
, 0, 'h' },
62 { "version", no_argument
, 0, 'V' },
63 { "config", required_argument
, 0, 'c' },
64 { "debug", no_argument
, 0, 'd' },
65 { "local", no_argument
, 0, 'l' },
66 { "no-per-user-config", no_argument
, 0, 'N' },
67 { "help-commands", no_argument
, 0, 'H' },
68 { "user", required_argument
, 0, 'u' },
69 { "password", required_argument
, 0, 'p' },
70 { "wait-for-root", no_argument
, 0, 'W' },
74 /* display usage message and terminate */
75 static void help(void) {
77 " disorder [OPTIONS] COMMAND ...\n"
79 " --help, -h Display usage message\n"
80 " --help-commands, -H List commands\n"
81 " --version, -V Display version number\n"
82 " --config PATH, -c PATH Set configuration file\n"
83 " --local, -l Force connection to local server\n"
84 " --debug, -d Turn on debugging\n");
89 static disorder_client
*getclient(void) {
91 if(!(client
= disorder_new(1))) exit(EXIT_FAILURE
);
92 if(disorder_connect(client
)) exit(EXIT_FAILURE
);
97 static void cf_version(char attribute((unused
)) **argv
) {
100 if(disorder_version(getclient(), &v
)) exit(EXIT_FAILURE
);
101 xprintf("%s\n", nullcheck(utf82mb(v
)));
104 static void print_queue_entry(const struct queue_entry
*q
) {
105 if(q
->track
) xprintf("track %s\n", nullcheck(utf82mb(q
->track
)));
106 if(q
->id
) xprintf(" id %s\n", nullcheck(utf82mb(q
->id
)));
110 case origin_scheduled
:
111 xprintf(" %s by %s at %s",
112 track_origins
[q
->origin
],
113 nullcheck(utf82mb(q
->submitter
)), ctime(&q
->when
));
118 if(q
->played
) xprintf(" played at %s", ctime(&q
->played
));
119 if(q
->state
== playing_started
120 || q
->state
== playing_paused
) xprintf(" %lds so far", q
->sofar
);
121 else if(q
->expected
) xprintf(" might start at %s", ctime(&q
->expected
));
122 if(q
->scratched
) xprintf(" scratched by %s\n",
123 nullcheck(utf82mb(q
->scratched
)));
124 else xprintf(" %s\n", playing_states
[q
->state
]);
125 if(q
->wstat
) xprintf(" %s\n", wstat(q
->wstat
));
128 static void cf_playing(char attribute((unused
)) **argv
) {
129 struct queue_entry
*q
;
131 if(disorder_playing(getclient(), &q
)) exit(EXIT_FAILURE
);
133 print_queue_entry(q
);
135 xprintf("nothing\n");
138 static void cf_play(char **argv
) {
140 if(disorder_play(getclient(), *argv
++)) exit(EXIT_FAILURE
);
143 static void cf_remove(char **argv
) {
144 if(disorder_remove(getclient(), argv
[0])) exit(EXIT_FAILURE
);
147 static void cf_disable(char attribute((unused
)) **argv
) {
148 if(disorder_disable(getclient())) exit(EXIT_FAILURE
);
151 static void cf_enable(char attribute((unused
)) **argv
) {
152 if(disorder_enable(getclient())) exit(EXIT_FAILURE
);
155 static void cf_scratch(char **argv
) {
156 if(disorder_scratch(getclient(), argv
[0])) exit(EXIT_FAILURE
);
159 static void cf_shutdown(char attribute((unused
)) **argv
) {
160 if(disorder_shutdown(getclient())) exit(EXIT_FAILURE
);
163 static void cf_reconfigure(char attribute((unused
)) **argv
) {
164 /* Re-check configuration for server */
165 if(config_read(1, NULL
)) fatal(0, "cannot read configuration");
166 if(disorder_reconfigure(getclient())) exit(EXIT_FAILURE
);
169 static void cf_rescan(char attribute((unused
)) **argv
) {
170 if(disorder_rescan(getclient())) exit(EXIT_FAILURE
);
173 static void cf_somequeue(int (*fn
)(disorder_client
*c
,
174 struct queue_entry
**qp
)) {
175 struct queue_entry
*q
;
177 if(fn(getclient(), &q
)) exit(EXIT_FAILURE
);
179 print_queue_entry(q
);
184 static void cf_recent(char attribute((unused
)) **argv
) {
185 cf_somequeue(disorder_recent
);
188 static void cf_queue(char attribute((unused
)) **argv
) {
189 cf_somequeue(disorder_queue
);
192 static void cf_quack(char attribute((unused
)) **argv
) {
193 if(!strcasecmp(nl_langinfo(CODESET
), "utf-8")) {
194 #define TL "\xE2\x95\xAD"
195 #define TR "\xE2\x95\xAE"
196 #define BR "\xE2\x95\xAF"
197 #define BL "\xE2\x95\xB0"
198 #define H "\xE2\x94\x80"
199 #define V "\xE2\x94\x82"
200 #define T "\xE2\x94\xAC"
202 " "TL H H H H H H H H H H H H H H H H H H TR
"\n"
203 " "V
" Naath is a babe! "V
"\n"
204 " "BL H H H H H H H H H T H H H H H H H H BR
"\n"
208 "~~~~~~~~~~~~~~~~~~~~~~\n"
212 " .------------------.\n"
213 " | Naath is a babe! |\n"
214 " `---------+--------'\n"
218 "~~~~~~~~~~~~~~~~~~~~~~\n"
223 static void cf_somelist(char **argv
,
224 int (*fn
)(disorder_client
*c
,
225 const char *arg
, const char *re
,
226 char ***vecp
, int *nvecp
)) {
231 re
= xstrdup(argv
[1] + 1);
234 if(fn(getclient(), argv
[0], re
, &vec
, 0)) exit(EXIT_FAILURE
);
236 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
239 static int isarg_regexp(const char *s
) {
243 static void cf_dirs(char **argv
) {
244 cf_somelist(argv
, disorder_directories
);
247 static void cf_files(char **argv
) {
248 cf_somelist(argv
, disorder_files
);
251 static void cf_allfiles(char **argv
) {
252 cf_somelist(argv
, disorder_allfiles
);
255 static void cf_get(char **argv
) {
258 if(disorder_get(getclient(), argv
[0], argv
[1], &value
)) exit(EXIT_FAILURE
);
259 xprintf("%s\n", nullcheck(utf82mb(value
)));
262 static void cf_length(char **argv
) {
265 if(disorder_length(getclient(), argv
[0], &length
)) exit(EXIT_FAILURE
);
266 xprintf("%ld\n", length
);
269 static void cf_set(char **argv
) {
270 if(disorder_set(getclient(), argv
[0], argv
[1], argv
[2])) exit(EXIT_FAILURE
);
273 static void cf_unset(char **argv
) {
274 if(disorder_unset(getclient(), argv
[0], argv
[1])) exit(EXIT_FAILURE
);
277 static void cf_prefs(char **argv
) {
280 if(disorder_prefs(getclient(), argv
[0], &k
)) exit(EXIT_FAILURE
);
281 for(; k
; k
= k
->next
)
283 nullcheck(utf82mb(k
->name
)), nullcheck(utf82mb(k
->value
)));
286 static void cf_search(char **argv
) {
290 if(disorder_search(getclient(), *argv
, &results
, &nresults
)) exit(EXIT_FAILURE
);
291 for(n
= 0; n
< nresults
; ++n
)
292 xprintf("%s\n", nullcheck(utf82mb(results
[n
])));
295 static void cf_random_disable(char attribute((unused
)) **argv
) {
296 if(disorder_random_disable(getclient())) exit(EXIT_FAILURE
);
299 static void cf_random_enable(char attribute((unused
)) **argv
) {
300 if(disorder_random_enable(getclient())) exit(EXIT_FAILURE
);
303 static void cf_stats(char attribute((unused
)) **argv
) {
306 if(disorder_stats(getclient(), &vec
, 0)) exit(EXIT_FAILURE
);
308 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
311 static void cf_get_volume(char attribute((unused
)) **argv
) {
314 if(disorder_get_volume(getclient(), &l
, &r
)) exit(EXIT_FAILURE
);
315 xprintf("%d %d\n", l
, r
);
318 static void cf_set_volume(char **argv
) {
319 if(disorder_set_volume(getclient(), atoi(argv
[0]), atoi(argv
[1]))) exit(EXIT_FAILURE
);
322 static void cf_log(char attribute((unused
)) **argv
) {
323 if(disorder_log(getclient(), sink_stdio("stdout", stdout
))) exit(EXIT_FAILURE
);
326 static void cf_move(char **argv
) {
330 if((e
= xstrtol(&n
, argv
[1], 0, 10)))
331 fatal(e
, "cannot convert '%s'", argv
[1]);
332 if(n
> INT_MAX
|| n
< INT_MIN
)
333 fatal(e
, "%ld out of range", n
);
334 if(disorder_move(getclient(), argv
[0], (int)n
)) exit(EXIT_FAILURE
);
337 static void cf_part(char **argv
) {
340 if(disorder_part(getclient(), &s
, argv
[0], argv
[1], argv
[2])) exit(EXIT_FAILURE
);
341 xprintf("%s\n", nullcheck(utf82mb(s
)));
344 static int isarg_filename(const char *s
) {
348 static void cf_authorize(char **argv
) {
349 authorize(getclient(), argv
[0], argv
[1]);
352 static void cf_resolve(char **argv
) {
355 if(disorder_resolve(getclient(), &track
, argv
[0])) exit(EXIT_FAILURE
);
356 xprintf("%s\n", nullcheck(utf82mb(track
)));
359 static void cf_pause(char attribute((unused
)) **argv
) {
360 if(disorder_pause(getclient())) exit(EXIT_FAILURE
);
363 static void cf_resume(char attribute((unused
)) **argv
) {
364 if(disorder_resume(getclient())) exit(EXIT_FAILURE
);
367 static void cf_tags(char attribute((unused
)) **argv
) {
370 if(disorder_tags(getclient(), &vec
, 0)) exit(EXIT_FAILURE
);
372 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
375 static void cf_users(char attribute((unused
)) **argv
) {
378 if(disorder_users(getclient(), &vec
, 0)) exit(EXIT_FAILURE
);
380 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
383 static void cf_get_global(char **argv
) {
386 if(disorder_get_global(getclient(), argv
[0], &value
)) exit(EXIT_FAILURE
);
387 xprintf("%s\n", nullcheck(utf82mb(value
)));
390 static void cf_set_global(char **argv
) {
391 if(disorder_set_global(getclient(), argv
[0], argv
[1])) exit(EXIT_FAILURE
);
394 static void cf_unset_global(char **argv
) {
395 if(disorder_unset_global(getclient(), argv
[0])) exit(EXIT_FAILURE
);
398 static int isarg_integer(const char *s
) {
401 if(!isdigit((unsigned char)*s
))
408 static void cf_new(char **argv
) {
411 if(disorder_new_tracks(getclient(), &vec
, 0, argv
[0] ?
atoi(argv
[0]) : 0))
414 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
417 static void cf_rtp_address(char attribute((unused
)) **argv
) {
418 char *address
, *port
;
420 if(disorder_rtp_address(getclient(), &address
, &port
)) exit(EXIT_FAILURE
);
421 xprintf("address: %s\nport: %s\n", address
, port
);
424 static int isarg_rights(const char *arg
) {
425 return strchr(arg
, ',') || !parse_rights(arg
, 0, 0);
428 static void cf_adduser(char **argv
) {
429 if(disorder_adduser(getclient(), argv
[0], argv
[1], argv
[2]))
433 static void cf_deluser(char **argv
) {
434 if(disorder_deluser(getclient(), argv
[0]))
438 static void cf_edituser(char **argv
) {
439 if(disorder_edituser(getclient(), argv
[0], argv
[1], argv
[2]))
443 static void cf_userinfo(char **argv
) {
446 if(disorder_userinfo(getclient(), argv
[0], argv
[1], &s
))
448 xprintf("%s\n", nullcheck(utf82mb(s
)));
451 static int isarg_option(const char *arg
) {
452 return arg
[0] == '-';
455 static int argvlen(char **argv
) {
463 static const struct option setup_guest_options
[] = {
464 { "help", no_argument
, 0, 'h' },
465 { "online-registration", no_argument
, 0, 'r' },
466 { "no-online-registration", no_argument
, 0, 'R' },
470 static void help_setup_guest(void) {
472 " disorder setup-guest [OPTIONS]\n"
474 " --help, -h Display usage message\n"
475 " --online-registration Enable online registration (default)\n"
476 " --no-online-registration Disable online registration\n");
481 static void cf_setup_guest(char **argv
) {
482 int n
, online_registration
= 1;
484 while((n
= getopt_long(argvlen(argv
) + 1, argv
- 1,
485 "hrR", setup_guest_options
, 0)) >= 0) {
487 case 'h': help_setup_guest();
488 case 'r': online_registration
= 1; break;
489 case 'R': online_registration
= 0; break;
490 default: fatal(0, "invalid option");
493 if(online_registration
&& !config
->mail_sender
)
494 fatal(0, "you MUST set mail_sender if you want online registration");
495 if(disorder_adduser(getclient(), "guest", "",
496 online_registration ?
"read,register" : "read"))
500 struct scheduled_event
{
502 struct kvp
*actiondata
;
506 static int compare_event(const void *av
, const void *bv
) {
507 struct scheduled_event
*a
= (void *)av
, *b
= (void *)bv
;
509 /* Primary sort key is the trigger time */
510 if(a
->when
< b
->when
)
512 else if(a
->when
> b
->when
)
514 /* For events that go off at the same time just sort by ID */
515 return strcmp(a
->id
, b
->id
);
518 static void cf_schedule_list(char attribute((unused
)) **argv
) {
521 struct scheduled_event
*events
;
523 const char *action
, *key
, *value
, *priority
;
526 /* Get all known events */
527 if(disorder_schedule_list(getclient(), &ids
, &nids
))
529 events
= xcalloc(nids
, sizeof *events
);
530 for(n
= 0; n
< nids
; ++n
) {
531 events
[n
].id
= ids
[n
];
532 if(disorder_schedule_get(getclient(), ids
[n
], &events
[n
].actiondata
))
534 events
[n
].when
= atoll(kvp_get(events
[n
].actiondata
, "when"));
536 /* Sort by trigger time */
537 qsort(events
, nids
, sizeof *events
, compare_event
);
539 for(n
= 0; n
< nids
; ++n
) {
540 strftime(tb
, sizeof tb
, "%Y-%m-%d %H:%M:%S %Z", localtime(&events
[n
].when
));
541 action
= kvp_get(events
[n
].actiondata
, "action");
542 priority
= kvp_get(events
[n
].actiondata
, "priority");
543 if(!strcmp(priority
, "junk"))
545 else if(!strcmp(priority
, "normal"))
549 xprintf("%11s %-25s %c %-8s %s",
550 events
[n
].id
, tb
, prichar
, kvp_get(events
[n
].actiondata
, "who"),
552 if(!strcmp(action
, "play"))
554 nullcheck(utf82mb(kvp_get(events
[n
].actiondata
, "track"))));
555 else if(!strcmp(action
, "set-global")) {
556 key
= kvp_get(events
[n
].actiondata
, "key");
557 value
= kvp_get(events
[n
].actiondata
, "value");
560 nullcheck(utf82mb(key
)),
561 nullcheck(utf82mb(value
)));
564 nullcheck(utf82mb(key
)));
570 static void cf_schedule_del(char **argv
) {
571 if(disorder_schedule_del(getclient(), argv
[0]))
575 static void cf_schedule_play(char **argv
) {
576 if(disorder_schedule_add(getclient(),
584 static void cf_schedule_set_global(char **argv
) {
585 if(disorder_schedule_add(getclient(),
594 static void cf_schedule_unset_global(char **argv
) {
595 if(disorder_schedule_add(getclient(),
604 static void cf_adopt(char **argv
) {
605 if(disorder_adopt(getclient(), argv
[0]))
609 static void cf_playlists(char attribute((unused
)) **argv
) {
612 if(disorder_playlists(getclient(), &vec
, 0))
615 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
618 static void cf_playlist_del(char **argv
) {
619 if(disorder_playlist_delete(getclient(), argv
[0]))
623 static void cf_playlist_get(char **argv
) {
626 if(disorder_playlist_get(getclient(), argv
[0], &vec
, 0))
629 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
632 static void cf_playlist_set(char **argv
) {
639 // Read track list from file
640 if(!(input
= fopen(argv
[1], "r")))
641 fatal(errno
, "opening %s", argv
[1]);
644 // Read track list from standard input
649 while(!inputline(tag
, input
, &l
, '\n')) {
655 fatal(errno
, "reading %s", tag
);
658 if(disorder_playlist_lock(getclient(), argv
[0])
659 || disorder_playlist_set(getclient(), argv
[0], v
->vec
, v
->nvec
)
660 || disorder_playlist_unlock(getclient()))
664 static const struct command
{
668 int (*isarg
)(const char *);
669 const char *argstr
, *desc
;
671 { "adduser", 2, 3, cf_adduser
, isarg_rights
, "USERNAME PASSWORD [RIGHTS]",
672 "Create a new user" },
673 { "adopt", 1, 1, cf_adopt
, 0, "ID",
674 "Adopt a randomly picked track" },
675 { "allfiles", 1, 2, cf_allfiles
, isarg_regexp
, "DIR [~REGEXP]",
676 "List all files and directories in DIR" },
677 { "authorize", 1, 2, cf_authorize
, isarg_rights
, "USERNAME [RIGHTS]",
678 "Authorize user USERNAME to connect" },
679 { "deluser", 1, 1, cf_deluser
, 0, "USERNAME",
680 "Delete user USERNAME" },
681 { "dirs", 1, 2, cf_dirs
, isarg_regexp
, "DIR [~REGEXP]",
682 "List directories in DIR" },
683 { "disable", 0, 0, cf_disable
, 0, "",
685 { "disable-random", 0, 0, cf_random_disable
, 0, "",
686 "Disable random play" },
687 { "edituser", 3, 3, cf_edituser
, 0, "USERNAME PROPERTY VALUE",
688 "Set a property of user USERNAME" },
689 { "enable", 0, 0, cf_enable
, 0, "",
691 { "enable-random", 0, 0, cf_random_enable
, 0, "",
692 "Enable random play" },
693 { "files", 1, 2, cf_files
, isarg_regexp
, "DIR [~REGEXP]",
694 "List files in DIR" },
695 { "get", 2, 2, cf_get
, 0, "TRACK NAME",
696 "Get a preference value" },
697 { "get-global", 1, 1, cf_get_global
, 0, "NAME",
698 "Get a global preference value" },
699 { "get-volume", 0, 0, cf_get_volume
, 0, "",
700 "Get the current volume" },
701 { "length", 1, 1, cf_length
, 0, "TRACK",
702 "Get the length of TRACK in seconds" },
703 { "log", 0, 0, cf_log
, 0, "",
704 "Copy event log to stdout" },
705 { "move", 2, 2, cf_move
, 0, "TRACK DELTA",
706 "Move a track in the queue" },
707 { "new", 0, 1, cf_new
, isarg_integer
, "[MAX]",
708 "Get the most recently added MAX tracks" },
709 { "part", 3, 3, cf_part
, 0, "TRACK CONTEXT PART",
710 "Find a track name part" },
711 { "pause", 0, 0, cf_pause
, 0, "",
712 "Pause the currently playing track" },
713 { "play", 1, INT_MAX
, cf_play
, isarg_filename
, "TRACKS...",
714 "Add TRACKS to the end of the queue" },
715 { "playing", 0, 0, cf_playing
, 0, "",
716 "Report the playing track" },
717 { "playlist-del", 1, 1, cf_playlist_del
, 0, "PLAYLIST",
718 "Delete a playlist" },
719 { "playlist-get", 1, 1, cf_playlist_get
, 0, "PLAYLIST",
720 "Get the contents of a playlist" },
721 { "playlist-set", 1, 2, cf_playlist_set
, isarg_filename
, "PLAYLIST [PATH]",
722 "Set the contents of a playlist" },
723 { "playlists", 0, 0, cf_playlists
, 0, "",
725 { "prefs", 1, 1, cf_prefs
, 0, "TRACK",
726 "Display all the preferences for TRACK" },
727 { "quack", 0, 0, cf_quack
, 0, 0, 0 },
728 { "queue", 0, 0, cf_queue
, 0, "",
729 "Display the current queue" },
730 { "random-disable", 0, 0, cf_random_disable
, 0, "",
731 "Disable random play" },
732 { "random-enable", 0, 0, cf_random_enable
, 0, "",
733 "Enable random play" },
734 { "recent", 0, 0, cf_recent
, 0, "",
735 "Display recently played track" },
736 { "reconfigure", 0, 0, cf_reconfigure
, 0, "",
737 "Reconfigure the daemon" },
738 { "remove", 1, 1, cf_remove
, 0, "TRACK",
739 "Remove a track from the queue" },
740 { "rescan", 0, 0, cf_rescan
, 0, "",
741 "Rescan for new tracks" },
742 { "resolve", 1, 1, cf_resolve
, 0, "TRACK",
743 "Resolve alias for TRACK" },
744 { "resume", 0, 0, cf_resume
, 0, "",
745 "Resume after a pause" },
746 { "rtp-address", 0, 0, cf_rtp_address
, 0, "",
747 "Report server's broadcast address" },
748 { "schedule-del", 1, 1, cf_schedule_del
, 0, "EVENT",
749 "Delete a scheduled event" },
750 { "schedule-list", 0, 0, cf_schedule_list
, 0, "",
751 "List scheduled events" },
752 { "schedule-play", 3, 3, cf_schedule_play
, 0, "WHEN PRI TRACK",
753 "Play TRACK later" },
754 { "schedule-set-global", 4, 4, cf_schedule_set_global
, 0, "WHEN PRI NAME VAL",
755 "Set a global preference later" },
756 { "schedule-unset-global", 3, 3, cf_schedule_unset_global
, 0, "WHEN PRI NAME",
757 "Unset a global preference later" },
758 { "scratch", 0, 0, cf_scratch
, 0, "",
759 "Scratch the currently playing track" },
760 { "scratch-id", 1, 1, cf_scratch
, 0, "ID",
761 "Scratch the currently playing track" },
762 { "search", 1, 1, cf_search
, 0, "WORDS",
763 "Display tracks matching all the words" },
764 { "set", 3, 3, cf_set
, 0, "TRACK NAME VALUE",
765 "Set a preference value" },
766 { "set-global", 2, 2, cf_set_global
, 0, "NAME VALUE",
767 "Set a global preference value" },
768 { "set-volume", 2, 2, cf_set_volume
, 0, "LEFT RIGHT",
770 { "setup-guest", 0, INT_MAX
, cf_setup_guest
, isarg_option
, "[OPTIONS]",
771 "Create the guest login" },
772 { "shutdown", 0, 0, cf_shutdown
, 0, "",
773 "Shut down the daemon" },
774 { "stats", 0, 0, cf_stats
, 0, "",
775 "Display server statistics" },
776 { "tags", 0, 0, cf_tags
, 0, "",
778 { "unset", 2, 2, cf_unset
, 0, "TRACK NAME",
779 "Unset a preference" },
780 { "unset-global", 1, 1, cf_unset_global
, 0, "NAME",
781 "Unset a global preference" },
782 { "userinfo", 2, 2, cf_userinfo
, 0, "USERNAME PROPERTY",
783 "Get a property of a user" },
784 { "users", 0, 0, cf_users
, 0, "",
786 { "version", 0, 0, cf_version
, 0, "",
787 "Display the server version" },
790 static void help_commands(void) {
791 unsigned n
, max
= 0, l
;
793 xprintf("Command summary:\n");
794 for(n
= 0; n
< sizeof commands
/ sizeof *commands
; ++n
) {
795 if(!commands
[n
].desc
) continue;
796 l
= strlen(commands
[n
].name
);
797 if(*commands
[n
].argstr
)
798 l
+= strlen(commands
[n
].argstr
) + 1;
802 for(n
= 0; n
< sizeof commands
/ sizeof *commands
; ++n
) {
803 if(!commands
[n
].desc
) continue;
804 l
= strlen(commands
[n
].name
);
805 if(*commands
[n
].argstr
)
806 l
+= strlen(commands
[n
].argstr
) + 1;
807 xprintf(" %s%s%s%*s %s\n", commands
[n
].name
,
808 *commands
[n
].argstr ?
" " : "",
817 static void wait_for_root(void) {
818 const char *password
;
820 while(!trackdb_readable()) {
821 info("waiting for trackdb...");
824 trackdb_init(TRACKDB_NO_RECOVER
|TRACKDB_NO_UPGRADE
);
826 trackdb_open(TRACKDB_READ_ONLY
);
827 password
= trackdb_get_password("root");
831 info("waiting for root user to be created...");
837 int main(int argc
, char **argv
) {
838 int n
, i
, j
, local
= 0, wfr
= 0;
841 const char *user
= 0, *password
= 0;
844 /* garbage-collect PCRE's memory */
845 pcre_malloc
= xmalloc
;
847 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
848 if(!setlocale(LC_TIME
, "")) fatal(errno
, "error calling setlocale");
849 while((n
= getopt_long(argc
, argv
, "+hVc:dHlNu:p:W", options
, 0)) >= 0) {
852 case 'H': help_commands();
853 case 'V': version("disorder");
854 case 'c': configfile
= optarg
; break;
855 case 'd': debugging
= 1; break;
856 case 'l': local
= 1; break;
857 case 'N': config_per_user
= 0; break;
858 case 'u': user
= optarg
; break;
859 case 'p': password
= optarg
; break;
860 case 'W': wfr
= 1; break;
861 default: fatal(0, "invalid option");
864 if(config_read(0, NULL
)) fatal(0, "cannot read configuration");
866 config
->username
= user
;
867 config
->password
= 0;
870 config
->password
= password
;
872 config
->connect
.af
= -1;
876 optind
= 1; /* for subsequent getopt calls */
877 /* gcrypt initialization */
878 if(!gcry_check_version(NULL
))
879 disorder_fatal(0, "gcry_check_version failed");
880 gcry_control(GCRYCTL_INIT_SECMEM
, 0);
881 gcry_control (GCRYCTL_INITIALIZATION_FINISHED
, 0);
882 /* accumulate command args */
884 if((i
= TABLE_FIND(commands
, name
, argv
[n
])) < 0)
885 fatal(0, "unknown command '%s'", argv
[n
]);
886 if(n
+ commands
[i
].min
>= argc
)
887 fatal(0, "missing arguments to '%s'", argv
[n
]);
889 /* Include the command name in the args, but at element -1, for
890 * the benefit of subcommand getopt calls */
891 vector_append(&args
, argv
[n
]);
893 for(j
= 0; j
< commands
[i
].min
; ++j
)
894 vector_append(&args
, nullcheck(mb2utf8(argv
[n
+ j
])));
895 for(; j
< commands
[i
].max
897 && commands
[i
].isarg(argv
[n
+ j
]); ++j
)
898 vector_append(&args
, nullcheck(mb2utf8(argv
[n
+ j
])));
899 vector_terminate(&args
);
900 commands
[i
].fn(args
.vec
+ 1);
903 if(client
&& disorder_close(client
)) exit(EXIT_FAILURE
);
904 if(fclose(stdout
) < 0) fatal(errno
, "error closing stdout");