2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 - 2020 Claire Wolf <claire@symbioticeda.com>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "kernel/register.h"
21 #include "kernel/log.h"
26 # define WIFEXITED(x) 1
27 # define WIFSIGNALED(x) 0
28 # define WIFSTOPPED(x) 0
29 # define WEXITSTATUS(x) ((x) & 0xff)
30 # define WTERMSIG(x) SIGTERM
31 # define WSTOPSIG(x) 0
33 # include <sys/wait.h>
37 PRIVATE_NAMESPACE_BEGIN
39 struct ExecPass
: public Pass
{
40 ExecPass() : Pass("exec", "execute commands in the operating system shell") { }
41 void help() YS_OVERRIDE
43 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
45 log(" exec [options] -- [command]\n");
47 log("Execute a command in the operating system shell. All supplied arguments are\n");
48 log("concatenated and passed as a command to popen(3). Whitespace is not guaranteed\n");
49 log("to be preserved, even if quoted. stdin and stderr are not connected, while stdout is\n");
50 log("logged unless the \"-q\" option is specified.\n");
54 log(" Suppress stdout and stderr from subprocess\n");
56 log(" -expect-return <int>\n");
57 log(" Generate an error if popen() does not return specified value.\n");
58 log(" May only be specified once; the final specified value is controlling\n");
59 log(" if specified multiple times.\n");
61 log(" -expect-stdout <regex>\n");
62 log(" Generate an error if the specified regex does not match any line\n");
63 log(" in subprocess's stdout. May be specified multiple times.\n");
65 log(" -not-expect-stdout <regex>\n");
66 log(" Generate an error if the specified regex matches any line\n");
67 log(" in subprocess's stdout. May be specified multiple times.\n");
70 log(" Example: exec -q -expect-return 0 -- echo \"bananapie\" | grep \"nana\"\n");
74 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) YS_OVERRIDE
78 std::string linebuf
= "";
79 bool flag_cmd
= false;
80 bool flag_quiet
= false;
81 bool flag_expect_return
= false;
82 int expect_return_value
= 0;
83 bool flag_expect_stdout
= false;
84 struct expect_stdout_elem
{
86 bool polarity
; //true: this regex must match at least one line
87 //false: this regex must not match any line
91 expect_stdout_elem() : matched(false), polarity(true), str(), re(){};
93 std::vector
<expect_stdout_elem
> expect_stdout
;
96 log_cmd_error("No command provided.\n");
98 for(size_t argidx
= 1; argidx
< args
.size(); ++argidx
) {
100 cmd
+= args
[argidx
] + (argidx
!= (args
.size() - 1)? " " : "");
102 if (args
[argidx
] == "--")
104 else if (args
[argidx
] == "-q")
106 else if (args
[argidx
] == "-expect-return") {
107 flag_expect_return
= true;
109 if (argidx
>= args
.size())
110 log_cmd_error("No expected return value specified.\n");
112 expect_return_value
= atoi(args
[argidx
].c_str());
113 } else if (args
[argidx
] == "-expect-stdout") {
114 flag_expect_stdout
= true;
116 if (argidx
>= args
.size())
117 log_cmd_error("No expected regular expression specified.\n");
120 expect_stdout_elem x
;
121 x
.str
= args
[argidx
];
122 x
.re
= YS_REGEX_COMPILE(args
[argidx
]);
123 expect_stdout
.push_back(x
);
124 } catch (const YS_REGEX_NS::regex_error
& e
) {
125 log_cmd_error("Error in regex expression '%s' !\n", args
[argidx
].c_str());
127 } else if (args
[argidx
] == "-not-expect-stdout") {
128 flag_expect_stdout
= true;
130 if (argidx
>= args
.size())
131 log_cmd_error("No expected regular expression specified.\n");
134 expect_stdout_elem x
;
135 x
.str
= args
[argidx
];
136 x
.re
= YS_REGEX_COMPILE(args
[argidx
]);
138 expect_stdout
.push_back(x
);
139 } catch (const YS_REGEX_NS::regex_error
& e
) {
140 log_cmd_error("Error in regex expression '%s' !\n", args
[argidx
].c_str());
144 log_cmd_error("Unknown option \"%s\" or \"--\" doesn\'t precede command.", args
[argidx
].c_str());
148 log_header(design
, "Executing command \"%s\".\n", cmd
.c_str());
152 bool keep_reading
= true;
157 FILE *f
= popen(cmd
.c_str(), "r");
159 log_cmd_error("errno %d after popen() returned NULL.\n", errno
);
160 while (keep_reading
) {
161 keep_reading
= (fgets(buf
, sizeof(buf
), f
) != nullptr);
163 memset(buf
, 0, sizeof(buf
));
165 auto pos
= linebuf
.find('\n');
166 while (pos
!= std::string::npos
) {
167 std::string line
= linebuf
.substr(0, pos
);
168 linebuf
.erase(0, pos
+ 1);
170 log("%s\n", line
.c_str());
172 if (flag_expect_stdout
)
173 for(auto &x
: expect_stdout
)
174 if (YS_REGEX_NS::regex_search(line
, x
.re
))
177 pos
= linebuf
.find('\n');
183 if(WIFEXITED(status
)) {
184 retval
= WEXITSTATUS(status
);
186 else if(WIFSIGNALED(status
)) {
187 retval
= WTERMSIG(status
);
189 else if(WIFSTOPPED(status
)) {
190 retval
= WSTOPSIG(status
);
193 if (flag_expect_return
&& retval
!= expect_return_value
)
194 log_cmd_error("Return value %d did not match expected return value %d.\n", retval
, expect_return_value
);
196 if (flag_expect_stdout
)
197 for (auto &x
: expect_stdout
)
198 if (x
.polarity
^ x
.matched
)
199 log_cmd_error("Command stdout did%s have a line matching given regex \"%s\".\n", (x
.polarity
? " not" : ""), x
.str
.c_str());
205 PRIVATE_NAMESPACE_END