diff --git a/src/builtin_read.cpp b/src/builtin_read.cpp index 4c7895882..fe4b842ae 100644 --- a/src/builtin_read.cpp +++ b/src/builtin_read.cpp @@ -202,7 +202,6 @@ static int read_interactive(wcstring &buff, int nchars, bool shell, bool silent, const wchar_t *prompt, const wchar_t *right_prompt, const wchar_t *commandline) { int exit_res = STATUS_CMD_OK; - const wchar_t *line; // TODO: rationalize this. const auto &vars = env_stack_t::principal(); @@ -227,17 +226,16 @@ static int read_interactive(wcstring &buff, int nchars, bool shell, bool silent, proc_push_interactive(1); event_fire_generic(L"fish_prompt"); - line = reader_readline(nchars); + auto mline = reader_readline(nchars); proc_pop_interactive(); - if (line) { - if (0 < nchars && (size_t)nchars < wcslen(line)) { + if (mline) { + buff = mline.acquire(); + if (nchars > 0 && (size_t)nchars < buff.size()) { // 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); + buff.resize(nchars); } } else { exit_res = STATUS_CMD_ERROR; diff --git a/src/reader.cpp b/src/reader.cpp index 8308943ec..a370e2719 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -2373,12 +2373,12 @@ static int read_i() { // Put buff in temporary string and clear buff, so that we can handle a call to // reader_set_buffer during evaluation. - const wchar_t *tmp = reader_readline(0); + maybe_t tmp = reader_readline(0); if (shell_is_exiting()) { handle_end_loop(); } else if (tmp) { - const wcstring command = tmp; + const wcstring command = tmp.acquire(); update_buff_pos(&data->command_line, 0); data->command_line.text.clear(); data->command_line_changed(&data->command_line); @@ -2448,7 +2448,7 @@ static bool text_ends_in_comment(const wcstring &text) { return token.type == TOK_COMMENT; } -const wchar_t *reader_readline(int nchars) { +maybe_t reader_readline(int nchars) { wint_t c; int last_char = 0; size_t yank_len = 0; @@ -3350,7 +3350,7 @@ const wchar_t *reader_readline(int nchars) { outputter_t::stdoutput().set_color(rgb_color_t::reset(), rgb_color_t::reset()); } - return finished ? data->command_line.text.c_str() : NULL; + return finished ? maybe_t{data->command_line.text} : none(); } bool jump(jump_direction_t dir, jump_precision_t precision, editable_line_t *el, wchar_t target) { diff --git a/src/reader.h b/src/reader.h index 0c28109b2..871547cfd 100644 --- a/src/reader.h +++ b/src/reader.h @@ -144,7 +144,7 @@ bool reader_thread_job_is_stale(); /// characters even if a full line has not yet been read. Note: the returned value may be longer /// than nchars if a single keypress resulted in multiple characters being inserted into the /// commandline. -const wchar_t *reader_readline(int nchars); +maybe_t reader_readline(int nchars); /// Push a new reader environment. void reader_push(const wcstring &name);