From: Guus Sliepen Date: Wed, 29 Jan 2014 16:17:59 +0000 (+0100) Subject: Don't ask questions if we are not running interactively. X-Git-Tag: release-1.1pre10~9 X-Git-Url: https://git.tinc-vpn.org/git/browse?a=commitdiff_plain;h=2e318f379992a730f592b4c5261d26d8e1a38cfd;p=tinc Don't ask questions if we are not running interactively. When creating invitations or using them to join a VPN, and the tinc command is not run interactively (ie, when stdin and stdout are not connected or redirected to/from a file), don't ask questions. If normally tinc would ask for a confirmation, just assume the default answer instead. If tinc really needs some input, just print an error message instead. In case an invitation is used for a VPN which uses a netname that is already in use on the local host, tinc will store the configuration in a temporary directory. Normally it asks for an alternative netname and then renames the temporary directory, but when not run interactively, it now just prints the location of the unchanged temporary directory. --- diff --git a/src/invitation.c b/src/invitation.c index a94460e8..110e3953 100644 --- a/src/invitation.c +++ b/src/invitation.c @@ -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)); @@ -571,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. @@ -747,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)); @@ -771,6 +778,8 @@ ask_netname: make_names(); } + fprintf(stderr, "Configuration stored in: %s\n", confbase); + return true; } @@ -854,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)); diff --git a/src/tincctl.c b/src/tincctl.c index c6d75824..5d37fda4 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -308,8 +308,7 @@ static FILE *ask_and_open(const char *filename, const char *what, const char *mo /* Check stdin and stdout */ if(ask && tty) { /* Ask for a file and/or directory name. */ - fprintf(stdout, "Please enter a file to save %s to [%s]: ", what, filename); - fflush(stdout); + fprintf(stderr, "Please enter a file to save %s to [%s]: ", what, filename); if(fgets(buf, sizeof buf, stdin) == NULL) { fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno)); @@ -1711,8 +1710,7 @@ static int cmd_init(int argc, char *argv[]) { } else if(argc < 2) { if(tty) { char buf[1024]; - fprintf(stdout, "Enter the Name you want your tinc node to have: "); - fflush(stdout); + fprintf(stderr, "Enter the Name you want your tinc node to have: "); if(!fgets(buf, sizeof buf, stdin)) { fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno)); return 1;