1 //===- llvm/System/Program.h ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the llvm::sys::Program class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SYSTEM_PROGRAM_H
15 #define LLVM_SYSTEM_PROGRAM_H
17 #include "llvm/System/Path.h"
22 // TODO: Add operations to communicate with the process, redirect its I/O,
25 /// This class provides an abstraction for programs that are executable by the
26 /// operating system. It provides a platform generic way to find executable
27 /// programs from the path and to execute them in various ways. The sys::Path
28 /// class is used to specify the location of the Program.
30 /// @brief An abstraction for finding and executing programs.
39 Program() : Pid_(0) {}
42 /// Return process ID of this program.
43 unsigned GetPid() const { return Pid_; }
45 /// This function executes the program using the \p arguments provided. The
46 /// invoked program will inherit the stdin, stdout, and stderr file
47 /// descriptors, the environment and other configuration settings of the
48 /// invoking program. If Path::executable() does not return true when this
49 /// function is called then a std::string is thrown.
50 /// @returns false in case of error, true otherwise.
51 /// @see FindProgramByName
52 /// @brief Executes the program with the given set of \p args.
54 ( const Path& path, ///< sys::Path object providing the path of the
55 ///< program to be executed. It is presumed this is the result of
56 ///< the FindProgramByName method.
57 const char** args, ///< A vector of strings that are passed to the
58 ///< program. The first element should be the name of the program.
59 ///< The list *must* be terminated by a null char* entry.
60 const char ** env = 0, ///< An optional vector of strings to use for
61 ///< the program's environment. If not provided, the current program's
62 ///< environment will be used.
63 const sys::Path** redirects = 0, ///< An optional array of pointers to
64 ///< Paths. If the array is null, no redirection is done. The array
65 ///< should have a size of at least three. If the pointer in the array
66 ///< are not null, then the inferior process's stdin(0), stdout(1),
67 ///< and stderr(2) will be redirected to the corresponding Paths.
68 ///< When an empty Path is passed in, the corresponding file
69 ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
71 unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
72 ///< of memory can be allocated by process. If memory usage will be
73 ///< higher limit, the child is killed and this call returns. If zero
74 ///< - no memory limit.
75 std::string* ErrMsg = 0 ///< If non-zero, provides a pointer to a string
76 ///< instance in which error messages will be returned. If the string
77 ///< is non-empty upon return an error occurred while invoking the
81 /// This function waits for the program to exit. This function will block
82 /// the current program until the invoked program exits.
83 /// @returns an integer result code indicating the status of the program.
84 /// A zero or positive value indicates the result code of the program. A
85 /// negative value is the signal number on which it terminated.
87 /// @brief Waits for the program to exit.
89 ( unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount
90 ///< of time to wait for the child process to exit. If the time
91 ///< expires, the child is killed and this call returns. If zero,
92 ///< this function will wait until the child finishes or forever if
94 std::string* ErrMsg = 0 ///< If non-zero, provides a pointer to a string
95 ///< instance in which error messages will be returned. If the string
96 ///< is non-empty upon return an error occurred while waiting.
99 /// This function terminates the program.
100 /// @returns true if an error occured.
102 /// @brief Terminates the program.
104 ( std::string* ErrMsg = 0 ///< If non-zero, provides a pointer to a string
105 ///< instance in which error messages will be returned. If the string
106 ///< is non-empty upon return an error occurred while killing the
110 /// This static constructor (factory) will attempt to locate a program in
111 /// the operating system's file system using some pre-determined set of
112 /// locations to search (e.g. the PATH on Unix).
113 /// @returns A Path object initialized to the path of the program or a
114 /// Path object that is empty (invalid) if the program could not be found.
116 /// @brief Construct a Program by finding it by name.
117 static Path FindProgramByName(const std::string& name);
119 // These methods change the specified standard stream (stdin or stdout) to
120 // binary mode. They return true if an error occurred
121 static bool ChangeStdinToBinary();
122 static bool ChangeStdoutToBinary();
124 /// A convenience function equivalent to Program prg; prg.Execute(..);
127 /// @see Execute, Wait
128 static int ExecuteAndWait(const Path& path,
130 const char ** env = 0,
131 const sys::Path** redirects = 0,
132 unsigned secondsToWait = 0,
133 unsigned memoryLimit = 0,
134 std::string* ErrMsg = 0);
136 /// A convenience function equivalent to Program prg; prg.Execute(..);
139 static void ExecuteNoWait(const Path& path,
141 const char ** env = 0,
142 const sys::Path** redirects = 0,
143 unsigned memoryLimit = 0,
144 std::string* ErrMsg = 0);