From 6a22cda2e997f5aaf25143cd1d75c4b6b1b3ae28 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 13 Oct 2019 21:48:31 -0700 Subject: [PATCH] More code cleanup and guards to check tty_in/out exist --- bb.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bb.c b/bb.c index 93fe37e..154cc63 100644 --- a/bb.c +++ b/bb.c @@ -265,8 +265,8 @@ int fputs_escaped(FILE *f, const char *str, const char *color) void init_term(void) { static int first_time = 1; - tty_in = fopen("/dev/tty", "r"); - tty_out = fopen("/dev/tty", "w"); + if (!tty_in) tty_in = fopen("/dev/tty", "r"); + if (!tty_out) tty_out = fopen("/dev/tty", "w"); if (first_time) { tcgetattr(fileno(tty_out), &orig_termios); first_time = 0; @@ -746,11 +746,12 @@ void run_bbcmd(bb_t *bb, const char *cmd) set_selected(bb, e, !IS_SELECTED(e)); } else { invalid_cmd: - fputs(T_LEAVE_BBMODE, tty_out); + if (tty_out) fputs(T_LEAVE_BBMODE, tty_out); restore_term(&orig_termios); fprintf(stderr, "Invalid bb command: +%s", cmd); fprintf(stderr, "\n"); init_term(); + bb->dirty = 1; } } @@ -1268,8 +1269,8 @@ int main(int argc, char *argv[]) write(cmdfd, "\0", 1); for (int i = 0; i < argc; i++) { if (argv[i][0] == '+') { - char *colon = strchr(argv[i], ':'); char *cmd = argv[i]; + char *colon = strchr(cmd, ':'); if (colon && !colon[1]) { for (++i; i < argc; i++) { write(cmdfd, cmd, strlen(cmd));