source: rtems-tools/rtemstoolkit/rld-process.h @ 6c79a16

5
Last change on this file since 6c79a16 was 718b230, checked in by Cillian O'Donnell <cpodonnell8@…>, on 05/31/17 at 08:26:18

rtemstoolkit/rld-process.h: Include rld.h to find 'strings' definition

  • Property mode set to 100644
File size: 6.3 KB
Line 
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#include "rld.h"
32
33namespace rld
34{
35  namespace process
36  {
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
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       */
61      typedef std::list < tempfile_ref > tempfile_container;
62
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       */
76      const std::string get (const std::string& suffix = ".rldxx",
77                             bool               keep = false);
78
79      /**
80       * Remove the temporary file.
81       */
82      void erase (const std::string& name);
83
84      /**
85       * Set the tempfile reference keep state to true.
86       */
87      void keep (const std::string& name);
88
89      /**
90       * Remove all temporary files.
91       */
92      void clean_up ();
93
94    private:
95
96      /*
97       * Delete the tempfile given the reference if not keeping.
98       */
99      void unlink (const tempfile_ref& ref);
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      /**
113       * Get a temporary file name given a suffix.
114       */
115      tempfile (const std::string& suffix = ".rldxx", bool keep = false);
116
117      /**
118       * Clean up the temporary file.
119       */
120      ~tempfile ();
121
122      /**
123       * Open the temporary file. It can optionally be written too.
124       */
125      void open (bool writable = false);
126
127      /**
128       * Close the temporary file.
129       */
130      void close ();
131
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
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      /**
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.
165       */
166      void write (const std::string& s);
167
168      /**
169       * Write the string as a line to the file.
170       */
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);
177
178      /**
179       * Output the file.
180       */
181      void output (const std::string& prefix,
182                   std::ostream&      out,
183                   bool               line_numbers = false);
184
185      /**
186       * Output the file.
187       */
188      void output (std::ostream& out);
189
190    private:
191
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.
197      size_t            level;      //< The level of data in the buffer.
198    };
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
210    /**
211     * The arguments containter has a single argument per element.
212     */
213    typedef std::vector < std::string > arg_container;
214
215    /**
216     * Split a string and append to the arguments.
217     */
218    void args_append (arg_container& args, const std::string& str);
219
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      };
231
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     */
240    status execute (const std::string&   pname,
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     */
249    status execute (const std::string& pname,
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
Note: See TracBrowser for help on using the repository browser.