[ec24a37] | 1 | /* |
---|
| 2 | * Copyright (c) 2011, Chris Johns <chrisj@rtems.org> |
---|
| 3 | * |
---|
| 4 | * Permission to use, copy, modify, and/or distribute this software for any |
---|
| 5 | * purpose with or without fee is hereby granted, provided that the above |
---|
| 6 | * copyright notice and this permission notice appear in all copies. |
---|
| 7 | * |
---|
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
| 15 | */ |
---|
| 16 | /** |
---|
| 17 | * @file |
---|
| 18 | * |
---|
| 19 | * @ingroup rtems-ld |
---|
| 20 | * |
---|
| 21 | * @brief Process execute and various supporting parts. |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #if !defined (_RLD_PEX_H_) |
---|
| 26 | #define _RLD_PEX_H_ |
---|
| 27 | |
---|
| 28 | #include <list> |
---|
| 29 | #include <string> |
---|
| 30 | #include <vector> |
---|
| 31 | |
---|
| 32 | namespace rld |
---|
| 33 | { |
---|
| 34 | namespace process |
---|
| 35 | { |
---|
| 36 | /** |
---|
| 37 | * Manage temporary files. We keep these so we can delete them when |
---|
| 38 | * we exit. |
---|
| 39 | */ |
---|
| 40 | class temporary_files |
---|
| 41 | { |
---|
| 42 | public: |
---|
| 43 | /** |
---|
| 44 | * Container of temporary file names. |
---|
| 45 | */ |
---|
| 46 | typedef std::list < std::string > tempfile_container; |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Construct the temporary files. |
---|
| 50 | */ |
---|
| 51 | temporary_files (); |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * Destruct cleaning up. |
---|
| 55 | */ |
---|
| 56 | ~temporary_files (); |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * Get a new temporary file name. |
---|
| 60 | */ |
---|
| 61 | const std::string get (); |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Remove the temporary file. |
---|
| 65 | */ |
---|
| 66 | void erase (const std::string& name); |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * Remove all temporary files. |
---|
| 70 | */ |
---|
| 71 | void clean_up (); |
---|
| 72 | |
---|
| 73 | private: |
---|
| 74 | |
---|
| 75 | /* |
---|
| 76 | * Delete the file. |
---|
| 77 | */ |
---|
| 78 | void unlink (const std::string& name); |
---|
| 79 | |
---|
| 80 | tempfile_container tempfiles; //< The temporary files. |
---|
| 81 | |
---|
| 82 | }; |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | * The temporary files. |
---|
| 86 | */ |
---|
| 87 | static temporary_files temporaries; |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * Handle the output files from the process. |
---|
| 91 | */ |
---|
| 92 | class tempfile |
---|
| 93 | { |
---|
| 94 | public: |
---|
| 95 | |
---|
| 96 | /** |
---|
| 97 | * Get a temporary file name. |
---|
| 98 | */ |
---|
| 99 | tempfile (); |
---|
| 100 | |
---|
| 101 | /** |
---|
| 102 | * Clean up the temporary file. |
---|
| 103 | */ |
---|
| 104 | ~tempfile (); |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * Open the temporary file. |
---|
| 108 | */ |
---|
| 109 | void open (); |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * Close the temporary file. |
---|
| 113 | */ |
---|
| 114 | void close (); |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | * The name of the temp file. |
---|
| 118 | */ |
---|
| 119 | const std::string& name () const; |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | * Size of the file. |
---|
| 123 | */ |
---|
| 124 | size_t size (); |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | * Get all the file. |
---|
| 128 | */ |
---|
| 129 | void get (std::string& all); |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | * Get time. |
---|
| 133 | */ |
---|
| 134 | void getline (std::string& line); |
---|
| 135 | |
---|
| 136 | /** |
---|
| 137 | * Output the file. |
---|
| 138 | */ |
---|
| 139 | void output (const std::string& prefix, |
---|
| 140 | std::ostream& out, |
---|
| 141 | bool line_numbers = false); |
---|
| 142 | |
---|
| 143 | /** |
---|
| 144 | * Output the file. |
---|
| 145 | */ |
---|
| 146 | void output (std::ostream& out); |
---|
| 147 | |
---|
| 148 | private: |
---|
| 149 | |
---|
| 150 | std::string _name; //< The name of the file. |
---|
| 151 | int fd; //< The file descriptor |
---|
| 152 | char buf[256]; //< The read buffer. |
---|
| 153 | int level; //< The level of data in the buffer. |
---|
| 154 | }; |
---|
| 155 | |
---|
| 156 | /** |
---|
| 157 | * The arguments containter has a single argument per element. |
---|
| 158 | */ |
---|
| 159 | typedef std::vector < std::string > arg_container; |
---|
| 160 | |
---|
| 161 | /** |
---|
| 162 | * Execute result. |
---|
| 163 | */ |
---|
| 164 | struct status |
---|
| 165 | { |
---|
| 166 | enum types |
---|
| 167 | { |
---|
| 168 | normal, //< The process terminated normally by a call to _exit(2) or exit(3). |
---|
| 169 | signal, //< The process terminated due to receipt of a signal. |
---|
| 170 | stopped //< The process has not terminated, but has stopped and can be restarted. |
---|
| 171 | }; |
---|
| 172 | |
---|
| 173 | types type; //< Type of status. |
---|
| 174 | int code; //< The status code returned. |
---|
| 175 | }; |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | * Execute a process and capture stdout and stderr. The first element is |
---|
| 179 | * the program name to run. Return an error code. |
---|
| 180 | */ |
---|
| 181 | status execute (const std::string& pname, |
---|
| 182 | const arg_container& args, |
---|
| 183 | const std::string& outname, |
---|
| 184 | const std::string& errname); |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | * Execute a process and capture stdout and stderr given a command line |
---|
| 188 | * string. Return an error code. |
---|
| 189 | */ |
---|
| 190 | status execute (const std::string& pname, |
---|
| 191 | const std::string& command, |
---|
| 192 | const std::string& outname, |
---|
| 193 | const std::string& errname); |
---|
| 194 | |
---|
| 195 | /** |
---|
| 196 | * Parse a command line into arguments. It support quoting. |
---|
| 197 | */ |
---|
| 198 | void parse_command_line (const std::string& command, arg_container& args); |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | #endif |
---|