X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=ctdlsh%2Fmain.c;h=daa356833127cc2acfd08e274f61af719a641a5f;hb=93f719e3132da1eb448f7ca27c852fbe0953a63c;hp=fd6663ecf6fb0a4e517ace1f6b38a0d42f3294dc;hpb=67be6683e8603c4f8926e3d0c06afc7add9f4cae;p=citadel.git diff --git a/ctdlsh/main.c b/ctdlsh/main.c index fd6663ecf..daa356833 100644 --- a/ctdlsh/main.c +++ b/ctdlsh/main.c @@ -1,17 +1,12 @@ /* - * (c) 2009-2017 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 */ @@ -31,48 +26,37 @@ COMMAND commands[] = { {"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"}, - {"exit", cmd_quit, "Quit using ctdlsh"}, - {"quit", cmd_quit, "Quit using ctdlsh"}, {"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[] = { - {{ "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" } -}; - - - - - - - - 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); + printf("%10s %s\n", commands[i].name, commands[i].doc); } } -/* Auto-completer function */ -char *command_generator(const char *text, int state) +int do_one_command(int server_socket, char *cmd) { - static int list_index; - static int len; + 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; +} + +char *command_name_generator(const char *text, int state) +{ + static int list_index, len; char *name; if (!state) { @@ -80,44 +64,20 @@ char *command_generator(const char *text, int state) len = strlen(text); } - while (name = commands[list_index].name) { - ++list_index; - - if (!strncmp(name, text, len)) { - return (strdup(name)); + while (name = commands[list_index++].name) { + if (strncmp(name, text, len) == 0) { + return strdup(name); } } - return (NULL); -} - - -/* Auto-completer function */ -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 { - rl_bind_key('\t', rl_abort); - } - - return (matches); + return NULL; } -int do_one_command(int server_socket, char *cmd) +char **command_name_completion(const char *text, int start, int end) { - 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; + rl_attempted_completion_over = 1; + return rl_completion_matches(text, command_name_generator); } @@ -130,7 +90,6 @@ 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 */ @@ -146,24 +105,13 @@ void do_main_loop(int server_socket) } } - /* Tell libreadline how we will help with auto-completion of commands */ - rl_attempted_completion_function = ctdlsh_completion; - /* Here we go ... main command loop */ - while ((ret != cmdret_exit) && (cmd = readline(prompt))) { - - if ((cmd) && (*cmd)) { + rl_attempted_completion_function = command_name_completion; + while ( (cmd = readline(prompt)) , cmd ) { + if (*cmd) { add_history(cmd); ret = do_one_command(server_socket, 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); - //} - //} - } - free(cmd); } } @@ -181,25 +129,6 @@ int main(int argc, char **argv) char cmd[1024] = { 0 }; int exitcode = 0; - - int num_wows = sizeof(wows) / sizeof(struct wow); - int j; - for (i=0; i