source: rtems-tools/rtemstoolkit/rld-process.h @ 37a0843

5
Last change on this file since 37a0843 was 87e0e76, checked in by Chris Johns <chrisj@…>, on 09/13/14 at 02:09:16

Refactor code into the RTEMS Toolkit.

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