Skip to content

[LLVM] [Support] Query the terminal width using the termios API #143514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions llvm/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@ 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)
set(HAVE_PTHREAD_H 1)
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)
set(HAVE_PTHREAD_H 0)
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
Expand All @@ -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)
Expand All @@ -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) )
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/Config/config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
24 changes: 18 additions & 6 deletions llvm/lib/Support/Unix/Process.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Loading