+void start_server(int f_in, int f_out, int argc, char *argv[])
+{
+ setup_protocol(f_out, f_in);
+
+ if (am_sender) {
+ recv_exclude_list(f_in);
+ if (cvs_exclude)
+ add_cvs_excludes();
+ do_server_sender(f_in, f_out, argc, argv);
+ } else {
+ do_server_recv(f_in, f_out, argc, argv);
+ }
+ exit_cleanup(0);
+}
+
+static int client_run(int f_in, int f_out, int pid, int argc, char *argv[])
+{
+ struct file_list *flist;
+ int status = 0, status2 = 0;
+ char *local_name = NULL;
+
+ setup_protocol(f_out,f_in);
+
+ if (am_sender) {
+ if (cvs_exclude)
+ add_cvs_excludes();
+ if (delete_mode)
+ send_exclude_list(f_out);
+ flist = send_file_list(f_out,argc,argv);
+ if (verbose > 3)
+ rprintf(FINFO,"file list sent\n");
+ send_files(flist,f_out,f_in);
+ if (pid != -1) {
+ if (verbose > 3)
+ rprintf(FINFO,"waiting on %d\n",pid);
+ waitpid(pid, &status, 0);
+ }
+ report(-1);
+ exit_cleanup(status);
+ }
+
+ send_exclude_list(f_out);
+
+ flist = recv_file_list(f_in);
+ if (!flist || flist->count == 0) {
+ rprintf(FINFO,"nothing to do\n");
+ exit_cleanup(0);
+ }
+
+ local_name = get_local_name(flist,argv[0]);
+
+ status2 = do_recv(f_in,f_out,flist,local_name);
+
+ report(f_in);
+
+ if (pid != -1) {
+ waitpid(pid, &status, 0);
+ }
+
+ return status | status2;
+}
+
+
+int start_socket_client(char *host, char *path, int argc, char *argv[])
+{
+ int fd, i;
+ char *sargs[MAX_ARGS];
+ int sargc=0;
+ char line[1024];
+ char *p;
+ int version;
+
+ fd = open_socket_out(host, port);
+ if (fd == -1) {
+ exit_cleanup(1);
+ }
+
+ server_options(sargs,&sargc);
+
+ sargs[sargc++] = ".";
+
+ if (path && *path)
+ sargs[sargc++] = path;
+
+ sargs[sargc] = NULL;
+
+ p = strchr(path,'/');
+ if (p) *p = 0;
+ io_printf(fd,"%s\n",path);
+ if (p) *p = '/';
+
+ if (!read_line(fd, line, sizeof(line)-1)) {
+ return -1;
+ }
+
+ if (sscanf(line,"RSYNCD %d", &version) != 1) {
+ return -1;
+ }
+
+ while (1) {
+ if (!read_line(fd, line, sizeof(line)-1)) {
+ return -1;
+ }
+ if (strcmp(line,"RSYNCD: OK") == 0) break;
+ rprintf(FINFO,"%s\n", line);
+ }
+
+ for (i=0;i<sargc;i++) {
+ io_printf(fd,"%s\n", sargs[i]);
+ }
+ io_printf(fd,"\n");
+
+#if 0
+ while (1) {
+ if (!read_line(fd, line, sizeof(line)-1)) {
+ return -1;
+ }
+ rprintf(FINFO,"%s\n", line);
+ }
+#endif
+
+ return client_run(fd, fd, -1, argc, argv);
+}
+
+int start_client(int argc, char *argv[])
+{
+ char *p;
+ char *shell_machine = NULL;
+ char *shell_path = NULL;
+ char *shell_user = NULL;
+ int pid;
+ int f_in,f_out;
+
+ p = strchr(argv[0],':');
+
+ if (p) {
+ if (p[1] == ':') {
+ *p = 0;
+ return start_socket_client(argv[0], p+2, argc-1, argv+1);
+ }
+ am_sender = 0;
+ *p = 0;
+ shell_machine = argv[0];
+ shell_path = p+1;
+ argc--;
+ argv++;
+ } else {
+ am_sender = 1;
+
+ p = strchr(argv[argc-1],':');
+ if (!p) {
+ local_server = 1;
+ } else if (p[1] == ':') {
+ *p = 0;
+ return start_socket_client(argv[argc-1], p+2, argc-1, argv);
+ }
+
+ if (local_server) {
+ shell_machine = NULL;
+ shell_path = argv[argc-1];
+ } else {
+ *p = 0;
+ shell_machine = argv[argc-1];
+ shell_path = p+1;
+ }
+ argc--;
+ }
+
+ if (shell_machine) {
+ p = strchr(shell_machine,'@');
+ if (p) {
+ *p = 0;
+ shell_user = shell_machine;
+ shell_machine = p+1;
+ }
+ }
+
+ if (verbose > 3) {
+ rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
+ shell_cmd?shell_cmd:"",
+ shell_machine?shell_machine:"",
+ shell_user?shell_user:"",
+ shell_path?shell_path:"");
+ }
+
+ if (!am_sender && argc != 1) {
+ usage(FERROR);
+ exit_cleanup(1);
+ }
+
+ pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
+
+#if HAVE_SETLINEBUF
+ setlinebuf(stdout);
+ setlinebuf(stderr);
+#endif
+
+ return client_run(f_in, f_out, pid, argc, argv);
+}
+
+
+static void usage(int F)