From 697de91c861777fb407ce792556569680a84625b Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 2 Jun 2016 12:45:11 +0200 Subject: [PATCH] Fix #5038 - Disable color when stdout is not in a terminal --- binr/radare2/radare2.c | 11 ++++++++--- libr/cons/cons.c | 33 +++++++++++++++++++++++++++++---- libr/include/r_cons.h | 1 + 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/binr/radare2/radare2.c b/binr/radare2/radare2.c index 4dba4d1f9c..5dabeb9c6a 100644 --- a/binr/radare2/radare2.c +++ b/binr/radare2/radare2.c @@ -892,7 +892,11 @@ int main(int argc, char **argv, char **envp) { } r_cons_flush (); } - +#if __UNIX__ + if (!r_cons_isatty ()) { + r_config_set_i (r.config, "scr.color", 0); + } +#endif ret = run_commands (cmds, files, quiet); r_list_free (cmds); r_list_free (files); @@ -914,7 +918,6 @@ int main(int argc, char **argv, char **envp) { r_config_set (r.config, "scr.interactive", "false"); r_config_set (r.config, "scr.prompt", "false"); } - r.num->value = 0; if (patchfile) { char *data = r_file_slurp (patchfile, NULL); @@ -1006,13 +1009,15 @@ int main(int argc, char **argv, char **envp) { } } #if __UNIX__ - if (isatty (0)) { + if (r_cons_isatty ()) { #endif if (r_config_get_i (r.config, "scr.histsave") && r_config_get_i (r.config, "scr.interactive") && !r_sandbox_enable (0)) r_line_hist_save (R2_HOMEDIR"/history"); #if __UNIX__ + } else { + r_config_set_i (r.config, "scr.color", 0); } #endif // TODO: kill thread diff --git a/libr/cons/cons.c b/libr/cons/cons.c index 7130f9a634..ef12d4fed4 100644 --- a/libr/cons/cons.c +++ b/libr/cons/cons.c @@ -723,6 +723,29 @@ R_API int r_cons_get_cursor(int *rows) { return col; } +R_API bool r_cons_isatty() { +#if __UNIX__ || __CYGWIN__ + struct winsize win = { 0 }; + const char *tty; + struct stat sb; + + if (!isatty (1)) + return false; + if (ioctl (1, TIOCGWINSZ, &win)) + return false; + if ((win.ws_col == 0) || (win.ws_row == 0)) + return false; + tty = ttyname (1); + if (!tty) + return false; + if (stat(tty, &sb) || !S_ISCHR(sb.st_mode)) + return false; + return true; +#endif + /* non-UNIX do not have ttys */ + return false; +} + // XXX: if this function returns <0 in rows or cols expect MAYHEM R_API int r_cons_get_size(int *rows) { #if __WINDOWS__ && !__CYGWIN__ @@ -738,8 +761,8 @@ R_API int r_cons_get_size(int *rows) { struct winsize win = { 0 }; if (isatty (0) && ioctl (0, TIOCGWINSZ, &win) == 0) { if ((win.ws_col == 0) || (win.ws_row == 0)) { - // TODO: use ttyname() ? - int fd = open ("/dev/tty", O_RDONLY); + const char *tty = ttyname (1); + int fd = open (tty, O_RDONLY); if (fd != -1) { int ret = ioctl (fd, TIOCGWINSZ, &win); if ((ret != 0) || (win.ws_col == 0) || (win.ws_row == 0)) { @@ -775,10 +798,12 @@ R_API int r_cons_get_size(int *rows) { I.rows = -1; I.columns = -1; #endif - if (I.rows<0) + if (I.rows < 0) { I.rows = 0; - if (I.columns<0) + } + if (I.columns < 0) { I.columns = 0; + } if (I.force_columns) I.columns = I.force_columns; if (I.force_rows) I.rows = I.force_rows; if (I.fix_columns) I.columns += I.fix_columns; diff --git a/libr/include/r_cons.h b/libr/include/r_cons.h index 6b09db3db4..5dacf08c80 100644 --- a/libr/include/r_cons.h +++ b/libr/include/r_cons.h @@ -438,6 +438,7 @@ R_API char *r_cons_rgb_tostring(ut8 r, ut8 g, ut8 b); R_API void r_cons_pal_list (int rad, const char *arg); R_API void r_cons_pal_show (void); R_API int r_cons_get_size(int *rows); +R_API bool r_cons_isatty(); R_API int r_cons_get_cursor(int *rows); R_API int r_cons_arrow_to_hjkl(int ch); R_API int r_cons_html_print(const char *ptr);