1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
/* Yash: yet another shell */
/* lineedit.c: command line editing */
/* (C) 2007-2016 magicant */
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "../common.h"
#include "lineedit.h"
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include "../option.h"
#include "../sig.h"
#include "../strbuf.h"
#include "../util.h"
#include "../variable.h"
#include "display.h"
#include "editing.h"
#include "key.h"
#include "keymap.h"
#include "terminfo.h"
#include "trie.h"
static void reader_init(bool trap);
static void reader_finalize(void);
static void read_next(void);
static int get_read_timeout(void)
__attribute__((pure));
static char pop_prebuffer(void);
static inline bool has_meta_bit(char c)
__attribute__((pure));
static inline trieget_T make_trieget(const wchar_t *keyseq)
__attribute__((nonnull,const));
static void append_to_second_buffer(wchar_t wc);
/* The state of line-editing. */
enum le_state_T le_state;
/* The state of editing. */
enum le_editstate_T le_editstate;
/* Do line-editing using the specified prompts.
* The prompts may contain backslash escapes specified in "input.c".
* If `trap' is true, traps are handled while waiting for input.
* The result is returned as a newly malloced wide string, including the
* trailing newline. It is assigned to `*resultp' iff the return value is
* INPUT_OK. Returns INPUT_ERROR iff failed to set up the terminal. */
inputresult_T le_readline(
struct promptset_T prompt, bool trap, wchar_t **resultp)
{
assert(is_interactive_now);
assert(le_state == LE_STATE_INACTIVE);
if (!isatty(STDIN_FILENO) || !isatty(STDERR_FILENO)
|| !le_setupterm(true) || !le_set_terminal())
return INPUT_ERROR;
le_state = LE_STATE_ACTIVE;
le_keymap_init();
le_editing_init();
le_display_init(prompt);
reader_init(trap);
le_editstate = LE_EDITSTATE_EDITING;
do
read_next();
while (le_editstate == LE_EDITSTATE_EDITING);
wchar_t *resultline;
reader_finalize();
le_display_finalize();
resultline = le_editing_finalize();
le_restore_terminal();
le_state = LE_STATE_INACTIVE;
switch (le_editstate) {
case LE_EDITSTATE_DONE:
*resultp = resultline;
return INPUT_OK;
case LE_EDITSTATE_ERROR:
free(resultline);
return INPUT_EOF;
case LE_EDITSTATE_INTERRUPTED:
free(resultline);
return INPUT_INTERRUPTED;
case LE_EDITSTATE_EDITING:
assert(false);
}
assert(false);
}
/* Clears the edit line and restores the terminal state.
* Does nothing if line-editing is not active. */
void le_suspend_readline(void)
{
if (le_state == LE_STATE_ACTIVE) {
le_state = LE_STATE_SUSPENDED;
le_display_clear(false);
le_restore_terminal();
}
}
/* Resumes line-editing suspended by `le_suspend_readline'.
* Does nothing if the line-editing is not suspended. */
void le_resume_readline(void)
{
if (le_state == LE_STATE_SUSPENDED) {
le_state = LE_STATE_ACTIVE;
le_setupterm(true);
le_set_terminal();
le_display_update(true);
le_display_flush();
}
}
/* Re-retrieves the terminfo and reprints everything. */
/* When the display size is changed, this function is called. */
void le_display_size_changed(void)
{
if (le_state == LE_STATE_ACTIVE) {
le_display_clear(false);
le_setupterm(true);
le_display_update(true);
le_display_flush();
}
}
/********** Input Reading **********/
/* True if traps should be handled while reading. */
static bool reader_trap;
/* Temporary buffer that contains bytes that are treated as input. */
static xstrbuf_T reader_prebuffer;
/* Temporary buffer that contains input bytes. */
static xstrbuf_T reader_first_buffer;
/* Conversion state used in reading input. */
static mbstate_t reader_state;
/* Temporary buffer that contains input converted into wide characters. */
static xwcsbuf_T reader_second_buffer;
/* If true, next input will be inserted directly to the main buffer. */
bool le_next_verbatim;
/* Initializes the state of the reader. */
void reader_init(bool trap)
{
reader_trap = trap;
sb_init(&reader_first_buffer);
memset(&reader_state, 0, sizeof reader_state);
wb_init(&reader_second_buffer);
le_next_verbatim = false;
}
/* Frees memory used by the reader. */
void reader_finalize(void)
{
sb_destroy(&reader_first_buffer);
wb_destroy(&reader_second_buffer);
}
/* Reads the next byte from the standard input and take all the corresponding
* actions.
* May return without doing anything if a signal was caught, if the shell was
* interrupted, etc.
* The caller must check `le_state' after this function returned. This function
* should be called repeatedly while `le_editstate' is LE_STATE_EDITING. */
void read_next(void)
{
static bool incomplete_wchar = false;
static bool keycode_ambiguous = false;
assert(le_editstate == LE_EDITSTATE_EDITING);
bool timeout = false;
char c = pop_prebuffer();
if (c != '\0')
goto direct_first_buffer;
le_display_update(true);
le_display_flush();
/* wait for and read the next byte */
switch (wait_for_input(STDIN_FILENO, reader_trap,
keycode_ambiguous ? get_read_timeout() : -1)) {
case W_READY:
switch (read(STDIN_FILENO, &c, 1)) {
case 0:
incomplete_wchar = keycode_ambiguous = false;
le_editstate = LE_EDITSTATE_ERROR;
return;
case 1:
break;
case -1:
switch (errno) {
case EAGAIN:
#if EAGAIN != EWOULDBLOCK
case EWOULDBLOCK:
#endif
case EINTR:
return;
default:
xerror(errno, Ngt("cannot read input"));
incomplete_wchar = keycode_ambiguous = false;
le_editstate = LE_EDITSTATE_ERROR;
return;
}
default:
assert(false);
}
if (has_meta_bit(c)) {
sb_ccat(&reader_first_buffer, ESCAPE_CHAR);
sb_ccat(&reader_first_buffer, c & ~META_BIT);
} else {
direct_first_buffer:
sb_ccat(&reader_first_buffer, c);
}
break;
case W_TIMED_OUT:
timeout = true;
break;
case W_INTERRUPTED:
le_editstate = LE_EDITSTATE_INTERRUPTED;
return;
case W_ERROR:
le_editstate = LE_EDITSTATE_ERROR;
return;
}
if (incomplete_wchar || le_next_verbatim)
goto process_wide;
/* process the content in the first buffer */
keycode_ambiguous = false;
while (reader_first_buffer.length > 0) {
/* check if `reader_first_buffer' is a special sequence */
int firstchar = (unsigned char) reader_first_buffer.contents[0];
trieget_T tg;
if (firstchar == le_interrupt_char)
tg = make_trieget(Key_interrupt);
else if (firstchar == le_eof_char)
tg = make_trieget(Key_eof);
else if (firstchar == le_kill_char)
tg = make_trieget(Key_kill);
else if (firstchar == le_erase_char)
tg = make_trieget(Key_erase);
else
tg = trie_get(le_keycodes,
reader_first_buffer.contents, reader_first_buffer.length);
switch (tg.type) {
case TG_NOMATCH:
goto process_wide;
case TG_AMBIGUOUS:
if (timeout) {
case TG_EXACTMATCH:
sb_remove(&reader_first_buffer, 0, tg.matchlength);
wb_cat(&reader_second_buffer, tg.value.keyseq);
continue;
} else {
keycode_ambiguous = true;
}
/* falls thru! */
case TG_PREFIXMATCH:
goto process_keymap;
}
}
/* convert bytes in the first buffer into wide characters and append to the
* second buffer */
process_wide:
while (reader_first_buffer.length > 0) {
wchar_t wc;
size_t n = mbrtowc(&wc, reader_first_buffer.contents,
reader_first_buffer.length, &reader_state);
incomplete_wchar = false;
switch (n) {
case 0: // read null character
sb_clear(&reader_first_buffer);
append_to_second_buffer(L'\0');
break;
case (size_t) -1: // conversion error
lebuf_print_alert(true);
memset(&reader_state, 0, sizeof reader_state);
sb_clear(&reader_first_buffer);
le_next_verbatim = false;
goto process_keymap;
case (size_t) -2: // more bytes needed
incomplete_wchar = true;
sb_clear(&reader_first_buffer);
goto process_keymap;
default:
sb_remove(&reader_first_buffer, 0, n);
append_to_second_buffer(wc);
break;
}
}
/* process key mapping for wide characters in the second buffer */
process_keymap:
while (reader_second_buffer.length > 0) {
register wchar_t c;
trieget_T tg = trie_getw(
le_current_mode->keymap, reader_second_buffer.contents);
switch (tg.type) {
case TG_NOMATCH:
assert(reader_second_buffer.length > 0);
if (reader_second_buffer.length > 1)
c = L'\0';
else
c = reader_second_buffer.contents[0];
le_invoke_command(le_current_mode->default_command, c);
wb_clear(&reader_second_buffer);
break;
case TG_EXACTMATCH:
assert(tg.matchlength > 0);
c = reader_second_buffer.contents[tg.matchlength - 1];
le_invoke_command(tg.value.cmdfunc, c);
wb_remove(&reader_second_buffer, 0, tg.matchlength);
break;
case TG_PREFIXMATCH:
case TG_AMBIGUOUS:
return;
/* For an unmatched control sequence, `cmd_self_insert' should not
* receive any part of the sequence as argument (because the sequence
* should not be inserted in the main buffer), so the input is passed
* to the default command iff the input is a usual character.
* For a matched control sequence, the last character of the sequence
* is passed to the command so that commands like `cmd_digit_argument'
* work. */
}
if (le_editstate != LE_EDITSTATE_EDITING)
break;
}
}
/* Returns a timeout value to be passed to the `wait_for_input' function.
* The value is taken from the $YASH_LE_TIMEOUT variable. */
int get_read_timeout(void)
{
#ifndef LE_TIMEOUT_DEFAULT
#define LE_TIMEOUT_DEFAULT 100
#endif
const wchar_t *v = getvar(L VAR_YASH_LE_TIMEOUT);
if (v != NULL) {
int i;
if (xwcstoi(v, 0, &i))
return i;
}
return LE_TIMEOUT_DEFAULT;
}
/* Appends `s' to the prebuffer. String `s' is freed in this function. */
void le_append_to_prebuffer(char *s)
{
if (reader_prebuffer.contents == NULL)
sb_initwith(&reader_prebuffer, s);
else
sb_catfree(&reader_prebuffer, s);
}
/* Removes and returns the next character in the prebuffer if available;
* otherwise, returns '\0'. */
char pop_prebuffer(void)
{
if (reader_prebuffer.contents != NULL) {
char result = reader_prebuffer.contents[0];
sb_remove(&reader_prebuffer, 0, 1);
if (reader_prebuffer.length == 0) {
sb_destroy(&reader_prebuffer);
reader_prebuffer.contents = NULL;
}
return result;
}
return '\0';
}
/* Tests if the specified character has the meta flag set. */
bool has_meta_bit(char c)
{
switch (shopt_le_convmeta) {
case SHOPT_YES:
break;
case SHOPT_NO:
return false;
case SHOPT_AUTO:
if (le_meta_bit8)
break;
else
return false;
}
return (c & META_BIT) != 0;
}
trieget_T make_trieget(const wchar_t *keyseq)
{
return (trieget_T) {
.type = TG_EXACTMATCH,
.matchlength = 1,
.value.keyseq = keyseq,
};
}
/* Appends the specified character to the second buffer.
* If `le_next_verbatim' is true, the character is directly processed by the
* default command. */
void append_to_second_buffer(wchar_t wc)
{
if (le_next_verbatim) {
le_next_verbatim = false;
le_invoke_command(le_current_mode->default_command, wc);
} else {
switch (wc) {
case L'\0':
break;
case L'\\':
wb_cat(&reader_second_buffer, Key_backslash);
break;
default:
wb_wccat(&reader_second_buffer, wc);
break;
}
}
}
/* vim: set ts=8 sts=4 sw=4 et tw=80: */
|