2 * Command-line utility to transmit a server command.
4 * Copyright (c) 1987-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
18 #include <sys/types.h>
27 #include <sys/socket.h>
30 #include "include/citadel_dirs.h"
31 #include <libcitadel.h>
37 int uds_connectsock(char *sockpath)
40 struct sockaddr_un addr;
42 memset(&addr, 0, sizeof(addr));
43 addr.sun_family = AF_UNIX;
44 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
46 s = socket(AF_UNIX, SOCK_STREAM, 0);
48 fprintf(stderr, "sendcommand: Can't create socket: %s\n", strerror(errno));
52 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
53 fprintf(stderr, "sendcommand: can't connect: %s\n", strerror(errno));
63 * input binary data from socket
65 void serv_read(char *buf, int bytes)
71 rlen = read(serv_sock, &buf[len], bytes - len);
81 * send binary to server
83 void serv_write(char *buf, int nbytes)
85 int bytes_written = 0;
87 while (bytes_written < nbytes) {
88 retval = write(serv_sock, &buf[bytes_written], nbytes - bytes_written);
92 bytes_written = bytes_written + retval;
99 * input string from socket - implemented in terms of serv_read()
101 void serv_gets(char *buf)
105 /* Read one character at a time.
108 serv_read(&buf[i], 1);
109 if (buf[i] == '\n' || i == (SIZ-1))
113 /* If we got a long line, discard characters until the newline.
116 while (buf[i] != '\n') {
117 serv_read(&buf[i], 1);
121 /* Strip all trailing nonprintables (crlf)
128 * send line to server - implemented in terms of serv_write()
130 void serv_puts(char *buf)
132 serv_write(buf, strlen(buf));
140 * Main loop. Do things and have fun.
142 int main(int argc, char **argv)
150 char relhome[PATH_MAX]="";
151 char ctdldir[PATH_MAX]=CTDLDIR;
153 StartLibCitadel(SIZ);
155 /* Parse command line */
156 while ((a = getopt(argc, argv, "h:w:")) != EOF) {
161 strncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
163 strncpy(relhome, optarg, sizeof relhome);
168 watchdog = atoi(optarg);
171 fprintf(stderr, "sendcommand: usage: sendcommand [-h server_dir] [-w watchdog_timeout]\n");
176 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
178 fprintf(stderr, "sendcommand: started (pid=%d) connecting to Citadel server at %s\n",
180 file_citadel_admin_socket
185 serv_sock = uds_connectsock(file_citadel_admin_socket);
188 fprintf(stderr, "%s\n", buf);
191 for (a=optind; a<argc; ++a) {
195 strcat(buf, argv[a]);
198 fprintf(stderr, "%s\n", buf);
201 fprintf(stderr, "%s\n", buf);
205 if ((xfermode == '4') || (xfermode == '8')) { /* send text */
210 memset(&IOB, 0, sizeof(0));
211 IOB.Buf = NewStrBufPlain(NULL, SIZ);
213 FDIOBufferInit(&FDIO, &IOB, fileno(stdin), -1);
215 while (FileSendChunked(&FDIO, &ErrStr) >= 0)
216 alarm(watchdog); /* reset the watchdog timer */
218 fprintf(stderr, "Error while piping stuff: %s\n", ErrStr);
219 FDIOBufferDelete(&FDIO);
220 FreeStrBuf(&IOB.Buf);
224 if ((xfermode == '1') || (xfermode == '8')) { /* receive text */
226 StrBuf *Line, *OutBuf;
229 memset(&IOB, 0, sizeof(IOB));
230 IOB.Buf = NewStrBufPlain(NULL, SIZ);
232 Line = NewStrBufPlain(NULL, SIZ);
233 OutBuf = NewStrBufPlain(NULL, SIZ * 10);
235 while (!Finished && (StrBuf_read_one_chunk_callback (serv_sock, 0, &IOB) >= 0))
239 State = eReadSuccess;
240 while (!Finished && (State == eReadSuccess))
242 if (IOBufferStrLength(&IOB) == 0)
244 State = eMustReadMore;
247 State = StrBufChunkSipLine(Line, &IOB);
251 if (!strcmp(ChrPtr(Line), "000"))
256 StrBufAppendBuf(OutBuf, Line, 0);
257 StrBufAppendBufPlain(OutBuf, HKEY("\n"), 0);
258 alarm(watchdog); /* reset the watchdog timer */
260 case eBufferNotEmpty:
265 fprintf(stderr, "WTF? Exit!\n");
269 if (StrLength(OutBuf) > 5*SIZ)
271 fwrite(ChrPtr(OutBuf), 1, StrLength(OutBuf), stdout);
276 if (StrLength(OutBuf) > 0)
278 fwrite(ChrPtr(OutBuf), 1, StrLength(OutBuf), stdout);
282 FreeStrBuf(&IOB.Buf);
285 if (xfermode == '6') { /* receive binary */
286 size_t len = atoi(&buf[4]);
287 size_t bytes_remaining = len;
289 while (bytes_remaining > 0) {
290 size_t this_block = bytes_remaining;
291 if (this_block > SIZ) this_block = SIZ;
292 serv_read(buf, this_block);
293 fwrite(buf, this_block, 1, stdout);
294 bytes_remaining -= this_block;
299 alarm(0); /* cancel the watchdog timer */
300 fprintf(stderr, "sendcommand: processing ended.\n");
301 if (xfermode == '5') {