[ec24a37] | 1 | /* |
---|
[a136346] | 2 | * Copyright (c) 2011, Chris Johns <chrisj@rtems.org> |
---|
[ec24a37] | 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. |
---|
[a136346] | 7 | * |
---|
[ec24a37] | 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> |
---|
[718b230] | 31 | #include "rld.h" |
---|
[ec24a37] | 32 | |
---|
| 33 | namespace rld |
---|
| 34 | { |
---|
| 35 | namespace process |
---|
| 36 | { |
---|
[32cd4fc] | 37 | /** |
---|
| 38 | * Temporary file is a name and keep state. |
---|
| 39 | */ |
---|
| 40 | struct tempfile_ref |
---|
| 41 | { |
---|
| 42 | const std::string name; //< The name of the tempfile. |
---|
| 43 | bool keep; //< If true do not delete this file. |
---|
| 44 | |
---|
| 45 | tempfile_ref (const std::string& name, bool keep = false) |
---|
| 46 | : name (name), |
---|
| 47 | keep (keep) { |
---|
| 48 | } |
---|
| 49 | }; |
---|
| 50 | |
---|
[ec24a37] | 51 | /** |
---|
| 52 | * Manage temporary files. We keep these so we can delete them when |
---|
| 53 | * we exit. |
---|
| 54 | */ |
---|
| 55 | class temporary_files |
---|
| 56 | { |
---|
| 57 | public: |
---|
| 58 | /** |
---|
| 59 | * Container of temporary file names. |
---|
| 60 | */ |
---|
[32cd4fc] | 61 | typedef std::list < tempfile_ref > tempfile_container; |
---|
[a136346] | 62 | |
---|
[ec24a37] | 63 | /** |
---|
| 64 | * Construct the temporary files. |
---|
| 65 | */ |
---|
| 66 | temporary_files (); |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * Destruct cleaning up. |
---|
| 70 | */ |
---|
| 71 | ~temporary_files (); |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * Get a new temporary file name. |
---|
| 75 | */ |
---|
[32cd4fc] | 76 | const std::string get (const std::string& suffix = ".rldxx", |
---|
| 77 | bool keep = false); |
---|
[ec24a37] | 78 | |
---|
| 79 | /** |
---|
| 80 | * Remove the temporary file. |
---|
| 81 | */ |
---|
| 82 | void erase (const std::string& name); |
---|
| 83 | |
---|
[32cd4fc] | 84 | /** |
---|
| 85 | * Set the tempfile reference keep state to true. |
---|
| 86 | */ |
---|
| 87 | void keep (const std::string& name); |
---|
| 88 | |
---|
[ec24a37] | 89 | /** |
---|
| 90 | * Remove all temporary files. |
---|
| 91 | */ |
---|
| 92 | void clean_up (); |
---|
| 93 | |
---|
| 94 | private: |
---|
| 95 | |
---|
| 96 | /* |
---|
[32cd4fc] | 97 | * Delete the tempfile given the reference if not keeping. |
---|
[ec24a37] | 98 | */ |
---|
[32cd4fc] | 99 | void unlink (const tempfile_ref& ref); |
---|
[ec24a37] | 100 | |
---|
| 101 | tempfile_container tempfiles; //< The temporary files. |
---|
| 102 | |
---|
| 103 | }; |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | * Handle the output files from the process. |
---|
| 107 | */ |
---|
| 108 | class tempfile |
---|
| 109 | { |
---|
| 110 | public: |
---|
| 111 | |
---|
| 112 | /** |
---|
[32cd4fc] | 113 | * Get a temporary file name given a suffix. |
---|
[ec24a37] | 114 | */ |
---|
[32cd4fc] | 115 | tempfile (const std::string& suffix = ".rldxx", bool keep = false); |
---|
[ec24a37] | 116 | |
---|
| 117 | /** |
---|
| 118 | * Clean up the temporary file. |
---|
| 119 | */ |
---|
| 120 | ~tempfile (); |
---|
| 121 | |
---|
| 122 | /** |
---|
[a136346] | 123 | * Open the temporary file. It can optionally be written too. |
---|
[ec24a37] | 124 | */ |
---|
[a136346] | 125 | void open (bool writable = false); |
---|
[ec24a37] | 126 | |
---|
| 127 | /** |
---|
| 128 | * Close the temporary file. |
---|
| 129 | */ |
---|
| 130 | void close (); |
---|
| 131 | |
---|
[32cd4fc] | 132 | /** |
---|
| 133 | * Override the temp file name automatically assigned with this name. The |
---|
| 134 | * suffix is appended. |
---|
| 135 | */ |
---|
| 136 | void override (const std::string& name); |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * Set the temp file keep state to true so it is not deleted. |
---|
| 140 | */ |
---|
| 141 | void keep (); |
---|
| 142 | |
---|
[ec24a37] | 143 | /** |
---|
| 144 | * The name of the temp file. |
---|
| 145 | */ |
---|
| 146 | const std::string& name () const; |
---|
| 147 | |
---|
| 148 | /** |
---|
| 149 | * Size of the file. |
---|
| 150 | */ |
---|
| 151 | size_t size (); |
---|
| 152 | |
---|
| 153 | /** |
---|
[a136346] | 154 | * Read all the file. |
---|
| 155 | */ |
---|
| 156 | void read (std::string& all); |
---|
| 157 | |
---|
| 158 | /** |
---|
| 159 | * Read a line at a time. |
---|
| 160 | */ |
---|
| 161 | void read_line (std::string& line); |
---|
| 162 | |
---|
| 163 | /** |
---|
| 164 | * Write the string to the file. |
---|
[ec24a37] | 165 | */ |
---|
[a136346] | 166 | void write (const std::string& s); |
---|
[ec24a37] | 167 | |
---|
| 168 | /** |
---|
[a136346] | 169 | * Write the string as a line to the file. |
---|
[ec24a37] | 170 | */ |
---|
[a136346] | 171 | void write_line (const std::string& s); |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | * Write the strings to the file using a suitable line separator. |
---|
| 175 | */ |
---|
| 176 | void write_lines (const rld::strings& ss); |
---|
[ec24a37] | 177 | |
---|
| 178 | /** |
---|
| 179 | * Output the file. |
---|
| 180 | */ |
---|
[a136346] | 181 | void output (const std::string& prefix, |
---|
| 182 | std::ostream& out, |
---|
[ec24a37] | 183 | bool line_numbers = false); |
---|
| 184 | |
---|
| 185 | /** |
---|
| 186 | * Output the file. |
---|
| 187 | */ |
---|
| 188 | void output (std::ostream& out); |
---|
| 189 | |
---|
| 190 | private: |
---|
| 191 | |
---|
[32cd4fc] | 192 | std::string _name; //< The name of the file. |
---|
| 193 | const std::string suffix; //< The temp file's suffix. |
---|
| 194 | bool overridden; //< The name is overridden; may no exist. |
---|
| 195 | int fd; //< The file descriptor |
---|
| 196 | char buf[256]; //< The read buffer. |
---|
[67369b14] | 197 | size_t level; //< The level of data in the buffer. |
---|
[ec24a37] | 198 | }; |
---|
[a136346] | 199 | |
---|
| 200 | /** |
---|
| 201 | * Keep the temporary files. |
---|
| 202 | */ |
---|
| 203 | void set_keep_temporary_files (); |
---|
| 204 | |
---|
| 205 | /** |
---|
| 206 | * Clean up the temporaryes. |
---|
| 207 | */ |
---|
| 208 | void temporaries_clean_up (); |
---|
| 209 | |
---|
[ec24a37] | 210 | /** |
---|
| 211 | * The arguments containter has a single argument per element. |
---|
| 212 | */ |
---|
| 213 | typedef std::vector < std::string > arg_container; |
---|
| 214 | |
---|
[8807135] | 215 | /** |
---|
| 216 | * Split a string and append to the arguments. |
---|
| 217 | */ |
---|
| 218 | void args_append (arg_container& args, const std::string& str); |
---|
| 219 | |
---|
[ec24a37] | 220 | /** |
---|
| 221 | * Execute result. |
---|
| 222 | */ |
---|
| 223 | struct status |
---|
| 224 | { |
---|
| 225 | enum types |
---|
| 226 | { |
---|
| 227 | normal, //< The process terminated normally by a call to _exit(2) or exit(3). |
---|
| 228 | signal, //< The process terminated due to receipt of a signal. |
---|
| 229 | stopped //< The process has not terminated, but has stopped and can be restarted. |
---|
| 230 | }; |
---|
[a136346] | 231 | |
---|
[ec24a37] | 232 | types type; //< Type of status. |
---|
| 233 | int code; //< The status code returned. |
---|
| 234 | }; |
---|
| 235 | |
---|
| 236 | /** |
---|
| 237 | * Execute a process and capture stdout and stderr. The first element is |
---|
| 238 | * the program name to run. Return an error code. |
---|
| 239 | */ |
---|
[a136346] | 240 | status execute (const std::string& pname, |
---|
[ec24a37] | 241 | const arg_container& args, |
---|
| 242 | const std::string& outname, |
---|
| 243 | const std::string& errname); |
---|
| 244 | |
---|
| 245 | /** |
---|
| 246 | * Execute a process and capture stdout and stderr given a command line |
---|
| 247 | * string. Return an error code. |
---|
| 248 | */ |
---|
[a136346] | 249 | status execute (const std::string& pname, |
---|
[ec24a37] | 250 | const std::string& command, |
---|
| 251 | const std::string& outname, |
---|
| 252 | const std::string& errname); |
---|
| 253 | |
---|
| 254 | /** |
---|
| 255 | * Parse a command line into arguments. It support quoting. |
---|
| 256 | */ |
---|
| 257 | void parse_command_line (const std::string& command, arg_container& args); |
---|
| 258 | } |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | #endif |
---|