[LLVM] [Support] Query the terminal width using ioctl() (#143514)
On unix systems, we were trying to determine the terminal width using
the `COULMNS` environment variable. Unfortunately, `COLUMNS` is not
exported by all shells and thus not available on some systems.
We were previously using `ioctl()` for this; fall back to doing so if `COLUMNS`
does not exist or does not store a positive integer.
This essentially reverts a3eb3d3d92 and
parts of https://reviews.llvm.org/D61326.
For more information, see #139499.
Fixes #139499.
This commit is contained in:
@@ -19,6 +19,7 @@ if (ANDROID OR CYGWIN OR CMAKE_SYSTEM_NAME MATCHES "AIX|DragonFly|FreeBSD|Haiku|
|
||||
set(HAVE_SYS_MMAN_H 1)
|
||||
set(HAVE_SYSEXITS_H 1)
|
||||
set(HAVE_UNISTD_H 1)
|
||||
set(HAVE_SYS_IOCTL_H 1)
|
||||
elseif (APPLE)
|
||||
set(HAVE_MACH_MACH_H 1)
|
||||
set(HAVE_MALLOC_MALLOC_H 1)
|
||||
@@ -26,6 +27,7 @@ elseif (APPLE)
|
||||
set(HAVE_SYS_MMAN_H 1)
|
||||
set(HAVE_SYSEXITS_H 1)
|
||||
set(HAVE_UNISTD_H 1)
|
||||
set(HAVE_SYS_IOCTL_H 1)
|
||||
elseif (WIN32)
|
||||
set(HAVE_MACH_MACH_H 0)
|
||||
set(HAVE_MALLOC_MALLOC_H 0)
|
||||
@@ -33,6 +35,7 @@ elseif (WIN32)
|
||||
set(HAVE_SYS_MMAN_H 0)
|
||||
set(HAVE_SYSEXITS_H 0)
|
||||
set(HAVE_UNISTD_H 0)
|
||||
set(HAVE_SYS_IOCTL_H 0)
|
||||
elseif (ZOS)
|
||||
# Confirmed in
|
||||
# https://github.com/llvm/llvm-project/pull/104706#issuecomment-2297109613
|
||||
@@ -42,6 +45,7 @@ elseif (ZOS)
|
||||
set(HAVE_SYS_MMAN_H 1)
|
||||
set(HAVE_SYSEXITS_H 0)
|
||||
set(HAVE_UNISTD_H 1)
|
||||
set(HAVE_SYS_IOCTL_H 1)
|
||||
else()
|
||||
# Other platforms that we don't promise support for.
|
||||
check_include_file(mach/mach.h HAVE_MACH_MACH_H)
|
||||
@@ -50,6 +54,7 @@ else()
|
||||
check_include_file(sys/mman.h HAVE_SYS_MMAN_H)
|
||||
check_include_file(sysexits.h HAVE_SYSEXITS_H)
|
||||
check_include_file(unistd.h HAVE_UNISTD_H)
|
||||
check_include_file(sys/ioctl.h HAVE_SYS_IOCTL_H)
|
||||
endif()
|
||||
|
||||
if( UNIX AND NOT (APPLE OR BEOS OR HAIKU) )
|
||||
|
||||
@@ -164,6 +164,9 @@
|
||||
/* Define to 1 if you have the <sys/mman.h> header file. */
|
||||
#cmakedefine HAVE_SYS_MMAN_H ${HAVE_SYS_MMAN_H}
|
||||
|
||||
/* Define to 1 if you have the <sys/ioctl.h> header file. */
|
||||
#cmakedefine HAVE_SYS_IOCTL_H ${HAVE_SYS_IOCTL_H}
|
||||
|
||||
/* Define to 1 if stat struct has st_mtimespec member .*/
|
||||
#cmakedefine HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC ${HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC}
|
||||
|
||||
|
||||
@@ -34,6 +34,9 @@
|
||||
#ifdef HAVE_GETAUXVAL
|
||||
#include <sys/auxv.h>
|
||||
#endif
|
||||
#ifdef HAVE_SYS_IOCTL_H
|
||||
#include <sys/ioctl.h>
|
||||
#endif
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
//=== WARNING: Implementation here must contain only generic UNIX code that
|
||||
@@ -304,31 +307,40 @@ bool Process::FileDescriptorIsDisplayed(int fd) {
|
||||
#endif
|
||||
}
|
||||
|
||||
static unsigned getColumns() {
|
||||
static unsigned getColumns(int FileID) {
|
||||
// If COLUMNS is defined in the environment, wrap to that many columns.
|
||||
// This matches GCC.
|
||||
if (const char *ColumnsStr = std::getenv("COLUMNS")) {
|
||||
int Columns = std::atoi(ColumnsStr);
|
||||
if (Columns > 0)
|
||||
return Columns;
|
||||
}
|
||||
|
||||
// We used to call ioctl TIOCGWINSZ to determine the width. It is considered
|
||||
// unuseful.
|
||||
return 0;
|
||||
// Some shells do not export COLUMNS; query the column count via ioctl()
|
||||
// instead if it isn't available.
|
||||
unsigned Columns = 0;
|
||||
|
||||
#ifdef HAVE_SYS_IOCTL_H
|
||||
struct winsize ws;
|
||||
if (ioctl(FileID, TIOCGWINSZ, &ws) == 0)
|
||||
Columns = ws.ws_col;
|
||||
#endif
|
||||
|
||||
return Columns;
|
||||
}
|
||||
|
||||
unsigned Process::StandardOutColumns() {
|
||||
if (!StandardOutIsDisplayed())
|
||||
return 0;
|
||||
|
||||
return getColumns();
|
||||
return getColumns(0);
|
||||
}
|
||||
|
||||
unsigned Process::StandardErrColumns() {
|
||||
if (!StandardErrIsDisplayed())
|
||||
return 0;
|
||||
|
||||
return getColumns();
|
||||
return getColumns(1);
|
||||
}
|
||||
|
||||
static bool terminalHasColors() {
|
||||
|
||||
Reference in New Issue
Block a user