19c69e6ae698f44703725eec8ff27630b6b81f0e3Daniel Dunbar//===- llvm/Support/Process.h -----------------------------------*- C++ -*-===//
263b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman//
3e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer//                     The LLVM Compiler Infrastructure
4e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
763b3afa98460ce38a1c48d3c44ef6edfdaf37b77Misha Brukman//
8e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer//===----------------------------------------------------------------------===//
9dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth/// \file
10dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth///
11dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth/// Provides a library for accessing information about this process and other
12dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth/// processes on the operating system. Also provides means of spawning
13dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth/// subprocess for commands. The design of this library is modeled after the
14dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth/// proposed design of the Boost.Process library, and is design specifically to
15dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth/// follow the style of standard libraries and potentially become a proposal
16dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth/// for a standard library.
17dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth///
18dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth/// This file declares the llvm::sys::Process class which contains a collection
19dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth/// of legacy static interfaces for extracting various information about the
20dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth/// current process. The goal is to migrate users of this API over to the new
21dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth/// interfaces.
22dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth///
23e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer//===----------------------------------------------------------------------===//
24e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer
25674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_SUPPORT_PROCESS_H
26674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_SUPPORT_PROCESS_H
27e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer
286a971bb8f59f4e20c953a2cc360cab7bae8642e4David Majnemer#include "llvm/ADT/ArrayRef.h"
29f42d4247ae1138c6deed50f92dcd1a4f34e07decRui Ueyama#include "llvm/ADT/Optional.h"
300184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth#include "llvm/Config/llvm-config.h"
316a971bb8f59f4e20c953a2cc360cab7bae8642e4David Majnemer#include "llvm/Support/Allocator.h"
320184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth#include "llvm/Support/DataTypes.h"
331f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/TimeValue.h"
34cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines#include <system_error>
35e6b77ede40392f0b18ea642a7f3f56edbd30770bReid Spencer
36e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencernamespace llvm {
37f42d4247ae1138c6deed50f92dcd1a4f34e07decRui Ueyamaclass StringRef;
38f42d4247ae1138c6deed50f92dcd1a4f34e07decRui Ueyama
39e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencernamespace sys {
40e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer
410184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruthclass self_process;
420184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth
430184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth/// \brief Generic base class which exposes information about an operating
440184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth/// system process.
450184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth///
460184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth/// This base class is the core interface behind any OS process. It exposes
470184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth/// methods to query for generic information about a particular process.
480184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth///
490184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth/// Subclasses implement this interface based on the mechanisms available, and
500184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth/// can optionally expose more interfaces unique to certain process kinds.
510184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruthclass process {
520184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruthprotected:
530184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  /// \brief Only specific subclasses of process objects can be destroyed.
540184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  virtual ~process();
550184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth
560184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruthpublic:
570184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  /// \brief Operating system specific type to identify a process.
580184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  ///
590c793018079803628b5aeee6bbc1e6464fbefd65Aaron Ballman  /// Note that the windows one is defined to 'unsigned long' as this is the
600c793018079803628b5aeee6bbc1e6464fbefd65Aaron Ballman  /// documented type for DWORD on windows, and we don't want to pull in the
610184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  /// Windows headers here.
620184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth#if defined(LLVM_ON_UNIX)
630184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  typedef pid_t id_type;
640184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth#elif defined(LLVM_ON_WIN32)
650c793018079803628b5aeee6bbc1e6464fbefd65Aaron Ballman  typedef unsigned long id_type; // Must match the type of DWORD.
660184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth#else
670184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth#error Unsupported operating system.
680184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth#endif
690184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth
700184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  /// \brief Get the operating system specific identifier for this process.
710184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  virtual id_type get_id() = 0;
720184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth
7373c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth  /// \brief Get the user time consumed by this process.
7473c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth  ///
7573c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth  /// Note that this is often an approximation and may be zero on platforms
7673c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth  /// where we don't have good support for the functionality.
7773c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth  virtual TimeValue get_user_time() const = 0;
7873c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth
7973c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth  /// \brief Get the system time consumed by this process.
8073c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth  ///
8173c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth  /// Note that this is often an approximation and may be zero on platforms
8273c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth  /// where we don't have good support for the functionality.
8373c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth  virtual TimeValue get_system_time() const = 0;
8473c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth
8573c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth  /// \brief Get the wall time consumed by this process.
8673c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth  ///
8773c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth  /// Note that this is often an approximation and may be zero on platforms
8873c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth  /// where we don't have good support for the functionality.
8973c35d86b9d5236be5b3f49bc8df11008b96636eChandler Carruth  virtual TimeValue get_wall_time() const = 0;
900184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth
910184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  /// \name Static factory routines for processes.
920184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  /// @{
930184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth
940184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  /// \brief Get the process object for the current process.
950184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  static self_process *get_self();
960184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth
970184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  /// @}
980184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth
990184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth};
1000184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth
1010184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth/// \brief The specific class representing the current process.
1020184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth///
1030184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth/// The current process can both specialize the implementation of the routines
1040184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth/// and can expose certain information not available for other OS processes.
1050184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruthclass self_process : public process {
1060184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  friend class process;
1070184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth
1080184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  /// \brief Private destructor, as users shouldn't create objects of this
1090184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  /// type.
1100184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth  virtual ~self_process();
1110184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth
1120184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruthpublic:
11336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  id_type get_id() override;
11436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  TimeValue get_user_time() const override;
11536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  TimeValue get_system_time() const override;
11636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  TimeValue get_wall_time() const override;
1170184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth
118814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth  /// \name Process configuration (sysconf on POSIX)
119814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth  /// @{
120814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth
121814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth  /// \brief Get the virtual memory page size.
122814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth  ///
123814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth  /// Query the operating system for this process's page size.
124814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth  size_t page_size() const { return PageSize; };
125814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth
126814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth  /// @}
127814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth
1280184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruthprivate:
129814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth  /// \name Cached process state.
130814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth  /// @{
131814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth
132814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth  /// \brief Cached page size, this cannot vary during the life of the process.
133814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth  size_t PageSize;
134814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth
135814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth  /// @}
136814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth
137814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth  /// \brief Constructor, used by \c process::get_self() only.
138814afe91ccad0e5e1f767303d780fa0318fa5212Chandler Carruth  self_process();
1390184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth};
1400184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth
1410184a841d3914bb78c7c6fa87ce9da86a2d5992aChandler Carruth
142dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth/// \brief A collection of legacy interfaces for querying information about the
143dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth/// current executing process.
144dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruthclass Process {
145dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruthpublic:
146dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// \brief Return process memory usage.
147dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// This static function will return the total amount of memory allocated
148dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// by the process. This only counts the memory allocated via the malloc,
149dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// calloc and realloc functions and includes any "free" holes in the
150dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// allocated space.
151dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static size_t GetMallocUsage();
152dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
153dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// This static function will set \p user_time to the amount of CPU time
154dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// spent in user (non-kernel) mode and \p sys_time to the amount of CPU
155dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// time spent in system (kernel) mode.  If the operating system does not
156dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// support collection of these metrics, a zero TimeValue will be for both
157dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// values.
158dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// \param elapsed Returns the TimeValue::now() giving current time
159dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// \param user_time Returns the current amount of user time for the process
160dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// \param sys_time Returns the current amount of system time for the process
161dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time,
162dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth                           TimeValue &sys_time);
163dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
164dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// This function makes the necessary calls to the operating system to
165dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// prevent core files or any other kind of large memory dumps that can
166dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// occur when a program fails.
167dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// @brief Prevent core file generation.
168dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static void PreventCoreFiles();
169dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
170f42d4247ae1138c6deed50f92dcd1a4f34e07decRui Ueyama  // This function returns the environment variable \arg name's value as a UTF-8
171f42d4247ae1138c6deed50f92dcd1a4f34e07decRui Ueyama  // string. \arg Name is assumed to be in UTF-8 encoding too.
172f42d4247ae1138c6deed50f92dcd1a4f34e07decRui Ueyama  static Optional<std::string> GetEnv(StringRef name);
173f42d4247ae1138c6deed50f92dcd1a4f34e07decRui Ueyama
174cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// This function searches for an existing file in the list of directories
175cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// in a PATH like environment variable, and returns the first file found,
176cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// according to the order of the entries in the PATH like environment
177cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// variable.
178cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  static Optional<std::string> FindInEnvPath(const std::string& EnvName,
179cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                                             const std::string& FileName);
180cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines
1816a971bb8f59f4e20c953a2cc360cab7bae8642e4David Majnemer  /// This function returns a SmallVector containing the arguments passed from
1826a971bb8f59f4e20c953a2cc360cab7bae8642e4David Majnemer  /// the operating system to the program.  This function expects to be handed
1836a971bb8f59f4e20c953a2cc360cab7bae8642e4David Majnemer  /// the vector passed in from main.
184cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  static std::error_code
1856a971bb8f59f4e20c953a2cc360cab7bae8642e4David Majnemer  GetArgumentVector(SmallVectorImpl<const char *> &Args,
1866a971bb8f59f4e20c953a2cc360cab7bae8642e4David Majnemer                    ArrayRef<const char *> ArgsFromMain,
1876a971bb8f59f4e20c953a2cc360cab7bae8642e4David Majnemer                    SpecificBumpPtrAllocator<char> &ArgAllocator);
1886a971bb8f59f4e20c953a2cc360cab7bae8642e4David Majnemer
189dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// This function determines if the standard input is connected directly
190dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// to a user's input (keyboard probably), rather than coming from a file
191dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// or pipe.
192dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static bool StandardInIsUserInput();
193dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
194dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// This function determines if the standard output is connected to a
195dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// "tty" or "console" window. That is, the output would be displayed to
196dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// the user rather than being put on a pipe or stored in a file.
197dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static bool StandardOutIsDisplayed();
198dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
199dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// This function determines if the standard error is connected to a
200dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// "tty" or "console" window. That is, the output would be displayed to
201dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// the user rather than being put on a pipe or stored in a file.
202dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static bool StandardErrIsDisplayed();
203dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
204dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// This function determines if the given file descriptor is connected to
205dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// a "tty" or "console" window. That is, the output would be displayed to
206dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// the user rather than being put on a pipe or stored in a file.
207dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static bool FileDescriptorIsDisplayed(int fd);
208dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
209dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// This function determines if the given file descriptor is displayd and
210dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// supports colors.
211dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static bool FileDescriptorHasColors(int fd);
212dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
213dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// This function determines the number of columns in the window
214dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// if standard output is connected to a "tty" or "console"
215dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// window. If standard output is not connected to a tty or
216dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// console, or if the number of columns cannot be determined,
217dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// this routine returns zero.
218dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static unsigned StandardOutColumns();
219dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
220dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// This function determines the number of columns in the window
221dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// if standard error is connected to a "tty" or "console"
222dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// window. If standard error is not connected to a tty or
223dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// console, or if the number of columns cannot be determined,
224dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// this routine returns zero.
225dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static unsigned StandardErrColumns();
226dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
227dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// This function determines whether the terminal connected to standard
228dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// output supports colors. If standard output is not connected to a
229dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// terminal, this function returns false.
230dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static bool StandardOutHasColors();
231dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
232dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// This function determines whether the terminal connected to standard
233dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// error supports colors. If standard error is not connected to a
234dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// terminal, this function returns false.
235dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static bool StandardErrHasColors();
236dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
23744a61bde15d456527156ee2080f0964344b939feNico Rieck  /// Enables or disables whether ANSI escape sequences are used to output
23844a61bde15d456527156ee2080f0964344b939feNico Rieck  /// colors. This only has an effect on Windows.
23944a61bde15d456527156ee2080f0964344b939feNico Rieck  /// Note: Setting this option is not thread-safe and should only be done
24044a61bde15d456527156ee2080f0964344b939feNico Rieck  /// during initialization.
24144a61bde15d456527156ee2080f0964344b939feNico Rieck  static void UseANSIEscapeCodes(bool enable);
24244a61bde15d456527156ee2080f0964344b939feNico Rieck
243dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// Whether changing colors requires the output to be flushed.
244dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// This is needed on systems that don't support escape sequences for
245dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// changing colors.
246dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static bool ColorNeedsFlush();
247dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
248dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// This function returns the colorcode escape sequences.
249dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// If ColorNeedsFlush() is true then this function will change the colors
250dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// and return an empty escape sequence. In that case it is the
251dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// responsibility of the client to flush the output stream prior to
252dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// calling this function.
253dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static const char *OutputColor(char c, bool bold, bool bg);
254dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
255dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// Same as OutputColor, but only enables the bold attribute.
256dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static const char *OutputBold(bool bg);
257dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
258dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// This function returns the escape sequence to reverse forground and
259dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// background colors.
260dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static const char *OutputReverse();
261dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
262dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// Resets the terminals colors, or returns an escape sequence to do so.
263dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static const char *ResetColor();
264dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
265dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// Get the result of a process wide random number generator. The
266dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  /// generator will be automatically seeded in non-deterministic fashion.
267dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth  static unsigned GetRandomNumber();
268dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth};
269dac1eeb4b7948456682e9aa641d2d5ef3eba8869Chandler Carruth
270e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer}
271e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer}
272e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer
273e38001f6c6d0b792b6138ec9e924db8ffaa781e0Reid Spencer#endif
274