This file is indexed.

/usr/bin/genhtml is in lcov 1.12-2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
#!/usr/bin/perl -w
#
#   Copyright (c) International Business Machines  Corp., 2002,2012
#
#   This program is free software;  you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or (at
#   your option) any later version.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY;  without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   General Public License for more details. 
#
#   You should have received a copy of the GNU General Public License
#   along with this program;  if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# genhtml
#
#   This script generates HTML output from .info files as created by the
#   geninfo script. Call it with --help and refer to the genhtml man page
#   to get information on usage and available options.
#
#
# History:
#   2002-08-23 created by Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
#                         IBM Lab Boeblingen
#        based on code by Manoj Iyer <manjo@mail.utexas.edu> and
#                         Megan Bock <mbock@us.ibm.com>
#                         IBM Austin
#   2002-08-27 / Peter Oberparleiter: implemented frame view
#   2002-08-29 / Peter Oberparleiter: implemented test description filtering
#                so that by default only descriptions for test cases which
#                actually hit some source lines are kept
#   2002-09-05 / Peter Oberparleiter: implemented --no-sourceview
#   2002-09-05 / Mike Kobler: One of my source file paths includes a "+" in
#                the directory name.  I found that genhtml.pl died when it
#                encountered it. I was able to fix the problem by modifying
#                the string with the escape character before parsing it.
#   2002-10-26 / Peter Oberparleiter: implemented --num-spaces
#   2003-04-07 / Peter Oberparleiter: fixed bug which resulted in an error
#                when trying to combine .info files containing data without
#                a test name
#   2003-04-10 / Peter Oberparleiter: extended fix by Mike to also cover
#                other special characters
#   2003-04-30 / Peter Oberparleiter: made info write to STDERR, not STDOUT
#   2003-07-10 / Peter Oberparleiter: added line checksum support
#   2004-08-09 / Peter Oberparleiter: added configuration file support
#   2005-03-04 / Cal Pierog: added legend to HTML output, fixed coloring of
#                "good coverage" background
#   2006-03-18 / Marcus Boerger: added --custom-intro, --custom-outro and
#                overwrite --no-prefix if --prefix is present
#   2006-03-20 / Peter Oberparleiter: changes to custom_* function (rename
#                to html_prolog/_epilog, minor modifications to implementation),
#                changed prefix/noprefix handling to be consistent with current
#                logic
#   2006-03-20 / Peter Oberparleiter: added --html-extension option
#   2008-07-14 / Tom Zoerner: added --function-coverage command line option;
#                added function table to source file page
#   2008-08-13 / Peter Oberparleiter: modified function coverage
#                implementation (now enabled per default),
#                introduced sorting option (enabled per default)
#

use strict;
use File::Basename;
use File::Temp qw(tempfile);
use Getopt::Long;
use Digest::MD5 qw(md5_base64);
use Cwd qw/abs_path/;


# Global constants
our $title		= "LCOV - code coverage report";
our $tool_dir		= abs_path(dirname($0));
our $lcov_version	= "LCOV version 1.12";
our $lcov_url		= "http://ltp.sourceforge.net/coverage/lcov.php";
our $tool_name		= basename($0);

# Specify coverage rate default precision
our $default_precision = 1;

# Specify coverage rate limits (in %) for classifying file entries
# HI:   $hi_limit <= rate <= 100          graph color: green
# MED: $med_limit <= rate <  $hi_limit    graph color: orange
# LO:          0  <= rate <  $med_limit   graph color: red

# For line coverage/all coverage types if not specified
our $hi_limit = 90;
our $med_limit = 75;

# For function coverage
our $fn_hi_limit;
our $fn_med_limit;

# For branch coverage
our $br_hi_limit;
our $br_med_limit;

# Width of overview image
our $overview_width = 80;

# Resolution of overview navigation: this number specifies the maximum
# difference in lines between the position a user selected from the overview
# and the position the source code window is scrolled to.
our $nav_resolution = 4;

# Clicking a line in the overview image should show the source code view at
# a position a bit further up so that the requested line is not the first
# line in the window. This number specifies that offset in lines.
our $nav_offset = 10;

# Clicking on a function name should show the source code at a position a
# few lines before the first line of code of that function. This number
# specifies that offset in lines.
our $func_offset = 2;

our $overview_title = "top level";

# Width for line coverage information in the source code view
our $line_field_width = 12;

# Width for branch coverage information in the source code view
our $br_field_width = 16;

# Internal Constants

# Header types
our $HDR_DIR		= 0;
our $HDR_FILE		= 1;
our $HDR_SOURCE		= 2;
our $HDR_TESTDESC	= 3;
our $HDR_FUNC		= 4;

# Sort types
our $SORT_FILE		= 0;
our $SORT_LINE		= 1;
our $SORT_FUNC		= 2;
our $SORT_BRANCH	= 3;

# Fileview heading types
our $HEAD_NO_DETAIL	= 1;
our $HEAD_DETAIL_HIDDEN	= 2;
our $HEAD_DETAIL_SHOWN	= 3;

# Offsets for storing branch coverage data in vectors
our $BR_BLOCK		= 0;
our $BR_BRANCH		= 1;
our $BR_TAKEN		= 2;
our $BR_VEC_ENTRIES	= 3;
our $BR_VEC_WIDTH	= 32;
our $BR_VEC_MAX		= vec(pack('b*', 1 x $BR_VEC_WIDTH), 0, $BR_VEC_WIDTH);

# Additional offsets used when converting branch coverage data to HTML
our $BR_LEN	= 3;
our $BR_OPEN	= 4;
our $BR_CLOSE	= 5;

# Branch data combination types
our $BR_SUB = 0;
our $BR_ADD = 1;

# Error classes which users may specify to ignore during processing
our $ERROR_SOURCE	= 0;
our %ERROR_ID = (
	"source" => $ERROR_SOURCE,
);

# Data related prototypes
sub print_usage(*);
sub gen_html();
sub html_create($$);
sub process_dir($);
sub process_file($$$);
sub info(@);
sub read_info_file($);
sub get_info_entry($);
sub set_info_entry($$$$$$$$$;$$$$$$);
sub get_prefix($@);
sub shorten_prefix($);
sub get_dir_list(@);
sub get_relative_base_path($);
sub read_testfile($);
sub get_date_string();
sub create_sub_dir($);
sub subtract_counts($$);
sub add_counts($$);
sub apply_baseline($$);
sub remove_unused_descriptions();
sub get_found_and_hit($);
sub get_affecting_tests($$$);
sub combine_info_files($$);
sub merge_checksums($$$);
sub combine_info_entries($$$);
sub apply_prefix($@);
sub system_no_output($@);
sub read_config($);
sub apply_config($);
sub get_html_prolog($);
sub get_html_epilog($);
sub write_dir_page($$$$$$$$$$$$$$$$$);
sub classify_rate($$$$);
sub br_taken_add($$);
sub br_taken_sub($$);
sub br_ivec_len($);
sub br_ivec_get($$);
sub br_ivec_push($$$$);
sub combine_brcount($$$);
sub get_br_found_and_hit($);
sub warn_handler($);
sub die_handler($);
sub parse_ignore_errors(@);
sub parse_dir_prefix(@);
sub rate($$;$$$);


# HTML related prototypes
sub escape_html($);
sub get_bar_graph_code($$$);

sub write_png_files();
sub write_htaccess_file();
sub write_css_file();
sub write_description_file($$$$$$$);
sub write_function_table(*$$$$$$$$$$);

sub write_html(*$);
sub write_html_prolog(*$$);
sub write_html_epilog(*$;$);

sub write_header(*$$$$$$$$$$);
sub write_header_prolog(*$);
sub write_header_line(*@);
sub write_header_epilog(*$);

sub write_file_table(*$$$$$$$);
sub write_file_table_prolog(*$@);
sub write_file_table_entry(*$$$@);
sub write_file_table_detail_entry(*$@);
sub write_file_table_epilog(*);

sub write_test_table_prolog(*$);
sub write_test_table_entry(*$$);
sub write_test_table_epilog(*);

sub write_source($$$$$$$);
sub write_source_prolog(*);
sub write_source_line(*$$$$$$);
sub write_source_epilog(*);

sub write_frameset(*$$$);
sub write_overview_line(*$$$);
sub write_overview(*$$$$);

# External prototype (defined in genpng)
sub gen_png($$$@);


# Global variables & initialization
our %info_data;		# Hash containing all data from .info file
our @opt_dir_prefix;	# Array of prefixes to remove from all sub directories
our @dir_prefix;
our %test_description;	# Hash containing test descriptions if available
our $date = get_date_string();

our @info_filenames;	# List of .info files to use as data source
our $test_title;	# Title for output as written to each page header
our $output_directory;	# Name of directory in which to store output
our $base_filename;	# Optional name of file containing baseline data
our $desc_filename;	# Name of file containing test descriptions
our $css_filename;	# Optional name of external stylesheet file to use
our $quiet;		# If set, suppress information messages
our $help;		# Help option flag
our $version;		# Version option flag
our $show_details;	# If set, generate detailed directory view
our $no_prefix;		# If set, do not remove filename prefix
our $func_coverage;	# If set, generate function coverage statistics
our $no_func_coverage;	# Disable func_coverage
our $br_coverage;	# If set, generate branch coverage statistics
our $no_br_coverage;	# Disable br_coverage
our $sort = 1;		# If set, provide directory listings with sorted entries
our $no_sort;		# Disable sort
our $frames;		# If set, use frames for source code view
our $keep_descriptions;	# If set, do not remove unused test case descriptions
our $no_sourceview;	# If set, do not create a source code view for each file
our $highlight;		# If set, highlight lines covered by converted data only
our $legend;		# If set, include legend in output
our $tab_size = 8;	# Number of spaces to use in place of tab
our $config;		# Configuration file contents
our $html_prolog_file;	# Custom HTML prolog file (up to and including <body>)
our $html_epilog_file;	# Custom HTML epilog file (from </body> onwards)
our $html_prolog;	# Actual HTML prolog
our $html_epilog;	# Actual HTML epilog
our $html_ext = "html";	# Extension for generated HTML files
our $html_gzip = 0;	# Compress with gzip
our $demangle_cpp = 0;	# Demangle C++ function names
our @opt_ignore_errors;	# Ignore certain error classes during processing
our @ignore;
our $opt_config_file;	# User-specified configuration file location
our %opt_rc;
our $charset = "UTF-8";	# Default charset for HTML pages
our @fileview_sortlist;
our @fileview_sortname = ("", "-sort-l", "-sort-f", "-sort-b");
our @funcview_sortlist;
our @rate_name = ("Lo", "Med", "Hi");
our @rate_png = ("ruby.png", "amber.png", "emerald.png");
our $lcov_func_coverage = 1;
our $lcov_branch_coverage = 0;
our $rc_desc_html = 0;	# lcovrc: genhtml_desc_html

our $cwd = `pwd`;	# Current working directory
chomp($cwd);


#
# Code entry point
#

$SIG{__WARN__} = \&warn_handler;
$SIG{__DIE__} = \&die_handler;

# Check command line for a configuration file name
Getopt::Long::Configure("pass_through", "no_auto_abbrev");
GetOptions("config-file=s" => \$opt_config_file,
	   "rc=s%" => \%opt_rc);
Getopt::Long::Configure("default");

{
	# Remove spaces around rc options
	my %new_opt_rc;

	while (my ($key, $value) = each(%opt_rc)) {
		$key =~ s/^\s+|\s+$//g;
		$value =~ s/^\s+|\s+$//g;

		$new_opt_rc{$key} = $value;
	}
	%opt_rc = %new_opt_rc;
}

# Read configuration file if available
if (defined($opt_config_file)) {
	$config = read_config($opt_config_file);
} elsif (defined($ENV{"HOME"}) && (-r $ENV{"HOME"}."/.lcovrc"))
{
	$config = read_config($ENV{"HOME"}."/.lcovrc");
}
elsif (-r "/etc/lcovrc")
{
	$config = read_config("/etc/lcovrc");
}

if ($config || %opt_rc)
{
	# Copy configuration file and --rc values to variables
	apply_config({
		"genhtml_css_file"		=> \$css_filename,
		"genhtml_hi_limit"		=> \$hi_limit,
		"genhtml_med_limit"		=> \$med_limit,
		"genhtml_line_field_width"	=> \$line_field_width,
		"genhtml_overview_width"	=> \$overview_width,
		"genhtml_nav_resolution"	=> \$nav_resolution,
		"genhtml_nav_offset"		=> \$nav_offset,
		"genhtml_keep_descriptions"	=> \$keep_descriptions,
		"genhtml_no_prefix"		=> \$no_prefix,
		"genhtml_no_source"		=> \$no_sourceview,
		"genhtml_num_spaces"		=> \$tab_size,
		"genhtml_highlight"		=> \$highlight,
		"genhtml_legend"		=> \$legend,
		"genhtml_html_prolog"		=> \$html_prolog_file,
		"genhtml_html_epilog"		=> \$html_epilog_file,
		"genhtml_html_extension"	=> \$html_ext,
		"genhtml_html_gzip"		=> \$html_gzip,
		"genhtml_precision"		=> \$default_precision,
		"genhtml_function_hi_limit"	=> \$fn_hi_limit,
		"genhtml_function_med_limit"	=> \$fn_med_limit,
		"genhtml_function_coverage"	=> \$func_coverage,
		"genhtml_branch_hi_limit"	=> \$br_hi_limit,
		"genhtml_branch_med_limit"	=> \$br_med_limit,
		"genhtml_branch_coverage"	=> \$br_coverage,
		"genhtml_branch_field_width"	=> \$br_field_width,
		"genhtml_sort"			=> \$sort,
		"genhtml_charset"		=> \$charset,
		"genhtml_desc_html"		=> \$rc_desc_html,
		"lcov_function_coverage"	=> \$lcov_func_coverage,
		"lcov_branch_coverage"		=> \$lcov_branch_coverage,
		});
}

# Copy related values if not specified
$fn_hi_limit	= $hi_limit if (!defined($fn_hi_limit));
$fn_med_limit	= $med_limit if (!defined($fn_med_limit));
$br_hi_limit	= $hi_limit if (!defined($br_hi_limit));
$br_med_limit	= $med_limit if (!defined($br_med_limit));
$func_coverage	= $lcov_func_coverage if (!defined($func_coverage));
$br_coverage	= $lcov_branch_coverage if (!defined($br_coverage));

# Parse command line options
if (!GetOptions("output-directory|o=s"	=> \$output_directory,
		"title|t=s"		=> \$test_title,
		"description-file|d=s"	=> \$desc_filename,
		"keep-descriptions|k"	=> \$keep_descriptions,
		"css-file|c=s"		=> \$css_filename,
		"baseline-file|b=s"	=> \$base_filename,
		"prefix|p=s"		=> \@opt_dir_prefix,
		"num-spaces=i"		=> \$tab_size,
		"no-prefix"		=> \$no_prefix,
		"no-sourceview"		=> \$no_sourceview,
		"show-details|s"	=> \$show_details,
		"frames|f"		=> \$frames,
		"highlight"		=> \$highlight,
		"legend"		=> \$legend,
		"quiet|q"		=> \$quiet,
		"help|h|?"		=> \$help,
		"version|v"		=> \$version,
		"html-prolog=s"		=> \$html_prolog_file,
		"html-epilog=s"		=> \$html_epilog_file,
		"html-extension=s"	=> \$html_ext,
		"html-gzip"		=> \$html_gzip,
		"function-coverage"	=> \$func_coverage,
		"no-function-coverage"	=> \$no_func_coverage,
		"branch-coverage"	=> \$br_coverage,
		"no-branch-coverage"	=> \$no_br_coverage,
		"sort"			=> \$sort,
		"no-sort"		=> \$no_sort,
		"demangle-cpp"		=> \$demangle_cpp,
		"ignore-errors=s"	=> \@opt_ignore_errors,
		"config-file=s"		=> \$opt_config_file,
		"rc=s%"			=> \%opt_rc,
		"precision=i"		=> \$default_precision,
		))
{
	print(STDERR "Use $tool_name --help to get usage information\n");
	exit(1);
} else {
	# Merge options
	if ($no_func_coverage) {
		$func_coverage = 0;
	}
	if ($no_br_coverage) {
		$br_coverage = 0;
	}

	# Merge sort options
	if ($no_sort) {
		$sort = 0;
	}
}

@info_filenames = @ARGV;

# Check for help option
if ($help)
{
	print_usage(*STDOUT);
	exit(0);
}

# Check for version option
if ($version)
{
	print("$tool_name: $lcov_version\n");
	exit(0);
}

# Determine which errors the user wants us to ignore
parse_ignore_errors(@opt_ignore_errors);

# Split the list of prefixes if needed
parse_dir_prefix(@opt_dir_prefix);

# Check for info filename
if (!@info_filenames)
{
	die("No filename specified\n".
	    "Use $tool_name --help to get usage information\n");
}

# Generate a title if none is specified
if (!$test_title)
{
	if (scalar(@info_filenames) == 1)
	{
		# Only one filename specified, use it as title
		$test_title = basename($info_filenames[0]);
	}
	else
	{
		# More than one filename specified, used default title
		$test_title = "unnamed";
	}
}

# Make sure css_filename is an absolute path (in case we're changing
# directories)
if ($css_filename)
{
	if (!($css_filename =~ /^\/(.*)$/))
	{
		$css_filename = $cwd."/".$css_filename;
	}
}

# Make sure tab_size is within valid range
if ($tab_size < 1)
{
	print(STDERR "ERROR: invalid number of spaces specified: ".
		     "$tab_size!\n");
	exit(1);
}

# Get HTML prolog and epilog
$html_prolog = get_html_prolog($html_prolog_file);
$html_epilog = get_html_epilog($html_epilog_file);

# Issue a warning if --no-sourceview is enabled together with --frames
if ($no_sourceview && defined($frames))
{
	warn("WARNING: option --frames disabled because --no-sourceview ".
	     "was specified!\n");
	$frames = undef;
}

# Issue a warning if --no-prefix is enabled together with --prefix
if ($no_prefix && @dir_prefix)
{
	warn("WARNING: option --prefix disabled because --no-prefix was ".
	     "specified!\n");
	@dir_prefix = undef;
}

@fileview_sortlist = ($SORT_FILE);
@funcview_sortlist = ($SORT_FILE);

if ($sort) {
	push(@fileview_sortlist, $SORT_LINE);
	push(@fileview_sortlist, $SORT_FUNC) if ($func_coverage);
	push(@fileview_sortlist, $SORT_BRANCH) if ($br_coverage);
	push(@funcview_sortlist, $SORT_LINE);
}

if ($frames)
{
	# Include genpng code needed for overview image generation
	do("$tool_dir/genpng");
}

# Ensure that the c++filt tool is available when using --demangle-cpp
if ($demangle_cpp)
{
	if (system_no_output(3, "c++filt", "--version")) {
		die("ERROR: could not find c++filt tool needed for ".
		    "--demangle-cpp\n");
	}
}

# Make sure precision is within valid range
if ($default_precision < 1 || $default_precision > 4)
{
	die("ERROR: specified precision is out of range (1 to 4)\n");
}


# Make sure output_directory exists, create it if necessary
if ($output_directory)
{
	stat($output_directory);

	if (! -e _)
	{
		create_sub_dir($output_directory);
	}
}

# Do something
gen_html();

exit(0);



#
# print_usage(handle)
#
# Print usage information.
#

sub print_usage(*)
{
	local *HANDLE = $_[0];

	print(HANDLE <<END_OF_USAGE);
Usage: $tool_name [OPTIONS] INFOFILE(S)

Create HTML output for coverage data found in INFOFILE. Note that INFOFILE
may also be a list of filenames.

Misc:
  -h, --help                        Print this help, then exit
  -v, --version                     Print version number, then exit
  -q, --quiet                       Do not print progress messages
      --config-file FILENAME        Specify configuration file location
      --rc SETTING=VALUE            Override configuration file setting
      --ignore-errors ERRORS        Continue after ERRORS (source)

Operation:
  -o, --output-directory OUTDIR     Write HTML output to OUTDIR
  -s, --show-details                Generate detailed directory view
  -d, --description-file DESCFILE   Read test case descriptions from DESCFILE
  -k, --keep-descriptions           Do not remove unused test descriptions
  -b, --baseline-file BASEFILE      Use BASEFILE as baseline file
  -p, --prefix PREFIX               Remove PREFIX from all directory names
      --no-prefix                   Do not remove prefix from directory names
      --(no-)function-coverage      Enable (disable) function coverage display
      --(no-)branch-coverage        Enable (disable) branch coverage display

HTML output:
  -f, --frames                      Use HTML frames for source code view
  -t, --title TITLE                 Display TITLE in header of all pages
  -c, --css-file CSSFILE            Use external style sheet file CSSFILE
      --no-source                   Do not create source code view
      --num-spaces NUM              Replace tabs with NUM spaces in source view
      --highlight                   Highlight lines with converted-only data
      --legend                      Include color legend in HTML output
      --html-prolog FILE            Use FILE as HTML prolog for generated pages
      --html-epilog FILE            Use FILE as HTML epilog for generated pages
      --html-extension EXT          Use EXT as filename extension for pages
      --html-gzip                   Use gzip to compress HTML
      --(no-)sort                   Enable (disable) sorted coverage views
      --demangle-cpp                Demangle C++ function names
      --precision NUM               Set precision of coverage rate

For more information see: $lcov_url
END_OF_USAGE
	;
}


#
# get_rate(found, hit)
#
# Return a relative value for the specified found&hit values
# which is used for sorting the corresponding entries in a
# file list.
#

sub get_rate($$)
{
	my ($found, $hit) = @_;

	if ($found == 0) {
		return 10000;
	}
	return int($hit * 1000 / $found) * 10 + 2 - (1 / $found);
}


#
# get_overall_line(found, hit, name_singular, name_plural)
#
# Return a string containing overall information for the specified
# found/hit data.
#

sub get_overall_line($$$$)
{
	my ($found, $hit, $name_sn, $name_pl) = @_;
	my $name;

	return "no data found" if (!defined($found) || $found == 0);
	$name = ($found == 1) ? $name_sn : $name_pl;
	return rate($hit, $found, "% ($hit of $found $name)");
}


#
# print_overall_rate(ln_do, ln_found, ln_hit, fn_do, fn_found, fn_hit, br_do
#                    br_found, br_hit)
#
# Print overall coverage rates for the specified coverage types.
#

sub print_overall_rate($$$$$$$$$)
{
	my ($ln_do, $ln_found, $ln_hit, $fn_do, $fn_found, $fn_hit,
	    $br_do, $br_found, $br_hit) = @_;

	info("Overall coverage rate:\n");
	info("  lines......: %s\n",
	     get_overall_line($ln_found, $ln_hit, "line", "lines"))
		if ($ln_do);
	info("  functions..: %s\n",
	     get_overall_line($fn_found, $fn_hit, "function", "functions"))
		if ($fn_do);
	info("  branches...: %s\n",
	     get_overall_line($br_found, $br_hit, "branch", "branches"))
		if ($br_do);
}

sub get_fn_list($)
{
	my ($info) = @_;
	my %fns;
	my @result;

	foreach my $filename (keys(%{$info})) {
		my $data = $info->{$filename};
		my $funcdata = $data->{"func"};
		my $sumfnccount = $data->{"sumfnc"};

		if (defined($funcdata)) {
			foreach my $func_name (keys(%{$funcdata})) {
				$fns{$func_name} = 1;
			}
		}

		if (defined($sumfnccount)) {
			foreach my $func_name (keys(%{$sumfnccount})) {
				$fns{$func_name} = 1;
			}
		}
	}

	@result = keys(%fns);

	return \@result;
}

#
# rename_functions(info, conv)
#
# Rename all function names in INFO according to CONV: OLD_NAME -> NEW_NAME.
# In case two functions demangle to the same name, assume that they are
# different object code implementations for the same source function.
#

sub rename_functions($$)
{
	my ($info, $conv) = @_;

	foreach my $filename (keys(%{$info})) {
		my $data = $info->{$filename};
		my $funcdata;
		my $testfncdata;
		my $sumfnccount;
		my %newfuncdata;
		my %newsumfnccount;
		my $f_found;
		my $f_hit;

		# funcdata: function name -> line number
		$funcdata = $data->{"func"};
		foreach my $fn (keys(%{$funcdata})) {
			my $cn = $conv->{$fn};

			# Abort if two functions on different lines map to the
			# same demangled name.
			if (defined($newfuncdata{$cn}) &&
			    $newfuncdata{$cn} != $funcdata->{$fn}) {
				die("ERROR: Demangled function name $cn ".
				    "maps to different lines (".
				    $newfuncdata{$cn}." vs ".
				    $funcdata->{$fn}.") in $filename\n");
			}
			$newfuncdata{$cn} = $funcdata->{$fn};
		}
		$data->{"func"} = \%newfuncdata;

		# testfncdata: test name -> testfnccount
		# testfnccount: function name -> execution count
		$testfncdata = $data->{"testfnc"};
		foreach my $tn (keys(%{$testfncdata})) {
			my $testfnccount = $testfncdata->{$tn};
			my %newtestfnccount;

			foreach my $fn (keys(%{$testfnccount})) {
				my $cn = $conv->{$fn};

				# Add counts for different functions that map
				# to the same name.
				$newtestfnccount{$cn} +=
					$testfnccount->{$fn};
			}
			$testfncdata->{$tn} = \%newtestfnccount;
		}

		# sumfnccount: function name -> execution count
		$sumfnccount = $data->{"sumfnc"};
		foreach my $fn (keys(%{$sumfnccount})) {
			my $cn = $conv->{$fn};

			# Add counts for different functions that map
			# to the same name.
			$newsumfnccount{$cn} += $sumfnccount->{$fn};
		}
		$data->{"sumfnc"} = \%newsumfnccount;

		# Update function found and hit counts since they may have
		# changed
		$f_found = 0;
		$f_hit = 0;
		foreach my $fn (keys(%newsumfnccount)) {
			$f_found++;
			$f_hit++ if ($newsumfnccount{$fn} > 0);
		}
		$data->{"f_found"} = $f_found;
		$data->{"f_hit"} = $f_hit;
	}
}

#
# gen_html()
#
# Generate a set of HTML pages from contents of .info file INFO_FILENAME.
# Files will be written to the current directory. If provided, test case
# descriptions will be read from .tests file TEST_FILENAME and included
# in ouput.
#
# Die on error.
#

sub gen_html()
{
	local *HTML_HANDLE;
	my %overview;
	my %base_data;
	my $lines_found;
	my $lines_hit;
	my $fn_found;
	my $fn_hit;
	my $br_found;
	my $br_hit;
	my $overall_found = 0;
	my $overall_hit = 0;
	my $total_fn_found = 0;
	my $total_fn_hit = 0;
	my $total_br_found = 0;
	my $total_br_hit = 0;
	my $dir_name;
	my $link_name;
	my @dir_list;
	my %new_info;

	# Read in all specified .info files
	foreach (@info_filenames)
	{
		%new_info = %{read_info_file($_)};

		# Combine %new_info with %info_data
		%info_data = %{combine_info_files(\%info_data, \%new_info)};
	}

	info("Found %d entries.\n", scalar(keys(%info_data)));

	# Read and apply baseline data if specified
	if ($base_filename)
	{
		# Read baseline file
		info("Reading baseline file $base_filename\n");
		%base_data = %{read_info_file($base_filename)};
		info("Found %d entries.\n", scalar(keys(%base_data)));

		# Apply baseline
		info("Subtracting baseline data.\n");
		%info_data = %{apply_baseline(\%info_data, \%base_data)};
	}

	@dir_list = get_dir_list(keys(%info_data));

	if ($no_prefix)
	{
		# User requested that we leave filenames alone
		info("User asked not to remove filename prefix\n");
	}
	elsif (! @dir_prefix)
	{
		# Get prefix common to most directories in list
		my $prefix = get_prefix(1, keys(%info_data));

		if ($prefix)
		{
			info("Found common filename prefix \"$prefix\"\n");
			$dir_prefix[0] = $prefix;

		}
		else
		{
			info("No common filename prefix found!\n");
			$no_prefix=1;
		}
	}
	else
	{
		my $msg = "Using user-specified filename prefix ";
		for my $i (0 .. $#dir_prefix)
		{
				$dir_prefix[$i] =~ s/\/+$//;
				$msg .= ", " unless 0 == $i;
				$msg .= "\"" . $dir_prefix[$i] . "\"";
		}
		info($msg . "\n");
	}


	# Read in test description file if specified
	if ($desc_filename)
	{
		info("Reading test description file $desc_filename\n");
		%test_description = %{read_testfile($desc_filename)};

		# Remove test descriptions which are not referenced
		# from %info_data if user didn't tell us otherwise
		if (!$keep_descriptions)
		{
			remove_unused_descriptions();
		}
	}

	# Change to output directory if specified
	if ($output_directory)
	{
		chdir($output_directory)
			or die("ERROR: cannot change to directory ".
			"$output_directory!\n");
	}

	info("Writing .css and .png files.\n");
	write_css_file();
	write_png_files();

	if ($html_gzip)
	{
		info("Writing .htaccess file.\n");
		write_htaccess_file();
	}

	info("Generating output.\n");

	# Process each subdirectory and collect overview information
	foreach $dir_name (@dir_list)
	{
		($lines_found, $lines_hit, $fn_found, $fn_hit,
		 $br_found, $br_hit)
			= process_dir($dir_name);

		# Handle files in root directory gracefully
		$dir_name = "root" if ($dir_name eq "");

		# Remove prefix if applicable
		if (!$no_prefix && @dir_prefix)
		{
			# Match directory names beginning with one of @dir_prefix
			$dir_name = apply_prefix($dir_name,@dir_prefix);
		}

		# Generate name for directory overview HTML page
		if ($dir_name =~ /^\/(.*)$/)
		{
			$link_name = substr($dir_name, 1)."/index.$html_ext";
		}
		else
		{
			$link_name = $dir_name."/index.$html_ext";
		}

		$overview{$dir_name} = [$lines_found, $lines_hit, $fn_found,
					$fn_hit, $br_found, $br_hit, $link_name,
					get_rate($lines_found, $lines_hit),
					get_rate($fn_found, $fn_hit),
					get_rate($br_found, $br_hit)];
		$overall_found	+= $lines_found;
		$overall_hit	+= $lines_hit;
		$total_fn_found	+= $fn_found;
		$total_fn_hit	+= $fn_hit;
		$total_br_found	+= $br_found;
		$total_br_hit	+= $br_hit;
	}

	# Generate overview page
	info("Writing directory view page.\n");

	# Create sorted pages
	foreach (@fileview_sortlist) {
		write_dir_page($fileview_sortname[$_], ".", "", $test_title,
			       undef, $overall_found, $overall_hit,
			       $total_fn_found, $total_fn_hit, $total_br_found,
			       $total_br_hit, \%overview, {}, {}, {}, 0, $_);
	}

	# Check if there are any test case descriptions to write out
	if (%test_description)
	{
		info("Writing test case description file.\n");
		write_description_file( \%test_description,
					$overall_found, $overall_hit,
					$total_fn_found, $total_fn_hit,
					$total_br_found, $total_br_hit);
	}

	print_overall_rate(1, $overall_found, $overall_hit,
			   $func_coverage, $total_fn_found, $total_fn_hit,
			   $br_coverage, $total_br_found, $total_br_hit);

	chdir($cwd);
}

#
# html_create(handle, filename)
#

sub html_create($$)
{
	my $handle = $_[0];
	my $filename = $_[1];

	if ($html_gzip)
	{
		open($handle, "|-", "gzip -c >'$filename'")
			or die("ERROR: cannot open $filename for writing ".
			       "(gzip)!\n");
	}
	else
	{
		open($handle, ">", $filename)
			or die("ERROR: cannot open $filename for writing!\n");
	}
}

sub write_dir_page($$$$$$$$$$$$$$$$$)
{
	my ($name, $rel_dir, $base_dir, $title, $trunc_dir, $overall_found,
	    $overall_hit, $total_fn_found, $total_fn_hit, $total_br_found,
	    $total_br_hit, $overview, $testhash, $testfnchash, $testbrhash,
	    $view_type, $sort_type) = @_;

	# Generate directory overview page including details
	html_create(*HTML_HANDLE, "$rel_dir/index$name.$html_ext");
	if (!defined($trunc_dir)) {
		$trunc_dir = "";
	}
	$title .= " - " if ($trunc_dir ne "");
	write_html_prolog(*HTML_HANDLE, $base_dir, "LCOV - $title$trunc_dir");
	write_header(*HTML_HANDLE, $view_type, $trunc_dir, $rel_dir,
		     $overall_found, $overall_hit, $total_fn_found,
		     $total_fn_hit, $total_br_found, $total_br_hit, $sort_type);
	write_file_table(*HTML_HANDLE, $base_dir, $overview, $testhash,
			 $testfnchash, $testbrhash, $view_type, $sort_type);
	write_html_epilog(*HTML_HANDLE, $base_dir);
	close(*HTML_HANDLE);
}


#
# process_dir(dir_name)
#

sub process_dir($)
{
	my $abs_dir = $_[0];
	my $trunc_dir;
	my $rel_dir = $abs_dir;
	my $base_dir;
	my $filename;
	my %overview;
	my $lines_found;
	my $lines_hit;
	my $fn_found;
	my $fn_hit;
	my $br_found;
	my $br_hit;
	my $overall_found=0;
	my $overall_hit=0;
	my $total_fn_found=0;
	my $total_fn_hit=0;
	my $total_br_found = 0;
	my $total_br_hit = 0;
	my $base_name;
	my $extension;
	my $testdata;
	my %testhash;
	my $testfncdata;
	my %testfnchash;
	my $testbrdata;
	my %testbrhash;
	my @sort_list;
	local *HTML_HANDLE;

	# Remove prefix if applicable
	if (!$no_prefix)
	{
		# Match directory name beginning with one of @dir_prefix
		$rel_dir = apply_prefix($rel_dir,@dir_prefix);
	}

	$trunc_dir = $rel_dir;

	# Remove leading /
	if ($rel_dir =~ /^\/(.*)$/)
	{
		$rel_dir = substr($rel_dir, 1);
	}

	# Handle files in root directory gracefully
	$rel_dir = "root" if ($rel_dir eq "");
	$trunc_dir = "root" if ($trunc_dir eq "");

	$base_dir = get_relative_base_path($rel_dir);

	create_sub_dir($rel_dir);

	# Match filenames which specify files in this directory, not including
	# sub-directories
	foreach $filename (grep(/^\Q$abs_dir\E\/[^\/]*$/,keys(%info_data)))
	{
		my $page_link;
		my $func_link;

		($lines_found, $lines_hit, $fn_found, $fn_hit, $br_found,
		 $br_hit, $testdata, $testfncdata, $testbrdata) =
			process_file($trunc_dir, $rel_dir, $filename);

		$base_name = basename($filename);

		if ($no_sourceview) {
			$page_link = "";
		} elsif ($frames) {
			# Link to frameset page
			$page_link = "$base_name.gcov.frameset.$html_ext";
		} else {
			# Link directory to source code view page
			$page_link = "$base_name.gcov.$html_ext";
		}
		$overview{$base_name} = [$lines_found, $lines_hit, $fn_found,
					 $fn_hit, $br_found, $br_hit,
					 $page_link,
					 get_rate($lines_found, $lines_hit),
					 get_rate($fn_found, $fn_hit),
					 get_rate($br_found, $br_hit)];

		$testhash{$base_name} = $testdata;
		$testfnchash{$base_name} = $testfncdata;
		$testbrhash{$base_name} = $testbrdata;

		$overall_found	+= $lines_found;
		$overall_hit	+= $lines_hit;

		$total_fn_found += $fn_found;
		$total_fn_hit   += $fn_hit;

		$total_br_found += $br_found;
		$total_br_hit   += $br_hit;
	}

	# Create sorted pages
	foreach (@fileview_sortlist) {
		# Generate directory overview page (without details)	
		write_dir_page($fileview_sortname[$_], $rel_dir, $base_dir,
			       $test_title, $trunc_dir, $overall_found,
			       $overall_hit, $total_fn_found, $total_fn_hit,
			       $total_br_found, $total_br_hit, \%overview, {},
			       {}, {}, 1, $_);
		if (!$show_details) {
			next;
		}
		# Generate directory overview page including details
		write_dir_page("-detail".$fileview_sortname[$_], $rel_dir,
			       $base_dir, $test_title, $trunc_dir,
			       $overall_found, $overall_hit, $total_fn_found,
			       $total_fn_hit, $total_br_found, $total_br_hit,
			       \%overview, \%testhash, \%testfnchash,
			       \%testbrhash, 1, $_);
	}

	# Calculate resulting line counts
	return ($overall_found, $overall_hit, $total_fn_found, $total_fn_hit,
		$total_br_found, $total_br_hit);
}


#
# get_converted_lines(testdata)
#
# Return hash of line numbers of those lines which were only covered in
# converted data sets.
#

sub get_converted_lines($)
{
	my $testdata = $_[0];
	my $testcount;
	my %converted;
	my %nonconverted;
	my $hash;
	my $testcase;
	my $line;
	my %result;


	# Get a hash containing line numbers with positive counts both for
	# converted and original data sets
	foreach $testcase (keys(%{$testdata}))
	{
		# Check to see if this is a converted data set
		if ($testcase =~ /,diff$/)
		{
			$hash = \%converted;
		}
		else
		{
			$hash = \%nonconverted;
		}

		$testcount = $testdata->{$testcase};
		# Add lines with a positive count to hash
		foreach $line (keys%{$testcount})
		{
			if ($testcount->{$line} > 0)
			{
				$hash->{$line} = 1;
			}
		}
	}

	# Combine both hashes to resulting list
	foreach $line (keys(%converted))
	{
		if (!defined($nonconverted{$line}))
		{
			$result{$line} = 1;
		}
	}

	return \%result;
}


sub write_function_page($$$$$$$$$$$$$$$$$$)
{
	my ($base_dir, $rel_dir, $trunc_dir, $base_name, $title,
	    $lines_found, $lines_hit, $fn_found, $fn_hit, $br_found, $br_hit,
	    $sumcount, $funcdata, $sumfnccount, $testfncdata, $sumbrcount,
	    $testbrdata, $sort_type) = @_;
	my $pagetitle;
	my $filename;

	# Generate function table for this file
	if ($sort_type == 0) {
		$filename = "$rel_dir/$base_name.func.$html_ext";
	} else {
		$filename = "$rel_dir/$base_name.func-sort-c.$html_ext";
	}
	html_create(*HTML_HANDLE, $filename);
	$pagetitle = "LCOV - $title - $trunc_dir/$base_name - functions";
	write_html_prolog(*HTML_HANDLE, $base_dir, $pagetitle);
	write_header(*HTML_HANDLE, 4, "$trunc_dir/$base_name",
		     "$rel_dir/$base_name", $lines_found, $lines_hit,
		     $fn_found, $fn_hit, $br_found, $br_hit, $sort_type);
	write_function_table(*HTML_HANDLE, "$base_name.gcov.$html_ext",
			     $sumcount, $funcdata,
			     $sumfnccount, $testfncdata, $sumbrcount,
			     $testbrdata, $base_name,
			     $base_dir, $sort_type);
	write_html_epilog(*HTML_HANDLE, $base_dir, 1);
	close(*HTML_HANDLE);
}


#
# process_file(trunc_dir, rel_dir, filename)
#

sub process_file($$$)
{
	info("Processing file ".apply_prefix($_[2], @dir_prefix)."\n");

	my $trunc_dir = $_[0];
	my $rel_dir = $_[1];
	my $filename = $_[2];
	my $base_name = basename($filename);
	my $base_dir = get_relative_base_path($rel_dir);
	my $testdata;
	my $testcount;
	my $sumcount;
	my $funcdata;
	my $checkdata;
	my $testfncdata;
	my $sumfnccount;
	my $testbrdata;
	my $sumbrcount;
	my $lines_found;
	my $lines_hit;
	my $fn_found;
	my $fn_hit;
	my $br_found;
	my $br_hit;
	my $converted;
	my @source;
	my $pagetitle;
	local *HTML_HANDLE;

	($testdata, $sumcount, $funcdata, $checkdata, $testfncdata,
	 $sumfnccount, $testbrdata, $sumbrcount, $lines_found, $lines_hit,
	 $fn_found, $fn_hit, $br_found, $br_hit)
		= get_info_entry($info_data{$filename});

	# Return after this point in case user asked us not to generate
	# source code view
	if ($no_sourceview)
	{
		return ($lines_found, $lines_hit, $fn_found, $fn_hit,
			$br_found, $br_hit, $testdata, $testfncdata,
			$testbrdata);
	}

	$converted = get_converted_lines($testdata);
	# Generate source code view for this file
	html_create(*HTML_HANDLE, "$rel_dir/$base_name.gcov.$html_ext");
	$pagetitle = "LCOV - $test_title - $trunc_dir/$base_name";
	write_html_prolog(*HTML_HANDLE, $base_dir, $pagetitle);
	write_header(*HTML_HANDLE, 2, "$trunc_dir/$base_name",
		     "$rel_dir/$base_name", $lines_found, $lines_hit,
		     $fn_found, $fn_hit, $br_found, $br_hit, 0);
	@source = write_source(*HTML_HANDLE, $filename, $sumcount, $checkdata,
			       $converted, $funcdata, $sumbrcount);

	write_html_epilog(*HTML_HANDLE, $base_dir, 1);
	close(*HTML_HANDLE);

	if ($func_coverage) {
		# Create function tables
		foreach (@funcview_sortlist) {
			write_function_page($base_dir, $rel_dir, $trunc_dir,
					    $base_name, $test_title,
					    $lines_found, $lines_hit,
					    $fn_found, $fn_hit, $br_found,
					    $br_hit, $sumcount,
					    $funcdata, $sumfnccount,
					    $testfncdata, $sumbrcount,
					    $testbrdata, $_);
		}
	}

	# Additional files are needed in case of frame output
	if (!$frames)
	{
		return ($lines_found, $lines_hit, $fn_found, $fn_hit,
			$br_found, $br_hit, $testdata, $testfncdata,
			$testbrdata);
	}

	# Create overview png file
	gen_png("$rel_dir/$base_name.gcov.png", $overview_width, $tab_size,
		@source);

	# Create frameset page
	html_create(*HTML_HANDLE,
		    "$rel_dir/$base_name.gcov.frameset.$html_ext");
	write_frameset(*HTML_HANDLE, $base_dir, $base_name, $pagetitle);
	close(*HTML_HANDLE);

	# Write overview frame
	html_create(*HTML_HANDLE,
		    "$rel_dir/$base_name.gcov.overview.$html_ext");
	write_overview(*HTML_HANDLE, $base_dir, $base_name, $pagetitle,
		       scalar(@source));
	close(*HTML_HANDLE);

	return ($lines_found, $lines_hit, $fn_found, $fn_hit, $br_found,
		$br_hit, $testdata, $testfncdata, $testbrdata);
}


#
# read_info_file(info_filename)
#
# Read in the contents of the .info file specified by INFO_FILENAME. Data will
# be returned as a reference to a hash containing the following mappings:
#
# %result: for each filename found in file -> \%data
#
# %data: "test"  -> \%testdata
#        "sum"   -> \%sumcount
#        "func"  -> \%funcdata
#        "found" -> $lines_found (number of instrumented lines found in file)
#	 "hit"   -> $lines_hit (number of executed lines in file)
#        "f_found" -> $fn_found (number of instrumented functions found in file)
#	 "f_hit"   -> $fn_hit (number of executed functions in file)
#        "b_found" -> $br_found (number of instrumented branches found in file)
#	 "b_hit"   -> $br_hit (number of executed branches in file)
#        "check" -> \%checkdata
#        "testfnc" -> \%testfncdata
#        "sumfnc"  -> \%sumfnccount
#        "testbr"  -> \%testbrdata
#        "sumbr"   -> \%sumbrcount
#
# %testdata   : name of test affecting this file -> \%testcount
# %testfncdata: name of test affecting this file -> \%testfnccount
# %testbrdata:  name of test affecting this file -> \%testbrcount
#
# %testcount   : line number   -> execution count for a single test
# %testfnccount: function name -> execution count for a single test
# %testbrcount : line number   -> branch coverage data for a single test
# %sumcount    : line number   -> execution count for all tests
# %sumfnccount : function name -> execution count for all tests
# %sumbrcount  : line number   -> branch coverage data for all tests
# %funcdata    : function name -> line number
# %checkdata   : line number   -> checksum of source code line
# $brdata      : vector of items: block, branch, taken
# 
# Note that .info file sections referring to the same file and test name
# will automatically be combined by adding all execution counts.
#
# Note that if INFO_FILENAME ends with ".gz", it is assumed that the file
# is compressed using GZIP. If available, GUNZIP will be used to decompress
# this file.
#
# Die on error.
#

sub read_info_file($)
{
	my $tracefile = $_[0];		# Name of tracefile
	my %result;			# Resulting hash: file -> data
	my $data;			# Data handle for current entry
	my $testdata;			#       "             "
	my $testcount;			#       "             "
	my $sumcount;			#       "             "
	my $funcdata;			#       "             "
	my $checkdata;			#       "             "
	my $testfncdata;
	my $testfnccount;
	my $sumfnccount;
	my $testbrdata;
	my $testbrcount;
	my $sumbrcount;
	my $line;			# Current line read from .info file
	my $testname;			# Current test name
	my $filename;			# Current filename
	my $hitcount;			# Count for lines hit
	my $count;			# Execution count of current line
	my $negative;			# If set, warn about negative counts
	my $changed_testname;		# If set, warn about changed testname
	my $line_checksum;		# Checksum of current line
	my $br_found;
	my $br_hit;
	my $notified_about_relative_paths;
	local *INFO_HANDLE;		# Filehandle for .info file

	info("Reading data file $tracefile\n");

	# Check if file exists and is readable
	stat($_[0]);
	if (!(-r _))
	{
		die("ERROR: cannot read file $_[0]!\n");
	}

	# Check if this is really a plain file
	if (!(-f _))
	{
		die("ERROR: not a plain file: $_[0]!\n");
	}

	# Check for .gz extension
	if ($_[0] =~ /\.gz$/)
	{
		# Check for availability of GZIP tool
		system_no_output(1, "gunzip" ,"-h")
			and die("ERROR: gunzip command not available!\n");

		# Check integrity of compressed file
		system_no_output(1, "gunzip", "-t", $_[0])
			and die("ERROR: integrity check failed for ".
				"compressed file $_[0]!\n");

		# Open compressed file
		open(INFO_HANDLE, "-|", "gunzip -c '$_[0]'")
			or die("ERROR: cannot start gunzip to decompress ".
			       "file $_[0]!\n");
	}
	else
	{
		# Open decompressed file
		open(INFO_HANDLE, "<", $_[0])
			or die("ERROR: cannot read file $_[0]!\n");
	}

	$testname = "";
	while (<INFO_HANDLE>)
	{
		chomp($_);
		$line = $_;

		# Switch statement
		foreach ($line)
		{
			/^TN:([^,]*)(,diff)?/ && do
			{
				# Test name information found
				$testname = defined($1) ? $1 : "";
				if ($testname =~ s/\W/_/g)
				{
					$changed_testname = 1;
				}
				$testname .= $2 if (defined($2));
				last;
			};

			/^[SK]F:(.*)/ && do
			{
				# Filename information found
				# Retrieve data for new entry
				$filename = File::Spec->rel2abs($1, Cwd::cwd());

				if (!File::Spec->file_name_is_absolute($1) &&
				    !$notified_about_relative_paths)
				{
					info("Resolved relative source file ".
					     "path \"$1\" with CWD to ".
					     "\"$filename\".\n");
					$notified_about_relative_paths = 1;
				}

				$data = $result{$filename};
				($testdata, $sumcount, $funcdata, $checkdata,
				 $testfncdata, $sumfnccount, $testbrdata,
				 $sumbrcount) =
					get_info_entry($data);

				if (defined($testname))
				{
					$testcount = $testdata->{$testname};
					$testfnccount = $testfncdata->{$testname};
					$testbrcount = $testbrdata->{$testname};
				}
				else
				{
					$testcount = {};
					$testfnccount = {};
					$testbrcount = {};
				}
				last;
			};

			/^DA:(\d+),(-?\d+)(,[^,\s]+)?/ && do
			{
				# Fix negative counts
				$count = $2 < 0 ? 0 : $2;
				if ($2 < 0)
				{
					$negative = 1;
				}
				# Execution count found, add to structure
				# Add summary counts
				$sumcount->{$1} += $count;

				# Add test-specific counts
				if (defined($testname))
				{
					$testcount->{$1} += $count;
				}

				# Store line checksum if available
				if (defined($3))
				{
					$line_checksum = substr($3, 1);

					# Does it match a previous definition
					if (defined($checkdata->{$1}) &&
					    ($checkdata->{$1} ne
					     $line_checksum))
					{
						die("ERROR: checksum mismatch ".
						    "at $filename:$1\n");
					}

					$checkdata->{$1} = $line_checksum;
				}
				last;
			};

			/^FN:(\d+),([^,]+)/ && do
			{
				last if (!$func_coverage);

				# Function data found, add to structure
				$funcdata->{$2} = $1;

				# Also initialize function call data
				if (!defined($sumfnccount->{$2})) {
					$sumfnccount->{$2} = 0;
				}
				if (defined($testname))
				{
					if (!defined($testfnccount->{$2})) {
						$testfnccount->{$2} = 0;
					}
				}
				last;
			};

			/^FNDA:(\d+),([^,]+)/ && do
			{
				last if (!$func_coverage);
				# Function call count found, add to structure
				# Add summary counts
				$sumfnccount->{$2} += $1;

				# Add test-specific counts
				if (defined($testname))
				{
					$testfnccount->{$2} += $1;
				}
				last;
			};

			/^BRDA:(\d+),(\d+),(\d+),(\d+|-)/ && do {
				# Branch coverage data found
				my ($line, $block, $branch, $taken) =
				   ($1, $2, $3, $4);

				last if (!$br_coverage);
				$sumbrcount->{$line} =
					br_ivec_push($sumbrcount->{$line},
						     $block, $branch, $taken);

				# Add test-specific counts
				if (defined($testname)) {
					$testbrcount->{$line} =
						br_ivec_push(
							$testbrcount->{$line},
							$block, $branch,
							$taken);
				}
				last;
			};

			/^end_of_record/ && do
			{
				# Found end of section marker
				if ($filename)
				{
					# Store current section data
					if (defined($testname))
					{
						$testdata->{$testname} =
							$testcount;
						$testfncdata->{$testname} =
							$testfnccount;
						$testbrdata->{$testname} =
							$testbrcount;
					}	

					set_info_entry($data, $testdata,
						       $sumcount, $funcdata,
						       $checkdata, $testfncdata,
						       $sumfnccount,
						       $testbrdata,
						       $sumbrcount);
					$result{$filename} = $data;
					last;
				}
			};

			# default
			last;
		}
	}
	close(INFO_HANDLE);

	# Calculate lines_found and lines_hit for each file
	foreach $filename (keys(%result))
	{
		$data = $result{$filename};

		($testdata, $sumcount, undef, undef, $testfncdata,
		 $sumfnccount, $testbrdata, $sumbrcount) =
			get_info_entry($data);

		# Filter out empty files
		if (scalar(keys(%{$sumcount})) == 0)
		{
			delete($result{$filename});
			next;
		}
		# Filter out empty test cases
		foreach $testname (keys(%{$testdata}))
		{
			if (!defined($testdata->{$testname}) ||
			    scalar(keys(%{$testdata->{$testname}})) == 0)
			{
				delete($testdata->{$testname});
				delete($testfncdata->{$testname});
			}
		}

		$data->{"found"} = scalar(keys(%{$sumcount}));
		$hitcount = 0;

		foreach (keys(%{$sumcount}))
		{
			if ($sumcount->{$_} > 0) { $hitcount++; }
		}

		$data->{"hit"} = $hitcount;

		# Get found/hit values for function call data
		$data->{"f_found"} = scalar(keys(%{$sumfnccount}));
		$hitcount = 0;

		foreach (keys(%{$sumfnccount})) {
			if ($sumfnccount->{$_} > 0) {
				$hitcount++;
			}
		}
		$data->{"f_hit"} = $hitcount;

		# Get found/hit values for branch data
		($br_found, $br_hit) = get_br_found_and_hit($sumbrcount);

		$data->{"b_found"} = $br_found;
		$data->{"b_hit"} = $br_hit;
	}

	if (scalar(keys(%result)) == 0)
	{
		die("ERROR: no valid records found in tracefile $tracefile\n");
	}
	if ($negative)
	{
		warn("WARNING: negative counts found in tracefile ".
		     "$tracefile\n");
	}
	if ($changed_testname)
	{
		warn("WARNING: invalid characters removed from testname in ".
		     "tracefile $tracefile\n");
	}

	return(\%result);
}


#
# get_info_entry(hash_ref)
#
# Retrieve data from an entry of the structure generated by read_info_file().
# Return a list of references to hashes:
# (test data hash ref, sum count hash ref, funcdata hash ref, checkdata hash
#  ref, testfncdata hash ref, sumfnccount hash ref, lines found, lines hit,
#  functions found, functions hit)
#

sub get_info_entry($)
{
	my $testdata_ref = $_[0]->{"test"};
	my $sumcount_ref = $_[0]->{"sum"};
	my $funcdata_ref = $_[0]->{"func"};
	my $checkdata_ref = $_[0]->{"check"};
	my $testfncdata = $_[0]->{"testfnc"};
	my $sumfnccount = $_[0]->{"sumfnc"};
	my $testbrdata = $_[0]->{"testbr"};
	my $sumbrcount = $_[0]->{"sumbr"};
	my $lines_found = $_[0]->{"found"};
	my $lines_hit = $_[0]->{"hit"};
	my $fn_found = $_[0]->{"f_found"};
	my $fn_hit = $_[0]->{"f_hit"};
	my $br_found = $_[0]->{"b_found"};
	my $br_hit = $_[0]->{"b_hit"};

	return ($testdata_ref, $sumcount_ref, $funcdata_ref, $checkdata_ref,
		$testfncdata, $sumfnccount, $testbrdata, $sumbrcount,
		$lines_found, $lines_hit, $fn_found, $fn_hit,
		$br_found, $br_hit);
}


#
# set_info_entry(hash_ref, testdata_ref, sumcount_ref, funcdata_ref,
#                checkdata_ref, testfncdata_ref, sumfcncount_ref,
#                testbrdata_ref, sumbrcount_ref[,lines_found,
#                lines_hit, f_found, f_hit, $b_found, $b_hit])
#
# Update the hash referenced by HASH_REF with the provided data references.
#

sub set_info_entry($$$$$$$$$;$$$$$$)
{
	my $data_ref = $_[0];

	$data_ref->{"test"} = $_[1];
	$data_ref->{"sum"} = $_[2];
	$data_ref->{"func"} = $_[3];
	$data_ref->{"check"} = $_[4];
	$data_ref->{"testfnc"} = $_[5];
	$data_ref->{"sumfnc"} = $_[6];
	$data_ref->{"testbr"} = $_[7];
	$data_ref->{"sumbr"} = $_[8];

	if (defined($_[9])) { $data_ref->{"found"} = $_[9]; }
	if (defined($_[10])) { $data_ref->{"hit"} = $_[10]; }
	if (defined($_[11])) { $data_ref->{"f_found"} = $_[11]; }
	if (defined($_[12])) { $data_ref->{"f_hit"} = $_[12]; }
	if (defined($_[13])) { $data_ref->{"b_found"} = $_[13]; }
	if (defined($_[14])) { $data_ref->{"b_hit"} = $_[14]; }
}


#
# add_counts(data1_ref, data2_ref)
#
# DATA1_REF and DATA2_REF are references to hashes containing a mapping
#
#   line number -> execution count
#
# Return a list (RESULT_REF, LINES_FOUND, LINES_HIT) where RESULT_REF
# is a reference to a hash containing the combined mapping in which
# execution counts are added.
#

sub add_counts($$)
{
	my $data1_ref = $_[0];	# Hash 1
	my $data2_ref = $_[1];	# Hash 2
	my %result;		# Resulting hash
	my $line;		# Current line iteration scalar
	my $data1_count;	# Count of line in hash1
	my $data2_count;	# Count of line in hash2
	my $found = 0;		# Total number of lines found
	my $hit = 0;		# Number of lines with a count > 0

	foreach $line (keys(%$data1_ref))
	{
		$data1_count = $data1_ref->{$line};
		$data2_count = $data2_ref->{$line};

		# Add counts if present in both hashes
		if (defined($data2_count)) { $data1_count += $data2_count; }

		# Store sum in %result
		$result{$line} = $data1_count;

		$found++;
		if ($data1_count > 0) { $hit++; }
	}

	# Add lines unique to data2_ref
	foreach $line (keys(%$data2_ref))
	{
		# Skip lines already in data1_ref
		if (defined($data1_ref->{$line})) { next; }

		# Copy count from data2_ref
		$result{$line} = $data2_ref->{$line};

		$found++;
		if ($result{$line} > 0) { $hit++; }
	}

	return (\%result, $found, $hit);
}


#
# merge_checksums(ref1, ref2, filename)
#
# REF1 and REF2 are references to hashes containing a mapping
#
#   line number -> checksum
#
# Merge checksum lists defined in REF1 and REF2 and return reference to
# resulting hash. Die if a checksum for a line is defined in both hashes
# but does not match.
#

sub merge_checksums($$$)
{
	my $ref1 = $_[0];
	my $ref2 = $_[1];
	my $filename = $_[2];
	my %result;
	my $line;

	foreach $line (keys(%{$ref1}))
	{
		if (defined($ref2->{$line}) &&
		    ($ref1->{$line} ne $ref2->{$line}))
		{
			die("ERROR: checksum mismatch at $filename:$line\n");
		}
		$result{$line} = $ref1->{$line};
	}

	foreach $line (keys(%{$ref2}))
	{
		$result{$line} = $ref2->{$line};
	}

	return \%result;
}


#
# merge_func_data(funcdata1, funcdata2, filename)
#

sub merge_func_data($$$)
{
	my ($funcdata1, $funcdata2, $filename) = @_;
	my %result;
	my $func;

	if (defined($funcdata1)) {
		%result = %{$funcdata1};
	}

	foreach $func (keys(%{$funcdata2})) {
		my $line1 = $result{$func};
		my $line2 = $funcdata2->{$func};

		if (defined($line1) && ($line1 != $line2)) {
			warn("WARNING: function data mismatch at ".
			     "$filename:$line2\n");
			next;
		}
		$result{$func} = $line2;
	}

	return \%result;
}


#
# add_fnccount(fnccount1, fnccount2)
#
# Add function call count data. Return list (fnccount_added, f_found, f_hit)
#

sub add_fnccount($$)
{
	my ($fnccount1, $fnccount2) = @_;
	my %result;
	my $fn_found;
	my $fn_hit;
	my $function;

	if (defined($fnccount1)) {
		%result = %{$fnccount1};
	}
	foreach $function (keys(%{$fnccount2})) {
		$result{$function} += $fnccount2->{$function};
	}
	$fn_found = scalar(keys(%result));
	$fn_hit = 0;
	foreach $function (keys(%result)) {
		if ($result{$function} > 0) {
			$fn_hit++;
		}
	}

	return (\%result, $fn_found, $fn_hit);
}

#
# add_testfncdata(testfncdata1, testfncdata2)
#
# Add function call count data for several tests. Return reference to
# added_testfncdata.
#

sub add_testfncdata($$)
{
	my ($testfncdata1, $testfncdata2) = @_;
	my %result;
	my $testname;

	foreach $testname (keys(%{$testfncdata1})) {
		if (defined($testfncdata2->{$testname})) {
			my $fnccount;

			# Function call count data for this testname exists
			# in both data sets: add
			($fnccount) = add_fnccount(
				$testfncdata1->{$testname},
				$testfncdata2->{$testname});
			$result{$testname} = $fnccount;
			next;
		}
		# Function call count data for this testname is unique to
		# data set 1: copy
		$result{$testname} = $testfncdata1->{$testname};
	}

	# Add count data for testnames unique to data set 2
	foreach $testname (keys(%{$testfncdata2})) {
		if (!defined($result{$testname})) {
			$result{$testname} = $testfncdata2->{$testname};
		}
	}
	return \%result;
}


#
# brcount_to_db(brcount)
#
# Convert brcount data to the following format:
#
# db:          line number    -> block hash
# block hash:  block number   -> branch hash
# branch hash: branch number  -> taken value
#

sub brcount_to_db($)
{
	my ($brcount) = @_;
	my $line;
	my $db;

	# Add branches from first count to database
	foreach $line (keys(%{$brcount})) {
		my $brdata = $brcount->{$line};
		my $i;
		my $num = br_ivec_len($brdata);

		for ($i = 0; $i < $num; $i++) {
			my ($block, $branch, $taken) = br_ivec_get($brdata, $i);

			$db->{$line}->{$block}->{$branch} = $taken;
		}
	}

	return $db;
}


#
# db_to_brcount(db)
#
# Convert branch coverage data back to brcount format.
#

sub db_to_brcount($)
{
	my ($db) = @_;
	my $line;
	my $brcount = {};
	my $br_found = 0;
	my $br_hit = 0;

	# Convert database back to brcount format
	foreach $line (sort({$a <=> $b} keys(%{$db}))) {
		my $ldata = $db->{$line};
		my $brdata;
		my $block;

		foreach $block (sort({$a <=> $b} keys(%{$ldata}))) {
			my $bdata = $ldata->{$block};
			my $branch;

			foreach $branch (sort({$a <=> $b} keys(%{$bdata}))) {
				my $taken = $bdata->{$branch};

				$br_found++;
				$br_hit++ if ($taken ne "-" && $taken > 0);
				$brdata = br_ivec_push($brdata, $block,
						       $branch, $taken);
			}
		}
		$brcount->{$line} = $brdata;
	}

	return ($brcount, $br_found, $br_hit);
}


#
# combine_brcount(brcount1, brcount2, type)
#
# If add is BR_ADD, add branch coverage data and return list (brcount_added,
# br_found, br_hit). If add is BR_SUB, subtract the taken values of brcount2
# from brcount1 and return (brcount_sub, br_found, br_hit).
#

sub combine_brcount($$$)
{
	my ($brcount1, $brcount2, $type) = @_;
	my $line;
	my $block;
	my $branch;
	my $taken;
	my $db;
	my $br_found = 0;
	my $br_hit = 0;
	my $result;

	# Convert branches from first count to database
	$db = brcount_to_db($brcount1);
	# Combine values from database and second count
	foreach $line (keys(%{$brcount2})) {
		my $brdata = $brcount2->{$line};
		my $num = br_ivec_len($brdata);
		my $i;

		for ($i = 0; $i < $num; $i++) {
			($block, $branch, $taken) = br_ivec_get($brdata, $i);
			my $new_taken = $db->{$line}->{$block}->{$branch};

			if ($type == $BR_ADD) {
				$new_taken = br_taken_add($new_taken, $taken);
			} elsif ($type == $BR_SUB) {
				$new_taken = br_taken_sub($new_taken, $taken);
			}
			$db->{$line}->{$block}->{$branch} = $new_taken
				if (defined($new_taken));
		}
	}
	# Convert database back to brcount format
	($result, $br_found, $br_hit) = db_to_brcount($db);

	return ($result, $br_found, $br_hit);
}


#
# add_testbrdata(testbrdata1, testbrdata2)
#
# Add branch coverage data for several tests. Return reference to
# added_testbrdata.
#

sub add_testbrdata($$)
{
	my ($testbrdata1, $testbrdata2) = @_;
	my %result;
	my $testname;

	foreach $testname (keys(%{$testbrdata1})) {
		if (defined($testbrdata2->{$testname})) {
			my $brcount;

			# Branch coverage data for this testname exists
			# in both data sets: add
			($brcount) = combine_brcount($testbrdata1->{$testname},
					 $testbrdata2->{$testname}, $BR_ADD);
			$result{$testname} = $brcount;
			next;
		}
		# Branch coverage data for this testname is unique to
		# data set 1: copy
		$result{$testname} = $testbrdata1->{$testname};
	}

	# Add count data for testnames unique to data set 2
	foreach $testname (keys(%{$testbrdata2})) {
		if (!defined($result{$testname})) {
			$result{$testname} = $testbrdata2->{$testname};
		}
	}
	return \%result;
}


#
# combine_info_entries(entry_ref1, entry_ref2, filename)
#
# Combine .info data entry hashes referenced by ENTRY_REF1 and ENTRY_REF2.
# Return reference to resulting hash.
#

sub combine_info_entries($$$)
{
	my $entry1 = $_[0];	# Reference to hash containing first entry
	my $testdata1;
	my $sumcount1;
	my $funcdata1;
	my $checkdata1;
	my $testfncdata1;
	my $sumfnccount1;
	my $testbrdata1;
	my $sumbrcount1;

	my $entry2 = $_[1];	# Reference to hash containing second entry
	my $testdata2;
	my $sumcount2;
	my $funcdata2;
	my $checkdata2;
	my $testfncdata2;
	my $sumfnccount2;
	my $testbrdata2;
	my $sumbrcount2;

	my %result;		# Hash containing combined entry
	my %result_testdata;
	my $result_sumcount = {};
	my $result_funcdata;
	my $result_testfncdata;
	my $result_sumfnccount;
	my $result_testbrdata;
	my $result_sumbrcount;
	my $lines_found;
	my $lines_hit;
	my $fn_found;
	my $fn_hit;
	my $br_found;
	my $br_hit;

	my $testname;
	my $filename = $_[2];

	# Retrieve data
	($testdata1, $sumcount1, $funcdata1, $checkdata1, $testfncdata1,
	 $sumfnccount1, $testbrdata1, $sumbrcount1) = get_info_entry($entry1);
	($testdata2, $sumcount2, $funcdata2, $checkdata2, $testfncdata2,
	 $sumfnccount2, $testbrdata2, $sumbrcount2) = get_info_entry($entry2);

	# Merge checksums
	$checkdata1 = merge_checksums($checkdata1, $checkdata2, $filename);

	# Combine funcdata
	$result_funcdata = merge_func_data($funcdata1, $funcdata2, $filename);

	# Combine function call count data
	$result_testfncdata = add_testfncdata($testfncdata1, $testfncdata2);
	($result_sumfnccount, $fn_found, $fn_hit) =
		add_fnccount($sumfnccount1, $sumfnccount2);
	
	# Combine branch coverage data
	$result_testbrdata = add_testbrdata($testbrdata1, $testbrdata2);
	($result_sumbrcount, $br_found, $br_hit) =
		combine_brcount($sumbrcount1, $sumbrcount2, $BR_ADD);

	# Combine testdata
	foreach $testname (keys(%{$testdata1}))
	{
		if (defined($testdata2->{$testname}))
		{
			# testname is present in both entries, requires
			# combination
			($result_testdata{$testname}) =
				add_counts($testdata1->{$testname},
					   $testdata2->{$testname});
		}
		else
		{
			# testname only present in entry1, add to result
			$result_testdata{$testname} = $testdata1->{$testname};
		}

		# update sum count hash
		($result_sumcount, $lines_found, $lines_hit) =
			add_counts($result_sumcount,
				   $result_testdata{$testname});
	}

	foreach $testname (keys(%{$testdata2}))
	{
		# Skip testnames already covered by previous iteration
		if (defined($testdata1->{$testname})) { next; }

		# testname only present in entry2, add to result hash
		$result_testdata{$testname} = $testdata2->{$testname};

		# update sum count hash
		($result_sumcount, $lines_found, $lines_hit) =
			add_counts($result_sumcount,
				   $result_testdata{$testname});
	}
	
	# Calculate resulting sumcount

	# Store result
	set_info_entry(\%result, \%result_testdata, $result_sumcount,
		       $result_funcdata, $checkdata1, $result_testfncdata,
		       $result_sumfnccount, $result_testbrdata,
		       $result_sumbrcount, $lines_found, $lines_hit,
		       $fn_found, $fn_hit, $br_found, $br_hit);

	return(\%result);
}


#
# combine_info_files(info_ref1, info_ref2)
#
# Combine .info data in hashes referenced by INFO_REF1 and INFO_REF2. Return
# reference to resulting hash.
#

sub combine_info_files($$)
{
	my %hash1 = %{$_[0]};
	my %hash2 = %{$_[1]};
	my $filename;

	foreach $filename (keys(%hash2))
	{
		if ($hash1{$filename})
		{
			# Entry already exists in hash1, combine them
			$hash1{$filename} =
				combine_info_entries($hash1{$filename},
						     $hash2{$filename},
						     $filename);
		}
		else
		{
			# Entry is unique in both hashes, simply add to
			# resulting hash
			$hash1{$filename} = $hash2{$filename};
		}
	}

	return(\%hash1);
}


#
# get_prefix(min_dir, filename_list)
#
# Search FILENAME_LIST for a directory prefix which is common to as many
# list entries as possible, so that removing this prefix will minimize the
# sum of the lengths of all resulting shortened filenames while observing
# that no filename has less than MIN_DIR parent directories.
#

sub get_prefix($@)
{
	my ($min_dir, @filename_list) = @_;
	my %prefix;			# mapping: prefix -> sum of lengths
	my $current;			# Temporary iteration variable

	# Find list of prefixes
	foreach (@filename_list)
	{
		# Need explicit assignment to get a copy of $_ so that
		# shortening the contained prefix does not affect the list
		$current = $_;
		while ($current = shorten_prefix($current))
		{
			$current .= "/";

			# Skip rest if the remaining prefix has already been
			# added to hash
			if (exists($prefix{$current})) { last; }

			# Initialize with 0
			$prefix{$current}="0";
		}

	}

	# Remove all prefixes that would cause filenames to have less than
	# the minimum number of parent directories
	foreach my $filename (@filename_list) {
		my $dir = dirname($filename);

		for (my $i = 0; $i < $min_dir; $i++) {
			delete($prefix{$dir."/"});
			$dir = shorten_prefix($dir);
		}
	}

	# Check if any prefix remains
	return undef if (!%prefix);

	# Calculate sum of lengths for all prefixes
	foreach $current (keys(%prefix))
	{
		foreach (@filename_list)
		{
			# Add original length
			$prefix{$current} += length($_);

			# Check whether prefix matches
			if (substr($_, 0, length($current)) eq $current)
			{
				# Subtract prefix length for this filename
				$prefix{$current} -= length($current);
			}
		}
	}

	# Find and return prefix with minimal sum
	$current = (keys(%prefix))[0];

	foreach (keys(%prefix))
	{
		if ($prefix{$_} < $prefix{$current})
		{
			$current = $_;
		}
	}

	$current =~ s/\/$//;

	return($current);
}


#
# shorten_prefix(prefix)
#
# Return PREFIX shortened by last directory component.
#

sub shorten_prefix($)
{
	my @list = split("/", $_[0]);

	pop(@list);
	return join("/", @list);
}



#
# get_dir_list(filename_list)
#
# Return sorted list of directories for each entry in given FILENAME_LIST.
#

sub get_dir_list(@)
{
	my %result;

	foreach (@_)
	{
		$result{shorten_prefix($_)} = "";
	}

	return(sort(keys(%result)));
}


#
# get_relative_base_path(subdirectory)
#
# Return a relative path string which references the base path when applied
# in SUBDIRECTORY.
#
# Example: get_relative_base_path("fs/mm") -> "../../"
#

sub get_relative_base_path($)
{
	my $result = "";
	my $index;

	# Make an empty directory path a special case
	if (!$_[0]) { return(""); }

	# Count number of /s in path
	$index = ($_[0] =~ s/\//\//g);

	# Add a ../ to $result for each / in the directory path + 1
	for (; $index>=0; $index--)
	{
		$result .= "../";
	}

	return $result;
}


#
# read_testfile(test_filename)
#
# Read in file TEST_FILENAME which contains test descriptions in the format:
#
#   TN:<whitespace><test name>
#   TD:<whitespace><test description>
#
# for each test case. Return a reference to a hash containing a mapping
#
#   test name -> test description.
#
# Die on error.
#

sub read_testfile($)
{
	my %result;
	my $test_name;
	my $changed_testname;
	local *TEST_HANDLE;

	open(TEST_HANDLE, "<", $_[0])
		or die("ERROR: cannot open $_[0]!\n");

	while (<TEST_HANDLE>)
	{
		chomp($_);

		# Match lines beginning with TN:<whitespace(s)>
		if (/^TN:\s+(.*?)\s*$/)
		{
			# Store name for later use
			$test_name = $1;
			if ($test_name =~ s/\W/_/g)
			{
				$changed_testname = 1;
			}
		}

		# Match lines beginning with TD:<whitespace(s)>
		if (/^TD:\s+(.*?)\s*$/)
		{
			if (!defined($test_name)) {
				die("ERROR: Found test description without prior test name in $_[0]:$.\n");
			}
			# Check for empty line
			if ($1)
			{
				# Add description to hash
				$result{$test_name} .= " $1";
			}
			else
			{
				# Add empty line
				$result{$test_name} .= "\n\n";
			}
		}
	}

	close(TEST_HANDLE);

	if ($changed_testname)
	{
		warn("WARNING: invalid characters removed from testname in ".
		     "descriptions file $_[0]\n");
	}

	return \%result;
}


#
# escape_html(STRING)
#
# Return a copy of STRING in which all occurrences of HTML special characters
# are escaped.
#

sub escape_html($)
{
	my $string = $_[0];

	if (!$string) { return ""; }

	$string =~ s/&/&amp;/g;		# & -> &amp;
	$string =~ s/</&lt;/g;		# < -> &lt;
	$string =~ s/>/&gt;/g;		# > -> &gt;
	$string =~ s/\"/&quot;/g;	# " -> &quot;

	while ($string =~ /^([^\t]*)(\t)/)
	{
		my $replacement = " "x($tab_size - (length($1) % $tab_size));
		$string =~ s/^([^\t]*)(\t)/$1$replacement/;
	}

	$string =~ s/\n/<br>/g;		# \n -> <br>

	return $string;
}


#
# get_date_string()
#
# Return the current date in the form: yyyy-mm-dd
#

sub get_date_string()
{
	my $year;
	my $month;
	my $day;
	my $hour;
	my $min;
	my $sec;

	($year, $month, $day, $hour, $min, $sec) =
		(localtime())[5, 4, 3, 2, 1, 0];

	return sprintf("%d-%02d-%02d %02d:%02d:%02d", $year+1900, $month+1,
		       $day, $hour, $min, $sec);
}


#
# create_sub_dir(dir_name)
#
# Create subdirectory DIR_NAME if it does not already exist, including all its
# parent directories.
#
# Die on error.
#

sub create_sub_dir($)
{
	my ($dir) = @_;

	system("mkdir", "-p" ,$dir)
		and die("ERROR: cannot create directory $dir!\n");
}


#
# write_description_file(descriptions, overall_found, overall_hit,
#                        total_fn_found, total_fn_hit, total_br_found,
#                        total_br_hit)
#
# Write HTML file containing all test case descriptions. DESCRIPTIONS is a
# reference to a hash containing a mapping
#
#   test case name -> test case description
#
# Die on error.
#

sub write_description_file($$$$$$$)
{
	my %description = %{$_[0]};
	my $found = $_[1];
	my $hit = $_[2];
	my $fn_found = $_[3];
	my $fn_hit = $_[4];
	my $br_found = $_[5];
	my $br_hit = $_[6];
	my $test_name;
	local *HTML_HANDLE;

	html_create(*HTML_HANDLE,"descriptions.$html_ext");
	write_html_prolog(*HTML_HANDLE, "", "LCOV - test case descriptions");
	write_header(*HTML_HANDLE, 3, "", "", $found, $hit, $fn_found,
		     $fn_hit, $br_found, $br_hit, 0);

	write_test_table_prolog(*HTML_HANDLE,
			 "Test case descriptions - alphabetical list");

	foreach $test_name (sort(keys(%description)))
	{
		my $desc = $description{$test_name};

		$desc = escape_html($desc) if (!$rc_desc_html);
		write_test_table_entry(*HTML_HANDLE, $test_name, $desc);
	}

	write_test_table_epilog(*HTML_HANDLE);
	write_html_epilog(*HTML_HANDLE, "");

	close(*HTML_HANDLE);
}



#
# write_png_files()
#
# Create all necessary .png files for the HTML-output in the current
# directory. .png-files are used as bar graphs.
#
# Die on error.
#

sub write_png_files()
{
	my %data;
	local *PNG_HANDLE;

	$data{"ruby.png"} =
		[0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 
		 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 
		 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x00, 0x00, 0x00, 0x25, 
		 0xdb, 0x56, 0xca, 0x00, 0x00, 0x00, 0x07, 0x74, 0x49, 0x4d, 
		 0x45, 0x07, 0xd2, 0x07, 0x11, 0x0f, 0x18, 0x10, 0x5d, 0x57, 
		 0x34, 0x6e, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 
		 0x00, 0x00, 0x0b, 0x12, 0x00, 0x00, 0x0b, 0x12, 0x01, 0xd2, 
		 0xdd, 0x7e, 0xfc, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41, 0x4d, 
		 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc, 0x61, 0x05, 0x00, 
		 0x00, 0x00, 0x06, 0x50, 0x4c, 0x54, 0x45, 0xff, 0x35, 0x2f, 
		 0x00, 0x00, 0x00, 0xd0, 0x33, 0x9a, 0x9d, 0x00, 0x00, 0x00, 
		 0x0a, 0x49, 0x44, 0x41, 0x54, 0x78, 0xda, 0x63, 0x60, 0x00, 
		 0x00, 0x00, 0x02, 0x00, 0x01, 0xe5, 0x27, 0xde, 0xfc, 0x00, 
		 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 
		 0x82];
	$data{"amber.png"} =
		[0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 
		 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 
		 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x00, 0x00, 0x00, 0x25, 
		 0xdb, 0x56, 0xca, 0x00, 0x00, 0x00, 0x07, 0x74, 0x49, 0x4d, 
		 0x45, 0x07, 0xd2, 0x07, 0x11, 0x0f, 0x28, 0x04, 0x98, 0xcb, 
		 0xd6, 0xe0, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 
		 0x00, 0x00, 0x0b, 0x12, 0x00, 0x00, 0x0b, 0x12, 0x01, 0xd2, 
		 0xdd, 0x7e, 0xfc, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41, 0x4d, 
		 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc, 0x61, 0x05, 0x00, 
		 0x00, 0x00, 0x06, 0x50, 0x4c, 0x54, 0x45, 0xff, 0xe0, 0x50, 
		 0x00, 0x00, 0x00, 0xa2, 0x7a, 0xda, 0x7e, 0x00, 0x00, 0x00, 
		 0x0a, 0x49, 0x44, 0x41, 0x54, 0x78, 0xda, 0x63, 0x60, 0x00, 
	  	 0x00, 0x00, 0x02, 0x00, 0x01, 0xe5, 0x27, 0xde, 0xfc, 0x00, 
		 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 
		 0x82];
	$data{"emerald.png"} =
		[0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 
		 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 
		 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x00, 0x00, 0x00, 0x25, 
		 0xdb, 0x56, 0xca, 0x00, 0x00, 0x00, 0x07, 0x74, 0x49, 0x4d, 
		 0x45, 0x07, 0xd2, 0x07, 0x11, 0x0f, 0x22, 0x2b, 0xc9, 0xf5, 
		 0x03, 0x33, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 
		 0x00, 0x00, 0x0b, 0x12, 0x00, 0x00, 0x0b, 0x12, 0x01, 0xd2, 
		 0xdd, 0x7e, 0xfc, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41, 0x4d, 
		 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc, 0x61, 0x05, 0x00, 
		 0x00, 0x00, 0x06, 0x50, 0x4c, 0x54, 0x45, 0x1b, 0xea, 0x59, 
		 0x0a, 0x0a, 0x0a, 0x0f, 0xba, 0x50, 0x83, 0x00, 0x00, 0x00, 
		 0x0a, 0x49, 0x44, 0x41, 0x54, 0x78, 0xda, 0x63, 0x60, 0x00, 
		 0x00, 0x00, 0x02, 0x00, 0x01, 0xe5, 0x27, 0xde, 0xfc, 0x00, 
		 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 
		 0x82];
	$data{"snow.png"} =
		[0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 
		 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 
		 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x00, 0x00, 0x00, 0x25, 
		 0xdb, 0x56, 0xca, 0x00, 0x00, 0x00, 0x07, 0x74, 0x49, 0x4d, 
		 0x45, 0x07, 0xd2, 0x07, 0x11, 0x0f, 0x1e, 0x1d, 0x75, 0xbc, 
		 0xef, 0x55, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 
		 0x00, 0x00, 0x0b, 0x12, 0x00, 0x00, 0x0b, 0x12, 0x01, 0xd2, 
		 0xdd, 0x7e, 0xfc, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41, 0x4d, 
		 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc, 0x61, 0x05, 0x00, 
		 0x00, 0x00, 0x06, 0x50, 0x4c, 0x54, 0x45, 0xff, 0xff, 0xff, 
		 0x00, 0x00, 0x00, 0x55, 0xc2, 0xd3, 0x7e, 0x00, 0x00, 0x00, 
		 0x0a, 0x49, 0x44, 0x41, 0x54, 0x78, 0xda, 0x63, 0x60, 0x00, 
		 0x00, 0x00, 0x02, 0x00, 0x01, 0xe5, 0x27, 0xde, 0xfc, 0x00, 
		 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 
		 0x82];
	$data{"glass.png"} =
		[0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 
		 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 
		 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x00, 0x00, 0x00, 0x25, 
		 0xdb, 0x56, 0xca, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41, 0x4d, 
		 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc, 0x61, 0x05, 0x00, 
		 0x00, 0x00, 0x06, 0x50, 0x4c, 0x54, 0x45, 0xff, 0xff, 0xff, 
		 0x00, 0x00, 0x00, 0x55, 0xc2, 0xd3, 0x7e, 0x00, 0x00, 0x00, 
		 0x01, 0x74, 0x52, 0x4e, 0x53, 0x00, 0x40, 0xe6, 0xd8, 0x66, 
		 0x00, 0x00, 0x00, 0x01, 0x62, 0x4b, 0x47, 0x44, 0x00, 0x88, 
		 0x05, 0x1d, 0x48, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 
		 0x73, 0x00, 0x00, 0x0b, 0x12, 0x00, 0x00, 0x0b, 0x12, 0x01, 
		 0xd2, 0xdd, 0x7e, 0xfc, 0x00, 0x00, 0x00, 0x07, 0x74, 0x49, 
		 0x4d, 0x45, 0x07, 0xd2, 0x07, 0x13, 0x0f, 0x08, 0x19, 0xc4, 
		 0x40, 0x56, 0x10, 0x00, 0x00, 0x00, 0x0a, 0x49, 0x44, 0x41, 
		 0x54, 0x78, 0x9c, 0x63, 0x60, 0x00, 0x00, 0x00, 0x02, 0x00, 
		 0x01, 0x48, 0xaf, 0xa4, 0x71, 0x00, 0x00, 0x00, 0x00, 0x49, 
		 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82];
	$data{"updown.png"} =
		[0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 
		 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x0a, 
		 0x00, 0x00, 0x00, 0x0e, 0x08, 0x06, 0x00, 0x00, 0x00, 0x16, 
		 0xa3, 0x8d, 0xab, 0x00, 0x00, 0x00, 0x3c, 0x49, 0x44, 0x41, 
		 0x54, 0x28, 0xcf, 0x63, 0x60, 0x40, 0x03, 0xff, 0xa1, 0x00, 
		 0x5d, 0x9c, 0x11, 0x5d, 0x11, 0x8a, 0x24, 0x23, 0x23, 0x23, 
		 0x86, 0x42, 0x6c, 0xa6, 0x20, 0x2b, 0x66, 0xc4, 0xa7, 0x08, 
		 0x59, 0x31, 0x23, 0x21, 0x45, 0x30, 0xc0, 0xc4, 0x30, 0x60, 
		 0x80, 0xfa, 0x6e, 0x24, 0x3e, 0x78, 0x48, 0x0a, 0x70, 0x62, 
		 0xa2, 0x90, 0x81, 0xd8, 0x44, 0x01, 0x00, 0xe9, 0x5c, 0x2f, 
		 0xf5, 0xe2, 0x9d, 0x0f, 0xf9, 0x00, 0x00, 0x00, 0x00, 0x49, 
		 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82] if ($sort);
	foreach (keys(%data))
	{
		open(PNG_HANDLE, ">", $_)
			or die("ERROR: cannot create $_!\n");
		binmode(PNG_HANDLE);
		print(PNG_HANDLE map(chr,@{$data{$_}}));
		close(PNG_HANDLE);
	}
}


#
# write_htaccess_file()
#

sub write_htaccess_file()
{
	local *HTACCESS_HANDLE;
	my $htaccess_data;

	open(*HTACCESS_HANDLE, ">", ".htaccess")
		or die("ERROR: cannot open .htaccess for writing!\n");

	$htaccess_data = (<<"END_OF_HTACCESS")
AddEncoding x-gzip .html
END_OF_HTACCESS
	;

	print(HTACCESS_HANDLE $htaccess_data);
	close(*HTACCESS_HANDLE);
}


#
# write_css_file()
#
# Write the cascading style sheet file gcov.css to the current directory.
# This file defines basic layout attributes of all generated HTML pages.
#

sub write_css_file()
{
	local *CSS_HANDLE;

	# Check for a specified external style sheet file
	if ($css_filename)
	{
		# Simply copy that file
		system("cp", $css_filename, "gcov.css")
			and die("ERROR: cannot copy file $css_filename!\n");
		return;
	}

	open(CSS_HANDLE, ">", "gcov.css")
		or die ("ERROR: cannot open gcov.css for writing!\n");


	# *************************************************************

	my $css_data = ($_=<<"END_OF_CSS")
	/* All views: initial background and text color */
	body
	{
	  color: #000000;
	  background-color: #FFFFFF;
	}
	
	/* All views: standard link format*/
	a:link
	{
	  color: #284FA8;
	  text-decoration: underline;
	}
	
	/* All views: standard link - visited format */
	a:visited
	{
	  color: #00CB40;
	  text-decoration: underline;
	}
	
	/* All views: standard link - activated format */
	a:active
	{
	  color: #FF0040;
	  text-decoration: underline;
	}
	
	/* All views: main title format */
	td.title
	{
	  text-align: center;
	  padding-bottom: 10px;
	  font-family: sans-serif;
	  font-size: 20pt;
	  font-style: italic;
	  font-weight: bold;
	}
	
	/* All views: header item format */
	td.headerItem
	{
	  text-align: right;
	  padding-right: 6px;
	  font-family: sans-serif;
	  font-weight: bold;
	  vertical-align: top;
	  white-space: nowrap;
	}
	
	/* All views: header item value format */
	td.headerValue
	{
	  text-align: left;
	  color: #284FA8;
	  font-family: sans-serif;
	  font-weight: bold;
	  white-space: nowrap;
	}

	/* All views: header item coverage table heading */
	td.headerCovTableHead
	{
	  text-align: center;
	  padding-right: 6px;
	  padding-left: 6px;
	  padding-bottom: 0px;
	  font-family: sans-serif;
	  font-size: 80%;
	  white-space: nowrap;
	}
	
	/* All views: header item coverage table entry */
	td.headerCovTableEntry
	{
	  text-align: right;
	  color: #284FA8;
	  font-family: sans-serif;
	  font-weight: bold;
	  white-space: nowrap;
	  padding-left: 12px;
	  padding-right: 4px;
	  background-color: #DAE7FE;
	}
	
	/* All views: header item coverage table entry for high coverage rate */
	td.headerCovTableEntryHi
	{
	  text-align: right;
	  color: #000000;
	  font-family: sans-serif;
	  font-weight: bold;
	  white-space: nowrap;
	  padding-left: 12px;
	  padding-right: 4px;
	  background-color: #A7FC9D;
	}
	
	/* All views: header item coverage table entry for medium coverage rate */
	td.headerCovTableEntryMed
	{
	  text-align: right;
	  color: #000000;
	  font-family: sans-serif;
	  font-weight: bold;
	  white-space: nowrap;
	  padding-left: 12px;
	  padding-right: 4px;
	  background-color: #FFEA20;
	}
	
	/* All views: header item coverage table entry for ow coverage rate */
	td.headerCovTableEntryLo
	{
	  text-align: right;
	  color: #000000;
	  font-family: sans-serif;
	  font-weight: bold;
	  white-space: nowrap;
	  padding-left: 12px;
	  padding-right: 4px;
	  background-color: #FF0000;
	}
	
	/* All views: header legend value for legend entry */
	td.headerValueLeg
	{
	  text-align: left;
	  color: #000000;
	  font-family: sans-serif;
	  font-size: 80%;
	  white-space: nowrap;
	  padding-top: 4px;
	}
	
	/* All views: color of horizontal ruler */
	td.ruler
	{
	  background-color: #6688D4;
	}
	
	/* All views: version string format */
	td.versionInfo
	{
	  text-align: center;
	  padding-top: 2px;
	  font-family: sans-serif;
	  font-style: italic;
	}
	
	/* Directory view/File view (all)/Test case descriptions:
	   table headline format */
	td.tableHead
	{
	  text-align: center;
	  color: #FFFFFF;
	  background-color: #6688D4;
	  font-family: sans-serif;
	  font-size: 120%;
	  font-weight: bold;
	  white-space: nowrap;
	  padding-left: 4px;
	  padding-right: 4px;
	}

	span.tableHeadSort
	{
	  padding-right: 4px;
	}
	
	/* Directory view/File view (all): filename entry format */
	td.coverFile
	{
	  text-align: left;
	  padding-left: 10px;
	  padding-right: 20px; 
	  color: #284FA8;
	  background-color: #DAE7FE;
	  font-family: monospace;
	}
	
	/* Directory view/File view (all): bar-graph entry format*/
	td.coverBar
	{
	  padding-left: 10px;
	  padding-right: 10px;
	  background-color: #DAE7FE;
	}
	
	/* Directory view/File view (all): bar-graph outline color */
	td.coverBarOutline
	{
	  background-color: #000000;
	}
	
	/* Directory view/File view (all): percentage entry for files with
	   high coverage rate */
	td.coverPerHi
	{
	  text-align: right;
	  padding-left: 10px;
	  padding-right: 10px;
	  background-color: #A7FC9D;
	  font-weight: bold;
	  font-family: sans-serif;
	}
	
	/* Directory view/File view (all): line count entry for files with
	   high coverage rate */
	td.coverNumHi
	{
	  text-align: right;
	  padding-left: 10px;
	  padding-right: 10px;
	  background-color: #A7FC9D;
	  white-space: nowrap;
	  font-family: sans-serif;
	}
	
	/* Directory view/File view (all): percentage entry for files with
	   medium coverage rate */
	td.coverPerMed
	{
	  text-align: right;
	  padding-left: 10px;
	  padding-right: 10px;
	  background-color: #FFEA20;
	  font-weight: bold;
	  font-family: sans-serif;
	}
	
	/* Directory view/File view (all): line count entry for files with
	   medium coverage rate */
	td.coverNumMed
	{
	  text-align: right;
	  padding-left: 10px;
	  padding-right: 10px;
	  background-color: #FFEA20;
	  white-space: nowrap;
	  font-family: sans-serif;
	}
	
	/* Directory view/File view (all): percentage entry for files with
	   low coverage rate */
	td.coverPerLo
	{
	  text-align: right;
	  padding-left: 10px;
	  padding-right: 10px;
	  background-color: #FF0000;
	  font-weight: bold;
	  font-family: sans-serif;
	}
	
	/* Directory view/File view (all): line count entry for files with
	   low coverage rate */
	td.coverNumLo
	{
	  text-align: right;
	  padding-left: 10px;
	  padding-right: 10px;
	  background-color: #FF0000;
	  white-space: nowrap;
	  font-family: sans-serif;
	}
	
	/* File view (all): "show/hide details" link format */
	a.detail:link
	{
	  color: #B8D0FF;
	  font-size:80%;
	}
	
	/* File view (all): "show/hide details" link - visited format */
	a.detail:visited
	{
	  color: #B8D0FF;
	  font-size:80%;
	}
	
	/* File view (all): "show/hide details" link - activated format */
	a.detail:active
	{
	  color: #FFFFFF;
	  font-size:80%;
	}
	
	/* File view (detail): test name entry */
	td.testName
	{
	  text-align: right;
	  padding-right: 10px;
	  background-color: #DAE7FE;
	  font-family: sans-serif;
	}
	
	/* File view (detail): test percentage entry */
	td.testPer
	{
	  text-align: right;
	  padding-left: 10px;
	  padding-right: 10px; 
	  background-color: #DAE7FE;
	  font-family: sans-serif;
	}
	
	/* File view (detail): test lines count entry */
	td.testNum
	{
	  text-align: right;
	  padding-left: 10px;
	  padding-right: 10px; 
	  background-color: #DAE7FE;
	  font-family: sans-serif;
	}
	
	/* Test case descriptions: test name format*/
	dt
	{
	  font-family: sans-serif;
	  font-weight: bold;
	}
	
	/* Test case descriptions: description table body */
	td.testDescription
	{
	  padding-top: 10px;
	  padding-left: 30px;
	  padding-bottom: 10px;
	  padding-right: 30px;
	  background-color: #DAE7FE;
	}
	
	/* Source code view: function entry */
	td.coverFn
	{
	  text-align: left;
	  padding-left: 10px;
	  padding-right: 20px; 
	  color: #284FA8;
	  background-color: #DAE7FE;
	  font-family: monospace;
	}

	/* Source code view: function entry zero count*/
	td.coverFnLo
	{
	  text-align: right;
	  padding-left: 10px;
	  padding-right: 10px;
	  background-color: #FF0000;
	  font-weight: bold;
	  font-family: sans-serif;
	}

	/* Source code view: function entry nonzero count*/
	td.coverFnHi
	{
	  text-align: right;
	  padding-left: 10px;
	  padding-right: 10px;
	  background-color: #DAE7FE;
	  font-weight: bold;
	  font-family: sans-serif;
	}

	/* Source code view: source code format */
	pre.source
	{
	  font-family: monospace;
	  white-space: pre;
	  margin-top: 2px;
	}
	
	/* Source code view: line number format */
	span.lineNum
	{
	  background-color: #EFE383;
	}
	
	/* Source code view: format for lines which were executed */
	td.lineCov,
	span.lineCov
	{
	  background-color: #CAD7FE;
	}
	
	/* Source code view: format for Cov legend */
	span.coverLegendCov
	{
	  padding-left: 10px;
	  padding-right: 10px;
	  padding-bottom: 2px;
	  background-color: #CAD7FE;
	}
	
	/* Source code view: format for lines which were not executed */
	td.lineNoCov,
	span.lineNoCov
	{
	  background-color: #FF6230;
	}
	
	/* Source code view: format for NoCov legend */
	span.coverLegendNoCov
	{
	  padding-left: 10px;
	  padding-right: 10px;
	  padding-bottom: 2px;
	  background-color: #FF6230;
	}
	
	/* Source code view (function table): standard link - visited format */
	td.lineNoCov > a:visited,
	td.lineCov > a:visited
	{  
	  color: black;
	  text-decoration: underline;
	}  
	
	/* Source code view: format for lines which were executed only in a
	   previous version */
	span.lineDiffCov
	{
	  background-color: #B5F7AF;
	}
	
	/* Source code view: format for branches which were executed
	 * and taken */
	span.branchCov
	{
	  background-color: #CAD7FE;
	}

	/* Source code view: format for branches which were executed
	 * but not taken */
	span.branchNoCov
	{
	  background-color: #FF6230;
	}

	/* Source code view: format for branches which were not executed */
	span.branchNoExec
	{
	  background-color: #FF6230;
	}

	/* Source code view: format for the source code heading line */
	pre.sourceHeading
	{
	  white-space: pre;
	  font-family: monospace;
	  font-weight: bold;
	  margin: 0px;
	}

	/* All views: header legend value for low rate */
	td.headerValueLegL
	{
	  font-family: sans-serif;
	  text-align: center;
	  white-space: nowrap;
	  padding-left: 4px;
	  padding-right: 2px;
	  background-color: #FF0000;
	  font-size: 80%;
	}

	/* All views: header legend value for med rate */
	td.headerValueLegM
	{
	  font-family: sans-serif;
	  text-align: center;
	  white-space: nowrap;
	  padding-left: 2px;
	  padding-right: 2px;
	  background-color: #FFEA20;
	  font-size: 80%;
	}

	/* All views: header legend value for hi rate */
	td.headerValueLegH
	{
	  font-family: sans-serif;
	  text-align: center;
	  white-space: nowrap;
	  padding-left: 2px;
	  padding-right: 4px;
	  background-color: #A7FC9D;
	  font-size: 80%;
	}

	/* All views except source code view: legend format for low coverage */
	span.coverLegendCovLo
	{
	  padding-left: 10px;
	  padding-right: 10px;
	  padding-top: 2px;
	  background-color: #FF0000;
	}

	/* All views except source code view: legend format for med coverage */
	span.coverLegendCovMed
	{
	  padding-left: 10px;
	  padding-right: 10px;
	  padding-top: 2px;
	  background-color: #FFEA20;
	}

	/* All views except source code view: legend format for hi coverage */
	span.coverLegendCovHi
	{
	  padding-left: 10px;
	  padding-right: 10px;
	  padding-top: 2px;
	  background-color: #A7FC9D;
	}
END_OF_CSS
	;

	# *************************************************************


	# Remove leading tab from all lines
	$css_data =~ s/^\t//gm;

	print(CSS_HANDLE $css_data);

	close(CSS_HANDLE);
}


#
# get_bar_graph_code(base_dir, cover_found, cover_hit)
#
# Return a string containing HTML code which implements a bar graph display
# for a coverage rate of cover_hit * 100 / cover_found.
#

sub get_bar_graph_code($$$)
{
	my ($base_dir, $found, $hit) = @_;
	my $rate;
	my $alt;
	my $width;
	my $remainder;
	my $png_name;
	my $graph_code;

	# Check number of instrumented lines
	if ($_[1] == 0) { return ""; }

	$alt		= rate($hit, $found, "%");
	$width		= rate($hit, $found, undef, 0);
	$remainder	= 100 - $width;

	# Decide which .png file to use
	$png_name = $rate_png[classify_rate($found, $hit, $med_limit,
					    $hi_limit)];

	if ($width == 0)
	{
		# Zero coverage
		$graph_code = (<<END_OF_HTML)
	        <table border=0 cellspacing=0 cellpadding=1><tr><td class="coverBarOutline"><img src="$_[0]snow.png" width=100 height=10 alt="$alt"></td></tr></table>
END_OF_HTML
		;
	}
	elsif ($width == 100)
	{
		# Full coverage
		$graph_code = (<<END_OF_HTML)
		<table border=0 cellspacing=0 cellpadding=1><tr><td class="coverBarOutline"><img src="$_[0]$png_name" width=100 height=10 alt="$alt"></td></tr></table>
END_OF_HTML
		;
	}
	else
	{
		# Positive coverage
		$graph_code = (<<END_OF_HTML)
		<table border=0 cellspacing=0 cellpadding=1><tr><td class="coverBarOutline"><img src="$_[0]$png_name" width=$width height=10 alt="$alt"><img src="$_[0]snow.png" width=$remainder height=10 alt="$alt"></td></tr></table>
END_OF_HTML
		;
	}

	# Remove leading tabs from all lines
	$graph_code =~ s/^\t+//gm;
	chomp($graph_code);

	return($graph_code);
}

#
# sub classify_rate(found, hit, med_limit, high_limit)
#
# Return 0 for low rate, 1 for medium rate and 2 for hi rate.
#

sub classify_rate($$$$)
{
	my ($found, $hit, $med, $hi) = @_;
	my $rate;

	if ($found == 0) {
		return 2;
	}
	$rate = rate($hit, $found);
	if ($rate < $med) {
		return 0;
	} elsif ($rate < $hi) {
		return 1;
	}
	return 2;
}


#
# write_html(filehandle, html_code)
#
# Write out HTML_CODE to FILEHANDLE while removing a leading tabulator mark
# in each line of HTML_CODE.
#

sub write_html(*$)
{
	local *HTML_HANDLE = $_[0];
	my $html_code = $_[1];

	# Remove leading tab from all lines
	$html_code =~ s/^\t//gm;

	print(HTML_HANDLE $html_code)
		or die("ERROR: cannot write HTML data ($!)\n");
}


#
# write_html_prolog(filehandle, base_dir, pagetitle)
#
# Write an HTML prolog common to all HTML files to FILEHANDLE. PAGETITLE will
# be used as HTML page title. BASE_DIR contains a relative path which points
# to the base directory.
#

sub write_html_prolog(*$$)
{
	my $basedir = $_[1];
	my $pagetitle = $_[2];
	my $prolog;

	$prolog = $html_prolog;
	$prolog =~ s/\@pagetitle\@/$pagetitle/g;
	$prolog =~ s/\@basedir\@/$basedir/g;

	write_html($_[0], $prolog);
}


#
# write_header_prolog(filehandle, base_dir)
#
# Write beginning of page header HTML code.
#

sub write_header_prolog(*$)
{
	# *************************************************************

	write_html($_[0], <<END_OF_HTML)
	  <table width="100%" border=0 cellspacing=0 cellpadding=0>
	    <tr><td class="title">$title</td></tr>
	    <tr><td class="ruler"><img src="$_[1]glass.png" width=3 height=3 alt=""></td></tr>

	    <tr>
	      <td width="100%">
	        <table cellpadding=1 border=0 width="100%">
END_OF_HTML
	;

	# *************************************************************
}


#
# write_header_line(handle, content)
#
# Write a header line with the specified table contents.
#

sub write_header_line(*@)
{
	my ($handle, @content) = @_;
	my $entry;

	write_html($handle, "          <tr>\n");
	foreach $entry (@content) {
		my ($width, $class, $text, $colspan) = @{$entry};

		if (defined($width)) {
			$width = " width=\"$width\"";
		} else {
			$width = "";
		}
		if (defined($class)) {
			$class = " class=\"$class\"";
		} else {
			$class = "";
		}
		if (defined($colspan)) {
			$colspan = " colspan=\"$colspan\"";
		} else {
			$colspan = "";
		}
		$text = "" if (!defined($text));
		write_html($handle,
			   "            <td$width$class$colspan>$text</td>\n");
	}
	write_html($handle, "          </tr>\n");
}


#
# write_header_epilog(filehandle, base_dir)
#
# Write end of page header HTML code.
#

sub write_header_epilog(*$)
{
	# *************************************************************

	write_html($_[0], <<END_OF_HTML)
	          <tr><td><img src="$_[1]glass.png" width=3 height=3 alt=""></td></tr>
	        </table>
	      </td>
	    </tr>

	    <tr><td class="ruler"><img src="$_[1]glass.png" width=3 height=3 alt=""></td></tr>
	  </table>

END_OF_HTML
	;

	# *************************************************************
}


#
# write_file_table_prolog(handle, file_heading, ([heading, num_cols], ...))
#
# Write heading for file table.
#

sub write_file_table_prolog(*$@)
{
	my ($handle, $file_heading, @columns) = @_;
	my $num_columns = 0;
	my $file_width;
	my $col;
	my $width;

	$width = 20 if (scalar(@columns) == 1);
	$width = 10 if (scalar(@columns) == 2);
	$width = 8 if (scalar(@columns) > 2);

	foreach $col (@columns) {
		my ($heading, $cols) = @{$col};

		$num_columns += $cols;
	}
	$file_width = 100 - $num_columns * $width;

	# Table definition
	write_html($handle, <<END_OF_HTML);
	  <center>
	  <table width="80%" cellpadding=1 cellspacing=1 border=0>

	    <tr>
	      <td width="$file_width%"><br></td>
END_OF_HTML
	# Empty first row
	foreach $col (@columns) {
		my ($heading, $cols) = @{$col};

		while ($cols-- > 0) {
			write_html($handle, <<END_OF_HTML);
	      <td width="$width%"></td>
END_OF_HTML
		}
	}
	# Next row
	write_html($handle, <<END_OF_HTML);
	    </tr>

	    <tr>
	      <td class="tableHead">$file_heading</td>
END_OF_HTML
	# Heading row
	foreach $col (@columns) {
		my ($heading, $cols) = @{$col};
		my $colspan = "";

		$colspan = " colspan=$cols" if ($cols > 1);
		write_html($handle, <<END_OF_HTML);
	      <td class="tableHead"$colspan>$heading</td>
END_OF_HTML
	}
	write_html($handle, <<END_OF_HTML);
	    </tr>
END_OF_HTML
}


# write_file_table_entry(handle, base_dir, filename, page_link,
#			 ([ found, hit, med_limit, hi_limit, graph ], ..)
#
# Write an entry of the file table.
#

sub write_file_table_entry(*$$$@)
{
	my ($handle, $base_dir, $filename, $page_link, @entries) = @_;
	my $file_code;
	my $entry;
	my $esc_filename = escape_html($filename);

	# Add link to source if provided
	if (defined($page_link) && $page_link ne "") {
		$file_code = "<a href=\"$page_link\">$esc_filename</a>";
	} else {
		$file_code = $esc_filename;
	}

	# First column: filename
	write_html($handle, <<END_OF_HTML);
	    <tr>
	      <td class="coverFile">$file_code</td>
END_OF_HTML
	# Columns as defined
	foreach $entry (@entries) {
		my ($found, $hit, $med, $hi, $graph) = @{$entry};
		my $bar_graph;
		my $class;
		my $rate;

		# Generate bar graph if requested
		if ($graph) {
			$bar_graph = get_bar_graph_code($base_dir, $found,
							$hit);
			write_html($handle, <<END_OF_HTML);
	      <td class="coverBar" align="center">
	        $bar_graph
	      </td>
END_OF_HTML
		}
		# Get rate color and text
		if ($found == 0) {
			$rate = "-";
			$class = "Hi";
		} else {
			$rate = rate($hit, $found, "&nbsp;%");
			$class = $rate_name[classify_rate($found, $hit,
					    $med, $hi)];
		}
		write_html($handle, <<END_OF_HTML);
	      <td class="coverPer$class">$rate</td>
	      <td class="coverNum$class">$hit / $found</td>
END_OF_HTML
	}
	# End of row
        write_html($handle, <<END_OF_HTML);
	    </tr>
END_OF_HTML
}


#
# write_file_table_detail_entry(filehandle, test_name, ([found, hit], ...))
#
# Write entry for detail section in file table.
#

sub write_file_table_detail_entry(*$@)
{
	my ($handle, $test, @entries) = @_;
	my $entry;

	if ($test eq "") {
		$test = "<span style=\"font-style:italic\">&lt;unnamed&gt;</span>";
	} elsif ($test =~ /^(.*),diff$/) {
		$test = $1." (converted)";
	}
	# Testname
	write_html($handle, <<END_OF_HTML);
	    <tr>
	      <td class="testName" colspan=2>$test</td>
END_OF_HTML
	# Test data
	foreach $entry (@entries) {
		my ($found, $hit) = @{$entry};
		my $rate = rate($hit, $found, "&nbsp;%");

		write_html($handle, <<END_OF_HTML);
	      <td class="testPer">$rate</td>
	      <td class="testNum">$hit&nbsp;/&nbsp;$found</td>
END_OF_HTML
	}

        write_html($handle, <<END_OF_HTML);
	    </tr>

END_OF_HTML

	# *************************************************************
}


#
# write_file_table_epilog(filehandle)
#
# Write end of file table HTML code.
#

sub write_file_table_epilog(*)
{
	# *************************************************************

	write_html($_[0], <<END_OF_HTML)
	  </table>
	  </center>
	  <br>

END_OF_HTML
	;

	# *************************************************************
}


#
# write_test_table_prolog(filehandle, table_heading)
#
# Write heading for test case description table.
#

sub write_test_table_prolog(*$)
{
	# *************************************************************

	write_html($_[0], <<END_OF_HTML)
	  <center>
	  <table width="80%" cellpadding=2 cellspacing=1 border=0>

	    <tr>
	      <td><br></td>
	    </tr>

	    <tr>
	      <td class="tableHead">$_[1]</td>
	    </tr>

	    <tr>
	      <td class="testDescription">
	        <dl>
END_OF_HTML
	;

	# *************************************************************
}


#
# write_test_table_entry(filehandle, test_name, test_description)
#
# Write entry for the test table.
#

sub write_test_table_entry(*$$)
{
	# *************************************************************

	write_html($_[0], <<END_OF_HTML)
          <dt>$_[1]<a name="$_[1]">&nbsp;</a></dt>
          <dd>$_[2]<br><br></dd>
END_OF_HTML
	;

	# *************************************************************
}


#
# write_test_table_epilog(filehandle)
#
# Write end of test description table HTML code.
#

sub write_test_table_epilog(*)
{
	# *************************************************************

	write_html($_[0], <<END_OF_HTML)
	        </dl>
	      </td>
	    </tr>
	  </table>
	  </center>
	  <br>

END_OF_HTML
	;

	# *************************************************************
}


sub fmt_centered($$)
{
	my ($width, $text) = @_;
	my $w0 = length($text);
	my $w1 = int(($width - $w0) / 2);
	my $w2 = $width - $w0 - $w1;

	return (" "x$w1).$text.(" "x$w2);
}


#
# write_source_prolog(filehandle)
#
# Write start of source code table.
#

sub write_source_prolog(*)
{
	my $lineno_heading = "         ";
	my $branch_heading = "";
	my $line_heading = fmt_centered($line_field_width, "Line data");
	my $source_heading = " Source code";

	if ($br_coverage) {
		$branch_heading = fmt_centered($br_field_width, "Branch data").
				  " ";
	}
	# *************************************************************

	write_html($_[0], <<END_OF_HTML)
	  <table cellpadding=0 cellspacing=0 border=0>
	    <tr>
	      <td><br></td>
	    </tr>
	    <tr>
	      <td>
<pre class="sourceHeading">${lineno_heading}${branch_heading}${line_heading} ${source_heading}</pre>
<pre class="source">
END_OF_HTML
	;

	# *************************************************************
}


#
# get_branch_blocks(brdata)
#
# Group branches that belong to the same basic block.
#
# Returns: [block1, block2, ...]
# block:   [branch1, branch2, ...]
# branch:  [block_num, branch_num, taken_count, text_length, open, close]
#

sub get_branch_blocks($)
{
	my ($brdata) = @_;
	my $last_block_num;
	my $block = [];
	my @blocks;
	my $i;
	my $num = br_ivec_len($brdata);

	# Group branches
	for ($i = 0; $i < $num; $i++) {
		my ($block_num, $branch, $taken) = br_ivec_get($brdata, $i);
		my $br;

		if (defined($last_block_num) && $block_num != $last_block_num) {
			push(@blocks, $block);
			$block = [];
		}
		$br = [$block_num, $branch, $taken, 3, 0, 0];
		push(@{$block}, $br);
		$last_block_num = $block_num;
	}
	push(@blocks, $block) if (scalar(@{$block}) > 0);

	# Add braces to first and last branch in group
	foreach $block (@blocks) {
		$block->[0]->[$BR_OPEN] = 1;
		$block->[0]->[$BR_LEN]++;
		$block->[scalar(@{$block}) - 1]->[$BR_CLOSE] = 1;
		$block->[scalar(@{$block}) - 1]->[$BR_LEN]++;
	}

	return @blocks;
}

#
# get_block_len(block)
#
# Calculate total text length of all branches in a block of branches.
#

sub get_block_len($)
{
	my ($block) = @_;
	my $len = 0;
	my $branch;

	foreach $branch (@{$block}) {
		$len += $branch->[$BR_LEN];
	}

	return $len;
}


#
# get_branch_html(brdata)
#
# Return a list of HTML lines which represent the specified branch coverage
# data in source code view.
#

sub get_branch_html($)
{
	my ($brdata) = @_;
	my @blocks = get_branch_blocks($brdata);
	my $block;
	my $branch;
	my $line_len = 0;
	my $line = [];	# [branch2|" ", branch|" ", ...]
	my @lines;	# [line1, line2, ...]
	my @result;

	# Distribute blocks to lines
	foreach $block (@blocks) {
		my $block_len = get_block_len($block);

		# Does this block fit into the current line?
		if ($line_len + $block_len <= $br_field_width) {
			# Add it
			$line_len += $block_len;
			push(@{$line}, @{$block});
			next;
		} elsif ($block_len <= $br_field_width) {
			# It would fit if the line was empty - add it to new
			# line
			push(@lines, $line);
			$line_len = $block_len;
			$line = [ @{$block} ];
			next;
		}
		# Split the block into several lines
		foreach $branch (@{$block}) {
			if ($line_len + $branch->[$BR_LEN] >= $br_field_width) {
				# Start a new line
				if (($line_len + 1 <= $br_field_width) &&
				    scalar(@{$line}) > 0 &&
				    !$line->[scalar(@$line) - 1]->[$BR_CLOSE]) {
					# Try to align branch symbols to be in
					# one # row
					push(@{$line}, " ");
				}
				push(@lines, $line);
				$line_len = 0;
				$line = [];
			}
			push(@{$line}, $branch);
			$line_len += $branch->[$BR_LEN];
		}
	}
	push(@lines, $line);

	# Convert to HTML
	foreach $line (@lines) {
		my $current = "";
		my $current_len = 0;

		foreach $branch (@$line) {
			# Skip alignment space
			if ($branch eq " ") {
				$current .= " ";
				$current_len++;
				next;
			}

			my ($block_num, $br_num, $taken, $len, $open, $close) =
			   @{$branch};
			my $class;
			my $title;
			my $text;

			if ($taken eq '-') {
				$class	= "branchNoExec";
				$text	= " # ";
				$title	= "Branch $br_num was not executed";
			} elsif ($taken == 0) {
				$class	= "branchNoCov";
				$text	= " - ";
				$title	= "Branch $br_num was not taken";
			} else {
				$class	= "branchCov";
				$text	= " + ";
				$title	= "Branch $br_num was taken $taken ".
					  "time";
				$title .= "s" if ($taken > 1);
			}
			$current .= "[" if ($open);
			$current .= "<span class=\"$class\" title=\"$title\">";
			$current .= $text."</span>";
			$current .= "]" if ($close);
			$current_len += $len;
		}

		# Right-align result text
		if ($current_len < $br_field_width) {
			$current = (" "x($br_field_width - $current_len)).
				   $current;
		}
		push(@result, $current);
	}

	return @result;
}


#
# format_count(count, width)
#
# Return a right-aligned representation of count that fits in width characters.
#

sub format_count($$)
{
	my ($count, $width) = @_;
	my $result;
	my $exp;

	$result = sprintf("%*.0f", $width, $count);
	while (length($result) > $width) {
		last if ($count < 10);
		$exp++;
		$count = int($count/10);
		$result = sprintf("%*s", $width, ">$count*10^$exp");
	}
	return $result;
}

#
# write_source_line(filehandle, line_num, source, hit_count, converted,
#                   brdata, add_anchor)
#
# Write formatted source code line. Return a line in a format as needed
# by gen_png()
#

sub write_source_line(*$$$$$$)
{
	my ($handle, $line, $source, $count, $converted, $brdata,
	    $add_anchor) = @_;
	my $source_format;
	my $count_format;
	my $result;
	my $anchor_start = "";
	my $anchor_end = "";
	my $count_field_width = $line_field_width - 1;
	my @br_html;
	my $html;

	# Get branch HTML data for this line
	@br_html = get_branch_html($brdata) if ($br_coverage);

	if (!defined($count)) {
		$result		= "";
		$source_format	= "";
		$count_format	= " "x$count_field_width;
	}
	elsif ($count == 0) {
		$result		= $count;
		$source_format	= '<span class="lineNoCov">';
		$count_format	= format_count($count, $count_field_width);
	}
	elsif ($converted && defined($highlight)) {
		$result		= "*".$count;
		$source_format	= '<span class="lineDiffCov">';
		$count_format	= format_count($count, $count_field_width);
	}
	else {
		$result		= $count;
		$source_format	= '<span class="lineCov">';
		$count_format	= format_count($count, $count_field_width);
	}
	$result .= ":".$source;

	# Write out a line number navigation anchor every $nav_resolution
	# lines if necessary
	if ($add_anchor)
	{
		$anchor_start	= "<a name=\"$_[1]\">";
		$anchor_end	= "</a>";
	}


	# *************************************************************

	$html = $anchor_start;
	$html .= "<span class=\"lineNum\">".sprintf("%8d", $line)." </span>";
	$html .= shift(@br_html).":" if ($br_coverage);
	$html .= "$source_format$count_format : ";
	$html .= escape_html($source);
	$html .= "</span>" if ($source_format);
	$html .= $anchor_end."\n";

	write_html($handle, $html);

	if ($br_coverage) {
		# Add lines for overlong branch information
		foreach (@br_html) {
			write_html($handle, "<span class=\"lineNum\">".
				   "         </span>$_\n");
		}
	}
	# *************************************************************

	return($result);
}


#
# write_source_epilog(filehandle)
#
# Write end of source code table.
#

sub write_source_epilog(*)
{
	# *************************************************************

	write_html($_[0], <<END_OF_HTML)
	</pre>
	      </td>
	    </tr>
	  </table>
	  <br>

END_OF_HTML
	;

	# *************************************************************
}


#
# write_html_epilog(filehandle, base_dir[, break_frames])
#
# Write HTML page footer to FILEHANDLE. BREAK_FRAMES should be set when
# this page is embedded in a frameset, clicking the URL link will then
# break this frameset.
#

sub write_html_epilog(*$;$)
{
	my $basedir = $_[1];
	my $break_code = "";
	my $epilog;

	if (defined($_[2]))
	{
		$break_code = " target=\"_parent\"";
	}

	# *************************************************************

	write_html($_[0], <<END_OF_HTML)
	  <table width="100%" border=0 cellspacing=0 cellpadding=0>
	    <tr><td class="ruler"><img src="$_[1]glass.png" width=3 height=3 alt=""></td></tr>
	    <tr><td class="versionInfo">Generated by: <a href="$lcov_url"$break_code>$lcov_version</a></td></tr>
	  </table>
	  <br>
END_OF_HTML
	;

	$epilog = $html_epilog;
	$epilog =~ s/\@basedir\@/$basedir/g;

	write_html($_[0], $epilog);
}


#
# write_frameset(filehandle, basedir, basename, pagetitle)
#
#

sub write_frameset(*$$$)
{
	my $frame_width = $overview_width + 40;

	# *************************************************************

	write_html($_[0], <<END_OF_HTML)
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">

	<html lang="en">

	<head>
	  <meta http-equiv="Content-Type" content="text/html; charset=$charset">
	  <title>$_[3]</title>
	  <link rel="stylesheet" type="text/css" href="$_[1]gcov.css">
	</head>

	<frameset cols="$frame_width,*">
	  <frame src="$_[2].gcov.overview.$html_ext" name="overview">
	  <frame src="$_[2].gcov.$html_ext" name="source">
	  <noframes>
	    <center>Frames not supported by your browser!<br></center>
	  </noframes>
	</frameset>

	</html>
END_OF_HTML
	;

	# *************************************************************
}


#
# sub write_overview_line(filehandle, basename, line, link)
#
#

sub write_overview_line(*$$$)
{
	my $y1 = $_[2] - 1;
	my $y2 = $y1 + $nav_resolution - 1;
	my $x2 = $overview_width - 1;

	# *************************************************************

	write_html($_[0], <<END_OF_HTML)
	    <area shape="rect" coords="0,$y1,$x2,$y2" href="$_[1].gcov.$html_ext#$_[3]" target="source" alt="overview">
END_OF_HTML
	;

	# *************************************************************
}


#
# write_overview(filehandle, basedir, basename, pagetitle, lines)
#
#

sub write_overview(*$$$$)
{
	my $index;
	my $max_line = $_[4] - 1;
	my $offset;

	# *************************************************************

	write_html($_[0], <<END_OF_HTML)
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

	<html lang="en">

	<head>
	  <title>$_[3]</title>
	  <meta http-equiv="Content-Type" content="text/html; charset=$charset">
	  <link rel="stylesheet" type="text/css" href="$_[1]gcov.css">
	</head>

	<body>
	  <map name="overview">
END_OF_HTML
	;

	# *************************************************************

	# Make $offset the next higher multiple of $nav_resolution
	$offset = ($nav_offset + $nav_resolution - 1) / $nav_resolution;
	$offset = sprintf("%d", $offset ) * $nav_resolution;

	# Create image map for overview image
	for ($index = 1; $index <= $_[4]; $index += $nav_resolution)
	{
		# Enforce nav_offset
		if ($index < $offset + 1)
		{
			write_overview_line($_[0], $_[2], $index, 1);
		}
		else
		{
			write_overview_line($_[0], $_[2], $index, $index - $offset);
		}
	}

	# *************************************************************

	write_html($_[0], <<END_OF_HTML)
	  </map>

	  <center>
	  <a href="$_[2].gcov.$html_ext#top" target="source">Top</a><br><br>
	  <img src="$_[2].gcov.png" width=$overview_width height=$max_line alt="Overview" border=0 usemap="#overview">
	  </center>
	</body>
	</html>
END_OF_HTML
	;

	# *************************************************************
}


sub max($$)
{
	my ($a, $b) = @_;

	return $a if ($a > $b);
	return $b;
}


#
# write_header(filehandle, type, trunc_file_name, rel_file_name, lines_found,
# lines_hit, funcs_found, funcs_hit, sort_type)
#
# Write a complete standard page header. TYPE may be (0, 1, 2, 3, 4)
# corresponding to (directory view header, file view header, source view
# header, test case description header, function view header)
#

sub write_header(*$$$$$$$$$$)
{
	local *HTML_HANDLE = $_[0];
	my $type = $_[1];
	my $trunc_name = $_[2];
	my $rel_filename = $_[3];
	my $lines_found = $_[4];
	my $lines_hit = $_[5];
	my $fn_found = $_[6];
	my $fn_hit = $_[7];
	my $br_found = $_[8];
	my $br_hit = $_[9];
	my $sort_type = $_[10];
	my $base_dir;
	my $view;
	my $test;
	my $base_name;
	my $style;
	my $rate;
	my @row_left;
	my @row_right;
	my $num_rows;
	my $i;
	my $esc_trunc_name = escape_html($trunc_name);

	$base_name = basename($rel_filename);

	# Prepare text for "current view" field
	if ($type == $HDR_DIR)
	{
		# Main overview
		$base_dir = "";
		$view = $overview_title;
	}
	elsif ($type == $HDR_FILE)
	{
		# Directory overview
		$base_dir = get_relative_base_path($rel_filename);
		$view = "<a href=\"$base_dir"."index.$html_ext\">".
			"$overview_title</a> - $esc_trunc_name";
	}
	elsif ($type == $HDR_SOURCE || $type == $HDR_FUNC)
	{
		# File view
		my $dir_name = dirname($rel_filename);
		my $esc_base_name = escape_html($base_name);
		my $esc_dir_name = escape_html($dir_name);

		$base_dir = get_relative_base_path($dir_name);
		if ($frames)
		{
			# Need to break frameset when clicking any of these
			# links
			$view = "<a href=\"$base_dir"."index.$html_ext\" ".
				"target=\"_parent\">$overview_title</a> - ".
				"<a href=\"index.$html_ext\" target=\"_parent\">".
				"$esc_dir_name</a> - $esc_base_name";
		}
		else
		{
			$view = "<a href=\"$base_dir"."index.$html_ext\">".
				"$overview_title</a> - ".
				"<a href=\"index.$html_ext\">".
				"$esc_dir_name</a> - $esc_base_name";
		}

		# Add function suffix
		if ($func_coverage) {
			$view .= "<span style=\"font-size: 80%;\">";
			if ($type == $HDR_SOURCE) {
				if ($sort) {
					$view .= " (source / <a href=\"$base_name.func-sort-c.$html_ext\">functions</a>)";
				} else {
					$view .= " (source / <a href=\"$base_name.func.$html_ext\">functions</a>)";
				}
			} elsif ($type == $HDR_FUNC) {
				$view .= " (<a href=\"$base_name.gcov.$html_ext\">source</a> / functions)";
			}
			$view .= "</span>";
		}
	}
	elsif ($type == $HDR_TESTDESC)
	{
		# Test description header
		$base_dir = "";
		$view = "<a href=\"$base_dir"."index.$html_ext\">".
			"$overview_title</a> - test case descriptions";
	}

	# Prepare text for "test" field
	$test = escape_html($test_title);

	# Append link to test description page if available
	if (%test_description && ($type != $HDR_TESTDESC))
	{
		if ($frames && ($type == $HDR_SOURCE || $type == $HDR_FUNC))
		{
			# Need to break frameset when clicking this link
			$test .= " ( <span style=\"font-size:80%;\">".
				 "<a href=\"$base_dir".
				 "descriptions.$html_ext\" target=\"_parent\">".
				 "view descriptions</a></span> )";
		}
		else
		{
			$test .= " ( <span style=\"font-size:80%;\">".
				 "<a href=\"$base_dir".
				 "descriptions.$html_ext\">".
				 "view descriptions</a></span> )";
		}
	}

	# Write header
	write_header_prolog(*HTML_HANDLE, $base_dir);

	# Left row
	push(@row_left, [[ "10%", "headerItem", "Current view:" ],
			 [ "35%", "headerValue", $view ]]);
	push(@row_left, [[undef, "headerItem", "Test:"],
			 [undef, "headerValue", $test]]);
	push(@row_left, [[undef, "headerItem", "Date:"],
			 [undef, "headerValue", $date]]);

	# Right row
	if ($legend && ($type == $HDR_SOURCE || $type == $HDR_FUNC)) {
		my $text = <<END_OF_HTML;
            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
END_OF_HTML
		if ($br_coverage) {
			$text .= <<END_OF_HTML;
            | Branches:
            <span class="coverLegendCov">+</span> taken
            <span class="coverLegendNoCov">-</span> not taken
            <span class="coverLegendNoCov">#</span> not executed
END_OF_HTML
		}
		push(@row_left, [[undef, "headerItem", "Legend:"],
				 [undef, "headerValueLeg", $text]]);
	} elsif ($legend && ($type != $HDR_TESTDESC)) {
		my $text = <<END_OF_HTML;
	    Rating:
            <span class="coverLegendCovLo" title="Coverage rates below $med_limit % are classified as low">low: &lt; $med_limit %</span>
            <span class="coverLegendCovMed" title="Coverage rates between $med_limit % and $hi_limit % are classified as medium">medium: &gt;= $med_limit %</span>
            <span class="coverLegendCovHi" title="Coverage rates of $hi_limit % and more are classified as high">high: &gt;= $hi_limit %</span>
END_OF_HTML
		push(@row_left, [[undef, "headerItem", "Legend:"],
				 [undef, "headerValueLeg", $text]]);
	}
	if ($type == $HDR_TESTDESC) {
		push(@row_right, [[ "55%" ]]);
	} else {
		push(@row_right, [["15%", undef, undef ],
				  ["10%", "headerCovTableHead", "Hit" ],
				  ["10%", "headerCovTableHead", "Total" ],
				  ["15%", "headerCovTableHead", "Coverage"]]);
	}
	# Line coverage
	$style = $rate_name[classify_rate($lines_found, $lines_hit,
					  $med_limit, $hi_limit)];
	$rate = rate($lines_hit, $lines_found, " %");
	push(@row_right, [[undef, "headerItem", "Lines:"],
			  [undef, "headerCovTableEntry", $lines_hit],
			  [undef, "headerCovTableEntry", $lines_found],
			  [undef, "headerCovTableEntry$style", $rate]])
			if ($type != $HDR_TESTDESC);
	# Function coverage
	if ($func_coverage) {
		$style = $rate_name[classify_rate($fn_found, $fn_hit,
						  $fn_med_limit, $fn_hi_limit)];
		$rate = rate($fn_hit, $fn_found, " %");
		push(@row_right, [[undef, "headerItem", "Functions:"],
				  [undef, "headerCovTableEntry", $fn_hit],
				  [undef, "headerCovTableEntry", $fn_found],
				  [undef, "headerCovTableEntry$style", $rate]])
			if ($type != $HDR_TESTDESC);
	}
	# Branch coverage
	if ($br_coverage) {
		$style = $rate_name[classify_rate($br_found, $br_hit,
						  $br_med_limit, $br_hi_limit)];
		$rate = rate($br_hit, $br_found, " %");
		push(@row_right, [[undef, "headerItem", "Branches:"],
				  [undef, "headerCovTableEntry", $br_hit],
				  [undef, "headerCovTableEntry", $br_found],
				  [undef, "headerCovTableEntry$style", $rate]])
			if ($type != $HDR_TESTDESC);
	}

	# Print rows
	$num_rows = max(scalar(@row_left), scalar(@row_right));
	for ($i = 0; $i < $num_rows; $i++) {
		my $left = $row_left[$i];
		my $right = $row_right[$i];

		if (!defined($left)) {
			$left = [[undef, undef, undef], [undef, undef, undef]];
		}
		if (!defined($right)) {
			$right = [];
		}
		write_header_line(*HTML_HANDLE, @{$left},
				  [ $i == 0 ? "5%" : undef, undef, undef],
				  @{$right});
	}

	# Fourth line
	write_header_epilog(*HTML_HANDLE, $base_dir);
}


#
# get_sorted_keys(hash_ref, sort_type)
#

sub get_sorted_keys($$)
{
	my ($hash, $type) = @_;

	if ($type == $SORT_FILE) {
		# Sort by name
		return sort(keys(%{$hash}));
	} elsif ($type == $SORT_LINE) {
		# Sort by line coverage
		return sort({$hash->{$a}[7] <=> $hash->{$b}[7]} keys(%{$hash}));
	} elsif ($type == $SORT_FUNC) {
		# Sort by function coverage;
		return sort({$hash->{$a}[8] <=> $hash->{$b}[8]}	keys(%{$hash}));
	} elsif ($type == $SORT_BRANCH) {
		# Sort by br coverage;
		return sort({$hash->{$a}[9] <=> $hash->{$b}[9]}	keys(%{$hash}));
	}
}

sub get_sort_code($$$)
{
	my ($link, $alt, $base) = @_;
	my $png;
	my $link_start;
	my $link_end;

	if (!defined($link)) {
		$png = "glass.png";
		$link_start = "";
		$link_end = "";
	} else {
		$png = "updown.png";
		$link_start = '<a href="'.$link.'">';
		$link_end = "</a>";
	}

	return ' <span class="tableHeadSort">'.$link_start.
	       '<img src="'.$base.$png.'" width=10 height=14 '.
	       'alt="'.$alt.'" title="'.$alt.'" border=0>'.$link_end.'</span>';
}

sub get_file_code($$$$)
{
	my ($type, $text, $sort_button, $base) = @_;
	my $result = $text;
	my $link;

	if ($sort_button) {
		if ($type == $HEAD_NO_DETAIL) {
			$link = "index.$html_ext";
		} else {
			$link = "index-detail.$html_ext";
		}
	}
	$result .= get_sort_code($link, "Sort by name", $base);

	return $result;
}

sub get_line_code($$$$$)
{
	my ($type, $sort_type, $text, $sort_button, $base) = @_;
	my $result = $text;
	my $sort_link;

	if ($type == $HEAD_NO_DETAIL) {
		# Just text
		if ($sort_button) {
			$sort_link = "index-sort-l.$html_ext";
		}
	} elsif ($type == $HEAD_DETAIL_HIDDEN) {
		# Text + link to detail view
		$result .= ' ( <a class="detail" href="index-detail'.
			   $fileview_sortname[$sort_type].'.'.$html_ext.
			   '">show details</a> )';
		if ($sort_button) {
			$sort_link = "index-sort-l.$html_ext";
		}
	} else {
		# Text + link to standard view
		$result .= ' ( <a class="detail" href="index'.
			   $fileview_sortname[$sort_type].'.'.$html_ext.
			   '">hide details</a> )';
		if ($sort_button) {
			$sort_link = "index-detail-sort-l.$html_ext";
		}
	}
	# Add sort button
	$result .= get_sort_code($sort_link, "Sort by line coverage", $base);

	return $result;
}

sub get_func_code($$$$)
{
	my ($type, $text, $sort_button, $base) = @_;
	my $result = $text;
	my $link;

	if ($sort_button) {
		if ($type == $HEAD_NO_DETAIL) {
			$link = "index-sort-f.$html_ext";
		} else {
			$link = "index-detail-sort-f.$html_ext";
		}
	}
	$result .= get_sort_code($link, "Sort by function coverage", $base);
	return $result;
}

sub get_br_code($$$$)
{
	my ($type, $text, $sort_button, $base) = @_;
	my $result = $text;
	my $link;

	if ($sort_button) {
		if ($type == $HEAD_NO_DETAIL) {
			$link = "index-sort-b.$html_ext";
		} else {
			$link = "index-detail-sort-b.$html_ext";
		}
	}
	$result .= get_sort_code($link, "Sort by branch coverage", $base);
	return $result;
}

#
# write_file_table(filehandle, base_dir, overview, testhash, testfnchash,
#                  testbrhash, fileview, sort_type)
#
# Write a complete file table. OVERVIEW is a reference to a hash containing
# the following mapping:
#
#   filename -> "lines_found,lines_hit,funcs_found,funcs_hit,page_link,
#		 func_link"
#
# TESTHASH is a reference to the following hash:
#
#   filename -> \%testdata
#   %testdata: name of test affecting this file -> \%testcount
#   %testcount: line number -> execution count for a single test
#
# Heading of first column is "Filename" if FILEVIEW is true, "Directory name"
# otherwise.
#

sub write_file_table(*$$$$$$$)
{
	local *HTML_HANDLE = $_[0];
	my $base_dir = $_[1];
	my $overview = $_[2];
	my $testhash = $_[3];
	my $testfnchash = $_[4];
	my $testbrhash = $_[5];
	my $fileview = $_[6];
	my $sort_type = $_[7];
	my $filename;
	my $bar_graph;
	my $hit;
	my $found;
	my $fn_found;
	my $fn_hit;
	my $br_found;
	my $br_hit;
	my $page_link;
	my $testname;
	my $testdata;
	my $testfncdata;
	my $testbrdata;
	my %affecting_tests;
	my $line_code = "";
	my $func_code;
	my $br_code;
	my $file_code;
	my @head_columns;

	# Determine HTML code for column headings
	if (($base_dir ne "") && $show_details)
	{
		my $detailed = keys(%{$testhash});

		$file_code = get_file_code($detailed ? $HEAD_DETAIL_HIDDEN :
					$HEAD_NO_DETAIL,
					$fileview ? "Filename" : "Directory",
					$sort && $sort_type != $SORT_FILE,
					$base_dir);
		$line_code = get_line_code($detailed ? $HEAD_DETAIL_SHOWN :
					$HEAD_DETAIL_HIDDEN,
					$sort_type,
					"Line Coverage",
					$sort && $sort_type != $SORT_LINE,
					$base_dir);
		$func_code = get_func_code($detailed ? $HEAD_DETAIL_HIDDEN :
					$HEAD_NO_DETAIL,
					"Functions",
					$sort && $sort_type != $SORT_FUNC,
					$base_dir);
		$br_code = get_br_code($detailed ? $HEAD_DETAIL_HIDDEN :
					$HEAD_NO_DETAIL,
					"Branches",
					$sort && $sort_type != $SORT_BRANCH,
					$base_dir);
	} else {
		$file_code = get_file_code($HEAD_NO_DETAIL,
					$fileview ? "Filename" : "Directory",
					$sort && $sort_type != $SORT_FILE,
					$base_dir);
		$line_code = get_line_code($HEAD_NO_DETAIL, $sort_type, "Line Coverage",
					$sort && $sort_type != $SORT_LINE,
					$base_dir);
		$func_code = get_func_code($HEAD_NO_DETAIL, "Functions",
					$sort && $sort_type != $SORT_FUNC,
					$base_dir);
		$br_code = get_br_code($HEAD_NO_DETAIL, "Branches",
					$sort && $sort_type != $SORT_BRANCH,
					$base_dir);
	}
	push(@head_columns, [ $line_code, 3 ]);
	push(@head_columns, [ $func_code, 2]) if ($func_coverage);
	push(@head_columns, [ $br_code, 2]) if ($br_coverage);

	write_file_table_prolog(*HTML_HANDLE, $file_code, @head_columns);

	foreach $filename (get_sorted_keys($overview, $sort_type))
	{
		my @columns;
		($found, $hit, $fn_found, $fn_hit, $br_found, $br_hit,
		 $page_link) = @{$overview->{$filename}};

		# Line coverage
		push(@columns, [$found, $hit, $med_limit, $hi_limit, 1]);
		# Function coverage
		if ($func_coverage) {
			push(@columns, [$fn_found, $fn_hit, $fn_med_limit,
					$fn_hi_limit, 0]);
		}
		# Branch coverage
		if ($br_coverage) {
			push(@columns, [$br_found, $br_hit, $br_med_limit,
					$br_hi_limit, 0]);
		}
		write_file_table_entry(*HTML_HANDLE, $base_dir, $filename,
				       $page_link, @columns);

		$testdata = $testhash->{$filename};
		$testfncdata = $testfnchash->{$filename};
		$testbrdata = $testbrhash->{$filename};

		# Check whether we should write test specific coverage
		# as well
		if (!($show_details && $testdata)) { next; }

		# Filter out those tests that actually affect this file
		%affecting_tests = %{ get_affecting_tests($testdata,
					$testfncdata, $testbrdata) };

		# Does any of the tests affect this file at all?
		if (!%affecting_tests) { next; }

		foreach $testname (keys(%affecting_tests))
		{
			my @results;
			($found, $hit, $fn_found, $fn_hit, $br_found, $br_hit) =
				split(",", $affecting_tests{$testname});

			# Insert link to description of available
			if ($test_description{$testname})
			{
				$testname = "<a href=\"$base_dir".
					    "descriptions.$html_ext#$testname\">".
					    "$testname</a>";
			}

			push(@results, [$found, $hit]);
			push(@results, [$fn_found, $fn_hit]) if ($func_coverage);
			push(@results, [$br_found, $br_hit]) if ($br_coverage);
			write_file_table_detail_entry(*HTML_HANDLE, $testname,
				@results);
		}
	}

	write_file_table_epilog(*HTML_HANDLE);
}


#
# get_found_and_hit(hash)
#
# Return the count for entries (found) and entries with an execution count
# greater than zero (hit) in a hash (linenumber -> execution count) as
# a list (found, hit)
#

sub get_found_and_hit($)
{
	my %hash = %{$_[0]};
	my $found = 0;
	my $hit = 0;

	# Calculate sum
	$found = 0;
	$hit = 0;
			
	foreach (keys(%hash))
	{
		$found++;
		if ($hash{$_}>0) { $hit++; }
	}

	return ($found, $hit);
}


#
# get_func_found_and_hit(sumfnccount)
#
# Return (f_found, f_hit) for sumfnccount
#

sub get_func_found_and_hit($)
{
	my ($sumfnccount) = @_;
	my $function;
	my $fn_found;
	my $fn_hit;

	$fn_found = scalar(keys(%{$sumfnccount}));
	$fn_hit = 0;
	foreach $function (keys(%{$sumfnccount})) {
		if ($sumfnccount->{$function} > 0) {
			$fn_hit++;
		}
	}
	return ($fn_found, $fn_hit);
}


#
# br_taken_to_num(taken)
#
# Convert a branch taken value .info format to number format.
#

sub br_taken_to_num($)
{
	my ($taken) = @_;

	return 0 if ($taken eq '-');
	return $taken + 1;
}


#
# br_num_to_taken(taken)
#
# Convert a branch taken value in number format to .info format.
#

sub br_num_to_taken($)
{
	my ($taken) = @_;

	return '-' if ($taken == 0);
	return $taken - 1;
}


#
# br_taken_add(taken1, taken2)
#
# Return the result of taken1 + taken2 for 'branch taken' values.
#

sub br_taken_add($$)
{
	my ($t1, $t2) = @_;

	return $t1 if (!defined($t2));
	return $t2 if (!defined($t1));
	return $t1 if ($t2 eq '-');
	return $t2 if ($t1 eq '-');
	return $t1 + $t2;
}


#
# br_taken_sub(taken1, taken2)
#
# Return the result of taken1 - taken2 for 'branch taken' values. Return 0
# if the result would become negative.
#

sub br_taken_sub($$)
{
	my ($t1, $t2) = @_;

	return $t1 if (!defined($t2));
	return undef if (!defined($t1));
	return $t1 if ($t1 eq '-');
	return $t1 if ($t2 eq '-');
	return 0 if $t2 > $t1;
	return $t1 - $t2;
}


#
# br_ivec_len(vector)
#
# Return the number of entries in the branch coverage vector.
#

sub br_ivec_len($)
{
	my ($vec) = @_;

	return 0 if (!defined($vec));
	return (length($vec) * 8 / $BR_VEC_WIDTH) / $BR_VEC_ENTRIES;
}


#
# br_ivec_get(vector, number)
#
# Return an entry from the branch coverage vector.
#

sub br_ivec_get($$)
{
	my ($vec, $num) = @_;
	my $block;
	my $branch;
	my $taken;
	my $offset = $num * $BR_VEC_ENTRIES;

	# Retrieve data from vector
	$block	= vec($vec, $offset + $BR_BLOCK, $BR_VEC_WIDTH);
	$block = -1 if ($block == $BR_VEC_MAX);
	$branch = vec($vec, $offset + $BR_BRANCH, $BR_VEC_WIDTH);
	$taken	= vec($vec, $offset + $BR_TAKEN, $BR_VEC_WIDTH);

	# Decode taken value from an integer
	$taken = br_num_to_taken($taken);

	return ($block, $branch, $taken);
}


#
# br_ivec_push(vector, block, branch, taken)
#
# Add an entry to the branch coverage vector. If an entry with the same
# branch ID already exists, add the corresponding taken values.
#

sub br_ivec_push($$$$)
{
	my ($vec, $block, $branch, $taken) = @_;
	my $offset;
	my $num = br_ivec_len($vec);
	my $i;

	$vec = "" if (!defined($vec));
	$block = $BR_VEC_MAX if $block < 0;

	# Check if branch already exists in vector
	for ($i = 0; $i < $num; $i++) {
		my ($v_block, $v_branch, $v_taken) = br_ivec_get($vec, $i);
		$v_block = $BR_VEC_MAX if $v_block < 0;

		next if ($v_block != $block || $v_branch != $branch);

		# Add taken counts
		$taken = br_taken_add($taken, $v_taken);
		last;
	}

	$offset = $i * $BR_VEC_ENTRIES;
	$taken = br_taken_to_num($taken);

	# Add to vector
	vec($vec, $offset + $BR_BLOCK, $BR_VEC_WIDTH) = $block;
	vec($vec, $offset + $BR_BRANCH, $BR_VEC_WIDTH) = $branch;
	vec($vec, $offset + $BR_TAKEN, $BR_VEC_WIDTH) = $taken;

	return $vec;
}


#
# get_br_found_and_hit(sumbrcount)
#
# Return (br_found, br_hit) for sumbrcount
#

sub get_br_found_and_hit($)
{
	my ($sumbrcount) = @_;
	my $line;
	my $br_found = 0;
	my $br_hit = 0;

	foreach $line (keys(%{$sumbrcount})) {
		my $brdata = $sumbrcount->{$line};
		my $i;
		my $num = br_ivec_len($brdata);

		for ($i = 0; $i < $num; $i++) {
			my $taken;

			(undef, undef, $taken) = br_ivec_get($brdata, $i);

			$br_found++;
			$br_hit++ if ($taken ne "-" && $taken > 0);
		}
	}

	return ($br_found, $br_hit);
}


#
# get_affecting_tests(testdata, testfncdata, testbrdata)
#
# HASHREF contains a mapping filename -> (linenumber -> exec count). Return
# a hash containing mapping filename -> "lines found, lines hit" for each
# filename which has a nonzero hit count.
#

sub get_affecting_tests($$$)
{
	my ($testdata, $testfncdata, $testbrdata) = @_;
	my $testname;
	my $testcount;
	my $testfnccount;
	my $testbrcount;
	my %result;
	my $found;
	my $hit;
	my $fn_found;
	my $fn_hit;
	my $br_found;
	my $br_hit;

	foreach $testname (keys(%{$testdata}))
	{
		# Get (line number -> count) hash for this test case
		$testcount = $testdata->{$testname};
		$testfnccount = $testfncdata->{$testname};
		$testbrcount = $testbrdata->{$testname};

		# Calculate sum
		($found, $hit) = get_found_and_hit($testcount);
		($fn_found, $fn_hit) = get_func_found_and_hit($testfnccount);
		($br_found, $br_hit) = get_br_found_and_hit($testbrcount);

		if ($hit>0)
		{
			$result{$testname} = "$found,$hit,$fn_found,$fn_hit,".
					     "$br_found,$br_hit";
		}
	}

	return(\%result);
}


sub get_hash_reverse($)
{
	my ($hash) = @_;
	my %result;

	foreach (keys(%{$hash})) {
		$result{$hash->{$_}} = $_;
	}

	return \%result;
}

#
# write_source(filehandle, source_filename, count_data, checksum_data,
#              converted_data, func_data, sumbrcount)
#
# Write an HTML view of a source code file. Returns a list containing
# data as needed by gen_png().
#
# Die on error.
#

sub write_source($$$$$$$)
{
	local *HTML_HANDLE = $_[0];
	local *SOURCE_HANDLE;
	my $source_filename = $_[1];
	my %count_data;
	my $line_number;
	my @result;
	my $checkdata = $_[3];
	my $converted = $_[4];
	my $funcdata  = $_[5];
	my $sumbrcount = $_[6];
	my $datafunc = get_hash_reverse($funcdata);
	my $add_anchor;
	my @file;

	if ($_[2])
	{
		%count_data = %{$_[2]};
	}

	if (!open(SOURCE_HANDLE, "<", $source_filename)) {
		my @lines;
		my $last_line = 0;

		if (!$ignore[$ERROR_SOURCE]) {
			die("ERROR: cannot read $source_filename\n");
		}

		# Continue without source file
		warn("WARNING: cannot read $source_filename!\n");

		@lines = sort( { $a <=> $b }  keys(%count_data));
		if (@lines) {
			$last_line = $lines[scalar(@lines) - 1];
		}
		return ( ":" ) if ($last_line < 1);

		# Simulate gcov behavior
		for ($line_number = 1; $line_number <= $last_line;
		     $line_number++) {
			push(@file, "/* EOF */");
		}
	} else {
		@file = <SOURCE_HANDLE>;
	}
	
	write_source_prolog(*HTML_HANDLE);
	$line_number = 0;
	foreach (@file) {
		$line_number++;
		chomp($_);

		# Also remove CR from line-end
		s/\015$//;

		# Source code matches coverage data?
		if (defined($checkdata->{$line_number}) &&
		    ($checkdata->{$line_number} ne md5_base64($_)))
		{
			die("ERROR: checksum mismatch  at $source_filename:".
			    "$line_number\n");
		}

		$add_anchor = 0;
		if ($frames) {
			if (($line_number - 1) % $nav_resolution == 0) {
				$add_anchor = 1;
			}
		}
		if ($func_coverage) {
			if ($line_number == 1) {
				$add_anchor = 1;
			} elsif (defined($datafunc->{$line_number +
						     $func_offset})) {
				$add_anchor = 1;
			}
		}
		push (@result,
		      write_source_line(HTML_HANDLE, $line_number,
					$_, $count_data{$line_number},
					$converted->{$line_number},
					$sumbrcount->{$line_number}, $add_anchor));
	}

	close(SOURCE_HANDLE);
	write_source_epilog(*HTML_HANDLE);
	return(@result);
}


sub funcview_get_func_code($$$)
{
	my ($name, $base, $type) = @_;
	my $result;
	my $link;

	if ($sort && $type == 1) {
		$link = "$name.func.$html_ext";
	}
	$result = "Function Name";
	$result .= get_sort_code($link, "Sort by function name", $base);

	return $result;
}

sub funcview_get_count_code($$$)
{
	my ($name, $base, $type) = @_;
	my $result;
	my $link;

	if ($sort && $type == 0) {
		$link = "$name.func-sort-c.$html_ext";
	}
	$result = "Hit count";
	$result .= get_sort_code($link, "Sort by hit count", $base);

	return $result;
}

#
# funcview_get_sorted(funcdata, sumfncdata, sort_type)
#
# Depending on the value of sort_type, return a list of functions sorted
# by name (type 0) or by the associated call count (type 1).
#

sub funcview_get_sorted($$$)
{
	my ($funcdata, $sumfncdata, $type) = @_;

	if ($type == 0) {
		return sort(keys(%{$funcdata}));
	}
	return sort({
		$sumfncdata->{$b} == $sumfncdata->{$a} ?
			$a cmp $b : $sumfncdata->{$a} <=> $sumfncdata->{$b}
		} keys(%{$sumfncdata}));
}

sub demangle_list($)
{
	my ($list) = @_;
	my $tmpfile;
	my $handle;
	my %demangle;
	my %versions;

	# Write function names to file
	($handle, $tmpfile) = tempfile();
	die("ERROR: could not create temporary file") if (!defined($tmpfile));
	print($handle join("\n", @$list));
	close($handle);

	# Build translation hash from c++filt output
	open($handle, "-|", "c++filt < $tmpfile") or
		die("ERROR: could not run c++filt: $!\n");
	foreach my $func (@$list) {
		my $translated = <$handle>;
		my $version;

		last if (!defined($translated));
		chomp($translated);

		$version = ++$versions{$translated};
		$translated .= ".$version" if ($version > 1);
		$demangle{$func} = $translated;
	}
	close($handle);

	if (scalar(keys(%demangle)) != scalar(@$list)) {
		die("ERROR: c++filt output not as expected (".
		    scalar(keys(%demangle))." vs ".scalar(@$list).") lines\n");
	}

	unlink($tmpfile) or
		warn("WARNING: could not remove temporary file $tmpfile: $!\n");

	return \%demangle;
}

#
# write_function_table(filehandle, source_file, sumcount, funcdata,
#		       sumfnccount, testfncdata, sumbrcount, testbrdata,
#		       base_name, base_dir, sort_type)
#
# Write an HTML table listing all functions in a source file, including
# also function call counts and line coverages inside of each function.
#
# Die on error.
#

sub write_function_table(*$$$$$$$$$$)
{
	local *HTML_HANDLE = $_[0];
	my $source = $_[1];
	my $sumcount = $_[2];
	my $funcdata = $_[3];
	my $sumfncdata = $_[4];
	my $testfncdata = $_[5];
	my $sumbrcount = $_[6];
	my $testbrdata = $_[7];
	my $name = $_[8];
	my $base = $_[9];
	my $type = $_[10];
	my $func;
	my $func_code;
	my $count_code;
	my $demangle;

	# Get HTML code for headings
	$func_code = funcview_get_func_code($name, $base, $type);
	$count_code = funcview_get_count_code($name, $base, $type);
	write_html(*HTML_HANDLE, <<END_OF_HTML)
	  <center>
	  <table width="60%" cellpadding=1 cellspacing=1 border=0>
	    <tr><td><br></td></tr>
	    <tr>
	      <td width="80%" class="tableHead">$func_code</td>
	      <td width="20%" class="tableHead">$count_code</td>
	    </tr>
END_OF_HTML
	;

	# Get demangle translation hash
	if ($demangle_cpp) {
		$demangle = demangle_list([ sort(keys(%{$funcdata})) ]);
	}

	# Get a sorted table
	foreach $func (funcview_get_sorted($funcdata, $sumfncdata, $type)) {
		if (!defined($funcdata->{$func}))
		{
			next;
		}

		my $startline = $funcdata->{$func} - $func_offset;
		my $name = $func;
		my $count = $sumfncdata->{$name};
		my $countstyle;

		# Replace function name with demangled version if available
		$name = $demangle->{$name} if (exists($demangle->{$name}));

		# Escape special characters
		$name = escape_html($name);
		if ($startline < 1) {
			$startline = 1;
		}
		if ($count == 0) {
			$countstyle = "coverFnLo";
		} else {
			$countstyle = "coverFnHi";
		}

		write_html(*HTML_HANDLE, <<END_OF_HTML)
	    <tr>
              <td class="coverFn"><a href="$source#$startline">$name</a></td>
              <td class="$countstyle">$count</td>
            </tr>
END_OF_HTML
                ;
	}
	write_html(*HTML_HANDLE, <<END_OF_HTML)
	  </table>
	  <br>
	  </center>
END_OF_HTML
	;
}


#
# info(printf_parameter)
#
# Use printf to write PRINTF_PARAMETER to stdout only when the $quiet flag
# is not set.
#

sub info(@)
{
	if (!$quiet)
	{
		# Print info string
		printf(@_);
	}
}


#
# subtract_counts(data_ref, base_ref)
#

sub subtract_counts($$)
{
	my %data = %{$_[0]};
	my %base = %{$_[1]};
	my $line;
	my $data_count;
	my $base_count;
	my $hit = 0;
	my $found = 0;

	foreach $line (keys(%data))
	{
		$found++;
		$data_count = $data{$line};
		$base_count = $base{$line};

		if (defined($base_count))
		{
			$data_count -= $base_count;

			# Make sure we don't get negative numbers
			if ($data_count<0) { $data_count = 0; }
		}

		$data{$line} = $data_count;
		if ($data_count > 0) { $hit++; }
	}

	return (\%data, $found, $hit);
}


#
# subtract_fnccounts(data, base)
#
# Subtract function call counts found in base from those in data.
# Return (data, f_found, f_hit).
#

sub subtract_fnccounts($$)
{
	my %data;
	my %base;
	my $func;
	my $data_count;
	my $base_count;
	my $fn_hit = 0;
	my $fn_found = 0;

	%data = %{$_[0]} if (defined($_[0]));
	%base = %{$_[1]} if (defined($_[1]));
	foreach $func (keys(%data)) {
		$fn_found++;
		$data_count = $data{$func};
		$base_count = $base{$func};

		if (defined($base_count)) {
			$data_count -= $base_count;

			# Make sure we don't get negative numbers
			if ($data_count < 0) {
				$data_count = 0;
			}
		}

		$data{$func} = $data_count;
		if ($data_count > 0) {
			$fn_hit++;
		}
	}

	return (\%data, $fn_found, $fn_hit);
}


#
# apply_baseline(data_ref, baseline_ref)
#
# Subtract the execution counts found in the baseline hash referenced by
# BASELINE_REF from actual data in DATA_REF.
#

sub apply_baseline($$)
{
	my %data_hash = %{$_[0]};
	my %base_hash = %{$_[1]};
	my $filename;
	my $testname;
	my $data;
	my $data_testdata;
	my $data_funcdata;
	my $data_checkdata;
	my $data_testfncdata;
	my $data_testbrdata;
	my $data_count;
	my $data_testfnccount;
	my $data_testbrcount;
	my $base;
	my $base_checkdata;
	my $base_sumfnccount;
	my $base_sumbrcount;
	my $base_count;
	my $sumcount;
	my $sumfnccount;
	my $sumbrcount;
	my $found;
	my $hit;
	my $fn_found;
	my $fn_hit;
	my $br_found;
	my $br_hit;

	foreach $filename (keys(%data_hash))
	{
		# Get data set for data and baseline
		$data = $data_hash{$filename};
		$base = $base_hash{$filename};

		# Skip data entries for which no base entry exists
		if (!defined($base))
		{
			next;
		}

		# Get set entries for data and baseline
		($data_testdata, undef, $data_funcdata, $data_checkdata,
		 $data_testfncdata, undef, $data_testbrdata) =
			get_info_entry($data);
		(undef, $base_count, undef, $base_checkdata, undef,
		 $base_sumfnccount, undef, $base_sumbrcount) =
			get_info_entry($base);

		# Check for compatible checksums
		merge_checksums($data_checkdata, $base_checkdata, $filename);

		# sumcount has to be calculated anew
		$sumcount = {};
		$sumfnccount = {};
		$sumbrcount = {};

		# For each test case, subtract test specific counts
		foreach $testname (keys(%{$data_testdata}))
		{
			# Get counts of both data and baseline
			$data_count = $data_testdata->{$testname};
			$data_testfnccount = $data_testfncdata->{$testname};
			$data_testbrcount = $data_testbrdata->{$testname};

			($data_count, undef, $hit) =
				subtract_counts($data_count, $base_count);
			($data_testfnccount) =
				subtract_fnccounts($data_testfnccount,
						   $base_sumfnccount);
			($data_testbrcount) =
				combine_brcount($data_testbrcount,
						 $base_sumbrcount, $BR_SUB);


			# Check whether this test case did hit any line at all
			if ($hit > 0)
			{
				# Write back resulting hash
				$data_testdata->{$testname} = $data_count;
				$data_testfncdata->{$testname} =
					$data_testfnccount;
				$data_testbrdata->{$testname} =
					$data_testbrcount;
			}
			else
			{
				# Delete test case which did not impact this
				# file
				delete($data_testdata->{$testname});
				delete($data_testfncdata->{$testname});
				delete($data_testbrdata->{$testname});
			}

			# Add counts to sum of counts
			($sumcount, $found, $hit) =
				add_counts($sumcount, $data_count);
			($sumfnccount, $fn_found, $fn_hit) =
				add_fnccount($sumfnccount, $data_testfnccount);
			($sumbrcount, $br_found, $br_hit) =
				combine_brcount($sumbrcount, $data_testbrcount,
						$BR_ADD);
		}

		# Write back resulting entry
		set_info_entry($data, $data_testdata, $sumcount, $data_funcdata,
			       $data_checkdata, $data_testfncdata, $sumfnccount,
			       $data_testbrdata, $sumbrcount, $found, $hit,
			       $fn_found, $fn_hit, $br_found, $br_hit);

		$data_hash{$filename} = $data;
	}

	return (\%data_hash);
}


#
# remove_unused_descriptions()
#
# Removes all test descriptions from the global hash %test_description which
# are not present in %info_data.
#

sub remove_unused_descriptions()
{
	my $filename;		# The current filename
	my %test_list;		# Hash containing found test names
	my $test_data;		# Reference to hash test_name -> count_data
	my $before;		# Initial number of descriptions
	my $after;		# Remaining number of descriptions
	
	$before = scalar(keys(%test_description));

	foreach $filename (keys(%info_data))
	{
		($test_data) = get_info_entry($info_data{$filename});
		foreach (keys(%{$test_data}))
		{
			$test_list{$_} = "";
		}
	}

	# Remove descriptions for tests which are not in our list
	foreach (keys(%test_description))
	{
		if (!defined($test_list{$_}))
		{
			delete($test_description{$_});
		}
	}

	$after = scalar(keys(%test_description));
	if ($after < $before)
	{
		info("Removed ".($before - $after).
		     " unused descriptions, $after remaining.\n");
	}
}


#
# apply_prefix(filename, PREFIXES)
#
# If FILENAME begins with PREFIX from PREFIXES, remove PREFIX from FILENAME
# and return resulting string, otherwise return FILENAME.
#

sub apply_prefix($@)
{
	my $filename = shift;
	my @dir_prefix = @_;

	if (@dir_prefix)
	{
		foreach my $prefix (@dir_prefix)
		{
			if ($prefix ne "" && $filename =~ /^\Q$prefix\E\/(.*)$/)
			{
				return substr($filename, length($prefix) + 1);
			}
		}
	}

	return $filename;
}


#
# system_no_output(mode, parameters)
#
# Call an external program using PARAMETERS while suppressing depending on
# the value of MODE:
#
#   MODE & 1: suppress STDOUT
#   MODE & 2: suppress STDERR
#
# Return 0 on success, non-zero otherwise.
#

sub system_no_output($@)
{
	my $mode = shift;
	my $result;
	local *OLD_STDERR;
	local *OLD_STDOUT;

	# Save old stdout and stderr handles
	($mode & 1) && open(OLD_STDOUT, ">>&", "STDOUT");
	($mode & 2) && open(OLD_STDERR, ">>&", "STDERR");

	# Redirect to /dev/null
	($mode & 1) && open(STDOUT, ">", "/dev/null");
	($mode & 2) && open(STDERR, ">", "/dev/null");

	system(@_);
	$result = $?;

	# Close redirected handles
	($mode & 1) && close(STDOUT);
	($mode & 2) && close(STDERR);

	# Restore old handles
	($mode & 1) && open(STDOUT, ">>&", "OLD_STDOUT");
	($mode & 2) && open(STDERR, ">>&", "OLD_STDERR");

	return $result;
}


#
# read_config(filename)
#
# Read configuration file FILENAME and return a reference to a hash containing
# all valid key=value pairs found.
#

sub read_config($)
{
	my $filename = $_[0];
	my %result;
	my $key;
	my $value;
	local *HANDLE;

	if (!open(HANDLE, "<", $filename))
	{
		warn("WARNING: cannot read configuration file $filename\n");
		return undef;
	}
	while (<HANDLE>)
	{
		chomp;
		# Skip comments
		s/#.*//;
		# Remove leading blanks
		s/^\s+//;
		# Remove trailing blanks
		s/\s+$//;
		next unless length;
		($key, $value) = split(/\s*=\s*/, $_, 2);
		if (defined($key) && defined($value))
		{
			$result{$key} = $value;
		}
		else
		{
			warn("WARNING: malformed statement in line $. ".
			     "of configuration file $filename\n");
		}
	}
	close(HANDLE);
	return \%result;
}


#
# apply_config(REF)
#
# REF is a reference to a hash containing the following mapping:
#
#   key_string => var_ref
#
# where KEY_STRING is a keyword and VAR_REF is a reference to an associated
# variable. If the global configuration hashes CONFIG or OPT_RC contain a value
# for keyword KEY_STRING, VAR_REF will be assigned the value for that keyword. 
#

sub apply_config($)
{
	my $ref = $_[0];

	foreach (keys(%{$ref}))
	{
		if (defined($opt_rc{$_})) {
			${$ref->{$_}} = $opt_rc{$_};
		} elsif (defined($config->{$_})) {
			${$ref->{$_}} = $config->{$_};
		}
	}
}


#
# get_html_prolog(FILENAME)
#
# If FILENAME is defined, return contents of file. Otherwise return default
# HTML prolog. Die on error.
#

sub get_html_prolog($)
{
	my $filename = $_[0];
	my $result = "";

	if (defined($filename))
	{
		local *HANDLE;

		open(HANDLE, "<", $filename)
			or die("ERROR: cannot open html prolog $filename!\n");
		while (<HANDLE>)
		{
			$result .= $_;
		}
		close(HANDLE);
	}
	else
	{
		$result = <<END_OF_HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=$charset">
  <title>\@pagetitle\@</title>
  <link rel="stylesheet" type="text/css" href="\@basedir\@gcov.css">
</head>

<body>

END_OF_HTML
		;
	}

	return $result;
}


#
# get_html_epilog(FILENAME)
#
# If FILENAME is defined, return contents of file. Otherwise return default
# HTML epilog. Die on error.
#
sub get_html_epilog($)
{
	my $filename = $_[0];
	my $result = "";

	if (defined($filename))
	{
		local *HANDLE;

		open(HANDLE, "<", $filename)
			or die("ERROR: cannot open html epilog $filename!\n");
		while (<HANDLE>)
		{
			$result .= $_;
		}
		close(HANDLE);
	}
	else
	{
		$result = <<END_OF_HTML

</body>
</html>
END_OF_HTML
		;
	}

	return $result;

}

sub warn_handler($)
{
	my ($msg) = @_;

	warn("$tool_name: $msg");
}

sub die_handler($)
{
	my ($msg) = @_;

	die("$tool_name: $msg");
}

#
# parse_ignore_errors(@ignore_errors)
#
# Parse user input about which errors to ignore.
#

sub parse_ignore_errors(@)
{
	my (@ignore_errors) = @_;
	my @items;
	my $item;

	return if (!@ignore_errors);

	foreach $item (@ignore_errors) {
		$item =~ s/\s//g;
		if ($item =~ /,/) {
			# Split and add comma-separated parameters
			push(@items, split(/,/, $item));
		} else {
			# Add single parameter
			push(@items, $item);
		}
	}
	foreach $item (@items) {
		my $item_id = $ERROR_ID{lc($item)};

		if (!defined($item_id)) {
			die("ERROR: unknown argument for --ignore-errors: ".
			    "$item\n");
		}
		$ignore[$item_id] = 1;
	}
}

#
# parse_dir_prefix(@dir_prefix)
#
# Parse user input about the prefix list
#

sub parse_dir_prefix(@)
{
	my (@opt_dir_prefix) = @_;
	my $item;

	return if (!@opt_dir_prefix);

	foreach $item (@opt_dir_prefix) {
		if ($item =~ /,/) {
			# Split and add comma-separated parameters
			push(@dir_prefix, split(/,/, $item));
		} else {
			# Add single parameter
			push(@dir_prefix, $item);
		}
	}
}

#
# rate(hit, found[, suffix, precision, width])
#
# Return the coverage rate [0..100] for HIT and FOUND values. 0 is only
# returned when HIT is 0. 100 is only returned when HIT equals FOUND.
# PRECISION specifies the precision of the result. SUFFIX defines a
# string that is appended to the result if FOUND is non-zero. Spaces
# are added to the start of the resulting string until it is at least WIDTH
# characters wide.
#

sub rate($$;$$$)
{
        my ($hit, $found, $suffix, $precision, $width) = @_;
        my $rate; 

	# Assign defaults if necessary
	$precision	= $default_precision if (!defined($precision));
	$suffix		= ""	if (!defined($suffix));
	$width		= 0	if (!defined($width));
        
        return sprintf("%*s", $width, "-") if (!defined($found) || $found == 0);
        $rate = sprintf("%.*f", $precision, $hit * 100 / $found);

	# Adjust rates if necessary
        if ($rate == 0 && $hit > 0) {
		$rate = sprintf("%.*f", $precision, 1 / 10 ** $precision);
        } elsif ($rate == 100 && $hit != $found) {
		$rate = sprintf("%.*f", $precision, 100 - 1 / 10 ** $precision);
	}

	return sprintf("%*s", $width, $rate.$suffix);
}