1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
|
/*
* Copyright (C) 1984-2024 Mark Nudelman
*
* You may distribute under the terms of either the GNU General Public
* License or the Less License, as specified in the README file.
*
* For more information, see the README file.
*/
/*
* Functions which manipulate the command buffer.
* Used only by command() and related functions.
*/
#include "less.h"
#include "cmd.h"
#include "charset.h"
#if HAVE_STAT
#include <sys/stat.h>
#endif
extern int sc_width;
extern int utf_mode;
extern int no_hist_dups;
extern int marks_modified;
static char cmdbuf[CMDBUF_SIZE]; /* Buffer for holding a multi-char command */
static int cmd_col; /* Current column of the cursor */
static int prompt_col; /* Column of cursor just after prompt */
static char *cp; /* Pointer into cmdbuf */
static int cmd_offset; /* Index into cmdbuf of first displayed char */
static lbool literal; /* Next input char should not be interpreted */
static size_t updown_match; /* Prefix length in up/down movement */
static lbool have_updown_match = FALSE;
#if TAB_COMPLETE_FILENAME
static int cmd_complete(int action);
/*
* These variables are statics used by cmd_complete.
*/
static lbool in_completion = FALSE;
static char *tk_text;
static char *tk_original;
static constant char *tk_ipoint;
static constant char *tk_trial = NULL;
static struct textlist tk_tlist;
#endif
static int cmd_left();
static int cmd_right();
#if SPACES_IN_FILENAMES
public char openquote = '"';
public char closequote = '"';
#endif
#if CMD_HISTORY
/* History file */
#define HISTFILE_FIRST_LINE ".less-history-file:"
#define HISTFILE_SEARCH_SECTION ".search"
#define HISTFILE_SHELL_SECTION ".shell"
#define HISTFILE_MARK_SECTION ".mark"
/*
* A mlist structure represents a command history.
*/
struct mlist
{
struct mlist *next;
struct mlist *prev;
struct mlist *curr_mp;
char *string;
lbool modified;
};
/*
* These are the various command histories that exist.
*/
struct mlist mlist_search =
{ &mlist_search, &mlist_search, &mlist_search, NULL, 0 };
public void *ml_search = (void *) &mlist_search;
struct mlist mlist_examine =
{ &mlist_examine, &mlist_examine, &mlist_examine, NULL, 0 };
public void *ml_examine = (void *) &mlist_examine;
#if SHELL_ESCAPE || PIPEC
struct mlist mlist_shell =
{ &mlist_shell, &mlist_shell, &mlist_shell, NULL, 0 };
public void *ml_shell = (void *) &mlist_shell;
#endif
#else /* CMD_HISTORY */
/* If CMD_HISTORY is off, these are just flags. */
public void *ml_search = (void *)1;
public void *ml_examine = (void *)2;
#if SHELL_ESCAPE || PIPEC
public void *ml_shell = (void *)3;
#endif
#endif /* CMD_HISTORY */
/*
* History for the current command.
*/
static struct mlist *curr_mlist = NULL;
static int curr_cmdflags;
static char cmd_mbc_buf[MAX_UTF_CHAR_LEN];
static int cmd_mbc_buf_len;
static int cmd_mbc_buf_index;
/*
* Reset command buffer (to empty).
*/
public void cmd_reset(void)
{
cp = cmdbuf;
*cp = '\0';
cmd_col = 0;
cmd_offset = 0;
literal = FALSE;
cmd_mbc_buf_len = 0;
have_updown_match = FALSE;
}
/*
* Clear command line.
*/
public void clear_cmd(void)
{
cmd_col = prompt_col = 0;
cmd_mbc_buf_len = 0;
have_updown_match = FALSE;
}
/*
* Display a string, usually as a prompt for input into the command buffer.
*/
public void cmd_putstr(constant char *s)
{
LWCHAR prev_ch = 0;
LWCHAR ch;
constant char *endline = s + strlen(s);
while (*s != '\0')
{
constant char *os = s;
int width;
ch = step_charc(&s, +1, endline);
while (os < s)
putchr(*os++);
if (!utf_mode)
width = 1;
else if (is_composing_char(ch) || is_combining_char(prev_ch, ch))
width = 0;
else
width = is_wide_char(ch) ? 2 : 1;
cmd_col += width;
prompt_col += width;
prev_ch = ch;
}
}
/*
* How many characters are in the command buffer?
*/
public int len_cmdbuf(void)
{
constant char *s = cmdbuf;
constant char *endline = s + strlen(s);
int len = 0;
while (*s != '\0')
{
step_charc(&s, +1, endline);
len++;
}
return (len);
}
/*
* Common part of cmd_step_right() and cmd_step_left().
* {{ Returning pwidth and bswidth separately is a historical artifact
* since they're always the same. Maybe clean this up someday. }}
*/
static constant char * cmd_step_common(char *p, LWCHAR ch, size_t len, int *pwidth, int *bswidth)
{
constant char *pr;
int width;
if (len == 1)
{
pr = prchar(ch);
width = (int) strlen(pr);
} else
{
pr = prutfchar(ch);
if (is_composing_char(ch))
width = 0;
else if (is_ubin_char(ch))
width = (int) strlen(pr);
else
{
LWCHAR prev_ch = step_char(&p, -1, cmdbuf);
if (is_combining_char(prev_ch, ch))
width = 0;
else
width = is_wide_char(ch) ? 2 : 1;
}
}
if (pwidth != NULL)
*pwidth = width;
if (bswidth != NULL)
*bswidth = width;
return (pr);
}
/*
* Step a pointer one character right in the command buffer.
*/
static constant char * cmd_step_right(char **pp, int *pwidth, int *bswidth)
{
char *p = *pp;
LWCHAR ch = step_char(pp, +1, p + strlen(p));
return cmd_step_common(p, ch, ptr_diff(*pp, p), pwidth, bswidth);
}
/*
* Step a pointer one character left in the command buffer.
*/
static constant char * cmd_step_left(char **pp, int *pwidth, int *bswidth)
{
char *p = *pp;
LWCHAR ch = step_char(pp, -1, cmdbuf);
return cmd_step_common(*pp, ch, ptr_diff(p, *pp), pwidth, bswidth);
}
/*
* Put the cursor at "home" (just after the prompt),
* and set cp to the corresponding char in cmdbuf.
*/
static void cmd_home(void)
{
while (cmd_col > prompt_col)
{
int width, bswidth;
cmd_step_left(&cp, &width, &bswidth);
while (bswidth-- > 0)
putbs();
cmd_col -= width;
}
cp = &cmdbuf[cmd_offset];
}
/*
* Repaint the line from cp onwards.
* Then position the cursor just after the char old_cp (a pointer into cmdbuf).
*/
public void cmd_repaint(constant char *old_cp)
{
/*
* Repaint the line from the current position.
*/
if (old_cp == NULL)
{
old_cp = cp;
cmd_home();
}
clear_eol();
while (*cp != '\0')
{
char *np = cp;
int width;
constant char *pr = cmd_step_right(&np, &width, NULL);
if (cmd_col + width >= sc_width)
break;
cp = np;
putstr(pr);
cmd_col += width;
}
while (*cp != '\0')
{
char *np = cp;
int width;
constant char *pr = cmd_step_right(&np, &width, NULL);
if (width > 0)
break;
cp = np;
putstr(pr);
}
/*
* Back up the cursor to the correct position.
*/
while (cp > old_cp)
cmd_left();
}
/*
* Shift the cmdbuf display left a half-screen.
*/
static void cmd_lshift(void)
{
char *s;
char *save_cp;
int cols;
/*
* Start at the first displayed char, count how far to the
* right we'd have to move to reach the center of the screen.
*/
s = cmdbuf + cmd_offset;
cols = 0;
while (cols < (sc_width - prompt_col) / 2 && *s != '\0')
{
int width;
cmd_step_right(&s, &width, NULL);
cols += width;
}
while (*s != '\0')
{
int width;
char *ns = s;
cmd_step_right(&ns, &width, NULL);
if (width > 0)
break;
s = ns;
}
cmd_offset = (int) (s - cmdbuf);
save_cp = cp;
cmd_home();
cmd_repaint(save_cp);
}
/*
* Shift the cmdbuf display right a half-screen.
*/
static void cmd_rshift(void)
{
char *s;
char *save_cp;
int cols;
/*
* Start at the first displayed char, count how far to the
* left we'd have to move to traverse a half-screen width
* of displayed characters.
*/
s = cmdbuf + cmd_offset;
cols = 0;
while (cols < (sc_width - prompt_col) / 2 && s > cmdbuf)
{
int width;
cmd_step_left(&s, &width, NULL);
cols += width;
}
cmd_offset = (int) (s - cmdbuf);
save_cp = cp;
cmd_home();
cmd_repaint(save_cp);
}
/*
* Move cursor right one character.
*/
static int cmd_right(void)
{
constant char *pr;
char *ncp;
int width;
if (*cp == '\0')
{
/* Already at the end of the line. */
return (CC_OK);
}
ncp = cp;
pr = cmd_step_right(&ncp, &width, NULL);
if (cmd_col + width >= sc_width)
cmd_lshift();
else if (cmd_col + width == sc_width - 1 && cp[1] != '\0')
cmd_lshift();
cp = ncp;
cmd_col += width;
putstr(pr);
while (*cp != '\0')
{
pr = cmd_step_right(&ncp, &width, NULL);
if (width > 0)
break;
putstr(pr);
cp = ncp;
}
return (CC_OK);
}
/*
* Move cursor left one character.
*/
static int cmd_left(void)
{
char *ncp;
int width = 0;
int bswidth = 0;
if (cp <= cmdbuf)
{
/* Already at the beginning of the line */
return (CC_OK);
}
ncp = cp;
while (ncp > cmdbuf)
{
cmd_step_left(&ncp, &width, &bswidth);
if (width > 0)
break;
}
if (cmd_col < prompt_col + width)
cmd_rshift();
cp = ncp;
cmd_col -= width;
while (bswidth-- > 0)
putbs();
return (CC_OK);
}
/*
* Insert a char into the command buffer, at the current position.
*/
static int cmd_ichar(constant char *cs, size_t clen)
{
char *s;
if (strlen(cmdbuf) + clen >= sizeof(cmdbuf)-1)
{
/* No room in the command buffer for another char. */
bell();
return (CC_ERROR);
}
/*
* Make room for the new character (shift the tail of the buffer right).
*/
for (s = &cmdbuf[strlen(cmdbuf)]; s >= cp; s--)
s[clen] = s[0];
/*
* Insert the character into the buffer.
*/
for (s = cp; s < cp + clen; s++)
*s = *cs++;
/*
* Reprint the tail of the line from the inserted char.
*/
have_updown_match = FALSE;
cmd_repaint(cp);
cmd_right();
return (CC_OK);
}
/*
* Backspace in the command buffer.
* Delete the char to the left of the cursor.
*/
static int cmd_erase(void)
{
char *s;
int clen;
if (cp == cmdbuf)
{
/*
* Backspace past beginning of the buffer:
* this usually means abort the command.
*/
return (CC_QUIT);
}
/*
* Move cursor left (to the char being erased).
*/
s = cp;
cmd_left();
clen = (int) (s - cp);
/*
* Remove the char from the buffer (shift the buffer left).
*/
for (s = cp; ; s++)
{
s[0] = s[clen];
if (s[0] == '\0')
break;
}
/*
* Repaint the buffer after the erased char.
*/
have_updown_match = FALSE;
cmd_repaint(cp);
/*
* We say that erasing the entire command string causes us
* to abort the current command, if CF_QUIT_ON_ERASE is set.
*/
if ((curr_cmdflags & CF_QUIT_ON_ERASE) && cp == cmdbuf && *cp == '\0')
return (CC_QUIT);
return (CC_OK);
}
/*
* Delete the char under the cursor.
*/
static int cmd_delete(void)
{
if (*cp == '\0')
{
/* At end of string; there is no char under the cursor. */
return (CC_OK);
}
/*
* Move right, then use cmd_erase.
*/
cmd_right();
cmd_erase();
return (CC_OK);
}
/*
* Delete the "word" to the left of the cursor.
*/
static int cmd_werase(void)
{
if (cp > cmdbuf && cp[-1] == ' ')
{
/*
* If the char left of cursor is a space,
* erase all the spaces left of cursor (to the first non-space).
*/
while (cp > cmdbuf && cp[-1] == ' ')
(void) cmd_erase();
} else
{
/*
* If the char left of cursor is not a space,
* erase all the nonspaces left of cursor (the whole "word").
*/
while (cp > cmdbuf && cp[-1] != ' ')
(void) cmd_erase();
}
return (CC_OK);
}
/*
* Delete the "word" under the cursor.
*/
static int cmd_wdelete(void)
{
if (*cp == ' ')
{
/*
* If the char under the cursor is a space,
* delete it and all the spaces right of cursor.
*/
while (*cp == ' ')
(void) cmd_delete();
} else
{
/*
* If the char under the cursor is not a space,
* delete it and all nonspaces right of cursor (the whole word).
*/
while (*cp != ' ' && *cp != '\0')
(void) cmd_delete();
}
return (CC_OK);
}
/*
* Delete all chars in the command buffer.
*/
static int cmd_kill(void)
{
if (cmdbuf[0] == '\0')
{
/* Buffer is already empty; abort the current command. */
return (CC_QUIT);
}
cmd_offset = 0;
cmd_home();
*cp = '\0';
have_updown_match = FALSE;
cmd_repaint(cp);
/*
* We say that erasing the entire command string causes us
* to abort the current command, if CF_QUIT_ON_ERASE is set.
*/
if (curr_cmdflags & CF_QUIT_ON_ERASE)
return (CC_QUIT);
return (CC_OK);
}
/*
* Select an mlist structure to be the current command history.
*/
public void set_mlist(void *mlist, int cmdflags)
{
#if CMD_HISTORY
curr_mlist = (struct mlist *) mlist;
curr_cmdflags = cmdflags;
/* Make sure the next up-arrow moves to the last string in the mlist. */
if (curr_mlist != NULL)
curr_mlist->curr_mp = curr_mlist;
#endif
}
#if CMD_HISTORY
/*
* Move up or down in the currently selected command history list.
* Only consider entries whose first updown_match chars are equal to
* cmdbuf's corresponding chars.
*/
static int cmd_updown(int action)
{
constant char *s;
struct mlist *ml;
if (curr_mlist == NULL)
{
/*
* The current command has no history list.
*/
bell();
return (CC_OK);
}
if (!have_updown_match)
{
updown_match = ptr_diff(cp, cmdbuf);
have_updown_match = TRUE;
}
/*
* Find the next history entry which matches.
*/
for (ml = curr_mlist->curr_mp;;)
{
ml = (action == EC_UP) ? ml->prev : ml->next;
if (ml == curr_mlist)
{
/*
* We reached the end (or beginning) of the list.
*/
break;
}
if (strncmp(cmdbuf, ml->string, updown_match) == 0)
{
/*
* This entry matches; stop here.
* Copy the entry into cmdbuf and echo it on the screen.
*/
curr_mlist->curr_mp = ml;
s = ml->string;
if (s == NULL)
s = "";
cmd_offset = 0;
cmd_home();
clear_eol();
strcpy(cmdbuf, s);
for (cp = cmdbuf; *cp != '\0'; )
cmd_right();
return (CC_OK);
}
}
/*
* We didn't find a history entry that matches.
*/
bell();
return (CC_OK);
}
/*
* Yet another lesson in the evils of global variables.
*/
public ssize_t save_updown_match(void)
{
if (!have_updown_match)
return (ssize_t)(-1);
return (ssize_t) updown_match;
}
public void restore_updown_match(ssize_t udm)
{
updown_match = udm;
have_updown_match = (udm != (ssize_t)(-1));
}
#endif /* CMD_HISTORY */
/*
*
*/
static void ml_link(struct mlist *mlist, struct mlist *ml)
{
ml->next = mlist;
ml->prev = mlist->prev;
mlist->prev->next = ml;
mlist->prev = ml;
}
/*
*
*/
static void ml_unlink(struct mlist *ml)
{
ml->prev->next = ml->next;
ml->next->prev = ml->prev;
}
/*
* Add a string to an mlist.
*/
public void cmd_addhist(struct mlist *mlist, constant char *cmd, lbool modified)
{
#if CMD_HISTORY
struct mlist *ml;
/*
* Don't save a trivial command.
*/
if (strlen(cmd) == 0)
return;
if (no_hist_dups)
{
struct mlist *next = NULL;
for (ml = mlist->next; ml->string != NULL; ml = next)
{
next = ml->next;
if (strcmp(ml->string, cmd) == 0)
{
ml_unlink(ml);
free(ml->string);
free(ml);
}
}
}
/*
* Save the command unless it's a duplicate of the
* last command in the history.
*/
ml = mlist->prev;
if (ml == mlist || strcmp(ml->string, cmd) != 0)
{
/*
* Did not find command in history.
* Save the command and put it at the end of the history list.
*/
ml = (struct mlist *) ecalloc(1, sizeof(struct mlist));
ml->string = save(cmd);
ml->modified = modified;
ml_link(mlist, ml);
}
/*
* Point to the cmd just after the just-accepted command.
* Thus, an UPARROW will always retrieve the previous command.
*/
mlist->curr_mp = ml->next;
#endif
}
/*
* Accept the command in the command buffer.
* Add it to the currently selected history list.
*/
public void cmd_accept(void)
{
#if CMD_HISTORY
/*
* Nothing to do if there is no currently selected history list.
*/
if (curr_mlist == NULL || curr_mlist == ml_examine)
return;
cmd_addhist(curr_mlist, cmdbuf, TRUE);
curr_mlist->modified = TRUE;
#endif
}
/*
* Try to perform a line-edit function on the command buffer,
* using a specified char as a line-editing command.
* Returns:
* CC_PASS The char does not invoke a line edit function.
* CC_OK Line edit function done.
* CC_QUIT The char requests the current command to be aborted.
*/
static int cmd_edit(char c)
{
int action;
int flags;
#if TAB_COMPLETE_FILENAME
#define not_in_completion() in_completion = 0
#else
#define not_in_completion(void)
#endif
/*
* See if the char is indeed a line-editing command.
*/
flags = 0;
#if CMD_HISTORY
if (curr_mlist == NULL)
/*
* No current history; don't accept history manipulation cmds.
*/
flags |= ECF_NOHISTORY;
#endif
#if TAB_COMPLETE_FILENAME
if (curr_mlist == ml_search || curr_mlist == NULL)
/*
* Don't accept file-completion cmds in contexts
* such as search pattern, digits, long option name, etc.
*/
flags |= ECF_NOCOMPLETE;
#endif
action = editchar(c, flags);
switch (action)
{
case A_NOACTION:
return (CC_OK);
case EC_RIGHT:
not_in_completion();
return (cmd_right());
case EC_LEFT:
not_in_completion();
return (cmd_left());
case EC_W_RIGHT:
not_in_completion();
while (*cp != '\0' && *cp != ' ')
cmd_right();
while (*cp == ' ')
cmd_right();
return (CC_OK);
case EC_W_LEFT:
not_in_completion();
while (cp > cmdbuf && cp[-1] == ' ')
cmd_left();
while (cp > cmdbuf && cp[-1] != ' ')
cmd_left();
return (CC_OK);
case EC_HOME:
not_in_completion();
cmd_offset = 0;
cmd_home();
cmd_repaint(cp);
return (CC_OK);
case EC_END:
not_in_completion();
while (*cp != '\0')
cmd_right();
return (CC_OK);
case EC_INSERT:
not_in_completion();
return (CC_OK);
case EC_BACKSPACE:
not_in_completion();
return (cmd_erase());
case EC_LINEKILL:
not_in_completion();
return (cmd_kill());
case EC_ABORT:
not_in_completion();
(void) cmd_kill();
return (CC_QUIT);
case EC_W_BACKSPACE:
not_in_completion();
return (cmd_werase());
case EC_DELETE:
not_in_completion();
return (cmd_delete());
case EC_W_DELETE:
not_in_completion();
return (cmd_wdelete());
case EC_LITERAL:
literal = TRUE;
return (CC_OK);
#if CMD_HISTORY
case EC_UP:
case EC_DOWN:
not_in_completion();
return (cmd_updown(action));
#endif
#if TAB_COMPLETE_FILENAME
case EC_F_COMPLETE:
case EC_B_COMPLETE:
case EC_EXPAND:
return (cmd_complete(action));
#endif
default:
not_in_completion();
return (CC_PASS);
}
}
#if TAB_COMPLETE_FILENAME
/*
* Insert a string into the command buffer, at the current position.
*/
static int cmd_istr(constant char *str)
{
constant char *endline = str + strlen(str);
constant char *s;
int action;
for (s = str; *s != '\0'; )
{
constant char *os = s;
step_charc(&s, +1, endline);
action = cmd_ichar(os, ptr_diff(s, os));
if (action != CC_OK)
return (action);
}
return (CC_OK);
}
/*
* Find the beginning and end of the "current" word.
* This is the word which the cursor (cp) is inside or at the end of.
* Return pointer to the beginning of the word and put the
* cursor at the end of the word.
*/
static char * delimit_word(void)
{
char *word;
#if SPACES_IN_FILENAMES
char *p;
int delim_quoted = FALSE;
int meta_quoted = FALSE;
constant char *esc = get_meta_escape();
size_t esclen = strlen(esc);
#endif
/*
* Move cursor to end of word.
*/
if (*cp != ' ' && *cp != '\0')
{
/*
* Cursor is on a nonspace.
* Move cursor right to the next space.
*/
while (*cp != ' ' && *cp != '\0')
cmd_right();
} else if (cp > cmdbuf && cp[-1] != ' ')
{
/*
* Cursor is on a space, and char to the left is a nonspace.
* We're already at the end of the word.
*/
;
#if 0
} else
{
/*
* Cursor is on a space and char to the left is a space.
* Huh? There's no word here.
*/
return (NULL);
#endif
}
/*
* Find the beginning of the word which the cursor is in.
*/
if (cp == cmdbuf)
return (NULL);
#if SPACES_IN_FILENAMES
/*
* If we have an unbalanced quote (that is, an open quote
* without a corresponding close quote), we return everything
* from the open quote, including spaces.
*/
for (word = cmdbuf; word < cp; word++)
if (*word != ' ')
break;
if (word >= cp)
return (cp);
for (p = cmdbuf; p < cp; p++)
{
if (meta_quoted)
{
meta_quoted = FALSE;
} else if (esclen > 0 && p + esclen < cp &&
strncmp(p, esc, esclen) == 0)
{
meta_quoted = TRUE;
p += esclen - 1;
} else if (delim_quoted)
{
if (*p == closequote)
delim_quoted = FALSE;
} else /* (!delim_quoted) */
{
if (*p == openquote)
delim_quoted = TRUE;
else if (*p == ' ')
word = p+1;
}
}
#endif
return (word);
}
/*
* Set things up to enter completion mode.
* Expand the word under the cursor into a list of filenames
* which start with that word, and set tk_text to that list.
*/
static void init_compl(void)
{
char *word;
char c;
/*
* Get rid of any previous tk_text.
*/
if (tk_text != NULL)
{
free(tk_text);
tk_text = NULL;
}
/*
* Find the original (uncompleted) word in the command buffer.
*/
word = delimit_word();
if (word == NULL)
return;
/*
* Set the insertion point to the point in the command buffer
* where the original (uncompleted) word now sits.
*/
tk_ipoint = word;
/*
* Save the original (uncompleted) word
*/
if (tk_original != NULL)
free(tk_original);
tk_original = (char *) ecalloc(ptr_diff(cp,word)+1, sizeof(char));
strncpy(tk_original, word, ptr_diff(cp,word));
/*
* Get the expanded filename.
* This may result in a single filename, or
* a blank-separated list of filenames.
*/
c = *cp;
*cp = '\0';
if (*word != openquote)
{
tk_text = fcomplete(word);
} else
{
#if MSDOS_COMPILER
char *qword = NULL;
#else
char *qword = shell_quote(word+1);
#endif
if (qword == NULL)
tk_text = fcomplete(word+1);
else
{
tk_text = fcomplete(qword);
free(qword);
}
}
*cp = c;
}
/*
* Return the next word in the current completion list.
*/
static constant char * next_compl(int action, constant char *prev)
{
switch (action)
{
case EC_F_COMPLETE:
return (forw_textlist(&tk_tlist, prev));
case EC_B_COMPLETE:
return (back_textlist(&tk_tlist, prev));
}
/* Cannot happen */
return ("?");
}
/*
* Complete the filename before (or under) the cursor.
* cmd_complete may be called multiple times. The global in_completion
* remembers whether this call is the first time (create the list),
* or a subsequent time (step thru the list).
*/
static int cmd_complete(int action)
{
constant char *s;
if (!in_completion || action == EC_EXPAND)
{
/*
* Expand the word under the cursor and
* use the first word in the expansion
* (or the entire expansion if we're doing EC_EXPAND).
*/
init_compl();
if (tk_text == NULL)
{
bell();
return (CC_OK);
}
if (action == EC_EXPAND)
{
/*
* Use the whole list.
*/
tk_trial = tk_text;
} else
{
/*
* Use the first filename in the list.
*/
in_completion = TRUE;
init_textlist(&tk_tlist, tk_text);
tk_trial = next_compl(action, (char*)NULL);
}
} else
{
/*
* We already have a completion list.
* Use the next/previous filename from the list.
*/
tk_trial = next_compl(action, tk_trial);
}
/*
* Remove the original word, or the previous trial completion.
*/
while (cp > tk_ipoint)
(void) cmd_erase();
if (tk_trial == NULL)
{
/*
* There are no more trial completions.
* Insert the original (uncompleted) filename.
*/
in_completion = FALSE;
if (cmd_istr(tk_original) != CC_OK)
goto fail;
} else
{
/*
* Insert trial completion.
*/
if (cmd_istr(tk_trial) != CC_OK)
goto fail;
/*
* If it is a directory, append a slash.
*/
if (is_dir(tk_trial))
{
if (cp > cmdbuf && cp[-1] == closequote)
(void) cmd_erase();
s = lgetenv("LESSSEPARATOR");
if (s == NULL)
s = PATHNAME_SEP;
if (cmd_istr(s) != CC_OK)
goto fail;
}
}
return (CC_OK);
fail:
in_completion = FALSE;
bell();
return (CC_OK);
}
#endif /* TAB_COMPLETE_FILENAME */
/*
* Process a single character of a multi-character command, such as
* a number, or the pattern of a search command.
* Returns:
* CC_OK The char was accepted.
* CC_QUIT The char requests the command to be aborted.
* CC_ERROR The char could not be accepted due to an error.
*/
public int cmd_char(char c)
{
int action;
size_t len;
if (!utf_mode)
{
cmd_mbc_buf[0] = c;
len = 1;
} else
{
/* Perform strict validation in all possible cases. */
if (cmd_mbc_buf_len == 0)
{
retry:
cmd_mbc_buf_index = 1;
*cmd_mbc_buf = c;
if (IS_ASCII_OCTET(c))
cmd_mbc_buf_len = 1;
#if MSDOS_COMPILER || OS2
else if (c == '\340' && IS_ASCII_OCTET(peekcc()))
{
/* Assume a special key. */
cmd_mbc_buf_len = 1;
}
#endif
else if (IS_UTF8_LEAD(c))
{
cmd_mbc_buf_len = utf_len(c);
return (CC_OK);
} else
{
/* UTF8_INVALID or stray UTF8_TRAIL */
bell();
return (CC_ERROR);
}
} else if (IS_UTF8_TRAIL(c))
{
cmd_mbc_buf[cmd_mbc_buf_index++] = c;
if (cmd_mbc_buf_index < cmd_mbc_buf_len)
return (CC_OK);
if (!is_utf8_well_formed(cmd_mbc_buf, cmd_mbc_buf_index))
{
/* complete, but not well formed (non-shortest form), sequence */
cmd_mbc_buf_len = 0;
bell();
return (CC_ERROR);
}
} else
{
/* Flush incomplete (truncated) sequence. */
cmd_mbc_buf_len = 0;
bell();
/* Handle new char. */
goto retry;
}
len = (size_t) cmd_mbc_buf_len; /*{{type-issue}}*/
cmd_mbc_buf_len = 0;
}
if (literal)
{
/*
* Insert the char, even if it is a line-editing char.
*/
literal = FALSE;
return (cmd_ichar(cmd_mbc_buf, len));
}
/*
* See if it is a line-editing character.
*/
if (in_mca() && len == 1)
{
action = cmd_edit(c);
switch (action)
{
case CC_OK:
case CC_QUIT:
return (action);
case CC_PASS:
break;
}
}
/*
* Insert the char into the command buffer.
*/
return (cmd_ichar(cmd_mbc_buf, len));
}
/*
* Return the number currently in the command buffer.
*/
public LINENUM cmd_int(mutable long *frac)
{
constant char *p;
LINENUM n = 0;
lbool err;
for (p = cmdbuf; *p >= '0' && *p <= '9'; p++)
{
if (ckd_mul(&n, n, 10) || ckd_add(&n, n, *p - '0'))
{
error("Integer is too big", NULL_PARG);
return (0);
}
}
*frac = 0;
if (*p++ == '.')
{
*frac = getfraction(&p, NULL, &err);
/* {{ do something if err is set? }} */
}
return (n);
}
/*
* Return a pointer to the command buffer.
*/
public constant char * get_cmdbuf(void)
{
if (cmd_mbc_buf_index < cmd_mbc_buf_len)
/* Don't return buffer containing an incomplete multibyte char. */
return (NULL);
return (cmdbuf);
}
#if CMD_HISTORY
/*
* Return the last (most recent) string in the current command history.
*/
public constant char * cmd_lastpattern(void)
{
if (curr_mlist == NULL)
return (NULL);
return (curr_mlist->curr_mp->prev->string);
}
#endif
#if CMD_HISTORY
/*
*/
static int mlist_size(struct mlist *ml)
{
int size = 0;
for (ml = ml->next; ml->string != NULL; ml = ml->next)
++size;
return size;
}
/*
* Get the name of the history file.
*/
static char * histfile_find(lbool must_exist)
{
constant char *home = lgetenv("HOME");
char *name = NULL;
/* Try in $XDG_STATE_HOME, then in $HOME/.local/state, then in $XDG_DATA_HOME, then in $HOME. */
#if OS2
if (isnullenv(home))
home = lgetenv("INIT");
#endif
name = dirfile(lgetenv("XDG_STATE_HOME"), &LESSHISTFILE[1], must_exist);
if (name == NULL)
{
char *dir = dirfile(home, ".local/state", 1);
if (dir != NULL)
{
name = dirfile(dir, &LESSHISTFILE[1], must_exist);
free(dir);
}
}
if (name == NULL)
name = dirfile(lgetenv("XDG_DATA_HOME"), &LESSHISTFILE[1], must_exist);
if (name == NULL)
name = dirfile(home, LESSHISTFILE, must_exist);
return (name);
}
static char * histfile_name(lbool must_exist)
{
constant char *name;
char *wname;
/* See if filename is explicitly specified by $LESSHISTFILE. */
name = lgetenv("LESSHISTFILE");
if (!isnullenv(name))
{
if (strcmp(name, "-") == 0 || strcmp(name, "/dev/null") == 0)
/* $LESSHISTFILE == "-" means don't use a history file. */
return (NULL);
return (save(name));
}
/* See if history file is disabled in the build. */
if (strcmp(LESSHISTFILE, "") == 0 || strcmp(LESSHISTFILE, "-") == 0)
return (NULL);
wname = NULL;
if (!must_exist)
{
/* If we're writing the file and the file already exists, use it. */
wname = histfile_find(TRUE);
}
if (wname == NULL)
wname = histfile_find(must_exist);
return (wname);
}
/*
* Read a .lesshst file and call a callback for each line in the file.
*/
static void read_cmdhist2(void (*action)(void*,struct mlist*,constant char*), void *uparam, int skip_search, int skip_shell)
{
struct mlist *ml = NULL;
char line[CMDBUF_SIZE];
char *filename;
FILE *f;
int *skip = NULL;
filename = histfile_name(TRUE);
if (filename == NULL)
return;
f = fopen(filename, "r");
free(filename);
if (f == NULL)
return;
if (fgets(line, sizeof(line), f) == NULL ||
strncmp(line, HISTFILE_FIRST_LINE, strlen(HISTFILE_FIRST_LINE)) != 0)
{
fclose(f);
return;
}
while (fgets(line, sizeof(line), f) != NULL)
{
char *p;
for (p = line; *p != '\0'; p++)
{
if (*p == '\n' || *p == '\r')
{
*p = '\0';
break;
}
}
if (strcmp(line, HISTFILE_SEARCH_SECTION) == 0)
{
ml = &mlist_search;
skip = &skip_search;
} else if (strcmp(line, HISTFILE_SHELL_SECTION) == 0)
{
#if SHELL_ESCAPE || PIPEC
ml = &mlist_shell;
skip = &skip_shell;
#else
ml = NULL;
skip = NULL;
#endif
} else if (strcmp(line, HISTFILE_MARK_SECTION) == 0)
{
ml = NULL;
} else if (*line == '"')
{
if (ml != NULL)
{
if (skip != NULL && *skip > 0)
--(*skip);
else
(*action)(uparam, ml, line+1);
}
} else if (*line == 'm')
{
(*action)(uparam, NULL, line);
}
}
fclose(f);
}
static void read_cmdhist(void (*action)(void*,struct mlist*,constant char*), void *uparam, lbool skip_search, lbool skip_shell)
{
if (!secure_allow(SF_HISTORY))
return;
read_cmdhist2(action, uparam, skip_search, skip_shell);
(*action)(uparam, NULL, NULL); /* signal end of file */
}
static void addhist_init(void *uparam, struct mlist *ml, constant char *string)
{
(void) uparam;
if (ml != NULL)
cmd_addhist(ml, string, 0);
else if (string != NULL)
restore_mark(string);
}
#endif /* CMD_HISTORY */
/*
* Initialize history from a .lesshist file.
*/
public void init_cmdhist(void)
{
#if CMD_HISTORY
read_cmdhist(&addhist_init, NULL, 0, 0);
#endif /* CMD_HISTORY */
}
/*
* Write the header for a section of the history file.
*/
#if CMD_HISTORY
static void write_mlist_header(struct mlist *ml, FILE *f)
{
if (ml == &mlist_search)
fprintf(f, "%s\n", HISTFILE_SEARCH_SECTION);
#if SHELL_ESCAPE || PIPEC
else if (ml == &mlist_shell)
fprintf(f, "%s\n", HISTFILE_SHELL_SECTION);
#endif
}
/*
* Write all modified entries in an mlist to the history file.
*/
static void write_mlist(struct mlist *ml, FILE *f)
{
for (ml = ml->next; ml->string != NULL; ml = ml->next)
{
if (!ml->modified)
continue;
fprintf(f, "\"%s\n", ml->string);
ml->modified = FALSE;
}
ml->modified = FALSE; /* entire mlist is now unmodified */
}
/*
* Make a temp name in the same directory as filename.
*/
static char * make_tempname(constant char *filename)
{
char lastch;
char *tempname = ecalloc(1, strlen(filename)+1);
strcpy(tempname, filename);
lastch = tempname[strlen(tempname)-1];
tempname[strlen(tempname)-1] = (lastch == 'Q') ? 'Z' : 'Q';
return tempname;
}
struct save_ctx
{
struct mlist *mlist;
FILE *fout;
};
/*
* Copy entries from the saved history file to a new file.
* At the end of each mlist, append any new entries
* created during this session.
*/
static void copy_hist(void *uparam, struct mlist *ml, constant char *string)
{
struct save_ctx *ctx = (struct save_ctx *) uparam;
if (ml != NULL && ml != ctx->mlist) {
/* We're changing mlists. */
if (ctx->mlist)
/* Append any new entries to the end of the current mlist. */
write_mlist(ctx->mlist, ctx->fout);
/* Write the header for the new mlist. */
ctx->mlist = ml;
write_mlist_header(ctx->mlist, ctx->fout);
}
if (string == NULL) /* End of file */
{
/* Write any sections that were not in the original file. */
if (mlist_search.modified)
{
write_mlist_header(&mlist_search, ctx->fout);
write_mlist(&mlist_search, ctx->fout);
}
#if SHELL_ESCAPE || PIPEC
if (mlist_shell.modified)
{
write_mlist_header(&mlist_shell, ctx->fout);
write_mlist(&mlist_shell, ctx->fout);
}
#endif
} else if (ml != NULL)
{
/* Copy mlist entry. */
fprintf(ctx->fout, "\"%s\n", string);
}
/* Skip marks */
}
#endif /* CMD_HISTORY */
/*
* Make a file readable only by its owner.
*/
static void make_file_private(FILE *f)
{
#if HAVE_FCHMOD
lbool do_chmod = TRUE;
#if HAVE_STAT
struct stat statbuf;
int r = fstat(fileno(f), &statbuf);
if (r < 0 || !S_ISREG(statbuf.st_mode))
/* Don't chmod if not a regular file. */
do_chmod = FALSE;
#endif
if (do_chmod)
fchmod(fileno(f), 0600);
#endif
}
/*
* Does the history file need to be updated?
*/
#if CMD_HISTORY
static lbool histfile_modified(void)
{
if (mlist_search.modified)
return TRUE;
#if SHELL_ESCAPE || PIPEC
if (mlist_shell.modified)
return TRUE;
#endif
if (marks_modified)
return TRUE;
return FALSE;
}
#endif
/*
* Update the .lesshst file.
*/
public void save_cmdhist(void)
{
#if CMD_HISTORY
char *histname;
char *tempname;
int skip_search;
int skip_shell;
struct save_ctx ctx;
constant char *s;
FILE *fout = NULL;
int histsize = 0;
if (!secure_allow(SF_HISTORY) || !histfile_modified())
return;
histname = histfile_name(0);
if (histname == NULL)
return;
tempname = make_tempname(histname);
fout = fopen(tempname, "w");
if (fout != NULL)
{
make_file_private(fout);
s = lgetenv("LESSHISTSIZE");
if (s != NULL)
histsize = atoi(s);
if (histsize <= 0)
histsize = 100;
skip_search = mlist_size(&mlist_search) - histsize;
#if SHELL_ESCAPE || PIPEC
skip_shell = mlist_size(&mlist_shell) - histsize;
#endif
fprintf(fout, "%s\n", HISTFILE_FIRST_LINE);
ctx.fout = fout;
ctx.mlist = NULL;
read_cmdhist(©_hist, &ctx, skip_search, skip_shell);
save_marks(fout, HISTFILE_MARK_SECTION);
fclose(fout);
#if MSDOS_COMPILER==WIN32C
/*
* Windows rename doesn't remove an existing file,
* making it useless for atomic operations. Sigh.
*/
remove(histname);
#endif
rename(tempname, histname);
}
free(tempname);
free(histname);
#endif /* CMD_HISTORY */
}
|