aboutsummaryrefslogtreecommitdiff
path: root/tests/builtin/driver.cxx
blob: bab74aa1f54d94423a70b8b61a599e8bb466e0fe (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
// file      : tests/builtin/driver.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifdef _WIN32
#  include <libbutl/win32-utility.hxx>
#endif

#include <cassert>

#ifndef __cpp_lib_modules_ts
#include <string>
#include <vector>
#include <chrono>
#include <utility>  // move()
#include <cstdint>  // uint8_t
#include <ostream>
#include <iostream>
#ifndef _WIN32
#  include <thread> // this_thread::sleep_for()
#endif
#endif

// Other includes.

#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.path;
import butl.utility;   // eof()
import butl.builtin;
import butl.optional;
import butl.timestamp; // to_stream(duration)
#else
#include <libbutl/path.mxx>
#include <libbutl/utility.mxx>
#include <libbutl/builtin.mxx>
#include <libbutl/optional.mxx>
#include <libbutl/timestamp.mxx>
#endif

using namespace std;
using namespace butl;

inline ostream&
operator<< (ostream& os, const path& p)
{
  return os << p.representation ();
}

// Usage: argv[0] [-d <dir>] [-o <opt>] [-c] [-i] [-t <msec>] [-s <sec>]
//        <builtin> <builtin-args>
//
// Execute the builtin and exit with its exit status.
//
// -d <dir>   use as a current working directory
// -c         use callbacks that, in particular, trace calls to stdout
// -o <opt>   additional builtin option recognized by the callback
// -i         read lines from stdin and append them to the builtin arguments
// -t <msec>  print diag if the builtin didn't complete in <msec> milliseconds
// -s <sec>   sleep <sec> seconds prior to running the builtin
//
// Note that the 'roundtrip' builtin name is also recognized and results in
// running the pseudo-builtin that just roundtrips stdin to stdout.
//
int
main (int argc, char* argv[])
{
  using butl::optional;

  cin.exceptions  (ios::badbit);
  cout.exceptions (ios::failbit | ios::badbit);
  cerr.exceptions (ios::failbit | ios::badbit);

  bool in (false);
  dir_path cwd;
  string option;
  builtin_callbacks callbacks;
  optional<duration> timeout;
  optional<chrono::seconds> sec;

  string name;
  vector<string> args;

  auto flag = [] (bool v) {return v ? "true" : "false";};

  auto num = [] (const string& s)
  {
    assert (!s.empty ());

    char* e (nullptr);
    errno = 0; // We must clear it according to POSIX.
    uint64_t r (strtoull (s.c_str (), &e, 10)); // Can't throw.
    assert (errno != ERANGE && e == s.c_str () + s.size ());
    return r;
  };

  // Parse the driver options and arguments.
  //
  int i (1);
  for (; i != argc; ++i)
  {
    string a (argv[i]);

    if (a == "-d")
    {
      ++i;

      assert (i != argc);
      cwd = dir_path (argv[i]);
    }
    else if (a == "-o")
    {
      ++i;

      assert (i != argc);
      option = argv[i];
    }
    else if (a == "-c")
    {
      callbacks = builtin_callbacks (
        [&flag] (const path& p, bool pre)
        {
          cout << "create " << p << ' ' << flag (pre) << endl;
        },
        [&flag] (const path& from, const path& to, bool force, bool pre)
        {
          cout << "move " << from << ' ' << to << ' ' << flag (force) << ' '
                          << flag (pre) << endl;
        },
        [&flag] (const path& p, bool force, bool pre)
        {
          cout << "remove " << p << ' ' << flag (force) << ' ' << flag (pre)
                            << endl;
        },
        [&option] (const vector<string>& args, size_t i)
        {
          cout << "option " << args[i] << endl;
          return !option.empty () && args[i] == option ? 1 : 0;
        },
        [] (const duration& d)
        {
          cout << "sleep ";
          to_stream (cout, d, false /* nanoseconds */);
          cout << endl;
        }
      );
    }
    else if (a == "-i")
    {
      in = true;
    }
    else if (a == "-t")
    {
      ++i;

      assert (i != argc);
      timeout = chrono::milliseconds (num (argv[i]));
    }
    else if (a == "-s")
    {
      ++i;

      assert (i != argc);
      sec = chrono::seconds (num (argv[i]));
    }
    else
      break;
  }

  // Parse the builtin name and arguments.
  //
  assert (i != argc);
  name = argv[i++];

  for (; i != argc; ++i)
    args.push_back (argv[i]);

  // Read out additional arguments from stdin.
  //
  if (in)
  {
    string s;
    while (!eof (getline (cin, s)))
      args.push_back (move (s));
  }

  auto sleep = [&sec] ()
  {
    if (sec)
    {
      // MINGW GCC 4.9 doesn't implement this_thread so use Win32 Sleep().
      //
#ifndef _WIN32
      this_thread::sleep_for (*sec);
#else
      Sleep (static_cast<DWORD> (sec->count () * 1000));
#endif
    }
  };

  auto wait = [&timeout] (builtin& b)
  {
    optional<uint8_t> r;

    if (timeout)
    {
      r = b.timed_wait (*timeout);

      if (!r)
      {
        cerr << "timeout expired" << endl;

        b.wait ();
        r = 1;
      }
    }
    else
      r = b.wait ();

    assert (b.try_wait ()); // While at it, test try_wait().

    return *r;
  };

  // Execute the builtin.
  //
  if (name != "roundtrip")
  {
    const builtin_info* bi (builtins.find (name));

    if (bi == nullptr)
    {
      cerr << "unknown builtin '" << name << "'" << endl;
      return 1;
    }

    if (bi->function == nullptr)
    {
      cerr << "external builtin '" << name << "'" << endl;
      return 1;
    }

    sleep ();

    uint8_t r; // Storage.
    builtin b (bi->function (r, args, nullfd, nullfd, nullfd, cwd, callbacks));
    return wait (b);
  }
  else
  {
    uint8_t r; // Storage.

    auto run = [&r, &sleep] ()
    {
      // While at it, test that a non-copyable lambda can be used as a
      // builtin.
      //
      auto_fd fd;

      return pseudo_builtin (
        r,
        [&sleep, fd = move (fd)] () mutable noexcept
        {
          fd.reset ();

          sleep ();

          if (cin.peek () != istream::traits_type::eof ())
            cout << cin.rdbuf ();

          return 0;
        });
    };

    builtin b (run ());
    return wait (b);
  }
}