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
|
/*
Copyright (C) 1996-1997 Id Software, 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 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
$Id: cmd.c,v 1.92 2007-10-28 02:45:19 qqshka Exp $
*/
#ifndef _WIN32
#include <strings.h>
#endif
#include "quakedef.h"
#ifdef WITH_TCL
#include "embed_tcl.h"
#endif
#include "gl_model.h"
#include "gl_local.h"
#include "teamplay.h"
#include "rulesets.h"
#include "tp_triggers.h"
#include "parser.h"
#include "utils.h"
#include "keys.h"
qbool CL_CheckServerCommand (void);
static void Cmd_ExecuteStringEx (cbuf_t *context, char *text);
static int gtf = 0; // global trigger flag
cvar_t cl_warncmd = {"cl_warncmd", "1"};
cvar_t cl_warnexec = {"cl_warnexec", "1"};
cvar_t cl_curlybraces = {"cl_curlybraces", "0"};
cbuf_t cbuf_main;
cbuf_t cbuf_svc;
cbuf_t cbuf_safe, cbuf_formatted_comms;
char *hud262_load_buff = NULL;
cbuf_t *cbuf_current = NULL;
//=============================================================================
//Causes execution of the remainder of the command buffer to be delayed until next frame.
//This allows commands like: bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
void Cmd_Wait_f (void)
{
#ifdef WITH_TCL
if (in_tcl) {
Com_Printf ("command wait cant be used with TCL\n");
return;
}
#endif
if (cbuf_current)
cbuf_current->wait = true;
return;
}
void Hud262_CatchStringsOnLoad(char *line)
{
char *tmpbuff;
if (Utils_RegExpMatch("^((\\s+)?(?i)hud262_(add|alpha|bg|blink|disable|enable|position|width))", line))
{
if (hud262_load_buff == NULL)
{
hud262_load_buff = (char*) Q_malloc( (strlen(line) + 2) * sizeof(char));
snprintf(hud262_load_buff, strlen(line) + 2, "%s\n", line);
}
else
{
tmpbuff = (char *) Q_malloc(strlen(hud262_load_buff) + 1);
strcpy(tmpbuff, hud262_load_buff);
hud262_load_buff = (char *) Q_realloc(hud262_load_buff, (strlen(tmpbuff) + strlen(line) + 2) * sizeof(char));
snprintf(hud262_load_buff, strlen(tmpbuff) + strlen(line) + 2, "%s%s\n", tmpbuff, line);
Q_free(tmpbuff);
}
}
}
/*
=============================================================================
COMMAND BUFFER
=============================================================================
*/
void Cbuf_AddText (const char *text)
{
Cbuf_AddTextEx (&cbuf_main, text);
}
void Cbuf_InsertText (const char *text)
{
Cbuf_InsertTextEx (&cbuf_main, text);
}
void Cbuf_Execute (void)
{
Cbuf_ExecuteEx (&cbuf_main);
Cbuf_ExecuteEx (&cbuf_safe);
Cbuf_ExecuteEx (&cbuf_formatted_comms);
}
//fuh : ideally we should have 'cbuf_t *Cbuf_Register(int maxsize, int flags, qbool (*blockcmd)(void))
//fuh : so that cbuf_svc and cbuf_safe can be registered outside cmd.c in cl_* .c
//fuh : flags can be used to deal with newline termination etc for cbuf_svc, and *blockcmd can be used for blocking cmd's for cbuf_svc
//fuh : this way cmd.c would be independant of '#ifdef CLIENTONLY's'.
//fuh : I'll take care of that one day.
static void Cbuf_Register (cbuf_t *cbuf, int maxsize)
{
assert (!host_initialized);
cbuf->maxsize = maxsize;
cbuf->text_buf = (char *) Hunk_Alloc(maxsize);
cbuf->text_start = cbuf->text_end = (cbuf->maxsize >> 1);
cbuf->wait = false;
}
void Cbuf_Init (void)
{
Cbuf_Register (&cbuf_main, 1 << 18); // 256kb
Cbuf_Register (&cbuf_svc, 1 << 13); // 8kb
Cbuf_Register (&cbuf_safe, 1 << 11); // 2kb
Cbuf_Register (&cbuf_formatted_comms, 1 << 11); // 2kb
}
//Adds command text at the end of the buffer
void Cbuf_AddTextEx (cbuf_t *cbuf, const char *text)
{
int new_start, new_bufsize;
size_t len;
len = strlen (text);
if (cbuf->text_end + len <= cbuf->maxsize) {
memcpy (cbuf->text_buf + cbuf->text_end, text, len);
cbuf->text_end += len;
return;
}
new_bufsize = cbuf->text_end-cbuf->text_start+len;
if (new_bufsize > cbuf->maxsize) {
Com_Printf ("Cbuf_AddText: overflow\n");
return;
}
// Calculate optimal position of text in buffer
new_start = ((cbuf->maxsize - new_bufsize) >> 1);
memcpy (cbuf->text_buf + new_start, cbuf->text_buf + cbuf->text_start, cbuf->text_end-cbuf->text_start);
memcpy (cbuf->text_buf + new_start + cbuf->text_end-cbuf->text_start, text, len);
cbuf->text_start = new_start;
cbuf->text_end = cbuf->text_start + new_bufsize;
}
//Adds command text at the beginning of the buffer
void Cbuf_InsertTextEx (cbuf_t *cbuf, const char *text)
{
int new_start, new_bufsize;
size_t len;
len = strlen (text);
if (len <= cbuf->text_start) {
memcpy (cbuf->text_buf + (cbuf->text_start - len), text, len);
cbuf->text_start -= len;
return;
}
new_bufsize = cbuf->text_end - cbuf->text_start + len;
if (new_bufsize > cbuf->maxsize) {
Com_Printf ("Cbuf_InsertText: overflow\n");
return;
}
// Calculate optimal position of text in buffer
new_start = ((cbuf->maxsize - new_bufsize) >> 1);
memmove (cbuf->text_buf + (new_start + len), cbuf->text_buf + cbuf->text_start, cbuf->text_end - cbuf->text_start);
memcpy (cbuf->text_buf + new_start, text, len);
cbuf->text_start = new_start;
cbuf->text_end = cbuf->text_start + new_bufsize;
}
#define MAX_RUNAWAYLOOP 1000
void Cbuf_ExecuteEx (cbuf_t *cbuf)
{
int i, j, cursize, nextsize;
char *text, line[1024], *src, *dest;
qbool comment;
int quotes;
if (cbuf == &cbuf_safe)
gtf++;
nextsize = cbuf->text_end - cbuf->text_start;
while (cbuf->text_end > cbuf->text_start)
{
// find a \n or ; line break
text = (char *) cbuf->text_buf + cbuf->text_start;
cursize = cbuf->text_end - cbuf->text_start;
comment = false;
quotes = 0;
for (i = 0; i < cursize; i++)
{
if (cl_curlybraces.integer)
{
if (text[i] == '\\')
{
if (i + 1 < cursize && text[i+1] == '\n')
{ // escaped endline
text[i] = text[i+1] = '\r'; // '\r' removed later during copying
i++;
continue;
}
else if (i + 2 < cursize && text[i+1] == '\r' && text[i+2] == '\n')
{ // escaped dos endline
text[i] = text[i+2] = '\r';
i+=2;
continue;
}
}
}
if (text[i] == '\n')
break;
if (text[i] == '"' && quotes <= 0)
{
if (!quotes)
quotes = -1;
else
quotes = 0;
}
else if (quotes >= 0)
{
if (cl_curlybraces.integer)
{
if (text[i] == '{')
quotes++;
else if (text[i] == '}')
quotes--;
}
}
if (comment || quotes)
continue;
if (text[i] == '/' && i + 1 < cursize && text[i + 1] == '/')
comment = true;
else if (text[i] == ';' && !quotes)
break;
}
if ((cursize - i) < nextsize) // have we reached the next command?
nextsize = cursize - i;
// don't execute lines without ending \n; this fixes problems with
// partially stuffed aliases not being executed properly
if (cbuf_current == &cbuf_svc && i == cursize)
break;
// Copy text to line, skipping carriage return chars
src = text;
dest = line;
j = min (i, sizeof (line) - 1);
for ( ; j; j--, src++)
{
if (*src != '\r')
*dest++ = *src;
}
*dest = 0;
// delete the text from the command buffer and move remaining commands down This is necessary
// because commands (exec, alias) can insert data at the beginning of the text buffer
if (i == cursize)
{
cbuf->text_start = cbuf->text_end = (cbuf->maxsize >> 1);
}
else
{
i++;
cbuf->text_start += i;
}
cursize = cbuf->text_end - cbuf->text_start;
// TODO: make it in a more right way
// since, hud262_add can not correctly create hud elements during normal start
// (some cvars are not created/initialized at the time when we want to use them in hud262)
// we should save these commands to buffer and execute it when all
// cvars will be created
if(!host_initialized)
Hud262_CatchStringsOnLoad(line);
Cmd_ExecuteStringEx (cbuf, line); // execute the command line
if (cbuf->text_end - cbuf->text_start > cursize)
cbuf->runAwayLoop++;
if (cbuf->runAwayLoop > MAX_RUNAWAYLOOP)
{
Com_Printf("\x02" "A recursive alias has caused an infinite loop.");
Com_Printf("\x02" " Clearing execution buffer to prevent lockup.\n");
cbuf->text_start = cbuf->text_end = (cbuf->maxsize >> 1);
cbuf->runAwayLoop = 0;
}
if (cbuf->wait)
{
// skip out while text still remains in buffer, leaving it for next frame
cbuf->wait = false;
cbuf->runAwayLoop += Q_rint (0.5 * cls.frametime * MAX_RUNAWAYLOOP);
if (cbuf == &cbuf_safe)
gtf--;
return;
}
}
if (cbuf == &cbuf_safe)
gtf--;
cbuf->runAwayLoop = 0;
return;
}
/*
==============================================================================
SCRIPT COMMANDS
==============================================================================
*/
/*
Set commands are added early, so they are guaranteed to be set before
the client and server initialize for the first time.
Other commands are added late, after all initialization is complete.
*/
void Cbuf_AddEarlyCommands (void)
{
int i;
for (i = 0; i < COM_Argc () - 2; i++) {
if (strcasecmp (COM_Argv(i), "+set"))
continue;
Cbuf_AddText (va ("set %s %s\n", COM_Argv (i + 1), COM_Argv (i + 2)));
i += 2;
}
}
qbool Cmd_IsAllowedStuffCmdsCommand(const char *str)
{
char* banned_list[] = {"set ", "cfg_load ", NULL};
char** banned_cmd = banned_list;
while(*banned_cmd)
{
if(strncasecmp(str, *banned_cmd, strlen(*banned_cmd)) == 0)
{
//+set is processed in Cbuf_AddEarlyCommands(), +cfg_load allowed only once in Host_Init()
if((strncasecmp(str, "set ", 4) != 0) && (strncasecmp(str, "cfg_load ", 9) != 0))
{
Com_Printf("+%s is not allowed in cmdline or obsolete.\n", *banned_cmd);
}
return false;
}
banned_cmd++;
}
return true;
}
/*
Adds command line parameters as script statements
Commands lead with a +, and continue until a - or another +
quake +prog jctest.qp +cmd amlev1
quake -nosound +cmd amlev1
*/
void Cmd_StuffCmds_f (void)
{
int k, len = 0;
char *s, *text, *token;
// build the combined string to parse from
for (k = 1; k < COM_Argc(); k++)
len += strlen (COM_Argv(k)) + 1;
if (!len)
return;
text = (char *) Q_malloc(len + 1);
text[0] = '\0';
for (k = 1; k < COM_Argc(); k++) {
strlcat (text, COM_Argv(k), len + 1);
if (k != COM_Argc() - 1)
strlcat (text, " ", len + 1);
}
// pull out the commands
token = (char *) Q_malloc(len + 1);
s = text;
while (*s) {
if (*s == '+') {
k = 0;
for (s = s + 1; s[0] && (s[0] != ' ' || (s[1] != '-' && s[1] != '+')); s++)
token[k++] = s[0];
token[k++] = '\n';
token[k] = 0;
if(Cmd_IsAllowedStuffCmdsCommand(token))
Cbuf_AddText (token);
} else if (*s == '-') {
for (s = s + 1; s[0] && s[0] != ' '; s++)
;
} else {
s++;
}
}
Q_free(text);
Q_free (token);
}
void Cmd_Exec_f (void)
{
char *f, name[MAX_OSPATH];
int mark;
if (Cmd_Argc () != 2) {
Com_Printf ("exec <filename> : execute a script file\n");
return;
}
strlcpy (name, Cmd_Argv(1), sizeof(name) - 4);
mark = Hunk_LowMark();
if (!(f = (char *) FS_LoadHunkFile (name, NULL))) {
const char *p;
p = COM_SkipPath (name);
if (!strchr (p, '.')) {
// no extension, so try the default (.cfg)
strlcat (name, ".cfg", sizeof (name));
f = (char *) FS_LoadHunkFile (name, NULL);
}
if (!f) {
Com_Printf ("couldn't exec %s\n", Cmd_Argv(1));
return;
}
}
if (cl_warnexec.integer || developer.integer) {
Com_Printf("execing %s/%s\n", FS_Locate_GetPath(name), name);
}
if (cbuf_current == &cbuf_svc) {
Cbuf_AddTextEx (&cbuf_main, f);
Cbuf_AddTextEx (&cbuf_main, "\n");
} else
{
Cbuf_InsertText ("\n");
Cbuf_InsertText (f);
}
Hunk_FreeToLowMark (mark);
}
//Just prints the rest of the line to the console
/*void Cmd_Echo_f (void) {
int i;
for (i = 1; i < Cmd_Argc(); i++)
Com_Printf ("%s ", Cmd_Argv(i));
Com_Printf ("\n");
}*/
void Cmd_Echo_f (void)
{
int i;
char *str;
char args[MAX_MACRO_STRING];
char buf[MAX_MACRO_STRING];
memset (args, 0, MAX_MACRO_STRING);
snprintf (args, MAX_MACRO_STRING, "%s", Cmd_Argv(1));
for (i = 2; i < Cmd_Argc(); i++) {
strlcat (args, " ", MAX_MACRO_STRING);
strlcat (args, Cmd_Argv(i), MAX_MACRO_STRING);
}
// str = TP_ParseMacroString(args);
str = TP_ParseMacroString(args);
str = TP_ParseFunChars(str, false);
strlcpy (buf, str, MAX_MACRO_STRING);
CL_SearchForReTriggers (buf, RE_PRINT_ECHO); // BorisU
Print_flags[Print_current] |= PR_TR_SKIP;
Com_Printf ("%s\n", buf);
}
/*
=============================================================================
ALIASES
=============================================================================
*/
#define ALIAS_HASHPOOL_SIZE 256
cmd_alias_t *cmd_alias_hash[ALIAS_HASHPOOL_SIZE];
cmd_alias_t *cmd_alias;
cmd_alias_t *Cmd_FindAlias (const char *name)
{
int key;
cmd_alias_t *alias;
key = Com_HashKey (name) % ALIAS_HASHPOOL_SIZE;
for (alias = cmd_alias_hash[key]; alias; alias = alias->hash_next) {
if (!strcasecmp(name, alias->name))
return alias;
}
return NULL;
}
char *Cmd_AliasString (char *name)
{
int key;
cmd_alias_t *alias;
key = Com_HashKey (name) % ALIAS_HASHPOOL_SIZE;
for (alias = cmd_alias_hash[key]; alias; alias = alias->hash_next) {
if (!strcasecmp(name, alias->name))
#ifdef WITH_TCL
if (!(alias->flags & ALIAS_TCL))
#endif
return alias->value;
}
return NULL;
}
void Cmd_Viewalias_f (void)
{
cmd_alias_t *alias;
char *name;
int i,m;
if (Cmd_Argc() < 2) {
Com_Printf ("viewalias <cvar> [<cvar2>..] : view body of alias\n");
return;
}
for (i=1; i<Cmd_Argc(); i++) {
name = Cmd_Argv(i);
if ( IsRegexp(name) ) {
if (!ReSearchInit(name))
return;
Com_Printf ("Current alias commands:\n");
for (alias = cmd_alias, i=m=0; alias ; alias=alias->next, i++)
if (ReSearchMatch(alias->name)) {
#ifdef WITH_TCL
if (alias->flags & ALIAS_TCL)
Com_Printf ("%s : Tcl procedure\n", alias->name);
else
#endif
Com_Printf ("%s : %s\n", alias->name, alias->value);
m++;
}
Com_Printf ("------------\n%i/%i aliases\n", m, i);
ReSearchDone();
} else {
if ((alias = Cmd_FindAlias(name)))
#ifdef WITH_TCL
if (alias->flags & ALIAS_TCL)
Com_Printf ("%s : Tcl procedure\n", name);
else
#endif
Com_Printf ("%s : \"%s\"\n", Cmd_Argv(i), alias->value);
else
Com_Printf ("No such alias: %s\n", Cmd_Argv(i));
}
}
}
int Cmd_AliasCompare (const void *p1, const void *p2)
{
cmd_alias_t *a1, *a2;
a1 = *((cmd_alias_t **) p1);
a2 = *((cmd_alias_t **) p2);
if (a1->name[0] == '+') {
if (a2->name[0] == '+')
return strcasecmp(a1->name + 1, a2->name + 1);
else
return -1;
} else if (a1->name[0] == '-') {
if (a2->name[0] == '+')
return 1;
else if (a2->name[0] == '-')
return strcasecmp(a1->name + 1, a2->name + 1);
else
return -1;
} else if (a2->name[0] == '+' || a2->name[0] == '-') {
return 1;
} else {
return strcasecmp(a1->name, a2->name);
}
}
void Cmd_AliasList_f (void)
{
cmd_alias_t *a;
int i, c, m = 0;
static int count;
static cmd_alias_t *sorted_aliases[4096];
#define MAX_SORTED_ALIASES (sizeof(sorted_aliases) / sizeof(sorted_aliases[0]))
for (a = cmd_alias, count = 0; a && count < MAX_SORTED_ALIASES; a = a->next, count++)
sorted_aliases[count] = a;
qsort(sorted_aliases, count, sizeof (cmd_alias_t *), Cmd_AliasCompare);
if (count == MAX_SORTED_ALIASES)
assert(!"count == MAX_SORTED_ALIASES");
c = Cmd_Argc();
if (c>1)
if (!ReSearchInit(Cmd_Argv(1)))
return;
Com_Printf ("List of aliases:\n");
for (i = 0; i < count; i++) {
a = sorted_aliases[i];
if (c==1 || ReSearchMatch(a->name)) {
Com_Printf ("\x02%s :", sorted_aliases[i]->name);
Com_Printf (" %s\n", sorted_aliases[i]->value);
m++;
}
}
if (c>1)
ReSearchDone();
Com_Printf ("------------\n%i/%i aliases\n", m, count);
}
void Cmd_EditAlias_f (void)
{
cmd_alias_t *a;
char *s, final_string[MAXCMDLINE - 1];
int c;
c = Cmd_Argc();
if (c == 1) {
Com_Printf ("%s <name> : modify an alias\n", Cmd_Argv(0));
Com_Printf ("aliaslist : list all aliases\n");
return;
}
a = Cmd_FindAlias(Cmd_Argv(1));
if ( a == NULL ) {
s = Q_strdup("");
} else {
s = Q_strdup(a->value);
}
snprintf(final_string, sizeof(final_string), "/alias \"%s\" \"%s\"", Cmd_Argv(1), s);
Key_ClearTyping();
memcpy (key_lines[edit_line]+1, str2wcs(final_string), strlen(final_string)*sizeof(wchar));
Q_free(s);
}
static cmd_alias_t* Cmd_AliasCreate (char* name)
{
cmd_alias_t *a;
int key;
key = Com_HashKey(name) % ALIAS_HASHPOOL_SIZE;
a = (cmd_alias_t *) Q_malloc(sizeof(cmd_alias_t));
a->next = cmd_alias;
cmd_alias = a;
a->hash_next = cmd_alias_hash[key];
cmd_alias_hash[key] = a;
strlcpy (a->name, name, sizeof (a->name));
return a;
}
//Creates a new command that executes a command string (possibly ; separated)
void Cmd_Alias_f (void)
{
cmd_alias_t *a;
char *s;
int c, key;
c = Cmd_Argc();
if (c == 1) {
Com_Printf ("%s <name> <command> : create or modify an alias\n", Cmd_Argv(0));
Com_Printf ("aliaslist : list all aliases\n");
return;
}
s = Cmd_Argv(1);
if (strlen(s) >= MAX_ALIAS_NAME) {
Com_Printf ("Alias name is too long\n");
return;
}
key = Com_HashKey(s) % ALIAS_HASHPOOL_SIZE;
// if the alias already exists, reuse it
for (a = cmd_alias_hash[key]; a; a = a->hash_next) {
if (!strcasecmp(a->name, s)) {
Q_free(a->value);
break;
}
}
if (!a) {
a = (cmd_alias_t *) Q_malloc(sizeof(cmd_alias_t));
a->next = cmd_alias;
cmd_alias = a;
a->hash_next = cmd_alias_hash[key];
cmd_alias_hash[key] = a;
}
strlcpy (a->name, s, sizeof (a->name));
a->flags = 0;
// QW262 -->
s=Cmd_MakeArgs(2);
while (*s) {
if (*s == '%' && ( s[1]>='0' || s[1]<='9')) {
a->flags |= ALIAS_HAS_PARAMETERS;
break;
}
++s;
}
// <-- QW262
if (cbuf_current == &cbuf_svc)
a->flags |= ALIAS_SERVER;
if (!strcasecmp(Cmd_Argv(0), "tempalias"))
a->flags |= ALIAS_TEMP;
// copy the rest of the command line
a->value = Q_strdup(Cmd_MakeArgs(2));
}
qbool Cmd_DeleteAlias (char *name)
{
cmd_alias_t *a, *prev;
int key;
key = Com_HashKey (name) % ALIAS_HASHPOOL_SIZE;
prev = NULL;
for (a = cmd_alias_hash[key]; a; a = a->hash_next) {
if (!strcasecmp(a->name, name)) {
// unlink from hash
if (prev)
prev->hash_next = a->hash_next;
else
cmd_alias_hash[key] = a->hash_next;
break;
}
prev = a;
}
if (!a)
return false; // not found
prev = NULL;
for (a = cmd_alias; a; a = a->next) {
if (!strcasecmp(a->name, name)) {
// unlink from alias list
if (prev)
prev->next = a->next;
else
cmd_alias = a->next;
// free
Q_free(a->value);
Q_free(a);
return true;
}
prev = a;
}
assert(!"Cmd_DeleteAlias: alias list broken");
return false; // shut up compiler
}
void Cmd_UnAlias (qbool use_regex)
{
int i;
char *name;
cmd_alias_t *a, *next;
qbool re_search = false;
if (Cmd_Argc() < 2) {
Com_Printf ("unalias <cvar> [<cvar2>..]: erase an existing alias\n");
return;
}
for (i=1; i<Cmd_Argc(); i++) {
name = Cmd_Argv(i);
if (use_regex && (re_search = IsRegexp(name)))
if(!ReSearchInit(name))
continue;
if (strlen(name) >= MAX_ALIAS_NAME) {
Com_Printf ("Alias name is too long: \"%s\"\n", Cmd_Argv(i));
continue;
}
if (use_regex && re_search) {
for (a = cmd_alias; a; a = next) {
next = a->next;
if (ReSearchMatch(a->name))
Cmd_DeleteAlias(a->name);
}
} else {
if (!Cmd_DeleteAlias(Cmd_Argv(i)))
Com_Printf ("unalias: unknown alias \"%s\"\n", Cmd_Argv(i));
}
if (use_regex && re_search)
ReSearchDone();
}
}
void Cmd_UnAlias_f (void)
{
Cmd_UnAlias(false);
}
void Cmd_UnAlias_re_f (void)
{
Cmd_UnAlias(true);
}
/*
* Remove all aliases unless connected, then remove
* all aliases except the server created aliases
*/
void Cmd_UnAliasAll_f (void)
{
cmd_alias_t *a, *next;
/* FIXME: Optimize this, its n^2 slow atm since Cmd_DeleteAlias will loop through
* the list again for each entry
*/
if (cls.state >= ca_connected) {
Com_Printf("Connected to a server, will not remove server aliases\n");
for (a = cmd_alias; a; a = next) {
next = a->next;
if ((a->flags & ALIAS_SERVER) == 0) {
Cmd_DeleteAlias(a->name);
}
}
} else {
for (a = cmd_alias; a ; a = next) {
next = a->next;
Q_free(a->value);
Q_free(a);
}
cmd_alias = NULL;
// clear hash
memset (cmd_alias_hash, 0, sizeof(cmd_alias_t*) * ALIAS_HASHPOOL_SIZE);
}
}
void DeleteServerAliases(void)
{
cmd_alias_t *a, *next;
for (a = cmd_alias; a; a = next) {
next = a->next;
if (a->flags & ALIAS_SERVER)
Cmd_DeleteAlias (a->name);
}
}
/*
=============================================================================
LEGACY COMMANDS
=============================================================================
*/
typedef struct legacycmd_s
{
char *oldname, *newname;
struct legacycmd_s *next;
} legacycmd_t;
static legacycmd_t *legacycmds = NULL;
void Cmd_AddLegacyCommand (char *oldname, char *newname)
{
legacycmd_t *cmd;
cmd = (legacycmd_t *) Q_malloc(sizeof(legacycmd_t));
cmd->next = legacycmds;
legacycmds = cmd;
cmd->oldname = oldname;
cmd->newname = newname;
}
qbool Cmd_IsLegacyCommand (char *oldname)
{
legacycmd_t *cmd;
for (cmd = legacycmds; cmd; cmd = cmd->next) {
if (!strcasecmp(cmd->oldname, oldname))
return true;
}
return false;
}
static qbool Cmd_LegacyCommand (void)
{
qbool recursive = false;
legacycmd_t *cmd;
char text[1024];
for (cmd = legacycmds; cmd; cmd = cmd->next) {
if (!strcasecmp(cmd->oldname, Cmd_Argv(0)))
break;
}
if (!cmd)
return false;
if (!cmd->newname[0])
return true; // just ignore this command
// build new command string
strlcpy (text, cmd->newname, sizeof(text));
strlcat (text, " ", sizeof(text));
strlcat (text, Cmd_Args(), sizeof(text));
assert (!recursive);
recursive = true;
Cmd_ExecuteString (text);
recursive = false;
return true;
}
/*
=============================================================================
COMMAND EXECUTION
=============================================================================
*/
#define CMD_HASHPOOL_SIZE 512
cmd_function_t *cmd_hash_array[CMD_HASHPOOL_SIZE];
/*static*/ cmd_function_t *cmd_functions; // possible commands to execute
static tokenizecontext_t cmd_tokenizecontext;
static char *cmd_null_string = "";
int Cmd_ArgcEx (tokenizecontext_t *ctx)
{
return ctx->cmd_argc;
}
char *Cmd_ArgvEx (tokenizecontext_t *ctx, int arg)
{
if (arg >= ctx->cmd_argc || arg < 0)
return cmd_null_string;
return ctx->cmd_argv[arg];
}
// Returns a single string containing argv(1) to argv(argc() - 1)
char *Cmd_ArgsEx (tokenizecontext_t *ctx)
{
return ctx->cmd_args;
}
// Returns a single string containing argv(start) to argv(argc() - 1)
// Unlike Cmd_Args, shrinks spaces between argvs
char *Cmd_MakeArgsEx (tokenizecontext_t *ctx, int start)
{
int i, c;
ctx->text[0] = 0;
c = Cmd_ArgcEx(ctx);
for (i = start; i < c; i++)
{
if (i > start)
strlcat (ctx->text, " ", sizeof (ctx->text) - strlen (ctx->text));
strlcat (ctx->text, Cmd_ArgvEx(ctx, i), sizeof (ctx->text) - strlen (ctx->text));
}
return ctx->text;
}
// Parses the given string into command line tokens.
void Cmd_TokenizeStringEx (tokenizecontext_t *ctx, char *text)
{
int idx = 0, token_len;
memset(ctx, 0, sizeof(*ctx));
while (1)
{
// skip whitespace
while (*text == ' ' || *text == '\t' || *text == '\r')
text++;
// a newline separates commands in the buffer
if (*text == '\n')
return;
if (!*text)
return;
if (ctx->cmd_argc == 1)
strlcpy(ctx->cmd_args, text, sizeof(ctx->cmd_args));
text = COM_Parse (text);
if (!text)
return;
if (ctx->cmd_argc >= MAX_ARGS)
return;
token_len = strlen(com_token);
// ouch ouch, no more space
if (idx + token_len + 1 > sizeof(ctx->argv_buf))
return;
ctx->cmd_argv[ctx->cmd_argc] = ctx->argv_buf + idx;
strcpy (ctx->cmd_argv[ctx->cmd_argc], com_token);
ctx->cmd_argc++;
idx += token_len + 1;
}
}
// and wrappers for backward compatibility
int Cmd_Argc (void)
{
return Cmd_ArgcEx(&cmd_tokenizecontext);
}
char *Cmd_Argv (int arg)
{
return Cmd_ArgvEx(&cmd_tokenizecontext, arg);
}
//Returns a single string containing argv(1) to argv(argc() - 1)
char *Cmd_Args (void)
{
return Cmd_ArgsEx(&cmd_tokenizecontext);
}
//Returns a single string containing argv(start) to argv(argc() - 1)
//Unlike Cmd_Args, shrinks spaces between argvs
char *Cmd_MakeArgs (int start)
{
return Cmd_MakeArgsEx(&cmd_tokenizecontext, start);
}
//Parses the given string into command line tokens.
void Cmd_TokenizeString (char *text)
{
Cmd_TokenizeStringEx(&cmd_tokenizecontext, text);
}
// save cmd_tokenizecontext struct to ctx
void Cmd_SaveContext(tokenizecontext_t *ctx)
{
ctx[0] = cmd_tokenizecontext;
}
// restore cmd_tokenizecontext struct from ctx
void Cmd_RestoreContext(tokenizecontext_t *ctx)
{
cmd_tokenizecontext = ctx[0];
}
void Cmd_AddCommand (char *cmd_name, xcommand_t function)
{
cmd_function_t *cmd;
int key;
/* commented out when vid_restart was added
if (host_initialized) // because hunk allocation would get stomped
assert (!"Cmd_AddCommand after host_initialized");
*/
/* // fail if the command is a variable name
if (Cvar_Find(cmd_name)) {
Com_Printf ("Cmd_AddCommand: %s already defined as a var\n", cmd_name);
return;
} */
key = Com_HashKey (cmd_name) % CMD_HASHPOOL_SIZE;
// fail if the command already exists
for (cmd = cmd_hash_array[key]; cmd; cmd=cmd->hash_next) {
if (!strcasecmp (cmd_name, cmd->name)) {
Com_Printf ("Cmd_AddCommand: %s already defined\n", cmd_name);
return;
}
}
cmd = (cmd_function_t *) Hunk_Alloc (sizeof(cmd_function_t));
cmd->name = cmd_name;
cmd->function = function;
cmd->zmalloced = false;
cmd->next = cmd_functions;
cmd_functions = cmd;
cmd->hash_next = cmd_hash_array[key];
cmd_hash_array[key] = cmd;
}
qbool Cmd_AddRemCommand (char *cmd_name, xcommand_t function)
{
cmd_function_t *cmd;
int key;
key = Com_HashKey (cmd_name) % CMD_HASHPOOL_SIZE;
// fail if the command already exists
for (cmd = cmd_hash_array[key]; cmd; cmd=cmd->hash_next) {
if (!strcasecmp (cmd_name, cmd->name)) {
Com_Printf ("Cmd_AddCommand: %s already defined\n", cmd_name);
return false;
}
}
cmd = (cmd_function_t*)Q_malloc(sizeof(cmd_function_t)+strlen(cmd_name)+1);
cmd->name = (char*)(cmd+1); // points to extra space created after the structure
strcpy(cmd->name, cmd_name);
cmd->function = function;
cmd->next = cmd_functions;
cmd->zmalloced = true;
cmd_functions = cmd;
cmd->hash_next = cmd_hash_array[key];
cmd_hash_array[key] = cmd;
return true;
}
// removes command from the hash map of the commands
cmd_function_t *Cmd_RemoveCommand_Hash(char *cmd_name)
{
int key = Com_HashKey (cmd_name) % CMD_HASHPOOL_SIZE;
cmd_function_t *cmd = cmd_hash_array[key];
cmd_function_t *prev = NULL;
cmd_function_t *retval = NULL;
if (strcasecmp(cmd_name, cmd->name) == 0) {
retval = cmd;
cmd_hash_array[key] = cmd_hash_array[key]->hash_next;
}
else
{
prev = cmd;
for (cmd = cmd->hash_next; cmd; cmd = cmd->hash_next) {
if (strcasecmp(cmd_name, cmd->name) == 0) {
retval = cmd;
prev->hash_next = cmd->hash_next;
}
}
}
return retval;
}
// removes command from the linked list of all commands
cmd_function_t *Cmd_RemoveCommand_List (char *cmd_name)
{
cmd_function_t *cmd, **back;
back = &cmd_functions;
while (1)
{
cmd = *back;
if (!cmd)
{
return NULL;
}
if (!strcmp (cmd_name, cmd->name))
{
*back = cmd->next;
return cmd;
}
back = &cmd->next;
}
}
// removes command from all structures and deallocates it
void Cmd_RemoveCommand (char *cmd_name)
{
cmd_function_t *cmd;
cmd = Cmd_RemoveCommand_List(cmd_name);
cmd = Cmd_RemoveCommand_Hash(cmd_name);
if (cmd) {
if (cmd->zmalloced)
{
Q_free(cmd);
}
else
{
Con_Printf("Cmd_RemoveCommand: %s was not added dynamically\n", cmd_name);
}
}
}
qbool Cmd_Exists (char *cmd_name)
{
int key;
cmd_function_t *cmd;
key = Com_HashKey (cmd_name) % CMD_HASHPOOL_SIZE;
for (cmd=cmd_hash_array[key]; cmd; cmd = cmd->hash_next) {
if (!strcasecmp (cmd_name, cmd->name))
return true;
}
return false;
}
cmd_function_t *Cmd_FindCommand (const char *cmd_name)
{
int key;
cmd_function_t *cmd;
key = Com_HashKey (cmd_name) % CMD_HASHPOOL_SIZE;
for (cmd = cmd_hash_array[key]; cmd; cmd = cmd->hash_next) {
if (!strcasecmp (cmd_name, cmd->name))
return cmd;
}
return NULL;
}
char *Cmd_CompleteCommand (char *partial)
{
cmd_function_t *cmd;
int len;
cmd_alias_t *alias;
len = strlen(partial);
if (!len)
return NULL;
// check for exact match
for (cmd = cmd_functions; cmd; cmd = cmd->next)
if (!strcasecmp (partial, cmd->name))
return cmd->name;
for (alias = cmd_alias; alias; alias = alias->next)
if (!strcasecmp (partial, alias->name))
return alias->name;
// check for partial match
for (cmd = cmd_functions; cmd; cmd = cmd->next)
if (!strncasecmp (partial, cmd->name, len))
return cmd->name;
for (alias = cmd_alias; alias; alias = alias->next)
if (!strncasecmp (partial, alias->name, len))
return alias->name;
return NULL;
}
int Cmd_CompleteCountPossible (char *partial)
{
cmd_function_t *cmd;
int len, c = 0;
len = strlen(partial);
if (!len)
return 0;
for (cmd = cmd_functions; cmd; cmd = cmd->next)
if (!strncasecmp (partial, cmd->name, len))
c++;
return c;
}
int Cmd_AliasCompleteCountPossible (char *partial)
{
cmd_alias_t *alias;
int len, c = 0;
len = strlen(partial);
if (!len)
return 0;
for (alias = cmd_alias; alias; alias = alias->next)
if (!strncasecmp (partial, alias->name, len))
c++;
return c;
}
int Cmd_CommandCompare (const void *p1, const void *p2)
{
return strcmp((*((cmd_function_t **) p1))->name, (*((cmd_function_t **) p2))->name);
}
void Cmd_CmdList (qbool use_regex)
{
static cmd_function_t *sorted_cmds[1024];
int i, c, m = 0, count;
cmd_function_t *cmd;
char *pattern;
#define MAX_SORTED_CMDS (sizeof (sorted_cmds) / sizeof (sorted_cmds[0]))
for (cmd = cmd_functions, count = 0; cmd && count < MAX_SORTED_CMDS; cmd = cmd->next, count++)
sorted_cmds[count] = cmd;
qsort (sorted_cmds, count, sizeof (cmd_function_t *), Cmd_CommandCompare);
if (count == MAX_SORTED_CMDS)
assert(!"count == MAX_SORTED_CMDS");
pattern = (Cmd_Argc() > 1) ? Cmd_Argv(1) : NULL;
if (((c = Cmd_Argc()) > 1) && use_regex)
if (!ReSearchInit(Cmd_Argv(1)))
return;
Com_Printf ("List of commands:\n");
for (i = 0; i < count; i++) {
cmd = sorted_cmds[i];
if (use_regex) {
if (!(c == 1 || ReSearchMatch (cmd->name)))
continue;
} else {
if (pattern && !Q_glob_match (pattern, cmd->name))
continue;
}
Com_Printf ("%s\n", cmd->name);
m++;
}
if (use_regex && (c > 1))
ReSearchDone ();
Com_Printf ("------------\n%i/%i commands\n", m,count);
}
void Cmd_CmdList_f (void)
{
Cmd_CmdList (false);
}
void Cmd_CmdList_re_f (void)
{
Cmd_CmdList (true);
}
#define MAX_MACROS 64
static macro_command_t macro_commands[MAX_MACROS];
static int macro_count = 0;
void Cmd_ReInitAllMacro (void)
{
int i;
int teamplay;
teamplay = (int) Rulesets_RestrictTriggers ();
for (i = 0; i < macro_count; i++)
if (macro_commands[i].teamplay != MACRO_NORULES)
macro_commands[i].teamplay = teamplay;
}
void Cmd_AddMacroEx (const char *s, char *(*f) (void), int teamplay)
{
if (macro_count == MAX_MACROS)
Sys_Error ("Cmd_AddMacro: macro_count == MAX_MACROS");
snprintf (macro_commands[macro_count].name, sizeof (macro_commands[macro_count].name), "%s", s);
macro_commands[macro_count].func = f;
macro_commands[macro_count].teamplay = teamplay;
#ifdef WITH_TCL
// disconnect: it seems macro are safe with TCL NOW
// if (!teamplay) // don't allow teamplay protected macros since there's no protection for this in TCL yet
TCL_RegisterMacro (macro_commands + macro_count);
#endif
macro_count++;
}
void Cmd_AddMacro (const char *s, char *(*f) (void))
{
Cmd_AddMacroEx (s, f, MACRO_NORULES);
}
char *Cmd_MacroString (const char *s, int *macro_length)
{
int i;
macro_command_t *macro;
for (i = 0; i < macro_count; i++) {
macro = ¯o_commands[i];
if (!strncasecmp (s, macro->name, strlen (macro->name))) {
if (cbuf_current == &cbuf_main && (macro->teamplay == MACRO_DISALLOWED))
cbuf_current = &cbuf_formatted_comms;
*macro_length = strlen (macro->name);
return macro->func();
}
macro++;
}
*macro_length = 0;
return NULL;
}
static int Cmd_MacroCompare (const void *p1, const void *p2)
{
return strcmp ((*((macro_command_t **) p1))->name, (*((macro_command_t **) p2))->name);
}
void Cmd_MacroList_f (void)
{
int i, c, m = 0;
static macro_command_t *sorted_macros[MAX_MACROS];
for (i = 0; i < macro_count; i++)
sorted_macros[i] = ¯o_commands[i];
qsort (sorted_macros, macro_count, sizeof (macro_command_t *), Cmd_MacroCompare);
if (macro_count == MAX_MACROS)
assert(!"count == MAX_MACROS");
c = Cmd_Argc();
if (c > 1)
if (!ReSearchInit (Cmd_Argv (1)))
return;
Com_Printf ("List of macros:\n");
for (i = 0; i < macro_count; i++) {
if (c==1 || ReSearchMatch (sorted_macros[i]->name)) {
Com_Printf ("$%s\n", sorted_macros[i]->name);
m++;
}
}
if (c > 1)
ReSearchDone();
Com_Printf ("------------\n%i/%i macros\n", m, macro_count);
}
//Expands all $cvar expressions to cvar values
//Also expands $macro expressions
//Note: dest must point to a 1024 byte buffer
void Cmd_ExpandString (const char *data, char *dest)
{
unsigned int c;
char buf[255], *str;
int i, len = 0, quotes = 0, name_length = 0;
cvar_t *var, *bestvar;
int macro_length;
while ((c = *data)) {
if (c == '"')
quotes++;
if (c == '$' && !(quotes & 1)) {
data++;
// Copy the text after '$' to a temp buffer
i = 0;
buf[0] = 0;
bestvar = NULL;
while ((c = *data) > 32) {
if (c == '$')
break;
data++;
buf[i++] = c;
buf[i] = 0;
if ((var = Cvar_Find(buf)))
bestvar = var;
if (i >= (int) sizeof (buf) - 1)
break; // there no more space in buf
}
str = Cmd_MacroString (buf, ¯o_length);
name_length = macro_length;
if (bestvar && (!str || (strlen (bestvar->name) > macro_length))) {
str = bestvar->string;
name_length = strlen(bestvar->name);
if (bestvar->teamplay)
cbuf_current = &cbuf_formatted_comms;
}
if (str) {
// check buffer size
if (len + strlen (str) >= 1024 - 1)
break;
strcpy (&dest[len], str);
len += strlen (str);
i = name_length;
while (buf[i])
dest[len++] = buf[i++];
} else {
// no matching cvar or macro
dest[len++] = '$';
if (len + strlen (buf) >= 1024 - 1)
break;
strcpy (&dest[len], buf);
len += strlen (buf);
}
} else {
dest[len] = c;
data++;
len++;
if (len >= 1024 - 1)
break;
}
}
dest[len] = 0;
}
int Commands_Compare_Func (const void * arg1, const void * arg2)
{
return strcasecmp (*(char**) arg1, *(char**) arg2);
}
char *msgtrigger_commands[] = {
"play", "playvol", "stopsound", "set", "echo", "say", "say_team",
"alias", "unalias", "msg_trigger", "inc", "bind", "unbind", "record",
"easyrecord", "stop", "if", "if_exists", "wait", "log", "match_forcestart",
"dns", "addserver", "connect", "join", "observe",
"tcl_proc", "tcl_exec", "tcl_eval", "exec",
"set_ex", "set_alias_str", "set_bind_str","unset", "unset_re" ,
"toggle", "toggle_re", "set_calc", "rcon", "user", "users",
"unalias", "unalias_re",
"re_trigger", "re_trigger_options", "re_trigger_delete",
"re_trigger_enable","re_trigger_disable", "re_trigger_match",
"hud262_add","hud262_remove","hud262_position","hud262_bg",
"hud262_move","hud262_width","hud262_alpha","hud262_blink",
"hud262_disable","hud262_enable","hud262_list","hud262_bringtofront",
"hud_262font","hud262_hover","hud262_button",
"alias_in", "alias_out", "cvar_in", "cvar_out"
// ,NULL
};
char *formatted_comms_commands[] = {
"if", "wait", "echo", "say", "say_team", "set_tp",
"tp_point", "tp_pickup", "tp_took",
NULL
};
float impulse_time = -9999;
int impulse_counter;
qbool AllowedImpulse(int imp)
{
static int Allowed_TF_Impulses[] = {
135, 99, 101, 102, 103, 104, 105, 106, 107, 108, 109, 23, 144, 145,
159, 160, 161, 162, 163, 164, 165, 166, 167
};
int i;
if (!cl.teamfortress) return false;
for (i=0; i<sizeof(Allowed_TF_Impulses)/sizeof(Allowed_TF_Impulses[0]); i++) {
if (Allowed_TF_Impulses[i] == imp) {
if(++impulse_counter >= 30) {
if (cls.realtime < impulse_time + 5 && !cls.demoplayback) {
return false;
}
impulse_time = cls.realtime;
impulse_counter = 0;
}
return true;
}
}
return false;
}
static qbool Cmd_IsCommandAllowedInMessageTrigger( const char *command )
{
if( !strcasecmp( command, "impulse") )
return AllowedImpulse(Q_atoi(Cmd_Argv(1)));
return bsearch( &(command), msgtrigger_commands,
sizeof(msgtrigger_commands)/sizeof(msgtrigger_commands[0]),
sizeof(msgtrigger_commands[0]),Commands_Compare_Func) != NULL;
}
static qbool Cmd_IsCommandAllowedInTeamPlayMacros( const char *command )
{
char **s;
for (s = formatted_comms_commands; *s; s++) {
if (!strcasecmp(command, *s))
break;
}
return *s != NULL;
}
//A complete command line has been parsed, so try to execute it
static void Cmd_ExecuteStringEx (cbuf_t *context, char *text)
{
cvar_t *v;
cmd_function_t *cmd;
cmd_alias_t *a;
static char buf[1024];
cbuf_t *inserttarget, *oldcontext;
char *p, *n, *s;
char text_exp[1024];
oldcontext = cbuf_current;
cbuf_current = context;
Cmd_ExpandString (text, text_exp);
Cmd_TokenizeString (text_exp);
if (!Cmd_Argc())
goto done; // no tokens
if (cbuf_current == &cbuf_svc) {
if (CL_CheckServerCommand())
goto done;
}
// check functions
if ((cmd = Cmd_FindCommand(Cmd_Argv(0)))) {
if (gtf || cbuf_current == &cbuf_safe) {
if (!Cmd_IsCommandAllowedInMessageTrigger(Cmd_Argv(0))) {
Com_Printf ("\"%s\" cannot be used in message triggers\n", Cmd_Argv(0));
goto done;
}
} else if ((cbuf_current == &cbuf_formatted_comms)) {
if (!Cmd_IsCommandAllowedInTeamPlayMacros(Cmd_Argv(0))) {
Com_Printf ("\"%s\" cannot be used in combination with teamplay $macros\n", Cmd_Argv(0));
goto done;
}
}
if (cmd->function)
cmd->function();
else
Cmd_ForwardToServer ();
goto done;
}
// some bright guy decided to use "skill" as a mod command in Custom TF, sigh
if (!strcmp(Cmd_Argv(0), "skill") && Cmd_Argc() == 1 && Cmd_FindAlias("skill"))
goto checkaliases;
// check cvars
if ((v = Cvar_Find(Cmd_Argv(0)))) {
if ((cbuf_current == &cbuf_formatted_comms)) {
Com_Printf ("\"%s\" cannot be used in combination with teamplay $macros\n", Cmd_Argv(0));
goto done;
}
if (Cvar_Command())
goto done;
}
// check aliases
checkaliases:
if ((a = Cmd_FindAlias(Cmd_Argv(0)))) {
// QW262 -->
#ifdef WITH_TCL
if (a->flags & ALIAS_TCL)
{
TCL_ExecuteAlias (a);
goto done;
}
#endif
if (a->value[0]=='\0') goto done; // alias is empty.
if(a->flags & ALIAS_HAS_PARAMETERS) { // %parameters are given in alias definition
s=a->value;
buf[0] = '\0';
do {
n = strchr(s, '%');
if(n) {
if(*++n >= '1' && *n <= '9') {
n[-1] = 0;
strlcat(buf, s, sizeof(buf));
n[-1] = '%';
// insert numbered parameter
strlcat(buf,Cmd_Argv(*n-'0'), sizeof(buf));
} else if (*n == '0') {
n[-1] = 0;
strlcat(buf, s, sizeof(buf));
n[-1] = '%';
// insert all parameters
strlcat(buf, Cmd_Args(), sizeof(buf));
} else if (*n == '%') {
n[0] = 0;
strlcat(buf, s, sizeof(buf));
n[0] = '%';
} else {
if (*n) {
char tmp = n[1];
n[1] = 0;
strlcat(buf, s, sizeof(buf));
n[1] = tmp;
} else
strlcat(buf, s, sizeof(buf));
}
s=n+1;
}
} while(n);
strlcat(buf, s, sizeof(buf));
p = buf;
} else // alias has no parameters
p = a->value;
// <-- QW262
if (cbuf_current == &cbuf_svc)
{
Cbuf_AddText (p);
Cbuf_AddText ("\n");
} else
{
inserttarget = cbuf_current ? cbuf_current : &cbuf_main;
Cbuf_InsertTextEx (inserttarget, "\n");
// if the alias value is a command or cvar and
// the alias is called with parameters, add them
if (Cmd_Argc() > 1 && !strchr(p, ' ') && !strchr(p, '\t') &&
(Cvar_Find(p) || (Cmd_FindCommand(p) && p[0] != '+' && p[0] != '-'))
) {
Cbuf_InsertTextEx (inserttarget, Cmd_Args());
Cbuf_InsertTextEx (inserttarget, " ");
}
Cbuf_InsertTextEx (inserttarget, p);
}
goto done;
}
if (Cmd_LegacyCommand())
goto done;
if (!host_initialized && Cmd_Argc() > 1) {
if (Cvar_CreateTempVar())
goto done;
}
if (cbuf_current != &cbuf_svc)
{
if (cl_warncmd.integer || developer.integer)
Com_Printf ("Unknown command \"%s\"\n", Cmd_Argv(0));
}
done:
cbuf_current = oldcontext;
}
void Cmd_ExecuteString (char *text)
{
Cmd_ExecuteStringEx (NULL, text);
}
static qbool is_numeric (char *c)
{
return ( isdigit((int)(unsigned char)*c) ||
((*c == '-' || *c == '+') && (c[1] == '.' || isdigit((int)(unsigned char)c[1]))) ||
(*c == '.' && isdigit((int)(unsigned char)c[1])) );
}
void Re_Trigger_Copy_Subpatterns (const char *s, int* offsets, int num, cvar_t *re_sub); // QW262
extern cvar_t re_sub[10]; // QW262
void Cmd_CatchTriggerSubpatterns(const char *s, int* offsets, int num)
{
Re_Trigger_Copy_Subpatterns(s, offsets, min(num, 10), re_sub);
}
// this is a test replacement of the "if" command
void Cmd_If_New(void)
{
// syntax of this command has two possibilities:
// if (<expr>) then <cmd1> [else <cmd2>]
// if o1 o2 o3 [then] <cmd1> [else <cmd2>]
// the second one is for backward compatibility
parser_extra pars_ex;
int c;
int then_pos = 0;
qbool then_found = false, else_found = false;
int i, clen;
size_t expr_len = 0;
char* expr, * curarg;
int result, error;
char buf[1024];
pars_ex.subpatt_fnc = Cmd_CatchTriggerSubpatterns;
pars_ex.var2val_fnc = NULL;
c = Cmd_Argc();
// 0 1 2 3
// if e then c
if (c < 4) {
Com_Printf("Usage: if <expr> then <cmds> [else <cmds>]\n");
return;
}
for (i = 2; i < c; i++) {
if (!strcmp(Cmd_Argv(i), "then")) {
then_pos = i; then_found = true; break;
}
}
if (!then_found) {
Com_Printf("if command: \"then\" not found\n");
return;
}
for (i = 1; i < then_pos; i++) {
clen = strlen(Cmd_Argv(i));
expr_len += clen ? clen + 1 : 3; // we will take '' as a representation of an empty string
}
expr = (char *) Q_malloc(expr_len + 1);
expr[0] = '\0';
for (i = 1; i < then_pos; i++) {
if (i > 1)
strlcat(expr, " ", expr_len + 1);
curarg = Cmd_Argv(i);
if (*curarg)
strlcat(expr, curarg, expr_len + 1);
else
strlcat(expr, "''", expr_len + 1);
}
error = Expr_Eval_Bool(expr, &pars_ex, &result);
if (error != EXPR_EVAL_SUCCESS) {
Com_Printf("Error in condition: %s (\"%s\")\n", Parser_Error_Description(error), expr);
Q_free(expr);
return;
}
Q_free(expr);
then_pos++; // skip "then"
buf[0] = '\0';
if (result) // true case
{
for (i = then_pos; i < c; i++) {
if (!else_found && !strcmp(Cmd_Argv(i), "else"))
break;
if (buf[0])
strlcat (buf, " ", sizeof(buf));
strlcat (buf, Cmd_Argv(i), sizeof(buf));
}
}
else // result = false
{
for (i = then_pos; i < c; i++) {
if (else_found) {
if (buf[0])
strlcat (buf, " ", sizeof(buf));
strlcat (buf, Cmd_Argv(i), sizeof(buf));
}
if (!else_found && !strcmp(Cmd_Argv(i), "else")) else_found = true;
}
}
strlcat (buf, "\n", sizeof(buf));
Cbuf_InsertTextEx (cbuf_current ? cbuf_current : &cbuf_main, buf);
}
void Cmd_If_Old (void)
{
int i, c;
char *op, buf[1024] = {0};
qbool result;
if ((c = Cmd_Argc()) < 5) {
Com_Printf ("Usage: if <expr1> <op> <expr2> <command> [else <command>]\n");
return;
}
op = Cmd_Argv(2);
if (!strcmp(op, "==") || !strcmp(op, "=") || !strcmp(op, "!=") || !strcmp(op, "<>")) {
if (is_numeric(Cmd_Argv(1)) && is_numeric(Cmd_Argv(3)))
result = Q_atof(Cmd_Argv(1)) == Q_atof(Cmd_Argv(3));
else
result = !strcmp(Cmd_Argv(1), Cmd_Argv(3));
if (op[0] != '=')
result = !result;
} else if (!strcmp(op, ">")) {
result = Q_atof(Cmd_Argv(1)) > Q_atof(Cmd_Argv(3));
} else if (!strcmp(op, "<")) {
result = Q_atof(Cmd_Argv(1)) < Q_atof(Cmd_Argv(3));
} else if (!strcmp(op, ">=")) {
result = Q_atof(Cmd_Argv(1)) >= Q_atof(Cmd_Argv(3));
} else if (!strcmp(op, "<=")) {
result = Q_atof(Cmd_Argv(1)) <= Q_atof(Cmd_Argv(3));
} else if (!strcmp(op, "isin")) {
result = (strstr(Cmd_Argv(3), Cmd_Argv(1)) ? 1 : 0);
} else if (!strcmp(op, "!isin")) {
result = (strstr(Cmd_Argv(3), Cmd_Argv(1)) ? 0 : 1);
} else if (!strcmp(op, "=~") || !strcmp(op, "!~")) {
pcre* regexp;
const char *error;
int error_offset;
int rc;
int offsets[99];
regexp = pcre_compile (Cmd_Argv(3), 0, &error, &error_offset, NULL);
if (!regexp) {
Com_Printf ("Error in regexp: %s\n", error);
return;
}
rc = pcre_exec (regexp, NULL, Cmd_Argv(1), strlen(Cmd_Argv(1)),
0, 0, offsets, 99);
if (rc >= 0) {
Re_Trigger_Copy_Subpatterns (Cmd_Argv(1), offsets, min(rc, 10), re_sub);
result = true;
} else
result = false;
if (op[0] != '=')
result = !result;
pcre_free (regexp);
} else {
Com_Printf ("unknown operator: %s\n", op);
Com_Printf ("valid operators are ==, =, !=, <>, >, <, >=, <=, isin, !isin, =~, !~\n");
return;
}
if (result) {
for (i = 4; i < c; i++) {
if ((i == 4) && !strcasecmp(Cmd_Argv(i), "then"))
continue;
if (!strcasecmp(Cmd_Argv(i), "else"))
break;
if (buf[0])
strlcat (buf, " ", sizeof (buf) - strlen (buf) - 1);
strlcat (buf, Cmd_Argv(i), sizeof (buf) - strlen (buf) - 1);
}
} else {
for (i = 4; i < c ; i++) {
if (!strcasecmp(Cmd_Argv(i), "else"))
break;
}
if (i == c)
return;
for (i++; i < c; i++) {
if (buf[0])
strlcat (buf, " ", sizeof (buf) - strlen (buf) - 1);
strlcat (buf, Cmd_Argv(i), sizeof (buf) - strlen (buf) - 1);
}
}
strlcat (buf, "\n", sizeof (buf) - strlen (buf));
Cbuf_InsertTextEx (cbuf_current ? cbuf_current : &cbuf_main, buf);
}
void Cmd_If_f(void) {
if (Cmd_Argc() > 2 && Cmd_Argv(1)[0] == '(')
// new "if" requires parentheses around the condition
// while the original "if" wouldn't work with these so it's safe
// to presume noone used it there
Cmd_If_New();
else Cmd_If_Old();
}
void Cmd_If_Exists_f(void)
{
int argc;
char *type;
char *name;
qbool exists;
qbool iscvar, isalias, istrigger, ishud;
argc = Cmd_Argc();
if ( argc < 4 || argc > 5) {
Com_Printf ("if_exists <type> <name> <cmd1> [<cmd2>] - conditional execution\n");
return;
}
type = Cmd_Argv(1);
name = Cmd_Argv(2);
if ( ( (iscvar = !strcmp(type, "cvar")) && Cvar_Find(name) ) ||
( (isalias = !strcmp(type, "alias")) && Cmd_FindAlias (name) ) ||
( (istrigger = !strcmp(type, "trigger")) && CL_FindReTrigger (name) ) ||
( (ishud = !strcmp(type, "hud")) && Hud_ElementExists (name) ) )
exists = true;
else {
exists = false;
if (!(iscvar || isalias || istrigger || ishud)) {
Com_Printf("if_exists: <type> can be cvar, alias, trigger, hud\n");
return;
}
}
if (exists) {
Cbuf_InsertTextEx (cbuf_current ? cbuf_current : &cbuf_main,"\n");
Cbuf_InsertTextEx (cbuf_current ? cbuf_current : &cbuf_main,Cmd_Argv(3));
} else if (argc == 5) {
Cbuf_InsertTextEx (cbuf_current ? cbuf_current : &cbuf_main,"\n");
Cbuf_InsertTextEx (cbuf_current ? cbuf_current : &cbuf_main,Cmd_Argv(4));
} else
return;
}
void Cmd_Eval_f(void)
{
int errn;
expr_val value;
if (Cmd_Argc() != 2) {
Com_Printf("Usage: eval <expression>\n"
"Prints the value of given expression after evaluation in the internal parser\n");
return;
}
value = Expr_Eval(Cmd_Argv(1), NULL, &errn);
if (errn != EXPR_EVAL_SUCCESS)
{
Com_Printf("Error occured: %s\n", Parser_Error_Description(errn));
return;
}
else
{
switch (value.type) {
case ET_INT: Com_Printf("Result: %i (integer)\n", value.i_val); break;
case ET_DBL: Com_Printf("Result: %f (double)\n", value.d_val); break;
case ET_BOOL: Com_Printf("Result: %s (bool)\n", value.b_val ? "true" : "false"); break;
case ET_STR: Com_Printf("Result: (string)\n\"%s\"\n", value.s_val); Q_free(value.s_val); break;
default: Com_Printf("Error: Unknown value type\n"); break;
}
}
}
// QW262 -->
static qbool do_in(char *buf, char *orig, char *str, int options)
{
if ((options & 2) && strstr(orig, str))
return false;
if (options & 1) { // buf size is 1024 both in Cmd_Alias_In_f and Cmd_Cvar_In_f
strlcpy(buf, orig, 1024);
strlcat(buf, str, 1024);
} else {
strlcpy(buf, str, 1024);
strlcat(buf, orig, 1024);
}
return true;
}
static qbool do_out(char *orig, char *str, int options)
{
char *p;
int len = strlen(str);
if (!(p=strstr(orig, str)))
return false;
if (!(options & 1))
memmove(p, p+len, strlen(p)+1);
return true;
}
void Cmd_Alias_In_f (void)
{
cmd_alias_t *alias;
cvar_t *var;
char buf[1024];
char *alias_name;
int options;
if (Cmd_Argc() < 3 || Cmd_Argc() > 4) {
Com_Printf ("alias_in <alias> <cvar> [<options>]\n");
return;
}
alias_name = Cmd_Argv(1);
alias = Cmd_FindAlias(alias_name);
options = atoi(Cmd_Argv(3));
if (!alias) {
if ((options & 8)) {
alias = Cmd_AliasCreate(alias_name);
if (!alias)
return;
alias->value = Q_strdup("");
} else {
Com_Printf ("alias_in: unknown alias \"%s\"\n", alias_name);
return;
}
}
var = Cvar_Find(Cmd_Argv(2));
if (!var) {
Com_Printf ("alias_in: unknown cvar \"%s\"\n", Cmd_Argv(2));
return;
}
if (!do_in (buf, alias->value, var->string, options)) {
if (options & 4)
Com_Printf ("alias_in: already inserted\n");
return;
}
Q_free(alias->value);
alias->value = Q_strdup(buf);
if (strchr(buf, '%'))
alias->flags |= ALIAS_HAS_PARAMETERS;
}
void Cmd_Alias_Out_f (void)
{
cmd_alias_t *alias;
cvar_t *var;
int options;
if (Cmd_Argc() < 3 || Cmd_Argc() > 4) {
Com_Printf ("alias_out <alias> <cvar> [options]\n");
return;
}
options = atoi(Cmd_Argv(3));
alias = Cmd_FindAlias(Cmd_Argv(1));
if (!alias) {
Com_Printf ("alias_out: unknown alias \"%s\"\n", Cmd_Argv(1));
return;
}
var = Cvar_Find(Cmd_Argv(2));
if (!var) {
Com_Printf ("alias_out: unknown cvar \"%s\"\n", Cmd_Argv(2));
return;
}
if (!do_out (alias->value, var->string, options)) {
if (!(options & 2))
Com_Printf ("alias_out: not found\n");
return;
}
}
void Cmd_Cvar_In_f (void)
{
cvar_t *var1;
cvar_t *var2;
char buf[1024];
char *var_name;
int options;
if (Cmd_Argc() < 3 || Cmd_Argc() > 4) {
Com_Printf ("cvar_in <cvar1> <cvar2> [<options>]\n");
return;
}
var_name = Cmd_Argv(1);
options = atoi(Cmd_Argv(3));
var1 = Cvar_Find(var_name);
if (!var1) {
if ((options & 8)) {
var1 = Cvar_Create (var_name, "", 0);
} else {
Com_Printf ("cvar_in: unknown cvar \"%s\"\n", var_name);
return;
}
}
var2 = Cvar_Find(Cmd_Argv(2));
if (!var2) {
Com_Printf ("cvar_in: unknown cvar \"%s\"\n", Cmd_Argv(2));
return;
}
if (!do_in (buf, var1->string, var2->string, options)) {
if (options & 4)
Com_Printf ("cvar_in: already inserted\n");
return;
}
Cvar_Set (var1, buf);
}
void Cmd_Cvar_Out_f (void)
{
cvar_t *var1;
cvar_t *var2;
char buf[1024];
int options;
if (Cmd_Argc()<3 || Cmd_Argc()>4){
Com_Printf ("cvar_out <cvar1> <cvar2> [<options>]\n");
return;
}
options = atoi(Cmd_Argv(3));
var1 = Cvar_Find(Cmd_Argv(1));
if (!var1) {
Com_Printf ("cvar_out: unknown cvar \"%s\"\n", Cmd_Argv(1));
return;
}
var2 = Cvar_Find(Cmd_Argv(2));
if (!var2) {
Com_Printf ("cvar_out: unknown cvar \"%s\"\n", Cmd_Argv(2));
return;
}
strcpy (buf, var1->string);
if (!do_out (buf, var2->string, options)) {
if (!(options & 2))
Com_Printf ("cvar_out: not found\n");
return;
}
Cvar_Set (var1, buf);
}
// <-- QW262
/*
============
Cmd_Shutdown
============
*/
void Cmd_Shutdown(void)
{
}
void Cmd_Init (void)
{
// register our commands
Cmd_AddCommand ("exec", Cmd_Exec_f);
Cmd_AddCommand ("echo", Cmd_Echo_f);
Cmd_AddCommand ("aliaslist", Cmd_AliasList_f);
Cmd_AddCommand ("aliasedit", Cmd_EditAlias_f);
Cmd_AddCommand ("alias", Cmd_Alias_f);
Cmd_AddCommand ("tempalias", Cmd_Alias_f);
Cmd_AddCommand ("viewalias", Cmd_Viewalias_f);
Cmd_AddCommand ("unaliasall", Cmd_UnAliasAll_f);
Cmd_AddCommand ("unalias", Cmd_UnAlias_f);
Cmd_AddCommand ("unalias_re", Cmd_UnAlias_re_f);
Cmd_AddCommand ("wait", Cmd_Wait_f);
Cmd_AddCommand ("cmdlist", Cmd_CmdList_f);
Cmd_AddCommand ("cmdlist_re", Cmd_CmdList_re_f);
Cmd_AddCommand ("if", Cmd_If_f);
Cmd_AddCommand ("if_exists", Cmd_If_Exists_f);
Cmd_AddCommand ("eval", Cmd_Eval_f);
// QW262 -->
Cmd_AddCommand ("alias_in", Cmd_Alias_In_f);
Cmd_AddCommand ("alias_out", Cmd_Alias_Out_f);
Cmd_AddCommand ("cvar_in", Cmd_Cvar_In_f);
Cmd_AddCommand ("cvar_out", Cmd_Cvar_Out_f);
// <-- QW262
Cvar_Register(&cl_curlybraces);
Cvar_Register(&cl_warnexec);
Cmd_AddCommand ("macrolist", Cmd_MacroList_f);
qsort(msgtrigger_commands,
sizeof(msgtrigger_commands)/sizeof(msgtrigger_commands[0]),
sizeof(msgtrigger_commands[0]),Commands_Compare_Func);
}
|