summaryrefslogtreecommitdiff
path: root/usr/src/cmd/make/bin/doname.cc
blob: c97e1593a64290edc3d5d874cdebf3920523ac5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2016 RackTop Systems.
 * Copyright (c) 2016 by Delphix. All rights reserved.
 */

/*
 *	doname.c
 *
 *	Figure out which targets are out of date and rebuild them
 */

/*
 * Included files
 */
#include <alloca.h>		/* alloca() */
#include <fcntl.h>
#include <mk/defs.h>
#include <mksh/i18n.h>		/* get_char_semantics_value() */
#include <mksh/macro.h>		/* getvar(), expand_value() */
#include <mksh/misc.h>		/* getmem() */
#include <poll.h>
#include <libintl.h>
#include <signal.h>
#include <stropts.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>	/* uname() */
#include <sys/wait.h>
#include <unistd.h>		/* close() */

/*
 * Defined macros
 */
#	define LOCALHOST "localhost"

#define MAXRULES 100

// Sleep for .1 seconds between stat()'s
const int	STAT_RETRY_SLEEP_TIME = 100000;

/*
 * typedefs & structs
 */

/*
 * Static variables
 */
static char	hostName[MAXNAMELEN] = "";
static char	userName[MAXNAMELEN] = "";


static int	second_pass = 0;

/*
 * File table of contents
 */
extern	Doname		doname_check(register Name target, register Boolean do_get, register Boolean implicit, register Boolean automatic);
extern	Doname		doname(register Name target, register Boolean do_get, register Boolean implicit, register Boolean automatic);
static	Boolean		check_dependencies(Doname *result, Property line, Boolean do_get, Name target, Name true_target, Boolean doing_subtree, Chain *out_of_date_tail, Property old_locals, Boolean implicit, Property *command, Name less, Boolean rechecking_target, Boolean recheck_conditionals);
void		dynamic_dependencies(Name target);
static	Doname		run_command(register Property line, Boolean print_machine);
extern	Doname		execute_serial(Property line);
extern	Name		vpath_translation(register Name cmd);
extern	void		check_state(Name temp_file_name);
static	void		read_dependency_file(register Name filename);
static	void		check_read_state_file(void);
static	void		do_assign(register Name line, register Name target);
static	void		build_command_strings(Name target, register Property line);
static	Doname		touch_command(register Property line, register Name target, Doname result);
extern	void		update_target(Property line, Doname result);
static	Doname		sccs_get(register Name target, register Property *command);
extern	void		read_directory_of_file(register Name file);
static	void		add_pattern_conditionals(register Name target);
extern	void		set_locals(register Name target, register Property old_locals);
extern	void		reset_locals(register Name target, register Property old_locals, register Property conditional, register int index);
extern	Boolean		check_auto_dependencies(Name target, int auto_count, Name *automatics);
static	void		delete_query_chain(Chain ch);

// From read2.cc
extern	Name		normalize_name(register wchar_t *name_string, register int length);



/*
 * DONE.
 *
 *	doname_check(target, do_get, implicit, automatic)
 *
 *	Will call doname() and then inspect the return value
 *
 *	Return value:
 *				Indication if the build failed or not
 *
 *	Parameters:
 *		target		The target to build
 *		do_get		Passed thru to doname()
 *		implicit	Passed thru to doname()
 *		automatic	Are we building a hidden dependency?
 *
 *	Global variables used:
 *		build_failed_seen	Set if -k is on and error occurs
 *		continue_after_error	Indicates that -k is on
 *		report_dependencies	No error msg if -P is on
 */
Doname
doname_check(register Name target, register Boolean do_get, register Boolean implicit, register Boolean automatic)
{
	int first_time = 1;
	(void) fflush(stdout);
try_again:
	switch (doname(target, do_get, implicit, automatic)) {
	case build_ok:
		second_pass = 0;
		return build_ok;
	case build_running:
		second_pass = 0;
		return build_running;
	case build_failed:
		if (!continue_after_error) {
			fatal(gettext("Target `%s' not remade because of errors"),
			      target->string_mb);
		}
		build_failed_seen = true;
		second_pass = 0;
		return build_failed;
	case build_dont_know:
		/*
		 * If we can't figure out how to build an automatic
		 * (hidden) dependency, we just ignore it.
		 * We later declare the target to be out of date just in
		 * case something changed.
		 * Also, don't complain if just reporting the dependencies
		 * and not building anything.
		 */
		if (automatic || (report_dependencies_level > 0)) {
			second_pass = 0;
			return build_dont_know;
		}
		if(first_time) {
			first_time = 0;
			second_pass = 1;
			goto try_again;
		}
		second_pass = 0;
		if (continue_after_error && !svr4) {
			warning(gettext("Don't know how to make target `%s'"),
				target->string_mb);
			build_failed_seen = true;
			return build_failed;
		}
		fatal(gettext("Don't know how to make target `%s'"), target->string_mb);
		break;
	}
#ifdef lint
	return build_failed;
#endif
}


void
enter_explicit_rule_from_dynamic_rule(Name target, Name source)
{
	Property line, source_line;
	Dependency dependency;

	source_line = get_prop(source->prop, line_prop);
	line = maybe_append_prop(target, line_prop);
	line->body.line.sccs_command = false;
	line->body.line.target = target;
	if (line->body.line.command_template == NULL) {
		line->body.line.command_template = source_line->body.line.command_template;
		for (dependency = source_line->body.line.dependencies;
		     dependency != NULL;
		     dependency = dependency->next) {
			enter_dependency(line, dependency->name, false);
		}
		line->body.line.less = target;
	}
	line->body.line.percent = NULL;
}



Name
find_dyntarget(Name target)
{
	Dyntarget		p;
	int			i;
	String_rec		string;
	wchar_t			buffer[STRING_BUFFER_LENGTH];
	wchar_t			*pp, * bufend;
	wchar_t			tbuffer[MAXPATHLEN];
	Wstring			wcb(target);

	for (p = dyntarget_list; p != NULL; p = p->next) {
		INIT_STRING_FROM_STACK(string, buffer);
		expand_value(p->name, &string, false);
		i = 0;
		pp = string.buffer.start;
		bufend = pp + STRING_BUFFER_LENGTH;
		while((*pp != nul_char) && (pp < bufend)) {
			if(iswspace(*pp)) {
				tbuffer[i] = nul_char;
				if(i > 0) {
					if (wcb.equal(tbuffer)) {
						enter_explicit_rule_from_dynamic_rule(target, p->name);
						return(target);
					}
				}
				pp++;
				i = 0;
				continue;
			}
			tbuffer[i] = *pp;
			i++;
			pp++;
			if(*pp == nul_char) {
				tbuffer[i] = nul_char;
				if(i > 0) {
					if (wcb.equal(tbuffer)) {
						enter_explicit_rule_from_dynamic_rule(target, p->name);
						return(target);
					}
				}
				break;
			}
		}
	}
	return(NULL);
}

/*
 * DONE.
 *
 *	doname(target, do_get, implicit)
 *
 *	Chases all files the target depends on and builds any that
 *	are out of date. If the target is out of date it is then rebuilt.
 *
 *	Return value:
 *				Indiates if build failed or nt
 *
 *	Parameters:
 *		target		Target to build
 *		do_get		Run sccs get is nessecary
 *		implicit	doname is trying to find an implicit rule
 *
 *	Global variables used:
 *		assign_done	True if command line assgnment has happened
 *		commands_done	Preserved for the case that we need local value
 *		debug_level	Should we trace make's actions?
 *		default_rule	The rule for ".DEFAULT", used as last resort
 *		empty_name	The Name "", used when looking for single sfx
 *		keep_state	Indicates that .KEEP_STATE is on
 *		parallel	True if building in parallel
 *		recursion_level	Used for tracing
 *		report_dependencies make -P is on
 */
Doname
doname(register Name target, register Boolean do_get, register Boolean implicit, register Boolean automatic)
{
	Doname			result = build_dont_know;
	Chain			out_of_date_list = NULL;
	Chain			target_group;
	Property		old_locals = NULL;
	register Property	line;
	Property		command = NULL;
	register Dependency	dependency;
	Name			less = NULL;
	Name			true_target = target;
	Name			*automatics = NULL;
	register int		auto_count;
	Boolean			rechecking_target = false;
	Boolean			saved_commands_done;
	Boolean			restart = false;
	Boolean			save_parallel = parallel;
	Boolean			doing_subtree = false;

	Boolean			recheck_conditionals = false;

	if (target->state == build_running) {
		return build_running;
	}
	line = get_prop(target->prop, line_prop);
	if (line != NULL) {
		/*
		 * If this target is a member of target group and one of the
		 * other members of the group is running, mark this target
		 * as running.
		 */
		for (target_group = line->body.line.target_group;
		     target_group != NULL;
		     target_group = target_group->next) {
			if (is_running(target_group->name)) {
				target->state = build_running;
				add_pending(target,
					    recursion_level,
					    do_get,
					    implicit,
					    false);
				return build_running;
			}
		}
	}
	/*
	 * If the target is a constructed one for a "::" target,
	 * we need to consider that.
	 */
	if (target->has_target_prop) {
		true_target = get_prop(target->prop,
				       target_prop)->body.target.target;
		if (true_target->colon_splits > 0) {
			/* Make sure we have a valid time for :: targets */
			Property        time;

			time = get_prop(true_target->prop, time_prop);
			if (time != NULL) {
				true_target->stat.time = time->body.time.time;
			}
		}
	}
	(void) exists(true_target);
	/*
	 * If the target has been processed, we don't need to do it again,
	 * unless it depends on conditional macros or a delayed assignment,
	 * or it has been done when KEEP_STATE is on.
	 */
	if (target->state == build_ok) {
		if((!keep_state || (!target->depends_on_conditional && !assign_done))) {
			return build_ok;
		} else {
			recheck_conditionals = true;
		}
  	}
	if (target->state == build_subtree) {
		/* A dynamic macro subtree is being built */
		target->state = build_dont_know;
		doing_subtree = true;
		if (!target->checking_subtree) {
			/*
			 * This target has been started before and therefore
			 * not all dependencies have to be built.
			 */
			restart = true;
		}
	} else if (target->state == build_pending) {
		target->state = build_dont_know;
		restart = true;
/*
	} else if (parallel &&
		   keep_state &&
		   (target->conditional_cnt > 0)) {
	    if (!parallel_ok(target, false)) {
		add_subtree(target, recursion_level, do_get, implicit);
		target->state = build_running;
		return build_running;
	    }
 */
	}
	/*
	 * If KEEP_STATE is on, we have to rebuild the target if the
	 * building of it caused new automatic dependencies to be reported.
	 * This is where we restart the build.
	 */
	if (line != NULL) {
		line->body.line.percent = NULL;
	}
recheck_target:
	/* Init all local variables */
	result = build_dont_know;
	out_of_date_list = NULL;
	command = NULL;
	less = NULL;
	auto_count = 0;
	if (!restart && line != NULL) {
		/*
		 * If this target has never been built before, mark all
		 * of the dependencies as never built.
		 */
		for (dependency = line->body.line.dependencies;
		     dependency != NULL;
		     dependency = dependency->next) {
			dependency->built = false;
		}
	}
	/* Save the set of automatic depes defined for this target */
	if (keep_state &&
	    (line != NULL) &&
	    (line->body.line.dependencies != NULL)) {
		Name *p;

		/*
		 * First run thru the dependency list to see how many
		 * autos there are.
		 */
		for (dependency = line->body.line.dependencies;
		     dependency != NULL;
		     dependency = dependency->next) {
			if (dependency->automatic && !dependency->stale) {
				auto_count++;
			}
		}
		/* Create vector to hold the current autos */
		automatics =
		  (Name *) alloca((int) (auto_count * sizeof (Name)));
		/* Copy them */
		for (p = automatics, dependency = line->body.line.dependencies;
		     dependency != NULL;
		     dependency = dependency->next) {
			if (dependency->automatic && !dependency->stale) {
				*p++ = dependency->name;
			}
		}
	}
	if (debug_level > 1) {
		(void) printf("%*sdoname(%s)\n",
			      recursion_level,
			      "",
			      target->string_mb);
	}
	recursion_level++;
	/* Avoid infinite loops */
	if (target->state == build_in_progress) {
		warning(gettext("Infinite loop: Target `%s' depends on itself"),
			target->string_mb);
		return build_ok;
	}
	target->state = build_in_progress;

	/* Activate conditional macros for the target */
	if (!target->added_pattern_conditionals) {
		add_pattern_conditionals(target);
		target->added_pattern_conditionals = true;
	}
	if (target->conditional_cnt > 0) {
		old_locals = (Property) alloca(target->conditional_cnt *
					       sizeof (Property_rec));
		set_locals(target, old_locals);
	}

/*
 * after making the call to dynamic_dependecies unconditional we can handle
 * target names that are same as file name. In this case $$@ in the
 * dependencies did not mean anything. WIth this change it expands it
 * as expected.
 */
	if (!target->has_depe_list_expanded)
	{
		dynamic_dependencies(target);
	}

/*
 *	FIRST SECTION -- GO THROUGH DEPENDENCIES AND COLLECT EXPLICIT
 *	COMMANDS TO RUN
 */
	if ((line = get_prop(target->prop, line_prop)) != NULL) {
		if (check_dependencies(&result,
				       line,
				       do_get,
				       target,
				       true_target,
				       doing_subtree,
				       &out_of_date_list,
				       old_locals,
				       implicit,
				       &command,
				       less,
				       rechecking_target,
				       recheck_conditionals)) {
			return build_running;
		}
		if (line->body.line.query != NULL) {
			delete_query_chain(line->body.line.query);
		}
		line->body.line.query = out_of_date_list;
	}


/*
 * If the target is a :: type, do not try to find the rule for the target,
 * all actions will be taken by separate branches.
 * Else, we try to find an implicit rule using various methods,
 * we quit as soon as one is found.
 *
 * [tolik, 12 Sep 2002] Do not try to find implicit rule for the target
 * being rechecked - the target is being rechecked means that it already
 * has explicit dependencies derived from an implicit rule found
 * in previous step.
 */
	if (target->colon_splits == 0 && !rechecking_target) {
		/* Look for percent matched rule */
		if ((result == build_dont_know) &&
		    (command == NULL)) {
			switch (find_percent_rule(
					target,
					&command,
					recheck_conditionals)) {
			case build_failed:
				result = build_failed;
				break;
			case build_running:
				target->state = build_running;
				add_pending(target,
					    --recursion_level,
					    do_get,
					    implicit,
					    false);
				if (target->conditional_cnt > 0) {
					reset_locals(target,
						     old_locals,
						     get_prop(target->prop,
							     conditional_prop),
						     0);
				}
				return build_running;
			case build_ok:
				result = build_ok;
				break;
			}
		}
		/* Look for double suffix rule */
		if (result == build_dont_know) {
			Property member;

			if (target->is_member &&
			    ((member = get_prop(target->prop, member_prop)) !=
			     NULL)) {
			        switch (find_ar_suffix_rule(target,
						member->body.
						member.member,
						&command,
						recheck_conditionals)) {
				case build_failed:
					result = build_failed;
					break;
				case build_running:
					target->state = build_running;
					add_pending(target,
						    --recursion_level,
						    do_get,
						    implicit,
						    false);
				    if (target->conditional_cnt > 0) {
					    reset_locals(target,
							 old_locals,
							 get_prop(target->prop,
							     conditional_prop),
							 0);
				    }
					return build_running;
				default:
					/* ALWAYS bind $% for old style */
					/* ar rules */
					if (line == NULL) {
						line =
						  maybe_append_prop(target,
								    line_prop);
					}
					line->body.line.percent =
					  member->body.member.member;
					break;
				}
			} else {
				switch (find_double_suffix_rule(target,
						&command,
						recheck_conditionals)) {
				case build_failed:
					result = build_failed;
					break;
				case build_running:
					target->state = build_running;
					add_pending(target,
						    --recursion_level,
						    do_get,
						    implicit,
						    false);
					if (target->conditional_cnt > 0) {
						reset_locals(target,
							     old_locals,
							     get_prop(target->
								      prop,
								      conditional_prop),
							     0);
					}
					return build_running;
				}
			}
		}
		/* Look for single suffix rule */

/* /tolik/
 * I commented !implicit to fix bug 1247448: Suffix Rules failed when combine with Pattern Matching Rules.
 * This caused problem with SVR4 tilde rules (infinite recursion). So I made some changes in "implicit.cc"
 */
/* /tolik, 06.21.96/
 * Regression! See BugId 1255360
 * If more than one percent rules are defined for the same target then
 * the behaviour of 'make' with my previous fix may be different from one
 * of the 'old make'.
 * The global variable second_pass (maybe it should be an argument to doname())
 * is intended to avoid this regression. It is set in doname_check().
 * First, 'make' will work as it worked before. Only when it is
 * going to say "don't know how to make target" it sets second_pass to true and
 * run 'doname' again but now trying to use Single Suffix Rules.
 */
		if ((result == build_dont_know) && !automatic && (!implicit || second_pass) &&
		    ((line == NULL) ||
		     ((line->body.line.target != NULL) &&
		      !line->body.line.target->has_regular_dependency))) {
			switch (find_suffix_rule(target,
						 target,
						 empty_name,
						 &command,
						 recheck_conditionals)) {
			case build_failed:
				result = build_failed;
				break;
			case build_running:
				target->state = build_running;
				add_pending(target,
					    --recursion_level,
					    do_get,
					    implicit,
					    false);
				if (target->conditional_cnt > 0) {
					reset_locals(target,
						     old_locals,
						     get_prop(target->prop,
							     conditional_prop),
						     0);
				}
				return build_running;
			}
		}
		/* Try to sccs get */
		if ((command == NULL) &&
		    (result == build_dont_know) &&
		    do_get) {
			result = sccs_get(target, &command);
		}

		/* Use .DEFAULT rule if it is defined. */
		if ((command == NULL) &&
		    (result == build_dont_know) &&
		    (true_target->colons == no_colon) &&
		    default_rule &&
		    !implicit) {
			/* Make sure we have a line prop */
			line = maybe_append_prop(target, line_prop);
			command = line;
			Boolean out_of_date;
			if (true_target->is_member) {
				out_of_date = (Boolean) OUT_OF_DATE_SEC(true_target->stat.time,
									line->body.line.dependency_time);
			} else {
				out_of_date = (Boolean) OUT_OF_DATE(true_target->stat.time,
								    line->body.line.dependency_time);
			}
			if (build_unconditional || out_of_date) {
				line->body.line.is_out_of_date = true;
				if (debug_level > 0) {
					(void) printf(gettext("%*sBuilding %s using .DEFAULT because it is out of date\n"),
						      recursion_level,
						      "",
						      true_target->string_mb);
				}
			}
			line->body.line.sccs_command = false;
			line->body.line.command_template = default_rule;
			line->body.line.target = true_target;
			line->body.line.star = NULL;
			line->body.line.less = true_target;
			line->body.line.percent = NULL;
		}
	}

	/* We say "target up to date" if no cmd were executed for the target */
	if (!target->is_double_colon_parent) {
		commands_done = false;
	}

	silent = silent_all;
	ignore_errors = ignore_errors_all;
	if  (posix)
	{
	  if  (!silent)
	  {
            silent = (Boolean) target->silent_mode;
	  }
	  if  (!ignore_errors)
	  {
            ignore_errors = (Boolean) target->ignore_error_mode;
	  }
	}

	int doname_dyntarget = 0;
r_command:
	/* Run commands if any. */
	if ((command != NULL) &&
	    (command->body.line.command_template != NULL)) {
		if (result != build_failed) {
			result = run_command(command,
					     (Boolean) ((parallel || save_parallel) && !silent));
		}
		switch (result) {
		case build_running:
			add_running(target,
				    true_target,
				    command,
				    --recursion_level,
				    auto_count,
				    automatics,
				    do_get,
				    implicit);
			target->state = build_running;
			if ((line = get_prop(target->prop,
					     line_prop)) != NULL) {
				if (line->body.line.query != NULL) {
					delete_query_chain(line->body.line.query);
				}
				line->body.line.query = NULL;
			}
			if (target->conditional_cnt > 0) {
				reset_locals(target,
					     old_locals,
					     get_prop(target->prop,
						     conditional_prop),
					     0);
			}
			return build_running;
		case build_serial:
			add_serial(target,
				   --recursion_level,
				   do_get,
				   implicit);
			target->state = build_running;
			line = get_prop(target->prop, line_prop);
			if (line != NULL) {
				if (line->body.line.query != NULL) {
					delete_query_chain(line->body.line.query);
				}
				line->body.line.query = NULL;
			}
			if (target->conditional_cnt > 0) {
				reset_locals(target,
					     old_locals,
					     get_prop(target->prop,
						     conditional_prop),
					     0);
			}
			return build_running;
		case build_ok:
			/* If all went OK set a nice timestamp */
			if (true_target->stat.time == file_doesnt_exist) {
				true_target->stat.time = file_max_time;
			}
			break;
		}
	} else {
		/*
		 * If no command was found for the target, and it doesn't
		 * exist, and it is mentioned as a target in the makefile,
		 * we say it is extremely new and that it is OK.
		 */
		if (target->colons != no_colon) {
			if (true_target->stat.time == file_doesnt_exist){
				true_target->stat.time = file_max_time;
			}
			result = build_ok;
		}
		/*
		 * Trying dynamic targets.
		 */
		if(!doname_dyntarget) {
			doname_dyntarget = 1;
			Name dtarg = find_dyntarget(target);
			if(dtarg!=NULL) {
				if (!target->has_depe_list_expanded) {
					dynamic_dependencies(target);
				}
				if ((line = get_prop(target->prop, line_prop)) != NULL) {
					if (check_dependencies(&result,
					                       line,
					                       do_get,
					                       target,
					                       true_target,
					                       doing_subtree,
					                       &out_of_date_list,
					                       old_locals,
					                       implicit,
					                       &command,
					                       less,
					                       rechecking_target,
					                       recheck_conditionals))
					{
						return build_running;
					}
					if (line->body.line.query != NULL) {
						delete_query_chain(line->body.line.query);
					}
					line->body.line.query = out_of_date_list;
				}
				goto r_command;
			}
		}
		/*
		 * If the file exists, it is OK that we couldnt figure
		 * out how to build it.
		 */
		(void) exists(target);
		if ((target->stat.time != file_doesnt_exist) &&
		    (result == build_dont_know)) {
			result = build_ok;
		}
	}

	/*
	 * Some of the following is duplicated in the function finish_doname.
	 * If anything is changed here, check to see if it needs to be
	 * changed there.
	 */
	if ((line = get_prop(target->prop, line_prop)) != NULL) {
		if (line->body.line.query != NULL) {
			delete_query_chain(line->body.line.query);
		}
		line->body.line.query = NULL;
	}
	target->state = result;
	parallel = save_parallel;
	if (target->conditional_cnt > 0) {
		reset_locals(target,
			     old_locals,
			     get_prop(target->prop, conditional_prop),
			     0);
	}
	recursion_level--;
	if (target->is_member) {
		Property member;

		/* Propagate the timestamp from the member file to the member*/
		if ((target->stat.time != file_max_time) &&
		    ((member = get_prop(target->prop, member_prop)) != NULL) &&
		    (exists(member->body.member.member) > file_doesnt_exist)) {
			target->stat.time =
			  member->body.member.member->stat.time;
		}
	}
	/*
	 * Check if we found any new auto dependencies when we
	 * built the target.
	 */
	if ((result == build_ok) && check_auto_dependencies(target,
							    auto_count,
							    automatics)) {
		if (debug_level > 0) {
			(void) printf(gettext("%*sTarget `%s' acquired new dependencies from build, rechecking all dependencies\n"),
				      recursion_level,
				      "",
				      true_target->string_mb);
		}
		rechecking_target = true;
		saved_commands_done = commands_done;
		goto recheck_target;
	}

	if (rechecking_target && !commands_done) {
		commands_done = saved_commands_done;
	}

	return result;
}

/*
 * DONE.
 *
 *	check_dependencies(result, line, do_get,
 *			target, true_target, doing_subtree, out_of_date_tail,
 *			old_locals, implicit, command, less, rechecking_target)
 *
 *	Return value:
 *				True returned if some dependencies left running
 *
 *	Parameters:
 *		result		Pointer to cell we update if build failed
 *		line		We get the dependencies from here
 *		do_get		Allow use of sccs get in recursive doname()
 *		target		The target to chase dependencies for
 *		true_target	The real one for :: and lib(member)
 *		doing_subtree	True if building a conditional macro subtree
 *		out_of_date_tail Used to set the $? list
 *		old_locals	Used for resetting the local macros
 *		implicit	Called when scanning for implicit rules?
 *		command		Place to stuff command
 *		less		Set to $< value
 *
 *	Global variables used:
 *		command_changed	Set if we suspect .make.state needs rewrite
 *		debug_level	Should we trace actions?
 *		force		The Name " FORCE", compared against
 *		recursion_level	Used for tracing
 *		rewrite_statefile Set if .make.state needs rewriting
 *		wait_name	The Name ".WAIT", compared against
 */
static Boolean
check_dependencies(Doname *result, Property line, Boolean do_get, Name target, Name true_target, Boolean doing_subtree, Chain *out_of_date_tail, Property old_locals, Boolean implicit, Property *command, Name less, Boolean rechecking_target, Boolean recheck_conditionals)
{
	Boolean			dependencies_running;
	register Dependency	dependency;
	Doname			dep_result;
	Boolean			dependency_changed = false;

	line->body.line.dependency_time = file_doesnt_exist;
	if (line->body.line.query != NULL) {
		delete_query_chain(line->body.line.query);
	}
	line->body.line.query = NULL;
	line->body.line.is_out_of_date = false;
	dependencies_running = false;
	/*
	 * Run thru all the dependencies and call doname() recursively
	 * on each of them.
	 */
	for (dependency = line->body.line.dependencies;
	     dependency != NULL;
	     dependency = dependency->next) {
		Boolean this_dependency_changed = false;

		if (!dependency->automatic &&
		    (rechecking_target || target->rechecking_target)) {
			/*
			 * We only bother with the autos when rechecking
			 */
			continue;
		}

		if (dependency->name == wait_name) {
			/*
			 * The special target .WAIT means finish all of
			 * the prior dependencies before continuing.
			 */
			if (dependencies_running) {
				break;
			}
		} else if ((!parallel_ok(dependency->name, false)) &&
			   (dependencies_running)) {
			/*
			 * If we can't execute the current dependency in
			 * parallel, hold off the dependency processing
			 * to preserve the order of the dependencies.
			 */
			break;
		} else {
			timestruc_t	depe_time = file_doesnt_exist;


			if (true_target->is_member) {
				depe_time = exists(dependency->name);
			}
			if (dependency->built ||
			    (dependency->name->state == build_failed)) {
				dep_result = (Doname) dependency->name->state;
			} else {
				dep_result = doname_check(dependency->name,
							  do_get,
							  false,
							  (Boolean) dependency->automatic);
			}
			if (true_target->is_member || dependency->name->is_member) {
				/* should compare only secs, cause lib members does not have nsec time resolution */
				if (depe_time.tv_sec != dependency->name->stat.time.tv_sec) {
					this_dependency_changed =
					  dependency_changed =
					    true;
				}
			} else {
				if (depe_time != dependency->name->stat.time) {
					this_dependency_changed =
					  dependency_changed =
					    true;
				}
			}
			dependency->built = true;
			switch (dep_result) {
			case build_running:
				dependencies_running = true;
				continue;
			case build_failed:
				*result = build_failed;
				break;
			case build_dont_know:
/*
 * If make can't figure out how to make a dependency, maybe the dependency
 * is out of date. In this case, we just declare the target out of date
 * and go on. If we really need the dependency, the make'ing of the target
 * will fail. This will only happen for automatic (hidden) dependencies.
 */
				if(!recheck_conditionals) {
					line->body.line.is_out_of_date = true;
				}
				/*
				 * Make sure the dependency is not saved
				 * in the state file.
				 */
				dependency->stale = true;
				rewrite_statefile =
				  command_changed =
				    true;
				if (debug_level > 0) {
					(void) printf(gettext("Target %s rebuilt because dependency %s does not exist\n"),
						     true_target->string_mb,
						     dependency->name->string_mb);
				}
				break;
			}
			if (dependency->name->depends_on_conditional) {
				target->depends_on_conditional = true;
			}
			if (dependency->name == force) {
				target->stat.time =
				  dependency->name->stat.time;
			}
			/*
			 * Propagate new timestamp from "member" to
			 * "lib.a(member)".
			 */
			(void) exists(dependency->name);

			/* Collect the timestamp of the youngest dependency */
			line->body.line.dependency_time =
			  MAX(dependency->name->stat.time,
			      line->body.line.dependency_time);

			/* Correction: do not consider nanosecs for members */
			if(true_target->is_member || dependency->name->is_member) {
				line->body.line.dependency_time.tv_nsec = 0;
			}

			if (debug_level > 1) {
				(void) printf(gettext("%*sDate(%s)=%s \n"),
					      recursion_level,
					      "",
					      dependency->name->string_mb,
					      time_to_string(dependency->name->
							     stat.time));
				if (dependency->name->stat.time > line->body.line.dependency_time) {
					(void) printf(gettext("%*sDate-dependencies(%s) set to %s\n"),
						      recursion_level,
						      "",
						      true_target->string_mb,
						      time_to_string(line->body.line.
								     dependency_time));
				}
			}

			/* Build the $? list */
			if (true_target->is_member) {
				if (this_dependency_changed == true) {
					true_target->stat.time = dependency->name->stat.time;
					true_target->stat.time.tv_sec--;
				} else {
					/* Dina:
					 * The next statement is commented
					 * out as a fix for bug #1051032.
					 * if dependency hasn't changed
					 * then there's no need to invalidate
					 * true_target. This statemnt causes
					 * make to take much longer to process
					 * an already-built archive. Soren
					 * said it was a quick fix for some
					 * problem he doesn't remember.
					true_target->stat.time = file_no_time;
					 */
					(void) exists(true_target);
				}
			} else {
				(void) exists(true_target);
			}
			Boolean out_of_date;
			if (true_target->is_member || dependency->name->is_member) {
				out_of_date = (Boolean) OUT_OF_DATE_SEC(true_target->stat.time,
							                dependency->name->stat.time);
			} else {
				out_of_date = (Boolean) OUT_OF_DATE(true_target->stat.time,
							            dependency->name->stat.time);
			}
			if ((build_unconditional || out_of_date) &&
			    (dependency->name != force) &&
			    (dependency->stale == false)) {
				*out_of_date_tail = ALLOC(Chain);
				if (dependency->name->is_member &&
				    (get_prop(dependency->name->prop,
					      member_prop) != NULL)) {
					(*out_of_date_tail)->name =
					  get_prop(dependency->name->prop,
						   member_prop)->
						     body.member.member;
				} else {
					(*out_of_date_tail)->name =
					  dependency->name;
				}
				(*out_of_date_tail)->next = NULL;
				out_of_date_tail = &(*out_of_date_tail)->next;
				if (debug_level > 0) {
					if (dependency->name->stat.time == file_max_time) {
						(void) printf(gettext("%*sBuilding %s because %s does not exist\n"),
							      recursion_level,
							      "",
							      true_target->string_mb,
							      dependency->name->string_mb);
					} else {
						(void) printf(gettext("%*sBuilding %s because it is out of date relative to %s\n"),
							      recursion_level,
							      "",
							      true_target->string_mb,
							      dependency->name->string_mb);
					}
				}
			}
			if (dependency->name == force) {
				force->stat.time =
				  file_max_time;
				force->state = build_dont_know;
			}
		}
	}
	if (dependencies_running) {
		if (doing_subtree) {
			if (target->conditional_cnt > 0) {
				reset_locals(target,
					     old_locals,
					     get_prop(target->prop,
						      conditional_prop),
					     0);
			}
			return true;
		} else {
			target->state = build_running;
			add_pending(target,
				    --recursion_level,
				    do_get,
				    implicit,
				    false);
			if (target->conditional_cnt > 0) {
				reset_locals(target,
					     old_locals,
					     get_prop(target->prop,
						      conditional_prop),
					     0);
			}
			return true;
		}
	}
	/*
	 * Collect the timestamp of the youngest double colon target
	 * dependency.
	 */
	if (target->is_double_colon_parent) {
		for (dependency = line->body.line.dependencies;
		     dependency != NULL;
		     dependency = dependency->next) {
			Property        tmp_line;

			if ((tmp_line = get_prop(dependency->name->prop, line_prop)) != NULL) {
				if(tmp_line->body.line.dependency_time != file_max_time) {
					target->stat.time =
					  MAX(tmp_line->body.line.dependency_time,
					      target->stat.time);
				}
			}
		}
	}
	if ((true_target->is_member) && (dependency_changed == true)) {
		true_target->stat.time = file_no_time;
	}
	/*
	 * After scanning all the dependencies, we check the rule
	 * if we found one.
	 */
	if (line->body.line.command_template != NULL) {
		if (line->body.line.command_template_redefined) {
			warning(gettext("Too many rules defined for target %s"),
				target->string_mb);
		}
		*command = line;
		/* Check if the target is out of date */
		Boolean out_of_date;
		if (true_target->is_member) {
			out_of_date = (Boolean) OUT_OF_DATE_SEC(true_target->stat.time,
				                                line->body.line.dependency_time);
		} else {
			out_of_date = (Boolean) OUT_OF_DATE(true_target->stat.time,
				                            line->body.line.dependency_time);
		}
		if (build_unconditional || out_of_date){
			if(!recheck_conditionals) {
				line->body.line.is_out_of_date = true;
			}
		}
		line->body.line.sccs_command = false;
		line->body.line.target = true_target;
		if(gnu_style) {

			// set $< for explicit rule
			if(line->body.line.dependencies != NULL) {
				less = line->body.line.dependencies->name;
			}

			// set $* for explicit rule
			Name			target_body;
			Name			tt = true_target;
			Property		member;
			register wchar_t	*target_end;
			register Dependency	suffix;
			register int		suffix_length;
			Wstring			targ_string;
			Wstring			suf_string;

			if (true_target->is_member &&
			    ((member = get_prop(target->prop, member_prop)) !=
			     NULL)) {
				tt = member->body.member.member;
			}
			targ_string.init(tt);
			target_end = targ_string.get_string() + tt->hash.length;
			for (suffix = suffixes; suffix != NULL; suffix = suffix->next) {
				suffix_length = suffix->name->hash.length;
				suf_string.init(suffix->name);
				if (tt->hash.length < suffix_length) {
					continue;
				} else if (!IS_WEQUALN(suf_string.get_string(),
						(target_end - suffix_length),
						suffix_length)) {
					continue;
				}
				target_body = GETNAME(
					targ_string.get_string(),
					(int)(tt->hash.length - suffix_length)
				);
				line->body.line.star = target_body;
			}

			// set result = build_ok so that implicit rules are not used.
			if(*result == build_dont_know) {
				*result = build_ok;
			}
		}
		if (less != NULL) {
			line->body.line.less = less;
		}
	}

	return false;
}

/*
 *	dynamic_dependencies(target)
 *
 *	Checks if any dependency contains a macro ref
 *	If so, it replaces the dependency with the expanded version.
 *	Here, "$@" gets translated to target->string. That is
 *	the current name on the left of the colon in the
 *	makefile.  Thus,
 *		xyz:	s.$@.c
 *	translates into
 *		xyz:	s.xyz.c
 *
 *	Also, "$(@F)" translates to the same thing without a preceeding
 *	directory path (if one exists).
 *	Note, to enter "$@" on a dependency line in a makefile
 *	"$$@" must be typed. This is because make expands
 *	macros in dependency lists upon reading them.
 *	dynamic_dependencies() also expands file wildcards.
 *	If there are any Shell meta characters in the name,
 *	search the directory, and replace the dependency
 *	with the set of files the pattern matches
 *
 *	Parameters:
 *		target		Target to sanitize dependencies for
 *
 *	Global variables used:
 *		c_at		The Name "@", used to set macro value
 *		debug_level	Should we trace actions?
 *		dot		The Name ".", used to read directory
 *		recursion_level	Used for tracing
 */
void
dynamic_dependencies(Name target)
{
	wchar_t			pattern[MAXPATHLEN];
	register wchar_t	*p;
	Property		line;
	register Dependency	dependency;
	register Dependency	*remove;
	String_rec		string;
	wchar_t			buffer[MAXPATHLEN];
	register Boolean	set_at = false;
	register wchar_t	*start;
	Dependency		new_depe;
	register Boolean	reuse_cell;
	Dependency		first_member;
	Name			directory;
	Name			lib;
	Name			member;
	Property		prop;
	Name			true_target = target;
	wchar_t			*library;

	if ((line = get_prop(target->prop, line_prop)) == NULL) {
		return;
	}
	/* If the target is constructed from a "::" target we consider that */
	if (target->has_target_prop) {
		true_target = get_prop(target->prop,
				       target_prop)->body.target.target;
	}
	/* Scan all dependencies and process the ones that contain "$" chars */
	for (dependency = line->body.line.dependencies;
	     dependency != NULL;
	     dependency = dependency->next) {
		if (!dependency->name->dollar) {
			continue;
		}
		target->has_depe_list_expanded = true;

		/* The make macro $@ is bound to the target name once per */
		/* invocation of dynamic_dependencies() */
		if (!set_at) {
			(void) SETVAR(c_at, true_target, false);
			set_at = true;
		}
		/* Expand this dependency string */
		INIT_STRING_FROM_STACK(string, buffer);
		expand_value(dependency->name, &string, false);
		/* Scan the expanded string. It could contain whitespace */
		/* which mean it expands to several dependencies */
		start = string.buffer.start;
		while (iswspace(*start)) {
			start++;
		}
		/* Remove the cell (later) if the macro was empty */
		if (start[0] == (int) nul_char) {
			dependency->name = NULL;
		}

/* azv 10/26/95 to fix bug BID_1170218 */
		if ((start[0] == (int) period_char) &&
		    (start[1] == (int) slash_char)) {
			start += 2;
		}
/* azv */

		first_member = NULL;
		/* We use the original dependency cell for the first */
		/* dependency from the expansion */
		reuse_cell = true;
		/* We also have to deal with dependencies that expand to */
		/* lib.a(members) notation */
		for (p = start; *p != (int) nul_char; p++) {
			if ((*p == (int) parenleft_char)) {
				lib = GETNAME(start, p - start);
				lib->is_member = true;
				first_member = dependency;
				start = p + 1;
				while (iswspace(*start)) {
					start++;
				}
				break;
			}
		}
		do {
		    /* First skip whitespace */
			for (p = start; *p != (int) nul_char; p++) {
				if ((*p == (int) nul_char) ||
				    iswspace(*p) ||
				    (*p == (int) parenright_char)) {
					break;
				}
			}
			/* Enter dependency from expansion */
			if (p != start) {
				/* Create new dependency cell if */
				/* this is not the first dependency */
				/* picked from the expansion */
				if (!reuse_cell) {
					new_depe = ALLOC(Dependency);
					new_depe->next = dependency->next;
					new_depe->automatic = false;
					new_depe->stale = false;
					new_depe->built = false;
					dependency->next = new_depe;
					dependency = new_depe;
				}
				reuse_cell = false;
				/* Internalize the dependency name */
				// tolik. Fix for bug 4110429: inconsistent expansion for macros that
				// include "//" and "/./"
				//dependency->name = GETNAME(start, p - start);
				dependency->name = normalize_name(start, p - start);
				if ((debug_level > 0) &&
				    (first_member == NULL)) {
					(void) printf(gettext("%*sDynamic dependency `%s' for target `%s'\n"),
						      recursion_level,
						      "",
						      dependency->name->string_mb,
						      true_target->string_mb);
				}
				for (start = p; iswspace(*start); start++);
				p = start;
			}
		} while ((*p != (int) nul_char) &&
			 (*p != (int) parenright_char));
		/* If the expansion was of lib.a(members) format we now */
		/* enter the proper member cells */
		if (first_member != NULL) {
			/* Scan the new dependencies and transform them from */
			/* "foo" to "lib.a(foo)" */
			for (; 1; first_member = first_member->next) {
				/* Build "lib.a(foo)" name */
				INIT_STRING_FROM_STACK(string, buffer);
				APPEND_NAME(lib,
					      &string,
					      (int) lib->hash.length);
				append_char((int) parenleft_char, &string);
				APPEND_NAME(first_member->name,
					      &string,
					      FIND_LENGTH);
				append_char((int) parenright_char, &string);
				member = first_member->name;
				/* Replace "foo" with "lib.a(foo)" */
				first_member->name =
				  GETNAME(string.buffer.start, FIND_LENGTH);
				if (string.free_after_use) {
					retmem(string.buffer.start);
				}
				if (debug_level > 0) {
					(void) printf(gettext("%*sDynamic dependency `%s' for target `%s'\n"),
						      recursion_level,
						      "",
						      first_member->name->
						      string_mb,
						      true_target->string_mb);
				}
				first_member->name->is_member = lib->is_member;
				/* Add member property to member */
				prop = maybe_append_prop(first_member->name,
							 member_prop);
				prop->body.member.library = lib;
				prop->body.member.entry = NULL;
				prop->body.member.member = member;
				if (first_member == dependency) {
					break;
				}
			}
		}
	}
	Wstring wcb;
	/* Then scan all the dependencies again. This time we want to expand */
	/* shell file wildcards */
	for (remove = &line->body.line.dependencies, dependency = *remove;
	     dependency != NULL;
	     dependency = *remove) {
		if (dependency->name == NULL) {
			dependency = *remove = (*remove)->next;
			continue;
		}
		/* If dependency name string contains shell wildcards */
		/* replace the name with the expansion */
		if (dependency->name->wildcard) {
			wcb.init(dependency->name);
			if ((start = (wchar_t *) wcschr(wcb.get_string(),
					   (int) parenleft_char)) != NULL) {
				/* lib(*) type pattern */
				library = buffer;
				(void) wcsncpy(buffer,
					      wcb.get_string(),
					      start - wcb.get_string());
				buffer[start-wcb.get_string()] =
				  (int) nul_char;
				(void) wcsncpy(pattern,
					      start + 1,
(int) (dependency->name->hash.length-(start-wcb.get_string())-2));
				pattern[dependency->name->hash.length -
					(start-wcb.get_string()) - 2] =
					  (int) nul_char;
			} else {
				library = NULL;
				(void) wcsncpy(pattern,
					      wcb.get_string(),
					      (int) dependency->name->hash.length);
				pattern[dependency->name->hash.length] =
				  (int) nul_char;
			}
			start = (wchar_t *) wcsrchr(pattern, (int) slash_char);
			if (start == NULL) {
				directory = dot;
				p = pattern;
			} else {
				directory = GETNAME(pattern, start-pattern);
				p = start+1;
			}
			/* The expansion is handled by the read_dir() routine*/
			if (read_dir(directory, p, line, library)) {
				*remove = (*remove)->next;
			} else {
				remove = &dependency->next;
			}
		} else {
			remove = &dependency->next;
		}
        }

	/* Then unbind $@ */
	(void) SETVAR(c_at, (Name) NULL, false);
}

/*
 * DONE.
 *
 *	run_command(line)
 *
 *	Takes one Cmd_line and runs the commands from it.
 *
 *	Return value:
 *				Indicates if the command failed or not
 *
 *	Parameters:
 *		line		The command line to run
 *
 *	Global variables used:
 *		commands_done	Set if we do run command
 *		current_line	Set to the line we run a command from
 *		current_target	Set to the target we run a command for
 *		file_number	Used to form temp file name
 *		keep_state	Indicates that .KEEP_STATE is on
 *		make_state	The Name ".make.state", used to check timestamp
 *		parallel	True if currently building in parallel
 *		parallel_process_cnt Count of parallel processes running
 *		quest		Indicates that make -q is on
 *		rewrite_statefile Set if we do run a command
 *		sunpro_dependencies The Name "SUNPRO_DEPENDENCIES", set value
 *		temp_file_directory Used to form temp fie name
 *		temp_file_name	Set to the name of the temp file
 *		touch		Indicates that make -t is on
 */
static Doname
run_command(register Property line, Boolean)
{
	register Doname		result = build_ok;
	register Boolean	remember_only = false;
	register Name		target = line->body.line.target;
	wchar_t			*string;
	char			tmp_file_path[MAXPATHLEN];

	if (!line->body.line.is_out_of_date && target->rechecking_target) {
		target->rechecking_target = false;
		return build_ok;
	}

	/*
	 * Build the command if we know the target is out of date,
	 * or if we want to check cmd consistency.
	 */
	if (line->body.line.is_out_of_date || keep_state) {
		/* Hack for handling conditional macros in DMake. */
		if (!line->body.line.dont_rebuild_command_used) {
			build_command_strings(target, line);
		}
	}
	/* Never mind */
	if (!line->body.line.is_out_of_date) {
		return build_ok;
	}
	/* If quest, then exit(1) because the target is out of date */
	if (quest) {
		if (posix) {
			result = execute_parallel(line, true);
		}
		exit_status = 1;
		exit(1);
	}
	/* We actually had to do something this time */
	rewrite_statefile = commands_done = true;
	/*
	 * If this is an sccs command, we have to do some extra checking
	 * and possibly complain. If the file can't be gotten because it's
	 * checked out, we complain and behave as if the command was
	 * executed eventhough we ignored the command.
	 */
	if (!touch &&
	    line->body.line.sccs_command &&
	    (target->stat.time != file_doesnt_exist) &&
	    ((target->stat.mode & 0222) != 0)) {
		fatal(gettext("%s is writable so it cannot be sccs gotten"),
		      target->string_mb);
		target->has_complained = remember_only = true;
	}
	/*
	 * If KEEP_STATE is on, we make sure we have the timestamp for
	 * .make.state. If .make.state changes during the command run,
	 * we reread .make.state after the command. We also setup the
	 * environment variable that asks utilities to report dependencies.
	 */
	if (!touch &&
	    keep_state &&
	    !remember_only) {
		(void) exists(make_state);
		if((strlen(temp_file_directory) == 1) &&
			(temp_file_directory[0] == '/')) {
		   tmp_file_path[0] = '\0';
		} else {
		   strcpy(tmp_file_path, temp_file_directory);
		}
		sprintf(mbs_buffer,
				"%s/.make.dependency.%08x.%d.%d",
			        tmp_file_path,
			        hostid,
			        getpid(),
			        file_number++);
		MBSTOWCS(wcs_buffer, mbs_buffer);
		Boolean fnd;
		temp_file_name = getname_fn(wcs_buffer, FIND_LENGTH, false, &fnd);
		temp_file_name->stat.is_file = true;
		int len = 2*MAXPATHLEN + strlen(target->string_mb) + 2;
		wchar_t *to = string = ALLOC_WC(len);
		for (wchar_t *from = wcs_buffer; *from != (int) nul_char; ) {
			if (*from == (int) space_char) {
				*to++ = (int) backslash_char;
			}
			*to++ = *from++;
		}
		*to++ = (int) space_char;
		MBSTOWCS(to, target->string_mb);
		Name sprodep_name = getname_fn(string, FIND_LENGTH, false, &fnd);
		(void) SETVAR(sunpro_dependencies,
			      sprodep_name,
			      false);
		retmem(string);
	} else {
		temp_file_name = NULL;
	}

	/*
	 * In case we are interrupted, we need to know what was going on.
	 */
	current_target = target;
	/*
	 * We also need to be able to save an empty command instead of the
	 * interrupted one in .make.state.
	 */
	current_line = line;
	if (remember_only) {
		/* Empty block!!! */
	} else if (touch) {
		result = touch_command(line, target, result);
		if (posix) {
			result = execute_parallel(line, true);
		}
	} else {
		/*
		 * If this is not a touch run, we need to execute the
		 * proper command(s) for the target.
		 */
		if (parallel) {
			if (!parallel_ok(target, true)) {
				/*
				 * We are building in parallel, but
				 * this target must be built in serial.
				 */
				/*
				 * If nothing else is building,
				 * do this one, else wait.
				 */
				if (parallel_process_cnt == 0) {
					result = execute_parallel(line, true, target->localhost);
				} else {
					current_target = NULL;
					current_line = NULL;
/*
					line->body.line.command_used = NULL;
 */
					line->body.line.dont_rebuild_command_used = true;
					return build_serial;
				}
			} else {
				result = execute_parallel(line, false);
				switch (result) {
				case build_running:
					return build_running;
				case build_serial:
					if (parallel_process_cnt == 0) {
						result = execute_parallel(line, true, target->localhost);
					} else {
						current_target = NULL;
						current_line = NULL;
						target->parallel = false;
						line->body.line.command_used =
						  			NULL;
						return build_serial;
					}
				}
			}
		} else {
			result = execute_parallel(line, true, target->localhost);
		}
	}
	temp_file_name = NULL;
	if (report_dependencies_level == 0){
		update_target(line, result);
	}
	current_target = NULL;
	current_line = NULL;
	return result;
}

/*
 *	execute_serial(line)
 *
 *	Runs thru the command line for the target and
 *	executes the rules one by one.
 *
 *	Return value:
 *				The result of the command build
 *
 *	Parameters:
 *		line		The command to execute
 *
 *	Static variables used:
 *
 *	Global variables used:
 *		continue_after_error -k flag
 *		do_not_exec_rule -n flag
 *		report_dependencies -P flag
 *		silent		Don't echo commands before executing
 *		temp_file_name	Temp file for auto dependencies
 *		vpath_defined	If true, translate path for command
 */
Doname
execute_serial(Property line)
{
	int			child_pid = 0;
	Boolean			printed_serial;
	Doname			result = build_ok;
	Cmd_line		rule, cmd_tail, command = NULL;
	char			mbstring[MAXPATHLEN];
	int			filed;
	Name			target = line->body.line.target;

	target->has_recursive_dependency = false;
	// We have to create a copy of the rules chain for processing because
	// the original one can be destroyed during .make.state file rereading.
	for (rule = line->body.line.command_used;
	     rule != NULL;
	     rule = rule->next) {
		if (command == NULL) {
			command = cmd_tail = ALLOC(Cmd_line);
		} else {
			cmd_tail->next = ALLOC(Cmd_line);
			cmd_tail = cmd_tail->next;
		}
		*cmd_tail = *rule;
	}
	if (command) {
		cmd_tail->next = NULL;
	}
	for (rule = command; rule != NULL; rule = rule->next) {
		if (posix && (touch || quest) && !rule->always_exec) {
			continue;
		}
		if (vpath_defined) {
			rule->command_line =
			  vpath_translation(rule->command_line);
		}
		/* Echo command line, maybe. */
		if ((rule->command_line->hash.length > 0) &&
		    !silent &&
		    (!rule->silent || do_not_exec_rule) &&
		    (report_dependencies_level == 0)) {
			(void) printf("%s\n", rule->command_line->string_mb);
		}
		if (rule->command_line->hash.length > 0) {
			/* Do assignment if command line prefixed with "=" */
			if (rule->assign) {
				result = build_ok;
				do_assign(rule->command_line, target);
			} else if (report_dependencies_level == 0) {
				/* Execute command line. */
				setvar_envvar();
				result = dosys(rule->command_line,
				               (Boolean) rule->ignore_error,
				               (Boolean) rule->make_refd,
				               /* ds 98.04.23 bug #4085164. make should always show error messages */
				               false,
				               /* BOOLEAN(rule->silent &&
				                       rule->ignore_error), */
				               (Boolean) rule->always_exec,
				               target);
				check_state(temp_file_name);
			}
		} else {
			result = build_ok;
		}
		if (result == build_failed) {
			if (silent || rule->silent) {
				(void) printf(gettext("The following command caused the error:\n%s\n"),
				              rule->command_line->string_mb);
			}
			if (!rule->ignore_error && !ignore_errors) {
				if (!continue_after_error) {
					fatal(gettext("Command failed for target `%s'"),
					      target->string_mb);
				}
				/*
				 * Make sure a failing command is not
				 * saved in .make.state.
				 */
				line->body.line.command_used = NULL;
				break;
			} else {
				result = build_ok;
			}
		}
	}
	for (rule = command; rule != NULL; rule = cmd_tail) {
		cmd_tail = rule->next;
		free(rule);
	}
	command = NULL;
	if (temp_file_name != NULL) {
		free_name(temp_file_name);
	}
        temp_file_name = NULL;

	Property spro = get_prop(sunpro_dependencies->prop, macro_prop);
	if(spro != NULL) {
		Name val = spro->body.macro.value;
		if(val != NULL) {
			free_name(val);
			spro->body.macro.value = NULL;
		}
	}
	spro = get_prop(sunpro_dependencies->prop, env_mem_prop);
	if(spro) {
		char *val = spro->body.env_mem.value;
		if(val != NULL) {
			/*
			 * Do not return memory allocated for SUNPRO_DEPENDENCIES
			 * It will be returned in setvar_daemon() in macro.cc
			 */
			//	retmem_mb(val);
			spro->body.env_mem.value = NULL;
		}
	}

        return result;
}



/*
 *	vpath_translation(cmd)
 *
 *	Translates one command line by
 *	checking each word. If the word has an alias it is translated.
 *
 *	Return value:
 *				The translated command
 *
 *	Parameters:
 *		cmd		Command to translate
 *
 *	Global variables used:
 */
Name
vpath_translation(register Name cmd)
{
	wchar_t			buffer[STRING_BUFFER_LENGTH];
	String_rec		new_cmd;
	wchar_t			*p;
	wchar_t			*start;

	if (!vpath_defined || (cmd == NULL) || (cmd->hash.length == 0)) {
		return cmd;
	}
	INIT_STRING_FROM_STACK(new_cmd, buffer);

	Wstring wcb(cmd);
	p = wcb.get_string();

	while (*p != (int) nul_char) {
		while (iswspace(*p) && (*p != (int) nul_char)) {
			append_char(*p++, &new_cmd);
		}
		start = p;
		while (!iswspace(*p) && (*p != (int) nul_char)) {
			p++;
		}
		cmd = GETNAME(start, p - start);
		if (cmd->has_vpath_alias_prop) {
			cmd = get_prop(cmd->prop, vpath_alias_prop)->
						body.vpath_alias.alias;
			APPEND_NAME(cmd,
				      &new_cmd,
				      (int) cmd->hash.length);
		} else {
			append_string(start, &new_cmd, p - start);
		}
	}
	cmd = GETNAME(new_cmd.buffer.start, FIND_LENGTH);
	if (new_cmd.free_after_use) {
		retmem(new_cmd.buffer.start);
	}
	return cmd;
}

/*
 *	check_state(temp_file_name)
 *
 *	Reads and checks the state changed by the previously executed command.
 *
 *	Parameters:
 *		temp_file_name	The auto dependency temp file
 *
 *	Global variables used:
 */
void
check_state(Name temp_file_name)
{
	if (!keep_state) {
		return;
	}

	/*
	 * Then read the temp file that now might
	 * contain dependency reports from utilities
	 */
	read_dependency_file(temp_file_name);

	/*
	 * And reread .make.state if it
	 * changed (the command ran recursive makes)
	 */
	check_read_state_file();
	if (temp_file_name != NULL) {
		(void) unlink(temp_file_name->string_mb);
	}
}

/*
 *	read_dependency_file(filename)
 *
 *	Read the temp file used for reporting dependencies to make
 *
 *	Parameters:
 *		filename	The name of the file with the state info
 *
 *	Global variables used:
 *		makefile_type	The type of makefile being read
 *		read_trace_level Debug flag
 *		temp_file_number The always increasing number for unique files
 *		trace_reader	Debug flag
 */
static void
read_dependency_file(register Name filename)
{
	register Makefile_type	save_makefile_type;

	if (filename == NULL) {
		return;
	}
	filename->stat.time = file_no_time;
	if (exists(filename) > file_doesnt_exist) {
		save_makefile_type = makefile_type;
		makefile_type = reading_cpp_file;
		if (read_trace_level > 1) {
			trace_reader = true;
		}
		temp_file_number++;
		(void) read_simple_file(filename,
					false,
					false,
					false,
					false,
					false,
					false);
		trace_reader = false;
		makefile_type = save_makefile_type;
	}
}

/*
 *	check_read_state_file()
 *
 *	Check if .make.state has changed
 *	If it has we reread it
 *
 *	Parameters:
 *
 *	Global variables used:
 *		make_state	Make state file name
 *		makefile_type	Type of makefile being read
 *		read_trace_level Debug flag
 *		trace_reader	Debug flag
 */
static void
check_read_state_file(void)
{
	timestruc_t		previous = make_state->stat.time;
	register Makefile_type	save_makefile_type;
	register Property	makefile;

	make_state->stat.time = file_no_time;
	if ((exists(make_state) == file_doesnt_exist) ||
	    (make_state->stat.time == previous)) {
		return;
	}
	save_makefile_type = makefile_type;
	makefile_type = rereading_statefile;
	/* Make sure we clear the old cached contents of .make.state */
	makefile = maybe_append_prop(make_state, makefile_prop);
	if (makefile->body.makefile.contents != NULL) {
		retmem(makefile->body.makefile.contents);
		makefile->body.makefile.contents = NULL;
	}
	if (read_trace_level > 1) {
		trace_reader = true;
	}
	temp_file_number++;
	(void) read_simple_file(make_state,
				false,
				false,
				false,
				false,
				false,
				true);
	trace_reader = false;
	makefile_type = save_makefile_type;
}

/*
 *	do_assign(line, target)
 *
 *	Handles runtime assignments for command lines prefixed with "=".
 *
 *	Parameters:
 *		line		The command that contains an assignment
 *		target		The Name of the target, used for error reports
 *
 *	Global variables used:
 *		assign_done	Set to indicate doname needs to reprocess
 */
static void
do_assign(register Name line, register Name target)
{
	Wstring wcb(line);
	register wchar_t	*string = wcb.get_string();
	register wchar_t	*equal;
	register Name		name;
	register Boolean	append = false;

	/*
	 * If any runtime assignments are done, doname() must reprocess all
	 * targets in the future since the macro values used to build the
	 * command lines for the targets might have changed.
	 */
	assign_done = true;
	/* Skip white space. */
	while (iswspace(*string)) {
		string++;
	}
	equal = string;
	/* Find "+=" or "=". */
	while (!iswspace(*equal) &&
	       (*equal != (int) plus_char) &&
	       (*equal != (int) equal_char)) {
		equal++;
	}
	/* Internalize macro name. */
	name = GETNAME(string, equal - string);
	/* Skip over "+=" "=". */
	while (!((*equal == (int) nul_char) ||
		 (*equal == (int) equal_char) ||
		 (*equal == (int) plus_char))) {
		equal++;
	}
	switch (*equal) {
	case nul_char:
		fatal(gettext("= expected in rule `%s' for target `%s'"),
		      line->string_mb,
		      target->string_mb);
	case plus_char:
		append = true;
		equal++;
		break;
	}
	equal++;
	/* Skip over whitespace in front of value. */
	while (iswspace(*equal)) {
		equal++;
	}
	/* Enter new macro value. */
	enter_equal(name,
		    GETNAME(equal, wcb.get_string() + line->hash.length - equal),
		    append);
}

/*
 *	build_command_strings(target, line)
 *
 *	Builds the command string to used when
 *	building a target. If the string is different from the previous one
 *	is_out_of_date is set.
 *
 *	Parameters:
 *		target		Target to build commands for
 *		line		Where to stuff result
 *
 *	Global variables used:
 *		c_at		The Name "@", used to set macro value
 *		command_changed	Set if command is different from old
 *		debug_level	Should we trace activities?
 *		do_not_exec_rule Always echo when running -n
 *		empty_name	The Name "", used for empty rule
 *		funny		Semantics of characters
 *		ignore_errors	Used to init field for line
 *		is_conditional	Set to false befor evaling macro, checked
 *				after expanding macros
 *		keep_state	Indicates that .KEEP_STATE is on
 *		make_word_mentioned Set by macro eval, inits field for cmd
 *		query		The Name "?", used to set macro value
 *		query_mentioned	Set by macro eval, inits field for cmd
 *		recursion_level	Used for tracing
 *		silent		Used to init field for line
 */
static void
build_command_strings(Name target, register Property line)
{
	String_rec		command_line;
	register Cmd_line	command_template = line->body.line.command_template;
	register Cmd_line	*insert = &line->body.line.command_used;
	register Cmd_line	used = *insert;
	wchar_t			buffer[STRING_BUFFER_LENGTH];
	wchar_t			*start;
	Name			new_command_line;
	register Boolean	new_command_longer = false;
	register Boolean	ignore_all_command_dependency = true;
	Property		member;
	static Name		less_name;
	static Name		percent_name;
	static Name		star;
	Name			tmp_name;

	if (less_name == NULL) {
		MBSTOWCS(wcs_buffer, "<");
		less_name = GETNAME(wcs_buffer, FIND_LENGTH);
		MBSTOWCS(wcs_buffer, "%");
		percent_name = GETNAME(wcs_buffer, FIND_LENGTH);
		MBSTOWCS(wcs_buffer, "*");
		star = GETNAME(wcs_buffer, FIND_LENGTH);
	}

	/* We have to check if a target depends on conditional macros */
	/* Targets that do must be reprocessed by doname() each time around */
	/* since the macro values used when building the target might have */
	/* changed */
	conditional_macro_used = false;
	/* If we are building a lib.a(member) target $@ should be bound */
	/* to lib.a */
	if (target->is_member &&
	    ((member = get_prop(target->prop, member_prop)) != NULL)) {
		target = member->body.member.library;
	}
	/* If we are building a "::" help target $@ should be bound to */
	/* the real target name */
	/* A lib.a(member) target is never :: */
	if (target->has_target_prop) {
		target = get_prop(target->prop, target_prop)->
		  body.target.target;
	}
	/* Bind the magic macros that make supplies */
	tmp_name = target;
	if(tmp_name != NULL) {
		if (tmp_name->has_vpath_alias_prop) {
			tmp_name = get_prop(tmp_name->prop, vpath_alias_prop)->
					body.vpath_alias.alias;
		}
	}
	(void) SETVAR(c_at, tmp_name, false);

	tmp_name = line->body.line.star;
	if(tmp_name != NULL) {
		if (tmp_name->has_vpath_alias_prop) {
			tmp_name = get_prop(tmp_name->prop, vpath_alias_prop)->
					body.vpath_alias.alias;
		}
	}
	(void) SETVAR(star, tmp_name, false);

	tmp_name = line->body.line.less;
	if(tmp_name != NULL) {
		if (tmp_name->has_vpath_alias_prop) {
			tmp_name = get_prop(tmp_name->prop, vpath_alias_prop)->
					body.vpath_alias.alias;
		}
	}
	(void) SETVAR(less_name, tmp_name, false);

	tmp_name = line->body.line.percent;
	if(tmp_name != NULL) {
		if (tmp_name->has_vpath_alias_prop) {
			tmp_name = get_prop(tmp_name->prop, vpath_alias_prop)->
					body.vpath_alias.alias;
		}
	}
	(void) SETVAR(percent_name, tmp_name, false);

	/* $? is seldom used and it is expensive to build */
	/* so we store the list form and build the string on demand */
	Chain query_list = NULL;
	Chain *query_list_tail = &query_list;

	for (Chain ch = line->body.line.query; ch != NULL; ch = ch->next) {
		*query_list_tail = ALLOC(Chain);
		(*query_list_tail)->name = ch->name;
		if ((*query_list_tail)->name->has_vpath_alias_prop) {
			(*query_list_tail)->name =
				get_prop((*query_list_tail)->name->prop,
					vpath_alias_prop)->body.vpath_alias.alias;
		}
		(*query_list_tail)->next = NULL;
		query_list_tail = &(*query_list_tail)->next;
	}
	(void) setvar_daemon(query,
			     (Name) query_list,
			     false,
			     chain_daemon,
                             false,
                             debug_level);

	/* build $^ */
	Chain hat_list = NULL;
	Chain *hat_list_tail = &hat_list;

	for (Dependency dependency = line->body.line.dependencies;
		dependency != NULL;
		dependency = dependency->next) {
		/* skip automatic dependencies */
		if (!dependency->automatic) {
			if ((dependency->name != force) &&
				(dependency->stale == false)) {
				*hat_list_tail = ALLOC(Chain);

				if (dependency->name->is_member &&
					(get_prop(dependency->name->prop, member_prop) != NULL)) {
					(*hat_list_tail)->name =
							get_prop(dependency->name->prop,
								member_prop)->body.member.member;
				} else {
					(*hat_list_tail)->name = dependency->name;
				}

				if((*hat_list_tail)->name != NULL) {
					if ((*hat_list_tail)->name->has_vpath_alias_prop) {
						(*hat_list_tail)->name =
							get_prop((*hat_list_tail)->name->prop,
								vpath_alias_prop)->body.vpath_alias.alias;
					}
				}

				(*hat_list_tail)->next = NULL;
				hat_list_tail = &(*hat_list_tail)->next;
			}
		}
	}
	(void) setvar_daemon(hat,
			     (Name) hat_list,
			     false,
			     chain_daemon,
                             false,
                             debug_level);

/* We have two command sequences we need to handle */
/* The old one that we probably read from .make.state */
/* and the new one we are building that will replace the old one */
/* Even when KEEP_STATE is not on we build a new command sequence and store */
/* it in the line prop. This command sequence is then executed by */
/* run_command(). If KEEP_STATE is on it is also later written to */
/* .make.state. The routine replaces the old command line by line with the */
/* new one trying to reuse Cmd_lines */

	/* If there is no old command_used we have to start creating */
	/* Cmd_lines to keep the new cmd in */
	if (used == NULL) {
		new_command_longer = true;
		*insert = used = ALLOC(Cmd_line);
		used->next = NULL;
		used->command_line = NULL;
		insert = &used->next;
	}
	/* Run thru the template for the new command and build the expanded */
	/* new command lines */
	for (;
	     command_template != NULL;
	     command_template = command_template->next, insert = &used->next, used = *insert) {
		/* If there is no old command_used Cmd_line we need to */
		/* create one and say that cmd consistency failed */
		if (used == NULL) {
			new_command_longer = true;
			*insert = used = ALLOC(Cmd_line);
			used->next = NULL;
			used->command_line = empty_name;
		}
		/* Prepare the Cmd_line for the processing */
		/* The command line prefixes "@-=?" are stripped and that */
		/* information is saved in the Cmd_line */
		used->assign = false;
		used->ignore_error = ignore_errors;
		used->silent = silent;
		used->always_exec = false;
		/* Expand the macros in the command line */
		INIT_STRING_FROM_STACK(command_line, buffer);
		make_word_mentioned =
		  query_mentioned =
		    false;
		expand_value(command_template->command_line, &command_line, true);
		/* If the macro $(MAKE) is mentioned in the command */
		/* "make -n" runs actually execute the command */
		used->make_refd = make_word_mentioned;
		used->ignore_command_dependency = query_mentioned;
		/* Strip the prefixes */
		start = command_line.buffer.start;
		for (;
		     iswspace(*start) ||
		     (get_char_semantics_value(*start) & (int) command_prefix_sem);
		     start++) {
			switch (*start) {
			case question_char:
				used->ignore_command_dependency = true;
				break;
			case exclam_char:
				used->ignore_command_dependency = false;
				break;
			case equal_char:
				used->assign = true;
				break;
			case hyphen_char:
				used->ignore_error = true;
				break;
			case at_char:
				if (!do_not_exec_rule) {
					used->silent = true;
				}
				break;
			case plus_char:
				if(posix) {
				  used->always_exec  = true;
				}
				break;
			}
		}
		/* If all command lines of the template are prefixed with "?"*/
		/* the VIRTUAL_ROOT is not used for cmd consistency checks */
		if (!used->ignore_command_dependency) {
			ignore_all_command_dependency = false;
		}
		/* Internalize the expanded and stripped command line */
		new_command_line = GETNAME(start, FIND_LENGTH);
		if ((used->command_line == NULL) &&
		    (line->body.line.sccs_command)) {
			used->command_line = new_command_line;
			new_command_longer = false;
		}
		/* Compare it with the old one for command consistency */
		if (used->command_line != new_command_line) {
			Name vpath_translated = vpath_translation(new_command_line);
			if (keep_state &&
			    !used->ignore_command_dependency && (vpath_translated != used->command_line)) {
				if (debug_level > 0) {
					if (used->command_line != NULL
					    && *used->command_line->string_mb !=
					    '\0') {
						(void) printf(gettext("%*sBuilding %s because new command \n\t%s\n%*sdifferent from old\n\t%s\n"),
							      recursion_level,
							      "",
							      target->string_mb,
							      vpath_translated->string_mb,
							      recursion_level,
							      "",
							      used->
							      command_line->
							      string_mb);
					} else {
						(void) printf(gettext("%*sBuilding %s because new command \n\t%s\n%*sdifferent from empty old command\n"),
							      recursion_level,
							      "",
							      target->string_mb,
							      vpath_translated->string_mb,
							      recursion_level,
							      "");
					}
				}
				command_changed = true;
                                line->body.line.is_out_of_date = true;
			}
			used->command_line = new_command_line;
		}
		if (command_line.free_after_use) {
			retmem(command_line.buffer.start);
		}
	}
	/* Check if the old command is longer than the new for */
	/* command consistency */
	if (used != NULL) {
		*insert = NULL;
		if (keep_state &&
		    !ignore_all_command_dependency) {
			if (debug_level > 0) {
				(void) printf(gettext("%*sBuilding %s because new command shorter than old\n"),
					      recursion_level,
					      "",
					      target->string_mb);
			}
			command_changed = true;
                        line->body.line.is_out_of_date = true;
		}
	}
	/* Check if the new command is longer than the old command for */
	/* command consistency */
	if (new_command_longer &&
	    !ignore_all_command_dependency &&
	    keep_state) {
		if (debug_level > 0) {
			(void) printf(gettext("%*sBuilding %s because new command longer than old\n"),
				      recursion_level,
				      "",
				      target->string_mb);
		}
		command_changed = true;
                line->body.line.is_out_of_date = true;
	}
	/* Unbind the magic macros */
	(void) SETVAR(c_at, (Name) NULL, false);
	(void) SETVAR(star, (Name) NULL, false);
	(void) SETVAR(less_name, (Name) NULL, false);
	(void) SETVAR(percent_name, (Name) NULL, false);
	(void) SETVAR(query, (Name) NULL, false);
        if (query_list != NULL) {
        	delete_query_chain(query_list);
        }
	(void) SETVAR(hat, (Name) NULL, false);
        if (hat_list != NULL) {
        	delete_query_chain(hat_list);
        }

	if (conditional_macro_used) {
		target->conditional_macro_list = cond_macro_list;
		cond_macro_list = NULL;
		target->depends_on_conditional = true;
	}
}

/*
 *	touch_command(line, target, result)
 *
 *	If this is an "make -t" run we do this.
 *	We touch all targets in the target group ("foo + fie:") if any.
 *
 *	Return value:
 *				Indicates if the command failed or not
 *
 *	Parameters:
 *		line		The command line to update
 *		target		The target we are touching
 *		result		Initial value for the result we return
 *
 *	Global variables used:
 *		do_not_exec_rule Indicates that -n is on
 *		silent		Do not echo commands
 */
static Doname
touch_command(register Property line, register Name target, Doname result)
{
	Name			name;
	register Chain		target_group;
	String_rec		touch_string;
	wchar_t			buffer[MAXPATHLEN];
	Name			touch_cmd;
	Cmd_line		rule;

	for (name = target, target_group = NULL; name != NULL;) {
		if (!name->is_member) {
			/*
			 * Build a touch command that can be passed
			 * to dosys(). If KEEP_STATE is on, "make -t"
			 * will save the proper command, not the
			 * "touch" in .make.state.
			 */
			INIT_STRING_FROM_STACK(touch_string, buffer);
			MBSTOWCS(wcs_buffer, "touch ");
			append_string(wcs_buffer, &touch_string, FIND_LENGTH);
			touch_cmd = name;
			if (name->has_vpath_alias_prop) {
				touch_cmd = get_prop(name->prop,
						 vpath_alias_prop)->
						   body.vpath_alias.alias;
			}
			APPEND_NAME(touch_cmd,
				      &touch_string,
				      FIND_LENGTH);
			touch_cmd = GETNAME(touch_string.buffer.start,
					    FIND_LENGTH);
			if (touch_string.free_after_use) {
				retmem(touch_string.buffer.start);
			}
			if (!silent ||
			    do_not_exec_rule &&
			    (target_group == NULL)) {
				(void) printf("%s\n", touch_cmd->string_mb);
			}
			/* Run the touch command, or simulate it */
			if (!do_not_exec_rule) {
				result = dosys(touch_cmd,
					       false,
					       false,
					       false,
					       false,
					       name);
			} else {
				result = build_ok;
			}
		} else {
			result = build_ok;
		}
		if (target_group == NULL) {
			target_group = line->body.line.target_group;
		} else {
			target_group = target_group->next;
		}
		if (target_group != NULL) {
			name = target_group->name;
		} else {
			name = NULL;
		}
	}
	return result;
}

/*
 *	update_target(line, result)
 *
 *	updates the status of a target after executing its commands.
 *
 *	Parameters:
 *		line		The command line block to update
 *		result		Indicates that build is OK so can update
 *
 *	Global variables used:
 *		do_not_exec_rule Indicates that -n is on
 *		touch		Fake the new timestamp if we are just touching
 */
void
update_target(Property line, Doname result)
{
	Name			target;
	Chain			target_group;
	Property		line2;
	timestruc_t		old_stat_time;
	Property		member;

	/*
	 * [tolik] Additional fix for bug 1063790. It was fixed
	 * for serial make long ago, but DMake dumps core when
	 * target is a symlink and sccs file is newer then target.
	 * In this case, finish_children() calls update_target()
	 * with line==NULL.
	 */
	if(line == NULL) {
		/* XXX. Should we do anything here? */
		return;
	}

	target = line->body.line.target;

	if ((result == build_ok) && (line->body.line.command_used != NULL)) {
		if (do_not_exec_rule ||
		    touch ||
		    (target->is_member &&
		     (line->body.line.command_template != NULL) &&
		     (line->body.line.command_template->command_line->string_mb[0] == 0) &&
		     (line->body.line.command_template->next == NULL))) {
			/* If we are simulating execution we need to fake a */
			/* new timestamp for the target we didnt build */
			target->stat.time = file_max_time;
		} else {
			/*
			 * If we really built the target we read the new
			 * timestamp.
			 * Fix for bug #1110906: if .c file is newer than
			 * the corresponding .o file which is in an archive
			 * file, make will compile the .c file but it won't
			 * update the object in the .a file.
			 */
			old_stat_time = target->stat.time;
			target->stat.time = file_no_time;
			(void) exists(target);
			if ((target->is_member) &&
			    (target->stat.time == old_stat_time)) {
				member = get_prop(target->prop, member_prop);
				if (member != NULL) {
					target->stat.time = member->body.member.library->stat.time;
					target->stat.time.tv_sec++;
				}
			}
		}
		/* If the target is part of a group we need to propagate the */
		/* result of the run to all members */
		for (target_group = line->body.line.target_group;
		     target_group != NULL;
		     target_group = target_group->next) {
			target_group->name->stat.time = target->stat.time;
			line2 = maybe_append_prop(target_group->name,
						  line_prop);
			line2->body.line.command_used =
			  line->body.line.command_used;
			line2->body.line.target = target_group->name;
		}
	}
	target->has_built = true;
}

/*
 *	sccs_get(target, command)
 *
 *	Figures out if it possible to sccs get a file
 *	and builds the command to do it if it is.
 *
 *	Return value:
 *				Indicates if sccs get failed or not
 *
 *	Parameters:
 *		target		Target to get
 *		command		Where to deposit command to use
 *
 *	Global variables used:
 *		debug_level	Should we trace activities?
 *		recursion_level	Used for tracing
 *		sccs_get_rule	The rule to used for sccs getting
 */
static Doname
sccs_get(register Name target, register Property *command)
{
	register int		result;
	char			link[MAXPATHLEN];
	String_rec		string;
	wchar_t			name[MAXPATHLEN];
	register wchar_t	*p;
	timestruc_t		sccs_time;
	register Property	line;
	int			sym_link_depth = 0;

	/* For sccs, we need to chase symlinks. */
        while (target->stat.is_sym_link) {
		if (sym_link_depth++ > 90) {
			fatal(gettext("Can't read symbolic link `%s': Number of symbolic links encountered during path name traversal exceeds 90."),
			      target->string_mb);
		}
                /* Read the value of the link. */
                result = readlink_vroot(target->string_mb,
					link,
					sizeof(link),
					NULL,
					VROOT_DEFAULT);
                if (result == -1) {
                        fatal(gettext("Can't read symbolic link `%s': %s"),
                              target->string_mb, errmsg(errno));
		}
		link[result] = 0;
                /* Use the value to build the proper filename. */
                INIT_STRING_FROM_STACK(string, name);

		Wstring wcb(target);
                if ((link[0] != slash_char) &&
                    ((p = (wchar_t *) wcsrchr(wcb.get_string(), slash_char)) != NULL)) {
                        append_string(wcb.get_string(), &string, p - wcb.get_string() + 1);
		}
                append_string(link, &string, result);
                /* Replace the old name with the translated name. */
		target = normalize_name(string.buffer.start, string.text.p - string.buffer.start);
                (void) exists(target);
                if (string.free_after_use) {
                        retmem(string.buffer.start);
		}
        }

	/*
	 * read_dir() also reads the ?/SCCS dir and saves information
	 * about which files have SCSC/s. files.
	 */
	if (target->stat.has_sccs == DONT_KNOW_SCCS) {
		read_directory_of_file(target);
	}
	switch (target->stat.has_sccs) {
	case DONT_KNOW_SCCS:
		/* We dont know by now there is no SCCS/s.* */
		target->stat.has_sccs = NO_SCCS;
	case NO_SCCS:
		/*
		 * If there is no SCCS/s.* but the plain file exists,
		 * we say things are OK.
		 */
		if (target->stat.time > file_doesnt_exist) {
			return build_ok;
		}
		/* If we cant find the plain file, we give up. */
		return build_dont_know;
	case HAS_SCCS:
		/*
		 * Pay dirt. We now need to figure out if the plain file
		 * is out of date relative to the SCCS/s.* file.
		 */
		sccs_time = exists(get_prop(target->prop,
					    sccs_prop)->body.sccs.file);
		break;
	}

	if ((!target->has_complained &&
	    (sccs_time != file_doesnt_exist) &&
	    (sccs_get_rule != NULL))) {
		/* only checking */
		if (command == NULL) {
			return build_ok;
		}
		/*
		 * We provide a command line for the target. The line is a
		 * "sccs get" command from default.mk.
		 */
		line = maybe_append_prop(target, line_prop);
		*command = line;
		if (sccs_time > target->stat.time) {
			/*
			 * And only if the plain file is out of date do we
			 * request execution of the command.
			 */
			line->body.line.is_out_of_date = true;
			if (debug_level > 0) {
				(void) printf(gettext("%*sSccs getting %s because s. file is younger than source file\n"),
					      recursion_level,
					      "",
					      target->string_mb);
			}
		}
		line->body.line.sccs_command = true;
		line->body.line.command_template = sccs_get_rule;
		if(!svr4 && (!allrules_read || posix)) {
		   if((target->prop) &&
		      (target->prop->body.sccs.file) &&
		      (target->prop->body.sccs.file->string_mb)) {
		      if((strlen(target->prop->body.sccs.file->string_mb) ==
			strlen(target->string_mb) + 2) &&
		        (target->prop->body.sccs.file->string_mb[0] == 's') &&
		        (target->prop->body.sccs.file->string_mb[1] == '.')) {

		         line->body.line.command_template = get_posix_rule;
		      }
		   }
		}
		line->body.line.target = target;
		/*
		 * Also make sure the rule is build with $* and $<
		 * bound properly.
		 */
		line->body.line.star = NULL;
		line->body.line.less = NULL;
		line->body.line.percent = NULL;
		return build_ok;
	}
	return build_dont_know;
}

/*
 *	read_directory_of_file(file)
 *
 *	Reads the directory the specified file lives in.
 *
 *	Parameters:
 *		file		The file we need to read dir for
 *
 *	Global variables used:
 *		dot		The Name ".", used as the default dir
 */
void
read_directory_of_file(register Name file)
{

	Wstring file_string(file);
	wchar_t * wcb = file_string.get_string();
	wchar_t usr_include_buf[MAXPATHLEN];
	wchar_t usr_include_sys_buf[MAXPATHLEN];

	register Name		directory = dot;
	register wchar_t	*p = (wchar_t *) wcsrchr(wcb,
							(int) slash_char);
	register int		length = p - wcb;
	static Name		usr_include;
	static Name		usr_include_sys;

	if (usr_include == NULL) {
		MBSTOWCS(usr_include_buf, "/usr/include");
		usr_include = GETNAME(usr_include_buf, FIND_LENGTH);
		MBSTOWCS(usr_include_sys_buf, "/usr/include/sys");
		usr_include_sys = GETNAME(usr_include_sys_buf, FIND_LENGTH);
	}

	/*
	 * If the filename contains a "/" we have to extract the path
	 * Else the path defaults to ".".
	 */
	if (p != NULL) {
		/*
		 * Check some popular directories first to possibly
		 * save time. Compare string length first to gain speed.
		 */
		if ((usr_include->hash.length == length) &&
		    IS_WEQUALN(usr_include_buf,
			       wcb,
			       length)) {
			directory = usr_include;
		} else if ((usr_include_sys->hash.length == length) &&
		           IS_WEQUALN(usr_include_sys_buf,
		                      wcb,
		                      length)) {
			directory = usr_include_sys;
		} else {
			directory = GETNAME(wcb, length);
		}
	}
	(void) read_dir(directory,
			(wchar_t *) NULL,
			(Property) NULL,
			(wchar_t *) NULL);
}

/*
 *	add_pattern_conditionals(target)
 *
 *	Scan the list of conditionals defined for pattern targets and add any
 *	that match this target to its list of conditionals.
 *
 *	Parameters:
 *		target		The target we should add conditionals for
 *
 *	Global variables used:
 *		conditionals	The list of pattern conditionals
 */
static void
add_pattern_conditionals(register Name target)
{
	register Property	conditional;
	Property		new_prop;
	Property		*previous;
	Name_rec		dummy;
	wchar_t			*pattern;
	wchar_t			*percent;
	int			length;

	Wstring wcb(target);
	Wstring wcb1;

	for (conditional = get_prop(conditionals->prop, conditional_prop);
	     conditional != NULL;
	     conditional = get_prop(conditional->next, conditional_prop)) {
		wcb1.init(conditional->body.conditional.target);
		pattern = wcb1.get_string();
		if (pattern[1] != 0) {
			percent = (wchar_t *) wcschr(pattern, (int) percent_char);
			/* Check for possible buffer under-read */
			if ((length = wcb.length()-wcslen(percent+1)) <= 0) {
				continue;
			}
			if (!wcb.equaln(pattern, percent-pattern) ||
			    !IS_WEQUAL(wcb.get_string(length), percent+1)) {
				continue;
			}
		}
		for (previous = &target->prop;
		     *previous != NULL;
		     previous = &(*previous)->next) {
			if (((*previous)->type == conditional_prop) &&
			    ((*previous)->body.conditional.sequence >
			     conditional->body.conditional.sequence)) {
				break;
			}
		}
		if (*previous == NULL) {
			new_prop = append_prop(target, conditional_prop);
		} else {
			dummy.prop = NULL;
			new_prop = append_prop(&dummy, conditional_prop);
			new_prop->next = *previous;
			*previous = new_prop;
		}
		target->conditional_cnt++;
		new_prop->body.conditional = conditional->body.conditional;
	}
}

/*
 *	set_locals(target, old_locals)
 *
 *	Sets any conditional macros for the target.
 *	Each target carries a possibly empty set of conditional properties.
 *
 *	Parameters:
 *		target		The target to set conditional macros for
 *		old_locals	Space to store old values in
 *
 *	Global variables used:
 *		debug_level	Should we trace activity?
 *		is_conditional	We need to preserve this value
 *		recursion_level	Used for tracing
 */
void
set_locals(register Name target, register Property old_locals)
{
	register Property	conditional;
	register int		i;
	register Boolean	saved_conditional_macro_used;
	Chain			cond_name;
	Chain			cond_chain;

	if (target->dont_activate_cond_values) {
		return;
	}

	saved_conditional_macro_used = conditional_macro_used;

	/* Scan the list of conditional properties and apply each one */
	for (conditional = get_prop(target->prop, conditional_prop), i = 0;
	     conditional != NULL;
	     conditional = get_prop(conditional->next, conditional_prop),
	     i++) {
		/* Save the old value */
		old_locals[i].body.macro =
		  maybe_append_prop(conditional->body.conditional.name,
				    macro_prop)->body.macro;
		if (debug_level > 1) {
			(void) printf(gettext("%*sActivating conditional value: "),
				      recursion_level,
				      "");
		}
		/* Set the conditional value. Macros are expanded when the */
		/* macro is refd as usual */
		if ((conditional->body.conditional.name != virtual_root) ||
		    (conditional->body.conditional.value != virtual_root)) {
			(void) SETVAR(conditional->body.conditional.name,
				      conditional->body.conditional.value,
				      (Boolean) conditional->body.conditional.append);
		}
		cond_name = ALLOC(Chain);
		cond_name->name = conditional->body.conditional.name;
	}
	/* Put this target on the front of the chain of conditional targets */
	cond_chain = ALLOC(Chain);
	cond_chain->name = target;
	cond_chain->next = conditional_targets;
	conditional_targets = cond_chain;
	conditional_macro_used = saved_conditional_macro_used;
}

/*
 *	reset_locals(target, old_locals, conditional, index)
 *
 *	Removes any conditional macros for the target.
 *
 *	Parameters:
 *		target		The target we are retoring values for
 *		old_locals	The values to restore
 *		conditional	The first conditional block for the target
 *		index		into the old_locals vector
 *	Global variables used:
 *		debug_level	Should we trace activities?
 *		recursion_level	Used for tracing
 */
void
reset_locals(register Name target, register Property old_locals, register Property conditional, register int index)
{
	register Property	this_conditional;
	Chain			cond_chain;

	if (target->dont_activate_cond_values) {
		return;
	}

	/* Scan the list of conditional properties and restore the old value */
	/* to each one Reverse the order relative to when we assigned macros */
	this_conditional = get_prop(conditional->next, conditional_prop);
	if (this_conditional != NULL) {
		reset_locals(target, old_locals, this_conditional, index+1);
	} else {
		/* Remove conditional target from chain */
		if (conditional_targets == NULL ||
		    conditional_targets->name != target) {
			warning(gettext("Internal error: reset target not at head of condtional_targets chain"));
		} else {
			cond_chain = conditional_targets->next;
			retmem_mb((caddr_t) conditional_targets);
			conditional_targets = cond_chain;
		}
	}
	get_prop(conditional->body.conditional.name->prop,
		 macro_prop)->body.macro = old_locals[index].body.macro;
	if (conditional->body.conditional.name == virtual_root) {
		(void) SETVAR(virtual_root, getvar(virtual_root), false);
	}
	if (debug_level > 1) {
		if (old_locals[index].body.macro.value != NULL) {
			(void) printf(gettext("%*sdeactivating conditional value: %s= %s\n"),
				      recursion_level,
				      "",
				      conditional->body.conditional.name->
				      string_mb,
				      old_locals[index].body.macro.value->
				      string_mb);
		} else {
			(void) printf(gettext("%*sdeactivating conditional value: %s =\n"),
				      recursion_level,
				      "",
				      conditional->body.conditional.name->
				      string_mb);
		}
	}
}

/*
 *	check_auto_dependencies(target, auto_count, automatics)
 *
 *	Returns true if the target now has a dependency
 *	it didn't previously have (saved on automatics).
 *
 *	Return value:
 *				true if new dependency found
 *
 *	Parameters:
 *		target		Target we check
 *		auto_count	Number of old automatic vars
 *		automatics	Saved old automatics
 *
 *	Global variables used:
 *		keep_state	Indicates that .KEEP_STATE is on
 */
Boolean
check_auto_dependencies(Name target, int auto_count, Name *automatics)
{
	Name		*p;
	int		n;
	Property	line;
	Dependency	dependency;

	if (keep_state) {
		if ((line = get_prop(target->prop, line_prop)) == NULL) {
			return false;
		}
		/* Go thru new list of automatic depes */
		for (dependency = line->body.line.dependencies;
		     dependency != NULL;
		     dependency = dependency->next) {
			/* And make sure that each one existed before we */
			/* built the target */
			if (dependency->automatic && !dependency->stale) {
				for (n = auto_count, p = automatics;
				     n > 0;
				     n--) {
					if (*p++ == dependency->name) {
						/* If we can find it on the */
						/* saved list of autos we */
						/* are OK  */
						goto not_new;
					}
				}
				/* But if we scan over the old list */
				/* of auto. without finding it it is */
				/* new and we must check it */
				return true;
			}
		not_new:;
		}
		return false;
	} else {
		return false;
	}
}


// Recursively delete each of the Chain struct on the chain.

static void
delete_query_chain(Chain ch)
{
	if (ch == NULL) {
		return;
	} else {
		delete_query_chain(ch->next);
		retmem_mb((char *) ch);
	}
}

Doname
target_can_be_built(register Name target) {
	Doname		result = build_dont_know;
	Name		true_target = target;
	Property	line;

	if (target == wait_name) {
		return(build_ok);
	}
	/*
	 * If the target is a constructed one for a "::" target,
	 * we need to consider that.
	 */
	if (target->has_target_prop) {
		true_target = get_prop(target->prop,
				       target_prop)->body.target.target;
	}

	(void) exists(true_target);

	if (true_target->state == build_running) {
		return(build_running);
	}
	if (true_target->stat.time != file_doesnt_exist) {
		result = build_ok;
	}

	/* get line property for the target */
	line = get_prop(true_target->prop, line_prop);

	/* first check for explicit rule */
	if (line != NULL && line->body.line.command_template != NULL) {
		result = build_ok;
	}
	/* try to find pattern rule */
	if (result == build_dont_know) {
		result = find_percent_rule(target, NULL, false);
	}

	/* try to find double suffix rule */
	if (result == build_dont_know) {
		if (target->is_member) {
			Property member = get_prop(target->prop, member_prop);
			if (member != NULL && member->body.member.member != NULL) {
				result = find_ar_suffix_rule(target, member->body.member.member, NULL, false);
			} else {
				result = find_double_suffix_rule(target, NULL, false);
			}
		} else {
			result = find_double_suffix_rule(target, NULL, false);
		}
	}

	/* try to find suffix rule */
	if ((result == build_dont_know) && second_pass) {
		result = find_suffix_rule(target, target, empty_name, NULL, false);
	}

	/* check for sccs */
	if (result == build_dont_know) {
		result = sccs_get(target, NULL);
	}

	/* try to find dyn target */
	if (result == build_dont_know) {
		Name dtarg = find_dyntarget(target);
		if (dtarg != NULL) {
			result = target_can_be_built(dtarg);
		}
	}

	/* check whether target was mentioned in makefile */
	if (result == build_dont_know) {
		if (target->colons != no_colon) {
			result = build_ok;
		}
	}

	/* result */
	return result;
}