Remove global variables from builtin_commandline

There was no reason to have builtin_commandline store its buffer in a global,
these can be local variables.
This commit is contained in:
ridiculousfish 2018-10-01 09:34:58 -07:00
parent 551ca813e0
commit a722a4b967

View file

@ -36,18 +36,6 @@ enum {
APPEND_MODE // insert at end of current token/command/buffer
};
/// Pointer to what the commandline builtin considers to be the current contents of the command line
/// buffer.
static const wchar_t *current_buffer = 0;
// What the commandline builtin considers to be the current cursor position.
static size_t current_cursor_pos = (size_t)(-1);
/// Returns the current commandline buffer.
static const wchar_t *get_buffer() { return current_buffer; }
/// Returns the position of the cursor.
static size_t get_cursor_pos() { return current_cursor_pos; }
static owning_lock<wcstring_list_t> &get_transient_stack() {
ASSERT_IS_MAIN_THREAD();
static owning_lock<wcstring_list_t> s_transient_stack;
@ -85,10 +73,11 @@ builtin_commandline_scoped_transient_t::~builtin_commandline_scoped_transient_t(
/// \param insert the string to insert
/// \param append_mode can be one of REPLACE_MODE, INSERT_MODE or APPEND_MODE, affects the way the
/// test update is performed
/// \param buff the original command line buffer
/// \param cursor_pos the position of the cursor in the command line
static void replace_part(const wchar_t *begin, const wchar_t *end, const wchar_t *insert,
int append_mode) {
const wchar_t *buff = get_buffer();
size_t out_pos = get_cursor_pos();
int append_mode, const wchar_t *buff, size_t cursor_pos) {
size_t out_pos = cursor_pos;
wcstring out;
@ -106,7 +95,7 @@ static void replace_part(const wchar_t *begin, const wchar_t *end, const wchar_t
break;
}
case INSERT_MODE: {
long cursor = get_cursor_pos() - (begin - buff);
long cursor = cursor_pos - (begin - buff);
out.append(begin, cursor);
out.append(insert);
out.append(begin + cursor, end - begin - cursor);
@ -129,9 +118,11 @@ static void replace_part(const wchar_t *begin, const wchar_t *end, const wchar_t
/// \param cut_at_cursor whether printing should stop at the surrent cursor position
/// \param tokenize whether the string should be tokenized, printing one string token on every line
/// and skipping non-string tokens
/// \param buffer the original command line buffer
/// \param cursor_pos the position of the cursor in the command line
static void write_part(const wchar_t *begin, const wchar_t *end, int cut_at_cursor, int tokenize,
io_streams_t &streams) {
size_t pos = get_cursor_pos() - (begin - get_buffer());
const wchar_t *buffer, size_t cursor_pos, io_streams_t &streams) {
size_t pos = cursor_pos - (begin - buffer);
if (tokenize) {
// fwprintf( stderr, L"Subshell: %ls, end char %lc\n", buff, *end );
@ -163,6 +154,13 @@ static void write_part(const wchar_t *begin, const wchar_t *end, int cut_at_curs
/// The commandline builtin. It is used for specifying a new value for the commandline.
int builtin_commandline(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
// Pointer to what the commandline builtin considers to be the current contents of the command
// line buffer.
const wchar_t *current_buffer = 0;
// What the commandline builtin considers to be the current cursor position.
size_t current_cursor_pos = (size_t)(-1);
wchar_t *cmd = argv[0];
int buffer_part = 0;
int cut_at_cursor = 0;
@ -181,9 +179,6 @@ int builtin_commandline(parser_t &parser, io_streams_t &streams, wchar_t **argv)
int paging_mode = 0;
const wchar_t *begin = NULL, *end = NULL;
scoped_push<const wchar_t *> saved_current_buffer(&current_buffer);
scoped_push<size_t> saved_current_cursor_pos(&current_cursor_pos);
wcstring transient_commandline;
if (get_top_transient(&transient_commandline)) {
current_buffer = transient_commandline.c_str();
@ -193,7 +188,7 @@ int builtin_commandline(parser_t &parser, io_streams_t &streams, wchar_t **argv)
current_cursor_pos = reader_get_cursor_pos();
}
if (!get_buffer()) {
if (!current_buffer) {
if (is_interactive_session) {
// Prompt change requested while we don't have a prompt, most probably while reading the
// init files. Just ignore it.
@ -428,20 +423,20 @@ int builtin_commandline(parser_t &parser, io_streams_t &streams, wchar_t **argv)
switch (buffer_part) {
case STRING_MODE: {
begin = get_buffer();
begin = current_buffer;
end = begin + wcslen(begin);
break;
}
case PROCESS_MODE: {
parse_util_process_extent(get_buffer(), get_cursor_pos(), &begin, &end);
parse_util_process_extent(current_buffer, current_cursor_pos, &begin, &end);
break;
}
case JOB_MODE: {
parse_util_job_extent(get_buffer(), get_cursor_pos(), &begin, &end);
parse_util_job_extent(current_buffer, current_cursor_pos, &begin, &end);
break;
}
case TOKEN_MODE: {
parse_util_token_extent(get_buffer(), get_cursor_pos(), &begin, &end, 0, 0);
parse_util_token_extent(current_buffer, current_cursor_pos, &begin, &end, 0, 0);
break;
}
default: {
@ -452,16 +447,17 @@ int builtin_commandline(parser_t &parser, io_streams_t &streams, wchar_t **argv)
int arg_count = argc - w.woptind;
if (arg_count == 0) {
write_part(begin, end, cut_at_cursor, tokenize, streams);
write_part(begin, end, cut_at_cursor, tokenize, current_buffer, current_cursor_pos,
streams);
} else if (arg_count == 1) {
replace_part(begin, end, argv[w.woptind], append_mode);
replace_part(begin, end, argv[w.woptind], append_mode, current_buffer, current_cursor_pos);
} else {
wcstring sb = argv[w.woptind];
for (int i = w.woptind + 1; i < argc; i++) {
sb.push_back(L'\n');
sb.append(argv[i]);
}
replace_part(begin, end, sb.c_str(), append_mode);
replace_part(begin, end, sb.c_str(), append_mode, current_buffer, current_cursor_pos);
}
return STATUS_CMD_OK;