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
|
/* sumlines.c - total the numbers appearing in various input lines. */
/* B. D. McKay. Version of May 12, 2023. */
#ifndef GMP
#define GMP 1 /* Non-zero if gmp multi-precise integers are allowed.
In this case you need the GNU multi-precision library,
available with -lgmp if it is installed. */
#endif
#define USAGE \
"sumlines [-w|-W] [-v] [-d] [-n] [-f fmtfile]... file file file ..."
#define HELPTEXT \
" Sum lines matching specified formats.\n\
\n\
Any number of input files can be given. \"-\" means stdin.\n\
If there are no files given, just stdin is assumed.\n\
File names can contain wildcards, in which case all matching files\n\
are used in numerically sorted order.\n\
\n\
Formats are read from four sources in this order:\n\
(1) Any files mentioned with -f on the command line (any number).\n\
(2) The file named in the environment variable SUMLINES.FMT (if any)\n\
(3) The file sumlines.fmt in the current directory (if it exists)\n\
(4) The file sumlines.fmt in the home directory (if it exists)\n\
All these are read if they exist and the results concatenated.\n\
Formats exactly matching earlier formats (except perhaps for flags)\n\
are not used.\n\
\n\
Each format occupies exactly two lines. The first line gives a\n\
list of flags (DEFAULT FINAL ERROR UNIQUE COUNT CONTINUE NUMERIC\n\
SILENT ENDFILE P=# NOPREFIX BEFORE/.../ AFTER/.../ separated by\n\
spaces, commas or |s).\n\
The second line gives the format itself.\n\
\n\
Example. This totals the summary lines of autoson runs:\n\
DEFAULT # comment \n\
cpu=%fu,%fs,%fx pf=%d\n\
There can also be blank lines and lines with only comments, but\n\
not between the flags line and the format itself.\n\
\n\
-d don't read sumlines.fmt or ~/sumlines.fmt or $SUMLINES.FMT \n\
-w suppresses warning messages about no matching lines or no\n\
matching final lines.\n\
-W in addition, suppresses warning about missing cases.\n\
-n don't write the number of matching lines for each format.\n\
-v produces a list of all the formats.\n"
#define DEFAULT 0 /* No special flags */
#define FINAL 1 /* At least one of these must be in each input file */
#define ERROR 2 /* Must be none of these */
#define UNIQUE 4 /* The %s and %c parts must be unique over all inputs */
#define COUNT 8 /* The output only states how many lines matched */
#define CONTINUE 16 /* Try to match later formats too */
#define NUMERIC 32 /* Use numerical comparison (see numstrcmp() below) */
#define SILENT 64 /* Don't report, just check */
#define ENDFILE 128 /* Usually appears at end of output */
#define NOPREFIX 256 /* Print without prefix >X, where X is a letter */
#define BEFORE 512 /* Output a string or blank line before these lines */
#define AFTER 1024 /* Output a string or blank line after these lines */
/* The formats are tried against each input line one at a time, and the
first one that matches is accepted. The entire line must match.
If the CONTINUE flag is present, the input line is also matched
against further formats.
Except in the case of formats with the COUNT flag, each format that
matches any lines produces output giving the total value of each of the
integers %d or real numbers %f in the lines which match. If there are
any %s or %c controls in the format, the output is given separately for
each value of the matching strings which appear in the input lines.
In the case of the COUNT flag, the program only reports the number of
input lines which matched the format.
If a format has the UNIQUE flag, no two input lines may match with the
same values of the %s and %c controls present. Otherwise a warning
message is written for each duplicate match.
If a format starts with >X, where X is a letter, the flag NOPREFIX causes
the summary to be written without that prefix.
The BEFORE flag followed by white space (including end of line) causes a
blank line to appear in the output before lines associated with this
format. If a non-alphanumeric character c other than '=' follows BEFORE
immediately, everything after the c and before the next c or end of line
is written. AFTER is similar, written after lines associated with this
format.
The sequence P=# where # is an integer value defines the base for the
%p directive. There can be no spaces in the sequence "P=#". The
default base is 2.
%d - matches an integer (small enough for 64 bits)
%x - same as %d but accumulates maximum rather than the sum
%n - same as %d but accumulates minimum rather than the sum
%p - same as %d but accumulates the value modulo a base
%m - matches a integer of unbounded size (if GMP!=0)
%f - matches a real number of the form ddddd.ddd or -ddddd.ddd
%v - same as %f but reports the average rather than the sum
%X - same as %f but reports the maximum rather than the sum
%h - similar to %d:%d:%f taken as h:m:s with a single floating value
%sx - matches a string, where 'x' is any character.
If 'x' is not a space, match zero or more characters from the
current position up but not including the first 'x'.
If 'x' is a space, match one or more characters from the current
position up to and including the first non-space character
which is followed by a space.
%c - matches a non-white character
%% - matches the character '%'
% - (with a space following the '%') matches zero or more spaces or
tabs, as many as appear in the input. In the output, this
sequence appears as one space.
% - (appearing exactly at the end of the format) matches zero or
more spaces at the end of the line. In the output, nothing.
%*d, %*m, %*x, %*p, %*f, %*sx, %*c - these are similar to the versions
without the '*' except that the value is ignored (not used for
summing, and not used to divide the output). In the output,
this field appears as a single character '*'.
%# - matches an unsigned integer. For each format containing this
control, a report is made of any breaks or duplicates in the
sequence of matching numbers. (So this is useful for checking a
sequence of case numbers.) At most one %# may appear in each format.
%l - matches a list of arbitrarily many (%d sized) integers
The %d, %x, %n, %p, %f, %v, and %X controls can have a numerical field
width after the %, for example %10d. This has no effect on matching
input lines but causes the output number to be written in a minimum
of that many characters, with space padding. A negative field width
indicates left-adjustment within the field.
At least one FINAL format must match in each file or a warning is given
(unless -w is used, in which case no warning is given).
A format marked ENDFILE will cause sumlines to act as if it started
reading from a new input file. This can have some effects on the
order of output lines.
*/
#define HAS(i,flgs) ((format[i].flags&(flgs)) != 0)
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <pwd.h>
#include <stdlib.h>
#include <glob.h>
#include <limits.h>
#include <unistd.h>
#if GMP
#include <gmp.h>
#endif
typedef long long integer;
#define DOUT "%*lld"
#define FOUT "%*.2f"
#define VOUT "%*.4f"
#define HMSOUT1 "%lld:%lld:%lld"
#define HMSOUT2 "%lld:%lld:%.2f"
static char *dout,*fout,*Xout,*vout,*hmsout1,*hmsout2;
static integer maxint; /* set by find_maxint() */
#define INCR(x,inc) \
{if (((x) > 0 && maxint-(x) < (inc)) || ((x) < 0 && (maxint)+(x) < -(inc))) \
{fprintf(stderr,">E overflow with %%d or %%p format\n"); exit(1);} \
x += (inc);} /* x += inc with safety check */
typedef int boolean;
#define FALSE 0
#define TRUE 1
typedef struct
{
int nvals;
integer *val;
} integerlist;
typedef union
{
double f;
integer d;
integerlist *l;
#if GMP
mpz_t *m;
#endif
} number;
#define D 0 /* Code for "integer" */
#define F 1 /* Code for "real" */
#define M 2 /* Code for "multiprecision integer" */
#define X 3 /* Code for "integer, take maximum" */
#define V 4 /* Code for "real, take average" */
#define P 5 /* Code for "integer, modulo some base" */
#define LD 6 /* Code for "list of integer" */
#define H 8 /* Code for "h:m:s" */
#define FX 9 /* Code for "real, take maximum" */
#define N 10 /* Code for "integer, take minimum" */
#define MAXLINELEN 100000 /* Maximum input line size
(longer lines are broken in bits) */
#define MAXVALUES 32 /* Maximum total number of
%d,%x,%n,%p,%m,%v,%f,%h or %l items in a format */
#define MAXFORMATS 1000
static struct fmt_st
{
integer pmod;
int flags;
char *fmt;
char *before,*after;
} format[MAXFORMATS];
typedef struct countrec
{
struct countrec *left,*right,*parent;
char *fmt;
unsigned long count;
number total[MAXVALUES];
} countnode;
static countnode *count_root[MAXFORMATS];
static unsigned long matching_lines[MAXFORMATS];
static integer total_position[MAXFORMATS];
static integer lastseq[MAXFORMATS];
#if GMP
static mpz_t mp_value[MAXVALUES];
#endif
static integerlist il[MAXVALUES];
#define A 0
#define L 1
#define R 2
#define LL 3
#define LR 4
#define RL 5
#define RR 6
#ifndef GLOB_BRACE /* Allow {} processing -- Linux extension */
#define GLOB_BRACE 0
#endif
#ifndef GLOB_TILDE /* Allow ~ processing -- Linux extension */
#define GLOB_TILDE 0
#endif
#ifndef GLOB_NOMATCH
#define GLOB_NOMATCH 0 /* Some versions don't have a special return for this */
#endif
#define GLOB_FLAGS (GLOB_ERR|GLOB_NOSORT|GLOB_BRACE|GLOB_TILDE)
#define HELP if (argc > 1 && (strcmp(argv[1],"-help")==0 \
|| strcmp(argv[1],"--help")==0)) \
{ printf("\nUsage: %s\n\n%s",USAGE,HELPTEXT); return 0;}
/****************************************************************************/
static int
numstrcmp(char *s1, char *s2)
/* Same behaviour as strcmp(), except that when an unsigned integer is
found in each string, the numerical values are compared instead of
the ascii values. Overflow is impossible. Leading spaces before
numbers are considered part of the numbers. A number in one string
is considered less than a non-number in the other string. */
{
char *a1,*a2;
while (1)
{
for (a1 = s1; *a1 == ' '; ++a1) {}
if (isdigit(*a1))
{
for (s1 = a1+1; isdigit(*s1); ++s1) {}
}
else
{
a1 = s1;
++s1;
}
for (a2 = s2; *a2 == ' '; ++a2) {}
if (isdigit(*a2))
{
for (s2 = a2+1; isdigit(*s2); ++s2) {}
}
else
{
a2 = s2;
++s2;
}
if (!isdigit(*a1))
{
if (!isdigit(*a2))
{
if (*a1 < *a2) return -1;
if (*a1 > *a2) return 1;
if (*a1 == '\0') return 0;
}
else
return 1;
}
else
{
if (!isdigit(*a2))
return -1;
else
{
for (; *a1 == '0'; ++a1) {}
for (; *a2 == '0'; ++a2) {}
if (s1-a1 < s2-a2) return -1;
if (s1-a1 > s2-a2) return 1;
for (; a1 < s1 && *a1 == *a2; ++a1, ++a2) {}
if (a1 < s1)
{
if (*a1 < *a2) return -1;
else return 1;
}
}
}
}
}
/****************************************************************************/
static int
readwidth(char **p)
/* Read an integer that starts at *p. Leave *p pointing
* to the character immediately following the integer. */
{
int ans;
char *s;
boolean neg;
s = *p;
ans = 0;
neg = FALSE;
if (*s == '-')
{
neg = TRUE;
++s;
}
while (isdigit(*s))
{
ans = 10*ans + (*s - '0');
++s;
}
*p = s;
return (neg ? -ans : ans);
}
/****************************************************************************/
static void
writeline(char *outf, number *val, unsigned long count)
/* Write an output line with the given format and values */
{
int i,n;
integer mins,nsecs;
double secs,hms;
boolean neg;
int width;
n = 0;
for (; *outf != '\0'; ++outf)
{
if (*outf == '%')
{
++outf;
if (*outf == '-' || isdigit(*outf))
width = readwidth(&outf);
else
width = 1;
if (*outf == '%' || *outf == '#')
putchar(*outf);
else if (*outf == 'd' || *outf == 'x'
|| *outf == 'n' || *outf == 'p')
printf(dout,width,val[n++].d);
else if (*outf == 'f')
printf(fout,width,val[n++].f);
else if (*outf == 'v')
printf(vout,width,val[n++].f/count);
else if (*outf == 'X')
printf(Xout,width,val[n++].f);
else if (*outf == 'h')
{
if (val[n].f < 0)
{
neg = TRUE;
hms = -val[n].f;
}
else
{
neg = FALSE;
hms = val[n].f;
}
mins = hms/60.0;
secs = hms - 60*mins;
nsecs = secs;
++n;
if (neg) printf("-");
if (secs == nsecs)
printf(hmsout1,mins/60,mins%60,nsecs);
else
printf(hmsout2,mins/60,mins%60,secs);
}
else if (*outf == 'l')
{
for (i = 0; i < val[n].l->nvals; ++i)
{
if (i > 0) printf(" ");
printf(dout,width,val[n].l->val[i]);
}
++n;
}
#if GMP
else if (*outf == 'm')
mpz_out_str(NULL,10,*(val[n++].m));
#endif
else
{
fprintf(stderr,">E unknown output format %%%c\n",*outf);
exit(1);
}
}
else
putchar(*outf);
}
}
/*********************************************************************/
static void
print_counts(countnode *root, boolean prefix, boolean printcounts)
/* Use a non-recursive inorder traversal to print the tree */
{
int code;
countnode *p;
char *fmt;
p = root;
code = A;
while (p)
{
switch (code) /* deliberate flow-ons */
{
case A:
if (p->left)
{
p = p->left;
break;
}
case L:
if (printcounts) printf("%5lu: ",p->count);
fmt = p->fmt;
if (prefix && fmt[0] == '>'
&& ((fmt[1] >= 'a' && fmt[1] <= 'z')
|| (fmt[1] >= 'A' && fmt[1] <= 'Z')))
fmt += 2;
writeline(fmt,p->total,p->count);
if (p->right)
{
p = p->right;
code = A;
break;
}
case R:
if (p->parent && p->parent->left == p) code = L;
else code = R;
p = p->parent;
break;
}
}
}
/*********************************************************************/
static void
print_common(countnode *root, boolean prefix)
/* Print the common ends of the formats in the tree */
{
int code;
countnode *p;
char *s0,*s1,*t0,*t1,*fmt;
int i,comm0,comm1,minlen,maxlen;
if (root == NULL) return;
p = root;
code = A;
fmt = p->fmt;
if (prefix && fmt[0] == '>'
&& ((fmt[1] >= 'a' && fmt[1] <= 'z')
|| (fmt[1] >= 'A' && fmt[1] <= 'Z')))
fmt += 2;
s0 = s1 = fmt;
while (*s1 != '\0') ++s1;
comm0 = comm1 = minlen = maxlen = s1-s0;
while (p)
{
switch (code) /* deliberate flow-ons */
{
case A:
if (p->left)
{
p = p->left;
break;
}
case L:
fmt = p->fmt;
if (prefix && fmt[0] == '>'
&& ((fmt[1] >= 'a' && fmt[1] <= 'z')
|| (fmt[1] >= 'A' && fmt[1] <= 'Z')))
fmt += 2;
t0 = t1 = fmt;
for (i = 0; i < comm0; ++i)
if (s0[i] != t0[i]) break;
comm0 = i;
while (*t1 != '\0') ++t1;
for (i = 1; i <= comm1; ++i)
if (s1[-i] != t1[-i]) break;
comm1 = i-1;
if (t1-t0 < minlen) minlen = t1-t0;
if (t1-t0 > maxlen) maxlen = t1-t0;
if (p->right)
{
p = p->right;
code = A;
break;
}
case R:
if (p->parent && p->parent->left == p) code = L;
else code = R;
p = p->parent;
break;
}
}
if (comm0 + comm1 > minlen) comm1 = minlen - comm0;
for (i = 0; i < comm0; ++i)
printf("%c",s0[i]);
if (comm0 + comm1 < maxlen) printf("*");
for (i = comm1; i > 0; --i)
printf("%c",s1[-i]);
}
/*********************************************************************/
static void
splay(countnode *p)
/* Splay the node p. It becomes the new root. */
{
countnode *q,*r,*s;
countnode *a,*b,*c;
int code;
#define LCHILD(x,y) {(x)->left = y; if (y) (y)->parent = x;}
#define RCHILD(x,y) {(x)->right = y; if (y) (y)->parent = x;}
while (p->parent)
{
a = p->left;
b = p->right;
q = p->parent;
if (q->left == p)
{
code = L;
c = q->right;
}
else
{
code = R;
c = q->left;
}
r = q->parent;
if (r)
{
if (r->left == q) code = (code == L ? LL : LR);
else code = (code == L ? RL : RR);
s = r->parent;
p->parent = s;
if (s)
{
if (s->left == r) s->left = p;
else s->right = p;
}
}
else
{
p->parent = NULL;
}
switch (code)
{
case L:
RCHILD(p,q); LCHILD(q,b); break;
case R:
LCHILD(p,q); RCHILD(q,a); break;
case LL:
RCHILD(p,q); RCHILD(q,r); LCHILD(q,b); LCHILD(r,c); break;
case RR:
LCHILD(p,q); LCHILD(q,r); RCHILD(r,c); RCHILD(q,a); break;
case LR:
LCHILD(p,q); RCHILD(p,r); RCHILD(q,a); LCHILD(r,b); break;
case RL:
LCHILD(p,r); RCHILD(p,q); RCHILD(r,a); LCHILD(q,b); break;
}
}
}
/*********************************************************************/
static void
add_one(countnode **to_root, char *fmt, integer pmod, int nval,
number *val, int *valtype, int which, boolean numcompare)
/* Add one match to the node with the given format, creating it if it is new.
The tree is then splayed to ensure good efficiency. */
{
int i,j,cmp;
countnode *p,*ppar,*new_node;
integer w;
p = *to_root;
cmp = 0;
while (p != NULL)
{
cmp = (numcompare ? numstrcmp(fmt,p->fmt) : strcmp(fmt,p->fmt));
if (cmp == 0)
{
if (HAS(which,UNIQUE) && p->count == 1)
printf("ERROR: Multiple matches for %s",fmt);
for (i = 0; i < nval; ++i)
if (valtype[i] == D)
{INCR(p->total[i].d,val[i].d);}
else if (valtype[i] == X)
{if (val[i].d > p->total[i].d) p->total[i].d = val[i].d;}
else if (valtype[i] == N)
{if (val[i].d < p->total[i].d) p->total[i].d = val[i].d;}
else if (valtype[i] == P)
{w = val[i].d % pmod; INCR(p->total[i].d,w);
p->total[i].d %= pmod;}
else if (valtype[i] == LD)
{
if (p->total[i].l->nvals < val[i].l->nvals)
{
if ((p->total[i].l->val
= (integer*)realloc(p->total[i].l->val,
sizeof(integer)*val[i].l->nvals))
== NULL)
{
fprintf(stderr,"Malloc failed\n");
exit(1);
}
}
for (j = 0; j < p->total[i].l->nvals &&
j < val[i].l->nvals; ++j)
INCR(p->total[i].l->val[j],val[i].l->val[j]);
if (p->total[i].l->nvals < val[i].l->nvals)
{
for (j = p->total[i].l->nvals;
j < val[i].l->nvals; ++j)
p->total[i].l->val[j] = val[i].l->val[j];
p->total[i].l->nvals = val[i].l->nvals;
}
}
#if GMP
else if (valtype[i] == M)
mpz_add(*(p->total[i].m),*(p->total[i].m),*(val[i].m));
#endif
else if (valtype[i] == FX)
{if (val[i].f > p->total[i].f) p->total[i].f = val[i].f;}
else
p->total[i].f += val[i].f; /* F, V and H */
++p->count;
splay(p);
*to_root = p;
return;
}
else if (cmp < 0)
{
ppar = p;
p = p->left;
}
else
{
ppar = p;
p = p->right;
}
}
if ((new_node = (countnode*)malloc(sizeof(countnode))) == NULL)
{
fprintf(stderr,">E malloc failed in add_one()\n");
exit(1);
}
if ((new_node->fmt = (char*)malloc(strlen(fmt)+1)) == NULL)
{
fprintf(stderr,">E malloc failed in add_one()\n");
exit(1);
}
new_node->count = 1;
strcpy(new_node->fmt,fmt);
for (i = 0; i < nval; ++i)
{
#if GMP
if (valtype[i] == M)
{
if ((new_node->total[i].m
= (mpz_t*)malloc(sizeof(mpz_t))) == NULL)
{
fprintf(stderr,"Malloc failed\n");
exit(1);
}
mpz_init_set(*(new_node->total[i].m),*(val[i].m));
}
else
#endif
if (valtype[i] == LD)
{
if ((new_node->total[i].l
= (integerlist*)malloc(sizeof(integerlist))) == NULL)
{
fprintf(stderr,"Malloc failed\n");
exit(1);
}
if ((new_node->total[i].l->val
= (integer*)malloc(sizeof(integer)*val[i].l->nvals)) == NULL)
{
fprintf(stderr,"Malloc failed\n");
exit(1);
}
new_node->total[i].l->nvals = val[i].l->nvals;
for (j = 0; j < val[i].l->nvals; ++j)
new_node->total[i].l->val[j] = val[i].l->val[j];
}
else
new_node->total[i] = val[i];
}
new_node->left = new_node->right = NULL;
if (cmp == 0)
{
*to_root = new_node;
new_node->parent = NULL;
}
else if (cmp < 0)
{
ppar->left = new_node;
new_node->parent = ppar;
}
else
{
ppar->right = new_node;
new_node->parent = ppar;
}
splay(new_node);
*to_root = new_node;
}
/****************************************************************************/
static int
scanline(char *s, char *f, number *val, int *valtype,
integer *seqno, char *outf)
/* Perform sscanf-like scan of line.
The whole format must match. outf is set to be an output format
with unassigned values replaced by '*' and %s replaced by what
it matches. Assigned values except %s are put into val[] with
their types in valtype[]. The number of values (not counting %#)
is returned.
Integers matching %# are put into *seqno, with an error if there
are more than one, and -1 if there are none.
If the format doesn't match, -1 is returned.
WARNING: the gmp and ilist values are pointers to static data,
so they need to be copied if the values array is copied.
See the comments at the start of the program for more information.
*/
{
int n; /* Number of values assigned */
int digit;
boolean doass,neg,oflow,badgmp;
integer ival;
double dval,digval,comval;
char ends,*saves;
static boolean gmp_warning = FALSE;
integer *ilist;
size_t ilist_sz;
int nilist,width;
#if GMP
char mp_line[MAXLINELEN+1],*mp;
#endif
n = 0;
*seqno = -1;
badgmp = oflow = FALSE;
while (*f != '\0')
{
if (*f == '%')
{
++f;
if (*f == '*')
{
doass = FALSE;
++f;
}
else
doass = TRUE;
if (*f == '-' || isdigit(*f))
width = readwidth(&f);
else
width = 0;
if (*f == '%')
{
if (!doass)
{
fprintf(stderr,"Bad format item %%*\n");
exit(1);
}
if (*s++ != '%') return -1;
++f;
*outf++ = '%';
*outf++ = '%';
}
else if (*f == '\n')
{
if (!doass)
{
fprintf(stderr,"Bad format item %%*\n");
exit(1);
}
while (*s != '\0')
{
if (*s != ' ' && *s != '\n') return -1;
++s;
}
--s;
}
else if (*f == 'c')
{
if (*s == ' ' || *s == '\t' || *s == '\n') return -1;
if (doass) *outf++ = *s;
else *outf++ = '*';
++f;
++s;
}
else if (*f == 's')
{
ends = *(f+1);
if (ends == ' ')
{
while (*s == ' ' || *s == '\t')
{
if (doass) *outf++ = *s;
++s;
}
}
while (*s != '\n' && *s != ends)
{
if (doass) *outf++ = *s;
++s;
}
if (!doass) *outf++ = '*';
++f;
}
#if GMP
else if (*f == 'd' || *f == 'x' || *f == 'n' || *f == 'p')
{
#else
else if (*f == 'd' || *f == 'x' || *f == 'n'
|| *f == 'p' || *f == 'm')
{
if (*f == 'm' && !gmp_warning)
{
fprintf(stderr,
">W not compiled with GMP, treating %%m like %%d\n");
gmp_warning = TRUE;
}
#endif
while (*s == ' ' || *s == '\t') ++s;
if (!isdigit(*s) && *s != '-' && *s != '+') return -1;
neg = (*s == '-');
if (*s == '-' || *s == '+') ++s;
ival = 0;
while (isdigit(*s))
{
digit = *s++ - '0';
if (ival > (maxint-digit)/10)
oflow = TRUE;
else
ival = ival*10 + digit;
}
if (doass)
{
*outf++ = '%';
if (width != 0)
{
sprintf(outf,"%d",width);
while (*outf != '\0') ++outf;
}
if (*f == 'd' || *f == 'm')
{
*outf++ = 'd';
valtype[n] = D;
}
else if (*f == 'x')
{
*outf++ = 'x';
valtype[n] = X;
}
else if (*f == 'n')
{
*outf++ = 'n';
valtype[n] = N;
}
else
{
*outf++ = 'p';
valtype[n] = P;
}
val[n++].d = (neg ? -ival : ival);
}
else
*outf++ = '*';
++f;
}
else if (*f == 'l')
{
nilist = 0;
if ((ilist = (integer*)malloc(200*sizeof(integer)))
== NULL)
{
fprintf(stderr,"Malloc failed\n");
exit(1);
}
ilist_sz = 200;
for (;;)
{
saves = s;
while (*s == ' ' || *s == '\t') ++s;
if (!isdigit(*s) && *s != '-' && *s != '+')
{
s = saves;
break;
}
neg = (*s == '-');
if (*s == '-' || *s == '+') ++s;
ival = 0;
while (isdigit(*s))
{
digit = *s++ - '0';
if (ival > (maxint-digit)/10)
oflow = TRUE;
else
ival = ival*10 + digit;
}
if (neg) ival = -ival;
if (nilist == ilist_sz)
{
if ((ilist
= (integer*)realloc((void*)ilist,
(ilist_sz+500)*sizeof(integer)))
== NULL)
{
fprintf(stderr,"Malloc failed\n");
exit(1);
}
ilist_sz += 500;
}
ilist[nilist++] = ival;
}
if (doass)
{
valtype[n] = LD;
val[n].l = &il[n];
val[n].l->nvals = nilist;
if (val[n].l->val) free(val[n].l->val);
val[n].l->val = ilist;
++n;
*outf++ = '%';
if (width != 0)
{
sprintf(outf,"%d",width);
while (*outf != '\0') ++outf;
}
*outf++ = 'l';
}
else
{
free(ilist);
*outf++ = '*';
}
++f;
}
#if GMP
else if (*f == 'm')
{
while (*s == ' ' || *s == '\t') ++s;
if (!isdigit(*s) && *s != '-' && *s != '+') return -1;
mp = mp_line;
if (*s == '-') *mp++ = *s++;
else if (*s == '+') s++;
while (isdigit(*s)) *mp++ = *s++;
*mp = '\0';
if (doass)
{
valtype[n] = M;
val[n].m = &mp_value[n];
if (mpz_set_str(mp_value[n],mp_line,10) < 0)
badgmp = TRUE;
++n;
*outf++ = '%';
*outf++ = 'm';
}
else
*outf++ = '*';
++f;
}
#endif
else if (*f == '#')
{
while (*s == ' ' || *s == '\t') ++s;
if (!isdigit(*s)) return -1;
ival = 0;
while (isdigit(*s))
{
digit = *s++ - '0';
if (ival > (maxint-digit)/10)
oflow = TRUE;
else
ival = ival*10 + digit;
}
if (*seqno >= 0)
{
fprintf(stderr,
">E %%# can only be used once per format\n");
exit(1);
}
*seqno = ival;
*outf++ = '#';
++f;
}
else if (*f == 'f' || *f == 'v' || *f == 'X')
{
while (*s == ' ' || *s == '\t') ++s;
if (!isdigit(*s) && *s != '.' && *s != '-' && *s != '+')
return -1;
neg = (*s == '-');
if (*s == '-' || *s == '+') ++s;
dval = 0.0;
while (isdigit(*s)) dval = dval*10.0 + (*s++ - '0');
if (*s == '.')
{
digval = 1.0;
++s;
while (isdigit(*s))
{
digval /= 10.0;
dval += (*s++ - '0') * digval;
}
}
if (doass)
{
valtype[n] = (*f == 'f' ? F : (*f == 'v' ? V : FX));
val[n++].f = (neg ? -dval : dval);
*outf++ = '%';
if (width != 0)
{
sprintf(outf,"%d",width);
while (*outf != '\0') ++outf;
}
*outf++ = *f;
}
else
*outf++ = '*';
++f;
}
else if (*f == 'h')
{
while (*s == ' ' || *s == '\t') ++s;
if (!isdigit(*s) && *s != '.' && *s != '-' && *s != '+' && *s != ':')
return -1;
neg = (*s == '-');
if (*s == '-' || *s == '+') ++s;
dval = 0.0;
comval = 0.0;
while (isdigit(*s) || *s == ':')
{
if (*s == ':')
{
dval = dval*60.0 + comval;
comval = 0.0;
++s;
}
else
comval = comval*10.0 + (*s++ - '0');
}
if (*s == '.')
{
digval = 1.0;
++s;
while (isdigit(*s))
{
digval /= 10.0;
comval += (*s++ - '0') * digval;
}
}
dval = dval*60.0 + comval;
if (doass)
{
valtype[n] = H;
val[n++].f = (neg ? -dval : dval);
*outf++ = '%';
*outf++ = *f;
}
else
*outf++ = '*';
++f;
}
else if (*f == ' ')
{
while (*s == ' ' || *s == '\t') ++s;
*outf++ = ' ';
++f;
}
else
{
fprintf(stderr,"Bad format item %%%c\n",*f);
exit(1);
}
}
else
{
if (*s != *f) return -1;
*outf++ = *f;
++s;
++f;
}
}
if (*s != '\0') return -1;
*outf = '\0';
if (oflow)
{
fprintf(stderr,"Integer too large\n");
exit(1);
}
if (badgmp)
{
fprintf(stderr,"Illegal multiprecision integer\n");
exit(1);
}
return n;
}
/****************************************************************************/
void
find_maxint(void)
{
/* Put the maximum possible integer value into maxint. */
/* New version with no integer overflow. */
integer x,y;
x = ((integer)1) << (8*sizeof(integer) - 2);
y = x - 1;
x += y;
if (x <= 0)
{
fprintf(stderr,">E find_maxint() failed\n");
exit(1);
}
maxint = x;
}
/****************************************************************************/
static void
sort_formats(int *order, int numformats)
/* Make order[0..numformats-1] a permutation of 0..numformats-1 being
a good order to display the results. */
{
double score[MAXFORMATS];
int h,i,j,iw;
for (i = 0; i < numformats; ++i)
{
if (matching_lines[i] == 0)
score[i] = -1.0;
else
score[i] = i +
((100.0*total_position[i]) / matching_lines[i]) * numformats;
order[i] = i;
}
j = numformats / 3;
h = 1;
do
h = 3 * h + 1;
while (h < j);
do
{
for (i = h; i < numformats; ++i)
{
iw = order[i];
for (j = i; score[order[j-h]] > score[iw]; )
{
order[j] = order[j-h];
if ((j -= h) < h) break;
}
order[j] = iw;
}
h /= 3;
}
while (h > 0);
}
/****************************************************************************/
static void
read_formats(char *filename, int *numformatsp, boolean mustexist)
/* Read formats from the given file. */
{
FILE *f;
int i,c,flags,ignore,delim;
char flagname[52];
char line[MAXLINELEN+3];
integer pmod;
char *s,*before,*after;
boolean oflow,badpmod;
int digit;
#define BEFORELEN 255
char str[BEFORELEN+2];
if (strcmp(filename,"-") == 0)
f = stdin;
else if ((f = fopen(filename,"r")) == NULL)
{
if (mustexist)
{
fprintf(stderr,">E Can't open %s for reading.\n",filename);
exit(1);
}
return;
}
line[MAXLINELEN+2] = '\0';
for (;;)
{
if ((c = getc(f)) == EOF) break;
while (c == ' ' || c == '\t') c = getc(f);
if (c == '\n') continue;
if (c == EOF) break;
if (c == '#')
{
while (c != '\n' && c != EOF) c = getc(f);
continue;
}
ungetc(c,f);
flags = 0;
before = after = NULL;
pmod = 2;
for (;;)
{
while ((c = getc(f)) == ' '
|| c == '|' || c == ',' || c == '\t') {}
if (c == '#')
while (c != '\n' && c != EOF) c = getc(f);
if (c == '\n' || c == EOF) break;
ungetc(c,f);
/* There appear to be some issues with the [ flag in fscanf,
* as to whether a null is appended. We'll take no chances. */
for (i = 0; i < 52; ++i) flagname[i] = '\0';
ignore = fscanf(f,"%50[A-Za-z0-9=]",flagname);
if (strcmp(flagname,"DEFAULT") == 0) {}
else if (strcmp(flagname,"FINAL") == 0) flags |= FINAL;
else if (strcmp(flagname,"ERROR") == 0) flags |= ERROR;
else if (strcmp(flagname,"UNIQUE") == 0) flags |= UNIQUE;
else if (strcmp(flagname,"COUNT") == 0) flags |= COUNT;
else if (strcmp(flagname,"CONTINUE") == 0) flags |= CONTINUE;
else if (strcmp(flagname,"NUMERIC") == 0) flags |= NUMERIC;
else if (strcmp(flagname,"SILENT") == 0) flags |= SILENT;
else if (strcmp(flagname,"ENDFILE") == 0) flags |= ENDFILE;
else if (strcmp(flagname,"NOPREFIX") == 0) flags |= NOPREFIX;
else if (strcmp(flagname,"BEFORE") == 0)
{
flags |= BEFORE;
before = NULL;
delim = fgetc(f);
if (delim != ' ' && delim != '\n'
&& delim != '\t' && delim != EOF)
{
i = 0;
for (;;)
{
c = fgetc(f);
if (c == delim || c == EOF || c == '\n') break;
if (i < BEFORELEN) str[i] = c;
++i;
}
str[i] = '\0';
if (c == '\n') ungetc(c,f);
before = strdup(str);
}
if (delim == '\n') ungetc(delim,f);
}
else if (strcmp(flagname,"AFTER") == 0)
{
flags |= AFTER;
after = NULL;
delim = fgetc(f);
if (delim != ' ' && delim != '\n'
&& delim != '\t' && delim != EOF)
{
i = 0;
for (;;)
{
c = fgetc(f);
if (c == delim || c == EOF || c == '\n') break;
if (i < BEFORELEN) str[i] = c;
++i;
}
str[i] = '\0';
if (c == '\n') ungetc(c,f);
after = strdup(str);
}
if (delim == '\n') ungetc(delim,f);
}
else if (flagname[0] == 'P' && flagname[1] == '=')
{
pmod = 0;
oflow = FALSE;
badpmod = (flagname[2] == '\0');
for (s = flagname+2; *s != '\0'; ++s)
{
if (isdigit(*s))
{
digit = *s - '0';
if (pmod > (maxint-digit)/10)
oflow = TRUE;
else
pmod = pmod*10 + digit;
}
else
badpmod = TRUE;
}
if (badpmod)
{
fprintf(stderr,">E Bad value for P= directive: %s\n",
flagname+2);
exit(1);
}
else if (oflow)
{
fprintf(stderr,">E Value for P= is too large\n");
exit(1);
}
}
else
{
fprintf(stderr,">E Unknown flag \"%s\" in %s\n",
flagname,filename);
exit(1);
}
}
if (fgets(line,MAXLINELEN,f) == NULL)
{
fprintf(stderr,">E Missing format in %s\n",filename);
exit(1);
}
for (i = 0; i < *numformatsp; ++i)
if (strcmp(line,format[i].fmt) == 0) break;
if (i < *numformatsp) continue;
if (*numformatsp == MAXFORMATS)
{
fprintf(stderr,">E Increase MAXFORMATS\n");
exit(1);
}
format[*numformatsp].flags = flags;
format[*numformatsp].pmod = pmod;
if ((format[*numformatsp].fmt
= (char*)malloc(strlen(line)+1)) == NULL)
{
fprintf(stderr,">E malloc() failed in read_formats()\n");
exit(1);
}
format[*numformatsp].before = before;
format[*numformatsp].after = after;
strcpy(format[*numformatsp].fmt,line);
++*numformatsp;
}
if (f != stdin) fclose(f);
}
/****************************************************************************/
static void
read_local_formats(int *numformatsp)
/* Read formats from sumlines.fmt in current directory */
{
read_formats("sumlines.fmt",numformatsp,FALSE);
}
/****************************************************************************/
static void
read_global_formats(int *numformatsp)
/* Read formats from sumlines.fmt in home directory */
{
struct passwd *pwd;
char *homedir;
char filename[4097];
homedir = getenv("HOME");
if (homedir == NULL && (pwd = getpwuid(getuid())) != NULL)
homedir = pwd->pw_dir;
if (homedir == NULL)
{
fprintf(stderr,">W Can't find home directory\n");
return;
}
sprintf(filename,"%s/sumlines.fmt",homedir);
read_formats(filename,numformatsp,FALSE);
}
/****************************************************************************/
static void
read_env_formats(int *numformatsp)
/* Read formats from $SUMLINES.FMT if it exists */
{
char *filename;
if ((filename = getenv("SUMLINES.FMT")) != 0)
read_formats(filename,numformatsp,FALSE);
}
/****************************************************************************/
static boolean
readoneline(FILE *f, char *line, int size, int *nulls)
/* Get a line. Read at most size-1 chars until EOF or \n.
If \n is read, it is stored. Then \0 is appended.
*nulls is set to the number of NUL chars (which are also stored). */
{
int i,c;
*nulls = 0;
for (i = 0; i < size-1; ++i)
{
c = getc(f);
if (c == EOF) break;
line[i] = c;
if (c == '\0') ++*nulls;
if (c == '\n') {++i; break;}
}
line[i] = '\0';
return i > 0;
}
/****************************************************************************/
static int
pnumstrcmp(const void *a, const void *b)
/* numstrcmp on strings pointed at by a and b */
{
return numstrcmp(*(char**)a,*(char**)b);
}
/****************************************************************************/
static void
doglob(char *patt, glob_t *globlk)
/* Find all files matching the given pattern, numeric sorting.
Give a warning message if there are none. */
{
int ret;
ret = glob(patt,GLOB_FLAGS,NULL,globlk);
if (ret != 0) globlk->gl_pathc = 0;
if (ret == GLOB_NOSPACE)
{
fprintf(stderr,"ERROR: ran out of space during glob()\n");
exit(1);
}
if (ret == GLOB_ERR)
{
fprintf(stderr,"ERROR: during glob(%s)\n",patt);
exit(1);
}
if (ret != 0 && ret != GLOB_NOMATCH)
{
fprintf(stderr,"ERROR: value %d from glob(%s)\n",ret,patt);
exit(1);
}
if (globlk->gl_pathc == 0) printf("WARNING: no files match %s\n",patt);
if (globlk->gl_pathc >= 2)
qsort(globlk->gl_pathv,globlk->gl_pathc,sizeof(char*),pnumstrcmp);
}
/****************************************************************************/
int
main(int argc, char *argv[])
{
int i,j,nvals,argnum;
number val[MAXVALUES];
int valtype[MAXVALUES];
char line[MAXLINELEN+2];
char outf[MAXLINELEN+MAXVALUES+6];
unsigned long matched,unmatched,finalmatched;
unsigned long errorlines,totalerrorlines;
unsigned long line_number,nullcount,numfiles,ifile;
char *filename;
FILE *infile;
int numformats,firstarg,nulls;
boolean havefinal,nowarn,noWarn,listformats,readfiles;
integer seq;
int order[MAXFORMATS];
glob_t globlk,globlk_stdin,*pglob;
char *glob_stdin_v[2];
boolean printcounts;
HELP;
find_maxint();
firstarg = 1;
numformats = 0;
nowarn = noWarn = FALSE;
listformats = FALSE;
readfiles = TRUE;
printcounts = TRUE;
globlk_stdin.gl_pathc = 1;
globlk_stdin.gl_pathv = glob_stdin_v;
glob_stdin_v[0] = "-";
glob_stdin_v[1] = NULL;
dout = DOUT;
fout = FOUT;
vout = VOUT;
Xout = FOUT;
hmsout1 = HMSOUT1;
hmsout2 = HMSOUT2;
for (; firstarg < argc; ++firstarg)
{
if (argv[firstarg][0] == '-' && argv[firstarg][1] == 'f')
{
if (argv[firstarg][2] != '\0')
read_formats(&argv[firstarg][2],&numformats,TRUE);
else if (firstarg == argc - 1)
{
fprintf(stderr,">E No argument for -f\n");
exit(1);
}
else
{
++firstarg;
read_formats(argv[firstarg],&numformats,TRUE);
}
}
else if (strcmp(argv[firstarg],"-W") == 0)
noWarn = TRUE;
else if (strcmp(argv[firstarg],"-w") == 0)
nowarn = TRUE;
else if (strcmp(argv[firstarg],"-v") == 0)
listformats = TRUE;
else if (strcmp(argv[firstarg],"-d") == 0)
readfiles = FALSE;
else if (strcmp(argv[firstarg],"-n") == 0)
printcounts = FALSE;
else if (strcmp(argv[firstarg],"-V") == 0)
vout = argv[++firstarg];
else if (strcmp(argv[firstarg],"-F") == 0)
fout = argv[++firstarg];
else if (strcmp(argv[firstarg],"-D") == 0)
dout = argv[++firstarg];
else
break;
}
#if GMP
for (i = 0; i < MAXVALUES; ++i) mpz_init(mp_value[i]);
#endif
for (i = 0; i < MAXVALUES; ++i)
{
il[i].nvals = 0;
il[i].val = NULL;
}
if (noWarn) nowarn = TRUE;
if (readfiles) read_local_formats(&numformats);
if (readfiles) read_env_formats(&numformats);
if (readfiles) read_global_formats(&numformats);
if (listformats)
{
printf("%d formats:\n",numformats);
for (i = 0; i < numformats; ++i)
printf("%03x %s",format[i].flags,format[i].fmt);
}
if (numformats == 0)
{
fprintf(stderr,">E No formats\n");
exit(1);
}
havefinal = FALSE;
for (i = 0; i < numformats; ++i)
{
count_root[i] = NULL;
matching_lines[i] = 0;
total_position[i] = 0;
if (HAS(i,FINAL)) havefinal = TRUE;
}
unmatched = totalerrorlines = 0;
numfiles = 0;
for (argnum = firstarg;
argnum < (argc == firstarg ? argc+1 : argc); ++argnum)
{
if (argnum >= argc || strcmp(argv[argnum],"-") == 0)
pglob = &globlk_stdin;
else
{
pglob = &globlk;
doglob(argv[argnum],pglob);
}
for (ifile = 0; ifile < pglob->gl_pathc; ++ifile)
{
matched = finalmatched = errorlines = 0;
++numfiles;
if (strcmp(pglob->gl_pathv[ifile],"-") == 0)
{
filename = "stdin";
infile = stdin;
}
else
{
filename = pglob->gl_pathv[ifile];
if ((infile = fopen(filename,"r")) == NULL)
{
fprintf(stderr,">E Can't open %s\n",filename);
exit(1);
}
}
line_number = 0;
nullcount = 0;
while (readoneline(infile,line,MAXLINELEN,&nulls))
{
nullcount += nulls;
line[MAXLINELEN] = '\n';
line[MAXLINELEN+1] = '\0';
if (line[0] == '\n') continue;
++line_number;
for (i = 0; i < numformats; ++i)
{
nvals
= scanline(line,format[i].fmt,val,valtype,&seq,outf);
if (nvals >= 0)
{
if (HAS(i,ENDFILE)) line_number = 0;
++matched;
if (HAS(i,FINAL)) ++finalmatched;
if (HAS(i,ERROR)) ++errorlines;
++matching_lines[i];
total_position[i] += line_number;
add_one(&count_root[i],outf,format[i].pmod,nvals,
val,valtype,i,HAS(i,NUMERIC));
if (!noWarn && matching_lines[i] > 1 && seq >= 0
&& seq != lastseq[i]+1)
{
printf("WARNING: Sequence number");
if (seq == lastseq[i])
{
printf(" ");
printf(dout,1,seq);
printf(" is repeated.\n");
}
else if (seq != lastseq[i]+2)
{
printf("s ");
printf(dout,1,lastseq[i]+1);
printf("-");
printf(dout,1,seq-1);
printf(" are missing.\n");
}
else
{
printf(" ");
printf(dout,1,seq-1);
printf(" is missing.\n");
}
}
lastseq[i] = seq;
if (!HAS(i,CONTINUE)) break;
}
}
if (i == numformats) ++unmatched;
}
if (errorlines != 0)
printf("ERRORS: Error lines in file %s\n",filename);
else if (matched == 0 && !nowarn)
printf("WARNING: No matching lines in file %s\n",filename);
else if (finalmatched == 0 && havefinal && !nowarn)
printf("WARNING: No final lines in file %s\n",filename);
if (nullcount > 0)
printf("WARNING: %ld NULs found in file %s\n",
nullcount,filename);
if (infile != stdin) fclose(infile);
totalerrorlines += errorlines;
}
if (pglob == &globlk) globfree(pglob);
}
sort_formats(order,numformats);
for (j = 0; j < numformats; ++j)
{
i = order[j];
if (HAS(i,SILENT)) continue;
if (HAS(i,COUNT))
{
if (matching_lines[i] > 0)
{
if (HAS(i,BEFORE))
{
if (format[i].before) printf("%s\n",format[i].before);
else printf("\n");
}
printf("%5lu lines matched ",matching_lines[i]);
print_common(count_root[i],HAS(i,NOPREFIX));
if (HAS(i,AFTER))
{
if (format[i].after) printf("%s\n",format[i].after);
else printf("\n");
}
}
}
else
{
if (count_root[i])
{
if (HAS(i,BEFORE))
{
if (format[i].before) printf("%s\n",format[i].before);
else printf("\n");
}
print_counts(count_root[i],HAS(i,NOPREFIX),printcounts);
if (HAS(i,AFTER))
{
if (format[i].after) printf("%s\n",format[i].after);
else printf("\n");
}
}
}
}
if (unmatched > 0)
printf("%5lu non-empty lines not matched\n",unmatched);
if (argc > firstarg) printf("%5lu files read altogether\n",numfiles);
if (totalerrorlines > 0) printf("%5lu errors found\n",totalerrorlines);
exit(0);
}
|