aboutsummaryrefslogtreecommitdiff
path: root/libbutl/process-run.txx
blob: a78b87a9a5a2e23601ffa5de743c4c91f9bbfacc (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
// file      : libbutl/process-run.txx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <cassert>
#include <utility> // forward(), index_sequence

namespace butl
{
  template <typename V>
  process_env::
  process_env (const process_path& p, const V& v)
      : process_env (p)
  {
    if (!v.empty ())
    {
      std::string storage;
      process_args_as (vars_, v, storage);
      assert (storage.empty ()); // We don't expect the storage to be used.

      vars_.push_back (nullptr);
      vars = vars_.data ();
    }
  }

  inline int process_stdin  (int v) {assert (v >= 0); return v;}
  inline int process_stdout (int v) {assert (v >= 0); return v;}
  inline int process_stderr (int v) {assert (v >= 0); return v;}

  inline int
  process_stdin (const auto_fd& v) {assert (v.get () >= 0); return v.get ();}

  inline int
  process_stdout (const auto_fd& v) {assert (v.get () >= 0); return v.get ();}

  inline int
  process_stderr (const auto_fd& v) {assert (v.get () >= 0); return v.get ();}

  LIBBUTL_SYMEXPORT process
  process_start (const dir_path* cwd,
                 const process_path& pp,
                 const char* cmd[],
                 const char* const* envvars,
                 int in,
                 int out,
                 int err);

  template <typename V, typename T>
  inline const char*
  process_args_as_wrapper (V& v, const T& x, std::string& storage)
  {
    process_args_as (v, x, storage);
    return nullptr;
  }

  template <typename C,
            typename I,
            typename O,
            typename E,
            typename... A,
            typename std::size_t... index>
  process
  process_start (std::index_sequence<index...>,
                 const C& cmdc,
                 I&& in,
                 O&& out,
                 E&& err,
                 const process_env& env,
                 A&&... args)
  {
    // Map stdin/stdout/stderr arguments to their integer values, as expected
    // by the process constructor.
    //
    int in_i  (process_stdin  (std::forward<I> (in)));
    int out_i (process_stdout (std::forward<O> (out)));
    int err_i (process_stderr (std::forward<E> (err)));

    // Construct the command line array.
    //
    const std::size_t args_size (sizeof... (args));

    small_vector<const char*, args_size + 2> cmd;

    assert (env.path != nullptr);
    cmd.push_back (env.path->recall_string ());

    std::string storage[args_size != 0 ? args_size : 1];

    const char* dummy[] = {
      nullptr, process_args_as_wrapper (cmd, args, storage[index])... };

    cmd.push_back (dummy[0]); // NULL (and get rid of unused warning).

    cmdc (cmd.data (), cmd.size ());

    // @@ Do we need to make sure certain fd's are closed before calling
    //    wait()? Is this only the case with pipes? Needs thinking.

    return process_start (env.cwd,
                          *env.path, cmd.data (),
                          env.vars,
                          in_i, out_i, err_i);
  }

  template <typename C,
            typename I,
            typename O,
            typename E,
            typename... A>
  inline process
  process_start (const C& cmdc,
                 I&& in,
                 O&& out,
                 E&& err,
                 const process_env& env,
                 A&&... args)
  {
    return process_start (std::index_sequence_for<A...> (),
                          cmdc,
                          std::forward<I> (in),
                          std::forward<O> (out),
                          std::forward<E> (err),
                          env,
                          std::forward<A> (args)...);
  }

  template <typename I,
            typename O,
            typename E,
            typename... A>
  inline process
  process_start (I&& in,
                 O&& out,
                 E&& err,
                 const process_env& env,
                 A&&... args)
  {
    return process_start ([] (const char* [], std::size_t) {},
                          std::forward<I> (in),
                          std::forward<O> (out),
                          std::forward<E> (err),
                          env,
                          std::forward<A> (args)...);
  }

  template <typename C,
            typename I,
            typename O,
            typename E,
            typename... A>
  inline process_exit
  process_run (const C& cmdc,
               I&& in,
               O&& out,
               E&& err,
               const process_env& env,
               A&&... args)
  {
    process pr (
      process_start (cmdc,
                     std::forward<I> (in),
                     std::forward<O> (out),
                     std::forward<E> (err),
                     env,
                     std::forward<A> (args)...));

    pr.wait ();
    return *pr.exit;
  }

  template <typename I,
            typename O,
            typename E,
            typename... A>
  inline process_exit
  process_run (I&& in,
               O&& out,
               E&& err,
               const process_env& env,
               A&&... args)
  {
    return process_run ([] (const char* [], std::size_t) {},
                        std::forward<I> (in),
                        std::forward<O> (out),
                        std::forward<E> (err),
                        env,
                        std::forward<A> (args)...);
  }
}