Remove duplication in Program::Execute{And,No}Wait.
[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     unsigned Pid_;
33
34     // Noncopyable.
35     Program(const Program& other);
36     Program& operator=(const Program& other);
37
38     /// @name Methods
39     /// @{
40   public:
41
42     Program() : Pid_(0)
43     {}
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 invoking the
97       ///< program.
98       );
99
100     /// This static constructor (factory) will attempt to locate a program in
101     /// the operating system's file system using some pre-determined set of
102     /// locations to search (e.g. the PATH on Unix).
103     /// @returns A Path object initialized to the path of the program or a
104     /// Path object that is empty (invalid) if the program could not be found.
105     /// @throws nothing
106     /// @brief Construct a Program by finding it by name.
107     static Path FindProgramByName(const std::string& name);
108
109     // These methods change the specified standard stream (stdin or stdout) to
110     // binary mode. They return true if an error occurred
111     static bool ChangeStdinToBinary();
112     static bool ChangeStdoutToBinary();
113
114     /// A convenience function equivalent to Program prg; prg.Execute(..);
115     /// prg.Wait(..);
116     /// @throws nothing
117     /// @see Execute, Wait
118     static int ExecuteAndWait(const Path& path,
119                               const char** args,
120                               const char ** env = 0,
121                               const sys::Path** redirects = 0,
122                               unsigned secondsToWait = 0,
123                               unsigned memoryLimit = 0,
124                               std::string* ErrMsg = 0);
125
126     /// A convenience function equivalent to Program prg; prg.Execute(..);
127     /// @throws nothing
128     /// @see Execute
129     static void ExecuteNoWait(const Path& path,
130                               const char** args,
131                               const char ** env = 0,
132                               const sys::Path** redirects = 0,
133                               unsigned memoryLimit = 0,
134                               std::string* ErrMsg = 0);
135
136     /// @}
137
138   };
139 }
140 }
141
142 #endif