From: Lennart Poettering Date: Mon, 21 Oct 2024 09:10:35 +0000 (+0200) Subject: varlinkctl: respect $COLUMNS when rebreaking lines and we are not connected to a TTY X-Git-Tag: v257-rc1~178 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e8139b15e151b9503dc7beb61a9c566967b6aba7;p=thirdparty%2Fsystemd.git varlinkctl: respect $COLUMNS when rebreaking lines and we are not connected to a TTY Let's provide a mechanism to select the number of screen columns for rebreaking comments in Varlink IDL connected to a TTY, by honouring the $COLUMNS env var then too. Previously we'd only honour when connected to a TTY, but it's also useful otherwise for rebreaking ridiculously long comments, hence honour it in this case too. --- diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index d5593bd01d4..8ded46f4938 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -944,24 +944,35 @@ int fd_columns(int fd) { return ws.ws_col; } +int getenv_columns(void) { + int r; + + const char *e = getenv("COLUMNS"); + if (!e) + return -ENXIO; + + unsigned c; + r = safe_atou_bounded(e, 1, USHRT_MAX, &c); + if (r < 0) + return r; + + return (int) c; +} + unsigned columns(void) { - const char *e; - int c; if (cached_columns > 0) return cached_columns; - c = 0; - e = getenv("COLUMNS"); - if (e) - (void) safe_atoi(e, &c); - - if (c <= 0 || c > USHRT_MAX) { + int c = getenv_columns(); + if (c < 0) { c = fd_columns(STDOUT_FILENO); - if (c <= 0) + if (c < 0) c = 80; } + assert(c > 0); + cached_columns = c; return cached_columns; } diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index b5c45c464bc..b446e547d60 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -95,6 +95,7 @@ void reset_dev_console_fd(int fd, bool switch_to_text); int lock_dev_console(void); int make_console_stdio(void); +int getenv_columns(void); int fd_columns(int fd); unsigned columns(void); int fd_lines(int fd); diff --git a/src/varlinkctl/varlinkctl.c b/src/varlinkctl/varlinkctl.c index 0474ab245e8..f88a1050934 100644 --- a/src/varlinkctl/varlinkctl.c +++ b/src/varlinkctl/varlinkctl.c @@ -346,6 +346,21 @@ static int verb_info(int argc, char *argv[], void *userdata) { return 0; } +static size_t break_columns(void) { + int r; + + /* Rebreak the interface data to the TTY width */ + if (on_tty()) + return columns(); + + /* if not connected to a tty, still allow the caller to control the columns via the usual env var */ + r = getenv_columns(); + if (r < 0) + return SIZE_MAX; + + return r; +} + typedef struct GetInterfaceDescriptionData { const char *description; } GetInterfaceDescriptionData; @@ -448,7 +463,7 @@ static int verb_introspect(int argc, char *argv[], void *userdata) { } } else { pager_open(arg_pager_flags); - r = sd_varlink_idl_dump(stdout, vi, SD_VARLINK_IDL_FORMAT_COLOR_AUTO, on_tty() ? columns() : SIZE_MAX); + r = sd_varlink_idl_dump(stdout, vi, SD_VARLINK_IDL_FORMAT_COLOR_AUTO, break_columns()); if (r < 0) return log_error_errno(r, "Failed to format parsed interface description: %m"); } @@ -705,7 +720,7 @@ static int verb_validate_idl(int argc, char *argv[], void *userdata) { pager_open(arg_pager_flags); - r = sd_varlink_idl_dump(stdout, vi, SD_VARLINK_IDL_FORMAT_COLOR_AUTO, on_tty() ? columns() : SIZE_MAX); + r = sd_varlink_idl_dump(stdout, vi, SD_VARLINK_IDL_FORMAT_COLOR_AUTO, break_columns()); if (r < 0) return log_error_errno(r, "Failed to format parsed interface description: %m");