ctdlsh can now run in interactive mode or accept a single command on the command...
[citadel.git] / ctdlsh / main.c
1 /*
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.
4  */
5
6 #include "ctdlsh.h"
7
8
9 int cmd_quit(int sock, char *cmdbuf) {
10         return(cmdret_exit);
11 }
12
13
14 /*
15  * Commands understood by ctdlsh
16  */
17 typedef struct {
18         char *name;
19         ctdlsh_cmdfunc_t *func;
20         char *doc;
21 } COMMAND;
22
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"                     },
35         {       NULL,           NULL,           NULL                                    }
36 };
37
38
39 int cmd_help(int sock, char *cmdbuf) {
40         int i;
41
42         for (i=0; commands[i].func != NULL; ++i) {
43                 printf("%-10s %s\n", commands[i].name, commands[i].doc);
44         }
45 }
46
47
48 /* Auto-completer function */
49 char *command_generator(const char *text, int state) {
50         static int list_index;
51         static int len;
52         char *name;
53
54         if (!state) {
55                 list_index = 0;
56                 len = strlen(text);
57         }
58
59         while (name = commands[list_index].name) {
60                 ++list_index;
61
62                 if (!strncmp(name, text, len)) {
63                         return(strdup(name));
64                 }
65         }
66
67         return(NULL);
68 }
69
70
71 /* Auto-completer function */
72 char **ctdlsh_completion(const char *text, int start, int end) {
73         char **matches = (char **) NULL;
74
75         if (start == 0) {
76                 matches = rl_completion_matches(text, command_generator);
77         }
78         else {
79                 rl_bind_key('\t', rl_abort);
80         }
81
82         return (matches);
83 }
84
85
86 int do_one_command(int server_socket, char *cmd) {
87         int i;
88         int ret;
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);
92                 }
93         }
94         return ret;
95 }
96
97
98 void do_main_loop(int server_socket) {
99         char *cmd = NULL;
100         char prompt[1024];
101         char buf[1024];
102         char server_reply[1024];
103         int i;
104         int ret = (-1);
105
106         strcpy(prompt, "> ");
107
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);
111         if (buf[0] == '1') {
112                 i = 0;
113                 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
114                         if (i == 1) {
115                                 sprintf(prompt, "\n%s> ", buf);
116                         }
117                         ++i;
118                 }
119         }
120
121         /* Tell libreadline how we will help with auto-completion of commands */
122         rl_attempted_completion_function = ctdlsh_completion;
123
124         /* Here we go ... main command loop */
125         while ((ret != cmdret_exit) && (cmd = readline(prompt))) {
126
127                 if ((cmd) && (*cmd)) {
128                         add_history(cmd);
129                         ret = do_one_command(server_socket, cmd);
130
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);
134                                 //}
135                         //}
136
137                 }
138
139                 free(cmd);
140         }
141 }
142
143
144 /*
145  * If you don't know what main() does by now you probably shouldn't be reading this code.
146  */
147 int main(int argc, char **argv)
148 {
149         int server_socket = 0;
150         char buf[1024];
151         int i;
152         char *ctdldir = CTDLDIR;
153         char cmd[1024] = { 0 } ;
154         int exitcode = 0;
155
156         for (i=1; i<argc; ++i) {
157                 if (!strcmp(argv[i], "-h")) {
158                         ctdldir = argv[++i];
159                 }
160                 else {
161                         if (strlen(cmd) > 0) {
162                                 strcat(cmd, " ");
163                         }
164                         strcat(cmd, argv[i]);
165                 }
166         }
167
168         int is_interactive = ((strlen(cmd) == 0) ? 1 : 0);
169
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"
174                 );
175                 printf("Trying %s...\n", ctdldir);
176         }
177
178         sprintf(buf, "%s/citadel-admin.socket", ctdldir);
179         server_socket = uds_connectsock(buf);
180         if (server_socket < 0) {
181                 exit(1);
182         }
183
184         sock_getln(server_socket, buf, sizeof buf);
185         if (buf[0] == '2') {
186                 if (is_interactive) {
187                         printf("Connected: %s\n", buf);
188                         do_main_loop(server_socket);
189                 }
190                 else {
191                         exitcode = do_one_command(server_socket, cmd);
192                 }
193         }
194
195         sock_puts(server_socket, "QUIT");
196         sock_getln(server_socket, buf, sizeof buf);
197         if (is_interactive) {
198                 printf("%s\n", buf);
199         }
200         close(server_socket);
201         exit(exitcode);
202 }