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 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758
|
/* Handle lists of commands, their decoding and documentation, for GDB.
Copyright (C) 1986-2024 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "symtab.h"
#include <ctype.h>
#include "gdbsupport/gdb_regex.h"
#include "completer.h"
#include "ui-out.h"
#include "cli/cli-cmds.h"
#include "cli/cli-decode.h"
#include "cli/cli-style.h"
#include <optional>
/* Prototypes for local functions. */
static void undef_cmd_error (const char *, const char *);
static cmd_list_element::aliases_list_type delete_cmd
(const char *name, cmd_list_element **list, cmd_list_element **prehook,
cmd_list_element **prehookee, cmd_list_element **posthook,
cmd_list_element **posthookee);
static struct cmd_list_element *find_cmd (const char *command,
int len,
struct cmd_list_element *clist,
int ignore_help_classes,
int *nfound);
static void help_cmd_list (struct cmd_list_element *list,
enum command_class theclass,
bool recurse,
struct ui_file *stream);
static void help_all (struct ui_file *stream);
static int lookup_cmd_composition_1 (const char *text,
struct cmd_list_element **alias,
struct cmd_list_element **prefix_cmd,
struct cmd_list_element **cmd,
struct cmd_list_element *cur_list);
/* Look up a command whose 'subcommands' field is SUBCOMMANDS. Return the
command if found, otherwise return NULL. */
static struct cmd_list_element *
lookup_cmd_with_subcommands (cmd_list_element **subcommands,
cmd_list_element *list)
{
struct cmd_list_element *p = NULL;
for (p = list; p != NULL; p = p->next)
{
struct cmd_list_element *q;
if (!p->is_prefix ())
continue;
else if (p->subcommands == subcommands)
{
/* If we found an alias, we must return the aliased
command. */
return p->is_alias () ? p->alias_target : p;
}
q = lookup_cmd_with_subcommands (subcommands, *(p->subcommands));
if (q != NULL)
return q;
}
return NULL;
}
static void
print_help_for_command (const cmd_list_element &c,
bool recurse, struct ui_file *stream);
static void
do_simple_func (const char *args, int from_tty, cmd_list_element *c)
{
c->function.simple_func (args, from_tty);
}
static void
set_cmd_simple_func (struct cmd_list_element *cmd, cmd_simple_func_ftype *simple_func)
{
if (simple_func == NULL)
cmd->func = NULL;
else
cmd->func = do_simple_func;
cmd->function.simple_func = simple_func;
}
int
cmd_simple_func_eq (struct cmd_list_element *cmd, cmd_simple_func_ftype *simple_func)
{
return (cmd->func == do_simple_func
&& cmd->function.simple_func == simple_func);
}
void
set_cmd_completer (struct cmd_list_element *cmd, completer_ftype *completer)
{
cmd->completer = completer; /* Ok. */
}
/* See definition in commands.h. */
void
set_cmd_completer_handle_brkchars (struct cmd_list_element *cmd,
completer_handle_brkchars_ftype *func)
{
cmd->completer_handle_brkchars = func;
}
std::string
cmd_list_element::prefixname () const
{
if (!this->is_prefix ())
/* Not a prefix command. */
return "";
std::string prefixname;
if (this->prefix != nullptr)
prefixname = this->prefix->prefixname ();
prefixname += this->name;
prefixname += " ";
return prefixname;
}
/* See cli/cli-decode.h. */
std::vector<std::string>
cmd_list_element::command_components () const
{
std::vector<std::string> result;
if (this->prefix != nullptr)
result = this->prefix->command_components ();
result.emplace_back (this->name);
return result;
}
/* Add element named NAME.
Space for NAME and DOC must be allocated by the caller.
THECLASS is the top level category into which commands are broken down
for "help" purposes.
FUN should be the function to execute the command;
it will get a character string as argument, with leading
and trailing blanks already eliminated.
DOC is a documentation string for the command.
Its first line should be a complete sentence.
It should start with ? for a command that is an abbreviation
or with * for a command that most users don't need to know about.
Add this command to command list *LIST.
Returns a pointer to the added command (not necessarily the head
of *LIST). */
static struct cmd_list_element *
do_add_cmd (const char *name, enum command_class theclass,
const char *doc, struct cmd_list_element **list)
{
struct cmd_list_element *c = new struct cmd_list_element (name, theclass,
doc);
/* Turn each alias of the old command into an alias of the new
command. */
c->aliases = delete_cmd (name, list, &c->hook_pre, &c->hookee_pre,
&c->hook_post, &c->hookee_post);
for (cmd_list_element &alias : c->aliases)
alias.alias_target = c;
if (c->hook_pre)
c->hook_pre->hookee_pre = c;
if (c->hookee_pre)
c->hookee_pre->hook_pre = c;
if (c->hook_post)
c->hook_post->hookee_post = c;
if (c->hookee_post)
c->hookee_post->hook_post = c;
if (*list == NULL || strcmp ((*list)->name, name) >= 0)
{
c->next = *list;
*list = c;
}
else
{
cmd_list_element *p = *list;
while (p->next && strcmp (p->next->name, name) <= 0)
{
p = p->next;
}
c->next = p->next;
p->next = c;
}
/* Search the prefix cmd of C, and assigns it to C->prefix.
See also add_prefix_cmd and update_prefix_field_of_prefixed_commands. */
cmd_list_element *prefixcmd = lookup_cmd_with_subcommands (list, cmdlist);
c->prefix = prefixcmd;
return c;
}
struct cmd_list_element *
add_cmd (const char *name, enum command_class theclass,
const char *doc, struct cmd_list_element **list)
{
cmd_list_element *result = do_add_cmd (name, theclass, doc, list);
result->func = NULL;
result->function.simple_func = NULL;
return result;
}
struct cmd_list_element *
add_cmd (const char *name, enum command_class theclass,
cmd_simple_func_ftype *fun,
const char *doc, struct cmd_list_element **list)
{
cmd_list_element *result = do_add_cmd (name, theclass, doc, list);
set_cmd_simple_func (result, fun);
return result;
}
/* Add an element with a suppress notification to the LIST of commands. */
struct cmd_list_element *
add_cmd_suppress_notification (const char *name, enum command_class theclass,
cmd_simple_func_ftype *fun, const char *doc,
struct cmd_list_element **list,
bool *suppress_notification)
{
struct cmd_list_element *element;
element = add_cmd (name, theclass, fun, doc, list);
element->suppress_notification = suppress_notification;
return element;
}
/* Deprecates a command CMD.
REPLACEMENT is the name of the command which should be used in
place of this command, or NULL if no such command exists.
This function does not check to see if command REPLACEMENT exists
since gdb may not have gotten around to adding REPLACEMENT when
this function is called.
Returns a pointer to the deprecated command. */
struct cmd_list_element *
deprecate_cmd (struct cmd_list_element *cmd, const char *replacement)
{
cmd->cmd_deprecated = 1;
cmd->deprecated_warn_user = 1;
if (replacement != NULL)
cmd->replacement = replacement;
else
cmd->replacement = NULL;
return cmd;
}
struct cmd_list_element *
add_alias_cmd (const char *name, cmd_list_element *target,
enum command_class theclass, int abbrev_flag,
struct cmd_list_element **list)
{
gdb_assert (target != nullptr);
struct cmd_list_element *c = add_cmd (name, theclass, target->doc, list);
/* If TARGET->DOC can be freed, we should make another copy. */
if (target->doc_allocated)
{
c->doc = xstrdup (target->doc);
c->doc_allocated = 1;
}
/* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
c->func = target->func;
c->function = target->function;
c->subcommands = target->subcommands;
c->allow_unknown = target->allow_unknown;
c->abbrev_flag = abbrev_flag;
c->alias_target = target;
target->aliases.push_front (*c);
return c;
}
/* Update the prefix field of all sub-commands of the prefix command C.
We must do this when a prefix command is defined as the GDB init sequence
does not guarantee that a prefix command is created before its sub-commands.
For example, break-catch-sig.c initialization runs before breakpoint.c
initialization, but it is breakpoint.c that creates the "catch" command used
by the "catch signal" command created by break-catch-sig.c. */
static void
update_prefix_field_of_prefixed_commands (struct cmd_list_element *c)
{
for (cmd_list_element *p = *c->subcommands; p != NULL; p = p->next)
{
p->prefix = c;
/* We must recursively update the prefix field to cover
e.g. 'info auto-load libthread-db' where the creation
order was:
libthread-db
auto-load
info
In such a case, when 'auto-load' was created by do_add_cmd,
the 'libthread-db' prefix field could not be updated, as the
'auto-load' command was not yet reachable by
lookup_cmd_for_subcommands (list, cmdlist)
that searches from the top level 'cmdlist'. */
if (p->is_prefix ())
update_prefix_field_of_prefixed_commands (p);
}
}
/* Like add_cmd but adds an element for a command prefix: a name that
should be followed by a subcommand to be looked up in another
command list. SUBCOMMANDS should be the address of the variable
containing that list. */
struct cmd_list_element *
add_prefix_cmd (const char *name, enum command_class theclass,
cmd_simple_func_ftype *fun,
const char *doc, struct cmd_list_element **subcommands,
int allow_unknown, struct cmd_list_element **list)
{
struct cmd_list_element *c = add_cmd (name, theclass, fun, doc, list);
c->subcommands = subcommands;
c->allow_unknown = allow_unknown;
/* Now that prefix command C is defined, we need to set the prefix field
of all prefixed commands that were defined before C itself was defined. */
update_prefix_field_of_prefixed_commands (c);
return c;
}
/* A helper function for add_basic_prefix_cmd. This is a command
function that just forwards to help_list. */
static void
do_prefix_cmd (const char *args, int from_tty, struct cmd_list_element *c)
{
/* Look past all aliases. */
while (c->is_alias ())
c = c->alias_target;
help_list (*c->subcommands, c->prefixname ().c_str (),
all_commands, gdb_stdout);
}
/* See command.h. */
struct cmd_list_element *
add_basic_prefix_cmd (const char *name, enum command_class theclass,
const char *doc, struct cmd_list_element **subcommands,
int allow_unknown, struct cmd_list_element **list)
{
struct cmd_list_element *cmd = add_prefix_cmd (name, theclass, nullptr,
doc, subcommands,
allow_unknown, list);
cmd->func = do_prefix_cmd;
return cmd;
}
/* A helper function for add_show_prefix_cmd. This is a command
function that just forwards to cmd_show_list. */
static void
do_show_prefix_cmd (const char *args, int from_tty, struct cmd_list_element *c)
{
cmd_show_list (*c->subcommands, from_tty);
}
/* See command.h. */
struct cmd_list_element *
add_show_prefix_cmd (const char *name, enum command_class theclass,
const char *doc, struct cmd_list_element **subcommands,
int allow_unknown, struct cmd_list_element **list)
{
struct cmd_list_element *cmd = add_prefix_cmd (name, theclass, nullptr,
doc, subcommands,
allow_unknown, list);
cmd->func = do_show_prefix_cmd;
return cmd;
}
/* See command.h. */
set_show_commands
add_setshow_prefix_cmd (const char *name, command_class theclass,
const char *set_doc, const char *show_doc,
cmd_list_element **set_subcommands_list,
cmd_list_element **show_subcommands_list,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
set_show_commands cmds;
cmds.set = add_basic_prefix_cmd (name, theclass, set_doc,
set_subcommands_list, 0,
set_list);
cmds.show = add_show_prefix_cmd (name, theclass, show_doc,
show_subcommands_list, 0,
show_list);
return cmds;
}
/* Like ADD_PREFIX_CMD but sets the suppress_notification pointer on the
new command list element. */
struct cmd_list_element *
add_prefix_cmd_suppress_notification
(const char *name, enum command_class theclass,
cmd_simple_func_ftype *fun,
const char *doc, struct cmd_list_element **subcommands,
int allow_unknown, struct cmd_list_element **list,
bool *suppress_notification)
{
struct cmd_list_element *element
= add_prefix_cmd (name, theclass, fun, doc, subcommands,
allow_unknown, list);
element->suppress_notification = suppress_notification;
return element;
}
/* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
struct cmd_list_element *
add_abbrev_prefix_cmd (const char *name, enum command_class theclass,
cmd_simple_func_ftype *fun, const char *doc,
struct cmd_list_element **subcommands,
int allow_unknown, struct cmd_list_element **list)
{
struct cmd_list_element *c = add_cmd (name, theclass, fun, doc, list);
c->subcommands = subcommands;
c->allow_unknown = allow_unknown;
c->abbrev_flag = 1;
return c;
}
/* This is an empty "simple func". */
void
not_just_help_class_command (const char *args, int from_tty)
{
}
/* This is an empty cmd func. */
static void
empty_func (const char *args, int from_tty, cmd_list_element *c)
{
}
/* Add element named NAME to command list LIST (the list for set/show
or some sublist thereof).
TYPE is set_cmd or show_cmd.
THECLASS is as in add_cmd.
VAR_TYPE is the kind of thing we are setting.
EXTRA_LITERALS if non-NULL define extra literals to be accepted in lieu of
a number for integer variables.
ARGS is a pre-validated type-erased reference to the variable being
controlled by this command.
DOC is the documentation string. */
static struct cmd_list_element *
add_set_or_show_cmd (const char *name,
enum cmd_types type,
enum command_class theclass,
var_types var_type,
const literal_def *extra_literals,
const setting::erased_args &arg,
const char *doc,
struct cmd_list_element **list)
{
struct cmd_list_element *c = add_cmd (name, theclass, doc, list);
gdb_assert (type == set_cmd || type == show_cmd);
c->type = type;
c->var.emplace (var_type, extra_literals, arg);
/* This needs to be something besides NULL so that this isn't
treated as a help class. */
c->func = empty_func;
return c;
}
/* Add element named NAME to both command lists SET_LIST and SHOW_LIST.
THECLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
setting. EXTRA_LITERALS if non-NULL define extra literals to be
accepted in lieu of a number for integer variables. ARGS is a
pre-validated type-erased reference to the variable being controlled
by this command. SET_FUNC and SHOW_FUNC are the callback functions
(if non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation
strings.
Return the newly created set and show commands. */
static set_show_commands
add_setshow_cmd_full_erased (const char *name,
enum command_class theclass,
var_types var_type,
const literal_def *extra_literals,
const setting::erased_args &args,
const char *set_doc, const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
struct cmd_list_element *set;
struct cmd_list_element *show;
gdb::unique_xmalloc_ptr<char> full_set_doc;
gdb::unique_xmalloc_ptr<char> full_show_doc;
if (help_doc != NULL)
{
full_set_doc = xstrprintf ("%s\n%s", set_doc, help_doc);
full_show_doc = xstrprintf ("%s\n%s", show_doc, help_doc);
}
else
{
full_set_doc = make_unique_xstrdup (set_doc);
full_show_doc = make_unique_xstrdup (show_doc);
}
set = add_set_or_show_cmd (name, set_cmd, theclass, var_type,
extra_literals, args,
full_set_doc.release (), set_list);
set->doc_allocated = 1;
if (set_func != NULL)
set->func = set_func;
show = add_set_or_show_cmd (name, show_cmd, theclass, var_type,
extra_literals, args,
full_show_doc.release (), show_list);
show->doc_allocated = 1;
show->show_value_func = show_func;
/* Disable the default symbol completer. Doesn't make much sense
for the "show" command to complete on anything. */
set_cmd_completer (show, nullptr);
return {set, show};
}
/* Completes on integer commands that support extra literals. */
static void
integer_literals_completer (struct cmd_list_element *c,
completion_tracker &tracker,
const char *text, const char *word)
{
const literal_def *extra_literals = c->var->extra_literals ();
if (*text == '\0')
{
tracker.add_completion (make_unique_xstrdup ("NUMBER"));
for (const literal_def *l = extra_literals;
l->literal != nullptr;
l++)
tracker.add_completion (make_unique_xstrdup (l->literal));
}
else
for (const literal_def *l = extra_literals;
l->literal != nullptr;
l++)
if (startswith (l->literal, text))
tracker.add_completion (make_unique_xstrdup (l->literal));
}
/* Add element named NAME to both command lists SET_LIST and SHOW_LIST.
THECLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
setting. VAR is address of the variable being controlled by this
command. EXTRA_LITERALS if non-NULL define extra literals to be
accepted in lieu of a number for integer variables. If nullptr is
given as VAR, then both SET_SETTING_FUNC and GET_SETTING_FUNC must
be provided. SET_SETTING_FUNC and GET_SETTING_FUNC are callbacks
used to access and modify the underlying property, whatever its
storage is. SET_FUNC and SHOW_FUNC are the callback functions
(if non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the
documentation strings.
Return the newly created set and show commands. */
template<typename T>
static set_show_commands
add_setshow_cmd_full (const char *name,
enum command_class theclass,
var_types var_type, T *var,
const literal_def *extra_literals,
const char *set_doc, const char *show_doc,
const char *help_doc,
typename setting_func_types<T>::set set_setting_func,
typename setting_func_types<T>::get get_setting_func,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
auto erased_args
= setting::erase_args (var_type, var,
set_setting_func, get_setting_func);
auto cmds = add_setshow_cmd_full_erased (name,
theclass,
var_type, extra_literals,
erased_args,
set_doc, show_doc,
help_doc,
set_func,
show_func,
set_list,
show_list);
if (extra_literals != nullptr)
set_cmd_completer (cmds.set, integer_literals_completer);
return cmds;
}
/* Same as above but omitting EXTRA_LITERALS. */
template<typename T>
static set_show_commands
add_setshow_cmd_full (const char *name,
enum command_class theclass,
var_types var_type, T *var,
const char *set_doc, const char *show_doc,
const char *help_doc,
typename setting_func_types<T>::set set_setting_func,
typename setting_func_types<T>::get get_setting_func,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
return add_setshow_cmd_full (name, theclass, var_type, var, nullptr,
set_doc, show_doc, help_doc,
set_setting_func, get_setting_func,
set_func, show_func, set_list, show_list);
}
/* Add element named NAME to command list LIST (the list for set or
some sublist thereof). THECLASS is as in add_cmd. ENUMLIST is a list
of strings which may follow NAME. VAR is address of the variable
which will contain the matching string (from ENUMLIST). */
set_show_commands
add_setshow_enum_cmd (const char *name,
enum command_class theclass,
const char *const *enumlist,
const char **var,
const char *set_doc,
const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
/* We require *VAR to be initialized before this call, and
furthermore it must be == to one of the values in ENUMLIST. */
gdb_assert (var != nullptr && *var != nullptr);
for (int i = 0; ; ++i)
{
gdb_assert (enumlist[i] != nullptr);
if (*var == enumlist[i])
break;
}
set_show_commands commands
= add_setshow_cmd_full<const char *> (name, theclass, var_enum, var,
set_doc, show_doc, help_doc,
nullptr, nullptr, set_func,
show_func, set_list, show_list);
commands.set->enums = enumlist;
return commands;
}
/* Same as above but using a getter and a setter function instead of a pointer
to a global storage buffer. */
set_show_commands
add_setshow_enum_cmd (const char *name, command_class theclass,
const char *const *enumlist, const char *set_doc,
const char *show_doc, const char *help_doc,
setting_func_types<const char *>::set set_func,
setting_func_types<const char *>::get get_func,
show_value_ftype *show_func,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
auto cmds = add_setshow_cmd_full<const char *> (name, theclass, var_enum,
nullptr, set_doc, show_doc,
help_doc, set_func, get_func,
nullptr, show_func, set_list,
show_list);
cmds.set->enums = enumlist;
return cmds;
}
/* See cli-decode.h. */
const char * const auto_boolean_enums[] = { "on", "off", "auto", NULL };
/* Add an auto-boolean command named NAME to both the set and show
command list lists. THECLASS is as in add_cmd. VAR is address of the
variable which will contain the value. DOC is the documentation
string. FUNC is the corresponding callback. */
set_show_commands
add_setshow_auto_boolean_cmd (const char *name,
enum command_class theclass,
enum auto_boolean *var,
const char *set_doc, const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
set_show_commands commands
= add_setshow_cmd_full<enum auto_boolean> (name, theclass, var_auto_boolean,
var, set_doc, show_doc, help_doc,
nullptr, nullptr, set_func,
show_func, set_list, show_list);
commands.set->enums = auto_boolean_enums;
return commands;
}
/* Same as above but using a getter and a setter function instead of a pointer
to a global storage buffer. */
set_show_commands
add_setshow_auto_boolean_cmd (const char *name, command_class theclass,
const char *set_doc, const char *show_doc,
const char *help_doc,
setting_func_types<enum auto_boolean>::set set_func,
setting_func_types<enum auto_boolean>::get get_func,
show_value_ftype *show_func,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
auto cmds = add_setshow_cmd_full<enum auto_boolean> (name, theclass,
var_auto_boolean,
nullptr, set_doc,
show_doc, help_doc,
set_func, get_func,
nullptr, show_func,
set_list, show_list);
cmds.set->enums = auto_boolean_enums;
return cmds;
}
/* See cli-decode.h. */
const char * const boolean_enums[] = { "on", "off", NULL };
/* Add element named NAME to both the set and show command LISTs (the
list for set/show or some sublist thereof). THECLASS is as in
add_cmd. VAR is address of the variable which will contain the
value. SET_DOC and SHOW_DOC are the documentation strings.
Returns the new command element. */
set_show_commands
add_setshow_boolean_cmd (const char *name, enum command_class theclass, bool *var,
const char *set_doc, const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
set_show_commands commands
= add_setshow_cmd_full<bool> (name, theclass, var_boolean, var,
set_doc, show_doc, help_doc,
nullptr, nullptr, set_func, show_func,
set_list, show_list);
commands.set->enums = boolean_enums;
return commands;
}
/* Same as above but using a getter and a setter function instead of a pointer
to a global storage buffer. */
set_show_commands
add_setshow_boolean_cmd (const char *name, command_class theclass,
const char *set_doc, const char *show_doc,
const char *help_doc,
setting_func_types<bool>::set set_func,
setting_func_types<bool>::get get_func,
show_value_ftype *show_func,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
auto cmds = add_setshow_cmd_full<bool> (name, theclass, var_boolean, nullptr,
set_doc, show_doc, help_doc,
set_func, get_func, nullptr,
show_func, set_list, show_list);
cmds.set->enums = boolean_enums;
return cmds;
}
/* Add element named NAME to both the set and show command LISTs (the
list for set/show or some sublist thereof). */
set_show_commands
add_setshow_filename_cmd (const char *name, enum command_class theclass,
std::string *var,
const char *set_doc, const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
set_show_commands commands
= add_setshow_cmd_full<std::string> (name, theclass, var_filename, var,
set_doc, show_doc, help_doc,
nullptr, nullptr, set_func,
show_func, set_list, show_list);
set_cmd_completer (commands.set, deprecated_filename_completer);
return commands;
}
/* Same as above but using a getter and a setter function instead of a pointer
to a global storage buffer. */
set_show_commands
add_setshow_filename_cmd (const char *name, command_class theclass,
const char *set_doc, const char *show_doc,
const char *help_doc,
setting_func_types<std::string>::set set_func,
setting_func_types<std::string>::get get_func,
show_value_ftype *show_func,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
auto cmds = add_setshow_cmd_full<std::string> (name, theclass, var_filename,
nullptr, set_doc, show_doc,
help_doc, set_func, get_func,
nullptr, show_func, set_list,
show_list);
set_cmd_completer (cmds.set, deprecated_filename_completer);
return cmds;
}
/* Add element named NAME to both the set and show command LISTs (the
list for set/show or some sublist thereof). */
set_show_commands
add_setshow_string_cmd (const char *name, enum command_class theclass,
std::string *var,
const char *set_doc, const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
set_show_commands commands
= add_setshow_cmd_full<std::string> (name, theclass, var_string, var,
set_doc, show_doc, help_doc,
nullptr, nullptr, set_func,
show_func, set_list, show_list);
/* Disable the default symbol completer. */
set_cmd_completer (commands.set, nullptr);
return commands;
}
/* Same as above but using a getter and a setter function instead of a pointer
to a global storage buffer. */
set_show_commands
add_setshow_string_cmd (const char *name, command_class theclass,
const char *set_doc, const char *show_doc,
const char *help_doc,
setting_func_types<std::string>::set set_func,
setting_func_types<std::string>::get get_func,
show_value_ftype *show_func,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
auto cmds = add_setshow_cmd_full<std::string> (name, theclass, var_string,
nullptr, set_doc, show_doc,
help_doc, set_func, get_func,
nullptr, show_func, set_list,
show_list);
/* Disable the default symbol completer. */
set_cmd_completer (cmds.set, nullptr);
return cmds;
}
/* Add element named NAME to both the set and show command LISTs (the
list for set/show or some sublist thereof). */
set_show_commands
add_setshow_string_noescape_cmd (const char *name, enum command_class theclass,
std::string *var,
const char *set_doc, const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
set_show_commands commands
= add_setshow_cmd_full<std::string> (name, theclass, var_string_noescape,
var, set_doc, show_doc, help_doc,
nullptr, nullptr, set_func, show_func,
set_list, show_list);
/* Disable the default symbol completer. */
set_cmd_completer (commands.set, nullptr);
return commands;
}
/* Same as above but using a getter and a setter function instead of a pointer
to a global storage buffer. */
set_show_commands
add_setshow_string_noescape_cmd (const char *name, command_class theclass,
const char *set_doc, const char *show_doc,
const char *help_doc,
setting_func_types<std::string>::set set_func,
setting_func_types<std::string>::get get_func,
show_value_ftype *show_func,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
auto cmds = add_setshow_cmd_full<std::string> (name, theclass,
var_string_noescape, nullptr,
set_doc, show_doc, help_doc,
set_func, get_func,
nullptr, show_func, set_list,
show_list);
/* Disable the default symbol completer. */
set_cmd_completer (cmds.set, nullptr);
return cmds;
}
/* Add element named NAME to both the set and show command LISTs (the
list for set/show or some sublist thereof). */
set_show_commands
add_setshow_optional_filename_cmd (const char *name, enum command_class theclass,
std::string *var,
const char *set_doc, const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
set_show_commands commands
= add_setshow_cmd_full<std::string> (name, theclass, var_optional_filename,
var, set_doc, show_doc, help_doc,
nullptr, nullptr, set_func, show_func,
set_list, show_list);
set_cmd_completer (commands.set, deprecated_filename_completer);
return commands;
}
/* Same as above but using a getter and a setter function instead of a pointer
to a global storage buffer. */
set_show_commands
add_setshow_optional_filename_cmd (const char *name, command_class theclass,
const char *set_doc, const char *show_doc,
const char *help_doc,
setting_func_types<std::string>::set set_func,
setting_func_types<std::string>::get get_func,
show_value_ftype *show_func,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
auto cmds =
add_setshow_cmd_full<std::string> (name, theclass, var_optional_filename,
nullptr, set_doc, show_doc, help_doc,
set_func, get_func, nullptr, show_func,
set_list,show_list);
set_cmd_completer (cmds.set, deprecated_filename_completer);
return cmds;
}
/* Add element named NAME to both the set and show command LISTs (the
list for set/show or some sublist thereof). THECLASS is as in
add_cmd. VAR is address of the variable which will contain the
value. SET_DOC and SHOW_DOC are the documentation strings. This
function is only used in Python API. Please don't use it elsewhere. */
set_show_commands
add_setshow_integer_cmd (const char *name, enum command_class theclass,
int *var, const literal_def *extra_literals,
const char *set_doc, const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
set_show_commands commands
= add_setshow_cmd_full<int> (name, theclass, var_integer, var,
extra_literals, set_doc, show_doc,
help_doc, nullptr, nullptr, set_func,
show_func, set_list, show_list);
return commands;
}
/* Same as above but using a getter and a setter function instead of a pointer
to a global storage buffer. */
set_show_commands
add_setshow_integer_cmd (const char *name, command_class theclass,
const literal_def *extra_literals,
const char *set_doc, const char *show_doc,
const char *help_doc,
setting_func_types<int>::set set_func,
setting_func_types<int>::get get_func,
show_value_ftype *show_func,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
auto cmds = add_setshow_cmd_full<int> (name, theclass, var_integer, nullptr,
extra_literals, set_doc, show_doc,
help_doc, set_func, get_func, nullptr,
show_func, set_list, show_list);
return cmds;
}
/* Accept `unlimited' or 0, translated internally to INT_MAX. */
const literal_def integer_unlimited_literals[] =
{
{ "unlimited", INT_MAX, 0 },
{ nullptr }
};
/* Same as above but using `integer_unlimited_literals', with a pointer
to a global storage buffer. */
set_show_commands
add_setshow_integer_cmd (const char *name, enum command_class theclass,
int *var,
const char *set_doc, const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
set_show_commands commands
= add_setshow_cmd_full<int> (name, theclass, var_integer, var,
integer_unlimited_literals,
set_doc, show_doc, help_doc,
nullptr, nullptr, set_func,
show_func, set_list, show_list);
return commands;
}
/* Same as above but using a getter and a setter function instead of a pointer
to a global storage buffer. */
set_show_commands
add_setshow_integer_cmd (const char *name, command_class theclass,
const char *set_doc, const char *show_doc,
const char *help_doc,
setting_func_types<int>::set set_func,
setting_func_types<int>::get get_func,
show_value_ftype *show_func,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
auto cmds = add_setshow_cmd_full<int> (name, theclass, var_integer, nullptr,
integer_unlimited_literals,
set_doc, show_doc, help_doc, set_func,
get_func, nullptr, show_func, set_list,
show_list);
return cmds;
}
/* Add element named NAME to both the set and show command LISTs (the
list for set/show or some sublist thereof). CLASS is as in
add_cmd. VAR is address of the variable which will contain the
value. SET_DOC and SHOW_DOC are the documentation strings. */
set_show_commands
add_setshow_pinteger_cmd (const char *name, enum command_class theclass,
int *var, const literal_def *extra_literals,
const char *set_doc, const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
set_show_commands commands
= add_setshow_cmd_full<int> (name, theclass, var_pinteger, var,
extra_literals, set_doc, show_doc,
help_doc, nullptr, nullptr, set_func,
show_func, set_list, show_list);
return commands;
}
/* Same as above but using a getter and a setter function instead of a pointer
to a global storage buffer. */
set_show_commands
add_setshow_pinteger_cmd (const char *name, command_class theclass,
const literal_def *extra_literals,
const char *set_doc, const char *show_doc,
const char *help_doc,
setting_func_types<int>::set set_func,
setting_func_types<int>::get get_func,
show_value_ftype *show_func,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
auto cmds = add_setshow_cmd_full<int> (name, theclass, var_pinteger, nullptr,
extra_literals, set_doc, show_doc,
help_doc, set_func, get_func, nullptr,
show_func, set_list, show_list);
return cmds;
}
/* Add element named NAME to both the set and show command LISTs (the
list for set/show or some sublist thereof). THECLASS is as in
add_cmd. VAR is address of the variable which will contain the
value. SET_DOC and SHOW_DOC are the documentation strings. */
set_show_commands
add_setshow_uinteger_cmd (const char *name, enum command_class theclass,
unsigned int *var, const literal_def *extra_literals,
const char *set_doc, const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
set_show_commands commands
= add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger, var,
extra_literals, set_doc, show_doc,
help_doc, nullptr, nullptr, set_func,
show_func, set_list, show_list);
return commands;
}
/* Same as above but using a getter and a setter function instead of a pointer
to a global storage buffer. */
set_show_commands
add_setshow_uinteger_cmd (const char *name, command_class theclass,
const literal_def *extra_literals,
const char *set_doc, const char *show_doc,
const char *help_doc,
setting_func_types<unsigned int>::set set_func,
setting_func_types<unsigned int>::get get_func,
show_value_ftype *show_func,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
auto cmds = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger,
nullptr, extra_literals,
set_doc, show_doc, help_doc,
set_func, get_func, nullptr,
show_func, set_list,
show_list);
return cmds;
}
/* Accept `unlimited' or 0, translated internally to UINT_MAX. */
const literal_def uinteger_unlimited_literals[] =
{
{ "unlimited", UINT_MAX, 0 },
{ nullptr }
};
/* Same as above but using `uinteger_unlimited_literals', with a pointer
to a global storage buffer. */
set_show_commands
add_setshow_uinteger_cmd (const char *name, enum command_class theclass,
unsigned int *var,
const char *set_doc, const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
set_show_commands commands
= add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger, var,
uinteger_unlimited_literals,
set_doc, show_doc, help_doc, nullptr,
nullptr, set_func, show_func,
set_list, show_list);
return commands;
}
/* Same as above but using a getter and a setter function instead of a pointer
to a global storage buffer. */
set_show_commands
add_setshow_uinteger_cmd (const char *name, command_class theclass,
const char *set_doc, const char *show_doc,
const char *help_doc,
setting_func_types<unsigned int>::set set_func,
setting_func_types<unsigned int>::get get_func,
show_value_ftype *show_func,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
auto cmds = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger,
nullptr,
uinteger_unlimited_literals,
set_doc, show_doc, help_doc,
set_func, get_func, nullptr,
show_func, set_list,
show_list);
return cmds;
}
/* Add element named NAME to both the set and show command LISTs (the
list for set/show or some sublist thereof). THECLASS is as in
add_cmd. VAR is address of the variable which will contain the
value. SET_DOC and SHOW_DOC are the documentation strings. */
set_show_commands
add_setshow_zinteger_cmd (const char *name, enum command_class theclass,
int *var,
const char *set_doc, const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
return add_setshow_cmd_full<int> (name, theclass, var_integer, var,
set_doc, show_doc, help_doc,
nullptr, nullptr, set_func,
show_func, set_list, show_list);
}
/* Same as above but using a getter and a setter function instead of a pointer
to a global storage buffer. */
set_show_commands
add_setshow_zinteger_cmd (const char *name, command_class theclass,
const char *set_doc, const char *show_doc,
const char *help_doc,
setting_func_types<int>::set set_func,
setting_func_types<int>::get get_func,
show_value_ftype *show_func,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
return add_setshow_cmd_full<int> (name, theclass, var_integer, nullptr,
set_doc, show_doc, help_doc, set_func,
get_func, nullptr, show_func, set_list,
show_list);
}
/* Accept `unlimited' or -1, using -1 internally. */
const literal_def pinteger_unlimited_literals[] =
{
{ "unlimited", -1, -1 },
{ nullptr }
};
/* Same as above but using `pinteger_unlimited_literals', with a pointer
to a global storage buffer. */
set_show_commands
add_setshow_zuinteger_unlimited_cmd (const char *name,
enum command_class theclass,
int *var,
const char *set_doc,
const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
set_show_commands commands
= add_setshow_cmd_full<int> (name, theclass, var_pinteger, var,
pinteger_unlimited_literals,
set_doc, show_doc, help_doc, nullptr,
nullptr, set_func, show_func, set_list,
show_list);
return commands;
}
/* Same as above but using a getter and a setter function instead of a pointer
to a global storage buffer. */
set_show_commands
add_setshow_zuinteger_unlimited_cmd (const char *name, command_class theclass,
const char *set_doc, const char *show_doc,
const char *help_doc,
setting_func_types<int>::set set_func,
setting_func_types<int>::get get_func,
show_value_ftype *show_func,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
auto cmds
= add_setshow_cmd_full<int> (name, theclass, var_pinteger, nullptr,
pinteger_unlimited_literals,
set_doc, show_doc, help_doc, set_func,
get_func, nullptr, show_func, set_list,
show_list);
return cmds;
}
/* Add element named NAME to both the set and show command LISTs (the
list for set/show or some sublist thereof). THECLASS is as in
add_cmd. VAR is address of the variable which will contain the
value. SET_DOC and SHOW_DOC are the documentation strings. */
set_show_commands
add_setshow_zuinteger_cmd (const char *name, enum command_class theclass,
unsigned int *var,
const char *set_doc, const char *show_doc,
const char *help_doc,
cmd_func_ftype *set_func,
show_value_ftype *show_func,
struct cmd_list_element **set_list,
struct cmd_list_element **show_list)
{
return add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger,
var, set_doc, show_doc, help_doc,
nullptr, nullptr, set_func,
show_func, set_list, show_list);
}
/* Same as above but using a getter and a setter function instead of a pointer
to a global storage buffer. */
set_show_commands
add_setshow_zuinteger_cmd (const char *name, command_class theclass,
const char *set_doc, const char *show_doc,
const char *help_doc,
setting_func_types<unsigned int>::set set_func,
setting_func_types<unsigned int>::get get_func,
show_value_ftype *show_func,
cmd_list_element **set_list,
cmd_list_element **show_list)
{
return add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger,
nullptr, set_doc, show_doc,
help_doc, set_func, get_func,
nullptr, show_func, set_list,
show_list);
}
/* Remove the command named NAME from the command list. Return the list
commands which were aliased to the deleted command. The various *HOOKs are
set to the pre- and post-hook commands for the deleted command. If the
command does not have a hook, the corresponding out parameter is set to
NULL. */
static cmd_list_element::aliases_list_type
delete_cmd (const char *name, struct cmd_list_element **list,
struct cmd_list_element **prehook,
struct cmd_list_element **prehookee,
struct cmd_list_element **posthook,
struct cmd_list_element **posthookee)
{
struct cmd_list_element *iter;
struct cmd_list_element **previous_chain_ptr;
cmd_list_element::aliases_list_type aliases;
*prehook = NULL;
*prehookee = NULL;
*posthook = NULL;
*posthookee = NULL;
previous_chain_ptr = list;
for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr)
{
if (strcmp (iter->name, name) == 0)
{
if (iter->destroyer)
iter->destroyer (iter, iter->context ());
if (iter->hookee_pre)
iter->hookee_pre->hook_pre = 0;
*prehook = iter->hook_pre;
*prehookee = iter->hookee_pre;
if (iter->hookee_post)
iter->hookee_post->hook_post = 0;
*posthook = iter->hook_post;
*posthookee = iter->hookee_post;
/* Update the link. */
*previous_chain_ptr = iter->next;
aliases = std::move (iter->aliases);
/* If this command was an alias, remove it from the list of
aliases. */
if (iter->is_alias ())
{
auto it = iter->alias_target->aliases.iterator_to (*iter);
iter->alias_target->aliases.erase (it);
}
delete iter;
/* We won't see another command with the same name. */
break;
}
else
previous_chain_ptr = &iter->next;
}
return aliases;
}
/* Shorthands to the commands above. */
/* Add an element to the list of info subcommands. */
struct cmd_list_element *
add_info (const char *name, cmd_simple_func_ftype *fun, const char *doc)
{
return add_cmd (name, class_info, fun, doc, &infolist);
}
/* Add an alias to the list of info subcommands. */
cmd_list_element *
add_info_alias (const char *name, cmd_list_element *target, int abbrev_flag)
{
return add_alias_cmd (name, target, class_run, abbrev_flag, &infolist);
}
/* Add an element to the list of commands. */
struct cmd_list_element *
add_com (const char *name, enum command_class theclass,
cmd_simple_func_ftype *fun,
const char *doc)
{
return add_cmd (name, theclass, fun, doc, &cmdlist);
}
/* Add an alias or abbreviation command to the list of commands.
For aliases predefined by GDB (such as bt), THECLASS must be
different of class_alias, as class_alias is used to identify
user defined aliases. */
cmd_list_element *
add_com_alias (const char *name, cmd_list_element *target,
command_class theclass, int abbrev_flag)
{
return add_alias_cmd (name, target, theclass, abbrev_flag, &cmdlist);
}
/* Add an element with a suppress notification to the list of commands. */
struct cmd_list_element *
add_com_suppress_notification (const char *name, enum command_class theclass,
cmd_simple_func_ftype *fun, const char *doc,
bool *suppress_notification)
{
return add_cmd_suppress_notification (name, theclass, fun, doc,
&cmdlist, suppress_notification);
}
/* Print the prefix of C followed by name of C in command style. */
static void
fput_command_name_styled (const cmd_list_element &c, struct ui_file *stream)
{
std::string prefixname
= c.prefix == nullptr ? "" : c.prefix->prefixname ();
fprintf_styled (stream, command_style.style (), "%s%s",
prefixname.c_str (), c.name);
}
/* True if ALIAS has a user-defined documentation. */
static bool
user_documented_alias (const cmd_list_element &alias)
{
gdb_assert (alias.is_alias ());
/* Alias is user documented if it has an allocated documentation
that differs from the aliased command. */
return (alias.doc_allocated
&& strcmp (alias.doc, alias.alias_target->doc) != 0);
}
/* Print the definition of alias C using title style for alias
and aliased command. */
static void
fput_alias_definition_styled (const cmd_list_element &c,
struct ui_file *stream)
{
gdb_assert (c.is_alias ());
gdb_puts (" alias ", stream);
fput_command_name_styled (c, stream);
gdb_printf (stream, " = ");
fput_command_name_styled (*c.alias_target, stream);
gdb_printf (stream, " %s\n", c.default_args.c_str ());
}
/* Print the definition of CMD aliases not deprecated and having default args
and not specifically documented by the user. */
static void
fput_aliases_definition_styled (const cmd_list_element &cmd,
struct ui_file *stream)
{
for (const cmd_list_element &alias : cmd.aliases)
if (!alias.cmd_deprecated
&& !user_documented_alias (alias)
&& !alias.default_args.empty ())
fput_alias_definition_styled (alias, stream);
}
/* If C has one or more aliases, style print the name of C and the name of its
aliases not documented specifically by the user, separated by commas.
If ALWAYS_FPUT_C_NAME, print the name of C even if it has no aliases.
If one or more names are printed, POSTFIX is printed after the last name.
*/
static void
fput_command_names_styled (const cmd_list_element &c,
bool always_fput_c_name, const char *postfix,
struct ui_file *stream)
{
/* First, check if we are going to print something. That is, either if
ALWAYS_FPUT_C_NAME is true or if there exists at least one non-deprecated
alias not documented specifically by the user. */
auto print_alias = [] (const cmd_list_element &alias)
{
return !alias.cmd_deprecated && !user_documented_alias (alias);
};
bool print_something = always_fput_c_name;
if (!print_something)
for (const cmd_list_element &alias : c.aliases)
{
if (!print_alias (alias))
continue;
print_something = true;
break;
}
if (print_something)
fput_command_name_styled (c, stream);
for (const cmd_list_element &alias : c.aliases)
{
if (!print_alias (alias))
continue;
gdb_puts (", ", stream);
stream->wrap_here (3);
fput_command_name_styled (alias, stream);
}
if (print_something)
gdb_puts (postfix, stream);
}
/* If VERBOSE, print the full help for command C and highlight the
documentation parts matching HIGHLIGHT,
otherwise print only one-line help for command C. */
static void
print_doc_of_command (const cmd_list_element &c, bool verbose,
compiled_regex &highlight, struct ui_file *stream)
{
/* When printing the full documentation, add a line to separate
this documentation from the previous command help, in the likely
case that apropos finds several commands. */
if (verbose)
gdb_puts ("\n", stream);
fput_command_names_styled (c, true,
verbose ? "" : " -- ", stream);
if (verbose)
{
gdb_puts ("\n", stream);
fput_aliases_definition_styled (c, stream);
fputs_highlighted (c.doc, highlight, stream);
gdb_puts ("\n", stream);
}
else
{
print_doc_line (stream, c.doc, false);
gdb_puts ("\n", stream);
fput_aliases_definition_styled (c, stream);
}
}
/* Recursively walk the commandlist structures, and print out the
documentation of commands that match our regex in either their
name, or their documentation.
If VERBOSE, prints the complete documentation and highlight the
documentation parts matching REGEX, otherwise prints only
the first line.
*/
void
apropos_cmd (struct ui_file *stream,
struct cmd_list_element *commandlist,
bool verbose, compiled_regex ®ex)
{
struct cmd_list_element *c;
int returnvalue;
/* Walk through the commands. */
for (c=commandlist;c;c=c->next)
{
if (c->is_alias () && !user_documented_alias (*c))
{
/* Command aliases/abbreviations not specifically documented by the
user are skipped to ensure we print the doc of a command only once,
when encountering the aliased command. */
continue;
}
returnvalue = -1; /* Needed to avoid double printing. */
if (c->name != NULL)
{
size_t name_len = strlen (c->name);
/* Try to match against the name. */
returnvalue = regex.search (c->name, name_len, 0, name_len, NULL);
if (returnvalue >= 0)
print_doc_of_command (*c, verbose, regex, stream);
/* Try to match against the name of the aliases. */
for (const cmd_list_element &alias : c->aliases)
{
name_len = strlen (alias.name);
returnvalue = regex.search (alias.name, name_len, 0, name_len, NULL);
if (returnvalue >= 0)
{
print_doc_of_command (*c, verbose, regex, stream);
break;
}
}
}
if (c->doc != NULL && returnvalue < 0)
{
size_t doc_len = strlen (c->doc);
/* Try to match against documentation. */
if (regex.search (c->doc, doc_len, 0, doc_len, NULL) >= 0)
print_doc_of_command (*c, verbose, regex, stream);
}
/* Check if this command has subcommands. */
if (c->is_prefix ())
{
/* Recursively call ourselves on the subcommand list,
passing the right prefix in. */
apropos_cmd (stream, *c->subcommands, verbose, regex);
}
}
}
/* This command really has to deal with two things:
1) I want documentation on *this string* (usually called by
"help commandname").
2) I want documentation on *this list* (usually called by giving a
command that requires subcommands. Also called by saying just
"help".)
I am going to split this into two separate commands, help_cmd and
help_list. */
void
help_cmd (const char *command, struct ui_file *stream)
{
struct cmd_list_element *c, *alias, *prefix_cmd, *c_cmd;
if (!command)
{
help_list (cmdlist, "", all_classes, stream);
return;
}
if (strcmp (command, "all") == 0)
{
help_all (stream);
return;
}
const char *orig_command = command;
c = lookup_cmd (&command, cmdlist, "", NULL, 0, 0);
if (c == 0)
return;
lookup_cmd_composition (orig_command, &alias, &prefix_cmd, &c_cmd);
/* There are three cases here.
If c->subcommands is nonzero, we have a prefix command.
Print its documentation, then list its subcommands.
If c->func is non NULL, we really have a command. Print its
documentation and return.
If c->func is NULL, we have a class name. Print its
documentation (as if it were a command) and then set class to the
number of this class so that the commands in the class will be
listed. */
if (alias == nullptr || !user_documented_alias (*alias))
{
/* Case of a normal command, or an alias not explicitly
documented by the user. */
/* If the user asked 'help somecommand' and there is no alias,
the false indicates to not output the (single) command name. */
fput_command_names_styled (*c, false, "\n", stream);
fput_aliases_definition_styled (*c, stream);
gdb_puts (c->doc, stream);
}
else
{
/* Case of an alias explicitly documented by the user.
Only output the alias definition and its explicit documentation. */
fput_alias_definition_styled (*alias, stream);
fput_command_names_styled (*alias, false, "\n", stream);
gdb_puts (alias->doc, stream);
}
gdb_puts ("\n", stream);
if (!c->is_prefix () && !c->is_command_class_help ())
return;
gdb_printf (stream, "\n");
/* If this is a prefix command, print it's subcommands. */
if (c->is_prefix ())
help_list (*c->subcommands, c->prefixname ().c_str (),
all_commands, stream);
/* If this is a class name, print all of the commands in the class. */
if (c->is_command_class_help ())
help_list (cmdlist, "", c->theclass, stream);
if (c->hook_pre || c->hook_post)
gdb_printf (stream,
"\nThis command has a hook (or hooks) defined:\n");
if (c->hook_pre)
gdb_printf (stream,
"\tThis command is run after : %s (pre hook)\n",
c->hook_pre->name);
if (c->hook_post)
gdb_printf (stream,
"\tThis command is run before : %s (post hook)\n",
c->hook_post->name);
}
/*
* Get a specific kind of help on a command list.
*
* LIST is the list.
* CMDTYPE is the prefix to use in the title string.
* THECLASS is the class with which to list the nodes of this list (see
* documentation for help_cmd_list below), As usual, ALL_COMMANDS for
* everything, ALL_CLASSES for just classes, and non-negative for only things
* in a specific class.
* and STREAM is the output stream on which to print things.
* If you call this routine with a class >= 0, it recurses.
*/
void
help_list (struct cmd_list_element *list, const char *cmdtype,
enum command_class theclass, struct ui_file *stream)
{
int len;
char *cmdtype1, *cmdtype2;
/* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
*/
len = strlen (cmdtype);
cmdtype1 = (char *) alloca (len + 1);
cmdtype1[0] = 0;
cmdtype2 = (char *) alloca (len + 4);
cmdtype2[0] = 0;
if (len)
{
cmdtype1[0] = ' ';
memcpy (cmdtype1 + 1, cmdtype, len - 1);
cmdtype1[len] = 0;
memcpy (cmdtype2, cmdtype, len - 1);
strcpy (cmdtype2 + len - 1, " sub");
}
if (theclass == all_classes)
gdb_printf (stream, "List of classes of %scommands:\n\n", cmdtype2);
else
gdb_printf (stream, "List of %scommands:\n\n", cmdtype2);
help_cmd_list (list, theclass, theclass >= 0, stream);
if (theclass == all_classes)
{
gdb_printf (stream, "\n\
Type \"%p[help%s%p]\" followed by a class name for a list of commands in ",
command_style.style ().ptr (),
cmdtype1,
nullptr);
stream->wrap_here (0);
gdb_printf (stream, "that class.");
gdb_printf (stream, "\n\
Type \"%ps\" for the list of all commands.",
styled_string (command_style.style (), "help all"));
}
gdb_printf (stream, "\nType \"%p[help%s%p]\" followed by %scommand name ",
command_style.style ().ptr (), cmdtype1, nullptr,
cmdtype2);
stream->wrap_here (0);
gdb_puts ("for ", stream);
stream->wrap_here (0);
gdb_puts ("full ", stream);
stream->wrap_here (0);
gdb_puts ("documentation.\n", stream);
gdb_printf (stream,
"Type \"%ps\" to search "
"for commands related to \"word\".\n",
styled_string (command_style.style (), "apropos word"));
gdb_printf (stream, "Type \"%ps\" for full documentation",
styled_string (command_style.style (), "apropos -v word"));
stream->wrap_here (0);
gdb_puts (" of commands related to \"word\".\n", stream);
gdb_puts ("Command name abbreviations are allowed if unambiguous.\n",
stream);
}
static void
help_all (struct ui_file *stream)
{
struct cmd_list_element *c;
int seen_unclassified = 0;
for (c = cmdlist; c; c = c->next)
{
if (c->abbrev_flag)
continue;
/* If this is a class name, print all of the commands in the
class. */
if (c->is_command_class_help ())
{
gdb_printf (stream, "\nCommand class: %s\n\n", c->name);
help_cmd_list (cmdlist, c->theclass, true, stream);
}
}
/* While it's expected that all commands are in some class,
as a safety measure, we'll print commands outside of any
class at the end. */
for (c = cmdlist; c; c = c->next)
{
if (c->abbrev_flag)
continue;
if (c->theclass == no_class)
{
if (!seen_unclassified)
{
gdb_printf (stream, "\nUnclassified commands\n\n");
seen_unclassified = 1;
}
print_help_for_command (*c, true, stream);
}
}
}
/* See cli-decode.h. */
void
print_doc_line (struct ui_file *stream, const char *str,
bool for_value_prefix)
{
static char *line_buffer = 0;
static int line_size;
const char *p;
if (!line_buffer)
{
line_size = 80;
line_buffer = (char *) xmalloc (line_size);
}
/* Searches for the first end of line or the end of STR. */
p = str;
while (*p && *p != '\n')
p++;
if (p - str > line_size - 1)
{
line_size = p - str + 1;
xfree (line_buffer);
line_buffer = (char *) xmalloc (line_size);
}
strncpy (line_buffer, str, p - str);
if (for_value_prefix)
{
if (islower (line_buffer[0]))
line_buffer[0] = toupper (line_buffer[0]);
gdb_assert (p > str);
if (line_buffer[p - str - 1] == '.')
line_buffer[p - str - 1] = '\0';
else
line_buffer[p - str] = '\0';
}
else
line_buffer[p - str] = '\0';
gdb_puts (line_buffer, stream);
}
/* Print one-line help for command C.
If RECURSE is non-zero, also print one-line descriptions
of all prefixed subcommands. */
static void
print_help_for_command (const cmd_list_element &c,
bool recurse, struct ui_file *stream)
{
fput_command_names_styled (c, true, " -- ", stream);
print_doc_line (stream, c.doc, false);
gdb_puts ("\n", stream);
if (!c.default_args.empty ())
fput_alias_definition_styled (c, stream);
fput_aliases_definition_styled (c, stream);
if (recurse
&& c.is_prefix ()
&& c.abbrev_flag == 0)
/* Subcommands of a prefix command typically have 'all_commands'
as class. If we pass CLASS to recursive invocation,
most often we won't see anything. */
help_cmd_list (*c.subcommands, all_commands, true, stream);
}
/*
* Implement a help command on command list LIST.
* RECURSE should be non-zero if this should be done recursively on
* all sublists of LIST.
* STREAM is the stream upon which the output should be written.
* THECLASS should be:
* A non-negative class number to list only commands in that
* ALL_COMMANDS to list all commands in list.
* ALL_CLASSES to list all classes in list.
*
* Note that aliases are only shown when THECLASS is class_alias.
* In the other cases, the aliases will be shown together with their
* aliased command.
*
* Note that RECURSE will be active on *all* sublists, not just the
* ones selected by the criteria above (ie. the selection mechanism
* is at the low level, not the high-level).
*/
static void
help_cmd_list (struct cmd_list_element *list, enum command_class theclass,
bool recurse, struct ui_file *stream)
{
struct cmd_list_element *c;
for (c = list; c; c = c->next)
{
if (c->abbrev_flag == 1 || c->cmd_deprecated)
{
/* Do not show abbreviations or deprecated commands. */
continue;
}
if (c->is_alias () && theclass != class_alias)
{
/* Do not show an alias, unless specifically showing the
list of aliases: for all other classes, an alias is
shown (if needed) together with its aliased command. */
continue;
}
if (theclass == all_commands
|| (theclass == all_classes && c->is_command_class_help ())
|| (theclass == c->theclass && !c->is_command_class_help ()))
{
/* show C when
- showing all commands
- showing all classes and C is a help class
- showing commands of THECLASS and C is not the help class */
/* If we show the class_alias and C is an alias, do not recurse,
as this would show the (possibly very long) not very useful
list of sub-commands of the aliased command. */
print_help_for_command
(*c,
recurse && (theclass != class_alias || !c->is_alias ()),
stream);
continue;
}
if (recurse
&& (theclass == class_user || theclass == class_alias)
&& c->is_prefix ())
{
/* User-defined commands or aliases may be subcommands. */
help_cmd_list (*c->subcommands, theclass, recurse, stream);
continue;
}
/* Do not show C or recurse on C, e.g. because C does not belong to
THECLASS or because C is a help class. */
}
}
/* Search the input clist for 'command'. Return the command if
found (or NULL if not), and return the number of commands
found in nfound. */
static struct cmd_list_element *
find_cmd (const char *command, int len, struct cmd_list_element *clist,
int ignore_help_classes, int *nfound)
{
struct cmd_list_element *found, *c;
found = NULL;
*nfound = 0;
for (c = clist; c; c = c->next)
if (!strncmp (command, c->name, len)
&& (!ignore_help_classes || !c->is_command_class_help ()))
{
found = c;
(*nfound)++;
if (c->name[len] == '\0')
{
*nfound = 1;
break;
}
}
return found;
}
/* Return the length of command name in TEXT. */
int
find_command_name_length (const char *text)
{
const char *p = text;
/* Treating underscores as part of command words is important
so that "set args_foo()" doesn't get interpreted as
"set args _foo()". */
/* Some characters are only used for TUI specific commands.
However, they are always allowed for the sake of consistency.
Note that this is larger than the character set allowed when
creating user-defined commands. */
/* Recognize the single character commands so that, e.g., "!ls"
works as expected. */
if (*p == '!' || *p == '|')
return 1;
while (valid_cmd_char_p (*p)
/* Characters used by TUI specific commands. */
|| *p == '+' || *p == '<' || *p == '>' || *p == '$')
p++;
return p - text;
}
/* See command.h. */
bool
valid_cmd_char_p (int c)
{
/* Alas "42" is a legitimate user-defined command.
In the interests of not breaking anything we preserve that. */
return isalnum (c) || c == '-' || c == '_' || c == '.';
}
/* See command.h. */
bool
valid_user_defined_cmd_name_p (const char *name)
{
const char *p;
if (*name == '\0')
return false;
for (p = name; *p != '\0'; ++p)
{
if (valid_cmd_char_p (*p))
; /* Ok. */
else
return false;
}
return true;
}
/* See command.h. */
struct cmd_list_element *
lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
struct cmd_list_element **result_list, std::string *default_args,
int ignore_help_classes, bool lookup_for_completion_p)
{
char *command;
int len, nfound;
struct cmd_list_element *found, *c;
bool found_alias = false;
const char *line = *text;
while (**text == ' ' || **text == '\t')
(*text)++;
/* Identify the name of the command. */
len = find_command_name_length (*text);
/* If nothing but whitespace, return 0. */
if (len == 0)
return 0;
/* *text and p now bracket the first command word to lookup (and
it's length is len). We copy this into a local temporary. */
command = (char *) alloca (len + 1);
memcpy (command, *text, len);
command[len] = '\0';
/* Look it up. */
found = 0;
nfound = 0;
found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
/* If nothing matches, we have a simple failure. */
if (nfound == 0)
return 0;
if (nfound > 1)
{
if (result_list != nullptr)
/* Will be modified in calling routine
if we know what the prefix command is. */
*result_list = 0;
if (default_args != nullptr)
*default_args = std::string ();
return CMD_LIST_AMBIGUOUS; /* Ambiguous. */
}
/* We've matched something on this list. Move text pointer forward. */
*text += len;
if (found->is_alias ())
{
/* We drop the alias (abbreviation) in favor of the command it
is pointing to. If the alias is deprecated, though, we need to
warn the user about it before we drop it. Note that while we
are warning about the alias, we may also warn about the command
itself and we will adjust the appropriate DEPRECATED_WARN_USER
flags. */
if (found->deprecated_warn_user && !lookup_for_completion_p)
deprecated_cmd_warning (line, clist);
/* Return the default_args of the alias, not the default_args
of the command it is pointing to. */
if (default_args != nullptr)
*default_args = found->default_args;
found = found->alias_target;
found_alias = true;
}
/* If we found a prefix command, keep looking. */
if (found->is_prefix ())
{
c = lookup_cmd_1 (text, *found->subcommands, result_list, default_args,
ignore_help_classes, lookup_for_completion_p);
if (!c)
{
/* Didn't find anything; this is as far as we got. */
if (result_list != nullptr)
*result_list = clist;
if (!found_alias && default_args != nullptr)
*default_args = found->default_args;
return found;
}
else if (c == CMD_LIST_AMBIGUOUS)
{
/* We've gotten this far properly, but the next step is
ambiguous. We need to set the result list to the best
we've found (if an inferior hasn't already set it). */
if (result_list != nullptr)
if (!*result_list)
/* This used to say *result_list = *found->subcommands.
If that was correct, need to modify the documentation
at the top of this function to clarify what is
supposed to be going on. */
*result_list = found;
/* For ambiguous commands, do not return any default_args args. */
if (default_args != nullptr)
*default_args = std::string ();
return c;
}
else
{
/* We matched! */
return c;
}
}
else
{
if (result_list != nullptr)
*result_list = clist;
if (!found_alias && default_args != nullptr)
*default_args = found->default_args;
return found;
}
}
/* All this hair to move the space to the front of cmdtype */
static void
undef_cmd_error (const char *cmdtype, const char *q)
{
error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
cmdtype,
q,
*cmdtype ? " " : "",
(int) strlen (cmdtype) - 1,
cmdtype);
}
/* Look up the contents of *LINE as a command in the command list LIST.
LIST is a chain of struct cmd_list_element's.
If it is found, return the struct cmd_list_element for that command,
update *LINE to point after the command name, at the first argument
and update *DEFAULT_ARGS (if DEFAULT_ARGS is not null) to the default
args to prepend to the user provided args when running the command.
Note that if the found cmd_list_element is found via an alias,
the default args of the alias are returned.
If not found, call error if ALLOW_UNKNOWN is zero
otherwise (or if error returns) return zero.
Call error if specified command is ambiguous,
unless ALLOW_UNKNOWN is negative.
CMDTYPE precedes the word "command" in the error message.
If IGNORE_HELP_CLASSES is nonzero, ignore any command list
elements which are actually help classes rather than commands (i.e.
the function field of the struct cmd_list_element is 0). */
struct cmd_list_element *
lookup_cmd (const char **line, struct cmd_list_element *list,
const char *cmdtype,
std::string *default_args,
int allow_unknown, int ignore_help_classes)
{
struct cmd_list_element *last_list = 0;
struct cmd_list_element *c;
/* Note: Do not remove trailing whitespace here because this
would be wrong for complete_command. Jim Kingdon */
if (!*line)
error (_("Lack of needed %scommand"), cmdtype);
c = lookup_cmd_1 (line, list, &last_list, default_args, ignore_help_classes);
if (!c)
{
if (!allow_unknown)
{
char *q;
int len = find_command_name_length (*line);
q = (char *) alloca (len + 1);
strncpy (q, *line, len);
q[len] = '\0';
undef_cmd_error (cmdtype, q);
}
else
return 0;
}
else if (c == CMD_LIST_AMBIGUOUS)
{
/* Ambigous. Local values should be off subcommands or called
values. */
int local_allow_unknown = (last_list ? last_list->allow_unknown :
allow_unknown);
std::string local_cmdtype
= last_list ? last_list->prefixname () : cmdtype;
struct cmd_list_element *local_list =
(last_list ? *(last_list->subcommands) : list);
if (local_allow_unknown < 0)
{
if (last_list)
return last_list; /* Found something. */
else
return 0; /* Found nothing. */
}
else
{
/* Report as error. */
int amb_len;
char ambbuf[100];
for (amb_len = 0;
((*line)[amb_len] && (*line)[amb_len] != ' '
&& (*line)[amb_len] != '\t');
amb_len++)
;
ambbuf[0] = 0;
for (c = local_list; c; c = c->next)
if (!strncmp (*line, c->name, amb_len))
{
if (strlen (ambbuf) + strlen (c->name) + 6
< (int) sizeof ambbuf)
{
if (strlen (ambbuf))
strcat (ambbuf, ", ");
strcat (ambbuf, c->name);
}
else
{
strcat (ambbuf, "..");
break;
}
}
error (_("Ambiguous %scommand \"%s\": %s."),
local_cmdtype.c_str (), *line, ambbuf);
}
}
else
{
if (c->type == set_cmd && **line != '\0' && !isspace (**line))
error (_("Argument must be preceded by space."));
/* We've got something. It may still not be what the caller
wants (if this command *needs* a subcommand). */
while (**line == ' ' || **line == '\t')
(*line)++;
if (c->is_prefix () && **line && !c->allow_unknown)
undef_cmd_error (c->prefixname ().c_str (), *line);
/* Seems to be what he wants. Return it. */
return c;
}
return 0;
}
/* See command.h. */
struct cmd_list_element *
lookup_cmd_exact (const char *name,
struct cmd_list_element *list,
bool ignore_help_classes)
{
const char *tem = name;
struct cmd_list_element *cmd = lookup_cmd (&tem, list, "", NULL, -1,
ignore_help_classes);
if (cmd != nullptr && strcmp (name, cmd->name) != 0)
cmd = nullptr;
return cmd;
}
/* We are here presumably because an alias or command in TEXT is
deprecated and a warning message should be generated. This
function decodes TEXT and potentially generates a warning message
as outlined below.
Example for 'set endian big' which has a fictitious alias 'seb'.
If alias wasn't used in TEXT, and the command is deprecated:
"warning: 'set endian big' is deprecated."
If alias was used, and only the alias is deprecated:
"warning: 'seb' an alias for the command 'set endian big' is deprecated."
If alias was used and command is deprecated (regardless of whether
the alias itself is deprecated:
"warning: 'set endian big' (seb) is deprecated."
After the message has been sent, clear the appropriate flags in the
command and/or the alias so the user is no longer bothered.
*/
void
deprecated_cmd_warning (const char *text, struct cmd_list_element *list)
{
struct cmd_list_element *alias = nullptr;
struct cmd_list_element *cmd = nullptr;
/* Return if text doesn't evaluate to a command. We place this lookup
within its own scope so that the PREFIX_CMD local is not visible
later in this function. The value returned in PREFIX_CMD is based on
the prefix found in TEXT, and is our case this prefix can be missing
in some situations (when LIST is not the global CMDLIST).
It is better for our purposes to use the prefix commands directly from
the ALIAS and CMD results. */
{
struct cmd_list_element *prefix_cmd = nullptr;
if (!lookup_cmd_composition_1 (text, &alias, &prefix_cmd, &cmd, list))
return;
}
/* Return if nothing is deprecated. */
if (!((alias != nullptr ? alias->deprecated_warn_user : 0)
|| cmd->deprecated_warn_user))
return;
/* Join command prefix (if any) and the command name. */
std::string tmp_cmd_str;
if (cmd->prefix != nullptr)
tmp_cmd_str += cmd->prefix->prefixname ();
tmp_cmd_str += std::string (cmd->name);
/* Display the appropriate first line, this warns that the thing the user
entered is deprecated. */
if (alias != nullptr)
{
/* Join the alias prefix (if any) and the alias name. */
std::string tmp_alias_str;
if (alias->prefix != nullptr)
tmp_alias_str += alias->prefix->prefixname ();
tmp_alias_str += std::string (alias->name);
if (cmd->cmd_deprecated)
gdb_printf (_("Warning: command '%ps' (%ps) is deprecated.\n"),
styled_string (command_style.style (),
tmp_cmd_str.c_str ()),
styled_string (command_style.style (),
tmp_alias_str.c_str ()));
else
gdb_printf (_("Warning: '%ps', an alias for the command '%ps', "
"is deprecated.\n"),
styled_string (command_style.style (),
tmp_alias_str.c_str ()),
styled_string (command_style.style (),
tmp_cmd_str.c_str ()));
}
else
gdb_printf (_("Warning: command '%ps' is deprecated.\n"),
styled_string (command_style.style (),
tmp_cmd_str.c_str ()));
/* Now display a second line indicating what the user should use instead.
If it is only the alias that is deprecated, we want to indicate the
new alias, otherwise we'll indicate the new command. */
const char *replacement;
if (alias != nullptr && !cmd->cmd_deprecated)
replacement = alias->replacement;
else
replacement = cmd->replacement;
if (replacement != nullptr)
gdb_printf (_("Use '%ps'.\n\n"),
styled_string (command_style.style (),
replacement));
else
gdb_printf (_("No alternative known.\n\n"));
/* We've warned you, now we'll keep quiet. */
if (alias != nullptr)
alias->deprecated_warn_user = 0;
cmd->deprecated_warn_user = 0;
}
/* Look up the contents of TEXT as a command in the command list CUR_LIST.
Return 1 on success, 0 on failure.
If TEXT refers to an alias, *ALIAS will point to that alias.
If TEXT is a subcommand (i.e. one that is preceded by a prefix
command) set *PREFIX_CMD.
Set *CMD to point to the command TEXT indicates, or to
CMD_LIST_AMBIGUOUS if there are multiple possible matches.
If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
exist, they are NULL when we return.
*/
static int
lookup_cmd_composition_1 (const char *text,
struct cmd_list_element **alias,
struct cmd_list_element **prefix_cmd,
struct cmd_list_element **cmd,
struct cmd_list_element *cur_list)
{
*alias = nullptr;
*prefix_cmd = cur_list->prefix;
*cmd = nullptr;
text = skip_spaces (text);
/* Go through as many command lists as we need to, to find the command
TEXT refers to. */
while (1)
{
/* Identify the name of the command. */
int len = find_command_name_length (text);
/* If nothing but whitespace, return. */
if (len == 0)
return 0;
/* TEXT is the start of the first command word to lookup (and
it's length is LEN). We copy this into a local temporary. */
std::string command (text, len);
/* Look it up. */
int nfound = 0;
*cmd = find_cmd (command.c_str (), len, cur_list, 1, &nfound);
/* We only handle the case where a single command was found. */
if (nfound > 1)
{
*cmd = CMD_LIST_AMBIGUOUS;
return 0;
}
else if (*cmd == nullptr)
return 0;
else
{
if ((*cmd)->is_alias ())
{
/* If the command was actually an alias, we note that an
alias was used (by assigning *ALIAS) and we set *CMD. */
*alias = *cmd;
*cmd = (*cmd)->alias_target;
}
}
text += len;
text = skip_spaces (text);
if ((*cmd)->is_prefix () && *text != '\0')
{
cur_list = *(*cmd)->subcommands;
*prefix_cmd = *cmd;
}
else
return 1;
}
}
/* Look up the contents of TEXT as a command in the command list 'cmdlist'.
Return 1 on success, 0 on failure.
If TEXT refers to an alias, *ALIAS will point to that alias.
If TEXT is a subcommand (i.e. one that is preceded by a prefix
command) set *PREFIX_CMD.
Set *CMD to point to the command TEXT indicates, or to
CMD_LIST_AMBIGUOUS if there are multiple possible matches.
If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
exist, they are NULL when we return.
*/
int
lookup_cmd_composition (const char *text,
struct cmd_list_element **alias,
struct cmd_list_element **prefix_cmd,
struct cmd_list_element **cmd)
{
return lookup_cmd_composition_1 (text, alias, prefix_cmd, cmd, cmdlist);
}
/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
/* Return a vector of char pointers which point to the different
possible completions in LIST of TEXT.
WORD points in the same buffer as TEXT, and completions should be
returned relative to this position. For example, suppose TEXT is
"foo" and we want to complete to "foobar". If WORD is "oo", return
"oobar"; if WORD is "baz/foo", return "baz/foobar". */
void
complete_on_cmdlist (struct cmd_list_element *list,
completion_tracker &tracker,
const char *text, const char *word,
int ignore_help_classes)
{
struct cmd_list_element *ptr;
int textlen = strlen (text);
int pass;
int saw_deprecated_match = 0;
/* We do one or two passes. In the first pass, we skip deprecated
commands. If we see no matching commands in the first pass, and
if we did happen to see a matching deprecated command, we do
another loop to collect those. */
for (pass = 0; pass < 2; ++pass)
{
bool got_matches = false;
for (ptr = list; ptr; ptr = ptr->next)
if (!strncmp (ptr->name, text, textlen)
&& !ptr->abbrev_flag
&& (!ignore_help_classes || !ptr->is_command_class_help ()
|| ptr->is_prefix ()))
{
if (pass == 0)
{
if (ptr->cmd_deprecated)
{
saw_deprecated_match = 1;
continue;
}
}
tracker.add_completion
(make_completion_match_str (ptr->name, text, word));
got_matches = true;
}
if (got_matches)
break;
/* If we saw no matching deprecated commands in the first pass,
just bail out. */
if (!saw_deprecated_match)
break;
}
}
/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
/* Add the different possible completions in ENUMLIST of TEXT.
WORD points in the same buffer as TEXT, and completions should be
returned relative to this position. For example, suppose TEXT is "foo"
and we want to complete to "foobar". If WORD is "oo", return
"oobar"; if WORD is "baz/foo", return "baz/foobar". */
void
complete_on_enum (completion_tracker &tracker,
const char *const *enumlist,
const char *text, const char *word)
{
int textlen = strlen (text);
int i;
const char *name;
for (i = 0; (name = enumlist[i]) != NULL; i++)
if (strncmp (name, text, textlen) == 0)
tracker.add_completion (make_completion_match_str (name, text, word));
}
/* Call the command function. */
void
cmd_func (struct cmd_list_element *cmd, const char *args, int from_tty)
{
if (!cmd->is_command_class_help ())
{
std::optional<scoped_restore_tmpl<bool>> restore_suppress;
if (cmd->suppress_notification != NULL)
restore_suppress.emplace (cmd->suppress_notification, true);
cmd->func (args, from_tty, cmd);
}
else
error (_("Invalid command"));
}
int
cli_user_command_p (struct cmd_list_element *cmd)
{
return cmd->theclass == class_user && cmd->func == do_simple_func;
}
|