7f96245130f51fde901c79c0583283db17326ad6
[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     /// Opaque handle for target specific data.
33         void *Data;
34
35     unsigned Pid_;
36
37     // Noncopyable.
38     Program(const Program& other);
39     Program& operator=(const Program& other);
40
41     /// @name Methods
42     /// @{
43   public:
44
45     Program();
46         ~Program();
47         
48     /// Return process ID of this program.
49     unsigned GetPid() { return Pid_; }
50
51     /// This function executes the program using the \p arguments provided.  The
52     /// invoked program will inherit the stdin, stdout, and stderr file
53     /// descriptors, the environment and other configuration settings of the
54     /// invoking program. If Path::executable() does not return true when this
55     /// function is called then a std::string is thrown.
56     /// @returns false in case of error, true otherwise.
57     /// @see FindProgramByName
58     /// @brief Executes the program with the given set of \p args.
59     bool Execute
60     ( const Path& path,  ///< sys::Path object providing the path of the
61       ///< program to be executed. It is presumed this is the result of
62       ///< the FindProgramByName method.
63       const char** args, ///< A vector of strings that are passed to the
64       ///< program.  The first element should be the name of the program.
65       ///< The list *must* be terminated by a null char* entry.
66       const char ** env = 0, ///< An optional vector of strings to use for
67       ///< the program's environment. If not provided, the current program's
68       ///< environment will be used.
69       const sys::Path** redirects = 0, ///< An optional array of pointers to
70       ///< Paths. If the array is null, no redirection is done. The array
71       ///< should have a size of at least three. If the pointer in the array
72       ///< are not null, then the inferior process's stdin(0), stdout(1),
73       ///< and stderr(2) will be redirected to the corresponding Paths.
74       ///< When an empty Path is passed in, the corresponding file
75       ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
76       ///< way.
77       unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
78       ///< of memory can be allocated by process. If memory usage will be
79       ///< higher limit, the child is killed and this call returns. If zero
80       ///< - no memory limit.
81       std::string* ErrMsg = 0 ///< If non-zero, provides a pointer to a string
82       ///< instance in which error messages will be returned. If the string
83       ///< is non-empty upon return an error occurred while invoking the
84       ///< program.
85       );
86
87     /// This function waits for the program to exit. This function will block
88     /// the current program until the invoked program exits.
89     /// @returns an integer result code indicating the status of the program.
90     /// A zero or positive value indicates the result code of the program. A
91     /// negative value is the signal number on which it terminated.
92     /// @see Execute
93     /// @brief Waits for the program to exit.
94     int Wait
95     ( unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount
96       ///< of time to wait for the child process to exit. If the time
97       ///< expires, the child is killed and this call returns. If zero,
98       ///< this function will wait until the child finishes or forever if
99       ///< it doesn't.
100       std::string* ErrMsg = 0 ///< If non-zero, provides a pointer to a string
101       ///< instance in which error messages will be returned. If the string
102       ///< is non-empty upon return an error occurred while invoking the
103       ///< program.
104       );
105
106     /// This static constructor (factory) will attempt to locate a program in
107     /// the operating system's file system using some pre-determined set of
108     /// locations to search (e.g. the PATH on Unix).
109     /// @returns A Path object initialized to the path of the program or a
110     /// Path object that is empty (invalid) if the program could not be found.
111     /// @throws nothing
112     /// @brief Construct a Program by finding it by name.
113     static Path FindProgramByName(const std::string& name);
114
115     // These methods change the specified standard stream (stdin or stdout) to
116     // binary mode. They return true if an error occurred
117     static bool ChangeStdinToBinary();
118     static bool ChangeStdoutToBinary();
119
120     /// A convenience function equivalent to Program prg; prg.Execute(..);
121     /// prg.Wait(..);
122     /// @throws nothing
123     /// @see Execute, Wait
124     static int ExecuteAndWait(const Path& path,
125                               const char** args,
126                               const char ** env = 0,
127                               const sys::Path** redirects = 0,
128                               unsigned secondsToWait = 0,
129                               unsigned memoryLimit = 0,
130                               std::string* ErrMsg = 0);
131
132     /// A convenience function equivalent to Program prg; prg.Execute(..);
133     /// @throws nothing
134     /// @see Execute
135     static void ExecuteNoWait(const Path& path,
136                               const char** args,
137                               const char ** env = 0,
138                               const sys::Path** redirects = 0,
139                               unsigned memoryLimit = 0,
140                               std::string* ErrMsg = 0);
141
142     /// @}
143
144   };
145 }
146 }
147
148 #endif