2 * (c) 2009-2011 by Art Cancro and citadel.org
3 * This program is released under the terms of the GNU General Public License v3.
9 int cmd_quit(int sock, char *cmdbuf) {
15 * Commands understood by ctdlsh
19 ctdlsh_cmdfunc_t *func;
23 COMMAND commands[] = {
24 { "?", cmd_help, "Display this message" },
25 { "help", cmd_help, "Display this message" },
26 { "quit", cmd_quit, "Quit using ctdlsh" },
27 { "exit", cmd_quit, "Quit using ctdlsh" },
28 { "date", cmd_datetime, "Print the server's date and time" },
29 { "time", cmd_datetime, "Print the server's date and time" },
30 { "passwd", cmd_passwd, "Set or change an account password" },
31 { "shutdown", cmd_shutdown, "Shut down the Citadel server" },
36 int cmd_help(int sock, char *cmdbuf) {
39 for (i=0; commands[i].func != NULL; ++i) {
40 printf("%-10s %s\n", commands[i].name, commands[i].doc);
45 /* Auto-completer function */
46 char *command_generator(const char *text, int state) {
47 static int list_index;
56 while (name = commands[list_index].name) {
59 if (!strncmp(name, text, len)) {
68 /* Auto-completer function */
69 char **ctdlsh_completion(const char *text, int start, int end) {
70 char **matches = (char **) NULL;
73 matches = rl_completion_matches(text, command_generator);
76 rl_bind_key('\t', rl_abort);
84 void do_main_loop(int server_socket) {
88 char server_reply[1024];
94 /* Do an INFO command and learn the hostname for the prompt */
95 sock_puts(server_socket, "INFO");
96 sock_getln(server_socket, buf, sizeof buf);
99 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
101 sprintf(prompt, "\n%s> ", buf);
107 /* Tell libreadline how we will help with auto-completion of commands */
108 rl_attempted_completion_function = ctdlsh_completion;
110 /* Here we go ... main command loop */
111 while ((ret != cmdret_exit) && (cmd = readline(prompt))) {
113 if ((cmd) && (*cmd)) {
116 for (i=0; commands[i].func != NULL; ++i) {
117 if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) {
118 ret = (*commands[i].func) (server_socket, cmd);
128 int main(int argc, char **argv)
130 int server_socket = 0;
133 char *ctdldir = CTDLDIR;
135 printf("\nCitadel administration shell v" PACKAGE_VERSION "\n");
136 printf("(c) 2009-2011 citadel.org GPLv3\n");
139 while ((c = getopt (argc, argv, "h:")) != -1) {
146 fprintf(stderr, "Option -%c requires an argument\n", optopt);
149 fprintf(stderr, "Unknown option '-%c'\n", optopt);
150 fprintf(stderr, "usage: %s [-h citadel_dir]\n", argv[0]);
156 printf("Trying %s...\n", ctdldir);
157 sprintf(buf, "%s/citadel-admin.socket", ctdldir);
158 server_socket = uds_connectsock(buf);
159 if (server_socket < 0) {
163 sock_getln(server_socket, buf, sizeof buf);
165 do_main_loop(server_socket);
168 sock_puts(server_socket, "QUIT");
169 sock_getln(server_socket, buf, sizeof buf);
171 close(server_socket);