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
|
// file : tests/command/driver.cxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#include <cassert>
#ifndef __cpp_lib_modules_ts
#include <ios>
#include <string>
#include <vector>
#include <iostream>
#include <stdexcept> // invalid_argument
#include <system_error>
#endif
// Other includes.
#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.path;
import butl.path_io;
import butl.process; // process::print()
import butl.command;
import butl.utility;
import butl.optional;
#else
#include <libbutl/path.mxx>
#include <libbutl/path-io.mxx>
#include <libbutl/process.mxx>
#include <libbutl/command.mxx>
#include <libbutl/utility.mxx>
#include <libbutl/optional.mxx>
#endif
using namespace std;
using namespace butl;
// Usages:
//
// argv[0] [-d <dir>] [-v <name>[=<value>]] [-s <name>=<value>] [-c <char>]
// [-p] <command>
//
// argv[0] -C [-A] [-D] [-V <name>] [-S <status>] <arguments>
//
// In the first form run the specified command, changing the current
// directory, (re)setting the environment variables, performing substitutions,
// and printing the "expanded" command line, if requested.
//
// In the second form optionally print the program arguments, CWD, the
// environment variable values and exit with the status specified. This mode
// is normally used for the command being tested to dump the environment
// obtained from the caller.
//
// -d <dir>
// Change the CWD for the command process.
//
// -v <name>[=<value>]
// (Un)set the environment variable for the command process. Can be
// specified multiple times.
//
// -s <name>=<value>
// Perform command line substitutions using the specified variable. Can be
// specified multiple times.
//
// -c <char>
// Substitution symbol. The default is '@'.
//
// -p
// Print the "expanded" command line.
//
// -C
// Print the program arguments (-A), CWD (-D), environment variables (-V)
// to stdout and exit with the status specifies (-S).
//
// -A
// Print the program arguments to stdout.
//
// -D
// Print the process CWD to stdout.
//
// -V <name>
// Print the environment variable value (or <unset>) to stdout. Can be
// specified multiple times.
//
// -S <status>
// Exit with the specified status code.
//
int
main (int argc, const char* argv[])
{
using butl::optional;
using butl::getenv;
// Parse and validate the arguments.
//
dir_path cwd;
vector<const char*> vars;
optional<command_substitution_map> substitutions;
char subst ('@');
optional<string> command;
bool print (false);
for (int i (1); i != argc; ++i)
{
string o (argv[i]);
if (o == "-d")
{
assert (++i != argc);
cwd = dir_path (argv[i]);
}
else if (o == "-v")
{
assert (++i != argc);
vars.push_back (argv[i]);
}
else if (o == "-s")
{
assert (++i != argc);
if (!substitutions)
substitutions = command_substitution_map ();
string v (argv[i]);
size_t p (v.find ('='));
assert (p != string::npos && p != 0);
(*substitutions)[string (v, 0, p)] = string (v, p + 1);
}
else if (o == "-c")
{
assert (++i != argc);
string v (argv[i]);
assert (v.size () == 1);
subst = v[0];
}
else if (o == "-p")
{
print = true;
}
else if (o == "-C")
{
assert (i == 1); // Must go first.
int ec (0);
bool print_cwd (false);
// Include the program path into the arguments list.
//
vector<const char*> args ({argv[0]});
for (++i; i != argc; ++i)
{
o = argv[i];
if (o == "-A")
{
print = true;
}
else if (o == "-D")
{
print_cwd = true;
}
else if (o == "-V")
{
assert (++i != argc);
vars.push_back (argv[i]);
}
else if (o == "-S")
{
assert (++i != argc);
ec = stoi (argv[i]);
}
else
args.push_back (argv[i]);
}
args.push_back (nullptr);
if (print)
{
process::print (cout, args.data ());
cout << endl;
}
if (print_cwd)
cout << dir_path::current_directory () << endl;
for (const auto& v: vars)
{
optional<string> vv (getenv (v));
cout << (vv ? *vv : "<unset>") << endl;
}
return ec;
}
else
{
assert (!command);
command = argv[i];
}
}
assert (command);
// Run the command.
//
try
{
optional<process_env> pe;
if (!cwd.empty () || !vars.empty ())
pe = process_env (process_path (), cwd, vars);
process_exit e (command_run (*command,
pe,
substitutions,
subst,
[print] (const char* const args[], size_t n)
{
if (print)
{
process::print (cout, args, n);
cout << endl;
}
}));
if (!e)
cerr << "process " << argv[0] << " " << e << endl;
return e.normal () ? e.code () : 1;
}
catch (const invalid_argument& e)
{
cerr << e << endl;
return 1;
}
catch (const ios::failure& e)
{
cerr << e << endl;
return 1;
}
// Also handles process_error exception (derived from system_error).
//
catch (const system_error& e)
{
cerr << e << endl;
return 1;
}
return 0;
}
|