[go: up one dir, main page]

File: cook.c

package info (click to toggle)
cook 2.19-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 7,316 kB
  • ctags: 3,969
  • sloc: ansic: 50,331; sh: 13,190; makefile: 4,542; yacc: 3,174; perl: 224; awk: 154
file content (1452 lines) | stat: -rw-r--r-- 28,799 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
/*
 *	cook - file construction tool
 *	Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2001 Peter Miller;
 *	All rights reserved.
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: functions to cook targets
 *
 * the kitchen
 *
 * This file contains the part of cook that actually decides which
 * recipes will be cooked.
 *
 * When cook has a target, cook performs the following actions in the order
 * given:
 *
 * 1.	Cook scans through the instanciated prerequisite recipes. All
 *	prerequisite recipes with the target in their target list are used.
 *
 *	If the recipe is used, any prerequisite files are recursively
 *	cooked, then if any of the prerequisite files were out of date, then
 *	all other explicit or implicit recipes with the same target will be
 *	deemed to be out of date.
 *
 * 2.	Cook then scans through the instanciated explicit recipes. All
 *	explicit recipes with the target in their target list are used.
 *
 *	If the recipe is used, any prerequisite files are recursively
 *	cooked, then if any prerequisites were out of date (including those
 *	of prerequisite recipes) then the actions bound to this recipe will
 *	be evaluated.
 *
 *	If there are no ingredients, then it is not out-of-date.  The
 *	body will be performed if (a) the target does not yet exist, or
 *	(b) the "force" flag is set, usually in the "set" clause of
 *	the recipe.
 *
 * 3.	If the target was not the subject of any explicit recipe, cook then
 *	scans the instanciated implicit recipes. Only first implicit recipe
 *	for which cook knows how to cook will be used.
 *
 *	Implicit recipe targets and prerequisites may contain a wilcard
 *	character (%), which is why they are implicit. If more than one
 *	wildcard character appears, only the last is considered the wilcard
 *	charcater.
 *
 *	If an implicit recipe is used, when expressions are evaluaded into
 *	word lists, any word containing the wildcard charcater (%) will be
 *	expanded out by the current wildcard expansion.
 *
 * 4.	If the target is not the subject of any prerequisite or explicit
 *	recipe, and no implicit recipes can be applied, then two things can
 *	happen.
 *		a. If the file exists, then it is up to date, or
 *		b. If the file does not exist then cook doesn't know how.
 *
 * If a command in the actions bound to any recipe fail, cook will not
 * evaluate those actions any further, and will not evaluate the actions
 * of any recipe for which the target of the failed actions was a
 * prerequisite.
 *
 * Cook will trap recursive looping of targets. If a recursion loop is
 * detected, then
 *	1. If the file exists, then it is up to date, or
 *	2. If the file does not exist then cook doesn't know how.
 */

#include <ac/stddef.h>
#include <ac/stdio.h>
#include <ac/time.h>

#include <cascade.h>
#include <cook.h>
#include <desist.h>
#include <error.h>
#include <error_intl.h>
#include <expr.h>
#include <flag.h>
#include <fingerprint.h>
#include <fingerprint/value.h>
#include <graph.h>
#include <graph/build.h>
#include <graph/file_pair.h>
#include <graph/leaf.h>
#include <graph/stats.h>
#include <graph/walk.h>
#include <graph/web.h>
#include <id.h>
#include <id/variable.h>
#include <match/new_by_recip.h>
#include <mem.h>
#include <opcode/context.h>
#include <option.h>
#include <os_interface.h>
#include <recipe.h>
#include <recipe/list.h>
#include <star.h>
#include <stmt.h>
#include <symtab.h>
#include <trace.h>


static	recipe_list_ty	explicit;	/* the explicit recipes */
static	symtab_ty	*explicit_stp;	/* the explicit recipes, indexed */
static	recipe_list_ty	implicit;	/* the implicit recipes */
static	symtab_ty	*implicit_stp;	/* the explicit recipes, indexed */
static	string_list_ty	cook_auto_list;
static	string_list_ty	cook_auto_list_nonleaf;


/*
 * NAME
 *	cook_search_list
 *
 * SYNOPSIS
 *	cook_search_list(string_list_ty *slp);
 *
 * DESCRIPTION
 *	The cook_search_list function is used to get the search list
 *	from the "search_list" variable.
 *
 *	Defaulting and clean-up are done here, also.
 *	If absent, defaults to ".".
 *	If the first element is not "." then it is inserted.
 *
 * ARGUMENTS
 *	slp - where to put the result
 */

void
cook_search_list(ocp, slp)
	const opcode_context_ty *ocp;
	string_list_ty	*slp;
{
	string_ty	*s;
	id_ty		*idp;

	/*
	 * make sure the variable exists
	 */
	trace(("cook_search_list()\n{\n"/*}*/));
	idp = opcode_context_id_search(ocp, id_search_list);
	if (!idp)
	{
		string_list_constructor(slp);
		s = str_from_c(".");
		string_list_append(slp, s);
		str_free(s);
		opcode_context_id_assign
		(
			(opcode_context_ty *)ocp,
			id_search_list,
			id_variable_new(slp),
			0
		);
		string_list_destructor(slp);
	}

	/*
	 * extract its string value
	 */
	id_variable_query(idp, slp);

	/*
	 * make sure the search list isn't empty
	 * make sure the search list has "." as the first element
	 */
	if
	(
		!slp->nstrings
	||
		slp->string[0]->str_length != 1
	||
		slp->string[0]->str_text[0] != '.'
	)
	{
		s = str_from_c(".");
		string_list_prepend(slp, s);
		str_free(s);
		opcode_context_id_assign
		(
			(opcode_context_ty *)ocp,
			id_search_list,
			id_variable_new(slp),
			0
		);
	}
	trace((/*{*/"}\n"));
}


/*
 * NAME
 *	cook_mtime_oldest
 *
 * SYNOPSIS
 *	time_t cook_mtime_oldest(string_ty *path, long *depth_p);
 *
 * DESCRIPTION
 *	The cook_mtime_oldest function is used to scan the search path
 *	for a file to determine the last-modified time of the file.
 *
 *	Look for the copies of the file which are identical to the
 *	shallowest copy; use the oldest time of all. Return the deepest
 *	level found.
 *
 * ARGUMENTS
 *	path	- file to get the mtime for
 *	depth_p	- where to put the depth
 *
 * RETURNS
 *	long; -1 on error, 0 if no such file, >0 for time
 *
 * CAVEAT
 *	The user must design recipes using the [resolve] function.
 */

time_t
cook_mtime_oldest(ocp, path, depth_p, max_fp_depth)
	const opcode_context_ty *ocp;
	string_ty	*path;
	long		*depth_p;
	long		max_fp_depth;
{
	time_t		result;
	long		bogus;

	trace(("cook_mtime_oldest(path = \"%s\", max = %ld)\n{\n"/*}*/,
		path->str_text, max_fp_depth));
	if (!depth_p)
		depth_p = &bogus;
	if (path->str_text[0] == '/')
	{
		result = os_mtime_oldest(path);
		*depth_p = 32767;
	}
	else
	{
		fp_value_ty	*prv_fp;
		string_list_ty	sl;
		long		j;

		prv_fp = 0;
		result = 0;
		cook_search_list(ocp, &sl);
		*depth_p = sl.nstrings;
		for (j = 0; j < sl.nstrings; ++j)
		{
			fp_value_ty	*fp;
			string_ty	*s1;
			string_ty	*s2;
			time_t		t;

			s1 = sl.string[j];
			if (s1->str_text[0] == '.' && !s1->str_text[1])
				s2 = str_copy(path);
			else
				s2 = str_format("%S/%S", s1, path);
			/*
			 * This allows the safe use of fp_search below,
			 * since os_mtime_oldest updates the fingerprint
			 * cache for the file.
			 */
			t = os_mtime_oldest(s2);

			if (!t)
			{
				/* File not found */
				str_free(s2);
				continue;
			}

			trace(("mtime(\"%s\") was %ld\n",
				s2->str_text, (long)t));

			/* File found */
			if (!prv_fp)
			{
				/* Shallowest file found */
				result = t;
				*depth_p = j;
				trace(("is the first\n"));
				if (option_test(OPTION_FINGERPRINT))
				{
					prv_fp = fp_search(s2);
					if (prv_fp)
					{
						/* Look for deeper copies */
						str_free(s2);
						continue;
					}
				}
				str_free(s2);
				break;
			}

			/* Found a deeper version */
			assert(option_test(OPTION_FINGERPRINT));
			if (j >= max_fp_depth)
			{
				str_free(s2);
				break;
			}
			fp = fp_search(s2);
			str_free(s2);

			if
			(
				fp
			&&
				str_equal
				(
					fp->contents_fingerprint,
					prv_fp->contents_fingerprint
				)
			)
			{
				/* Deeper version is same as shallow one */
				if (t < result)
					result = t;
				*depth_p = j;
			}
			else
			{
				trace(("was different\n"));
				break;
			}
		}
		string_list_destructor(&sl);
	}
	trace(("return %ld (%d);\n", (long)result, *depth_p));
	trace((/*{*/"}\n"));
	return result;
}


/*
 * NAME
 *	cook_mtime_newest
 *
 * SYNOPSIS
 *	time_t cook_mtime_newest(string_ty *path, long *depth_p);
 *
 * DESCRIPTION
 *	The cook_mtime_newest function is used to scan the search path
 *	for a file to determine the last-modified time of the file.
 *
 *	Look for the copies of the file which are identical to the
 *	shallowest copy; use the newest time of all. Return the
 *	shallowest level found.
 *
 * ARGUMENTS
 *	path	- file to get the mtime for
 *	depth_p	- where to put the depth
 *
 * RETURNS
 *	long; -1 on error, 0 if no such file, >0 for time
 *
 * CAVEAT
 *	The user must design recipes using the [resolve] function.
 */

time_t
cook_mtime_newest(ocp, path, depth_p, max_fp_depth)
	const opcode_context_ty *ocp;
	string_ty	*path;
	long		*depth_p;
	long		max_fp_depth;
{
	time_t		result;

	trace(("cook_mtime_newest(path = \"%s\")\n{\n"/*}*/, path->str_text));
	if (path->str_text[0] == '/')
	{
		result = os_mtime_newest(path);
		*depth_p = 0;
	}
	else
	{
		fp_value_ty	*prv_fp;
		string_list_ty	sl;
		long		j;

		prv_fp = 0;
		result = 0;
		cook_search_list(ocp, &sl);
		*depth_p = sl.nstrings;
		for (j = 0; j < sl.nstrings; ++j)
		{
			fp_value_ty	*fp;
			string_ty	*s1;
			string_ty	*s2;
			time_t		t;

			s1 = sl.string[j];
			if (s1->str_text[0] == '.' && !s1->str_text[1])
				s2 = str_copy(path);
			else
				s2 = str_format("%S/%S", s1, path);
			/*
			 * This allows the safe use of fp_search below,
			 * since os_mtime_newest updates the fingerprint
			 * cache for the file.
			 */
			t = os_mtime_newest(s2);

			if (!t)
			{
				/* File was not found */
				str_free(s2);
				continue;
			}

			trace(("mtime(\"%s\") was %ld\n",
				s2->str_text, (long)t));

			/* File found */
			if (!prv_fp)
			{
				/* Shallowest file found */
				result = t;
				*depth_p = j;
				trace(("is the first\n"));

				if (option_test(OPTION_FINGERPRINT))
				{
					prv_fp = fp_search(s2);
					if (prv_fp)
					{
						/* Look for deeper copies */
						str_free(s2);
						continue;
					}
				}
				str_free(s2);
				break;
			}

			/* Found a deeper version */
			assert(option_test(OPTION_FINGERPRINT));
			if (j >= max_fp_depth)
			{
				str_free(s2);
				break;
			}
			fp = fp_search(s2);
			str_free(s2);

			if
			(
				fp
			&&
				str_equal
				(
					fp->contents_fingerprint,
					prv_fp->contents_fingerprint
				)
			)
			{
				/* Deeper version is same as shallow one */
				if (t > result)
					result = t;
				/* do not alter depth */
			}
			else
			{
				trace(("was different\n"));
				break;
			}
		}
		string_list_destructor(&sl);
	}
	trace(("return %ld (%d);\n", (long)result, *depth_p));
	trace((/*{*/"}\n"));
	return result;
}


/*
 * NAME
 *	cook_mtime_resolve
 *
 * SYNOPSIS
 *	int cook_mtime_resolve(string_list_ty *output, string_list_ty *input);
 *
 * DESCRIPTION
 *	The cook_mtime_resolve function is used to
 *	resolve the name used for a file in the search list.
 *
 *	It implements the "resolve" built-in function.
 *
 * ARGUMENTS
 *	input - the function arguments (0 is the func name)
 *	output - where to put the results
 *
 * RETURNS
 *	int; 0 on success, -1 on error
 *
 * CAVEAT
 *	The user must design rules using the [resolve] function.
 */

int
cook_mtime_resolve(ocp, output, input, start)
	const opcode_context_ty *ocp;
	string_list_ty	*output;
	const string_list_ty *input;
	int		start;
{
	int		result;
	long		j;
	string_list_ty	sl;

	trace(("cook_mtime_resolve(input = %08lX, output = %08lX)\n{\n"/*}*/,
		input, output));
	cook_search_list(ocp, &sl);
	result = 0;
	for (j = start; j < input->nstrings; ++j)
	{
		string_ty	*arg;

		arg = input->string[j];
		if (arg->str_text[0] == '/')
			string_list_append(output, arg);
		else
		{
			int		done;
			long		k;

			done = 0;
			for (k = 0; k < sl.nstrings; ++k)
			{
				string_ty	*s1;
				string_ty	*s2;
				time_t		t;

				s1 = sl.string[k];
				if (s1->str_text[0] == '.' && !s1->str_text[1])
					s2 = str_copy(arg);
				else
					s2 = str_format("%S/%S", s1, arg);
				t = os_mtime_newest(s2);
				if (t < 0)
				{
					result = -1;
					str_free(s2);
					break;
				}
				if (t > 0)
				{
					string_list_append(output, s2);
					str_free(s2);
					done = 1;
					break;
				}
				str_free(s2);
			}
			if (!done)
				string_list_append(output, arg);
		}
		if (result < 0)
			break;
	}
	string_list_destructor(&sl);
	trace(("return %d;\n", result));
	trace((/*{*/"}\n"));
	return result;
}


/*
 * NAME
 *	cook - construct files
 *
 * SYNOPSIS
 *	int cook(string_list_ty *targets);
 *
 * DESCRIPTION
 *	The cook function is used to cook the given set of targets.
 *
 * RETURNS
 *	The cook function returns 0 if all of the targets cooked sucessfully,
 *	or 1 if there was any problem (exit statii).
 *
 * CAVEAT
 *	This function must be called after evrything has been initialized,
 *	and the cookbook read in.
 */

int
cook(wlp)
	string_list_ty	*wlp;
{
	int		retval;
	graph_ty	*gp;
	graph_build_status_ty gb_status;
	graph_walk_status_ty gw_status;

	/*
	 * set interrupts to catch
	 *
	 * Note that tee(1) [see listing.c] must ignore them
	 * for the generated messages to appear in the log file.
	 */
	trace(("cook(wlp = %08lX)\n{\n"/*}*/, wlp));
	desist_enable();

	/*
	 * Build the dependency graph.
	 */
	retval = 0;
	gp = graph_new();
	if
	(
		cook_auto_list_nonleaf.nstrings > 0
	&&
		cascade_used()
	&&
		option_test(OPTION_CASCADE)
	&&
		!option_test(OPTION_SILENT)
	&&
		option_test(OPTION_INCLUDE_COOKED_WARNING)
	)
	{
		gp->file_pair = graph_file_pair_new((string_list_ty *)0);
		graph_file_pair_foreign_derived
		(
			gp->file_pair,
			&cook_auto_list_nonleaf
		);
	}
	gb_status = graph_build_list(gp, wlp, graph_build_preference_error, 1);
	if (option_test(OPTION_REASON))
		graph_print_statistics(gp);
	switch (gb_status)
	{
	case graph_build_status_error:
		retval = 1;
		break;

	case graph_build_status_backtrack:
		/* assert(0); */
		retval = 1;
		break;

	case graph_build_status_success:
		break;
	}

	/*
	 * Walk the dependency graph.
	 */
	if (retval == 0)
	{
		gw_status = graph_walk(gp);
		switch (gw_status)
		{
		case graph_walk_status_uptodate:
		case graph_walk_status_uptodate_done:
		case graph_walk_status_done:
			break;

		case graph_walk_status_done_stop:
		case graph_walk_status_wait:
			assert(0);
			/* fall through... */

		case graph_walk_status_error:
			retval = 1;
			break;
		}
	}

	/*
	 * Release any resources held by the graph.
	 */
	graph_delete(gp);

	/*
	 * Return the result to the caller.
	 */
	trace(("return %d;\n", retval));
	trace((/*{*/"}\n"));
	return retval;
}


/*
 * NAME
 *	cook_pairs
 *
 * SYNOPSIS
 *	int cook_pairs(string_list_ty *);
 *
 * DESCRIPTION
 *	The cook_pairs function is used to print generate pair-wise file
 *	dependencies for the ancestors of the given targets.
 *
 * RETURNS
 *	int; 0 on success, 1 on failure (exit statii)
 */

int
cook_pairs(wlp)
	string_list_ty	*wlp;
{
	int		retval;
	graph_ty	*gp;
	graph_build_status_ty gb_status;
	graph_walk_status_ty gw_status;

	/*
	 * set interrupts to catch
	 *
	 * Note that tee(1) [see listing.c] must ignore them
	 * for the generated messages to appear in the log file.
	 */
	trace(("cook(wlp = %08lX)\n{\n"/*}*/, wlp));
	desist_enable();

	/*
	 * Build the dependency graph.
	 */
	retval = 0;
	gp = graph_new();
	gb_status = graph_build_list(gp, wlp, graph_build_preference_error, 0);
	if (option_test(OPTION_REASON))
		graph_print_statistics(gp);
	switch (gb_status)
	{
	case graph_build_status_error:
		retval = 1;
		break;

	case graph_build_status_backtrack:
		/* assert(0); */
		retval = 1;
		break;

	case graph_build_status_success:
		break;
	}

	/*
	 * Walk the dependency graph.
	 */
	if (retval == 0)
	{
		gw_status = graph_walk_pairs(gp);
		switch (gw_status)
		{
		case graph_walk_status_uptodate:
		case graph_walk_status_uptodate_done:
		case graph_walk_status_done:
			break;

		case graph_walk_status_done_stop:
		case graph_walk_status_wait:
			assert(0);
			/* fall through... */

		case graph_walk_status_error:
			retval = 1;
			break;
		}
	}

	/*
	 * Release any resources held by the graph.
	 */
	graph_delete(gp);

	/*
	 * Return the result to the caller.
	 */
	trace(("return %d;\n", retval));
	trace((/*{*/"}\n"));
	return retval;
}


/*
 * NAME
 *	cook_script
 *
 * SYNOPSIS
 *	void cook_script(string_list_ty *);
 *
 * DESCRIPTION
 *	The cook_script function is used to print a shell script to
 *	build the the given targets.  It's only an approximation of the
 *	full cook semantics.
 *
 * RETURNS
 *	int; 0 on success, 1 on failure (exit statii)
 */

int
cook_script(wlp)
	string_list_ty	*wlp;
{
	int		retval;
	graph_ty	*gp;
	graph_build_status_ty gb_status;
	graph_walk_status_ty gw_status;

	/*
	 * set interrupts to catch
	 *
	 * Note that tee(1) [see listing.c] must ignore them
	 * for the generated messages to appear in the log file.
	 */
	trace(("cook(wlp = %08lX)\n{\n"/*}*/, wlp));
	desist_enable();

	/*
	 * Build the dependency graph.
	 */
	retval = 0;
	gp = graph_new();
	gb_status = graph_build_list(gp, wlp, graph_build_preference_error, 0);
	if (option_test(OPTION_REASON))
		graph_print_statistics(gp);
	switch (gb_status)
	{
	case graph_build_status_error:
		retval = 1;
		break;

	case graph_build_status_backtrack:
		/* assert(0); */
		retval = 1;
		break;

	case graph_build_status_success:
		break;
	}

	/*
	 * Walk the dependency graph.
	 */
	if (retval == 0)
	{
		gw_status = graph_walk_script(gp);
		switch (gw_status)
		{
		case graph_walk_status_uptodate:
		case graph_walk_status_uptodate_done:
		case graph_walk_status_done:
			break;

		case graph_walk_status_done_stop:
		case graph_walk_status_wait:
			assert(0);
			/* fall through... */

		case graph_walk_status_error:
			retval = 1;
			break;
		}
	}

	/*
	 * Release any resources held by the graph.
	 */
	graph_delete(gp);

	/*
	 * Return the result to the caller.
	 */
	trace(("return %d;\n", retval));
	trace((/*{*/"}\n"));
	return retval;
}


/*
 * NAME
 *	cook_web
 *
 * SYNOPSIS
 *	void cook_web(string_list_ty *);
 *
 * DESCRIPTION
 *	The cook_web function is used to print a shell web to
 *	build the the given targets.  It's only an approximation of the
 *	full cook semantics.
 *
 * RETURNS
 *	int; 0 on success, 1 on failure (exit statii)
 */

int
cook_web(wlp)
	string_list_ty	*wlp;
{
	int		retval;
	graph_ty	*gp;
	graph_build_status_ty gb_status;

	/*
	 * set interrupts to catch
	 *
	 * Note that tee(1) [see listing.c] must ignore them
	 * for the generated messages to appear in the log file.
	 */
	trace(("cook(wlp = %08lX)\n{\n", wlp));
	desist_enable();

	/*
	 * Build the dependency graph.
	 */
	retval = 0;
	gp = graph_new();
	gb_status = graph_build_list(gp, wlp, graph_build_preference_error, 0);
	if (option_test(OPTION_REASON))
		graph_print_statistics(gp);
	switch (gb_status)
	{
	case graph_build_status_error:
		retval = 1;
		break;

	case graph_build_status_backtrack:
		/* assert(0); */
		retval = 1;
		break;

	case graph_build_status_success:
		break;
	}

	/*
	 * Walk the dependency graph.
	 */
	if (retval == 0)
		graph_walk_web(gp);

	/*
	 * Release any resources held by the graph.
	 */
	graph_delete(gp);

	/*
	 * Return the result to the caller.
	 */
	trace(("return %d;\n", retval));
	trace(("}\n"));
	return retval;
}


/*
 * NAME
 *	cook_auto
 *
 * SYNOPSIS
 *	void cook_auto(string_ty *);
 *
 * DESCRIPTION
 *	The cook_auto function is used to that this file needs to be
 *	automaticaly cooked.  This is done for files inccluded using the
 *	#include-cooked mechanism.
 */

void
cook_auto(wlp)
	string_list_ty	*wlp;
{
	string_list_append_list_unique(&cook_auto_list, wlp);
}


/*
 * NAME
 *	cook_auto_required
 *
 * SYNOPSIS
 *	int cook_auto_required(void);
 *
 * DESCRIPTION
 *	The cook_auto_required function is used to automaticly re-build
 *	any files included by the #include-cooked mechanism which may
 *	have been out of date.
 *
 * RETURNS
 *	int;	-1	on error
 *		0	if everything was up-to-date, the cookbook does not
 *			need to be read in again
 *		1	if one or more include files were re-build, and the
 *			cookbook must be read again.
 */

int
cook_auto_required()
{
	int		retval;
	graph_ty	*gp;
	graph_build_status_ty gb_status;
	graph_walk_status_ty gw_status;
	size_t		j;

	/*
	 * This may have been explicitly forbidden on the command line.
	 */
	if (!option_test(OPTION_INCLUDE_COOKED))
		return 0;

	retval = 0;

	option_set(OPTION_ACTION, OPTION_LEVEL_AUTO, 1);
	option_set(OPTION_TOUCH, OPTION_LEVEL_AUTO, 0);
	option_set(OPTION_REASON, OPTION_LEVEL_COOKBOOK, 0);

	/*
	 * Build the dependency graph.
	 */
	gp = graph_new();
	if
	(
		!option_test(OPTION_SILENT)
	&&
		option_test(OPTION_INCLUDE_COOKED_WARNING)
	)
		gp->file_pair = graph_file_pair_new(&cook_auto_list);
	gb_status =
		graph_build_list
		(
			gp,
			&cook_auto_list,
			graph_build_preference_error,
			0	/* not primary, no up-to-date commentary */
		);
	if (option_test(OPTION_REASON))
		graph_print_statistics(gp);
	switch (gb_status)
	{
	case graph_build_status_error:
		retval = -1;
		break;

	case graph_build_status_backtrack:
		/* assert(0); */
		retval = -1;
		break;

	case graph_build_status_success:
		break;
	}

	/*
	 * Build a list of non-leaf cook-auto files.
	 */
	string_list_destructor(&cook_auto_list_nonleaf);
	for (j = 0; j < cook_auto_list.nstrings; ++j)
	{
		string_ty	*fn;

		fn = cook_auto_list.string[j];
		if (!graph_file_leaf_p(gp, fn))
			string_list_append(&cook_auto_list_nonleaf, fn);
	}

	/*
	 * Walk the dependency graph.
	 */
	if (retval == 0)
	{
		gw_status = graph_walk(gp);
		switch (gw_status)
		{
		case graph_walk_status_uptodate:
		case graph_walk_status_uptodate_done:
			break;

		case graph_walk_status_done:
			retval = 1;
			break;

		case graph_walk_status_done_stop:
		case graph_walk_status_wait:
			assert(0);
			/* fall through... */

		case graph_walk_status_error:
			retval = -1;
			break;
		}
	}

	/*
	 * Release any resources held by the graph.
	 */
	graph_delete(gp);

	option_undo(OPTION_REASON, OPTION_LEVEL_COOKBOOK);
	option_undo(OPTION_ACTION, OPTION_LEVEL_AUTO);
	option_undo(OPTION_TOUCH, OPTION_LEVEL_AUTO);

	return retval;
}


/*
 * NAME
 *	cook_reset
 *
 * SYNOPSIS
 *	void cook_reset(void);
 *
 * DESCRIPTION
 *	The cook_reset function is used to reset the recipe lists in
 *	preparation for re-reading a cookbook.  Usually the result of a
 *	#include-cooked file being re-build.
 */

void
cook_reset()
{
	leaf_reset();
	string_list_destructor(&cook_auto_list);
	/* Don't nuke cook_auto_list_nonleaf, we need it for later */
	cook_implicit_nth_by_name(0, 0);
	if (explicit_stp)
	{
		symtab_free(explicit_stp);
		explicit_stp = 0;
	}
	if (implicit_stp)
	{
		symtab_free(implicit_stp);
		implicit_stp = 0;
	}
	recipe_list_destructor(&explicit);
	recipe_list_destructor(&implicit);
	cascade_reset();
}


/*
 * NAME
 *	cook_find_default
 *
 * SYNOPSIS
 *	void cook_find_default(string_list_ty *);
 *
 * DESCRIPTION
 *	The cook_find_default function is used to find the default
 *	target(s).  The explicit recipes are examined - the targets of
 *	the first one (that doesn't have nodefault set) are the default
 *	targets.
 */

void
cook_find_default(wlp)
	string_list_ty	*wlp;
{
	size_t		j;
	recipe_ty	*rp;

	/*
	 * use the forst one
	 * explicitly flagged default
	 */
	for (j = 0; j < explicit.nrecipes; ++j)
	{
		rp = explicit.recipe[j];
		if (flag_query(rp->flags, RF_DEFAULT))
		{
			string_list_copy_constructor(wlp, rp->target);
			return;
		}
	}

	/*
	 * use the first one
	 * not flagged nodefault
	 */
	for (j = 0; j < explicit.nrecipes; ++j)
	{
		rp = explicit.recipe[j];
		if (!flag_query(rp->flags, RF_DEFAULT_OFF))
		{
			string_list_copy_constructor(wlp, rp->target);
			return;
		}
	}

	/*
	 * fatal error otherwise
	 */
	fatal_intl(0, i18n("no default target"));
}


static void explicit_reap _((void *));

static void
explicit_reap(p)
	void		*p;
{
	recipe_list_ty	*rlp;

	rlp = p;
	recipe_list_delete(rlp);
}


static recipe_list_ty *cook_explicit_find _((string_ty *));

static recipe_list_ty *
cook_explicit_find(filename)
	string_ty	*filename;
{
	recipe_list_ty	*rlp;

	if (!explicit_stp)
	{
		explicit_stp = symtab_alloc(200);
		explicit_stp->reap = explicit_reap;
	}
	rlp = symtab_query(explicit_stp, filename);
	if (!rlp)
	{
		rlp = recipe_list_new();
		symtab_assign(explicit_stp, filename, rlp);
	}
	return rlp;
}


static recipe_list_ty *cook_implicit_find _((string_ty *));

static recipe_list_ty *
cook_implicit_find(filename)
	string_ty	*filename;
{
	recipe_list_ty	*rlp;

	if (!implicit_stp)
	{
		implicit_stp = symtab_alloc(200);
		implicit_stp->reap = explicit_reap;
	}
	rlp = symtab_query(implicit_stp, filename);
	if (!rlp)
	{
		rlp = recipe_list_new();
		symtab_assign(implicit_stp, filename, rlp);
	}
	return rlp;
}


/*
 * NAME
 *	cook_explicit_append
 *
 * SYNOPSIS
 *	void cook_explicit_append(recipe_ty *);
 *
 * DESCRIPTION
 *	The cook_explicit_append function is used to append a recipe to
 *	the explicit recipe list.  Used by the cookbook evaluation
 *	functions.
 */

void
cook_explicit_append(rp)
	recipe_ty	*rp;
{
	size_t		j;

	trace(("cook_explicit_append(rp = %08lX)\n{\n"/*}*/, (long)rp));
	recipe_list_append(&explicit, rp);
	for (j = 0; j < rp->target->nstrings; ++j)
	{
		string_ty	*filename;
		recipe_list_ty	*rlp;

		filename = rp->target->string[j];
		rlp = cook_explicit_find(filename);
		recipe_list_append(rlp, rp);
	}
	trace((/*{*/"}\n"));
}


/*
 * NAME
 *	cook_implicit_append
 *
 * SYNOPSIS
 *	void cook_implicit_append(recipe_ty *);
 *
 * DESCRIPTION
 *	The cook_implicit_append function is used to append a recipe to
 *	the explicit recipe list.  Used by the cookbook evaluation
 *	functions.
 */

void
cook_implicit_append(rp)
	recipe_ty	*rp;
{
	string_ty	*base;
	string_list_ty	base_list;
	recipe_list_ty	*rlp;
	size_t		j;
	match_ty	*mp;

	/*
	 * Create a suitable matching object.  We need to set the recipe
	 * flags, to know which matching flavour.
	 */
	mp = match_new_by_recipe(rp);

	string_list_constructor(&base_list);
	for (j = 0; j < rp->target->nstrings; ++j)
	{
		base = os_entryname(rp->target->string[j]);
		if (match_usage_mask(mp, base, &rp->pos) != 0)
		{
			match_delete(mp);
			str_free(base);
			string_list_destructor(&base_list);
			recipe_list_append(&implicit, rp);
			return;
		}
		string_list_append_unique(&base_list, base);
		str_free(base);
	}
	match_delete(mp);
	for (j = 0; j < base_list.nstrings; ++j)
	{
		base = base_list.string[j];
		rlp = cook_implicit_find(base);
		recipe_list_append(rlp, rp);
	}
	string_list_destructor(&base_list);
}


/*
 * NAME
 *	cook_explicit_nth
 *
 * SYNOPSIS
 *	recipe_ty *cook_explicit_nth(long);
 *
 * DESCRIPTION
 *	The cook_explicit_nth function is used to get the n'th recipe
 *	from the explicit recipe list.
 *
 * RETURNS
 *	recipe_ty *; the recipe you asked for, or NULL if you went off
 *	the end.
 */

const recipe_list_ty *
cook_explicit_by_name(filename)
	string_ty	*filename;
{
	return cook_explicit_find(filename);
}


/*
 * NAME
 *	cook_implicit_nth
 *
 * SYNOPSIS
 *	recipe_ty *cook_implicit_nth(long);
 *
 * DESCRIPTION
 *	The cook_implicit_nth function is used to get the n'th recipe
 *	from the implicit recipe list.
 *
 * RETURNS
 *	recipe_ty *; the recipe you asked for, or NULL if you went off
 *	the end.
 */

recipe_ty *
cook_implicit_nth(n)
	long	n;
{
	if (n < 0 || n >= implicit.nrecipes)
		return 0;
	return implicit.recipe[n];
}


/*
 * NAME
 *	cook_implicit_nth_by_name
 *
 * SYNOPSIS
 *	recipe_ty *cook_implicit_nth_by_name(long, string_ty *);
 *
 * DESCRIPTION
 *	The cook_implicit_nth_by_name function is used to get the n'th
 *	recipe from the implicit recipe list; indexed by the last filename
 *	element if non-pattern.
 *
 * RETURNS
 *	recipe_ty *; the recipe you asked for, or NULL if you went off
 *	the end.
 */

recipe_ty *
cook_implicit_nth_by_name(n, name)
	long 		n;
	string_ty	*name;
{
	static string_ty *prev;
	static recipe_list_ty *rlp;

	if (!name)
	{
		/* used to clear the state between passes */
		if (prev)
			str_free(prev);
		prev = 0;
		rlp = 0;
		return 0;
	}
	if (!prev || !str_equal(prev, name))
	{
		if (prev)
			str_free(prev);
		prev = str_copy(name);
		rlp = cook_implicit_find(name);
	}
	if (n < 0 || n >= rlp->nrecipes)
		return 0;
	return rlp->recipe[n];
}