2017-06-14 03:56:20 +08:00
|
|
|
// Implementation of the function builtin.
|
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stddef.h>
|
2017-06-15 03:26:05 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2017-06-14 03:56:20 +08:00
|
|
|
|
2017-06-15 03:26:05 +08:00
|
|
|
#include <memory>
|
2017-06-18 13:36:56 +08:00
|
|
|
#include <string>
|
2017-06-15 03:26:05 +08:00
|
|
|
#include <vector>
|
2017-06-14 03:56:20 +08:00
|
|
|
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "builtin_function.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-15 03:26:05 +08:00
|
|
|
#include "proc.h"
|
|
|
|
#include "signal.h"
|
2017-06-14 03:56:20 +08:00
|
|
|
#include "wgetopt.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
|
|
|
|
2017-06-16 08:57:37 +08:00
|
|
|
struct function_cmd_opts_t {
|
2017-06-14 03:56:20 +08:00
|
|
|
bool print_help = false;
|
|
|
|
bool shadow_scope = true;
|
2017-06-16 08:57:37 +08:00
|
|
|
wcstring description = L"";
|
2019-02-23 17:04:05 +08:00
|
|
|
std::vector<event_description_t> events;
|
2017-06-14 03:56:20 +08:00
|
|
|
wcstring_list_t named_arguments;
|
|
|
|
wcstring_list_t inherit_vars;
|
|
|
|
wcstring_list_t wrap_targets;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This command is atypical in using the "+" (REQUIRE_ORDER) option for flag parsing.
|
|
|
|
// This is needed due to the semantics of the -a/--argument-names flag.
|
2018-09-29 11:45:56 +08:00
|
|
|
static const wchar_t *const short_options = L"+:a:d:e:hj:p:s:v:w:SV:";
|
2017-06-17 12:00:24 +08:00
|
|
|
static const struct woption long_options[] = {{L"description", required_argument, NULL, 'd'},
|
|
|
|
{L"on-signal", required_argument, NULL, 's'},
|
|
|
|
{L"on-job-exit", required_argument, NULL, 'j'},
|
|
|
|
{L"on-process-exit", required_argument, NULL, 'p'},
|
|
|
|
{L"on-variable", required_argument, NULL, 'v'},
|
|
|
|
{L"on-event", required_argument, NULL, 'e'},
|
|
|
|
{L"wraps", required_argument, NULL, 'w'},
|
|
|
|
{L"help", no_argument, NULL, 'h'},
|
|
|
|
{L"argument-names", required_argument, NULL, 'a'},
|
|
|
|
{L"no-scope-shadowing", no_argument, NULL, 'S'},
|
|
|
|
{L"inherit-variable", required_argument, NULL, 'V'},
|
|
|
|
{NULL, 0, NULL, 0}};
|
2017-06-14 03:56:20 +08:00
|
|
|
|
2017-06-16 08:57:37 +08:00
|
|
|
static int parse_cmd_opts(function_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method)
|
2017-06-18 13:36:56 +08:00
|
|
|
int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) {
|
|
|
|
const wchar_t *cmd = L"function";
|
2017-06-14 03:56:20 +08:00
|
|
|
int opt;
|
|
|
|
wgetopter_t w;
|
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'd': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.description = w.woptarg;
|
2017-06-14 03:56:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 's': {
|
|
|
|
int sig = wcs2sig(w.woptarg);
|
|
|
|
if (sig == -1) {
|
2017-06-18 13:36:56 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Unknown signal '%ls'"), cmd, w.woptarg);
|
2017-06-14 03:56:20 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2019-02-23 17:04:05 +08:00
|
|
|
opts.events.push_back(event_description_t::signal(sig));
|
2017-06-14 03:56:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'v': {
|
|
|
|
if (!valid_var_name(w.woptarg)) {
|
2017-06-18 13:36:56 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_VARNAME, cmd, w.woptarg);
|
2017-06-14 03:56:20 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
2019-02-23 17:04:05 +08:00
|
|
|
opts.events.push_back(event_description_t::variable(w.woptarg));
|
2017-06-14 03:56:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'e': {
|
2019-02-23 17:04:05 +08:00
|
|
|
opts.events.push_back(event_description_t::generic(w.woptarg));
|
2017-06-14 03:56:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'j':
|
|
|
|
case 'p': {
|
|
|
|
pid_t pid;
|
2019-02-23 17:04:05 +08:00
|
|
|
event_description_t e(event_type_t::any);
|
2017-06-14 03:56:20 +08:00
|
|
|
|
|
|
|
if ((opt == 'j') && (wcscasecmp(w.woptarg, L"caller") == 0)) {
|
|
|
|
job_id_t job_id = -1;
|
|
|
|
|
2019-05-13 09:02:57 +08:00
|
|
|
if (parser.libdata().is_subshell) {
|
2019-05-13 05:42:18 +08:00
|
|
|
job_id = parser.libdata().caller_job_id;
|
2017-06-14 03:56:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (job_id == -1) {
|
2017-06-24 14:19:09 +08:00
|
|
|
streams.err.append_format(
|
|
|
|
_(L"%ls: Cannot find calling job for event handler"), cmd);
|
2017-06-14 03:56:20 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2019-02-21 14:42:58 +08:00
|
|
|
e.type = event_type_t::job_exit;
|
2017-06-14 03:56:20 +08:00
|
|
|
e.param1.job_id = job_id;
|
2018-04-29 18:31:47 +08:00
|
|
|
} else if ((opt == 'p') && (wcscasecmp(w.woptarg, L"%self") == 0)) {
|
|
|
|
pid = getpid();
|
2019-02-21 14:42:58 +08:00
|
|
|
e.type = event_type_t::exit;
|
2018-04-29 18:31:47 +08:00
|
|
|
e.param1.pid = pid;
|
2017-06-14 03:56:20 +08:00
|
|
|
} else {
|
|
|
|
pid = fish_wcstoi(w.woptarg);
|
|
|
|
if (errno || pid < 0) {
|
2017-06-18 13:36:56 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Invalid process id '%ls'"), cmd,
|
2017-06-24 14:19:09 +08:00
|
|
|
w.woptarg);
|
2017-06-14 03:56:20 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
2019-02-21 14:42:58 +08:00
|
|
|
e.type = event_type_t::exit;
|
2017-06-14 03:56:20 +08:00
|
|
|
e.param1.pid = (opt == 'j' ? -1 : 1) * abs(pid);
|
|
|
|
}
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.events.push_back(e);
|
2017-06-14 03:56:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'a': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.named_arguments.push_back(w.woptarg);
|
2017-06-14 03:56:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'S': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.shadow_scope = false;
|
2017-06-14 03:56:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'w': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.wrap_targets.push_back(w.woptarg);
|
2017-06-14 03:56:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'V': {
|
|
|
|
if (!valid_var_name(w.woptarg)) {
|
2017-06-18 13:36:56 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_VARNAME, cmd, w.woptarg);
|
2017-06-14 03:56:20 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.inherit_vars.push_back(w.woptarg);
|
2017-06-14 03:56:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'h': {
|
2017-06-16 08:57:37 +08:00
|
|
|
opts.print_help = true;
|
2017-06-15 13:12:29 +08:00
|
|
|
break;
|
2017-06-14 03:56:20 +08:00
|
|
|
}
|
|
|
|
case ':': {
|
2017-07-02 05:03:47 +08:00
|
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
2017-06-14 03:56:20 +08: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");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*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 13:36:56 +08:00
|
|
|
const wchar_t *cmd, io_streams_t &streams) {
|
2017-06-14 03:56:20 +08:00
|
|
|
if (argc < 2) {
|
|
|
|
// This is currently impossible but let's be paranoid.
|
2017-06-18 13:36:56 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Expected function name"), cmd);
|
2017-06-14 03:56:20 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
function_name = argv[1];
|
|
|
|
if (!valid_func_name(function_name)) {
|
2017-06-24 14:19:09 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Illegal function name '%ls'"), cmd,
|
|
|
|
function_name.c_str());
|
2017-06-14 03:56:20 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parser_keywords_is_reserved(function_name)) {
|
2017-06-18 13:36:56 +08:00
|
|
|
streams.err.append_format(
|
2017-06-14 03:56:20 +08:00
|
|
|
_(L"%ls: The name '%ls' is reserved,\nand can not be used as a function name"), cmd,
|
|
|
|
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.
|
|
|
|
int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_list_t &c_args,
|
2018-02-10 13:53:06 +08:00
|
|
|
const parsed_source_ref_t &source, tnode_t<grammar::job_list> body) {
|
|
|
|
assert(source && "Missing source in builtin_function");
|
2017-06-14 03:56:20 +08: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 13:36:56 +08:00
|
|
|
wcstring_list_t args = {L"function"};
|
2017-06-14 03:56:20 +08:00
|
|
|
args.insert(args.end(), c_args.begin(), c_args.end());
|
|
|
|
|
2018-08-05 07:15:06 +08:00
|
|
|
null_terminated_array_t<wchar_t> argv_array(args);
|
|
|
|
wchar_t **argv = argv_array.get();
|
2017-06-14 03:56:20 +08: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 13:36:56 +08:00
|
|
|
wcstring function_name;
|
|
|
|
int retval = validate_function_name(argc, argv, function_name, cmd, streams);
|
2017-06-14 03:56:20 +08:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
|
2017-06-18 13:36:56 +08:00
|
|
|
function_cmd_opts_t opts;
|
2017-06-14 03:56:20 +08:00
|
|
|
int optind;
|
2017-06-18 13:36:56 +08:00
|
|
|
retval = parse_cmd_opts(opts, &optind, argc, argv, parser, streams);
|
2017-06-14 03:56:20 +08:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
|
|
|
if (opts.print_help) {
|
2019-03-27 02:13:01 +08:00
|
|
|
builtin_print_error_trailer(parser, streams.err, cmd);
|
2017-06-14 03:56:20 +08:00
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc != optind) {
|
|
|
|
if (opts.named_arguments.size()) {
|
|
|
|
for (int i = optind; i < argc; i++) {
|
|
|
|
opts.named_arguments.push_back(argv[i]);
|
|
|
|
}
|
|
|
|
} else {
|
2017-06-18 13:36:56 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Unexpected positional argument '%ls'"), cmd,
|
2017-06-24 14:19:09 +08:00
|
|
|
argv[optind]);
|
2017-06-14 03:56:20 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have what we need to actually define the function.
|
|
|
|
function_data_t d;
|
|
|
|
d.name = function_name;
|
2017-06-16 08:57:37 +08:00
|
|
|
if (!opts.description.empty()) d.description = opts.description;
|
2017-06-17 12:00:24 +08:00
|
|
|
// d.description = opts.description;
|
2019-02-23 17:04:05 +08:00
|
|
|
d.events = std::move(opts.events);
|
2018-02-12 08:37:38 +08:00
|
|
|
d.props.shadow_scope = opts.shadow_scope;
|
|
|
|
d.props.named_arguments = std::move(opts.named_arguments);
|
|
|
|
d.inherit_vars = std::move(opts.inherit_vars);
|
2017-06-14 03:56:20 +08:00
|
|
|
|
2018-02-12 08:37:38 +08:00
|
|
|
d.props.parsed_source = source;
|
|
|
|
d.props.body_node = body;
|
|
|
|
function_add(std::move(d), parser);
|
2017-06-14 03:56:20 +08:00
|
|
|
|
|
|
|
// Handle wrap targets by creating the appropriate completions.
|
2018-02-12 08:37:38 +08:00
|
|
|
for (const wcstring &wt : opts.wrap_targets) complete_add_wrapper(function_name, wt);
|
2017-06-14 03:56:20 +08:00
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|