Skip to content

Commit

Permalink
fix stdin from pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
six-ddc committed Feb 9, 2018
1 parent 6bce6e7 commit c61938f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 2 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ extern struct hss_config {
char *output_file;
} *pconfig;

extern int stdout_isatty;

#endif //_HSS_COMMON_H_
6 changes: 1 addition & 5 deletions executor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 19 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ struct hss_config *pconfig = NULL;

bool enable_local = false;

int stdout_isatty = -1;

enum state {
REMOTE,
INNER,
Expand All @@ -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] >>> ";
}
Expand Down Expand Up @@ -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;
Expand All @@ -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, "");
Expand Down

0 comments on commit c61938f

Please sign in to comment.