Get rid of GetProcessId in Win32/Program.inc.
[oota-llvm.git] / include / llvm / System / Program.h
1 //===- llvm/System/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
14 #ifndef LLVM_SYSTEM_PROGRAM_H
15 #define LLVM_SYSTEM_PROGRAM_H
16
17 #include "llvm/System/Path.h"
18
19 namespace llvm {
20 namespace sys {
21
22   // TODO: Add operations to communicate with the process, redirect its I/O,
23   // etc.
24
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.
29   /// @since 1.4
30   /// @brief An abstraction for finding and executing programs.
31   class Program {
32
33     unsigned Pid_;
34
35     /// @name Methods
36     /// @{
37   public:
38
39     Program() : Pid_(0) {}
40     ~Program() {}
41
42     /// Return process ID of this program.
43     unsigned GetPid() const { return Pid_; }
44
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.
53     bool Execute
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
70       ///< way.
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
78       ///< program.
79       );
80
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.
86     /// @see Execute
87     /// @brief Waits for the program to exit.
88     int Wait
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
93       ///< it doesn't.
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.
97       );
98
99     /// This function terminates the program.
100     /// @returns true if an error occured.
101     /// @see Execute
102     /// @brief Terminates the program.
103     bool Kill
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
107       ///< program.
108       );
109
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.
115     /// @throws nothing
116     /// @brief Construct a Program by finding it by name.
117     static Path FindProgramByName(const std::string& name);
118
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();
123
124     /// A convenience function equivalent to Program prg; prg.Execute(..);
125     /// prg.Wait(..);
126     /// @throws nothing
127     /// @see Execute, Wait
128     static int ExecuteAndWait(const Path& path,
129                               const char** args,
130                               const char ** env = 0,
131                               const sys::Path** redirects = 0,
132                               unsigned secondsToWait = 0,
133                               unsigned memoryLimit = 0,
134                               std::string* ErrMsg = 0);
135
136     /// A convenience function equivalent to Program prg; prg.Execute(..);
137     /// @throws nothing
138     /// @see Execute
139     static void ExecuteNoWait(const Path& path,
140                               const char** args,
141                               const char ** env = 0,
142                               const sys::Path** redirects = 0,
143                               unsigned memoryLimit = 0,
144                               std::string* ErrMsg = 0);
145
146     /// @}
147
148   };
149 }
150 }
151
152 #endif