mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-30 14:53:11 +00:00
Eliminate reader_current_filename
Store this in the parser libdata instead.
This commit is contained in:
parent
686b84396c
commit
e91d68266c
8 changed files with 19 additions and 51 deletions
|
@ -74,7 +74,8 @@ int builtin_source(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const block_t *sb = parser.push_block(block_t::source_block(fn_intern));
|
const block_t *sb = parser.push_block(block_t::source_block(fn_intern));
|
||||||
reader_push_current_filename(fn_intern);
|
auto &ld = parser.libdata();
|
||||||
|
scoped_push<const wchar_t *> filename_push{&ld.current_filename, fn_intern};
|
||||||
|
|
||||||
// This is slightly subtle. If this is a bare `source` with no args then `argv + optind` already
|
// This is slightly subtle. If this is a bare `source` with no args then `argv + optind` already
|
||||||
// points to the end of argv. Otherwise we want to skip the file name to get to the args if any.
|
// points to the end of argv. Otherwise we want to skip the file name to get to the args if any.
|
||||||
|
@ -95,6 +96,5 @@ int builtin_source(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
||||||
|
|
||||||
// Do not close fd after calling reader_read. reader_read automatically closes it before calling
|
// Do not close fd after calling reader_read. reader_read automatically closes it before calling
|
||||||
// eval.
|
// eval.
|
||||||
reader_pop_current_filename();
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
|
@ -676,7 +676,7 @@ static bool handle_builtin_output(parser_t &parser, const std::shared_ptr<job_t>
|
||||||
|
|
||||||
/// Executes an external command.
|
/// Executes an external command.
|
||||||
/// \return true on success, false if there is an exec error.
|
/// \return true on success, false if there is an exec error.
|
||||||
static bool exec_external_command(env_stack_t &vars, const std::shared_ptr<job_t> &j, process_t *p,
|
static bool exec_external_command(parser_t &parser, const std::shared_ptr<job_t> &j, process_t *p,
|
||||||
const io_chain_t &proc_io_chain) {
|
const io_chain_t &proc_io_chain) {
|
||||||
assert(p->type == process_type_t::external && "Process is not external");
|
assert(p->type == process_type_t::external && "Process is not external");
|
||||||
// Get argv and envv before we fork.
|
// Get argv and envv before we fork.
|
||||||
|
@ -694,14 +694,13 @@ static bool exec_external_command(env_stack_t &vars, const std::shared_ptr<job_t
|
||||||
// (/dev/tty?).
|
// (/dev/tty?).
|
||||||
make_fd_blocking(STDIN_FILENO);
|
make_fd_blocking(STDIN_FILENO);
|
||||||
|
|
||||||
auto export_arr = vars.export_arr();
|
auto export_arr = parser.vars().export_arr();
|
||||||
;
|
|
||||||
const char *const *argv = argv_array.get();
|
const char *const *argv = argv_array.get();
|
||||||
const char *const *envv = export_arr->get();
|
const char *const *envv = export_arr->get();
|
||||||
|
|
||||||
std::string actual_cmd_str = wcs2string(p->actual_cmd);
|
std::string actual_cmd_str = wcs2string(p->actual_cmd);
|
||||||
const char *actual_cmd = actual_cmd_str.c_str();
|
const char *actual_cmd = actual_cmd_str.c_str();
|
||||||
const wchar_t *file = reader_current_filename();
|
const wchar_t *file = parser.libdata().current_filename;
|
||||||
|
|
||||||
#if FISH_USE_POSIX_SPAWN
|
#if FISH_USE_POSIX_SPAWN
|
||||||
// Prefer to use posix_spawn, since it's faster on some systems like OS X.
|
// Prefer to use posix_spawn, since it's faster on some systems like OS X.
|
||||||
|
@ -979,7 +978,7 @@ static bool exec_process_in_job(parser_t &parser, process_t *p, std::shared_ptr<
|
||||||
}
|
}
|
||||||
|
|
||||||
case process_type_t::external: {
|
case process_type_t::external: {
|
||||||
if (!exec_external_command(parser.vars(), j, p, process_net_io_chain)) {
|
if (!exec_external_command(parser, j, p, process_net_io_chain)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
14
src/fish.cpp
14
src/fish.cpp
|
@ -47,6 +47,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||||
#include "function.h"
|
#include "function.h"
|
||||||
#include "future_feature_flags.h"
|
#include "future_feature_flags.h"
|
||||||
#include "history.h"
|
#include "history.h"
|
||||||
|
#include "intern.h"
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "path.h"
|
#include "path.h"
|
||||||
|
@ -466,18 +467,15 @@ int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
parser.vars().set(L"argv", ENV_DEFAULT, list);
|
parser.vars().set(L"argv", ENV_DEFAULT, list);
|
||||||
|
|
||||||
const wcstring rel_filename = str2wcstring(file);
|
auto &ld = parser.libdata();
|
||||||
|
wcstring rel_filename = str2wcstring(file);
|
||||||
reader_push_current_filename(rel_filename.c_str());
|
scoped_push<const wchar_t *> filename_push{&ld.current_filename,
|
||||||
|
intern(rel_filename.c_str())};
|
||||||
res = reader_read(fd, {});
|
res = reader_read(fd, {});
|
||||||
|
|
||||||
if (res) {
|
if (res) {
|
||||||
debug(1, _(L"Error while reading file %ls\n"),
|
debug(1, _(L"Error while reading file %ls\n"),
|
||||||
reader_current_filename() ? reader_current_filename()
|
ld.current_filename ? ld.current_filename : _(L"Standard input"));
|
||||||
: _(L"Standard input"));
|
|
||||||
}
|
}
|
||||||
reader_pop_current_filename();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -190,7 +190,7 @@ void function_add(const function_data_t &data, const parser_t &parser) {
|
||||||
bool is_autoload = funcset->autoloader.autoload_in_progress(data.name);
|
bool is_autoload = funcset->autoloader.autoload_in_progress(data.name);
|
||||||
|
|
||||||
// Create and store a new function.
|
// Create and store a new function.
|
||||||
const wchar_t *filename = reader_current_filename();
|
const wchar_t *filename = parser.libdata().current_filename;
|
||||||
auto ins = funcset->funcs.emplace(data.name,
|
auto ins = funcset->funcs.emplace(data.name,
|
||||||
function_info_t(data, parser.vars(), filename, is_autoload));
|
function_info_t(data, parser.vars(), filename, is_autoload));
|
||||||
assert(ins.second && "Function should not already be present in the table");
|
assert(ins.second && "Function should not already be present in the table");
|
||||||
|
|
|
@ -498,12 +498,8 @@ const wchar_t *parser_t::current_filename() const {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// We query a global array for the current file name, but only do that if we are the principal
|
// Fall back to the file being sourced.
|
||||||
// parser.
|
return libdata().current_filename;
|
||||||
if (this == &principal_parser()) {
|
|
||||||
return reader_current_filename();
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wcstring parser_t::current_line() {
|
wcstring parser_t::current_line() {
|
||||||
|
|
|
@ -156,6 +156,10 @@ struct library_data_t {
|
||||||
|
|
||||||
/// Whether we should return from the current function.
|
/// Whether we should return from the current function.
|
||||||
bool returning{false};
|
bool returning{false};
|
||||||
|
|
||||||
|
/// The current filename we are evaluating, either from builtin source or on the command line.
|
||||||
|
/// This is an intern'd string.
|
||||||
|
const wchar_t *current_filename{};
|
||||||
};
|
};
|
||||||
|
|
||||||
class parser_t : public std::enable_shared_from_this<parser_t> {
|
class parser_t : public std::enable_shared_from_this<parser_t> {
|
||||||
|
|
|
@ -482,9 +482,6 @@ class reader_data_t : public std::enable_shared_from_this<reader_data_t> {
|
||||||
/// handled by the fish interrupt handler.
|
/// handled by the fish interrupt handler.
|
||||||
static volatile sig_atomic_t is_interactive_read;
|
static volatile sig_atomic_t is_interactive_read;
|
||||||
|
|
||||||
/// The stack containing names of files that are being parsed.
|
|
||||||
static std::stack<const wchar_t *, std::vector<const wchar_t *>> current_filename;
|
|
||||||
|
|
||||||
/// This variable is set to true by the signal handler when ^C is pressed.
|
/// This variable is set to true by the signal handler when ^C is pressed.
|
||||||
static volatile sig_atomic_t interrupted = 0;
|
static volatile sig_atomic_t interrupted = 0;
|
||||||
|
|
||||||
|
@ -689,21 +686,6 @@ void reader_handle_sigint() {
|
||||||
interrupted = 1;
|
interrupted = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const wchar_t *reader_current_filename() {
|
|
||||||
ASSERT_IS_MAIN_THREAD();
|
|
||||||
return current_filename.empty() ? NULL : current_filename.top();
|
|
||||||
}
|
|
||||||
|
|
||||||
void reader_push_current_filename(const wchar_t *fn) {
|
|
||||||
ASSERT_IS_MAIN_THREAD();
|
|
||||||
current_filename.push(intern(fn));
|
|
||||||
}
|
|
||||||
|
|
||||||
void reader_pop_current_filename() {
|
|
||||||
ASSERT_IS_MAIN_THREAD();
|
|
||||||
current_filename.pop();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Make sure buffers are large enough to hold the current string length.
|
/// Make sure buffers are large enough to hold the current string length.
|
||||||
void reader_data_t::command_line_changed(const editable_line_t *el) {
|
void reader_data_t::command_line_changed(const editable_line_t *el) {
|
||||||
ASSERT_IS_MAIN_THREAD();
|
ASSERT_IS_MAIN_THREAD();
|
||||||
|
|
11
src/reader.h
11
src/reader.h
|
@ -64,20 +64,9 @@ void reader_init();
|
||||||
/// Restore the term mode at startup.
|
/// Restore the term mode at startup.
|
||||||
void restore_term_mode();
|
void restore_term_mode();
|
||||||
|
|
||||||
/// Returns the filename of the file currently read.
|
|
||||||
const wchar_t *reader_current_filename();
|
|
||||||
|
|
||||||
/// Push a new filename on the stack of read files.
|
|
||||||
///
|
|
||||||
/// \param fn The fileanme to push
|
|
||||||
void reader_push_current_filename(const wchar_t *fn);
|
|
||||||
|
|
||||||
/// Change the history file for the current command reading context.
|
/// Change the history file for the current command reading context.
|
||||||
void reader_change_history(const wcstring &name);
|
void reader_change_history(const wcstring &name);
|
||||||
|
|
||||||
/// Pop the current filename from the stack of read files.
|
|
||||||
void reader_pop_current_filename();
|
|
||||||
|
|
||||||
/// Write the title to the titlebar. This function is called just before a new application starts
|
/// Write the title to the titlebar. This function is called just before a new application starts
|
||||||
/// executing and just after it finishes.
|
/// executing and just after it finishes.
|
||||||
///
|
///
|
||||||
|
|
Loading…
Reference in a new issue