2 * (c) 2009-2017 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 { "date", cmd_datetime, "Print the server's date and time" },
27 { "config", cmd_config, "Configure the Citadel server" },
28 { "export", cmd_export, "Export all Citadel databases" },
29 { "shutdown", cmd_shutdown, "Shut down the Citadel server" },
30 { "time", cmd_datetime, "Print the server's date and time" },
31 { "passwd", cmd_passwd, "Set or change an account password" },
32 { "who", cmd_who, "Display a list of online users" },
33 { "exit", cmd_quit, "Quit using ctdlsh" },
34 { "quit", cmd_quit, "Quit using ctdlsh" },
39 int cmd_help(int sock, char *cmdbuf) {
42 for (i=0; commands[i].func != NULL; ++i) {
43 printf("%-10s %s\n", commands[i].name, commands[i].doc);
48 /* Auto-completer function */
49 char *command_generator(const char *text, int state) {
50 static int list_index;
59 while (name = commands[list_index].name) {
62 if (!strncmp(name, text, len)) {
71 /* Auto-completer function */
72 char **ctdlsh_completion(const char *text, int start, int end) {
73 char **matches = (char **) NULL;
76 matches = rl_completion_matches(text, command_generator);
79 rl_bind_key('\t', rl_abort);
86 int do_one_command(int server_socket, char *cmd) {
89 for (i=0; commands[i].func != NULL; ++i) {
90 if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) {
91 ret = (*commands[i].func) (server_socket, cmd);
98 void do_main_loop(int server_socket) {
102 char server_reply[1024];
106 strcpy(prompt, "> ");
108 /* Do an INFO command and learn the hostname for the prompt */
109 sock_puts(server_socket, "INFO");
110 sock_getln(server_socket, buf, sizeof buf);
113 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
115 sprintf(prompt, "\n%s> ", buf);
121 /* Tell libreadline how we will help with auto-completion of commands */
122 rl_attempted_completion_function = ctdlsh_completion;
124 /* Here we go ... main command loop */
125 while ((ret != cmdret_exit) && (cmd = readline(prompt))) {
127 if ((cmd) && (*cmd)) {
129 ret = do_one_command(server_socket, cmd);
131 //for (i=0; commands[i].func != NULL; ++i) {
132 //if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) {
133 //ret = (*commands[i].func) (server_socket, cmd);
145 * If you don't know what main() does by now you probably shouldn't be reading this code.
147 int main(int argc, char **argv)
149 int server_socket = 0;
152 char *ctdldir = CTDLDIR;
153 char cmd[1024] = { 0 } ;
156 for (i=1; i<argc; ++i) {
157 if (!strcmp(argv[i], "-h")) {
161 if (strlen(cmd) > 0) {
164 strcat(cmd, argv[i]);
168 int is_interactive = ((strlen(cmd) == 0) ? 1 : 0);
170 if (is_interactive) {
171 printf("\nCitadel administration shell (c) 2009-2017 by citadel.org\n"
172 "This is open source software made available to you under the terms\n"
173 "of the GNU General Public License v3. All other rights reserved.\n"
175 printf("Trying %s...\n", ctdldir);
178 sprintf(buf, "%s/citadel-admin.socket", ctdldir);
179 server_socket = uds_connectsock(buf);
180 if (server_socket < 0) {
184 sock_getln(server_socket, buf, sizeof buf);
186 if (is_interactive) {
187 printf("Connected: %s\n", buf);
188 do_main_loop(server_socket);
191 exitcode = do_one_command(server_socket, cmd);
195 sock_puts(server_socket, "QUIT");
196 sock_getln(server_socket, buf, sizeof buf);
197 if (is_interactive) {
200 close(server_socket);