diff --git a/common.h b/common.h index b6aadbb..7349ff0 100644 --- a/common.h +++ b/common.h @@ -59,4 +59,6 @@ extern struct hss_config { char *output_file; } *pconfig; +extern int stdout_isatty; + #endif //_HSS_COMMON_H_ diff --git a/executor.c b/executor.c index f76fd57..ccde3ea 100644 --- a/executor.c +++ b/executor.c @@ -25,12 +25,8 @@ volatile int alive_children = 0; static void print_line(struct slot *pslot, int io_type, sstring buf, void *data) { - static int stdout_istty = -1; - if (stdout_istty == -1) { - stdout_istty = isatty(STDOUT_FILENO); - } FILE *output = (FILE *) data; - if (output == stdout && stdout_istty) { + if (output == stdout && stdout_isatty) { if (io_type == STDOUT_FILENO) { printf(ANSI_COLOR_GREEN "[O] %s -> " ANSI_COLOR_RESET, pslot->host); } else { diff --git a/main.c b/main.c index 6c3594c..001cd7c 100644 --- a/main.c +++ b/main.c @@ -28,6 +28,8 @@ struct hss_config *pconfig = NULL; bool enable_local = false; +int stdout_isatty = -1; + enum state { REMOTE, INNER, @@ -54,11 +56,23 @@ static const char * get_prompt() { switch (running_state) { case REMOTE: - return ANSI_COLOR_BOLD "[remote] >>> " ANSI_COLOR_RESET; + if (stdout_isatty) { + return ANSI_COLOR_BOLD "[remote] >>> " ANSI_COLOR_RESET; + } else { + return "[remote] >>> "; + } case INNER: - return ANSI_COLOR_MAGENTA_BOLD "[inner] >>> " ANSI_COLOR_RESET; + if (stdout_isatty) { + return ANSI_COLOR_MAGENTA_BOLD "[inner] >>> " ANSI_COLOR_RESET; + } else { + return "[inner] >>> "; + } case LOCAL: - return ANSI_COLOR_CYAN_BOLD "[local] >>> " ANSI_COLOR_RESET; + if (stdout_isatty) { + return ANSI_COLOR_CYAN_BOLD "[local] >>> " ANSI_COLOR_RESET; + } else { + return "[local] >>> "; + } default: return "[unknown] >>> "; } @@ -273,7 +287,7 @@ parse_opts(int argc, char **argv) { } if (argc == 0) { - if (!isatty(STDOUT_FILENO)) { + if (!stdout_isatty && isatty(STDIN_FILENO)) { usage("missing command parameter"); } return; @@ -290,6 +304,7 @@ main(int argc, char **argv) { slots = calloc(1, sizeof(struct slot)); inner_commands = calloc(1, sizeof(struct command)); + stdout_isatty = isatty(STDOUT_FILENO); /* Set the default locale values according to environment variables. */ setlocale(LC_ALL, "");