File: update.c

package info (click to toggle)
smake 1.2a23-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 2,820 kB
  • ctags: 2,459
  • sloc: ansic: 14,285; sh: 2,538; makefile: 113; pascal: 41
file content (1943 lines) | stat: -rw-r--r-- 43,838 bytes parent folder | download
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
/* @(#)update.c	1.49 04/04/06 Copyright 1985, 88, 91, 1995-2004 J. Schilling */
#ifndef lint
static	char sccsid[] =
	"@(#)update.c	1.49 04/04/06 Copyright 1985, 88, 91, 1995-2004 J. Schilling";
#endif
/*
 *	Make program
 *	Macro handling / Dependency Update
 *
 *	Copyright (c) 1985, 88, 91, 1995-2004 by J. Schilling
 */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; see the file COPYING.  If not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <mconfig.h>
#include <stdio.h>
#include <standard.h>
#include <strdefs.h>
#include <schily.h>
#include "make.h"

obj_t	*default_tgt = (obj_t *)NULL;	/* Current 'make' arg or default tgt */
BOOL	found_make;			/* Did we expand the $(MAKE) macro?  */

#define	RTYPE_NONE	-1		/* Undefined type (used to init)    */
#define	RTYPE_DEFAULT	0		/* Rule from .DEFAULT: target	    */
#define	RTYPE_SSUFFIX	1		/* Simple suffix rule		    */
#define	RTYPE_SUFFIX	2		/* Single suffix rule		    */
#define	RTYPE_DSUFFIX	3		/* Double suffix rule		    */
#define	RTYPE_PATTERN	4		/* Pattern matching rule	    */

#define	RTYPE_NEEDFREE	0x1000		/* cmd_t * list needs to be free'd  */

#define	rule_type(t)	((t) & ~RTYPE_NEEDFREE)

EXPORT	void	initchars	__PR((void));
EXPORT	char	*filename	__PR((char * name));
LOCAL	void	copy_dir	__PR((char * name, char *dir));
LOCAL	char	*get_suffix	__PR((char *name, char *suffix));
LOCAL	void	copy_base	__PR((char *name, char *dir, char *suffix));
EXPORT	BOOL	isprecious	__PR((obj_t * obj));
EXPORT	BOOL	isphony		__PR((obj_t * obj));
LOCAL	obj_t	*pattern_rule	__PR((obj_t * obj));
LOCAL	obj_t	*suffix_rule	__PR((obj_t * obj, int *rtypep));
LOCAL	obj_t	*ssuffix_rule	__PR((obj_t * obj));
LOCAL	obj_t	*default_rule	__PR((obj_t * obj, int *rtypep));
EXPORT	list_t	*list_nth	__PR((list_t * list, int n));
EXPORT	BOOL	build_path	__PR((int level, char *name, char *path));
LOCAL	void	grant_gbuf	__PR((int size));
LOCAL	void	sub_put		__PR((char *chunk, int size));
LOCAL	void	sub_c_put	__PR((int c));
LOCAL	void	sub_s_put	__PR((char *chunk));
LOCAL	BOOL	sub_arg		__PR((int n, list_t * depends, obj_t * target));
EXPORT	char	*substitute	__PR((char *cmd, obj_t * obj, obj_t * source, char *suffix));
LOCAL	char	*subst		__PR((char *cmd, obj_t * obj, obj_t * source, char *suffix, list_t * depends));
LOCAL	char	*dynmac		__PR((char *cmd, obj_t * obj, char *suffix, list_t * depends, BOOL  domod));
LOCAL	void	extr_filenames	__PR((char *names));
LOCAL	void	extr_dirnames	__PR((char *names, char *basep));
LOCAL	char	*exp_var	__PR((char end, char *cmd, obj_t * obj, obj_t * source, char *suffix, list_t *depends));
LOCAL	char	*rstr		__PR((char * s1, char * s2));
LOCAL	BOOL	patsub		__PR((char *name, char *f1, char *f2, char *t1, char *t2));
LOCAL	void	patmsub		__PR((char *name, char *f1, char *f2, char *t1, char *t2));
LOCAL	void	parsepat	__PR((char *pat, char **fp1, char **fp2, char **tp1, char **tp2));
EXPORT	char	*shout		__PR((char *cmd));
LOCAL	char	*shsub		__PR((list_t * l, obj_t * obj, obj_t * source, char *suffix, list_t * depends));
#ifdef	USE_SUBPAT
LOCAL	void	subpat		__PR((obj_t *obj, obj_t *source, list_t  *depends, char *pat,
					char **fp1, char **fp2, char **tp1, char **tp2));
#endif
LOCAL	void	exp_name	__PR((char * name, obj_t * obj, obj_t * source, char *suffix, list_t * depends, char *));
LOCAL	date_t	searchobj	__PR((obj_t * obj, int maxlevel, int mode));
LOCAL	obj_t	*patr_src	__PR((char *name, patr_t * prule, int *rtypep, char ** suffixp, cmd_t ** pcmd));
LOCAL	obj_t	*suff_src	__PR((char *name, obj_t * rule, int *rtypep, char ** suffixp, cmd_t ** pcmd));
LOCAL	obj_t	*one_suff_src	__PR((char *name, char *suffix, cmd_t **pcmd));
LOCAL	obj_t	*ssuff_src	__PR((char *name, obj_t * rule, int *rtypep, char ** suffixp, cmd_t ** pcmd));
LOCAL	obj_t	*findsrc	__PR((obj_t *obj, obj_t * rule, int *rtypep, char ** suffixp, cmd_t ** pcmd));
LOCAL	date_t	default_cmd	__PR((obj_t * obj, char *depname, date_t  deptime, int deplevel, BOOL  must_exist));
LOCAL	date_t	make		__PR((obj_t * obj, BOOL lust_exist));
EXPORT	BOOL	domake		__PR((char *name));
EXPORT	BOOL	omake		__PR((obj_t * obj, BOOL  must_exist));
EXPORT	BOOL	xmake		__PR((char *name, BOOL  must_exist));

char	chartype[256];

/*
 * Optimise character classification
 */
EXPORT void
initchars()
{
	char	*p;
	int	c;

	p = "@*<0123456789r^?";

	while ((c = *p++) != '\0') {
		chartype[c] |= DYNCHAR;
	}

	p = "0123456789";

	while ((c = *p++) != '\0') {
		chartype[c] |= NUMBER;
	}
}

/*
 * Return last pathname component.
 */
EXPORT char *
filename(name)
	register char	*name;
{
	register char	*fname;

	for (fname = name; *name; )
		if (*name++ == SLASH)
			fname = name;
	return (fname);
}

/*
 * Copy directory component of pathname.
 */
LOCAL void
copy_dir(name, dir)
	register char	*name;
	register char	*dir;
{
	register char	*p = filename(name);

	if (XDebug > 0)
		error("copy_dir(name:'%s', dir:'%s') fn: '%s' \n", name, dir, p);
	*dir = '\0';
	if (p == name) {
		*dir++ = '.';
		*dir = '\0';
	} else {
		while (name < p)
			*dir++ = *name++;
		*--dir = '\0';		/* POSIX wants the '/' to be removed */
					/* This will make dir(/filename)    */
					/* not usable...		    */
		*dir = '\0';
	}
}

/*
 * Return part after '.' of last pathname component.
 */
LOCAL char *
get_suffix(name, suffix)
	char	*name;
	char	*suffix;
{
	register char	*p;
	register char	*suff = (char *)NULL;

	if (suffix != NULL) {
		p = filename(name);
		suff = rstr(p, suffix);
		if (suff == (char *)NULL) /* No suffix: return end of string */
			suff = &p[strlen(p)];
		return (suff);
	}

	for (p = filename(name); *p; p++)
		if (*p == '.')
			suff = p;
	if (suff == (char *)NULL)	/* No suffix: return end of string */
		suff = p;
	return (suff);
}

/*
 * Copy namebase (everything before '.').
 */
LOCAL void
copy_base(name, dir, suffix)
	register char	*name;
	register char	*dir;
		char	*suffix;
{
	register char	*p = get_suffix(name, suffix);

	while (name < p)
		*dir++ = *name++;
	*dir = '\0';
}

/*
 * Return TRUE if 'obj' is in the list of targets that should not be removed.
 */
EXPORT BOOL
isprecious(obj)
	obj_t	*obj;
{
	list_t	*l;

	if (Precious == (obj_t *)NULL)
		return (FALSE);

	for (l = Precious->o_list; l; l = l->l_next)
		if (obj == l->l_obj)
			return (TRUE);
	return (FALSE);
}

/*
 * Return TRUE if 'obj' is in the list of targets that should not be checked
 * aginst existing files. A .PHONY target is asumed to be never up to date,
 * it is not removed in case a signal is received.
 */
EXPORT BOOL
isphony(obj)
	obj_t	*obj;
{
	list_t	*l;

	if (Phony == (obj_t *)NULL)
		return (FALSE);

	for (l = Phony->o_list; l; l = l->l_next)
		if (obj == l->l_obj)
			return (TRUE);
	return (FALSE);
}

/*
 * Find pattern rule for 'obj' ... not yet ready.
 */
LOCAL obj_t *
pattern_rule(obj)
	register obj_t	*obj;
{
		char	*name;
		char	*p;
	register patr_t *prule;
#ifdef	DEBUG
	register cmd_t	*cmd;
#endif

	if (Patrules == NULL)
		return ((obj_t *)0);

	if (Debug > 1)
		printf("Searching pattern rule for: %s \n", obj->o_name);

	name = obj->o_name;
	for (prule = Patrules; prule != NULL; prule = prule->p_next) {
		name = obj->o_name;
		/*
		 * XXX NeXT Step has a buggy strstr(); returns NULL if p == ""
		 */
		p = (char *)prule->p_tgt_prefix;

		if (*p != '\0' && strstr(name, p) != name)
			continue;		/* no matching prefix */

		name += strlen(p);		/* strip matching prefix */

		if ((p = rstr(name, (char *)prule->p_tgt_suffix)) == NULL)
			continue;

#ifdef	DEBUG
		if (Debug > 1) {
			printf("name: %s (%s %% %s): (%s %% %s)\n", obj->o_name,
				prule->p_tgt_prefix, prule->p_tgt_suffix,
				prule->p_src_prefix, prule->p_src_suffix);
		}
		if (Debug > 1) {
			for (cmd = prule->p_cmd; cmd; cmd = cmd->c_next) {
				printf("\t%s\n", cmd->c_line);
			}
		}
#endif
		break;
	}
	return ((obj_t *)prule);	/* XXX Hack for now, should return prule */
}

/*
 * Find a POSIX suffix rule.
 *
 * Check if obj has a file name with a default dependency for the
 * corresponding source and a rule to compile it.
 */
LOCAL obj_t *
suffix_rule(obj, rtypep)
	register obj_t	*obj;
		int	*rtypep;
{
	list_t	*l;
	list_t	*l2;
	obj_t	*o;
	char	*suffix;
	char	rulename[NAMEMAX*2];
	BOOL	found_suffix = FALSE;

	if (Suffixes == NULL)
		return ((obj_t *)0);

	if (Debug > 1)
		printf("Searching double suffix rule for: %s \n", obj->o_name);

	for (l = Suffixes; l; l = l->l_next) {
		suffix = l->l_obj->o_name;
		if (rstr(obj->o_name, suffix)) {	/* may be a suffix */

			found_suffix = TRUE;

			for (l2 = Suffixes; l2; l2 = l2->l_next) {
				strcatl(rulename, l2->l_obj->o_name, suffix, (char *)NULL);
				if ((o = objlook(rulename, FALSE)) != NULL && o->o_type == COLON) {
					*rtypep = RTYPE_DSUFFIX;
					return (o);
				}
			}
		}
	}
	if (found_suffix)
		return ((obj_t *) 0);

	if (Debug > 1)
		printf("Searching single suffix rule for: %s \n", obj->o_name);

	for (l2 = Suffixes; l2; l2 = l2->l_next) {
		strcatl(rulename, l2->l_obj->o_name, (char *)NULL);
		if ((o = objlook(rulename, FALSE)) != NULL && o->o_type == COLON) {
			*rtypep = RTYPE_SUFFIX;
			return (o);
		}
	}
	return ((obj_t *) 0);
}

/*
 * Find a simple suffix rule.
 *
 * Check if obj has a file name with a default dependency for the
 * corresponding source and a rule to compile it.
 */
LOCAL obj_t *
ssuffix_rule(obj)
	register obj_t	*obj;
{
	register obj_t *rule;
		char	*ext;

	if (!SSuffrules)
		return ((obj_t *)0);

	if (Debug > 1)
		printf("Searching simple suffix rule for: %s \n", obj->o_name);

	ext = get_suffix(obj->o_name, (char *)0); /* Use '.' (dot) suffix only*/

	if (ext[0] == '\0') {
		if (obj->o_list == (list_t *)NULL) {
			ext = "\"\"";		/* obj has no suffix: use "" */
		} else {
			return ((obj_t *)NULL);	/* obj has dependency list   */
		}
	}
	rule = ssufflook(ext, FALSE);
	if (rule == (obj_t *)NULL ||		/* no default rules known   */
	    rule->o_list == (list_t *)NULL ||	/* no source suffix list    */
	    rule->o_cmd == (cmd_t *)NULL)	/* no commands defined	    */
		return ((obj_t *)NULL);
	return (rule);
}

/*
 * Check if a default rules exists for the target.
 */
LOCAL obj_t *
default_rule(obj, rtypep)
	obj_t	*obj;
	int	*rtypep;
{
	obj_t *rule;

#ifdef	NO_SLASH_IMPLICIT
	if (strchr(obj->o_name, SLASH) != NULL) {
		if (Debug > 3)
			error("%s has slash, no implicit dependency searched.\n",
							obj->o_name);
		/*
		 * XXX We need to check if this is a good idea.
		 */
		rule = (obj_t *)NULL;
	}
#endif
	rule = pattern_rule(obj);
	if (rule) {
		*rtypep = RTYPE_PATTERN;
		return (rule);
	}

	rule = suffix_rule(obj, rtypep);
	if (rule) {
		return (rule);
	}

	rule = ssuffix_rule(obj);
	if (rule) {
		*rtypep = RTYPE_SSUFFIX;
		return (rule);
	}

	/*
	 * XXX Must exits wichtig ??
	 */
	*rtypep = RTYPE_DEFAULT;
	rule = Deflt;	/* .DEFAULT:	*/

	if (rule == (obj_t *)NULL || rule->o_cmd == (cmd_t *)NULL)
		return ((obj_t *)NULL);
	return ((obj_t *)rule->o_cmd);	/* XXX Hack for now, should return drule */
}

/*
 * Return the nth element of a list.
 */
EXPORT list_t *
list_nth(list, n)
	register list_t *list;
		int	n;
{
	for (; list; list = list->l_next)
		if (--n < 0)
			return (list);
	return ((list_t *)NULL);
}

/*
 * Create a new file name from name and the n'th directory in SearchList.
 * SearchList lists sourcedirs before objdirs, starting with
 * n = 0 for '.' and n = 1 for ObjDir.
 */
EXPORT BOOL
build_path(level, name, path)
	int	level;
	char	*name;
	char	*path;
{
	list_t *lp;
	char	*dirname = (char *)NULL;
	register int n = level;

	if (n <= 1) {
		if (level == OBJLEVEL)
			dirname = ObjDir;
	} else if (level != MAXLEVEL) {
		if ((lp = list_nth(SearchList, n - 2)) == (list_t *)NULL)
			return (FALSE);
		dirname = lp->l_obj->o_name;
	}
	if (dirname != (char *)NULL)
		path = strcatl(path, dirname, slash, (char *)NULL);
	strcpy(path, name);
	return (TRUE);
}


/*
 * The growable buffer (gbuf) defines a string with the following layout
 *      "xxxxxxxxxxxxxxxCxxxxxxxxxxxxxxx________"
 *      ^               ^               ^               ^
 *      |               |               |               |
 *      gbuf            textp           sub_ptr         gbufend
 *      textp points to a string that is currently been worked on,
 *	sub_ptr is the write pointer.
 */
static char *sub_ptr = (char *)NULL;

LOCAL void
grant_gbuf(size)
	int	size;
{
	while (sub_ptr + size >= gbufend)
		sub_ptr = growgbuf(sub_ptr);
}

/*
 * Put a string bounded by size into the growable buffer.
 */
LOCAL void
sub_put(chunk, size)
	char	*chunk;
	int	size;
{
	grant_gbuf(size);
	movebytes(chunk, sub_ptr, size);
	sub_ptr += size;
}

/*
 * Put a single character into the growable buffer.
 */
LOCAL void
sub_c_put(c)
	char	c;
{
	grant_gbuf(1);
	*sub_ptr++ = c;
}

/*
 * Put a string bounded by strlen() into the growable buffer.
 */
LOCAL void
sub_s_put(chunk)
	char	*chunk;
{
	sub_put(chunk, strlen(chunk));
}

/*
 * Put one arg into the growable buffer.
 *
 * It target is nonzero, check in addition if the target
 * depends on the date of the currently selected obj too.
 *
 * Return FALSE if no more list elements are available.
 */
LOCAL BOOL
sub_arg(n, depends, target)
	int	n;
	list_t	*depends;
	obj_t	*target;
{
	register obj_t	*obj;
		char	arg[NAMEMAX*2];

	if ((depends = list_nth(depends, n)) == (list_t *)NULL)
		return (FALSE);

	/*
	 * $0 is not available if no implicit source is present!
	 * Just skip it.
	 */
	if ((obj =
	    depends->l_obj) == (obj_t *)NULL)
		return (TRUE);

	if (target != NULL && target->o_date > obj->o_date)
		return (TRUE);

	if (build_path(obj->o_level, obj->o_name, arg)) {
		sub_s_put(arg);
	} else {
		sub_s_put(obj->o_name);
	}
	return (TRUE);
}

/*
 * Do macro substitution. Substitution is done in the growable buffer buf.
 * The buffer is used as stack to allow recursive substitution.
 */
EXPORT char *
substitute(cmd, obj, source, suffix)
	register char	*cmd;
		obj_t	*obj;
		obj_t	*source;
		char	*suffix;
{
		list_t	depends;

	found_make = FALSE;			/* we did not expand $(MAKE) */
	depends.l_obj = source;			/* define implicit source $< */
	depends.l_next = obj->o_list;

	sub_ptr = gbuf;
	return (subst(cmd, obj, source, suffix, &depends));
}

static int	depth = 0;			/* Keep track of recursion   */

/*
 * Substitute macros.
 */
		/* source wird eigentlich nicht gebraucht */
LOCAL char *
subst(cmd, obj, source, suffix, depends)
	register char	*cmd;
		obj_t	*obj;
		obj_t	*source;
		char	*suffix;
		list_t	*depends;
{
		char	*sp = sub_ptr;
		char	*sb = gbuf;
	register char	*p;
		char	name[2];

	if (++depth > 100)
		comerrno(EX_BAD, "Recursion in macro '%s'.\n", cmd);

	name[1] = '\0';
	while ((p = strchr(cmd, '$')) != NULL) {
		sub_put(cmd, p - cmd);
		cmd = ++p;
		switch (*cmd++) {

		default:
			if (chartype[*(Uchar *)p] & DYNCHAR) {
				cmd = dynmac(p, obj, suffix, depends, FALSE);
				continue;
			}
			name[0] = cmd[-1];
			exp_name(name, obj, source, suffix, depends, Nullstr);
			break;
		case '\0':
			*sub_ptr = '\0';
			if (sb != gbuf)
				sp = gbuf + (sp - sb);
			comerrno(EX_BAD,
				"Fatal error '$' at end of string '%s$'\n",
									sp);
		case '$':
			sub_c_put('$');
			break;
		case '(':
			cmd = exp_var(')', cmd, obj, source, suffix, depends);
			break;
		case '{':
			cmd = exp_var('}', cmd, obj, source, suffix, depends);
			break;
		}
	}
	sub_s_put(cmd);
	*sub_ptr = '\0';
	depth--;
	if (sb != gbuf)
		sp = gbuf + (sp - sb);
	return (sp);
}

/*
 * Substitute dynamic macros.
 */
LOCAL char *
dynmac(cmd, obj, suffix, depends, domod)
	char	*cmd;
	obj_t	*obj;
	char	*suffix;
	list_t	*depends;
	BOOL	domod;
{
	int	num;
	char	base[NAMEMAX];
	char	*sp = sub_ptr;
	register char	*p = cmd;

	switch (*cmd++) {

	default:
		return (cmd);

	case '@':
		sub_s_put(obj->o_name);		/* $@ -> full target name */
		break;
	case '*':
		copy_base(filename(obj->o_name), base, suffix);
		sub_s_put(base);		/* $* -> target name base */
		break;
	case '<':
		sub_arg(0, depends, (obj_t *)0); /* $< -> implicit source  */
		break;
	case '0': case '1': case '2': case '3': case '4':
	case '5': case '6': case '7': case '8': case '9':
		cmd = astoi(p, &num);		/* $1 -> first dependency */
		sub_arg(num, depends, (obj_t *)0); /* $0 -> implicit source  */
		break;
	case 'r':
		/*
		 * $r0 -> all dependencies + implicit source
		 * $r1 -> all dependencies
		 */
		cmd = astoi(cmd, &num);
		while (sub_arg(num++, depends, (obj_t *)0))
			sub_c_put(' ');
		break;
	case '^':
		num = 1;			/* $^ -> all dependencies */
		while (sub_arg(num++, depends, (obj_t *)0))
			sub_c_put(' ');
		break;
	case '?':
		num = 1;			/* $? -> outdated dependencies*/
		while (sub_arg(num++, depends, obj))
			sub_c_put(' ');
		break;
	}
	*sub_ptr = '\0';
	if (!domod)
		return (cmd);
	if (*cmd == 'F') {
		extr_filenames(sp);
		return (++cmd);
	}
	if (*cmd == 'D') {
		extr_dirnames(sp, base);
		return (++cmd);
	}
	return (cmd);
}

/*
 * Extract the filename parts from a string that contains a list of names.
 */
LOCAL void
extr_filenames(names)
	char	*names;
{
	char	*p;
	char	*np;
	char	*s;
	char	*sp;
	char	*sb;

	sp = ++sub_ptr;
	sb = gbuf;
	/*
	 * Make sure that gbu has enough room for the copy.
	 */
	grant_gbuf(sub_ptr - names);
	if (sb != gbuf)
		sp = gbuf + (sp - sb);

	for (np = p = names, s = sp; np && *np; p = np) {
		np = strchr(np, ' ');
		if (np)
			*np++ = '\0';
		p = filename(p);
		while (*p)
			*s++ = *p++;
		if (np)
			*s++ = ' ';
	}
	*s = '\0';
	/*
	 * Now copy down from uppe part of gbuf.
	 */
	for (s = names, p = sp; *p; )
		*s++ = *p++;
	*s = '\0';
	sub_ptr = s;
}

/*
 * Extract the directory parts from a string that contains a list of names.
 */
LOCAL void
extr_dirnames(names, basep)
	char	*names;
	char	*basep;
{
	char	*p;
	char	*np;
	char	*s;
	char	*sp;
	char	*sb;

	sp = ++sub_ptr;
	sb = gbuf;
	/*
	 * Make sure that gbu has enough room for the copy.
	 */
	grant_gbuf(sub_ptr - names);
	if (sb != gbuf)
		sp = gbuf + (sp - sb);

	for (np = p = names, s = sp; np && *np; p = np) {
		np = strchr(np, ' ');
		if (np)
			*np++ = '\0';
		copy_dir(p, basep);
		p = basep;
		while (*p)
			*s++ = *p++;
		if (np)
			*s++ = ' ';
	}
	*s = '\0';
	/*
	 * Now copy down from uppe part of gbuf.
	 */
	for (s = names, p = sp; *p; )
		*s++ = *p++;
	*s = '\0';
	sub_ptr = s;
}

#define	white(c)	((c) == ' ' || (c) == '\t')

/*
 * Expand a macro.
 * As the replacement may be a suffix rule or a pattern rule too,
 * we first must get the basic name the macro referres to.
 */
#ifdef	PROTOTYPES
LOCAL char *
exp_var(
	register char	end,
	char		*cmd,
	obj_t		*obj,
	obj_t		*source,
	char		*suffix,
	list_t		*depends)
#else
LOCAL char *
exp_var(end, cmd, obj, source, suffix, depends)
	register char	end;
		char	*cmd;
		obj_t	*obj;
		obj_t	*source;
		char	*suffix;
		list_t	*depends;
#endif
{
		char	name[NAMEMAX];
		char	pat[NAMEMAX];
	register char	beg = cmd[-1];
	register char	*s = cmd;
	register char	*rname = name;
	register char	ch;
	register int	nestlevel = 0;
		BOOL	funccall = FALSE;

/*error("end: %c cmd: %.50s\n", end, cmd);*/
	pat[0] = '\0';
	while ((ch = *s) && rname < (name+NAMEMAX-2) && ch != ':' && !white(ch)) {
		if (ch == beg)
			nestlevel++;
		if (ch == end)
			nestlevel--;
		if (nestlevel < 0) {
/*printf("name: %s\n", name);*/
			break;
		}
		*rname++ = *s++;
	}
	*rname = '\0';
	if (ch == ' ')
		funccall = TRUE;

	if (*s != end && *s != ':' && *s != ' ') {
		comerrno(EX_BAD, "Missing '%c' in macro call '%s'\n", end, name);
/*		return (cmd);*/
	}

	if (*s == ':' || *s == ' ') {
		rname = pat;
		if (funccall) {
			while (*s && white(*s))
				s++;
		} else {
			s++;
		}
		while ((ch = *s) && rname < (pat+NAMEMAX-2)) {
			if (ch == beg)
				nestlevel++;
			if (ch == end)
				nestlevel--;
			if (nestlevel < 0) {
/*printf("name: %s\n", name);*/
				break;
			}
			*rname++ = *s++;
		}
		*rname = '\0';
		if (nestlevel >= 0)
			comerrno(EX_BAD, "Missing '%c' in macro call '%s%c%s'\n",
					end, name, funccall?' ':':', pat);
	}
	if (*s)
		s++;

	if (name[0] == 'M' && streql(name, "MAKE"))
		found_make = TRUE;

	/*
	 * If the name of the macro contains a '$', resursively expand it.
	 * We need to check if we should rather expand anything between the
	 * brackets (e.g {...}) however, this may fail to expand long lists.
	 * See also comment in exp_name() regarding USE_SUBPAT
	 */
	if (strchr(name, '$')) {
		char	*sp = sub_ptr;
		char	*sb = gbuf;
		char	*s2;

		*sub_ptr++ = '\0';

		s2 = subst(name, obj, source, suffix, depends);
		if (sb != gbuf)
			sp = gbuf + (sp - sb);
		sub_ptr = sp;
		if (*s2) {
			strcpy(name, s2);
		}
	}

	if (funccall) {
		/*
		 * GNU type macro functions will go here.
		 */
		return (s);
	}
/*printf("name: '%s' pat: '%s'\n", name, pat);*/
	exp_name(name, obj, source, suffix, depends, pat);
	return (s);
}

/*
 * Check if s1 ends in strings s2
 */
LOCAL char *
rstr(s1, s2)
	char	*s1;
	char	*s2;
{
	int	l1;
	int	l2;

	l1 = strlen(s1);
	l2 = strlen(s2);
	if (l2 > l1)
		return ((char *)NULL);

	if (streql(&s1[l1 - l2], s2))
		return (&s1[l1 - l2]);
	return ((char *)NULL);
}

/*
 * Substitute a pattern:
 * 1)	select the part of 'name' that is surrounded by f1 & f2
 * 2a)	output the selected part from 'name' and t1 (suffix)
 * 2b)	output t2 -
 *	if t2 contains '%' substitute the selected part of 'name'
 */
LOCAL BOOL
patsub(name, f1, f2, t1, t2)
	char	*name;
	char	*f1;
	char	*f2;
	char	*t1;
	char	*t2;
{
	int	l;
	char	*p;

	/*
	 * XXX NeXT Step has a buggy strstr(); returns NULL if f1 == ""
	 * XXX f1 is guaranteed to be != NULL
	 */
/*	printf("name: '%s' f1: '%s' f2: '%s' t1: '%s' t2: '%s'\n", name, f1, f2, t1, t2);*/
	if (*f1 != '\0' && strstr(name, f1) != name)
		return (FALSE);		/* no matching prefix */

	name += strlen(f1);		/* strip matching prefix */

	if ((p = rstr(name, f2)) == NULL)
		return (FALSE);		/* no matching suffix */

	l = p - name;
	if (t1 != NULL) {		/* This is a suffix rule */
		sub_put(name, l);
		p = t1;
	} else {			/* This is a pattern rule */
		p = t2;
	}
	while (*p) {
		if (*p == '%') {
			p++;
			sub_put(name, l);
		} else {
			sub_c_put(*p++);
		}
	}
	return (TRUE);
}

#ifdef	needed
/*
 * Do pattern substitution in a macro that may only contain one word.
 */
LOCAL void
patssub(name, f1, f2, t1, t2)
	char	*name;
	char	*f1;
	char	*f2;
	char	*t1;
	char	*t2;
{
	char	*osp = name;
	char	*sp = sub_ptr;
	char	*sb = gbuf;

	*sub_ptr++ = '\0';

	if (!patsub(name, f1, f2, t1, t2)) {
		sub_ptr = sp;
		return;
	}
	*sub_ptr = '\0';
	sub_ptr = osp;
	if (sb != gbuf) {
		sp = gbuf + (sp - sb);
		sub_ptr = gbuf + (sub_ptr - sb);
	}
	sub_s_put(&sp[1]);
}
#endif

/*
 * Do pattern substitution in a macro that may contain multiple words.
 * Subtitution is applied separately to each word.
 */
LOCAL void
patmsub(name, f1, f2, t1, t2)
	char	*name;
	char	*f1;
	char	*f2;
	char	*t1;
	char	*t2;
{
	char	*osp = name;
	char	*sp = sub_ptr;
	char	*sb = gbuf;
	char	*b;

	*sub_ptr++ = '\0';

	do {
		b = name;
		while (*b != '\0' && !white(*b))
			b++;
		if (*b != '\0')
			*b++ = '\0';
		else
			b = NULL;

		if (!patsub(name, f1, f2, t1, t2)) {
			sub_s_put(name);
		}

		if (b) {
			while (*b != '\0' && white(*b))
				b++;
		}
		name = b;
		if (b)
			sub_c_put(' ');
	} while (b);
	*sub_ptr = '\0';
	sub_ptr = osp;
	if (sb != gbuf) {
		sp = gbuf + (sp - sb);
		sub_ptr = gbuf + (sub_ptr - sb);
	}
	sub_s_put(&sp[1]);
}

/*
 * Parse a pattern and divide pattern into parts.
 *
 * Check if this is a suffix rule or a pattern rule.
 *
 * If this is a suffix rule (suf1=suf2), tp2 will point to a NULL pointer,
 * if this is a pattern rule (pref1%suf1=pref1%suf2) tp1 will point to NULL.
 */
LOCAL void
parsepat(pat, fp1, fp2, tp1, tp2)
	char	*pat;
	char	**fp1;
	char	**fp2;
	char	**tp1;
	char	**tp2;
{
	char	*f1;
	char	*f2;
	char	*t1;
	char	*t2;

	t1 = strchr(pat, '=');
	if (t1 == NULL)
		comerrno(EX_BAD, "'=' missing in macro substitution.\n");
	*t1++ = '\0';

	f2 = strchr(pat, '%');		/* Find '%' in from patttern */
	if (f2 != NULL) {
		*f2++ = '\0';
		f1 = pat;
	} else {
		f2 = pat;
		f1 = Nullstr;
	}
	if (f1 == pat) {		/* This is a pattern rule */
		t2 = t1;
		t1 = NULL;
	} else {			/* This is a suffix rule */
		t2 = NULL;
	}
	*fp1 = f1;
	*fp2 = f2;
	*tp1 = t1;
	*tp2 = t2;
/*	printf("f1: '%s' f2: '%s' t1: '%s' t2: '%s'\n", f1, f2, t1, t2);*/
}

/*
 * Call shell command and capture the outpout in the growable buffer.
 */
EXPORT char *
shout(cmd)
	char	*cmd;
{
	FILE	*f;
	int	c;
	char	*sptr = sub_ptr;

	if (sub_ptr == NULL)
		sptr = sub_ptr = gbuf;

	f = popen(cmd, "r");
	if (f == NULL)
		comerr("Cannot popen '%s'.\n", cmd);

	while ((c = getc(f)) != EOF) {
		if (c == '\t' || c == '\n')
			c = ' ';
		sub_c_put(c);
	}
	if ((c = pclose(f)) != 0)
		comerr("Shell returns %d from command line.\n", c);
	*sub_ptr = '\0';
	return (sptr);
}

/*
 * Substitute a shell command output
 */
LOCAL char *
shsub(l, obj, source, suffix, depends)
	register list_t *l;
		obj_t	*obj;
		obj_t	*source;
		char	*suffix;
		list_t  *depends;
{
	char	*sptr = sub_ptr;
	char	*sbuf = gbuf;

	if (sub_ptr == NULL)
		sptr = sub_ptr = gbuf;

	for (;;) {
		subst(l->l_obj->o_name, obj, source, suffix, depends);
		if ((l = l->l_next) != NULL)
			sub_c_put(' ');
		else
			break;
	}
	*sub_ptr = '\0';
	if (sbuf != gbuf)
		sptr = gbuf + (sptr - sbuf);
	sub_ptr = sptr;

	return (shout(sptr));
}

#ifdef	USE_SUBPAT
LOCAL void
subpat(obj, source, depends, pat, fp1, fp2, tp1, tp2)
	obj_t	*obj;
	obj_t	*source;
	list_t  *depends;
	char	*pat;
	char	**fp1;
	char	**fp2;
	char	**tp1;
	char	**tp2;
{
	char	epat[NAMEMAX];
	char	*sp;
	char	*sb;

	sp = sub_ptr;
	sb = gbuf;
	subst(pat, obj, source, depends);
	if (sb != gbuf)
		sp = gbuf + (sp - sb);
	strcpy(epat, sp);
	sub_ptr = sp;
	parsepat(epat, fp1, fp2, tp1, tp2);
}
#endif

/*
 * Expand a macro for which the name is explicitely known.
 */
LOCAL void
exp_name(name, obj, source, suffix, depends, pat)
		char	*name;
		obj_t	*obj;
		obj_t	*source;
		char	*suffix;
		list_t  *depends;
		char	*pat;
{
	register list_t	*l = NULL;
		obj_t	*o = NULL;
		BOOL	ispat;
		char	epat[NAMEMAX];
		char	*f1, *f2;
		char	*t1, *t2;
		char	*sp;
		char	*sb;

	if ((chartype[*(Uchar *)name] & DYNCHAR) == 0 ||
			(*name == 'r' && (chartype[((Uchar *)name)[1]] & NUMBER) == 0)) {
		/*
		 * Allow dynamic macros to appear in $() or ${} too.
		 */
		o = objlook(name, FALSE);
		if (o == (obj_t *)NULL)
			return;
		if ((l = o->o_list) == (list_t *)NULL)
			return;
	}
	if (streql(pat, "sh")) {
		/*
		 * Allow SunPRO make compatible shell expansion as $(NAME:sh)
		 */
		if (o != NULL) {
			shsub(o->o_list, obj, source, suffix, depends);
		} else {
			sp = sub_ptr;
			sb = gbuf;
			dynmac(name, obj, suffix, depends, TRUE);
/*error("expanded1: '%s'\n", sp);*/
			if (sb != gbuf)
				sp = gbuf + (sp - sb);
			sub_ptr = sp;
			(void) shout(sp);
		}
		return;
	}
	ispat = pat[0] != '\0';
	if (ispat) {
#ifdef	USE_SUBPAT
		subpat(obj, source, depends, pat, &f1, &f2, &t1, &t2);
#else
		/*
		 * XXX Es sollte eine bessere Lsung geben (ohne static epat)
		 * XXX Aber die aktuelle subpat Implementierung kann nicht
		 * XXX Funktionieren, da das epat[] aus subpat() nicht
		 * XXX exportiert werden kann. Vielleicht geht eine Lsung
		 * XXX mit 'gbuf'.
		 */
		sp = sub_ptr;
		sb = gbuf;
		subst(pat, obj, source, suffix, depends);
		if (sb != gbuf)
			sp = gbuf + (sp - sb);
		strcpy(epat, sp);
		sub_ptr = sp;
		parsepat(epat, &f1, &f2, &t1, &t2);
#endif
	}

	if (o == NULL) {
		sp = sub_ptr;
		sb = gbuf;
		dynmac(name, obj, suffix, depends, TRUE);
/*error("expanded1: '%s'\n", sp);*/
		if (ispat) {
			if (sb != gbuf)
				sp = gbuf + (sp - sb);
			patmsub(sp, f1, f2, t1, t2);
/*error("expanded2: '%s'\n", sp);*/
		}
		return;
	}
	for (;;) {
		sp = sub_ptr;
		sb = gbuf;
		subst(l->l_obj->o_name, obj, source, suffix, depends);
/*error("expanded1: '%s'\n", sp);*/
		if (ispat) {
			if (sb != gbuf)
				sp = gbuf + (sp - sb);
			patmsub(sp, f1, f2, t1, t2);
/*error("expanded2: '%s'\n", sp);*/
		}
		if ((l = l->l_next) != NULL)
			sub_c_put(' ');
		else
			break;
/*error("expanded3: '%s'\n", sp);*/
	}
}

/*
 * Find a file in .SEARCHLIST
 * .SEARCHLIST is a list of of src/obj dir pairs
 * Before .SEARCHLIST is searched, look for "." and .OBJDIR
 */
LOCAL date_t
searchobj(obj, maxlevel, mode)
	register obj_t	*obj;
		int	maxlevel;
		int	mode;
{
		char	name[NAMEMAX*3];
	register int	level = MAXLEVEL;
		date_t	filedate;

	name[0] = '\0';			/* for clean debug print */

	if ((maxlevel & 1) == 0)	/* Source has lower level	*/
		maxlevel += 1;		/* include OBJ directory level	*/

	if (obj->o_date == PHONYTIME) {
		filedate = PHONYTIME;

	} else if (isphony(obj)) {
		obj->o_date = filedate = PHONYTIME;

	} else if (strchr(obj->o_name, SLASH) != (char *)NULL) {
		/*
		 * Do not search for pathnames.
		 */
		filedate = gftime(obj->o_name);
	} else for (level = 0, filedate = 0; level <= maxlevel; level++) {
		if (level & 1 ? mode & SOBJ : mode & SSRC) {
			if (!build_path(level, obj->o_name, name))
				break;
			if ((filedate = gftime(name)) != 0)
				break;
			if (level == WDLEVEL && ObjDir == (char *)NULL) {
				/*
				 * Working dir has just been searched don't
				 * look again if no .OBJDIR has been defined.
				 */
				level++;
			}
		}
	}
	if (filedate == 0 || filedate == PHONYTIME)
		level = MAXLEVEL;

	obj->o_date = filedate;
	obj->o_level = level;

	if (Debug > 2) {
		error("search(%s, %d, %s) = %s %s %d\n",
				obj->o_name, maxlevel, searchtype(mode),
				prtime(filedate), name, level);
	}
	if (filedate == PHONYTIME)
		return (NOTIME);
	return (filedate);
}

/*
 * Find a source file for a given object file, use pattern matching rules.
 */
LOCAL obj_t *
patr_src(name, prule, rtypep, suffixp, pcmd)
	char	*name;
	patr_t	*prule;
	int	*rtypep;
	char	**suffixp;
	cmd_t	**pcmd;
{
		char	*xname = name;
	register obj_t	*source;
		obj_t	newsource;
		char	sourcename[NAMEMAX*2];
		char	pat[NAMEMAX];
		char	*p;

	*suffixp = (char *)prule->p_tgt_suffix;

	p = (char *)prule->p_tgt_prefix;
	xname += strlen(p);		/* strip matching prefix */
/*error("name: '%s'\n", xname);*/
	p = rstr(xname, (char *)prule->p_tgt_suffix);
/*error("p: '%s'\n", p);*/
	strncpy(pat, xname, p - xname);
	pat[p-xname] = '\0';
/*error("pat: '%s'\n", pat);*/
	strcatl(sourcename, prule->p_src_prefix, pat, prule->p_src_suffix, (char *)NULL);
/*error("sourcename: '%s'\n", sourcename);*/

	newsource.o_name = sourcename;
	newsource.o_date = 0;
	newsource.o_type = 0;
	newsource.o_list = (list_t *)NULL;
	newsource.o_cmd = (cmd_t *)NULL;

	if ((source = objlook(sourcename, FALSE)) != (obj_t *)NULL) {
		make(source, TRUE);		/* source found, update it  */

	} else if (make(&newsource, FALSE) != 0) {  /* found or made source */
		source = objlook(sourcename, TRUE);
		source->o_date = newsource.o_date;
		source->o_level = newsource.o_level;
	} else {
		*pcmd = (cmd_t *)NULL;
		return ((obj_t *)0);
	}

	*pcmd = prule->p_cmd;
	return (source);
}

/*
 * Find a source file for a given object file, use POSIX suffix rules.
 * Loop over all possible source names for all possible target suffixes.
 */
LOCAL obj_t *
suff_src(name, rule, rtypep, suffixp, pcmd)
	char	*name;
	obj_t	*rule;
	int	*rtypep;
	char	**suffixp;
	cmd_t	**pcmd;
{
	register list_t	*suf;		/* List of possible suffixes */
	register obj_t	*source;
	register char	*suffix = NULL;
		BOOL	found_suffix = FALSE;

	for (suf = Suffixes; suf; suf = suf->l_next) {
		suffix = suf->l_obj->o_name;
		if (rstr(name, suffix) == NULL)	/* may not be a suffix */
			continue;

		found_suffix = TRUE;
		*suffixp = suffix;
		if ((source = one_suff_src(name, suffix, pcmd)) != NULL)
			return (source);
	}
	if (found_suffix) {
		*pcmd = (cmd_t *)NULL;
		return ((obj_t *)0);
	}
	/*
	 * XXX We should never come here.
	 */
	errmsgno(EX_BAD, "XXX Expected double suffix rule but found none.\n");
	return (one_suff_src(name, Nullstr, pcmd));
}

/*
 * Find a source file for a given object file, use POSIX suffix rules.
 * Loop over all possible source names for one given target suffix.
 */
LOCAL obj_t *
one_suff_src(name, suffix, pcmd)
	char	*name;
	char	*suffix;
	cmd_t	**pcmd;
{
	register list_t	*suf;		/* List of possible suffixes */
	register obj_t	*o;
	register obj_t	*source;
		obj_t	newsource;
		char	sourcename[NAMEMAX*2];
		char	*endbase;
		char	rulename[NAMEMAX*2];

	copy_base(name, sourcename, suffix);
	endbase = sourcename + strlen(sourcename);
	newsource.o_name = sourcename;
	newsource.o_date = 0;
	newsource.o_type = 0;
	newsource.o_list = (list_t *)NULL;
	newsource.o_cmd = (cmd_t *)NULL;

	for (suf = Suffixes; suf; suf = suf->l_next) {
		strcatl(rulename, suf->l_obj->o_name, suffix, (char *)NULL);
		if ((o = objlook(rulename, FALSE)) == NULL || o->o_type != COLON)
			continue;

		if (Debug > 1)
			error("Trying %s suffix rule '%s' for: %s (suffix: '%s')\n",
				*suffix?"double":"single", rulename, name, suffix);

		strcatl(endbase, suf->l_obj->o_name, (char *)NULL);
		if ((source = objlook(sourcename, FALSE)) != (obj_t *)NULL) {
			make(source, TRUE);	/* source known, update it */

			*pcmd = o->o_cmd;
			return (source);
		}
		if (make(&newsource, FALSE) != 0) {  /* found or made source */
			source = objlook(sourcename, TRUE);
			source->o_date = newsource.o_date;
			source->o_level = newsource.o_level;

			*pcmd = o->o_cmd;
			return (source);
		}
	}
	*pcmd = (cmd_t *)NULL;
	return ((obj_t *)0);
}

/*
 * Find a source file for a given object file, use simple suffix rules.
 */
LOCAL obj_t *
ssuff_src(name, rule, rtypep, suffixp, pcmd)
	char	*name;
	obj_t	*rule;
	int	*rtypep;
	char	**suffixp;
	cmd_t	**pcmd;
{
	register list_t	*suf = rule->o_list;	/* List of possible suffixes */
	register cmd_t	*cmd = rule->o_cmd;	/* List of implicit commands */
		cmd_t	*ncmd;			/* for allocated new command */
	register obj_t	*source;
		obj_t	newsource;
		char	sourcename[NAMEMAX*2];
		char	*endbase;

	*suffixp = get_suffix(name, (char *)0);	/* Use '.' (dot) suffix only */

	copy_base(name, sourcename, (char *)NULL);
	endbase = sourcename + strlen(sourcename);
	newsource.o_name = sourcename;
	newsource.o_date = 0;
	newsource.o_type = 0;
	newsource.o_list = (list_t *)NULL;
	newsource.o_cmd = (cmd_t *)NULL;
	do {
		strcatl(endbase, suf->l_obj->o_name, (char *)NULL);
		if ((source = objlook(sourcename, FALSE)) != (obj_t *)NULL) {
			make(source, TRUE);	/* source known, update it */
			break;
		}
		if (make(&newsource, FALSE) != 0) {  /* found or made source */
			source = objlook(sourcename, TRUE);
			source->o_date = newsource.o_date;
			source->o_level = newsource.o_level;
			break;
		}
	} while ((suf = suf->l_next) != (list_t *)NULL &&
				(cmd = cmd->c_next) != (cmd_t *)NULL);
	if (source) {
		ncmd = (cmd_t *)fastalloc(sizeof (cmd_t));
		ncmd->c_next = (cmd_t *)NULL;
		ncmd->c_line = cmd->c_line;
		*pcmd = ncmd;
		*rtypep |= RTYPE_NEEDFREE;
	} else {
		*pcmd = (cmd_t *)NULL;
	}
	return (source);
}

LOCAL obj_t	*BadObj;
/*
 * Find a source file for a given object file.
 */
LOCAL obj_t *
findsrc(obj, rule, rtypep, suffixp, pcmd)
	obj_t	*obj;
	obj_t	*rule;
	int	*rtypep;
	char	**suffixp;
	cmd_t	**pcmd;
{
	int	rtype = *rtypep;

	if (BadObj == NULL) {
		BadObj = objlook("BAD OBJECT", TRUE);
		BadObj->o_date = BADTIME;
		BadObj->o_type = ':';
	}
	*suffixp = (char *)NULL;
	*pcmd = (cmd_t *)NULL;

	switch (rtype) {

	case RTYPE_PATTERN:
		return (patr_src(obj->o_name, (patr_t *)rule, rtypep, suffixp, pcmd));

	case RTYPE_DSUFFIX:
		return (suff_src(obj->o_name, rule, rtypep, suffixp, pcmd));

	case RTYPE_SUFFIX:
		*suffixp = Nullstr;
		return (one_suff_src(obj->o_name, Nullstr, pcmd));

	case RTYPE_SSUFFIX:
		return (ssuff_src(obj->o_name, rule, rtypep, suffixp, pcmd));

	case RTYPE_DEFAULT:
		*pcmd = (cmd_t *)rule;
		return (obj);		/* This causes $< to be == $@ */

	default:
		break;
	}
	return (BadObj);
}

/*
 * Process a target with no explicit command list.
 */
LOCAL date_t
default_cmd(obj, depname, deptime, deplevel, must_exist)
	register obj_t	*obj;
		char	*depname;
		date_t	deptime;
		int	deplevel;
		BOOL	must_exist;
{
	obj_t	*rule;
	obj_t	*source;
	cmd_t	*cmd;
	cmd_t	*cp;
	char	*suffix;
	int	rtype = RTYPE_NONE;

	if ((rule = default_rule(obj, &rtype)) == (obj_t *)NULL) {
		/*
		 * Found no default dependency rule for this target.
		 */
		if (obj->o_list != (list_t *)NULL) { /* Place holder or macro */
			obj->o_level = deplevel;
			return (deptime);
		}
		/*
		 * Found no rule to make target.
		 */
		if (obj == default_tgt) {
			errmsgno(EX_BAD, "'%s' does not say how to make '%s'.\n",
				MakeFileNames[obj->o_fileindex], obj->o_name);
			return (BADTIME);
		}
		/*
		 * Check if it is a source.
		 */
		if (searchobj(obj, MAXLEVEL, SSRC))
			return (obj->o_date);
		if (!must_exist)
			return (0);
		errmsgno(EX_BAD, "'%s' does not exist.\n", obj->o_name);
		return (BADTIME);
	}
	if ((source =
		findsrc(obj, rule, &rtype, &suffix, &cmd)) == (obj_t *)NULL) {

		/* Probably a obj file name. */
		if (searchobj(obj, MAXLEVEL, ObjSearch))
			return (obj->o_date);

		if (Debug > 2)
			error("Cannot find source for: %s->time: %s %s(type: %X)\n",
				obj->o_name, prtime(obj->o_date),
				must_exist?"but must ":"", obj->o_type);
		if (!must_exist)
			return (0);
		/*
		 * A target must not exist, a dependency must.
		 */
		if (obj->o_type == COLON)
			return (0);
		if (!NoWarn)
			errmsgno(EX_BAD,
				"Can't find any source for '%s'.\n",
							obj->o_name);
		return (BADTIME);
	}

	if (source->o_date == BADTIME)
		goto badtime;

	if (source->o_date > deptime) {	/* target depends on this source too */
		depname = source->o_name;
		deptime = source->o_date;
	}
	if (source->o_level < deplevel)
		deplevel = source->o_level;
	if (deptime == BADTIME)		/* Errors occured cannot make source.*/
		goto badtime;
	if (deptime >= newtime ||		/* made some targets    */
	    !searchobj(obj, deplevel, SALL) ||	/* found no obj file    */
	    deptime > obj->o_date) {		/* target is out of date */

		if (Debug > 0)
			error("Default: %s is out of date to: %s\n",
						obj->o_name, depname);
		if (Tflag) {
			if (!touch_file(obj->o_name))
				goto badtime;
		} else for (cp = cmd; cp; cp = cp->c_next) {
			if (docmd(substitute(cp->c_line, obj, source, suffix), obj)) {
				goto badtime;
			}
		}
		obj->o_date = newtime;
		obj->o_level = WDLEVEL;
	}
	if ((rtype & RTYPE_NEEDFREE) != 0 && cmd != NULL)
		fastfree((char *)cmd, sizeof (*cmd));

	if (!Tflag && ObjDir != (char *)NULL && obj->o_level == WDLEVEL) {
		if (Debug > 3) {
			list_t	*l = list_nth(SearchList, source->o_level-2);

			error("%s: obj->o_level: %d %s: source->o_level: %d '%s'\n",
					obj->o_name, obj->o_level,
					source->o_name, source->o_level,
					l ? l->l_obj->o_name:Nullstr);
		}
		obj->o_level = source->o_level;
		if ((source->o_level & 1) == 0)	/* Source has lower level  */
			obj->o_level += 1;	/* use corresponding ObjDir*/

		if (!move_tgt(obj)) {		/* move target into ObjDir */
			obj->o_level = WDLEVEL;
			return (BADTIME);	/* move failed */
		}
/*		obj->o_level = OBJLEVEL;*/
	}
	return (obj->o_date);
badtime:
	if ((rtype & RTYPE_NEEDFREE) != 0 && cmd != NULL)
		fastfree((char *)cmd, sizeof (*cmd));

	return (BADTIME);
}

/*
 * Do all commands that are required to make the current target up to date.
 *
 * If any step fails, return BADTIME. This will stop dependent commands but
 * all other commands, not related to the failed target may be made.
 *
 * If must_exist is FALSE, return value 0 if no source file exists.
 * In this case, findsrc will try other suffixes in the implicit rules.
 */
LOCAL date_t
make(obj, must_exist)
	register obj_t	*obj;
		BOOL	must_exist;
{
		char	*depname = 0;
		date_t	deptime = 0;
		date_t	xt = 0;
		short	deplevel = MAXLEVEL;
	register list_t	*l;
	register obj_t	*dep;
	register cmd_t	*cmd;

	if (Debug > 1)
		error("%sing %s\n", must_exist?"Mak":"Check", obj->o_name);

	if (obj->o_date != 0) {
		if (Debug > 2)
			error("make: %s->time: %s\n",
				obj->o_name, prtime(obj->o_date));
		/*
		 * We have been here before!
		 */
		if (obj->o_date == RECURSETIME)
			comerrno(EX_BAD, "Recursion in dependencies for '%s'.\n",
				obj->o_name);
		return (obj->o_date);
	}
	obj->o_date = RECURSETIME;	/* mark obj "we have been here" */

	/*
	 * Loop through the list of dependencies for obj.
	 */
	for (l = obj->o_list; l; l = l->l_next) {
		dep = l->l_obj;
		xt = make(dep, TRUE);


		if (dep->o_date > deptime) {
			/*
			 * Remember the date of the newest
			 * object in the list.
			 */
			depname = dep->o_name;
			deptime = dep->o_date;
		}
		if (dep->o_level < deplevel) {
			/*
			 * Remember the highest index in the search path
			 * for all objects in the list.
			 */
			deplevel = dep->o_level;
		}
	}
	if (deptime == BADTIME)		/* Errors occured cannot make target.*/
		return (obj->o_date = BADTIME);

	if (obj->o_cmd == (cmd_t *)NULL)	/* Get default command list */
		return (obj->o_date = default_cmd(obj, depname, deptime,
							deplevel, must_exist));

	/*
	 * There is an explicit command list for the current target.
	 */
	if (Debug > 2)
		error("deptime: %s\n", prtime(deptime));
	if (deptime >= newtime ||			/* made some targets */
	    !searchobj(obj, deplevel, ObjSearch) ||	/* found no obj file */
	    deptime > obj->o_date) {			/* tgt is out of date*/

		if (Debug > 0)
			error("Make:    %s is out of date to: %s\n",
						obj->o_name, depname);
		if (Tflag) {
			if (!touch_file(obj->o_name))
				return (obj->o_date = BADTIME);
		} else for (cmd = obj->o_cmd; cmd; cmd = cmd->c_next)
			if (docmd(substitute(cmd->c_line, obj, (obj_t *)NULL, (char *)NULL),
									obj)) {
				return (obj->o_date = BADTIME);
			}

		if (obj->o_level == MAXLEVEL)
			searchobj(obj, deplevel, SALL);
		obj->o_date = newtime;
	}
	return (obj->o_date);
}

/*
 * This is the interface function that is to be called from main()
 * If called with a NULL pointer, it checks for the default target first,
 * otherwise the named target is made.
 */
EXPORT BOOL
domake(name)
	char	*name;
{
	date_t	mtime;

	if (name) {
		default_tgt = objlook(name, TRUE);
	} else if (default_tgt) {
		name = default_tgt->o_name;
		if (Debug > 0)
			error("Target:  %s\n", name);
	}
	if (default_tgt) {
		mtime = make(default_tgt, TRUE);
		if (mtime == BADTIME) {
			errmsgno(EX_BAD, "Couldn't make '%s'.\n", name);
			if (Debug > 0)
				error("Current working directory:  %s\n", curwdir());
			return (FALSE);
		}
		if (mtime != (date_t)0 && mtime != newtime) {
			/*
			 * Nothing to do.
			 */
			if (!Qflag)
				errmsgno(EX_BAD, "'%s' is up to date.\n", name);
			return (TRUE);
		}
		if (Qflag)
			return (FALSE);
		return (TRUE);
	}
	errmsgno(EX_BAD, "No default Command defined.\n");
	usage(1);
	return (FALSE);	/* keep lint happy */
}

/*
 * Try to make target 'obj'.
 * Return TRUE, if target could be made.
 *
 * omake and xmake are used to make intermediate targets with no direct
 * dependency (e.g. .INIT and included files).
 */
EXPORT BOOL
omake(obj, must_exist)
	obj_t	*obj;
	BOOL	must_exist;
{
	date_t	mtime;

	if (obj == (obj_t *)NULL)
		return (TRUE);

	if (Debug > 2)
		error("xmake: %s->time: %s\n",
			obj->o_name, prtime(obj->o_date));
	mtime = make(obj, must_exist);

	if (Debug > 2)
		error("xmake: %s\n", prtime(mtime));
	if (mtime == BADTIME) {
/*		errmsgno(EX_BAD, "Couldn't make '%s'.\n", obj->o_name);*/
		return (FALSE);
	}
	return (TRUE);
}

/*
 * First do an objlook(name), then make it using xmake.
 */
EXPORT BOOL
xmake(name, must_exist)
	char	*name;
	BOOL	must_exist;
{
	obj_t	*o;

	o = objlook(name, TRUE);
	return (omake(o, must_exist));
}