diff options
author | 2016-11-28 13:35:23 +0000 | |
---|---|---|
committer | 2016-11-28 15:29:33 +0000 | |
commit | d0a12695894095d27f33020bacb5c2d9d318d7f8 (patch) | |
tree | 43405a95d291379f38a2c7438621ac739bc697d9 | |
parent | 49970e0136d0626460253b7c496cdc74fe7ee237 (diff) |
Bazel client: more platform-specific logic
Move terminal-querying functions and GetUserName
from blaze_util.cc into blaze_util_<platform>.
See https://github.com/bazelbuild/bazel/issues/2107
--
MOS_MIGRATED_REVID=140346402
-rw-r--r-- | src/main/cpp/blaze_util.cc | 65 | ||||
-rw-r--r-- | src/main/cpp/blaze_util.h | 12 | ||||
-rw-r--r-- | src/main/cpp/blaze_util_platform.h | 12 | ||||
-rw-r--r-- | src/main/cpp/blaze_util_posix.cc | 59 | ||||
-rw-r--r-- | src/main/cpp/blaze_util_windows.cc | 86 |
5 files changed, 157 insertions, 77 deletions
diff --git a/src/main/cpp/blaze_util.cc b/src/main/cpp/blaze_util.cc index d316a3cbe9..7a69ad20cf 100644 --- a/src/main/cpp/blaze_util.cc +++ b/src/main/cpp/blaze_util.cc @@ -16,19 +16,10 @@ #include <errno.h> #include <fcntl.h> -#include <pwd.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <sys/file.h> -#include <sys/ioctl.h> -#include <sys/socket.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <unistd.h> - -#include <sstream> #include "src/main/cpp/blaze_util_platform.h" #include "src/main/cpp/util/errors.h" @@ -50,20 +41,6 @@ using std::vector; const char kServerPidFile[] = "server.pid.txt"; const char kServerPidSymlink[] = "server.pid"; -string GetUserName() { - string user = GetEnv("USER"); - if (!user.empty()) { - return user; - } - errno = 0; - passwd *pwent = getpwuid(getuid()); // NOLINT (single-threaded) - if (pwent == NULL || pwent->pw_name == NULL) { - pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, - "$USER is not set, and unable to look up name of current user"); - } - return pwent->pw_name; -} - string MakeAbsolute(const string &path) { // Check if path is already absolute. if (path.empty() || path[0] == '/' || (isalpha(path[0]) && path[1] == ':')) { @@ -77,48 +54,6 @@ string MakeAbsolute(const string &path) { return cwd + separator + path; } -bool IsEmacsTerminal() { - string emacs = GetEnv("EMACS"); - string inside_emacs = GetEnv("INSIDE_EMACS"); - // GNU Emacs <25.1 (and ~all non-GNU emacsen) set EMACS=t, but >=25.1 doesn't - // do that and instead sets INSIDE_EMACS=<stuff> (where <stuff> can look like - // e.g. "25.1.1,comint"). So we check both variables for maximum - // compatibility. - return emacs == "t" || !inside_emacs.empty(); -} - -// Returns true iff both stdout and stderr are connected to a -// terminal, and it can support color and cursor movement -// (this is computed heuristically based on the values of -// environment variables). -bool IsStandardTerminal() { - string term = GetEnv("TERM"); - if (term.empty() || term == "dumb" || term == "emacs" || - term == "xterm-mono" || term == "symbolics" || term == "9term" || - IsEmacsTerminal()) { - return false; - } - return isatty(STDOUT_FILENO) && isatty(STDERR_FILENO); -} - -// Returns the number of columns of the terminal to which stdout is -// connected, or $COLUMNS (default 80) if there is no such terminal. -int GetTerminalColumns() { - struct winsize ws; - if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) { - return ws.ws_col; - } - string columns_env = GetEnv("COLUMNS"); - if (!columns_env.empty()) { - char* endptr; - int columns = blaze_util::strto32(columns_env.c_str(), &endptr, 10); - if (*endptr == '\0') { // $COLUMNS is a valid number - return columns; - } - } - return 80; // default if not a terminal. -} - const char* GetUnaryOption(const char *arg, const char *next_arg, const char *key) { diff --git a/src/main/cpp/blaze_util.h b/src/main/cpp/blaze_util.h index 85bf866769..ec61405764 100644 --- a/src/main/cpp/blaze_util.h +++ b/src/main/cpp/blaze_util.h @@ -35,8 +35,6 @@ extern const char kServerPidFile[]; // used to write PID symlinks will probably no longer be in use. extern const char kServerPidSymlink[]; -std::string GetUserName(); - // Returns the given path in absolute form. Does not change paths that are // already absolute. // @@ -46,16 +44,6 @@ std::string GetUserName(); // MakeAbsolute("C:/foo") ---> "C:/foo" std::string MakeAbsolute(const std::string &path); -// Returns true iff the current terminal is running inside an Emacs. -bool IsEmacsTerminal(); - -// Returns true iff the current terminal can support color and cursor movement. -bool IsStandardTerminal(); - -// Returns the number of columns of the terminal to which stdout is -// connected, or 80 if there is no such terminal. -int GetTerminalColumns(); - // If 'arg' matches 'key=value', returns address of 'value'. // If it matches 'key' alone, returns address of next_arg. // Returns NULL otherwise. diff --git a/src/main/cpp/blaze_util_platform.h b/src/main/cpp/blaze_util_platform.h index 504d221304..1542b66218 100644 --- a/src/main/cpp/blaze_util_platform.h +++ b/src/main/cpp/blaze_util_platform.h @@ -190,6 +190,18 @@ ATTRIBUTE_NORETURN void ExitImmediately(int exit_code); // Ensure we have open file descriptors for stdin/stdout/stderr. void SetupStdStreams(); +std::string GetUserName(); + +// Returns true iff the current terminal is running inside an Emacs. +bool IsEmacsTerminal(); + +// Returns true iff the current terminal can support color and cursor movement. +bool IsStandardTerminal(); + +// Returns the number of columns of the terminal to which stdout is +// connected, or 80 if there is no such terminal. +int GetTerminalColumns(); + } // namespace blaze #endif // BAZEL_SRC_MAIN_CPP_BLAZE_UTIL_PLATFORM_H_ diff --git a/src/main/cpp/blaze_util_posix.cc b/src/main/cpp/blaze_util_posix.cc index 1564086c88..60bf2bebd7 100644 --- a/src/main/cpp/blaze_util_posix.cc +++ b/src/main/cpp/blaze_util_posix.cc @@ -15,9 +15,11 @@ #include <errno.h> #include <fcntl.h> #include <limits.h> // PATH_MAX +#include <pwd.h> #include <signal.h> #include <stdarg.h> #include <stdio.h> +#include <sys/ioctl.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/wait.h> @@ -32,6 +34,7 @@ #include "src/main/cpp/util/file.h" #include "src/main/cpp/util/file_platform.h" #include "src/main/cpp/util/md5.h" +#include "src/main/cpp/util/numbers.h" namespace blaze { @@ -589,4 +592,60 @@ void ReleaseLock(BlazeLock* blaze_lock) { close(blaze_lock->lockfd); } +string GetUserName() { + string user = GetEnv("USER"); + if (!user.empty()) { + return user; + } + errno = 0; + passwd *pwent = getpwuid(getuid()); // NOLINT (single-threaded) + if (pwent == NULL || pwent->pw_name == NULL) { + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "$USER is not set, and unable to look up name of current user"); + } + return pwent->pw_name; +} + +bool IsEmacsTerminal() { + string emacs = GetEnv("EMACS"); + string inside_emacs = GetEnv("INSIDE_EMACS"); + // GNU Emacs <25.1 (and ~all non-GNU emacsen) set EMACS=t, but >=25.1 doesn't + // do that and instead sets INSIDE_EMACS=<stuff> (where <stuff> can look like + // e.g. "25.1.1,comint"). So we check both variables for maximum + // compatibility. + return emacs == "t" || !inside_emacs.empty(); +} + +// Returns true iff both stdout and stderr are connected to a +// terminal, and it can support color and cursor movement +// (this is computed heuristically based on the values of +// environment variables). +bool IsStandardTerminal() { + string term = GetEnv("TERM"); + if (term.empty() || term == "dumb" || term == "emacs" || + term == "xterm-mono" || term == "symbolics" || term == "9term" || + IsEmacsTerminal()) { + return false; + } + return isatty(STDOUT_FILENO) && isatty(STDERR_FILENO); +} + +// Returns the number of columns of the terminal to which stdout is +// connected, or $COLUMNS (default 80) if there is no such terminal. +int GetTerminalColumns() { + struct winsize ws; + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) { + return ws.ws_col; + } + string columns_env = GetEnv("COLUMNS"); + if (!columns_env.empty()) { + char* endptr; + int columns = blaze_util::strto32(columns_env.c_str(), &endptr, 10); + if (*endptr == '\0') { // $COLUMNS is a valid number + return columns; + } + } + return 80; // default if not a terminal. +} + } // namespace blaze. diff --git a/src/main/cpp/blaze_util_windows.cc b/src/main/cpp/blaze_util_windows.cc index a8d7351d3e..fc9b9af3a8 100644 --- a/src/main/cpp/blaze_util_windows.cc +++ b/src/main/cpp/blaze_util_windows.cc @@ -17,7 +17,9 @@ #ifndef COMPILER_MSVC #include <fcntl.h> +#include <pwd.h> #include <sys/cygwin.h> +#include <sys/ioctl.h> #include <sys/socket.h> #include <sys/stat.h> #include <sys/statfs.h> @@ -40,6 +42,7 @@ #include "src/main/cpp/util/file_platform.h" #include "src/main/cpp/util/md5.h" #include "src/main/cpp/util/strings.h" +#include "src/main/cpp/util/numbers.h" namespace blaze { @@ -1365,4 +1368,87 @@ void ReleaseLock(BlazeLock* blaze_lock) { #endif // COMPILER_MSVC } +#ifdef GetUserName +// By including <windows.h>, we have GetUserName defined either as +// GetUserNameA or GetUserNameW. +#undef GetUserName +#endif + +string GetUserName() { +#ifdef COMPILER_MSVC + // TODO(bazel-team): implement this. + pdie(255, "blaze::GetUserName is not implemented on Windows"); + return ""; +#else // not COMPILER_MSVC + string user = GetEnv("USER"); + if (!user.empty()) { + return user; + } + errno = 0; + passwd *pwent = getpwuid(getuid()); // NOLINT (single-threaded) + if (pwent == NULL || pwent->pw_name == NULL) { + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "$USER is not set, and unable to look up name of current user"); + } + return pwent->pw_name; +#endif // COMPILER_MSVC +} + +bool IsEmacsTerminal() { +#ifdef COMPILER_MSVC + pdie(255, "blaze::IsEmacsTerminal is not implemented on Windows"); + return false; +#else // not COMPILER_MSVC + string emacs = GetEnv("EMACS"); + string inside_emacs = GetEnv("INSIDE_EMACS"); + // GNU Emacs <25.1 (and ~all non-GNU emacsen) set EMACS=t, but >=25.1 doesn't + // do that and instead sets INSIDE_EMACS=<stuff> (where <stuff> can look like + // e.g. "25.1.1,comint"). So we check both variables for maximum + // compatibility. + return emacs == "t" || !inside_emacs.empty(); +#endif // COMPILER_MSVC +} + +// Returns true iff both stdout and stderr are connected to a +// terminal, and it can support color and cursor movement +// (this is computed heuristically based on the values of +// environment variables). +bool IsStandardTerminal() { +#ifdef COMPILER_MSVC + pdie(255, "blaze::IsStandardTerminal is not implemented on Windows"); + return false; +#else // not COMPILER_MSVC + string term = GetEnv("TERM"); + if (term.empty() || term == "dumb" || term == "emacs" || + term == "xterm-mono" || term == "symbolics" || term == "9term" || + IsEmacsTerminal()) { + return false; + } + return isatty(STDOUT_FILENO) && isatty(STDERR_FILENO); +#endif // COMPILER_MSVC +} + +// Returns the number of columns of the terminal to which stdout is +// connected, or $COLUMNS (default 80) if there is no such terminal. +int GetTerminalColumns() { +#ifdef COMPILER_MSVC + pdie(255, "blaze::GetTerminalColumns is not implemented on Windows"); + return 0; +#else // not COMPILER_MSVC + struct winsize ws; + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) { + return ws.ws_col; + } + string columns_env = GetEnv("COLUMNS"); + if (!columns_env.empty()) { + char* endptr; + int columns = blaze_util::strto32(columns_env.c_str(), &endptr, 10); + if (*endptr == '\0') { // $COLUMNS is a valid number + return columns; + } + } + return 80; // default if not a terminal. +#endif // COMPILER_MSVC +} + } // namespace blaze |