busybox/libbb/lineedit.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * Command line editing.
   4 *
   5 * Copyright (c) 1986-2003 may safely be consumed by a BSD or GPL license.
   6 * Written by:   Vladimir Oleynik <dzo@simtreas.ru>
   7 *
   8 * Used ideas:
   9 *      Adam Rogoyski    <rogoyski@cs.utexas.edu>
  10 *      Dave Cinege      <dcinege@psychosis.com>
  11 *      Jakub Jelinek (c) 1995
  12 *      Erik Andersen    <andersen@codepoet.org> (Majorly adjusted for busybox)
  13 *
  14 * This code is 'as is' with no warranty.
  15 */
  16/*
  17 * Usage and known bugs:
  18 * Terminal key codes are not extensive, more needs to be added.
  19 * This version was created on Debian GNU/Linux 2.x.
  20 * Delete, Backspace, Home, End, and the arrow keys were tested
  21 * to work in an Xterm and console. Ctrl-A also works as Home.
  22 * Ctrl-E also works as End.
  23 *
  24 * The following readline-like commands are not implemented:
  25 * CTL-t -- Transpose two characters
  26 *
  27 * lineedit does not know that the terminal escape sequences do not
  28 * take up space on the screen. The redisplay code assumes, unless
  29 * told otherwise, that each character in the prompt is a printable
  30 * character that takes up one character position on the screen.
  31 * You need to tell lineedit that some sequences of characters
  32 * in the prompt take up no screen space. Compatibly with readline,
  33 * use the \[ escape to begin a sequence of non-printing characters,
  34 * and the \] escape to signal the end of such a sequence. Example:
  35 *
  36 * PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] '
  37 *
  38 * Unicode in PS1 is not fully supported: prompt length calulation is wrong,
  39 * resulting in line wrap problems with long (multi-line) input.
  40 */
  41#include "busybox.h"
  42#include "NUM_APPLETS.h"
  43#include "unicode.h"
  44#ifndef _POSIX_VDISABLE
  45# define _POSIX_VDISABLE '\0'
  46#endif
  47
  48
  49#ifdef TEST
  50# define ENABLE_FEATURE_EDITING 0
  51# define ENABLE_FEATURE_TAB_COMPLETION 0
  52# define ENABLE_FEATURE_USERNAME_COMPLETION 0
  53#endif
  54
  55
  56/* Entire file (except TESTing part) sits inside this #if */
  57#if ENABLE_FEATURE_EDITING
  58
  59
  60#if !ENABLE_SHELL_ASH && !ENABLE_SHELL_HUSH
  61/* so far only shells use these features */
  62# undef ENABLE_FEATURE_EDITING_FANCY_PROMPT
  63# undef ENABLE_FEATURE_TAB_COMPLETION
  64# undef ENABLE_FEATURE_USERNAME_COMPLETION
  65# define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0
  66# define ENABLE_FEATURE_TAB_COMPLETION       0
  67# define ENABLE_FEATURE_USERNAME_COMPLETION  0
  68#endif
  69
  70
  71#define ENABLE_USERNAME_OR_HOMEDIR \
  72        (ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT)
  73#if ENABLE_USERNAME_OR_HOMEDIR
  74# define IF_USERNAME_OR_HOMEDIR(...) __VA_ARGS__
  75#else
  76# define IF_USERNAME_OR_HOMEDIR(...) /*nothing*/
  77#endif
  78
  79
  80#undef CHAR_T
  81#if ENABLE_UNICODE_SUPPORT
  82# define BB_NUL ((wchar_t)0)
  83# define CHAR_T wchar_t
  84static bool BB_isspace(CHAR_T c)
  85{
  86        return ((unsigned)c < 256 && isspace(c));
  87}
  88# if ENABLE_FEATURE_EDITING_VI
  89static bool BB_isalnum_or_underscore(CHAR_T c)
  90{
  91        return ((unsigned)c < 256 && isalnum(c)) || c == '_';
  92}
  93# endif
  94static bool BB_ispunct(CHAR_T c)
  95{
  96        return ((unsigned)c < 256 && ispunct(c));
  97}
  98# undef isspace
  99# undef isalnum
 100# undef ispunct
 101# undef isprint
 102# define isspace isspace_must_not_be_used
 103# define isalnum isalnum_must_not_be_used
 104# define ispunct ispunct_must_not_be_used
 105# define isprint isprint_must_not_be_used
 106#else
 107# define BB_NUL '\0'
 108# define CHAR_T char
 109# define BB_isspace(c) isspace(c)
 110# if ENABLE_FEATURE_EDITING_VI
 111static bool BB_isalnum_or_underscore(CHAR_T c)
 112{
 113        return isalnum(c) || c == '_';
 114}
 115# endif
 116# define BB_ispunct(c) ispunct(c)
 117#endif
 118#if ENABLE_UNICODE_PRESERVE_BROKEN
 119# define unicode_mark_raw_byte(wc)   ((wc) | 0x20000000)
 120# define unicode_is_raw_byte(wc)     ((wc) & 0x20000000)
 121#else
 122# define unicode_is_raw_byte(wc)     0
 123#endif
 124
 125
 126#define ESC "\033"
 127
 128#define SEQ_CLEAR_TILL_END_OF_SCREEN  ESC"[J"
 129//#define SEQ_CLEAR_TILL_END_OF_LINE  ESC"[K"
 130
 131
 132enum {
 133        MAX_LINELEN = CONFIG_FEATURE_EDITING_MAX_LEN < 0x7ff0
 134                      ? CONFIG_FEATURE_EDITING_MAX_LEN
 135                      : 0x7ff0
 136};
 137
 138/* We try to minimize both static and stack usage. */
 139struct lineedit_statics {
 140        line_input_t *state;
 141
 142        unsigned cmdedit_termw; /* = 80; */ /* actual terminal width */
 143
 144        unsigned cmdedit_x;        /* real x (col) terminal position */
 145        unsigned cmdedit_y;        /* pseudoreal y (row) terminal position */
 146        unsigned cmdedit_prmt_len; /* on-screen length of last/sole prompt line */
 147
 148        unsigned cursor;
 149        int command_len; /* must be signed */
 150        /* signed maxsize: we want x in "if (x > S.maxsize)"
 151         * to _not_ be promoted to unsigned */
 152        int maxsize;
 153        CHAR_T *command_ps;
 154
 155        const char *cmdedit_prompt;
 156        const char *prompt_last_line;  /* last/sole prompt line */
 157
 158#if ENABLE_USERNAME_OR_HOMEDIR
 159        char *user_buf;
 160        char *home_pwd_buf;
 161        smallint got_user_strings;
 162#endif
 163
 164#if ENABLE_FEATURE_TAB_COMPLETION
 165        unsigned num_matches;
 166        char **matches;
 167#endif
 168
 169#if ENABLE_FEATURE_EDITING_WINCH
 170        unsigned SIGWINCH_saved;
 171        volatile unsigned SIGWINCH_count;
 172        volatile smallint ok_to_redraw;
 173#endif
 174
 175#if ENABLE_FEATURE_EDITING_VI
 176# define DELBUFSIZ 128
 177        smallint newdelflag;     /* whether delbuf should be reused yet */
 178        CHAR_T *delptr;
 179        CHAR_T delbuf[DELBUFSIZ];  /* a place to store deleted characters */
 180#endif
 181#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
 182        smallint sent_ESC_br6n;
 183#endif
 184
 185#if ENABLE_FEATURE_EDITING_WINCH
 186        /* Largish struct, keeping it last results in smaller code */
 187        struct sigaction SIGWINCH_handler;
 188#endif
 189};
 190
 191/* See lineedit_ptr_hack.c */
 192extern struct lineedit_statics *BB_GLOBAL_CONST lineedit_ptr_to_statics;
 193
 194#define S (*lineedit_ptr_to_statics)
 195#define state            (S.state           )
 196#define cmdedit_termw    (S.cmdedit_termw   )
 197#define cmdedit_x        (S.cmdedit_x       )
 198#define cmdedit_y        (S.cmdedit_y       )
 199#define cmdedit_prmt_len (S.cmdedit_prmt_len)
 200#define cursor           (S.cursor          )
 201#define command_len      (S.command_len     )
 202#define command_ps       (S.command_ps      )
 203#define cmdedit_prompt   (S.cmdedit_prompt  )
 204#define prompt_last_line (S.prompt_last_line)
 205#define user_buf         (S.user_buf        )
 206#define home_pwd_buf     (S.home_pwd_buf    )
 207#define got_user_strings (S.got_user_strings)
 208#define num_matches      (S.num_matches     )
 209#define matches          (S.matches         )
 210#define delptr           (S.delptr          )
 211#define newdelflag       (S.newdelflag      )
 212#define delbuf           (S.delbuf          )
 213
 214#define INIT_S() do { \
 215        XZALLOC_CONST_PTR(&lineedit_ptr_to_statics, sizeof(S)); \
 216} while (0)
 217
 218static void deinit_S(void)
 219{
 220#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
 221        /* This one is allocated only if FANCY_PROMPT is on
 222         * (otherwise it points to verbatim prompt (NOT malloced)) */
 223        free((char*)cmdedit_prompt);
 224#endif
 225#if ENABLE_USERNAME_OR_HOMEDIR
 226        free(user_buf);
 227        free(home_pwd_buf);
 228#endif
 229        free(lineedit_ptr_to_statics);
 230}
 231#define DEINIT_S() deinit_S()
 232
 233
 234#if ENABLE_USERNAME_OR_HOMEDIR
 235/* Call getpwuid() only if necessary.
 236 * E.g. if PS1=':', no user database reading is needed to generate prompt.
 237 * (Unfortunately, default PS1='\w \$' needs it, \w abbreviates homedir
 238 * as ~/... - for that it needs to *know* the homedir...)
 239 */
 240static void get_user_strings(void)
 241{
 242        struct passwd *entry;
 243
 244        got_user_strings = 1;
 245        entry = getpwuid(geteuid());
 246        if (entry) {
 247                user_buf = xstrdup(entry->pw_name);
 248                home_pwd_buf = xstrdup(entry->pw_dir);
 249        }
 250}
 251
 252static NOINLINE const char *get_homedir_or_NULL(void)
 253{
 254        const char *home;
 255
 256# if ENABLE_SHELL_ASH || ENABLE_SHELL_HUSH
 257        home = state && state->sh_get_var ? state->sh_get_var("HOME") : getenv("HOME");
 258# else
 259        home = getenv("HOME");
 260# endif
 261        if (home != NULL && home[0] != '\0')
 262                return home;
 263
 264        if (!got_user_strings)
 265                get_user_strings();
 266        return home_pwd_buf;
 267}
 268#endif
 269
 270#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
 271static const char *get_username_str(void)
 272{
 273        if (!got_user_strings)
 274                get_user_strings();
 275        return user_buf ? user_buf : "";
 276        /* btw, bash uses "I have no name!" string if uid has no entry */
 277}
 278#endif
 279
 280#if ENABLE_UNICODE_SUPPORT
 281static size_t load_string(const char *src)
 282{
 283        if (unicode_status == UNICODE_ON) {
 284                ssize_t len = mbstowcs(command_ps, src, S.maxsize - 1);
 285                if (len < 0)
 286                        len = 0;
 287                command_ps[len] = BB_NUL;
 288                return len;
 289        } else {
 290                unsigned i = 0;
 291                while (src[i] && i < S.maxsize - 1) {
 292                        command_ps[i] = src[i];
 293                        i++;
 294                }
 295                command_ps[i] = BB_NUL;
 296                return i;
 297        }
 298}
 299static unsigned save_string(char *dst, unsigned maxsize)
 300{
 301        if (unicode_status == UNICODE_ON) {
 302# if !ENABLE_UNICODE_PRESERVE_BROKEN
 303                ssize_t len = wcstombs(dst, command_ps, maxsize - 1);
 304                if (len < 0)
 305                        len = 0;
 306                dst[len] = '\0';
 307                return len;
 308# else
 309                unsigned dstpos = 0;
 310                unsigned srcpos = 0;
 311
 312                maxsize--;
 313                while (dstpos < maxsize) {
 314                        wchar_t wc;
 315                        int n = srcpos;
 316
 317                        /* Convert up to 1st invalid byte (or up to end) */
 318                        while ((wc = command_ps[srcpos]) != BB_NUL
 319                            && !unicode_is_raw_byte(wc)
 320                        ) {
 321                                srcpos++;
 322                        }
 323                        command_ps[srcpos] = BB_NUL;
 324                        n = wcstombs(dst + dstpos, command_ps + n, maxsize - dstpos);
 325                        if (n < 0) /* should not happen */
 326                                break;
 327                        dstpos += n;
 328                        if (wc == BB_NUL) /* usually is */
 329                                break;
 330
 331                        /* We do have invalid byte here! */
 332                        command_ps[srcpos] = wc; /* restore it */
 333                        srcpos++;
 334                        if (dstpos == maxsize)
 335                                break;
 336                        dst[dstpos++] = (char) wc;
 337                }
 338                dst[dstpos] = '\0';
 339                return dstpos;
 340# endif
 341        } else {
 342                unsigned i = 0;
 343                while ((dst[i] = command_ps[i]) != 0)
 344                        i++;
 345                return i;
 346        }
 347}
 348/* I thought just fputwc(c, stderr) would work. But no... */
 349static void BB_PUTCHAR(wchar_t c)
 350{
 351        if (unicode_status == UNICODE_ON) {
 352                char buf[MB_CUR_MAX + 1];
 353                mbstate_t mbst = { 0 };
 354                ssize_t len = wcrtomb(buf, c, &mbst);
 355                if (len > 0) {
 356                        buf[len] = '\0';
 357                        fputs(buf, stderr);
 358                }
 359        } else {
 360                /* In this case, c is always one byte */
 361                bb_putchar_stderr(c);
 362        }
 363}
 364# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
 365static wchar_t adjust_width_and_validate_wc(unsigned *width_adj, wchar_t wc)
 366# else
 367static wchar_t adjust_width_and_validate_wc(wchar_t wc)
 368#  define adjust_width_and_validate_wc(width_adj, wc) \
 369        ((*(width_adj))++, adjust_width_and_validate_wc(wc))
 370# endif
 371{
 372        int w = 1;
 373
 374        if (unicode_status == UNICODE_ON) {
 375                if (wc > CONFIG_LAST_SUPPORTED_WCHAR) {
 376                        /* note: also true for unicode_is_raw_byte(wc) */
 377                        goto subst;
 378                }
 379                w = wcwidth(wc);
 380                if ((ENABLE_UNICODE_COMBINING_WCHARS && w < 0)
 381                 || (!ENABLE_UNICODE_COMBINING_WCHARS && w <= 0)
 382                 || (!ENABLE_UNICODE_WIDE_WCHARS && w > 1)
 383                ) {
 384 subst:
 385                        w = 1;
 386                        wc = CONFIG_SUBST_WCHAR;
 387                }
 388        }
 389
 390# if ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS
 391        *width_adj += w;
 392#endif
 393        return wc;
 394}
 395#else /* !UNICODE */
 396static size_t load_string(const char *src)
 397{
 398        safe_strncpy(command_ps, src, S.maxsize);
 399        return strlen(command_ps);
 400}
 401# if ENABLE_FEATURE_TAB_COMPLETION
 402static void save_string(char *dst, unsigned maxsize)
 403{
 404        safe_strncpy(dst, command_ps, maxsize);
 405}
 406# endif
 407# define BB_PUTCHAR(c) bb_putchar_stderr(c)
 408/* Should never be called: */
 409int adjust_width_and_validate_wc(unsigned *width_adj, int wc);
 410#endif
 411
 412
 413/* Put 'command_ps[cursor]', cursor++.
 414 * Advance cursor on screen. If we reached right margin, scroll text up
 415 * and remove terminal margin effect by printing 'next_char' */
 416#define HACK_FOR_WRONG_WIDTH 1
 417static void put_cur_glyph_and_inc_cursor(void)
 418{
 419        CHAR_T c = command_ps[cursor];
 420        unsigned width = 0;
 421        int ofs_to_right;
 422
 423        if (c == BB_NUL) {
 424                /* erase character after end of input string */
 425                c = ' ';
 426        } else {
 427                /* advance cursor only if we aren't at the end yet */
 428                cursor++;
 429                if (unicode_status == UNICODE_ON) {
 430                        IF_UNICODE_WIDE_WCHARS(width = cmdedit_x;)
 431                        c = adjust_width_and_validate_wc(&cmdedit_x, c);
 432                        IF_UNICODE_WIDE_WCHARS(width = cmdedit_x - width;)
 433                } else {
 434                        cmdedit_x++;
 435                }
 436        }
 437
 438        ofs_to_right = cmdedit_x - cmdedit_termw;
 439        if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right <= 0) {
 440                /* c fits on this line */
 441                BB_PUTCHAR(c);
 442        }
 443
 444        if (ofs_to_right >= 0) {
 445                /* we go to the next line */
 446#if HACK_FOR_WRONG_WIDTH
 447                /* This works better if our idea of term width is wrong
 448                 * and it is actually wider (often happens on serial lines).
 449                 * Printing CR,LF *forces* cursor to next line.
 450                 * OTOH if terminal width is correct AND terminal does NOT
 451                 * have automargin (IOW: it is moving cursor to next line
 452                 * by itself (which is wrong for VT-10x terminals)),
 453                 * this will break things: there will be one extra empty line */
 454                puts("\r"); /* + implicit '\n' */
 455#else
 456                /* VT-10x terminals don't wrap cursor to next line when last char
 457                 * on the line is printed - cursor stays "over" this char.
 458                 * Need to print _next_ char too (first one to appear on next line)
 459                 * to make cursor move down to next line.
 460                 */
 461                /* Works ok only if cmdedit_termw is correct. */
 462                c = command_ps[cursor];
 463                if (c == BB_NUL)
 464                        c = ' ';
 465                BB_PUTCHAR(c);
 466                bb_putchar_stderr('\b');
 467#endif
 468                cmdedit_y++;
 469                if (!ENABLE_UNICODE_WIDE_WCHARS || ofs_to_right == 0) {
 470                        width = 0;
 471                } else { /* ofs_to_right > 0 */
 472                        /* wide char c didn't fit on prev line */
 473                        BB_PUTCHAR(c);
 474                }
 475                cmdedit_x = width;
 476        }
 477}
 478
 479/* Move to end of line (by printing all chars till the end) */
 480static void put_till_end_and_adv_cursor(void)
 481{
 482        while (cursor < command_len)
 483                put_cur_glyph_and_inc_cursor();
 484}
 485
 486/* Go to the next line */
 487static void goto_new_line(void)
 488{
 489        put_till_end_and_adv_cursor();
 490        /* "cursor == 0" is only if prompt is "" and user input is empty */
 491        if (cursor == 0 || cmdedit_x != 0)
 492                bb_putchar_stderr('\n');
 493}
 494
 495static void beep(void)
 496{
 497        bb_putchar_stderr('\007');
 498}
 499
 500/* Full or last/sole prompt line, reset edit cursor, calculate terminal cursor.
 501 * cmdedit_y is always calculated for the last/sole prompt line.
 502 */
 503static void put_prompt_custom(bool is_full)
 504{
 505        /* https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
 506         * says that shells must write $PSn to stderr, not stdout.
 507         */
 508        fputs((is_full ? cmdedit_prompt : prompt_last_line), stderr);
 509        cursor = 0;
 510        cmdedit_y = cmdedit_prmt_len / cmdedit_termw; /* new quasireal y */
 511        cmdedit_x = cmdedit_prmt_len % cmdedit_termw;
 512}
 513
 514#define put_prompt_last_line() put_prompt_custom(0)
 515#define put_prompt()           put_prompt_custom(1)
 516
 517/* Move back one character */
 518/* (optimized for slow terminals) */
 519static void input_backward(unsigned num)
 520{
 521        if (num > cursor)
 522                num = cursor;
 523        if (num == 0)
 524                return;
 525        cursor -= num;
 526
 527        if ((ENABLE_UNICODE_COMBINING_WCHARS || ENABLE_UNICODE_WIDE_WCHARS)
 528         && unicode_status == UNICODE_ON
 529        ) {
 530                /* correct NUM to be equal to _screen_ width */
 531                int n = num;
 532                num = 0;
 533                while (--n >= 0)
 534                        adjust_width_and_validate_wc(&num, command_ps[cursor + n]);
 535                if (num == 0)
 536                        return;
 537        }
 538
 539        if (cmdedit_x >= num) {
 540                cmdedit_x -= num;
 541                if (num <= 4) {
 542                        /* This is longer by 5 bytes on x86.
 543                         * Also gets miscompiled for ARM users
 544                         * (busybox.net/bugs/view.php?id=2274).
 545                         * fprintf(("\b\b\b\b" + 4) - num, stderr);
 546                         * return;
 547                         */
 548                        do {
 549                                bb_putchar_stderr('\b');
 550                        } while (--num);
 551                        return;
 552                }
 553                fprintf(stderr, ESC"[%uD", num);
 554                return;
 555        }
 556
 557        /* Need to go one or more lines up */
 558        if (ENABLE_UNICODE_WIDE_WCHARS) {
 559                /* With wide chars, it is hard to "backtrack"
 560                 * and reliably figure out where to put cursor.
 561                 * Example (<> is a wide char; # is an ordinary char, _ cursor):
 562                 * |prompt: <><> |
 563                 * |<><><><><><> |
 564                 * |_            |
 565                 * and user presses left arrow. num = 1, cmdedit_x = 0,
 566                 * We need to go up one line, and then - how do we know that
 567                 * we need to go *10* positions to the right? Because
 568                 * |prompt: <>#<>|
 569                 * |<><><>#<><><>|
 570                 * |_            |
 571                 * in this situation we need to go *11* positions to the right.
 572                 *
 573                 * A simpler thing to do is to redraw everything from the start
 574                 * up to new cursor position (which is already known):
 575                 */
 576                unsigned sv_cursor;
 577                /* go to 1st column; go up to first line */
 578                fprintf(stderr, "\r" ESC"[%uA", cmdedit_y);
 579                cmdedit_y = 0;
 580                sv_cursor = cursor;
 581                put_prompt_last_line(); /* sets cursor to 0 */
 582                while (cursor < sv_cursor)
 583                        put_cur_glyph_and_inc_cursor();
 584        } else {
 585                int lines_up;
 586                /* num = chars to go back from the beginning of current line: */
 587                num -= cmdedit_x;
 588                /* num=1...w: one line up, w+1...2w: two, etc: */
 589                lines_up = 1 + (num - 1) / cmdedit_termw;
 590                cmdedit_x = (cmdedit_termw * cmdedit_y - num) % cmdedit_termw;
 591                cmdedit_y -= lines_up;
 592                /* go to 1st column; go up */
 593                fprintf(stderr, "\r" ESC"[%uA", lines_up);
 594                /* go to correct column.
 595                 * xterm, konsole, Linux VT interpret 0 as 1 below! wow.
 596                 * need to *make sure* we skip it if cmdedit_x == 0 */
 597                if (cmdedit_x)
 598                        fprintf(stderr, ESC"[%uC", cmdedit_x);
 599        }
 600}
 601
 602/* See redraw and draw_full below */
 603static void draw_custom(int y, int back_cursor, bool is_full)
 604{
 605        if (y > 0) /* up y lines */
 606                fprintf(stderr, ESC"[%uA", y);
 607        bb_putchar_stderr('\r');
 608        put_prompt_custom(is_full);
 609        put_till_end_and_adv_cursor();
 610        fputs(SEQ_CLEAR_TILL_END_OF_SCREEN, stderr);
 611        input_backward(back_cursor);
 612}
 613
 614/* Move y lines up, draw last/sole prompt line, editor line[s], and clear tail.
 615 * goal: redraw the prompt+input+cursor in-place, overwriting the previous */
 616#define redraw(y, back_cursor) draw_custom((y), (back_cursor), 0)
 617
 618/* Like above, but without moving up, and while using all the prompt lines.
 619 * goal: draw a full prompt+input+cursor unrelated to a previous position.
 620 * note: cmdedit_y always ends up relating to the last/sole prompt line */
 621#define draw_full(back_cursor) draw_custom(0, (back_cursor), 1)
 622
 623/* Delete the char in front of the cursor, optionally saving it
 624 * for later putback */
 625#if !ENABLE_FEATURE_EDITING_VI
 626static void input_delete(void)
 627#define input_delete(save) input_delete()
 628#else
 629static void input_delete(int save)
 630#endif
 631{
 632        int j = cursor;
 633
 634        if (j == (int)command_len)
 635                return;
 636
 637#if ENABLE_FEATURE_EDITING_VI
 638        if (save) {
 639                if (newdelflag) {
 640                        delptr = delbuf;
 641                        newdelflag = 0;
 642                }
 643                if ((delptr - delbuf) < DELBUFSIZ)
 644                        *delptr++ = command_ps[j];
 645        }
 646#endif
 647
 648        memmove(command_ps + j, command_ps + j + 1,
 649                        /* (command_len + 1 [because of NUL]) - (j + 1)
 650                         * simplified into (command_len - j) */
 651                        (command_len - j) * sizeof(command_ps[0]));
 652        command_len--;
 653        put_till_end_and_adv_cursor();
 654        /* Last char is still visible, erase it (and more) */
 655        fputs(SEQ_CLEAR_TILL_END_OF_SCREEN, stderr);
 656        input_backward(cursor - j);     /* back to old pos cursor */
 657}
 658
 659#if ENABLE_FEATURE_EDITING_VI
 660static void put(void)
 661{
 662        int ocursor;
 663        int j = delptr - delbuf;
 664
 665        if (j == 0)
 666                return;
 667        ocursor = cursor;
 668        /* open hole and then fill it */
 669        memmove(command_ps + cursor + j, command_ps + cursor,
 670                        (command_len - cursor + 1) * sizeof(command_ps[0]));
 671        memcpy(command_ps + cursor, delbuf, j * sizeof(command_ps[0]));
 672        command_len += j;
 673        put_till_end_and_adv_cursor();
 674        input_backward(cursor - ocursor - j + 1); /* at end of new text */
 675}
 676#endif
 677
 678/* Delete the char in back of the cursor */
 679static void input_backspace(void)
 680{
 681        if (cursor > 0) {
 682                input_backward(1);
 683                input_delete(0);
 684        }
 685}
 686
 687/* Move forward one character */
 688static void input_forward(void)
 689{
 690        if (cursor < command_len)
 691                put_cur_glyph_and_inc_cursor();
 692}
 693
 694#if ENABLE_FEATURE_TAB_COMPLETION
 695
 696//FIXME:
 697//needs to be more clever: currently it thinks that "foo\ b<TAB>
 698//matches the file named "foo bar", which is untrue.
 699//Also, perhaps "foo b<TAB> needs to complete to "foo bar" <cursor>,
 700//not "foo bar <cursor>...
 701
 702static void free_tab_completion_data(void)
 703{
 704        if (matches) {
 705                while (num_matches)
 706                        free(matches[--num_matches]);
 707                free(matches);
 708                matches = NULL;
 709        }
 710}
 711
 712static void add_match(char *matched)
 713{
 714        unsigned char *p = (unsigned char*)matched;
 715        while (*p) {
 716                /* ESC attack fix: drop any string with control chars */
 717                if (*p < ' '
 718                 || (!ENABLE_UNICODE_SUPPORT && *p >= 0x7f)
 719                 || (ENABLE_UNICODE_SUPPORT && *p == 0x7f)
 720                ) {
 721                        free(matched);
 722                        return;
 723                }
 724                p++;
 725        }
 726        matches = xrealloc_vector(matches, 4, num_matches);
 727        matches[num_matches] = matched;
 728        num_matches++;
 729}
 730
 731# if ENABLE_FEATURE_USERNAME_COMPLETION
 732/* Replace "~user/..." with "/homedir/...".
 733 * The parameter is malloced, free it or return it
 734 * unchanged if no user is matched.
 735 */
 736static char *username_path_completion(char *ud)
 737{
 738        struct passwd *entry;
 739        char *tilde_name = ud;
 740        const char *home = NULL;
 741
 742        ud++; /* skip ~ */
 743        if (*ud == '/') {       /* "~/..." */
 744                home = get_homedir_or_NULL();
 745        } else {
 746                /* "~user/..." */
 747                ud = strchr(ud, '/');
 748                *ud = '\0';           /* "~user" */
 749                entry = getpwnam(tilde_name + 1);
 750                *ud = '/';            /* restore "~user/..." */
 751                if (entry)
 752                        home = entry->pw_dir;
 753        }
 754        if (home) {
 755                ud = concat_path_file(home, ud);
 756                free(tilde_name);
 757                tilde_name = ud;
 758        }
 759        return tilde_name;
 760}
 761
 762/* ~use<tab> - find all users with this prefix.
 763 * Return the length of the prefix used for matching.
 764 */
 765static NOINLINE unsigned complete_username(const char *ud)
 766{
 767        struct passwd *pw;
 768        unsigned userlen;
 769
 770        ud++; /* skip ~ */
 771        userlen = strlen(ud);
 772
 773        setpwent();
 774        while ((pw = getpwent()) != NULL) {
 775                /* Null usernames should result in all users as possible completions. */
 776                if (/* !ud[0] || */ is_prefixed_with(pw->pw_name, ud)) {
 777                        add_match(xasprintf("~%s/", pw->pw_name));
 778                }
 779        }
 780        endpwent(); /* don't keep password file open */
 781
 782        return 1 + userlen;
 783}
 784# endif  /* FEATURE_USERNAME_COMPLETION */
 785
 786enum {
 787        FIND_EXE_ONLY = 0,
 788        FIND_DIR_ONLY = 1,
 789        FIND_FILE_ONLY = 2,
 790};
 791
 792static unsigned path_parse(char ***p)
 793{
 794        unsigned npth;
 795        const char *pth;
 796        char *tmp;
 797        char **res;
 798
 799# if EDITING_HAS_path_lookup
 800        if (state->flags & WITH_PATH_LOOKUP)
 801                pth = state->path_lookup;
 802        else
 803# endif
 804                pth = getenv("PATH");
 805
 806        /* PATH="" or PATH=":"? */
 807        if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
 808                return 1;
 809
 810        tmp = (char*)pth;
 811        npth = 1; /* path component count */
 812        while (1) {
 813                tmp = strchr(tmp, ':');
 814                if (!tmp)
 815                        break;
 816                tmp++;
 817                npth++;
 818        }
 819
 820        *p = res = xzalloc((npth + 1) * sizeof(res[0]));
 821        res[0] = tmp = xstrdup(pth);
 822        npth = 1;
 823        while (1) {
 824                tmp = strchr(tmp, ':');
 825                if (!tmp)
 826                        break;
 827                *tmp++ = '\0'; /* ':' -> '\0' */
 828                res[npth++] = tmp;
 829        }
 830        /* special case: "match subdirectories of the current directory" */
 831        /*res[npth] = NULL; - filled by xzalloc() */
 832        return npth + 1;
 833}
 834
 835/* Complete command, directory or file name.
 836 * Return the length of the prefix used for matching.
 837 */
 838static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
 839{
 840        char *path1[1];
 841        char **paths = path1;
 842        unsigned npaths;
 843        unsigned i;
 844        unsigned baselen;
 845        const char *basecmd;
 846        char *dirbuf = NULL;
 847
 848        npaths = 1;
 849        path1[0] = (char*)".";
 850
 851        basecmd = strrchr(command, '/');
 852        if (!basecmd) {
 853                if (type == FIND_EXE_ONLY)
 854                        npaths = path_parse(&paths);
 855                basecmd = command;
 856        } else {
 857                /* point to 'l' in "..../last_component" */
 858                basecmd++;
 859                /* dirbuf = ".../.../.../" */
 860                dirbuf = xstrndup(command, basecmd - command);
 861# if ENABLE_FEATURE_USERNAME_COMPLETION
 862                if (dirbuf[0] == '~')   /* ~/... or ~user/... */
 863                        dirbuf = username_path_completion(dirbuf);
 864# endif
 865                path1[0] = dirbuf;
 866        }
 867        baselen = strlen(basecmd);
 868
 869        if (type == FIND_EXE_ONLY && !dirbuf) {
 870# if ENABLE_FEATURE_SH_STANDALONE && NUM_APPLETS != 1
 871                const char *p = applet_names;
 872                while (*p) {
 873                        if (strncmp(basecmd, p, baselen) == 0)
 874                                add_match(xstrdup(p));
 875                        while (*p++ != '\0')
 876                                continue;
 877                }
 878# endif
 879# if ENABLE_SHELL_ASH || ENABLE_SHELL_HUSH
 880                if (state->get_exe_name) {
 881                        i = 0;
 882                        for (;;) {
 883                                const char *b = state->get_exe_name(i++);
 884                                if (!b)
 885                                        break;
 886                                if (strncmp(basecmd, b, baselen) == 0)
 887                                        add_match(xstrdup(b));
 888                        }
 889                }
 890# endif
 891        }
 892
 893        for (i = 0; i < npaths; i++) {
 894                DIR *dir;
 895                struct dirent *next;
 896                struct stat st;
 897                char *found;
 898                const char *lpath;
 899
 900                if (paths[i] == NULL) { /* path_parse()'s last component? */
 901                        /* in PATH completion, current dir's subdir names
 902                         * can be completions (but only subdirs, not files).
 903                         */
 904                        type = FIND_DIR_ONLY;
 905                        paths[i] = (char *)".";
 906                }
 907
 908                lpath = *paths[i] ? paths[i] : ".";
 909                dir = opendir(lpath);
 910                if (!dir)
 911                        continue; /* don't print an error */
 912
 913                while ((next = readdir(dir)) != NULL) {
 914                        unsigned len;
 915                        const char *name_found = next->d_name;
 916
 917                        /* .../<tab>: bash 3.2.0 shows dotfiles, but not . and .. */
 918                        if (!basecmd[0] && DOT_OR_DOTDOT(name_found))
 919                                continue;
 920                        /* match? */
 921                        if (strncmp(basecmd, name_found, baselen) != 0)
 922                                continue; /* no */
 923
 924                        found = concat_path_file(lpath, name_found);
 925                        /* NB: stat() first so that we see is it a directory;
 926                         * but if that fails, use lstat() so that
 927                         * we still match dangling links */
 928                        if (stat(found, &st) && lstat(found, &st))
 929                                goto cont; /* hmm, remove in progress? */
 930
 931                        /* Save only name */
 932                        len = strlen(name_found);
 933                        found = xrealloc(found, len + 2); /* +2: for slash and NUL */
 934                        strcpy(found, name_found);
 935
 936                        if (S_ISDIR(st.st_mode)) {
 937                                /* skip directories if searching PATH */
 938                                if (type == FIND_EXE_ONLY && !dirbuf)
 939                                        goto cont;
 940                                /* name is a directory, add slash */
 941                                found[len] = '/';
 942                                found[len + 1] = '\0';
 943                        } else {
 944                                /* skip files if looking for dirs only (example: cd) */
 945                                if (type == FIND_DIR_ONLY)
 946                                        goto cont;
 947                        }
 948                        /* add it to the list */
 949                        add_match(found);
 950                        continue;
 951 cont:
 952                        free(found);
 953                }
 954                closedir(dir);
 955        } /* for every path */
 956
 957        if (paths != path1) {
 958                free(paths[0]); /* allocated memory is only in first member */
 959                free(paths);
 960        }
 961        free(dirbuf);
 962
 963        return baselen;
 964}
 965
 966/* build_match_prefix:
 967 * On entry, match_buf contains everything up to cursor at the moment <tab>
 968 * was pressed. This function looks at it, figures out what part of it
 969 * constitutes the command/file/directory prefix to use for completion,
 970 * and rewrites match_buf to contain only that part.
 971 */
 972#define dbg_bmp 0
 973/* Helpers: */
 974/* QUOT is used on elements of int_buf[], which are bytes,
 975 * not Unicode chars. Therefore it works correctly even in Unicode mode.
 976 */
 977#define QUOT (UCHAR_MAX+1)
 978static void remove_chunk(int16_t *int_buf, int beg, int end)
 979{
 980        /* beg must be <= end */
 981        if (beg == end)
 982                return;
 983
 984        while ((int_buf[beg] = int_buf[end]) != 0)
 985                beg++, end++;
 986
 987        if (dbg_bmp) {
 988                int i;
 989                for (i = 0; int_buf[i]; i++)
 990                        bb_putchar_stderr((unsigned char)int_buf[i]);
 991                bb_putchar_stderr('\n');
 992        }
 993}
 994/* Caller ensures that match_buf points to a malloced buffer
 995 * big enough to hold strlen(match_buf)*2 + 2
 996 */
 997static NOINLINE int build_match_prefix(char *match_buf)
 998{
 999        int i, j;
1000        int command_mode;
1001        int16_t *int_buf = (int16_t*)match_buf;
1002
1003        if (dbg_bmp) printf("\n%s\n", match_buf);
1004
1005        /* Copy in reverse order, since they overlap */
1006        i = strlen(match_buf);
1007        do {
1008                int_buf[i] = (unsigned char)match_buf[i];
1009                i--;
1010        } while (i >= 0);
1011
1012        /* Mark every \c as "quoted c" */
1013        for (i = 0; int_buf[i]; i++) {
1014                if (int_buf[i] == '\\') {
1015                        remove_chunk(int_buf, i, i + 1);
1016                        int_buf[i] |= QUOT;
1017                }
1018        }
1019        /* Quote-mark "chars" and 'chars', drop delimiters */
1020        {
1021                int in_quote = 0;
1022                i = 0;
1023                while (int_buf[i]) {
1024                        int cur = int_buf[i];
1025                        if (!cur)
1026                                break;
1027                        if (cur == '\'' || cur == '"') {
1028                                if (!in_quote || (cur == in_quote)) {
1029                                        in_quote ^= cur;
1030                                        remove_chunk(int_buf, i, i + 1);
1031                                        continue;
1032                                }
1033                        }
1034                        if (in_quote)
1035                                int_buf[i] = cur | QUOT;
1036                        i++;
1037                }
1038        }
1039
1040        /* Remove everything up to command delimiters:
1041         * ';' ';;' '&' '|' '&&' '||',
1042         * but careful with '>&' '<&' '>|'
1043         */
1044        for (i = 0; int_buf[i]; i++) {
1045                int cur = int_buf[i];
1046                if (cur == ';' || cur == '&' || cur == '|') {
1047                        int prev = i ? int_buf[i - 1] : 0;
1048                        if (cur == '&' && (prev == '>' || prev == '<')) {
1049                                continue;
1050                        } else if (cur == '|' && prev == '>') {
1051                                continue;
1052                        }
1053                        remove_chunk(int_buf, 0, i + 1 + (cur == int_buf[i + 1]));
1054                        i = -1;  /* back to square 1 */
1055                }
1056        }
1057        /* Remove all `cmd` */
1058        for (i = 0; int_buf[i]; i++) {
1059                if (int_buf[i] == '`') {
1060                        for (j = i + 1; int_buf[j]; j++) {
1061                                if (int_buf[j] == '`') {
1062                                        /* `cmd` should count as a word:
1063                                         * `cmd` c<tab> should search for files c*,
1064                                         * not commands c*. Therefore we don't drop
1065                                         * `cmd` entirely, we replace it with single `.
1066                                         */
1067                                        remove_chunk(int_buf, i, j);
1068                                        goto next;
1069                                }
1070                        }
1071                        /* No closing ` - command mode, remove all up to ` */
1072                        remove_chunk(int_buf, 0, i + 1);
1073                        break;
1074 next: ;
1075                }
1076        }
1077
1078        /* Remove "cmd (" and "cmd {"
1079         * Example: "if { c<tab>"
1080         * In this example, c should be matched as command pfx.
1081         */
1082        for (i = 0; int_buf[i]; i++) {
1083                if (int_buf[i] == '(' || int_buf[i] == '{') {
1084                        remove_chunk(int_buf, 0, i + 1);
1085                        i = -1;  /* back to square 1 */
1086                }
1087        }
1088
1089        /* Remove leading unquoted spaces */
1090        for (i = 0; int_buf[i]; i++)
1091                if (int_buf[i] != ' ')
1092                        break;
1093        remove_chunk(int_buf, 0, i);
1094
1095        /* Determine completion mode */
1096        command_mode = FIND_EXE_ONLY;
1097        for (i = 0; int_buf[i]; i++) {
1098                if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
1099                        if (int_buf[i] == ' '
1100                         && command_mode == FIND_EXE_ONLY
1101                         && (char)int_buf[0] == 'c'
1102                         && (char)int_buf[1] == 'd'
1103                         && i == 2 /* -> int_buf[2] == ' ' */
1104                        ) {
1105                                command_mode = FIND_DIR_ONLY;
1106                        } else {
1107                                command_mode = FIND_FILE_ONLY;
1108                                break;
1109                        }
1110                }
1111        }
1112        if (dbg_bmp) printf("command_mode(0:exe/1:dir/2:file):%d\n", command_mode);
1113
1114        /* Remove everything except last word */
1115        for (i = 0; int_buf[i]; i++) /* quasi-strlen(int_buf) */
1116                continue;
1117        for (--i; i >= 0; i--) {
1118                int cur = int_buf[i];
1119                if (cur == ' ' || cur == '<' || cur == '>' || cur == '|' || cur == '&' || cur == '=') {
1120                        remove_chunk(int_buf, 0, i + 1);
1121                        break;
1122                }
1123        }
1124
1125        /* Convert back to string of _chars_ */
1126        i = 0;
1127        while ((match_buf[i] = int_buf[i]) != '\0')
1128                i++;
1129
1130        if (dbg_bmp) printf("final match_buf:'%s'\n", match_buf);
1131
1132        return command_mode;
1133}
1134
1135/*
1136 * Display by column (original idea from ls applet,
1137 * very optimized by me [Vladimir] :)
1138 */
1139static void showfiles(void)
1140{
1141        int ncols, row;
1142        int column_width = 0;
1143        int nfiles = num_matches;
1144        int nrows = nfiles;
1145        int l;
1146
1147        /* find the longest file name - use that as the column width */
1148        for (row = 0; row < nrows; row++) {
1149                l = unicode_strwidth(matches[row]);
1150                if (column_width < l)
1151                        column_width = l;
1152        }
1153        column_width += 2;              /* min space for columns */
1154        ncols = cmdedit_termw / column_width;
1155
1156        if (ncols > 1) {
1157                nrows /= ncols;
1158                if (nfiles % ncols)
1159                        nrows++;        /* round up fractionals */
1160        } else {
1161                ncols = 1;
1162        }
1163        for (row = 0; row < nrows; row++) {
1164                int n = row;
1165                int nc;
1166
1167                for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
1168                        fprintf(stderr, "%s%-*s", matches[n],
1169                                (int)(column_width - unicode_strwidth(matches[n])), ""
1170                        );
1171                }
1172                if (ENABLE_UNICODE_SUPPORT)
1173                        puts(printable_string(matches[n]));
1174                else
1175                        puts(matches[n]);
1176        }
1177}
1178
1179static const char *is_special_char(char c)
1180{
1181        // {: It's mandatory to escape { only if entire name is "{"
1182        // (otherwise it's not special. Example: file named "{ "
1183        // can be escaped simply as "{\ "; "{a" or "a{" need no escaping),
1184        // or if shell supports brace expansion
1185        // (ash doesn't, hush optionally does).
1186        // (): unlike {, shell treats () specially even in contexts
1187        // where they clearly are not valid (e.g. "echo )" is an error).
1188        // #: needs escaping to not start a shell comment.
1189        return strchr(" `'\"\\#$~?*[{()&;|<>", c);
1190        // Used to also have %^=+}]: but not necessary to escape?
1191}
1192
1193static char *quote_special_chars(char *found)
1194{
1195        int l = 0;
1196        char *s = xzalloc((strlen(found) + 1) * 2);
1197
1198        while (*found) {
1199                if (is_special_char(*found))
1200                        s[l++] = '\\';
1201                s[l++] = *found++;
1202        }
1203        /* s[l] = '\0'; - already is */
1204        return s;
1205}
1206
1207/* Do TAB completion */
1208static NOINLINE void input_tab(smallint *lastWasTab)
1209{
1210        char *chosen_match;
1211        char *match_buf;
1212        size_t len_found;
1213        /* Length of string used for matching */
1214        unsigned match_pfx_len = match_pfx_len;
1215        int find_type;
1216# if ENABLE_UNICODE_SUPPORT
1217        /* cursor pos in command converted to multibyte form */
1218        int cursor_mb;
1219# endif
1220        if (!(state->flags & TAB_COMPLETION))
1221                return;
1222
1223        if (*lastWasTab) {
1224                /* The last char was a TAB too.
1225                 * Print a list of all the available choices.
1226                 */
1227                if (num_matches > 0) {
1228                        /* cursor will be changed by goto_new_line() */
1229                        int sav_cursor = cursor;
1230                        goto_new_line();
1231                        showfiles();
1232                        draw_full(command_len - sav_cursor);
1233                }
1234                return;
1235        }
1236
1237        *lastWasTab = 1;
1238        chosen_match = NULL;
1239
1240        /* Make a local copy of the string up to the position of the cursor.
1241         * build_match_prefix will expand it into int16_t's, need to allocate
1242         * twice as much as the string_len+1.
1243         * (we then also (ab)use this extra space later - see (**))
1244         */
1245        match_buf = xmalloc(MAX_LINELEN * sizeof(int16_t));
1246# if !ENABLE_UNICODE_SUPPORT
1247        save_string(match_buf, cursor + 1); /* +1 for NUL */
1248# else
1249        {
1250                CHAR_T wc = command_ps[cursor];
1251                command_ps[cursor] = BB_NUL;
1252                save_string(match_buf, MAX_LINELEN);
1253                command_ps[cursor] = wc;
1254                cursor_mb = strlen(match_buf);
1255        }
1256# endif
1257        find_type = build_match_prefix(match_buf);
1258
1259        /* Free up any memory already allocated */
1260        free_tab_completion_data();
1261
1262# if ENABLE_FEATURE_USERNAME_COMPLETION
1263        /* If the word starts with ~ and there is no slash in the word,
1264         * then try completing this word as a username. */
1265        if (state->flags & USERNAME_COMPLETION)
1266                if (match_buf[0] == '~' && strchr(match_buf, '/') == NULL)
1267                        match_pfx_len = complete_username(match_buf);
1268# endif
1269        /* If complete_username() did not match,
1270         * try to match a command in $PATH, or a directory, or a file */
1271        if (!matches)
1272                match_pfx_len = complete_cmd_dir_file(match_buf, find_type);
1273
1274        /* Account for backslashes which will be inserted
1275         * by quote_special_chars() later */
1276        {
1277                const char *e = match_buf + strlen(match_buf);
1278                const char *s = e - match_pfx_len;
1279                while (s < e)
1280                        if (is_special_char(*s++))
1281                                match_pfx_len++;
1282        }
1283
1284        /* Remove duplicates */
1285        if (matches) {
1286                unsigned i, n = 0;
1287                qsort_string_vector(matches, num_matches);
1288                for (i = 0; i < num_matches - 1; ++i) {
1289                        //if (matches[i] && matches[i+1]) { /* paranoia */
1290                                if (strcmp(matches[i], matches[i+1]) == 0) {
1291                                        free(matches[i]);
1292                                        //matches[i] = NULL; /* paranoia */
1293                                } else {
1294                                        matches[n++] = matches[i];
1295                                }
1296                        //}
1297                }
1298                matches[n++] = matches[i];
1299                num_matches = n;
1300        }
1301
1302        /* Did we find exactly one match? */
1303        if (num_matches != 1) { /* no */
1304                char *cp;
1305                beep();
1306                if (!matches)
1307                        goto ret; /* no matches at all */
1308                /* Find common prefix */
1309                chosen_match = xstrdup(matches[0]);
1310                for (cp = chosen_match; *cp; cp++) {
1311                        unsigned n;
1312                        for (n = 1; n < num_matches; n++) {
1313                                if (matches[n][cp - chosen_match] != *cp) {
1314                                        goto stop;
1315                                }
1316                        }
1317                }
1318 stop:
1319                if (cp == chosen_match) { /* have unique prefix? */
1320                        goto ret; /* no */
1321                }
1322                *cp = '\0';
1323                cp = quote_special_chars(chosen_match);
1324                free(chosen_match);
1325                chosen_match = cp;
1326                len_found = strlen(chosen_match);
1327        } else {                        /* exactly one match */
1328                /* Next <tab> is not a double-tab */
1329                *lastWasTab = 0;
1330
1331                chosen_match = quote_special_chars(matches[0]);
1332                len_found = strlen(chosen_match);
1333                if (chosen_match[len_found-1] != '/') {
1334                        chosen_match[len_found] = ' ';
1335                        chosen_match[++len_found] = '\0';
1336                }
1337        }
1338
1339# if !ENABLE_UNICODE_SUPPORT
1340        /* Have space to place the match? */
1341        /* The result consists of three parts with these lengths: */
1342        /* cursor + (len_found - match_pfx_len) + (command_len - cursor) */
1343        /* it simplifies into: */
1344        if ((int)(len_found - match_pfx_len + command_len) < S.maxsize) {
1345                int pos;
1346                /* save tail */
1347                strcpy(match_buf, &command_ps[cursor]);
1348                /* add match and tail */
1349                sprintf(&command_ps[cursor], "%s%s", chosen_match + match_pfx_len, match_buf);
1350                command_len = strlen(command_ps);
1351                /* new pos */
1352                pos = cursor + len_found - match_pfx_len;
1353                /* write out the matched command */
1354                redraw(cmdedit_y, command_len - pos);
1355        }
1356# else
1357        {
1358                /* Use 2nd half of match_buf as scratch space - see (**) */
1359                char *command = match_buf + MAX_LINELEN;
1360                int len = save_string(command, MAX_LINELEN);
1361                /* Have space to place the match? */
1362                /* cursor_mb + (len_found - match_pfx_len) + (len - cursor_mb) */
1363                if ((int)(len_found - match_pfx_len + len) < MAX_LINELEN) {
1364                        int pos;
1365                        /* save tail */
1366                        strcpy(match_buf, &command[cursor_mb]);
1367                        /* where do we want to have cursor after all? */
1368                        strcpy(&command[cursor_mb], chosen_match + match_pfx_len);
1369                        len = load_string(command);
1370                        /* add match and tail */
1371                        stpcpy(stpcpy(&command[cursor_mb], chosen_match + match_pfx_len), match_buf);
1372                        command_len = load_string(command);
1373                        /* write out the matched command */
1374                        /* paranoia: load_string can return 0 on conv error,
1375                         * prevent passing pos = (0 - 12) to redraw */
1376                        pos = command_len - len;
1377                        redraw(cmdedit_y, pos >= 0 ? pos : 0);
1378                }
1379        }
1380# endif
1381 ret:
1382        free(chosen_match);
1383        free(match_buf);
1384}
1385
1386#endif  /* FEATURE_TAB_COMPLETION */
1387
1388
1389line_input_t* FAST_FUNC new_line_input_t(int flags)
1390{
1391        line_input_t *n = xzalloc(sizeof(*n));
1392        n->flags = flags;
1393        n->timeout = -1;
1394#if MAX_HISTORY > 0
1395        n->max_history = MAX_HISTORY;
1396#endif
1397        return n;
1398}
1399
1400
1401#if MAX_HISTORY > 0
1402
1403unsigned FAST_FUNC size_from_HISTFILESIZE(const char *hp)
1404{
1405        int size = MAX_HISTORY;
1406        if (hp) {
1407                size = atoi(hp);
1408                if (size <= 0)
1409                        return 1;
1410                if (size > MAX_HISTORY)
1411                        return MAX_HISTORY;
1412        }
1413        return size;
1414}
1415
1416static void save_command_ps_at_cur_history(void)
1417{
1418        if (command_ps[0] != BB_NUL) {
1419                int cur = state->cur_history;
1420                free(state->history[cur]);
1421
1422# if ENABLE_UNICODE_SUPPORT
1423                {
1424                        char tbuf[MAX_LINELEN];
1425                        save_string(tbuf, sizeof(tbuf));
1426                        state->history[cur] = xstrdup(tbuf);
1427                }
1428# else
1429                state->history[cur] = xstrdup(command_ps);
1430# endif
1431        }
1432}
1433
1434/* state->flags is already checked to be nonzero */
1435static int get_previous_history(void)
1436{
1437        if ((state->flags & DO_HISTORY) && state->cur_history) {
1438                save_command_ps_at_cur_history();
1439                state->cur_history--;
1440                return 1;
1441        }
1442        beep();
1443        return 0;
1444}
1445
1446static int get_next_history(void)
1447{
1448        if (state->flags & DO_HISTORY) {
1449                if (state->cur_history < state->cnt_history) {
1450                        save_command_ps_at_cur_history(); /* save the current history line */
1451                        return ++state->cur_history;
1452                }
1453        }
1454        beep();
1455        return 0;
1456}
1457
1458/* Lists command history. Used by shell 'history' builtins */
1459void FAST_FUNC show_history(const line_input_t *st)
1460{
1461        int i;
1462
1463        if (!st)
1464                return;
1465        for (i = 0; i < st->cnt_history; i++)
1466                fprintf(stderr, "%4d %s\n", i, st->history[i]);
1467}
1468
1469# if ENABLE_FEATURE_EDITING_SAVEHISTORY
1470void FAST_FUNC free_line_input_t(line_input_t *n)
1471{
1472        if (n) {
1473                int i = n->cnt_history;
1474                while (i > 0)
1475                        free(n->history[--i]);
1476                free(n);
1477        }
1478}
1479# else
1480/* #defined to free() in libbb.h */
1481# endif
1482
1483# if ENABLE_FEATURE_EDITING_SAVEHISTORY
1484/* We try to ensure that concurrent additions to the history
1485 * do not overwrite each other.
1486 * Otherwise shell users get unhappy.
1487 *
1488 * History file is trimmed lazily, when it grows several times longer
1489 * than configured MAX_HISTORY lines.
1490 */
1491
1492/* state->flags is already checked to be nonzero */
1493static void load_history(line_input_t *st_parm)
1494{
1495        char *temp_h[MAX_HISTORY];
1496        char *line;
1497        FILE *fp;
1498        unsigned idx, i, line_len;
1499
1500        /* NB: do not trash old history if file can't be opened */
1501
1502        fp = fopen_for_read(st_parm->hist_file);
1503        if (fp) {
1504                /* clean up old history */
1505                for (idx = st_parm->cnt_history; idx > 0;) {
1506                        idx--;
1507                        free(st_parm->history[idx]);
1508                        st_parm->history[idx] = NULL;
1509                }
1510
1511                /* fill temp_h[], retaining only last MAX_HISTORY lines */
1512                memset(temp_h, 0, sizeof(temp_h));
1513                idx = 0;
1514                st_parm->cnt_history_in_file = 0;
1515                while ((line = xmalloc_fgetline(fp)) != NULL) {
1516                        if (line[0] == '\0') {
1517                                free(line);
1518                                continue;
1519                        }
1520                        free(temp_h[idx]);
1521                        temp_h[idx] = line;
1522                        st_parm->cnt_history_in_file++;
1523                        idx++;
1524                        if (idx == st_parm->max_history)
1525                                idx = 0;
1526                }
1527                fclose(fp);
1528
1529                /* find first non-NULL temp_h[], if any */
1530                if (st_parm->cnt_history_in_file) {
1531                        while (temp_h[idx] == NULL) {
1532                                idx++;
1533                                if (idx == st_parm->max_history)
1534                                        idx = 0;
1535                        }
1536                }
1537
1538                /* copy temp_h[] to st_parm->history[] */
1539                for (i = 0; i < st_parm->max_history;) {
1540                        line = temp_h[idx];
1541                        if (!line)
1542                                break;
1543                        idx++;
1544                        if (idx == st_parm->max_history)
1545                                idx = 0;
1546                        line_len = strlen(line);
1547                        if (line_len >= MAX_LINELEN)
1548                                line[MAX_LINELEN-1] = '\0';
1549                        st_parm->history[i++] = line;
1550                }
1551                st_parm->cnt_history = i;
1552                if (ENABLE_FEATURE_EDITING_SAVE_ON_EXIT)
1553                        st_parm->cnt_history_in_file = i;
1554        }
1555}
1556
1557#  if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1558void save_history(line_input_t *st)
1559{
1560        FILE *fp;
1561
1562        if (!st || !st->hist_file)
1563                return;
1564        if (st->cnt_history <= st->cnt_history_in_file)
1565                return;
1566
1567        fp = fopen(st->hist_file, "a");
1568        if (fp) {
1569                int i, fd;
1570                char *new_name;
1571                line_input_t *st_temp;
1572
1573                for (i = st->cnt_history_in_file; i < st->cnt_history; i++)
1574                        fprintf(fp, "%s\n", st->history[i]);
1575                fclose(fp);
1576
1577                /* we may have concurrently written entries from others.
1578                 * load them */
1579                st_temp = new_line_input_t(st->flags);
1580                st_temp->hist_file = st->hist_file;
1581                st_temp->max_history = st->max_history;
1582                load_history(st_temp);
1583
1584                /* write out temp file and replace hist_file atomically */
1585                new_name = xasprintf("%s.%u.new", st->hist_file, (int) getpid());
1586                fd = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1587                if (fd >= 0) {
1588                        fp = xfdopen_for_write(fd);
1589                        for (i = 0; i < st_temp->cnt_history; i++)
1590                                fprintf(fp, "%s\n", st_temp->history[i]);
1591                        fclose(fp);
1592                        if (rename(new_name, st->hist_file) == 0)
1593                                st->cnt_history_in_file = st_temp->cnt_history;
1594                }
1595                free(new_name);
1596                free_line_input_t(st_temp);
1597        }
1598}
1599#  else
1600static void save_history(char *str)
1601{
1602        int fd;
1603        int len, len2;
1604
1605        if (!state->hist_file)
1606                return;
1607
1608        fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0600);
1609        if (fd < 0)
1610                return;
1611        xlseek(fd, 0, SEEK_END); /* paranoia */
1612        len = strlen(str);
1613        str[len] = '\n'; /* we (try to) do atomic write */
1614        len2 = full_write(fd, str, len + 1);
1615        str[len] = '\0';
1616        close(fd);
1617        if (len2 != len + 1)
1618                return; /* "wtf?" */
1619
1620        /* did we write so much that history file needs trimming? */
1621        state->cnt_history_in_file++;
1622        if (state->cnt_history_in_file > state->max_history * 4) {
1623                char *new_name;
1624                line_input_t *st_temp;
1625
1626                /* we may have concurrently written entries from others.
1627                 * load them */
1628                st_temp = new_line_input_t(state->flags);
1629                st_temp->hist_file = state->hist_file;
1630                st_temp->max_history = state->max_history;
1631                load_history(st_temp);
1632
1633                /* write out temp file and replace hist_file atomically */
1634                new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid());
1635                fd = open(new_name, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1636                if (fd >= 0) {
1637                        FILE *fp;
1638                        int i;
1639
1640                        fp = xfdopen_for_write(fd);
1641                        for (i = 0; i < st_temp->cnt_history; i++)
1642                                fprintf(fp, "%s\n", st_temp->history[i]);
1643                        fclose(fp);
1644                        if (rename(new_name, state->hist_file) == 0)
1645                                state->cnt_history_in_file = st_temp->cnt_history;
1646                }
1647                free(new_name);
1648                free_line_input_t(st_temp);
1649        }
1650}
1651#  endif
1652# else
1653#  define load_history(a) ((void)0)
1654#  define save_history(a) ((void)0)
1655# endif /* FEATURE_COMMAND_SAVEHISTORY */
1656
1657static void remember_in_history(char *str)
1658{
1659        int i;
1660
1661        if (!(state->flags & DO_HISTORY))
1662                return;
1663        if (str[0] == '\0')
1664                return;
1665        i = state->cnt_history;
1666        /* Don't save dupes */
1667        if (i && strcmp(state->history[i-1], str) == 0)
1668                return;
1669
1670        free(state->history[state->max_history]); /* redundant, paranoia */
1671        state->history[state->max_history] = NULL; /* redundant, paranoia */
1672
1673        /* If history[] is full, remove the oldest command */
1674        /* we need to keep history[state->max_history] empty, hence >=, not > */
1675        if (i >= state->max_history) {
1676                free(state->history[0]);
1677                for (i = 0; i < state->max_history-1; i++)
1678                        state->history[i] = state->history[i+1];
1679                /* i == state->max_history-1 */
1680# if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1681                if (state->cnt_history_in_file)
1682                        state->cnt_history_in_file--;
1683# endif
1684        }
1685        /* i <= state->max_history-1 */
1686        state->history[i++] = xstrdup(str);
1687        /* i <= state->max_history */
1688        state->cur_history = i;
1689        state->cnt_history = i;
1690# if ENABLE_FEATURE_EDITING_SAVEHISTORY && !ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
1691        save_history(str);
1692# endif
1693}
1694
1695#else /* MAX_HISTORY == 0 */
1696# define remember_in_history(a) ((void)0)
1697#endif /* MAX_HISTORY */
1698
1699
1700#if ENABLE_FEATURE_EDITING_VI
1701/*
1702 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
1703 */
1704static void
1705vi_Word_motion(int eat)
1706{
1707        CHAR_T *command = command_ps;
1708
1709        while (cursor < command_len && !BB_isspace(command[cursor]))
1710                input_forward();
1711        if (eat) while (cursor < command_len && BB_isspace(command[cursor]))
1712                input_forward();
1713}
1714
1715static void
1716vi_word_motion(int eat)
1717{
1718        CHAR_T *command = command_ps;
1719
1720        if (BB_isalnum_or_underscore(command[cursor])) {
1721                while (cursor < command_len
1722                 && (BB_isalnum_or_underscore(command[cursor+1]))
1723                ) {
1724                        input_forward();
1725                }
1726        } else if (BB_ispunct(command[cursor])) {
1727                while (cursor < command_len && BB_ispunct(command[cursor+1]))
1728                        input_forward();
1729        }
1730
1731        if (cursor < command_len)
1732                input_forward();
1733
1734        if (eat) {
1735                while (cursor < command_len && BB_isspace(command[cursor]))
1736                        input_forward();
1737        }
1738}
1739
1740static void
1741vi_End_motion(void)
1742{
1743        CHAR_T *command = command_ps;
1744
1745        input_forward();
1746        while (cursor < command_len && BB_isspace(command[cursor]))
1747                input_forward();
1748        while (cursor < command_len-1 && !BB_isspace(command[cursor+1]))
1749                input_forward();
1750}
1751
1752static void
1753vi_end_motion(void)
1754{
1755        CHAR_T *command = command_ps;
1756
1757        if (cursor >= command_len-1)
1758                return;
1759        input_forward();
1760        while (cursor < command_len-1 && BB_isspace(command[cursor]))
1761                input_forward();
1762        if (cursor >= command_len-1)
1763                return;
1764        if (BB_isalnum_or_underscore(command[cursor])) {
1765                while (cursor < command_len-1
1766                 && (BB_isalnum_or_underscore(command[cursor+1]))
1767                ) {
1768                        input_forward();
1769                }
1770        } else if (BB_ispunct(command[cursor])) {
1771                while (cursor < command_len-1 && BB_ispunct(command[cursor+1]))
1772                        input_forward();
1773        }
1774}
1775
1776static void
1777vi_Back_motion(void)
1778{
1779        CHAR_T *command = command_ps;
1780
1781        while (cursor > 0 && BB_isspace(command[cursor-1]))
1782                input_backward(1);
1783        while (cursor > 0 && !BB_isspace(command[cursor-1]))
1784                input_backward(1);
1785}
1786
1787static void
1788vi_back_motion(void)
1789{
1790        CHAR_T *command = command_ps;
1791
1792        if (cursor <= 0)
1793                return;
1794        input_backward(1);
1795        while (cursor > 0 && BB_isspace(command[cursor]))
1796                input_backward(1);
1797        if (cursor <= 0)
1798                return;
1799        if (BB_isalnum_or_underscore(command[cursor])) {
1800                while (cursor > 0
1801                 && (BB_isalnum_or_underscore(command[cursor-1]))
1802                ) {
1803                        input_backward(1);
1804                }
1805        } else if (BB_ispunct(command[cursor])) {
1806                while (cursor > 0 && BB_ispunct(command[cursor-1]))
1807                        input_backward(1);
1808        }
1809}
1810#endif /* ENABLE_FEATURE_EDITING_VI */
1811
1812/* Modelled after bash 4.0 behavior of Ctrl-<arrow> */
1813static void ctrl_left(void)
1814{
1815        CHAR_T *command = command_ps;
1816
1817        while (1) {
1818                CHAR_T c;
1819
1820                input_backward(1);
1821                if (cursor == 0)
1822                        break;
1823                c = command[cursor];
1824                if (c != ' ' && !BB_ispunct(c)) {
1825                        /* we reached a "word" delimited by spaces/punct.
1826                         * go to its beginning */
1827                        while (1) {
1828                                c = command[cursor - 1];
1829                                if (c == ' ' || BB_ispunct(c))
1830                                        break;
1831                                input_backward(1);
1832                                if (cursor == 0)
1833                                        break;
1834                        }
1835                        break;
1836                }
1837        }
1838}
1839static void ctrl_right(void)
1840{
1841        CHAR_T *command = command_ps;
1842
1843        while (1) {
1844                CHAR_T c;
1845
1846                c = command[cursor];
1847                if (c == BB_NUL)
1848                        break;
1849                if (c != ' ' && !BB_ispunct(c)) {
1850                        /* we reached a "word" delimited by spaces/punct.
1851                         * go to its end + 1 */
1852                        while (1) {
1853                                input_forward();
1854                                c = command[cursor];
1855                                if (c == BB_NUL || c == ' ' || BB_ispunct(c))
1856                                        break;
1857                        }
1858                        break;
1859                }
1860                input_forward();
1861        }
1862}
1863
1864
1865/*
1866 * read_line_input and its helpers
1867 */
1868
1869#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
1870static void ask_terminal(void)
1871{
1872        /* Ask terminal where is the cursor now.
1873         * lineedit_read_key handles response and corrects
1874         * our idea of current cursor position.
1875         * Testcase: run "echo -n long_line_long_line_long_line",
1876         * then type in a long, wrapping command and try to
1877         * delete it using backspace key.
1878         * Note: we print it _after_ prompt, because
1879         * prompt may contain CR. Example: PS1='\[\r\n\]\w '
1880         */
1881        /* Problem: if there is buffered input on stdin,
1882         * the response will be delivered later,
1883         * possibly to an unsuspecting application.
1884         * Testcase: "sleep 1; busybox ash" + press and hold [Enter].
1885         * Result:
1886         * ~/srcdevel/bbox/fix/busybox.t4 #
1887         * ~/srcdevel/bbox/fix/busybox.t4 #
1888         * ^[[59;34~/srcdevel/bbox/fix/busybox.t4 #  <-- garbage
1889         * ~/srcdevel/bbox/fix/busybox.t4 #
1890         *
1891         * Checking for input with poll only makes the race narrower,
1892         * I still can trigger it. Strace:
1893         *
1894         * write(1, "~/srcdevel/bbox/fix/busybox.t4 # ", 33) = 33
1895         * poll([{fd=0, events=POLLIN}], 1, 0) = 0 (Timeout)  <-- no input exists
1896         * write(1, "\33[6n", 4) = 4  <-- send the ESC sequence, quick!
1897         * poll([{fd=0, events=POLLIN}], 1, -1) = 1 ([{fd=0, revents=POLLIN}])
1898         * read(0, "\n", 1)      = 1  <-- oh crap, user's input got in first
1899         */
1900        struct pollfd pfd;
1901
1902        pfd.fd = STDIN_FILENO;
1903        pfd.events = POLLIN;
1904        if (safe_poll(&pfd, 1, 0) == 0) {
1905                S.sent_ESC_br6n = 1;
1906                fputs(ESC"[6n", stderr);
1907                fflush_all(); /* make terminal see it ASAP! */
1908        }
1909}
1910#else
1911# define ask_terminal() ((void)0)
1912#endif
1913
1914/* Note about multi-line PS1 (e.g. "\n\w \u@\h\n> ") and prompt redrawing:
1915 *
1916 * If the prompt has any newlines, after we print it once we use only its last
1917 * line to redraw in-place, which makes it simpler to calculate how many lines
1918 * we should move the cursor up to align the redraw (cmdedit_y). The earlier
1919 * prompt lines just stay on screen and we redraw below them.
1920 *
1921 * Use cases for all prompt lines beyond the initial draw:
1922 * - After clear-screen (^L) or after displaying tab-completion choices, we
1923 *   print the full prompt, as it isn't redrawn in-place.
1924 * - During terminal resize we could try to redraw all lines, but we don't,
1925 *   because it requires delicate alignment, it's good enough with only the
1926 *   last line, and doing it wrong is arguably worse than not doing it at all.
1927 *
1928 * Terminology wise, if it doesn't mention "full", then it means the last/sole
1929 * prompt line. We use the prompt (last/sole line) while redrawing in-place,
1930 * and the full where we need a fresh one unrelated to an earlier position.
1931 *
1932 * If PS1 is not multiline, the last/sole line and the full are the same string.
1933 */
1934
1935/* Called just once at read_line_input() init time */
1936#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
1937static void parse_and_put_prompt(const char *prmt_ptr)
1938{
1939        const char *p;
1940        cmdedit_prompt = prompt_last_line = prmt_ptr;
1941        p = strrchr(prmt_ptr, '\n');
1942        if (p)
1943                prompt_last_line = p + 1;
1944        cmdedit_prmt_len = unicode_strwidth(prompt_last_line);
1945        put_prompt();
1946}
1947#else
1948static void parse_and_put_prompt(const char *prmt_ptr)
1949{
1950        int prmt_size = 0;
1951        char *prmt_mem_ptr = xzalloc(1);
1952        char *cwd_buf = NULL;
1953        char flg_not_length = '[';
1954        char cbuf[2];
1955
1956        /*cmdedit_prmt_len = 0; - already is */
1957
1958        cbuf[1] = '\0'; /* never changes */
1959
1960        while (*prmt_ptr) {
1961                char timebuf[sizeof("HH:MM:SS")];
1962                char *free_me = NULL;
1963                char *pbuf;
1964                char c;
1965
1966                pbuf = cbuf;
1967                c = *prmt_ptr++;
1968                if (c == '\\') {
1969                        const char *cp;
1970                        int l;
1971/*
1972 * Supported via bb_process_escape_sequence:
1973 * \a   ASCII bell character (07)
1974 * \e   ASCII escape character (033)
1975 * \n   newline
1976 * \r   carriage return
1977 * \\   backslash
1978 * \nnn char with octal code nnn
1979 * Supported:
1980 * \$   if the effective UID is 0, a #, otherwise a $
1981 * \w   current working directory, with $HOME abbreviated with a tilde
1982 *      Note: we do not support $PROMPT_DIRTRIM=n feature
1983 * \W   basename of the current working directory, with $HOME abbreviated with a tilde
1984 * \h   hostname up to the first '.'
1985 * \H   hostname
1986 * \u   username
1987 * \[   begin a sequence of non-printing characters
1988 * \]   end a sequence of non-printing characters
1989 * \T   current time in 12-hour HH:MM:SS format
1990 * \@   current time in 12-hour am/pm format
1991 * \A   current time in 24-hour HH:MM format
1992 * \t   current time in 24-hour HH:MM:SS format
1993 *      (all of the above work as \A)
1994 * Not supported:
1995 * \!   history number of this command
1996 * \#   command number of this command
1997 * \j   number of jobs currently managed by the shell
1998 * \l   basename of the shell's terminal device name
1999 * \s   name of the shell, the basename of $0 (the portion following the final slash)
2000 * \V   release of bash, version + patch level (e.g., 2.00.0)
2001 * \d   date in "Weekday Month Date" format (e.g., "Tue May 26")
2002 * \D{format}
2003 *      format is passed to strftime(3).
2004 *      An empty format results in a locale-specific time representation.
2005 *      The braces are required.
2006 * Mishandled by bb_process_escape_sequence:
2007 * \v   version of bash (e.g., 2.00)
2008 */
2009                        cp = prmt_ptr;
2010                        c = *cp;
2011                        if (c != 't') /* don't treat \t as tab */
2012                                c = bb_process_escape_sequence(&prmt_ptr);
2013                        if (prmt_ptr == cp) {
2014                                if (*cp == '\0')
2015                                        break;
2016                                c = *prmt_ptr++;
2017
2018                                switch (c) {
2019                                case 'u':
2020                                        pbuf = (char*)get_username_str();
2021                                        break;
2022                                case 'H':
2023                                case 'h':
2024                                        pbuf = free_me = safe_gethostname();
2025                                        if (c == 'h')
2026                                                strchrnul(pbuf, '.')[0] = '\0';
2027                                        break;
2028                                case '$':
2029                                        c = (geteuid() == 0 ? '#' : '$');
2030                                        break;
2031                                case 'T': /* 12-hour HH:MM:SS format */
2032                                case '@': /* 12-hour am/pm format */
2033                                case 'A': /* 24-hour HH:MM format */
2034                                case 't': /* 24-hour HH:MM:SS format */
2035                                        /* We show all of them as 24-hour HH:MM */
2036                                        strftime_HHMMSS(timebuf, sizeof(timebuf), NULL)[-3] = '\0';
2037                                        pbuf = timebuf;
2038                                        break;
2039                                case 'w': /* current dir */
2040                                case 'W': /* basename of cur dir */
2041                                        if (!cwd_buf) {
2042                                                const char *home;
2043# if EDITING_HAS_sh_get_var
2044                                                cwd_buf = state && state->sh_get_var
2045                                                        ? xstrdup(state->sh_get_var("PWD"))
2046                                                        : xrealloc_getcwd_or_warn(NULL);
2047# else
2048                                                cwd_buf = xrealloc_getcwd_or_warn(NULL);
2049# endif
2050                                                if (!cwd_buf)
2051                                                        cwd_buf = (char *)bb_msg_unknown;
2052                                                else if ((home = get_homedir_or_NULL()) != NULL && home[0]) {
2053                                                        char *after_home_user;
2054
2055                                                        /* /home/user[/something] -> ~[/something] */
2056                                                        after_home_user = is_prefixed_with(cwd_buf, home);
2057                                                        if (after_home_user
2058                                                         && (*after_home_user == '/' || *after_home_user == '\0')
2059                                                        ) {
2060                                                                cwd_buf[0] = '~';
2061                                                                overlapping_strcpy(cwd_buf + 1, after_home_user);
2062                                                        }
2063                                                }
2064                                        }
2065                                        pbuf = cwd_buf;
2066                                        if (c == 'w')
2067                                                break;
2068                                        cp = strrchr(pbuf, '/');
2069                                        if (cp)
2070                                                pbuf = (char*)cp + 1;
2071                                        break;
2072// bb_process_escape_sequence does this now:
2073//                              case 'e': case 'E':     /* \e \E = \033 */
2074//                                      c = '\033';
2075//                                      break;
2076                                case 'x': case 'X': {
2077                                        char buf2[4];
2078                                        for (l = 0; l < 3;) {
2079                                                unsigned h;
2080                                                buf2[l++] = *prmt_ptr;
2081                                                buf2[l] = '\0';
2082                                                h = strtoul(buf2, &pbuf, 16);
2083                                                if (h > UCHAR_MAX || (pbuf - buf2) < l) {
2084                                                        buf2[--l] = '\0';
2085                                                        break;
2086                                                }
2087                                                prmt_ptr++;
2088                                        }
2089                                        c = (char)strtoul(buf2, NULL, 16);
2090                                        if (c == 0)
2091                                                c = '?';
2092                                        pbuf = cbuf;
2093                                        break;
2094                                }
2095                                case '[': case ']':
2096                                        if (c == flg_not_length) {
2097                                                /* Toggle '['/']' hex 5b/5d */
2098                                                flg_not_length ^= 6;
2099                                                continue;
2100                                        }
2101                                        break;
2102                                } /* switch */
2103                        } /* if */
2104                } /* if */
2105                cbuf[0] = c;
2106                {
2107                        int n = strlen(pbuf);
2108                        prmt_size += n;
2109                        if (c == '\n')
2110                                cmdedit_prmt_len = 0;
2111                        else if (flg_not_length != ']') {
2112# if ENABLE_UNICODE_SUPPORT
2113                                if (n == 1) {
2114                                        /* Only count single-byte characters and the first of multi-byte characters */
2115                                        if ((unsigned char)*pbuf < 0x80  /* single byte character */
2116                                         || (unsigned char)*pbuf >= 0xc0 /* first of multi-byte characters */
2117                                        ) {
2118                                                cmdedit_prmt_len += n;
2119                                        }
2120                                } else {
2121                                        cmdedit_prmt_len += unicode_strwidth(pbuf);
2122                                }
2123# else
2124                                cmdedit_prmt_len += n;
2125# endif
2126                        }
2127                }
2128                prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_size+1), pbuf);
2129                free(free_me);
2130        } /* while */
2131
2132        if (cwd_buf != (char *)bb_msg_unknown)
2133                free(cwd_buf);
2134        /* see comment (above this function) about multiline prompt redrawing */
2135        cmdedit_prompt = prompt_last_line = prmt_mem_ptr;
2136        prmt_ptr = strrchr(cmdedit_prompt, '\n');
2137        if (prmt_ptr)
2138                prompt_last_line = prmt_ptr + 1;
2139        put_prompt();
2140}
2141#endif /* FEATURE_EDITING_FANCY_PROMPT */
2142
2143#if ENABLE_FEATURE_EDITING_WINCH
2144static void cmdedit_setwidth(void)
2145{
2146        int new_y;
2147
2148        cmdedit_termw = get_terminal_width(STDIN_FILENO);
2149        /* new y for current cursor */
2150        new_y = (cursor + cmdedit_prmt_len) / cmdedit_termw;
2151        /* redraw */
2152        redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
2153}
2154
2155static void win_changed(int nsig UNUSED_PARAM)
2156{
2157        if (S.ok_to_redraw) {
2158                /* We are in read_key(), safe to redraw immediately */
2159                int sv_errno = errno;
2160                cmdedit_setwidth();
2161                fflush_all();
2162                errno = sv_errno;
2163        } else {
2164                /* Signal main loop that redraw is necessary */
2165                S.SIGWINCH_count++;
2166        }
2167}
2168#endif
2169
2170static int lineedit_read_key(char *read_key_buffer, int timeout)
2171{
2172        int64_t ic;
2173#if ENABLE_UNICODE_SUPPORT
2174        char unicode_buf[MB_CUR_MAX + 1];
2175        int unicode_idx = 0;
2176#endif
2177
2178        fflush_all();
2179        for (;;) {
2180                /* Wait for input. TIMEOUT = -1 makes read_key wait even
2181                 * on nonblocking stdin, TIMEOUT = 50 makes sure we won't
2182                 * insist on full MB_CUR_MAX buffer to declare input like
2183                 * "\xff\n",pause,"ls\n" invalid and thus won't lose "ls".
2184                 *
2185                 * If LI_INTERRUPTIBLE, return -1 if got EINTR in poll()
2186                 * inside read_key and bb_got_signal became != 0,
2187                 * or if bb_got_signal != 0 (IOW: if signal
2188                 * arrived before poll() is reached).
2189                 *
2190                 * Note: read_key sets errno to 0 on success.
2191                 */
2192                for (;;) {
2193                        if ((state->flags & LI_INTERRUPTIBLE) && bb_got_signal) {
2194                                errno = EINTR;
2195                                return -1;
2196                        }
2197//FIXME: still races here with signals, but small window to poll() inside read_key
2198                        IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 1;)
2199                        /* errno = 0; - read_key does this itself */
2200                        ic = read_key(STDIN_FILENO, read_key_buffer, timeout);
2201                        IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 0;)
2202                        if (errno != EINTR)
2203                                break;
2204                        /* It was EINTR. Repeat read_key() unless... */
2205                        if (state->flags & LI_INTERRUPTIBLE) {
2206                                /* LI_INTERRUPTIBLE bails out on EINTR
2207                                 * if bb_got_signal became nonzero.
2208                                 * (It may stay zero: for example, our SIGWINCH
2209                                 * handler does not set it. This is used for signals
2210                                 * which should not interrupt line editing).
2211                                 */
2212                                if (bb_got_signal != 0)
2213                                        goto ret; /* will return -1 */
2214                        }
2215                }
2216
2217                if (errno) {
2218#if ENABLE_UNICODE_SUPPORT
2219                        if (errno == EAGAIN && unicode_idx != 0)
2220                                goto pushback;
2221#endif
2222                        break;
2223                }
2224
2225#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
2226                if ((int32_t)ic == KEYCODE_CURSOR_POS
2227                 && S.sent_ESC_br6n
2228                ) {
2229                        S.sent_ESC_br6n = 0;
2230                        if (cursor == 0) { /* otherwise it may be bogus */
2231                                int col = ((ic >> 32) & 0x7fff) - 1;
2232                                /*
2233                                 * Is col > cmdedit_prmt_len?
2234                                 * If yes (terminal says cursor is farther to the right
2235                                 * of where we think it should be),
2236                                 * the prompt wasn't printed starting at col 1,
2237                                 * there was additional text before it.
2238                                 */
2239                                if ((int)(col - cmdedit_prmt_len) > 0) {
2240                                        /* Fix our understanding of current x position */
2241                                        cmdedit_x += (col - cmdedit_prmt_len);
2242                                        while (cmdedit_x >= cmdedit_termw) {
2243                                                cmdedit_x -= cmdedit_termw;
2244                                                cmdedit_y++;
2245                                        }
2246                                }
2247                        }
2248                        continue;
2249                }
2250#endif
2251
2252#if ENABLE_UNICODE_SUPPORT
2253                if (unicode_status == UNICODE_ON) {
2254                        wchar_t wc;
2255
2256                        if ((int32_t)ic < 0) /* KEYCODE_xxx */
2257                                break;
2258                        // TODO: imagine sequence like: 0xff,<left-arrow>: we are currently losing 0xff...
2259
2260                        unicode_buf[unicode_idx++] = ic;
2261                        unicode_buf[unicode_idx] = '\0';
2262                        if (mbstowcs(&wc, unicode_buf, 1) != 1) {
2263                                /* Not (yet?) a valid unicode char */
2264                                if (unicode_idx < MB_CUR_MAX) {
2265                                        timeout = 50;
2266                                        continue;
2267                                }
2268 pushback:
2269                                /* Invalid sequence. Save all "bad bytes" except first */
2270                                read_key_ungets(read_key_buffer, unicode_buf + 1, unicode_idx - 1);
2271# if !ENABLE_UNICODE_PRESERVE_BROKEN
2272                                ic = CONFIG_SUBST_WCHAR;
2273# else
2274                                ic = unicode_mark_raw_byte(unicode_buf[0]);
2275# endif
2276                        } else {
2277                                /* Valid unicode char, return its code */
2278                                ic = wc;
2279                        }
2280                }
2281#endif
2282                break;
2283        }
2284 ret:
2285        return ic;
2286}
2287
2288#if ENABLE_UNICODE_BIDI_SUPPORT
2289static int isrtl_str(void)
2290{
2291        int idx = cursor;
2292
2293        while (idx < command_len && unicode_bidi_is_neutral_wchar(command_ps[idx]))
2294                idx++;
2295        return unicode_bidi_isrtl(command_ps[idx]);
2296}
2297#else
2298# define isrtl_str() 0
2299#endif
2300
2301/* leave out the "vi-mode"-only case labels if vi editing isn't
2302 * configured. */
2303#define vi_case(caselabel) IF_FEATURE_EDITING_VI(case caselabel)
2304
2305/* convert uppercase ascii to equivalent control char, for readability */
2306#undef CTRL
2307#define CTRL(a) ((a) & ~0x40)
2308
2309enum {
2310        VI_CMDMODE_BIT = 0x40000000,
2311        /* 0x80000000 bit flags KEYCODE_xxx */
2312};
2313
2314#if ENABLE_FEATURE_REVERSE_SEARCH
2315/* Mimic readline Ctrl-R reverse history search.
2316 * When invoked, it shows the following prompt:
2317 * (reverse-i-search)'': user_input [cursor pos unchanged by Ctrl-R]
2318 * and typing results in search being performed:
2319 * (reverse-i-search)'tmp': cd /tmp [cursor under t in /tmp]
2320 * Search is performed by looking at progressively older lines in history.
2321 * Ctrl-R again searches for the next match in history.
2322 * Backspace deletes last matched char.
2323 * Control keys exit search and return to normal editing (at current history line).
2324 */
2325static int32_t reverse_i_search(int timeout)
2326{
2327        char match_buf[128]; /* for user input */
2328        char read_key_buffer[KEYCODE_BUFFER_SIZE];
2329        const char *matched_history_line;
2330        const char *saved_prompt;
2331        unsigned saved_prmt_len;
2332        int32_t ic;
2333
2334        matched_history_line = NULL;
2335        read_key_buffer[0] = 0;
2336        match_buf[0] = '\0';
2337
2338        /* Save and replace the prompt */
2339        saved_prompt = prompt_last_line;
2340        saved_prmt_len = cmdedit_prmt_len;
2341        goto set_prompt;
2342
2343        while (1) {
2344                int h;
2345                unsigned match_buf_len = strlen(match_buf);
2346
2347//FIXME: correct timeout? (i.e. count it down?)
2348                ic = lineedit_read_key(read_key_buffer, timeout);
2349
2350                switch (ic) {
2351                case CTRL('R'): /* searching for the next match */
2352                        break;
2353
2354                case '\b':
2355                case '\x7f':
2356                        /* Backspace */
2357                        if (unicode_status == UNICODE_ON) {
2358                                while (match_buf_len != 0) {
2359                                        uint8_t c = match_buf[--match_buf_len];
2360                                        if ((c & 0xc0) != 0x80) /* start of UTF-8 char? */
2361                                                break; /* yes */
2362                                }
2363                        } else {
2364                                if (match_buf_len != 0)
2365                                        match_buf_len--;
2366                        }
2367                        match_buf[match_buf_len] = '\0';
2368                        break;
2369
2370                default:
2371                        if (ic < ' '
2372                         || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
2373                         || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
2374                        ) {
2375                                goto ret;
2376                        }
2377
2378                        /* Append this char */
2379# if ENABLE_UNICODE_SUPPORT
2380                        if (unicode_status == UNICODE_ON) {
2381                                mbstate_t mbstate = { 0 };
2382                                char buf[MB_CUR_MAX + 1];
2383                                int len = wcrtomb(buf, ic, &mbstate);
2384                                if (len > 0) {
2385                                        buf[len] = '\0';
2386                                        if (match_buf_len + len < sizeof(match_buf))
2387                                                strcpy(match_buf + match_buf_len, buf);
2388                                }
2389                        } else
2390# endif
2391                        if (match_buf_len < sizeof(match_buf) - 1) {
2392                                match_buf[match_buf_len] = ic;
2393                                match_buf[match_buf_len + 1] = '\0';
2394                        }
2395                        break;
2396                } /* switch (ic) */
2397
2398                /* Search in history for match_buf */
2399                h = state->cur_history;
2400                if (ic == CTRL('R'))
2401                        h--;
2402                while (h >= 0) {
2403                        if (state->history[h]) {
2404                                char *match = strstr(state->history[h], match_buf);
2405                                if (match) {
2406                                        state->cur_history = h;
2407                                        matched_history_line = state->history[h];
2408                                        command_len = load_string(matched_history_line);
2409                                        cursor = match - matched_history_line;
2410//FIXME: cursor position for Unicode case
2411
2412                                        free((char*)prompt_last_line);
2413 set_prompt:
2414                                        prompt_last_line = xasprintf("(reverse-i-search)'%s': ", match_buf);
2415                                        cmdedit_prmt_len = unicode_strwidth(prompt_last_line);
2416                                        goto do_redraw;
2417                                }
2418                        }
2419                        h--;
2420                }
2421
2422                /* Not found */
2423                match_buf[match_buf_len] = '\0';
2424                beep();
2425                continue;
2426
2427 do_redraw:
2428                redraw(cmdedit_y, command_len - cursor);
2429        } /* while (1) */
2430
2431 ret:
2432        if (matched_history_line)
2433                command_len = load_string(matched_history_line);
2434
2435        free((char*)prompt_last_line);
2436        prompt_last_line = saved_prompt;
2437        cmdedit_prmt_len = saved_prmt_len;
2438        redraw(cmdedit_y, command_len - cursor);
2439
2440        return ic;
2441}
2442#endif /* ENABLE_FEATURE_REVERSE_SEARCH */
2443
2444#if ENABLE_FEATURE_EDITING_WINCH
2445static void sigaction2(int sig, struct sigaction *act)
2446{
2447        // Grr... gcc 8.1.1:
2448        // "passing argument 3 to restrict-qualified parameter aliases with argument 2"
2449        // dance around that...
2450        struct sigaction *oact FIX_ALIASING;
2451        oact = act;
2452        sigaction(sig, act, oact);
2453}
2454#endif
2455
2456/* maxsize must be >= 2.
2457 * Returns:
2458 * -1 on read errors or EOF, or on bare Ctrl-D,
2459 * 0  on ctrl-C (the line entered is still returned in 'command'),
2460 * (in both cases the cursor remains on the input line, '\n' is not printed)
2461 * >0 length of input string, including terminating '\n'
2462 */
2463int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize)
2464{
2465        int len, n;
2466        int timeout;
2467#if ENABLE_FEATURE_TAB_COMPLETION
2468        smallint lastWasTab = 0;
2469#endif
2470        smallint break_out = 0;
2471#if ENABLE_FEATURE_EDITING_VI
2472        smallint vi_cmdmode = 0;
2473#endif
2474        struct termios initial_settings;
2475        struct termios new_settings;
2476        char read_key_buffer[KEYCODE_BUFFER_SIZE];
2477
2478        INIT_S();
2479        //command_len = 0; - done by INIT_S()
2480        //cmdedit_y = 0;  /* quasireal y, not true if line > xt*yt */
2481        cmdedit_termw = 80;
2482        IF_FEATURE_EDITING_VI(delptr = delbuf;)
2483
2484        n = get_termios_and_make_raw(STDIN_FILENO, &new_settings, &initial_settings, 0
2485                | TERMIOS_CLEAR_ISIG /* turn off INTR (ctrl-C), QUIT, SUSP */
2486        );
2487        if (n != 0 || (initial_settings.c_lflag & (ECHO|ICANON)) == ICANON) {
2488                /* Happens when e.g. stty -echo was run before.
2489                 * But if ICANON is not set, we don't come here.
2490                 * (example: interactive python ^Z-backgrounded,
2491                 * tty is still in "raw mode").
2492                 */
2493                parse_and_put_prompt(prompt);
2494                fflush_all();
2495                if (fgets(command, maxsize, stdin) == NULL)
2496                        len = -1; /* EOF or error */
2497                else
2498                        len = strlen(command);
2499                DEINIT_S();
2500                return len;
2501        }
2502
2503        init_unicode();
2504
2505// FIXME: audit & improve this
2506        if (maxsize > MAX_LINELEN)
2507                maxsize = MAX_LINELEN;
2508        S.maxsize = maxsize;
2509
2510        timeout = -1;
2511        /* Make state->flags == 0 if st is NULL.
2512         * With zeroed flags, no other fields are ever referenced.
2513         */
2514        state = (line_input_t*) &const_int_0;
2515        if (st) {
2516                state = st;
2517                timeout = st->timeout;
2518        }
2519#if MAX_HISTORY > 0
2520        if (state->flags & DO_HISTORY) {
2521# if ENABLE_FEATURE_EDITING_SAVEHISTORY
2522                if (state->hist_file)
2523                        if (state->cnt_history == 0)
2524                                load_history(state);
2525# endif
2526                state->cur_history = state->cnt_history;
2527        }
2528#endif
2529
2530        /* prepare before init handlers */
2531#if ENABLE_UNICODE_SUPPORT
2532        command_ps = xzalloc(maxsize * sizeof(command_ps[0]));
2533#else
2534        command_ps = command;
2535        command[0] = '\0';
2536#endif
2537#define command command_must_not_be_used
2538
2539        tcsetattr_stdin_TCSANOW(&new_settings);
2540
2541#if 0
2542        for (i = 0; i <= state->max_history; i++)
2543                bb_error_msg("history[%d]:'%s'", i, state->history[i]);
2544        bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history);
2545#endif
2546
2547        /* Get width (before printing prompt) */
2548        cmdedit_termw = get_terminal_width(STDIN_FILENO);
2549        /* Print out the command prompt, optionally ask where cursor is */
2550        parse_and_put_prompt(prompt);
2551        ask_terminal();
2552
2553#if ENABLE_FEATURE_EDITING_WINCH
2554        /* Install window resize handler (NB: after *all* init is complete) */
2555        S.SIGWINCH_handler.sa_handler = win_changed;
2556        S.SIGWINCH_handler.sa_flags = SA_RESTART;
2557        sigaction2(SIGWINCH, &S.SIGWINCH_handler);
2558#endif
2559        read_key_buffer[0] = 0;
2560        while (1) {
2561                /*
2562                 * The emacs and vi modes share much of the code in the big
2563                 * command loop.  Commands entered when in vi's command mode
2564                 * (aka "escape mode") get an extra bit added to distinguish
2565                 * them - this keeps them from being self-inserted. This
2566                 * clutters the big switch a bit, but keeps all the code
2567                 * in one place.
2568                 */
2569                int32_t ic, ic_raw;
2570#if ENABLE_FEATURE_EDITING_WINCH
2571                unsigned count;
2572
2573                count = S.SIGWINCH_count;
2574                if (S.SIGWINCH_saved != count) {
2575                        S.SIGWINCH_saved = count;
2576                        cmdedit_setwidth();
2577                }
2578#endif
2579                ic = ic_raw = lineedit_read_key(read_key_buffer, timeout);
2580
2581#if ENABLE_FEATURE_REVERSE_SEARCH
2582 again:
2583#endif
2584#if ENABLE_FEATURE_EDITING_VI
2585                newdelflag = 1;
2586                if (vi_cmdmode) {
2587                        /* btw, since KEYCODE_xxx are all < 0, this doesn't
2588                         * change ic if it contains one of them: */
2589                        ic |= VI_CMDMODE_BIT;
2590                }
2591#endif
2592
2593                switch (ic) {
2594                case '\n':
2595                case '\r':
2596                vi_case('\n'|VI_CMDMODE_BIT:)
2597                vi_case('\r'|VI_CMDMODE_BIT:)
2598                        /* Enter */
2599                        goto_new_line();
2600                        break_out = 1;
2601                        break;
2602                case CTRL('A'):
2603                vi_case('0'|VI_CMDMODE_BIT:)
2604                        /* Control-a -- Beginning of line */
2605                        input_backward(cursor);
2606                        break;
2607                case CTRL('B'):
2608                vi_case('h'|VI_CMDMODE_BIT:)
2609                vi_case('\b'|VI_CMDMODE_BIT:) /* ^H */
2610                vi_case('\x7f'|VI_CMDMODE_BIT:) /* DEL */
2611                        input_backward(1); /* Move back one character */
2612                        break;
2613                case CTRL('E'):
2614                vi_case('$'|VI_CMDMODE_BIT:)
2615                        /* Control-e -- End of line */
2616                        put_till_end_and_adv_cursor();
2617                        break;
2618                case CTRL('F'):
2619                vi_case('l'|VI_CMDMODE_BIT:)
2620                vi_case(' '|VI_CMDMODE_BIT:)
2621                        input_forward(); /* Move forward one character */
2622                        break;
2623                case '\b':   /* ^H */
2624                case '\x7f': /* DEL */
2625                        if (!isrtl_str())
2626                                input_backspace();
2627                        else
2628                                input_delete(0);
2629                        break;
2630                case KEYCODE_DELETE:
2631                        if (!isrtl_str())
2632                                input_delete(0);
2633                        else
2634                                input_backspace();
2635                        break;
2636#if ENABLE_FEATURE_TAB_COMPLETION
2637                case '\t':
2638                        input_tab(&lastWasTab);
2639                        break;
2640#endif
2641                case CTRL('K'):
2642                        /* Control-k -- clear to end of line */
2643                        command_ps[cursor] = BB_NUL;
2644                        command_len = cursor;
2645                        fputs(SEQ_CLEAR_TILL_END_OF_SCREEN, stderr);
2646                        break;
2647                case CTRL('L'):
2648                vi_case(CTRL('L')|VI_CMDMODE_BIT:)
2649                        /* Control-l -- clear screen */
2650                        /* cursor to top,left; clear to the end of screen */
2651                        fputs(ESC"[H" ESC"[J", stderr);
2652                        draw_full(command_len - cursor);
2653                        break;
2654#if MAX_HISTORY > 0
2655                case CTRL('N'):
2656                vi_case(CTRL('N')|VI_CMDMODE_BIT:)
2657                vi_case('j'|VI_CMDMODE_BIT:)
2658                        /* Control-n -- Get next command in history */
2659                        if (get_next_history())
2660                                goto rewrite_line;
2661                        break;
2662                case CTRL('P'):
2663                vi_case(CTRL('P')|VI_CMDMODE_BIT:)
2664                vi_case('k'|VI_CMDMODE_BIT:)
2665                        /* Control-p -- Get previous command from history */
2666                        if (get_previous_history())
2667                                goto rewrite_line;
2668                        break;
2669#endif
2670                case CTRL('U'):
2671                vi_case(CTRL('U')|VI_CMDMODE_BIT:)
2672                        /* Control-U -- Clear line before cursor */
2673                        if (cursor) {
2674                                command_len -= cursor;
2675                                memmove(command_ps, command_ps + cursor,
2676                                        (command_len + 1) * sizeof(command_ps[0]));
2677                                redraw(cmdedit_y, command_len);
2678                        }
2679                        break;
2680                case CTRL('W'):
2681                vi_case(CTRL('W')|VI_CMDMODE_BIT:)
2682                        /* Control-W -- Remove the last word */
2683                        while (cursor > 0 && BB_isspace(command_ps[cursor-1]))
2684                                input_backspace();
2685                        while (cursor > 0 && !BB_isspace(command_ps[cursor-1]))
2686                                input_backspace();
2687                        break;
2688                case KEYCODE_ALT_D: {
2689                        /* Delete word forward */
2690                        int nc, sc = cursor;
2691                        ctrl_right();
2692                        nc = cursor - sc;
2693                        input_backward(nc);
2694                        while (--nc >= 0)
2695                                input_delete(1);
2696                        break;
2697                }
2698                case KEYCODE_ALT_BACKSPACE: {
2699                        /* Delete word backward */
2700                        int sc = cursor;
2701                        ctrl_left();
2702                        while (sc-- > cursor)
2703                                input_delete(1);
2704                        break;
2705                }
2706#if ENABLE_FEATURE_REVERSE_SEARCH
2707                case CTRL('R'):
2708                        ic = ic_raw = reverse_i_search(timeout);
2709                        goto again;
2710#endif
2711
2712#if ENABLE_FEATURE_EDITING_VI
2713                case 'i'|VI_CMDMODE_BIT:
2714                        vi_cmdmode = 0;
2715                        break;
2716                case 'I'|VI_CMDMODE_BIT:
2717                        input_backward(cursor);
2718                        vi_cmdmode = 0;
2719                        break;
2720                case 'a'|VI_CMDMODE_BIT:
2721                        input_forward();
2722                        vi_cmdmode = 0;
2723                        break;
2724                case 'A'|VI_CMDMODE_BIT:
2725                        put_till_end_and_adv_cursor();
2726                        vi_cmdmode = 0;
2727                        break;
2728                case 'x'|VI_CMDMODE_BIT:
2729                        input_delete(1);
2730                        break;
2731                case 'X'|VI_CMDMODE_BIT:
2732                        if (cursor > 0) {
2733                                input_backward(1);
2734                                input_delete(1);
2735                        }
2736                        break;
2737                case 'W'|VI_CMDMODE_BIT:
2738                        vi_Word_motion(1);
2739                        break;
2740                case 'w'|VI_CMDMODE_BIT:
2741                        vi_word_motion(1);
2742                        break;
2743                case 'E'|VI_CMDMODE_BIT:
2744                        vi_End_motion();
2745                        break;
2746                case 'e'|VI_CMDMODE_BIT:
2747                        vi_end_motion();
2748                        break;
2749                case 'B'|VI_CMDMODE_BIT:
2750                        vi_Back_motion();
2751                        break;
2752                case 'b'|VI_CMDMODE_BIT:
2753                        vi_back_motion();
2754                        break;
2755                case 'C'|VI_CMDMODE_BIT:
2756                        vi_cmdmode = 0;
2757                        /* fall through */
2758                case 'D'|VI_CMDMODE_BIT:
2759                        goto clear_to_eol;
2760
2761                case 'c'|VI_CMDMODE_BIT:
2762                        vi_cmdmode = 0;
2763                        /* fall through */
2764                case 'd'|VI_CMDMODE_BIT: {
2765                        int nc, sc;
2766
2767                        ic = lineedit_read_key(read_key_buffer, timeout);
2768                        if (errno) /* error */
2769                                goto return_error_indicator;
2770                        if (ic == ic_raw) { /* "cc", "dd" */
2771                                input_backward(cursor);
2772                                goto clear_to_eol;
2773                                break;
2774                        }
2775
2776                        sc = cursor;
2777                        switch (ic) {
2778                        case 'w':
2779                        case 'W':
2780                        case 'e':
2781                        case 'E':
2782                                switch (ic) {
2783                                case 'w':   /* "dw", "cw" */
2784                                        vi_word_motion(vi_cmdmode);
2785                                        break;
2786                                case 'W':   /* 'dW', 'cW' */
2787                                        vi_Word_motion(vi_cmdmode);
2788                                        break;
2789                                case 'e':   /* 'de', 'ce' */
2790                                        vi_end_motion();
2791                                        input_forward();
2792                                        break;
2793                                case 'E':   /* 'dE', 'cE' */
2794                                        vi_End_motion();
2795                                        input_forward();
2796                                        break;
2797                                }
2798                                nc = cursor;
2799                                input_backward(cursor - sc);
2800                                while (nc-- > cursor)
2801                                        input_delete(1);
2802                                break;
2803                        case 'b':  /* "db", "cb" */
2804                        case 'B':  /* implemented as B */
2805                                if (ic == 'b')
2806                                        vi_back_motion();
2807                                else
2808                                        vi_Back_motion();
2809                                while (sc-- > cursor)
2810                                        input_delete(1);
2811                                break;
2812                        case ' ':  /* "d ", "c " */
2813                                input_delete(1);
2814                                break;
2815                        case '$':  /* "d$", "c$" */
2816 clear_to_eol:
2817                                while (cursor < command_len)
2818                                        input_delete(1);
2819                                break;
2820                        }
2821                        break;
2822                }
2823                case 'p'|VI_CMDMODE_BIT:
2824                        input_forward();
2825                        /* fallthrough */
2826                case 'P'|VI_CMDMODE_BIT:
2827                        put();
2828                        break;
2829                case 'r'|VI_CMDMODE_BIT:
2830//FIXME: unicode case?
2831                        ic = lineedit_read_key(read_key_buffer, timeout);
2832                        if (errno) /* error */
2833                                goto return_error_indicator;
2834                        if (ic < ' ' || ic > 255) {
2835                                beep();
2836                        } else {
2837                                command_ps[cursor] = ic;
2838                                bb_putchar_stderr(ic);
2839                                bb_putchar_stderr('\b');
2840                        }
2841                        break;
2842                case '\x1b': /* ESC */
2843                        if (state->flags & VI_MODE) {
2844                                /* insert mode --> command mode */
2845                                vi_cmdmode = 1;
2846                                input_backward(1);
2847                        }
2848                        break;
2849#endif /* FEATURE_COMMAND_EDITING_VI */
2850
2851#if MAX_HISTORY > 0
2852                case KEYCODE_UP:
2853                        if (get_previous_history())
2854                                goto rewrite_line;
2855                        beep();
2856                        break;
2857                case KEYCODE_DOWN:
2858                        if (!get_next_history())
2859                                break;
2860 rewrite_line:
2861                        /* Rewrite the line with the selected history item */
2862                        /* change command */
2863                        command_len = load_string(state->history[state->cur_history] ?
2864                                        state->history[state->cur_history] : "");
2865                        /* redraw and go to eol (bol, in vi) */
2866                        redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
2867                        break;
2868#endif
2869                case KEYCODE_RIGHT:
2870                        input_forward();
2871                        break;
2872                case KEYCODE_LEFT:
2873                        input_backward(1);
2874                        break;
2875                case KEYCODE_CTRL_LEFT:
2876                case KEYCODE_ALT_LEFT: /* bash doesn't do it */
2877                        ctrl_left();
2878                        break;
2879                case KEYCODE_CTRL_RIGHT:
2880                case KEYCODE_ALT_RIGHT: /* bash doesn't do it */
2881                        ctrl_right();
2882                        break;
2883                case KEYCODE_HOME:
2884                        input_backward(cursor);
2885                        break;
2886                case KEYCODE_END:
2887                        put_till_end_and_adv_cursor();
2888                        break;
2889
2890                default:
2891                        if (initial_settings.c_cc[VINTR] != 0
2892                         && ic_raw == initial_settings.c_cc[VINTR]
2893                        ) {
2894                                /* Ctrl-C (usually) - stop gathering input */
2895                                command_len = 0;
2896                                break_out = -1; /* "do not append '\n'" */
2897                                break;
2898                        }
2899                        if (initial_settings.c_cc[VEOF] != 0
2900                         && ic_raw == initial_settings.c_cc[VEOF]
2901                        ) {
2902                                /* Ctrl-D (usually) - delete one character,
2903                                 * or exit if len=0 and no chars to delete */
2904                                if (command_len == 0) {
2905                                        errno = 0;
2906
2907                case -1: /* error (e.g. EIO when tty is destroyed) */
2908 IF_FEATURE_EDITING_VI(return_error_indicator:)
2909                                        break_out = command_len = -1;
2910                                        break;
2911                                }
2912                                input_delete(0);
2913                                break;
2914                        }
2915//                      /* Control-V -- force insert of next char */
2916//                      if (c == CTRL('V')) {
2917//                              if (safe_read(STDIN_FILENO, &c, 1) < 1)
2918//                                      goto return_error_indicator;
2919//                              if (c == 0) {
2920//                                      beep();
2921//                                      break;
2922//                              }
2923//                      }
2924                        if (ic < ' '
2925                         || (!ENABLE_UNICODE_SUPPORT && ic >= 256)
2926                         || (ENABLE_UNICODE_SUPPORT && ic >= VI_CMDMODE_BIT)
2927                        ) {
2928                                /* If VI_CMDMODE_BIT is set, ic is >= 256
2929                                 * and vi mode ignores unexpected chars.
2930                                 * Otherwise, we are here if ic is a
2931                                 * control char or an unhandled ESC sequence,
2932                                 * which is also ignored.
2933                                 */
2934                                break;
2935                        }
2936                        if ((int)command_len >= (maxsize - 2)) {
2937                                /* Not enough space for the char and EOL */
2938                                break;
2939                        }
2940
2941                        command_len++;
2942                        if (cursor == (command_len - 1)) {
2943                                /* We are at the end, append */
2944                                command_ps[cursor] = ic;
2945                                command_ps[cursor + 1] = BB_NUL;
2946                                put_cur_glyph_and_inc_cursor();
2947                                if (unicode_bidi_isrtl(ic))
2948                                        input_backward(1);
2949                        } else {
2950                                /* In the middle, insert */
2951                                int sc = cursor;
2952
2953                                memmove(command_ps + sc + 1, command_ps + sc,
2954                                        (command_len - sc) * sizeof(command_ps[0]));
2955                                command_ps[sc] = ic;
2956                                /* is right-to-left char, or neutral one (e.g. comma) was just added to rtl text? */
2957                                if (!isrtl_str())
2958                                        sc++; /* no */
2959                                put_till_end_and_adv_cursor();
2960                                /* to prev x pos + 1 */
2961                                input_backward(cursor - sc);
2962                        }
2963                        break;
2964                } /* switch (ic) */
2965
2966                if (break_out)
2967                        break;
2968
2969#if ENABLE_FEATURE_TAB_COMPLETION
2970                if (ic_raw != '\t')
2971                        lastWasTab = 0;
2972#endif
2973        } /* while (1) */
2974
2975#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
2976        if (S.sent_ESC_br6n) {
2977                /* "sleep 1; busybox ash" + hold [Enter] to trigger.
2978                 * We sent "ESC [ 6 n", but got '\n' first, and
2979                 * KEYCODE_CURSOR_POS response is now buffered from terminal.
2980                 * It's bad already and not much can be done with it
2981                 * (it _will_ be visible for the next process to read stdin),
2982                 * but without this delay it even shows up on the screen
2983                 * as garbage because we restore echo settings with tcsetattr
2984                 * before it comes in. UGLY!
2985                 */
2986                usleep(20*1000);
2987// MAYBE? tcflush(STDIN_FILENO, TCIFLUSH); /* flushes data received but not read */
2988        }
2989#endif
2990
2991/* End of bug-catching "command_must_not_be_used" trick */
2992#undef command
2993
2994#if ENABLE_UNICODE_SUPPORT
2995        command[0] = '\0';
2996        if (command_len > 0)
2997                command_len = save_string(command, maxsize - 1);
2998        free(command_ps);
2999#endif
3000
3001        if (command_len > 0) {
3002                remember_in_history(command);
3003        }
3004
3005        if (break_out > 0) {
3006                command[command_len++] = '\n';
3007                command[command_len] = '\0';
3008        }
3009
3010#if ENABLE_FEATURE_TAB_COMPLETION
3011        free_tab_completion_data();
3012#endif
3013
3014        /* restore initial_settings */
3015        tcsetattr_stdin_TCSANOW(&initial_settings);
3016#if ENABLE_FEATURE_EDITING_WINCH
3017        /* restore SIGWINCH handler */
3018        sigaction_set(SIGWINCH, &S.SIGWINCH_handler);
3019#endif
3020        fflush_all();
3021
3022        len = command_len;
3023        DEINIT_S();
3024
3025        return len; /* can't return command_len, DEINIT_S() destroys it */
3026}
3027
3028#else  /* !FEATURE_EDITING */
3029
3030#undef read_line_input
3031int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
3032{
3033        /* https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
3034         * says that shells must write $PSn to stderr, not stdout.
3035         */
3036        fputs(prompt, stderr);
3037        fflush_all();
3038        if (!fgets(command, maxsize, stdin))
3039                return -1;
3040        return strlen(command);
3041}
3042
3043#endif  /* !FEATURE_EDITING */
3044
3045
3046/*
3047 * Testing
3048 */
3049
3050#ifdef TEST
3051
3052#include <locale.h>
3053
3054const char *applet_name = "debug stuff usage";
3055
3056int main(int argc, char **argv)
3057{
3058        char buff[MAX_LINELEN];
3059        char *prompt =
3060#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
3061                "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
3062                "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
3063                "\\!\\[\\e[36;1m\\]\\$ \\[\\E[m\\]";
3064#else
3065                "% ";
3066#endif
3067
3068        while (1) {
3069                int l;
3070                l = read_line_input(prompt, buff);
3071                if (l <= 0 || buff[l-1] != '\n')
3072                        break;
3073                buff[l-1] = '\0';
3074                printf("*** read_line_input() returned line =%s=\n", buff);
3075        }
3076        printf("*** read_line_input() detect ^D\n");
3077        return 0;
3078}
3079
3080#endif  /* TEST */
3081