+extern struct filter_list_struct filter_list;
+
+static int *flag_ptr[] = {
+ &recurse, /* 0 */
+ &preserve_uid, /* 1 */
+ &preserve_gid, /* 2 */
+ &preserve_links, /* 3 */
+ &preserve_devices, /* 4 */
+ &preserve_hard_links, /* 5 */
+ &always_checksum, /* 6 */
+ &xfer_dirs, /* 7 (protocol 29) */
+ &do_compression, /* 8 (protocol 29) */
+ NULL
+};
+
+static char *flag_name[] = {
+ "--recurse (-r)",
+ "--owner (-o)",
+ "--group (-g)",
+ "--links (-l)",
+ "--devices (-D)",
+ "--hard-links (-H)",
+ "--checksum (-c)",
+ "--dirs (-d)",
+ "--compress (-z)",
+ NULL
+};
+
+void write_stream_flags(int fd)
+{
+ int i, flags;
+
+ /* Start the batch file with a bitmap of data-stream-affecting
+ * flags. */
+ if (protocol_version < 29)
+ flag_ptr[7] = NULL;
+ for (i = 0, flags = 0; flag_ptr[i]; i++) {
+ if (*flag_ptr[i])
+ flags |= 1 << i;
+ }
+ write_int(fd, flags);
+}
+
+void read_stream_flags(int fd)
+{
+ int i, flags;
+
+ if (protocol_version < 29)
+ flag_ptr[7] = NULL;
+ for (i = 0, flags = read_int(fd); flag_ptr[i]; i++) {
+ int set = flags & (1 << i) ? 1 : 0;
+ if (*flag_ptr[i] != set) {
+ if (verbose) {
+ rprintf(FINFO,
+ "%sing the %s option to match the batchfile.\n",
+ set ? "Sett" : "Clear", flag_name[i]);
+ }
+ *flag_ptr[i] = set;
+ }
+ }
+ if (protocol_version < 29) {
+ if (recurse)
+ xfer_dirs |= 1;
+ else if (xfer_dirs < 2)
+ xfer_dirs = 0;
+ }
+}
+
+static void write_arg(int fd, char *arg)
+{
+ char *x, *s;
+
+ if (*arg == '-' && (x = strchr(arg, '=')) != NULL) {
+ write(fd, arg, x - arg + 1);
+ arg += x - arg + 1;
+ }
+
+ if (strpbrk(arg, " \"'&;|[]()$#!*?^\\") != NULL) {
+ write(fd, "'", 1);
+ for (s = arg; (x = strchr(s, '\'')) != NULL; s = x + 1) {
+ write(fd, s, x - s + 1);
+ write(fd, "'", 1);
+ }
+ write(fd, s, strlen(s));
+ write(fd, "'", 1);
+ return;
+ }
+
+ write(fd, arg, strlen(arg));
+}
+
+static void write_filter_rules(int fd)
+{
+ struct filter_struct *ent;
+
+ write_sbuf(fd, " <<'#E#'\n");
+ for (ent = filter_list.head; ent; ent = ent->next) {
+ unsigned int plen;
+ char *p = get_rule_prefix(ent->match_flags, "- ", 0, &plen);
+ write_buf(fd, p, plen);
+ write_sbuf(fd, ent->pattern);
+ if (ent->match_flags & MATCHFLG_DIRECTORY)
+ write_byte(fd, '/');
+ write_byte(fd, eol_nulls ? 0 : '\n');
+ }
+ if (eol_nulls)
+ write_sbuf(fd, ";\n");
+ write_sbuf(fd, "#E#");
+}
+
+/* This routine tries to write out an equivalent --read-batch command
+ * given the user's --write-batch args. However, it doesn't really
+ * understand most of the options, so it uses some overly simple
+ * heuristics to munge the command line into something that will
+ * (hopefully) work. */
+void write_batch_shell_file(int argc, char *argv[], int file_arg_cnt)