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:
|
|
|
|
//
|
|
|
|
// <tt>static int builtin_NAME( parser_t &parser, wchar_t ** args )</tt>
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 3). Create a file doc_src/NAME.txt, containing the manual for the builtin in Doxygen-format.
|
|
|
|
// Check the other builtin manuals for proper syntax.
|
|
|
|
//
|
|
|
|
// 4). Use 'git add 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>
|
|
|
|
#include <fcntl.h>
|
2016-04-20 10:49:15 +08:00
|
|
|
#include <limits.h>
|
2016-11-22 10:10:15 +08:00
|
|
|
#include <stdarg.h>
|
2017-02-13 12:24:22 +08:00
|
|
|
#include <stdint.h>
|
2017-02-14 12:37:27 +08:00
|
|
|
#include <stdio.h>
|
2016-04-19 10:25:12 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
2017-02-14 12:37:27 +08:00
|
|
|
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
2016-06-24 08:24:19 +08:00
|
|
|
#include <memory>
|
2016-11-29 04:57:58 +08:00
|
|
|
#include <random>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <string>
|
2017-03-17 01:35:44 +08:00
|
|
|
#include <set>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <utility>
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
#include "builtin.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "builtin_commandline.h"
|
|
|
|
#include "builtin_complete.h"
|
|
|
|
#include "builtin_jobs.h"
|
|
|
|
#include "builtin_printf.h"
|
|
|
|
#include "builtin_set.h"
|
|
|
|
#include "builtin_set_color.h"
|
|
|
|
#include "builtin_string.h"
|
|
|
|
#include "builtin_test.h"
|
|
|
|
#include "builtin_ulimit.h"
|
|
|
|
#include "common.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "complete.h"
|
|
|
|
#include "env.h"
|
2005-10-06 06:37:08 +08:00
|
|
|
#include "event.h"
|
2006-11-18 00:24:38 +08:00
|
|
|
#include "exec.h"
|
2016-04-19 10:25:12 +08:00
|
|
|
#include "expand.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2016-04-19 10:25:12 +08:00
|
|
|
#include "function.h"
|
2007-01-30 00:26:24 +08:00
|
|
|
#include "highlight.h"
|
2016-04-19 10:25:12 +08:00
|
|
|
#include "history.h"
|
|
|
|
#include "input.h"
|
|
|
|
#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"
|
2007-04-22 17:50:26 +08:00
|
|
|
#include "parser_keywords.h"
|
2006-10-19 19:50:23 +08:00
|
|
|
#include "path.h"
|
2016-04-19 10:25:12 +08:00
|
|
|
#include "proc.h"
|
|
|
|
#include "reader.h"
|
|
|
|
#include "signal.h"
|
|
|
|
#include "tokenizer.h"
|
2014-09-22 10:18:56 +08:00
|
|
|
#include "wcstringutil.h"
|
2016-04-19 10:25:12 +08:00
|
|
|
#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
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
bool builtin_data_t::operator<(const wcstring &other) const {
|
2012-02-01 12:22:25 +08:00
|
|
|
return wcscmp(this->name, other.c_str()) < 0;
|
2012-02-01 11:47:56 +08:00
|
|
|
}
|
2006-02-05 21:08:40 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
bool builtin_data_t::operator<(const builtin_data_t *other) const {
|
2012-02-01 11:47:56 +08:00
|
|
|
return wcscmp(this->name, other->name) < 0;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-11-22 10:10:15 +08:00
|
|
|
static void builtin_append_format(wcstring &str, const wchar_t *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
append_formatv(str, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool builtin_is_valid_varname(const wchar_t *varname, wcstring &errstr, const wchar_t *cmd) {
|
|
|
|
const wchar_t *invalid_char = wcsvarname(varname);
|
|
|
|
if (!invalid_char) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*invalid_char == L'\0') {
|
|
|
|
builtin_append_format(errstr, BUILTIN_ERR_VARNAME_ZERO, cmd);
|
|
|
|
} else {
|
|
|
|
builtin_append_format(errstr, BUILTIN_ERR_VARCHAR, cmd, *invalid_char);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-09-22 02:24:49 +08:00
|
|
|
char *err = 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
|
|
|
}
|
|
|
|
|
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);
|
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());
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!streams.out_is_redirected && isatty(STDOUT_FILENO)) {
|
2014-10-10 15:11:23 +08:00
|
|
|
// since we're using a subshell, __fish_print_help can't tell we're in
|
|
|
|
// a terminal. Tell it ourselves.
|
|
|
|
int cols = common_get_width();
|
|
|
|
cmd = format_string(L"__fish_print_help --tty-width %d %ls", cols, name_esc.c_str());
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
if (exec_subshell(cmd, lst, false /* don't apply exit status */) >= 0) {
|
|
|
|
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.
|
2012-11-19 08:30:30 +08:00
|
|
|
int screen_height, lines;
|
2016-04-19 10:25:12 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
screen_height = common_get_height();
|
|
|
|
lines = count_char(str, L'\n');
|
2016-05-15 11:35:54 +08:00
|
|
|
if (!shell_is_interactive() || (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++) {
|
|
|
|
pos = 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 10:49:15 +08:00
|
|
|
// Here follows the definition of all builtin commands. The function names are all of the form
|
2016-04-19 10:25:12 +08:00
|
|
|
// builtin_NAME where NAME is the name of the builtin. so the function name for the builtin 'fg' is
|
|
|
|
// 'builtin_fg'.
|
|
|
|
//
|
|
|
|
// A few builtins, including 'while', 'command' and 'builtin' are not defined here as they are
|
|
|
|
// handled directly by the parser. (They are not parsed as commands, instead they only alter the
|
|
|
|
// parser state)
|
|
|
|
//
|
|
|
|
// The builtins 'break' and 'continue' are so closely related that they share the same
|
|
|
|
// implementation, namely 'builtin_break_continue.
|
|
|
|
//
|
|
|
|
// Several other builtins, including jobs, ulimit and set are so big that they have been given their
|
2016-04-20 10:49:15 +08:00
|
|
|
// own module. These files are all named 'builtin_NAME.cpp', where NAME is the name of the builtin.
|
2015-09-13 04:36:22 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// List a single key binding.
|
|
|
|
/// Returns false if no binding with that sequence and mode exists.
|
2016-04-19 10:25:12 +08:00
|
|
|
static bool builtin_bind_list_one(const wcstring &seq, const wcstring &bind_mode,
|
|
|
|
io_streams_t &streams) {
|
2014-09-23 12:04:06 +08:00
|
|
|
std::vector<wcstring> ecmds;
|
|
|
|
wcstring sets_mode;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!input_mapping_get(seq, bind_mode, &ecmds, &sets_mode)) {
|
2014-09-23 12:04:06 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(L"bind");
|
2013-12-31 08:52:41 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Append the mode flags if applicable.
|
|
|
|
if (bind_mode != DEFAULT_BIND_MODE) {
|
2014-09-23 12:04:06 +08:00
|
|
|
const wcstring emode = escape_string(bind_mode, ESCAPE_ALL);
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(L" -M ");
|
|
|
|
streams.out.append(emode);
|
2014-09-23 12:04:06 +08:00
|
|
|
}
|
2017-02-01 06:49:56 +08:00
|
|
|
if (!sets_mode.empty() && sets_mode != bind_mode) {
|
2014-09-23 12:04:06 +08:00
|
|
|
const wcstring esets_mode = escape_string(sets_mode, ESCAPE_ALL);
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(L" -m ");
|
|
|
|
streams.out.append(esets_mode);
|
2014-09-23 12:04:06 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Append the name.
|
2014-09-23 12:04:06 +08:00
|
|
|
wcstring tname;
|
2016-04-19 10:25:12 +08:00
|
|
|
if (input_terminfo_get_name(seq, &tname)) {
|
|
|
|
// Note that we show -k here because we have an input key name.
|
|
|
|
streams.out.append_format(L" -k %ls", tname.c_str());
|
|
|
|
} else {
|
|
|
|
// No key name, so no -k; we show the escape sequence directly.
|
2014-09-23 12:04:06 +08:00
|
|
|
const wcstring eseq = escape_string(seq, ESCAPE_ALL);
|
2016-04-19 10:25:12 +08:00
|
|
|
streams.out.append_format(L" %ls", eseq.c_str());
|
2014-09-23 12:04:06 +08:00
|
|
|
}
|
2014-09-23 03:37:11 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Now show the list of commands.
|
|
|
|
for (size_t i = 0; i < ecmds.size(); i++) {
|
2014-09-23 12:04:06 +08:00
|
|
|
const wcstring &ecmd = ecmds.at(i);
|
|
|
|
const wcstring escaped_ecmd = escape_string(ecmd, ESCAPE_ALL);
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.push_back(' ');
|
|
|
|
streams.out.append(escaped_ecmd);
|
2014-09-23 12:04:06 +08:00
|
|
|
}
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.push_back(L'\n');
|
2014-09-23 03:37:11 +08:00
|
|
|
|
2014-09-23 12:04:06 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-09-23 03:37:11 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// List all current key bindings.
|
2016-04-19 10:25:12 +08:00
|
|
|
static void builtin_bind_list(const wchar_t *bind_mode, io_streams_t &streams) {
|
2014-09-23 12:04:06 +08:00
|
|
|
const std::vector<input_mapping_name_t> lst = input_mapping_get_names();
|
2014-09-23 03:37:11 +08:00
|
|
|
|
2017-02-01 06:49:56 +08:00
|
|
|
for (const input_mapping_name_t &binding : lst) {
|
|
|
|
if (bind_mode != NULL && bind_mode != binding.mode) {
|
2014-09-23 12:04:06 +08:00
|
|
|
continue;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2014-09-23 12:04:06 +08:00
|
|
|
|
2017-02-01 06:49:56 +08:00
|
|
|
builtin_bind_list_one(binding.seq, binding.mode, streams);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2007-09-26 00:14:47 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Print terminfo key binding names to string buffer used for standard output.
|
|
|
|
///
|
|
|
|
/// \param all if set, all terminfo key binding names will be printed. If not set, only ones that
|
|
|
|
/// are defined for this terminal are printed.
|
2016-04-19 10:25:12 +08:00
|
|
|
static void builtin_bind_key_names(int all, io_streams_t &streams) {
|
2012-11-19 08:30:30 +08:00
|
|
|
const wcstring_list_t names = input_terminfo_get_names(!all);
|
2016-04-19 10:25:12 +08:00
|
|
|
for (size_t i = 0; i < names.size(); i++) {
|
2012-02-08 09:06:45 +08:00
|
|
|
const wcstring &name = names.at(i);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
streams.out.append_format(L"%ls\n", name.c_str());
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2007-09-26 00:14:47 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Print all the special key binding functions to string buffer used for standard output.
|
2016-04-19 10:25:12 +08:00
|
|
|
static void builtin_bind_function_names(io_streams_t &streams) {
|
2012-01-24 12:48:47 +08:00
|
|
|
wcstring_list_t names = input_function_get_names();
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
for (size_t i = 0; i < names.size(); i++) {
|
2012-11-19 08:30:30 +08:00
|
|
|
const wchar_t *seq = names.at(i).c_str();
|
2016-04-19 10:25:12 +08:00
|
|
|
streams.out.append_format(L"%ls\n", seq);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2007-09-26 00:14:47 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Wraps input_terminfo_get_sequence(), appending the correct error messages as needed.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int get_terminfo_sequence(const wchar_t *seq, wcstring *out_seq, io_streams_t &streams) {
|
|
|
|
if (input_terminfo_get_sequence(seq, out_seq)) {
|
2014-09-23 10:21:08 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2016-10-23 11:32:25 +08:00
|
|
|
|
2014-09-23 12:30:44 +08:00
|
|
|
wcstring eseq = escape_string(seq, 0);
|
2016-10-23 11:32:25 +08:00
|
|
|
if (errno == ENOENT) {
|
|
|
|
streams.err.append_format(_(L"%ls: No key with name '%ls' found\n"), L"bind", eseq.c_str());
|
|
|
|
} else if (errno == EILSEQ) {
|
|
|
|
streams.err.append_format(_(L"%ls: Key with name '%ls' does not have any mapping\n"),
|
|
|
|
L"bind", eseq.c_str());
|
|
|
|
} else {
|
|
|
|
streams.err.append_format(_(L"%ls: Unknown error trying to bind to key named '%ls'\n"),
|
|
|
|
L"bind", eseq.c_str());
|
2014-09-23 10:21:08 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Add specified key binding.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_bind_add(const wchar_t *seq, const wchar_t *const *cmds, size_t cmds_len,
|
2015-09-22 02:24:49 +08:00
|
|
|
const wchar_t *mode, const wchar_t *sets_mode, int terminfo,
|
2016-04-19 10:25:12 +08:00
|
|
|
io_streams_t &streams) {
|
|
|
|
if (terminfo) {
|
2013-04-16 04:07:17 +08:00
|
|
|
wcstring seq2;
|
2016-04-19 10:25:12 +08:00
|
|
|
if (get_terminfo_sequence(seq, &seq2, streams)) {
|
2014-01-15 18:39:19 +08:00
|
|
|
input_mapping_add(seq2.c_str(), cmds, cmds_len, mode, sets_mode);
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
2014-01-15 18:39:19 +08:00
|
|
|
input_mapping_add(seq, cmds, cmds_len, mode, sets_mode);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2007-09-26 00:14:47 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Erase specified key bindings
|
|
|
|
///
|
2016-08-17 06:30:49 +08:00
|
|
|
/// @param seq
|
|
|
|
/// an array of all key bindings to erase
|
|
|
|
/// @param all
|
|
|
|
/// if specified, _all_ key bindings will be erased
|
|
|
|
/// @param mode
|
|
|
|
/// if specified, only bindings from that mode will be erased. If not given
|
2016-10-02 08:21:40 +08:00
|
|
|
/// and @c all is @c false, @c DEFAULT_BIND_MODE will be used.
|
2016-08-17 06:30:49 +08:00
|
|
|
/// @param use_terminfo
|
|
|
|
/// Whether to look use terminfo -k name
|
|
|
|
///
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_bind_erase(wchar_t **seq, int all, const wchar_t *mode, int use_terminfo,
|
|
|
|
io_streams_t &streams) {
|
|
|
|
if (all) {
|
2014-09-23 12:04:06 +08:00
|
|
|
const std::vector<input_mapping_name_t> lst = input_mapping_get_names();
|
|
|
|
for (std::vector<input_mapping_name_t>::const_iterator it = lst.begin(), end = lst.end();
|
2016-04-19 10:25:12 +08:00
|
|
|
it != end; ++it) {
|
|
|
|
if (mode == NULL || mode == it->mode) {
|
2014-09-23 12:04:06 +08:00
|
|
|
input_mapping_erase(it->seq, it->mode);
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-10-02 08:21:40 +08:00
|
|
|
|
|
|
|
return 0;
|
2016-05-05 06:19:47 +08:00
|
|
|
}
|
2016-10-02 08:21:40 +08:00
|
|
|
|
2016-05-05 06:19:47 +08:00
|
|
|
int res = 0;
|
2016-10-21 09:53:31 +08:00
|
|
|
if (mode == NULL) mode = DEFAULT_BIND_MODE; //!OCLINT(parameter reassignment)
|
2016-10-02 08:21:40 +08:00
|
|
|
|
2016-05-05 06:19:47 +08:00
|
|
|
while (*seq) {
|
|
|
|
if (use_terminfo) {
|
|
|
|
wcstring seq2;
|
|
|
|
if (get_terminfo_sequence(*seq++, &seq2, streams)) {
|
2016-10-02 08:21:40 +08:00
|
|
|
input_mapping_erase(seq2, mode);
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
2016-10-02 08:21:40 +08:00
|
|
|
res = 1;
|
2014-09-23 10:21:08 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
} else {
|
2016-10-02 08:21:40 +08:00
|
|
|
input_mapping_erase(*seq++, mode);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
|
|
|
|
return res;
|
2005-12-12 06:21:01 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 01:35:44 +08:00
|
|
|
/// List all current bind modes.
|
|
|
|
static void builtin_bind_list_modes(io_streams_t &streams) {
|
|
|
|
const std::vector<input_mapping_name_t> lst = input_mapping_get_names();
|
|
|
|
// A set accomplishes two things for us here:
|
|
|
|
// - It removes duplicates (no twenty "default" entries).
|
|
|
|
// - It sorts it, which makes it nicer on the user.
|
|
|
|
std::set<wcstring> modes;
|
|
|
|
|
|
|
|
for (const input_mapping_name_t &binding : lst) {
|
|
|
|
modes.insert(binding.mode);
|
|
|
|
}
|
|
|
|
for (const auto& mode : modes) {
|
|
|
|
streams.out.append_format(L"%ls\n", mode.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// The bind builtin, used for setting character sequences.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_bind(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2015-07-26 09:16:00 +08:00
|
|
|
wgetopter_t w;
|
2016-04-19 10:25:12 +08:00
|
|
|
enum { BIND_INSERT, BIND_ERASE, BIND_KEY_NAMES, BIND_FUNCTION_NAMES };
|
|
|
|
int argc = builtin_count_args(argv);
|
2012-11-19 08:30:30 +08:00
|
|
|
int mode = BIND_INSERT;
|
|
|
|
int res = STATUS_BUILTIN_OK;
|
2016-10-02 08:21:40 +08:00
|
|
|
int all = 0;
|
2013-12-31 08:52:41 +08:00
|
|
|
const wchar_t *bind_mode = DEFAULT_BIND_MODE;
|
2013-12-31 21:53:29 +08:00
|
|
|
bool bind_mode_given = false;
|
2017-02-01 06:49:56 +08:00
|
|
|
const wchar_t *sets_bind_mode = L"";
|
2016-10-02 08:21:40 +08:00
|
|
|
int use_terminfo = 0;
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
w.woptind = 0;
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
static const struct woption long_options[] = {{L"all", no_argument, 0, 'a'},
|
|
|
|
{L"erase", no_argument, 0, 'e'},
|
|
|
|
{L"function-names", no_argument, 0, 'f'},
|
|
|
|
{L"help", no_argument, 0, 'h'},
|
|
|
|
{L"key", no_argument, 0, 'k'},
|
|
|
|
{L"key-names", no_argument, 0, 'K'},
|
|
|
|
{L"mode", required_argument, 0, 'M'},
|
2017-03-17 01:35:44 +08:00
|
|
|
{L"list-modes", no_argument, 0, 'L'},
|
2016-04-19 10:25:12 +08:00
|
|
|
{L"sets-mode", required_argument, 0, 'm'},
|
|
|
|
{0, 0, 0, 0}};
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
while (1) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int opt_index = 0;
|
2017-03-17 01:35:44 +08:00
|
|
|
int opt = w.wgetopt_long_only(argc, argv, L"aehkKfM:Lm:", long_options, &opt_index);
|
2016-04-19 10:25:12 +08:00
|
|
|
if (opt == -1) break;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 0: {
|
|
|
|
if (long_options[opt_index].flag != 0) break;
|
|
|
|
streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
|
|
|
|
long_options[opt_index].name);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'a': {
|
2016-10-02 08:21:40 +08:00
|
|
|
all = 1;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'e': {
|
2012-11-19 16:31:03 +08:00
|
|
|
mode = BIND_ERASE;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'h': {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'k': {
|
2016-10-02 08:21:40 +08:00
|
|
|
use_terminfo = 1;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'K': {
|
2012-11-19 16:31:03 +08:00
|
|
|
mode = BIND_KEY_NAMES;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'f': {
|
2012-11-19 16:31:03 +08:00
|
|
|
mode = BIND_FUNCTION_NAMES;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'M': {
|
2015-07-26 09:16:00 +08:00
|
|
|
bind_mode = w.woptarg;
|
2013-12-31 21:53:29 +08:00
|
|
|
bind_mode_given = true;
|
2013-12-31 08:52:41 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'm': {
|
2015-07-26 09:16:00 +08:00
|
|
|
sets_bind_mode = w.woptarg;
|
2013-12-31 08:52:41 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2017-03-17 01:35:44 +08:00
|
|
|
case 'L': {
|
|
|
|
builtin_bind_list_modes(streams);
|
|
|
|
return STATUS_BUILTIN_OK;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case '?': {
|
|
|
|
builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2013-12-31 21:53:29 +08:00
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (mode) {
|
|
|
|
case BIND_ERASE: {
|
|
|
|
if (builtin_bind_erase(&argv[w.woptind], all, bind_mode_given ? bind_mode : NULL,
|
|
|
|
use_terminfo, streams)) {
|
2014-09-23 10:21:08 +08:00
|
|
|
res = STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case BIND_INSERT: {
|
2016-10-23 11:32:25 +08:00
|
|
|
int arg_count = argc - w.woptind;
|
|
|
|
if (arg_count == 0) {
|
|
|
|
builtin_bind_list(bind_mode_given ? bind_mode : NULL, streams);
|
|
|
|
} else if (arg_count == 1) {
|
|
|
|
wcstring seq;
|
|
|
|
if (use_terminfo) {
|
|
|
|
if (!get_terminfo_sequence(argv[w.woptind], &seq, streams)) {
|
|
|
|
res = STATUS_BUILTIN_ERROR;
|
|
|
|
// get_terminfo_sequence already printed the error.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
seq = argv[w.woptind];
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2016-10-23 11:32:25 +08:00
|
|
|
if (!builtin_bind_list_one(seq, bind_mode, streams)) {
|
|
|
|
res = STATUS_BUILTIN_ERROR;
|
|
|
|
wcstring eseq = escape_string(argv[w.woptind], 0);
|
2016-04-19 10:25:12 +08:00
|
|
|
if (use_terminfo) {
|
2016-10-23 11:32:25 +08:00
|
|
|
streams.err.append_format(_(L"%ls: No binding found for key '%ls'\n"),
|
|
|
|
argv[0], eseq.c_str());
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
2016-10-23 11:32:25 +08:00
|
|
|
streams.err.append_format(_(L"%ls: No binding found for sequence '%ls'\n"),
|
|
|
|
argv[0], eseq.c_str());
|
2014-09-23 12:30:44 +08:00
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2016-10-23 11:32:25 +08:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (builtin_bind_add(argv[w.woptind], argv + (w.woptind + 1),
|
|
|
|
argc - (w.woptind + 1), bind_mode, sets_bind_mode,
|
|
|
|
use_terminfo, streams)) {
|
|
|
|
res = STATUS_BUILTIN_ERROR;
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case BIND_KEY_NAMES: {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_bind_key_names(all, streams);
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case BIND_FUNCTION_NAMES: {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_bind_function_names(streams);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
default: {
|
2012-11-19 16:31:03 +08:00
|
|
|
res = STATUS_BUILTIN_ERROR;
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Invalid state\n"), argv[0]);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-03-26 13:41:22 +08:00
|
|
|
}
|
2005-12-17 20:25:46 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return res;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// The block builtin, used for temporarily blocking events.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_block(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2015-07-26 09:16:00 +08:00
|
|
|
wgetopter_t w;
|
2016-04-19 10:25:12 +08:00
|
|
|
enum {
|
2012-11-19 08:30:30 +08:00
|
|
|
UNSET,
|
|
|
|
GLOBAL,
|
|
|
|
LOCAL,
|
2016-04-19 10:25:12 +08:00
|
|
|
};
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
int scope = UNSET;
|
2012-11-19 08:30:30 +08:00
|
|
|
int erase = 0;
|
2016-04-19 10:25:12 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
2012-03-08 03:35:22 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
w.woptind = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
static const struct woption long_options[] = {{L"erase", no_argument, 0, 'e'},
|
|
|
|
{L"local", no_argument, 0, 'l'},
|
|
|
|
{L"global", no_argument, 0, 'g'},
|
|
|
|
{L"help", no_argument, 0, 'h'},
|
|
|
|
{0, 0, 0, 0}};
|
2012-10-17 17:56:03 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
while (1) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int opt_index = 0;
|
2016-04-19 10:25:12 +08:00
|
|
|
int opt = w.wgetopt_long(argc, argv, L"elgh", long_options, &opt_index);
|
|
|
|
if (opt == -1) break;
|
|
|
|
|
|
|
|
switch (opt) {
|
|
|
|
case 0: {
|
|
|
|
if (long_options[opt_index].flag != 0) break;
|
|
|
|
streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
|
|
|
|
long_options[opt_index].name);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'h': {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'g': {
|
2012-11-19 16:31:03 +08:00
|
|
|
scope = GLOBAL;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'l': {
|
2012-11-19 16:31:03 +08:00
|
|
|
scope = LOCAL;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'e': {
|
2012-11-19 16:31:03 +08:00
|
|
|
erase = 1;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
|
|
|
builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (erase) {
|
|
|
|
if (scope != UNSET) {
|
|
|
|
streams.err.append_format(_(L"%ls: Can not specify scope when removing block\n"),
|
|
|
|
argv[0]);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (parser.global_event_blocks.empty()) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append_format(_(L"%ls: No blocks defined\n"), argv[0]);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
parser.global_event_blocks.pop_front();
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
2013-12-21 09:41:21 +08:00
|
|
|
size_t block_idx = 0;
|
|
|
|
block_t *block = parser.block_at_index(block_idx);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
event_blockage_t eb = {};
|
2016-04-19 10:25:12 +08:00
|
|
|
eb.typemask = (1 << EVENT_ANY);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (scope) {
|
|
|
|
case LOCAL: {
|
2013-12-21 09:41:21 +08:00
|
|
|
// If this is the outermost block, then we're global
|
2016-04-19 10:25:12 +08:00
|
|
|
if (block_idx + 1 >= parser.block_count()) {
|
2013-12-21 09:41:21 +08:00
|
|
|
block = NULL;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case GLOBAL: {
|
|
|
|
block = NULL;
|
2016-11-02 13:36:30 +08:00
|
|
|
break;
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case UNSET: {
|
|
|
|
while (block != NULL && block->type() != FUNCTION_CALL &&
|
|
|
|
block->type() != FUNCTION_CALL_NO_SHADOW) {
|
2013-12-21 09:41:21 +08:00
|
|
|
// Set it in function scope
|
|
|
|
block = parser.block_at_index(++block_idx);
|
|
|
|
}
|
2016-11-02 13:36:30 +08:00
|
|
|
break;
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected scope");
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
if (block) {
|
2012-11-19 08:30:30 +08:00
|
|
|
block->event_blocks.push_front(eb);
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
parser.global_event_blocks.push_front(eb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_BUILTIN_OK;
|
2012-10-17 17:56:03 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// The builtin builtin, used for giving builtins precedence over functions. Mostly handled by the
|
|
|
|
/// parser. All this code does is some additional operational modes, such as printing a list of all
|
|
|
|
/// builtins, printing help, etc.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_builtin(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
|
|
|
int argc = builtin_count_args(argv);
|
|
|
|
int list = 0;
|
2015-07-26 09:16:00 +08:00
|
|
|
wgetopter_t w;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
static const struct woption long_options[] = {
|
|
|
|
{L"names", no_argument, 0, 'n'}, {L"help", no_argument, 0, 'h'}, {0, 0, 0, 0}};
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
while (1) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int opt_index = 0;
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
int opt = w.wgetopt_long(argc, argv, L"nh", long_options, &opt_index);
|
|
|
|
if (opt == -1) break;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 0: {
|
|
|
|
if (long_options[opt_index].flag != 0) break;
|
|
|
|
streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
|
|
|
|
long_options[opt_index].name);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'h': {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'n': {
|
|
|
|
list = 1;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
|
|
|
builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-10-17 17:56:03 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (list) {
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring_list_t names = builtin_get_names();
|
|
|
|
sort(names.begin(), names.end());
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
for (size_t i = 0; i < names.size(); i++) {
|
2012-11-19 08:30:30 +08:00
|
|
|
const wchar_t *el = names.at(i).c_str();
|
|
|
|
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(el);
|
|
|
|
streams.out.append(L"\n");
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-10-17 17:56:03 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Implementation of the builtin emit command, used to create events.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_emit(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2015-07-26 09:16:00 +08:00
|
|
|
wgetopter_t w;
|
2016-04-19 10:25:12 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
static const struct woption long_options[] = {{L"help", no_argument, 0, 'h'}, {0, 0, 0, 0}};
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
while (1) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int opt_index = 0;
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
int opt = w.wgetopt_long(argc, argv, L"h", long_options, &opt_index);
|
|
|
|
if (opt == -1) break;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 0: {
|
|
|
|
if (long_options[opt_index].flag != 0) break;
|
|
|
|
streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
|
|
|
|
long_options[opt_index].name);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'h': {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
|
|
|
builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!argv[w.woptind]) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append_format(L"%ls: expected event name\n", argv[0]);
|
2012-12-23 04:21:31 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2015-07-26 09:16:00 +08:00
|
|
|
const wchar_t *eventname = argv[w.woptind];
|
|
|
|
wcstring_list_t args(argv + w.woptind + 1, argv + argc);
|
2012-12-20 08:11:55 +08:00
|
|
|
event_fire_generic(eventname, &args);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
return STATUS_BUILTIN_OK;
|
2012-10-17 17:56:03 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Implementation of the builtin 'command'. Actual command running is handled by the parser, this
|
|
|
|
/// just processes the flags.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_command(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2015-07-26 09:16:00 +08:00
|
|
|
wgetopter_t w;
|
2016-04-19 10:25:12 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
2016-11-28 23:26:01 +08:00
|
|
|
bool find_path = false;
|
|
|
|
bool quiet = false;
|
2014-07-10 09:21:06 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
w.woptind = 0;
|
2014-07-10 09:21:06 +08:00
|
|
|
|
2016-12-04 12:12:53 +08:00
|
|
|
static const struct woption long_options[] = {{L"quiet", no_argument, NULL, 'q'},
|
|
|
|
{L"search", no_argument, NULL, 's'},
|
|
|
|
{L"help", no_argument, NULL, 'h'},
|
|
|
|
{NULL, 0, NULL, 0}};
|
2014-07-10 09:21:06 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
while (1) {
|
2014-07-10 09:21:06 +08:00
|
|
|
int opt_index = 0;
|
|
|
|
|
2016-11-28 23:26:01 +08:00
|
|
|
int opt = w.wgetopt_long(argc, argv, L"qsvh", long_options, &opt_index);
|
2016-04-19 10:25:12 +08:00
|
|
|
if (opt == -1) break;
|
2014-07-10 09:21:06 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 0: {
|
|
|
|
if (long_options[opt_index].flag != 0) break;
|
|
|
|
streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
|
|
|
|
long_options[opt_index].name);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2014-07-10 09:21:06 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'h': {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
2014-07-10 09:21:06 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2014-07-11 10:16:32 +08:00
|
|
|
case 's':
|
2016-04-19 10:25:12 +08:00
|
|
|
case 'v': {
|
2016-11-28 23:26:01 +08:00
|
|
|
find_path = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'q': {
|
|
|
|
quiet = true;
|
2014-07-10 09:21:06 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
|
|
|
builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2014-07-10 09:21:06 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2014-07-10 09:21:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 23:26:01 +08:00
|
|
|
if (!find_path) {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
2014-07-10 09:21:06 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
int found = 0;
|
2014-07-10 09:21:06 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
for (int idx = w.woptind; argv[idx]; ++idx) {
|
2014-07-10 09:21:06 +08:00
|
|
|
const wchar_t *command_name = argv[idx];
|
|
|
|
wcstring path;
|
2016-04-19 10:25:12 +08:00
|
|
|
if (path_get_path(command_name, &path)) {
|
2016-11-28 23:26:01 +08:00
|
|
|
if (!quiet) streams.out.append_format(L"%ls\n", path.c_str());
|
2014-07-10 09:21:06 +08:00
|
|
|
++found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found ? STATUS_BUILTIN_OK : STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-07-26 09:16:00 +08:00
|
|
|
wgetopter_t w;
|
2016-04-19 10:25:12 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
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) {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
2014-01-13 07:10:59 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
static const struct woption long_options[] = {{L"help", no_argument, 0, 'h'}, {0, 0, 0, 0}};
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
while (1) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int opt_index = 0;
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
int opt = w.wgetopt_long(argc, argv, L"h", long_options, &opt_index);
|
|
|
|
if (opt == -1) break;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 0: {
|
|
|
|
if (long_options[opt_index].flag != 0) break;
|
|
|
|
streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
|
|
|
|
long_options[opt_index].name);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'h': {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
|
|
|
builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2012-05-20 07:59:56 +08:00
|
|
|
}
|
2012-03-08 03:35:22 +08:00
|
|
|
}
|
2016-05-09 06:57:56 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-10-17 17:56:03 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Return a definition of the specified function. Used by the functions builtin.
|
2016-04-19 10:25:12 +08:00
|
|
|
static wcstring functions_def(const wcstring &name) {
|
2016-11-04 07:37:26 +08:00
|
|
|
CHECK(!name.empty(), L""); //!OCLINT(multiple unary operator)
|
2015-09-22 02:24:49 +08:00
|
|
|
wcstring out;
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring desc, def;
|
|
|
|
function_get_desc(name, &desc);
|
|
|
|
function_get_definition(name, &def);
|
|
|
|
event_t search(EVENT_ANY);
|
|
|
|
search.function_name = name;
|
2017-01-22 08:48:07 +08:00
|
|
|
std::vector<std::shared_ptr<event_t>> ev;
|
2012-12-20 18:48:36 +08:00
|
|
|
event_get(search, &ev);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
out.append(L"function ");
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Typically we prefer to specify the function name first, e.g. "function foo --description bar"
|
|
|
|
// But If the function name starts with a -, we'll need to output it after all the options.
|
2012-11-19 08:30:30 +08:00
|
|
|
bool defer_function_name = (name.at(0) == L'-');
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!defer_function_name) {
|
2013-10-09 22:39:24 +08:00
|
|
|
out.append(escape_string(name, true));
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!desc.empty()) {
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring esc_desc = escape_string(desc, true);
|
|
|
|
out.append(L" --description ");
|
|
|
|
out.append(esc_desc);
|
|
|
|
}
|
|
|
|
|
2016-05-09 06:57:56 +08:00
|
|
|
if (!function_get_shadow_scope(name)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
out.append(L" --no-scope-shadowing");
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
for (size_t i = 0; i < ev.size(); i++) {
|
2017-01-22 08:48:07 +08:00
|
|
|
const event_t *next = ev.at(i).get();
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (next->type) {
|
|
|
|
case EVENT_SIGNAL: {
|
2012-11-19 16:31:03 +08:00
|
|
|
append_format(out, L" --on-signal %ls", sig2wcs(next->param1.signal));
|
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case EVENT_VARIABLE: {
|
2012-11-19 16:31:03 +08:00
|
|
|
append_format(out, L" --on-variable %ls", next->str_param1.c_str());
|
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case EVENT_EXIT: {
|
2012-11-19 16:31:03 +08:00
|
|
|
if (next->param1.pid > 0)
|
|
|
|
append_format(out, L" --on-process-exit %d", next->param1.pid);
|
2016-10-02 08:21:40 +08:00
|
|
|
else
|
2012-11-19 16:31:03 +08:00
|
|
|
append_format(out, L" --on-job-exit %d", -next->param1.pid);
|
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case EVENT_JOB_ID: {
|
2012-11-19 16:31:03 +08:00
|
|
|
const job_t *j = job_get(next->param1.job_id);
|
2016-04-19 10:25:12 +08:00
|
|
|
if (j) append_format(out, L" --on-job-exit %d", j->pgid);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case EVENT_GENERIC: {
|
2012-11-19 16:31:03 +08:00
|
|
|
append_format(out, L" --on-event %ls", next->str_param1.c_str());
|
|
|
|
break;
|
|
|
|
}
|
2016-11-02 13:36:30 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected next->type");
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wcstring_list_t named = function_get_named_arguments(name);
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!named.empty()) {
|
2012-11-19 08:30:30 +08:00
|
|
|
append_format(out, L" --argument");
|
2016-04-19 10:25:12 +08:00
|
|
|
for (size_t i = 0; i < named.size(); i++) {
|
2012-11-19 08:30:30 +08:00
|
|
|
append_format(out, L" %ls", named.at(i).c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Output the function name if we deferred it.
|
|
|
|
if (defer_function_name) {
|
2012-11-19 08:30:30 +08:00
|
|
|
out.append(L" -- ");
|
2013-10-09 22:39:24 +08:00
|
|
|
out.append(escape_string(name, true));
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Output any inherited variables as `set -l` lines.
|
|
|
|
std::map<wcstring, env_var_t> inherit_vars = function_get_inherit_vars(name);
|
|
|
|
for (std::map<wcstring, env_var_t>::const_iterator it = inherit_vars.begin(),
|
|
|
|
end = inherit_vars.end();
|
|
|
|
it != end; ++it) {
|
2014-10-03 06:59:24 +08:00
|
|
|
wcstring_list_t lst;
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!it->second.missing()) {
|
2014-10-03 06:59:24 +08:00
|
|
|
tokenize_variable_array(it->second, lst);
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// This forced tab is crummy, but we don't know what indentation style the function uses.
|
2014-10-03 06:59:24 +08:00
|
|
|
append_format(out, L"\n\tset -l %ls", it->first.c_str());
|
2016-04-19 10:25:12 +08:00
|
|
|
for (wcstring_list_t::const_iterator arg_it = lst.begin(), arg_end = lst.end();
|
|
|
|
arg_it != arg_end; ++arg_it) {
|
2014-10-03 06:59:24 +08:00
|
|
|
wcstring earg = escape_string(*arg_it, ESCAPE_ALL);
|
|
|
|
out.push_back(L' ');
|
|
|
|
out.append(earg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// This forced tab is sort of crummy - not all functions start with a tab.
|
2012-11-19 08:30:30 +08:00
|
|
|
append_format(out, L"\n\t%ls", def.c_str());
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Append a newline before the 'end', unless there already is one there.
|
|
|
|
if (!string_suffixes_string(L"\n", def)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
out.push_back(L'\n');
|
|
|
|
}
|
|
|
|
out.append(L"end\n");
|
2015-09-22 02:24:49 +08:00
|
|
|
return out;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-10 14:49:33 +08:00
|
|
|
static int report_function_metadata(const wchar_t *funcname, bool verbose, io_streams_t &streams,
|
2017-01-27 12:00:43 +08:00
|
|
|
bool metadata_as_comments) {
|
2017-01-10 14:49:33 +08:00
|
|
|
const wchar_t *path = L"n/a";
|
|
|
|
const wchar_t *autoloaded = L"n/a";
|
|
|
|
const wchar_t *shadows_scope = L"n/a";
|
2017-03-14 08:22:33 +08:00
|
|
|
wcstring description = L"n/a";
|
2017-01-10 14:49:33 +08:00
|
|
|
int line_number = 0;
|
|
|
|
|
|
|
|
if (function_exists(funcname)) {
|
|
|
|
path = function_get_definition_file(funcname);
|
|
|
|
if (path) {
|
|
|
|
autoloaded = function_is_autoloaded(funcname) ? L"autoloaded" : L"not-autoloaded";
|
|
|
|
line_number = function_get_definition_offset(funcname);
|
|
|
|
} else {
|
|
|
|
path = L"stdin";
|
|
|
|
}
|
|
|
|
shadows_scope =
|
|
|
|
function_get_shadow_scope(funcname) ? L"scope-shadowing" : L"no-scope-shadowing";
|
2017-03-14 08:22:33 +08:00
|
|
|
function_get_desc(funcname, &description);
|
|
|
|
description = escape_string(description, ESCAPE_NO_QUOTED);
|
2017-01-10 14:49:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (metadata_as_comments) {
|
2017-01-22 03:51:06 +08:00
|
|
|
if (wcscmp(path, L"stdin")) {
|
2017-01-10 14:49:33 +08:00
|
|
|
streams.out.append_format(L"# Defined in %ls @ line %d\n", path, line_number);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
streams.out.append_format(L"%ls\n", path);
|
|
|
|
if (verbose) {
|
|
|
|
streams.out.append_format(L"%ls\n", autoloaded);
|
|
|
|
streams.out.append_format(L"%d\n", line_number);
|
|
|
|
streams.out.append_format(L"%ls\n", shadows_scope);
|
2017-03-14 08:22:33 +08:00
|
|
|
streams.out.append_format(L"%ls\n", description.c_str());
|
2017-01-10 14:49:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_BUILTIN_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// The functions builtin, used for listing and erasing functions.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2015-07-26 09:16:00 +08:00
|
|
|
wgetopter_t w;
|
2012-11-19 08:30:30 +08:00
|
|
|
int i;
|
2016-04-19 10:25:12 +08:00
|
|
|
int erase = 0;
|
|
|
|
wchar_t *desc = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
|
|
|
int list = 0;
|
|
|
|
int show_hidden = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
int res = STATUS_BUILTIN_OK;
|
|
|
|
int query = 0;
|
|
|
|
int copy = 0;
|
2017-01-10 14:49:33 +08:00
|
|
|
bool report_metadata = false;
|
|
|
|
bool verbose = false;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
static const struct woption long_options[] = {
|
2017-01-10 14:49:33 +08:00
|
|
|
{L"erase", no_argument, NULL, 'e'}, {L"description", required_argument, NULL, 'd'},
|
|
|
|
{L"names", no_argument, NULL, 'n'}, {L"all", no_argument, NULL, 'a'},
|
|
|
|
{L"help", no_argument, NULL, 'h'}, {L"query", no_argument, NULL, 'q'},
|
|
|
|
{L"copy", no_argument, NULL, 'c'}, {L"metadata", no_argument, NULL, 'm'},
|
|
|
|
{L"verbose", no_argument, NULL, 'v'}, {NULL, 0, NULL, 0}};
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
while (1) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int opt_index = 0;
|
|
|
|
|
2017-01-10 14:49:33 +08:00
|
|
|
int opt = w.wgetopt_long(argc, argv, L"ed:mnahqcv", long_options, &opt_index);
|
2016-04-19 10:25:12 +08:00
|
|
|
if (opt == -1) break;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 0: {
|
|
|
|
if (long_options[opt_index].flag != 0) break;
|
|
|
|
streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
|
|
|
|
long_options[opt_index].name);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2017-01-10 14:49:33 +08:00
|
|
|
case 'v': {
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case 'e': {
|
|
|
|
erase = 1;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2017-01-10 14:49:33 +08:00
|
|
|
case 'm': {
|
|
|
|
report_metadata = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case 'd': {
|
|
|
|
desc = w.woptarg;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'n': {
|
|
|
|
list = 1;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'a': {
|
|
|
|
show_hidden = 1;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'h': {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'q': {
|
2012-11-19 16:31:03 +08:00
|
|
|
query = 1;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'c': {
|
2012-11-19 16:31:03 +08:00
|
|
|
copy = 1;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
|
|
|
builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Erase, desc, query, copy and list are mutually exclusive.
|
2016-11-04 07:37:26 +08:00
|
|
|
int describe = desc ? 1 : 0;
|
|
|
|
if (erase + describe + list + query + copy > 1) {
|
2016-04-19 10:25:12 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Invalid combination of options\n"), argv[0]);
|
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);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (erase) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int i;
|
2016-04-19 10:25:12 +08:00
|
|
|
for (i = w.woptind; i < argc; i++) function_remove(argv[i]);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
2016-04-19 10:25:12 +08:00
|
|
|
} else if (desc) {
|
2012-11-19 08:30:30 +08:00
|
|
|
wchar_t *func;
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (argc - w.woptind != 1) {
|
|
|
|
streams.err.append_format(_(L"%ls: Expected exactly one function name\n"), argv[0]);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2015-07-26 09:16:00 +08:00
|
|
|
func = argv[w.woptind];
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!function_exists(func)) {
|
|
|
|
streams.err.append_format(_(L"%ls: Function '%ls' does not exist\n"), argv[0], func);
|
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);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
function_set_desc(func, desc);
|
|
|
|
return STATUS_BUILTIN_OK;
|
2017-01-10 14:49:33 +08:00
|
|
|
} else if (report_metadata) {
|
|
|
|
if (argc - w.woptind != 1) {
|
|
|
|
streams.err.append_format(
|
2017-01-27 12:00:43 +08:00
|
|
|
_(L"%ls: Expected exactly one function name for --metadata\n"), argv[0]);
|
2017-01-10 14:49:33 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t *funcname = argv[w.woptind];
|
|
|
|
return report_function_metadata(funcname, verbose, streams, false);
|
2016-04-19 10:25:12 +08:00
|
|
|
} else if (list || (argc == w.woptind)) {
|
2015-09-22 02:24:49 +08:00
|
|
|
int is_screen = !streams.out_is_redirected && isatty(STDOUT_FILENO);
|
2012-11-19 08:30:30 +08:00
|
|
|
size_t i;
|
|
|
|
wcstring_list_t names = function_get_names(show_hidden);
|
|
|
|
std::sort(names.begin(), names.end());
|
2016-04-19 10:25:12 +08:00
|
|
|
if (is_screen) {
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring buff;
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
for (i = 0; i < names.size(); i++) {
|
2012-11-19 08:30:30 +08:00
|
|
|
buff.append(names.at(i));
|
|
|
|
buff.append(L", ");
|
|
|
|
}
|
|
|
|
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(reformat_for_screen(buff));
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < names.size(); i++) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(names.at(i).c_str());
|
|
|
|
streams.out.append(L"\n");
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_BUILTIN_OK;
|
2016-04-19 10:25:12 +08:00
|
|
|
} else if (copy) {
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring current_func;
|
|
|
|
wcstring new_func;
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (argc - w.woptind != 2) {
|
|
|
|
streams.err.append_format(_(L"%ls: Expected exactly two names (current function name, "
|
|
|
|
L"and new function name)\n"),
|
|
|
|
argv[0]);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2015-07-26 09:16:00 +08:00
|
|
|
current_func = argv[w.woptind];
|
2016-04-19 10:25:12 +08:00
|
|
|
new_func = argv[w.woptind + 1];
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!function_exists(current_func)) {
|
|
|
|
streams.err.append_format(_(L"%ls: Function '%ls' does not exist\n"), argv[0],
|
|
|
|
current_func.c_str());
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-11-29 10:04:37 +08:00
|
|
|
if (!wcsfuncname(new_func) || parser_keywords_is_reserved(new_func)) {
|
2016-04-19 10:25:12 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Illegal function name '%ls'\n"), argv[0],
|
|
|
|
new_func.c_str());
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Keep things simple: don't allow existing names to be copy targets.
|
|
|
|
if (function_exists(new_func)) {
|
|
|
|
streams.err.append_format(
|
|
|
|
_(L"%ls: Function '%ls' already exists. Cannot create copy '%ls'\n"), argv[0],
|
|
|
|
new_func.c_str(), current_func.c_str());
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (function_copy(current_func, new_func)) return STATUS_BUILTIN_OK;
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
for (i = w.woptind; i < argc; i++) {
|
2012-11-19 08:30:30 +08:00
|
|
|
if (!function_exists(argv[i]))
|
|
|
|
res++;
|
2016-04-19 10:25:12 +08:00
|
|
|
else {
|
|
|
|
if (!query) {
|
|
|
|
if (i != w.woptind) streams.out.append(L"\n");
|
2017-01-10 14:49:33 +08:00
|
|
|
const wchar_t *funcname = argv[w.woptind];
|
|
|
|
report_function_metadata(funcname, verbose, streams, true);
|
|
|
|
streams.out.append(functions_def(funcname));
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-10-31 10:17:08 +08:00
|
|
|
// Convert a octal or hex character to its binary value. Surprisingly a version
|
|
|
|
// of this function using a lookup table is only ~1.5% faster than the `switch`
|
|
|
|
// statement version below. Since that requires initializing a table statically
|
|
|
|
// (which is problematic if we run on an EBCDIC system) we don't use that
|
|
|
|
// solution. Also, we relax the style rule that `case` blocks should always be
|
|
|
|
// enclosed in parentheses given the nature of this code.
|
2016-04-19 10:25:12 +08:00
|
|
|
static unsigned int builtin_echo_digit(wchar_t wc, unsigned int base) {
|
|
|
|
assert(base == 8 || base == 16); // base must be hex or octal
|
|
|
|
switch (wc) {
|
2016-10-31 10:17:08 +08:00
|
|
|
case L'0':
|
2012-11-19 16:31:03 +08:00
|
|
|
return 0;
|
2016-10-31 10:17:08 +08:00
|
|
|
case L'1':
|
2012-11-19 16:31:03 +08:00
|
|
|
return 1;
|
2016-10-31 10:17:08 +08:00
|
|
|
case L'2':
|
2012-11-19 16:31:03 +08:00
|
|
|
return 2;
|
2016-10-31 10:17:08 +08:00
|
|
|
case L'3':
|
2012-11-19 16:31:03 +08:00
|
|
|
return 3;
|
2016-10-31 10:17:08 +08:00
|
|
|
case L'4':
|
2012-11-19 16:31:03 +08:00
|
|
|
return 4;
|
2016-10-31 10:17:08 +08:00
|
|
|
case L'5':
|
2012-11-19 16:31:03 +08:00
|
|
|
return 5;
|
2016-10-31 10:17:08 +08:00
|
|
|
case L'6':
|
2012-11-19 16:31:03 +08:00
|
|
|
return 6;
|
2016-10-31 10:17:08 +08:00
|
|
|
case L'7':
|
2012-11-19 16:31:03 +08:00
|
|
|
return 7;
|
2016-10-30 08:25:48 +08:00
|
|
|
default: { break; }
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-10-31 10:17:08 +08:00
|
|
|
if (base != 16) return UINT_MAX;
|
|
|
|
|
|
|
|
switch (wc) {
|
|
|
|
case L'8':
|
|
|
|
return 8;
|
|
|
|
case L'9':
|
|
|
|
return 9;
|
|
|
|
case L'a':
|
|
|
|
case L'A':
|
|
|
|
return 10;
|
|
|
|
case L'b':
|
|
|
|
case L'B':
|
|
|
|
return 11;
|
|
|
|
case L'c':
|
|
|
|
case L'C':
|
|
|
|
return 12;
|
|
|
|
case L'd':
|
|
|
|
case L'D':
|
|
|
|
return 13;
|
|
|
|
case L'e':
|
|
|
|
case L'E':
|
|
|
|
return 14;
|
|
|
|
case L'f':
|
|
|
|
case L'F':
|
|
|
|
return 15;
|
|
|
|
default: { break; }
|
2016-10-30 08:25:48 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return UINT_MAX;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Parse a numeric escape sequence in str, returning whether we succeeded. Also return the number
|
|
|
|
/// of characters consumed and the resulting value. Supported escape sequences:
|
|
|
|
///
|
|
|
|
/// \0nnn: octal value, zero to three digits
|
|
|
|
/// \nnn: octal value, one to three digits
|
|
|
|
/// \xhh: hex value, one to two digits
|
2016-04-19 10:25:12 +08:00
|
|
|
static bool builtin_echo_parse_numeric_sequence(const wchar_t *str, size_t *consumed,
|
|
|
|
unsigned char *out_val) {
|
2012-11-19 08:30:30 +08:00
|
|
|
bool success = false;
|
2016-04-19 10:25:12 +08:00
|
|
|
unsigned int start = 0; // the first character of the numeric part of the sequence
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
unsigned int base = 0, max_digits = 0;
|
2016-04-19 10:25:12 +08:00
|
|
|
if (builtin_echo_digit(str[0], 8) != UINT_MAX) {
|
2012-11-19 08:30:30 +08:00
|
|
|
// Octal escape
|
|
|
|
base = 8;
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// If the first digit is a 0, we allow four digits (including that zero); otherwise, we
|
|
|
|
// allow 3.
|
2012-11-19 08:30:30 +08:00
|
|
|
max_digits = (str[0] == L'0' ? 4 : 3);
|
2016-04-19 10:25:12 +08:00
|
|
|
} else if (str[0] == L'x') {
|
2012-11-19 08:30:30 +08:00
|
|
|
// Hex escape
|
|
|
|
base = 16;
|
|
|
|
max_digits = 2;
|
|
|
|
|
|
|
|
// Skip the x
|
|
|
|
start = 1;
|
|
|
|
}
|
|
|
|
|
2016-10-31 10:17:08 +08:00
|
|
|
if (base == 0) {
|
|
|
|
return success;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-10-31 10:17:08 +08:00
|
|
|
unsigned int idx;
|
|
|
|
unsigned char val = 0; // resulting character
|
|
|
|
for (idx = start; idx < start + max_digits; idx++) {
|
|
|
|
unsigned int digit = builtin_echo_digit(str[idx], base);
|
|
|
|
if (digit == UINT_MAX) break;
|
|
|
|
val = val * base + digit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We succeeded if we consumed at least one digit.
|
|
|
|
if (idx > start) {
|
|
|
|
*consumed = idx;
|
|
|
|
*out_val = val;
|
|
|
|
success = true;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// The echo builtin.
|
|
|
|
///
|
|
|
|
/// Bash only respects -n if it's the first argument. We'll do the same. We also support a new
|
|
|
|
/// option -s to mean "no spaces"
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_echo(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2016-10-10 05:38:26 +08:00
|
|
|
UNUSED(parser);
|
2016-08-17 06:30:49 +08:00
|
|
|
// Skip first arg
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!*argv++) return STATUS_BUILTIN_ERROR;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Process options. Options must come at the beginning - the first non-option kicks us out.
|
2012-11-19 08:30:30 +08:00
|
|
|
bool print_newline = true, print_spaces = true, interpret_special_chars = false;
|
2014-05-16 15:19:07 +08:00
|
|
|
size_t option_idx = 0;
|
2016-04-19 10:25:12 +08:00
|
|
|
for (option_idx = 0; argv[option_idx] != NULL; option_idx++) {
|
2014-05-16 15:19:07 +08:00
|
|
|
const wchar_t *arg = argv[option_idx];
|
|
|
|
assert(arg != NULL);
|
|
|
|
bool arg_is_valid_option = false;
|
2016-04-19 10:25:12 +08:00
|
|
|
if (arg[0] == L'-') {
|
2014-05-16 15:19:07 +08:00
|
|
|
// We have a leading dash. Ensure that every subseqnent character is a valid option.
|
|
|
|
size_t i = 1;
|
2016-04-19 10:25:12 +08:00
|
|
|
while (arg[i] != L'\0' && wcschr(L"nesE", arg[i]) != NULL) {
|
2014-05-16 15:19:07 +08:00
|
|
|
i++;
|
2013-09-20 05:20:05 +08:00
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
// We must have at least two characters to be a valid option, and have consumed the
|
|
|
|
// whole string.
|
2014-05-16 15:19:07 +08:00
|
|
|
arg_is_valid_option = (i >= 2 && arg[i] == L'\0');
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
|
|
|
|
if (!arg_is_valid_option) {
|
|
|
|
// This argument is not an option, so there are no more options.
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
|
2014-05-16 15:19:07 +08:00
|
|
|
// Ok, we are sure the argument is an option. Parse it.
|
|
|
|
assert(arg_is_valid_option);
|
2016-04-19 10:25:12 +08:00
|
|
|
for (size_t i = 1; arg[i] != L'\0'; i++) {
|
|
|
|
switch (arg[i]) {
|
|
|
|
case L'n': {
|
2014-05-16 15:19:07 +08:00
|
|
|
print_newline = false;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'e': {
|
2014-05-16 15:19:07 +08:00
|
|
|
interpret_special_chars = true;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L's': {
|
2014-05-16 15:19:07 +08:00
|
|
|
print_spaces = false;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'E': {
|
2014-05-16 15:19:07 +08:00
|
|
|
interpret_special_chars = false;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
default: {
|
2016-11-02 12:19:34 +08:00
|
|
|
DIE("unexpected character in builtin_echo argument");
|
2016-11-02 13:36:30 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2014-05-16 15:19:07 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// The special character \c can be used to indicate no more output.
|
2012-11-19 08:30:30 +08:00
|
|
|
bool continue_output = true;
|
2016-04-19 10:25:12 +08:00
|
|
|
|
2014-05-16 15:19:07 +08:00
|
|
|
/* Skip over the options */
|
2016-04-19 10:25:12 +08:00
|
|
|
const wchar_t *const *args_to_echo = argv + option_idx;
|
|
|
|
for (size_t idx = 0; continue_output && args_to_echo[idx] != NULL; idx++) {
|
|
|
|
if (print_spaces && idx > 0) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.push_back(' ');
|
2014-05-16 15:19:07 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2014-05-16 15:19:07 +08:00
|
|
|
const wchar_t *str = args_to_echo[idx];
|
2016-04-19 10:25:12 +08:00
|
|
|
for (size_t j = 0; continue_output && str[j]; j++) {
|
|
|
|
if (!interpret_special_chars || str[j] != L'\\') {
|
|
|
|
// Not an escape.
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.push_back(str[j]);
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
|
|
|
// Most escapes consume one character in addition to the backslash; the numeric
|
|
|
|
// sequences may consume more, while an unrecognized escape sequence consumes none.
|
2012-11-19 08:30:30 +08:00
|
|
|
wchar_t wc;
|
|
|
|
size_t consumed = 1;
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (str[j + 1]) {
|
|
|
|
case L'a': {
|
2012-11-19 16:31:03 +08:00
|
|
|
wc = L'\a';
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'b': {
|
2012-11-19 16:31:03 +08:00
|
|
|
wc = L'\b';
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'e': {
|
2016-12-24 08:37:00 +08:00
|
|
|
wc = L'\e';
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'f': {
|
2012-11-19 16:31:03 +08:00
|
|
|
wc = L'\f';
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'n': {
|
2012-11-19 16:31:03 +08:00
|
|
|
wc = L'\n';
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'r': {
|
2012-11-19 16:31:03 +08:00
|
|
|
wc = L'\r';
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L't': {
|
2012-11-19 16:31:03 +08:00
|
|
|
wc = L'\t';
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'v': {
|
2012-11-19 16:31:03 +08:00
|
|
|
wc = L'\v';
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'\\': {
|
2012-11-19 16:31:03 +08:00
|
|
|
wc = L'\\';
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'c': {
|
2012-11-19 16:31:03 +08:00
|
|
|
wc = 0;
|
|
|
|
continue_output = false;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// Octal and hex escape sequences.
|
2012-11-19 16:31:03 +08:00
|
|
|
unsigned char narrow_val = 0;
|
2016-04-19 10:25:12 +08:00
|
|
|
if (builtin_echo_parse_numeric_sequence(str + j + 1, &consumed,
|
|
|
|
&narrow_val)) {
|
|
|
|
// Here consumed must have been set to something. The narrow_val is a
|
|
|
|
// literal byte that we want to output (#1894).
|
2015-01-16 03:21:07 +08:00
|
|
|
wc = ENCODE_DIRECT_BASE + narrow_val % 256;
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
|
|
|
// Not a recognized escape. We consume only the backslash.
|
2012-11-19 16:31:03 +08:00
|
|
|
wc = L'\\';
|
|
|
|
consumed = 0;
|
|
|
|
}
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Skip over characters that were part of this escape sequence (but not the
|
|
|
|
// backslash, which will be handled by the loop increment.
|
2012-11-19 08:30:30 +08:00
|
|
|
j += consumed;
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (continue_output) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.push_back(wc);
|
2014-05-16 15:19:07 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
if (print_newline && continue_output) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.push_back('\n');
|
2014-05-16 15:19:07 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// The pwd builtin. We don't respect -P to resolve symbolic links because we
|
|
|
|
/// try to always resolve them.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_pwd(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2016-10-10 05:38:26 +08:00
|
|
|
UNUSED(parser);
|
|
|
|
if (argv[1] != NULL) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_ARG_COUNT1, argv[0], 0, builtin_count_args(argv));
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-03-11 10:17:39 +08:00
|
|
|
wcstring res = wgetcwd();
|
2016-04-19 10:25:12 +08:00
|
|
|
if (res.empty()) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
streams.out.append(res);
|
|
|
|
streams.out.push_back(L'\n');
|
|
|
|
return STATUS_BUILTIN_OK;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-23 12:24:03 +08:00
|
|
|
static int validate_function_name(int argc, const wchar_t *const *argv, wcstring &function_name,
|
|
|
|
const wchar_t *cmd, wcstring *out_err) {
|
2016-11-17 14:00:33 +08:00
|
|
|
if (argc < 2) {
|
|
|
|
// This is currently impossible but let's be paranoid.
|
|
|
|
append_format(*out_err, _(L"%ls: Expected function name"), cmd);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
function_name = argv[1];
|
|
|
|
if (!wcsfuncname(function_name)) {
|
|
|
|
append_format(*out_err, _(L"%ls: Illegal function name '%ls'"), cmd, function_name.c_str());
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parser_keywords_is_reserved(function_name)) {
|
|
|
|
append_format(
|
|
|
|
*out_err,
|
|
|
|
_(L"%ls: The name '%ls' is reserved,\nand can not be used as a function name"), cmd,
|
|
|
|
function_name.c_str());
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_BUILTIN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Define a function. Calls into `function.cpp` to perform the heavy lifting of defining a
|
|
|
|
/// function.
|
2016-05-09 06:57:56 +08:00
|
|
|
int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_list_t &c_args,
|
|
|
|
const wcstring &contents, int definition_line_offset, wcstring *out_err) {
|
2013-12-27 17:38:43 +08:00
|
|
|
assert(out_err != NULL);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-11-17 14:00:33 +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.
|
2013-12-27 19:58:42 +08:00
|
|
|
wcstring_list_t args;
|
|
|
|
args.push_back(L"function");
|
|
|
|
args.insert(args.end(), c_args.begin(), c_args.end());
|
2013-12-27 17:38:43 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Hackish const_cast matches the one in builtin_run.
|
2013-12-27 17:38:43 +08:00
|
|
|
const null_terminated_array_t<wchar_t> argv_array(args);
|
|
|
|
wchar_t **argv = const_cast<wchar_t **>(argv_array.get());
|
2016-11-17 14:00:33 +08:00
|
|
|
wchar_t *cmd = argv[0];
|
2012-11-19 08:30:30 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
2016-11-17 14:00:33 +08:00
|
|
|
wchar_t *desc = NULL;
|
|
|
|
bool shadow_scope = true;
|
|
|
|
wcstring function_name;
|
2012-11-19 08:30:30 +08:00
|
|
|
std::vector<event_t> events;
|
2015-05-18 05:17:01 +08:00
|
|
|
wcstring_list_t named_arguments;
|
2014-10-03 06:59:24 +08:00
|
|
|
wcstring_list_t inherit_vars;
|
2014-08-16 09:14:36 +08:00
|
|
|
wcstring_list_t wrap_targets;
|
2016-04-19 10:25:12 +08:00
|
|
|
|
2016-11-17 14:00:33 +08:00
|
|
|
// 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.
|
|
|
|
const wchar_t *short_options = L"+:a:d:e:hj:p:s:v:w:SV:";
|
|
|
|
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}};
|
|
|
|
|
|
|
|
// A valid function name has to be the first argument.
|
|
|
|
if (validate_function_name(argc, argv, function_name, cmd, out_err) == STATUS_BUILTIN_OK) {
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
} else {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-11-17 14:00:33 +08:00
|
|
|
int opt;
|
|
|
|
wgetopter_t w;
|
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 'd': {
|
|
|
|
desc = w.woptarg;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 's': {
|
2015-07-26 09:16:00 +08:00
|
|
|
int sig = wcs2sig(w.woptarg);
|
2016-11-17 14:00:33 +08:00
|
|
|
if (sig == -1) {
|
|
|
|
append_format(*out_err, _(L"%ls: Unknown signal '%ls'"), cmd, w.woptarg);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
events.push_back(event_t::signal_event(sig));
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case 'v': {
|
|
|
|
if (wcsvarname(w.woptarg)) {
|
2016-11-17 14:00:33 +08:00
|
|
|
append_format(*out_err, _(L"%ls: Invalid variable name '%ls'"), cmd, w.woptarg);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
|
2015-07-26 09:16:00 +08:00
|
|
|
events.push_back(event_t::variable_event(w.woptarg));
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case 'e': {
|
2015-07-26 09:16:00 +08:00
|
|
|
events.push_back(event_t::generic_event(w.woptarg));
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'j':
|
2016-04-19 10:25:12 +08:00
|
|
|
case 'p': {
|
2016-10-02 08:21:40 +08:00
|
|
|
pid_t pid;
|
2012-11-19 16:31:03 +08:00
|
|
|
event_t e(EVENT_ANY);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if ((opt == 'j') && (wcscasecmp(w.woptarg, L"caller") == 0)) {
|
2017-01-27 06:47:32 +08:00
|
|
|
job_id_t job_id = -1;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (is_subshell) {
|
2013-12-21 09:41:21 +08:00
|
|
|
size_t block_idx = 0;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Find the outermost substitution block.
|
|
|
|
for (block_idx = 0;; block_idx++) {
|
2013-12-21 09:41:21 +08:00
|
|
|
const block_t *b = parser.block_at_index(block_idx);
|
2016-04-19 10:25:12 +08:00
|
|
|
if (b == NULL || b->type() == SUBST) break;
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Go one step beyond that, to get to the caller.
|
2013-12-21 09:41:21 +08:00
|
|
|
const block_t *caller_block = parser.block_at_index(block_idx + 1);
|
2016-04-19 10:25:12 +08:00
|
|
|
if (caller_block != NULL && caller_block->job != NULL) {
|
2013-12-21 09:41:21 +08:00
|
|
|
job_id = caller_block->job->job_id;
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (job_id == -1) {
|
2013-12-27 17:38:43 +08:00
|
|
|
append_format(*out_err,
|
2016-11-17 14:00:33 +08:00
|
|
|
_(L"%ls: Cannot find calling job for event handler"), cmd);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-11-23 12:24:03 +08:00
|
|
|
e.type = EVENT_JOB_ID;
|
|
|
|
e.param1.job_id = job_id;
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
2016-11-23 12:24:03 +08:00
|
|
|
pid = fish_wcstoi(w.woptarg);
|
|
|
|
if (errno || pid < 0) {
|
|
|
|
append_format(*out_err, _(L"%ls: Invalid process id '%ls'"), cmd,
|
|
|
|
w.woptarg);
|
2016-11-17 14:00:33 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
e.type = EVENT_EXIT;
|
2016-04-19 10:25:12 +08:00
|
|
|
e.param1.pid = (opt == 'j' ? -1 : 1) * abs(pid);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-11-17 14:00:33 +08:00
|
|
|
events.push_back(e);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case 'a': {
|
2016-11-17 14:00:33 +08:00
|
|
|
named_arguments.push_back(w.woptarg);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'S': {
|
2016-05-09 06:57:56 +08:00
|
|
|
shadow_scope = false;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'w': {
|
2015-07-26 09:16:00 +08:00
|
|
|
wrap_targets.push_back(w.woptarg);
|
2014-08-16 09:14:36 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'V': {
|
|
|
|
if (wcsvarname(w.woptarg)) {
|
2016-11-17 14:00:33 +08:00
|
|
|
append_format(*out_err, _(L"%ls: Invalid variable name '%ls'"), cmd, w.woptarg);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
2014-10-03 06:59:24 +08:00
|
|
|
}
|
|
|
|
|
2015-07-26 09:16:00 +08:00
|
|
|
inherit_vars.push_back(w.woptarg);
|
2014-10-03 06:59:24 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case 'h': {
|
2016-11-17 14:00:33 +08:00
|
|
|
builtin_print_help(parser, streams, cmd, streams.out);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-11-17 14:00:33 +08:00
|
|
|
case ':': {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_MISSING, cmd, argv[w.woptind - 1]);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
2016-11-17 14:00:33 +08:00
|
|
|
builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
2016-11-17 14:00:33 +08:00
|
|
|
DIE("unexpected retval from wgetopt_long");
|
2016-10-30 08:25:48 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 14:00:33 +08:00
|
|
|
if (argc != w.woptind) {
|
|
|
|
if (named_arguments.size()) {
|
|
|
|
for (int i = w.woptind; i < argc; i++) {
|
|
|
|
named_arguments.push_back(argv[i]);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-11-23 12:24:03 +08:00
|
|
|
} else {
|
2016-11-17 14:00:33 +08:00
|
|
|
append_format(*out_err, _(L"%ls: Unexpected positional argument '%ls'"), cmd,
|
2016-11-23 12:24:03 +08:00
|
|
|
argv[w.woptind]);
|
2016-11-17 14:00:33 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-10-31 10:17:08 +08:00
|
|
|
}
|
2015-05-18 05:20:18 +08:00
|
|
|
|
2016-11-17 14:00:33 +08:00
|
|
|
// We have what we need to actually define the function.
|
2016-10-31 10:17:08 +08:00
|
|
|
function_data_t d;
|
|
|
|
d.name = function_name;
|
|
|
|
if (desc) d.description = desc;
|
|
|
|
d.events.swap(events);
|
|
|
|
d.shadow_scope = shadow_scope;
|
|
|
|
d.named_arguments.swap(named_arguments);
|
|
|
|
d.inherit_vars.swap(inherit_vars);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-10-31 10:17:08 +08:00
|
|
|
for (size_t i = 0; i < d.events.size(); i++) {
|
|
|
|
event_t &e = d.events.at(i);
|
|
|
|
e.function_name = d.name;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-10-31 10:17:08 +08:00
|
|
|
d.definition = contents.c_str();
|
|
|
|
function_add(d, parser, definition_line_offset);
|
2014-10-03 06:59:24 +08:00
|
|
|
|
2016-11-17 14:00:33 +08:00
|
|
|
// Handle wrap targets by creating the appropriate completions.
|
2016-10-31 10:17:08 +08:00
|
|
|
for (size_t w = 0; w < wrap_targets.size(); w++) {
|
|
|
|
complete_add_wrapper(function_name, wrap_targets.at(w));
|
2013-12-27 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2016-11-17 14:00:33 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-07-11 09:22:49 +08:00
|
|
|
/// The random builtin generates random numbers.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_random(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2016-11-29 04:57:58 +08:00
|
|
|
static bool seeded = false;
|
|
|
|
static std::minstd_rand engine;
|
|
|
|
if (!seeded) {
|
|
|
|
// seed engine with 2*32 bits of random data
|
|
|
|
// for the 64 bits of internal state of minstd_rand
|
|
|
|
std::random_device rd;
|
|
|
|
std::seed_seq seed{rd(), rd()};
|
|
|
|
engine.seed(seed);
|
|
|
|
seeded = true;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2015-07-26 09:16:00 +08:00
|
|
|
wgetopter_t w;
|
2016-11-29 04:57:58 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
|
|
|
static const struct woption long_options[] = {{L"help", no_argument, NULL, 'h'},
|
|
|
|
{NULL, 0, NULL, 0}};
|
2016-04-19 10:25:12 +08:00
|
|
|
while (1) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int opt_index = 0;
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
int opt = w.wgetopt_long(argc, argv, L"h", long_options, &opt_index);
|
|
|
|
if (opt == -1) break;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 0: {
|
|
|
|
if (long_options[opt_index].flag != 0) break;
|
|
|
|
streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
|
|
|
|
long_options[opt_index].name);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'h': {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
2016-11-29 04:57:58 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
|
|
|
builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-23 11:32:25 +08:00
|
|
|
int arg_count = argc - w.woptind;
|
2016-11-29 04:57:58 +08:00
|
|
|
long long start, end;
|
|
|
|
unsigned long long step;
|
|
|
|
bool choice = false;
|
|
|
|
if (arg_count >= 1 && !wcscmp(argv[w.woptind], L"choice")) {
|
|
|
|
if (arg_count == 1) {
|
|
|
|
streams.err.append_format(L"%ls: nothing to choose from\n", argv[0]);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2016-11-29 04:57:58 +08:00
|
|
|
choice = true;
|
|
|
|
start = 1;
|
|
|
|
step = 1;
|
|
|
|
end = arg_count - 1;
|
|
|
|
} else {
|
|
|
|
bool parse_error = false;
|
|
|
|
auto parse_ll = [&](const wchar_t *str) {
|
|
|
|
long long ll = fish_wcstoll(str);
|
|
|
|
if (errno) {
|
|
|
|
streams.err.append_format(L"%ls: %ls is not a valid integer\n", argv[0], str);
|
|
|
|
parse_error = true;
|
|
|
|
}
|
|
|
|
return ll;
|
|
|
|
};
|
|
|
|
auto parse_ull = [&](const wchar_t *str) {
|
|
|
|
unsigned long long ull = fish_wcstoull(str);
|
|
|
|
if (errno) {
|
|
|
|
streams.err.append_format(L"%ls: %ls is not a valid integer\n", argv[0], str);
|
|
|
|
parse_error = true;
|
|
|
|
}
|
|
|
|
return ull;
|
|
|
|
};
|
|
|
|
if (arg_count == 0) {
|
|
|
|
start = 0;
|
|
|
|
end = 32767;
|
|
|
|
step = 1;
|
|
|
|
} else if (arg_count == 1) {
|
|
|
|
long long seed = parse_ll(argv[w.woptind]);
|
|
|
|
if (parse_error) return STATUS_BUILTIN_ERROR;
|
|
|
|
engine.seed(static_cast<uint32_t>(seed));
|
|
|
|
return STATUS_BUILTIN_OK;
|
|
|
|
} else if (arg_count == 2) {
|
|
|
|
start = parse_ll(argv[w.woptind]);
|
|
|
|
step = 1;
|
|
|
|
end = parse_ll(argv[w.woptind + 1]);
|
|
|
|
} else if (arg_count == 3) {
|
|
|
|
start = parse_ll(argv[w.woptind]);
|
|
|
|
step = parse_ull(argv[w.woptind + 1]);
|
|
|
|
end = parse_ll(argv[w.woptind + 2]);
|
|
|
|
} else {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2016-11-29 04:57:58 +08:00
|
|
|
|
|
|
|
if (parse_error) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
} else if (start >= end) {
|
|
|
|
streams.err.append_format(L"%ls: END must be greater than START\n", argv[0]);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
} else if (step == 0) {
|
|
|
|
streams.err.append_format(L"%ls: STEP must be a positive integer\n", argv[0]);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// only for negative argument
|
|
|
|
auto safe_abs = [](long long ll) -> unsigned long long {
|
|
|
|
return -static_cast<unsigned long long>(ll);
|
|
|
|
};
|
|
|
|
long long real_end;
|
|
|
|
if (start >= 0 || end < 0) {
|
|
|
|
// 0 <= start <= end
|
|
|
|
long long diff = end - start;
|
|
|
|
// 0 <= diff <= LL_MAX
|
|
|
|
real_end = start + static_cast<long long>(diff / step);
|
2016-10-23 11:32:25 +08:00
|
|
|
} else {
|
2016-11-29 04:57:58 +08:00
|
|
|
// start < 0 <= end
|
|
|
|
unsigned long long abs_start = safe_abs(start);
|
|
|
|
unsigned long long diff = (end + abs_start);
|
|
|
|
real_end = diff / step - abs_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!choice && start == real_end) {
|
|
|
|
streams.err.append_format(L"%ls: range contains only one possible value\n", argv[0]);
|
2016-10-23 11:32:25 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-11-29 04:57:58 +08:00
|
|
|
|
|
|
|
std::uniform_int_distribution<long long> dist(start, real_end);
|
|
|
|
long long random = dist(engine);
|
|
|
|
long long result;
|
|
|
|
if (start >= 0) {
|
|
|
|
// 0 <= start <= random <= end
|
|
|
|
long long diff = random - start;
|
|
|
|
// 0 < step * diff <= end - start <= LL_MAX
|
|
|
|
result = start + static_cast<long long>(diff * step);
|
|
|
|
} else if (random < 0) {
|
|
|
|
// start <= random < 0
|
|
|
|
long long diff = random - start;
|
|
|
|
result = diff * step - safe_abs(start);
|
|
|
|
} else {
|
|
|
|
// start < 0 <= random
|
|
|
|
unsigned long long abs_start = safe_abs(start);
|
|
|
|
unsigned long long diff = (random + abs_start);
|
|
|
|
result = diff * step - abs_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (choice) {
|
|
|
|
streams.out.append_format(L"%ls\n", argv[w.woptind + result]);
|
|
|
|
} else {
|
|
|
|
streams.out.append_format(L"%lld\n", result);
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
|
|
|
}
|
|
|
|
|
2017-01-02 08:15:55 +08:00
|
|
|
/// Read from the tty. This is only valid when the stream is stdin and it is attached to a tty and
|
|
|
|
/// we weren't asked to split on null characters.
|
2017-04-10 02:48:21 +08:00
|
|
|
static int read_interactive(wcstring &buff, int nchars, bool shell, bool silent,
|
|
|
|
const wchar_t *mode_name, const wchar_t *prompt,
|
|
|
|
const wchar_t *right_prompt, const wchar_t *commandline) {
|
2017-01-02 08:15:55 +08:00
|
|
|
int exit_res = STATUS_BUILTIN_OK;
|
|
|
|
const wchar_t *line;
|
|
|
|
|
|
|
|
reader_push(mode_name);
|
|
|
|
reader_set_left_prompt(prompt);
|
|
|
|
reader_set_right_prompt(right_prompt);
|
|
|
|
if (shell) {
|
|
|
|
reader_set_complete_function(&complete);
|
|
|
|
reader_set_highlight_function(&highlight_shell);
|
|
|
|
reader_set_test_function(&reader_shell_test);
|
|
|
|
}
|
|
|
|
// No autosuggestions or abbreviations in builtin_read.
|
|
|
|
reader_set_allow_autosuggesting(false);
|
|
|
|
reader_set_expand_abbreviations(false);
|
|
|
|
reader_set_exit_on_interrupt(true);
|
2017-04-10 02:48:21 +08:00
|
|
|
reader_set_silent_status(silent);
|
2017-01-02 08:15:55 +08:00
|
|
|
|
|
|
|
reader_set_buffer(commandline, wcslen(commandline));
|
|
|
|
proc_push_interactive(1);
|
|
|
|
|
|
|
|
event_fire_generic(L"fish_prompt");
|
|
|
|
line = reader_readline(nchars);
|
|
|
|
proc_pop_interactive();
|
|
|
|
if (line) {
|
|
|
|
if (0 < nchars && (size_t)nchars < wcslen(line)) {
|
|
|
|
// Line may be longer than nchars if a keybinding used `commandline -i`
|
|
|
|
// note: we're deliberately throwing away the tail of the commandline.
|
|
|
|
// It shouldn't be unread because it was produced with `commandline -i`,
|
|
|
|
// not typed.
|
|
|
|
buff = wcstring(line, nchars);
|
|
|
|
} else {
|
|
|
|
buff = wcstring(line);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
exit_res = STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
reader_pop();
|
|
|
|
return exit_res;
|
|
|
|
}
|
|
|
|
|
2017-01-02 10:07:42 +08:00
|
|
|
/// Bash uses 128 bytes for its chunk size. Very informal testing I did suggested that a smaller
|
|
|
|
/// chunk size performed better. However, we're going to use the bash value under the assumption
|
|
|
|
/// they've done more extensive testing.
|
|
|
|
#define READ_CHUNK_SIZE 128
|
|
|
|
|
2017-02-08 09:21:35 +08:00
|
|
|
/// Read from the fd in chunks until we see newline or null, as requested, is seen. This is only
|
2017-01-02 10:07:42 +08:00
|
|
|
/// used when the fd is seekable (so not from a tty or pipe) and we're not reading a specific number
|
|
|
|
/// of chars.
|
2017-02-08 09:21:35 +08:00
|
|
|
///
|
|
|
|
/// Returns an exit status.
|
2017-01-02 10:07:42 +08:00
|
|
|
static int read_in_chunks(int fd, wcstring &buff, bool split_null) {
|
2017-01-02 08:15:55 +08:00
|
|
|
int exit_res = STATUS_BUILTIN_OK;
|
2017-01-02 10:07:42 +08:00
|
|
|
std::string str;
|
2017-01-02 08:15:55 +08:00
|
|
|
bool eof = false;
|
2017-01-02 10:07:42 +08:00
|
|
|
bool finished = false;
|
2017-01-02 08:15:55 +08:00
|
|
|
|
2017-01-02 10:07:42 +08:00
|
|
|
while (!finished) {
|
|
|
|
char inbuf[READ_CHUNK_SIZE];
|
2017-01-22 03:53:49 +08:00
|
|
|
long bytes_read = read_blocked(fd, inbuf, READ_CHUNK_SIZE);
|
2017-01-02 08:15:55 +08:00
|
|
|
|
2017-01-02 10:07:42 +08:00
|
|
|
if (bytes_read <= 0) {
|
|
|
|
eof = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-01-02 08:15:55 +08:00
|
|
|
|
2017-01-22 04:43:20 +08:00
|
|
|
const char *end = std::find(inbuf, inbuf + bytes_read, split_null ? L'\0' : L'\n');
|
2017-02-08 09:21:35 +08:00
|
|
|
long bytes_consumed = end - inbuf; // must be signed for use in lseek
|
2017-01-22 04:43:20 +08:00
|
|
|
assert(bytes_consumed <= bytes_read);
|
|
|
|
str.append(inbuf, bytes_consumed);
|
|
|
|
if (bytes_consumed < bytes_read) {
|
2017-02-08 09:21:35 +08:00
|
|
|
// We found a splitter. The +1 because we need to treat the splitter as consumed, but
|
|
|
|
// not append it to the string.
|
2017-01-22 04:43:20 +08:00
|
|
|
CHECK(lseek(fd, bytes_consumed - bytes_read + 1, SEEK_CUR) != -1, STATUS_BUILTIN_ERROR)
|
|
|
|
finished = true;
|
2017-02-08 09:21:35 +08:00
|
|
|
} else if (str.size() > read_byte_limit) {
|
|
|
|
exit_res = STATUS_READ_TOO_MUCH;
|
|
|
|
finished = true;
|
2017-01-02 08:15:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-22 04:43:20 +08:00
|
|
|
buff = str2wcstring(str);
|
2017-01-02 08:15:55 +08:00
|
|
|
if (buff.empty() && eof) {
|
|
|
|
exit_res = STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return exit_res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read from the fd on char at a time until we've read the requested number of characters or a
|
|
|
|
/// newline or null, as appropriate, is seen. This is inefficient so should only be used when the
|
|
|
|
/// fd is not seekable.
|
|
|
|
static int read_one_char_at_a_time(int fd, wcstring &buff, int nchars, bool split_null) {
|
|
|
|
int exit_res = STATUS_BUILTIN_OK;
|
|
|
|
bool eof = false;
|
2017-02-08 09:21:35 +08:00
|
|
|
size_t nbytes = 0;
|
2017-01-02 08:15:55 +08:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
bool finished = false;
|
|
|
|
wchar_t res = 0;
|
|
|
|
mbstate_t state = {};
|
|
|
|
|
|
|
|
while (!finished) {
|
|
|
|
char b;
|
|
|
|
if (read_blocked(fd, &b, 1) <= 0) {
|
|
|
|
eof = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-02-08 09:21:35 +08:00
|
|
|
nbytes++;
|
2017-01-02 08:15:55 +08:00
|
|
|
if (MB_CUR_MAX == 1) {
|
|
|
|
res = (unsigned char)b;
|
|
|
|
finished = true;
|
|
|
|
} else {
|
|
|
|
size_t sz = mbrtowc(&res, &b, 1, &state);
|
|
|
|
if (sz == (size_t)-1) {
|
|
|
|
memset(&state, 0, sizeof(state));
|
|
|
|
} else if (sz != (size_t)-2) {
|
|
|
|
finished = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 09:21:35 +08:00
|
|
|
if (nbytes > read_byte_limit) {
|
|
|
|
exit_res = STATUS_READ_TOO_MUCH;
|
|
|
|
break;
|
|
|
|
}
|
2017-01-02 08:15:55 +08:00
|
|
|
if (eof) break;
|
|
|
|
if (!split_null && res == L'\n') break;
|
|
|
|
if (split_null && res == L'\0') break;
|
|
|
|
|
|
|
|
buff.push_back(res);
|
2017-02-08 09:21:35 +08:00
|
|
|
if (nchars > 0 && (size_t)nchars <= buff.size()) {
|
2017-01-02 08:15:55 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buff.empty() && eof) {
|
|
|
|
exit_res = STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return exit_res;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// The read builtin. Reads from stdin and stores the values in environment variables.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2014-09-22 10:18:56 +08:00
|
|
|
wcstring buff;
|
2017-03-25 23:20:55 +08:00
|
|
|
wchar_t *cmd = argv[0];
|
2017-01-02 08:15:55 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
2012-11-19 08:30:30 +08:00
|
|
|
int place = ENV_USER;
|
2017-03-25 23:20:55 +08:00
|
|
|
wcstring prompt_cmd;
|
|
|
|
const wchar_t *prompt = NULL;
|
|
|
|
const wchar_t *prompt_str = NULL;
|
2014-09-22 12:09:21 +08:00
|
|
|
const wchar_t *right_prompt = L"";
|
2012-11-19 08:30:30 +08:00
|
|
|
const wchar_t *commandline = L"";
|
2016-04-19 10:25:12 +08:00
|
|
|
int exit_res = STATUS_BUILTIN_OK;
|
2012-11-19 08:30:30 +08:00
|
|
|
const wchar_t *mode_name = READ_MODE_NAME;
|
2016-04-19 10:25:12 +08:00
|
|
|
int nchars = 0;
|
2017-01-02 08:15:55 +08:00
|
|
|
bool shell = false;
|
|
|
|
bool array = false;
|
2017-04-10 02:48:21 +08:00
|
|
|
bool silent = false;
|
2014-09-22 10:18:56 +08:00
|
|
|
bool split_null = false;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-04-10 02:48:21 +08:00
|
|
|
const wchar_t *short_options = L"ac:ghilm:n:p:suxzP:UR:";
|
2017-03-25 23:20:55 +08:00
|
|
|
const struct woption long_options[] = {{L"export", no_argument, NULL, 'x'},
|
|
|
|
{L"global", no_argument, NULL, 'g'},
|
|
|
|
{L"local", no_argument, NULL, 'l'},
|
|
|
|
{L"universal", no_argument, NULL, 'U'},
|
|
|
|
{L"unexport", no_argument, NULL, 'u'},
|
|
|
|
{L"prompt", required_argument, NULL, 'p'},
|
|
|
|
{L"prompt-str", required_argument, NULL, 'P'},
|
|
|
|
{L"right-prompt", required_argument, NULL, 'R'},
|
|
|
|
{L"command", required_argument, NULL, 'c'},
|
|
|
|
{L"mode-name", required_argument, NULL, 'm'},
|
2017-04-10 02:48:21 +08:00
|
|
|
{L"silent", no_argument, NULL, 'i'},
|
2017-03-25 23:20:55 +08:00
|
|
|
{L"nchars", required_argument, NULL, 'n'},
|
|
|
|
{L"shell", no_argument, NULL, 's'},
|
|
|
|
{L"array", no_argument, NULL, 'a'},
|
|
|
|
{L"null", no_argument, NULL, 'z'},
|
|
|
|
{L"help", no_argument, NULL, 'h'},
|
|
|
|
{NULL, 0, NULL, 0}};
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-03-25 23:20:55 +08:00
|
|
|
int opt;
|
|
|
|
wgetopter_t w;
|
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (opt) {
|
|
|
|
case L'x': {
|
2012-11-19 16:31:03 +08:00
|
|
|
place |= ENV_EXPORT;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'g': {
|
2012-11-19 16:31:03 +08:00
|
|
|
place |= ENV_GLOBAL;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'l': {
|
2012-11-19 16:31:03 +08:00
|
|
|
place |= ENV_LOCAL;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'U': {
|
2012-11-19 16:31:03 +08:00
|
|
|
place |= ENV_UNIVERSAL;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'u': {
|
2012-11-19 16:31:03 +08:00
|
|
|
place |= ENV_UNEXPORT;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'p': {
|
2015-07-26 09:16:00 +08:00
|
|
|
prompt = w.woptarg;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2017-03-25 23:20:55 +08:00
|
|
|
case L'P': {
|
|
|
|
prompt_str = w.woptarg;
|
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case L'R': {
|
2015-07-26 09:16:00 +08:00
|
|
|
right_prompt = w.woptarg;
|
2014-09-22 12:09:21 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'c': {
|
2015-07-26 09:16:00 +08:00
|
|
|
commandline = w.woptarg;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'm': {
|
2015-07-26 09:16:00 +08:00
|
|
|
mode_name = w.woptarg;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case L'n': {
|
2016-11-23 12:24:03 +08:00
|
|
|
nchars = fish_wcstoi(w.woptarg);
|
|
|
|
if (errno) {
|
2016-10-23 11:32:25 +08:00
|
|
|
if (errno == ERANGE) {
|
|
|
|
streams.err.append_format(_(L"%ls: Argument '%ls' is out of range\n"),
|
|
|
|
argv[0], w.woptarg);
|
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
2014-08-19 20:28:08 +08:00
|
|
|
}
|
2016-10-29 08:42:50 +08:00
|
|
|
|
|
|
|
streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"),
|
|
|
|
argv[0], w.woptarg);
|
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
2014-08-19 20:28:08 +08:00
|
|
|
}
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 's': {
|
2017-01-02 08:15:55 +08:00
|
|
|
shell = true;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'a': {
|
2017-01-02 08:15:55 +08:00
|
|
|
array = true;
|
2014-07-14 13:36:26 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2017-04-10 02:48:21 +08:00
|
|
|
case L'i': {
|
|
|
|
silent = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case L'z': {
|
2014-09-22 10:18:56 +08:00
|
|
|
split_null = true;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'h': {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2017-03-25 23:20:55 +08:00
|
|
|
case ':': {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_MISSING, cmd, argv[w.woptind - 1]);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case L'?': {
|
|
|
|
builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
2017-03-25 23:20:55 +08:00
|
|
|
DIE("unexpected retval from wgetopt_long");
|
2016-10-30 08:25:48 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:20:55 +08:00
|
|
|
if (prompt && prompt_str) {
|
|
|
|
streams.err.append_format(_(L"%ls: You can't specify both -p and -P\n"), argv[0]);
|
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prompt_str) {
|
|
|
|
prompt_cmd = L"echo " + escape_string(prompt_str, ESCAPE_ALL);
|
|
|
|
prompt = prompt_cmd.c_str();
|
|
|
|
} else if (!prompt) {
|
|
|
|
prompt = DEFAULT_READ_PROMPT;
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if ((place & ENV_UNEXPORT) && (place & ENV_EXPORT)) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_EXPUNEXP, argv[0]);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if ((place & ENV_LOCAL ? 1 : 0) + (place & ENV_GLOBAL ? 1 : 0) +
|
|
|
|
(place & ENV_UNIVERSAL ? 1 : 0) >
|
|
|
|
1) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_GLOCAL, argv[0]);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (array && w.woptind + 1 != argc) {
|
|
|
|
streams.err.append_format(_(L"%ls: --array option requires a single variable name.\n"),
|
|
|
|
argv[0]);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2014-07-14 13:36:26 +08:00
|
|
|
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Verify all variable names.
|
2016-11-22 10:10:15 +08:00
|
|
|
wcstring errstr;
|
2017-01-02 08:15:55 +08:00
|
|
|
for (int i = w.woptind; i < argc; i++) {
|
2016-11-22 10:10:15 +08:00
|
|
|
if (!builtin_is_valid_varname(argv[i], errstr, argv[0])) {
|
|
|
|
streams.err.append(errstr);
|
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-02 10:07:42 +08:00
|
|
|
// TODO: Determine if the original set of conditions for interactive reads should be reinstated:
|
|
|
|
// if (isatty(0) && streams.stdin_fd == STDIN_FILENO && !split_null) {
|
|
|
|
int stream_stdin_is_a_tty = isatty(streams.stdin_fd);
|
|
|
|
if (stream_stdin_is_a_tty && !split_null) {
|
2017-01-02 08:15:55 +08:00
|
|
|
// We should read interactively using reader_readline(). This does not support splitting on
|
|
|
|
// null. The call to reader_readline may change woptind, so we save and restore it.
|
|
|
|
int saved_woptind = w.woptind;
|
2017-04-10 02:48:21 +08:00
|
|
|
exit_res = read_interactive(buff, nchars, shell, silent, mode_name, prompt, right_prompt,
|
|
|
|
commandline);
|
2017-01-02 08:15:55 +08:00
|
|
|
w.woptind = saved_woptind;
|
2017-01-02 10:07:42 +08:00
|
|
|
} else if (!nchars && !stream_stdin_is_a_tty && lseek(streams.stdin_fd, 0, SEEK_CUR) != -1) {
|
|
|
|
exit_res = read_in_chunks(streams.stdin_fd, buff, split_null);
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
2017-01-02 08:15:55 +08:00
|
|
|
exit_res = read_one_char_at_a_time(streams.stdin_fd, buff, nchars, split_null);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-02 08:15:55 +08:00
|
|
|
if (w.woptind == argc || exit_res != STATUS_BUILTIN_OK) {
|
2016-10-31 10:17:08 +08:00
|
|
|
return exit_res;
|
|
|
|
}
|
|
|
|
|
|
|
|
env_var_t ifs = env_get_string(L"IFS");
|
|
|
|
if (ifs.missing_or_empty()) {
|
|
|
|
// Every character is a separate token.
|
|
|
|
size_t bufflen = buff.size();
|
|
|
|
if (array) {
|
|
|
|
if (bufflen > 0) {
|
|
|
|
wcstring chars(bufflen + (bufflen - 1), ARRAY_SEP);
|
|
|
|
wcstring::iterator out = chars.begin();
|
|
|
|
for (wcstring::const_iterator it = buff.begin(), end = buff.end(); it != end;
|
|
|
|
++it) {
|
|
|
|
*out = *it;
|
|
|
|
out += 2;
|
2014-07-14 13:36:26 +08:00
|
|
|
}
|
2017-01-02 08:15:55 +08:00
|
|
|
env_set(argv[w.woptind], chars.c_str(), place);
|
2016-10-31 10:17:08 +08:00
|
|
|
} else {
|
2017-01-02 08:15:55 +08:00
|
|
|
env_set(argv[w.woptind], NULL, place);
|
2016-10-31 10:17:08 +08:00
|
|
|
}
|
|
|
|
} else { // not array
|
|
|
|
size_t j = 0;
|
2017-01-02 08:15:55 +08:00
|
|
|
for (; w.woptind + 1 < argc; ++w.woptind) {
|
2016-10-31 10:17:08 +08:00
|
|
|
if (j < bufflen) {
|
|
|
|
wchar_t buffer[2] = {buff[j++], 0};
|
2017-01-02 08:15:55 +08:00
|
|
|
env_set(argv[w.woptind], buffer, place);
|
2016-10-31 10:17:08 +08:00
|
|
|
} else {
|
2017-01-02 08:15:55 +08:00
|
|
|
env_set(argv[w.woptind], L"", place);
|
2014-07-14 13:36:26 +08:00
|
|
|
}
|
|
|
|
}
|
2017-01-02 08:15:55 +08:00
|
|
|
if (w.woptind < argc) env_set(argv[w.woptind], &buff[j], place);
|
2016-10-31 10:17:08 +08:00
|
|
|
}
|
|
|
|
} else if (array) {
|
|
|
|
wcstring tokens;
|
|
|
|
tokens.reserve(buff.size());
|
|
|
|
bool empty = true;
|
2016-04-19 10:25:12 +08:00
|
|
|
|
2016-10-31 10:17:08 +08:00
|
|
|
for (wcstring_range loc = wcstring_tok(buff, ifs); loc.first != wcstring::npos;
|
|
|
|
loc = wcstring_tok(buff, ifs, loc)) {
|
|
|
|
if (!empty) tokens.push_back(ARRAY_SEP);
|
|
|
|
tokens.append(buff, loc.first, loc.second);
|
|
|
|
empty = false;
|
|
|
|
}
|
2017-01-02 08:15:55 +08:00
|
|
|
env_set(argv[w.woptind], empty ? NULL : tokens.c_str(), place);
|
2016-10-31 10:17:08 +08:00
|
|
|
} else { // not array
|
|
|
|
wcstring_range loc = wcstring_range(0, 0);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-01-02 08:15:55 +08:00
|
|
|
while (w.woptind < argc) {
|
|
|
|
loc = wcstring_tok(buff, (w.woptind + 1 < argc) ? ifs : wcstring(), loc);
|
|
|
|
env_set(argv[w.woptind], loc.first == wcstring::npos ? L"" : &buff.c_str()[loc.first],
|
|
|
|
place);
|
|
|
|
++w.woptind;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return exit_res;
|
|
|
|
}
|
|
|
|
|
2016-11-05 13:32:43 +08:00
|
|
|
enum status_cmd_t {
|
2016-11-09 07:30:52 +08:00
|
|
|
STATUS_IS_LOGIN = 1,
|
2016-11-05 13:32:43 +08:00
|
|
|
STATUS_IS_INTERACTIVE,
|
|
|
|
STATUS_IS_BLOCK,
|
|
|
|
STATUS_IS_COMMAND_SUB,
|
|
|
|
STATUS_IS_FULL_JOB_CTRL,
|
|
|
|
STATUS_IS_INTERACTIVE_JOB_CTRL,
|
|
|
|
STATUS_IS_NO_JOB_CTRL,
|
|
|
|
STATUS_CURRENT_FILENAME,
|
|
|
|
STATUS_CURRENT_LINE_NUMBER,
|
|
|
|
STATUS_SET_JOB_CONTROL,
|
2016-11-09 07:30:52 +08:00
|
|
|
STATUS_PRINT_STACK_TRACE,
|
|
|
|
STATUS_UNDEF
|
2016-11-05 13:32:43 +08:00
|
|
|
};
|
2016-11-12 07:26:16 +08:00
|
|
|
// Must be sorted by string, not enum or random.
|
2016-11-09 07:30:52 +08:00
|
|
|
const enum_map<status_cmd_t> status_enum_map[] = {
|
2016-11-12 07:26:16 +08:00
|
|
|
{STATUS_CURRENT_FILENAME, L"current-filename"},
|
|
|
|
{STATUS_CURRENT_LINE_NUMBER, L"current-line-number"},
|
2016-11-09 07:30:52 +08:00
|
|
|
{STATUS_IS_BLOCK, L"is-block"},
|
2016-12-21 11:47:42 +08:00
|
|
|
{STATUS_IS_COMMAND_SUB, L"is-command-substitution"},
|
2016-11-09 07:30:52 +08:00
|
|
|
{STATUS_IS_FULL_JOB_CTRL, L"is-full-job-control"},
|
2016-11-12 07:26:16 +08:00
|
|
|
{STATUS_IS_INTERACTIVE, L"is-interactive"},
|
2016-11-09 07:30:52 +08:00
|
|
|
{STATUS_IS_INTERACTIVE_JOB_CTRL, L"is-interactive-job-control"},
|
2016-11-12 07:26:16 +08:00
|
|
|
{STATUS_IS_LOGIN, L"is-login"},
|
2016-11-09 07:30:52 +08:00
|
|
|
{STATUS_IS_NO_JOB_CTRL, L"is-no-job-control"},
|
|
|
|
{STATUS_SET_JOB_CONTROL, L"job-control"},
|
|
|
|
{STATUS_PRINT_STACK_TRACE, L"print-stack-trace"},
|
|
|
|
{STATUS_UNDEF, NULL}};
|
2016-11-12 07:26:16 +08:00
|
|
|
#define status_enum_map_len (sizeof status_enum_map / sizeof *status_enum_map)
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-11-05 13:32:43 +08:00
|
|
|
/// Remember the status subcommand and disallow selecting more than one status subcommand.
|
|
|
|
static bool set_status_cmd(wchar_t *const cmd, status_cmd_t *status_cmd, status_cmd_t sub_cmd,
|
|
|
|
io_streams_t &streams) {
|
2016-11-09 07:30:52 +08:00
|
|
|
if (*status_cmd != STATUS_UNDEF) {
|
2016-11-05 13:32:43 +08:00
|
|
|
wchar_t err_text[1024];
|
2016-11-09 07:30:52 +08:00
|
|
|
const wchar_t *subcmd_str1 = enum_to_str(*status_cmd, status_enum_map);
|
|
|
|
const wchar_t *subcmd_str2 = enum_to_str(sub_cmd, status_enum_map);
|
2016-11-05 13:32:43 +08:00
|
|
|
swprintf(err_text, sizeof(err_text) / sizeof(wchar_t),
|
2016-11-09 07:30:52 +08:00
|
|
|
_(L"you cannot do both '%ls' and '%ls' in the same invocation"), subcmd_str1,
|
|
|
|
subcmd_str2);
|
2016-11-05 13:32:43 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_COMBO2, cmd, err_text);
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
|
2016-11-05 13:32:43 +08:00
|
|
|
*status_cmd = sub_cmd;
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-11-09 07:30:52 +08:00
|
|
|
#define CHECK_FOR_UNEXPECTED_STATUS_ARGS(status_cmd) \
|
|
|
|
if (args.size() != 0) { \
|
|
|
|
const wchar_t *subcmd_str = enum_to_str(status_cmd, status_enum_map); \
|
|
|
|
if (!subcmd_str) subcmd_str = L"default"; \
|
|
|
|
streams.err.append_format(BUILTIN_ERR_ARG_COUNT2, cmd, subcmd_str, 0, args.size()); \
|
|
|
|
status = STATUS_BUILTIN_ERROR; \
|
|
|
|
break; \
|
2016-11-05 13:32:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int job_control_str_to_mode(const wchar_t *mode, wchar_t *cmd, io_streams_t &streams) {
|
|
|
|
if (wcscmp(mode, L"full") == 0) {
|
|
|
|
return JOB_CONTROL_ALL;
|
|
|
|
} else if (wcscmp(mode, L"interactive") == 0) {
|
|
|
|
return JOB_CONTROL_INTERACTIVE;
|
|
|
|
} else if (wcscmp(mode, L"none") == 0) {
|
|
|
|
return JOB_CONTROL_NONE;
|
|
|
|
}
|
|
|
|
streams.err.append_format(L"%ls: Invalid job control mode '%ls'\n", cmd, mode);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-11-05 13:32:43 +08:00
|
|
|
/// The status builtin. Gives various status information on fish.
|
|
|
|
static int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
|
|
|
wchar_t *cmd = argv[0];
|
|
|
|
int argc = builtin_count_args(argv);
|
2016-11-09 07:30:52 +08:00
|
|
|
status_cmd_t status_cmd = STATUS_UNDEF;
|
2016-11-05 13:32:43 +08:00
|
|
|
int status = STATUS_BUILTIN_OK;
|
|
|
|
int new_job_control_mode = -1;
|
|
|
|
|
|
|
|
/// Note: Do not add new flags that represent subcommands. We're encouraging people to switch to
|
|
|
|
/// the non-flag subcommand form. While these flags are deprecated they must be supported at
|
|
|
|
/// least until fish 3.0 and possibly longer to avoid breaking everyones config.fish and other
|
|
|
|
/// scripts.
|
|
|
|
const wchar_t *short_options = L":cbilfnhj:t";
|
|
|
|
const struct woption long_options[] = {{L"help", no_argument, 0, 'h'},
|
|
|
|
{L"is-command-substitution", no_argument, 0, 'c'},
|
|
|
|
{L"is-block", no_argument, 0, 'b'},
|
|
|
|
{L"is-interactive", no_argument, 0, 'i'},
|
|
|
|
{L"is-login", no_argument, 0, 'l'},
|
|
|
|
{L"is-full-job-control", no_argument, 0, 1},
|
|
|
|
{L"is-interactive-job-control", no_argument, 0, 2},
|
|
|
|
{L"is-no-job-control", no_argument, 0, 3},
|
|
|
|
{L"current-filename", no_argument, 0, 'f'},
|
|
|
|
{L"current-line-number", no_argument, 0, 'n'},
|
|
|
|
{L"job-control", required_argument, 0, 'j'},
|
|
|
|
{L"print-stack-trace", no_argument, 0, 't'},
|
|
|
|
{0, 0, 0, 0}};
|
|
|
|
|
|
|
|
int opt;
|
|
|
|
wgetopter_t w;
|
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (opt) {
|
2016-11-05 13:32:43 +08:00
|
|
|
case 1: {
|
|
|
|
if (!set_status_cmd(cmd, &status_cmd, STATUS_IS_FULL_JOB_CTRL, streams)) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: {
|
|
|
|
if (!set_status_cmd(cmd, &status_cmd, STATUS_IS_INTERACTIVE_JOB_CTRL, streams)) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: {
|
|
|
|
if (!set_status_cmd(cmd, &status_cmd, STATUS_IS_NO_JOB_CTRL, streams)) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'c': {
|
2016-11-05 13:32:43 +08:00
|
|
|
if (!set_status_cmd(cmd, &status_cmd, STATUS_IS_COMMAND_SUB, streams)) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'b': {
|
2016-11-05 13:32:43 +08:00
|
|
|
if (!set_status_cmd(cmd, &status_cmd, STATUS_IS_BLOCK, streams)) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'i': {
|
2016-11-05 13:32:43 +08:00
|
|
|
if (!set_status_cmd(cmd, &status_cmd, STATUS_IS_INTERACTIVE, streams)) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'l': {
|
2016-11-05 13:32:43 +08:00
|
|
|
if (!set_status_cmd(cmd, &status_cmd, STATUS_IS_LOGIN, streams)) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'f': {
|
2016-11-05 13:32:43 +08:00
|
|
|
if (!set_status_cmd(cmd, &status_cmd, STATUS_CURRENT_FILENAME, streams)) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'n': {
|
2016-11-05 13:32:43 +08:00
|
|
|
if (!set_status_cmd(cmd, &status_cmd, STATUS_CURRENT_LINE_NUMBER, streams)) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'j': {
|
2016-11-05 13:32:43 +08:00
|
|
|
if (!set_status_cmd(cmd, &status_cmd, STATUS_SET_JOB_CONTROL, streams)) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
new_job_control_mode = job_control_str_to_mode(w.woptarg, cmd, streams);
|
|
|
|
if (new_job_control_mode == -1) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 't': {
|
2016-11-05 13:32:43 +08:00
|
|
|
if (!set_status_cmd(cmd, &status_cmd, STATUS_PRINT_STACK_TRACE, streams)) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-11-05 13:32:43 +08:00
|
|
|
case 'h': {
|
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
|
|
|
return STATUS_BUILTIN_OK;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case ':': {
|
|
|
|
builtin_missing_argument(parser, streams, argv[0], argv[w.woptind - 1]);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
|
|
|
builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
2016-11-05 13:32:43 +08:00
|
|
|
DIE("unexpected retval from wgetopt_long");
|
2016-10-30 08:25:48 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-05 13:32:43 +08:00
|
|
|
// If a status command hasn't already been specified via a flag check the first word.
|
|
|
|
// Note that this can be simplified after we eliminate allowing subcommands as flags.
|
|
|
|
if (w.woptind < argc) {
|
2016-11-12 07:26:16 +08:00
|
|
|
status_cmd_t subcmd = str_to_enum(argv[w.woptind], status_enum_map, status_enum_map_len);
|
2016-11-09 07:30:52 +08:00
|
|
|
if (subcmd != STATUS_UNDEF) {
|
2016-11-05 13:32:43 +08:00
|
|
|
if (!set_status_cmd(cmd, &status_cmd, subcmd, streams)) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
w.woptind++;
|
|
|
|
}
|
2016-10-31 10:17:08 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-11-05 13:32:43 +08:00
|
|
|
// Every argument that we haven't consumed already is an argument for a subcommand.
|
|
|
|
const wcstring_list_t args(argv + w.woptind, argv + argc);
|
|
|
|
|
|
|
|
switch (status_cmd) {
|
2016-11-09 07:30:52 +08:00
|
|
|
case STATUS_UNDEF: {
|
2016-11-05 13:32:43 +08:00
|
|
|
CHECK_FOR_UNEXPECTED_STATUS_ARGS(status_cmd)
|
|
|
|
if (is_login) {
|
|
|
|
streams.out.append_format(_(L"This is a login shell\n"));
|
|
|
|
} else {
|
|
|
|
streams.out.append_format(_(L"This is not a login shell\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
streams.out.append_format(
|
|
|
|
_(L"Job control: %ls\n"),
|
|
|
|
job_control_mode == JOB_CONTROL_INTERACTIVE
|
|
|
|
? _(L"Only on interactive jobs")
|
|
|
|
: (job_control_mode == JOB_CONTROL_NONE ? _(L"Never") : _(L"Always")));
|
|
|
|
streams.out.append(parser.stack_trace());
|
|
|
|
break;
|
2016-10-31 10:17:08 +08:00
|
|
|
}
|
2016-11-05 13:32:43 +08:00
|
|
|
case STATUS_SET_JOB_CONTROL: {
|
|
|
|
if (new_job_control_mode != -1) {
|
|
|
|
// Flag form was used.
|
|
|
|
CHECK_FOR_UNEXPECTED_STATUS_ARGS(status_cmd)
|
|
|
|
} else {
|
|
|
|
if (args.size() != 1) {
|
2016-11-09 07:30:52 +08:00
|
|
|
const wchar_t *subcmd_str = enum_to_str(status_cmd, status_enum_map);
|
|
|
|
streams.err.append_format(BUILTIN_ERR_ARG_COUNT2, cmd, subcmd_str, 1,
|
2016-11-05 13:32:43 +08:00
|
|
|
args.size());
|
|
|
|
status = STATUS_BUILTIN_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
new_job_control_mode = job_control_str_to_mode(args[0].c_str(), cmd, streams);
|
|
|
|
if (new_job_control_mode == -1) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
job_control_mode = new_job_control_mode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case STATUS_CURRENT_FILENAME: {
|
|
|
|
CHECK_FOR_UNEXPECTED_STATUS_ARGS(status_cmd)
|
2016-10-31 10:17:08 +08:00
|
|
|
const wchar_t *fn = parser.current_filename();
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-10-31 10:17:08 +08:00
|
|
|
if (!fn) fn = _(L"Standard input");
|
|
|
|
streams.out.append_format(L"%ls\n", fn);
|
2016-11-05 13:32:43 +08:00
|
|
|
break;
|
2016-10-31 10:17:08 +08:00
|
|
|
}
|
2016-11-05 13:32:43 +08:00
|
|
|
case STATUS_CURRENT_LINE_NUMBER: {
|
|
|
|
CHECK_FOR_UNEXPECTED_STATUS_ARGS(status_cmd)
|
2016-10-31 10:17:08 +08:00
|
|
|
streams.out.append_format(L"%d\n", parser.get_lineno());
|
2016-11-05 13:32:43 +08:00
|
|
|
break;
|
2016-10-31 10:17:08 +08:00
|
|
|
}
|
2016-11-05 13:32:43 +08:00
|
|
|
case STATUS_IS_INTERACTIVE: {
|
|
|
|
CHECK_FOR_UNEXPECTED_STATUS_ARGS(status_cmd)
|
|
|
|
status = !is_interactive_session;
|
|
|
|
break;
|
2016-10-31 10:17:08 +08:00
|
|
|
}
|
2016-11-05 13:32:43 +08:00
|
|
|
case STATUS_IS_COMMAND_SUB: {
|
|
|
|
CHECK_FOR_UNEXPECTED_STATUS_ARGS(status_cmd)
|
|
|
|
status = !is_subshell;
|
|
|
|
break;
|
2016-10-31 10:17:08 +08:00
|
|
|
}
|
2016-11-05 13:32:43 +08:00
|
|
|
case STATUS_IS_BLOCK: {
|
|
|
|
CHECK_FOR_UNEXPECTED_STATUS_ARGS(status_cmd)
|
|
|
|
status = !is_block;
|
|
|
|
break;
|
2016-10-31 10:17:08 +08:00
|
|
|
}
|
2016-11-05 13:32:43 +08:00
|
|
|
case STATUS_IS_LOGIN: {
|
|
|
|
CHECK_FOR_UNEXPECTED_STATUS_ARGS(status_cmd)
|
|
|
|
status = !is_login;
|
|
|
|
break;
|
2016-10-31 10:17:08 +08:00
|
|
|
}
|
2016-11-05 13:32:43 +08:00
|
|
|
case STATUS_IS_FULL_JOB_CTRL: {
|
|
|
|
CHECK_FOR_UNEXPECTED_STATUS_ARGS(status_cmd)
|
|
|
|
status = job_control_mode != JOB_CONTROL_ALL;
|
|
|
|
break;
|
2016-10-31 10:17:08 +08:00
|
|
|
}
|
2016-11-05 13:32:43 +08:00
|
|
|
case STATUS_IS_INTERACTIVE_JOB_CTRL: {
|
|
|
|
CHECK_FOR_UNEXPECTED_STATUS_ARGS(status_cmd)
|
|
|
|
status = job_control_mode != JOB_CONTROL_INTERACTIVE;
|
|
|
|
break;
|
2016-10-31 10:17:08 +08:00
|
|
|
}
|
2016-11-05 13:32:43 +08:00
|
|
|
case STATUS_IS_NO_JOB_CTRL: {
|
|
|
|
CHECK_FOR_UNEXPECTED_STATUS_ARGS(status_cmd)
|
|
|
|
status = job_control_mode != JOB_CONTROL_NONE;
|
|
|
|
break;
|
2016-10-31 10:17:08 +08:00
|
|
|
}
|
2016-11-05 13:32:43 +08:00
|
|
|
case STATUS_PRINT_STACK_TRACE: {
|
|
|
|
CHECK_FOR_UNEXPECTED_STATUS_ARGS(status_cmd)
|
2016-10-31 10:17:08 +08:00
|
|
|
streams.out.append(parser.stack_trace());
|
2016-11-05 13:32:43 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-11-05 13:32:43 +08:00
|
|
|
return status;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// The exit builtin. Calls reader_exit to exit and returns the value specified.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_exit(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
|
|
|
|
2016-10-23 11:32:25 +08:00
|
|
|
if (argc > 2) {
|
2016-11-17 14:00:33 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Too many arguments\n"), argv[0]);
|
2016-10-23 11:32:25 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
|
2016-10-23 11:32:25 +08:00
|
|
|
long ec;
|
|
|
|
if (argc == 1) {
|
|
|
|
ec = proc_get_last_status();
|
|
|
|
} else {
|
2016-11-23 12:24:03 +08:00
|
|
|
ec = fish_wcstol(argv[1]);
|
|
|
|
if (errno) {
|
2016-10-23 11:32:25 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), argv[0],
|
|
|
|
argv[1]);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reader_exit(1, 0);
|
|
|
|
return (int)ec;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 09:23:10 +08:00
|
|
|
/// The cd builtin. Changes the current directory to the one specified or to $HOME if none is
|
|
|
|
/// specified. The directory can be relative to any directory in the CDPATH variable.
|
2016-04-20 09:17:39 +08:00
|
|
|
/// The cd builtin. Changes the current directory to the one specified or to $HOME if none is
|
|
|
|
/// specified. The directory can be relative to any directory in the CDPATH variable.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_cd(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2012-11-19 08:30:30 +08:00
|
|
|
env_var_t dir_in;
|
|
|
|
wcstring dir;
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (argv[1] == NULL) {
|
2012-11-19 08:30:30 +08:00
|
|
|
dir_in = env_get_string(L"HOME");
|
2016-04-19 10:25:12 +08:00
|
|
|
if (dir_in.missing_or_empty()) {
|
|
|
|
streams.err.append_format(_(L"%ls: Could not find home directory\n"), argv[0]);
|
2016-11-29 15:59:37 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
2016-02-28 11:38:15 +08:00
|
|
|
dir_in = env_var_t(argv[1]);
|
2012-05-09 17:33:42 +08:00
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2012-07-21 13:11:05 +08:00
|
|
|
bool got_cd_path = false;
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!dir_in.missing()) {
|
2012-07-21 13:11:05 +08:00
|
|
|
got_cd_path = path_get_cdpath(dir_in, &dir);
|
2012-05-09 17:33:42 +08:00
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!got_cd_path) {
|
2016-11-29 15:59:37 +08:00
|
|
|
if (errno == ENOTDIR) {
|
2016-04-19 10:25:12 +08:00
|
|
|
streams.err.append_format(_(L"%ls: '%ls' is not a directory\n"), argv[0],
|
|
|
|
dir_in.c_str());
|
|
|
|
} else if (errno == ENOENT) {
|
|
|
|
streams.err.append_format(_(L"%ls: The directory '%ls' does not exist\n"), argv[0],
|
|
|
|
dir_in.c_str());
|
|
|
|
} else if (errno == EROTTEN) {
|
|
|
|
streams.err.append_format(_(L"%ls: '%ls' is a rotten symlink\n"), argv[0],
|
|
|
|
dir_in.c_str());
|
|
|
|
} else {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Unknown error trying to locate directory '%ls'\n"),
|
2016-04-19 10:25:12 +08:00
|
|
|
argv[0], dir_in.c_str());
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 15:59:37 +08:00
|
|
|
if (!shell_is_interactive()) streams.err.append(parser.current_line());
|
|
|
|
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-11-29 15:59:37 +08:00
|
|
|
if (wchdir(dir) != 0) {
|
2012-11-19 08:30:30 +08:00
|
|
|
struct stat buffer;
|
2016-10-02 08:21:40 +08:00
|
|
|
int status;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
status = wstat(dir, &buffer);
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!status && S_ISDIR(buffer.st_mode)) {
|
|
|
|
streams.err.append_format(_(L"%ls: Permission denied: '%ls'\n"), argv[0], dir.c_str());
|
2016-08-17 09:23:10 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
|
|
|
streams.err.append_format(_(L"%ls: '%ls' is not a directory\n"), argv[0], dir.c_str());
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-05-15 11:35:54 +08:00
|
|
|
if (!shell_is_interactive()) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append(parser.current_line());
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 15:59:37 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!env_set_pwd()) {
|
2016-08-17 09:23:10 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Could not set PWD variable\n"), argv[0]);
|
2016-11-29 15:59:37 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 15:59:37 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
2005-09-20 21:26:39 +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);
|
2012-11-19 08:30:30 +08:00
|
|
|
int argc;
|
|
|
|
argc = builtin_count_args(argv);
|
2016-04-19 10:25:12 +08:00
|
|
|
streams.out.append_format(L"%d\n", argc - 1);
|
|
|
|
return !(argc - 1);
|
2007-08-01 05:23:32 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Implementation of the builtin contains command, used to check if a specified string is part of
|
|
|
|
/// a list.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_contains(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2015-07-26 09:16:00 +08:00
|
|
|
wgetopter_t w;
|
2012-11-19 08:30:30 +08:00
|
|
|
int argc;
|
|
|
|
argc = builtin_count_args(argv);
|
|
|
|
wchar_t *needle;
|
2013-04-29 05:35:00 +08:00
|
|
|
bool should_output_index = false;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
const struct woption long_options[] = {
|
|
|
|
{L"help", no_argument, 0, 'h'}, {L"index", no_argument, 0, 'i'}, {0, 0, 0, 0}};
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
while (1) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int opt_index = 0;
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
int opt = w.wgetopt_long(argc, argv, L"+hi", long_options, &opt_index);
|
|
|
|
if (opt == -1) break;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 0: {
|
|
|
|
assert(opt_index >= 0 &&
|
|
|
|
(size_t)opt_index < sizeof long_options / sizeof *long_options);
|
|
|
|
if (long_options[opt_index].flag != 0) break;
|
|
|
|
streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
|
|
|
|
long_options[opt_index].name);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'h': {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case ':': {
|
|
|
|
builtin_missing_argument(parser, streams, argv[0], argv[w.woptind - 1]);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
|
|
|
builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
|
|
|
case 'i': {
|
2013-04-29 05:35:00 +08:00
|
|
|
should_output_index = true;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-26 09:16:00 +08:00
|
|
|
needle = argv[w.woptind];
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!needle) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Key not specified\n"), argv[0]);
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
|
|
|
for (int i = w.woptind + 1; i < argc; i++) {
|
|
|
|
if (!wcscmp(needle, argv[i])) {
|
|
|
|
if (should_output_index) streams.out.append_format(L"%d\n", i - w.woptind);
|
2016-03-04 10:49:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2007-08-02 06:53:18 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// The . (dot) builtin, sometimes called source. Evaluates the contents of a file.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_source(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2012-03-02 16:27:40 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-19 08:30:30 +08:00
|
|
|
int fd;
|
|
|
|
int res = STATUS_BUILTIN_OK;
|
|
|
|
struct stat buf;
|
|
|
|
int argc;
|
|
|
|
|
|
|
|
argc = builtin_count_args(argv);
|
|
|
|
|
2013-12-16 08:43:22 +08:00
|
|
|
const wchar_t *fn, *fn_intern;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (argc < 2 || (wcscmp(argv[1], L"-") == 0)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
fn = L"-";
|
|
|
|
fn_intern = fn;
|
2015-09-22 02:24:49 +08:00
|
|
|
fd = dup(streams.stdin_fd);
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
|
|
|
if ((fd = wopen_cloexec(argv[1], O_RDONLY)) == -1) {
|
|
|
|
streams.err.append_format(_(L"%ls: Error encountered while sourcing file '%ls':\n"),
|
|
|
|
argv[0], argv[1]);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_wperror(L"source", streams);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (fstat(fd, &buf) == -1) {
|
2012-11-19 08:30:30 +08:00
|
|
|
close(fd);
|
2016-04-19 10:25:12 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Error encountered while sourcing file '%ls':\n"),
|
|
|
|
argv[0], argv[1]);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_wperror(L"source", streams);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!S_ISREG(buf.st_mode)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
close(fd);
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append_format(_(L"%ls: '%ls' is not a file\n"), argv[0], argv[1]);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-12-16 08:43:22 +08:00
|
|
|
fn_intern = intern(argv[1]);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-22 07:35:35 +08:00
|
|
|
const source_block_t *sb = parser.push_block<source_block_t>(fn_intern);
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_push_current_filename(fn_intern);
|
|
|
|
|
2016-04-07 04:04:44 +08:00
|
|
|
env_set_argv(argc > 1 ? argv + 2 : argv + 1);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2015-09-22 02:24:49 +08:00
|
|
|
res = reader_read(fd, streams.io_chain ? *streams.io_chain : io_chain_t());
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-01-22 07:35:35 +08:00
|
|
|
parser.pop_block(sb);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (res) {
|
|
|
|
streams.err.append_format(_(L"%ls: Error while reading file '%ls'\n"), argv[0],
|
|
|
|
fn_intern == intern_static(L"-") ? L"<stdin>" : fn_intern);
|
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
res = proc_get_last_status();
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Do not close fd after calling reader_read. reader_read automatically closes it before calling
|
|
|
|
// eval.
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_pop_current_filename();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Builtin for putting a job in the foreground.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_fg(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
|
|
|
job_t *j = NULL;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (argv[1] == 0) {
|
|
|
|
// Select last constructed job (I.e. first job in the job que) that is possible to put in
|
|
|
|
// the foreground.
|
2012-11-19 08:30:30 +08:00
|
|
|
job_iterator_t jobs;
|
2016-04-19 10:25:12 +08:00
|
|
|
while ((j = jobs.next())) {
|
2017-01-27 07:06:58 +08:00
|
|
|
if (j->get_flag(JOB_CONSTRUCTED) && (!job_is_completed(j)) &&
|
|
|
|
((job_is_stopped(j) || (!j->get_flag(JOB_FOREGROUND))) &&
|
|
|
|
j->get_flag(JOB_CONTROL))) {
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!j) {
|
|
|
|
streams.err.append_format(_(L"%ls: There are no suitable jobs\n"), argv[0]);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
} else if (argv[2] != 0) {
|
2016-06-18 22:41:27 +08:00
|
|
|
// Specifying more than one job to put to the foreground is a syntax error, we still
|
2016-04-19 10:25:12 +08:00
|
|
|
// try to locate the job argv[1], since we want to know if this is an ambigous job
|
|
|
|
// specification or if this is an malformed job id.
|
2016-10-02 08:21:40 +08:00
|
|
|
int pid;
|
|
|
|
int found_job = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-11-23 12:24:03 +08:00
|
|
|
pid = fish_wcstoi(argv[1]);
|
|
|
|
if (!(errno || pid < 0)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
j = job_get_from_pid(pid);
|
2016-10-02 08:21:40 +08:00
|
|
|
if (j) found_job = 1;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (found_job) {
|
|
|
|
streams.err.append_format(_(L"%ls: Ambiguous job\n"), argv[0]);
|
|
|
|
} else {
|
|
|
|
streams.err.append_format(_(L"%ls: '%ls' is not a job\n"), 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);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
j = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
2016-11-23 12:24:03 +08:00
|
|
|
int pid = abs(fish_wcstoi(argv[1]));
|
|
|
|
if (errno) {
|
2016-04-19 10:25:12 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_NOT_NUMBER, argv[0], argv[1]);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
j = job_get_from_pid(pid);
|
2017-01-27 07:06:58 +08:00
|
|
|
if (!j || !j->get_flag(JOB_CONSTRUCTED) || job_is_completed(j)) {
|
2016-04-19 10:25:12 +08:00
|
|
|
streams.err.append_format(_(L"%ls: No suitable job: %d\n"), argv[0], pid);
|
2016-10-02 08:21:40 +08:00
|
|
|
j = 0;
|
2017-01-27 07:06:58 +08:00
|
|
|
} else if (!j->get_flag(JOB_CONTROL)) {
|
2016-04-19 10:25:12 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Can't put job %d, '%ls' to foreground because "
|
|
|
|
L"it is not under job control\n"),
|
|
|
|
argv[0], pid, j->command_wcstr());
|
2016-10-02 08:21:40 +08:00
|
|
|
j = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-31 10:17:08 +08:00
|
|
|
if (!j) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-10-31 10:17:08 +08:00
|
|
|
if (streams.err_is_redirected) {
|
|
|
|
streams.err.append_format(FG_MSG, j->job_id, j->command_wcstr());
|
|
|
|
} else {
|
|
|
|
// If we aren't redirecting, send output to real stderr, since stuff in sb_err won't get
|
|
|
|
// printed until the command finishes.
|
|
|
|
fwprintf(stderr, FG_MSG, j->job_id, j->command_wcstr());
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-10-31 10:17:08 +08:00
|
|
|
const wcstring ft = tok_first(j->command());
|
|
|
|
if (!ft.empty()) env_set(L"_", ft.c_str(), ENV_EXPORT);
|
|
|
|
reader_write_title(j->command());
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2017-01-27 06:47:32 +08:00
|
|
|
job_promote(j);
|
2017-01-27 07:06:58 +08:00
|
|
|
j->set_flag(JOB_FOREGROUND, true);
|
2016-10-31 10:17:08 +08:00
|
|
|
|
|
|
|
job_continue(j, job_is_stopped(j));
|
|
|
|
return STATUS_BUILTIN_OK;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Helper function for builtin_bg().
|
2017-04-13 13:48:32 +08:00
|
|
|
static int send_to_bg(parser_t &parser, io_streams_t &streams, job_t *j) {
|
2017-04-02 23:02:55 +08:00
|
|
|
assert(j != NULL);
|
|
|
|
if (!j->get_flag(JOB_CONTROL)) {
|
2016-04-19 10:25:12 +08:00
|
|
|
streams.err.append_format(
|
|
|
|
_(L"%ls: Can't put job %d, '%ls' to background because it is not under job control\n"),
|
|
|
|
L"bg", j->job_id, j->command_wcstr());
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, L"bg", streams.err);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
|
|
|
|
streams.err.append_format(_(L"Send job %d '%ls' to background\n"), j->job_id,
|
|
|
|
j->command_wcstr());
|
2017-01-27 06:47:32 +08:00
|
|
|
job_promote(j);
|
2017-01-27 07:06:58 +08:00
|
|
|
j->set_flag(JOB_FOREGROUND, false);
|
2012-11-19 08:30:30 +08:00
|
|
|
job_continue(j, job_is_stopped(j));
|
|
|
|
return STATUS_BUILTIN_OK;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Builtin for putting a job in the background.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_bg(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int res = STATUS_BUILTIN_OK;
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2017-04-13 13:48:32 +08:00
|
|
|
if (!argv[1]) {
|
|
|
|
// No jobs were specified so use the most recent (i.e., last) job.
|
2012-11-19 08:30:30 +08:00
|
|
|
job_t *j;
|
2012-01-30 08:36:21 +08:00
|
|
|
job_iterator_t jobs;
|
2016-04-19 10:25:12 +08:00
|
|
|
while ((j = jobs.next())) {
|
2017-01-27 07:06:58 +08:00
|
|
|
if (job_is_stopped(j) && j->get_flag(JOB_CONTROL) && (!job_is_completed(j))) {
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (!j) {
|
|
|
|
streams.err.append_format(_(L"%ls: There are no suitable jobs\n"), argv[0]);
|
2016-06-18 23:00:40 +08:00
|
|
|
res = STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
} else {
|
2017-04-13 13:48:32 +08:00
|
|
|
res = send_to_bg(parser, streams, j);
|
2017-04-02 23:02:55 +08:00
|
|
|
}
|
2017-04-13 13:48:32 +08:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The user specified at least one job to be backgrounded.
|
|
|
|
std::vector<int> pids;
|
|
|
|
|
|
|
|
// If one argument is not a valid pid (i.e. integer >= 0), fail without backgrounding anything,
|
|
|
|
// but still print errors for all of them.
|
|
|
|
for (int i = 1; argv[i]; i++) {
|
|
|
|
int pid = fish_wcstoi(argv[i]);
|
|
|
|
if (errno || pid < 0) {
|
|
|
|
streams.err.append_format(_(L"%ls: '%ls' is not a valid job specifier\n"), L"bg",
|
|
|
|
argv[i]);
|
|
|
|
res = STATUS_BUILTIN_ERROR;
|
2017-04-02 23:02:55 +08:00
|
|
|
}
|
2017-04-13 13:48:32 +08:00
|
|
|
pids.push_back(pid);
|
|
|
|
}
|
2016-10-02 08:21:40 +08:00
|
|
|
|
2017-04-13 13:48:32 +08:00
|
|
|
if (res == STATUS_BUILTIN_ERROR) return res;
|
|
|
|
|
|
|
|
// Background all existing jobs that match the pids.
|
|
|
|
// Non-existent jobs aren't an error, but information about them is useful.
|
|
|
|
for (auto p : pids) {
|
|
|
|
if (job_t* j = job_get_from_pid(p)) {
|
|
|
|
res |= send_to_bg(parser, streams, j);
|
|
|
|
} else {
|
|
|
|
streams.err.append_format(_(L"%ls: Could not find job '%d'\n"), argv[0], p);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
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) {
|
|
|
|
int is_break = (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);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
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);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
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;
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_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) {
|
2016-10-10 05:38:26 +08:00
|
|
|
if (argv[1] != NULL) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_ARG_COUNT1, argv[0], 0, builtin_count_args(argv));
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-01-22 07:35:35 +08:00
|
|
|
const breakpoint_block_t *bpb = parser.push_block<breakpoint_block_t>();
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2015-09-22 02:24:49 +08:00
|
|
|
reader_read(STDIN_FILENO, streams.io_chain ? *streams.io_chain : io_chain_t());
|
2012-11-19 08:30:30 +08:00
|
|
|
|
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-20 09:17:39 +08:00
|
|
|
/// Function for handling the \c return builtin.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_return(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
|
|
|
|
2016-10-23 11:32:25 +08:00
|
|
|
if (argc > 2) {
|
2016-11-18 06:53:50 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
|
2016-10-23 11:32:25 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int status;
|
|
|
|
if (argc == 2) {
|
2016-11-23 12:24:03 +08:00
|
|
|
status = fish_wcstoi(argv[1]);
|
|
|
|
if (errno) {
|
2016-10-23 11:32:25 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Argument '%ls' must be an integer\n"), argv[0],
|
|
|
|
argv[1]);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-10-23 11:32:25 +08:00
|
|
|
} else {
|
|
|
|
status = proc_get_last_status();
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// Find the function block.
|
2013-12-21 09:41:21 +08:00
|
|
|
size_t function_block_idx;
|
2016-04-19 10:25:12 +08:00
|
|
|
for (function_block_idx = 0; function_block_idx < parser.block_count(); function_block_idx++) {
|
2013-12-21 09:41:21 +08:00
|
|
|
const block_t *b = parser.block_at_index(function_block_idx);
|
2016-04-19 10:25:12 +08:00
|
|
|
if (b->type() == FUNCTION_CALL || b->type() == FUNCTION_CALL_NO_SHADOW) break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (function_block_idx >= parser.block_count()) {
|
|
|
|
streams.err.append_format(_(L"%ls: Not inside of function\n"), argv[0]);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.err);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2017-01-22 06:15:03 +08:00
|
|
|
// Skip everything up to and including the function block.
|
|
|
|
for (size_t i = 0; i <= function_block_idx; i++) {
|
2013-12-21 09:41:21 +08:00
|
|
|
block_t *b = parser.block_at_index(i);
|
|
|
|
b->skip = true;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
return status;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-11-09 07:30:52 +08:00
|
|
|
enum hist_cmd_t { HIST_SEARCH = 1, HIST_DELETE, HIST_CLEAR, HIST_MERGE, HIST_SAVE, HIST_UNDEF };
|
2016-11-12 07:26:16 +08:00
|
|
|
// Must be sorted by string, not enum or random.
|
|
|
|
const enum_map<hist_cmd_t> hist_enum_map[] = {{HIST_CLEAR, L"clear"}, {HIST_DELETE, L"delete"},
|
|
|
|
{HIST_MERGE, L"merge"}, {HIST_SAVE, L"save"},
|
|
|
|
{HIST_SEARCH, L"search"}, {HIST_UNDEF, NULL}};
|
|
|
|
#define hist_enum_map_len (sizeof hist_enum_map / sizeof *hist_enum_map)
|
2016-07-11 16:45:30 +08:00
|
|
|
|
2016-07-14 13:33:50 +08:00
|
|
|
/// Remember the history subcommand and disallow selecting more than one history subcommand.
|
|
|
|
static bool set_hist_cmd(wchar_t *const cmd, hist_cmd_t *hist_cmd, hist_cmd_t sub_cmd,
|
|
|
|
io_streams_t &streams) {
|
2016-11-09 07:30:52 +08:00
|
|
|
if (*hist_cmd != HIST_UNDEF) {
|
2016-07-14 13:33:50 +08:00
|
|
|
wchar_t err_text[1024];
|
2016-11-09 07:30:52 +08:00
|
|
|
const wchar_t *subcmd_str1 = enum_to_str(*hist_cmd, hist_enum_map);
|
|
|
|
const wchar_t *subcmd_str2 = enum_to_str(sub_cmd, hist_enum_map);
|
2016-07-14 13:33:50 +08:00
|
|
|
swprintf(err_text, sizeof(err_text) / sizeof(wchar_t),
|
2016-11-09 07:30:52 +08:00
|
|
|
_(L"you cannot do both '%ls' and '%ls' in the same invocation"), subcmd_str1,
|
|
|
|
subcmd_str2);
|
2016-07-14 13:33:50 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_COMBO2, cmd, err_text);
|
|
|
|
return false;
|
2016-06-28 03:49:34 +08:00
|
|
|
}
|
2016-07-14 13:33:50 +08:00
|
|
|
|
|
|
|
*hist_cmd = sub_cmd;
|
2016-06-28 03:49:34 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-19 11:21:27 +08:00
|
|
|
#define CHECK_FOR_UNEXPECTED_HIST_ARGS(hist_cmd) \
|
2016-10-17 11:33:33 +08:00
|
|
|
if (history_search_type_defined || show_time_format || null_terminate) { \
|
2016-11-09 07:30:52 +08:00
|
|
|
const wchar_t *subcmd_str = enum_to_str(hist_cmd, hist_enum_map); \
|
2016-09-19 11:21:27 +08:00
|
|
|
streams.err.append_format(_(L"%ls: you cannot use any options with the %ls command\n"), \
|
2016-11-09 07:30:52 +08:00
|
|
|
cmd, subcmd_str); \
|
2016-09-19 11:21:27 +08:00
|
|
|
status = STATUS_BUILTIN_ERROR; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
if (args.size() != 0) { \
|
2016-11-09 07:30:52 +08:00
|
|
|
const wchar_t *subcmd_str = enum_to_str(hist_cmd, hist_enum_map); \
|
|
|
|
streams.err.append_format(BUILTIN_ERR_ARG_COUNT2, cmd, subcmd_str, 0, args.size()); \
|
2016-09-19 11:21:27 +08:00
|
|
|
status = STATUS_BUILTIN_ERROR; \
|
|
|
|
break; \
|
2016-07-14 13:33:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Manipulate history of interactive commands executed by the user.
|
2016-04-19 10:25:12 +08:00
|
|
|
static int builtin_history(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2016-07-14 13:33:50 +08:00
|
|
|
wchar_t *cmd = argv[0];
|
2012-06-05 12:24:42 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
2016-11-09 07:30:52 +08:00
|
|
|
hist_cmd_t hist_cmd = HIST_UNDEF;
|
2016-07-30 12:24:26 +08:00
|
|
|
history_search_type_t search_type = (history_search_type_t)-1;
|
2016-09-21 13:32:38 +08:00
|
|
|
long max_items = LONG_MAX;
|
2016-07-30 12:24:26 +08:00
|
|
|
bool history_search_type_defined = false;
|
2016-09-19 11:21:27 +08:00
|
|
|
const wchar_t *show_time_format = NULL;
|
2016-09-24 11:12:15 +08:00
|
|
|
bool case_sensitive = false;
|
2016-10-17 11:33:33 +08:00
|
|
|
bool null_terminate = false;
|
2016-09-11 11:42:52 +08:00
|
|
|
|
2016-11-05 13:32:43 +08:00
|
|
|
/// Note: Do not add new flags that represent subcommands. We're encouraging people to switch to
|
|
|
|
/// the non-flag subcommand form. While many of these flags are deprecated they must be
|
|
|
|
/// supported at least until fish 3.0 and possibly longer to avoid breaking everyones
|
|
|
|
/// config.fish and other scripts.
|
2016-10-17 11:33:33 +08:00
|
|
|
const wchar_t *short_options = L":Cmn:epchtz";
|
2016-09-11 11:42:52 +08:00
|
|
|
const struct woption long_options[] = {{L"prefix", no_argument, NULL, 'p'},
|
|
|
|
{L"contains", no_argument, NULL, 'c'},
|
|
|
|
{L"help", no_argument, NULL, 'h'},
|
2016-09-19 11:21:27 +08:00
|
|
|
{L"show-time", optional_argument, NULL, 't'},
|
|
|
|
{L"with-time", optional_argument, NULL, 't'},
|
2016-09-11 11:42:52 +08:00
|
|
|
{L"exact", no_argument, NULL, 'e'},
|
2016-09-21 13:32:38 +08:00
|
|
|
{L"max", required_argument, NULL, 'n'},
|
2016-10-17 11:33:33 +08:00
|
|
|
{L"null", no_argument, 0, 'z'},
|
2016-09-24 11:12:15 +08:00
|
|
|
{L"case-sensitive", no_argument, 0, 'C'},
|
2016-09-11 11:42:52 +08:00
|
|
|
{L"delete", no_argument, NULL, 1},
|
|
|
|
{L"search", no_argument, NULL, 2},
|
|
|
|
{L"save", no_argument, NULL, 3},
|
|
|
|
{L"clear", no_argument, NULL, 4},
|
|
|
|
{L"merge", no_argument, NULL, 5},
|
|
|
|
{NULL, 0, NULL, 0}};
|
2012-06-05 12:24:42 +08:00
|
|
|
|
|
|
|
history_t *history = reader_get_history();
|
2016-04-19 10:25:12 +08:00
|
|
|
// Use the default history if we have none (which happens if invoked non-interactively, e.g.
|
|
|
|
// from webconfig.py.
|
|
|
|
if (!history) history = &history_t::history_with_name(L"fish");
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-09-11 11:42:52 +08:00
|
|
|
int opt;
|
2016-07-14 13:33:50 +08:00
|
|
|
wgetopter_t w;
|
2016-09-11 11:42:52 +08:00
|
|
|
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
|
2016-04-19 10:25:12 +08:00
|
|
|
switch (opt) {
|
2016-09-11 11:42:52 +08:00
|
|
|
case 1: {
|
|
|
|
if (!set_hist_cmd(cmd, &hist_cmd, HIST_DELETE, streams)) {
|
2016-07-14 13:33:50 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-09-11 11:42:52 +08:00
|
|
|
case 2: {
|
|
|
|
if (!set_hist_cmd(cmd, &hist_cmd, HIST_SEARCH, streams)) {
|
2016-07-14 13:33:50 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-09-11 11:42:52 +08:00
|
|
|
case 3: {
|
2016-07-14 13:33:50 +08:00
|
|
|
if (!set_hist_cmd(cmd, &hist_cmd, HIST_SAVE, streams)) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-09-11 11:42:52 +08:00
|
|
|
case 4: {
|
|
|
|
if (!set_hist_cmd(cmd, &hist_cmd, HIST_CLEAR, streams)) {
|
2016-07-14 13:33:50 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-09-11 11:42:52 +08:00
|
|
|
case 5: {
|
|
|
|
if (!set_hist_cmd(cmd, &hist_cmd, HIST_MERGE, streams)) {
|
2016-07-14 13:33:50 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-09-24 11:12:15 +08:00
|
|
|
case 'C': {
|
|
|
|
case_sensitive = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-07-14 13:33:50 +08:00
|
|
|
case 'p': {
|
|
|
|
search_type = HISTORY_SEARCH_TYPE_PREFIX;
|
2016-07-30 12:24:26 +08:00
|
|
|
history_search_type_defined = true;
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-07-14 13:33:50 +08:00
|
|
|
case 'c': {
|
|
|
|
search_type = HISTORY_SEARCH_TYPE_CONTAINS;
|
2016-07-30 12:24:26 +08:00
|
|
|
history_search_type_defined = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'e': {
|
|
|
|
search_type = HISTORY_SEARCH_TYPE_EXACT;
|
|
|
|
history_search_type_defined = true;
|
2014-07-26 01:08:21 +08:00
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-06-28 03:49:34 +08:00
|
|
|
case 't': {
|
2016-09-19 11:21:27 +08:00
|
|
|
show_time_format = w.woptarg ? w.woptarg : L"# %c%n";
|
2016-06-28 03:49:34 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-09-21 13:32:38 +08:00
|
|
|
case 'n': {
|
2016-11-23 12:24:03 +08:00
|
|
|
max_items = fish_wcstol(w.woptarg);
|
|
|
|
if (errno) {
|
2016-10-05 11:06:14 +08:00
|
|
|
streams.err.append_format(_(L"%ls: max value '%ls' is not a valid number\n"),
|
|
|
|
argv[0], w.woptarg);
|
2016-09-21 13:32:38 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-10-17 11:33:33 +08:00
|
|
|
case 'z': {
|
|
|
|
null_terminate = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-04-19 10:25:12 +08:00
|
|
|
case 'h': {
|
2016-07-14 13:33:50 +08:00
|
|
|
builtin_print_help(parser, streams, cmd, streams.out);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-09-11 11:42:52 +08:00
|
|
|
case ':': {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_MISSING, cmd, argv[w.woptind - 1]);
|
2012-11-19 16:31:03 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-09-11 11:42:52 +08:00
|
|
|
case '?': {
|
2016-09-21 13:32:38 +08:00
|
|
|
// Try to parse it as a number; e.g., "-123".
|
2016-11-23 12:24:03 +08:00
|
|
|
max_items = fish_wcstol(argv[w.woptind - 1] + 1);
|
|
|
|
if (errno) {
|
2016-09-21 13:32:38 +08:00
|
|
|
streams.err.append_format(BUILTIN_ERR_UNKNOWN, cmd, argv[w.woptind - 1]);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
w.nextchar = NULL;
|
|
|
|
break;
|
2016-04-19 10:25:12 +08:00
|
|
|
}
|
2016-11-02 13:36:30 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected retval from wgetopt_long");
|
|
|
|
break;
|
|
|
|
}
|
2016-09-11 11:42:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 13:32:38 +08:00
|
|
|
if (max_items <= 0) {
|
|
|
|
streams.err.append_format(_(L"%ls: max value '%ls' is not a valid number\n"), argv[0],
|
|
|
|
w.woptarg);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:42:52 +08:00
|
|
|
// If a history command hasn't already been specified via a flag check the first word.
|
|
|
|
// Note that this can be simplified after we eliminate allowing subcommands as flags.
|
|
|
|
// See the TODO above regarding the `long_options` array.
|
2016-11-05 13:32:43 +08:00
|
|
|
if (w.woptind < argc) {
|
2016-11-12 07:26:16 +08:00
|
|
|
hist_cmd_t subcmd = str_to_enum(argv[w.woptind], hist_enum_map, hist_enum_map_len);
|
2016-11-09 07:30:52 +08:00
|
|
|
if (subcmd != HIST_UNDEF) {
|
2016-11-05 13:32:43 +08:00
|
|
|
if (!set_hist_cmd(cmd, &hist_cmd, subcmd, streams)) {
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2016-09-11 11:42:52 +08:00
|
|
|
w.woptind++;
|
2012-06-05 12:24:42 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-06-05 12:24:42 +08:00
|
|
|
|
2016-09-11 11:42:52 +08:00
|
|
|
// Every argument that we haven't consumed already is an argument for a subcommand (e.g., a
|
|
|
|
// search term).
|
2015-07-26 09:16:00 +08:00
|
|
|
const wcstring_list_t args(argv + w.woptind, argv + argc);
|
2014-07-26 01:08:21 +08:00
|
|
|
|
2016-09-11 11:42:52 +08:00
|
|
|
// Establish appropriate defaults.
|
2016-11-09 07:30:52 +08:00
|
|
|
if (hist_cmd == HIST_UNDEF) hist_cmd = HIST_SEARCH;
|
2016-07-30 12:24:26 +08:00
|
|
|
if (!history_search_type_defined) {
|
|
|
|
if (hist_cmd == HIST_SEARCH) search_type = HISTORY_SEARCH_TYPE_CONTAINS;
|
|
|
|
if (hist_cmd == HIST_DELETE) search_type = HISTORY_SEARCH_TYPE_EXACT;
|
|
|
|
}
|
2012-07-27 15:31:00 +08:00
|
|
|
|
2016-07-14 13:33:50 +08:00
|
|
|
int status = STATUS_BUILTIN_OK;
|
|
|
|
switch (hist_cmd) {
|
|
|
|
case HIST_SEARCH: {
|
2016-09-24 11:12:15 +08:00
|
|
|
if (!history->search(search_type, args, show_time_format, max_items, case_sensitive,
|
2016-10-17 11:33:33 +08:00
|
|
|
null_terminate, streams)) {
|
2016-07-14 13:33:50 +08:00
|
|
|
status = STATUS_BUILTIN_ERROR;
|
2012-07-27 15:31:00 +08:00
|
|
|
}
|
2016-07-14 13:33:50 +08:00
|
|
|
break;
|
2012-06-05 12:24:42 +08:00
|
|
|
}
|
2016-07-14 13:33:50 +08:00
|
|
|
case HIST_DELETE: {
|
2016-09-24 11:12:15 +08:00
|
|
|
// TODO: Move this code to the history module and support the other search types
|
|
|
|
// including case-insensitive matches. At this time we expect the non-exact deletions to
|
|
|
|
// be handled only by the history function's interactive delete feature.
|
2016-07-30 12:24:26 +08:00
|
|
|
if (search_type != HISTORY_SEARCH_TYPE_EXACT) {
|
2016-09-11 11:42:52 +08:00
|
|
|
streams.err.append_format(_(L"builtin history delete only supports --exact\n"));
|
2016-07-30 12:24:26 +08:00
|
|
|
status = STATUS_BUILTIN_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2016-09-24 11:12:15 +08:00
|
|
|
if (!case_sensitive) {
|
|
|
|
streams.err.append_format(
|
|
|
|
_(L"builtin history delete only supports --case-sensitive\n"));
|
|
|
|
status = STATUS_BUILTIN_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2016-07-14 13:33:50 +08:00
|
|
|
for (wcstring_list_t::const_iterator iter = args.begin(); iter != args.end(); ++iter) {
|
|
|
|
wcstring delete_string = *iter;
|
2016-07-30 12:24:26 +08:00
|
|
|
if (delete_string[0] == '"' && delete_string[delete_string.length() - 1] == '"') {
|
2016-07-14 13:33:50 +08:00
|
|
|
delete_string = delete_string.substr(1, delete_string.length() - 2);
|
2016-07-30 12:24:26 +08:00
|
|
|
}
|
2016-07-14 13:33:50 +08:00
|
|
|
history->remove(delete_string);
|
2016-07-11 16:45:30 +08:00
|
|
|
}
|
2016-07-14 13:33:50 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HIST_CLEAR: {
|
2016-09-10 08:15:23 +08:00
|
|
|
CHECK_FOR_UNEXPECTED_HIST_ARGS(hist_cmd)
|
2016-07-14 13:33:50 +08:00
|
|
|
history->clear();
|
|
|
|
history->save();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HIST_MERGE: {
|
2016-09-10 08:15:23 +08:00
|
|
|
CHECK_FOR_UNEXPECTED_HIST_ARGS(hist_cmd)
|
2016-07-14 13:33:50 +08:00
|
|
|
history->incorporate_external_changes();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HIST_SAVE: {
|
2016-09-10 08:15:23 +08:00
|
|
|
CHECK_FOR_UNEXPECTED_HIST_ARGS(hist_cmd)
|
2016-07-14 13:33:50 +08:00
|
|
|
history->save();
|
|
|
|
break;
|
|
|
|
}
|
2016-11-09 07:30:52 +08:00
|
|
|
case HIST_UNDEF: {
|
|
|
|
DIE("Unexpected HIST_UNDEF seen");
|
2016-07-14 13:33:50 +08:00
|
|
|
break;
|
2016-07-11 16:45:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 13:33:50 +08:00
|
|
|
return status;
|
2012-06-05 12:24:42 +08:00
|
|
|
}
|
|
|
|
|
2016-04-04 10:02:46 +08:00
|
|
|
#if 0
|
|
|
|
// Disabled for the 2.2.0 release: https://github.com/fish-shell/fish-shell/issues/1809.
|
2015-09-22 02:24:49 +08:00
|
|
|
int builtin_parse(parser_t &parser, io_streams_t &streams, wchar_t **argv)
|
2013-06-16 05:32:38 +08:00
|
|
|
{
|
2013-07-01 06:38:31 +08:00
|
|
|
struct sigaction act;
|
|
|
|
sigemptyset(& act.sa_mask);
|
|
|
|
act.sa_flags=0;
|
|
|
|
act.sa_handler=SIG_DFL;
|
|
|
|
sigaction(SIGINT, &act, 0);
|
|
|
|
|
2013-06-16 05:32:38 +08:00
|
|
|
std::vector<char> txt;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
char buff[256];
|
2015-09-22 02:24:49 +08:00
|
|
|
ssize_t amt = read_loop(streams.stdin_fd, buff, sizeof buff);
|
2013-06-16 05:32:38 +08:00
|
|
|
if (amt <= 0) break;
|
|
|
|
txt.insert(txt.end(), buff, buff + amt);
|
|
|
|
}
|
|
|
|
if (! txt.empty())
|
|
|
|
{
|
|
|
|
const wcstring src = str2wcstring(&txt.at(0), txt.size());
|
|
|
|
parse_node_tree_t parse_tree;
|
2013-06-16 06:21:35 +08:00
|
|
|
parse_error_list_t errors;
|
2016-11-05 09:40:22 +08:00
|
|
|
bool success = parse_tree_from_string(src, parse_flag_include_comments, &parse_tree,
|
|
|
|
&errors);
|
2013-06-16 06:21:35 +08:00
|
|
|
if (! success)
|
|
|
|
{
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(L"Parsing failed:\n");
|
2013-06-16 06:21:35 +08:00
|
|
|
for (size_t i=0; i < errors.size(); i++)
|
|
|
|
{
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(errors.at(i).describe(src));
|
|
|
|
streams.out.push_back(L'\n');
|
2013-06-16 06:21:35 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(L"(Reparsed with continue after error)\n");
|
2013-12-09 06:13:23 +08:00
|
|
|
parse_tree.clear();
|
|
|
|
errors.clear();
|
2016-11-05 09:40:22 +08:00
|
|
|
parse_tree_from_string(src, parse_flag_continue_after_error |
|
|
|
|
parse_flag_include_comments, &parse_tree, &errors);
|
2013-12-09 06:13:23 +08:00
|
|
|
}
|
|
|
|
const wcstring dump = parse_dump_tree(parse_tree, src);
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(dump);
|
2013-06-16 05:32:38 +08:00
|
|
|
}
|
|
|
|
return STATUS_BUILTIN_OK;
|
|
|
|
}
|
2016-04-04 10:02:46 +08:00
|
|
|
#endif
|
2012-06-05 12:24:42 +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) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_ARG_COUNT1, argv[0], 0, builtin_count_args(argv));
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2014-09-30 04:26:28 +08:00
|
|
|
return STATUS_BUILTIN_OK;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
streams.err.append_format(BUILTIN_ERR_ARG_COUNT1, argv[0], 0, builtin_count_args(argv));
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
2014-09-30 04:26:28 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-14 08:14:50 +08:00
|
|
|
/// An implementation of the external realpath command that doesn't support any options. It's meant
|
|
|
|
/// to be used only by scripts which need to be portable. In general scripts shouldn't invoke this
|
|
|
|
/// directly. They should just use `realpath` which will fallback to this builtin if an external
|
|
|
|
/// command cannot be found. This behaves like the external `realpath --canonicalize-existing`;
|
|
|
|
/// that is, it requires all path components, including the final, to exist.
|
2016-10-04 08:51:27 +08:00
|
|
|
int builtin_realpath(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2016-04-14 08:14:50 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
|
|
|
|
|
|
|
if (argc != 2) {
|
2016-08-17 06:30:49 +08:00
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
2016-04-14 08:14:50 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t *real_path = wrealpath(argv[1], NULL);
|
|
|
|
if (real_path) {
|
|
|
|
streams.out.append(real_path);
|
|
|
|
free((void *)real_path);
|
|
|
|
} else {
|
2016-10-04 08:51:27 +08:00
|
|
|
// We don't actually know why it failed. We should check errno.
|
2016-04-14 08:14:50 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Invalid path: %ls\n"), argv[0], argv[1]);
|
|
|
|
return STATUS_BUILTIN_ERROR;
|
|
|
|
}
|
|
|
|
streams.out.append(L"\n");
|
|
|
|
return STATUS_BUILTIN_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
// END OF BUILTIN COMMANDS
|
|
|
|
// Below are functions for handling the builtin commands.
|
|
|
|
// 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.
|
|
|
|
// NOTE: These must be kept in sorted order!
|
|
|
|
static const builtin_data_t builtin_datas[] = {
|
|
|
|
{L"[", &builtin_test, N_(L"Test a condition")},
|
2015-05-03 12:35:49 +08:00
|
|
|
#if 0
|
2016-04-04 10:02:46 +08:00
|
|
|
// Disabled for the 2.2.0 release: https://github.com/fish-shell/fish-shell/issues/1809.
|
2016-08-17 09:40:01 +08:00
|
|
|
{ L"__fish_parse", &builtin_parse, N_(L"Try out the new parser") },
|
2015-05-03 12:35:49 +08:00
|
|
|
#endif
|
2016-04-19 10:25:12 +08:00
|
|
|
{L"and", &builtin_generic, N_(L"Execute command if previous command suceeded")},
|
|
|
|
{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")},
|
|
|
|
{L"breakpoint", &builtin_breakpoint,
|
|
|
|
N_(L"Temporarily halt execution of a script and launch an interactive debug prompt")},
|
|
|
|
{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")},
|
|
|
|
{L"continue", &builtin_break_continue,
|
|
|
|
N_(L"Skip the rest of the current lap of the innermost loop")},
|
|
|
|
{L"count", &builtin_count, N_(L"Count the number of arguments")},
|
|
|
|
{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")},
|
|
|
|
{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")},
|
|
|
|
{L"while", &builtin_generic, N_(L"Perform a command multiple times")}};
|
2012-02-01 11:47:56 +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) {
|
2012-02-01 11:47:56 +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);
|
2016-04-19 10:25:12 +08:00
|
|
|
if (found != array_end && name == found->name) {
|
2012-02-01 11:47:56 +08:00
|
|
|
return found;
|
|
|
|
}
|
2016-05-05 06:19:47 +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() {
|
|
|
|
for (size_t i = 0; i < BUILTIN_COUNT; i++) {
|
2012-11-19 08:30:30 +08:00
|
|
|
intern_static(builtin_datas[i].name);
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:30:49 +08:00
|
|
|
/// Destroy builtin data.
|
2016-04-19 10:25:12 +08:00
|
|
|
void builtin_destroy() {}
|
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
|
|
|
|
2016-08-17 06:30:49 +08:00
|
|
|
/// If builtin takes care of printing help itself
|
2017-04-05 12:28:57 +08:00
|
|
|
static const wcstring_list_t help_builtins({L"for", L"while", L"function", L"if", L"end", L"switch",
|
|
|
|
L"case", L"count", L"printf"});
|
2016-08-17 06:30:49 +08:00
|
|
|
static bool builtin_handles_help(const wchar_t *cmd) {
|
2012-11-19 08:30:30 +08:00
|
|
|
CHECK(cmd, 0);
|
2017-04-05 12:28:57 +08:00
|
|
|
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
|
2016-04-19 10:25:12 +08:00
|
|
|
int builtin_run(parser_t &parser, const wchar_t *const *argv, io_streams_t &streams) {
|
2016-11-02 10:12:14 +08:00
|
|
|
UNUSED(parser);
|
|
|
|
UNUSED(streams);
|
2016-08-17 06:30:49 +08:00
|
|
|
if (argv == NULL || argv[0] == NULL) return STATUS_BUILTIN_ERROR;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2012-02-01 11:47:56 +08:00
|
|
|
const builtin_data_t *data = builtin_lookup(argv[0]);
|
2016-10-23 02:21:13 +08:00
|
|
|
if (argv[1] != NULL && !builtin_handles_help(argv[0]) && argv[2] == NULL &&
|
|
|
|
parse_util_argument_is_help(argv[1], 0)) {
|
|
|
|
builtin_print_help(parser, streams, argv[0], streams.out);
|
|
|
|
return STATUS_BUILTIN_OK;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 10:25:12 +08:00
|
|
|
if (data != NULL) {
|
2016-11-02 10:12:14 +08:00
|
|
|
// Warning: layering violation and naughty cast. The code originally had a much more
|
|
|
|
// complicated solution to achieve exactly the same result: lie about the constness of argv.
|
|
|
|
// Some of the builtins we call do mutate the array via their calls to wgetopt() which could
|
|
|
|
// result in the pointers being reordered. This is harmless because we only get called once
|
|
|
|
// with a given argv array and nothing else will look at the contents of the array after we
|
|
|
|
// return.
|
|
|
|
return data->func(parser, streams, (wchar_t **)argv);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
|
|
|
|
debug(0, UNKNOWN_BUILTIN_ERR_MSG, argv[0]);
|
2012-11-19 08:30:30 +08:00
|
|
|
return STATUS_BUILTIN_ERROR;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:30:49 +08:00
|
|
|
/// Returns a list of all builtin names.
|
2016-04-19 10:25:12 +08:00
|
|
|
wcstring_list_t builtin_get_names(void) {
|
2012-02-01 11:47:56 +08:00
|
|
|
wcstring_list_t result;
|
|
|
|
result.reserve(BUILTIN_COUNT);
|
2016-04-19 10:25:12 +08:00
|
|
|
for (size_t i = 0; i < BUILTIN_COUNT; i++) {
|
2012-02-01 11:47:56 +08:00
|
|
|
result.push_back(builtin_datas[i].name);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
list->reserve(list->size() + BUILTIN_COUNT);
|
2016-04-19 10:25:12 +08:00
|
|
|
for (size_t i = 0; i < BUILTIN_COUNT; i++) {
|
2014-01-08 06:57:58 +08:00
|
|
|
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.
|
2016-04-19 10:25:12 +08:00
|
|
|
wcstring builtin_get_desc(const wcstring &name) {
|
2012-05-18 10:37:46 +08:00
|
|
|
wcstring 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) {
|
2012-05-18 10:37:46 +08:00
|
|
|
result = _(builtin->desc);
|
|
|
|
}
|
|
|
|
return result;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|