playing around with readline
[citadel.git] / ctdlsh / main.c
1 /*
2  * (c) 2009-2019 by Art Cancro and citadel.org
3  * This program is open source.  It runs great on the Linux operating system.
4  * It's released under the General Public License (GPL) version 3.
5  */
6
7 #include "ctdlsh.h"
8
9
10 /*
11  * Commands understood by ctdlsh
12  */
13 typedef struct {
14         char *name;
15         ctdlsh_cmdfunc_t *func;
16         char *doc;
17 } COMMAND;
18
19 COMMAND commands[] = {
20         {"?", cmd_help, "Display this message"},
21         {"help", cmd_help, "Display this message"},
22         {"date", cmd_datetime, "Print the server's date and time"},
23         {"config", cmd_config, "Configure the Citadel server"},
24         {"export", cmd_export, "Export all Citadel databases"},
25         {"shutdown", cmd_shutdown, "Shut down the Citadel server"},
26         {"time", cmd_datetime, "Print the server's date and time"},
27         {"passwd", cmd_passwd, "Set or change an account password"},
28         {"who", cmd_who, "Display a list of online users"},
29         {"mailq", cmd_mailq, "Show the outbound email queue"},
30         {NULL, NULL, NULL}
31 };
32
33
34
35 int cmd_help(int sock, char *cmdbuf)
36 {
37         int i;
38
39         for (i = 0; commands[i].func != NULL; ++i) {
40                 printf("%10s %s\n", commands[i].name, commands[i].doc);
41         }
42 }
43
44
45 int do_one_command(int server_socket, char *cmd)
46 {
47         int i;
48         int ret;
49         for (i = 0; commands[i].func != NULL; ++i) {
50                 if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) {
51                         ret = (*commands[i].func) (server_socket, cmd);
52                 }
53         }
54         return ret;
55 }
56
57 char *command_name_generator(const char *text, int state)
58 {
59         static int list_index, len;
60         char *name;
61
62         if (!state) {
63                 list_index = 0;
64                 len = strlen(text);
65         }
66
67         while (name = commands[list_index++].name) {
68                 if (strncmp(name, text, len) == 0) {
69                         return strdup(name);
70                 }
71         }
72
73         return NULL;
74 }
75
76
77 char **command_name_completion(const char *text, int start, int end)
78 {
79         rl_attempted_completion_over = 1;
80         return rl_completion_matches(text, command_name_generator);
81 }
82
83
84 void do_main_loop(int server_socket)
85 {
86         char *cmd = NULL;
87         char prompt[1024];
88         char buf[1024];
89         char server_reply[1024];
90         int i;
91         int ret = (-1);
92
93         strcpy(prompt, "> ");
94
95         /* Do an INFO command and learn the hostname for the prompt */
96         sock_puts(server_socket, "INFO");
97         sock_getln(server_socket, buf, sizeof buf);
98         if (buf[0] == '1') {
99                 i = 0;
100                 while (sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
101                         if (i == 1) {
102                                 sprintf(prompt, "\n%s> ", buf);
103                         }
104                         ++i;
105                 }
106         }
107
108         /* Here we go ... main command loop */
109         rl_attempted_completion_function = command_name_completion;
110         while ( (cmd = readline(prompt)) , cmd ) {
111                 if (*cmd) {
112                         add_history(cmd);
113                         ret = do_one_command(server_socket, cmd);
114                 }
115                 free(cmd);
116         }
117 }
118
119
120 /*
121  * If you don't know what main() does by now you probably shouldn't be reading this code.
122  */
123 int main(int argc, char **argv)
124 {
125         int server_socket = 0;
126         char buf[1024];
127         int i;
128         char *ctdldir = CTDLDIR;
129         char cmd[1024] = { 0 };
130         int exitcode = 0;
131
132         for (i = 1; i < argc; ++i) {
133                 if (!strcmp(argv[i], "-h")) {
134                         ctdldir = argv[++i];
135                 } else {
136                         if (strlen(cmd) > 0) {
137                                 strcat(cmd, " ");
138                         }
139                         strcat(cmd, argv[i]);
140                 }
141         }
142
143         int is_interactive = ((strlen(cmd) == 0) ? 1 : 0);
144
145         if (is_interactive) {
146                 printf("\nCitadel administration shell (c) 2009-2019 by citadel.org\n"
147                        "This is open source software made available to you under the terms\n"
148                        "of the GNU General Public License v3.  All other rights reserved.\n");
149                 printf("Connecting to Citadel server in %s...\n", ctdldir);
150         }
151
152         sprintf(buf, "%s/citadel-admin.socket", ctdldir);
153         server_socket = uds_connectsock(buf);
154         if (server_socket < 0) {
155                 exit(1);
156         }
157
158         sock_getln(server_socket, buf, sizeof buf);
159         if (buf[0] == '2') {
160                 if (is_interactive) {
161                         printf("Connected: %s\n", buf);
162                         do_main_loop(server_socket);
163                 } else {
164                         exitcode = do_one_command(server_socket, cmd);
165                 }
166         }
167
168         sock_puts(server_socket, "QUIT");
169         sock_getln(server_socket, buf, sizeof buf);
170         if (is_interactive) {
171                 printf("%s\n", buf);
172         }
173         close(server_socket);
174         exit(exitcode);
175 }