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
|
/* q2c - parser generator for PSPP procedures.
Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
Written by Ben Pfaff <blp@gnu.org>.
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., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA. */
#include <config.h>
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <errno.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "str.h"
/* Brokenness. */
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif
#if !HAVE_STRERROR
#include "misc/strerror.c"
#endif
#include "debug-print.h"
/* Max length of an input line. */
#define MAX_LINE_LEN 1024
/* Max token length. */
#define MAX_TOK_LEN 1024
/* argv[0]. */
char *pgmname;
/* Have the input and output files been opened yet? */
int is_open;
/* Input, output files. */
FILE *in, *out;
/* Input, output file names. */
char *ifn, *ofn;
/* Input, output file line number. */
int ln, oln = 1;
/* Input line buffer, current position. */
char *buf, *cp;
/* Token types. */
enum
{
T_STRING = 256, /* String literal. */
T_ID = 257 /* Identifier. */
};
/* Current token: either one of the above, or a single character. */
int token;
/* Token string value. */
char *tokstr;
/* Utility functions. */
char nullstr[] = "";
/* Close all open files and delete the output file, on failure. */
static void
finish_up (void)
{
if (!is_open)
return;
is_open = 0;
fclose (in);
fclose (out);
if (remove (ofn) == -1)
fprintf (stderr, "%s: %s: remove: %s\n", pgmname, ofn, strerror (errno));
}
void hcf (void) NO_RETURN;
/* Terminate unsuccessfully. */
void
hcf (void)
{
finish_up ();
exit (EXIT_FAILURE);
}
int fail (const char *, ...) PRINTF_FORMAT (1, 2);
int error (const char *, ...) PRINTF_FORMAT (1, 2);
/* Output an error message and terminate unsuccessfully. */
int
fail (const char *format, ...)
{
va_list args;
va_start (args, format);
fprintf (stderr, "%s: ", pgmname);
vfprintf (stderr, format, args);
fprintf (stderr, "\n");
va_end (args);
hcf ();
}
/* Output a context-dependent error message and terminate
unsuccessfully. */
int
error (const char *format,...)
{
va_list args;
va_start (args, format);
fprintf (stderr, "%s:%d: (column %d) ", ifn, ln, (int) (cp - buf));
vfprintf (stderr, format, args);
fprintf (stderr, "\n");
va_end (args);
hcf ();
}
#define VME "virtual memory exhausted"
/* Allocate a block of SIZE bytes and return a pointer to its
beginning. */
static void *
xmalloc (size_t size)
{
void *vp;
if (size == 0)
return NULL;
vp = malloc (size);
if (!vp)
fail ("xmalloc(%lu): %s", (unsigned long) size, VME);
return vp;
}
/* Make a dynamically allocated copy of string S and return a pointer
to the first character. */
static char *
xstrdup (const char *s)
{
size_t size;
char *t;
assert (s != NULL);
size = strlen (s) + 1;
t = malloc (size);
if (!t)
fail ("xstrdup(%lu): %s", (unsigned long) strlen (s), VME);
memcpy (t, s, size);
return t;
}
/* Returns a pointer to one of 8 static buffers. The buffers are used
in rotation. */
static char *
get_buffer (void)
{
static char b[8][256];
static int cb;
if (++cb >= 8)
cb = 0;
return b[cb];
}
/* Copies a string to a static buffer, converting it to lowercase in
the process, and returns a pointer to the static buffer. */
static char *
st_lower (const char *s)
{
char *p, *cp;
p = cp = get_buffer ();
while (*s)
*cp++ = tolower ((unsigned char) (*s++));
*cp++ = '\0';
return p;
}
/* Copies a string to a static buffer, converting it to uppercase in
the process, and returns a pointer to the static buffer. */
static char *
st_upper (const char *s)
{
char *p, *cp;
p = cp = get_buffer ();
while (*s)
*cp++ = toupper ((unsigned char) (*s++));
*cp++ = '\0';
return p;
}
/* Returns the address of the first non-whitespace character in S, or
the address of the null terminator if none. */
static char *
skip_ws (const char *s)
{
while (isspace ((unsigned char) *s))
s++;
return (char *) s;
}
/* Read one line from the input file into buf. Lines having special
formats are handled specially. */
static int
get_line (void)
{
ln++;
if (0 == fgets (buf, MAX_LINE_LEN, in))
{
if (ferror (in))
fail ("%s: fgets: %s", ifn, strerror (errno));
return 0;
}
cp = strchr (buf, '\n');
if (cp != NULL)
*cp = '\0';
cp = buf;
return 1;
}
/* Symbol table manager. */
/* Symbol table entry. */
typedef struct symbol symbol;
struct symbol
{
symbol *next; /* Next symbol in symbol table. */
char *name; /* Symbol name. */
int unique; /* 1=Name must be unique in this file. */
int ln; /* Line number of definition. */
int value; /* Symbol value. */
};
/* Symbol table. */
symbol *symtab;
/* Add a symbol to the symbol table having name NAME, uniqueness
UNIQUE, and value VALUE. If a symbol having the same name is found
in the symbol table, its sequence number is returned and the symbol
table is not modified. Otherwise, the symbol is added and the next
available sequence number is returned. */
static int
add_symbol (const char *name, int unique, int value)
{
symbol *iter, *sym;
int x;
sym = xmalloc (sizeof (symbol));
sym->name = xstrdup (name);
sym->unique = unique;
sym->value = value;
sym->next = NULL;
sym->ln = ln;
if (!symtab)
{
symtab = sym;
return 1;
}
iter = symtab;
x = 1;
for (;;)
{
if (!strcmp (iter->name, name))
{
if (iter->unique)
{
fprintf (stderr, "%s:%d: `%s' is already defined above\n", ifn,
ln, name);
fprintf (stderr, "%s:%d: location of previous definition\n", ifn,
iter->ln);
hcf ();
}
free (sym->name);
free (sym);
return x;
}
if (!iter->next)
break;
iter = iter->next;
x++;
}
iter->next = sym;
return ++x;
}
/* Finds the symbol having given sequence number X within the symbol
table, and returns the associated symbol structure. */
static symbol *
find_symbol (int x)
{
symbol *iter;
iter = symtab;
while (x > 1 && iter)
{
iter = iter->next;
x--;
}
assert (iter);
return iter;
}
#if DEBUGGING
/* Writes a printable representation of the current token to
stdout. */
static void
dump_token (void)
{
switch (token)
{
case T_STRING:
printf ("STRING\t\"%s\"\n", tokstr);
break;
case T_ID:
printf ("ID\t%s\n", tokstr);
break;
default:
printf ("PUNCT\t%c\n", token);
}
}
#endif /* DEBUGGING */
/* Reads a token from the input file. */
static int
lex_get (void)
{
/* Skip whitespace and check for end of file. */
for (;;)
{
cp = skip_ws (cp);
if (*cp != '\0')
break;
if (!get_line ())
fail ("%s: Unexpected end of file.", ifn);
}
if (*cp == '"')
{
char *dest = tokstr;
token = T_STRING;
cp++;
while (*cp != '"' && *cp)
{
if (*cp == '\\')
{
cp++;
if (!*cp)
error ("Unterminated string literal.");
*dest++ = *cp++;
}
else
*dest++ = *cp++;
}
*dest++ = 0;
if (!*cp)
error ("Unterminated string literal.");
cp++;
}
else if (*cp == '_' || isalnum ((unsigned char) *cp))
{
char *dest = tokstr;
token = T_ID;
while (*cp == '_' || isalnum ((unsigned char) *cp))
*dest++ = toupper ((unsigned char) (*cp++));
*dest++ = '\0';
}
else
token = *cp++;
#if DEBUGGING
dump_token ();
#endif
return token;
}
/* Force the current token to be an identifier token. */
static void
force_id (void)
{
if (token != T_ID)
error ("Identifier expected.");
}
/* Force the current token to be a string token. */
static void
force_string (void)
{
if (token != T_STRING)
error ("String expected.");
}
/* Checks whether the current token is the identifier S; if so, skips
the token and returns 1; otherwise, returns 0. */
static int
match_id (const char *s)
{
if (token == T_ID && !strcmp (tokstr, s))
{
lex_get ();
return 1;
}
return 0;
}
/* Checks whether the current token is T. If so, skips the token and
returns 1; otherwise, returns 0. */
static int
match_token (int t)
{
if (token == t)
{
lex_get ();
return 1;
}
return 0;
}
/* Force the current token to be T, and skip it. */
static void
skip_token (int t)
{
if (token != t)
error ("`%c' expected.", t);
lex_get ();
}
/* Structures. */
/* Some specifiers have associated values. */
enum
{
VAL_NONE, /* No value. */
VAL_INT, /* Integer value. */
VAL_DBL /* Floating point value. */
};
/* For those specifiers with values, the syntax of those values. */
enum
{
VT_PLAIN, /* Unadorned value. */
VT_PAREN /* Value must be enclosed in parentheses. */
};
/* Forward definition. */
typedef struct specifier specifier;
/* A single setting. */
typedef struct setting setting;
struct setting
{
specifier *parent; /* Owning specifier. */
setting *next; /* Next in the chain. */
char *specname; /* Name of the setting. */
int con; /* Sequence number. */
/* Values. */
int valtype; /* One of VT_*. */
int value; /* One of VAL_*. */
int optvalue; /* 1=value is optional, 0=value is required. */
char *valname; /* Variable name for the value. */
char *restriction; /* !=NULL: expression specifying valid values. */
};
/* A single specifier. */
struct specifier
{
specifier *next; /* Next in the chain. */
char *varname; /* Variable name. */
setting *s; /* Associated settings. */
setting *def; /* Default setting. */
setting *omit_kw; /* Setting for which the keyword can be omitted. */
int index; /* Next array index. */
};
/* Subcommand types. */
typedef enum
{
SBC_PLAIN, /* The usual case. */
SBC_VARLIST, /* Variable list. */
SBC_INT, /* Integer value. */
SBC_PINT, /* Integer inside parentheses. */
SBC_DBL, /* Floating point value. */
SBC_INT_LIST, /* List of integers (?). */
SBC_DBL_LIST, /* List of floating points (?). */
SBC_CUSTOM, /* Custom. */
SBC_ARRAY, /* Array of boolean values. */
SBC_STRING, /* String value. */
SBC_VAR /* Single variable name. */
}
subcommand_type;
typedef enum
{
ARITY_ONCE_EXACTLY, /* must occur exactly once */
ARITY_ONCE_ONLY, /* zero or once */
ARITY_MANY /* 0, 1, ... , inf */
}subcommand_arity;
/* A single subcommand. */
typedef struct subcommand subcommand;
struct subcommand
{
subcommand *next; /* Next in the chain. */
char *name; /* Subcommand name. */
subcommand_type type; /* One of SBC_*. */
subcommand_arity arity; /* How many times should the subcommand occur*/
int narray; /* Index of next array element. */
const char *prefix; /* Prefix for variable and constant names. */
specifier *spec; /* Array of specifiers. */
/* SBC_STRING and SBC_INT only. */
char *restriction; /* Expression restricting string length. */
char *message; /* Error message. */
int translatable; /* Error message is translatable */
};
typedef struct aux_subcommand aux_subcommand;
struct aux_subcommand
{
aux_subcommand *next; /* Next in the chain. */
char *name; /* Subcommand name. */
char *value; /* Subcommand value */
};
static aux_subcommand *aux_subcommands ;
/* Name of the command; i.e., DESCRIPTIVES. */
char *cmdname;
/* Short prefix for the command; i.e., `dsc_'. */
char *prefix;
/* List of subcommands. */
subcommand *subcommands;
/* Default subcommand if any, or NULL. */
subcommand *def;
/* Parsing. */
void parse_subcommands (void);
/* Parse an entire specification. */
static void
parse (void)
{
/* Get the command name and prefix. */
if (token != T_STRING && token != T_ID)
error ("Command name expected.");
cmdname = xstrdup (tokstr);
lex_get ();
skip_token ('(');
force_id ();
prefix = xstrdup (tokstr);
lex_get ();
skip_token (')');
skip_token (':');
/* Read all the subcommands. */
subcommands = NULL;
def = NULL;
parse_subcommands ();
}
/* Parses a single setting into S, given subcommand information SBC
and specifier information SPEC. */
static void
parse_setting (setting *s, specifier *spec)
{
s->parent = spec;
if (match_token ('*'))
{
if (spec->omit_kw)
error ("Cannot have two settings with omittable keywords.");
else
spec->omit_kw = s;
}
if (match_token ('!'))
{
if (spec->def)
error ("Cannot have two default settings.");
else
spec->def = s;
}
force_id ();
s->specname = xstrdup (tokstr);
s->con = add_symbol (s->specname, 0, 0);
s->value = VAL_NONE;
lex_get ();
/* Parse setting value info if necessary. */
if (token != '/' && token != ';' && token != '.' && token != ',')
{
if (token == '(')
{
s->valtype = VT_PAREN;
lex_get ();
}
else
s->valtype = VT_PLAIN;
s->optvalue = match_token ('*');
if (match_id ("N"))
s->value = VAL_INT;
else if (match_id ("D"))
s->value = VAL_DBL;
else
error ("`n' or `d' expected.");
skip_token (':');
force_id ();
s->valname = xstrdup (tokstr);
lex_get ();
if (token == ',')
{
lex_get ();
force_string ();
s->restriction = xstrdup (tokstr);
lex_get ();
}
else
s->restriction = NULL;
if (s->valtype == VT_PAREN)
skip_token (')');
}
}
/* Parse a single specifier into SPEC, given subcommand information
SBC. */
static void
parse_specifier (specifier *spec, subcommand *sbc)
{
spec->index = 0;
spec->s = NULL;
spec->def = NULL;
spec->omit_kw = NULL;
spec->varname = NULL;
if (token == T_ID)
{
spec->varname = xstrdup (st_lower (tokstr));
lex_get ();
}
/* Handle array elements. */
if (token != ':')
{
spec->index = sbc->narray;
if (sbc->type == SBC_ARRAY)
{
if (token == '|')
token = ',';
else
sbc->narray++;
}
spec->s = NULL;
return;
}
skip_token (':');
if ( sbc->type == SBC_ARRAY && token == T_ID )
{
spec->varname = xstrdup (st_lower (tokstr));
spec->index = sbc->narray;
sbc->narray++;
}
/* Parse all the settings. */
{
setting **s = &spec->s;
for (;;)
{
*s = xmalloc (sizeof (setting));
parse_setting (*s, spec);
if (token == ',' || token == ';' || token == '.')
break;
skip_token ('/');
s = &(*s)->next;
}
(*s)->next = NULL;
}
}
/* Parse a list of specifiers for subcommand SBC. */
static void
parse_specifiers (subcommand *sbc)
{
specifier **spec = &sbc->spec;
if (token == ';' || token == '.')
{
*spec = NULL;
return;
}
for (;;)
{
*spec = xmalloc (sizeof (specifier));
parse_specifier (*spec, sbc);
if (token == ';' || token == '.')
break;
skip_token (',');
spec = &(*spec)->next;
}
(*spec)->next = NULL;
}
/* Parse a subcommand into SBC. */
static void
parse_subcommand (subcommand *sbc)
{
sbc->arity = ARITY_MANY;
if (match_token ('*'))
{
if (def)
error ("Multiple default subcommands.");
def = sbc;
}
if ( match_token('+'))
sbc->arity = ARITY_ONCE_ONLY ;
else if (match_token('^'))
sbc->arity = ARITY_ONCE_EXACTLY ;
force_id ();
sbc->name = xstrdup (tokstr);
lex_get ();
sbc->narray = 0;
sbc->type = SBC_PLAIN;
sbc->spec = NULL;
sbc->translatable = 0;
if (match_token ('['))
{
force_id ();
sbc->prefix = xstrdup (st_lower (tokstr));
lex_get ();
skip_token (']');
skip_token ('=');
sbc->type = SBC_ARRAY;
parse_specifiers (sbc);
}
else
{
if (match_token ('('))
{
force_id ();
sbc->prefix = xstrdup (st_lower (tokstr));
lex_get ();
skip_token (')');
}
else
sbc->prefix = "";
skip_token ('=');
if (match_id ("VAR"))
sbc->type = SBC_VAR;
if (match_id ("VARLIST"))
{
if (match_token ('('))
{
force_string ();
sbc->message = xstrdup (tokstr);
lex_get();
skip_token (')');
}
else sbc->message = NULL;
sbc->type = SBC_VARLIST;
}
else if (match_id ("INTEGER"))
{
sbc->type = match_id ("LIST") ? SBC_INT_LIST : SBC_INT;
if ( token == T_STRING)
{
sbc->restriction = xstrdup (tokstr);
lex_get ();
if ( match_id("N_") )
{
skip_token('(');
force_string ();
lex_get();
skip_token(')');
sbc->translatable = 1;
}
else {
force_string ();
lex_get ();
}
sbc->message = xstrdup (tokstr);
}
else
sbc->restriction = NULL;
}
else if (match_id ("PINT"))
sbc->type = SBC_PINT;
else if (match_id ("DOUBLE"))
{
if ( match_id ("LIST") )
sbc->type = SBC_DBL_LIST;
else
sbc->type = SBC_DBL;
}
else if (match_id ("STRING"))
{
sbc->type = SBC_STRING;
if (token == T_STRING)
{
sbc->restriction = xstrdup (tokstr);
lex_get ();
force_string ();
sbc->message = xstrdup (tokstr);
lex_get ();
}
else
sbc->restriction = NULL;
}
else if (match_id ("CUSTOM"))
sbc->type = SBC_CUSTOM;
else
parse_specifiers (sbc);
}
}
/* Parse all the subcommands. */
void
parse_subcommands (void)
{
subcommand **sbc = &subcommands;
for (;;)
{
*sbc = xmalloc (sizeof (subcommand));
(*sbc)->next = NULL;
parse_subcommand (*sbc);
if (token == '.')
return;
skip_token (';');
sbc = &(*sbc)->next;
}
}
/* Output. */
#define BASE_INDENT 2 /* Starting indent. */
#define INC_INDENT 2 /* Indent increment. */
/* Increment the indent. */
#define indent() indent += INC_INDENT
#define outdent() indent -= INC_INDENT
/* Size of the indent from the left margin. */
int indent;
void dump (int, const char *, ...) PRINTF_FORMAT (2, 3);
/* Write line FORMAT to the output file, formatted as with printf,
indented `indent' characters from the left margin. If INDENTION is
greater than 0, indents BASE_INDENT * INDENTION characters after
writing the line; if INDENTION is less than 0, dedents BASE_INDENT
* INDENTION characters _before_ writing the line. */
void
dump (int indention, const char *format, ...)
{
va_list args;
int i;
if (indention < 0)
indent += BASE_INDENT * indention;
oln++;
va_start (args, format);
for (i = 0; i < indent; i++)
putc (' ', out);
vfprintf (out, format, args);
putc ('\n', out);
va_end (args);
if (indention > 0)
indent += BASE_INDENT * indention;
}
/* Write the structure members for specifier SPEC to the output file.
SBC is the including subcommand. */
static void
dump_specifier_vars (const specifier *spec, const subcommand *sbc)
{
if (spec->varname)
dump (0, "long %s%s;", sbc->prefix, spec->varname);
{
setting *s;
for (s = spec->s; s; s = s->next)
{
if (s->value != VAL_NONE)
{
const char *typename;
assert (s->value == VAL_INT || s->value == VAL_DBL);
typename = s->value == VAL_INT ? "long" : "double";
dump (0, "%s %s%s;", typename, sbc->prefix, st_lower (s->valname));
}
}
}
}
/* Returns 1 if string T is a PSPP keyword, 0 otherwise. */
static int
is_keyword (const char *t)
{
static const char *kw[] =
{
"AND", "OR", "NOT", "EQ", "GE", "GT", "LE", "LT",
"NE", "ALL", "BY", "TO", "WITH", 0,
};
const char **cp;
for (cp = kw; *cp; cp++)
if (!strcmp (t, *cp))
return 1;
return 0;
}
/* Transforms a string NAME into a valid C identifier: makes
everything lowercase and maps nonalphabetic characters to
underscores. Returns a pointer to a static buffer. */
static char *
make_identifier (const char *name)
{
char *p = get_buffer ();
char *cp;
for (cp = p; *name; name++)
if (isalpha ((unsigned char) *name))
*cp++ = tolower ((unsigned char) (*name));
else
*cp++ = '_';
*cp = '\0';
return p;
}
/* Writes the struct and enum declarations for the parser. */
static void
dump_declarations (void)
{
indent = 0;
/* Write out enums for all the identifiers in the symbol table. */
{
int f, k;
symbol *sym;
char *buf = NULL;
/* Note the squirmings necessary to make sure that the last enum
is not followed by a comma, as mandated by ANSI C89. */
for (sym = symtab, f = k = 0; sym; sym = sym->next)
if (!sym->unique && !is_keyword (sym->name))
{
if (!f)
{
dump (0, "/* Settings for subcommand specifiers. */");
dump (1, "enum");
dump (1, "{");
f = 1;
}
if (buf == NULL)
buf = xmalloc (1024);
else
dump (0, buf);
if (k)
sprintf (buf, "%s%s,", st_upper (prefix), sym->name);
else
{
k = 1;
sprintf (buf, "%s%s = 1000,", st_upper (prefix), sym->name);
}
}
if (buf)
{
buf[strlen (buf) - 1] = 0;
dump (0, buf);
free (buf);
}
if (f)
{
dump (-1, "};");
dump (-1, nullstr);
}
}
/* Write out some type definitions */
{
dump (0, "#define MAXLISTS 10");
}
/* For every array subcommand, write out the associated enumerated
values. */
{
subcommand *sbc;
for (sbc = subcommands; sbc; sbc = sbc->next)
if (sbc->type == SBC_ARRAY && sbc->narray)
{
dump (0, "/* Array indices for %s subcommand. */", sbc->name);
dump (1, "enum");
dump (1, "{");
{
specifier *spec;
for (spec = sbc->spec; spec; spec = spec->next)
dump (0, "%s%s%s = %d,",
st_upper (prefix), st_upper (sbc->prefix),
st_upper (spec->varname), spec->index);
dump (0, "%s%scount", st_upper (prefix), st_upper (sbc->prefix));
dump (-1, "};");
dump (-1, nullstr);
}
}
}
/* Write out structure declaration. */
{
subcommand *sbc;
dump (0, "/* %s structure. */", cmdname);
dump (1, "struct cmd_%s", make_identifier (cmdname));
dump (1, "{");
for (sbc = subcommands; sbc; sbc = sbc->next)
{
int f = 0;
if (sbc != subcommands)
dump (0, nullstr);
dump (0, "/* %s subcommand. */", sbc->name);
dump (0, "int sbc_%s;", st_lower (sbc->name));
switch (sbc->type)
{
case SBC_ARRAY:
case SBC_PLAIN:
{
specifier *spec;
for (spec = sbc->spec; spec; spec = spec->next)
{
if (spec->s == 0)
{
if (sbc->type == SBC_PLAIN)
dump (0, "long int %s%s;", st_lower (sbc->prefix),
spec->varname);
else if (f == 0)
{
dump (0, "int a_%s[%s%scount];",
st_lower (sbc->name),
st_upper (prefix),
st_upper (sbc->prefix)
);
f = 1;
}
}
else
dump_specifier_vars (spec, sbc);
}
}
break;
case SBC_VARLIST:
dump (0, "int %sn_%s;", st_lower (sbc->prefix),
st_lower (sbc->name));
dump (0, "struct variable **%sv_%s;", st_lower (sbc->prefix),
st_lower (sbc->name));
break;
case SBC_VAR:
dump (0, "struct variable *%sv_%s;", st_lower (sbc->prefix),
st_lower (sbc->name));
break;
case SBC_STRING:
dump (0, "char *s_%s;", st_lower (sbc->name));
break;
case SBC_INT:
case SBC_PINT:
dump (0, "long n_%s[MAXLISTS];", st_lower (sbc->name));
break;
case SBC_DBL:
dump (0, "double n_%s[MAXLISTS];", st_lower (sbc->name));
break;
case SBC_DBL_LIST:
dump (0, "subc_list_double dl_%s[MAXLISTS];",
st_lower(sbc->name));
break;
case SBC_INT_LIST:
dump (0, "subc_list_int il_%s[MAXLISTS];",
st_lower(sbc->name));
break;
default:;
/* nothing */
}
}
dump (-1, "};");
dump (-1, nullstr);
}
/* Write out prototypes for custom_*() functions as necessary. */
{
int seen = 0;
subcommand *sbc;
for (sbc = subcommands; sbc; sbc = sbc->next)
if (sbc->type == SBC_CUSTOM)
{
if (!seen)
{
seen = 1;
dump (0, "/* Prototype for custom subcommands of %s. */",
cmdname);
}
dump (0, "static int %scustom_%s (struct cmd_%s *);",
st_lower (prefix), st_lower (sbc->name),
make_identifier (cmdname));
}
if (seen)
dump (0, nullstr);
}
/* Prototypes for parsing and freeing functions. */
{
dump (0, "/* Command parsing functions. */");
dump (0, "static int parse_%s (struct cmd_%s *);",
make_identifier (cmdname), make_identifier (cmdname));
dump (0, "static void free_%s (struct cmd_%s *);",
make_identifier (cmdname), make_identifier (cmdname));
dump (0, nullstr);
}
}
/* Writes out code to initialize all the variables that need
initialization for particular specifier SPEC inside subcommand SBC. */
static void
dump_specifier_init (const specifier *spec, const subcommand *sbc)
{
if (spec->varname)
{
char s[256];
if (spec->def)
sprintf (s, "%s%s",
st_upper (prefix), find_symbol (spec->def->con)->name);
else
strcpy (s, "-1");
dump (0, "p->%s%s = %s;", sbc->prefix, spec->varname, s);
}
{
setting *s;
for (s = spec->s; s; s = s->next)
{
if (s->value != VAL_NONE)
{
const char *init;
assert (s->value == VAL_INT || s->value == VAL_DBL);
init = s->value == VAL_INT ? "NOT_LONG" : "SYSMIS";
dump (0, "p->%s%s = %s;", sbc->prefix, st_lower (s->valname), init);
}
}
}
}
/* Write code to initialize all variables. */
static void
dump_vars_init (int persistent)
{
/* Loop through all the subcommands. */
{
subcommand *sbc;
for (sbc = subcommands; sbc; sbc = sbc->next)
{
int f = 0;
dump (0, "p->sbc_%s = 0;", st_lower (sbc->name));
if ( ! persistent )
{
switch (sbc->type)
{
case SBC_INT_LIST:
break;
case SBC_DBL_LIST:
dump (1, "{");
dump (0, "int i;");
dump (1, "for (i = 0; i < MAXLISTS; ++i)");
dump (0, "subc_list_double_create(&p->dl_%s[i]) ;",
st_lower (sbc->name)
);
dump (-2, "}");
break;
case SBC_DBL:
dump (1, "{");
dump (0, "int i;");
dump (1, "for (i = 0; i < MAXLISTS; ++i)");
dump (0, "p->n_%s[i] = SYSMIS;", st_lower (sbc->name));
dump (-2, "}");
break;
case SBC_CUSTOM:
/* nothing */
break;
case SBC_PLAIN:
case SBC_ARRAY:
{
specifier *spec;
for (spec = sbc->spec; spec; spec = spec->next)
if (spec->s == NULL)
{
if (sbc->type == SBC_PLAIN)
dump (0, "p->%s%s = 0;", sbc->prefix, spec->varname);
else if (f == 0)
{
dump (0, "memset (p->a_%s, 0, sizeof p->a_%s);",
st_lower (sbc->name), st_lower (sbc->name));
f = 1;
}
}
else
dump_specifier_init (spec, sbc);
}
break;
case SBC_VARLIST:
dump (0, "p->%sn_%s = 0;",
st_lower (sbc->prefix), st_lower (sbc->name));
dump (0, "p->%sv_%s = NULL;",
st_lower (sbc->prefix), st_lower (sbc->name));
break;
case SBC_VAR:
dump (0, "p->%sv_%s = NULL;",
st_lower (sbc->prefix), st_lower (sbc->name));
break;
case SBC_STRING:
dump (0, "p->s_%s = NULL;", st_lower (sbc->name));
break;
case SBC_INT:
case SBC_PINT:
dump (1, "{");
dump (0, "int i;");
dump (1, "for (i = 0; i < MAXLISTS; ++i)");
dump (0, "p->n_%s[i] = NOT_LONG;", st_lower (sbc->name));
dump (-2, "}");
break;
default:
assert (0);
}
}
}
}
}
/* Return a pointer to a static buffer containing an expression that
will match token T. */
static char *
make_match (const char *t)
{
char *s;
s = get_buffer ();
while (*t == '_')
t++;
if (is_keyword (t))
sprintf (s, "lex_match (T_%s)", t);
else if (!strcmp (t, "ON") || !strcmp (t, "YES"))
strcpy (s, "(lex_match_id (\"ON\") || lex_match_id (\"YES\") "
"|| lex_match_id (\"TRUE\"))");
else if (!strcmp (t, "OFF") || !strcmp (t, "NO"))
strcpy (s, "(lex_match_id (\"OFF\") || lex_match_id (\"NO\") "
"|| lex_match_id (\"FALSE\"))");
else if (isdigit ((unsigned char) t[0]))
sprintf (s, "lex_match_int (%s)", t);
else
sprintf (s, "lex_match_id (\"%s\")", t);
return s;
}
/* Write out the parsing code for specifier SPEC within subcommand
SBC. */
static void
dump_specifier_parse (const specifier *spec, const subcommand *sbc)
{
setting *s;
if (spec->omit_kw && spec->omit_kw->next)
error ("Omittable setting is not last setting in `%s' specifier.",
spec->varname);
if (spec->omit_kw && spec->omit_kw->parent->next)
error ("Default specifier is not in last specifier in `%s' "
"subcommand.", sbc->name);
for (s = spec->s; s; s = s->next)
{
int first = spec == sbc->spec && s == spec->s;
/* Match the setting's keyword. */
if (spec->omit_kw == s)
{
if (!first)
{
dump (1, "else");
dump (1, "{");
}
dump (1, "%s;", make_match (s->specname));
}
else
dump (1, "%sif (%s)", first ? "" : "else ",
make_match (s->specname));
/* Handle values. */
if (s->value == VAL_NONE)
dump (0, "p->%s%s = %s%s;", sbc->prefix, spec->varname,
st_upper (prefix), find_symbol (s->con)->name);
else
{
if (spec->omit_kw != s)
dump (1, "{");
if (spec->varname)
{
dump (0, "p->%s%s = %s%s;", sbc->prefix, spec->varname,
st_upper (prefix), find_symbol (s->con)->name);
if ( sbc->type == SBC_ARRAY )
dump (0, "p->a_%s[%s%s%s] = 1;",
st_lower (sbc->name),
st_upper (prefix), st_upper (sbc->prefix),
st_upper (spec->varname));
}
if (s->valtype == VT_PAREN)
{
if (s->optvalue)
{
dump (1, "if (lex_match ('('))");
dump (1, "{");
}
else
{
dump (1, "if (!lex_match ('('))");
dump (1, "{");
dump (0, "msg (SE, _(\"`(' expected after %s "
"specifier of %s subcommand.\"));",
s->specname, sbc->name);
dump (0, "goto lossage;");
dump (-1, "}");
outdent ();
}
}
if (s->value == VAL_INT)
{
dump (1, "if (!lex_is_integer ())");
dump (1, "{");
dump (0, "msg (SE, _(\"%s specifier of %s subcommand "
"requires an integer argument.\"));",
s->specname, sbc->name);
dump (0, "goto lossage;");
dump (-1, "}");
dump (-1, "p->%s%s = lex_integer ();",
sbc->prefix, st_lower (s->valname));
}
else
{
dump (1, "if (!lex_is_number ())");
dump (1, "{");
dump (0, "msg (SE, _(\"Number expected after %s "
"specifier of %s subcommand.\"));",
s->specname, sbc->name);
dump (0, "goto lossage;");
dump (-1, "}");
dump (-1, "p->%s%s = tokval;", sbc->prefix,
st_lower (s->valname));
}
if (s->restriction)
{
{
char *str, *str2;
str = xmalloc (MAX_TOK_LEN);
str2 = xmalloc (MAX_TOK_LEN);
sprintf (str2, "p->%s%s", sbc->prefix, st_lower (s->valname));
sprintf (str, s->restriction, str2, str2, str2, str2,
str2, str2, str2, str2);
dump (1, "if (!(%s))", str);
free (str);
free (str2);
}
dump (1, "{");
dump (0, "msg (SE, _(\"Bad argument for %s "
"specifier of %s subcommand.\"));",
s->specname, sbc->name);
dump (0, "goto lossage;");
dump (-1, "}");
outdent ();
}
dump (0, "lex_get ();");
if (s->valtype == VT_PAREN)
{
dump (1, "if (!lex_match (')'))");
dump (1, "{");
dump (0, "msg (SE, _(\"`)' expected after argument for "
"%s specifier of %s.\"));",
s->specname, sbc->name);
dump (0, "goto lossage;");
dump (-1, "}");
outdent ();
if (s->optvalue)
{
dump (-1, "}");
outdent ();
}
}
if (s != spec->omit_kw)
dump (-1, "}");
}
if (s == spec->omit_kw)
{
dump (-1, "}");
outdent ();
}
outdent ();
}
}
/* Write out the code to parse subcommand SBC. */
static void
dump_subcommand (const subcommand *sbc)
{
if (sbc->type == SBC_PLAIN || sbc->type == SBC_ARRAY)
{
int count;
dump (1, "while (token != '/' && token != '.')");
dump (1, "{");
{
specifier *spec;
for (count = 0, spec = sbc->spec; spec; spec = spec->next)
{
if (spec->s)
dump_specifier_parse (spec, sbc);
else
{
count++;
dump (1, "%sif (%s)", spec != sbc->spec ? "else " : "",
make_match (st_upper (spec->varname)));
if (sbc->type == SBC_PLAIN)
dump (0, "p->%s%s = 1;", st_lower (sbc->prefix),
spec->varname);
else
dump (0, "p->a_%s[%s%s%s] = 1;",
st_lower (sbc->name),
st_upper (prefix), st_upper (sbc->prefix),
st_upper (spec->varname));
outdent ();
}
}
}
{
specifier *spec;
setting *s;
/* This code first finds the last specifier in sbc. Then it
finds the last setting within that last specifier. Either
or both might be NULL. */
spec = sbc->spec;
s = NULL;
if (spec)
{
while (spec->next)
spec = spec->next;
s = spec->s;
if (s)
while (s->next)
s = s->next;
}
if (spec && (!spec->s || !spec->omit_kw))
{
dump (1, "else");
dump (1, "{");
dump (0, "lex_error (NULL);");
dump (0, "goto lossage;");
dump (-1, "}");
outdent ();
}
}
dump (0, "lex_match (',');");
dump (-1, "}");
outdent ();
}
else if (sbc->type == SBC_VARLIST)
{
dump (1, "if (!parse_variables (default_dict, &p->%sv_%s, &p->%sn_%s, "
"PV_APPEND%s%s))",
st_lower (sbc->prefix), st_lower (sbc->name),
st_lower (sbc->prefix), st_lower (sbc->name),
sbc->message ? " |" : "",
sbc->message ? sbc->message : "");
dump (0, "goto lossage;");
outdent ();
}
else if (sbc->type == SBC_VAR)
{
dump (0, "p->%sv_%s = parse_variable ();",
st_lower (sbc->prefix), st_lower (sbc->name));
dump (1, "if (!p->%sv_%s)",
st_lower (sbc->prefix), st_lower (sbc->name));
dump (0, "goto lossage;");
outdent ();
}
else if (sbc->type == SBC_STRING)
{
if (sbc->restriction)
{
dump (1, "{");
dump (0, "int x;");
}
dump (1, "if (!lex_force_string ())");
dump (0, "return 0;");
outdent ();
if (sbc->restriction)
{
dump (0, "x = ds_length (&tokstr);");
dump (1, "if (!(%s))", sbc->restriction);
dump (1, "{");
dump (0, "msg (SE, _(\"String for %s must be %s.\"));",
sbc->name, sbc->message);
dump (0, "goto lossage;");
dump (-1, "}");
outdent ();
}
dump (0, "free(p->s_%s);", st_lower(sbc->name) );
dump (0, "p->s_%s = xstrdup (ds_c_str (&tokstr));",
st_lower (sbc->name));
dump (0, "lex_get ();");
if (sbc->restriction)
dump (-1, "}");
}
else if (sbc->type == SBC_DBL)
{
dump (1, "if (!lex_force_num ())");
dump (0, "goto lossage;");
dump (-1, "p->n_%s[p->sbc_%s - 1] = lex_number ();",
st_lower (sbc->name), st_lower (sbc->name) );
dump (0, "lex_get();");
}
else if (sbc->type == SBC_INT)
{
dump(1, "{");
dump(0, "int x;");
dump (1, "if (!lex_force_int ())");
dump (0, "goto lossage;");
dump (-1, "x = lex_integer ();");
dump (0, "lex_get();");
if (sbc->restriction)
{
char buf[1024];
dump (1, "if (!(%s))", sbc->restriction);
dump (1, "{");
sprintf(buf,sbc->message,sbc->name);
if ( sbc->translatable )
dump (0, "msg (SE, gettext(\"%s\"));",buf);
else
dump (0, "msg (SE, \"%s\");",buf);
dump (0, "goto lossage;");
dump (-1, "}");
}
dump (0, "p->n_%s[p->sbc_%s - 1] = x;", st_lower (sbc->name), st_lower(sbc->name) );
dump (-1,"}");
}
else if (sbc->type == SBC_PINT)
{
dump (0, "lex_match ('(');");
dump (1, "if (!lex_force_int ())");
dump (0, "goto lossage;");
dump (-1, "p->n_%s = lex_integer ();", st_lower (sbc->name));
dump (0, "lex_match (')');");
}
else if (sbc->type == SBC_DBL_LIST)
{
dump (0, "if ( p->sbc_%s > MAXLISTS)",st_lower(sbc->name));
dump (1, "{");
dump (0, "msg (SE, \"No more than %%d %s subcommands allowed\",MAXLISTS);",st_lower(sbc->name));
dump (0, "goto lossage;");
dump (-1,"}");
dump (1, "while (token != '/' && token != '.')");
dump (1, "{");
dump (0, "lex_match(',');");
dump (0, "if (!lex_force_num ())");
dump (1, "{");
dump (0, "goto lossage;");
dump (-1,"}");
dump (0, "subc_list_double_push(&p->dl_%s[p->sbc_%s-1],lex_number ());",
st_lower (sbc->name),st_lower (sbc->name)
);
dump (0, "lex_get();");
dump (-1,"}");
}
else if (sbc->type == SBC_CUSTOM)
{
dump (1, "switch (%scustom_%s (p))",
st_lower (prefix), st_lower (sbc->name));
dump (0, "{");
dump (1, "case 0:");
dump (0, "goto lossage;");
dump (-1, "case 1:");
indent ();
dump (0, "break;");
dump (-1, "case 2:");
indent ();
dump (0, "lex_error (NULL);");
dump (0, "goto lossage;");
dump (-1, "default:");
indent ();
dump (0, "assert (0);");
dump (-1, "}");
outdent ();
}
}
/* Write out entire parser. */
static void
dump_parser (int persistent)
{
int f;
indent = 0;
dump (0, "static int");
dump (0, "parse_%s (struct cmd_%s *p)", make_identifier (cmdname),
make_identifier (cmdname));
dump (1, "{");
dump_vars_init (persistent);
dump (1, "for (;;)");
dump (1, "{");
f = 0;
if (def && (def->type == SBC_VARLIST))
{
if (def->type == SBC_VARLIST)
dump (1, "if (token == T_ID "
"&& dict_lookup_var (default_dict, tokid) != NULL "
"&& lex_look_ahead () != '=')");
else
{
dump (0, "if ((token == T_ID "
"&& dict_lookup_var (default_dict, tokid) "
"&& lex_look_ahead () != '=')");
dump (1, " || token == T_ALL)");
}
dump (1, "{");
dump (0, "p->sbc_%s++;", st_lower (def->name));
dump (1, "if (!parse_variables (default_dict, &p->%sv_%s, &p->%sn_%s, "
"PV_APPEND))",
st_lower (def->prefix), st_lower (def->name),
st_lower (def->prefix), st_lower (def->name));
dump (0, "goto lossage;");
dump (-2, "}");
outdent ();
f = 1;
}
else if (def && def->type == SBC_CUSTOM)
{
dump (1, "switch (%scustom_%s (p))",
st_lower (prefix), st_lower (def->name));
dump (0, "{");
dump (1, "case 0:");
dump (0, "goto lossage;");
dump (-1, "case 1:");
indent ();
dump (0, "p->sbc_%s++;", st_lower (def->name));
dump (0, "continue;");
dump (-1, "case 2:");
indent ();
dump (0, "break;");
dump (-1, "default:");
indent ();
dump (0, "assert (0);");
dump (-1, "}");
outdent ();
}
{
subcommand *sbc;
for (sbc = subcommands; sbc; sbc = sbc->next)
{
dump (1, "%sif (%s)", f ? "else " : "", make_match (sbc->name));
f = 1;
dump (1, "{");
dump (0, "lex_match ('=');");
dump (0, "p->sbc_%s++;", st_lower (sbc->name));
if (sbc->arity != ARITY_MANY)
{
dump (1, "if (p->sbc_%s > 1)", st_lower (sbc->name));
dump (1, "{");
dump (0, "msg (SE, _(\"%s subcommand may be given only once.\"));",
sbc->name);
dump (0, "goto lossage;");
dump (-1, "}");
outdent ();
}
dump_subcommand (sbc);
dump (-1, "}");
outdent ();
}
}
/* Now deal with the /ALGORITHM subcommand implicit to all commands */
dump(1,"else if ( get_syntax() != COMPATIBLE && lex_match_id(\"ALGORITHM\"))");
dump(1,"{");
dump (0, "lex_match ('=');");
dump(1,"if (lex_match_id(\"COMPATIBLE\"))");
dump(0,"set_cmd_algorithm(COMPATIBLE);");
outdent();
dump(1,"else if (lex_match_id(\"ENHANCED\"))");
dump(0,"set_cmd_algorithm(ENHANCED);");
dump (-1, "}");
outdent ();
dump (1, "if (!lex_match ('/'))");
dump (0, "break;");
dump (-2, "}");
outdent ();
dump (0, nullstr);
dump (1, "if (token != '.')");
dump (1, "{");
dump (0, "lex_error (_(\"expecting end of command\"));");
dump (0, "goto lossage;");
dump (-1, "}");
dump (0, nullstr);
outdent ();
{
/* Check that mandatory subcommands have been specified */
subcommand *sbc;
for (sbc = subcommands; sbc; sbc = sbc->next)
{
if ( sbc->arity == ARITY_ONCE_EXACTLY )
{
dump (0, "if ( 0 == p->sbc_%s)", st_lower (sbc->name));
dump (1, "{");
dump (0, "msg (SE, _(\"%s subcommand must be given.\"));",
sbc->name);
dump (0, "goto lossage;");
dump (-1, "}");
dump (0, nullstr);
}
}
}
dump (-1, "return 1;");
dump (0, nullstr);
dump (-1, "lossage:");
indent ();
dump (0, "free_%s (p);", make_identifier (cmdname));
dump (0, "return 0;");
dump (-1, "}");
dump (0, nullstr);
}
/* Write out the code to parse aux subcommand SBC. */
static void
dump_aux_subcommand (const subcommand *sbc)
{
if (sbc->type == SBC_PLAIN )
{
specifier *spec;
for (spec = sbc->spec; spec; spec = spec->next)
{
char buf[80];
sprintf(buf,"p->%s%s",st_lower(sbc->prefix),spec->varname);
dump (0, "msg(MM,\"%s is %%s\",",sbc->name);
dump (0, "(%s < 1000)?\"not set\":settings[%s - 1000]", buf, buf);
dump (0, ");");
}
}
else if (sbc->type == SBC_STRING)
{
dump (0, "msg(MM,\"%s is \\\"%%s\\\"\",p->s_%s);", sbc->name,st_lower(sbc->name) );
}
else if (sbc->type == SBC_INT)
{
dump (1, "{");
dump (0, "int i;");
dump (1, "for (i = 0; i < MAXLISTS; ++i)");
dump (0, "msg(MM,\"%s is %%ld\",p->n_%s[i]);", sbc->name,st_lower(sbc->name) );
outdent();
dump (-1, "}");
}
else if (sbc->type == SBC_CUSTOM)
{
dump (0, "aux_%scustom_%s(p);",st_lower(prefix),make_identifier(sbc->name));
}
else
assert(0);
}
/* Write out auxilliary parser. */
static void
dump_aux_parser (void)
{
int f=0;
subcommand *sbc;
aux_subcommand *asbc;
/* Write out English strings for all the identifiers in the symbol table. */
{
int f, k;
symbol *sym;
char *buf = NULL;
/* Note the squirmings necessary to make sure that the last string
is not followed by a comma (is it necessary to do that ?? ) */
for (sym = symtab, f = k = 0; sym; sym = sym->next)
if (!sym->unique && !is_keyword (sym->name))
{
if (!f)
{
dump (0, "/* Strings for subcommand specifiers. */");
dump (1, "static const char *settings[]=");
dump (1, "{");
f = 1;
}
if (buf == NULL)
buf = xmalloc (1024);
else
dump (0, buf);
sprintf (buf, "\"%s\",",sym->name);
}
if (buf)
{
buf[strlen (buf) - 1] = 0;
dump (0, buf);
free (buf);
}
if (f)
{
dump (-1, "};");
dump (-1, nullstr);
}
}
indent = 0;
dump (0, "static int");
dump (0, "aux_parse_%s (struct cmd_%s *p)", make_identifier (cmdname),
make_identifier (cmdname));
dump (1, "{");
dump (1, "for (;;)");
dump (1, "{");
for (sbc = subcommands; sbc; sbc = sbc->next)
{
dump (1, "%sif (%s)", f ? "else " : "", make_match (sbc->name));
f = 1;
dump (1, "{");
dump_aux_subcommand (sbc);
dump (-1, "}");
outdent ();
}
for (asbc = aux_subcommands ; asbc ; asbc = asbc->next)
{
dump (1, "%sif (%s)", f ? "else " : "", make_match (asbc->name));
f = 1;
dump (1, "{");
dump(0,"aux_%s();",make_identifier(asbc->value));
dump (-1, "}");
outdent ();
}
dump (1, "if (!lex_match ('/'))");
dump (0, "break;");
dump (-2, "}");
outdent ();
dump (0, nullstr);
dump (1, "if (token != '.')");
dump (1, "{");
dump (0, "lex_error (_(\"expecting end of command\"));");
dump (0, "goto lossage;");
dump (-1, "}");
dump (0, nullstr);
dump (-1, "return 1;");
dump (0, nullstr);
dump (-1, "lossage:");
indent ();
dump (0, "free_%s (p);", make_identifier (cmdname));
dump (0, "return 0;");
dump (-1, "} /* aux_parse_%s (struct cmd_%s *p) */",
make_identifier (cmdname), make_identifier (cmdname));
dump (0, nullstr);
}
/* Write the output file header. */
static void
dump_header (void)
{
time_t curtime;
struct tm *loctime;
char *timep;
indent = 0;
curtime = time (NULL);
loctime = localtime (&curtime);
timep = asctime (loctime);
timep[strlen (timep) - 1] = 0;
dump (0, "/* %s\t\t-*- mode: c; buffer-read-only: t -*-", ofn);
dump (0, nullstr);
dump (0, " Generated by q2c from %s on %s.", ifn, timep);
dump (0, " Do not modify!");
dump (0, " */");
}
/* Write out commands to free variable state. */
static void
dump_free (int persistent)
{
subcommand *sbc;
int used;
indent = 0;
used = 0;
if ( ! persistent )
{
for (sbc = subcommands; sbc; sbc = sbc->next)
{
if (sbc->type == SBC_STRING)
used = 1;
if (sbc->type == SBC_DBL_LIST)
used = 1;
}
}
dump (0, "static void");
dump (0, "free_%s (struct cmd_%s *p%s)", make_identifier (cmdname),
make_identifier (cmdname), used ? "" : " UNUSED");
dump (1, "{");
if ( ! persistent )
{
for (sbc = subcommands; sbc; sbc = sbc->next)
{
switch (sbc->type)
{
case SBC_VARLIST:
dump (0, "free (p->v_variables);");
break;
case SBC_STRING:
dump (0, "free (p->s_%s);", st_lower (sbc->name));
break;
case SBC_DBL_LIST:
dump (0, "int i;");
dump (1, "for(i = 0; i < MAXLISTS ; ++i)");
dump (0, "subc_list_double_destroy(&p->dl_%s[i]);", st_lower (sbc->name));
outdent();
break;
default:
break;
}
}
}
dump (-1, "}");
}
/* Returns the name of a directive found on the current input line, if
any, or a null pointer if none found. */
static const char *
recognize_directive (void)
{
static char directive[16];
char *sp, *ep;
sp = skip_ws (buf);
if (strncmp (sp, "/*", 2))
return NULL;
sp = skip_ws (sp + 2);
if (*sp != '(')
return NULL;
sp++;
ep = strchr (sp, ')');
if (ep == NULL)
return NULL;
if (ep - sp > 15)
ep = sp + 15;
memcpy (directive, sp, ep - sp);
directive[ep - sp] = '\0';
return directive;
}
static void aux_parse (void);
int
main (int argc, char *argv[])
{
pgmname = argv[0];
if (argc != 3)
fail ("Syntax: q2c input.q output.c");
ifn = argv[1];
in = fopen (ifn, "r");
if (!in)
fail ("%s: open: %s.", ifn, strerror (errno));
ofn = argv[2];
out = fopen (ofn, "w");
if (!out)
fail ("%s: open: %s.", ofn, strerror (errno));
is_open = 1;
buf = xmalloc (MAX_LINE_LEN);
tokstr = xmalloc (MAX_TOK_LEN);
dump_header ();
indent = 0;
dump (0, "#line %d \"%s\"", ln + 1, ifn);
while (get_line ())
{
const char *directive = recognize_directive ();
if (directive == NULL)
{
dump (0, "%s", buf);
continue;
}
dump (0, "#line %d \"%s\"", oln + 1, ofn);
if (!strcmp (directive, "specification"))
{
/* Skip leading slash-star line. */
get_line ();
lex_get ();
parse ();
/* Skip trailing star-slash line. */
get_line ();
}
else if (!strcmp (directive, "headers"))
{
indent = 0;
dump (0, "#include <stdlib.h>");
dump (0, "#include \"alloc.h\"");
dump (0, "#include \"error.h\"");
dump (0, "#include \"lexer.h\"");
dump (0, "#include \"settings.h\"");
dump (0, "#include \"str.h\"");
dump (0, "#include \"subclist.h\"");
dump (0, "#include \"var.h\"");
dump (0, nullstr);
dump (0, "#include \"gettext.h\"");
dump (0, "#define _(msgid) gettext (msgid)");
dump (0, nullstr);
}
else if (!strcmp (directive, "declarations"))
dump_declarations ();
else if (!strcmp (directive, "functions"))
{
dump_parser (0);
dump_free (0);
}
else if (!strcmp (directive, "_functions"))
{
dump_parser (1);
dump_free (1);
}
else if (!strcmp (directive, "aux_functions"))
{
aux_parse();
dump_aux_parser ();
}
else
error ("unknown directive `%s'", directive);
indent = 0;
dump (0, "#line %d \"%s\"", ln + 1, ifn);
}
return EXIT_SUCCESS;
}
/* Parse an entire auxilliary specification. */
static void
aux_parse (void)
{
aux_subcommand *sbc;
aux_subcommand *prevsbc = 0 ;
get_line();
lex_get();
for (;;)
{
sbc = xmalloc(sizeof(aux_subcommand));
sbc->next = prevsbc;
sbc->name = xstrdup (tokstr);
lex_get();
skip_token('=');
sbc->value = xstrdup (tokstr);
lex_get();
if (token == '.')
break;
skip_token(';');
prevsbc = sbc;
}
/* Skip trailing star-slash line. */
get_line ();
aux_subcommands = sbc;
}
|