+#include "ctdlsh.h"
+
+#define CTDLDIR "/appl/citadel"
+
+
+
+int com_quit(char *cmdbuf) {
+ abort();
+}
+
+
+
+
+/*
+ * Commands understood by ctdlsh
+ */
+typedef struct {
+ char *name;
+ rl_icpfunc_t *func;
+ char *doc;
+} COMMAND;
+
+COMMAND commands[] = {
+ { "quit", com_quit, "Quit using ctdlsh" },
+ { "exit", com_quit, "Quit using ctdlsh" },
+ { 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);
+ }
+
+ if (read(fd, &ccc, sizeof(struct partial_config)) != sizeof(struct partial_config)) {
+ fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
+ return(-1);
+ }
+ if (close(fd) != 0) {
+ fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
+ return(-1);
+ }
+ return(ccc.c_ipgm_secret);
+}
+
+
+void do_main_loop(int server_socket) {
+ char *cmd = NULL;
+ char prompt[1024];
+ char buf[1024];
+ char server_reply[1024];
+ int i;
+
+ strcpy(prompt, "> ");
+
+ /* Do an INFO command and learn the hostname for the prompt */
+ sock_puts(server_socket, "INFO");
+ sock_getln(server_socket, buf, sizeof buf);
+ if (buf[0] == '1') {
+ i = 0;
+ while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
+ if (i == 1) {
+ sprintf(prompt, "\n%s> ", buf);
+ }
+ ++i;
+ }
+ }
+
+ /* Here we go ... main command loop */
+ while (cmd = readline(prompt)) {
+
+ if ((cmd) && (*cmd)) {
+ add_history(cmd);
+
+ sock_puts(server_socket, cmd);
+ sock_getln(server_socket, server_reply, sizeof server_reply);
+ printf("%s\n", server_reply);
+
+ if ((server_reply[0] == '4') || (server_reply[0] == '8')) {
+ /* we might consider putting something here */
+ sock_puts(server_socket, "000");
+ }
+
+ if ((server_reply[0] == '1') || (server_reply[0] == '8')) {
+ while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
+ printf("%s\n", buf);
+ }
+ }
+
+ }
+
+ free(cmd);
+ }
+}