Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
6.6 KiB

  1. //===- llvm/Support/Program.h ------------------------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file declares the llvm::sys::Program class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_PROGRAM_H
  14. #define LLVM_SUPPORT_PROGRAM_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/Support/Path.h"
  17. namespace llvm {
  18. class error_code;
  19. namespace sys {
  20. // TODO: Add operations to communicate with the process, redirect its I/O,
  21. // etc.
  22. /// This class provides an abstraction for programs that are executable by the
  23. /// operating system. It provides a platform generic way to find executable
  24. /// programs from the path and to execute them in various ways. The sys::Path
  25. /// class is used to specify the location of the Program.
  26. /// @since 1.4
  27. /// @brief An abstraction for finding and executing programs.
  28. class Program {
  29. /// Opaque handle for target specific data.
  30. void *Data_;
  31. // Noncopyable.
  32. Program(const Program& other) LLVM_DELETED_FUNCTION;
  33. Program& operator=(const Program& other) LLVM_DELETED_FUNCTION;
  34. /// @name Methods
  35. /// @{
  36. Program();
  37. ~Program();
  38. /// This function executes the program using the \p arguments provided. The
  39. /// invoked program will inherit the stdin, stdout, and stderr file
  40. /// descriptors, the environment and other configuration settings of the
  41. /// invoking program. If Path::executable() does not return true when this
  42. /// function is called then a std::string is thrown.
  43. /// @returns false in case of error, true otherwise.
  44. /// @see FindProgramByName
  45. /// @brief Executes the program with the given set of \p args.
  46. bool Execute
  47. ( const Path& path, ///< sys::Path object providing the path of the
  48. ///< program to be executed. It is presumed this is the result of
  49. ///< the FindProgramByName method.
  50. const char** args, ///< A vector of strings that are passed to the
  51. ///< program. The first element should be the name of the program.
  52. ///< The list *must* be terminated by a null char* entry.
  53. const char ** env = 0, ///< An optional vector of strings to use for
  54. ///< the program's environment. If not provided, the current program's
  55. ///< environment will be used.
  56. const sys::Path** redirects = 0, ///< An optional array of pointers to
  57. ///< Paths. If the array is null, no redirection is done. The array
  58. ///< should have a size of at least three. If the pointer in the array
  59. ///< are not null, then the inferior process's stdin(0), stdout(1),
  60. ///< and stderr(2) will be redirected to the corresponding Paths.
  61. ///< When an empty Path is passed in, the corresponding file
  62. ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
  63. ///< way.
  64. unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
  65. ///< of memory can be allocated by process. If memory usage will be
  66. ///< higher limit, the child is killed and this call returns. If zero
  67. ///< - no memory limit.
  68. std::string* ErrMsg = 0 ///< If non-zero, provides a pointer to a string
  69. ///< instance in which error messages will be returned. If the string
  70. ///< is non-empty upon return an error occurred while invoking the
  71. ///< program.
  72. );
  73. /// This function waits for the program to exit. This function will block
  74. /// the current program until the invoked program exits.
  75. /// @returns an integer result code indicating the status of the program.
  76. /// A zero or positive value indicates the result code of the program.
  77. /// -1 indicates failure to execute
  78. /// -2 indicates a crash during execution or timeout
  79. /// @see Execute
  80. /// @brief Waits for the program to exit.
  81. int Wait
  82. ( const Path& path, ///< The path to the child process executable.
  83. unsigned secondsToWait, ///< If non-zero, this specifies the amount
  84. ///< of time to wait for the child process to exit. If the time
  85. ///< expires, the child is killed and this call returns. If zero,
  86. ///< this function will wait until the child finishes or forever if
  87. ///< it doesn't.
  88. std::string* ErrMsg ///< If non-zero, provides a pointer to a string
  89. ///< instance in which error messages will be returned. If the string
  90. ///< is non-empty upon return an error occurred while waiting.
  91. );
  92. public:
  93. /// This static constructor (factory) will attempt to locate a program in
  94. /// the operating system's file system using some pre-determined set of
  95. /// locations to search (e.g. the PATH on Unix). Paths with slashes are
  96. /// returned unmodified.
  97. /// @returns A Path object initialized to the path of the program or a
  98. /// Path object that is empty (invalid) if the program could not be found.
  99. /// @brief Construct a Program by finding it by name.
  100. static Path FindProgramByName(const std::string& name);
  101. // These methods change the specified standard stream (stdin, stdout, or
  102. // stderr) to binary mode. They return errc::success if the specified stream
  103. // was changed. Otherwise a platform dependent error is returned.
  104. static error_code ChangeStdinToBinary();
  105. static error_code ChangeStdoutToBinary();
  106. static error_code ChangeStderrToBinary();
  107. /// A convenience function equivalent to Program prg; prg.Execute(..);
  108. /// prg.Wait(..);
  109. /// @see Execute, Wait
  110. static int ExecuteAndWait(const Path& path,
  111. const char** args,
  112. const char ** env = 0,
  113. const sys::Path** redirects = 0,
  114. unsigned secondsToWait = 0,
  115. unsigned memoryLimit = 0,
  116. std::string* ErrMsg = 0,
  117. bool *ExecutionFailed = 0);
  118. /// A convenience function equivalent to Program prg; prg.Execute(..);
  119. /// @see Execute
  120. static void ExecuteNoWait(const Path& path,
  121. const char** args,
  122. const char ** env = 0,
  123. const sys::Path** redirects = 0,
  124. unsigned memoryLimit = 0,
  125. std::string* ErrMsg = 0);
  126. /// @}
  127. };
  128. // Return true if the given arguments fit within system-specific
  129. // argument length limits.
  130. bool argumentsFitWithinSystemLimits(ArrayRef<const char*> Args);
  131. }
  132. }
  133. #endif