Reworked ctdlsh to use the new admin socket instead of ipgm
[citadel.git] / ctdlsh / src / main.c
index c47b89c997db3edcc84f3c1c4de3ac94caa4f625..99c85d00c6b5e2b3dcd38c5d42299ecbb4328e78 100644 (file)
@@ -6,53 +6,39 @@
 #include "ctdlsh.h"
 
 
-
-
-int cmd_quit(char *cmdbuf) {
-       abort();
+int cmd_quit(int sock, char *cmdbuf) {
+       return(cmdret_exit);
 }
 
 
-
-
 /*
  * Commands understood by ctdlsh
  */
 typedef struct {
        char *name;
-       rl_icpfunc_t *func;
+       ctdlsh_cmdfunc_t *func;
        char *doc;
 } COMMAND;
 
-
 COMMAND commands[] = {
+       {       "?",            cmd_help,       "Display this message"                  },
+       {       "help",         cmd_help,       "Display this message"                  },
        {       "quit",         cmd_quit,       "Quit using ctdlsh"                     },
+       {       "exit",         cmd_quit,       "Quit using ctdlsh"                     },
        {       "date",         cmd_datetime,   "Print the server's date and time"      },
+       {       "time",         cmd_datetime,   "Print the server's date and time"      },
+       {       "passwd",       cmd_passwd,     "Set or change an account password"     },
+       {       "shutdown",     cmd_shutdown,   "Shut down the Citadel server"          },
        {       NULL,           NULL,           NULL                                    }
 };
 
 
-int discover_ipgm_secret(char *dirname) {
-       int fd;
-       struct partial_config ccc;
-       char configfile[1024];
-
-       sprintf(configfile, "%s/citadel.config", dirname);
-       fd = open(configfile, O_RDONLY);
-       if (fd < 0) {
-               fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
-               return(-1);
-       }
+int cmd_help(int sock, char *cmdbuf) {
+       int i;
 
-       if (read(fd, &ccc, sizeof(struct partial_config)) != sizeof(struct partial_config)) {
-               fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
-               return(-1);
+       for (i=0; commands[i].func != NULL; ++i) {
+               printf("%-10s %s\n", commands[i].name, commands[i].doc);
        }
-       if (close(fd) != 0) {
-               fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
-               return(-1);
-       }
-       return(ccc.c_ipgm_secret);
 }
 
 
@@ -101,6 +87,7 @@ void do_main_loop(int server_socket) {
        char buf[1024];
        char server_reply[1024];
        int i;
+       int ret = (-1);
 
        strcpy(prompt, "> ");
 
@@ -121,11 +108,17 @@ void do_main_loop(int server_socket) {
        rl_attempted_completion_function = ctdlsh_completion;
 
        /* Here we go ... main command loop */
-       while (cmd = readline(prompt)) {
+       while ((ret != cmdret_exit) && (cmd = readline(prompt))) {
 
                if ((cmd) && (*cmd)) {
                        add_history(cmd);
-                       /* FIXME put something here */
+
+                       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);
@@ -136,7 +129,6 @@ int main(int argc, char **argv)
 {
        int server_socket = 0;
        char buf[1024];
-       int ipgm_secret = (-1);
        int c;
        char *ctdldir = CTDLDIR;
 
@@ -161,25 +153,14 @@ int main(int argc, char **argv)
                }
        }
 
-       ipgm_secret = discover_ipgm_secret(ctdldir);
-       if (ipgm_secret < 0) {
-               exit(1);
-       }
-
        printf("Trying %s...\n", ctdldir);
-       sprintf(buf, "%s/citadel.socket", ctdldir);
+       sprintf(buf, "%s/citadel-admin.socket", ctdldir);
        server_socket = uds_connectsock(buf);
        if (server_socket < 0) {
                exit(1);
        }
 
        sock_getln(server_socket, buf, sizeof buf);
-       printf("%s\n", buf);
-
-       sock_printf(server_socket, "IPGM %d\n", ipgm_secret);
-       sock_getln(server_socket, buf, sizeof buf);
-       printf("%s\n", buf);
-
        if (buf[0] == '2') {
                do_main_loop(server_socket);
        }