Don't ask questions if we are not running interactively.
[tinc] / src / invitation.c
index 6ccfc1f..110e395 100644 (file)
@@ -142,12 +142,19 @@ char *get_my_hostname() {
                }
        }
 
+       if(!tty) {
+               if(!hostname) {
+                       fprintf(stderr, "Could not determine the external address or hostname. Please set Address manually.\n");
+                       return NULL;
+               }
+               goto save;
+       }
+
 again:
-       printf("Please enter your host's external address or hostname");
+       fprintf(stderr, "Please enter your host's external address or hostname");
        if(hostname)
-               printf(" [%s]", hostname);
-       printf(": ");
-       fflush(stdout);
+               fprintf(stderr, " [%s]", hostname);
+       fprintf(stderr, ": ");
 
        if(!fgets(line, sizeof line, stdin)) {
                fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
@@ -404,8 +411,12 @@ int cmd_invite(int argc, char *argv[]) {
                char buf[1024];
                while(fgets(buf, sizeof buf, tc)) {
                        if((!strncasecmp(buf, "Mode", 4) && strchr(" \t=", buf[4]))
-                                       || (!strncasecmp(buf, "Broadcast", 9) && strchr(" \t=", buf[9])))
+                                       || (!strncasecmp(buf, "Broadcast", 9) && strchr(" \t=", buf[9]))) {
                                fputs(buf, f);
+                               // Make sure there is a newline character.
+                               if(!strchr(buf, '\n'))
+                                       fputc('\n', f);
+                       }
                }
                fclose(tc);
        }
@@ -567,7 +578,7 @@ make_names:
 
        if(!access(tinc_conf, F_OK)) {
                fprintf(stderr, "Configuration file %s already exists!\n", tinc_conf);
-               if(!tty || confbasegiven)
+               if(confbasegiven)
                        return false;
 
                // Generate a random netname, ask for a better one later.
@@ -743,7 +754,7 @@ make_names:
        check_port(name);
 
 ask_netname:
-       if(ask_netname) {
+       if(ask_netname && tty) {
                fprintf(stderr, "Enter a new netname: ");
                if(!fgets(line, sizeof line, stdin)) {
                        fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
@@ -767,6 +778,8 @@ ask_netname:
                make_names();
        }
 
+       fprintf(stderr, "Configuration stored in: %s\n", confbase);
+
        return true;
 }
 
@@ -823,7 +836,7 @@ int cmd_join(int argc, char *argv[]) {
        }
 
        // Make sure confbase exists and is accessible.
-       if(strcmp(confdir, confbase) && mkdir(confdir, 0755) && errno != EEXIST) {
+       if(!confbase_given && mkdir(confdir, 0755) && errno != EEXIST) {
                fprintf(stderr, "Could not create directory %s: %s\n", confdir, strerror(errno));
                return 1;
        }
@@ -850,10 +863,8 @@ int cmd_join(int argc, char *argv[]) {
        if(argc > 1) {
                invitation = argv[1];
        } else {
-               if(tty) {
-                       printf("Enter invitation URL: ");
-                       fflush(stdout);
-               }
+               if(tty)
+                       fprintf(stderr, "Enter invitation URL: ");
                errno = EPIPE;
                if(!fgets(line, sizeof line, stdin)) {
                        fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));