2016-04-19 10:25:12 +08:00
|
|
|
// Functions for executing builtin functions.
|
|
|
|
//
|
|
|
|
// How to add a new builtin function:
|
|
|
|
//
|
|
|
|
// 1). Create a function in builtin.c with the following signature:
|
|
|
|
//
|
2017-03-22 19:43:13 +08:00
|
|
|
// <tt>static int builtin_NAME(parser_t &parser, io_streams_t &streams, wchar_t **argv)</tt>
|
2016-04-19 10:25:12 +08:00
|
|
|
//
|
|
|
|
// where NAME is the name of the builtin, and args is a zero-terminated list of arguments.
|
|
|
|
//
|
|
|
|
// 2). Add a line like { L"NAME", &builtin_NAME, N_(L"Bla bla bla") }, to the builtin_data_t
|
|
|
|
// variable. The description is used by the completion system. Note that this array is sorted.
|
|
|
|
//
|
2019-02-25 11:41:51 +08:00
|
|
|
// 3). Create a file sphinx_doc_src/NAME.rst, containing the manual for the builtin in
|
|
|
|
// reStructuredText-format. Check the other builtin manuals for proper syntax.
|
2016-04-19 10:25:12 +08:00
|
|
|
//
|
2019-02-25 11:41:51 +08:00
|
|
|
// 4). Use 'git add sphinx_doc_src/NAME.txt' to start tracking changes to the documentation file.
|
2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
#include <errno.h>
|
2016-04-19 10:25:12 +08:00
|
|
|
#include <stdlib.h>
|
2019-03-13 06:07:07 +08:00
|
|
|
#include <cstring>
|
2016-04-19 10:25:12 +08:00
|
|
|
#include <unistd.h>
|
2019-03-13 05:06:01 +08:00
|
|
|
#include <cwchar>
|
2017-02-14 12:37:27 +08:00
|
|
|
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <algorithm>
|
2016-06-24 08:24:19 +08:00
|
|
|
#include <memory>
|
2017-04-14 14:13:55 +08:00
|
|
|
#include <string>
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
#include "builtin.h"
|
2017-07-08 05:32:41 +08:00
|
|
|
#include "builtin_argparse.h"
|
2017-06-15 09:25:51 +08:00
|
|
|
#include "builtin_bg.h"
|
2017-06-13 08:19:13 +08:00
|
|
|
#include "builtin_bind.h"
|
2017-06-13 09:22:57 +08:00
|
|
|
#include "builtin_block.h"
|
2017-06-14 13:03:27 +08:00
|
|
|
#include "builtin_builtin.h"
|
2017-06-14 12:40:53 +08:00
|
|
|
#include "builtin_cd.h"
|
2017-06-14 12:50:55 +08:00
|
|
|
#include "builtin_command.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "builtin_commandline.h"
|
|
|
|
#include "builtin_complete.h"
|
2017-06-14 13:18:56 +08:00
|
|
|
#include "builtin_contains.h"
|
2017-06-14 09:27:03 +08:00
|
|
|
#include "builtin_disown.h"
|
2017-06-14 08:48:47 +08:00
|
|
|
#include "builtin_echo.h"
|
2017-06-13 09:40:58 +08:00
|
|
|
#include "builtin_emit.h"
|
2017-06-15 12:19:59 +08:00
|
|
|
#include "builtin_exit.h"
|
2017-06-15 04:45:57 +08:00
|
|
|
#include "builtin_fg.h"
|
2017-06-13 10:39:16 +08:00
|
|
|
#include "builtin_functions.h"
|
2017-06-13 12:11:42 +08:00
|
|
|
#include "builtin_history.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "builtin_jobs.h"
|
2017-08-23 10:57:30 +08:00
|
|
|
#include "builtin_math.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "builtin_printf.h"
|
2017-06-15 04:31:05 +08:00
|
|
|
#include "builtin_pwd.h"
|
2017-06-13 13:26:24 +08:00
|
|
|
#include "builtin_random.h"
|
2017-06-13 12:34:24 +08:00
|
|
|
#include "builtin_read.h"
|
2017-06-15 12:19:59 +08:00
|
|
|
#include "builtin_realpath.h"
|
2017-06-15 10:13:45 +08:00
|
|
|
#include "builtin_return.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "builtin_set.h"
|
|
|
|
#include "builtin_set_color.h"
|
2017-06-14 12:20:21 +08:00
|
|
|
#include "builtin_source.h"
|
2017-06-13 12:34:24 +08:00
|
|
|
#include "builtin_status.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "builtin_string.h"
|
|
|
|
#include "builtin_test.h"
|
|
|
|
#include "builtin_ulimit.h"
|
2017-10-22 15:10:23 +08:00
|
|
|
#include "builtin_wait.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "common.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "complete.h"
|
2006-11-18 00:24:38 +08:00
|
|
|
#include "exec.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2016-04-19 10:25:12 +08:00
|
|
|
#include "intern.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "io.h"
|
2016-04-19 10:25:12 +08:00
|
|
|
#include "parse_constants.h"
|
2006-02-15 03:56:36 +08:00
|
|
|
#include "parse_util.h"
|
2016-04-19 10:25:12 +08:00
|
|
|
#include "parser.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "reader.h"
|
|
|
|
#include "wgetopt.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2006-06-21 05:20:16 +08:00
|
|
|
|
2018-09-29 12:22:24 +08:00
|
|
|
bool builtin_data_t::operator<(const wcstring &other) const {
|
2019-03-13 05:06:01 +08:00
|
|
|
return std::wcscmp(this->name, other.c_str()) < 0;
|
2018-09-29 12:22:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool builtin_data_t::operator<(const builtin_data_t *other) const {
|
2019-03-13 05:06:01 +08:00
|
|
|
return std::wcscmp(this->name, other->name) < 0;
|
2018-09-29 12:22:24 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:30:49 +08:00
|
|
|
/// Counts the number of arguments in the specified null-terminated array
|
2016-04-19 10:25:12 +08:00
|
|
|
int builtin_count_args(const wchar_t *const *argv) {
|
2016-08-17 06:30:49 +08:00
|
|
|
int argc;
|
2016-10-05 11:06:14 +08:00
|
|
|
for (argc = 1; argv[argc] != NULL;) {
|
2016-09-11 05:55:27 +08:00
|
|
|
argc++;
|
|
|
|
}
|
2016-08-17 06:30:49 +08:00
|
|
|
|
|
|
|
assert(argv[argc] == NULL);
|
2012-11-19 08:30:30 +08:00
|
|
|
return argc;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// This function works like wperror, but it prints its result into the streams.err string instead
|
|
|
|
/// to stderr. Used by the builtin commands.
|
2016-04-21 14:00:54 +08:00
|
|
|
void builtin_wperror(const wchar_t *s, io_streams_t &streams) {
|
2019-03-13 06:07:07 +08:00
|
|
|
char *err = std::strerror(errno);
|
2016-04-19 10:25:12 +08:00
|
|
|
if (s != NULL) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append(s);
|
|
|
|
streams.err.append(L": ");
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
if (err != NULL) {
|
2012-12-20 05:31:06 +08:00
|
|
|
const wcstring werr = str2wcstring(err);
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append(werr);
|
|
|
|
streams.err.push_back(L'\n');
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2018-09-29 11:45:56 +08:00
|
|
|
static const wchar_t *const short_options = L"+:h";
|
2017-06-15 09:25:51 +08:00
|
|
|
static const struct woption long_options[] = {{L"help", no_argument, NULL, 'h'},
|
|
|
|
{NULL, 0, NULL, 0}};
|
|
|
|
|
2017-06-17 11:42:33 +08:00
|
|
|
int parse_help_only_cmd_opts(struct help_only_cmd_opts_t &opts, int *optind, int argc,
|
|
|
|
wchar_t **argv, parser_t &parser, io_streams_t &streams) {
|
2017-06-15 09:25:51 +08:00
|
|
|
wchar_t *cmd = argv[0];
|
|
|
|
int opt;
|
|
|
|
wgetopter_t w;
|
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
|
|
|
|
switch (opt) { //!OCLINT(too few branches)
|
|
|
|
case 'h': {
|
2017-06-17 11:42:33 +08:00
|
|
|
opts.print_help = true;
|
2017-06-15 12:19:59 +08:00
|
|
|
break;
|
2017-06-15 09:25:51 +08:00
|
|
|
}
|
2017-06-30 12:49:57 +08:00
|
|
|
case ':': {
|
2017-07-02 05:03:47 +08:00
|
|
|
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
|
2017-06-30 12:49:57 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
|
|
|
}
|
2017-06-15 09:25:51 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Count the number of times the specified character occurs in the specified string.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int count_char(const wchar_t *str, wchar_t c) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int res = 0;
|
2016-04-19 10:25:12 +08:00
|
|
|
for (; *str; str++) {
|
|
|
|
res += (*str == c);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
return res;
|
2006-05-26 19:24:02 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-08-17 06:30:49 +08:00
|
|
|
/// Obtain help/usage information for the specified builtin from manpage in subshell
|
|
|
|
///
|
|
|
|
/// @param name
|
|
|
|
/// builtin name to get up help for
|
|
|
|
///
|
|
|
|
/// @return
|
|
|
|
/// A wcstring with a formatted manpage.
|
|
|
|
///
|
2016-04-19 10:25:12 +08:00
|
|
|
wcstring builtin_help_get(parser_t &parser, io_streams_t &streams, const wchar_t *name) {
|
2016-10-10 05:38:26 +08:00
|
|
|
UNUSED(parser);
|
2019-01-14 07:03:19 +08:00
|
|
|
UNUSED(streams);
|
2016-04-19 10:25:12 +08:00
|
|
|
// This won't ever work if no_exec is set.
|
|
|
|
if (no_exec) return wcstring();
|
2013-05-05 17:33:17 +08:00
|
|
|
|
2012-01-01 07:57:30 +08:00
|
|
|
wcstring_list_t lst;
|
2012-02-01 12:22:25 +08:00
|
|
|
wcstring out;
|
|
|
|
const wcstring name_esc = escape_string(name, 1);
|
2014-10-10 15:11:23 +08:00
|
|
|
wcstring cmd = format_string(L"__fish_print_help %ls", name_esc.c_str());
|
2018-09-22 12:52:47 +08:00
|
|
|
if (exec_subshell(cmd, parser, lst, false /* don't apply exit status */) >= 0) {
|
2016-04-19 10:25:12 +08:00
|
|
|
for (size_t i = 0; i < lst.size(); i++) {
|
2012-02-01 12:22:25 +08:00
|
|
|
out.append(lst.at(i));
|
|
|
|
out.push_back(L'\n');
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
2006-11-18 00:24:38 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:30:49 +08:00
|
|
|
/// Process and print for the specified builtin. If @c b is `sb_err`, also print the line
|
|
|
|
/// information.
|
2016-04-20 09:17:39 +08:00
|
|
|
///
|
2016-08-17 06:30:49 +08:00
|
|
|
/// If @c b is the buffer representing standard error, and the help message is about to be printed
|
2016-04-20 09:17:39 +08:00
|
|
|
/// to an interactive screen, it may be shortened to fit the screen.
|
2016-08-17 06:30:49 +08:00
|
|
|
///
|
2016-04-19 10:25:12 +08:00
|
|
|
void builtin_print_help(parser_t &parser, io_streams_t &streams, const wchar_t *cmd,
|
|
|
|
output_stream_t &b) {
|
2016-05-04 12:31:32 +08:00
|
|
|
bool is_stderr = &b == &streams.err;
|
2016-04-19 10:25:12 +08:00
|
|
|
if (is_stderr) {
|
2015-09-22 02:24:49 +08:00
|
|
|
b.append(parser.current_line());
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
|
2015-09-22 02:24:49 +08:00
|
|
|
const wcstring h = builtin_help_get(parser, streams, cmd);
|
2016-04-19 10:25:12 +08:00
|
|
|
|
|
|
|
if (!h.size()) return;
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
wchar_t *str = wcsdup(h.c_str());
|
2016-04-19 10:25:12 +08:00
|
|
|
if (str) {
|
2014-01-13 05:33:35 +08:00
|
|
|
bool is_short = false;
|
2016-04-19 10:25:12 +08:00
|
|
|
if (is_stderr) {
|
|
|
|
// Interactive mode help to screen - only print synopsis if the rest won't fit.
|
2018-12-11 23:48:45 +08:00
|
|
|
int screen_height, my_lines;
|
2016-04-19 10:25:12 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
screen_height = common_get_height();
|
2018-12-11 23:48:45 +08:00
|
|
|
my_lines = count_char(str, L'\n');
|
|
|
|
if (!shell_is_interactive() || (my_lines > 2 * screen_height / 3)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
wchar_t *pos;
|
2016-04-19 10:25:12 +08:00
|
|
|
int cut = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
int i;
|
2016-04-19 10:25:12 +08:00
|
|
|
|
2014-01-13 05:33:35 +08:00
|
|
|
is_short = true;
|
2016-04-19 10:25:12 +08:00
|
|
|
|
|
|
|
// First move down 4 lines.
|
2012-11-19 08:30:30 +08:00
|
|
|
pos = str;
|
2016-04-19 10:25:12 +08:00
|
|
|
for (i = 0; (i < 4) && pos && *pos; i++) {
|
2019-03-13 05:06:01 +08:00
|
|
|
pos = std::wcschr(pos + 1, L'\n');
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
|
|
|
|
if (pos && *pos) {
|
|
|
|
// Then find the next empty line.
|
|
|
|
for (; *pos; pos++) {
|
2016-10-31 10:17:08 +08:00
|
|
|
if (*pos != L'\n') {
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
|
2016-10-31 10:17:08 +08:00
|
|
|
int is_empty = 1;
|
|
|
|
wchar_t *pos2;
|
|
|
|
for (pos2 = pos + 1; *pos2; pos2++) {
|
|
|
|
if (*pos2 == L'\n') break;
|
2016-04-19 10:25:12 +08:00
|
|
|
|
2016-10-31 10:17:08 +08:00
|
|
|
if (*pos2 != L'\t' && *pos2 != L' ') {
|
|
|
|
is_empty = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-31 10:17:08 +08:00
|
|
|
if (is_empty) {
|
|
|
|
// And cut it.
|
|
|
|
*(pos2 + 1) = L'\0';
|
|
|
|
cut = 1;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
|
|
|
|
// We did not find a good place to cut message to shorten it - so we make sure we
|
|
|
|
// don't print anything.
|
|
|
|
if (!cut) {
|
2012-11-19 08:30:30 +08:00
|
|
|
*str = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
|
2012-02-23 02:51:06 +08:00
|
|
|
b.append(str);
|
2016-04-19 10:25:12 +08:00
|
|
|
if (is_short) {
|
2015-09-22 02:24:49 +08:00
|
|
|
b.append_format(_(L"%ls: Type 'help %ls' for related documentation\n\n"), cmd, cmd);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
free(str);
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2006-06-21 05:20:16 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Perform error reporting for encounter with unknown option.
|
2016-04-21 14:00:54 +08:00
|
|
|
void builtin_unknown_option(parser_t &parser, io_streams_t &streams, const wchar_t *cmd,
|
|
|
|
const wchar_t *opt) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_UNKNOWN, cmd, opt);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
2007-01-21 22:55:27 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Perform error reporting for encounter with missing argument.
|
2016-04-21 14:00:54 +08:00
|
|
|
void builtin_missing_argument(parser_t &parser, io_streams_t &streams, const wchar_t *cmd,
|
|
|
|
const wchar_t *opt) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_MISSING, cmd, opt);
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.err);
|
2007-08-02 03:44:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// A generic bultin that only supports showing a help message. This is only a placeholder that
|
|
|
|
/// prints the help message. Useful for commands that live in the parser.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_generic(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2017-06-15 13:46:12 +08:00
|
|
|
const wchar_t *cmd = argv[0];
|
2016-04-19 10:25:12 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
2017-06-17 11:42:33 +08:00
|
|
|
help_only_cmd_opts_t opts;
|
2017-06-15 13:46:12 +08:00
|
|
|
int optind;
|
2017-06-17 11:42:33 +08:00
|
|
|
int retval = parse_help_only_cmd_opts(opts, &optind, argc, argv, parser, streams);
|
2017-06-15 13:46:12 +08:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
|
|
|
if (opts.print_help) {
|
|
|
|
builtin_print_help(parser, streams, cmd, streams.out);
|
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Hackish - if we have no arguments other than the command, we are a "naked invocation" and we
|
|
|
|
// just print help.
|
|
|
|
if (argc == 1) {
|
2017-06-15 13:46:12 +08:00
|
|
|
builtin_print_help(parser, streams, cmd, streams.out);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2014-01-13 07:10:59 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2017-05-04 15:18:02 +08:00
|
|
|
return STATUS_CMD_ERROR;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-10-17 17:56:03 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Implementation of the builtin count command, used to count the number of arguments sent to it.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_count(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2016-10-10 05:38:26 +08:00
|
|
|
UNUSED(parser);
|
2017-06-14 09:27:03 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
2016-04-19 10:25:12 +08:00
|
|
|
streams.out.append_format(L"%d\n", argc - 1);
|
2017-06-14 09:27:03 +08:00
|
|
|
return argc - 1 == 0 ? STATUS_CMD_ERROR : STATUS_CMD_OK;
|
2007-08-01 05:23:32 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// This function handles both the 'continue' and the 'break' builtins that are used for loop
|
|
|
|
/// control.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_break_continue(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2019-03-13 05:06:01 +08:00
|
|
|
int is_break = (std::wcscmp(argv[0], L"break") == 0);
|
2012-11-19 08:30:30 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (argc != 1) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0], argv[1]);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Find the index of the enclosing for or while loop. Recall that incrementing loop_idx goes
|
|
|
|
// 'up' to outer blocks.
|
2013-12-21 09:41:21 +08:00
|
|
|
size_t loop_idx;
|
2016-04-19 10:25:12 +08:00
|
|
|
for (loop_idx = 0; loop_idx < parser.block_count(); loop_idx++) {
|
2013-12-21 09:41:21 +08:00
|
|
|
const block_t *b = parser.block_at_index(loop_idx);
|
2016-04-19 10:25:12 +08:00
|
|
|
if (b->type() == WHILE || b->type() == FOR) break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (loop_idx >= parser.block_count()) {
|
|
|
|
streams.err.append_format(_(L"%ls: Not inside of loop\n"), argv[0]);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2017-05-04 15:18:02 +08:00
|
|
|
return STATUS_CMD_ERROR;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-22 06:15:03 +08:00
|
|
|
// Skip blocks interior to the loop (but not the loop itself)
|
2013-12-21 09:41:21 +08:00
|
|
|
size_t block_idx = loop_idx;
|
2016-04-19 10:25:12 +08:00
|
|
|
while (block_idx--) {
|
2013-12-21 09:41:21 +08:00
|
|
|
parser.block_at_index(block_idx)->skip = true;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2017-01-22 06:15:03 +08:00
|
|
|
// Mark the loop's status
|
2013-12-21 09:41:21 +08:00
|
|
|
block_t *loop_block = parser.block_at_index(loop_idx);
|
|
|
|
loop_block->loop_status = is_break ? LOOP_BREAK : LOOP_CONTINUE;
|
2017-05-04 15:18:02 +08:00
|
|
|
return STATUS_CMD_OK;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Implementation of the builtin breakpoint command, used to launch the interactive debugger.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_breakpoint(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2017-06-20 12:05:34 +08:00
|
|
|
wchar_t *cmd = argv[0];
|
2016-10-10 05:38:26 +08:00
|
|
|
if (argv[1] != NULL) {
|
2017-06-20 12:05:34 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_ARG_COUNT1, cmd, 0, builtin_count_args(argv) - 1);
|
2017-05-05 12:35:41 +08:00
|
|
|
return STATUS_INVALID_ARGS;
|
2016-10-10 05:38:26 +08:00
|
|
|
}
|
|
|
|
|
2017-06-20 12:05:34 +08:00
|
|
|
// If we're not interactive then we can't enter the debugger. So treat this command as a no-op.
|
|
|
|
if (!shell_is_interactive()) {
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-06-20 12:05:34 +08:00
|
|
|
// Ensure we don't allow creating a breakpoint at an interactive prompt. There may be a simpler
|
|
|
|
// or clearer way to do this but this works.
|
|
|
|
const block_t *block1 = parser.block_at_index(1);
|
|
|
|
if (!block1 || block1->type() == BREAKPOINT) {
|
|
|
|
streams.err.append_format(_(L"%ls: Command not valid at an interactive prompt\n"), cmd);
|
|
|
|
return STATUS_ILLEGAL_CMD;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-06-20 12:05:34 +08:00
|
|
|
const breakpoint_block_t *bpb = parser.push_block<breakpoint_block_t>();
|
|
|
|
reader_read(STDIN_FILENO, streams.io_chain ? *streams.io_chain : io_chain_t());
|
2017-01-22 07:35:35 +08:00
|
|
|
parser.pop_block(bpb);
|
2012-11-19 08:30:30 +08:00
|
|
|
return proc_get_last_status();
|
2006-11-11 18:54:00 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
int builtin_true(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2016-10-10 05:38:26 +08:00
|
|
|
UNUSED(parser);
|
|
|
|
UNUSED(streams);
|
|
|
|
if (argv[1] != NULL) {
|
2017-05-05 12:35:41 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_ARG_COUNT1, argv[0], 0, builtin_count_args(argv) - 1);
|
|
|
|
return STATUS_INVALID_ARGS;
|
2016-10-10 05:38:26 +08:00
|
|
|
}
|
2017-05-04 15:18:02 +08:00
|
|
|
return STATUS_CMD_OK;
|
2014-09-30 04:26:28 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
int builtin_false(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2016-10-10 05:38:26 +08:00
|
|
|
UNUSED(parser);
|
|
|
|
UNUSED(streams);
|
|
|
|
if (argv[1] != NULL) {
|
2017-05-05 12:35:41 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_ARG_COUNT1, argv[0], 0, builtin_count_args(argv) - 1);
|
|
|
|
return STATUS_INVALID_ARGS;
|
2016-10-10 05:38:26 +08:00
|
|
|
}
|
2017-05-04 15:18:02 +08:00
|
|
|
return STATUS_CMD_ERROR;
|
2014-09-30 04:26:28 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// END OF BUILTIN COMMANDS
|
|
|
|
// Below are functions for handling the builtin commands.
|
2018-09-29 12:22:24 +08:00
|
|
|
// THESE MUST BE SORTED BY NAME! Completion lookup uses binary search.
|
2005-12-15 21:59:02 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Data about all the builtin commands in fish.
|
|
|
|
// Functions that are bound to builtin_generic are handled directly by the parser.
|
2018-09-29 12:22:24 +08:00
|
|
|
// NOTE: These must be kept in sorted order!
|
|
|
|
static const builtin_data_t builtin_datas[] = {
|
2016-04-19 10:25:12 +08:00
|
|
|
{L"[", &builtin_test, N_(L"Test a condition")},
|
|
|
|
{L"and", &builtin_generic, N_(L"Execute command if previous command suceeded")},
|
2017-07-08 05:32:41 +08:00
|
|
|
{L"argparse", &builtin_argparse, N_(L"Parse options in fish script")},
|
2016-04-19 10:25:12 +08:00
|
|
|
{L"begin", &builtin_generic, N_(L"Create a block of code")},
|
|
|
|
{L"bg", &builtin_bg, N_(L"Send job to background")},
|
|
|
|
{L"bind", &builtin_bind, N_(L"Handle fish key bindings")},
|
|
|
|
{L"block", &builtin_block, N_(L"Temporarily block delivery of events")},
|
|
|
|
{L"break", &builtin_break_continue, N_(L"Stop the innermost loop")},
|
2018-09-29 12:22:24 +08:00
|
|
|
{L"breakpoint", &builtin_breakpoint,
|
|
|
|
N_(L"Temporarily halt execution of a script and launch an interactive debug prompt")},
|
2016-04-19 10:25:12 +08:00
|
|
|
{L"builtin", &builtin_builtin, N_(L"Run a builtin command instead of a function")},
|
|
|
|
{L"case", &builtin_generic, N_(L"Conditionally execute a block of commands")},
|
|
|
|
{L"cd", &builtin_cd, N_(L"Change working directory")},
|
|
|
|
{L"command", &builtin_command, N_(L"Run a program instead of a function or builtin")},
|
|
|
|
{L"commandline", &builtin_commandline, N_(L"Set or get the commandline")},
|
|
|
|
{L"complete", &builtin_complete, N_(L"Edit command specific completions")},
|
|
|
|
{L"contains", &builtin_contains, N_(L"Search for a specified string in a list")},
|
2018-09-29 12:22:24 +08:00
|
|
|
{L"continue", &builtin_break_continue,
|
|
|
|
N_(L"Skip the rest of the current lap of the innermost loop")},
|
2016-04-19 10:25:12 +08:00
|
|
|
{L"count", &builtin_count, N_(L"Count the number of arguments")},
|
2017-03-23 08:50:57 +08:00
|
|
|
{L"disown", &builtin_disown, N_(L"Remove job from job list")},
|
2016-04-19 10:25:12 +08:00
|
|
|
{L"echo", &builtin_echo, N_(L"Print arguments")},
|
|
|
|
{L"else", &builtin_generic, N_(L"Evaluate block if condition is false")},
|
|
|
|
{L"emit", &builtin_emit, N_(L"Emit an event")},
|
|
|
|
{L"end", &builtin_generic, N_(L"End a block of commands")},
|
|
|
|
{L"exec", &builtin_generic, N_(L"Run command in current process")},
|
|
|
|
{L"exit", &builtin_exit, N_(L"Exit the shell")},
|
|
|
|
{L"false", &builtin_false, N_(L"Return an unsuccessful result")},
|
|
|
|
{L"fg", &builtin_fg, N_(L"Send job to foreground")},
|
|
|
|
{L"for", &builtin_generic, N_(L"Perform a set of commands multiple times")},
|
|
|
|
{L"function", &builtin_generic, N_(L"Define a new function")},
|
|
|
|
{L"functions", &builtin_functions, N_(L"List or remove functions")},
|
|
|
|
{L"history", &builtin_history, N_(L"History of commands executed by user")},
|
|
|
|
{L"if", &builtin_generic, N_(L"Evaluate block if condition is true")},
|
|
|
|
{L"jobs", &builtin_jobs, N_(L"Print currently running jobs")},
|
2017-08-23 10:57:30 +08:00
|
|
|
{L"math", &builtin_math, N_(L"Evaluate math expressions")},
|
2016-04-19 10:25:12 +08:00
|
|
|
{L"not", &builtin_generic, N_(L"Negate exit status of job")},
|
|
|
|
{L"or", &builtin_generic, N_(L"Execute command if previous command failed")},
|
|
|
|
{L"printf", &builtin_printf, N_(L"Prints formatted text")},
|
|
|
|
{L"pwd", &builtin_pwd, N_(L"Print the working directory")},
|
|
|
|
{L"random", &builtin_random, N_(L"Generate random number")},
|
|
|
|
{L"read", &builtin_read, N_(L"Read a line of input into variables")},
|
2016-10-04 08:51:27 +08:00
|
|
|
{L"realpath", &builtin_realpath, N_(L"Convert path to absolute path without symlinks")},
|
2016-04-19 10:25:12 +08:00
|
|
|
{L"return", &builtin_return, N_(L"Stop the currently evaluated function")},
|
|
|
|
{L"set", &builtin_set, N_(L"Handle environment variables")},
|
|
|
|
{L"set_color", &builtin_set_color, N_(L"Set the terminal color")},
|
|
|
|
{L"source", &builtin_source, N_(L"Evaluate contents of file")},
|
|
|
|
{L"status", &builtin_status, N_(L"Return status information about fish")},
|
|
|
|
{L"string", &builtin_string, N_(L"Manipulate strings")},
|
|
|
|
{L"switch", &builtin_generic, N_(L"Conditionally execute a block of commands")},
|
|
|
|
{L"test", &builtin_test, N_(L"Test a condition")},
|
|
|
|
{L"true", &builtin_true, N_(L"Return a successful result")},
|
|
|
|
{L"ulimit", &builtin_ulimit, N_(L"Set or get the shells resource usage limits")},
|
2017-10-22 15:10:23 +08:00
|
|
|
{L"wait", &builtin_wait, N_(L"Wait for background processes completed")},
|
2016-04-19 10:25:12 +08:00
|
|
|
{L"while", &builtin_generic, N_(L"Perform a command multiple times")}};
|
2012-02-01 11:47:56 +08:00
|
|
|
|
2018-09-29 12:22:24 +08:00
|
|
|
#define BUILTIN_COUNT (sizeof builtin_datas / sizeof *builtin_datas)
|
|
|
|
|
2016-08-17 06:30:49 +08:00
|
|
|
/// Look up a builtin_data_t for a specified builtin
|
|
|
|
///
|
|
|
|
/// @param name
|
|
|
|
/// Name of the builtin
|
|
|
|
///
|
|
|
|
/// @return
|
|
|
|
/// Pointer to a builtin_data_t
|
|
|
|
///
|
2016-04-19 10:25:12 +08:00
|
|
|
static const builtin_data_t *builtin_lookup(const wcstring &name) {
|
2018-09-29 12:22:24 +08:00
|
|
|
const builtin_data_t *array_end = builtin_datas + BUILTIN_COUNT;
|
|
|
|
const builtin_data_t *found = std::lower_bound(builtin_datas, array_end, name);
|
|
|
|
if (found != array_end && name == found->name) {
|
|
|
|
return found;
|
2012-02-01 11:47:56 +08:00
|
|
|
}
|
2018-09-29 12:22:24 +08:00
|
|
|
return NULL;
|
2006-02-05 21:08:40 +08:00
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2016-08-17 06:30:49 +08:00
|
|
|
/// Initialize builtin data.
|
2016-04-19 10:25:12 +08:00
|
|
|
void builtin_init() {
|
2018-09-29 12:22:24 +08:00
|
|
|
for (size_t i = 0; i < BUILTIN_COUNT; i++) {
|
2018-09-29 12:26:07 +08:00
|
|
|
const wchar_t *name = builtin_datas[i].name;
|
|
|
|
intern_static(name);
|
2019-03-13 05:06:01 +08:00
|
|
|
assert((i == 0 || std::wcscmp(builtin_datas[i - 1].name, name) < 0) &&
|
2018-09-29 12:26:07 +08:00
|
|
|
"builtins are not sorted alphabetically");
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:30:49 +08:00
|
|
|
/// Is there a builtin command with the given name?
|
2016-10-24 04:58:12 +08:00
|
|
|
bool builtin_exists(const wcstring &cmd) { return static_cast<bool>(builtin_lookup(cmd)); }
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2017-07-20 02:44:53 +08:00
|
|
|
/// Is the command a keyword we need to special-case the handling of `-h` and `--help`.
|
2018-10-01 07:57:05 +08:00
|
|
|
static const wchar_t *const help_builtins[] = {L"for", L"while", L"function", L"if",
|
|
|
|
L"end", L"switch", L"case"};
|
2017-06-24 14:19:09 +08:00
|
|
|
static bool cmd_needs_help(const wchar_t *cmd) { return contains(help_builtins, cmd); }
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-08-17 06:30:49 +08:00
|
|
|
/// Execute a builtin command
|
2019-02-26 02:05:42 +08:00
|
|
|
proc_status_t builtin_run(parser_t &parser, int job_pgid, wchar_t **argv, io_streams_t &streams) {
|
2016-11-02 10:12:14 +08:00
|
|
|
UNUSED(parser);
|
|
|
|
UNUSED(streams);
|
2019-02-26 02:05:42 +08:00
|
|
|
if (argv == NULL || argv[0] == NULL) return proc_status_t::from_exit_code(STATUS_INVALID_ARGS);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-06-19 13:07:48 +08:00
|
|
|
// We can be handed a keyword by the parser as if it was a command. This happens when the user
|
|
|
|
// follows the keyword by `-h` or `--help`. Since it isn't really a builtin command we need to
|
|
|
|
// handle displaying help for it here.
|
|
|
|
if (argv[1] && !argv[2] && parse_util_argument_is_help(argv[1]) && cmd_needs_help(argv[0])) {
|
2016-10-23 02:21:13 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
2019-02-26 02:05:42 +08:00
|
|
|
return proc_status_t::from_exit_code(STATUS_CMD_OK);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2018-08-05 07:15:06 +08:00
|
|
|
if (const builtin_data_t *data = builtin_lookup(argv[0])) {
|
2018-08-05 08:32:04 +08:00
|
|
|
// If we are interactive, save the foreground pgroup and restore it after in case the
|
|
|
|
// builtin needs to read from the terminal. See #4540.
|
|
|
|
bool grab_tty = is_interactive_session && isatty(streams.stdin_fd);
|
2018-08-19 07:56:01 +08:00
|
|
|
pid_t pgroup_to_restore = grab_tty ? terminal_acquire_before_builtin(job_pgid) : -1;
|
2018-08-05 08:32:04 +08:00
|
|
|
int ret = data->func(parser, streams, argv);
|
|
|
|
if (pgroup_to_restore >= 0) {
|
|
|
|
tcsetpgrp(STDIN_FILENO, pgroup_to_restore);
|
|
|
|
}
|
2019-02-26 02:05:42 +08:00
|
|
|
return proc_status_t::from_exit_code(ret);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
|
|
|
|
debug(0, UNKNOWN_BUILTIN_ERR_MSG, argv[0]);
|
2019-02-26 02:05:42 +08:00
|
|
|
return proc_status_t::from_exit_code(STATUS_CMD_ERROR);
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:30:49 +08:00
|
|
|
/// Returns a list of all builtin names.
|
2018-02-19 10:33:04 +08:00
|
|
|
wcstring_list_t builtin_get_names() {
|
2012-02-01 11:47:56 +08:00
|
|
|
wcstring_list_t result;
|
2018-09-29 12:22:24 +08:00
|
|
|
result.reserve(BUILTIN_COUNT);
|
|
|
|
for (size_t i = 0; i < BUILTIN_COUNT; i++) {
|
|
|
|
result.push_back(builtin_datas[i].name);
|
2012-02-01 11:47:56 +08:00
|
|
|
}
|
|
|
|
return result;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:30:49 +08:00
|
|
|
/// Insert all builtin names into list.
|
2016-04-19 10:25:12 +08:00
|
|
|
void builtin_get_names(std::vector<completion_t> *list) {
|
2015-07-28 09:45:47 +08:00
|
|
|
assert(list != NULL);
|
2018-09-29 12:22:24 +08:00
|
|
|
list->reserve(list->size() + BUILTIN_COUNT);
|
|
|
|
for (size_t i = 0; i < BUILTIN_COUNT; i++) {
|
|
|
|
append_completion(list, builtin_datas[i].name);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-01-17 00:56:47 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:30:49 +08:00
|
|
|
/// Return a one-line description of the specified builtin.
|
2019-03-15 06:12:14 +08:00
|
|
|
const wchar_t *builtin_get_desc(const wcstring &name) {
|
|
|
|
const wchar_t *result;
|
2012-11-19 08:30:30 +08:00
|
|
|
const builtin_data_t *builtin = builtin_lookup(name);
|
2016-04-19 10:25:12 +08:00
|
|
|
if (builtin) {
|
2018-09-29 12:22:24 +08:00
|
|
|
result = _(builtin->desc);
|
2012-05-18 10:37:46 +08:00
|
|
|
}
|
|
|
|
return result;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|