* ctdlsh: use getopt to allow -h directory
[citadel.git] / ctdlsh / src / main.c
1 /*
2  * (c) 2009 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 <config.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <readline/readline.h>
13 #include "ctdlsh.h"
14
15 #define CTDLDIR "/root/ctdl/trunk/citadel"
16
17 int discover_ipgm_secret(char *dirname) {
18         int fd;
19         struct partial_config ccc;
20         char configfile[1024];
21
22         sprintf(configfile, "%s/citadel.config", dirname);
23         fd = open(configfile, O_RDONLY);
24         if (fd < 0) {
25                 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
26                 return(-1);
27         }
28
29         if (read(fd, &ccc, sizeof(struct partial_config)) != sizeof(struct partial_config)) {
30                 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
31                 return(-1);
32         }
33         if (close(fd) != 0) {
34                 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
35                 return(-1);
36         }
37         return(ccc.c_ipgm_secret);
38 }
39
40
41 void do_main_loop(int server_socket) {
42         char *cmd = NULL;
43         char prompt[1024];
44         char buf[1024];
45         char server_reply[1024];
46         int i;
47
48         strcpy(prompt, "> ");
49
50         /* Do an INFO command and learn the hostname for the prompt */
51         sock_puts(server_socket, "INFO");
52         sock_getln(server_socket, buf, sizeof buf);
53         if (buf[0] == '1') {
54                 i = 0;
55                 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
56                         if (i == 1) {
57                                 sprintf(prompt, "\n%s> ", buf);
58                         }
59                         ++i;
60                 }
61         }
62
63         /* Here we go ... main command loop */
64         while (cmd = readline(prompt)) {
65
66                 if ((cmd) && (*cmd)) {
67                         add_history(cmd);
68
69                         sock_puts(server_socket, cmd);
70                         sock_getln(server_socket, server_reply, sizeof server_reply);
71                         printf("%s\n", server_reply);
72
73                         if ((server_reply[0] == '4') || (server_reply[0] == '8')) {
74                                 // FIXME
75                         }
76
77                         if ((server_reply[0] == '1') || (server_reply[0] == '8')) {
78                                 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
79                                         printf("%s\n", buf);
80                                 }
81                         }
82
83                 }
84
85                 free(cmd);
86         }
87 }
88
89 int main(int argc, char **argv)
90 {
91         int server_socket = 0;
92         char buf[1024];
93         int ipgm_secret = (-1);
94         int c;
95         char *ctdldir = CTDLDIR;
96
97         printf("\nCitadel administration shell v" PACKAGE_VERSION "\n");
98         printf("(c) 2009 citadel.org GPLv3\n");
99
100         opterr = 0;
101         while ((c = getopt (argc, argv, "h:")) != -1) {
102                 switch(c) {
103                 case 'h':
104                         ctdldir = optarg;
105                         break;
106                 case '?':
107                         if (optopt == 'h') {
108                                 fprintf(stderr, "Option -%c requires an argument\n", optopt);
109                         }
110                         else {
111                                 fprintf(stderr, "Unknown option '-%c'\n", optopt);
112                                 fprintf(stderr, "usage: %s [-h citadel_dir]\n", argv[0]);
113                         }
114                         exit(1);
115                 }
116         }
117
118         ipgm_secret = discover_ipgm_secret(ctdldir);
119         if (ipgm_secret < 0) {
120                 exit(1);
121         }
122
123         printf("Attaching to server...\r");
124         fflush(stdout);
125         sprintf(buf, "%s/citadel.socket", ctdldir);
126         server_socket = uds_connectsock(buf);
127         if (server_socket < 0) {
128                 exit(1);
129         }
130         printf("                      \r");
131
132         sock_getln(server_socket, buf, sizeof buf);
133         printf("%s\n", buf);
134
135         sock_printf(server_socket, "IPGM %d\n", ipgm_secret);
136         sock_getln(server_socket, buf, sizeof buf);
137         printf("%s\n", buf);
138
139         if (buf[0] == '2') {
140                 do_main_loop(server_socket);
141         }
142
143         sock_puts(server_socket, "QUIT");
144         sock_getln(server_socket, buf, sizeof buf);
145         printf("%s\n", buf);
146         close(server_socket);
147         exit(0);
148 }