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

#include <utility> // move()

namespace butl
{
  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)
      : initial (p.initial),
        recall (std::move (p.recall)),
        effect (std::move (p.effect)),
        args0_ (p.args0_)
  {
    p.args0_ = nullptr;
  }

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

      initial = p.initial;
      recall = std::move (p.recall);
      effect = std::move (p.effect);
      args0_ = p.args0_;

      p.args0_ = nullptr;
    }

    return *this;
  }

  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 ();
  }

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

    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)
  {
    return path_search (f.c_str (), i, fb);
  }

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

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

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

  inline process::
  process (optional<status_type> s)
      : handle (0),
        status (s),
        out_fd (-1),
        in_ofd (-1),
        in_efd (-1)
  {
  }

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

  inline process::
  process (const process_path& pp, const char* args[],
           int in, int out, int err)
      : process (nullptr, pp, args, in, out, err) {}

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

  inline process::
  process (const process_path& pp, const char* args[],
           process& in, int out, int err)
      : process (nullptr, pp, args, in, out, err) {}

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

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

  inline process::
  process (process&& p)
      : handle (p.handle),
        status (p.status),
        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;
      status = std::move (p.status);
      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;
  }
}