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

namespace butl
{
  // process_path
  //
  inline process_path::
  ~process_path ()
  {
    if (args0_ != nullptr)
      *args0_ = initial;
  }

  inline process_path::
  process_path (const char* i, path&& r, path&& e)
      : initial (i),
        recall (std::move (r)),
        effect (std::move (e)),
        args0_ (nullptr) {}

  inline process_path::
  process_path (process_path&& p)
      : effect (std::move (p.effect)),
        args0_ (p.args0_)
  {
    bool init (p.initial != p.recall.string ().c_str ());

    recall  = std::move (p.recall);
    initial = init ? p.initial : recall.string ().c_str ();

    p.args0_ = nullptr;
  }

  inline process_path& process_path::
  operator= (process_path&& p)
  {
    if (this != &p)
    {
      if (args0_ != nullptr)
        *args0_ = initial; // Restore.

      bool init (p.initial != p.recall.string ().c_str ());

      recall  = std::move (p.recall);
      effect  = std::move (p.effect);
      initial = init ? p.initial : recall.string ().c_str ();

      args0_  = p.args0_;
      p.args0_ = nullptr;
    }

    return *this;
  }

  inline process_path::
  process_path (const process_path& p, bool init)
      : recall (p.recall), effect (p.effect)
  {
    assert (p.args0_ == nullptr);

    if (!p.empty ())
    {
      assert (init == (p.initial != p.recall.string ().c_str ()));
      initial = init ? p.initial : recall.string ().c_str ();
    }
  }

  inline const char* process_path::
  recall_string () const
  {
    return recall.empty () ? initial : recall.string ().c_str ();
  }

  inline const char* process_path::
  effect_string () const
  {
    return effect.empty () ? recall_string () : effect.string ().c_str ();
  }

  // process_exit
  //
#ifdef _WIN32
  inline int process_exit::
  signal () const
  {
    return 0;
  }

  inline bool process_exit::
  core () const
  {
    return false;
  }
#endif

  // process
  //
#ifndef _WIN32
  inline process::id_type process::
  id () const
  {
    return handle;
  }
#endif

  inline process_path process::
  path_search (const char*& a0, const dir_path& fb, bool po)
  {
    process_path r (path_search (a0, true, fb, po));

    if (!r.recall.empty ())
    {
      r.args0_ = &a0;
      a0 = r.recall.string ().c_str ();
    }

    return r;
  }

  inline process_path process::
  path_search (const std::string& f, bool i, const dir_path& fb, bool po)
  {
    return path_search (f.c_str (), i, fb, po);
  }

  inline process_path process::
  path_search (const path& f, bool i, const dir_path& fb, bool po)
  {
    return path_search (f.string ().c_str (), i, fb, po);
  }

  inline process_path process::
  try_path_search (const std::string& f, bool i, const dir_path& fb, bool po)
  {
    return try_path_search (f.c_str (), i, fb, po);
  }

  inline process_path process::
  try_path_search (const path& f, bool i, const dir_path& fb, bool po)
  {
    return try_path_search (f.string ().c_str (), i, fb, po);
  }

  inline process::
  process (optional<process_exit> e)
      : handle (0),
        exit (std::move (e)),
        out_fd (-1),
        in_ofd (-1),
        in_efd (-1)
  {
  }

  inline process::
  process (const process_path& pp, const char* args[],
           int in, int out, int err,
           const char* cwd,
           const char* const* envvars)
      : process (pp,
                 args,
                 pipe (in, -1), pipe (-1, out), pipe (-1, err),
                 cwd,
                 envvars)
  {
  }

  inline process::
  process (const char* args[],
           int in, int out, int err,
           const char* cwd,
           const char* const* envvars)
      : process (path_search (args[0]), args, in, out, err, cwd, envvars) {}

  inline process::
  process (const process_path& pp, const char* args[],
           process& in, int out, int err,
           const char* cwd,
           const char* const* envvars)
      : process (pp, args, in.in_ofd.get (), out, err, cwd, envvars)
  {
    assert (in.in_ofd.get () != -1); // Should be a pipe.
    in.in_ofd.reset (); // Close it on our side.
  }

  inline process::
  process (const char* args[],
           process& in, int out, int err,
           const char* cwd,
           const char* const* envvars)
      : process (path_search (args[0]), args, in, out, err, cwd, envvars) {}

  inline process::
  process (process&& p)
      : handle (p.handle),
        exit   (std::move (p.exit)),
        out_fd (std::move (p.out_fd)),
        in_ofd (std::move (p.in_ofd)),
        in_efd (std::move (p.in_efd))
  {
    p.handle = 0;
  }

  inline process& process::
  operator= (process&& p)
  {
    if (this != &p)
    {
      if (handle != 0)
        wait ();

      handle = p.handle;
      exit   = std::move (p.exit);
      out_fd = std::move (p.out_fd);
      in_ofd = std::move (p.in_ofd);
      in_efd = std::move (p.in_efd);

      p.handle = 0;
    }

    return *this;
  }

  // Implement timed_wait() function templates in terms of their milliseconds
  // specialization.
  //
  template <>
  optional<bool> process::
  timed_wait (const std::chrono::milliseconds&);

  template <typename R, typename P>
  inline optional<bool> process::
  timed_wait (const std::chrono::duration<R, P>& d)
  {
    using namespace std::chrono;
    return timed_wait (duration_cast<milliseconds> (d));
  }

  // process_env
  //
  inline process_env::
  process_env (process_env&& e)
  {
    *this = std::move (e);
  }

  inline process_env& process_env::
  operator= (process_env&& e)
  {
    if (this != &e)
    {
      cwd = e.cwd;

      bool sp (e.path == &e.path_);
      path_ = std::move (e.path_);
      path = sp ? &path_ : e.path;

      bool sv (e.vars == e.vars_.data ());
      vars_ = std::move (e.vars_);
      vars = sv ? vars_.data () : e.vars;
    }

    return *this;
  }
}