Add a non-interactive mode to tinc commands.
[tinc] / src / tincctl.c
index fc21d42..c0c8a25 100644 (file)
@@ -75,6 +75,7 @@ char *scriptextension = "";
 static char *prompt;
 
 static struct option const long_options[] = {
+       {"batch", no_argument, NULL, 'b'},
        {"config", required_argument, NULL, 'c'},
        {"net", required_argument, NULL, 'n'},
        {"help", no_argument, NULL, 1},
@@ -100,6 +101,7 @@ static void usage(bool status) {
        } else {
                printf("Usage: %s [options] command\n\n", program_name);
                printf("Valid options are:\n"
+                               "  -b, --batch             Don't ask for anything (non-interactive mode).\n"
                                "  -c, --config=DIR        Read configuration options from DIR.\n"
                                "  -n, --net=NETNAME       Connect to net NETNAME.\n"
                                "      --pidfile=FILENAME  Read control cookie from FILENAME.\n"
@@ -158,6 +160,10 @@ static bool parse_options(int argc, char **argv) {
                        case 0:   /* long option */
                                break;
 
+                       case 'b':
+                               tty = false;
+                               break;
+
                        case 'c': /* config file */
                                confbase = xstrdup(optarg);
                                confbasegiven = true;
@@ -810,16 +816,31 @@ static int cmd_start(int argc, char *argv[]) {
        int nargc = 0;
        char **nargv = xzalloc((optind + argc) * sizeof *nargv);
 
-       nargv[nargc++] = c;
+       char *arg0 = c;
+#ifdef HAVE_MINGW
+       /*
+          Windows has no real concept of an "argv array". A command line is just one string.
+          The CRT of the new process will decode the command line string to generate argv before calling main(), and (by convention)
+          it uses quotes to handle spaces in arguments.
+          Therefore we need to quote all arguments that might contain spaces. No, execvp() won't do that for us (see MSDN).
+          If we don't do that, then execvp() will run fine but any spaces in the filename contained in arg0 will bleed
+          into the next arguments when the spawned process' CRT parses its command line, resulting in chaos.
+       */
+       xasprintf(&arg0, "\"%s\"", arg0);
+#endif
+       nargv[nargc++] = arg0;
        for(int i = 1; i < optind; i++)
                nargv[nargc++] = orig_argv[i];
        for(int i = 1; i < argc; i++)
                nargv[nargc++] = argv[i];
 
 #ifdef HAVE_MINGW
-       execvp(c, nargv);
-       fprintf(stderr, "Error starting %s: %s\n", c, strerror(errno));
-       return 1;
+       int status = spawnvp(_P_WAIT, c, nargv);
+       if (status == -1) {
+               fprintf(stderr, "Error starting %s: %s\n", c, strerror(errno));
+               return 1;
+       }
+       return status;
 #else
        pid_t pid = fork();
        if(pid == -1) {
@@ -2424,6 +2445,7 @@ int main(int argc, char *argv[]) {
        program_name = argv[0];
        orig_argv = argv;
        orig_argc = argc;
+       tty = isatty(0) && isatty(1);
 
        if(!parse_options(argc, argv))
                return 1;
@@ -2454,8 +2476,6 @@ int main(int argc, char *argv[]) {
        srand(time(NULL));
        crypto_init();
 
-       tty = isatty(0) && isatty(1);
-
        if(optind >= argc)
                return cmd_shell(argc, argv);