2 * (c) 2009-2011 by Art Cancro and citadel.org
3 * This program is released under the terms of the GNU General Public License v3.
11 int cmd_quit(char *cmdbuf) {
19 * Commands understood by ctdlsh
28 COMMAND commands[] = {
29 { "quit", cmd_quit, "Quit using ctdlsh" },
30 { "date", cmd_datetime, "Print the server's date and time" },
35 int discover_ipgm_secret(char *dirname) {
37 struct partial_config ccc;
38 char configfile[1024];
40 sprintf(configfile, "%s/citadel.config", dirname);
41 fd = open(configfile, O_RDONLY);
43 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
47 if (read(fd, &ccc, sizeof(struct partial_config)) != sizeof(struct partial_config)) {
48 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
52 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
55 return(ccc.c_ipgm_secret);
59 /* Auto-completer function */
60 char *command_generator(const char *text, int state) {
61 static int list_index;
70 while (name = commands[list_index].name) {
73 if (!strncmp(name, text, len)) {
82 /* Auto-completer function */
83 char **ctdlsh_completion(const char *text, int start, int end) {
84 char **matches = (char **) NULL;
87 matches = rl_completion_matches(text, command_generator);
90 rl_bind_key('\t', rl_abort);
98 void do_main_loop(int server_socket) {
102 char server_reply[1024];
105 strcpy(prompt, "> ");
107 /* Do an INFO command and learn the hostname for the prompt */
108 sock_puts(server_socket, "INFO");
109 sock_getln(server_socket, buf, sizeof buf);
112 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
114 sprintf(prompt, "\n%s> ", buf);
120 /* Tell libreadline how we will help with auto-completion of commands */
121 rl_attempted_completion_function = ctdlsh_completion;
123 /* Here we go ... main command loop */
124 while (cmd = readline(prompt)) {
126 if ((cmd) && (*cmd)) {
128 /* FIXME put something here */
135 int main(int argc, char **argv)
137 int server_socket = 0;
139 int ipgm_secret = (-1);
141 char *ctdldir = CTDLDIR;
143 printf("\nCitadel administration shell v" PACKAGE_VERSION "\n");
144 printf("(c) 2009-2011 citadel.org GPLv3\n");
147 while ((c = getopt (argc, argv, "h:")) != -1) {
154 fprintf(stderr, "Option -%c requires an argument\n", optopt);
157 fprintf(stderr, "Unknown option '-%c'\n", optopt);
158 fprintf(stderr, "usage: %s [-h citadel_dir]\n", argv[0]);
164 ipgm_secret = discover_ipgm_secret(ctdldir);
165 if (ipgm_secret < 0) {
169 printf("Trying %s...\n", ctdldir);
170 sprintf(buf, "%s/citadel.socket", ctdldir);
171 server_socket = uds_connectsock(buf);
172 if (server_socket < 0) {
176 sock_getln(server_socket, buf, sizeof buf);
179 sock_printf(server_socket, "IPGM %d\n", ipgm_secret);
180 sock_getln(server_socket, buf, sizeof buf);
184 do_main_loop(server_socket);
187 sock_puts(server_socket, "QUIT");
188 sock_getln(server_socket, buf, sizeof buf);
190 close(server_socket);