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
|
/*
* Fenix - Videogame compiler/interpreter
* Current release : FENIX - PROJECT 1.0 - R 0.84
* Last stable release :
* Project documentation : http://fenix.divsite.net
*
*
* 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
*
* Copyright 1999 Jos Luis Cebrin Page
* Copyright 2002 Fenix Team
*
*/
#ifdef TARGET_BEOS
#include <posix/assert.h>
#else
#include <assert.h>
#endif
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#ifdef USE_GETTEXT
#include <libintl.h>
#define _(String) gettext (String)
#else
#define _(String) String
#endif
#include "fxi.h"
#include "dcb.h"
/* ---------------------------------------------------------------------- */
#define MAXTEXT 5000
#define CHARWIDTH 6
#define CHARHEIGHT 8
#define CONSOLE_HISTORY 512
#define CONSOLE_LINES 25
#define CONSOLE_COLUMNS 80
#define COMMAND_HISTORY 128
#define HELPTXT _( \
"02Process Info07\n" \
"04INSTANCES 07 List all running processes\n" \
"04GLOBALS 07 Show global vars with values\n" \
"04LOCALS proc 07 Show a process's local vars\n" \
"04PRIVATES proc 07 Show a process's private vars\n" \
"04PUBLICS proc 07 Show a process's public vars\n" \
"\n" \
"02Debugging Commands07\n" \
"04TRACE 07 Execute one instruction and Debug\n" \
"04BREAK 07 List breakpoints\n" \
"04BREAK proc 07 Add a breakpoint on process execution\n"\
"04DELETE proc 07 Delete a breakpoint\n" \
"04CONTINUE 07 Continue the execution\n" \
"04DOFRAME 07 Continue to next frame\n" \
"\n" \
"02Misc07\n" \
"04SHOW expression07 Evaluate and show some expression\n" \
"04STRINGS 07 Show all strings in memory\n" \
"04VARS 07 Show internals vars\n" \
"04QUIT 07 Kill the program and exit\n\n" \
"\n" \
"02Process Interaction07\n" \
"04RUN proc [args]07 Run a process\n" \
"04KILL proc 07 Kill a process\n" \
"04WAKEUP proc 07 Wakeup a process\n" \
"04SLEEP proc 07 Sleep a process\n" \
"04FREEZE proc 07 Freeze a process\n" \
"04KILLALL proc 07 Kill all process with criteria\n" \
"04WAKEUPALL proc 07 Wakeup all process with criteria\n" \
"04SLEEPALL proc 07 Sleep all process with criteria\n" \
"04FREEZEALL proc 07 Freeze all process with criteria\n" \
"\n" \
"You can evaluate free expressions in the console,\n" \
"and you can see/change local, public and private vars\n" \
"using the '.' operator (pe: 65535.X, MAIN.X, etc.)\n")
/* ---------------------------------------------------------------------- */
static void eval_immediate() ;
static void eval_value() ;
static void eval_factor() ;
static void eval_subexpression() ;
static char * eval_expression(const char * here, int interactive);
static int type_size (DCB_TYPEDEF orig) ;
static DCB_TYPEDEF reduce_type (DCB_TYPEDEF orig) ;
static void var2const() ;
/* Console contents */
static int console_lines = CONSOLE_LINES ;
static int console_columns = CONSOLE_COLUMNS ;
static char * console[CONSOLE_HISTORY] ;
static int console_initialized = 0 ;
static int console_head = 0 ;
static int console_tail = 0 ;
static char * command[COMMAND_HISTORY] ;
static int command_initialized = 0 ;
static int command_head = 0 ;
static int command_tail = 0 ;
static int command_count = 0 ;
static char * show_expression = 0;
static int console_showcolor = 0xffffff;
/* Trace */
extern int force_debug;
extern int debug_next;
extern int debug_on_frame;
extern int trace_sentence;
extern INSTANCE * trace_instance;
/* ---------------------------------------------------------------------- */
/* Tipos de token */
#define IDENTIFIER 1
#define STRING 2
#define NUMBER 3
#define OPERATOR 4
#define NOTOKEN 5
struct
{
int type ;
DCB_VAR var ;
double value ;
void * data ;
char name[256] ;
} result, lvalue ;
struct
{
enum { T_ERROR, T_VARIABLE, T_NUMBER, T_CONSTANT, T_STRING } type ;
char name[128] ;
double code ;
} token ;
static const char * token_ptr ;
/* ---------------------------------------------------------------------- */
#define N_CONSOLE_VARS (sizeof(console_vars)/sizeof(console_vars[0]))
#define CON_DWORD 0x0001
#define CON_DWORD_HEX 0x8001
struct
{
char * name ;
void * value ;
int type ;
}
console_vars[] =
{
{ "ENABLE_FILTERING", &enable_filtering, CON_DWORD },
#ifdef MMX_FUNCTIONS
{ "MMX", &MMX_available, CON_DWORD },
#endif
{ "REPORT_GRAPHICS", &report_graphics, CON_DWORD },
{ "REPORT_STRING", &report_string, CON_DWORD },
{ "DEBUG", &debug, CON_DWORD },
{ "CONSOLE_LINES", &console_lines, CON_DWORD },
{ "CONSOLE_COLUMNS", &console_columns, CON_DWORD },
{ "SHOW_COLOR", &console_showcolor, CON_DWORD_HEX },
{ "FILES", &opened_files, CON_DWORD },
} ;
static char * describe_type (DCB_TYPEDEF type, int from) ;
static char * show_value (DCB_TYPEDEF type, void * data) ;
static void show_struct (int num, char * title, int indent, void * data) ;
static void show_var (DCB_VAR var, char * name, void * data, char * title, int indent) ;
/* ---------------------------------------------------------------------- */
void gr_con_printf (const char *fmt, ...)
{
char text[MAXTEXT], * ptr, * iptr ;
va_list ap;
va_start(ap, fmt);
_vsnprintf(text, sizeof(text), fmt, ap);
va_end(ap);
text[sizeof(text)-1] = 0;
if (*text == '[')
{
memmove (text+3, text, strlen(text)+1) ;
memmove (text, "08", 3) ;
ptr = strchr(text, ']') ;
if (ptr)
{
ptr++ ;
memmove (ptr+3, ptr, strlen(ptr)+1) ;
memmove (ptr, "07", 3) ;
}
}
iptr = text ;
ptr = text ;
while (*ptr)
{
if (*ptr == '\n')
{
*ptr = 0 ;
gr_con_putline(iptr) ;
iptr = ptr+1 ;
}
if (*ptr == '')
{
ptr++ ;
if (isdigit(*ptr)) ptr++ ;
if (isdigit(*ptr)) ptr++ ;
continue ;
}
ptr++ ;
}
if (ptr > iptr) {
gr_con_putline(iptr) ;
}
}
void gr_con_putline (char * text)
{
if (!console_initialized)
{
memset (console, 0, sizeof(console)) ;
console_initialized = 1 ;
}
if (console[console_tail])
free(console[console_tail]) ;
console[console_tail] = strdup(text) ;
console_tail++ ;
if (console_tail == CONSOLE_HISTORY)
console_tail = 0 ;
if (console_tail == console_head)
{
console_head++ ;
if (console_head == CONSOLE_HISTORY)
console_head = 0 ;
}
}
void gr_con_putcommand (const char * commandline)
{
if (!command_initialized)
{
memset (command, 0, sizeof(command));
command_initialized = 1;
}
if (command[command_tail])
free(command[command_tail]);
command[command_tail++] = strdup(commandline);
if (command_tail == COMMAND_HISTORY)
command_tail = 0;
if (command_tail == command_head)
{
if (++command_head == COMMAND_HISTORY)
command_head = 0;
}
else
command_count++;
}
const char * gr_con_getcommand (int offset)
{
if (offset >= 0 || offset < -command_count)
return NULL;
offset = command_tail + offset;
while (offset < 0)
offset = COMMAND_HISTORY + offset;
return command[offset];
}
static int console_showing = 0 ;
static int console_scroll_pos = 0 ;
static int console_scroll_lateral_pos = 0 ;
static char console_input[128] ;
void gr_con_getkey(int key, int sym)
{
static int history_offset = 0;
char buffer[2] ;
const char * command;
if (key == 0 && sym == 273)
{
command = gr_con_getcommand(--history_offset);
if (command == NULL)
history_offset++;
else
strncpy (console_input, command, 127);
}
if (key == 0 && sym == 274)
{
if (history_offset == -1)
{
*console_input = 0;
history_offset++;
}
else
{
command = gr_con_getcommand(++history_offset);
if (command == NULL)
history_offset--;
else
strncpy (console_input, command, 127);
}
}
if (key == 8 && *console_input)
console_input[strlen(console_input)-1] = 0 ;
if (key == 27)
*console_input = 0 ;
if (key == 13 && *console_input)
{
console_scroll_pos = 0 ;
gr_con_printf ("15> %s", console_input) ;
gr_con_putcommand (console_input);
gr_con_do (console_input) ;
*console_input = 0 ;
history_offset = 0;
}
if (key >= 32 && key <= 255)
{
buffer[0] = key ;
buffer[1] = 0 ;
strcat (console_input, buffer) ;
}
}
void gr_con_show(int doit)
{
/* Slo se puede mostrar la consola si hay informacin de debug */
if (dcb.data.NID > 0) {
console_showing = doit ;
}
}
extern int * stack_gptr;
void gr_con_draw()
{
int x, y, line, count ;
static int con_y = 0 ;
static int vy = CHARHEIGHT ;
if (!console_initialized)
return ;
if (console_columns > scrbitmap->width / CHARWIDTH) {
console_columns = scrbitmap->width / CHARWIDTH ;
}
if (console_lines > ( scrbitmap->height - CHARHEIGHT * 2 ) / CHARHEIGHT) {
console_lines = ( scrbitmap->height - CHARHEIGHT * 2 ) / CHARHEIGHT ;
}
if (console_showing)
{
if (con_y < console_lines*CHARHEIGHT)
{
con_y += vy ;
if (vy > 4) vy-- ;
}
if (con_y > console_lines*CHARHEIGHT) con_y = console_lines*CHARHEIGHT, vy = CHARHEIGHT ;
}
else
{
if (con_y > 0)
{
con_y -= vy, vy++ ;
background_dirty = 1;
}
if (con_y < 0) { con_y = 0 ; vy = CHARHEIGHT ; return ; }
}
x = (scrbitmap->width - console_columns*CHARWIDTH)/2 ;
y = -console_lines*CHARHEIGHT + con_y ;
if (show_expression != 0)
{
char * result = eval_expression (show_expression, 0);
if (result == 0)
{
free (show_expression);
show_expression = 0;
}
else
{
gr_sys_color (console_showcolor, 0) ;
gr_sys_puts (scrbitmap,
(scrbitmap->width - strlen(result)*CHARWIDTH)/2,
(con_y <= 0) ? 2:con_y+CHARHEIGHT, result, strlen(result));
gr_mark_rect (0, (con_y <= 0) ? 2:con_y+CHARHEIGHT, scr_width, CHARHEIGHT);
}
}
if (con_y <= 0) return ;
line = console_tail ;
for (count = 0 ; count < console_lines+console_scroll_pos ; count++)
{
if (line == console_head)
break ;
line-- ;
if (line < 0) line = CONSOLE_HISTORY-1 ;
}
console_scroll_pos = count-console_lines ;
if (console_scroll_pos < 0) console_scroll_pos = 0 ;
gr_mark_rect (x, y, console_columns * CHARWIDTH, console_lines * CHARHEIGHT + CHARHEIGHT*2);
for (count = 0 ; count < console_lines ; count++)
{
gr_sys_color (0xC0C0C0, 0x402040) ;
if (!console[line] || console_scroll_lateral_pos >= strlen(console[line]))
gr_sys_puts (scrbitmap, x, y, "", console_columns) ;
else {
int pos = 0 ;
int off = 0;
do {
if (console[line][pos] == '') {
off += 3;
}
pos++;
} while (pos-off<console_scroll_lateral_pos) ;
do {
if (console[line][pos] == '') {
gr_sys_puts (scrbitmap, x, y, console[line]+pos, 3) ;
break;
}
} while (pos--) ;
gr_sys_puts (scrbitmap, x, y, console[line]+console_scroll_lateral_pos+off, console_columns) ;
}
y += CHARHEIGHT ;
line++ ;
if (line == CONSOLE_HISTORY) line = 0 ;
}
if (console_showing && trace_sentence != -1) {
if (trace_instance && instance_exists(trace_instance) && dcb.sourcecount[trace_sentence >> 24]) {
gr_con_printf("[%s(%d):%d]\n%s",
(trace_instance->inproc ? trace_instance->inproc : trace_instance->proc)->name,
LOCDWORD(trace_instance, PROCESS_ID),
trace_sentence & 0xFFFFFF,
dcb.sourcelines [trace_sentence >> 24] [(trace_sentence & 0xFFFFFF)-1]) ;
}
debug_on_frame = 0;
force_debug = 0;
debug_next = 0;
trace_sentence = -1;
}
gr_sys_color (0xFFFFFF, 0x404040) ;
gr_sys_puts (scrbitmap, x, y, ">", 2) ;
strcat(console_input, "_") ;
gr_sys_puts (scrbitmap, x+CHARWIDTH*2, y, console_input, console_columns-2) ;
console_input[strlen(console_input)-1] = 0 ;
}
void gr_con_scroll (int direction)
{
if (direction > 0)
{
console_scroll_pos -= console_lines ;
if (console_scroll_pos < 0)
console_scroll_pos = 0 ;
}
else
{
console_scroll_pos += console_lines ;
if (console_scroll_pos > CONSOLE_HISTORY)
console_scroll_pos = CONSOLE_HISTORY ;
}
}
void gr_con_lateral_scroll (int direction)
{
if (direction > 0)
{
console_scroll_lateral_pos-- ;
if (console_scroll_lateral_pos < 0)
console_scroll_lateral_pos = 0 ;
}
else
{
console_scroll_lateral_pos++ ;
if (console_scroll_lateral_pos > MAXTEXT)
console_scroll_lateral_pos = MAXTEXT ;
}
}
/* ---------------------------------------------------------------------- */
static char * describe_type (DCB_TYPEDEF type, int from)
{
static char buffer[512] ;
int i ;
if (!from) buffer[0] = 0 ;
switch (type.BaseType[from])
{
case TYPE_ARRAY:
for (i = from ; type.BaseType[i] == TYPE_ARRAY; i++) ;
describe_type (type, i) ;
for (i = from ; type.BaseType[i] == TYPE_ARRAY; i++)
_snprintf (buffer+strlen(buffer), 512-strlen(buffer), "[%d]", type.Count[i]-1) ;
break ;
case TYPE_STRUCT:
strcat (buffer, "STRUCT") ;
break ;
case TYPE_DWORD:
strcat (buffer, "DWORD") ;
break ;
case TYPE_INT:
strcat (buffer, "INT") ;
break ;
case TYPE_SHORT:
strcat (buffer, "SHORT") ;
break ;
case TYPE_WORD:
strcat (buffer, "WORD") ;
break ;
case TYPE_BYTE:
strcat (buffer, "BYTE") ;
break ;
case TYPE_SBYTE:
strcat (buffer, "SIGNED BYTE") ;
break ;
case TYPE_CHAR:
strcat (buffer, "CHAR") ;
break ;
case TYPE_STRING:
strcat (buffer, "STRING") ;
break ;
case TYPE_FLOAT:
strcat (buffer, "FLOAT") ;
break ;
case TYPE_POINTER:
describe_type (type, from+1) ;
strcat (buffer, " POINTER") ;
break ;
}
return buffer ;
}
static char * show_value (DCB_TYPEDEF type, void * data)
{
static char buffer[256] ;
char * newbuffer ;
int subsize;
unsigned int n ;
unsigned int count ;
switch (type.BaseType[0])
{
case TYPE_ARRAY:
count = type.Count[0];
type = reduce_type(type) ;
subsize = type_size(type) ;
if (type.BaseType[0] == TYPE_STRUCT) {
return "" ;
}
newbuffer = (char *) malloc(256) ;
strcpy (newbuffer, "= (") ;
for (n = 0 ; n < count ; n++)
{
if (n)
strcat (newbuffer, ", ") ;
show_value (type, data) ;
if (strlen(newbuffer) + strlen(buffer) > 30)
{
strcat (newbuffer, "...") ;
break ;
}
strcat (newbuffer, buffer + 2) ;
data = (Uint8*)data + subsize ;
}
strcat (newbuffer, ")") ;
strcpy (buffer, newbuffer) ;
free(newbuffer) ;
return buffer ;
case TYPE_STRUCT:
return "" ;
case TYPE_STRING:
_snprintf (buffer, 512, "= \"%s\"", string_get(*(Uint32 *)data)) ;
return buffer ;
case TYPE_BYTE:
_snprintf (buffer, 512, "= %d", *(Uint8 *)data) ;
return buffer ;
case TYPE_SBYTE:
_snprintf (buffer, 512, "= %d", *(Sint8 *)data) ;
return buffer ;
case TYPE_CHAR:
if (*(Uint8 *)data >= 32)
_snprintf (buffer, 512, "= '%c'", *(Uint8 *)data) ;
else
_snprintf (buffer, 512, "= '\\x%02X'", *(Uint8 *)data) ;
return buffer ;
case TYPE_FLOAT:
_snprintf (buffer, 512, "= %g", *(float *)data) ;
return buffer ;
case TYPE_WORD:
_snprintf (buffer, 512, "= %d", *(Uint16 *)data) ;
return buffer ;
case TYPE_DWORD:
_snprintf (buffer, 512, "= %ud", *(Uint32 *)data) ;
return buffer ;
case TYPE_SHORT:
_snprintf (buffer, 512, "= %d", *(Sint16 *)data) ;
return buffer ;
case TYPE_INT:
_snprintf (buffer, 512, "= %d", *(Sint32 *)data) ;
return buffer ;
case TYPE_POINTER:
_snprintf (buffer, 512, "= 0x%08X", *(Uint32 *)data) ;
return buffer ;
default:
return "?" ;
}
}
static void show_struct (int num, char * title, int indent, void * data)
{
int n, count ;
DCB_VAR * vars ;
vars = dcb.varspace_vars[num] ;
count = dcb.varspace[num].NVars ;
for (n = 0 ; n < count ; n++)
{
show_var (vars[n], 0, data ? (Uint8*)data + vars[n].Offset : 0, title, indent) ;
}
}
static void show_var (DCB_VAR var, char * name, void * data, char * title, int indent)
{
char spaces[256] ;
strncpy (spaces, " ", indent) ;
spaces[indent] = 0 ;
if (!name)
{
unsigned int code ;
name = "?" ;
for (code = 0 ; code < dcb.data.NID ; code++)
{
if (dcb.id[code].Code == var.ID)
{
name = dcb.id[code].Name ;
break ;
}
}
}
if (data) {
gr_con_printf ("%s%s %s %s %s\n", title, spaces, describe_type(var.Type, 0), name, show_value(var.Type, data)) ;
} else {
gr_con_printf ("%s%s %s %s\n", title, spaces, describe_type(var.Type, 0), name) ;
}
if (var.Type.BaseType[0] == TYPE_STRUCT)
{
show_struct (var.Type.Members, title, indent+3, data) ;
gr_con_printf ("%s%s END STRUCT", title, spaces) ;
}
}
/* ---------------------------------------------------------------------- */
/* Very simple tokenizer */
static void get_token()
{
char * ptr ;
unsigned int n ;
while (isspace(*token_ptr)) token_ptr++ ;
if (!*token_ptr)
{
token.type = NOTOKEN ;
return ;
}
/* Numbers */
if (ISNUM(*token_ptr))
{
const char * ptr ;
double num = 0, dec ;
int base = 10 ;
/* Hex/Bin/Octal numbers with the h/b/o sufix */
ptr = token_ptr ;
while (*ptr == '1' || *ptr == '0') ptr++ ;
if ((*ptr == 'b' || *ptr == 'B') && !ISALNUM(ptr[1]))
base = 2 ;
else
{
ptr = token_ptr ;
while (ISALNUM(*ptr)) ptr++ ;
if (*ptr == 'h' || *ptr == 'H')
base = 16 ;
if (*ptr == 'o' || *ptr == 'O')
base = 8 ;
}
/* Calculate the number value */
while (ISNUM(*token_ptr) || (base > 10 && ISALNUM(*token_ptr)))
{
if (ISNUM(*token_ptr))
num = num * base + (*token_ptr++ - '0') ;
if (*token_ptr >= 'a' && *token_ptr <= 'f' && base > 10)
num = num * base + (*token_ptr++ - 'a' + 10) ;
if (*token_ptr >= 'A' && *token_ptr <= 'F' && base > 10)
num = num * base + (*token_ptr++ - 'A' + 10) ;
}
token.type = NUMBER ;
token.code = num ;
/* We have the integer part now - convert to int/float */
if ((*token_ptr) == '.' && base == 10)
{
token_ptr++ ;
if (!ISNUM(*token_ptr))
token_ptr-- ;
else
{
dec = 1.0 / (double)base ;
while (ISNUM(*token_ptr) || (base > 100 && ISALNUM(*token_ptr)))
{
if (ISNUM(*token_ptr))
num = num + dec * (*token_ptr++ - '0') ;
if (*token_ptr >= 'a' && *token_ptr <= 'f' && base > 10)
num = num + dec * (*token_ptr++ - 'a' + 10) ;
if (*token_ptr >= 'A' && *token_ptr <= 'F' && base > 10)
num = num + dec * (*token_ptr++ - 'A' + 10) ;
dec /= (double)base ;
}
token.code = num ;
}
}
/* Skip the base sufix */
if (base == 16 && *token_ptr == 'h')
token_ptr++;
if (base == 8 && *token_ptr == 'o')
token_ptr++;
if (base == 2 && *token_ptr == 'b')
token_ptr++;
_snprintf (token.name, sizeof(token.name), "%g", token.code) ;
return ;
}
if (*token_ptr == '"' || *token_ptr == '\'') /* Cadena */
{
char c = *token_ptr++ ;
token.type = STRING ;
ptr = token.name;
while (*token_ptr && *token_ptr != c)
*ptr++ = *token_ptr++ ;
if (*token_ptr == c) token_ptr++ ;
*ptr = 0 ;
return ;
}
ptr = token.name ;
*ptr++ = TOUPPER(*token_ptr) ;
if (ISWORDCHAR(*token_ptr++))
{
while (ISWORDCHAR(*token_ptr))
*ptr++ = TOUPPER(*token_ptr++) ;
}
*ptr = 0 ;
for (n = 0 ; n < dcb.data.NID ; n++)
{
if (strcmp(dcb.id[n].Name, token.name) == 0)
{
token.type = IDENTIFIER ;
token.code = dcb.id[n].Code ;
strcpy (token.name, dcb.id[n].Name) ;
return ;
}
}
token.type = OPERATOR ;
}
static DCB_TYPEDEF reduce_type (DCB_TYPEDEF orig)
{
int n ;
for (n = 0 ; n < MAX_TYPECHUNKS-1 ; n++)
{
orig.BaseType[n] = orig.BaseType[n+1] ;
orig.Count[n] = orig.Count[n+1] ;
}
return orig ;
}
static void var2const()
{
while (result.type == T_VARIABLE && result.var.Type.BaseType[0] == TYPE_ARRAY)
result.var.Type = reduce_type(result.var.Type) ;
if (result.type == T_VARIABLE && result.var.Type.BaseType[0] == TYPE_STRING)
{
result.type = T_STRING ;
strncpy (result.name, string_get(*(Sint32 *)(result.data)), 127) ;
result.name[127] = 0 ;
}
if (result.type == T_VARIABLE && result.var.Type.BaseType[0] == TYPE_FLOAT)
{
result.type = T_CONSTANT ;
result.value = *(float *)(result.data) ;
}
if (result.type == T_VARIABLE && (result.var.Type.BaseType[0] == TYPE_DWORD || result.var.Type.BaseType[0] == TYPE_INT))
{
result.type = T_CONSTANT ;
result.value = *(Sint32 *)(result.data) ;
}
if (result.type == T_VARIABLE && (result.var.Type.BaseType[0] == TYPE_WORD || result.var.Type.BaseType[0] == TYPE_SHORT))
{
result.type = T_CONSTANT ;
result.value = *(Sint16 *)(result.data) ;
}
if (result.type == T_VARIABLE && (result.var.Type.BaseType[0] == TYPE_BYTE || result.var.Type.BaseType[0] == TYPE_SBYTE))
{
result.type = T_CONSTANT ;
result.value = *(Sint8 *)(result.data) ;
}
if (result.type == T_VARIABLE && result.var.Type.BaseType[0] == TYPE_CHAR)
{
result.type = T_STRING ;
if (*(Uint8 *)result.data >= 32)
_snprintf (result.name, sizeof(result.name), "%c", *(Uint8 *)result.data) ;
else
_snprintf (result.name, sizeof(result.name), "\\x%02X", *(Uint8 *)result.data) ;
}
}
static int type_size (DCB_TYPEDEF orig)
{
unsigned int n, total ;
switch (orig.BaseType[0])
{
case TYPE_ARRAY:
return orig.Count[0] * type_size(reduce_type(orig)) ;
case TYPE_POINTER:
case TYPE_STRING:
case TYPE_DWORD:
case TYPE_FLOAT:
case TYPE_INT:
return 4 ;
case TYPE_WORD:
case TYPE_SHORT:
return 2 ;
case TYPE_BYTE:
case TYPE_SBYTE:
case TYPE_CHAR:
return 1 ;
case TYPE_STRUCT:
total = 0 ;
for (n = 0 ; n < dcb.varspace[orig.Members].NVars ; n++)
total += type_size (dcb.varspace_vars[orig.Members][n].Type) ;
return total ;
default:
return 0 ;
}
}
/* ---------------------------------------------------------------------- */
void eval_local (DCB_PROC * proc, INSTANCE * i)
{
unsigned int n ;
for (n = 0 ; n < dcb.data.NGloVars ; n++)
{
if (dcb.locvar[n].ID == token.code)
{
strcpy (result.name, token.name) ;
result.type = T_VARIABLE ;
result.var = dcb.locvar[n] ;
result.data = (Uint8*)i->locdata + dcb.locvar[n].Offset ;
get_token() ;
return ;
}
}
for (n = 0 ; n < proc->data.NPriVars ; n++)
{
if (proc->privar[n].ID == token.code)
{
strcpy (result.name, token.name) ;
result.type = T_VARIABLE ;
result.var = proc->privar[n] ;
result.data = (Uint8*)i->pridata + proc->privar[n].Offset ;
get_token() ;
return ;
}
}
for (n = 0 ; n < proc->data.NPubVars ; n++)
{
if (proc->pubvar[n].ID == token.code)
{
strcpy (result.name, token.name) ;
result.type = T_VARIABLE ;
result.var = proc->pubvar[n] ;
result.data = (Uint8*)i->pubdata + proc->pubvar[n].Offset ;
get_token() ;
return ;
}
}
gr_con_printf (_("Local or private or public variable not found")) ;
result.type = T_ERROR ;
}
void eval_immediate()
{
unsigned int n ;
if (token.type == NUMBER)
{
result.type = T_CONSTANT ;
result.value = token.code ;
get_token() ;
return ;
}
if (token.type == STRING)
{
result.type = T_STRING ;
_snprintf (result.name, sizeof(result.name), "%s", token.name) ;
get_token() ;
return ;
}
if (token.type != IDENTIFIER)
{
gr_con_printf (_("Not a valid expression")) ;
result.type = T_ERROR ;
return ;
}
if (token.name[0] == '(')
{
get_token() ;
eval_subexpression() ;
if (token.name[0] != ')')
{
gr_con_printf (_("Unbalanced parens")) ;
result.type = T_ERROR ;
return ;
}
get_token() ;
return ;
}
if (token.name[0] == '-')
{
get_token() ;
eval_immediate() ;
var2const() ;
if (result.type != T_CONSTANT)
{
result.type = T_ERROR ;
gr_con_printf (_("Operand is not a number\n")) ;
return ;
}
result.value = -result.value ;
_snprintf (result.name, sizeof(result.name), "%g", result.value) ;
return ;
}
for (n = 0 ; n < dcb.data.NGloVars ; n++)
{
if (dcb.glovar[n].ID == token.code)
{
strcpy (result.name, token.name) ;
result.type = T_VARIABLE ;
result.var = dcb.glovar[n] ;
result.data = (Uint8*)globaldata + dcb.glovar[n].Offset ;
get_token() ;
return ;
}
}
if (strcmp(token.name, "MAIN") == 0)
token.code = dcb.proc[0].data.ID ;
for (n = 0 ; n < dcb.data.NProcs ; n++)
{
if (dcb.proc[n].data.ID == token.code)
{
INSTANCE * i = first_instance ;
while (i) {
if (i->proc->type == (int)n) break ;
i = i->next ;
}
if (!i)
{
gr_con_printf (_("No instance of process %s is active"), token.name) ;
result.type = T_ERROR ;
return ;
}
get_token() ;
if (token.name[0] != '.')
{
result.type = T_ERROR ;
gr_con_printf (_("Invalid use of a process name")) ;
return ;
}
get_token() ;
eval_local(&dcb.proc[n], i) ;
return ;
}
}
gr_con_printf (_("Variable does not exist (%s)"), token.name) ;
result.type = T_ERROR ;
return ;
}
void eval_value()
{
eval_immediate() ;
if (result.type == T_ERROR) return ;
for (;;)
{
if (token.name[0] == '.')
{
DCB_VARSPACE * v ;
DCB_VAR * var ;
unsigned int n ;
var2const() ;
if (result.type == T_CONSTANT)
{
INSTANCE * i ;
i = instance_get((int)result.value) ;
if (!i)
{
result.type = T_ERROR ;
gr_con_printf (_("Instance %d does not exist\n"), (int)result.value) ;
return ;
}
get_token() ;
eval_local (&dcb.proc[i->proc->type], i) ;
continue;
}
if (result.type != T_VARIABLE
|| result.var.Type.BaseType[0] != TYPE_STRUCT)
{
gr_con_printf (_("%s is not an struct"), result.name);
result.type = T_ERROR ;
return ;
}
get_token() ;
if (token.type != IDENTIFIER)
{
gr_con_printf (_("%s is not a member"), token.name) ;
result.type = T_ERROR ;
return ;
}
v = &dcb.varspace[result.var.Type.Members] ;
var = dcb.varspace_vars[result.var.Type.Members] ;
for (n = 0 ; n < v->NVars ; n++)
{
if (var[n].ID == token.code)
break ;
}
if (n == v->NVars)
{
gr_con_printf (_("%s is not a member"), token.name) ;
result.type = T_ERROR ;
return ;
}
result.type = T_VARIABLE ;
result.var = var[n] ;
result.data = (Uint8 *)result.data + var[n].Offset ;
strcat (result.name, ".") ;
strcat (result.name, token.name) ;
get_token() ;
continue ;
}
if (token.name[0] == '[')
{
DCB_VAR i = result.var ;
void * i_data = result.data ;
char name[256] ;
if (result.type != T_VARIABLE || result.var.Type.BaseType[0] != TYPE_ARRAY)
{
gr_con_printf (_("%s is not an array"), result.name) ;
result.type = T_ERROR ;
return ;
}
strcpy (name, result.name) ;
get_token() ;
eval_subexpression() ;
if (result.type == T_ERROR) return ;
if (token.name[0] == ']') get_token() ;
var2const() ;
if (result.type != T_CONSTANT)
{
gr_con_printf (_("%s is not an integer"), result.name) ;
result.type = T_ERROR ;
return ;
}
if (result.value < 0)
{
gr_con_printf (_("Index (%d) less than zero"), result.value) ;
result.type = T_ERROR ;
return ;
}
if (result.value >= i.Type.Count[0])
{
gr_con_printf (_("Index (%d) out of bounds"), result.value) ;
result.type = T_ERROR ;
return ;
}
result.type = T_VARIABLE ;
result.var = i ;
result.var.Type = reduce_type(i.Type) ;
result.data = (Uint8 *)i_data + (int)result.value * type_size(result.var.Type) ;
_snprintf (result.name, sizeof(result.name), "%s[%d]", name, (int)result.value) ;
continue ;
}
break ;
}
}
void eval_factor()
{
double base = 1 ;
int op = 0 ;
for (;;)
{
eval_value() ;
if (result.type == T_ERROR) return ;
if (!strchr ("*/%", token.name[0]) && !op) return ;
var2const() ;
if (result.type != T_CONSTANT)
{
result.type = T_ERROR ;
gr_con_printf (_("Operand is not a number\n")) ;
return ;
}
if (!op) op = 1 ;
if (op > 1 && !result.value)
{
result.type = T_ERROR ;
gr_con_printf (_("Divide by zero\n")) ;
return ;
}
if (op == 1) base *= result.value ;
if (op == 2) base /= result.value ;
if (op == 3) base = (int)base % (int)result.value ;
if (!strchr ("*/%", token.name[0]))
{
result.type = T_CONSTANT ;
result.value = base ;
_snprintf (result.name, sizeof(result.name), "%g", base) ;
return ;
}
op = (token.name[0] == '*') ? 1: (token.name[0] == '/') ? 2:3 ;
get_token() ;
}
}
static void eval_subexpression()
{
double base = 0 ;
int op = 0 ;
for (;;)
{
eval_factor() ;
if (result.type == T_ERROR) return ;
if (token.name[0] != '+' && token.name[0] != '-' && !op) return ;
var2const() ;
if (result.type != T_CONSTANT)
{
result.type = T_ERROR ;
gr_con_printf (_("Operand is not a number\n")) ;
return ;
}
if (!op) op = 1 ;
base += op * result.value ;
if (token.name[0] != '+' && token.name[0] != '-')
{
result.type = T_CONSTANT ;
result.value = base ;
_snprintf (result.name, sizeof(result.name), "%g", base) ;
return ;
}
op = (token.name[0] == '+') ? 1:-1 ;
get_token() ;
}
}
static char * eval_expression(const char * here, int interactive)
{
static char buffer[1024];
static char part[1024];
while (*here == ' ') here++;
token_ptr = here ;
get_token() ;
eval_subexpression() ;
if (token.type != NOTOKEN && token.name[0] != ',' && token.name[0] != '=')
{
if (result.type != T_ERROR)
{
gr_con_printf (_("Invalid expression"));
result.type = T_ERROR;
}
return 0;
}
memset (part, 0, sizeof(buffer));
strncpy (part, here, token_ptr - here - ((token.type != NOTOKEN) ? 1:0));
if (result.type == T_CONSTANT)
{
_snprintf (buffer, 1024, "%s = %g\n", part, result.value);
if (interactive) gr_con_printf ("%s", buffer) ;
}
else if (result.type == T_STRING)
{
gr_con_printf ("%s = \"%s\"\n", part, result.name) ;
}
else if (result.type == T_VARIABLE)
{
lvalue = result ;
if (token.name[0] == '=')
{
if (lvalue.type != T_VARIABLE)
{
strcpy (buffer, _("Not an lvalue")) ;
if (interactive) gr_con_printf ("%s", buffer) ;
return buffer ;
}
get_token() ;
eval_subexpression() ;
if (result.type == T_ERROR) return "" ;
var2const() ;
if ((lvalue.var.Type.BaseType[0] == TYPE_DWORD || lvalue.var.Type.BaseType[0] == TYPE_INT) && result.type == T_CONSTANT)
*(Sint32 *)(lvalue.data) = (Sint32)result.value ;
else if ((lvalue.var.Type.BaseType[0] == TYPE_WORD || lvalue.var.Type.BaseType[0] == TYPE_SHORT) && result.type == T_CONSTANT)
*(Uint16 *)(lvalue.data) = (Uint16)result.value ;
else if ((lvalue.var.Type.BaseType[0] == TYPE_BYTE || lvalue.var.Type.BaseType[0] == TYPE_SBYTE) && result.type == T_CONSTANT)
*(Uint8 *)(lvalue.data) = (Uint8)result.value ;
else if (lvalue.var.Type.BaseType[0] == TYPE_CHAR && result.type == T_STRING) {
if (*result.name=='\\' && *(result.name+1)=='x')
*(Uint8 *)(lvalue.data) = (Uint8)strtol(result.name+2, NULL, 16);
else
*(Uint8 *)(lvalue.data) = (Uint8)*(result.name);
}
else if (lvalue.var.Type.BaseType[0] == TYPE_FLOAT && result.type == T_CONSTANT)
*(float *)(lvalue.data) = (float)result.value ;
else if (lvalue.var.Type.BaseType[0] == TYPE_STRING && result.type == T_STRING)
{
string_discard (*(Uint32 *) lvalue.data) ;
*(Uint32 *)(lvalue.data) = string_new (result.name) ;
string_use (*(Uint32 *) lvalue.data) ;
}
else
{
strcpy (buffer, _("Invalid assignation")) ;
if (interactive) gr_con_printf ("%s", buffer) ;
return buffer ;
}
}
if (interactive) {
show_var (lvalue.var, lvalue.name, lvalue.data, "", 0) ;
} else {
strcpy (buffer, part) ;
strcat (buffer, " ") ;
strcat (buffer, show_value (lvalue.var.Type, lvalue.data));
}
}
if (token.name[0] == ',')
{
char * temporary = strdup(buffer);
int size = strlen(temporary);
if (eval_expression (token_ptr, interactive) == 0)
{
free (temporary);
return 0;
}
if (strlen(buffer) + size < 1020 && !interactive)
{
memmove (buffer+size+2, buffer, strlen(buffer)+1);
memcpy (buffer, temporary, size);
memcpy (buffer+size, ", ", 2);
}
free (temporary);
return buffer;
}
return buffer;
}
/* ---------------------------------------------------------------------- */
extern void freestrings(PROCDEF * proc, int * data);
INSTANCE * findproc(INSTANCE * last, char * action, char * ptr)
{
INSTANCE * i = NULL;
char * aptr;
int procno;
int n;
if (*ptr)
{
if (*ptr >= '0' && *ptr <= '9')
{
if (last) return NULL;
procno = atoi(ptr);
for (i = first_instance ; i ; i = i->next)
if (LOCDWORD(i, PROCESS_ID) == procno)
break;
if (!i) {
gr_con_printf (_("Instance %d does not exist"), procno);
return NULL;
}
}
else
{
aptr = action;
while (ISWORDCHAR(*ptr)) {
*aptr++ = TOUPPER(*ptr); ptr++;
}
*aptr = 0;
for (n = 0 ; n < (int)dcb.data.NID ; n++)
if (strcmp(dcb.id[n].Name, action) == 0)
break;
for (procno = 0 ; procno < (int)dcb.data.NProcs ; procno++)
if (dcb.proc[procno].data.ID == dcb.id[n].Code)
break;
if (procno == (int)dcb.data.NProcs) {
gr_con_printf (_("Unknown process %s"), action);
return NULL;
}
for (i = last ? last->next : first_instance ; i ; i = i->next)
if (i->proc->type == procno)
break;
if (!i && !last) {
gr_con_printf (_("No instance of process %s created\n"), action);
return NULL;
}
}
}
return (i);
}
void gr_con_do (const char * command)
{
const char * ptr ;
char * aptr ;
char action[256] ;
unsigned int var ;
int n ;
int procno ;
PROCDEF * p = NULL ;
INSTANCE * i = NULL ;
ptr = command ;
while (*ptr && *ptr != ' ') ptr++ ;
strncpy (action, command, ptr - command) ;
action[ptr - command] = 0 ;
while (*ptr == ' ') ptr++ ;
aptr = action ;
while (*aptr) {
*aptr = TOUPPER(*aptr);
aptr++;
}
/* Comandos */
if (strcmp (action, "HELP") == 0)
{
gr_con_printf ( HELPTXT );
return;
}
if (strcmp (action, "CONTINUE") == 0)
{
show_console = 0;
force_debug = 0;
debug_next = 0;
return ;
}
if (strcmp (action, "DOFRAME") == 0)
{
show_console = 0;
debug_on_frame = 1;
force_debug = 0;
debug_next = 0;
return ;
}
if (strcmp (action, "TRACE") == 0)
{
show_console = 0;
debug_on_frame = 0;
force_debug = 0;
debug_next = 1;
return ;
}
if (strcmp (action, "BREAK") == 0)
{
if (*ptr) {
if (*ptr >= '0' && *ptr <= '9')
{
procno = atoi(ptr);
for (i = first_instance ; i ; i = i->next)
if (LOCDWORD(i, PROCESS_ID) == procno)
break;
if (!i) {
gr_con_printf (_("Instance %d does not exist"), procno);
} else {
i->breakpoint = 1;
gr_con_printf (_("Ok"));
}
}
else
{
aptr = action;
while (ISWORDCHAR(*ptr)) {
*aptr++ = TOUPPER(*ptr); ptr++;
}
*aptr = 0;
if (*action) {
p = procdef_get_by_name(action);
if (!p) {
gr_con_printf (_("Process type %d does not exist"), procno);
} else {
p->breakpoint = 1;
gr_con_printf (_("Ok"));
}
}
}
} else {
int f=0;
for (n = 0 ; n < procdef_count; n++) {
if (procs[n].breakpoint) {
if (!f) {
gr_con_printf(_("02Process type breakpoints07\n"));
f = 1;
}
gr_con_printf("%s\n", procs[n].name);
}
}
if (f)
gr_con_printf("\n");
f = 0;
for (i = first_instance ; i ; i = i->next) {
if (i->breakpoint) {
if (!f) {
gr_con_printf(_("02Process breakpoints07\n"));
f = 1;
}
gr_con_printf("%d\n", LOCDWORD(i, PROCESS_ID));
}
}
if (f)
gr_con_printf("\n");
}
return ;
}
if (strcmp (action, "DELETE") == 0)
{
if (*ptr) {
if (*ptr >= '0' && *ptr <= '9')
{
procno = atoi(ptr);
for (i = first_instance ; i ; i = i->next)
if (LOCDWORD(i, PROCESS_ID) == procno)
break;
if (!i) {
gr_con_printf (_("Instance %d does not exist"), procno);
} else {
i->breakpoint = 0;
gr_con_printf (_("Ok"));
}
}
else
{
aptr = action;
while (ISWORDCHAR(*ptr)) {
*aptr++ = TOUPPER(*ptr); ptr++;
}
*aptr = 0;
p = procdef_get_by_name(action);
if (!p) {
gr_con_printf (_("Process type %d does not exist"), procno);
} else {
p->breakpoint = 0;
gr_con_printf (_("Ok"));
}
}
}
return ;
}
if (strcmp (action, "STRINGS") == 0)
{
string_dump() ;
return ;
}
if (strcmp (action, "INSTANCES") == 0)
{
instance_dump_all() ;
return ;
}
if (strcmp (action, "GLOBALS") == 0)
{
for (var = 0 ; var < dcb.data.NGloVars ; var++)
{
DCB_VAR * v = &dcb.glovar[var] ;
show_var (*v, 0, (Uint8*)globaldata + v->Offset, "[GLOBAL]", 0) ;
}
return ;
}
if (strcmp (action, "LOCALS") == 0 ||
strcmp (action, "PRIVATES") == 0 ||
strcmp (action, "PUBLICS") == 0)
{
int show_locals = action[0] == 'L';
int show_public = action[0] == 'P' && action[1] == 'U' ;
i = findproc(NULL, action, ptr);
if (show_locals) {
for (var = 0 ; var < dcb.data.NLocVars ; var++)
{
DCB_VAR * v = &dcb.locvar[var] ;
show_var (*v, 0, i ? (char*)i->locdata + v->Offset : 0, "[LOCAL]", 0) ;
}
} else if (show_public) {
if (!i) {
gr_con_printf (_("Usage: PUBLICS process"));
return;
}
for (var = 0 ; var < dcb.proc[i->proc->type].data.NPubVars ; var++)
{
DCB_VAR * v = &dcb.proc[i->proc->type].pubvar[var] ;
/* Unnamed private vars are temporary params and loop
counters, and are ignored by this command */
if ((int)v->ID >= 0) {
show_var (*v, 0, (char*)i->pubdata + v->Offset, "[PUBLIC]", 0) ;
}
}
} else {
if (!i) {
gr_con_printf (_("Usage: PRIVATES process"));
return;
}
for (var = 0 ; var < dcb.proc[i->proc->type].data.NPriVars ; var++)
{
DCB_VAR * v = &dcb.proc[i->proc->type].privar[var] ;
/* Unnamed private vars are temporary params and loop
counters, and are ignored by this command */
if ((int)v->ID >= 0) {
show_var (*v, 0, (char*)i->pridata + v->Offset, "[PRIVATE]", 0) ;
}
}
}
return ;
}
if (strcmp (action, "SHOW") == 0)
{
if (show_expression) free(show_expression);
show_expression = *ptr ? strdup(ptr) : 0;
return;
}
if (strcmp (action, "VARS") == 0)
{
for (var = 0 ; var < N_CONSOLE_VARS ; var++)
{
switch (console_vars[var].type)
{
case CON_DWORD:
gr_con_printf ("[FXI] %s = %d\n", console_vars[var].name, *(int *)console_vars[var].value) ;
break;
case CON_DWORD_HEX:
gr_con_printf ("[FXI] %s = %08Xh\n", console_vars[var].name, *(int *)console_vars[var].value) ;
break;
}
}
return ;
}
if (strcmp (action, "RUN") == 0)
{
if (*ptr) {
aptr = action;
while (ISWORDCHAR(*ptr)) {
*aptr++ = TOUPPER(*ptr); ptr++;
}
*aptr = 0;
if (*action) {
int i;
INSTANCE * inst ;
p = procdef_get_by_name(action);
if (p) {
token_ptr = ptr ;
gr_con_printf ("%s\n", ptr );
inst = instance_new (p, NULL);
for ( i=0; i < p->params; i++ ){
int type = dcb.proc[p->type].privar[i].Type.BaseType[0];
get_token() ;
eval_subexpression() ;
if (result.type == T_VARIABLE)
var2const();
switch(result.type){
case T_CONSTANT:
switch(type){
case TYPE_FLOAT:
PRIDWORD(inst, 4*i) = *(int *)&result.value ;
break;
case TYPE_INT:
case TYPE_DWORD:
case TYPE_POINTER:
case TYPE_SHORT:
case TYPE_WORD:
case TYPE_BYTE:
case TYPE_SBYTE:
case TYPE_CHAR:
PRIDWORD(inst, 4*i) = (int) result.value ;
break;
case TYPE_STRING:
default:
freestrings(p, inst->pridata);
instance_destroy(inst);
gr_con_printf(_("Invalid argument %d\n"), i);
return;
}
break;
case T_STRING:
PRIDWORD(inst, 4*i) = (int) string_new(result.name) ;
string_use(PRIDWORD(inst, 4*i));
break;
case T_VARIABLE:
default:
freestrings(p, inst->pridata);
instance_destroy(inst);
gr_con_printf(_("Invalid argument %d\n"), i);
return;
}
}
gr_con_printf(_("Process %s is executed!\n"), p->name );
} else {
gr_con_printf(_("Process %s not found!\n"), p->name );
}
return;
}
}
}
if (strcmp (action, "KILLALL") == 0 ||
strcmp (action, "SLEEPALL") == 0 ||
strcmp (action, "WAKEUPALL") == 0 ||
strcmp (action, "FREEZEALL") == 0 )
{
char act = *action;
int found = 0;
char * oaction = strdup(action);
char * optr = ptr;
i = NULL;
while ((i = findproc(i, action, ptr))) {
found = 1;
switch (act) {
case 'K':
LOCDWORD(i, STATUS) = (LOCDWORD(i, STATUS) & STATUS_WAITING_MASK) | STATUS_KILLED ;
break;
case 'W':
LOCDWORD(i, STATUS) = (LOCDWORD(i, STATUS) & STATUS_WAITING_MASK) | STATUS_RUNNING ;
break ;
case 'S':
LOCDWORD(i, STATUS) = (LOCDWORD(i, STATUS) & STATUS_WAITING_MASK) | STATUS_SLEEPING ;
break ;
case 'F':
LOCDWORD(i, STATUS) = (LOCDWORD(i, STATUS) & STATUS_WAITING_MASK) | STATUS_FROZEN ;
break;
}
strcpy(action, oaction);
ptr = optr;
}
if(oaction) free(oaction);
if(found) gr_con_printf ("Ok\n");
return ;
}
if (strcmp (action, "KILL") == 0 ||
strcmp (action, "SLEEP") == 0 ||
strcmp (action, "WAKEUP") == 0 ||
strcmp (action, "FREEZE") == 0 )
{
char act=*action;
i = findproc(NULL, action, ptr);
if (!i) return;
switch (act) {
case 'K':
LOCDWORD(i, STATUS) = (LOCDWORD(i, STATUS) & STATUS_WAITING_MASK) | STATUS_KILLED ;
break;
case 'W':
LOCDWORD(i, STATUS) = (LOCDWORD(i, STATUS) & STATUS_WAITING_MASK) | STATUS_RUNNING ;
break ;
case 'S':
LOCDWORD(i, STATUS) = (LOCDWORD(i, STATUS) & STATUS_WAITING_MASK) | STATUS_SLEEPING ;
break ;
case 'F':
LOCDWORD(i, STATUS) = (LOCDWORD(i, STATUS) & STATUS_WAITING_MASK) | STATUS_FROZEN ;
break;
}
gr_con_printf ("Ok\n");
return ;
}
if (strcmp (action, "QUIT") == 0)
{
do_exit(1) ;
return ;
}
/* Variables FXI */
for (var = 0 ; var < N_CONSOLE_VARS ; var++)
{
if (strcmp(console_vars[var].name, action) == 0)
{
switch (console_vars[var].type)
{
case CON_DWORD:
if (*ptr)
{
while (*ptr == '=' || *ptr == ' ') ptr++;
eval_expression (ptr, 0);
if (result.type != T_ERROR)
*(int *)console_vars[var].value = (int) result.value ;
}
gr_con_printf ("[FXI] %s = %d", console_vars[var].name, *(int *)console_vars[var].value) ;
return ;
case CON_DWORD_HEX:
if (*ptr)
{
while (*ptr == '=' || *ptr == ' ') ptr++;
eval_expression (ptr, 0);
if (result.type != T_ERROR)
*(int *)console_vars[var].value = (int) result.value ;
}
gr_con_printf ("[FXI] %s = %08Xh\n", console_vars[var].name, *(int *)console_vars[var].value) ;
return ;
}
}
}
/* Expresiones */
eval_expression (command, 1) ;
}
/* ---------------------------------------------------------------------- */
|