source: rtems-tools/linkers/rld-process.h @ 7ccb6701

4.104.115
Last change on this file since 7ccb6701 was a136346, checked in by Chris Johns <chrisj@…>, on 08/05/14 at 13:01:15

Fix temporary file handling and add tempfile write support.

Move the static objects into the rld-process file and change the
clean up to a call.

Add support to write to tempfiles.

  • Property mode set to 100644
File size: 5.2 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
32namespace 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 (const std::string& suffix = ".rldxx");
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     * Handle the output files from the process.
86     */
87    class tempfile
88    {
89    public:
90
91      /**
92       * Get a temporary file name.
93       */
94      tempfile (const std::string& suffix = ".rldxx");
95
96      /**
97       * Clean up the temporary file.
98       */
99      ~tempfile ();
100
101      /**
102       * Open the temporary file. It can optionally be written too.
103       */
104      void open (bool writable = false);
105
106      /**
107       * Close the temporary file.
108       */
109      void close ();
110
111      /**
112       * The name of the temp file.
113       */
114      const std::string& name () const;
115
116      /**
117       * Size of the file.
118       */
119      size_t size ();
120
121      /**
122       * Read all the file.
123       */
124      void read (std::string& all);
125
126      /**
127       * Read a line at a time.
128       */
129      void read_line (std::string& line);
130
131      /**
132       * Write the string to the file.
133       */
134      void write (const std::string& s);
135
136      /**
137       * Write the string as a line to the file.
138       */
139      void write_line (const std::string& s);
140
141      /**
142       * Write the strings to the file using a suitable line separator.
143       */
144      void write_lines (const rld::strings& ss);
145
146      /**
147       * Output the file.
148       */
149      void output (const std::string& prefix,
150                   std::ostream&      out,
151                   bool               line_numbers = false);
152
153      /**
154       * Output the file.
155       */
156      void output (std::ostream& out);
157
158    private:
159
160      std::string       _name;    //< The name of the file.
161      const std::string suffix;   //< The temp file's suffix.
162      int               fd;       //< The file descriptor
163      char              buf[256]; //< The read buffer.
164      int               level;    //< The level of data in the buffer.
165    };
166
167    /**
168     * Keep the temporary files.
169     */
170    void set_keep_temporary_files ();
171
172    /**
173     * Clean up the temporaryes.
174     */
175    void temporaries_clean_up ();
176
177    /**
178     * The arguments containter has a single argument per element.
179     */
180    typedef std::vector < std::string > arg_container;
181
182    /**
183     * Execute result.
184     */
185    struct status
186    {
187      enum types
188      {
189        normal, //< The process terminated normally by a call to _exit(2) or exit(3).
190        signal, //< The process terminated due to receipt of a signal.
191        stopped //< The process has not terminated, but has stopped and can be restarted.
192      };
193
194      types type; //< Type of status.
195      int   code; //< The status code returned.
196    };
197
198    /**
199     * Execute a process and capture stdout and stderr. The first element is
200     * the program name to run. Return an error code.
201     */
202    status execute (const std::string&   pname,
203                    const arg_container& args,
204                    const std::string&   outname,
205                    const std::string&   errname);
206
207    /**
208     * Execute a process and capture stdout and stderr given a command line
209     * string. Return an error code.
210     */
211    status execute (const std::string& pname,
212                    const std::string& command,
213                    const std::string& outname,
214                    const std::string& errname);
215
216    /**
217     * Parse a command line into arguments. It support quoting.
218     */
219    void parse_command_line (const std::string& command, arg_container& args);
220  }
221}
222
223#endif
Note: See TracBrowser for help on using the repository browser.