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
|
/*** yuck.c -- generate umbrella commands
*
* Copyright (C) 2013-2014 Sebastian Freundt
*
* Author: Sebastian Freundt <freundt@ga-group.nl>
*
* This file is part of yuck.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the author nor the names of any contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
***/
#if defined HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
/* for fgetln() */
#if !defined _NETBSD_SOURCE
# define _NETBSD_SOURCE
#endif /* !_NETBSD_SOURCE */
#if !defined _DARWIN_SOURCE
# define _DARWIN_SOURCE
#endif /* !_DARWIN_SOURCE */
#if !defined _ALL_SOURCE
# define _ALL_SOURCE
#endif /* !_ALL_SOURCE */
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <assert.h>
#include <errno.h>
#include <ctype.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <time.h>
#if defined WITH_SCMVER
# include <yuck-scmver.h>
#endif /* WITH_SCMVER */
#if !defined LIKELY
# define LIKELY(_x) __builtin_expect((_x), 1)
#endif /* !LIKELY */
#if !defined UNLIKELY
# define UNLIKELY(_x) __builtin_expect((_x), 0)
#endif /* UNLIKELY */
#if !defined UNUSED
# define UNUSED(_x) _x __attribute__((unused))
#endif /* !UNUSED */
#if !defined countof
# define countof(x) (sizeof(x) / sizeof(*x))
#endif /* !countof */
#define _paste(x, y) x ## y
#define paste(x, y) _paste(x, y)
#if !defined with
# define with(args...) \
for (args, *paste(__ep, __LINE__) = (void*)1; \
paste(__ep, __LINE__); paste(__ep, __LINE__) = 0)
#endif /* !with */
#if !defined HAVE_GETLINE && !defined HAVE_FGETLN
/* as a service to people including this file in their project
* but who might not necessarily run the corresponding AC_CHECK_FUNS
* we assume that a getline() is available. */
# define HAVE_GETLINE 1
#endif /* !HAVE_GETLINE && !HAVE_FGETLN */
typedef enum {
YOPT_NONE,
YOPT_ALLOW_UNKNOWN_DASH,
YOPT_ALLOW_UNKNOWN_DASHDASH,
} yopt_t;
struct usg_s {
char *umb;
char *cmd;
char *parg;
char *desc;
};
struct opt_s {
char sopt;
char *lopt;
char *larg;
char *desc;
unsigned int oarg:1U;
unsigned int marg:1U;
};
#if !defined BOOTSTRAP && defined WITH_SCMVER
static const char *yscm_strs[] = {
[YUCK_SCM_TARBALL] = "tarball",
[YUCK_SCM_GIT] = "git",
[YUCK_SCM_BZR] = "bzr",
[YUCK_SCM_HG] = "hg",
};
#elif !defined BOOTSTRAP
/* just forward declare this type so function signatures will work */
struct yuck_version_s;
#endif /* WITH_SCMVER */
static __attribute__((format(printf, 1, 2))) void
error(const char *fmt, ...)
{
va_list vap;
va_start(vap, fmt);
vfprintf(stderr, fmt, vap);
va_end(vap);
if (errno) {
fputs(": ", stderr);
fputs(strerror(errno), stderr);
}
fputc('\n', stderr);
return;
}
static inline __attribute__((unused)) void*
deconst(const void *cp)
{
union {
const void *c;
void *p;
} tmp = {cp};
return tmp.p;
}
static inline __attribute__((always_inline)) unsigned int
yfls(unsigned int x)
{
return x ? sizeof(x) * 8U - __builtin_clz(x) : 0U;
}
static inline __attribute__((always_inline)) size_t
max_zu(size_t x, size_t y)
{
return x > y ? x : y;
}
static size_t
xstrncpy(char *restrict dst, const char *src, size_t ssz)
{
memcpy(dst, src, ssz);
dst[ssz] = '\0';
return ssz;
}
static __attribute__((unused)) size_t
xstrlcpy(char *restrict dst, const char *src, size_t dsz)
{
size_t ssz = strlen(src);
if (ssz > dsz) {
ssz = dsz - 1U;
}
memcpy(dst, src, ssz);
dst[ssz] = '\0';
return ssz;
}
static __attribute__((unused)) size_t
xstrlncpy(char *restrict dst, size_t dsz, const char *src, size_t ssz)
{
if (ssz > dsz) {
ssz = dsz - 1U;
}
memcpy(dst, src, ssz);
dst[ssz] = '\0';
return ssz;
}
static __attribute__((unused)) bool
xstreqp(const char *s1, const char *s2)
{
if (s1 == NULL && s2 == NULL) {
return true;
} else if (s1 == NULL || s2 == NULL) {
/* one of them isn't NULL */
return false;
}
/* resort to normal strcmp */
return !strcasecmp(s1, s2);
}
static bool
only_whitespace_p(const char *line, size_t llen)
{
for (const char *lp = line, *const ep = line + llen; lp < ep; lp++) {
if (!isspace(*lp)) {
return false;
}
}
return true;
}
static bool
isdashdash(const char c)
{
switch (c) {
default:
return true;
case '=':
case '[':
case ']':
case '.':
case '\0' ... ' ':
return false;
}
}
static void
massage_desc(char *str)
{
/* kick final newline and escape m4 quoting characters */
char *sp;
for (sp = str; *sp; sp++) {
switch (*sp) {
default:
break;
case '[':
/* map to STX (start of text) */
*sp = '\002';
break;
case ']':
/* map to ETX (end of text) */
*sp = '\003';
break;
case '(':
/* map to SO (shift out) */
*sp = '\016';
break;
case ')':
/* map to SI (shift in) */
*sp = '\017';
break;
}
}
if (sp > str && sp[-1] == '\n') {
*--sp = '\0';
}
return;
}
#if !defined BOOTSTRAP
static void
unmassage_buf(char *restrict buf, size_t bsz)
{
/* turn m4 quoting character substitutes into brackets again */
for (char *restrict sp = buf, *const ep = buf + bsz; sp < ep; sp++) {
switch (*sp) {
default:
break;
case '\002':
/* unmap STX (start of text) */
*sp = '[';
break;
case '\003':
/* unmap ETX (end of text) */
*sp = ']';
break;
case '\016':
/* unmap SO (shift out) */
*sp = '(';
break;
case '\017':
/* unmap SI (shift in) */
*sp = ')';
break;
}
}
return;
}
static int
mktempp(char *restrict tmpl[static 1U], int prefixlen)
{
static mode_t umsk;
char *bp = *tmpl + prefixlen;
char *const ep = *tmpl + strlen(*tmpl);
mode_t m;
int fd;
if (UNLIKELY(!umsk)) {
umsk = umask(0022);
}
if (ep[-6] != 'X' || ep[-5] != 'X' || ep[-4] != 'X' ||
ep[-3] != 'X' || ep[-2] != 'X' || ep[-1] != 'X') {
if ((fd = open(bp, O_RDWR | O_CREAT | O_EXCL, 0666)) < 0 &&
(bp -= prefixlen,
fd = open(bp, O_RDWR | O_CREAT | O_EXCL, 0666)) < 0) {
/* fuck that then */
return -1;
}
} else if (m = umask(S_IXUSR | S_IRWXG | S_IRWXO),
UNLIKELY((fd = mkstemp(bp), umask(m), fd < 0)) &&
UNLIKELY((bp -= prefixlen,
/* reset to XXXXXX */
memset(ep - 6, 'X', 6U),
fd = mkstemp(bp)) < 0)) {
/* at least we tried */
return -1;
}
/* store result */
*tmpl = bp;
return fd;
}
static FILE*
mkftempp(char *restrict tmpl[static 1U], int prefixlen)
{
int fd;
if (UNLIKELY((fd = mktempp(tmpl, prefixlen)) < 0)) {
return NULL;
}
return fdopen(fd, "w");
}
# if defined WITH_SCMVER
static bool
regfilep(const char *fn)
{
struct stat st[1U];
return stat(fn, st) == 0 && S_ISREG(st->st_mode);
}
# endif /* WITH_SCMVER */
#endif /* !BOOTSTRAP */
/* bang buffers */
typedef struct {
/* the actual buffer (resizable) */
char *s;
/* current size */
size_t z;
} bbuf_t;
static char*
bbuf_cpy(bbuf_t b[static 1U], const char *str, size_t ssz)
{
size_t nu = max_zu(yfls(ssz + 1U) + 1U, 6U);
size_t ol = b->z ? max_zu(yfls(b->z) + 1U, 6U) : 0U;
if (UNLIKELY(nu > ol)) {
b->s = realloc(b->s, (1U << nu) * sizeof(*b->s));
}
xstrncpy(b->s, str, ssz);
b->z += ssz;
return b->s;
}
static char*
bbuf_cat(bbuf_t b[static 1U], const char *str, size_t ssz)
{
size_t nu = max_zu(yfls(b->z + ssz + 1U) + 1U, 6U);
size_t ol = b->z ? max_zu(yfls(b->z) + 1U, 6U) : 0U;
if (UNLIKELY(nu > ol)) {
b->s = realloc(b->s, (1U << nu) * sizeof(*b->s));
}
xstrncpy(b->s + b->z, str, ssz);
b->z += ssz;
return b->s;
}
static void yield_usg(const struct usg_s *arg);
static void yield_opt(const struct opt_s *arg);
static void yield_inter(const bbuf_t x[static 1U]);
static void yield_setopt(yopt_t);
#define DEBUG(args...)
static int
usagep(const char *line, size_t llen)
{
#define STREQLITP(x, lit) (!strncasecmp((x), lit, sizeof(lit) - 1))
static struct usg_s cur_usg;
static bbuf_t umb[1U];
static bbuf_t cmd[1U];
static bbuf_t parg[1U];
static bbuf_t desc[1U];
static bool cur_usg_yldd_p;
static bool umb_yldd_p;
const char *sp;
const char *up;
const char *cp;
const char *const ep = line + llen;
if (UNLIKELY(line == NULL)) {
goto yield;
}
DEBUG("USAGEP CALLED with %s", line);
if (STREQLITP(line, "setopt")) {
/* it's a setopt */
return 0;
} else if (!STREQLITP(line, "usage:")) {
if (only_whitespace_p(line, llen) && !desc->z) {
return 1;
} else if (!isspace(*line) && !cur_usg_yldd_p) {
/* append to description */
cur_usg.desc = bbuf_cat(desc, line, llen);
return 1;
}
yield:
#define RESET cur_usg.cmd = cur_usg.parg = cur_usg.desc = NULL, desc->z = 0U
if (!cur_usg_yldd_p) {
yield_usg(&cur_usg);
/* reset */
RESET;
cur_usg_yldd_p = true;
umb_yldd_p = true;
}
return 0;
} else if (!cur_usg_yldd_p) {
/* can't just goto yield because they wander off */
yield_usg(&cur_usg);
/* reset */
RESET;
cur_usg_yldd_p = true;
umb_yldd_p = true;
}
/* overread whitespace then */
for (sp = line + sizeof("usage:") - 1; sp < ep && isspace(*sp); sp++);
/* first thing should name the umbrella, find its end */
for (up = sp; sp < ep && !isspace(*sp); sp++);
if (cur_usg.umb && !strncasecmp(cur_usg.umb, up, sp - up)) {
/* nothing new and fresh */
;
} else {
cur_usg.umb = bbuf_cpy(umb, up, sp - up);
umb_yldd_p = false;
}
/* overread more whitespace and [--BLA] decls then */
overread:
for (; sp < ep && isspace(*sp); sp++);
/* we might be strafed with option decls here */
switch (*sp) {
case '[':
if (sp[1U] == '-') {
/* might be option spec [-x], read on */
;
} else if (STREQLITP(sp + 1U, "OPTION")) {
/* definitely an option marker innit? */
;
} else {
/* could be posarg, better exit here */
break;
}
/* otherwise read till closing bracket */
for (sp++; sp < ep && *sp++ != ']';);
/* and also read over `...' */
for (sp++; sp < ep && *sp == '.'; sp++);
goto overread;
default:
/* best leave the loop */
break;
}
/* now it's time for the command innit */
for (cp = sp; sp < ep && !isspace(*sp); sp++);
if (cur_usg.cmd && !strncasecmp(cur_usg.cmd, cp, sp - cp)) {
/* nothing new and fresh */
;
} else if ((*cp != '<' || (cp++, *--sp == '>')) &&
!strncasecmp(cp, "command", sp - cp)) {
/* special command COMMAND or <command> */
cur_usg.cmd = NULL;
} else if (*cp >= 'a' && *cp <= 'z' && umb_yldd_p) {
/* we mandate commands start with a lower case alpha char */
cur_usg.cmd = bbuf_cpy(cmd, cp, sp - cp);
} else {
/* not a command, could be posarg innit, so rewind */
sp = cp;
}
/* now there might be positional args, snarf them */
for (; sp < ep && isspace(*sp); sp++);
if (sp < ep) {
cur_usg.parg = bbuf_cpy(parg, sp, ep - sp - 1U);
}
cur_usg_yldd_p = false;
return 1;
}
static int
optionp(const char *line, size_t llen)
{
static struct opt_s cur_opt;
static bbuf_t desc[1U];
static bbuf_t lopt[1U];
static bbuf_t larg[1U];
const char *sp = line;
const char *const ep = line + llen;
if (UNLIKELY(line == NULL)) {
goto yield;
}
DEBUG("OPTIONP CALLED with %s", line);
/* overread whitespace */
for (; sp < ep && isspace(*sp); sp++) {
if (*sp == '\t') {
/* make a tab character count 8 in total */
sp += 7U;
}
}
if ((sp - line >= 8 || (sp - line >= 1 && *sp != '-')) &&
(cur_opt.sopt || cur_opt.lopt)) {
/* should be description */
goto desc;
}
yield:
/* must yield the old current option before it's too late */
if (cur_opt.sopt || cur_opt.lopt) {
yield_opt(&cur_opt);
}
/* complete reset */
memset(&cur_opt, 0, sizeof(cur_opt));
if (sp - line < 2) {
/* can't be an option, can it? */
return 0;
} else if (!*sp) {
/* not an option either */
return 0;
}
/* no yield pressure anymore, try parsing the line */
sp++;
if (*sp >= '0') {
char sopt = *sp++;
/* eat a comma as well */
if (*sp == ',') {
sp++;
}
if (!isspace(*sp)) {
/* dont know -x.SOMETHING? */
return 0;
}
/* start over with the new option */
sp++;
cur_opt.sopt = sopt;
if (*sp == '\0') {
/* just the short option then innit? */
return 1;
} else if (isspace(*sp)) {
/* no arg name, no longopt */
;
} else if (*sp == '-') {
/* must be a --long now, maybe */
sp++;
} else {
/* just an arg name */
const char *ap;
if (*sp == '[') {
cur_opt.oarg = 1U;
sp++;
}
if (*sp == '=') {
sp++;
}
for (ap = sp; sp < ep && isdashdash(*sp); sp++);
cur_opt.larg = bbuf_cpy(larg, ap, sp - ap);
if (cur_opt.oarg && *sp++ != ']') {
/* maybe not an optarg? */
;
}
if (*sp == '.') {
/* could be mularg */
if (sp[1U] == '.' && sp[2U] == '.') {
/* yay, 3 dots, read over dots */
for (sp += 3U; *sp == '.'; sp++);
cur_opt.marg = 1U;
}
}
}
} else if (*sp == '-') {
/* --option */
;
} else {
/* dont know what this is */
return 0;
}
/* --option */
if (*sp++ == '-') {
const char *op;
for (op = sp; sp < ep && isdashdash(*sp); sp++);
cur_opt.lopt = bbuf_cpy(lopt, op, sp - op);
switch (*sp++) {
case '[':
if (*sp++ != '=') {
/* just bullshit then innit? */
break;
}
/* otherwise optarg, fall through */
cur_opt.oarg = 1U;
case '=':;
/* has got an arg */
const char *ap;
for (ap = sp; sp < ep && isdashdash(*sp); sp++);
cur_opt.larg = bbuf_cpy(larg, ap, sp - ap);
if (cur_opt.oarg && *sp++ != ']') {
/* maybe not an optarg? */
;
}
if (*sp == '.') {
/* could be mularg */
if (sp[1U] == '.' && sp[2U] == '.') {
/* yay, 3 dots, read over dots */
for (sp += 3U; *sp == '.'; sp++);
cur_opt.marg = 1U;
}
}
default:
break;
}
}
/* require at least one more space? */
;
/* space eater */
for (; sp < ep && isspace(*sp); sp++);
/* dont free but reset the old guy */
desc->z = 0U;
desc:
with (size_t sz = llen - (sp - line)) {
if (LIKELY(sz > 0U)) {
cur_opt.desc = bbuf_cat(desc, sp, sz);
}
}
return 1;
}
static int
interp(const char *line, size_t llen)
{
static bbuf_t desc[1U];
bool only_ws_p = only_whitespace_p(line, llen);
if (UNLIKELY(line == NULL)) {
goto yield;
}
DEBUG("INTERP CALLED with %s", line);
if (only_ws_p && desc->z) {
yield:
yield_inter(desc);
/* reset */
desc->z = 0U;
} else if (!only_ws_p) {
if (STREQLITP(line, "setopt")) {
/* not an inter */
return 0;
}
/* snarf the line */
bbuf_cat(desc, line, llen);
return 1;
}
return 0;
}
static int
setoptp(const char *line, size_t UNUSED(llen))
{
if (UNLIKELY(line == NULL)) {
return 0;
}
DEBUG("SETOPTP CALLED with %s", line);
if (STREQLITP(line, "setopt")) {
/* 'nother option */
const char *lp = line + sizeof("setopt");
if (0) {
;
} else if (STREQLITP(lp, "allow-unknown-dash-options")) {
yield_setopt(YOPT_ALLOW_UNKNOWN_DASH);
} else if (STREQLITP(lp, "allow-unknown-dashdash-options")) {
yield_setopt(YOPT_ALLOW_UNKNOWN_DASHDASH);
} else {
/* unknown setopt option */
}
}
return 0;
}
static const char nul_str[] = "";
static const char *const auto_types[] = {"auto", "flag"};
static FILE *outf;
static struct {
unsigned int no_auto_flags:1U;
unsigned int no_auto_action:1U;
} global_tweaks;
static void
__identify(char *restrict idn)
{
for (char *restrict ip = idn; *ip; ip++) {
switch (*ip) {
case '0' ... '9':
case 'A' ... 'Z':
case 'a' ... 'z':
break;
default:
*ip = '_';
}
}
return;
}
static size_t
count_pargs(const char *parg)
{
/* return max posargs as helper for auto-dashdash commands */
const char *pp;
size_t res;
for (res = 0U, pp = parg; *pp;) {
/* allow [--] or -- as auto-dashdash declarators */
if (*pp == '[') {
pp++;
}
if (*pp++ == '-') {
if (*pp++ == '-') {
if (*pp == ']' || isspace(*pp)) {
/* found him! */
return res;
}
}
/* otherwise not the declarator we were looking for
* fast forward to the end */
for (; *pp && !isspace(*pp); pp++);
} else {
/* we know it's a bog-standard posarg for sure */
res++;
/* check for ellipsis */
for (; *pp && *pp != '.' && !isspace(*pp); pp++);
if (!*pp) {
/* end of parg string anyway */
break;
}
if (*pp++ == '.' && *pp++ == '.' && *pp++ == '.') {
/* ellipsis, set res to infinity and bog off */
break;
}
}
/* fast forward over all the whitespace */
for (; *pp && isspace(*pp); pp++);
}
return 0U;
}
static char*
make_opt_ident(const struct opt_s *arg)
{
static bbuf_t i[1U];
if (arg->lopt != NULL) {
bbuf_cpy(i, arg->lopt, strlen(arg->lopt));
} else if (arg->sopt) {
bbuf_cpy(i, "dash.", 5U);
i->s[4U] = arg->sopt;
} else {
static unsigned int cnt;
bbuf_cpy(i, "idnXXXX", 7U);
snprintf(i->s + 3U, 5U, "%u", cnt++);
}
__identify(i->s);
return i->s;
}
static char*
make_ident(const char *str)
{
static bbuf_t buf[1U];
bbuf_cpy(buf, str, strlen(str));
__identify(buf->s);
return buf->s;
}
static void
yield_help(void)
{
const char *type = auto_types[global_tweaks.no_auto_action];
fprintf(outf, "yuck_add_option([help], [h], [help], [%s])\n", type);
fprintf(outf, "yuck_set_option_desc([help], [\
display this help and exit])\n");
return;
}
static void
yield_version(void)
{
const char *type = auto_types[global_tweaks.no_auto_action];
fprintf(outf,
"yuck_add_option([version], [V], [version], [%s])\n", type);
fprintf(outf, "yuck_set_option_desc([version], [\
output version information and exit])\n");
return;
}
static void
yield_usg(const struct usg_s *arg)
{
const char *parg = arg->parg ?: nul_str;
size_t nparg = count_pargs(parg);
if (arg->desc != NULL) {
/* kick last newline */
massage_desc(arg->desc);
}
if (arg->cmd != NULL) {
const char *idn = make_ident(arg->cmd);
fprintf(outf, "\nyuck_add_command([%s], [%s], [%s])\n",
idn, arg->cmd, parg);
if (nparg) {
fprintf(outf,
"yuck_set_command_max_posargs([%s], [%zu])\n",
idn, nparg);
}
if (arg->desc != NULL) {
fprintf(outf, "yuck_set_command_desc([%s], [%s])\n",
idn, arg->desc);
}
} else if (arg->umb != NULL) {
const char *idn = make_ident(arg->umb);
fprintf(outf, "\nyuck_set_umbrella([%s], [%s], [%s])\n",
idn, arg->umb, parg);
if (nparg) {
fprintf(outf,
"yuck_set_umbrella_max_posargs([%s], [%zu])\n",
idn, nparg);
}
if (arg->desc != NULL) {
fprintf(outf, "yuck_set_umbrella_desc([%s], [%s])\n",
idn, arg->desc);
}
/* insert auto-help and auto-version */
if (!global_tweaks.no_auto_flags) {
yield_help();
yield_version();
}
}
return;
}
static void
yield_opt(const struct opt_s *arg)
{
char sopt[2U] = {arg->sopt, '\0'};
const char *opt = arg->lopt ?: nul_str;
const char *idn = make_opt_ident(arg);
if (arg->larg == NULL) {
fprintf(outf, "yuck_add_option([%s], [%s], [%s], "
"[flag]);\n", idn, sopt, opt);
} else {
const char *asufs[] = {
nul_str, ", opt", ", mul", ", mul, opt"
};
const char *asuf = asufs[arg->oarg | arg->marg << 1U];
fprintf(outf, "yuck_add_option([%s], [%s], [%s], "
"[arg, %s%s]);\n", idn, sopt, opt, arg->larg, asuf);
}
if (arg->desc != NULL) {
massage_desc(arg->desc);
fprintf(outf,
"yuck_set_option_desc([%s], [%s])\n", idn, arg->desc);
}
return;
}
static void
yield_inter(const bbuf_t x[static 1U])
{
if (x->z) {
if (x->s[x->z - 1U] == '\n') {
x->s[x->z - 1U] = '\0';
}
massage_desc(x->s);
fprintf(outf, "yuck_add_inter([%s])\n", x->s);
}
return;
}
static void
yield_setopt(yopt_t yo)
{
switch (yo) {
default:
case YOPT_NONE:
break;
case YOPT_ALLOW_UNKNOWN_DASH:
fputs("yuck_setopt_allow_unknown_dash\n", outf);
break;
case YOPT_ALLOW_UNKNOWN_DASHDASH:
fputs("yuck_setopt_allow_unknown_dashdash\n", outf);
break;
}
return;
}
static enum {
UNKNOWN,
SET_INTER,
SET_UMBCMD,
SET_OPTION,
SET_SETOPT,
}
snarf_ln(char *line, size_t llen)
{
static unsigned int st;
switch (st) {
case UNKNOWN:
case SET_UMBCMD:
usage:
/* first keep looking for Usage: lines */
if (usagep(line, llen)) {
st = SET_UMBCMD;
break;
} else if (st == SET_UMBCMD) {
/* reset state, go on with option parsing */
st = UNKNOWN;
goto option;
}
case SET_OPTION:
option:
/* check them option things */
if (optionp(line, llen)) {
st = SET_OPTION;
break;
} else if (st == SET_OPTION) {
/* reset state, go on with usage parsing */
st = UNKNOWN;
goto usage;
}
case SET_INTER:
/* check for some intro texts */
if (interp(line, llen)) {
st = SET_INTER;
break;
} else {
/* reset state, go on with setopt parsing */
st = UNKNOWN;
}
case SET_SETOPT:
/* check for setopt BLA lines */
if (setoptp(line, llen)) {
st = SET_SETOPT;
break;
} else {
/* reset state, go on with option parsing */
st = UNKNOWN;
}
default:
break;
}
return UNKNOWN;
}
static int
snarf_f(FILE *f)
{
char *line = NULL;
size_t llen = 0U;
#if defined HAVE_GETLINE
for (ssize_t nrd; (nrd = getline(&line, &llen, f)) > 0;) {
if (*line == '#') {
continue;
}
snarf_ln(line, nrd);
}
#elif defined HAVE_FGETLN
while ((line = fgetln(f, &llen)) != NULL) {
if (*line == '#') {
continue;
}
snarf_ln(line, llen);
}
#else
# error neither getline() nor fgetln() available, cannot read file line by line
#endif /* GETLINE/FGETLN */
/* drain */
snarf_ln(NULL, 0U);
#if defined HAVE_GETLINE
free(line);
#endif /* HAVE_GETLINE */
return 0;
}
#if defined BOOTSTRAP
static FILE*
get_fn(int argc, char *argv[])
{
FILE *res;
if (argc > 1) {
const char *fn = argv[1];
if (UNLIKELY((res = fopen(fn, "r")) == NULL)) {
error("cannot open file `%s'", fn);
}
} else {
res = stdin;
}
return res;
}
int
main(int argc, char *argv[])
{
int rc = 0;
FILE *yf;
if (UNLIKELY((yf = get_fn(argc, argv)) == NULL)) {
rc = -1;
} else {
/* always use stdout */
outf = stdout;
fputs("\
changequote([,])dnl\n\
divert([-1])\n", outf);
/* let the snarfing begin */
rc = snarf_f(yf);
fputs("\n\
changecom([//])\n\
divert[]dnl\n", outf);
/* clean up */
fclose(yf);
}
return -rc;
}
#endif /* BOOTSTRAP */
#if !defined BOOTSTRAP
#if !defined PATH_MAX
# define PATH_MAX (256U)
#endif /* !PATH_MAX */
static char dslfn[PATH_MAX];
static bool
aux_in_path_p(const char *aux, const char *path, size_t pathz)
{
char fn[PATH_MAX];
char *restrict fp = fn;
struct stat st[1U];
fp += xstrlncpy(fn, sizeof(fn), path, pathz);
*fp++ = '/';
xstrlcpy(fp, aux, sizeof(fn) - (fp - fn));
if (stat(fn, st) < 0) {
return false;
}
return S_ISREG(st->st_mode);
}
static ssize_t
get_myself(char *restrict buf, size_t bsz)
{
ssize_t off;
char *mp;
if ((off = readlink("/proc/self/exe", buf, bsz)) < 0) {
return -1;
}
/* go back to the dir bit */
for (mp = buf + off - 1U; mp > buf && *mp != '/'; mp--);
/* should be bin/, go up one level */
*mp = '\0';
for (; mp > buf && *mp != '/'; mp--);
/* check if we're right */
if (UNLIKELY(strcmp(++mp, "bin"))) {
/* oh, it's somewhere but not bin/? */
return -1;
}
/* now just use share/yuck/ */
xstrlcpy(mp, "share/yuck/", bsz - (mp - buf));
mp += sizeof("share/yuck");
return mp - buf;
}
static int
find_aux(char *restrict buf, size_t bsz, const char *aux)
{
/* look up path relative to binary position */
static char pkgdatadir[PATH_MAX];
static ssize_t pkgdatalen;
static const char *tmplpath;
static ssize_t tmplplen;
const char *path;
size_t plen;
/* start off by snarfing the environment */
if (tmplplen == 0U) {
if ((tmplpath = getenv("YUCK_TEMPLATE_PATH")) != NULL) {
tmplplen = strlen(tmplpath);
} else {
/* just set it to something non-0 to indicate initting
* and that also works with the loop below */
tmplplen = -1;
tmplpath = (void*)0x1U;
}
}
/* snarf pkgdatadir */
if (pkgdatalen == 0U) {
pkgdatalen = get_myself(pkgdatadir, sizeof(pkgdatadir));
}
/* go through the path first */
for (const char *pp = tmplpath, *ep, *const end = tmplpath + tmplplen;
pp < end; pp = ep + 1U) {
ep = strchr(pp, ':') ?: end;
if (aux_in_path_p(aux, pp, ep - pp)) {
path = pp;
plen = ep - pp;
goto bang;
}
}
/* no luck with the env path then aye */
if (pkgdatalen > 0 && aux_in_path_p(aux, pkgdatadir, pkgdatalen)) {
path = pkgdatadir;
plen = pkgdatalen;
goto bang;
}
#if defined YUCK_TEMPLATE_PATH
path = YUCK_TEMPLATE_PATH;
plen = sizeof(YUCK_TEMPLATE_PATH);
if (plen-- > 0U && aux_in_path_p(aux, path, plen)) {
goto bang;
}
#endif /* YUCK_TEMPLATE_PATH */
/* not what we wanted at all, must be christmas */
return -1;
bang:
with (size_t z) {
z = xstrlncpy(buf, bsz, path, plen);
buf[z++] = '/';
xstrlcpy(buf + z, aux, bsz - z);
}
return 0;
}
static int
find_dsl(void)
{
return find_aux(dslfn, sizeof(dslfn), "yuck.m4");
}
static void
unmassage_fd(int tgtfd, int srcfd)
{
static char buf[4096U];
for (ssize_t nrd; (nrd = read(srcfd, buf, sizeof(buf))) > 0;) {
const char *bp = buf;
const char *const ep = buf + nrd;
unmassage_buf(buf, nrd);
for (ssize_t nwr;
bp < ep && (nwr = write(tgtfd, bp, ep - bp)) > 0;
bp += nwr);
}
return;
}
static char *m4_cmdline[16U] = {
"m4",
};
static size_t cmdln_idx;
static int
prep_m4(void)
{
char *p;
/* checkout the environment, look for M4 */
if ((p = getenv("M4")) == NULL) {
cmdln_idx = 1U;
return 0;
}
/* otherwise it's big string massaging business */
do {
m4_cmdline[cmdln_idx++] = p;
/* mimic a shell's IFS */
for (; *p && !isspace(*p); p++) {
const char this = *p;
switch (this) {
default:
break;
case '"':
case '\'':
/* fast forward then */
while (*++p != this) {
if (*p == '\\') {
p++;
}
}
break;
}
}
if (!*p) {
break;
}
/* otherwise it's an IFS */
for (*p++ = '\0'; isspace(*p); p++);
} while (1);
return 0;
}
static __attribute__((noinline)) int
run_m4(const char *outfn, ...)
{
pid_t m4p;
/* to snarf off traffic from the child */
int intfd[2];
if (pipe(intfd) < 0) {
error("pipe setup to/from m4 failed");
return -1;
} else if (!cmdln_idx && prep_m4() < 0) {
error("m4 preparations failed");
return -1;
}
switch ((m4p = vfork())) {
case -1:
/* i am an error */
error("vfork for m4 failed");
return -1;
default:;
/* i am the parent */
int rc;
int st;
if (outfn != NULL) {
/* --output given */
const int outfl = O_RDWR | O_CREAT | O_TRUNC;
int outfd;
if ((outfd = open(outfn, outfl, 0666)) < 0) {
/* bollocks */
error("cannot open outfile `%s'", outfn);
goto bollocks;
}
/* really redir now */
dup2(outfd, STDOUT_FILENO);
close(outfd);
}
close(intfd[1]);
unmassage_fd(STDOUT_FILENO, intfd[0]);
rc = 2;
while (waitpid(m4p, &st, 0) != m4p);
if (WIFEXITED(st)) {
rc = WEXITSTATUS(st);
}
/* clean up the rest of the pipe */
close(intfd[0]);
return rc;
case 0:;
/* i am the child */
break;
}
/* child code here */
with (va_list vap) {
va_start(vap, outfn);
for (size_t i = cmdln_idx;
i < countof(m4_cmdline) &&
(m4_cmdline[i] = va_arg(vap, char*)) != NULL; i++);
va_end(vap);
}
dup2(intfd[1], STDOUT_FILENO);
close(intfd[0]);
execvp(m4_cmdline[0U], m4_cmdline);
error("execvp(m4) failed");
bollocks:
_exit(EXIT_FAILURE);
}
static int
wr_pre(void)
{
fputs("\
changequote`'changequote([,])dnl\n\
divert([-1])\n", outf);
return 0;
}
static int
wr_suf(void)
{
fputs("\n\
changequote`'dnl\n\
divert`'dnl\n", outf);
return 0;
}
static int
wr_intermediary(char *const args[], size_t nargs)
{
int rc = 0;
wr_pre();
if (nargs == 0U) {
if (snarf_f(stdin) < 0) {
errno = 0;
error("cannot interpret directives on stdin");
rc = 1;
}
}
for (unsigned int i = 0U; i < nargs && rc == 0; i++) {
const char *fn = args[i];
FILE *yf;
if (UNLIKELY((yf = fopen(fn, "r")) == NULL)) {
error("cannot open file `%s'", fn);
rc = 1;
break;
} else if (snarf_f(yf) < 0) {
errno = 0;
error("cannot interpret directives from `%s'", fn);
rc = 1;
}
/* clean up */
fclose(yf);
}
/* reset to sane values */
wr_suf();
return rc;
}
static int
wr_header(const char hdr[static 1U])
{
/* massage the hdr bit a bit */
if (strcmp(hdr, "/dev/null")) {
/* /dev/null just means ignore the header aye? */
const char *hp;
if ((hp = strrchr(hdr, '/')) == NULL) {
hp = hdr;
} else {
hp++;
};
wr_pre();
fprintf(outf, "define([YUCK_HEADER], [%s])dnl\n", hp);
wr_suf();
}
return 0;
}
static int
wr_man_date(void)
{
time_t now;
const struct tm *tp;
char buf[32U];
int rc = 0;
if ((now = time(NULL)) == (time_t)-1) {
rc = -1;
} else if ((tp = gmtime(&now)) == NULL) {
rc = -1;
} else if (!strftime(buf, sizeof(buf), "%B %Y", tp)) {
rc = -1;
} else {
fprintf(outf, "define([YUCK_MAN_DATE], [%s])dnl\n", buf);
}
return rc;
}
static int
wr_man_pkg(const char *pkg)
{
fprintf(outf, "define([YUCK_PKG_STR], [%s])dnl\n", pkg);
return 0;
}
static int
wr_man_nfo(const char *nfo)
{
fprintf(outf, "define([YUCK_NFO_STR], [%s])dnl\n", nfo);
return 0;
}
static int
wr_man_incln(FILE *fp, char *restrict ln, size_t lz)
{
static int verbp;
static int parap;
if (UNLIKELY(ln == NULL)) {
/* drain mode */
if (verbp) {
fputs(".fi\n", fp);
}
} else if (lz <= 1U/*has at least a newline?*/) {
if (verbp) {
/* close verbatim mode */
fputs(".fi\n", fp);
verbp = 0;
}
if (!parap) {
fputs(".PP\n", fp);
parap = 1;
}
} else if (*ln == '[' && ln[lz - 2U] == ']') {
/* section */
char *restrict lp = ln + 1U;
for (const char *const eol = ln + lz - 2U; lp < eol; lp++) {
*lp = (char)toupper(*lp);
}
*lp = '\0';
fputs(".SH ", fp);
fputs(ln + 1U, fp);
fputs("\n", fp);
/* reset state */
parap = 0;
verbp = 0;
} else if (ln[0U] == ' ' && ln[1U] == ' ' && !verbp) {
fputs(".nf\n", fp);
verbp = 1;
goto cp;
} else {
cp:
/* otherwise copy */
fwrite(ln, lz, sizeof(*ln), fp);
parap = 0;
}
return 0;
}
static int
wr_man_include(char **const inc)
{
char _ofn[] = P_tmpdir "/" "yuck_XXXXXX";
char *ofn = _ofn;
FILE *ofp;
char *line = NULL;
size_t llen = 0U;
FILE *fp;
if (UNLIKELY((fp = fopen(*inc, "r")) == NULL)) {
error("Cannot open include file `%s', ignoring", *inc);
*inc = NULL;
return -1;
} else if (UNLIKELY((ofp = mkftempp(&ofn, sizeof(P_tmpdir))) == NULL)) {
error("Cannot open output file `%s', ignoring", ofn);
*inc = NULL;
return -1;
}
/* make sure we pass on ofn */
*inc = strdup(ofn);
#if defined HAVE_GETLINE
for (ssize_t nrd; (nrd = getline(&line, &llen, fp)) > 0;) {
wr_man_incln(ofp, line, nrd);
}
#elif defined HAVE_FGETLN
while ((line = fgetln(fp, &llen)) != NULL) {
wr_man_incln(ofp, line, llen);
}
#else
# error neither getline() nor fgetln() available, cannot read file line by line
#endif /* GETLINE/FGETLN */
/* drain */
wr_man_incln(ofp, NULL, 0U);
#if defined HAVE_GETLINE
free(line);
#endif /* HAVE_GETLINE */
/* close files properly */
fclose(fp);
fclose(ofp);
return 0;
}
static int
wr_man_includes(char *incs[], size_t nincs)
{
for (size_t i = 0U; i < nincs; i++) {
/* massage file */
if (wr_man_include(incs + i) < 0) {
continue;
} else if (incs[i] == NULL) {
/* something else is wrong */
continue;
}
/* otherwise make a note to include this file */
fprintf(outf, "\
append([YUCK_INCLUDES], [%s], [,])dnl\n", incs[i]);
}
return 0;
}
static int
wr_version(const struct yuck_version_s *v, const char *vlit)
{
wr_pre();
if (v != NULL) {
#if defined WITH_SCMVER
const char *yscm = yscm_strs[v->scm];
fprintf(outf, "define([YUCK_SCMVER_VTAG], [%s])\n", v->vtag);
fprintf(outf, "define([YUCK_SCMVER_SCM], [%s])\n", yscm);
fprintf(outf, "define([YUCK_SCMVER_DIST], [%u])\n", v->dist);
fprintf(outf, "define([YUCK_SCMVER_RVSN], [%0*x])\n",
(int)(v->rvsn & 0x07U), v->rvsn >> 4U);
if (!v->dirty) {
fputs("define([YUCK_SCMVER_FLAG_CLEAN])\n", outf);
} else {
fputs("define([YUCK_SCMVER_FLAG_DIRTY])\n", outf);
}
/* for convenience */
fputs("define([YUCK_SCMVER_VERSION], [", outf);
fputs(v->vtag, outf);
if (v->scm > YUCK_SCM_TARBALL && v->dist) {
fputc('.', outf);
fputs(yscm_strs[v->scm], outf);
fprintf(outf, "%u.%0*x",
v->dist,
(int)(v->rvsn & 0x07U), v->rvsn >> 4U);
}
if (v->dirty) {
fputs(".dirty", outf);
}
fputs("])\n", outf);
#else /* !WITH_SCMVER */
errno = 0, error("\
scmver support not built in but ptr %p given to wr_version()", v);
#endif /* WITH_SCMVER */
}
if (vlit != NULL) {
fputs("define([YUCK_VERSION], [", outf);
fputs(vlit, outf);
fputs("])\n", outf);
}
wr_suf();
return 0;
}
static int
rm_intermediary(const char *fn, int keepp)
{
if (!keepp) {
if (unlink(fn) < 0) {
error("cannot remove intermediary `%s'", fn);
return -1;
}
} else {
/* otherwise print a nice message so users know
* the file we created */
errno = 0, error("intermediary `%s' kept", fn);
}
return 0;
}
static int
rm_includes(char *const incs[], size_t nincs, int keepp)
{
int rc = 0;
errno = 0;
for (size_t i = 0U; i < nincs; i++) {
char *restrict fn;
if ((fn = incs[i]) != NULL) {
if (!keepp && unlink(fn) < 0) {
error("cannot remove intermediary `%s'", fn);
rc = -1;
} else if (keepp) {
/* otherwise print a nice message so users know
* the file we created */
errno = 0, error("intermediary `%s' kept", fn);
}
free(fn);
}
}
return rc;
}
#endif /* !BOOTSTRAP */
#if !defined BOOTSTRAP
#include "yuck.yucc"
static int
cmd_gen(const struct yuck_cmd_gen_s argi[static 1U])
{
static char _deffn[] = P_tmpdir "/" "yuck_XXXXXX";
static char gencfn[PATH_MAX];
static char genhfn[PATH_MAX];
char *deffn = _deffn;
int rc = 0;
if (argi->no_auto_flags_flag) {
global_tweaks.no_auto_flags = 1U;
}
if (argi->no_auto_actions_flag) {
global_tweaks.no_auto_action = 1U;
}
/* deal with the output first */
if (UNLIKELY((outf = mkftempp(&deffn, sizeof(P_tmpdir))) == NULL)) {
error("cannot open intermediate file `%s'", deffn);
return -1;
}
/* write up our findings in DSL language */
rc = wr_intermediary(argi->args, argi->nargs);
/* deal with hard wired version numbers */
if (argi->version_arg) {
rc += wr_version(NULL, argi->version_arg);
}
/* special directive for the header or is it */
if (argi->header_arg != NULL) {
rc += wr_header(argi->header_arg);
}
/* and we're finished with the intermediary */
fclose(outf);
/* only proceed if there has been no error yet */
if (rc) {
goto out;
} else if (find_dsl() < 0) {
/* error whilst finding our DSL and things */
error("cannot find yuck dsl file");
rc = 2;
goto out;
} else if (find_aux(gencfn, sizeof(gencfn), "yuck-coru.c.m4") < 0 ||
find_aux(genhfn, sizeof(genhfn), "yuck-coru.h.m4") < 0) {
error("cannot find yuck template files");
rc = 2;
goto out;
}
/* now route that stuff through m4 */
with (const char *outfn = argi->output_arg,
*cusfn = argi->custom_arg ?: "/dev/null", *hdrfn) {
if ((hdrfn = argi->header_arg) != NULL) {
/* run a special one for the header */
if ((rc = run_m4(hdrfn, dslfn, deffn, genhfn, NULL))) {
break;
}
/* now run the whole shebang for the beef code */
rc = run_m4(outfn, dslfn, deffn, cusfn, gencfn, NULL);
break;
}
/* standard case: pipe directives, then header, then code */
rc = run_m4(outfn, dslfn, deffn, cusfn, genhfn, gencfn, NULL);
}
out:
/* unlink include files */
rm_intermediary(deffn, argi->keep_flag);
return rc;
}
static int
cmd_genman(const struct yuck_cmd_genman_s argi[static 1U])
{
static char _deffn[] = P_tmpdir "/" "yuck_XXXXXX";
static char genmfn[PATH_MAX];
char *deffn = _deffn;
int rc = 0;
/* deal with the output first */
if (UNLIKELY((outf = mkftempp(&deffn, sizeof(P_tmpdir))) == NULL)) {
error("cannot open intermediate file `%s'", deffn);
return -1;
}
/* write up our findings in DSL language */
rc = wr_intermediary(argi->args, argi->nargs);
if (argi->version_string_arg) {
rc += wr_version(NULL, argi->version_string_arg);
} else if (argi->version_file_arg) {
#if defined WITH_SCMVER
struct yuck_version_s v[1U];
const char *verfn = argi->version_file_arg;
if (yuck_version_read(v, verfn) < 0) {
error("cannot read version number from `%s'", verfn);
rc--;
} else {
rc += wr_version(v, NULL);
}
#else /* !WITH_SCMVER */
errno = 0;
error("\
scmver support not built in, --version-file cannot be used");
#endif /* WITH_SCMVER */
}
/* reset to sane values */
wr_pre();
/* at least give the man page template an idea for YUCK_MAN_DATE */
rc += wr_man_date();
if (argi->package_arg) {
/* package != umbrella */
rc += wr_man_pkg(argi->package_arg);
}
if (argi->info_page_arg) {
const char *nfo;
if ((nfo = argi->info_page_arg) == YUCK_OPTARG_NONE) {
nfo = "YUCK_PKG_STR";
}
rc += wr_man_nfo(nfo);
}
/* go through includes */
wr_man_includes(argi->include_args, argi->include_nargs);
/* reset to sane values */
wr_suf();
/* and we're finished with the intermediary */
fclose(outf);
/* only proceed if there has been no error yet */
if (rc) {
goto out;
} else if (find_dsl() < 0) {
/* error whilst finding our DSL and things */
error("cannot find yuck dsl and template files");
rc = 2;
goto out;
} else if (find_aux(genmfn, sizeof(genmfn), "yuck.man.m4") < 0) {
error("cannot find yuck template for man pages");
rc = 2;
goto out;
}
/* now route that stuff through m4 */
with (const char *outfn = argi->output_arg) {
/* standard case: pipe directives, then header, then code */
rc = run_m4(outfn, dslfn, deffn, genmfn, NULL);
}
out:
rm_includes(argi->include_args, argi->include_nargs, argi->keep_flag);
rm_intermediary(deffn, argi->keep_flag);
return rc;
}
static int
cmd_gendsl(const struct yuck_cmd_gendsl_s argi[static 1U])
{
int rc = 0;
if (argi->no_auto_flags_flag) {
global_tweaks.no_auto_flags = 1U;
}
if (argi->no_auto_actions_flag) {
global_tweaks.no_auto_action = 1U;
}
/* bang to stdout or argi->output_arg */
with (const char *outfn = argi->output_arg) {
if (outfn == NULL) {
outf = stdout;
} else if ((outf = fopen(outfn, "w")) == NULL) {
error("cannot open outfile `%s'", outfn);
return 1;
}
}
rc += wr_intermediary(argi->args, argi->nargs);
if (argi->version_arg) {
rc += wr_version(NULL, argi->version_arg);
}
return rc;
}
static int
cmd_scmver(const struct yuck_cmd_scmver_s argi[static 1U])
{
#if defined WITH_SCMVER
struct yuck_version_s v[1U];
struct yuck_version_s ref[1U];
const char *const reffn = argi->reference_arg;
const char *const infn = argi->args[0U];
int rc = 0;
/* read the reference file before it goes out of fashion */
if (reffn && yuck_version_read(ref, reffn) < 0 &&
/* only be fatal if we actually want to use the reference file */
argi->use_reference_flag) {
error("cannot read reference file `%s'", reffn);
return 1;
} else if (reffn == NULL && argi->use_reference_flag) {
errno = 0, error("\
flag -n|--use-reference requires -r|--reference parameter");
return 1;
} else if (!argi->use_reference_flag && yuck_version(v, infn) < 0) {
if (argi->ignore_noscm_flag) {
/* allow graceful exit through --ignore-noscm */
return 0;
}
errno = 0, error("cannot determine SCM");
return 1;
}
if (reffn && argi->use_reference_flag) {
/* must populate v then */
*v = *ref;
} else if (reffn && yuck_version_cmp(v, ref)) {
if (argi->verbose_flag) {
errno = 0, error("scm version differs from reference");
}
/* version stamps differ */
yuck_version_write(argi->reference_arg, v);
/* reserve exit code 3 for `updated reference file' */
rc = 3;
} else if (reffn && !argi->force_flag) {
/* make sure the output file exists */
const char *const outfn = argi->output_arg;
if (outfn == NULL || regfilep(outfn)) {
/* don't worry about anything then */
return 0;
}
/* otherwise create at least one version of the output */
}
if (infn != NULL && regfilep(infn)) {
static char _scmvfn[] = P_tmpdir "/" "yscm_XXXXXX";
static char tmplfn[PATH_MAX];
char *scmvfn = _scmvfn;
/* try the local dir first */
if ((outf = mkftempp(&scmvfn, sizeof(P_tmpdir))) == NULL) {
error("cannot open intermediate file `%s'", scmvfn);
rc = 1;
} else if (find_aux(tmplfn, sizeof(tmplfn),
"yuck-scmver.m4") < 0) {
error("cannot find yuck template for version strings");
rc = 1;
} else {
const char *outfn = argi->output_arg;
/* write the actual version info */
rc += wr_version(v, NULL);
/* and we're finished with the intermediary */
fclose(outf);
/* macro massage, vtmpfn is the template file */
rc = run_m4(outfn, scmvfn, tmplfn, infn, NULL);
rm_intermediary(scmvfn, argi->keep_flag);
}
} else {
fputs(v->vtag, stdout);
if (v->scm > YUCK_SCM_TARBALL && v->dist) {
fputc('.', stdout);
fputs(yscm_strs[v->scm], stdout);
fprintf(stdout, "%u.%0*x",
v->dist,
(int)(v->rvsn & 0x07U), v->rvsn >> 4U);
}
if (v->dirty) {
fputs(".dirty", stdout);
}
fputc('\n', stdout);
}
return rc;
#else /* !WITH_SCMVER */
fputs("scmver support not built in\n", stderr);
return argi->cmd == YUCK_CMD_SCMVER;
#endif /* WITH_SCMVER */
}
int
main(int argc, char *argv[])
{
yuck_t argi[1U];
int rc = 0;
if (yuck_parse(argi, argc, argv) < 0) {
rc = 1;
goto out;
}
switch (argi->cmd) {
default:
fputs("\
No valid command specified.\n\
See --help to obtain a list of available commands.\n", stderr);
rc = 1;
goto out;
case YUCK_CMD_GEN:
if ((rc = cmd_gen((const void*)argi)) < 0) {
rc = 1;
}
break;
case YUCK_CMD_GENDSL:
if ((rc = cmd_gendsl((const void*)argi)) < 0) {
rc = 1;
}
break;
case YUCK_CMD_GENMAN:
if ((rc = cmd_genman((const void*)argi)) < 0) {
rc = 1;
}
break;
case YUCK_CMD_SCMVER:
if ((rc = cmd_scmver((const void*)argi)) < 0) {
rc = 1;
}
break;
}
out:
yuck_free(argi);
return rc;
}
#endif /* !BOOTSTRAP */
/* yuck.c ends here */
|