aboutsummaryrefslogtreecommitdiff
path: root/libbutl/command.cxx
blob: c23dfd54e8c96b5bd6f4734c58a63860339f6743 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// file      : libbutl/command.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_modules_ts
#include <libbutl/command.mxx>
#endif

#include <cassert>

#ifndef __cpp_lib_modules_ts
#include <map>
#include <string>
#include <cstddef>
#include <functional>

#include <ios>          // ios::failure
#include <vector>
#include <utility>      // move()
#include <stdexcept>    // invalid_argument
#include <system_error>
#endif

// Other includes.

#ifdef __cpp_modules_ts
module butl.command;

// Only imports additional to interface.
#ifdef __clang__
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.process;
import butl.optional;
#endif

import butl.builtin;
import butl.fdstream;
import butl.string_parser;
#else
#include <libbutl/builtin.mxx>
#include <libbutl/fdstream.mxx>
#include <libbutl/string-parser.mxx>
#endif

using namespace std;

namespace butl
{
  string
  command_substitute (const string& s, size_t sp,
                      const function<command_substitution_callback>& sc,
                      char open, char close)
  {
    string r;
    size_t p (0); // Current parsing position.

    for (size_t n (s.size ()); sp != string::npos; sp = s.find (open, ++p))
    {
      // Append the source string fraction preceding this substitution.
      //
      r.append (s, p, sp - p);

      // See if this is an escape of the opening substitution character
      // (adjacent characters).
      //
      if (++sp != n && s[sp] == open)
      {
        p = sp;
        r += open;
        continue;
      }

      // In sp we now have the start of the variable name. Find its end.
      //
      p = s.find (close, sp);

      // Verify that the variable name is properly terminated, not empty, and
      // doesn't contain whitespaces.
      //
      if (p == string::npos)
        throw invalid_argument (string ("unmatched substitution character '") +
                                open + "'");

      if (p == sp)
        throw invalid_argument ("empty substitution variable");

      string vn (s, sp, p - sp);

      if (vn.find_first_of (" \t") != string::npos)
        throw invalid_argument ("whitespace in substitution variable '" +
                                vn + "'");

      // Find the variable and append its value or fail if it's unknown.
      //
      if (!sc (vn, r))
        throw invalid_argument ("unknown substitution variable '" + vn + "'");
    }

    // Append the source string tail following the last substitution.
    //
    r.append (s.begin () + p, s.end ());

    return r;
  }

  string
  command_substitute (const string& s, size_t sp,
                      const command_substitution_map& sm,
                      char o, char c)
  {
    return command_substitute (
      s,
      sp,
      [&sm] (const string& vn, string& r)
      {
        auto i (sm.find (vn));
        if (i == sm.end ())
          return false;
        r += i->second;
        return true;
      },
      o, c);
  }

  process_exit
  command_run (const string& cmd_str,
               const optional<process_env>& env,
               const optional<command_substitution_map>& substitutions,
               char subst,
               const function<command_callback>& callback)
  {
    // Split the command line into the program path, arguments, and redirects,
    // removing one level of quoting.
    //
    // Note: may throw invalid_argument.
    //
    vector<string> cmd (
      string_parser::parse_quoted (cmd_str, true /* unquote */));

    if (cmd.empty ())
      throw invalid_argument ("no program path specified");

    // Perform substitutions in a string. Throw invalid_argument for a
    // malformed substitution or an unknown variable name.
    //
    auto substitute = [&substitutions, subst] (string&& s)
    {
      size_t sp;
      if (substitutions && (sp = s.find (subst)) != string::npos)
        return command_substitute (s, sp, *substitutions, subst, subst);

      return move (s);
    };

    // Perform substitutions in the program path.
    //
    string prog (substitute (move (cmd.front ())));

    // Sort the remaining command line elements into the arguments and
    // redirects, performing the substitutions. Complete relative redirects
    // using CWD and use the rightmost redirect.
    //
    vector<string> args;

    optional<dir_path> redir;
    bool redir_append (false);

    const dir_path& cwd (env && env->cwd != nullptr ? *env->cwd : dir_path ());

    for (auto i (cmd.begin () + 1), e (cmd.end ()); i != e; ++i)
    {
      string a (move (*i));

      if (a[0] == '>') // Redirect.
      {
        redir_append = a[1] == '>';

        size_t n (redir_append ? 2 : 1);

        if (a.size () != n) // Strip the >/>> prefix.
        {
          a.erase (0, n);
        }
        else // Take the space-separated file path from the next element.
        {
          if (++i == e)
            throw invalid_argument ("no stdout redirect file specified");

          a = move (*i);
        }

        try
        {
          redir = dir_path (substitute (move (a)));
        }
        catch (const invalid_path& e)
        {
          throw invalid_argument ("invalid stdout redirect file path '" +
                                  e.path + "'");
        }

        if (redir->empty ())
          throw invalid_argument ("empty stdout redirect file path");

        if (redir->relative () && !cwd.empty ())
          redir = cwd / *redir;
      }
      else             // Argument.
      {
        args.push_back (substitute (move (a)));
      }
    }

    // Open the redirect file descriptor, if specified.
    //
    // Intercept the exception to make the error description more informative.
    //
    auto_fd rd;

    if (redir)
    try
    {
      fdopen_mode m (fdopen_mode::out | fdopen_mode::create);
      m |= redir_append ? fdopen_mode::at_end : fdopen_mode::truncate;

      rd = fdopen (*redir, m);
    }
    catch (const ios::failure& e)
    {
      // @@ For libstdc++ the resulting exception description will be
      //    something like:
      //
      //    unable to open stdout redirect file ...: No such file or directory
      //
      //    Maybe we should improve our operator<<(ostream,exception) to
      //    lowercase the first word that follows ": " (code description) for
      //    exceptions derived from system_error.
      //
      string msg ("unable to open stdout redirect file '" + redir->string () +
                  "'");

      // For old versions of g++ (as of 4.9) ios_base::failure is not derived
      // from system_error and so we cannot recover the errno value. Lets use
      // EIO in this case. This is a temporary code after all.
      //
      const system_error* se (dynamic_cast<const system_error*> (&e));

      throw_generic_ios_failure (se != nullptr ? se->code ().value () : EIO,
                                 msg.c_str ());
    }

    const builtin_info* bi (builtins.find (prog));

    if (bi != nullptr && bi->function != nullptr) // Execute the builtin.
    {
      if (callback)
      {
        // Build the complete arguments list, appending the stdout redirect,
        // if present.
        //
        vector<const char*> elems ({prog.c_str ()});
        for (const auto& a: args)
          elems.push_back (a.c_str ());

        string rd;
        if (redir)
        {
          rd = (redir_append ? ">>" : ">");
          rd += redir->string ();

          elems.push_back (rd.c_str ());
        }

        elems.push_back (nullptr);

        callback (elems.data (), elems.size ());
      }

      // Finally, run the builtin.
      //
      uint8_t r; // Storage.
      builtin_callbacks cb;

      builtin b (bi->function (r,
                               args,
                               nullfd    /* stdin */,
                               move (rd) /* stdout */,
                               nullfd    /* stderr */,
                               cwd,
                               cb));

      return process_exit (b.wait ());
    }
    else                                          // Execute the program.
    {
      // Strip the potential leading `^`, indicating that this is an external
      // program rather than a builtin. Consider only simple paths and don't
      // end up with an empty path.
      //
      const char* p (prog.size () > 1 &&
                     prog[0] == '^'   &&
                     path::traits_type::find_separator (prog) == string::npos
                     ? prog.c_str () + 1
                     : prog.c_str ());

      // Prepare the process environment.
      //
      // Note: cwd passed to process_env() may not be a temporary object.
      //
      process_env pe (p, cwd, env ? env->vars : nullptr);

      // Finally, run the process.
      //
      // If the callback is specified, then intercept its call, appending the
      // stdout redirect to the arguments list, if present.
      //
      return process_run_callback (
        [&callback, &redir, redir_append] (const char* const args[], size_t n)
        {
          if (callback)
          {
            if (redir)
            {
              vector<const char*> elems (args, args + n);
              string rd ((redir_append ? ">>" : ">") + redir->string ());

              // Inject the redirect prior to the trailing NULL.
              //
              assert (n > 0);

              elems.insert (elems.begin () + n - 1, rd.c_str ());

              callback (elems.data (), elems.size ());
            }
            else
              callback (args, n);
          }
        },
        0                     /* stdin */,
        redir ? rd.get () : 1 /* stdout */,
        2                     /* stderr */,
        pe,
        args);
    }
  }
}