X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=ctdlsh%2Fmain.c;h=69d7f777ba1c0ff6803494f475f1fe5d51132419;hb=aae29a7692b6df8429a1d81b1f33b43f703e78dd;hp=4e4009898a1d9d867b02628a8cdae56aac80e711;hpb=81ba4a49cd384ff484f6006fb508fc900d33a1af;p=citadel.git diff --git a/ctdlsh/main.c b/ctdlsh/main.c index 4e4009898..69d7f777b 100644 --- a/ctdlsh/main.c +++ b/ctdlsh/main.c @@ -1,16 +1,12 @@ /* - * (c) 2009-2012 by Art Cancro and citadel.org - * This program is released under the terms of the GNU General Public License v3. + * (c) 2009-2019 by Art Cancro and citadel.org + * This program is open source. It runs great on the Linux operating system. + * It's released under the General Public License (GPL) version 3. */ #include "ctdlsh.h" -int cmd_quit(int sock, char *cmdbuf) { - return(cmdret_exit); -} - - /* * Commands understood by ctdlsh */ @@ -21,31 +17,56 @@ typedef struct { } COMMAND; COMMAND commands[] = { - { "?", cmd_help, "Display this message" }, - { "help", cmd_help, "Display this message" }, - { "date", cmd_datetime, "Print the server's date and time" }, - { "exit", cmd_quit, "Quit using ctdlsh" }, - { "export", cmd_export, "Export all Citadel databases" }, - { "shutdown", cmd_shutdown, "Shut down the Citadel server" }, - { "time", cmd_datetime, "Print the server's date and time" }, - { "passwd", cmd_passwd, "Set or change an account password" }, - { "quit", cmd_quit, "Quit using ctdlsh" }, - { "who", cmd_who, "Display a list of online users" }, - { NULL, NULL, NULL } + {"?", cmd_help, "Display this message"}, + {"help", cmd_help, "Display this message"}, + {"date", cmd_datetime, "Print the server's date and time"}, + {"config", cmd_config, "Configure the Citadel server"}, + {"export", cmd_export, "Export all Citadel databases"}, + {"shutdown", cmd_shutdown, "Shut down the Citadel server"}, + {"time", cmd_datetime, "Print the server's date and time"}, + {"passwd", cmd_passwd, "Set or change an account password"}, + {"who", cmd_who, "Display a list of online users"}, + {"mailq", cmd_mailq, "Show the outbound email queue"}, + {NULL, NULL, NULL} +}; + + + +struct wow { + char *cmd[5]; // increase this if we need to have larger commands + char *description; }; +struct wow wows[] = { + {{ "help" } , "list available commands" }, + {{ "date" } , "print the server's date and time" }, + {{ "show", "eggs" } , "show how many eggs are available for serving" }, + {{ "kill", "mark", "zuckerberg" } , "die motherfucker die" }, + {{ "show", "undead", "zombies", "real" } , "show how many zombies are actually undead" }, + {{ "show", "undead", "zombies", "hollywood" } , "show how many zombies are hollywood communists" }, + {{ NULL } , "NULL" } +}; + + +int num_wows = sizeof(wows) / sizeof(struct wow); -int cmd_help(int sock, char *cmdbuf) { + + + + +int cmd_help(int sock, char *cmdbuf) +{ int i; - for (i=0; commands[i].func != NULL; ++i) { - printf("%-10s %s\n", commands[i].name, commands[i].doc); + for (i = 0; wows[i].cmd[0]; ++i) { + printf("%-10s %s\n", wows[i].cmd[0], wows[i].description); } } /* Auto-completer function */ -char *command_generator(const char *text, int state) { +char *command_generator(const char *text, int state) +{ static int list_index; static int len; char *name; @@ -59,22 +80,23 @@ char *command_generator(const char *text, int state) { ++list_index; if (!strncmp(name, text, len)) { - return(strdup(name)); + return (strdup(name)); } } - return(NULL); + return (NULL); } /* Auto-completer function */ -char **ctdlsh_completion(const char *text, int start, int end) { +char **ctdlsh_completion(const char *text, int start, int end) +{ char **matches = (char **) NULL; + rl_completer_word_break_characters = " "; if (start == 0) { matches = rl_completion_matches(text, command_generator); - } - else { + } else { rl_bind_key('\t', rl_abort); } @@ -82,7 +104,21 @@ char **ctdlsh_completion(const char *text, int start, int end) { } -void do_main_loop(int server_socket) { +int do_one_command(int server_socket, char *cmd) +{ + int i; + int ret; + for (i = 0; commands[i].func != NULL; ++i) { + if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) { + ret = (*commands[i].func) (server_socket, cmd); + } + } + return ret; +} + + +void do_main_loop(int server_socket) +{ char *cmd = NULL; char prompt[1024]; char buf[1024]; @@ -90,6 +126,7 @@ void do_main_loop(int server_socket) { int i; int ret = (-1); + strcpy(prompt, "> "); /* Do an INFO command and learn the hostname for the prompt */ @@ -97,7 +134,7 @@ void do_main_loop(int server_socket) { sock_getln(server_socket, buf, sizeof buf); if (buf[0] == '1') { i = 0; - while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) { + while (sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) { if (i == 1) { sprintf(prompt, "\n%s> ", buf); } @@ -109,19 +146,11 @@ void do_main_loop(int server_socket) { rl_attempted_completion_function = ctdlsh_completion; /* Here we go ... main command loop */ - while ((ret != cmdret_exit) && (cmd = readline(prompt))) { - - if ((cmd) && (*cmd)) { + while ( (cmd = readline(prompt)) , cmd ) { + if (*cmd) { add_history(cmd); - - for (i=0; commands[i].func != NULL; ++i) { - if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) { - ret = (*commands[i].func) (server_socket, cmd); - } - } - + ret = do_one_command(server_socket, cmd); } - free(cmd); } } @@ -134,33 +163,31 @@ int main(int argc, char **argv) { int server_socket = 0; char buf[1024]; - int c; + int i; char *ctdldir = CTDLDIR; - - printf("\nCitadel administration shell (c) 2009-2016 by citadel.org\n" - "This is open source software made available to you under the terms\n" - "of the GNU General Public License v3. All other rights reserved.\n" - ); - - opterr = 0; - while ((c = getopt (argc, argv, "h:")) != -1) { - switch(c) { - case 'h': - ctdldir = optarg; - break; - case '?': - if (optopt == 'h') { - fprintf(stderr, "Option -%c requires an argument\n", optopt); - } - else { - fprintf(stderr, "Unknown option '-%c'\n", optopt); - fprintf(stderr, "usage: %s [-h citadel_dir]\n", argv[0]); + char cmd[1024] = { 0 }; + int exitcode = 0; + + for (i = 1; i < argc; ++i) { + if (!strcmp(argv[i], "-h")) { + ctdldir = argv[++i]; + } else { + if (strlen(cmd) > 0) { + strcat(cmd, " "); } - exit(1); + strcat(cmd, argv[i]); } } - printf("Trying %s...\n", ctdldir); + int is_interactive = ((strlen(cmd) == 0) ? 1 : 0); + + if (is_interactive) { + printf("\nCitadel administration shell (c) 2009-2019 by citadel.org\n" + "This is open source software made available to you under the terms\n" + "of the GNU General Public License v3. All other rights reserved.\n"); + printf("Connecting to Citadel server in %s...\n", ctdldir); + } + sprintf(buf, "%s/citadel-admin.socket", ctdldir); server_socket = uds_connectsock(buf); if (server_socket < 0) { @@ -169,12 +196,19 @@ int main(int argc, char **argv) sock_getln(server_socket, buf, sizeof buf); if (buf[0] == '2') { - do_main_loop(server_socket); + if (is_interactive) { + printf("Connected: %s\n", buf); + do_main_loop(server_socket); + } else { + exitcode = do_one_command(server_socket, cmd); + } } sock_puts(server_socket, "QUIT"); sock_getln(server_socket, buf, sizeof buf); - printf("%s\n", buf); + if (is_interactive) { + printf("%s\n", buf); + } close(server_socket); - exit(0); + exit(exitcode); }