2017-06-13 19:56:20 +00:00
|
|
|
// Implementation of the function builtin.
|
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2019-10-13 22:50:48 +00:00
|
|
|
#include "builtin_function.h"
|
|
|
|
|
2017-06-14 19:26:05 +00:00
|
|
|
#include <unistd.h>
|
2017-06-13 19:56:20 +00:00
|
|
|
|
2019-11-19 01:11:16 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
2017-06-14 19:26:05 +00:00
|
|
|
#include <memory>
|
2017-06-18 05:36:56 +00:00
|
|
|
#include <string>
|
2017-06-14 19:26:05 +00:00
|
|
|
#include <vector>
|
2017-06-13 19:56:20 +00:00
|
|
|
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "complete.h"
|
|
|
|
#include "event.h"
|
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "function.h"
|
|
|
|
#include "io.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "parser_keywords.h"
|
2017-06-14 19:26:05 +00:00
|
|
|
#include "proc.h"
|
|
|
|
#include "signal.h"
|
2017-06-13 19:56:20 +00:00
|
|
|
#include "wgetopt.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
|
|
|
|
2017-06-16 00:57:37 +00:00
|
|
|
struct function_cmd_opts_t {
|
2017-06-13 19:56:20 +00:00
|
|
|
bool print_help = false;
|
|
|
|
bool shadow_scope = true;
|
2020-04-03 02:43:34 +00:00
|
|
|
wcstring description;
|
2019-02-23 09:04:05 +00:00
|
|
|
std::vector<event_description_t> events;
|
2017-06-13 19:56:20 +00:00
|
|
|
wcstring_list_t named_arguments;
|
|
|
|
wcstring_list_t inherit_vars;
|
|
|
|
wcstring_list_t wrap_targets;
|
|
|
|
};
|
|
|
|
|
2019-10-10 16:09:26 +00:00
|
|
|
// This command is atypical in using the "-" (RETURN_IN_ORDER) option for flag parsing.
|
2017-06-13 19:56:20 +00:00
|
|
|
// This is needed due to the semantics of the -a/--argument-names flag.
|
2019-10-10 16:09:26 +00:00
|
|
|
static const wchar_t *const short_options = L"-:a:d:e:hj:p:s:v:w:SV:";
|
2019-11-19 02:34:50 +00:00
|
|
|
static const struct woption long_options[] = {
|
|
|
|
{L"description", required_argument, nullptr, 'd'},
|
|
|
|
{L"on-signal", required_argument, nullptr, 's'},
|
|
|
|
{L"on-job-exit", required_argument, nullptr, 'j'},
|
|
|
|
{L"on-process-exit", required_argument, nullptr, 'p'},
|
|
|
|
{L"on-variable", required_argument, nullptr, 'v'},
|
|
|
|
{L"on-event", required_argument, nullptr, 'e'},
|
|
|
|
{L"wraps", required_argument, nullptr, 'w'},
|
|
|
|
{L"help", no_argument, nullptr, 'h'},
|
|
|
|
{L"argument-names", required_argument, nullptr, 'a'},
|
|
|
|
{L"no-scope-shadowing", no_argument, nullptr, 'S'},
|
|
|
|
{L"inherit-variable", required_argument, nullptr, 'V'},
|
|
|
|
{nullptr, 0, nullptr, 0}};
|
2017-06-13 19:56:20 +00:00
|
|
|
|
2017-06-16 00:57:37 +00:00
|
|
|
static int parse_cmd_opts(function_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method)
|
2017-06-18 05:36:56 +00:00
|
|
|
int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) {
|
|
|
|
const wchar_t *cmd = L"function";
|
2017-06-13 19:56:20 +00:00
|
|
|
int opt;
|
|
|
|
wgetopter_t w;
|
2019-10-10 16:09:26 +00:00
|
|
|
bool handling_named_arguments = false;
|
2019-11-19 02:34:50 +00:00
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, nullptr)) != -1) {
|
2019-10-10 16:09:26 +00:00
|
|
|
if (opt != 'a' && opt != 1) handling_named_arguments = false;
|
2017-06-13 19:56:20 +00:00
|
|
|
switch (opt) {
|
2019-10-10 16:09:26 +00:00
|
|
|
case 1: {
|
|
|
|
if (handling_named_arguments) {
|
|
|
|
opts.named_arguments.push_back(w.woptarg);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
streams.err.append_format(_(L"%ls: Unexpected positional argument '%ls'"), cmd,
|
|
|
|
w.woptarg);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
}
|
2017-06-13 19:56:20 +00:00
|
|
|
case 'd': {
|
2017-06-16 00:57:37 +00:00
|
|
|
opts.description = w.woptarg;
|
2017-06-13 19:56:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 's': {
|
|
|
|
int sig = wcs2sig(w.woptarg);
|
|
|
|
if (sig == -1) {
|
2017-06-18 05:36:56 +00:00
|
|
|
streams.err.append_format(_(L"%ls: Unknown signal '%ls'"), cmd, w.woptarg);
|
2017-06-13 19:56:20 +00:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2019-02-23 09:04:05 +00:00
|
|
|
opts.events.push_back(event_description_t::signal(sig));
|
2017-06-13 19:56:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'v': {
|
|
|
|
if (!valid_var_name(w.woptarg)) {
|
2017-06-18 05:36:56 +00:00
|
|
|
streams.err.append_format(BUILTIN_ERR_VARNAME, cmd, w.woptarg);
|
2017-06-13 19:56:20 +00:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
2019-02-23 09:04:05 +00:00
|
|
|
opts.events.push_back(event_description_t::variable(w.woptarg));
|
2017-06-13 19:56:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'e': {
|
2019-02-23 09:04:05 +00:00
|
|
|
opts.events.push_back(event_description_t::generic(w.woptarg));
|
2017-06-13 19:56:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'j':
|
|
|
|
case 'p': {
|
2019-02-23 09:04:05 +00:00
|
|
|
event_description_t e(event_type_t::any);
|
2017-06-13 19:56:20 +00:00
|
|
|
|
|
|
|
if ((opt == 'j') && (wcscasecmp(w.woptarg, L"caller") == 0)) {
|
2020-02-08 23:43:21 +00:00
|
|
|
internal_job_id_t caller_id =
|
|
|
|
parser.libdata().is_subshell ? parser.libdata().caller_id : 0;
|
|
|
|
if (caller_id == 0) {
|
2017-06-24 06:19:09 +00:00
|
|
|
streams.err.append_format(
|
|
|
|
_(L"%ls: Cannot find calling job for event handler"), cmd);
|
2017-06-13 19:56:20 +00:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2020-02-09 00:08:26 +00:00
|
|
|
e.type = event_type_t::caller_exit;
|
2020-02-08 23:43:21 +00:00
|
|
|
e.param1.caller_id = caller_id;
|
2018-04-29 10:31:47 +00:00
|
|
|
} else if ((opt == 'p') && (wcscasecmp(w.woptarg, L"%self") == 0)) {
|
2019-02-21 06:42:58 +00:00
|
|
|
e.type = event_type_t::exit;
|
2020-02-09 00:08:26 +00:00
|
|
|
e.param1.pid = getpid();
|
2017-06-13 19:56:20 +00:00
|
|
|
} else {
|
2020-02-09 00:08:26 +00:00
|
|
|
pid_t pid = fish_wcstoi(w.woptarg);
|
2017-06-13 19:56:20 +00:00
|
|
|
if (errno || pid < 0) {
|
2017-06-18 05:36:56 +00:00
|
|
|
streams.err.append_format(_(L"%ls: Invalid process id '%ls'"), cmd,
|
2017-06-24 06:19:09 +00:00
|
|
|
w.woptarg);
|
2017-06-13 19:56:20 +00:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
2019-02-21 06:42:58 +00:00
|
|
|
e.type = event_type_t::exit;
|
2017-06-13 19:56:20 +00:00
|
|
|
e.param1.pid = (opt == 'j' ? -1 : 1) * abs(pid);
|
|
|
|
}
|
2017-06-16 00:57:37 +00:00
|
|
|
opts.events.push_back(e);
|
2017-06-13 19:56:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'a': {
|
2019-10-10 16:09:26 +00:00
|
|
|
handling_named_arguments = true;
|
2017-06-16 00:57:37 +00:00
|
|
|
opts.named_arguments.push_back(w.woptarg);
|
2017-06-13 19:56:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'S': {
|
2017-06-16 00:57:37 +00:00
|
|
|
opts.shadow_scope = false;
|
2017-06-13 19:56:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'w': {
|
2017-06-16 00:57:37 +00:00
|
|
|
opts.wrap_targets.push_back(w.woptarg);
|
2017-06-13 19:56:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'V': {
|
|
|
|
if (!valid_var_name(w.woptarg)) {
|
2017-06-18 05:36:56 +00:00
|
|
|
streams.err.append_format(BUILTIN_ERR_VARNAME, cmd, w.woptarg);
|
2017-06-13 19:56:20 +00:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2017-06-16 00:57:37 +00:00
|
|
|
opts.inherit_vars.push_back(w.woptarg);
|
2017-06-13 19:56:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'h': {
|
2017-06-16 00:57:37 +00:00
|
|
|
opts.print_help = true;
|
2017-06-15 05:12:29 +00:00
|
|
|
break;
|
2017-06-13 19:56:20 +00:00
|
|
|
}
|
|
|
|
case ':': {
|
2017-07-01 21:03:47 +00:00
|
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
2017-06-13 19:56:20 +00:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
case '?': {
|
|
|
|
builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
DIE("unexpected retval from wgetopt_long");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*optind = w.woptind;
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int validate_function_name(int argc, const wchar_t *const *argv, wcstring &function_name,
|
2017-06-18 05:36:56 +00:00
|
|
|
const wchar_t *cmd, io_streams_t &streams) {
|
2017-06-13 19:56:20 +00:00
|
|
|
if (argc < 2) {
|
|
|
|
// This is currently impossible but let's be paranoid.
|
2017-06-18 05:36:56 +00:00
|
|
|
streams.err.append_format(_(L"%ls: Expected function name"), cmd);
|
2017-06-13 19:56:20 +00:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
function_name = argv[1];
|
|
|
|
if (!valid_func_name(function_name)) {
|
2017-06-24 06:19:09 +00:00
|
|
|
streams.err.append_format(_(L"%ls: Illegal function name '%ls'"), cmd,
|
|
|
|
function_name.c_str());
|
2017-06-13 19:56:20 +00:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parser_keywords_is_reserved(function_name)) {
|
2017-06-18 05:36:56 +00:00
|
|
|
streams.err.append_format(
|
2020-06-26 04:01:59 +00:00
|
|
|
_(L"%ls: The name '%ls' is reserved, and cannot be used as a function name"), cmd,
|
2017-06-13 19:56:20 +00:00
|
|
|
function_name.c_str());
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Define a function. Calls into `function.cpp` to perform the heavy lifting of defining a
|
|
|
|
/// function.
|
2020-07-18 17:25:43 +00:00
|
|
|
maybe_t<int> builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_list_t &c_args,
|
2020-07-03 18:16:51 +00:00
|
|
|
const parsed_source_ref_t &source, const ast::block_statement_t &func_node) {
|
2018-02-10 05:53:06 +00:00
|
|
|
assert(source && "Missing source in builtin_function");
|
2017-06-13 19:56:20 +00:00
|
|
|
// The wgetopt function expects 'function' as the first argument. Make a new wcstring_list with
|
|
|
|
// that property. This is needed because this builtin has a different signature than the other
|
|
|
|
// builtins.
|
2017-06-18 05:36:56 +00:00
|
|
|
wcstring_list_t args = {L"function"};
|
2017-06-13 19:56:20 +00:00
|
|
|
args.insert(args.end(), c_args.begin(), c_args.end());
|
|
|
|
|
2018-08-04 23:15:06 +00:00
|
|
|
null_terminated_array_t<wchar_t> argv_array(args);
|
|
|
|
wchar_t **argv = argv_array.get();
|
2017-06-13 19:56:20 +00:00
|
|
|
wchar_t *cmd = argv[0];
|
|
|
|
int argc = builtin_count_args(argv);
|
|
|
|
|
|
|
|
// A valid function name has to be the first argument.
|
2017-06-18 05:36:56 +00:00
|
|
|
wcstring function_name;
|
|
|
|
int retval = validate_function_name(argc, argv, function_name, cmd, streams);
|
2017-06-13 19:56:20 +00:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
|
2017-06-18 05:36:56 +00:00
|
|
|
function_cmd_opts_t opts;
|
2017-06-13 19:56:20 +00:00
|
|
|
int optind;
|
2017-06-18 05:36:56 +00:00
|
|
|
retval = parse_cmd_opts(opts, &optind, argc, argv, parser, streams);
|
2017-06-13 19:56:20 +00:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
|
|
|
if (opts.print_help) {
|
2019-03-26 18:13:01 +00:00
|
|
|
builtin_print_error_trailer(parser, streams.err, cmd);
|
2017-06-13 19:56:20 +00:00
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc != optind) {
|
2019-11-19 00:56:46 +00:00
|
|
|
if (!opts.named_arguments.empty()) {
|
2017-06-13 19:56:20 +00:00
|
|
|
for (int i = optind; i < argc; i++) {
|
2019-10-07 19:21:38 +00:00
|
|
|
if (!valid_var_name(argv[i])) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_VARNAME, cmd, argv[i]);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2017-06-13 19:56:20 +00:00
|
|
|
opts.named_arguments.push_back(argv[i]);
|
|
|
|
}
|
|
|
|
} else {
|
2017-06-18 05:36:56 +00:00
|
|
|
streams.err.append_format(_(L"%ls: Unexpected positional argument '%ls'"), cmd,
|
2017-06-24 06:19:09 +00:00
|
|
|
argv[optind]);
|
2017-06-13 19:56:20 +00:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have what we need to actually define the function.
|
2019-11-12 17:53:10 +00:00
|
|
|
auto props = std::make_shared<function_properties_t>();
|
|
|
|
props->shadow_scope = opts.shadow_scope;
|
|
|
|
props->named_arguments = std::move(opts.named_arguments);
|
2019-11-12 19:25:41 +00:00
|
|
|
props->parsed_source = source;
|
2020-07-03 18:16:51 +00:00
|
|
|
props->func_node = &func_node;
|
2019-11-12 17:53:10 +00:00
|
|
|
|
|
|
|
// Populate inherit_vars.
|
|
|
|
for (const wcstring &name : opts.inherit_vars) {
|
|
|
|
if (auto var = parser.vars().get(name)) {
|
|
|
|
props->inherit_vars[name] = var->as_list();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 19:25:41 +00:00
|
|
|
// Add the function itself.
|
|
|
|
function_add(function_name, opts.description, props, parser.libdata().current_filename);
|
2017-06-13 19:56:20 +00:00
|
|
|
|
2019-11-12 19:25:41 +00:00
|
|
|
// Add any event handlers.
|
|
|
|
for (const event_description_t &ed : opts.events) {
|
|
|
|
event_add_handler(std::make_shared<event_handler_t>(ed, function_name));
|
|
|
|
}
|
2017-06-13 19:56:20 +00:00
|
|
|
|
|
|
|
// Handle wrap targets by creating the appropriate completions.
|
2018-02-12 00:37:38 +00:00
|
|
|
for (const wcstring &wt : opts.wrap_targets) complete_add_wrapper(function_name, wt);
|
2017-06-13 19:56:20 +00:00
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|