[go: up one dir, main page]

File: main.c

package info (click to toggle)
cook 2.10-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,856 kB
  • ctags: 3,842
  • sloc: ansic: 47,714; sh: 12,150; makefile: 4,317; yacc: 3,104; awk: 154
file content (719 lines) | stat: -rw-r--r-- 17,074 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
/*
 *	cook - file construction tool
 *	Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999 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: operating system start point, and command line argument parsing
 */

#include <ac/stddef.h>
#include <ac/string.h>
#include <ac/stdio.h>
#include <ac/stdlib.h>

#include <arglex.h>
#include <builtin.h>
#include <cook.h>
#include <error_intl.h>
#include <fingerprint.h>
#include <help.h>
#include <id.h>
#include <id/variable.h>
#include <lex.h>
#include <listing.h>
#include <option.h>
#include <parse.h>
#include <star.h>
#include <progname.h>
#include <quit.h>
#include <trace.h>
#include <version.h>


enum
{
	arglex_token_action,
	arglex_token_action_not,
	arglex_token_book,
	arglex_token_book_not,
	arglex_token_cascade,
	arglex_token_cascade_not,
	arglex_token_disassemble,
	arglex_token_disassemble_not,
	arglex_token_errok,
	arglex_token_errok_not,
	arglex_token_fingerprint,
	arglex_token_fingerprint_not,
	arglex_token_fingerprint_update,
	arglex_token_force,
	arglex_token_force_not,
	arglex_token_include,
	arglex_token_include_cooked,
	arglex_token_include_cooked_not,
	arglex_token_include_cooked_warning,
	arglex_token_include_cooked_warning_not,
	arglex_token_log,
	arglex_token_log_not,
	arglex_token_metering,
	arglex_token_metering_not,
	arglex_token_pairs,
	arglex_token_parallel,
	arglex_token_parallel_not,
	arglex_token_pedantic,
	arglex_token_pedantic_not,
	arglex_token_persevere,
	arglex_token_persevere_not,
	arglex_token_precious,
	arglex_token_precious_not,
	arglex_token_reason,
	arglex_token_reason_not,
	arglex_token_script,
	arglex_token_shallow,
	arglex_token_shallow_not,
	arglex_token_silent,
	arglex_token_silent_not,
	arglex_token_star,
	arglex_token_star_not,
	arglex_token_strip_dot,
	arglex_token_strip_dot_not,
	arglex_token_touch,
	arglex_token_touch_not,
	arglex_token_tty,
	arglex_token_tty_not,
	arglex_token_update,
	arglex_token_update_not,
	arglex_token_web

	/*
	 * When you add an option to this list, you must
	 * also add it to the list in cook/builtin/options.c
	 */
};

static arglex_table_ty argtab[] =
{
	{ "-Action",		(arglex_token_ty)arglex_token_action,	},
	{ "-No_Action",		(arglex_token_ty)arglex_token_action_not, },
	{ "-Book",		(arglex_token_ty)arglex_token_book,	},
	{ "-No_Book",		(arglex_token_ty)arglex_token_book_not,	},
	{ "-CAScade",		(arglex_token_ty)arglex_token_cascade, },
	{ "-No_CAScade",	(arglex_token_ty)arglex_token_cascade_not, },
	{ "-Continue",		(arglex_token_ty)arglex_token_persevere, },
	{ "-No_Continue",	(arglex_token_ty)arglex_token_persevere_not, },
	{ "-DISassemble",	(arglex_token_ty)arglex_token_disassemble, },
	{ "-No_DISassemble",	(arglex_token_ty)arglex_token_disassemble_not, },
	{ "-Errok",		(arglex_token_ty)arglex_token_errok,	},
	{ "-No_Errok",		(arglex_token_ty)arglex_token_errok_not,	},
	{ "-FingerPrint",	(arglex_token_ty)arglex_token_fingerprint, },
	{ "-No_FingerPrint",	(arglex_token_ty)arglex_token_fingerprint_not, },
	{ "-FingerPrint_Update", (arglex_token_ty)arglex_token_fingerprint_update, },
	{ "-Forced",		(arglex_token_ty)arglex_token_force,	},
	{ "-No_Forced",		(arglex_token_ty)arglex_token_force_not,	},
	{ "-HyperText_Markup_Language", (arglex_token_ty)arglex_token_web, },
	{ "-Include",		(arglex_token_ty)arglex_token_include,	},
	{ "-\\I*",		(arglex_token_ty)arglex_token_include,	},
	{ "-Include_Cooked",	(arglex_token_ty)arglex_token_include_cooked, },
	{ "-Include_Cooked_Warning", (arglex_token_ty)arglex_token_include_cooked_warning, },
	{ "-Jobs",		(arglex_token_ty)arglex_token_parallel, },
	{ "-No_Include_Cooked",	(arglex_token_ty)arglex_token_include_cooked_not, },
	{ "-No_Include_Cooked_Warning", (arglex_token_ty)arglex_token_include_cooked_warning_not, },
	{ "-LOg",		(arglex_token_ty)arglex_token_log,	},
	{ "-List",		(arglex_token_ty)arglex_token_log,	},
	{ "-No_LOg",		(arglex_token_ty)arglex_token_log_not,	},
	{ "-No_List",		(arglex_token_ty)arglex_token_log_not,	},
	{ "-Meter",		(arglex_token_ty)arglex_token_metering,	},
	{ "-No_Meter",		(arglex_token_ty)arglex_token_metering_not, },
	{ "-PAirs",		(arglex_token_ty)arglex_token_pairs,	},
	{ "-PARallel",		(arglex_token_ty)arglex_token_parallel,	},
	{ "-No_PARallel",	(arglex_token_ty)arglex_token_parallel_not, },
	{ "-Precious",		(arglex_token_ty)arglex_token_precious,	},
	{ "-No_Precious",	(arglex_token_ty)arglex_token_precious_not, },
	{ "-Reason",		(arglex_token_ty)arglex_token_reason,	},
	{ "-No_Reason",		(arglex_token_ty)arglex_token_reason_not, },
	{ "-SCript",		(arglex_token_ty)arglex_token_script,	},
	{ "-Silent",		(arglex_token_ty)arglex_token_silent,	},
	{ "-No_Silent",		(arglex_token_ty)arglex_token_silent_not, },
	{ "-SHallow",		(arglex_token_ty)arglex_token_shallow,	},
	{ "-No_SHallow",	(arglex_token_ty)arglex_token_shallow_not, },
	{ "-STar",		(arglex_token_ty)arglex_token_star,	},
	{ "-No_STar",		(arglex_token_ty)arglex_token_star_not,	},
	{ "-Strip_Dot",		(arglex_token_ty)arglex_token_strip_dot, },
	{ "-No_Strip_Dot",	(arglex_token_ty)arglex_token_strip_dot_not, },
	{ "-TErminal",		(arglex_token_ty)arglex_token_tty,	},
	{ "-No_TErminal",	(arglex_token_ty)arglex_token_tty_not,	},
	{ "-Touch",		(arglex_token_ty)arglex_token_touch,	},
	{ "-No_Touch",		(arglex_token_ty)arglex_token_touch_not,	},
	{ "-TRace",		(arglex_token_ty)arglex_token_reason,	},
	{ "-No_TRace",		(arglex_token_ty)arglex_token_reason_not,	},
	{ "-Update",		(arglex_token_ty)arglex_token_update,	},
	{ "-Time_Adjust",	(arglex_token_ty)arglex_token_update,	},
	{ "-No_Update",		(arglex_token_ty)arglex_token_update_not, },
	{ "-No_Time_Adjust",	(arglex_token_ty)arglex_token_update_not, },
	{ "-Web",		(arglex_token_ty)arglex_token_web,	},
	{ 0, (arglex_token_ty)0, }, /* end marker */
};


/*
 *  NAME
 *	usage - options diagnostic
 *
 *  SYNOPSIS
 *	void usage(void);
 *
 *  DESCRIPTION
 *	Usage is called when the user has made a syntactic or semantic error
 *	on the command line.
 *
 *  CAVEAT
 *	This function does NOT return.
 */

static void usage _((void));

static void
usage()
{
	char		*progname;

	progname = progname_get();
	fprintf(stderr, "usage: %s [ <option>... ][ <filename>... ]\n", progname);
	fprintf(stderr, "       %s -Help\n", progname);
	fprintf(stderr, "       %s -VERSion\n", progname);
	quit(1);
	trace(("to silence warnings\n"));
}


/*
 * NAME
 *	argparse - parse command line
 *
 * SYNOPSIS
 *	void argparse(option_level_ty);
 *
 * DESCRIPTION
 *	The argparse function is used to parse command lines.
 *
 * RETURNS
 *	void
 */

static void argparse _((option_level_ty));

static void
argparse(level)
	option_level_ty	level;
{
	option_number_ty type;
	string_ty	*s;
	sub_context_ty	*scp;
	int		fingerprint_update;

	type = -1;
	fingerprint_update = 0;
	switch (arglex())
	{
	case arglex_token_help:
		if (level != OPTION_LEVEL_COMMAND_LINE)
		{
			not_in_env:
			scp = sub_context_new();
			sub_var_set(scp, "Name", "%s", arglex_value.alv_string);
			fatal_intl
			(
				scp,
			       i18n("may not use $name in environment variable")
			);
			/* NOTREACHED */
		}
		help((char *)0, usage);
		quit(0);
	
	case arglex_token_version:
		if (level != OPTION_LEVEL_COMMAND_LINE)
			goto not_in_env;
		version();
		quit(0);

	default:
		break;
	}
	while (arglex_token != arglex_token_eoln)
	{
		switch (arglex_token)
		{
		default:
			generic_argument(usage);
			continue;

		case arglex_token_include:
			if (arglex() != arglex_token_string)
			{
				arg_needs_string(arglex_token_include, usage);
				/* NOTREACHED */
			}
			s = str_from_c(arglex_value.alv_string);
			string_list_append_unique(&option.o_search_path, s);
			str_free(s);
			break;

		case arglex_token_reason:
			type = OPTION_REASON;
			normal_on:
			if (option_already(type, level))
			{
				too_many:
				arg_duplicate_cur(usage);
				/* NOTREACHED */
			}
			option_set(type, level, 1);
			break;

		case arglex_token_reason_not:
			type = OPTION_REASON;
			normal_off:
			if (option_already(type, level))
				goto too_many;
			option_set(type, level, 0);
			break;

		case arglex_token_cascade:
			type = OPTION_CASCADE;
			goto normal_on;

		case arglex_token_cascade_not:
			type = OPTION_CASCADE;
			goto normal_off;

		case arglex_token_disassemble:
			type = OPTION_DISASSEMBLE;
			goto normal_on;

		case arglex_token_disassemble_not:
			type = OPTION_DISASSEMBLE;
			goto normal_off;

		case arglex_token_tty:
			type = OPTION_TERMINAL;
			goto normal_on;

		case arglex_token_tty_not:
			type = OPTION_TERMINAL;
			goto normal_off;

		case arglex_token_precious:
			type = OPTION_PRECIOUS;
			goto normal_on;

		case arglex_token_precious_not:
			type = OPTION_PRECIOUS;
			goto normal_off;

		case arglex_token_log:
			if (option_already(OPTION_LOGGING, level))
				goto too_many;
			option_set(OPTION_LOGGING, level, 1);
			if (arglex() != arglex_token_string)
				continue;
			if (option.o_logfile)
				str_free(option.o_logfile);
			option.o_logfile = str_from_c(arglex_value.alv_string);
			break;

		case arglex_token_log_not:
			type = OPTION_LOGGING;
			goto normal_off;

		case arglex_token_book:
			if (option_already(OPTION_BOOK, level))
				goto too_many;
			option_set(OPTION_BOOK, level, 1);
			if (arglex() != arglex_token_string)
				continue;
			if (option.o_book)
				str_free(option.o_book);
			option.o_book = str_from_c(arglex_value.alv_string);
			break;

		case arglex_token_book_not:
			type = OPTION_BOOK;
			goto normal_off;

		case arglex_token_include_cooked:
			type = OPTION_INCLUDE_COOKED;
			goto normal_on;

		case arglex_token_include_cooked_not:
			type = OPTION_INCLUDE_COOKED;
			goto normal_off;

		case arglex_token_include_cooked_warning:
			type = OPTION_INCLUDE_COOKED_WARNING;
			goto normal_on;

		case arglex_token_include_cooked_warning_not:
			type = OPTION_INCLUDE_COOKED_WARNING;
			goto normal_off;

		case arglex_token_silent:
			type = OPTION_SILENT;
			goto normal_on;

		case arglex_token_silent_not:
			type = OPTION_SILENT;
			goto normal_off;

		case arglex_token_metering:
			type = OPTION_METER;
			goto normal_on;

		case arglex_token_metering_not:
			type = OPTION_METER;
			goto normal_off;

		case arglex_token_touch:
			type = OPTION_TOUCH;
			goto normal_on;

		case arglex_token_touch_not:
			type = OPTION_TOUCH;
			goto normal_off;

		case arglex_token_action:
			type = OPTION_ACTION;
			goto normal_on;

		case arglex_token_action_not:
			type = OPTION_ACTION;
			goto normal_off;

		case arglex_token_persevere:
			type = OPTION_PERSEVERE;
			goto normal_on;

		case arglex_token_persevere_not:
			type = OPTION_PERSEVERE;
			goto normal_off;

		case arglex_token_errok:
			type = OPTION_ERROK;
			goto normal_on;

		case arglex_token_errok_not:
			type = OPTION_ERROK;
			goto normal_off;

		case arglex_token_force:
			type = OPTION_FORCE;
			goto normal_on;

		case arglex_token_force_not:
			type = OPTION_FORCE;
			goto normal_off;

		case arglex_token_fingerprint:
			type = OPTION_FINGERPRINT;
			goto normal_on;

		case arglex_token_fingerprint_not:
			type = OPTION_FINGERPRINT;
			goto normal_off;

		case arglex_token_fingerprint_update:
			if (level != OPTION_LEVEL_COMMAND_LINE)
				goto not_in_env;
			if (option.fingerprint_update)
				goto too_many;
			option.fingerprint_update++;
			break;

		case arglex_token_pairs:
			if (level != OPTION_LEVEL_COMMAND_LINE)
				goto not_in_env;
			if (option.pairs)
				goto too_many;
			option.pairs++;
			break;

		case arglex_token_script:
			if (level != OPTION_LEVEL_COMMAND_LINE)
				goto not_in_env;
			if (option.script)
				goto too_many;
			option.script++;
			break;

		case arglex_token_web:
			if (level != OPTION_LEVEL_COMMAND_LINE)
				goto not_in_env;
			if (option.web)
				goto too_many;
			option.web++;
			break;

		case arglex_token_string:
			if (level != OPTION_LEVEL_COMMAND_LINE)
			{
				if (strchr(arglex_value.alv_string, '='))
				{
					fatal_intl
					(
						0,
			i18n("may not assign variables in environment variable")
					);
				}
				else
				{
					fatal_intl
					(
						0,
			    i18n("may not name targets in environment variable")
					);
				}
			}
			else
			{
				char		*cp;

				cp = strchr(arglex_value.alv_string, '=');
				if (!cp)
				{
					s = str_from_c(arglex_value.alv_string);
					string_list_append(&option.o_target, s);
					str_free(s);
				}
				else
				{
					s = str_from_c(arglex_value.alv_string);
					string_list_append(&option.o_vardef, s);
					str_free(s);
				}
			}
			break;

		case arglex_token_star:
			type = OPTION_STAR;
			goto normal_on;

		case arglex_token_star_not:
			type = OPTION_STAR;
			goto normal_off;

		case arglex_token_strip_dot:
			type = OPTION_STRIP_DOT;
			goto normal_on;

		case arglex_token_strip_dot_not:
			type = OPTION_STRIP_DOT;
			goto normal_off;

		case arglex_token_update:
			type = OPTION_UPDATE;
			goto normal_on;

		case arglex_token_update_not:
			type = OPTION_UPDATE;
			goto normal_off;

		case arglex_token_parallel:
			if (arglex() != arglex_token_number)
			{
				s = str_from_c("parallel_jobs=4");
				string_list_append(&option.o_vardef, s);
				str_free(s);
				continue;
			}
			s = str_format("parallel_jobs=%d", (int)arglex_value.alv_number);
			string_list_append(&option.o_vardef, s);
			str_free(s);
			break;

		case arglex_token_parallel_not:
			s = str_from_c("parallel_jobs=1");
			string_list_append(&option.o_vardef, s);
			str_free(s);
			break;

		case arglex_token_shallow:
			type = OPTION_SHALLOW;
			goto normal_on;

		case arglex_token_shallow_not:
			type = OPTION_SHALLOW;
			goto normal_off;
		}
		arglex();
	}
}


static void set_command_line_goals _((void));

static void
set_command_line_goals()
{
	string_ty	*name;

	name = str_from_c("command-line-goals");
	id_assign(name, id_variable_new(&option.o_target));
	str_free(name);
}


/*
 * NAME
 *	main - initial entry point for cook
 *
 * SYNOPSIS
 *	void main(int argc, char **argv);
 *
 * DESCRIPTION
 *	Main is the initial entry point for cook.
 *
 * RETURNS
 *	Exit is always through exit().
 *	The exit code will be 0 for success, or 1 for some error.
 */

int main _((int, char **));

int
main(argc, argv)
	int		argc;
	char		**argv;
{
	int		retval;

	/*
	 * initialize things
	 * (order is critical here)
	 */
	progname_set(argv[0]);
	str_initialize();
	id_initialize(); 
	lex_initialize();

	/*
	 * parse the COOK environment variable
	 */
	arglex_init_from_env(argv[0], argtab);
	argparse(OPTION_LEVEL_ENVIRONMENT);

	/*
	 * parse the command line
	 */
	arglex_init(argc, argv, argtab);
	argparse(OPTION_LEVEL_COMMAND_LINE);

	option_tidy_up();

	log_open();

	/*
	 * turn on progress stars if they asked for them
	 */
	if (option_test(OPTION_STAR))
		star_enable();

	/*
	 * If we were asked to update the fingerprints, do it here.
	 * We don't actually ant to read in the cookbook.
	 */
	if (option.fingerprint_update)
	{
		fp_tweak();
		quit(0);
	}

	/*
	 * read in the cook book
	 *
	 * If there are #include-cooked directives,
	 * we may need to do it more than once.
	 */
	if (!option.o_book)
		fatal_intl(0, i18n("no book found"));
	for (;;)
	{
		int	status;
		long	j;

		builtin_initialize();

		/*
		 * instanciate the command line variable assignments
		 */
		for (j = 0; j < option.o_vardef.nstrings; ++j)
		{
			char		*s;
			char		*cp;
			string_ty	*name;
			string_ty	*value;
			string_list_ty	wl;
	
			s = option.o_vardef.string[j]->str_text;
			cp = strchr(s, '=');
			assert(cp);
			if (!cp)
				continue;
			name = str_n_from_c(s, cp - s); 
			value = str_from_c(cp + 1);
			str2wl(&wl, value, (char *)0, 0);
			str_free(value);
			id_assign(name, id_variable_new(&wl));
			str_free(name);
			string_list_destructor(&wl);
		}

		set_command_line_goals();

		parse(option.o_book);
		status = cook_auto_required();
		if (status < 0)
			quit(1);
		if (!status)
			break;
		id_reset();
		cook_reset();
	}

	/*
	 * work out what to cook.
	 * If no targets have been given, use the first explicit recipe.
	 */
	set_command_line_goals();
	if (!option.o_target.nstrings)
		cook_find_default(&option.o_target);
	assert(option.o_target.nstrings);

	/*
	 * cook the target
	 */
	if (option.pairs)
		retval = cook_pairs(&option.o_target);
	else if (option.script)
		retval = cook_script(&option.o_target);
	else if (option.web)
		retval = cook_web(&option.o_target);
	else
		retval = cook(&option.o_target);

	quit(retval);
	/*NOTREACHED*/
	return 0;
}