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
|
#! @PERL@ -w
#
# pkglint - lint for package directory
#
# implemented by:
# Jun-ichiro itojun Hagino <itojun@itojun.org>
# Yoshishige Arai <ryo2@on.rim.or.jp>
#
# Copyright(c) 1997 by Jun-ichiro Hagino <itojun@itojun.org>.
# All rights reserved.
# Freely redistributable. Absolutely no warranty.
#
# From Id: portlint.pl,v 1.64 1998/02/28 02:34:05 itojun Exp
# $NetBSD: pkglint.pl,v 1.381 2005/11/24 08:05:01 rillig Exp $
#
# This version contains lots of changes necessary for NetBSD packages
# done by:
# Hubert Feyrer <hubertf@netbsd.org>,
# Thorsten Frueauf <frueauf@netbsd.org>,
# Thomas Klausner <wiz@netbsd.org>,
# Roland Illig <rillig@NetBSD.org>
# and others.
#
#==========================================================================
# Some comments on the overall structure: The @EXPORT clauses in the pack-
# ages must be in a BEGIN block, because otherwise the names starting with
# an uppercase letter are not recognized as subroutines but as file handles.
#==========================================================================
package PkgLint::Util;
#==========================================================================
# This package is a catch-all for subroutines that are not application-spe-
# cific. Currently it contains the boolean constants C<false> and C<true>,
# as well as a function to print text in a table format.
#==========================================================================
BEGIN {
use Exporter;
use vars qw(@ISA @EXPORT_OK);
@ISA = qw(Exporter);
@EXPORT_OK = qw(false print_table true);
}
use constant false => 0;
use constant true => 1;
# Prints the C<$table> on the C<$out> stream. The C<$table> shall be an
# array of rows, each row shall be an array of cells, and each cell shall
# be a string.
sub print_table($$) {
my ($out, $table) = @_;
my (@width) = ();
foreach my $row (@{$table}) {
foreach my $i (0..$#{$row}) {
if (!defined($width[$i]) || length($row->[$i]) > $width[$i]) {
$width[$i] = length($row->[$i]);
}
}
}
foreach my $row (@{$table}) {
my ($max) = ($#{$row});
foreach my $i (0..$max) {
if ($i != 0) {
print $out (" ");
}
print $out ($row->[$i]);
if ($i != $max) {
print $out (" " x ($width[$i] - length($row->[$i])));
}
}
print $out ("\n");
}
}
#== End of PkgLint::Util ==================================================
package PkgLint::Logging;
#==========================================================================
# This package provides subroutines for printing messages to the user in a
# common format. The subroutines all have the parameters C<$fname>,
# C<$lineno> and C<$message>. In case there's no appropriate filename for
# the message, NO_FILE may be passed, likewise for C<$lineno> and
# NO_LINE_NUMBER. Before printing, the filename is normalized, that is,
# "/foo/bar/../../" components are removed, as well as "." components.
# At the end of the program, the subroutine print_summary_and_exit should
# be called.
#
# Examples:
# log_error(NO_FILE, NO_LINE_NUMBER, "Invalid command line.");
# log_warning($fname, NO_LINE_NUMBER, "Not found.");
# log_info($fname, $lineno, sprintf("invalid character (0x%02x).", $c));
#==========================================================================
use strict;
use warnings;
BEGIN {
use Exporter;
use vars qw(@ISA @EXPORT_OK);
@ISA = qw(Exporter);
@EXPORT_OK = qw(
NO_FILE NO_LINE_NUMBER
log_fatal log_error log_warning log_note log_info log_debug
print_summary_and_exit set_verbosity get_verbosity
set_gcc_output_format
get_show_source_flag set_show_source_flag
);
import PkgLint::Util qw(
false true
);
}
use constant NO_FILE => undef;
use constant NO_LINE_NUMBER => undef;
my $errors = 0;
my $warnings = 0;
my $verbosity = 0;
my $gcc_output_format = false;
my $show_source_flag = false;
sub log_message($$$$$) {
my ($out, $file, $lineno, $type, $message) = @_;
my ($text, $sep);
if (defined($file)) {
# strip "." path components
$file =~ s,^(?:\./)+,,;
$file =~ s,/(?:\./)+,/,g;
$file =~ s,/+,/,g;
# strip intermediate "../.." path components
while ($file =~ s,/[^.][^/]*/[^.][^/]*/\.\./\.\./,/,) {
}
}
$text = "";
$sep = "";
if (!$gcc_output_format && defined($type)) {
$text .= "${sep}${type}:";
$sep = " ";
}
if (defined($file)) {
$text .= defined($lineno)
? "${sep}${file}:${lineno}"
: "${sep}${file}";
$sep = ": ";
}
if ($gcc_output_format && defined($type)) {
$text .= "${sep}${type}:";
$sep = " ";
}
if (defined($message)) {
$text .= "${sep}${message}";
$sep = "";
}
print $out ("${text}\n");
}
sub log_fatal($$$) {
my ($file, $lineno, $msg) = @_;
log_message(*STDERR, $file, $lineno, $gcc_output_format ? "fatal" : "FATAL", $msg);
exit(1);
}
sub log_error($$$) {
my ($file, $lineno, $msg) = @_;
log_message(*STDOUT, $file, $lineno, $gcc_output_format ? "error" : "ERROR", $msg);
$errors++;
}
sub log_warning($$$) {
my ($file, $lineno, $msg) = @_;
log_message(*STDOUT, $file, $lineno, $gcc_output_format ? "warning" : "WARN", $msg);
$warnings++;
}
sub log_note($$$) {
my ($file, $lineno, $msg) = @_;
log_message(*STDOUT, $file, $lineno, $gcc_output_format ? "note" : "NOTE", $msg);
}
sub log_info($$$) {
my ($file, $lineno, $msg) = @_;
if ($verbosity >= 1) {
log_message(*STDOUT, $file, $lineno, $gcc_output_format ? "info" : "OK", $msg);
}
}
sub log_debug($$$) {
my ($file, $lineno, $msg) = @_;
if ($verbosity >= 2) {
log_message(*STDOUT, $file, $lineno, $gcc_output_format ? "debug" : "DEBUG", $msg);
}
}
sub print_summary_and_exit($) {
my ($quiet) = @_;
if ($show_source_flag) {
print("\n");
}
if (!$quiet) {
if ($errors != 0 || $warnings != 0) {
print("$errors errors and $warnings warnings found.\n");
} else {
print "looks fine.\n";
}
}
exit($errors != 0);
}
sub get_verbosity() {
return $verbosity;
}
sub set_verbosity($) {
($verbosity) = @_;
}
sub set_gcc_output_format() {
$gcc_output_format = true;
}
sub get_show_source_flag() {
return $show_source_flag;
}
sub set_show_source_flag() {
$show_source_flag = true;
}
#== End of PkgLint::Logging ===============================================
package PkgLint::FileUtil::Line;
#==========================================================================
# When files are read in by pkglint, they are interpreted in terms of
# lines. For Makefiles, line continuations are handled properly, allowing
# multiple physical lines to end in a single logical line. For other files
# there is a 1:1 translation.
#
# A difference between the physical and the logical lines is that the
# physical lines include the line end sequence, whereas the logical lines
# do not.
#
# A logical line is a class having the read-only fields C<file>,
# C<lines>, C<text>, C<physlines> and C<is_changed>, as well as some
# methods for printing diagnostics easily.
#
# Some other methods allow modification of the physical lines, but leave
# the logical line (the C<text>) untouched. These methods are used in the
# --autofix mode.
#==========================================================================
BEGIN {
import PkgLint::Util qw(
false true
);
}
use constant FILE => 0;
use constant LINES => 1;
use constant TEXT => 2;
use constant PHYSLINES => 3;
use constant CHANGED => 4;
use constant BEFORE => 5;
use constant AFTER => 6;
sub new($$$$) {
my ($class, $file, $lines, $text, $physlines) = @_;
my ($self) = ([$file, $lines, $text, $physlines, false, [], []]);
bless($self, $class);
return $self;
}
sub file($) {
return shift(@_)->[FILE];
}
sub lines($) {
return shift(@_)->[LINES];
}
sub text($) {
return shift(@_)->[TEXT];
}
sub physlines($) {
my ($self) = @_;
return [@{$self->[BEFORE]}, @{$self->[PHYSLINES]}, @{$self->[AFTER]}];
}
sub is_changed($) {
return shift(@_)->[CHANGED];
}
sub show_source($$) {
my ($self, $out) = @_;
if (PkgLint::Logging::get_show_source_flag()) {
printf $out ("\n");
foreach my $line (@{$self->physlines}) {
printf $out ("> %s", $line->[1]);
}
}
}
sub log_fatal($$) {
my ($self, $text) = @_;
$self->show_source(*STDERR);
PkgLint::Logging::log_fatal($self->[FILE], $self->[LINES], $text);
}
sub log_error($$) {
my ($self, $text) = @_;
$self->show_source(*STDOUT);
PkgLint::Logging::log_error($self->[FILE], $self->[LINES], $text);
}
sub log_warning($$) {
my ($self, $text) = @_;
$self->show_source(*STDOUT);
PkgLint::Logging::log_warning($self->[FILE], $self->[LINES], $text);
}
sub log_note($$) {
my ($self, $text) = @_;
$self->show_source(*STDOUT);
PkgLint::Logging::log_note($self->[FILE], $self->[LINES], $text);
}
sub log_info($$) {
my ($self, $text) = @_;
if (PkgLint::Logging::get_verbosity() >= 1) {
$self->show_source(*STDOUT);
}
PkgLint::Logging::log_info($self->[FILE], $self->[LINES], $text);
}
sub log_debug($$) {
my ($self, $text) = @_;
if (PkgLint::Logging::get_verbosity() >= 2) {
$self->show_source(*STDOUT);
}
PkgLint::Logging::log_debug($self->[FILE], $self->[LINES], $text);
}
sub to_string($) {
my ($self) = @_;
return sprintf("%s:%s: %s", $self->[FILE], $self->[LINES], $self->[TEXT]);
}
sub prepend_before($$) {
my ($self, $text) = @_;
unshift(@{$self->[BEFORE]}, [0, "$text\n"]);
$self->[CHANGED] = true;
}
sub append_before($$) {
my ($self, $text) = @_;
push(@{$self->[BEFORE]}, [0, "$text\n"]);
$self->[CHANGED] = true;
}
sub prepend_after($$) {
my ($self, $text) = @_;
unshift(@{$self->[AFTER]}, [0, "$text\n"]);
$self->[CHANGED] = true;
}
sub append_after($$) {
my ($self, $text) = @_;
push(@{$self->[AFTER]}, [0, "$text\n"]);
$self->[CHANGED] = true;
}
sub delete($) {
my ($self) = @_;
$self->[PHYSLINES] = [];
$self->[CHANGED] = true;
}
#== End of PkgLint::FileUtil::Line ========================================
package PkgLint::FileUtil;
#==========================================================================
# This package provides subroutines for loading and saving line-oriented
# files. The load_file() subroutine loads a file completely into memory,
# optionally handling continuation line folding. The load_lines() subrou-
# tine is an abbreviation for the common case of loading files without
# continuation lines. The save_autofix_lines() subroutine examines an array
# of lines if some of them have changed. It then saves the modified files.
#==========================================================================
use strict;
use warnings;
BEGIN {
use Exporter;
use vars qw(@ISA @EXPORT_OK);
@ISA = qw(Exporter);
@EXPORT_OK = qw(
load_file load_lines
save_autofix_changes
);
import PkgLint::Util qw(
false true
);
import PkgLint::Logging qw(
NO_LINE_NUMBER
log_error log_note
);
}
sub load_physical_lines($) {
my ($fname) = @_;
my ($physlines, $line, $lineno);
$physlines = [];
open(F, "< $fname") or return undef;
$lineno = 0;
while (defined($line = <F>)) {
$lineno++;
push(@{$physlines}, [$lineno, $line]);
}
close(F) or return undef;
return $physlines;
}
sub get_logical_line($$$) {
my ($fname, $lines, $ref_lineno) = @_;
my ($value, $lineno, $first, $firstlineno, $lastlineno, $physlines);
$value = "";
$first = true;
$lineno = ${$ref_lineno};
$firstlineno = $lines->[$lineno]->[0];
$physlines = [];
for (; $lineno <= $#{$lines}; $lineno++) {
if ($lines->[$lineno]->[1] =~ qr"^([ \t]*)(.*?)([ \t]*)(\\?)\n?$") {
my ($indent, $text, $outdent, $cont) = ($1, $2, $3, $4);
if ($first) {
$value .= $indent;
$first = false;
}
$value .= $text;
push(@{$physlines}, $lines->[$lineno]);
if ($cont eq "\\") {
$value .= " ";
} else {
$value .= $outdent;
last;
}
}
}
if ($lineno > $#{$lines}) {
# The last line in the file is a continuation line
$lineno--;
}
$lastlineno = $lines->[$lineno]->[0];
${$ref_lineno} = $lineno + 1;
return PkgLint::FileUtil::Line->new($fname,
$firstlineno == $lastlineno
? $firstlineno
: "$firstlineno--$lastlineno",
$value,
$physlines);
}
sub load_lines($$) {
my ($fname, $fold_backslash_lines) = @_;
my ($physlines, $seen_newline, $loglines) = @_;
$physlines = load_physical_lines($fname);
if (!$physlines) {
return false;
}
$seen_newline = true;
$loglines = [];
if ($fold_backslash_lines) {
for (my $lineno = 0; $lineno <= $#{$physlines}; ) {
push(@{$loglines}, get_logical_line($fname, $physlines, \$lineno));
}
} else {
foreach my $physline (@{$physlines}) {
my $text = $physline->[1];
$text =~ s/\n$//;
push(@{$loglines}, PkgLint::FileUtil::Line->new($fname, $physline->[0], $text, [$physline]));
}
}
if (0 <= $#{$physlines} && $physlines->[-1]->[1] !~ qr"\n$") {
log_error($fname, $physlines->[-1]->[0], "File must end with a newline.");
}
return $loglines;
}
sub load_file($) {
my ($fname) = @_;
return load_lines($fname, false);
}
sub save_autofix_changes($) {
my ($lines) = @_;
my (%changed, %physlines);
foreach my $line (@{$lines}) {
if ($line->is_changed) {
$changed{$line->file}++;
}
push(@{$physlines{$line->file}}, @{$line->physlines});
}
foreach my $fname (sort(keys(%changed))) {
my $old = "${fname}.pkglint.old";
my $new = "${fname}.pkglint.new";
if (!open(F, ">", $new)) {
log_error($new, NO_LINE_NUMBER, "$!");
next;
}
foreach my $physline (@{$physlines{$fname}}) {
print F ($physline->[1]);
}
if (!close(F)) {
log_error($new, NO_LINE_NUMBER, "$!");
next;
}
unlink($old); # without error checking
if (!link($fname, $old)) {
log_error($fname, NO_LINE_NUMBER, "$!");
next;
}
if (!rename($new, $fname)) {
log_error($fname, NO_LINE_NUMBER, "$!");
next;
}
log_note($fname, NO_LINE_NUMBER, "Has been autofixed.");
}
}
#== End of PkgLint::FileUtil ==============================================
package main;
#==========================================================================
# This package contains the application-specific code of pkglint.
# Most subroutines in this package follow a strict naming convention:
#
# The get_*() functions provide easy access to important non-trivial data
# structures that are loaded from external files and are therefore cached.
#
# The is_*() functions return a boolean value and have no side effects.
#
# The checkline_*() procedures check a single line for compliance with some
# rules.
#
# The checklines_*() procedures check an array of lines for compliance.
# Usually they make use of several checkline_*() procedures.
#
# The checkfile_*() procedures load a file and check the lines of that
# file. Usually they make use of several checklines_*() and checkline_*()
# procedures.
#
# The checkdir_*() procedures check the files of a directory and call
# checkfile_*() on them.
#
# Note: The order of subroutines in the file is mostly historical. One
# important property is that there are no back-references, that is, if
# you start reading the code from the top to the bottom you won't find a
# call to a subroutine you haven't yet seen.
#==========================================================================
use strict;
use warnings;
use Digest::SHA1;
use Getopt::Long qw(:config no_ignore_case bundling require_order);
use Fcntl qw(:mode);
use File::Basename;
use File::stat;
use Cwd;
BEGIN {
import PkgLint::Util qw(
false true
);
import PkgLint::Logging qw(
NO_FILE NO_LINE_NUMBER
log_fatal log_error log_warning log_info log_debug
);
import PkgLint::FileUtil qw(
load_file load_lines
save_autofix_changes
);
}
#
# Buildtime configuration
#
use constant conf_rcsidstring => 'NetBSD';
use constant conf_distver => '@DISTVER@';
use constant conf_make => '@MAKE@';
use constant conf_datadir => '@DATADIR@';
#
# Command Line Options
#
my $opt_check_ALTERNATIVES = true;
my $opt_check_bl3 = true;
my $opt_check_DESCR = true;
my $opt_check_distinfo = true;
my $opt_check_extra = false;
my $opt_check_INSTALL = true;
my $opt_check_Makefile = true;
my $opt_check_MESSAGE = true;
my $opt_check_mk = true;
my $opt_check_patches = true;
my $opt_check_PLIST = true;
my (%checks) = (
"ALTERNATIVES" => [\$opt_check_ALTERNATIVES, "check ALTERNATIVES files"],
"bl3" => [\$opt_check_bl3, "check buildlink3 files"],
"DESCR" => [\$opt_check_DESCR, "check DESCR file"],
"distinfo" => [\$opt_check_distinfo, "check distinfo file"],
"extra" => [\$opt_check_extra, "check various additional files"],
"INSTALL" => [\$opt_check_INSTALL, "check INSTALL and DEINSTALL scripts"],
"Makefile" => [\$opt_check_Makefile, "check Makefiles"],
"MESSAGE" => [\$opt_check_MESSAGE, "check MESSAGE files"],
"mk" => [\$opt_check_mk, "check other .mk files"],
"patches" => [\$opt_check_patches, "check patches"],
"PLIST" => [\$opt_check_PLIST, "check PLIST files"],
);
my $opt_warn_absname = true;
my $opt_warn_directcmd = true;
my $opt_warn_order = true;
my $opt_warn_plist_sort = false;
my $opt_warn_types = true;
my (%warnings) = (
"absname" => [\$opt_warn_absname, "warn about use of absolute file names"],
"directcmd" => [\$opt_warn_directcmd, "warn about use of direct command names instead of Make variables"],
"order" => [\$opt_warn_order, "warn if Makefile entries are unordered"],
"plist-sort" => [\$opt_warn_plist_sort, "warn about unsorted entries in PLISTs"],
"types" => [\$opt_warn_types, "do some simple type checking in Makefiles"],
);
my $opt_autofix = false;
my $opt_dumpmakefile = false;
my $opt_import = false;
my $opt_quiet = false;
my $opt_recursive = false;
my $opt_rcsidstring = conf_rcsidstring;
my (@options) = (
# [ usage-opt, usage-message, getopt-opt, getopt-action ]
[ "-C{check,...}", "Enable or disable specific checks",
"check|C=s",
sub {
my ($opt, $val) = @_;
parse_multioption($val, \%checks);
} ],
[ "-F|--autofix", "Try to automatically fix some errors (experimental)",
"autofix|F", \$opt_autofix ],
[ "-I|--dumpmakefile", "Dump the Makefile after parsing",
"dumpmakefile|I", \$opt_dumpmakefile ],
[ "-R|--rcsidstring", "Set the allowed RCS Id strings",
"rcsidstring|R=s", \$opt_rcsidstring ],
[ "-V|--version", "print the version number of pkglint",
"version|V",
sub {
printf("%s\n", conf_distver);
exit(0);
} ],
[ "-W{warn,...}", "enable or disable specific warnings",
"warning|W=s",
sub {
my ($opt, $val) = @_;
parse_multioption($val, \%warnings);
} ],
[ "-g|--gcc-output-format", "Mimic the gcc output format",
"gcc-output-format|g",
sub {
PkgLint::Logging::set_gcc_output_format();
} ],
[ "-h|--help", "print a detailed help message",
"help|h",
sub {
help(*STDOUT, 0, 1);
} ],
[ "-i|--import", "Prepare the import of a wip package",
"import|i", \$opt_import ],
[ "-q|--quiet", "Don't print a summary line when finishing",
"quiet|q", \$opt_quiet ],
[ "-r|--recursive", "Recursive---check subdirectories, too",
"recursive|r", \$opt_recursive ],
[ "-s|--source", "Show the source lines together with diagnostics",
"source|s",
sub {
PkgLint::Logging::set_show_source_flag();
} ],
[ "-v|--verbose", "print progress messages",
"verbose|v",
sub {
PkgLint::Logging::set_verbosity(PkgLint::Logging::get_verbosity() + 1);
} ]
);
#
# Commonly used regular expressions.
#
use constant regex_pkgname => qr"^((?:[\w.+]|-[^\d])+)-(\d(?:\w|\.\d)*)$";
use constant regex_shellcmd => qr"^\t(.*)$";
use constant regex_unresolved => qr"\$\{";
use constant regex_validchars => qr"[\011\040-\176]";
use constant regex_varassign => qr"^([-A-Z_a-z0-9.\${}\[]+)\s*(=|\?=|\+=|:=|!=)\s*((?:\\#|[^#])*?)(?:\s*(#.*))?$";
# This "constant" is often used in contexts where interpolation comes
# handy, so it is a variable. Nevertheless it is not modified. Of course
# it cannot parse all kinds of shell programs, but this pattern will
# catch all shell programs that are portable enough to be used in pkgsrc.
my $regex_shellword = qr"\s*(
(?: '[^']*'
| \"(?:\\.|[^\"\\])*\"
| \`[^\`]*\`
| \\.
| [^'\"\\\s;&\|<>\#]
)+ | ;;? | &&? | \|\|? | <<? | >>? | \#.*)"sx;
#
# Global variables.
#
my $current_dir; # The currently checked directory.
my $is_wip; # Is the current directory from pkgsrc-wip?
my $pkgsrcdir; # The pkgsrc root directory
my $pkgdir; # PKGDIR from the package Makefile
my $filesdir; # FILESDIR from the package Makefile
my $patchdir; # PATCHDIR from the package Makefile
my $distinfo_file; # DISTINFO_FILE from the package Makefile
my $pkgname; # PKGNAME from the package Makefile
my $hack_php_patches; # Ignore non-existing patches in distinfo
my $makevar; # Table of variables and values
my $seen_Makefile_common; # Does the package have any .includes?
my @todo_items; # The list of directory entries that still need
# to be checked. Mostly relevant with
# --recursive.
#
# Command line parsing and handling.
#
sub help($$$) {
my ($out, $exitval, $show_all) = @_;
my ($prog) = (basename($0));
print $out ("usage: $prog [options] [package_directory]\n\n");
my (@option_table) = ();
foreach my $opt (@options) {
push(@option_table, [" ", $opt->[0], $opt->[1]]);
}
print $out ("options:\n");
PkgLint::Util::print_table($out, \@option_table);
print $out ("\n");
if (!$show_all) {
exit($exitval);
}
my (@checks_table) = (
[" ", "all", "", "enable all checks"],
[" ", "none", "", "disable all checks"],
);
foreach my $check (sort keys %checks) {
push(@checks_table, [ " ", $check,
(${$checks{$check}->[0]} ? "(enabled)" : "(disabled)"),
$checks{$check}->[1]]);
}
print $out ("checks: (use \"check\" to enable, \"no-check\" to disable)\n");
PkgLint::Util::print_table($out, \@checks_table);
print $out ("\n");
my (@warnings_table) = (
[" ", "all", "", "enable all warnings"],
[" ", "none", "", "disable all warnings"],
);
foreach my $warning (sort keys %warnings) {
push(@warnings_table, [ " ", $warning,
(${$warnings{$warning}->[0]} ? "(enabled)" : "(disabled)"),
$warnings{$warning}->[1]]);
}
print $out ("warnings: (use \"warn\" to enable, \"no-warn\" to disable)\n");
PkgLint::Util::print_table($out, \@warnings_table);
print $out ("\n");
exit($exitval);
}
sub parse_multioption($$) {
my ($value, $optdefs) = @_;
foreach my $opt (split(qr",", $value)) {
if ($opt eq "none") {
foreach my $key (keys %{$optdefs}) {
${$optdefs->{$key}->[0]} = false;
}
} elsif ($opt eq "all") {
foreach my $key (keys %{$optdefs}) {
${$optdefs->{$key}->[0]} = true;
}
} else {
my ($value) = (($opt =~ s/^no-//) ? false : true);
if (exists($optdefs->{$opt})) {
${$optdefs->{$opt}->[0]} = $value;
} else {
printf STDERR ("Invalid option: ${opt}\n");
help(*STDERR, 1, 0);
}
}
}
}
sub parse_command_line() {
my (%options);
foreach my $opt (@options) {
$options{$opt->[2]} = $opt->[3];
}
{
local $SIG{__WARN__} = sub {};
if (!GetOptions(%options)) {
help(*STDERR, 1, false);
}
}
}
#
# Loading pkglint-specific data from files, part 1.
#
sub load_make_vars_typemap() {
my ($vartypes);
my ($fname);
$vartypes = {};
$fname = conf_datadir."/makevars.map";
if ((my $lines = load_file($fname))) {
foreach my $line (@{$lines}) {
if ($line->text =~ qr"^(?:#.*|\s*)$") {
# ignore empty and comment lines
} elsif ($line->text =~ qr"^([\w\d_.]+)\s+([-!\+.\w\d_ \{\}]+)$") {
$vartypes->{$1} = $2;
} else {
$line->log_fatal("Unknown line format.");
}
}
} else {
log_fatal($fname, NO_LINE_NUMBER, "Cannot be read.");
}
# TODO: Enable when the time is ripe.
if (false) {
# Additionally, scan mk/defaults/mk.conf for variable
# definitions. All these variables are reserved for the user and
# must not be set within packages.
$fname = "${pkgsrcdir}/mk/defaults/mk.conf";
if ((my $lines = load_file($fname))) {
foreach my $line (@{$lines}) {
if ($line->text =~ qr"^#?([\w_]+)\?=") {
my ($varname) = ($1);
$line->log_info("Found user-definable variable ${varname}.");
$vartypes->{$varname} = "Userdefined";
}
}
} else {
log_fatal($fname, NO_LINE_NUMBER, "Cannot be read.");
}
}
return $vartypes;
}
my $load_dist_sites_url2name = undef;
my $load_dist_sites_names = undef;
sub load_dist_sites() {
my ($fname) = ("${pkgsrcdir}/mk/bsd.sites.mk");
my ($lines) = load_file($fname);
my ($varname) = undef;
my ($ignoring) = false;
my ($url2name) = {};
my ($names) = {};
if (!$lines) {
log_error($fname, NO_LINE_NUMBER, "Could not be read.");
$load_dist_sites_url2name = $url2name;
$load_dist_sites_names = $names;
return;
}
foreach my $line (@{$lines}) {
my $text = $line->text;
if ($text =~ qr"^(MASTER_SITE_\w+)\+=\s*\\$"o) {
$varname = $1;
$names->{$varname} = true;
$ignoring = false;
} elsif ($text eq "MASTER_SITE_BACKUP?=\t\\") {
$ignoring = true;
} elsif ($text =~ qr"^\t((?:http://|ftp://)\S+/)(?:|\s*\\)$"o) {
if (!$ignoring) {
if (defined($varname)) {
$url2name->{$1} = $varname;
} else {
$line->log_error("Lonely URL found.");
}
}
} elsif ($text =~ qr"^(?:#.*|\s*)$") {
# ignore empty and comment lines
} elsif ($text =~ qr"BSD_SITES_MK") {
# ignore multiple inclusion guards
} else {
$line->log_error("Unknown line type.");
}
}
# Explicitly allowed, although not defined in mk/bsd.sites.mk.
$names->{"MASTER_SITE_SUSE_UPD"} = true;
$names->{"MASTER_SITE_LOCAL"} = true;
log_info($fname, NO_LINE_NUMBER, sprintf("Loaded %d MASTER_SITE_* definitions.", scalar(keys(%{$url2name}))));
$load_dist_sites_url2name = $url2name;
$load_dist_sites_names = $names;
}
sub get_dist_sites() {
if (!defined($load_dist_sites_url2name)) {
load_dist_sites();
}
return $load_dist_sites_url2name;
}
sub get_dist_sites_names() {
if (!defined($load_dist_sites_names)) {
load_dist_sites();
}
return $load_dist_sites_names;
}
#
# Miscellaneous functions
#
sub is_committed($) {
my ($fname) = @_;
my ($basename, $entries);
$basename = basename($fname);
$entries = load_file(dirname($fname) . "/CVS/Entries");
if (!$entries) {
return false;
}
foreach my $entry (@{$entries}) {
if ($entry->text =~ qr"^/\Q${basename}\E/") {
return true;
}
}
return false;
}
# - A directory that has no entries except ".", ".." and "CVS" is empty.
# - A directory whose entries besides ".", ".." and "CVS" are all empty is empty.
# - No other directories are empty.
sub is_emptydir($);
sub is_emptydir($) {
my ($dir) = @_;
my ($rv);
if (!opendir(DIR, $dir)) {
return true;
}
$rv = true;
foreach my $subdir (readdir(DIR)) {
next if $subdir eq "." || $subdir eq ".." || $subdir eq "CVS";
next if -d "${dir}/${subdir}" && is_emptydir("${dir}/${subdir}");
$rv = false;
last;
}
closedir(DIR);
return $rv;
}
sub get_subdirs($) {
my ($dir) = @_;
my (@result) = ();
if (opendir(DIR, $dir)) {
foreach my $subdir (readdir(DIR)) {
if ($subdir ne "." && $subdir ne ".." && $subdir ne "CVS" && -d "${dir}/${subdir}" && !is_emptydir("${dir}/${subdir}")) {
push(@result, $subdir);
}
}
closedir(DIR);
}
return @result;
}
sub checkperms($) {
my ($fname) = @_;
if (-f $fname && -x $fname && !is_committed($fname)) {
log_warning($fname, NO_LINE_NUMBER, "Should not be executable.");
}
}
#
# Subroutines to check a single line.
#
sub checkline_length($$) {
my ($line, $maxlength) = @_;
if (length($line->text) > $maxlength) {
$line->log_warning("Line too long (should be no more than $maxlength characters).");
}
}
sub checkline_valid_characters($$) {
my ($line, $re_validchars) = @_;
my ($rest);
($rest = $line->text) =~ s/$re_validchars//g;
if ($rest ne "") {
my @chars = map { $_ = sprintf("0x%02x", ord($_)); } split(//, $rest);
$line->log_warning(sprintf("Line contains invalid characters (%s).", join(", ", @chars)));
}
}
sub checkline_valid_characters_in_variable($$) {
my ($line, $re_validchars) = @_;
my ($varname, $rest);
$rest = $line->text;
if ($rest =~ regex_varassign) {
($varname, undef, $rest) = ($1, $2, $3);
} else {
return;
}
$rest =~ s/$re_validchars//g;
if ($rest ne "") {
my @chars = map { $_ = sprintf("0x%02x", ord($_)); } split(//, $rest);
$line->log_warning(sprintf("${varname} contains invalid characters (%s).", join(", ", @chars)));
}
}
sub checkline_trailing_whitespace($) {
my ($line) = @_;
if ($line->text =~ /\s+$/) {
$line->log_warning("Trailing white-space.");
}
}
sub checkline_rcsid_regex($$$) {
my ($line, $prefix_regex, $prefix) = @_;
my ($id) = ($opt_rcsidstring . ($is_wip ? "|Id" : ""));
if ($line->text !~ qr"^${prefix_regex}\$(${id})(?::[^\$]*|)\$$") {
$line->log_error("\"${prefix}\$${opt_rcsidstring}\$\" expected.");
return false;
}
return true;
}
sub checkline_rcsid($$) {
my ($line, $prefix) = @_;
checkline_rcsid_regex($line, quotemeta($prefix), $prefix);
}
#
# Procedures to check an array of lines, part 1.
#
sub checklines_trailing_empty_lines($) {
my ($lines) = @_;
my ($last, $max);
$max = $#{$lines} + 1;
for ($last = $max; $last > 1 && $lines->[$last - 1]->text eq ""; ) {
$last--;
}
if ($last != $max) {
$lines->[$last]->log_warning("Trailing empty lines.");
}
}
#
# Procedures to check a file, part 1.
#
sub checkfile_DESCR($) {
my ($fname) = @_;
my ($maxchars, $maxlines) = (80, 24);
my ($descr);
log_info($fname, NO_LINE_NUMBER, "[checkfile_DESCR]");
checkperms($fname);
if (!($descr = load_file($fname))) {
log_error($fname, NO_LINE_NUMBER, "Cannot be read.");
return;
}
if (@{$descr} == 0) {
log_error($fname, NO_LINE_NUMBER, "Must not be empty.");
return;
}
foreach my $line (@{$descr}) {
checkline_length($line, $maxchars);
checkline_trailing_whitespace($line);
checkline_valid_characters($line, regex_validchars);
}
checklines_trailing_empty_lines($descr);
if (@{$descr} > $maxlines) {
log_warning($fname, NO_LINE_NUMBER, "File too long (should be no more than $maxlines lines).");
}
}
sub checkfile_distinfo($) {
my ($fname) = @_;
my ($lines, %in_distinfo, %sums);
log_info($fname, NO_LINE_NUMBER, "[checkfile_distinfo]");
checkperms($fname);
if (!($lines = load_file($fname))) {
log_error($fname, NO_LINE_NUMBER, "Cannot be read.");
return;
}
if (@{$lines} == 0) {
log_error($fname, NO_LINE_NUMBER, "Must not be empty.");
return;
}
checkline_rcsid($lines->[0], "");
if (1 <= $#{$lines} && $lines->[1]->text ne "") {
$lines->[1]->log_warning("Empty line expected.");
}
foreach my $line (@{$lines}[2..$#{$lines}]) {
if ($line->text !~ /^(MD5|SHA1|RMD160|Size) \(([^)]+)\) = (.*)(?: bytes)?$/) {
$line->log_error("Unknown line type.");
next;
}
my ($alg, $file, $sum) = ($1, $2, $3);
if ($file =~ /^patch-[A-Za-z0-9]+$/) {
if (open(PATCH, "< ${current_dir}/${patchdir}/${file}")) {
my $data = "";
foreach my $patchline (<PATCH>) {
$data .= $patchline unless $patchline =~ qr"\$NetBSD.*\$";
}
close(PATCH);
my $chksum = Digest::SHA1::sha1_hex($data);
if ($sum ne $chksum) {
$line->log_error("${alg} checksum of $file differs (expected ${sum}, got ${chksum}). Rerun '".conf_make." makepatchsum'.");
}
} elsif (!$hack_php_patches) {
$line->log_warning("$file does not exist.");
}
} else {
$sums{$alg}->{$file} = $line;
}
$in_distinfo{$file} = true;
}
checklines_trailing_empty_lines($lines);
# Check for distfiles that have SHA1, but not RMD160 checksums
foreach my $sha1_file (sort(keys(%{$sums{"SHA1"}}))) {
if (!exists($sums{"RMD160"}->{$sha1_file})) {
$sums{"SHA1"}->{$sha1_file}->log_error("RMD160 checksum missing for \"${sha1_file}\".");
}
}
foreach my $patch (<${current_dir}/$patchdir/patch-*>) {
$patch = basename($patch);
if (!exists($in_distinfo{$patch})) {
log_error($fname, NO_LINE_NUMBER, "$patch is not recorded. Rerun '".conf_make." makepatchsum'.");
}
}
}
sub checkfile_MESSAGE($) {
my ($fname) = @_;
my ($message);
log_info($fname, NO_LINE_NUMBER, "[checkfile_MESSAGE]");
checkperms($fname);
if (!($message = load_file($fname))) {
log_error($fname, NO_LINE_NUMBER, "Cannot be read.");
return;
}
if (@{$message} < 3) {
log_warning($fname, NO_LINE_NUMBER, "File too short.");
return;
}
if ($message->[0]->text ne "=" x 75) {
$message->[0]->log_warning("Expected a line of exactly 75 \"=\" characters.");
}
checkline_rcsid($message->[1], "");
foreach my $line (@{$message}) {
checkline_length($line, 80);
checkline_trailing_whitespace($line);
checkline_valid_characters($line, regex_validchars);
}
if ($message->[-1]->text ne "=" x 75) {
$message->[-1]->log_warning("Expected a line of exactly 75 \"=\" characters.");
}
checklines_trailing_empty_lines($message);
}
sub checkfile_PLIST($) {
my ($fname) = @_;
my ($plist, $last_file_seen);
log_info($fname, NO_LINE_NUMBER, "[checkfile_PLIST]");
checkperms($fname);
if (!($plist = load_file($fname))) {
log_error($fname, NO_LINE_NUMBER, "Cannot be read.");
return;
}
if (@{$plist} == 0) {
log_error($fname, NO_LINE_NUMBER, "Must not be empty.");
return;
}
checkline_rcsid($plist->[0], "\@comment ");
line:
foreach my $line (@{$plist}) {
my $text = $line->text;
checkline_trailing_whitespace($line);
if ($text =~ /^\@([a-z]+)\s+(.*)/) {
my ($cmd, $arg) = ($1, $2);
if ($cmd eq "unexec" && $arg =~ /^rmdir/) {
$line->log_warning("Use \"\@dirrm\" instead of \"\@unexec rmdir\".");
} elsif (($cmd eq "exec" || $cmd eq "unexec")) {
if ($arg =~ /(?:install-info|\$\{INSTALL_INFO\})/) {
$line->log_warning("\@exec/unexec install-info is deprecated.");
} elsif ($arg =~ /ldconfig/ && $arg !~ qr"/usr/bin/true") {
$line->log_error("ldconfig must be used with \"||/usr/bin/true\".");
}
} elsif ($cmd eq "comment" || $cmd eq "dirrm") {
# nothing to do
} else {
$line->log_warning("Unknown PLIST directive \"\@$cmd\".");
}
} elsif ($text =~ qr"^[A-Za-z0-9\$]") {
if ($opt_warn_plist_sort && $text =~ qr"^\w" && $text !~ regex_unresolved) {
if (defined($last_file_seen)) {
if ($last_file_seen gt $text) {
$line->log_warning("${text} should be sorted before ${last_file_seen}.");
}
}
$last_file_seen = $text;
}
if ($text =~ qr"^doc/") {
$line->log_error("Documentation must be installed under share/doc, not doc.");
} elsif ($text =~ qr"^etc/rc\.d/") {
$line->log_error("RCD_SCRIPTS must not be registered in the PLIST. Please use the RCD_SCRIPTS framework.");
} elsif ($text =~ qr"^etc/") {
$line->log_error("Configuration files must not be registered in the PLIST. Please use the CONF_FILES framework, which is described in mk/install/bsd.pkginstall.mk.");
} elsif ($text eq "info/dir") {
$line->log_error("\"info/dir\" must not be listed. Use install-info to add/remove an entry.");
} elsif ($text =~ qr"^lib/locale/") {
$line->log_error("\"lib/locale\" must not be listed. Use \${PKGLOCALEDIR}/locale and set USE_PKGLOCALEDIR instead.");
} elsif ($text =~ qr"^share/locale/") {
$line->log_warning("Use of \"share/locale\" is deprecated. Use \${PKGLOCALEDIR}/locale and set USE_PKGLOCALEDIR instead.");
} elsif ($text =~ qr"^share/man/") {
$line->log_warning("Man pages should be installed into man/, not share/man/.");
}
if ($text =~ /\${PKGLOCALEDIR}/ && defined($makevar) && !exists($makevar->{"USE_PKGLOCALEDIR"})) {
$line->log_warning("PLIST contains \${PKGLOCALEDIR}, but USE_PKGLOCALEDIR was not found.");
}
if ($text =~ qr"/CVS/") {
$line->log_warning("CVS files should not be in the PLIST.");
}
} else {
$line->log_error("Unknown line type.");
}
}
checklines_trailing_empty_lines($plist);
}
sub checkfile_extra($) {
my ($fname) = @_;
my ($lines);
log_info($fname, NO_LINE_NUMBER, "[checkfile_extra]");
$lines = load_file($fname);
if (!$lines) {
log_error($fname, NO_LINE_NUMBER, "Could not be read.");
return;
}
checklines_trailing_empty_lines($lines);
checkperms($fname);
}
#
# Procedures to check an array of lines, part 2.
#
sub checklines_multiple_patches($) {
my ($lines) = @_;
my ($files_in_patch, $patch_state, $line_type, $dellines);
$files_in_patch = 0;
$patch_state = "";
$dellines = 0;
foreach my $line (@{$lines}) {
my $text = $line->text;
if ($text =~ qr"^@@ -\d+,(\d+) \+\d+,\d+ @@") {
$line_type = "";
$dellines = $1;
} elsif ($dellines == 0 && index($text, "--- ") == 0 && $text !~ qr"^--- \d+(?:,\d+|) ----$") {
$line_type = "-";
} elsif (index($text, "*** ") == 0 && $text !~ qr"^\*\*\* \d+(?:,\d+|) \*\*\*\*$") {
$line->log_warning("Please use unified diffs (diff -u) for patches.");
$line_type = "*";
} elsif (index($text, "+++ ") == 0) {
$line_type = "+";
} elsif ($dellines > 0 && $text =~ qr"^(?:-|\s)") {
$line_type = "";
$dellines--;
} else {
$line_type = "";
}
if ($patch_state eq "*") {
if ($line_type eq "-") {
$files_in_patch++;
$patch_state = "";
} else {
$line->log_error("[internal] Unknown patch format.");
}
} elsif ($patch_state eq "-") {
if ($line_type eq "+") {
$files_in_patch++;
$patch_state = "";
} else {
$line->log_error("[internal] Unknown patch format.");
}
} elsif ($patch_state eq "") {
$patch_state = $line_type;
}
}
if ($files_in_patch > 1) {
log_warning($lines->[0]->file, NO_LINE_NUMBER, "Contains patches for $files_in_patch files, should be only one.");
} elsif ($files_in_patch == 0) {
log_error($lines->[0]->file, NO_LINE_NUMBER, "Contains no patch.");
}
}
#
# Procedures to check a file, part 2.
#
sub checkfile_patches_patch($) {
my ($fname) = @_;
my ($lines);
log_info($fname, NO_LINE_NUMBER, "[checkfile_patches_patch]");
checkperms($fname);
if (!($lines = load_file($fname))) {
log_error($fname, NO_LINE_NUMBER, "Could not be read.");
return;
}
if (@{$lines} == 0) {
log_error($fname, NO_LINE_NUMBER, "Must not be empty.");
return;
}
checkline_rcsid($lines->[0], "");
foreach my $line (@{$lines}[1..$#{$lines}]) {
if ($line->text =~ qr"\$(Author|Date|Header|Id|Locker|Log|Name|RCSfile|Revision|Source|State|$opt_rcsidstring)(?::[^\$]*?|)\$") {
my ($tag) = ($1);
$line->log_warning("Possible RCS tag \"\$${tag}\$\". Please remove it by reducing the number of context lines using pkgdiff or \"diff -U[210]\".");
}
}
checklines_trailing_empty_lines($lines);
checklines_multiple_patches($lines);
}
#
# Loading package-specific data from files.
#
sub readmakefile($$$$);
sub readmakefile($$$$) {
my ($file, $main_lines, $all_lines, $seen_Makefile_include) = @_;
my $contents = "";
my ($includefile, $dirname, $lines, $is_main_Makefile);
$lines = load_lines($file, true);
if (!$lines) {
return false;
}
$is_main_Makefile = (@{$main_lines} == 0);
foreach my $line (@{$lines}) {
my $text = $line->text;
if ($is_main_Makefile) {
push(@{$main_lines}, $line);
}
push(@{$all_lines}, $line);
# try to get any included file
my $is_include_line = false;
if ($text =~ qr"^\.\s*include\s+\"(.*)\"$") {
$includefile = $1;
$includefile =~ s/\$\{.CURDIR\}/./;
if ($includefile =~ regex_unresolved) {
$includefile =~ s,\$\{PHPPKGSRCDIR\},../../lang/php5,g;
$includefile =~ s,\$\{SUSE_DIR_PREFIX\},suse91,g;
$includefile =~ s,\$\{PYPKGSRCDIR\},../../lang/python23,g;
if ($file !~ qr"/mk/" && $includefile =~ regex_unresolved) {
$line->log_warning("Skipping include file \"${includefile}\". This may result in false warnings.");
}
} elsif (exists($seen_Makefile_include->{$includefile})) {
# Don't include any file twice
} else {
$is_include_line = true;
$seen_Makefile_include->{$includefile} = true;
}
}
if ($is_include_line) {
if ($includefile =~ qr"^\.\./[^./][^/]*/[^/]+") {
$line->log_warning("Relative directories should look like \"../../category/package\", not \"../package\".");
}
if ($includefile =~ qr"(?:^|/)Makefile.common$"
|| ($includefile =~ qr"^(?:\.\./(?:\.\./[^/]+/)?[^/]+/)?([^/]+)$" && $1 ne "buildlink3.mk")) {
$seen_Makefile_common = true;
}
if ($includefile =~ qr"/mk/") {
# skip these files
$contents .= $text . "\n";
} else {
$dirname = dirname($file);
# Only look in the directory relative to the
# current file and in the current working directory.
# We don't have an include dir list, like make(1) does.
if (!-f "$dirname/$includefile") {
$dirname = $current_dir;
}
if (!-f "$dirname/$includefile") {
$line->log_error("Cannot read $dirname/$includefile.");
} else {
$line->log_info("Including \"$dirname/$includefile\".");
$contents .= readmakefile("$dirname/$includefile", $main_lines, $all_lines, $seen_Makefile_include);
}
}
} elsif ($text =~ regex_varassign) {
my ($varname, $op, $value, $comment) = ($1, $2, $3, $4);
if ($op ne "?=" || !exists($makevar->{$varname})) {
$makevar->{$varname} = $line;
}
$contents .= $text . "\n";
} else {
$contents .= $text . "\n";
}
}
return $contents;
}
#
# Loading data from pkglint-specific files, part 2.
#
# The pkglint author thinks that variables containing lists of things
# should have a name indicating some plural form. Sadly, there are other
# reasons like backwards compatibility and other developer's
# expectations that make changes to most of the following variables
# highly unlikely.
my $get_regex_plurals_value = undef;
sub get_regex_plurals() {
if (defined($get_regex_plurals_value)) {
return $get_regex_plurals_value;
}
my @plurals_ok = qw(
.*S
.*LIST
.*_AWK
.*_ENV
.*_REQD
.*_SED
.*_SKIP
BUILDLINK_LDADD
BUILDLINK_RECOMMENDED
COMMENT
EXTRACT_ONLY
GENERATE_PLIST
PLIST_CAT
PLIST_PRE
PREPEND_PATH
);
my @plurals_missing_an_s = qw(
.*_OVERRIDE
.*_PREREQ
.*_SRC
.*_SUBST
.*_TARGET
.*_TMPL
BUILDLINK_DEPMETHOD
BUILDLINK_TRANSFORM
EVAL_PREFIX
INTERACTIVE_STAGE
LICENSE
MASTER_SITE_.*
MASTER_SORT_REGEX
NOT_FOR_COMPILER
NOT_FOR_PLATFORM
ONLY_FOR_COMPILER
ONLY_FOR_PLATFORM
PERL5_PACKLIST
PKG_FAIL_REASON
PKG_SKIP_REASON
);
my @plurals_reluctantly_accepted = qw(
CRYPTO
FIX_RPATH
PYTHON_VERSIONS_INCOMPATIBLE
REPLACE_INTERPRETER
REPLACE_PERL
REPLACE_RUBY
RESTRICTED
SITES_.*
TOOLS_ALIASES\.*
TOOLS_BROKEN
TOOLS_CREATE
TOOLS_GNU_MISSING
TOOLS_NOOP
);
my $plurals = join("|",
@plurals_ok,
@plurals_missing_an_s,
@plurals_reluctantly_accepted
);
$get_regex_plurals_value = qr"^(?:${plurals})$";
return $get_regex_plurals_value;
}
my $get_tool_names_value = undef;
sub get_tool_names() {
if (defined($get_tool_names_value)) {
return $get_tool_names_value;
}
my $tools = {};
foreach my $file (qw(autoconf automake defaults ldconfig make replace rpcgen texinfo)) {
my $fname = "${pkgsrcdir}/mk/tools/${file}.mk";
my $lines = load_lines($fname, true);
if (!$lines) {
log_fatal($fname, NO_LINE_NUMBER, "Cannot be read.");
next;
}
foreach my $line (@{$lines}) {
if ($line->text =~ regex_varassign) {
my ($varname, undef, $value, undef) = ($1, $2, $3, $4);
if ($varname eq "TOOLS_CREATE" && $value =~ qr"^([-\w.]+)$") {
$tools->{$value} = true;
} elsif ($varname =~ qr"^(?:TOOLS_PATH|_TOOLS_DEPMETHOD|_TOOLS_VARNAME)\.([-\w.]+|\[)$") {
$tools->{$1} = true;
} elsif ($varname eq "_TOOLS.x11-clients") {
foreach my $tool (split(qr"\s+", $value)) {
$tools->{$tool} = true;
}
}
}
}
}
log_debug(NO_FILE, NO_LINE_NUMBER, "Known tools: ".join(" ", sort(keys(%{$tools}))));
$get_tool_names_value = $tools;
return $get_tool_names_value;
}
#
# Procedures to check a single line, part 3.
#
sub checkline_basic_vartype($$$$$) {
my ($line, $varname, $type, $value, $comment) = @_;
my ($value_novar);
$value_novar = $value;
while ($value_novar =~ s/\$\{[^{}]*\}//g) {
}
if ($type eq "Category") {
my $allowed_categories = join("|", qw(
archivers audio
benchmarks biology
cad chat chinese comms converters cross crosspkgtools
databases devel
editors emulators
finance fonts
games geography gnome gnustep graphics
ham
inputmethod
japanese java
kde korean
lang linux
mail math mbone meta-pkgs misc multimedia
net news
packages parallel perl5 pkgtools plan9 print python
ruby
security shells sysutils
tcl textproc time tk
windowmaker wm www
x11 xmms
));
if ($value !~ qr"^(?:${allowed_categories})$") {
$line->log_error("Invalid category \"${value}\".");
}
} elsif ($type eq "Comment") {
if ($value =~ qr"^(a|an)\s+"i) {
$line->log_warning("COMMENT should not begin with '$1'.");
}
if ($value =~ qr"^[a-z]") {
$line->log_warning("COMMENT should start with a capital letter.");
}
if ($value =~ qr"\.$") {
$line->log_warning("COMMENT should not end with a period.");
}
if (length($value) > 70) {
$line->log_warning("COMMENT should not be longer than 70 characters.");
}
} elsif ($type eq "Dependency") {
if ($value eq $value_novar && $value !~ qr"^[-*+.0-9<=>\@A-Z_a-z\[\]]+$") {
$line->log_warning("\"${value}\" is not a valid dependency.");
}
} elsif ($type eq "DependencyWithPath") {
if ($value =~ regex_unresolved) {
# don't even try to check anything
} elsif ($value =~ qr":\.\./\.\./([^/]+)/([^/]+)$") {
my ($cat, $pkg) = ($1, $2);
if (!$is_wip && $cat eq "wip") {
$line->log_error("A pkgsrc package must not depend on any outside package.");
}
if (!-d "${pkgsrcdir}/${cat}/${pkg}") {
$line->log_error("The package ${cat}/${pkg} does not exist.");
}
if ($pkg eq "msgfmt" || $pkg eq "gettext") {
$line->log_warning("Please use BUILD_USES_MSGFMT=yes instead of this dependency.");
} elsif ($pkg =~ qr"^perl\d+") {
$line->log_warning("Please use USE_TOOLS+=perl:run instead of this dependency.");
} elsif ($pkg eq "gmake") {
$line->log_warning("Please use USE_TOOLS+=gmake instead of this dependency.");
} elsif ($pkg =~ qr"^([-a-zA-Z0-9]+)-dirs[-><=]+(.*)$") {
my ($dirs, $version) = ($1, $2);
$line->log_warning("Please use USE_DIRS+=${dirs}-${version} instead of this dependency.");
}
} elsif ($value =~ qr":\.\./[^/]+$") {
$line->log_warning("Dependencies should have the form \"../../category/package\".");
} else {
$line->log_warning("Unknown dependency format.");
}
} elsif ($type eq "DistSuffix") {
if ($value eq ".tar.gz") {
$line->log_note("\"${varname} is \".tar.gz\" by default, so this definition may be redundant.");
}
} elsif ($type eq "Filename") {
if ($value_novar !~ qr"^[-0-9A-Za-z._~+%]*$") {
$line->log_warning("\"${value}\" is not a valid filename.");
}
} elsif ($type eq "Filemask") {
if ($value_novar !~ qr"^[-0-9A-Za-z._~+%*?]*$") {
$line->log_warning("\"${value}\" is not a valid filename mask.");
}
} elsif ($type eq "Mail_Address") {
if ($value =~ qr"^([-\w\d_.]+)\@([-\w\d.]+)$") {
my (undef, $domain) = ($1, $2);
if ($domain =~ qr"^NetBSD.org"i && $domain ne "NetBSD.org") {
$line->log_warning("Please write NetBSD.org instead of ${domain}.");
}
} else {
$line->log_warning("\"${value}\" is not a valid mail address.");
}
} elsif ($type eq "Option") {
if ($value_novar !~ qr"^-?[a-z][-0-9a-z]*$") {
$line->log_warning("\"${value}\" is not a valid option name.");
}
} elsif ($type eq "Pathname") {
if ($value_novar !~ qr"^[-0-9A-Za-z._~+%/]*$") {
$line->log_warning("\"${value}\" is not a valid pathname.");
}
} elsif ($type eq "Pathmask") {
if ($value_novar !~ qr"^[-0-9A-Za-z._~+%*?/]*$") {
$line->log_warning("\"${value}\" is not a valid pathname mask.");
}
} elsif ($type eq "Perl5Packlist") {
if ($value ne $value_novar) {
$line->log_warning("${varname} should not depend on other variables.");
}
} elsif ($type eq "PkgName") {
if ($value eq $value_novar && $value !~ regex_pkgname) {
$line->log_warning("\"${value}\" is not a valid package name. A valid package name has the form packagename-version, where version consists only of digits, letters and dots.");
}
} elsif ($type eq "PkgRevision") {
if ($value !~ qr"^\d+$") {
$line->log_warning("\"${value}\" is not a valid Integer.");
}
if ($line->file !~ qr"(?:^|/)Makefile$") {
$line->log_error("${varname} must not be set outside the package Makefile.");
}
} elsif ($type eq "PlatformTuple") {
my $part = qr"(?:\[[^\]]+\]|[^-\[])+";
if ($value =~ qr"^(${part})-(${part})-(${part})$") {
my ($opsys, $os_version, $arch) = ($1, $2, $3);
if ($opsys !~ qr"^(?:\*|Darwin|DragonFly|FreeBSD|Interix|Linux|NetBSD|OpenBSD|SunOS|IRIX)$") {
$line->log_warning("Unknown operating system: ${opsys}");
}
# no check for $os_version
if ($arch !~ qr"^(?:\*|i386|alpha|amd64|arc|arm|arm32|cobalt|convex|dreamcast|hpcmips|hpcsh|hppa|ia64|m68k|m88k|mips|mips64|mipsel|mipseb|mipsn32|ns32k|pc532|pmax|powerpc|rs6000|s390|sparc|sparc64|vax|x86_64)$") {
$line->log_warning("Unknown hardware architecture: ${arch}");
}
} else {
$line->log_warning("\"${value}\" is not a valid platform tuple.");
}
} elsif ($type eq "Readonly") {
$line->log_error("\"${varname}\" is a read-only variable and therefore must not be modified.");
} elsif ($type eq "RelativePkgDir") {
if ($value !~ qr"^\.\./\.\./[^/]+/[^/]+$") {
$line->log_warning("\"${value}\" is not a valid relative package directory.");
}
} elsif ($type eq "ShellWord") {
if ($value =~ qr"^[\w_]+=(([\"']?)\$\{([\w_]+)\}\2)$") {
my ($vexpr, undef, $vname) = ($1, $2, $3);
# XXX: The following heuristics are too simple (example: OPSYS is not a plural).
my $mod = ($vname =~ get_regex_plurals()) ? ":M*:Q" : ":Q";
$line->log_warning("Please use \${${vname}${mod}} instead of ${vexpr}.");
} elsif ($value =~ qr"^[\w_]+=(\$\{([\w_]+):Q\})$") {
my ($vexpr, $vname) = ($1, $2);
# XXX: The following heuristics are too simple (example: OPSYS is not a plural).
if ($vname =~ get_regex_plurals()) {
$line->log_warning("Please use \${${vname}:M*:Q} instead of ${vexpr}.");
}
} elsif ($value ne "" && $value !~ qr"^${regex_shellword}$") {
$line->log_warning("Invalid shell word \"${value}\".");
}
} elsif ($type eq "Stage") {
if ($value !~ qr"^(?:pre|do|post)-(?:extract|patch|configure|build|install)$") {
$line->log_warning("Invalid stage name. Use one of {pre,do,post}-{extract,patch,configure,build,install}.");
}
} elsif ($type eq "Tool") {
if ($value =~ qr"^([-\w]+|\[)(?::(\w+))?$") {
my ($toolname, $tooldep) = ($1, $2);
if (!exists(get_tool_names()->{$toolname})) {
$line->log_error("Unknown tool \"${toolname}\".");
}
if (defined($tooldep) && $tooldep !~ qr"^(?:build|pkgsrc|run)$") {
$line->log_error("Unknown tool dependency \"${tooldep}\".");
}
} else {
$line->log_error("Invalid tool syntax: \"${value}\".");
}
} elsif ($type eq "URL") {
if ($value =~ qr"\$\{(MASTER_SITE_.*):=(.*)\}$") {
my ($name, $subdir) = ($1, $2);
if (!exists(get_dist_sites_names()->{$name})) {
$line->log_error("${name} does not exist.");
}
if ($subdir !~ qr"/$") {
$line->log_error("The subdirectory in ${name} must end with a slash.");
}
} elsif ($value =~ regex_unresolved) {
# No further checks
} elsif ($value =~ qr"^(?:http://|ftp://)[-0-9A-Za-z.]+(?::\d+)?/~?([-%&+,./0-9:=?\@A-Z_a-z]|\\#)*?$") {
my $sites = get_dist_sites();
foreach my $site (keys(%{$sites})) {
if (index($value, $site) == 0) {
my $subdir = substr($value, length($site));
$line->log_warning(sprintf("Please use \${%s:=%s} instead of \"%s\".", $sites->{$site}, $subdir, $value));
last;
}
}
} elsif ($value eq "" && defined($comment) && $comment =~ qr"^#") {
# Ok
} else {
$line->log_warning("\"${value}\" is not a valid URL.");
}
} elsif ($type eq "Userdefined") {
$line->log_error("\"${varname}\" may only be set by the user, not the package.");
} elsif ($type eq "Varname") {
if ($value ne "" && $value_novar eq "") {
# The value of another variable
} elsif ($value_novar !~ qr"^[A-Z_][0-9A-Z_]*(?:[.].*)?$") {
$line->log_warning("\"${value}\" is not a valid variable name.");
}
} elsif ($type eq "WrkdirSubdirectory") {
# TODO: check for ${WRKDIR}/${DISTNAME}/foo
} elsif ($type eq "WrksrcSubdirectory") {
if ($value =~ qr"^(\$\{WRKSRC\}(?:/|$))") {
my ($prefix) = ($1);
# TODO: uncomment after 2005Q4
#$line->log_note("The \"${prefix}\" prefix is not needed here.");
} elsif ($value ne "" && $value_novar eq "") {
# The value of another variable
} elsif ($value_novar !~ qr"^(?:\.|[0-9A-Za-z][-0-9A-Za-z._/+]*)$") {
$line->log_warning("\"${value}\" is not a valid subdirectory of \${WRKSRC}.");
}
} elsif ($type eq "Yes") {
if ($value !~ qr"^(?:YES|yes)(?:\s+#.*)?$") {
$line->log_warning("${varname} should be set to YES or yes.");
}
} elsif ($type eq "YesNo") {
if ($value !~ qr"^(?:YES|yes|NO|no)(?:\s+#.*)?$") {
$line->log_warning("${varname} should be set to YES, yes, NO, or no.");
}
} elsif ($type =~ qr"^\{\s*(.*?)\s*\}$") {
my ($values) = ($1);
my @enum = split(qr"\s+", $values);
my $found = false;
foreach my $v (@enum) {
if ($v eq $value) {
$found = true;
}
}
if (!$found) {
$line->log_warning("\"${value}\" is not valid for ${varname}. Use one of ${type} instead.");
}
} else {
$line->log_fatal("Type ${type} unknown.");
}
}
sub checkline_Makefile_vartype($$) {
my ($line, $vartypes) = @_;
if ($line->text =~ regex_varassign) {
my ($varname, $op, $value, $comment) = ($1, $2, $3, $4);
my $varbase = ($varname =~ qr"(.+?)\..*") ? $1 : $varname;
my $type = exists($vartypes->{$varname}) ? $vartypes->{$varname}
: exists($vartypes->{$varbase}) ? $vartypes->{$varbase}
: undef;
if ($op eq "+=") {
if ($varbase !~ qr"^_" && $varbase !~ get_regex_plurals()) {
$line->log_warning("As ${varname} is modified using \"+=\", its name should indicate plural.");
}
}
if (!defined($type)) {
if ($varname !~ qr"_MK$") {
$line->log_info("[checkline_Makefile_vartype] Unchecked variable ${varname}.");
}
} elsif ($type =~ qr"^List(!?)(\+?)(?: of (.*))?$") {
my ($internal_list, $append_only, $element_type) = ($1 eq "!", $2 eq "+", $3);
my (@words, $rest);
if ($append_only && $op ne "+=" && !($value eq "" && defined($comment) && $comment =~ qr"^#")) {
$line->log_warning("${varname} should be modified using \"+=\".");
}
if ($internal_list) {
@words = split(qr"\s+", $value);
$rest = "";
} else {
@words = ();
$rest = $value;
while ($rest =~ s/^$regex_shellword//) {
my ($word) = ($1);
last if ($word =~ qr"^#");
push(@words, $1);
}
}
foreach my $word (@words) {
if (defined($element_type)) {
checkline_basic_vartype($line, $varname, $element_type, $word, $comment);
}
}
if ($rest !~ qr"^\s*$") {
$line->log_warning("Invalid shell word \"${value}\" at the end.");
}
} else {
checkline_basic_vartype($line, $varname, $type, $value, $comment);
}
}
}
#
# Procedures to check an array of lines, part 3.
#
my $checklines_Makefile_varuse_map = undef;
sub checklines_Makefile_varuse($) {
my ($lines) = @_;
# Check variable name quoting
foreach my $line (@{$lines}) {
if ($line->text =~ qr"^[^#]*[^\$]\$(\w+)") {
my ($varname) = ($1);
$line->log_warning("\$$varname is ambiguous. Use \${$varname} if you mean a Makefile variable or \$\$$varname if you mean a shell variable.");
}
}
return if (!$opt_warn_types);
# Load variable type definitions
if (!defined($checklines_Makefile_varuse_map)) {
$checklines_Makefile_varuse_map = load_make_vars_typemap();
}
return if (!$checklines_Makefile_varuse_map);
# Check variable types
foreach my $line (@{$lines}) {
checkline_Makefile_vartype($line, $checklines_Makefile_varuse_map);
}
}
sub checklines_deprecated_variables($) {
my ($lines) = @_;
my ($fname) = (conf_datadir."/deprecated.map");
my ($deprecated, $varnames, $varuse_regex, %vars);
if (!($deprecated = load_file($fname))) {
log_error($fname, NO_LINE_NUMBER, "Cannot be read.");
return;
}
%vars = ();
foreach my $line (@{$deprecated}) {
if ($line->text =~ qr"^#" || $line->text =~ qr"^\s*$") {
next;
} elsif ($line->text =~ qr"^(\S+)\s+(.*)$") {
$vars{$1} = $2;
} else {
$line->log_fatal("Unknown line format.");
}
}
$varnames = join("|", sort(keys(%vars)));
$varuse_regex = qr"\$\{(${varnames})[:}]";
foreach my $line (@{$lines}) {
if ($line->text =~ regex_varassign) {
my ($varname, undef, $value) = ($1, $2, $3);
if (exists($vars{$varname})) {
$line->log_warning("${varname} is deprecated. $vars{$varname}");
}
if ($value =~ $varuse_regex) {
my ($varuse_name) = ($1);
$line->log_warning("Use of ${varuse_name} is deprecated. $vars{$varuse_name}");
}
}
}
}
sub checklines_direct_tools($) {
my ($subr) = "checklines_direct_tools";
my ($lines) = @_;
return if (!$opt_warn_directcmd);
log_info(NO_FILE, NO_LINE_NUMBER, "Checking direct use of tool names.");
my @tools = qw(
awk
basename
cat chgrp chmod chown cmp cp cut
digest dirname
echo egrep
false file find
gmake grep gtar gzcat
id ident install
ldconfig ln
md5 mkdir mtree mv
patch pax pkg_add pkg_create pkg_delete pkg_info
rm rmdir sed setenv sh sort su
tail test touch tr true type
wc
xmkmf
);
my @cmd_tools = qw(
file gunzip gzip);
my $tools = join("|", @tools, @cmd_tools);
my $regex_tools = qr"${tools}";
my $regex_tools_with_context = qr"(?:^|\s|/)($regex_tools)(?:\s|$)";
my @ok_vars = qw(
BUILDLINK_TRANSFORM BUILD_DEPENDS
COMMENT CONFLICTS
DEPENDS DISTNAME
EXTRACT_SUFX EXTRACT_USING
INTERACTIVE_STAGE
MANSOURCEPATH MASTER_SITES
PKGNAME PKGSRC_USE_TOOLS PKG_FAIL_REASON
SUBST_CLASSES
SUBST_MESSAGE\\..*
.*_TARGET
USE_TOOLS);
my @valid_shellcmds = (
qr"for file in",
qr"\./${regex_tools}\b",
qr"(?:\./Build|\$\{(?:BJAM_CMD|JAM_COMMAND)\})\s+(?:install|test)",
qr"\$\{(?:GMAKE|MAKE_PROGRAM)\}\s+(?:install)",
qr"\"[^\"]*${regex_tools}[^\"]*\"",
qr"\'[^\']*${regex_tools}[^\']*\'",
qr"#.*",
);
my %toolvar = ();
foreach my $tool (@tools) {
$toolvar{$tool} = uc($tool);
}
foreach my $tool (@cmd_tools) {
$toolvar{$tool} = uc($tool)."_CMD";
}
my $ok_vars = join("|", @ok_vars);
my $regex_ok_vars = qr"^(?:${ok_vars})$";
my $valid_shellcmds = join("|", @valid_shellcmds);
my $regex_valid_shellcmds = qr"(?:${valid_shellcmds})";
log_debug(NO_FILE, NO_LINE_NUMBER, "[${subr}] regex_tools=${regex_tools}");
log_debug(NO_FILE, NO_LINE_NUMBER, "[${subr}] regex_ok_vars=${regex_ok_vars}");
log_debug(NO_FILE, NO_LINE_NUMBER, "[${subr}] regex_valid_shellcmds=${regex_valid_shellcmds}");
foreach my $line (@{$lines}) {
my $text = $line->text;
next unless ($text =~ $regex_tools_with_context);
my ($tool) = ($1);
# skip comments
if ($text =~ qr"^#") {
# process variable assignments
} elsif ($text =~ regex_varassign) {
my ($varname, undef, $varvalue) = ($1, $2, $3);
if ($varname =~ $regex_ok_vars) {
$line->log_info("Legitimate direct use of tool \"${tool}\" in variable ${varname}.");
} elsif ($varvalue =~ $regex_tools_with_context) {
$line->log_warning("Possible direct use of tool \"${tool}\" in variable ${varname}. Please use \$\{$toolvar{$tool}\} instead.");
} else {
# the tool name has appeared in the comment
}
# process shell commands
} elsif ($text =~ regex_shellcmd) {
my ($shellcmd) = ($1);
my ($short_shellcmd, $remaining_shellcmd) = ($shellcmd, $shellcmd);
# Remove known legitimate uses from the string
$remaining_shellcmd =~ s,$regex_valid_shellcmds,,g;
# As shell commands tend to become long, extract
# the relevant part only.
if ($short_shellcmd =~ qr"(.{0,15})\Q${tool}\E(.{0,15})") {
my ($before, $after) = ($1, $2);
if (length($before) == 15) {
$before = "...${before}";
}
if (length($after) == 15) {
$after = "${after}...";
}
$short_shellcmd = "${before}${tool}${after}";
}
if ($remaining_shellcmd =~ $regex_tools_with_context) {
$line->log_warning("Possible direct use of tool \"${tool}\" in shell command \"${short_shellcmd}\". Please use \$\{$toolvar{$tool}\} instead.");
} else {
$line->log_info("Legitimate direct use of tool \"${tool}\" in shell command \"${short_shellcmd}\".");
}
# skip processing directives
} elsif ($text =~ qr"^\.") {
# skip dependency specifications
} elsif ($text =~ qr"^([-\w.]+):") {
} else {
$line->log_error("[internal:checklines_direct_tools] unknown line format.");
}
}
}
sub checklines_package_Makefile_varorder($) {
my ($lines) = @_;
# TODO: Add support for optional sections with non-optional variables.
use constant once => 0;
use constant optional => 1;
use constant many => 2;
my (@sections) = (
[ "initial comments", once,
[
]
],
[ "DISTNAME", once,
[
[ "DISTNAME", once ],
[ "PKGNAME", optional ],
[ "PKGREVISION", optional ],
[ "SVR4_PKGNAME", optional ],
[ "CATEGORIES", once ],
[ "MASTER_SITES", once ],
[ "DYNAMIC_MASTER_SITES", optional ],
[ "MASTER_SITE_SUBDIR", optional ],
[ "EXTRACT_SUFX", optional ],
[ "DISTFILES", optional ],
# The following are questionable.
# [ "NOT_FOR_PLATFORM", optional ],
# [ "ONLY_FOR_PLATFORM", optional ],
# [ "NO_BIN_ON_FTP", optional ],
# [ "NO_SRC_ON_FTP", optional ],
# [ "NO_BIN_ON_CDROM", optional ],
# [ "NO_SRC_ON_CDROM", optional ],
]
],
[ "PATCH_SITES", optional,
[
[ "PATCH_SITES", optional ], # or once?
[ "PATCH_SITE_SUBDIR", optional ],
[ "PATCHFILES", optional ], # or once?
[ "PATCH_DIST_ARGS", optional ],
[ "PATCH_DIST_STRIP", optional ],
[ "PATCH_DIST_CAT", optional ],
]
],
[ "MAINTAINER", once,
[
[ "MAINTAINER", once ],
[ "HOMEPAGE", optional ],
[ "COMMENT", once ],
]
],
[ "DEPENDS", optional,
[
[ "BUILD_DEPENDS", many ],
[ "DEPENDS", many ],
]
]
);
if (!defined($seen_Makefile_common) || $seen_Makefile_common) {
return;
}
my ($lineno, $sectindex, $varindex) = (0, -1, 0);
my ($next_section, $vars, $below, $below_what) = (true, undef, {}, undef);
# In each iteration, one of the following becomes true:
# - new.lineno > old.lineno
# - new.sectindex > old.sectindex
# - new.sectindex == old.sectindex && new.varindex > old.varindex
# - new.next_section == true && old.next_section == false
while ($lineno <= $#{$lines}) {
my $line = $lines->[$lineno];
my $text = $line->text;
if ($next_section) {
$next_section = false;
$sectindex++;
last if ($sectindex > $#sections);
$vars = $sections[$sectindex]->[2];
$varindex = 0;
}
if ($text =~ qr"^#") {
$lineno++;
} elsif ($text =~ regex_varassign) {
my ($varname, $op, $value, $comment) = ($1, $2, $3, $4);
if (exists($below->{$varname})) {
if (defined($below->{$varname})) {
$line->log_warning("${varname} appears too late. Please put it below $below->{$varname}.");
} else {
$line->log_warning("${varname} appears too late. It should be the very first definition.");
}
$lineno++;
next;
}
while ($varindex <= $#{$vars} && $varname ne $vars->[$varindex]->[0] && $vars->[$varindex]->[1] != once) {
$below->{$vars->[$varindex]->[0]} = $below_what;
$varindex++;
}
if ($varindex > $#{$vars}) {
if ($sections[$sectindex]->[1] != optional) {
$line->log_warning("Empty line expected.");
}
$next_section = true;
} elsif ($varname ne $vars->[$varindex]->[0]) {
$line->log_warning(sprintf("Expected %s, but found %s.", $vars->[$varindex]->[0], $varname));
$lineno++;
} else {
if ($vars->[$varindex]->[1] != many) {
$below->{$vars->[$varindex]->[0]} = $below_what;
$varindex++;
}
$lineno++;
}
$below_what = $varname;
} else {
while ($varindex <= $#{$vars}) {
if ($vars->[$varindex]->[1] == once) {
$line->log_warning(sprintf("%s should be set here.", $vars->[$varindex]->[0]));
}
$below->{$vars->[$varindex]->[0]} = $below_what;
$varindex++;
}
$next_section = true;
if ($text eq "") {
$below_what = "the previous empty line";
$lineno++;
}
}
}
}
# This subroutine contains "local" checks that can be made looking only
# at a single line at a time. The other checks are in
# checkfile_package_Makefile.
sub checklines_package_Makefile($) {
my ($lines) = @_;
foreach my $line (@{$lines}) {
my $text = $line->text;
if ($line->lines eq "1") {
checkline_rcsid_regex($line, qr"#\s+", "# ");
}
checkline_trailing_whitespace($line);
if ($text =~ /^\040{8}/) {
$line->log_warning("Use tab (not spaces) to make indentation.");
}
if ($text =~ qr"\$\{WRKSRC\}/\.\./") {
$line->log_error("Using \"\${WRKSRC}/..\" is conceptually wrong. Use a combination of WRKSRC, CONFIGURE_DIRS and BUILD_DIRS instead.");
}
if ($text =~ regex_varassign) {
my ($varname, $op, $value, $comment) = ($1, $2, $3, $4);
if ($varname =~ qr"^_") {
$line->log_error("Variable names starting with an underscore are reserved for internal pkgsrc use.");
}
if ($varname eq "PERL5_PACKLIST" && defined($pkgname) && $pkgname =~ qr"^p5-(.*)-[0-9].*") {
my ($guess) = ($1);
$guess =~ s/-/\//g;
$guess = "auto/${guess}/.packlist";
my ($ucvalue, $ucguess) = (uc($value), uc($guess));
if ($ucvalue ne $ucguess && $ucvalue ne "\${PERL5_SITEARCH\}/${ucguess}") {
$line->log_warning("Unusual value for PERL5_PACKLIST -- \"${guess}\" expected.");
}
}
if ($varname eq "SVR4_PKGNAME") {
if ($value =~ regex_unresolved) {
$line->log_error("SVR4_PKGNAME must not contain references to other variables.");
} elsif (length($value) > 5) {
$line->log_error("SVR4_PKGNAME must not be longer than 5 characters.");
}
}
if (defined($comment) && $comment eq "# defined" && $varname !~ qr".*(?:_MK|_COMMON)$") {
$line->log_warning("Please use \"# empty\", \"# none\" or \"yes\" instead of \"# defined\".");
}
if ($varname =~ qr"^NO_(.*)_ON_(.*)$") {
my ($what, $where) = ($1, $2);
if (($what ne "SRC" && $what ne "BIN") || ($where ne "FTP" && $where ne "CDROM")) {
$line->log_error("Misspelled variable: Valid names are USE_{BIN,SRC}_ON_{FTP,CDROM}.");
}
}
if ($value =~ qr"\$\{(PKGNAME|PKGVERSION)[:\}]") {
my ($pkgvarname) = ($1);
if ($varname =~ qr"^PKG_.*_REASON$") {
# ok
} elsif ($varname =~ qr"^(?:DIST_SUBDIR|WRKSRC)$") {
$line->log_warning("${pkgvarname} should not be used in ${varname}, as it sometimes includes the PKGREVISION. Please use ${pkgvarname}_NOREV instead.");
} else {
$line->log_info("Use of PKGNAME in ${varname}.");
}
}
} elsif ($text =~ regex_shellcmd) {
my ($shellcmd) = ($1);
# TODO: parse the shell command correctly
if ($shellcmd =~ qr"\$\{MKDIR\}[^&;|]*\$\{PREFIX\}/[/0-9a-zA-Z\${}]*") {
$line->log_warning("Please use one of the INSTALL_*_DIR commands instead of MKDIR.");
}
if ($shellcmd =~ qr"\$\{INSTALL\}([^&;|]*)") {
my ($args) = ($1);
if ($args =~ qr"-d" && $args !~ qr"-[ogm]") {
$line->log_warning("Please use one of the INSTALL_*_DIR commands instead of \${INSTALL}${args}.");
}
}
if ($shellcmd =~ qr"^\@*-(.*(MKDIR|INSTALL.*-d|INSTALL_.*_DIR).*)") {
my ($mkdir_cmd) = ($1);
$line->log_note("You don't need to use \"-\" before ${mkdir_cmd}.");
}
$line->log_debug("ShellCmd: $shellcmd");
while ($shellcmd =~ s/^$regex_shellword//) {
$line->log_debug("ShellWord: $1");
}
if ($shellcmd !~ qr"^\s*$") {
$line->log_warning("Invalid shell word \"${shellcmd}\".");
}
}
}
checklines_trailing_empty_lines($lines);
}
#
# Miscellaneous procedures.
#
sub expand_variable($$) {
my ($whole, $varname) = @_;
my ($value, $re);
$re = qr"\n${varname}([+:?]?)=[ \t]*([^\n#]*)";
$value = undef;
while ($whole =~ m/$re/g) {
my ($op, $val) = ($1, $2);
if ($op ne "?" || !defined($value)) {
$value = $val;
}
}
if (!defined($value)) {
return undef;
}
$value =~ s,\$\{\.CURDIR\},.,g;
$value =~ s,\$\{PKGSRCDIR\},../..,g;
$value =~ s,\$\{PHPPKGSRCDIR\},../../lang/php5,g;
if (defined($pkgdir)) {
$value =~ s,\$\{PKGDIR\},$pkgdir,g;
}
if ($value =~ regex_unresolved) {
log_info(NO_FILE, NO_LINE_NUMBER, "[expand_variable] The variable ${varname} could not be resolved completely. Its value is \"${value}\".");
}
return $value;
}
sub set_default_value($$) {
my ($varref, $value) = @_;
if (!defined(${$varref}) || ${$varref} =~ regex_unresolved) {
${$varref} = $value;
}
}
#
# Loading data from package-specific files, part 2.
#
sub load_package_Makefile($$$$) {
my ($subr) = "load_package_Makefile";
my ($fname, $ref_whole, $ref_main_lines, $ref_all_lines) = @_;
my ($whole, $main_lines, $all_lines);
log_info($fname, NO_LINE_NUMBER, "Checking package Makefile.");
$whole = readmakefile($fname, $main_lines = [], $all_lines = [], {});
if (!$whole) {
log_error($fname, NO_LINE_NUMBER, "Cannot be read.");
return false;
}
if ($opt_dumpmakefile) {
print("OK: whole Makefile (with all included files) follows:\n");
foreach my $line (@{$all_lines}) {
printf("%s\n", $line->to_string());
}
}
# HACK
if ($whole =~ qr"\nPHPEXT_MK" && $whole !~ qr"\nUSE_PHP_EXT_PATCHES") {
log_info($fname, NO_LINE_NUMBER, "[hack] USE_PHP_EXT_PATCHES");
$whole =~ s,\nPATCHDIR=.*PHPPKGSRCDIR.*,,;
$hack_php_patches = true;
}
# HACK
if ($whole =~ qr"\nPECL_VERSION") {
log_info($fname, NO_LINE_NUMBER, "[hack] PECL_VERSION");
$whole =~ s,\nDISTINFO_FILE=.*PHPPKGSRCDIR.*,,;
}
$pkgdir = expand_variable($whole, "PKGDIR");
set_default_value(\$pkgdir, ".");
$distinfo_file = expand_variable($whole, "DISTINFO_FILE");
set_default_value(\$distinfo_file, "distinfo");
$filesdir = expand_variable($whole, "FILESDIR");
set_default_value(\$filesdir, "files");
$patchdir = expand_variable($whole, "PATCHDIR");
set_default_value(\$patchdir, "patches");
log_info(NO_FILE, NO_LINE_NUMBER, "[${subr}] DISTINFO_FILE=$distinfo_file");
log_info(NO_FILE, NO_LINE_NUMBER, "[${subr}] FILESDIR=$filesdir");
log_info(NO_FILE, NO_LINE_NUMBER, "[${subr}] PATCHDIR=$patchdir");
log_info(NO_FILE, NO_LINE_NUMBER, "[${subr}] PKGDIR=$pkgdir");
${$ref_whole} = $whole;
${$ref_main_lines} = $main_lines;
${$ref_all_lines} = $all_lines;
return true;
}
#
# Procedures to check a file, part 3.
#
sub checkfile_ALTERNATIVES($) {
my ($fname) = @_;
my ($lines);
log_info($fname, NO_LINE_NUMBER, "[checkfile_ALTERNATIVES]");
checkperms($fname);
if (!($lines = load_file($fname))) {
log_error($fname, NO_LINE_NUMBER, "Cannot be read.");
return;
}
}
sub checkfile_INSTALL($) {
my ($fname) = @_;
my ($lines);
log_info($fname, NO_LINE_NUMBER, "[checkfile_INSTALL]");
checkperms($fname);
if (!($lines = load_file($fname))) {
log_error($fname, NO_LINE_NUMBER, "Cannot be read.");
return;
}
}
sub checkfile_mk($) {
my ($fname) = @_;
my ($lines);
log_info($fname, NO_LINE_NUMBER, "[checkfile_mk]");
checkperms($fname);
if (!($lines = load_lines($fname, true))) {
log_error($fname, NO_LINE_NUMBER, "Cannot be read.");
return;
}
checklines_deprecated_variables($lines);
checklines_direct_tools($lines);
checklines_package_Makefile($lines);
checklines_Makefile_varuse($lines);
}
sub checkfile_package_Makefile($$$$) {
my ($fname, $whole, $main_lines, $lines) = @_;
my ($distname, $category, $distfiles);
log_info($fname, NO_LINE_NUMBER, "[checkfile_package_Makefile]");
checkperms($fname);
$category = basename(dirname(Cwd::abs_path($current_dir)));
# TODO: "Use \${VARIABLE} instead of \$(VARIABLE)."
checklines_deprecated_variables($main_lines);
if (!exists($makevar->{"PLIST_SRC"})
&& !exists($makevar->{"NO_PKG_REGISTER"})
&& !-f "${current_dir}/$pkgdir/PLIST"
&& !-f "${current_dir}/$pkgdir/PLIST.common") {
log_warning($fname, NO_LINE_NUMBER, "Neither PLIST nor PLIST.common exist, and PLIST_SRC and NO_PKG_REGISTER are unset. Are you sure PLIST handling is ok?");
}
if (exists($makevar->{"NO_CHECKSUM"})) {
if (-f "${current_dir}/${distinfo_file}") {
log_warning("${current_dir}/${distinfo_file}", NO_LINE_NUMBER, "This file should not exist if NO_CHECKSUM is set.");
}
} else {
if (!-f "${current_dir}/${distinfo_file}") {
log_warning("${current_dir}/${distinfo_file}", NO_LINE_NUMBER, "File not found. Please run '".conf_make." makesum'.");
}
}
checklines_direct_tools($main_lines);
if ($whole =~ /etc\/rc\.d/) {
log_warning($fname, NO_LINE_NUMBER, "Use the RCD_SCRIPTS mechanism to install rc.d scripts automatically to \${RCD_SCRIPTS_EXAMPLEDIR}.");
}
if (exists($makevar->{"MASTER_SITES"})) {
if (exists($makevar->{"DYNAMIC_MASTER_SITES"})) {
$makevar->{"MASTER_SITES"}->log_warning("MASTER_SITES and ...");
$makevar->{"DYNAMIC_MASTER_SITES"}->log_warning("... DYNAMIC_MASTER_SITES conflict.");
}
} else {
if (!exists($makevar->{"DYNAMIC_MASTER_SITES"})) {
log_warning($fname, NO_LINE_NUMBER, "Neither MASTER_SITES nor DYNAMIC_MASTER_SITES found.");
}
}
# check DISTFILES and related items.
$distname = expand_variable($whole, "DISTNAME");
$pkgname = expand_variable($whole, "PKGNAME");
$distfiles = expand_variable($whole, "DISTFILES");
if (defined($pkgname) && defined($distname) && ($pkgname eq $distname || $pkgname eq "\${DISTNAME}")) {
log_warning($fname, NO_LINE_NUMBER, "PKGNAME is \${DISTNAME} by default. You don't need to define PKGNAME.");
}
if (!defined($pkgname) && defined($distname) && $distname !~ regex_unresolved && $distname !~ regex_pkgname) {
log_warning($fname, NO_LINE_NUMBER, "As DISTNAME ist not a valid package name, please define the PKGNAME explicitly.");
}
if (!defined($pkgname)) {
$pkgname = $distname;
}
if (!exists($makevar->{"COMMENT"})) {
log_warning($fname, NO_LINE_NUMBER, "No COMMENT given.");
}
if (exists($makevar->{"USE_IMAKE"}) && exists($makevar->{"USE_X11"})) {
$makevar->{"USE_IMAKE"}->log_note("USE_IMAKE makes ...");
$makevar->{"USE_X11"}->log_note("... USE_X11 superfluous.");
}
checklines_package_Makefile($main_lines);
# Disabled, as I don't like the current ordering scheme.
#checklines_package_Makefile_varorder($lines);
checklines_Makefile_varuse($main_lines);
}
sub checkfile($) {
my ($fname) = @_;
my ($st, $basename);
log_info($fname, NO_LINE_NUMBER, "[checkfile]");
$basename = basename($fname);
if ($basename =~ qr"^(?:work.*|.*~|.*\.orig|.*\.rej)$") {
if ($opt_import) {
log_error($fname, NO_LINE_NUMBER, "Must be cleaned up before committing the package.");
}
return;
}
if (!($st = lstat($fname))) {
log_error($fname, NO_LINE_NUMBER, "$!");
return;
}
if (S_ISDIR($st->mode)) {
if ($basename eq "files" || $basename eq "patches" || $basename eq "CVS") {
# Ok
} elsif (!is_emptydir($fname)) {
log_warning($fname, NO_LINE_NUMBER, "Unknown directory name.");
}
} elsif (!S_ISREG($st->mode)) {
log_error($fname, NO_LINE_NUMBER, "Only files and directories are allowed in pkgsrc.");
} elsif ($basename eq "ALTERNATIVES") {
$opt_check_ALTERNATIVES and checkfile_ALTERNATIVES($fname);
} elsif ($basename eq "buildlink3.mk") {
$opt_check_bl3 and checkfile_mk($fname);
} elsif ($basename =~ qr"^(?:.*\.mk|Makefile.*)$") {
$opt_check_mk and checkfile_mk($fname);
} elsif ($basename =~ qr"^DESCR") {
$opt_check_DESCR and checkfile_DESCR($fname);
} elsif ($basename =~ qr"^distinfo") {
$opt_check_distinfo and checkfile_distinfo($fname);
} elsif ($basename eq "DEINSTALL" || $basename eq "INSTALL") {
$opt_check_INSTALL and checkfile_INSTALL($fname);
} elsif ($basename =~ qr"^MESSAGE") {
$opt_check_MESSAGE and checkfile_MESSAGE($fname);
} elsif ($basename =~ qr"^patch-[A-Za-z0-9]*$") {
$opt_check_patches and checkfile_patches_patch($fname);
} elsif ($fname =~ qr"(?:^|/)patches/[^/]*$") {
log_warning($fname, NO_LINE_NUMBER, "Patch files should be named \"patch-\", followed by letters and digits only.");
} elsif ($basename =~ qr"^PLIST") {
$opt_check_PLIST and checkfile_PLIST($fname);
} elsif ($basename eq "TODO") {
# Ok
} elsif (!-T $fname) {
log_warning($fname, NO_LINE_NUMBER, "Unexpectedly found a binary file.");
} else {
log_warning($fname, NO_LINE_NUMBER, "Unexpected file found.");
$opt_check_extra and checkfile_extra($fname);
}
}
#
# Procedures to check a directory including the files in it.
#
sub checkdir_root() {
my ($fname) = "${current_dir}/Makefile";
my ($lines, $prev_subdir, @subdirs);
log_info($fname, NO_LINE_NUMBER, "[checkdir_root] Checking pkgsrc root directory.");
if (!($lines = load_file($fname))) {
log_error($fname, NO_LINE_NUMBER, "Cannot be read.");
return;
}
if (0 <= $#{$lines}) {
checkline_rcsid_regex($lines->[0], qr"#\s+", "# ");
}
foreach my $line (@{$lines}) {
if ($line->text =~ qr"^(#?)SUBDIR\s*\+=(\s*)(\S+)\s*(?:#\s*(.*?)\s*|)$") {
my ($comment_flag, $indentation, $subdir, $comment) = ($1, $2, $3, $4);
if ($comment_flag eq "#" && (!defined($comment) || $comment eq "")) {
$line->log_warning("${subdir} commented out without giving a reason.");
}
if ($indentation ne "\t") {
$line->log_warning("Indentation should be a single tab character.");
}
if ($subdir =~ qr"\$" || !-f "${current_dir}/${subdir}/Makefile") {
next;
}
if (defined($prev_subdir) && $subdir eq $prev_subdir) {
$line->log_error("${subdir} must only appear once.");
} elsif (defined($prev_subdir) && $subdir lt $prev_subdir) {
$line->log_warning("${subdir} should come before ${prev_subdir}.");
} else {
# correctly ordered
}
$prev_subdir = $subdir;
if ($comment_flag eq "") {
push(@subdirs, "${current_dir}/${subdir}");
}
}
}
if ($opt_recursive) {
push(@todo_items, @subdirs);
}
}
sub checkdir_category() {
my $fname = "${current_dir}/Makefile";
my ($lines, $lineno);
if (!($lines = load_file($fname))) {
log_error($fname, NO_LINE_NUMBER, "Cannot be read.");
return;
}
$lineno = 0;
# The first line must contain the RCS Id
if ($lineno <= $#{$lines} && checkline_rcsid_regex($lines->[$lineno], qr"#\s+", "# ")) {
$lineno++;
}
# Then, arbitrary comments may follow
while ($lineno <= $#{$lines} && $lines->[$lineno]->text =~ qr"^#") {
$lineno++;
}
# Then we need an empty line
if ($lineno <= $#{$lines} && $lines->[$lineno]->text eq "") {
$lineno++;
} else {
$lines->[$lineno]->log_error("Empty line expected.");
}
# Then comes the COMMENT line
if ($lineno <= $#{$lines} && $lines->[$lineno]->text =~ qr"^COMMENT=\t*(.*)") {
my ($comment) = ($1);
checkline_valid_characters_in_variable($lines->[$lineno], qr"[-\040'(),/0-9A-Za-z]");
$lineno++;
} else {
$lines->[$lineno]->log_error("COMMENT= line expected.");
}
# Then we need an empty line
if ($lineno <= $#{$lines} && $lines->[$lineno]->text eq "") {
$lineno++;
} else {
$lines->[$lineno]->log_error("Empty line expected.");
}
# And now to the most complicated part of the category Makefiles,
# the (hopefully) sorted list of SUBDIRs. The first step is to
# collect the SUBDIRs in the Makefile and in the file system.
my (@f_subdirs, @m_subdirs);
@f_subdirs = sort(get_subdirs($current_dir));
my $prev_subdir = undef;
while ($lineno <= $#{$lines}) {
my $line = $lines->[$lineno];
if ($line->text =~ qr"^(#?)SUBDIR\+=(\s*)(\S+)\s*(?:#\s*(.*?)\s*|)$") {
my ($comment_flag, $indentation, $subdir, $comment) = ($1, $2, $3, $4);
if ($comment_flag eq "#" && (!defined($comment) || $comment eq "")) {
$line->log_warning("${subdir} commented out without giving a reason.");
}
if ($indentation ne "\t") {
$line->log_warning("Indentation should be a single tab character.");
}
if (defined($prev_subdir) && $subdir eq $prev_subdir) {
$line->log_error("${subdir} must only appear once.");
} elsif (defined($prev_subdir) && $subdir lt $prev_subdir) {
$line->log_warning("${subdir} should come before ${prev_subdir}.");
} else {
# correctly ordered
}
push(@m_subdirs, [$subdir, $line, $comment_flag ? false : true]);
$prev_subdir = $subdir;
$lineno++;
} else {
if ($line->text ne "") {
$line->log_error("SUBDIR+= line or empty line expected.");
}
last;
}
}
# To prevent unnecessary warnings about subdirectories that are
# in one list, but not in the other, we generate the sets of
# subdirs of each list.
my (%f_check, %m_check);
foreach my $f (@f_subdirs) { $f_check{$f} = true; }
foreach my $m (@m_subdirs) { $m_check{$m->[0]} = true; }
my ($f_index, $f_atend, $f_neednext, $f_current) = (0, false, true, undef, undef);
my ($m_index, $m_atend, $m_neednext, $m_current) = (0, false, true, undef, undef);
my ($line, $m_recurse);
my (@subdirs);
while (!($m_atend && $f_atend)) {
if (!$m_atend && $m_neednext) {
$m_neednext = false;
if ($m_index > $#m_subdirs) {
$m_atend = true;
$line = $lines->[$lineno];
next;
} else {
$m_current = $m_subdirs[$m_index]->[0];
$line = $m_subdirs[$m_index]->[1];
$m_recurse = $m_subdirs[$m_index]->[2];
$m_index++;
}
}
if (!$f_atend && $f_neednext) {
$f_neednext = false;
if ($f_index > $#f_subdirs) {
$f_atend = true;
next;
} else {
$f_current = $f_subdirs[$f_index++];
}
}
if (!$f_atend && ($m_atend || $f_current lt $m_current)) {
if (!exists($m_check{$f_current})) {
$line->log_error("${f_current} exists in the file system, but not in the Makefile.");
$line->append_before("SUBDIR+=\t${f_current}");
}
$f_neednext = true;
} elsif (!$m_atend && ($f_atend || $m_current lt $f_current)) {
if (!exists($f_check{$m_current})) {
$line->log_error("${m_current} exists in the Makefile, but not in the file system.");
$line->delete();
}
$m_neednext = true;
} else { # $f_current eq $m_current
$f_neednext = true;
$m_neednext = true;
if ($m_recurse) {
push(@subdirs, "${current_dir}/${m_current}");
}
}
}
# the wip category Makefile may have its own targets for generating
# indexes and READMEs. Just skip them.
if ($is_wip) {
while ($lineno <= $#{$lines} - 2) {
$lineno++;
}
}
# Then we need an empty line
if ($lineno <= $#{$lines} && $lines->[$lineno]->text eq "") {
$lineno++;
} else {
$lines->[$lineno]->log_error("Empty line expected.");
}
# And, last but not least, the .include line
my $final_line = ".include \"../mk/bsd.pkg.subdir.mk\"";
if ($lineno <= $#{$lines} && $lines->[$lineno]->text eq $final_line) {
$lineno++;
} else {
$lines->[$lineno]->log_error("Expected this: ${final_line}.");
}
if ($lineno <= $#{$lines}) {
$lines->[$lineno]->log_error("The file should end here.");
}
# If the user has requested automatic fixing and we can do it, we do it.
if ($opt_autofix) {
save_autofix_changes($lines);
}
if ($opt_recursive) {
unshift(@todo_items, @subdirs);
}
}
sub checkdir_package() {
my ($whole, $main_lines, $lines, $have_distinfo, $have_patches);
# Initialize global variables
$makevar = {};
# we need to handle the Makefile first to get some variables
if (!load_package_Makefile("${current_dir}/Makefile", \$whole, \$main_lines, $lines)) {
log_error("${current_dir}/Makefile", NO_LINE_NUMBER, "Cannot be read.");
return;
}
my @files = <${current_dir}/*>;
if ($pkgdir ne ".") {
push(@files, <${current_dir}/${pkgdir}/*>);
}
if ($opt_check_extra) {
push(@files, <${current_dir}/${filesdir}/*>);
}
push(@files, <${current_dir}/${patchdir}/*>);
if ($distinfo_file !~ qr"^(?:\./)?distinfo$") {
push(@files, "${current_dir}/${distinfo_file}");
}
$have_distinfo = false;
$have_patches = false;
foreach my $fname (@files) {
if ($fname eq "${current_dir}/Makefile") {
$opt_check_Makefile and checkfile_package_Makefile($fname, $whole, $main_lines, $lines);
} else {
checkfile($fname);
}
if ($fname =~ qr"/patches/patch-[A-Za-z0-9]*$") {
$have_patches = true;
} elsif ($fname =~ qr"/distinfo$") {
$have_distinfo = true;
}
}
if ($opt_check_distinfo && $opt_check_patches) {
if ($have_patches && ! $have_distinfo) {
log_warning("${current_dir}/$distinfo_file", NO_LINE_NUMBER, "File not found. Please run '".conf_make." makepatchsum'.");
}
}
if (!is_emptydir("${current_dir}/scripts")) {
log_warning("${current_dir}/scripts", NO_LINE_NUMBER, "This directory and its contents are deprecated! Please call the script(s) explicitly from the corresponding target(s) in the pkg's Makefile.");
}
}
#
# Selecting the proper checking procedures for a directory entry.
#
sub checkitem($) {
my ($item) = @_;
my ($st, $is_dir, $is_reg);
if (!($st = lstat($item))) {
log_error($item, NO_LINE_NUMBER, "Does not exist.");
return;
}
$is_dir = S_ISDIR($st->mode);
$is_reg = S_ISREG($st->mode);
if (!$is_reg && !$is_dir) {
log_error($item, NO_LINE_NUMBER, "Must be a file or directory.");
return;
}
# Initialize global variables.
$pkgdir = ".";
$filesdir = "files";
$patchdir = "patches";
$distinfo_file = "distinfo";
$makevar = undef;
$seen_Makefile_common = undef;
$pkgname = undef;
$hack_php_patches = false;
$current_dir = $is_dir ? $item : dirname($item);
$is_wip = !$opt_import && (Cwd::abs_path($current_dir) =~ qr"/wip(?:/|$)");
if (-f "${current_dir}/../../mk/bsd.pkg.mk") {
$pkgsrcdir = "${current_dir}/../..";
if ($is_dir) {
checkdir_package();
}
} elsif (-f "${current_dir}/../mk/bsd.pkg.mk") {
$pkgsrcdir = "${current_dir}/..";
if ($is_dir) {
checkdir_category();
}
} elsif (-f "${current_dir}/mk/bsd.pkg.mk") {
$pkgsrcdir = $current_dir;
if ($is_dir) {
checkdir_root();
}
} else {
log_error($item, NO_LINE_NUMBER, sprintf("Don't know how to check this %s.", ($is_dir) ? "directory" : "file"));
return;
}
if ($is_reg) {
checkfile($item);
}
}
#
# The main program
#
sub main() {
parse_command_line();
@todo_items = (@ARGV != 0) ? @ARGV : (".");
while (@todo_items != 0) {
checkitem(shift(@todo_items));
}
PkgLint::Logging::print_summary_and_exit($opt_quiet);
}
main();
|