sendcommand now uses the new directory semantics
[citadel.git] / citadel / utils / sendcommand.c
index ba28d142da6f076d9fab349fb9041ce7beb2354e..7d8e089853fc1416e11739d8a29a23f3da922d06 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Command-line utility to transmit a server command.
  *
- * Copyright (c) 1987-2012 by the citadel.org team
+ * Copyright (c) 1987-2021 by the citadel.org team
  *
  * This program is open source software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 3.
 #include <sys/socket.h>
 #include <sys/un.h>
 #include "citadel.h"
-#include "include/citadel_dirs.h"
+#include "citadel_dirs.h"
 #include <libcitadel.h>
 
-
 int serv_sock = (-1);
 
-
 int uds_connectsock(char *sockpath)
 {
        int s;
@@ -94,7 +92,6 @@ void serv_write(char *buf, int nbytes)
 }
 
 
-
 /*
  * input string from socket - implemented in terms of serv_read()
  */
@@ -134,8 +131,6 @@ void serv_puts(char *buf)
 }
 
 
-
-
 /*
  * Main loop.  Do things and have fun.
  */
@@ -145,9 +140,6 @@ int main(int argc, char **argv)
        int watchdog = 60;
        char buf[SIZ];
        int xfermode = 0;
-       int relh=0;
-       int home=0;
-       char relhome[PATH_MAX]="";
        char ctdldir[PATH_MAX]=CTDLDIR;
 
        StartLibCitadel(SIZ);
@@ -156,13 +148,7 @@ int main(int argc, char **argv)
        while ((a = getopt(argc, argv, "h:w:")) != EOF) {
                switch (a) {
                case 'h':
-                       relh=optarg[0]!='/';
-                       if (!relh) {
-                               strncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
-                       } else {
-                               strncpy(relhome, optarg, sizeof relhome);
-                       }
-                       home = 1;
+                       strncpy(ctdldir, optarg, sizeof ctdldir);
                        break;
                case 'w':
                        watchdog = atoi(optarg);
@@ -173,17 +159,19 @@ int main(int argc, char **argv)
                }
        }
 
-       calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
-
-       fprintf(stderr, "sendcommand: started (pid=%d) connecting to Citadel server at %s\n",
+       fprintf(stderr, "sendcommand: started (pid=%d) connecting to Citadel server with data directory %s\n",
                (int) getpid(),
-               file_citadel_admin_socket
+               ctdldir
        );
        fflush(stderr);
 
+       if (chdir(ctdldir) != 0) {
+               fprintf(stderr, "sendcommand: %s: %s\n", ctdldir, strerror(errno));
+               exit(errno);
+       }
+
        alarm(watchdog);
        serv_sock = uds_connectsock(file_citadel_admin_socket);
-
        serv_gets(buf);
        fprintf(stderr, "%s\n", buf);
 
@@ -203,83 +191,19 @@ int main(int argc, char **argv)
        xfermode = buf[0];
 
        if ((xfermode == '4') || (xfermode == '8')) {           /* send text */
-               IOBuffer IOB;
-               FDIOBuffer FDIO;
-               const char *ErrStr;
-
-               memset(&IOB, 0, sizeof(0));
-               IOB.Buf = NewStrBufPlain(NULL, SIZ);
-               IOB.fd = serv_sock;
-               FDIOBufferInit(&FDIO, &IOB, fileno(stdin), -1);
-
-               while (FileSendChunked(&FDIO, &ErrStr) >= 0)
-                       alarm(watchdog);                        /* reset the watchdog timer */
-               if (ErrStr != NULL)
-                       fprintf(stderr, "Error while piping stuff: %s\n", ErrStr);
-               FDIOBufferDelete(&FDIO);
-               FreeStrBuf(&IOB.Buf);
+               while (fgets(buf, sizeof buf, stdin) > 0) {
+                       if (buf[strlen(buf)-1] == '\n') {
+                               buf[strlen(buf)-1] = 0;
+                       }
+                       serv_puts(buf);
+               }
                serv_puts("000");
        }
 
        if ((xfermode == '1') || (xfermode == '8')) {           /* receive text */
-               IOBuffer IOB;
-               StrBuf *Line, *OutBuf;
-               int Finished = 0;
-
-               memset(&IOB, 0, sizeof(IOB));
-               IOB.Buf = NewStrBufPlain(NULL, SIZ);
-               IOB.fd = serv_sock;
-               Line = NewStrBufPlain(NULL, SIZ);
-               OutBuf = NewStrBufPlain(NULL, SIZ * 10);
-
-               while (!Finished && (StrBuf_read_one_chunk_callback (serv_sock, 0, &IOB) >= 0))
-               {
-                       eReadState State;
-
-                       State = eReadSuccess;
-                       while (!Finished && (State == eReadSuccess))
-                       {
-                               if (IOBufferStrLength(&IOB) == 0)
-                               {
-                                       State = eMustReadMore;
-                                       break;
-                               }
-                               State = StrBufChunkSipLine(Line, &IOB);
-                               switch (State)
-                               {
-                               case eReadSuccess:
-                                       if (!strcmp(ChrPtr(Line), "000"))
-                                       {
-                                               Finished = 1;
-                                               break;
-                                       }
-                                       StrBufAppendBuf(OutBuf, Line, 0);
-                                       StrBufAppendBufPlain(OutBuf, HKEY("\n"), 0);
-                                       alarm(watchdog);                        /* reset the watchdog timer */
-                                       break;
-                               case eBufferNotEmpty:
-                                       break;
-                               case eMustReadMore:
-                                       continue;
-                               case eReadFail:
-                                       fprintf(stderr, "WTF? Exit!\n");
-                                       exit(-1);
-                                       break;
-                               }
-                               if (StrLength(OutBuf) > 5*SIZ)
-                               {
-                                       fwrite(ChrPtr(OutBuf), 1, StrLength(OutBuf), stdout);
-                                       FlushStrBuf(OutBuf);
-                               }
-                       }
-               }
-               if (StrLength(OutBuf) > 0)
-               {
-                       fwrite(ChrPtr(OutBuf), 1, StrLength(OutBuf), stdout);
+               while(serv_gets(buf), strcmp(buf, "000")) {
+                       printf("%s\n", buf);
                }
-               FreeStrBuf(&Line);
-               FreeStrBuf(&OutBuf);
-               FreeStrBuf(&IOB.Buf);
        }
        
        if (xfermode == '6') {                                  /* receive binary */