2016-05-03 08:22:44 +08:00
|
|
|
// The fish parser. Contains functions for parsing and evaluating code.
|
2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <wchar.h>
|
2017-02-14 12:37:27 +08:00
|
|
|
|
2012-02-28 23:50:09 +08:00
|
|
|
#include <algorithm>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <memory>
|
2018-02-19 10:39:03 +08:00
|
|
|
#include <utility>
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "env.h"
|
2005-10-06 06:37:08 +08:00
|
|
|
#include "event.h"
|
2016-05-03 08:22:44 +08:00
|
|
|
#include "expand.h"
|
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "function.h"
|
2006-02-02 23:23:56 +08:00
|
|
|
#include "intern.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "parse_constants.h"
|
2016-05-03 08:22:44 +08:00
|
|
|
#include "parse_execution.h"
|
|
|
|
#include "parse_util.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "reader.h"
|
|
|
|
#include "sanity.h"
|
2018-01-21 03:58:57 +08:00
|
|
|
#include "tnode.h"
|
2016-05-03 08:22:44 +08:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2016-04-21 14:00:54 +08:00
|
|
|
|
|
|
|
class io_chain_t;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Error for evaluating in illegal scope.
|
|
|
|
#define INVALID_SCOPE_ERR_MSG _(L"Tried to evaluate commands using invalid block type '%ls'")
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// While block description.
|
|
|
|
#define WHILE_BLOCK N_(L"'while' block")
|
|
|
|
|
|
|
|
/// For block description.
|
|
|
|
#define FOR_BLOCK N_(L"'for' block")
|
|
|
|
|
|
|
|
/// Breakpoint block.
|
2016-06-15 10:55:30 +08:00
|
|
|
#define BREAKPOINT_BLOCK N_(L"block created by breakpoint")
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// If block description.
|
|
|
|
#define IF_BLOCK N_(L"'if' conditional block")
|
|
|
|
|
|
|
|
/// Function invocation block description.
|
|
|
|
#define FUNCTION_CALL_BLOCK N_(L"function invocation block")
|
|
|
|
|
|
|
|
/// Function invocation block description.
|
|
|
|
#define FUNCTION_CALL_NO_SHADOW_BLOCK N_(L"function invocation block with no variable shadowing")
|
|
|
|
|
|
|
|
/// Switch block description.
|
|
|
|
#define SWITCH_BLOCK N_(L"'switch' block")
|
|
|
|
|
|
|
|
/// Top block description.
|
|
|
|
#define TOP_BLOCK N_(L"global root block")
|
|
|
|
|
|
|
|
/// Command substitution block description.
|
|
|
|
#define SUBST_BLOCK N_(L"command substitution block")
|
|
|
|
|
|
|
|
/// Begin block description.
|
|
|
|
#define BEGIN_BLOCK N_(L"'begin' unconditional block")
|
|
|
|
|
|
|
|
/// Source block description.
|
2016-06-15 10:55:30 +08:00
|
|
|
#define SOURCE_BLOCK N_(L"block created by the . builtin")
|
2016-05-03 08:22:44 +08:00
|
|
|
|
|
|
|
/// Source block description.
|
|
|
|
#define EVENT_BLOCK N_(L"event handler block")
|
|
|
|
|
|
|
|
/// Unknown block description.
|
|
|
|
#define UNKNOWN_BLOCK N_(L"unknown/invalid block")
|
|
|
|
|
2016-05-29 13:28:26 +08:00
|
|
|
/// Data structure to describe a block type, like while blocks, command substitution blocks, etc.
|
2016-05-03 08:22:44 +08:00
|
|
|
struct block_lookup_entry {
|
|
|
|
// The block type id. The legal values are defined in parser.h.
|
|
|
|
block_type_t type;
|
|
|
|
// The name of the builtin that creates this type of block, if any.
|
|
|
|
const wchar_t *name;
|
|
|
|
// A description of this block type.
|
2012-11-19 08:30:30 +08:00
|
|
|
const wchar_t *desc;
|
2012-01-23 13:57:30 +08:00
|
|
|
};
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// List of all legal block types.
|
|
|
|
static const struct block_lookup_entry block_lookup[] = {
|
|
|
|
{WHILE, L"while", WHILE_BLOCK},
|
|
|
|
{FOR, L"for", FOR_BLOCK},
|
|
|
|
{IF, L"if", IF_BLOCK},
|
|
|
|
{FUNCTION_CALL, 0, FUNCTION_CALL_BLOCK},
|
|
|
|
{FUNCTION_CALL_NO_SHADOW, 0, FUNCTION_CALL_NO_SHADOW_BLOCK},
|
|
|
|
{SWITCH, L"switch", SWITCH_BLOCK},
|
|
|
|
{TOP, 0, TOP_BLOCK},
|
|
|
|
{SUBST, 0, SUBST_BLOCK},
|
|
|
|
{BEGIN, L"begin", BEGIN_BLOCK},
|
2017-08-08 09:29:33 +08:00
|
|
|
{SOURCE, L"source", SOURCE_BLOCK},
|
2016-05-03 08:22:44 +08:00
|
|
|
{EVENT, 0, EVENT_BLOCK},
|
|
|
|
{BREAKPOINT, L"breakpoint", BREAKPOINT_BLOCK},
|
|
|
|
{(block_type_t)0, 0, 0}};
|
|
|
|
|
2013-12-17 07:33:20 +08:00
|
|
|
// Given a file path, return something nicer. Currently we just "unexpand" tildes.
|
2016-05-03 08:22:44 +08:00
|
|
|
static wcstring user_presentable_path(const wcstring &path) {
|
2013-12-17 07:33:20 +08:00
|
|
|
return replace_home_directory_with_tilde(path);
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
parser_t::parser_t() : cancellation_requested(false), is_within_fish_initialization(false) {}
|
2013-12-17 07:33:20 +08:00
|
|
|
|
2017-01-22 06:53:10 +08:00
|
|
|
// Out of line destructor to enable forward declaration of parse_execution_context_t
|
2018-02-19 10:44:58 +08:00
|
|
|
parser_t::~parser_t() = default;
|
2017-01-22 06:15:03 +08:00
|
|
|
|
|
|
|
static parser_t s_principal_parser;
|
2012-06-05 05:20:01 +08:00
|
|
|
|
2018-02-19 10:33:04 +08:00
|
|
|
parser_t &parser_t::principal_parser() {
|
2012-02-28 10:43:24 +08:00
|
|
|
ASSERT_IS_NOT_FORKED_CHILD();
|
2012-01-23 13:40:08 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2017-01-22 06:15:03 +08:00
|
|
|
return s_principal_parser;
|
2012-01-23 13:40:08 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
void parser_t::set_is_within_fish_initialization(bool flag) {
|
2014-02-21 02:57:13 +08:00
|
|
|
is_within_fish_initialization = flag;
|
|
|
|
}
|
|
|
|
|
2018-02-19 10:33:04 +08:00
|
|
|
void parser_t::skip_all_blocks() {
|
2016-05-03 08:22:44 +08:00
|
|
|
// Tell all blocks to skip.
|
2017-01-22 06:15:03 +08:00
|
|
|
// This may be called from a signal handler!
|
|
|
|
s_principal_parser.cancellation_requested = true;
|
2012-06-05 05:20:01 +08:00
|
|
|
}
|
|
|
|
|
2017-01-22 07:35:35 +08:00
|
|
|
// Given a new-allocated block, push it onto our block stack, acquiring ownership
|
|
|
|
void parser_t::push_block_int(block_t *new_current) {
|
2013-12-21 09:41:21 +08:00
|
|
|
const enum block_type_t type = new_current->type();
|
|
|
|
new_current->src_lineno = parser_t::get_lineno();
|
2014-03-17 07:45:00 +08:00
|
|
|
|
|
|
|
const wchar_t *filename = parser_t::current_filename();
|
2016-05-03 08:22:44 +08:00
|
|
|
if (filename != NULL) {
|
2014-03-17 07:45:00 +08:00
|
|
|
new_current->src_filename = intern(filename);
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-22 06:15:03 +08:00
|
|
|
// New blocks should be skipped if the outer block is skipped, except TOP and SUBST block, which
|
|
|
|
// open up new environments.
|
2013-12-21 09:41:21 +08:00
|
|
|
const block_t *old_current = this->current_block();
|
2017-01-22 06:15:03 +08:00
|
|
|
new_current->skip = old_current && old_current->skip;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// Type TOP and SUBST are never skipped.
|
|
|
|
if (type == TOP || type == SUBST) {
|
2017-01-22 06:15:03 +08:00
|
|
|
new_current->skip = false;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-22 06:15:03 +08:00
|
|
|
new_current->job = nullptr;
|
2016-05-03 08:22:44 +08:00
|
|
|
new_current->loop_status = LOOP_NORMAL;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2017-01-22 07:35:35 +08:00
|
|
|
// Push it onto our stack. This acquires ownership because of unique_ptr.
|
|
|
|
this->block_stack.emplace_back(new_current);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-06-20 12:05:34 +08:00
|
|
|
// Types TOP and SUBST are not considered blocks for the purposes of `status is-block`.
|
2016-05-03 08:22:44 +08:00
|
|
|
if (type != TOP && type != SUBST) {
|
2017-06-20 12:05:34 +08:00
|
|
|
is_block = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == BREAKPOINT) {
|
|
|
|
is_breakpoint = true;
|
2014-10-01 11:58:45 +08:00
|
|
|
}
|
|
|
|
|
2017-01-22 05:57:05 +08:00
|
|
|
if (new_current->type() != TOP) {
|
2012-11-19 08:30:30 +08:00
|
|
|
env_push(type == FUNCTION_CALL);
|
2013-12-21 09:41:21 +08:00
|
|
|
new_current->wants_pop_env = true;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2017-01-22 07:35:35 +08:00
|
|
|
void parser_t::pop_block(const block_t *expected) {
|
|
|
|
assert(expected == this->current_block());
|
2016-05-03 08:22:44 +08:00
|
|
|
if (block_stack.empty()) {
|
|
|
|
debug(1, L"function %s called on empty block stack.", __func__);
|
2012-11-19 08:30:30 +08:00
|
|
|
bugreport();
|
|
|
|
return;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-22 07:35:35 +08:00
|
|
|
// acquire ownership out of the block stack
|
|
|
|
// this will trigger deletion when it goes out of scope
|
|
|
|
std::unique_ptr<block_t> old = std::move(block_stack.back());
|
2013-12-21 09:41:21 +08:00
|
|
|
block_stack.pop_back();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
if (old->wants_pop_env) env_pop();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-06-20 12:05:34 +08:00
|
|
|
// Figure out if `status is-block` should consider us to be in a block now.
|
|
|
|
bool new_is_block = false;
|
2017-01-22 07:35:35 +08:00
|
|
|
for (const auto &b : block_stack) {
|
|
|
|
const enum block_type_t type = b->type();
|
2016-05-03 08:22:44 +08:00
|
|
|
if (type != TOP && type != SUBST) {
|
2017-06-20 12:05:34 +08:00
|
|
|
new_is_block = true;
|
2014-10-01 11:58:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
is_block = new_is_block;
|
2017-06-20 12:05:34 +08:00
|
|
|
|
|
|
|
// Are we still in a breakpoint?
|
|
|
|
bool new_is_breakpoint = false;
|
|
|
|
for (const auto &b : block_stack) {
|
|
|
|
const enum block_type_t type = b->type();
|
|
|
|
if (type == BREAKPOINT) {
|
|
|
|
new_is_breakpoint = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
is_breakpoint = new_is_breakpoint;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
const wchar_t *parser_t::get_block_desc(int block) const {
|
|
|
|
for (size_t i = 0; block_lookup[i].desc; i++) {
|
|
|
|
if (block_lookup[i].type == block) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return _(block_lookup[i].desc);
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
return _(UNKNOWN_BLOCK);
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-29 13:28:26 +08:00
|
|
|
#if 0
|
|
|
|
// TODO: Lint says this isn't used (which is true). Should this be removed?
|
2016-05-03 08:22:44 +08:00
|
|
|
wcstring parser_t::block_stack_description() const {
|
2014-03-17 07:45:00 +08:00
|
|
|
wcstring result;
|
|
|
|
size_t idx = this->block_count();
|
|
|
|
size_t spaces = 0;
|
2016-05-03 08:22:44 +08:00
|
|
|
while (idx--) {
|
|
|
|
if (spaces > 0) {
|
2014-03-17 07:45:00 +08:00
|
|
|
result.push_back(L'\n');
|
|
|
|
}
|
2016-05-03 08:22:44 +08:00
|
|
|
for (size_t j = 0; j < spaces; j++) {
|
2014-03-17 07:45:00 +08:00
|
|
|
result.push_back(L' ');
|
|
|
|
}
|
|
|
|
result.append(this->block_at_index(idx)->description());
|
|
|
|
spaces++;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2016-05-29 13:28:26 +08:00
|
|
|
#endif
|
2014-03-17 07:45:00 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
const block_t *parser_t::block_at_index(size_t idx) const {
|
|
|
|
// Zero corresponds to the last element in our vector.
|
2013-12-21 09:41:21 +08:00
|
|
|
size_t count = block_stack.size();
|
2017-01-22 07:35:35 +08:00
|
|
|
return idx < count ? block_stack.at(count - idx - 1).get() : NULL;
|
2013-12-21 09:41:21 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
block_t *parser_t::block_at_index(size_t idx) {
|
2013-12-21 09:41:21 +08:00
|
|
|
size_t count = block_stack.size();
|
2017-01-22 07:35:35 +08:00
|
|
|
return idx < count ? block_stack.at(count - idx - 1).get() : NULL;
|
2013-12-21 09:41:21 +08:00
|
|
|
}
|
|
|
|
|
2017-01-22 07:35:35 +08:00
|
|
|
block_t *parser_t::current_block() { return block_stack.empty() ? NULL : block_stack.back().get(); }
|
2013-12-21 09:41:21 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
void parser_t::forbid_function(const wcstring &function) { forbidden_function.push_back(function); }
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
void parser_t::allow_function() { forbidden_function.pop_back(); }
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Print profiling information to the specified stream.
|
2017-01-22 06:33:17 +08:00
|
|
|
static void print_profile(const std::vector<std::unique_ptr<profile_item_t>> &items, FILE *out) {
|
2016-05-03 08:22:44 +08:00
|
|
|
for (size_t pos = 0; pos < items.size(); pos++) {
|
2013-01-14 07:49:32 +08:00
|
|
|
const profile_item_t *me, *prev;
|
|
|
|
size_t i;
|
|
|
|
int my_time;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-22 06:33:17 +08:00
|
|
|
me = items.at(pos).get();
|
2016-10-31 05:49:22 +08:00
|
|
|
if (me->skipped) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
my_time = me->parse + me->exec;
|
|
|
|
for (i = pos + 1; i < items.size(); i++) {
|
2017-01-22 06:33:17 +08:00
|
|
|
prev = items.at(i).get();
|
2016-10-31 05:49:22 +08:00
|
|
|
if (prev->skipped) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (prev->level <= me->level) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (prev->level > me->level + 1) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-10-31 05:49:22 +08:00
|
|
|
my_time -= prev->parse + prev->exec;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-10-31 05:49:22 +08:00
|
|
|
if (me->cmd.size() == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-10-31 05:49:22 +08:00
|
|
|
if (fwprintf(out, L"%d\t%d\t", my_time, me->parse + me->exec) < 0) {
|
|
|
|
wperror(L"fwprintf");
|
|
|
|
return;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-10-31 05:49:22 +08:00
|
|
|
for (i = 0; i < me->level; i++) {
|
|
|
|
if (fwprintf(out, L"-") < 0) {
|
|
|
|
wperror(L"fwprintf");
|
|
|
|
return;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-10-31 05:49:22 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-10-31 05:49:22 +08:00
|
|
|
if (fwprintf(out, L"> %ls\n", me->cmd.c_str()) < 0) {
|
|
|
|
wperror(L"fwprintf");
|
|
|
|
return;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
void parser_t::emit_profiling(const char *path) const {
|
|
|
|
// Save profiling information. OK to not use CLO_EXEC here because this is called while fish is
|
|
|
|
// dying (and hence will not fork).
|
2014-02-10 06:04:43 +08:00
|
|
|
FILE *f = fopen(path, "w");
|
2016-05-03 08:22:44 +08:00
|
|
|
if (!f) {
|
|
|
|
debug(1, _(L"Could not write profiling information to file '%s'"), path);
|
|
|
|
} else {
|
|
|
|
if (fwprintf(f, _(L"Time\tSum\tCommand\n"), profile_items.size()) < 0) {
|
2014-02-10 06:04:43 +08:00
|
|
|
wperror(L"fwprintf");
|
2016-05-03 08:22:44 +08:00
|
|
|
} else {
|
2014-02-10 06:04:43 +08:00
|
|
|
print_profile(profile_items, f);
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
if (fclose(f)) {
|
2014-02-10 06:04:43 +08:00
|
|
|
wperror(L"fclose");
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
void parser_t::expand_argument_list(const wcstring &arg_list_src, expand_flags_t eflags,
|
|
|
|
std::vector<completion_t> *output_arg_list) {
|
2015-07-28 09:45:47 +08:00
|
|
|
assert(output_arg_list != NULL);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// Parse the string as an argument list.
|
2014-03-17 23:45:25 +08:00
|
|
|
parse_node_tree_t tree;
|
2016-05-03 08:22:44 +08:00
|
|
|
if (!parse_tree_from_string(arg_list_src, parse_flag_none, &tree, NULL /* errors */,
|
|
|
|
symbol_freestanding_argument_list)) {
|
|
|
|
// Failed to parse. Here we expect to have reported any errors in test_args.
|
2014-03-17 23:45:25 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-01-13 03:36:45 +08:00
|
|
|
// Get the root argument list and extract arguments from it.
|
2016-10-24 04:58:12 +08:00
|
|
|
assert(!tree.empty()); //!OCLINT(multiple unary operator)
|
2018-01-13 03:36:45 +08:00
|
|
|
tnode_t<grammar::freestanding_argument_list> arg_list(&tree, &tree.at(0));
|
|
|
|
while (auto arg = arg_list.next_in_list<grammar::argument>()) {
|
|
|
|
const wcstring arg_src = arg.get_source(arg_list_src);
|
|
|
|
if (expand_string(arg_src, output_arg_list, eflags, NULL) == EXPAND_ERROR) {
|
|
|
|
break; // failed to expand a string
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
wcstring parser_t::stack_trace() const {
|
2015-09-22 02:24:49 +08:00
|
|
|
wcstring trace;
|
|
|
|
this->stack_trace_internal(0, &trace);
|
|
|
|
return trace;
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
void parser_t::stack_trace_internal(size_t block_idx, wcstring *buff) const {
|
|
|
|
// Check if we should end the recursion.
|
|
|
|
if (block_idx >= this->block_count()) return;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2013-12-21 09:41:21 +08:00
|
|
|
const block_t *b = this->block_at_index(block_idx);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
if (b->type() == EVENT) {
|
|
|
|
// This is an event handler.
|
2012-08-27 13:42:29 +08:00
|
|
|
const event_block_t *eb = static_cast<const event_block_t *>(b);
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring description = event_get_desc(eb->event);
|
2015-09-22 02:24:49 +08:00
|
|
|
append_format(*buff, _(L"in event handler: %ls\n"), description.c_str());
|
|
|
|
buff->append(L"\n");
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// Stop recursing at event handler. No reason to believe that any other code is relevant.
|
|
|
|
//
|
|
|
|
// It might make sense in the future to continue printing the stack trace of the code that
|
|
|
|
// invoked the event, if this is a programmatic event, but we can't currently detect that.
|
2012-11-19 08:30:30 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
if (b->type() == FUNCTION_CALL || b->type() == FUNCTION_CALL_NO_SHADOW || b->type() == SOURCE ||
|
|
|
|
b->type() == SUBST) {
|
|
|
|
// These types of blocks should be printed.
|
2012-11-19 08:30:30 +08:00
|
|
|
int i;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
switch (b->type()) {
|
|
|
|
case SOURCE: {
|
|
|
|
const source_block_t *sb = static_cast<const source_block_t *>(b);
|
2012-11-19 16:31:03 +08:00
|
|
|
const wchar_t *source_dest = sb->source_file;
|
2016-05-03 08:22:44 +08:00
|
|
|
append_format(*buff, _(L"from sourcing file %ls\n"),
|
|
|
|
user_presentable_path(source_dest).c_str());
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FUNCTION_CALL:
|
2016-05-03 08:22:44 +08:00
|
|
|
case FUNCTION_CALL_NO_SHADOW: {
|
|
|
|
const function_block_t *fb = static_cast<const function_block_t *>(b);
|
2015-09-22 02:24:49 +08:00
|
|
|
append_format(*buff, _(L"in function '%ls'\n"), fb->name.c_str());
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-03 08:22:44 +08:00
|
|
|
case SUBST: {
|
2015-09-22 02:24:49 +08:00
|
|
|
append_format(*buff, _(L"in command substitution\n"));
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-03 08:22:44 +08:00
|
|
|
default: {
|
|
|
|
break; // can't get here
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
const wchar_t *file = b->src_filename;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
if (file) {
|
|
|
|
append_format(*buff, _(L"\tcalled on line %d of file %ls\n"), b->src_lineno,
|
2013-12-17 07:33:20 +08:00
|
|
|
user_presentable_path(file).c_str());
|
2016-05-03 08:22:44 +08:00
|
|
|
} else if (is_within_fish_initialization) {
|
2015-09-22 02:24:49 +08:00
|
|
|
append_format(*buff, _(L"\tcalled during startup\n"));
|
2016-05-03 08:22:44 +08:00
|
|
|
} else {
|
2015-09-22 02:24:49 +08:00
|
|
|
append_format(*buff, _(L"\tcalled on standard input\n"));
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
if (b->type() == FUNCTION_CALL) {
|
2012-08-27 13:42:29 +08:00
|
|
|
const function_block_t *fb = static_cast<const function_block_t *>(b);
|
2016-05-03 08:22:44 +08:00
|
|
|
const process_t *const process = fb->process;
|
|
|
|
if (process->argv(1)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring tmp;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
for (i = 1; process->argv(i); i++) {
|
|
|
|
if (i > 1) tmp.push_back(L' ');
|
2012-02-23 04:00:02 +08:00
|
|
|
tmp.append(process->argv(i));
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2015-09-22 02:24:49 +08:00
|
|
|
append_format(*buff, _(L"\twith parameter list '%ls'\n"), tmp.c_str());
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2015-09-22 02:24:49 +08:00
|
|
|
append_format(*buff, L"\n");
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// Recursively print the next block.
|
2015-09-22 02:24:49 +08:00
|
|
|
parser_t::stack_trace_internal(block_idx + 1, buff);
|
2006-01-26 22:48:10 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
/// Returns the name of the currently evaluated function if we are currently evaluating a function,
|
2017-06-24 13:14:21 +08:00
|
|
|
/// NULL otherwise. This is tested by moving down the block-scope-stack, checking every block if it
|
|
|
|
/// is of type FUNCTION_CALL. If the caller doesn't specify a starting position in the stack we
|
|
|
|
/// begin with the current block.
|
|
|
|
const wchar_t *parser_t::is_function(size_t idx) const {
|
2016-05-03 08:22:44 +08:00
|
|
|
// PCA: Have to make this a string somehow.
|
2012-02-08 09:06:45 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2013-12-21 09:41:21 +08:00
|
|
|
const wchar_t *result = NULL;
|
2017-06-24 13:14:21 +08:00
|
|
|
for (size_t block_idx = idx; block_idx < this->block_count(); block_idx++) {
|
2013-12-21 09:41:21 +08:00
|
|
|
const block_t *b = this->block_at_index(block_idx);
|
2016-05-03 08:22:44 +08:00
|
|
|
if (b->type() == FUNCTION_CALL || b->type() == FUNCTION_CALL_NO_SHADOW) {
|
2012-08-27 13:42:29 +08:00
|
|
|
const function_block_t *fb = static_cast<const function_block_t *>(b);
|
2013-12-21 09:41:21 +08:00
|
|
|
result = fb->name.c_str();
|
|
|
|
break;
|
2016-05-03 08:22:44 +08:00
|
|
|
} else if (b->type() == SOURCE) {
|
|
|
|
// If a function sources a file, obviously that function's offset doesn't contribute.
|
2014-03-17 07:45:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2013-12-21 09:41:21 +08:00
|
|
|
return result;
|
2006-01-26 22:48:10 +08:00
|
|
|
}
|
|
|
|
|
2017-06-24 13:14:21 +08:00
|
|
|
/// Return the function name for the specified stack frame. Default is zero (current frame).
|
|
|
|
/// The special value zero means the function frame immediately above the closest breakpoint frame.
|
|
|
|
const wchar_t *parser_t::get_function_name(int level) {
|
|
|
|
if (level == 0) {
|
|
|
|
// Return the function name for the level preceding the most recent breakpoint. If there
|
|
|
|
// isn't one return the function name for the current level.
|
|
|
|
int idx = 0;
|
|
|
|
for (const auto &b : block_stack) {
|
|
|
|
const enum block_type_t type = b->type();
|
|
|
|
if (type == BREAKPOINT) {
|
|
|
|
return this->is_function(idx);
|
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
return NULL; // couldn't find a breakpoint frame
|
|
|
|
} else if (level == 1) {
|
|
|
|
// Return the function name for the current level.
|
|
|
|
return this->is_function();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the function name for the specific function stack frame.
|
|
|
|
int idx = 0;
|
|
|
|
for (const auto &b : block_stack) {
|
|
|
|
const enum block_type_t type = b->type();
|
|
|
|
if (type == FUNCTION_CALL || type == FUNCTION_CALL_NO_SHADOW) {
|
|
|
|
if (--level == 0) return this->is_function(idx);
|
|
|
|
}
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
return NULL; // couldn't find that function level
|
|
|
|
}
|
2017-04-13 06:34:25 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
int parser_t::get_lineno() const {
|
2014-03-03 05:11:17 +08:00
|
|
|
int lineno = -1;
|
2018-02-12 14:00:17 +08:00
|
|
|
if (execution_context) {
|
|
|
|
lineno = execution_context->get_current_line_number();
|
2014-03-17 07:45:00 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// If we are executing a function, we have to add in its offset.
|
2014-03-17 07:45:00 +08:00
|
|
|
const wchar_t *function_name = is_function();
|
2016-05-03 08:22:44 +08:00
|
|
|
if (function_name != NULL) {
|
2018-02-10 13:53:06 +08:00
|
|
|
lineno += function_get_definition_lineno(function_name);
|
2014-03-17 07:45:00 +08:00
|
|
|
}
|
2014-03-02 08:04:13 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
return lineno;
|
2006-01-26 22:48:10 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
const wchar_t *parser_t::current_filename() const {
|
2012-02-03 07:05:08 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
for (size_t i = 0; i < this->block_count(); i++) {
|
2013-12-21 09:41:21 +08:00
|
|
|
const block_t *b = this->block_at_index(i);
|
2016-05-03 08:22:44 +08:00
|
|
|
if (b->type() == FUNCTION_CALL || b->type() == FUNCTION_CALL_NO_SHADOW) {
|
2012-08-27 13:42:29 +08:00
|
|
|
const function_block_t *fb = static_cast<const function_block_t *>(b);
|
2012-11-19 08:30:30 +08:00
|
|
|
return function_get_definition_file(fb->name);
|
2016-05-03 08:22:44 +08:00
|
|
|
} else if (b->type() == SOURCE) {
|
2014-03-17 07:45:00 +08:00
|
|
|
const source_block_t *sb = static_cast<const source_block_t *>(b);
|
|
|
|
return sb->source_file;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// We query a global array for the current file name, but only do that if we are the principal
|
|
|
|
// parser.
|
|
|
|
if (this == &principal_parser()) {
|
2014-01-02 07:29:56 +08:00
|
|
|
return reader_current_filename();
|
|
|
|
}
|
|
|
|
return NULL;
|
2006-01-26 22:48:10 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
wcstring parser_t::current_line() {
|
2018-02-12 14:00:17 +08:00
|
|
|
if (!execution_context) {
|
2014-03-17 13:06:32 +08:00
|
|
|
return wcstring();
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2018-02-12 14:00:17 +08:00
|
|
|
int source_offset = execution_context->get_current_source_offset();
|
2016-05-03 08:22:44 +08:00
|
|
|
if (source_offset < 0) {
|
2014-03-17 13:06:32 +08:00
|
|
|
return wcstring();
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2014-03-17 13:06:32 +08:00
|
|
|
const int lineno = this->get_lineno();
|
|
|
|
const wchar_t *file = this->current_filename();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2014-03-17 13:06:32 +08:00
|
|
|
wcstring prefix;
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// If we are not going to print a stack trace, at least print the line number and filename.
|
2016-05-15 11:35:54 +08:00
|
|
|
if (!shell_is_interactive() || is_function()) {
|
2016-05-03 08:22:44 +08:00
|
|
|
if (file) {
|
|
|
|
append_format(prefix, _(L"%ls (line %d): "), user_presentable_path(file).c_str(),
|
|
|
|
lineno);
|
|
|
|
} else if (is_within_fish_initialization) {
|
2017-04-25 12:05:51 +08:00
|
|
|
append_format(prefix, L"%ls (line %d): ", _(L"Startup"), lineno);
|
2016-05-03 08:22:44 +08:00
|
|
|
} else {
|
2017-04-25 12:05:51 +08:00
|
|
|
append_format(prefix, L"%ls (line %d): ", _(L"Standard input"), lineno);
|
2014-02-21 02:57:13 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-05-15 11:35:54 +08:00
|
|
|
bool is_interactive = shell_is_interactive();
|
2016-05-03 08:22:44 +08:00
|
|
|
bool skip_caret = is_interactive && !is_function();
|
2014-03-17 13:06:32 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// Use an error with empty text.
|
2014-03-17 13:06:32 +08:00
|
|
|
assert(source_offset >= 0);
|
|
|
|
parse_error_t empty_error = {};
|
|
|
|
empty_error.source_start = source_offset;
|
|
|
|
|
2018-02-12 14:00:17 +08:00
|
|
|
wcstring line_info = empty_error.describe_with_prefix(execution_context->get_source(), prefix,
|
|
|
|
is_interactive, skip_caret);
|
2016-05-03 08:22:44 +08:00
|
|
|
if (!line_info.empty()) {
|
2014-03-17 13:06:32 +08:00
|
|
|
line_info.push_back(L'\n');
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2015-09-22 02:24:49 +08:00
|
|
|
line_info.append(this->stack_trace());
|
2014-03-17 13:06:32 +08:00
|
|
|
return line_info;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2017-01-27 06:47:32 +08:00
|
|
|
void parser_t::job_add(shared_ptr<job_t> job) {
|
2013-12-27 17:38:43 +08:00
|
|
|
assert(job != NULL);
|
2017-01-27 12:00:43 +08:00
|
|
|
assert(!job->processes.empty());
|
2017-01-27 06:47:32 +08:00
|
|
|
this->my_job_list.push_front(std::move(job));
|
2013-12-27 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2016-12-30 08:09:52 +08:00
|
|
|
bool parser_t::job_remove(job_t *job) {
|
2017-01-27 06:47:32 +08:00
|
|
|
for (auto iter = my_job_list.begin(); iter != my_job_list.end(); ++iter) {
|
|
|
|
if (iter->get() == job) {
|
|
|
|
my_job_list.erase(iter);
|
|
|
|
return true;
|
|
|
|
}
|
2012-02-28 10:43:24 +08:00
|
|
|
}
|
2016-11-03 05:42:33 +08:00
|
|
|
|
|
|
|
debug(1, _(L"Job inconsistency"));
|
|
|
|
sanity_lose();
|
|
|
|
return false;
|
2012-02-28 10:43:24 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
void parser_t::job_promote(job_t *job) {
|
2017-01-27 06:47:32 +08:00
|
|
|
job_list_t::iterator loc;
|
|
|
|
for (loc = my_job_list.begin(); loc != my_job_list.end(); ++loc) {
|
|
|
|
if (loc->get() == job) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-02-28 10:43:24 +08:00
|
|
|
assert(loc != my_job_list.end());
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// Move the job to the beginning.
|
2018-11-12 08:57:30 +08:00
|
|
|
my_job_list.splice(my_job_list.begin(), my_job_list, loc);
|
2012-02-28 10:43:24 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
job_t *parser_t::job_get(job_id_t id) {
|
2017-01-27 06:47:32 +08:00
|
|
|
for (const auto &job : my_job_list) {
|
|
|
|
if (id <= 0 || job->job_id == id) return job.get();
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
return NULL;
|
2012-02-28 10:43:24 +08:00
|
|
|
}
|
|
|
|
|
2018-08-10 07:46:11 +08:00
|
|
|
job_t *parser_t::job_get_from_pid(pid_t pid) const {
|
2012-02-28 10:43:24 +08:00
|
|
|
job_iterator_t jobs;
|
|
|
|
job_t *job;
|
2017-04-23 22:04:37 +08:00
|
|
|
|
|
|
|
pid_t pgid = getpgid(pid);
|
|
|
|
|
|
|
|
if (pgid == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
while ((job = jobs.next())) {
|
2017-04-23 22:04:37 +08:00
|
|
|
if (job->pgid == pgid) {
|
|
|
|
for (const process_ptr_t &p : job->processes) {
|
|
|
|
if (p->pid == pid) {
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
return 0;
|
2012-02-28 10:43:24 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
profile_item_t *parser_t::create_profile_item() {
|
2017-01-22 06:33:17 +08:00
|
|
|
profile_item_t *result = nullptr;
|
2016-05-03 08:22:44 +08:00
|
|
|
if (g_profiling_active) {
|
2017-01-22 07:02:41 +08:00
|
|
|
profile_items.push_back(make_unique<profile_item_t>());
|
2017-01-22 06:33:17 +08:00
|
|
|
result = profile_items.back().get();
|
2014-02-10 06:04:43 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-23 06:40:15 +08:00
|
|
|
int parser_t::eval(wcstring cmd, const io_chain_t &io, enum block_type_t block_type) {
|
2016-05-03 08:22:44 +08:00
|
|
|
// Parse the source into a tree, if we can.
|
2014-10-13 11:09:45 +08:00
|
|
|
parse_error_list_t error_list;
|
2017-12-23 06:40:15 +08:00
|
|
|
parsed_source_ref_t ps = parse_source(cmd, parse_flag_none, &error_list);
|
|
|
|
if (!ps) {
|
2016-05-03 08:22:44 +08:00
|
|
|
// Get a backtrace. This includes the message.
|
2016-02-28 10:37:59 +08:00
|
|
|
wcstring backtrace_and_desc;
|
2017-06-18 13:36:56 +08:00
|
|
|
this->get_backtrace(cmd, error_list, backtrace_and_desc);
|
2016-05-03 08:22:44 +08:00
|
|
|
|
|
|
|
// Print it.
|
2017-01-03 13:11:53 +08:00
|
|
|
fwprintf(stderr, L"%ls\n", backtrace_and_desc.c_str());
|
2014-03-01 09:54:05 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2018-02-12 15:13:06 +08:00
|
|
|
this->eval(ps, io, block_type);
|
|
|
|
return 0;
|
2016-02-28 16:33:11 +08:00
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
|
2018-02-12 15:13:06 +08:00
|
|
|
void parser_t::eval(parsed_source_ref_t ps, const io_chain_t &io, enum block_type_t block_type) {
|
2016-02-28 16:33:11 +08:00
|
|
|
CHECK_BLOCK(1);
|
|
|
|
assert(block_type == TOP || block_type == SUBST);
|
2018-02-12 15:13:06 +08:00
|
|
|
if (!ps->tree.empty()) {
|
|
|
|
// Execute the first node.
|
|
|
|
tnode_t<grammar::job_list> start{&ps->tree, &ps->tree.front()};
|
2018-11-04 15:58:44 +08:00
|
|
|
this->eval_node(ps, start, io, block_type, nullptr /* parent */);
|
2016-02-28 16:33:11 +08:00
|
|
|
}
|
2014-03-01 09:54:05 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-02-11 11:16:35 +08:00
|
|
|
template <typename T>
|
2018-02-12 11:34:12 +08:00
|
|
|
int parser_t::eval_node(parsed_source_ref_t ps, tnode_t<T> node, const io_chain_t &io,
|
2018-11-04 15:58:44 +08:00
|
|
|
block_type_t block_type, std::shared_ptr<job_t> parent_job) {
|
2018-02-11 11:16:35 +08:00
|
|
|
static_assert(
|
|
|
|
std::is_same<T, grammar::statement>::value || std::is_same<T, grammar::job_list>::value,
|
|
|
|
"Unexpected node type");
|
2014-03-01 09:54:05 +08:00
|
|
|
CHECK_BLOCK(1);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// Handle cancellation requests. If our block stack is currently empty, then we already did
|
|
|
|
// successfully cancel (or there was nothing to cancel); clear the flag. If our block stack is
|
|
|
|
// not empty, we are still in the process of cancelling; refuse to evaluate anything.
|
|
|
|
if (this->cancellation_requested) {
|
|
|
|
if (!block_stack.empty()) {
|
2014-03-01 09:54:05 +08:00
|
|
|
return 1;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
this->cancellation_requested = false;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// Only certain blocks are allowed.
|
|
|
|
if ((block_type != TOP) && (block_type != SUBST)) {
|
|
|
|
debug(1, INVALID_SCOPE_ERR_MSG, parser_t::get_block_desc(block_type));
|
2014-03-01 09:54:05 +08:00
|
|
|
bugreport();
|
|
|
|
return 1;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-11-19 07:37:54 +08:00
|
|
|
job_reap(false); // not sure why we reap jobs here
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-22 06:53:10 +08:00
|
|
|
// Start it up
|
2017-01-22 07:35:35 +08:00
|
|
|
scope_block_t *scope_block = this->push_block<scope_block_t>(block_type);
|
2018-02-12 11:34:12 +08:00
|
|
|
|
2018-02-12 14:00:17 +08:00
|
|
|
// Create and set a new execution context.
|
|
|
|
using exc_ctx_ref_t = std::unique_ptr<parse_execution_context_t>;
|
|
|
|
scoped_push<exc_ctx_ref_t> exc(&execution_context,
|
2018-11-04 15:58:44 +08:00
|
|
|
make_unique<parse_execution_context_t>(ps, this, parent_job));
|
2018-02-12 14:00:17 +08:00
|
|
|
int result = execution_context->eval_node(node, scope_block, io);
|
|
|
|
exc.restore();
|
2017-01-22 07:35:35 +08:00
|
|
|
this->pop_block(scope_block);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-11-19 07:37:54 +08:00
|
|
|
job_reap(false); // reap again
|
2014-03-01 09:54:05 +08:00
|
|
|
return result;
|
2006-05-22 03:25:24 +08:00
|
|
|
}
|
|
|
|
|
2018-02-11 11:16:35 +08:00
|
|
|
// Explicit instantiations. TODO: use overloads instead?
|
2018-02-12 11:34:12 +08:00
|
|
|
template int parser_t::eval_node(parsed_source_ref_t, tnode_t<grammar::statement>,
|
2018-11-04 15:58:44 +08:00
|
|
|
const io_chain_t &, enum block_type_t,
|
|
|
|
std::shared_ptr<job_t> parent_job);
|
2018-02-12 11:34:12 +08:00
|
|
|
template int parser_t::eval_node(parsed_source_ref_t, tnode_t<grammar::job_list>,
|
2018-11-04 15:58:44 +08:00
|
|
|
const io_chain_t &, enum block_type_t,
|
|
|
|
std::shared_ptr<job_t> parent_job);
|
2018-02-11 11:16:35 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
bool parser_t::detect_errors_in_argument_list(const wcstring &arg_list_src, wcstring *out,
|
2018-08-10 07:46:11 +08:00
|
|
|
const wchar_t *prefix) const {
|
2014-03-19 05:14:32 +08:00
|
|
|
bool errored = false;
|
|
|
|
parse_error_list_t errors;
|
2013-02-22 22:34:30 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// Use empty string for the prefix if it's NULL.
|
2016-10-21 09:53:31 +08:00
|
|
|
if (!prefix) prefix = L""; //!OCLINT(parameter reassignment)
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// Parse the string as an argument list.
|
2014-03-19 05:14:32 +08:00
|
|
|
parse_node_tree_t tree;
|
2016-05-03 08:22:44 +08:00
|
|
|
if (!parse_tree_from_string(arg_list_src, parse_flag_none, &tree, &errors,
|
|
|
|
symbol_freestanding_argument_list)) {
|
|
|
|
// Failed to parse.
|
2014-03-19 05:14:32 +08:00
|
|
|
errored = true;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
if (!errored) {
|
2018-01-13 03:36:45 +08:00
|
|
|
// Get the root argument list and extract arguments from it.
|
2016-10-24 04:58:12 +08:00
|
|
|
assert(!tree.empty()); //!OCLINT(multiple unary operator)
|
2018-01-13 03:36:45 +08:00
|
|
|
tnode_t<grammar::freestanding_argument_list> arg_list(&tree, &tree.at(0));
|
|
|
|
while (auto arg = arg_list.next_in_list<grammar::argument>()) {
|
|
|
|
const wcstring arg_src = arg.get_source(arg_list_src);
|
|
|
|
if (parse_util_detect_errors_in_argument(arg, arg_src, &errors)) {
|
|
|
|
errored = true;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
if (!errors.empty() && out != NULL) {
|
|
|
|
out->assign(errors.at(0).describe_with_prefix(
|
|
|
|
arg_list_src, prefix, false /* not interactive */, false /* don't skip caret */));
|
2014-03-19 05:14:32 +08:00
|
|
|
}
|
|
|
|
return errored;
|
2006-05-22 03:25:24 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
void parser_t::get_backtrace(const wcstring &src, const parse_error_list_t &errors,
|
2017-06-18 13:36:56 +08:00
|
|
|
wcstring &output) const {
|
2016-05-03 08:22:44 +08:00
|
|
|
if (!errors.empty()) {
|
2013-12-27 17:38:43 +08:00
|
|
|
const parse_error_t &err = errors.at(0);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-15 11:35:54 +08:00
|
|
|
const bool is_interactive = shell_is_interactive();
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// Determine if we want to try to print a caret to point at the source error. The
|
|
|
|
// err.source_start <= src.size() check is due to the nasty way that slices work, which is
|
|
|
|
// by rewriting the source.
|
2014-03-22 08:13:33 +08:00
|
|
|
size_t which_line = 0;
|
|
|
|
bool skip_caret = true;
|
2016-05-03 08:22:44 +08:00
|
|
|
if (err.source_start != SOURCE_LOCATION_UNKNOWN && err.source_start <= src.size()) {
|
|
|
|
// Determine which line we're on.
|
2014-03-22 08:13:33 +08:00
|
|
|
which_line = 1 + std::count(src.begin(), src.begin() + err.source_start, L'\n');
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// Don't include the caret if we're interactive, this is the first line of text, and our
|
|
|
|
// source is at its beginning, because then it's obvious.
|
2014-03-22 08:13:33 +08:00
|
|
|
skip_caret = (is_interactive && which_line == 1 && err.source_start == 0);
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-03-22 08:13:33 +08:00
|
|
|
wcstring prefix;
|
2013-12-16 08:05:37 +08:00
|
|
|
const wchar_t *filename = this->current_filename();
|
2016-05-03 08:22:44 +08:00
|
|
|
if (filename) {
|
|
|
|
if (which_line > 0) {
|
|
|
|
prefix = format_string(_(L"%ls (line %lu): "),
|
|
|
|
user_presentable_path(filename).c_str(), which_line);
|
|
|
|
} else {
|
2014-03-22 08:13:33 +08:00
|
|
|
prefix = format_string(_(L"%ls: "), user_presentable_path(filename).c_str());
|
|
|
|
}
|
2016-05-03 08:22:44 +08:00
|
|
|
} else {
|
2014-02-18 06:51:51 +08:00
|
|
|
prefix = L"fish: ";
|
2013-12-16 08:05:37 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
const wcstring description =
|
|
|
|
err.describe_with_prefix(src, prefix, is_interactive, skip_caret);
|
|
|
|
if (!description.empty()) {
|
2017-06-18 13:36:56 +08:00
|
|
|
output.append(description);
|
|
|
|
output.push_back(L'\n');
|
2014-03-16 04:07:19 +08:00
|
|
|
}
|
2017-06-18 13:36:56 +08:00
|
|
|
output.append(this->stack_trace());
|
2013-12-12 10:34:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 12:10:57 +08:00
|
|
|
block_t::block_t(block_type_t t) : block_type(t) {}
|
2012-08-27 13:42:29 +08:00
|
|
|
|
2018-02-19 10:44:58 +08:00
|
|
|
block_t::~block_t() = default;
|
2012-08-27 14:16:20 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
wcstring block_t::description() const {
|
2014-03-17 07:45:00 +08:00
|
|
|
wcstring result;
|
2016-05-03 08:22:44 +08:00
|
|
|
switch (this->type()) {
|
|
|
|
case WHILE: {
|
2014-03-17 07:45:00 +08:00
|
|
|
result.append(L"while");
|
|
|
|
break;
|
2016-05-03 08:22:44 +08:00
|
|
|
}
|
|
|
|
case FOR: {
|
2014-03-17 07:45:00 +08:00
|
|
|
result.append(L"for");
|
|
|
|
break;
|
2016-05-03 08:22:44 +08:00
|
|
|
}
|
|
|
|
case IF: {
|
2014-03-17 07:45:00 +08:00
|
|
|
result.append(L"if");
|
|
|
|
break;
|
2016-05-03 08:22:44 +08:00
|
|
|
}
|
|
|
|
case FUNCTION_CALL: {
|
2014-03-17 07:45:00 +08:00
|
|
|
result.append(L"function_call");
|
|
|
|
break;
|
2016-05-03 08:22:44 +08:00
|
|
|
}
|
|
|
|
case FUNCTION_CALL_NO_SHADOW: {
|
2014-03-17 07:45:00 +08:00
|
|
|
result.append(L"function_call_no_shadow");
|
|
|
|
break;
|
2016-05-03 08:22:44 +08:00
|
|
|
}
|
|
|
|
case SWITCH: {
|
2014-03-17 07:45:00 +08:00
|
|
|
result.append(L"switch");
|
|
|
|
break;
|
2016-05-03 08:22:44 +08:00
|
|
|
}
|
|
|
|
case SUBST: {
|
2014-03-17 07:45:00 +08:00
|
|
|
result.append(L"substitution");
|
|
|
|
break;
|
2016-05-03 08:22:44 +08:00
|
|
|
}
|
|
|
|
case TOP: {
|
2014-03-17 07:45:00 +08:00
|
|
|
result.append(L"top");
|
|
|
|
break;
|
2016-05-03 08:22:44 +08:00
|
|
|
}
|
|
|
|
case BEGIN: {
|
2014-03-17 07:45:00 +08:00
|
|
|
result.append(L"begin");
|
|
|
|
break;
|
2016-05-03 08:22:44 +08:00
|
|
|
}
|
|
|
|
case SOURCE: {
|
2014-03-17 07:45:00 +08:00
|
|
|
result.append(L"source");
|
|
|
|
break;
|
2016-05-03 08:22:44 +08:00
|
|
|
}
|
|
|
|
case EVENT: {
|
2014-03-17 07:45:00 +08:00
|
|
|
result.append(L"event");
|
|
|
|
break;
|
2016-05-03 08:22:44 +08:00
|
|
|
}
|
|
|
|
case BREAKPOINT: {
|
2014-03-17 07:45:00 +08:00
|
|
|
result.append(L"breakpoint");
|
|
|
|
break;
|
2016-05-03 08:22:44 +08:00
|
|
|
}
|
2014-03-17 07:45:00 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
if (this->src_lineno >= 0) {
|
2014-03-17 07:45:00 +08:00
|
|
|
append_format(result, L" (line %d)", this->src_lineno);
|
|
|
|
}
|
2016-05-03 08:22:44 +08:00
|
|
|
if (this->src_filename != NULL) {
|
2014-03-17 07:45:00 +08:00
|
|
|
append_format(result, L" (file %ls)", this->src_filename);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
// Various block constructors.
|
2012-08-27 14:16:20 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
if_block_t::if_block_t() : block_t(IF) {}
|
2012-08-27 14:16:20 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
event_block_t::event_block_t(const event_t &evt) : block_t(EVENT), event(evt) {}
|
2012-08-27 14:16:20 +08:00
|
|
|
|
2018-02-19 10:39:03 +08:00
|
|
|
function_block_t::function_block_t(const process_t *p, wcstring n, bool shadows)
|
|
|
|
: block_t(shadows ? FUNCTION_CALL : FUNCTION_CALL_NO_SHADOW), process(p), name(std::move(n)) {}
|
2012-08-27 14:16:20 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
source_block_t::source_block_t(const wchar_t *src) : block_t(SOURCE), source_file(src) {}
|
2012-08-27 14:16:20 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
for_block_t::for_block_t() : block_t(FOR) {}
|
2012-08-27 14:16:20 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
while_block_t::while_block_t() : block_t(WHILE) {}
|
2012-08-27 14:16:20 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
switch_block_t::switch_block_t() : block_t(SWITCH) {}
|
2012-08-27 14:16:20 +08:00
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
scope_block_t::scope_block_t(block_type_t type) : block_t(type) {
|
2012-08-27 14:16:20 +08:00
|
|
|
assert(type == BEGIN || type == TOP || type == SUBST);
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:22:44 +08:00
|
|
|
breakpoint_block_t::breakpoint_block_t() : block_t(BREAKPOINT) {}
|