diff options
Diffstat (limited to 'shell')
-rw-r--r-- | shell/cmdedit.c | 130 |
1 files changed, 65 insertions, 65 deletions
diff --git a/shell/cmdedit.c b/shell/cmdedit.c index 31f4c7b20..d0e642250 100644 --- a/shell/cmdedit.c +++ b/shell/cmdedit.c | |||
@@ -442,9 +442,10 @@ static void redraw(int y, int back_cursor) | |||
442 | } | 442 | } |
443 | 443 | ||
444 | #ifdef CONFIG_FEATURE_COMMAND_EDITING_VI | 444 | #ifdef CONFIG_FEATURE_COMMAND_EDITING_VI |
445 | static char delbuf[BUFSIZ]; /* a place to store deleted characters */ | 445 | #define DELBUFSIZ 128 |
446 | static char *delp = delbuf; | 446 | static char *delbuf; /* a (malloced) place to store deleted characters */ |
447 | static int newdelflag; /* whether delbuf should be reused yet */ | 447 | static char *delp; |
448 | static char newdelflag; /* whether delbuf should be reused yet */ | ||
448 | #endif | 449 | #endif |
449 | 450 | ||
450 | /* Delete the char in front of the cursor, optionally saving it | 451 | /* Delete the char in front of the cursor, optionally saving it |
@@ -459,10 +460,13 @@ static void input_delete(int save) | |||
459 | #ifdef CONFIG_FEATURE_COMMAND_EDITING_VI | 460 | #ifdef CONFIG_FEATURE_COMMAND_EDITING_VI |
460 | if (save) { | 461 | if (save) { |
461 | if (newdelflag) { | 462 | if (newdelflag) { |
463 | if (!delbuf) | ||
464 | delbuf = malloc(DELBUFSIZ); | ||
465 | /* safe if malloc fails */ | ||
462 | delp = delbuf; | 466 | delp = delbuf; |
463 | newdelflag = 0; | 467 | newdelflag = 0; |
464 | } | 468 | } |
465 | if (delp - delbuf < BUFSIZ) | 469 | if (delbuf && (delp - delbuf < DELBUFSIZ)) |
466 | *delp++ = command_ps[j]; | 470 | *delp++ = command_ps[j]; |
467 | } | 471 | } |
468 | #endif | 472 | #endif |
@@ -1370,23 +1374,18 @@ vi_back_motion(char *command) | |||
1370 | #endif | 1374 | #endif |
1371 | 1375 | ||
1372 | /* | 1376 | /* |
1373 | * the normal emacs mode and vi's insert mode are the same. | 1377 | * the emacs and vi modes share much of the code in the big |
1374 | * commands entered when in vi command mode ("escape mode") get | 1378 | * command loop. commands entered when in vi's command mode (aka |
1375 | * an extra bit added to distinguish them. this lets them share | 1379 | * "escape mode") get an extra bit added to distinguish them -- |
1376 | * much of the code in the big switch and while loop. i | 1380 | * this keeps them from being self-inserted. this clutters the |
1377 | * experimented with an ugly macro to make the case labels for | 1381 | * big switch a bit, but keeps all the code in one place. |
1378 | * these cases go away entirely when vi mode isn't configured, in | ||
1379 | * hopes of letting the jump tables get smaller: | ||
1380 | * #define vcase(caselabel) caselabel | ||
1381 | * and then | ||
1382 | * case CNTRL('A'): | ||
1383 | * case vcase(VICMD('0'):) | ||
1384 | * but it didn't seem to make any difference in code size, | ||
1385 | * and the macro-ized code was too ugly. | ||
1386 | */ | 1382 | */ |
1387 | 1383 | ||
1388 | #define VI_cmdbit 0x100 | 1384 | #define vbit 0x100 |
1389 | #define VICMD(somecmd) ((somecmd)|VI_cmdbit) | 1385 | |
1386 | /* leave out the "vi-mode"-only case labels if vi editing isn't | ||
1387 | * configured. */ | ||
1388 | #define vi_case(caselabel) USE_FEATURE_COMMAND_EDITING(caselabel) | ||
1390 | 1389 | ||
1391 | /* convert uppercase ascii to equivalent control char, for readability */ | 1390 | /* convert uppercase ascii to equivalent control char, for readability */ |
1392 | #define CNTRL(uc_char) ((uc_char) - 0x40) | 1391 | #define CNTRL(uc_char) ((uc_char) - 0x40) |
@@ -1398,8 +1397,9 @@ int cmdedit_read_input(char *prompt, char command[BUFSIZ]) | |||
1398 | int break_out = 0; | 1397 | int break_out = 0; |
1399 | int lastWasTab = FALSE; | 1398 | int lastWasTab = FALSE; |
1400 | unsigned char c; | 1399 | unsigned char c; |
1400 | unsigned int ic; | ||
1401 | #ifdef CONFIG_FEATURE_COMMAND_EDITING_VI | 1401 | #ifdef CONFIG_FEATURE_COMMAND_EDITING_VI |
1402 | unsigned int ic, prevc; | 1402 | unsigned int prevc; |
1403 | int vi_cmdmode = 0; | 1403 | int vi_cmdmode = 0; |
1404 | #endif | 1404 | #endif |
1405 | /* prepare before init handlers */ | 1405 | /* prepare before init handlers */ |
@@ -1438,38 +1438,37 @@ int cmdedit_read_input(char *prompt, char command[BUFSIZ]) | |||
1438 | /* if we can't read input then exit */ | 1438 | /* if we can't read input then exit */ |
1439 | goto prepare_to_die; | 1439 | goto prepare_to_die; |
1440 | 1440 | ||
1441 | ic = c; | ||
1442 | |||
1441 | #ifdef CONFIG_FEATURE_COMMAND_EDITING_VI | 1443 | #ifdef CONFIG_FEATURE_COMMAND_EDITING_VI |
1442 | newdelflag = 1; | 1444 | newdelflag = 1; |
1443 | ic = c; | ||
1444 | if (vi_cmdmode) | 1445 | if (vi_cmdmode) |
1445 | ic |= VI_cmdbit; | 1446 | ic |= vbit; |
1446 | switch (ic) | ||
1447 | #else | ||
1448 | switch (c) | ||
1449 | #endif | 1447 | #endif |
1448 | switch (ic) | ||
1450 | { | 1449 | { |
1451 | case '\n': | 1450 | case '\n': |
1452 | case '\r': | 1451 | case '\r': |
1453 | case VICMD('\n'): | 1452 | vi_case( case '\n'|vbit: ) |
1454 | case VICMD('\r'): | 1453 | vi_case( case '\r'|vbit: ) |
1455 | /* Enter */ | 1454 | /* Enter */ |
1456 | goto_new_line(); | 1455 | goto_new_line(); |
1457 | break_out = 1; | 1456 | break_out = 1; |
1458 | break; | 1457 | break; |
1459 | case CNTRL('A'): | 1458 | case CNTRL('A'): |
1460 | case VICMD('0'): | 1459 | vi_case( case '0'|vbit: ) |
1461 | /* Control-a -- Beginning of line */ | 1460 | /* Control-a -- Beginning of line */ |
1462 | input_backward(cursor); | 1461 | input_backward(cursor); |
1463 | break; | 1462 | break; |
1464 | case CNTRL('B'): | 1463 | case CNTRL('B'): |
1465 | case VICMD('h'): | 1464 | vi_case( case 'h'|vbit: ) |
1466 | case VICMD('\b'): | 1465 | vi_case( case '\b'|vbit: ) |
1467 | case VICMD(DEL): | 1466 | vi_case( case DEL|vbit: ) |
1468 | /* Control-b -- Move back one character */ | 1467 | /* Control-b -- Move back one character */ |
1469 | input_backward(1); | 1468 | input_backward(1); |
1470 | break; | 1469 | break; |
1471 | case CNTRL('C'): | 1470 | case CNTRL('C'): |
1472 | case VICMD(CNTRL('C')): | 1471 | vi_case( case CNTRL('C')|vbit: ) |
1473 | /* Control-c -- stop gathering input */ | 1472 | /* Control-c -- stop gathering input */ |
1474 | goto_new_line(); | 1473 | goto_new_line(); |
1475 | #ifndef CONFIG_ASH | 1474 | #ifndef CONFIG_ASH |
@@ -1503,13 +1502,13 @@ prepare_to_die: | |||
1503 | } | 1502 | } |
1504 | break; | 1503 | break; |
1505 | case CNTRL('E'): | 1504 | case CNTRL('E'): |
1506 | case VICMD('$'): | 1505 | vi_case( case '$'|vbit: ) |
1507 | /* Control-e -- End of line */ | 1506 | /* Control-e -- End of line */ |
1508 | input_end(); | 1507 | input_end(); |
1509 | break; | 1508 | break; |
1510 | case CNTRL('F'): | 1509 | case CNTRL('F'): |
1511 | case VICMD('l'): | 1510 | vi_case( case 'l'|vbit: ) |
1512 | case VICMD(' '): | 1511 | vi_case( case ' '|vbit: ) |
1513 | /* Control-f -- Move forward one character */ | 1512 | /* Control-f -- Move forward one character */ |
1514 | input_forward(); | 1513 | input_forward(); |
1515 | break; | 1514 | break; |
@@ -1530,22 +1529,22 @@ prepare_to_die: | |||
1530 | printf("\033[J"); | 1529 | printf("\033[J"); |
1531 | break; | 1530 | break; |
1532 | case CNTRL('L'): | 1531 | case CNTRL('L'): |
1533 | case VICMD(CNTRL('L')): | 1532 | vi_case( case CNTRL('L')|vbit: ) |
1534 | /* Control-l -- clear screen */ | 1533 | /* Control-l -- clear screen */ |
1535 | printf("\033[H"); | 1534 | printf("\033[H"); |
1536 | redraw(0, len-cursor); | 1535 | redraw(0, len-cursor); |
1537 | break; | 1536 | break; |
1538 | #if MAX_HISTORY >= 1 | 1537 | #if MAX_HISTORY >= 1 |
1539 | case CNTRL('N'): | 1538 | case CNTRL('N'): |
1540 | case VICMD(CNTRL('N')): | 1539 | vi_case( case CNTRL('N')|vbit: ) |
1541 | case VICMD('j'): | 1540 | vi_case( case 'j'|vbit: ) |
1542 | /* Control-n -- Get next command in history */ | 1541 | /* Control-n -- Get next command in history */ |
1543 | if (get_next_history()) | 1542 | if (get_next_history()) |
1544 | goto rewrite_line; | 1543 | goto rewrite_line; |
1545 | break; | 1544 | break; |
1546 | case CNTRL('P'): | 1545 | case CNTRL('P'): |
1547 | case VICMD(CNTRL('P')): | 1546 | vi_case( case CNTRL('P')|vbit: ) |
1548 | case VICMD('k'): | 1547 | vi_case( case 'k'|vbit: ) |
1549 | /* Control-p -- Get previous command from history */ | 1548 | /* Control-p -- Get previous command from history */ |
1550 | if (cur_history > 0) { | 1549 | if (cur_history > 0) { |
1551 | get_previous_history(); | 1550 | get_previous_history(); |
@@ -1556,7 +1555,7 @@ prepare_to_die: | |||
1556 | break; | 1555 | break; |
1557 | #endif | 1556 | #endif |
1558 | case CNTRL('U'): | 1557 | case CNTRL('U'): |
1559 | case VICMD(CNTRL('U')): | 1558 | vi_case( case CNTRL('U')|vbit: ) |
1560 | /* Control-U -- Clear line before cursor */ | 1559 | /* Control-U -- Clear line before cursor */ |
1561 | if (cursor) { | 1560 | if (cursor) { |
1562 | strcpy(command, command + cursor); | 1561 | strcpy(command, command + cursor); |
@@ -1564,7 +1563,7 @@ prepare_to_die: | |||
1564 | } | 1563 | } |
1565 | break; | 1564 | break; |
1566 | case CNTRL('W'): | 1565 | case CNTRL('W'): |
1567 | case VICMD(CNTRL('W')): | 1566 | vi_case( case CNTRL('W')|vbit: ) |
1568 | /* Control-W -- Remove the last word */ | 1567 | /* Control-W -- Remove the last word */ |
1569 | while (cursor > 0 && isspace(command[cursor-1])) | 1568 | while (cursor > 0 && isspace(command[cursor-1])) |
1570 | input_backspace(); | 1569 | input_backspace(); |
@@ -1572,58 +1571,58 @@ prepare_to_die: | |||
1572 | input_backspace(); | 1571 | input_backspace(); |
1573 | break; | 1572 | break; |
1574 | #if CONFIG_FEATURE_COMMAND_EDITING_VI | 1573 | #if CONFIG_FEATURE_COMMAND_EDITING_VI |
1575 | case VICMD('i'): | 1574 | case 'i'|vbit: |
1576 | vi_cmdmode = 0; | 1575 | vi_cmdmode = 0; |
1577 | break; | 1576 | break; |
1578 | case VICMD('I'): | 1577 | case 'I'|vbit: |
1579 | input_backward(cursor); | 1578 | input_backward(cursor); |
1580 | vi_cmdmode = 0; | 1579 | vi_cmdmode = 0; |
1581 | break; | 1580 | break; |
1582 | case VICMD('a'): | 1581 | case 'a'|vbit: |
1583 | input_forward(); | 1582 | input_forward(); |
1584 | vi_cmdmode = 0; | 1583 | vi_cmdmode = 0; |
1585 | break; | 1584 | break; |
1586 | case VICMD('A'): | 1585 | case 'A'|vbit: |
1587 | input_end(); | 1586 | input_end(); |
1588 | vi_cmdmode = 0; | 1587 | vi_cmdmode = 0; |
1589 | break; | 1588 | break; |
1590 | case VICMD('x'): | 1589 | case 'x'|vbit: |
1591 | input_delete(1); | 1590 | input_delete(1); |
1592 | break; | 1591 | break; |
1593 | case VICMD('X'): | 1592 | case 'X'|vbit: |
1594 | if (cursor > 0) { | 1593 | if (cursor > 0) { |
1595 | input_backward(1); | 1594 | input_backward(1); |
1596 | input_delete(1); | 1595 | input_delete(1); |
1597 | } | 1596 | } |
1598 | break; | 1597 | break; |
1599 | case VICMD('W'): | 1598 | case 'W'|vbit: |
1600 | vi_Word_motion(command, 1); | 1599 | vi_Word_motion(command, 1); |
1601 | break; | 1600 | break; |
1602 | case VICMD('w'): | 1601 | case 'w'|vbit: |
1603 | vi_word_motion(command, 1); | 1602 | vi_word_motion(command, 1); |
1604 | break; | 1603 | break; |
1605 | case VICMD('E'): | 1604 | case 'E'|vbit: |
1606 | vi_End_motion(command); | 1605 | vi_End_motion(command); |
1607 | break; | 1606 | break; |
1608 | case VICMD('e'): | 1607 | case 'e'|vbit: |
1609 | vi_end_motion(command); | 1608 | vi_end_motion(command); |
1610 | break; | 1609 | break; |
1611 | case VICMD('B'): | 1610 | case 'B'|vbit: |
1612 | vi_Back_motion(command); | 1611 | vi_Back_motion(command); |
1613 | break; | 1612 | break; |
1614 | case VICMD('b'): | 1613 | case 'b'|vbit: |
1615 | vi_back_motion(command); | 1614 | vi_back_motion(command); |
1616 | break; | 1615 | break; |
1617 | case VICMD('C'): | 1616 | case 'C'|vbit: |
1618 | vi_cmdmode = 0; | 1617 | vi_cmdmode = 0; |
1619 | /* fall through */ | 1618 | /* fall through */ |
1620 | case VICMD('D'): | 1619 | case 'D'|vbit: |
1621 | goto clear_to_eol; | 1620 | goto clear_to_eol; |
1622 | 1621 | ||
1623 | case VICMD('c'): | 1622 | case 'c'|vbit: |
1624 | vi_cmdmode = 0; | 1623 | vi_cmdmode = 0; |
1625 | /* fall through */ | 1624 | /* fall through */ |
1626 | case VICMD('d'): | 1625 | case 'd'|vbit: |
1627 | { | 1626 | { |
1628 | int nc, sc; | 1627 | int nc, sc; |
1629 | sc = cursor; | 1628 | sc = cursor; |
@@ -1682,13 +1681,13 @@ prepare_to_die: | |||
1682 | } | 1681 | } |
1683 | } | 1682 | } |
1684 | break; | 1683 | break; |
1685 | case VICMD('p'): | 1684 | case 'p'|vbit: |
1686 | input_forward(); | 1685 | input_forward(); |
1687 | /* fallthrough */ | 1686 | /* fallthrough */ |
1688 | case VICMD('P'): | 1687 | case 'P'|vbit: |
1689 | put(); | 1688 | put(); |
1690 | break; | 1689 | break; |
1691 | case VICMD('r'): | 1690 | case 'r'|vbit: |
1692 | if (safe_read(0, &c, 1) < 1) | 1691 | if (safe_read(0, &c, 1) < 1) |
1693 | goto prepare_to_die; | 1692 | goto prepare_to_die; |
1694 | if (c == 0) | 1693 | if (c == 0) |
@@ -1700,7 +1699,9 @@ prepare_to_die: | |||
1700 | } | 1699 | } |
1701 | break; | 1700 | break; |
1702 | #endif /* CONFIG_FEATURE_COMMAND_EDITING_VI */ | 1701 | #endif /* CONFIG_FEATURE_COMMAND_EDITING_VI */ |
1703 | case ESC:{ | 1702 | |
1703 | case ESC: | ||
1704 | |||
1704 | #if CONFIG_FEATURE_COMMAND_EDITING_VI | 1705 | #if CONFIG_FEATURE_COMMAND_EDITING_VI |
1705 | if (vi_mode) { | 1706 | if (vi_mode) { |
1706 | /* ESC: insert mode --> command mode */ | 1707 | /* ESC: insert mode --> command mode */ |
@@ -1714,8 +1715,8 @@ prepare_to_die: | |||
1714 | goto prepare_to_die; | 1715 | goto prepare_to_die; |
1715 | /* different vt100 emulations */ | 1716 | /* different vt100 emulations */ |
1716 | if (c == '[' || c == 'O') { | 1717 | if (c == '[' || c == 'O') { |
1717 | case VICMD('['): | 1718 | vi_case( case '['|vbit: ) |
1718 | case VICMD('O'): | 1719 | vi_case( case 'O'|vbit: ) |
1719 | if (safe_read(0, &c, 1) < 1) | 1720 | if (safe_read(0, &c, 1) < 1) |
1720 | goto prepare_to_die; | 1721 | goto prepare_to_die; |
1721 | } | 1722 | } |
@@ -1787,7 +1788,6 @@ rewrite_line: | |||
1787 | beep(); | 1788 | beep(); |
1788 | } | 1789 | } |
1789 | break; | 1790 | break; |
1790 | } | ||
1791 | 1791 | ||
1792 | default: /* If it's regular input, do the normal thing */ | 1792 | default: /* If it's regular input, do the normal thing */ |
1793 | #ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT | 1793 | #ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |