-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrulparse.c
1745 lines (1438 loc) · 54.4 KB
/
rulparse.c
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
/*
--
-- SYNOPSIS
-- Perform semantic analysis on input
--
-- DESCRIPTION
-- This code performs semantic analysis on the input, and builds
-- the complex internal datastructure that is used to represent
-- the user makefile.
--
-- AUTHOR
-- Dennis Vadura, [email protected]
--
-- WWW
-- http://dmake.wticorp.com/
--
-- COPYRIGHT
-- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
--
-- This program is NOT free software; you can redistribute it and/or
-- modify it under the terms of the Software License Agreement Provided
-- in the file <distribution-root>/readme/license.txt.
--
-- LOG
-- Use cvs log to obtain detailed change logs.
*/
#include "extern.h"
/* prototypes for local functions */
static void _add_indirect_prereq ANSI((CELLPTR));
static int _add_root ANSI((CELLPTR));
static CELLPTR _build_graph ANSI((int, CELLPTR, CELLPTR));
static char* _build_meta ANSI((char*));
static int _do_magic ANSI((int, char*, CELLPTR, CELLPTR, t_attr, char*));
static void _do_special ANSI((int, int, t_attr,char*,CELLPTR,CELLPTR,int*));
static int _do_targets ANSI((int, t_attr, char*, CELLPTR, CELLPTR));
static t_attr _is_attribute ANSI((char*));
static int _is_special ANSI((char*));
static char* _is_magic ANSI((char*));
static int _is_percent ANSI((char*));
static CELLPTR _make_multi ANSI((CELLPTR));
static CELLPTR _replace_cell ANSI((CELLPTR,CELLPTR,CELLPTR));
static void _set_attributes ANSI((t_attr, char*, CELLPTR ));
static void _stick_at_head ANSI((CELLPTR, CELLPTR));
static void _set_global_attr ANSI((t_attr));
/* static variables that must persist across invocation of Parse_rule_def */
static CELLPTR _sv_targets = NIL(CELL);
static STRINGPTR _sv_rules = NIL(STRING); /* first recipe element. */
static STRINGPTR _sv_crule = NIL(STRING); /* current/last recipe element. */
static CELLPTR _sv_edgel = NIL(CELL);
static LINKPTR _sv_ind_prq = NIL(LINK); /* indirect prerequisites for % cell */
static int _sp_target = FALSE;
static t_attr _sv_attr;
static int _sv_flag;
/* unused in Bind_rules_to_targets */
#if 0
static int _sv_op;
#endif
static char *_sv_setdir;
static char _sv_globprq_only = 0;
/* Define for global attribute mask (A_SWAP == A_WINPATH) */
#define A_GLOB (A_PRECIOUS | A_SILENT | A_IGNORE | A_EPILOG | A_SWAP |\
A_SHELL | A_PROLOG | A_NOINFER | A_SEQ | A_MKSARGS )
PUBLIC int
Parse_rule_def( state )/*
=========================
Parse the rule definition contained in Buffer, and modify the state
if appropriate. The function returns 0, if the definition is found to
be an illegal rule definition, and it returns 1 if it is a rule definition.
*/
int *state;
{
TKSTR input; /* input string struct for token search */
CELLPTR targets; /* list of targets if any */
CELLPTR prereq; /* list of prereq if any */
CELLPTR prereqtail; /* tail of prerequisite list */
CELLPTR cp; /* temporary cell pointer for list making */
char *result; /* temporary storage for result */
char *tok; /* temporary pointer for tokens */
char *set_dir; /* value of setdir attribute */
char *brk; /* break char list for Get_token */
char *firstrcp; /* first recipe line, from ; in rule line */
t_attr attr; /* sum of attribute flags for current tgts*/
t_attr at; /* temp place to keep an attribute code */
int op; /* rule operator */
int special; /* indicate special targets in rule */
int augmeta; /* indicate .<suffix> like target */
int percent; /* indicate percent rule target */
int percent_prq; /* indicate mixed %-rule prereq possible */
DB_ENTER( "Parse_rule_def" );
op = 0;
attr = 0;
special = 0;
augmeta = 0;
percent = 0;
set_dir = NIL( char );
targets = NIL(CELL);
prereq = NIL(CELL);
prereqtail = NIL(CELL);
percent_prq = 0;
/* Check to see if the line is of the form:
* targets : prerequisites; first recipe line
* If so remember the first_recipe part of the line. */
firstrcp = strchr( Buffer, ';' );
if( firstrcp != NIL( char ) ) {
*firstrcp++ = 0;
firstrcp = DmStrSpn( firstrcp, " \t" );
}
result = Expand( Buffer );
/* Remove CONTINUATION_CHAR, keep the <nl> */
for( brk=strchr(result,CONTINUATION_CHAR); brk != NIL(char); brk=strchr(brk,CONTINUATION_CHAR) )
if( brk[1] == '\n' )
*brk = ' ';
else
brk++;
DB_PRINT( "par", ("Scanning: [%s]", result) );
SET_TOKEN( &input, result );
brk = ":-^!|";
Def_targets = TRUE;
/* Scan the input rule line collecting targets, the operator, and any
* prerequisites. Stop when we run out of targets and prerequisites. */
while( *(tok = Get_token( &input, brk, TRUE )) != '\0' )
if( !op ) {
/* we are scanning targets and attributes
* check to see if token is an operator. */
op = Rule_op( tok );
if( !op ) {
/* Define a new cell, or get pointer to pre-existing cell. */
/* Do we need cells for attributes? If not move the definition
* to the target part. */
cp = Def_cell( tok );
/* A $ character indicates either a literal $ in the pathname (this
* was broken before) or a dynamic macro (this is a syntax error).
* FIXME: Here would be the place to add a sanity check. */
DB_PRINT( "par", ("tg_cell [%s]", tok) );
if( (at = _is_attribute(tok)) != 0 ) {
/* Ignore .SILENT when -vr is active. */
if( (Verbose & V_FORCEECHO) && (at == A_SILENT) )
at = 0;
/* Logically OR the attributes specified into one main
* ATTRIBUTE mask. */
if( at == A_SETDIR ) {
if( set_dir != NIL( char ) )
Warning( "Multiple .SETDIR attribute ignored" );
else
set_dir = DmStrDup( tok );
}
attr |= at;
}
else {
/* Not an attribute, this must be a target. */
int tmp;
tmp = _is_special( tok );
if( _is_percent( tok ) ) {
/* First %-target checks if there were non-%-targets before. */
if( !percent && targets != NIL(CELL) )
Fatal( "A %%-target must not be mixed with non-%%-targets, offending target [%s]", tok );
percent++;
cp->ce_flag |= F_PERCENT;
} else {
if( percent )
Fatal( "A non-%%-target must not be mixed with %%-targets, offending target [%s]", tok );
}
if( _is_magic( tok ) ) {
/* Check that AUGMAKE targets are not mixed with other
* targets. The return value of _is_magic() is discarded and
* calculated again in _do_targets() if this rule definition
* really is a .<suffix> like target.
* If we would allow only one target per line we could easily
* store the result for later, but for multiple .<suffix>
* targets this creates too much overhead.
* These targets should be rare (obsolete?) anyway. */
if( !augmeta && targets != NIL(CELL) )
Fatal( "An AUGMAKE meta target must not be mixed with non AUGMAKE meta targets, offending target [%s]", tok );
augmeta++;
cp->ce_flag |= F_MAGIC; /* do_magic will also add F_PERCENT later. */
} else {
if( augmeta )
Fatal( "A non AUGMAKE meta target must not be mixed with AUGMAKE meta targets, offending target [%s]", tok );
}
if( special )
Fatal( "Special target must appear alone, found [%s]", tok );
else if( !(cp->ce_flag & F_MARK) ) {
/* Targets are kept in this list in lexically sorted order.
* This allows for easy equality comparison of target
* sets.*/
CELLPTR prev,cur;
for(prev=NIL(CELL),cur=targets;cur;prev=cur,cur=cur->ce_link)
if(strcmp(cur->CE_NAME,cp->CE_NAME) > 0)
break;
cp->ce_link = cur;
if (!prev)
targets = cp;
else
prev->ce_link = cp;
cp->ce_flag |= F_MARK | F_EXPLICIT;
special = tmp;
}
else
Warning( "Duplicate target [%s]", cp->CE_NAME );
}
}
else {
/* found an operator so empty out break list and clear mark
* bits on target list, setting them all to F_VISITED*/
brk = "";
for( cp=targets; cp != NIL(CELL); cp=cp->ce_link ) {
cp->ce_flag ^= F_MARK;
cp->ce_flag |= F_VISITED;
}
Def_targets = FALSE;
}
}
else {
/* Scanning prerequisites so build the prerequisite list. We use
* F_MARK flag to make certain we have only a single copy of the
* prerequisite in the list */
cp = Def_cell( tok );
/* %-prerequisits require eiter a %-target or this might be a rule of
* the "ATTRIBUTE_LIST : targets" form. */
if( _is_percent( tok ) ) {
if( percent || ((targets == NIL(CELL)) && attr) )
percent_prq = 1;
else
Fatal( "Syntax error in %% rule, missing %% target");
}
if( cp->ce_flag & F_VISITED ) {
if( cp->ce_attr & A_COMPOSITE )
continue;
else
Fatal( "Detected circular dependency in graph at [%s]",
cp->CE_NAME );
}
else if( !(cp->ce_flag & F_MARK) ) {
DB_PRINT( "par", ("pq_cell [%s]", tok) );
cp->ce_flag |= F_MARK;
if( prereqtail == NIL(CELL) ) /* keep prereq's in order */
prereq = cp;
else
prereqtail->ce_link = cp;
prereqtail = cp;
cp->ce_link = NIL(CELL);
}
else if( !(cp->ce_attr & A_LIBRARY) && (Verbose & V_WARNALL))
Warning("Duplicate entry [%s] in prerequisite list",cp->CE_NAME);
}
/* Check to see if we have a percent rule that has only global
* prerequisites, i.e. they are of the form: "%.a : foo".
* If so then set the flag so that later on, we don't issue
* an error if such targets supply an empty set of rules. */
if( percent && !percent_prq && (prereq != NIL(CELL)) )
_sv_globprq_only = 1;
/* It's ok to have targets with attributes, and no prerequisites, but it's
* not ok to have no targets and no attributes, or no operator */
CLEAR_TOKEN( &input ); FREE(result); result = NIL(char);
if( !op ) {
DB_PRINT( "par", ("Not a rule [%s]", Buffer) );
DB_RETURN( 0 );
}
/* More than one percent target didn't work with prior versions. */
#if 0
if( (percent > 1) && !(op & R_OP_OR) )
Warning( "Prior to dmake 4.5 only one\n"
"%%-target per target-definition worked reliably. Check your makefiles.\n" );
#endif
if( !attr && targets == NIL(CELL) ) {
Fatal( "Missing targets or attributes in rule" );
if( set_dir != NIL( char )) FREE( set_dir );
DB_RETURN( 0 );
}
/* We have established we have a legal rules line, so we must process it.
* In doing so we must handle any special targets. Special targets must
* appear alone possibly accompanied by attributes.
* NOTE: special != 0 ==> targets != NIL(CELL) */
if( prereqtail != NIL(CELL) ) prereqtail->ce_link = NIL(CELL);
/* Clear out MARK bits used in duplicate checking. I originally wanted
* to do this as the lists get processed but that got too error prone
* so I bit the bullit and added these two loops. */
for( cp=prereq; cp != NIL(CELL); cp=cp->ce_link ) cp->ce_flag &= ~F_MARK;
for( cp=targets; cp != NIL(CELL); cp=cp->ce_link ) cp->ce_flag &= ~F_VISITED;
/* Check to see if the previous recipe was bound, if not the call
* Bind_rules_to_targets() to bind the recipe (_sv_rules) to the
* target(s) (_sv_targets). */
/* was: if( _sv_rules != NIL(STRING) ) Bind_rules_to_targets( F_DEFAULT );*/
/* Only Add_recipe_to_list() sets _sv_rules and Bind_rules_to_targets()
* clears the (static) variables again. Bind_rules_to_targets() is
* (should be) called after State is leaving RULE_SCAN in Parse().
* Abort if there are unbound recipes. FIXME: Remove this paragraph
* if this never occurs. */
if( _sv_rules != NIL(STRING) )
Fatal( "Internal Error: _sv_rules not empty." );
/* Add the first recipe line to the list */
if( firstrcp != NIL( char ) )
Add_recipe_to_list( firstrcp, TRUE, FALSE );
/* Save these prior to calling _do_targets, since _build_graph needs the
* _sv_setdir value for matching edges. */
#if 0
/* unused in Bind_rules_to_targets */
_sv_op = op;
#endif
_sv_setdir = set_dir;
if( special )
/* _do_special() can alter *state */
_do_special( special, op, attr, set_dir, targets, prereq, state );
else
*state = _do_targets( op, attr, set_dir, targets, prereq );
if( (*state != RULE_SCAN) && (_sv_rules != NIL(STRING)) )
Fatal( "Unexpected recipe found." );
DB_RETURN( 1 );
}
PUBLIC int
Rule_op( op )/*
================
Check the passed in op string and map it to one of the rule operators */
char *op;
{
int ret = 0;
DB_ENTER( "rule_op" );
if( *op == TGT_DEP_SEP ) {
ret = R_OP_CL;
op++;
/* All rule operations begin with a :, but may include any one of the
* four modifiers. In order for the rule to be properly mapped we must
* check for each of the modifiers in turn, building up our return bit
* string. */
while( *op && ret )
switch( *op ) {
case ':': ret |= R_OP_DCL; op++; break;
case '!': ret |= R_OP_BG; op++; break;
case '^': ret |= R_OP_UP; op++; break;
case '-': ret |= R_OP_MI; op++; break;
case '|': ret |= R_OP_OR; op++; break;
default : ret = 0; /* an invalid modifier, chuck whole string */
}
if( *op != '\0' ) ret = 0;
}
DB_RETURN( ret );
}
PUBLIC void
Add_recipe_to_list( rule, white_too, no_check )/*
=================================================
Take the provided string and add it to the list of recipe lines
we are saving to be added to the list of targets we have built
previously. If white_too == TRUE add the rule EVEN IF it contains only
an empty string (whitespace is handled by Def_recipe()). */
char *rule;
int white_too;
int no_check;
{
DB_ENTER( "Add_recipe_to_list" );
if( rule != NIL( char ) && (*rule != '\0' || white_too) ) {
DB_PRINT( "par", ("Adding recipe [%s]", rule) );
_sv_crule = Def_recipe( rule, _sv_crule, white_too, no_check );
/* If _sv_rules is not yet set this must be the first recipe line,
* remember it. */
if( _sv_rules == NIL(STRING) )
_sv_rules = _sv_crule;
}
DB_VOID_RETURN;
}
PUBLIC void
Bind_rules_to_targets( flag )/*
===============================
Take the recipe lines we have defined and bind them with proper attributes
to the targets that were previously defined in the parse. The
attributes that get passed here are merged with those that are were
previously defined. (namely attribute F_SINGLE) */
int flag;
{
CELLPTR tg; /* pointer to current target in list */
LINKPTR lp; /* pointer to link cell */
int magic; /* TRUE if target of % or .xxx.yyy form */
int tflag; /* TRUE if we assigned targets here */
DB_ENTER( "Bind_rules_to_targets" );
/* This line is needed since Parse may call us twice when the last
* GROUP rule appears at the end of file. In this case the rules
* have already been bound and we want to ignore them. */
if( _sv_targets == NIL(CELL) ) { DB_VOID_RETURN; }
tflag = FALSE;
flag |= (_sv_flag & F_SINGLE);
flag |= ((_sv_attr & A_GROUP) ? F_GROUP : 0);
for( tg = _sv_targets; tg != NIL(CELL); tg = tg->ce_link ) {
DB_PRINT( "par", ("Binding to %s, %04x", tg->CE_NAME, tg->ce_flag) );
magic = tg->ce_flag & F_PERCENT;
#if 0
/* Check to see if we had a rule of the form '%.o : a.h b.h ; xxx'
* In which case we must build a NULL prq node to hold the recipe */
if( _sv_globprq_only && (_sv_rules != NIL(STRING)) )
_build_graph( _sv_op, tg, NIL(CELL) );
#endif
/* NOTE: For targets that are magic or special we ignore any
* previously defined rules, ie. We throw away the old definition
* and use the new, otherwise we complain. */
if( !(tg->ce_flag & F_MULTI) && !magic && (tg->CE_RECIPE != NIL(STRING))
&& !_sp_target && (_sv_rules != NIL(STRING)) )
Fatal( "Multiply defined recipe for target %s", tg->CE_NAME );
if( (magic || _sp_target) && (_sv_rules == NIL(STRING)) &&
!(tg->ce_flag & F_SPECIAL) && !_sv_globprq_only )
Warning( "Empty recipe for special or meta target %s", tg->CE_NAME );
if( magic ) {
CELLPTR ep;
for( ep=_sv_edgel; ep != NIL(CELL); ep=ep->ce_link ) {
DB_PRINT( "par", ("ep address: %#x", ep) );
/* %.xx :| '%.yy' abc xx '%.tt' ; touch $@
* loops here ... */
_set_attributes( _sv_attr, _sv_setdir, ep );
ep->ce_flag |= (F_TARGET|flag);
if( _sv_rules != NIL(STRING) ) {
ep->ce_recipe = _sv_rules;
ep->ce_indprq = _sv_ind_prq;
}
}
}
else {
tg->ce_attr |= _sv_attr;
tg->ce_flag |= flag;
if( _sv_rules != NIL(STRING) ) {
tg->ce_recipe = _sv_rules;
tg->ce_flag |= F_RULES | F_TARGET;
/* Bind the current set of prerequisites as belonging to the
* original recipe given for the target */
for( lp=tg->ce_prq; lp != NIL(LINK); lp = lp->cl_next )
if( !(lp->cl_flag & F_VISITED) ) lp->cl_flag |= F_TARGET;
}
else for( lp=tg->ce_prq; lp != NIL(LINK); lp = lp->cl_next )
lp->cl_flag |= F_VISITED;
}
tflag |= _add_root(tg);
}
if( tflag ) Target = TRUE;
if( _sv_setdir ) FREE(_sv_setdir);
_sv_rules = NIL(STRING);
_sv_crule = NIL(STRING);
_sv_targets = NIL(CELL);
_sv_ind_prq = NIL(LINK);
_sv_edgel = NIL(CELL);
_sp_target = FALSE;
_sv_globprq_only = 0;
DB_VOID_RETURN;
}
PUBLIC int
Set_group_attributes( list )/*
==============================
Scan list looking for the standard @,-,% and + (as in recipe line
defs) (+ is set but ignored for group recipes)
and set the flags accordingly so that they apply when we bind the
rules to the appropriate targets.
Return TRUE if group recipe start '[' was found, otherwise FALSE. */
char *list;
{
int res = FALSE;
char *s;
if ( !((_sv_attr|Glob_attr)&A_IGNOREGROUP) ) {
s = DmStrSpn(list,"@-%+ \t");
res = (*s == '[');
if( res ) {
/* Check for non-white space characters after the [. */
for( s++; *s && iswhite(*s) ; s++ )
;
if( *s )
Warning("Found non-white space character after '[' in [%s].", list);
_sv_attr |= Rcp_attribute(list);
}
}
return(res);
}
static void
_do_special( special, op, attr, set_dir, target, prereq, state )/*
==================================================================
Process a special target (always only a single target). So far the only
special targets we have are those recognized by the _is_special function.
Some of the special targets can take recipes, they call _do_targets()
and (implicitly) set *state to to RULE_SCAN. Otherwise *state remains
unaffected, i.e. NORMAL_SCAN.
target is always only a single special target.
NOTE: For the cases of .IMPORT, and .INCLUDE, the cells created by the
parser are never freed. This is due to the fact that it is too much
trouble to get them out of the hash table once they are defined, and
if they are per chance used again it will be ok, anyway, since the
cell is not really used by the code below. */
int special;
int op;
t_attr attr;
char *set_dir;
CELLPTR target;
CELLPTR prereq;
int *state;
{
HASHPTR hp; /* pointer to macro def cell */
CELLPTR cp; /* temporary pointer into cells list */
CELLPTR dp; /* pointer to directory dir cell */
LINKPTR lp; /* pointer at prerequisite list */
char *dir; /* current dir to prepend */
char *path; /* resulting path to try to read */
char *name; /* File name for processing a .INCLUDE */
char *tmp; /* temporary string pointer */
FILE *fil; /* File descriptor returned by Openfile */
DB_ENTER( "_do_special" );
target->ce_flag = F_SPECIAL; /* mark the target as special */
switch( special ) {
case ST_EXPORT:
for( ; prereq != NIL(CELL); prereq = prereq->ce_link ) {
DB_PRINT( "par", ("Exporting [%s]", prereq->CE_NAME) );
hp = GET_MACRO( prereq->CE_NAME );
if( hp != NIL(HASH) ) {
char *tmpstr = hp->ht_value;
if( tmpstr == NIL(char) ) tmpstr = "";
if( Write_env_string( prereq->CE_NAME, tmpstr ) != 0 )
Warning( "Could not export %s", prereq->CE_NAME );
}
}
break;
/* Simply cause the parser to fail on the next input read */
case ST_EXIT:
Skip_to_eof = TRUE;
break;
case ST_IMPORT:
for( ; prereq != NIL(CELL); prereq = prereq->ce_link ) {
char *tmpstr;
DB_PRINT( "par", ("Importing [%s]", prereq->CE_NAME) );
if( strcmp(prereq->CE_NAME, ".EVERYTHING") == 0 ) {
t_attr sattr = Glob_attr;
Glob_attr |= A_SILENT;
ReadEnvironment();
Glob_attr = sattr;
}
else {
tmpstr = Read_env_string( prereq->CE_NAME );
if( tmpstr != NIL(char) )
Def_macro(prereq->CE_NAME, tmpstr, M_EXPANDED|M_LITERAL);
else
if( !((Glob_attr | attr) & A_IGNORE) )
Fatal("Imported macro `%s' not found",prereq->CE_NAME);
}
}
attr &= ~A_IGNORE;
break;
case ST_INCLUDE:
{
int pushed = FALSE;
int first = (attr & A_FIRST);
int ignore = (((Glob_attr | attr) & A_IGNORE) != 0);
int found = FALSE;
int noinf = (attr & A_NOINFER);
LINKPTR prqlnk = NIL(LINK);
LINKPTR prqlst = NIL(LINK);
if( prereq == NIL(CELL) ) Fatal( "No .INCLUDE file(s) specified" );
dp = Def_cell( ".INCLUDEDIRS" );
if( (attr & A_SETDIR) && *(dir = strchr(set_dir, '=')+1) )
pushed = Push_dir( dir, ".INCLUDE", ignore );
for( cp=prereq; cp != NIL(CELL); cp = cp->ce_link ) {
LINKPTR ltmp;
TALLOC(ltmp, 1, LINK);
ltmp->cl_prq = cp;
if( prqlnk == NIL(LINK) )
prqlst = ltmp;
else
prqlnk->cl_next = ltmp;
prqlnk = ltmp;
}
for( ; prqlst != NIL(LINK); FREE(prqlst), prqlst=prqlnk ) {
prqlnk = prqlst->cl_next;
cp = prqlst->cl_prq;
name = cp->CE_NAME;
/* Leave this here, it ensures that prqlst gets propely free'd */
if ( first && found )
continue;
if( *name == '<' ) {
/* We have a file name enclosed in <....>
* so get rid of the <> arround the file name */
name++;
if( (tmp = strrchr( name, '>' )) != NIL( char ) )
*tmp = 0;
if( If_root_path( name ) )
fil = Openfile( name, FALSE, FALSE );
else
fil = NIL(FILE);
}
else
fil = Openfile( name, FALSE, FALSE );
if( fil == NIL(FILE) && !If_root_path( name ) ) { /*if true ==> not found in current dir*/
/* Now we must scan the list of prerequisites for .INCLUDEDIRS
* looking for the file in each of the specified directories.
* if we don't find it then we issue an error. The error
* message is suppressed if the .IGNORE attribute of attr is
* set. If a file is found we call Parse on the file to
* perform the parse and then continue on from where we left
* off. */
for(lp=dp->CE_PRQ; lp && fil == NIL(FILE); lp=lp->cl_next) {
dir = lp->cl_prq->CE_NAME;
if( strchr(dir, '$') ) dir = Expand(dir);
path = Build_path( dir, name );
DB_PRINT( "par", ("Trying to include [%s]", path) );
fil = Openfile( path, FALSE, FALSE );
if( dir != lp->cl_prq->CE_NAME ) FREE(dir);
}
}
if (!noinf && fil == NIL(FILE)) {
t_attr glob = Glob_attr;
t_attr cattr = prqlst->cl_prq->ce_attr;
prqlst->cl_next = NIL(LINK);
Glob_attr |= (attr&A_IGNORE);
prqlst->cl_prq->ce_attr &= ~A_FRINGE;
if( Verbose & V_FILE_IO )
printf( "%s: Inferring include file [%s].\n",
Pname, name );
fil = TryFiles(prqlst);
Glob_attr = glob;
prqlst->cl_prq->ce_attr |= (cattr & A_FRINGE);
}
if( fil != NIL(FILE) ) {
if( Verbose & V_FILE_IO )
printf( "%s: Parsing include file [%s].\n",
Pname, name );
Parse( fil );
found = TRUE;
}
else if( !(ignore || first) )
Fatal( "Include file %s, not found", name );
else if( Verbose & V_FILE_IO )
printf( "%s: Include file [%s] was not found.\n",
Pname, name );
}
if ( !ignore && first && !found )
Fatal( "No include file was found" );
if( pushed ) Pop_dir(FALSE);
attr &= ~(A_IGNORE|A_SETDIR|A_FIRST|A_NOINFER);
}
break;
case ST_SOURCE:
if( prereq != NIL(CELL) )
_do_targets( op & (R_OP_CL | R_OP_MI | R_OP_UP), attr, set_dir,
target, prereq );
else {
/* The old semantics of .SOURCE were that an empty list of
* prerequisites clears the .SOURCE list. So we must implement
* that here as a clearout prerequisite operation. Since this is
* a standard operation with the :- opcode we can simply call the
* proper routine with the target cell and it should do the trick
*/
if( op == R_OP_CL || (op & R_OP_MI) )
Clear_prerequisites( target );
}
op &= ~(R_OP_MI | R_OP_UP);
break;
case ST_KEEP:
if( Keep_state != NIL(char) ) break;
Def_macro( ".KEEP_STATE", "_state.mk", M_EXPANDED );
break;
case ST_REST:
/* The rest of the special targets can all take recipes, as such they
* must be able to affect the state of the parser. */
{
int s_targ = Target;
Target = TRUE;
_sp_target = TRUE;
*state = _do_targets( op, attr, set_dir, target, prereq );
Target = s_targ;
target->ce_flag |= F_TARGET;
attr = A_DEFAULT;
op = R_OP_CL;
}
break;
default:break;
}
if( op != R_OP_CL ) Warning( "Modifier(s) for operator ignored" );
if( attr != A_DEFAULT ) Warning( "Extra attributes ignored" );
DB_VOID_RETURN;
}
static int
_do_targets( op, attr, set_dir, targets, prereq )/*
===================================================
Evaluate the values derived from the current target definition
line. Helper functions _build_graph(), _do_magic(), _make_multi(),
_add_root(), _replace_cell(), _set_attributes(), Clear_prerequisites()
_stick_at_head(), Add_prerequisite() and _set_global_attr() are used.
If successfull "_sv_targets" is set to "targets".
Return RULE_SCAN if a recipe is expected to follow, otherwise
NORMAL_SCAN. */
int op; /* rule operator */
t_attr attr; /* attribute flags for current targets */
char *set_dir; /* value of setdir attribute */
CELLPTR targets; /* list of targets (each cell maybe already
* defined by a previous target definition
* line. */
CELLPTR prereq; /* list of prerequisites */
{
CELLPTR tg1; /* temporary target pointer */
CELLPTR tp1; /* temporary prerequisite pointer */
LINKPTR prev_cell; /* pointer for .UPDATEALL processing */
char *p; /* temporary char pointer */
int tflag = FALSE; /* set to TRUE if we add target to root */
int ret_state = RULE_SCAN; /* Return state */
DB_ENTER( "_do_targets" );
/* If .UPDATEALL is set sort the target list that was temporary linked
* with ce_link into a list using ce_link with ce_set pointing to the first
* element. */
/* FIXME: Check that .UPDATEALL and %-targets on one line work together. */
if( attr & A_UPDATEALL ) {
if( targets == NIL(CELL) )
Fatal( ".UPDATEALL attribute requires non-empty list of targets" );
if (targets->ce_set == NIL(CELL)) {
for(
prev_cell=CeMeToo(targets),tg1=targets->ce_link;
tg1 != NIL(CELL);
tg1=tg1->ce_link
) {
if (tg1->ce_set)
Fatal( "Target [%s] appears on multiple .UPDATEALL lists",
tg1->CE_NAME);
tg1->ce_set = targets;
TALLOC(prev_cell->cl_next, 1, LINK);
prev_cell = prev_cell->cl_next;
prev_cell->cl_prq = tg1;
}
targets->ce_set = targets;
}
else {
LINKPTR ap;
CELLPTR tp;
tp = targets;
ap = CeMeToo(targets);
while (ap && tp && ap->cl_prq == tp && tp->ce_set == targets) {
ap = ap->cl_next;
tp = tp->ce_link;
}
if (ap || tp)
Fatal("Inconsistent .UPDATEALL lists for target [%s]",
targets->CE_NAME);
}
targets->ce_link = NIL(CELL);
}
for( tg1 = targets; tg1 != NIL(CELL); tg1 = tg1->ce_link ) {
/* Check if tg1 is already marked as a %-target, but not a magic
* (.xxx.yyy) target. */
int purepercent = (tg1->ce_flag & F_PERCENT) && !(tg1->ce_flag & F_MAGIC);
/* Check each target. Check for inconsistencies between :: and : rule
* sets. :: may follow either : or :: but not the reverse.
*
* Any F_MULTI target (contains :: rules) is represented by a prerequisite
* list hanging off the main target cell where each of the prerequisites
* is a copy of the target cell but is not entered into the hash table.
*/
if( !(op & R_OP_DCL ) && (tg1->ce_flag & F_MULTI) && !purepercent )
Fatal( "':' vs '::' inconsistency in rules for %s", tg1->CE_NAME );
if( purepercent ) {
/* Handle %-targets. */
CELLPTR cur;
CELLPTR tpq = NIL(CELL);
CELLPTR nprq = NULL;
#ifdef DBUG
DB_PRINT( "%", ("Handling %%-target [%s : : <prerequisites follow, maybe empty>]",
tg1->CE_NAME) );
for(cur=prereq;cur;cur=cur->ce_link) {
DB_PRINT( "%", (" %%-prerequisites : %s ",
cur->CE_NAME ? cur->CE_NAME : "<empty>") );
}
#endif
/* Handle indirect (global) prerequisites first. */
for(cur=prereq;cur;cur=cur->ce_link) {
char *name = cur->CE_NAME;
int len = strlen(name);
if( *name == '\'' && name[len-1]=='\'' ){
name[len-1] = '\0';
len = strlen(name+1)+1;
memmove(name,name+1,len);
/* add indirect prerequisite */
_add_indirect_prereq( cur );
}
else {
/* Sort all "other" prerequisits into tpq, with nprq
* pointing to the first element. */
if (tpq)
tpq->ce_link = cur;
else
nprq = cur;
tpq = cur;
}
}
/* Mark the last element of nprq. */
if(tpq)
tpq->ce_link=NIL(CELL);
else
nprq = NIL(CELL);
/* Handle "normal" prerequisites now. */
if ( op & R_OP_OR ) {
/* for op == ':|' transform:
* <%-target> :| <prereq_1> ... <prereq_n> ; <recipe>
* into:
* <%-target> : <prereq_1> ; <recipe>
* ..
* <%-target> : <prereq_n> ; <recipe>
*/
for(tp1=nprq; tp1; tp1=tp1->ce_link) {
CELLPTR tmpcell = tp1->ce_link;
tp1->ce_link = NIL(CELL);
_build_graph(op,tg1,tp1);
tp1->ce_link = tmpcell;
}
}
else {
/* The inference mechanism for %-targets limits the number of
* (non-indirect) prerequisite to one, but an unlimited number
* of indirect prerequisites is possible. */
if ( nprq && nprq->ce_link && !(op & R_OP_OR))
Warning("More than one prerequisite\n"
"for %%-target. Use :| ruleop or indirect prerequisites.");
_build_graph(op,tg1,nprq);
}
}
else if( tg1->ce_flag & F_MAGIC &&
(p = _is_magic( tg1->CE_NAME )) != NIL(char) &&
_do_magic( op, p, tg1, prereq, attr, set_dir ) )
; /* _do_magic() does all that is needed (if return value is TRUE). */
else if( op & R_OP_DCL ) { /* op == :: */
CELLPTR tmp_cell = _make_multi(tg1);
/* Add the F_MULTI master to .TARGETS (If not set already).
* Do this here so that the member cell is not added instead
* when the recipies are bound in Bind_rules_to_targets(). */
tflag |= _add_root(tg1);
/* Replace the F_MULTI master with the member cell. */
targets = _replace_cell( targets, tg1, tmp_cell );
/* We have to set (add) the attributes also for the F_MULTI master
* target cell. As there is no recipe the setdir value is not
* needed. _set_attributes() that follows in approx. 8 lines
* will set the attributes for the F_MULTI member cell. */
tg1->ce_attr |= (attr & ~A_SETDIR);
/* Now switch tg1 to the current (F_MULTI prereq.) target.
* All recipes have to be added to that cell and not to the
* F_MULTI master. */
tg1 = tmp_cell;
}
if( !purepercent ) _set_attributes( attr, set_dir, tg1 );