2017-06-14 03:56:20 +08:00
|
|
|
// Implementation of the function builtin.
|
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2019-10-14 06:50:48 +08:00
|
|
|
#include "builtin_function.h"
|
|
|
|
|
2017-06-14 03:56:20 +08:00
|
|
|
#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 "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;
|
|
|
|
};
|
|
|
|
|
2019-10-11 00:09:26 +08:00
|
|
|
// This command is atypical in using the "-" (RETURN_IN_ORDER) option for flag parsing.
|
2017-06-14 03:56:20 +08:00
|
|
|
// This is needed due to the semantics of the -a/--argument-names flag.
|
2019-10-11 00:09:26 +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;
|
2019-10-11 00:09:26 +08:00
|
|
|
bool handling_named_arguments = false;
|
2017-06-14 03:56:20 +08:00
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
|
2019-10-11 00:09:26 +08:00
|
|
|
if (opt != 'a' && opt != 1) handling_named_arguments = false;
|
2017-06-14 03:56:20 +08:00
|
|
|
switch (opt) {
|
2019-10-11 00:09:26 +08: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-14 03:56:20 +08:00
|
|
|
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': {
|
2019-10-11 00:09:26 +08:00
|
|
|
handling_named_arguments = true;
|
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) {
|
2019-11-19 08:56:46 +08:00
|
|
|
if (!opts.named_arguments.empty()) {
|
2017-06-14 03:56:20 +08:00
|
|
|
for (int i = optind; i < argc; i++) {
|
2019-10-08 03:21:38 +08:00
|
|
|
if (!valid_var_name(argv[i])) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_VARNAME, cmd, argv[i]);
|
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2017-06-14 03:56:20 +08:00
|
|
|
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.
|
2019-11-13 01:53:10 +08: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-13 03:25:41 +08:00
|
|
|
props->parsed_source = source;
|
|
|
|
props->body_node = body;
|
2019-11-13 01:53:10 +08: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-13 03:25:41 +08:00
|
|
|
// Add the function itself.
|
|
|
|
function_add(function_name, opts.description, props, parser.libdata().current_filename);
|
2017-06-14 03:56:20 +08:00
|
|
|
|
2019-11-13 03:25:41 +08: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-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;
|
|
|
|
}
|