2019-04-13 13:42:27 +08:00
|
|
|
// Functions for executing the eval builtin.
|
2019-04-12 19:53:08 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2019-11-19 09:11:16 +08:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstddef>
|
2019-04-12 19:53:08 +08:00
|
|
|
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "common.h"
|
2019-04-14 02:05:04 +08:00
|
|
|
#include "exec.h"
|
2019-04-12 19:53:08 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "io.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "wgetopt.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
|
|
|
|
2019-04-13 13:42:27 +08:00
|
|
|
/// Implementation of eval builtin.
|
2019-04-12 19:53:08 +08:00
|
|
|
int builtin_eval(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
|
|
|
int argc = builtin_count_args(argv);
|
|
|
|
|
|
|
|
wcstring new_cmd;
|
2019-05-13 09:23:00 +08:00
|
|
|
for (int i = 1; i < argc; ++i) {
|
2019-04-13 13:42:27 +08:00
|
|
|
if (i > 1) new_cmd += L' ';
|
2019-04-12 19:53:08 +08:00
|
|
|
new_cmd += argv[i];
|
|
|
|
}
|
|
|
|
|
2019-04-13 13:42:27 +08:00
|
|
|
int status = STATUS_CMD_OK;
|
2019-04-12 19:53:08 +08:00
|
|
|
if (argc > 1) {
|
2020-01-24 09:34:46 +08:00
|
|
|
auto res = parser.eval(new_cmd, *streams.io_chain);
|
|
|
|
if (res.was_empty) {
|
2019-04-14 02:05:04 +08:00
|
|
|
// Issue #5692, in particular, to catch `eval ""`, `eval "begin; end;"`, etc.
|
|
|
|
// where we have an argument but nothing is executed.
|
|
|
|
status = STATUS_CMD_OK;
|
2019-04-13 13:42:27 +08:00
|
|
|
} else {
|
2020-01-24 09:34:46 +08:00
|
|
|
status = res.status.status_value();
|
2019-04-12 19:53:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|