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>
35 int uds_connectsock(char *sockpath)
38 struct sockaddr_un addr;
40 memset(&addr, 0, sizeof(addr));
41 addr.sun_family = AF_UNIX;
42 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
44 s = socket(AF_UNIX, SOCK_STREAM, 0);
46 fprintf(stderr, "sendcommand: Can't create socket: %s\n", strerror(errno));
50 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
51 fprintf(stderr, "sendcommand: can't connect: %s\n", strerror(errno));
61 * input binary data from socket
63 void serv_read(char *buf, int bytes)
69 rlen = read(serv_sock, &buf[len], bytes - len);
79 * send binary to server
81 void serv_write(char *buf, int nbytes)
83 int bytes_written = 0;
85 while (bytes_written < nbytes) {
86 retval = write(serv_sock, &buf[bytes_written], nbytes - bytes_written);
90 bytes_written = bytes_written + retval;
96 * input string from socket - implemented in terms of serv_read()
98 void serv_gets(char *buf)
102 /* Read one character at a time.
105 serv_read(&buf[i], 1);
106 if (buf[i] == '\n' || i == (SIZ-1))
110 /* If we got a long line, discard characters until the newline.
113 while (buf[i] != '\n') {
114 serv_read(&buf[i], 1);
118 /* Strip all trailing nonprintables (crlf)
125 * send line to server - implemented in terms of serv_write()
127 void serv_puts(char *buf)
129 serv_write(buf, strlen(buf));
135 * Main loop. Do things and have fun.
137 int main(int argc, char **argv)
145 char relhome[PATH_MAX]="";
146 char ctdldir[PATH_MAX]=CTDLDIR;
148 StartLibCitadel(SIZ);
150 /* Parse command line */
151 while ((a = getopt(argc, argv, "h:w:")) != EOF) {
156 strncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
158 strncpy(relhome, optarg, sizeof relhome);
163 watchdog = atoi(optarg);
166 fprintf(stderr, "sendcommand: usage: sendcommand [-h server_dir] [-w watchdog_timeout]\n");
171 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
173 fprintf(stderr, "sendcommand: started (pid=%d) connecting to Citadel server at %s\n",
175 file_citadel_admin_socket
180 serv_sock = uds_connectsock(file_citadel_admin_socket);
182 fprintf(stderr, "%s\n", buf);
185 for (a=optind; a<argc; ++a) {
189 strcat(buf, argv[a]);
192 fprintf(stderr, "%s\n", buf);
195 fprintf(stderr, "%s\n", buf);
199 if ((xfermode == '4') || (xfermode == '8')) { /* send text */
200 while (fgets(buf, sizeof buf, stdin) > 0) {
201 if (buf[strlen(buf)-1] == '\n') {
202 buf[strlen(buf)-1] = 0;
209 if ((xfermode == '1') || (xfermode == '8')) { /* receive text */
210 while(serv_gets(buf), strcmp(buf, "000")) {
215 if (xfermode == '6') { /* receive binary */
216 size_t len = atoi(&buf[4]);
217 size_t bytes_remaining = len;
219 while (bytes_remaining > 0) {
220 size_t this_block = bytes_remaining;
221 if (this_block > SIZ) this_block = SIZ;
222 serv_read(buf, this_block);
223 fwrite(buf, this_block, 1, stdout);
224 bytes_remaining -= this_block;
229 alarm(0); /* cancel the watchdog timer */
230 fprintf(stderr, "sendcommand: processing ended.\n");
231 if (xfermode == '5') {