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.
17 #include <sys/types.h>
26 #include <sys/socket.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;
97 * input string from socket - implemented in terms of serv_read()
99 void serv_gets(char *buf)
103 /* Read one character at a time.
106 serv_read(&buf[i], 1);
107 if (buf[i] == '\n' || i == (SIZ-1))
111 /* If we got a long line, discard characters until the newline.
114 while (buf[i] != '\n') {
115 serv_read(&buf[i], 1);
119 /* Strip all trailing nonprintables (crlf)
126 * send line to server - implemented in terms of serv_write()
128 void serv_puts(char *buf)
130 serv_write(buf, strlen(buf));
138 * Main loop. Do things and have fun.
140 int main(int argc, char **argv)
144 char *ctdl_home_directory = CTDLDIR;
148 /* Parse command line */
149 while ((a = getopt(argc, argv, "h:w:")) != EOF) {
152 ctdl_home_directory = strdup(optarg);
155 watchdog = atoi(optarg);
157 fprintf(stderr, "sendcommand: usage: sendcommand [-h server_dir] [-w watchdog_timeout]\n");
162 fprintf(stderr, "sendcommand: started (pid=%d) connecting to Citadel server in %s\n",
169 snprintf(buf, sizeof buf, "%s/citadel-admin.socket", ctdl_home_directory);
170 serv_sock = uds_connectsock(buf);
173 fprintf(stderr, "%s\n", buf);
176 for (a=optind; a<argc; ++a) {
180 strcat(buf, argv[a]);
183 fprintf(stderr, "%s\n", buf);
186 fprintf(stderr, "%s\n", buf);
190 if ((xfermode == '4') || (xfermode == '8')) { /* send text */
191 while (fgets(buf, sizeof buf, stdin)) {
192 buf[strlen(buf)-1] = 0;
194 alarm(watchdog); /* reset the watchdog timer */
199 if ((xfermode == '1') || (xfermode == '8')) { /* receive text */
200 while (serv_gets(buf), strcmp(buf, "000")) {
202 alarm(watchdog); /* reset the watchdog timer */
206 if (xfermode == '6') { /* receive binary */
207 size_t len = atoi(&buf[4]);
208 size_t bytes_remaining = len;
210 while (bytes_remaining > 0) {
211 size_t this_block = bytes_remaining;
212 if (this_block > SIZ) this_block = SIZ;
213 serv_read(buf, this_block);
214 fwrite(buf, this_block, 1, stdout);
215 bytes_remaining -= this_block;
220 alarm(0); /* cancel the watchdog timer */
221 fprintf(stderr, "sendcommand: processing ended.\n");
222 if (xfermode == '5') {