[pacman-dev] [PATCH] getcols: return 0 when not attached to a tty
Scripts that parse pacman's output (like pacsearch) generally do not want wrapped lines. Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> --- src/pacman/util.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/pacman/util.c b/src/pacman/util.c index 6a095fd..de103b6 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -46,7 +46,7 @@ #include "conf.h" #include "callback.h" -static int cached_columns = 0; +static int cached_columns = -1; struct table_cell_t { char *label; @@ -158,7 +158,7 @@ static int flush_term_input(int fd) void columns_cache_reset(void) { - cached_columns = 0; + cached_columns = -1; } static int getcols_fd(int fd) @@ -189,10 +189,15 @@ unsigned short getcols(void) const char *e; int c = 0; - if(cached_columns > 0) { + if(cached_columns >= 0) { return cached_columns; } + if(!isatty(STDOUT_FILENO)) { + cached_columns = 0; + return 0; + } + e = getenv("COLUMNS"); if(e) { c = strtol(e, NULL, 10); -- 2.0.1
On Mon, Aug 11, 2014 at 09:51:19AM -0400, Andrew Gregory wrote:
Scripts that parse pacman's output (like pacsearch) generally do not want wrapped lines.
Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> ---
I agree there's a bug here, but this doesn't seem like the right fix...
src/pacman/util.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/src/pacman/util.c b/src/pacman/util.c index 6a095fd..de103b6 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -46,7 +46,7 @@ #include "conf.h" #include "callback.h"
-static int cached_columns = 0; +static int cached_columns = -1;
struct table_cell_t { char *label; @@ -158,7 +158,7 @@ static int flush_term_input(int fd)
void columns_cache_reset(void) { - cached_columns = 0; + cached_columns = -1; }
static int getcols_fd(int fd) @@ -189,10 +189,15 @@ unsigned short getcols(void) const char *e; int c = 0;
- if(cached_columns > 0) { + if(cached_columns >= 0) { return cached_columns; }
+ if(!isatty(STDOUT_FILENO)) { + cached_columns = 0; + return 0; + } +
Doing this means that you can no longer use COLUMNS to control output width when not attached to a TTY.
e = getenv("COLUMNS"); if(e) { c = strtol(e, NULL, 10);
What isn't shown in the context here is that columns_fd is called next to determine the width of the terminal. I think we should add the isatty check there and then accept '0' as a valid return from that function.
-- 2.0.1
Scripts that parse pacman's output (like pacsearch) generally do not want wrapped lines. Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> --- * move isatty check to getcols_fd allowing COLUMNS to override * allow COLUMNS=0 src/pacman/util.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/pacman/util.c b/src/pacman/util.c index 6a095fd..5686d92 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -46,7 +46,7 @@ #include "conf.h" #include "callback.h" -static int cached_columns = 0; +static int cached_columns = -1; struct table_cell_t { char *label; @@ -158,13 +158,17 @@ static int flush_term_input(int fd) void columns_cache_reset(void) { - cached_columns = 0; + cached_columns = -1; } static int getcols_fd(int fd) { int width = -1; + if(!isatty(fd)) { + return 0; + } + #if defined(TIOCGSIZE) struct ttysize win; if(ioctl(fd, TIOCGSIZE, &win) == 0) { @@ -189,20 +193,24 @@ unsigned short getcols(void) const char *e; int c = 0; - if(cached_columns > 0) { + if(cached_columns >= 0) { return cached_columns; } e = getenv("COLUMNS"); - if(e) { - c = strtol(e, NULL, 10); + if(e && *e) { + char *p = NULL; + c = strtol(e, &p, 10); + if(*p != '\0') { + c= -1; + } } - if(c <= 0) { + if(c < 0) { c = getcols_fd(STDOUT_FILENO); } - if(c <= 0) { + if(c < 0) { c = 80; } -- 2.0.1
On 08/11/14 at 12:42pm, Andrew Gregory wrote:
Scripts that parse pacman's output (like pacsearch) generally do not want wrapped lines.
Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> ---
* move isatty check to getcols_fd allowing COLUMNS to override * allow COLUMNS=0
src/pacman/util.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)
diff --git a/src/pacman/util.c b/src/pacman/util.c index 6a095fd..5686d92 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -46,7 +46,7 @@ #include "conf.h" #include "callback.h"
-static int cached_columns = 0; +static int cached_columns = -1;
struct table_cell_t { char *label; @@ -158,13 +158,17 @@ static int flush_term_input(int fd)
void columns_cache_reset(void) { - cached_columns = 0; + cached_columns = -1; }
static int getcols_fd(int fd) { int width = -1;
+ if(!isatty(fd)) { + return 0; + } + #if defined(TIOCGSIZE) struct ttysize win; if(ioctl(fd, TIOCGSIZE, &win) == 0) { @@ -189,20 +193,24 @@ unsigned short getcols(void) const char *e; int c = 0;
c should be initialized to -1 or we'll only ever check the environment. Fixed on my 4.2 branch.
- if(cached_columns > 0) { + if(cached_columns >= 0) { return cached_columns; }
e = getenv("COLUMNS"); - if(e) { - c = strtol(e, NULL, 10); + if(e && *e) { + char *p = NULL; + c = strtol(e, &p, 10); + if(*p != '\0') { + c= -1; + } }
- if(c <= 0) { + if(c < 0) { c = getcols_fd(STDOUT_FILENO); }
- if(c <= 0) { + if(c < 0) { c = 80; }
participants (2)
-
Andrew Gregory
-
Dave Reisner