merge the formfield patch from ooo-build
[ooovba.git] / dmake / rulparse.c
blobc02860745bd36fb6552ca807eb37a431f25ebefd
1 /* $RCSfile: rulparse.c,v $
2 -- $Revision: 1.12 $
3 -- last change: $Author: ihi $ $Date: 2007-10-15 15:41:24 $
4 --
5 -- SYNOPSIS
6 -- Perform semantic analysis on input
7 --
8 -- DESCRIPTION
9 -- This code performs semantic analysis on the input, and builds
10 -- the complex internal datastructure that is used to represent
11 -- the user makefile.
12 --
13 -- AUTHOR
14 -- Dennis Vadura, dvadura@dmake.wticorp.com
16 -- WWW
17 -- http://dmake.wticorp.com/
19 -- COPYRIGHT
20 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
21 --
22 -- This program is NOT free software; you can redistribute it and/or
23 -- modify it under the terms of the Software License Agreement Provided
24 -- in the file <distribution-root>/readme/license.txt.
26 -- LOG
27 -- Use cvs log to obtain detailed change logs.
30 #include "extern.h"
32 /* prototypes for local functions */
33 static void _add_indirect_prereq ANSI((CELLPTR));
34 static int _add_root ANSI((CELLPTR));
35 static CELLPTR _build_graph ANSI((int, CELLPTR, CELLPTR));
36 static char* _build_meta ANSI((char*));
37 static int _do_magic ANSI((int, char*, CELLPTR, CELLPTR, t_attr, char*));
38 static void _do_special ANSI((int, int, t_attr,char*,CELLPTR,CELLPTR,int*));
39 static int _do_targets ANSI((int, t_attr, char*, CELLPTR, CELLPTR));
40 static t_attr _is_attribute ANSI((char*));
41 static int _is_special ANSI((char*));
42 static char* _is_magic ANSI((char*));
43 static int _is_percent ANSI((char*));
44 static CELLPTR _make_multi ANSI((CELLPTR));
45 static CELLPTR _replace_cell ANSI((CELLPTR,CELLPTR,CELLPTR));
46 static void _set_attributes ANSI((t_attr, char*, CELLPTR ));
47 static void _stick_at_head ANSI((CELLPTR, CELLPTR));
48 static void _set_global_attr ANSI((t_attr));
51 /* static variables that must persist across invocation of Parse_rule_def */
52 static CELLPTR _sv_targets = NIL(CELL);
53 static STRINGPTR _sv_rules = NIL(STRING); /* first recipe element. */
54 static STRINGPTR _sv_crule = NIL(STRING); /* current/last recipe element. */
55 static CELLPTR _sv_edgel = NIL(CELL);
56 static LINKPTR _sv_ind_prq = NIL(LINK); /* indirect prerequisites for % cell */
57 static int _sp_target = FALSE;
58 static t_attr _sv_attr;
59 static int _sv_flag;
60 static int _sv_op;
61 static char *_sv_setdir;
62 static char _sv_globprq_only = 0;
64 /* Define for global attribute mask (A_SWAP == A_WINPATH) */
65 #define A_GLOB (A_PRECIOUS | A_SILENT | A_IGNORE | A_EPILOG | A_SWAP |\
66 A_SHELL | A_PROLOG | A_NOINFER | A_SEQ | A_MKSARGS )
69 PUBLIC int
70 Parse_rule_def( state )/*
71 =========================
72 Parse the rule definition contained in Buffer, and modify the state
73 if appropriate. The function returns 0, if the definition is found to
74 be an illegal rule definition, and it returns 1 if it is a rule definition.
76 int *state;
78 TKSTR input; /* input string struct for token search */
79 CELLPTR targets; /* list of targets if any */
80 CELLPTR prereq; /* list of prereq if any */
81 CELLPTR prereqtail; /* tail of prerequisite list */
82 CELLPTR cp; /* temporary cell pointer for list making */
83 char *result; /* temporary storage for result */
84 char *tok; /* temporary pointer for tokens */
85 char *set_dir; /* value of setdir attribute */
86 char *brk; /* break char list for Get_token */
87 char *firstrcp; /* first recipe line, from ; in rule line */
88 t_attr attr; /* sum of attribute flags for current tgts*/
89 t_attr at; /* temp place to keep an attribute code */
90 int op; /* rule operator */
91 int special; /* indicate special targets in rule */
92 int augmeta; /* indicate .<suffix> like target */
93 int percent; /* indicate percent rule target */
94 int percent_prq; /* indicate mixed %-rule prereq possible */
96 DB_ENTER( "Parse_rule_def" );
98 op = 0;
99 attr = 0;
100 special = 0;
101 augmeta = 0;
102 percent = 0;
103 set_dir = NIL( char );
104 targets = NIL(CELL);
105 prereq = NIL(CELL);
106 prereqtail = NIL(CELL);
107 percent_prq = 0;
109 /* Check to see if the line is of the form:
110 * targets : prerequisites; first recipe line
111 * If so remember the first_recipe part of the line. */
113 firstrcp = strchr( Buffer, ';' );
114 if( firstrcp != NIL( char ) ) {
115 *firstrcp++ = 0;
116 firstrcp = DmStrSpn( firstrcp, " \t" );
119 result = Expand( Buffer );
120 /* Remove CONTINUATION_CHAR, keep the <nl> */
121 for( brk=strchr(result,CONTINUATION_CHAR); brk != NIL(char); brk=strchr(brk,CONTINUATION_CHAR) )
122 if( brk[1] == '\n' )
123 *brk = ' ';
124 else
125 brk++;
127 DB_PRINT( "par", ("Scanning: [%s]", result) );
129 SET_TOKEN( &input, result );
130 brk = ":-^!|";
131 Def_targets = TRUE;
133 /* Scan the input rule line collecting targets, the operator, and any
134 * prerequisites. Stop when we run out of targets and prerequisites. */
136 while( *(tok = Get_token( &input, brk, TRUE )) != '\0' )
137 if( !op ) {
138 /* we are scanning targets and attributes
139 * check to see if token is an operator. */
141 op = Rule_op( tok );
143 if( !op ) {
144 /* Define a new cell, or get pointer to pre-existing cell. */
145 /* Do we need cells for attributes? If not move the definition
146 * to the target part. */
147 cp = Def_cell( tok );
148 /* A $ character indicates either a literal $ in the pathname (this
149 * was broken before) or a dynamic macro (this is a syntax error).
150 * FIXME: Here would be the place to add a sanity check. */
151 DB_PRINT( "par", ("tg_cell [%s]", tok) );
153 if( (at = _is_attribute(tok)) != 0 ) {
154 /* Ignore .SILENT when -vr is active. */
155 if( (Verbose & V_FORCEECHO) && (at == A_SILENT) )
156 at = 0;
158 /* Logically OR the attributes specified into one main
159 * ATTRIBUTE mask. */
161 if( at == A_SETDIR ) {
162 if( set_dir != NIL( char ) )
163 Warning( "Multiple .SETDIR attribute ignored" );
164 else
165 set_dir = DmStrDup( tok );
168 attr |= at;
170 else {
171 /* Not an attribute, this must be a target. */
172 int tmp;
174 tmp = _is_special( tok );
176 if( _is_percent( tok ) ) {
177 /* First %-target checks if there were non-%-targets before. */
178 if( !percent && targets != NIL(CELL) )
179 Fatal( "A %%-target must not be mixed with non-%%-targets, offending target [%s]", tok );
181 percent++;
182 cp->ce_flag |= F_PERCENT;
183 } else {
184 if( percent )
185 Fatal( "A non-%%-target must not be mixed with %%-targets, offending target [%s]", tok );
188 if( _is_magic( tok ) ) {
189 /* Check that AUGMAKE targets are not mixed with other
190 * targets. The return value of _is_magic() is discarded and
191 * calculated again in _do_targets() if this rule definition
192 * really is a .<suffix> like target.
193 * If we would allow only one target per line we could easily
194 * store the result for later, but for multiple .<suffix>
195 * targets this creates too much overhead.
196 * These targets should be rare (obsolete?) anyway. */
197 if( !augmeta && targets != NIL(CELL) )
198 Fatal( "An AUGMAKE meta target must not be mixed with non AUGMAKE meta targets, offending target [%s]", tok );
200 augmeta++;
201 cp->ce_flag |= F_MAGIC; /* do_magic will also add F_PERCENT later. */
202 } else {
203 if( augmeta )
204 Fatal( "A non AUGMAKE meta target must not be mixed with AUGMAKE meta targets, offending target [%s]", tok );
207 if( special )
208 Fatal( "Special target must appear alone, found [%s]", tok );
209 else if( !(cp->ce_flag & F_MARK) ) {
210 /* Targets are kept in this list in lexically sorted order.
211 * This allows for easy equality comparison of target
212 * sets.*/
213 CELLPTR prev,cur;
214 for(prev=NIL(CELL),cur=targets;cur;prev=cur,cur=cur->ce_link)
215 if(strcmp(cur->CE_NAME,cp->CE_NAME) > 0)
216 break;
218 cp->ce_link = cur;
220 if (!prev)
221 targets = cp;
222 else
223 prev->ce_link = cp;
225 cp->ce_flag |= F_MARK | F_EXPLICIT;
226 special = tmp;
228 else
229 Warning( "Duplicate target [%s]", cp->CE_NAME );
232 else {
233 /* found an operator so empty out break list and clear mark
234 * bits on target list, setting them all to F_VISITED*/
236 brk = "";
237 for( cp=targets; cp != NIL(CELL); cp=cp->ce_link ) {
238 cp->ce_flag ^= F_MARK;
239 cp->ce_flag |= F_VISITED;
242 Def_targets = FALSE;
245 else {
246 /* Scanning prerequisites so build the prerequisite list. We use
247 * F_MARK flag to make certain we have only a single copy of the
248 * prerequisite in the list */
250 cp = Def_cell( tok );
252 /* %-prerequisits require eiter a %-target or this might be a rule of
253 * the "ATTRIBUTE_LIST : targets" form. */
254 if( _is_percent( tok ) ) {
255 if( percent || ((targets == NIL(CELL)) && attr) )
256 percent_prq = 1;
257 else
258 Fatal( "Syntax error in %% rule, missing %% target");
261 if( cp->ce_flag & F_VISITED ) {
262 if( cp->ce_attr & A_COMPOSITE )
263 continue;
264 else
265 Fatal( "Detected circular dependency in graph at [%s]",
266 cp->CE_NAME );
268 else if( !(cp->ce_flag & F_MARK) ) {
269 DB_PRINT( "par", ("pq_cell [%s]", tok) );
270 cp->ce_flag |= F_MARK;
272 if( prereqtail == NIL(CELL) ) /* keep prereq's in order */
273 prereq = cp;
274 else
275 prereqtail->ce_link = cp;
277 prereqtail = cp;
278 cp->ce_link = NIL(CELL);
280 else if( !(cp->ce_attr & A_LIBRARY) && (Verbose & V_WARNALL))
281 Warning("Duplicate entry [%s] in prerequisite list",cp->CE_NAME);
284 /* Check to see if we have a percent rule that has only global
285 * prerequisites, i.e. they are of the form: "%.a : foo".
286 * If so then set the flag so that later on, we don't issue
287 * an error if such targets supply an empty set of rules. */
289 if( percent && !percent_prq && (prereq != NIL(CELL)) )
290 _sv_globprq_only = 1;
292 /* It's ok to have targets with attributes, and no prerequisites, but it's
293 * not ok to have no targets and no attributes, or no operator */
295 if( !op ) {
296 CLEAR_TOKEN( &input );
297 DB_PRINT( "par", ("Not a rule [%s]", Buffer) );
298 DB_RETURN( 0 );
301 /* More than one percent target didn't work with prior versions. */
302 if( (percent > 1) && !(op & R_OP_OR) )
303 Warning( "Prior to dmake 4.5 only one\n"
304 "%%-target per target-definition worked reliably. Check your makefiles.\n" );
306 if( !attr && targets == NIL(CELL) ) {
307 Fatal( "Missing targets or attributes in rule" );
308 if( set_dir != NIL( char )) FREE( set_dir );
309 DB_RETURN( 0 );
312 /* We have established we have a legal rules line, so we must process it.
313 * In doing so we must handle any special targets. Special targets must
314 * appear alone possibly accompanied by attributes.
315 * NOTE: special != 0 ==> targets != NIL(CELL) */
317 if( prereqtail != NIL(CELL) ) prereqtail->ce_link = NIL(CELL);
319 /* Clear out MARK bits used in duplicate checking. I originally wanted
320 * to do this as the lists get processed but that got too error prone
321 * so I bit the bullit and added these two loops. */
323 for( cp=prereq; cp != NIL(CELL); cp=cp->ce_link ) cp->ce_flag &= ~F_MARK;
324 for( cp=targets; cp != NIL(CELL); cp=cp->ce_link ) cp->ce_flag &= ~F_VISITED;
326 /* Check to see if the previous recipe was bound, if not the call
327 * Bind_rules_to_targets() to bind the recipe (_sv_rules) to the
328 * target(s) (_sv_targets). */
329 /* was: if( _sv_rules != NIL(STRING) ) Bind_rules_to_targets( F_DEFAULT );*/
330 /* Only Add_recipe_to_list() sets _sv_rules and Bind_rules_to_targets()
331 * clears the (static) variables again. Bind_rules_to_targets() is
332 * (should be) called after State is leaving RULE_SCAN in Parse().
333 * Abort if there are unbound recipes. FIXME: Remove this paragraph
334 * if this never occurs. */
335 if( _sv_rules != NIL(STRING) )
336 Fatal( "Internal Error: _sv_rules not empty." );
338 /* Add the first recipe line to the list */
339 if( firstrcp != NIL( char ) )
340 Add_recipe_to_list( firstrcp, TRUE, FALSE );
342 /* Save these prior to calling _do_targets, since _build_graph needs the
343 * _sv_setdir value for matching edges. */
344 _sv_op = op;
345 _sv_setdir = set_dir;
347 if( special )
348 /* _do_special() can alter *state */
349 _do_special( special, op, attr, set_dir, targets, prereq, state );
350 else
351 *state = _do_targets( op, attr, set_dir, targets, prereq );
353 if( (*state != RULE_SCAN) && (_sv_rules != NIL(STRING)) )
354 Fatal( "Unexpected recipe found." );
356 DB_RETURN( 1 );
360 PUBLIC int
361 Rule_op( op )/*
362 ================
363 Check the passed in op string and map it to one of the rule operators */
364 char *op;
366 int ret = 0;
368 DB_ENTER( "rule_op" );
370 if( *op == TGT_DEP_SEP ) {
371 ret = R_OP_CL;
372 op++;
374 /* All rule operations begin with a :, but may include any one of the
375 * four modifiers. In order for the rule to be properly mapped we must
376 * check for each of the modifiers in turn, building up our return bit
377 * string. */
379 while( *op && ret )
380 switch( *op ) {
381 case ':': ret |= R_OP_DCL; op++; break;
382 case '!': ret |= R_OP_BG; op++; break;
383 case '^': ret |= R_OP_UP; op++; break;
384 case '-': ret |= R_OP_MI; op++; break;
385 case '|': ret |= R_OP_OR; op++; break;
387 default : ret = 0; /* an invalid modifier, chuck whole string */
390 if( *op != '\0' ) ret = 0;
393 DB_RETURN( ret );
397 PUBLIC void
398 Add_recipe_to_list( rule, white_too, no_check )/*
399 =================================================
400 Take the provided string and add it to the list of recipe lines
401 we are saving to be added to the list of targets we have built
402 previously. If white_too == TRUE add the rule EVEN IF it contains only
403 an empty string (whitespace is handled by Def_recipe()). */
404 char *rule;
405 int white_too;
406 int no_check;
408 DB_ENTER( "Add_recipe_to_list" );
410 if( rule != NIL( char ) && (*rule != '\0' || white_too) ) {
411 DB_PRINT( "par", ("Adding recipe [%s]", rule) );
412 _sv_crule = Def_recipe( rule, _sv_crule, white_too, no_check );
414 /* If _sv_rules is not yet set this must be the first recipe line,
415 * remember it. */
416 if( _sv_rules == NIL(STRING) )
417 _sv_rules = _sv_crule;
420 DB_VOID_RETURN;
424 PUBLIC void
425 Bind_rules_to_targets( flag )/*
426 ===============================
427 Take the recipe lines we have defined and bind them with proper attributes
428 to the targets that were previously defined in the parse. The
429 attributes that get passed here are merged with those that are were
430 previously defined. (namely attribute F_SINGLE) */
431 int flag;
433 CELLPTR tg; /* pointer to current target in list */
434 LINKPTR lp; /* pointer to link cell */
435 int magic; /* TRUE if target of % or .xxx.yyy form */
436 int tflag; /* TRUE if we assigned targets here */
438 DB_ENTER( "Bind_rules_to_targets" );
440 /* This line is needed since Parse may call us twice when the last
441 * GROUP rule appears at the end of file. In this case the rules
442 * have already been bound and we want to ignore them. */
444 if( _sv_targets == NIL(CELL) ) { DB_VOID_RETURN; }
446 tflag = FALSE;
447 flag |= (_sv_flag & F_SINGLE);
448 flag |= ((_sv_attr & A_GROUP) ? F_GROUP : 0);
450 for( tg = _sv_targets; tg != NIL(CELL); tg = tg->ce_link ) {
451 DB_PRINT( "par", ("Binding to %s, %04x", tg->CE_NAME, tg->ce_flag) );
452 magic = tg->ce_flag & F_PERCENT;
454 #if 0
455 /* Check to see if we had a rule of the form '%.o : a.h b.h ; xxx'
456 * In which case we must build a NULL prq node to hold the recipe */
457 if( _sv_globprq_only && (_sv_rules != NIL(STRING)) )
458 _build_graph( _sv_op, tg, NIL(CELL) );
459 #endif
461 /* NOTE: For targets that are magic or special we ignore any
462 * previously defined rules, ie. We throw away the old definition
463 * and use the new, otherwise we complain. */
464 if( !(tg->ce_flag & F_MULTI) && !magic && (tg->CE_RECIPE != NIL(STRING))
465 && !_sp_target && (_sv_rules != NIL(STRING)) )
466 Fatal( "Multiply defined recipe for target %s", tg->CE_NAME );
468 if( (magic || _sp_target) && (_sv_rules == NIL(STRING)) &&
469 !(tg->ce_flag & F_SPECIAL) && !_sv_globprq_only )
470 Warning( "Empty recipe for special or meta target %s", tg->CE_NAME );
472 if( magic ) {
473 CELLPTR ep;
475 for( ep=_sv_edgel; ep != NIL(CELL); ep=ep->ce_link ) {
476 DB_PRINT( "par", ("ep address: %#x", ep) );
477 /* %.xx :| '%.yy' abc xx '%.tt' ; touch $@
478 * loops here ... */
479 _set_attributes( _sv_attr, _sv_setdir, ep );
480 ep->ce_flag |= (F_TARGET|flag);
482 if( _sv_rules != NIL(STRING) ) {
483 ep->ce_recipe = _sv_rules;
484 ep->ce_indprq = _sv_ind_prq;
488 else {
489 tg->ce_attr |= _sv_attr;
490 tg->ce_flag |= flag;
492 if( _sv_rules != NIL(STRING) ) {
493 tg->ce_recipe = _sv_rules;
494 tg->ce_flag |= F_RULES | F_TARGET;
496 /* Bind the current set of prerequisites as belonging to the
497 * original recipe given for the target */
498 for( lp=tg->ce_prq; lp != NIL(LINK); lp = lp->cl_next )
499 if( !(lp->cl_flag & F_VISITED) ) lp->cl_flag |= F_TARGET;
501 else for( lp=tg->ce_prq; lp != NIL(LINK); lp = lp->cl_next )
502 lp->cl_flag |= F_VISITED;
505 tflag |= _add_root(tg);
508 if( tflag ) Target = TRUE;
509 if( _sv_setdir ) FREE(_sv_setdir);
510 _sv_rules = NIL(STRING);
511 _sv_crule = NIL(STRING);
512 _sv_targets = NIL(CELL);
513 _sv_ind_prq = NIL(LINK);
514 _sv_edgel = NIL(CELL);
515 _sp_target = FALSE;
516 _sv_globprq_only = 0;
518 DB_VOID_RETURN;
523 PUBLIC int
524 Set_group_attributes( list )/*
525 ==============================
526 Scan list looking for the standard @,-,% and + (as in recipe line
527 defs) (+ is set but ignored for group recipes)
528 and set the flags accordingly so that they apply when we bind the
529 rules to the appropriate targets.
530 Return TRUE if group recipe start '[' was found, otherwise FALSE. */
531 char *list;
533 int res = FALSE;
534 char *s;
536 if ( !((_sv_attr|Glob_attr)&A_IGNOREGROUP) ) {
537 s = DmStrSpn(list,"@-%+ \t");
538 res = (*s == '[');
539 if( res ) {
540 /* Check for non-white space characters after the [. */
541 for( s++; *s && iswhite(*s) ; s++ )
543 if( *s )
544 Warning("Found non-white space character after '[' in [%s].", list);
546 _sv_attr |= Rcp_attribute(list);
550 return(res);
554 static void
555 _do_special( special, op, attr, set_dir, target, prereq, state )/*
556 ==================================================================
557 Process a special target (always only a single target). So far the only
558 special targets we have are those recognized by the _is_special function.
559 Some of the special targets can take recipes, they call _do_targets()
560 and (implicitly) set *state to to RULE_SCAN. Otherwise *state remains
561 unaffected, i.e. NORMAL_SCAN.
563 target is always only a single special target.
565 NOTE: For the cases of .IMPORT, and .INCLUDE, the cells created by the
566 parser are never freed. This is due to the fact that it is too much
567 trouble to get them out of the hash table once they are defined, and
568 if they are per chance used again it will be ok, anyway, since the
569 cell is not really used by the code below. */
571 int special;
572 int op;
573 t_attr attr;
574 char *set_dir;
575 CELLPTR target;
576 CELLPTR prereq;
577 int *state;
579 HASHPTR hp; /* pointer to macro def cell */
580 CELLPTR cp; /* temporary pointer into cells list */
581 CELLPTR dp; /* pointer to directory dir cell */
582 LINKPTR lp; /* pointer at prerequisite list */
583 char *dir; /* current dir to prepend */
584 char *path; /* resulting path to try to read */
585 char *name; /* File name for processing a .INCLUDE */
586 char *tmp; /* temporary string pointer */
587 FILE *fil; /* File descriptor returned by Openfile */
589 DB_ENTER( "_do_special" );
591 target->ce_flag = F_SPECIAL; /* mark the target as special */
593 switch( special ) {
594 case ST_EXPORT:
595 for( ; prereq != NIL(CELL); prereq = prereq->ce_link ) {
596 DB_PRINT( "par", ("Exporting [%s]", prereq->CE_NAME) );
597 hp = GET_MACRO( prereq->CE_NAME );
599 if( hp != NIL(HASH) ) {
600 char *tmpstr = hp->ht_value;
602 if( tmpstr == NIL(char) ) tmpstr = "";
604 if( Write_env_string( prereq->CE_NAME, tmpstr ) != 0 )
605 Warning( "Could not export %s", prereq->CE_NAME );
608 break;
610 /* Simply cause the parser to fail on the next input read */
611 case ST_EXIT:
612 Skip_to_eof = TRUE;
613 break;
615 case ST_IMPORT:
616 for( ; prereq != NIL(CELL); prereq = prereq->ce_link ) {
617 char *tmpstr;
619 DB_PRINT( "par", ("Importing [%s]", prereq->CE_NAME) );
621 if( strcmp(prereq->CE_NAME, ".EVERYTHING") == 0 ) {
622 t_attr sattr = Glob_attr;
623 Glob_attr |= A_SILENT;
625 ReadEnvironment();
627 Glob_attr = sattr;
629 else {
630 tmpstr = Read_env_string( prereq->CE_NAME );
632 if( tmpstr != NIL(char) )
633 Def_macro(prereq->CE_NAME, tmpstr, M_EXPANDED|M_LITERAL);
634 else
635 if( !((Glob_attr | attr) & A_IGNORE) )
636 Fatal("Imported macro `%s' not found",prereq->CE_NAME);
640 attr &= ~A_IGNORE;
641 break;
643 case ST_INCLUDE:
645 int pushed = FALSE;
646 int first = (attr & A_FIRST);
647 int ignore = (((Glob_attr | attr) & A_IGNORE) != 0);
648 int found = FALSE;
649 int noinf = (attr & A_NOINFER);
650 LINKPTR prqlnk = NIL(LINK);
651 LINKPTR prqlst = NIL(LINK);
653 if( prereq == NIL(CELL) ) Fatal( "No .INCLUDE file(s) specified" );
655 dp = Def_cell( ".INCLUDEDIRS" );
657 if( (attr & A_SETDIR) && *(dir = strchr(set_dir, '=')+1) )
658 pushed = Push_dir( dir, ".INCLUDE", ignore );
660 for( cp=prereq; cp != NIL(CELL); cp = cp->ce_link ) {
661 LINKPTR ltmp;
662 TALLOC(ltmp, 1, LINK);
663 ltmp->cl_prq = cp;
665 if( prqlnk == NIL(LINK) )
666 prqlst = ltmp;
667 else
668 prqlnk->cl_next = ltmp;
670 prqlnk = ltmp;
673 for( ; prqlst != NIL(LINK); FREE(prqlst), prqlst=prqlnk ) {
674 prqlnk = prqlst->cl_next;
675 cp = prqlst->cl_prq;
676 name = cp->CE_NAME;
678 /* Leave this here, it ensures that prqlst gets propely free'd */
679 if ( first && found )
680 continue;
682 if( *name == '<' ) {
683 /* We have a file name enclosed in <....>
684 * so get rid of the <> arround the file name */
686 name++;
687 if( (tmp = strrchr( name, '>' )) != NIL( char ) )
688 *tmp = 0;
690 if( If_root_path( name ) )
691 fil = Openfile( name, FALSE, FALSE );
692 else
693 fil = NIL(FILE);
695 else
696 fil = Openfile( name, FALSE, FALSE );
698 if( fil == NIL(FILE) && !If_root_path( name ) ) { /*if true ==> not found in current dir*/
700 /* Now we must scan the list of prerequisites for .INCLUDEDIRS
701 * looking for the file in each of the specified directories.
702 * if we don't find it then we issue an error. The error
703 * message is suppressed if the .IGNORE attribute of attr is
704 * set. If a file is found we call Parse on the file to
705 * perform the parse and then continue on from where we left
706 * off. */
708 for(lp=dp->CE_PRQ; lp && fil == NIL(FILE); lp=lp->cl_next) {
709 dir = lp->cl_prq->CE_NAME;
710 if( strchr(dir, '$') ) dir = Expand(dir);
711 path = Build_path( dir, name );
713 DB_PRINT( "par", ("Trying to include [%s]", path) );
715 fil = Openfile( path, FALSE, FALSE );
716 if( dir != lp->cl_prq->CE_NAME ) FREE(dir);
720 if (!noinf && fil == NIL(FILE)) {
721 t_attr glob = Glob_attr;
722 t_attr cattr = prqlst->cl_prq->ce_attr;
724 prqlst->cl_next = NIL(LINK);
725 Glob_attr |= (attr&A_IGNORE);
726 prqlst->cl_prq->ce_attr &= ~A_FRINGE;
728 if( Verbose & V_FILE_IO )
729 printf( "%s: Inferring include file [%s].\n",
730 Pname, name );
731 fil = TryFiles(prqlst);
733 Glob_attr = glob;
734 prqlst->cl_prq->ce_attr |= (cattr & A_FRINGE);
737 if( fil != NIL(FILE) ) {
738 if( Verbose & V_FILE_IO )
739 printf( "%s: Parsing include file [%s].\n",
740 Pname, name );
741 Parse( fil );
742 found = TRUE;
744 else if( !(ignore || first) )
745 Fatal( "Include file %s, not found", name );
746 else if( Verbose & V_FILE_IO )
747 printf( "%s: Include file [%s] was not found.\n",
748 Pname, name );
751 if ( !ignore && first && !found )
752 Fatal( "No include file was found" );
754 if( pushed ) Pop_dir(FALSE);
755 attr &= ~(A_IGNORE|A_SETDIR|A_FIRST|A_NOINFER);
757 break;
759 case ST_SOURCE:
760 if( prereq != NIL(CELL) )
761 _do_targets( op & (R_OP_CL | R_OP_MI | R_OP_UP), attr, set_dir,
762 target, prereq );
763 else {
764 /* The old semantics of .SOURCE were that an empty list of
765 * prerequisites clears the .SOURCE list. So we must implement
766 * that here as a clearout prerequisite operation. Since this is
767 * a standard operation with the :- opcode we can simply call the
768 * proper routine with the target cell and it should do the trick
771 if( op == R_OP_CL || (op & R_OP_MI) )
772 Clear_prerequisites( target );
775 op &= ~(R_OP_MI | R_OP_UP);
776 break;
778 case ST_KEEP:
779 if( Keep_state != NIL(char) ) break;
780 Def_macro( ".KEEP_STATE", "_state.mk", M_EXPANDED );
781 break;
783 case ST_REST:
784 /* The rest of the special targets can all take recipes, as such they
785 * must be able to affect the state of the parser. */
788 int s_targ = Target;
790 Target = TRUE;
791 _sp_target = TRUE;
792 *state = _do_targets( op, attr, set_dir, target, prereq );
793 Target = s_targ;
795 target->ce_flag |= F_TARGET;
797 attr = A_DEFAULT;
798 op = R_OP_CL;
800 break;
802 default:break;
805 if( op != R_OP_CL ) Warning( "Modifier(s) for operator ignored" );
806 if( attr != A_DEFAULT ) Warning( "Extra attributes ignored" );
808 DB_VOID_RETURN;
813 static int
814 _do_targets( op, attr, set_dir, targets, prereq )/*
815 ===================================================
816 Evaluate the values derived from the current target definition
817 line. Helper functions _build_graph(), _do_magic(), _make_multi(),
818 _add_root(), _replace_cell(), _set_attributes(), Clear_prerequisites()
819 _stick_at_head(), Add_prerequisite() and _set_global_attr() are used.
820 If successfull "_sv_targets" is set to "targets".
821 Return RULE_SCAN if a recipe is expected to follow, otherwise
822 NORMAL_SCAN. */
823 int op; /* rule operator */
824 t_attr attr; /* attribute flags for current targets */
825 char *set_dir; /* value of setdir attribute */
826 CELLPTR targets; /* list of targets (each cell maybe already
827 * defined by a previous target definition
828 * line. */
829 CELLPTR prereq; /* list of prerequisites */
831 CELLPTR tg1; /* temporary target pointer */
832 CELLPTR tp1; /* temporary prerequisite pointer */
833 LINKPTR prev_cell; /* pointer for .UPDATEALL processing */
834 char *p; /* temporary char pointer */
835 int tflag = FALSE; /* set to TRUE if we add target to root */
836 int ret_state = RULE_SCAN; /* Return state */
838 DB_ENTER( "_do_targets" );
840 /* If .UPDATEALL is set sort the target list that was temporary linked
841 * with ce_link into a list using ce_link with ce_set pointing to the first
842 * element. */
843 /* FIXME: Check that .UPDATEALL and %-targets on one line work together. */
844 if( attr & A_UPDATEALL ) {
845 if( targets == NIL(CELL) )
846 Fatal( ".UPDATEALL attribute requires non-empty list of targets" );
848 if (targets->ce_set == NIL(CELL)) {
849 for(
850 prev_cell=CeMeToo(targets),tg1=targets->ce_link;
851 tg1 != NIL(CELL);
852 tg1=tg1->ce_link
854 if (tg1->ce_set)
855 Fatal( "Target [%s] appears on multiple .UPDATEALL lists",
856 tg1->CE_NAME);
857 tg1->ce_set = targets;
858 TALLOC(prev_cell->cl_next, 1, LINK);
859 prev_cell = prev_cell->cl_next;
860 prev_cell->cl_prq = tg1;
862 targets->ce_set = targets;
864 else {
865 LINKPTR ap;
866 CELLPTR tp;
868 tp = targets;
869 ap = CeMeToo(targets);
870 while (ap && tp && ap->cl_prq == tp && tp->ce_set == targets) {
871 ap = ap->cl_next;
872 tp = tp->ce_link;
874 if (ap || tp)
875 Fatal("Inconsistent .UPDATEALL lists for target [%s]",
876 targets->CE_NAME);
878 targets->ce_link = NIL(CELL);
881 for( tg1 = targets; tg1 != NIL(CELL); tg1 = tg1->ce_link ) {
882 /* Check if tg1 is already marked as a %-target, but not a magic
883 * (.xxx.yyy) target. */
884 int purepercent = (tg1->ce_flag & F_PERCENT) && !(tg1->ce_flag & F_MAGIC);
886 /* Check each target. Check for inconsistencies between :: and : rule
887 * sets. :: may follow either : or :: but not the reverse.
889 * Any F_MULTI target (contains :: rules) is represented by a prerequisite
890 * list hanging off the main target cell where each of the prerequisites
891 * is a copy of the target cell but is not entered into the hash table.
893 if( !(op & R_OP_DCL ) && (tg1->ce_flag & F_MULTI) && !purepercent )
894 Fatal( "':' vs '::' inconsistency in rules for %s", tg1->CE_NAME );
896 if( purepercent ) {
897 /* Handle %-targets. */
898 CELLPTR cur;
899 CELLPTR tpq = NIL(CELL);
900 CELLPTR nprq = NULL;
902 #ifdef DBUG
903 DB_PRINT( "%", ("Handling %%-target [%s : : <prerequisites follow, maybe empty>]",
904 tg1->CE_NAME) );
905 for(cur=prereq;cur;cur=cur->ce_link) {
906 DB_PRINT( "%", (" %%-prerequisites : %s ",
907 cur->CE_NAME ? cur->CE_NAME : "<empty>") );
909 #endif
911 /* Handle indirect (global) prerequisites first. */
912 for(cur=prereq;cur;cur=cur->ce_link) {
913 char *name = cur->CE_NAME;
914 int len = strlen(name);
916 if( *name == '\'' && name[len-1]=='\'' ){
917 name[len-1] = '\0';
918 len = strlen(name+1)+1;
919 memmove(name,name+1,len);
920 /* add indirect prerequisite */
921 _add_indirect_prereq( cur );
923 else {
924 /* Sort all "other" prerequisits into tpq, with nprq
925 * pointing to the first element. */
926 if (tpq)
927 tpq->ce_link = cur;
928 else
929 nprq = cur;
930 tpq = cur;
933 /* Mark the last element of nprq. */
934 if(tpq)
935 tpq->ce_link=NIL(CELL);
936 else
937 nprq = NIL(CELL);
939 /* Handle "normal" prerequisites now. */
941 if ( op & R_OP_OR ) {
942 /* for op == ':|' transform:
943 * <%-target> :| <prereq_1> ... <prereq_n> ; <recipe>
944 * into:
945 * <%-target> : <prereq_1> ; <recipe>
946 * ..
947 * <%-target> : <prereq_n> ; <recipe>
949 for(tp1=nprq; tp1; tp1=tp1->ce_link) {
950 CELLPTR tmpcell = tp1->ce_link;
951 tp1->ce_link = NIL(CELL);
952 _build_graph(op,tg1,tp1);
953 tp1->ce_link = tmpcell;
956 else {
957 /* The inference mechanism for %-targets limits the number of
958 * (non-indirect) prerequisite to one, but an unlimited number
959 * of indirect prerequisites is possible. */
960 if ( nprq && nprq->ce_link && !(op & R_OP_OR))
961 Warning("More than one prerequisite\n"
962 "for %%-target. Use :| ruleop or indirect prerequisites.");
964 _build_graph(op,tg1,nprq);
967 else if( tg1->ce_flag & F_MAGIC &&
968 (p = _is_magic( tg1->CE_NAME )) != NIL(char) &&
969 _do_magic( op, p, tg1, prereq, attr, set_dir ) )
970 ; /* _do_magic() does all that is needed (if return value is TRUE). */
971 else if( op & R_OP_DCL ) { /* op == :: */
972 CELLPTR tmp_cell = _make_multi(tg1);
974 /* Add the F_MULTI master to .TARGETS (If not set already).
975 * Do this here so that the member cell is not added instead
976 * when the recipies are bound in Bind_rules_to_targets(). */
977 tflag |= _add_root(tg1);
979 /* Replace the F_MULTI master with the member cell. */
980 targets = _replace_cell( targets, tg1, tmp_cell );
982 /* We have to set (add) the attributes also for the F_MULTI master
983 * target cell. As there is no recipe the setdir value is not
984 * needed. _set_attributes() that follows in approx. 8 lines
985 * will set the attributes for the F_MULTI member cell. */
986 tg1->ce_attr |= (attr & ~A_SETDIR);
988 /* Now switch tg1 to the current (F_MULTI prereq.) target.
989 * All recipes have to be added to that cell and not to the
990 * F_MULTI master. */
991 tg1 = tmp_cell;
994 if( !purepercent ) _set_attributes( attr, set_dir, tg1 );
996 /* Build the proper prerequisite list of the target. If the `-',
997 * modifier was used clear the prerequisite list before adding any
998 * new prerequisites. Else add them to the head/tail as appropriate.
1000 * If the target has F_PERCENT set then no prerequisites are used. */
1002 if( !(tg1->ce_flag & F_PERCENT) ) {
1003 if( op & R_OP_MI ) Clear_prerequisites( tg1 ); /* op == :- */
1005 if( (op & R_OP_UP) && (tg1->ce_prq != NIL(LINK)) ) /* op == :^ */
1006 _stick_at_head( tg1, prereq );
1007 else for( tp1=prereq; tp1 != NIL(CELL); tp1 = tp1->ce_link )
1008 Add_prerequisite( tg1, tp1, FALSE, FALSE );
1010 else if( op & (R_OP_MI | R_OP_UP) )
1011 Warning( "Modifier(s) `^-' for %-meta target ignored" );
1014 /* In case a F_MULTI member that was the first prerequisite of .TARGETS */
1015 if(tflag)
1016 Target = TRUE;
1018 /* Check to see if we have NO targets but some attributes, i.e. an
1019 * Attribute-Definition. If so then apply all of the attributes to the
1020 * complete list of prerequisites. No recipes are allowed to follow. */
1022 if( (targets == NIL(CELL)) && attr ) {
1023 ret_state = NORMAL_SCAN;
1024 if( prereq != NIL(CELL) )
1025 for( tp1=prereq; tp1 != NIL(CELL); tp1 = tp1->ce_link )
1026 _set_attributes( attr, set_dir, tp1 );
1027 else
1028 _set_global_attr( attr );
1031 /* Now that we have built the lists of targets, the parser must parse the
1032 * recipes if there are any. However we must start the recipe list with the
1033 * recipe specified as via the ; kludge, if there is one */
1034 _sv_targets = targets;
1035 _sv_attr = attr;
1036 _sv_flag = ((op & R_OP_BG) ? F_SINGLE : F_DEFAULT);
1038 DB_RETURN( ret_state );
1042 static int
1043 _do_magic( op, dot, target, prereq, attr, set_dir )/*
1044 =====================================================
1045 This function investigates dot for being a magic target of the form
1046 .<chars>.<chars> or .<chars> and creates the appropriate % rules for
1047 that target.
1048 If the target is given with an undefined syntax, i.e. with prerequisites,
1049 then this function terminates early without creating % rules and
1050 returns 0.
1051 If successful the function returns 1.
1053 The function builds the % rule, `%.o : %.c' from .c.o, and
1054 `% : %.a' from .a */
1056 int op;
1057 char *dot;
1058 CELLPTR target;
1059 CELLPTR prereq;
1060 t_attr attr;
1061 char *set_dir;
1063 CELLPTR tg;
1064 CELLPTR prq;
1065 char *tmp, *tmp2;
1067 DB_ENTER( "_do_magic" );
1069 DB_PRINT("%", ("Analysing magic target [%s]", target->CE_NAME));
1071 if( prereq != NIL(CELL) ) {
1072 Warning( "Ignoring AUGMAKE meta-target [%s] because prerequisites are present.", target->CE_NAME );
1073 DB_RETURN(0);
1076 if( dot == target->CE_NAME ) { /* its of the form .a */
1077 tg = Def_cell( "%" );
1078 tmp = _build_meta( target->CE_NAME );
1079 prq = Def_cell( tmp );
1080 FREE( tmp );
1082 _build_graph( op, tg, prq );
1084 else {
1085 tmp = _build_meta( dot );
1086 tg = Def_cell( tmp );
1087 FREE( tmp );
1089 tmp = _build_meta( tmp2 = DmSubStr( target->CE_NAME, dot ) );
1090 prq = Def_cell( tmp );
1091 FREE( tmp );
1092 FREE( tmp2 );
1094 _build_graph( op, tg, prq );
1097 tg->ce_flag |= F_PERCENT;
1098 target->ce_flag |= (F_MAGIC|F_PERCENT);
1100 _set_attributes( attr, set_dir, tg );
1102 DB_RETURN(1);
1106 static CELLPTR
1107 _replace_cell( lst, cell, rep )/*
1108 =================================
1109 Replace cell with rep in lst. Note if cell is not part of lst we are in
1110 real trouble. */
1111 CELLPTR lst;
1112 CELLPTR cell;
1113 CELLPTR rep;
1115 register CELLPTR tp;
1117 if( lst == cell ) {
1118 rep->ce_link = lst->ce_link;
1119 lst = rep;
1121 else {
1122 for( tp=lst; tp->ce_link != cell && tp ; tp=tp->ce_link );
1123 if( !tp )
1124 Fatal( "Internal Error: cell not part of lst." );
1125 rep->ce_link = tp->ce_link->ce_link;
1126 tp->ce_link = rep;
1129 return(lst);
1133 static char *
1134 _build_meta( name )/*
1135 =====================
1136 Check to see if the name is of the form .c~ if so and if Augmake
1137 translation is enabled then return s.%.c, else return %.suff, where if the
1138 suffix ends in '~' then leave it be.*/
1139 char *name;
1141 char *tmp;
1142 int test = (STOBOOL(Augmake) ? name[strlen(name)-1] == '~' : 0);
1144 tmp = DmStrJoin( test ? "s.%" : "%", name, -1, FALSE);
1145 if( test ) tmp[ strlen(tmp)-1 ] = '\0';
1147 return(tmp);
1151 static CELLPTR
1152 _build_graph( op, target, prereq )/*
1153 ====================================
1154 This function is called to build the graph for the % rule given by
1155 target : prereq cell combination. This function assumes that target
1156 is a % target and that prereq is one or multiple non-indirect prerequisite.
1157 It also assumes that target cell has F_PERCENT set already.
1159 NOTE: If more than one prerequisite is present this function handles them
1160 correctly but the lookup still only uses the first (BUG!).
1162 R_OP_CL (:) rules replace existing rules if any, %.o :: %.c is meaningless.
1164 The function always returns NIL(CELL). */
1165 int op;
1166 CELLPTR target;
1167 CELLPTR prereq;
1169 LINKPTR edl;
1170 CELLPTR edge = 0;
1171 CELLPTR tpq,cur;
1172 int match;
1174 #ifdef DBUG
1175 DB_ENTER( "_build_graph" );
1176 DB_PRINT( "%", ("Building graph for [%s : <prerequisites follow, maybe empty>]",
1177 target->CE_NAME) );
1178 for(tpq=prereq;tpq;tpq=tpq->ce_link) {
1179 DB_PRINT( "%", (" %%-prerequisites : %s ",
1180 tpq->CE_NAME ? tpq->CE_NAME : "<empty>") );
1182 #endif
1184 /* Currently multiple prerequisites are not (yet) handled correctly.
1185 * We already issue a warning in _do_targets(), don't issue it here
1186 * again.
1187 if ( prereq && prereq->ce_link )
1188 Warning( "Internal Error: more than one prerequisite in _build_graph." );
1191 /* There cannot be more than one target name ( linked with
1192 * (CeMeToo(target))->cl_next ) per %-target master.
1193 * FIXME: remove this check after verifying that it never triggers. */
1194 if ( (CeMeToo(target))->cl_next )
1195 Fatal( "Internal Error: more than one target name in _build_graph." );
1197 /* Search the list of prerequisites for the current target and see if
1198 * any of them match the current %-meta's : prereq's pair. NOTE that
1199 * %-metas are built as if they were F_MULTI targets, i.e. the target
1200 * definitions for the %-target members are stored in the prerequisites
1201 * list of the master target. */
1202 /* This relies on target->ce_prq being NULL if this is the first
1203 * occurence of this %-target and therefore not yet having a %-target
1204 * master. */
1205 match = FALSE;
1206 for(edl=target->ce_prq; !match && edl != NIL(LINK); edl=edl->cl_next) {
1207 LINKPTR l1,l2;
1208 edge = edl->cl_prq;
1210 DB_PRINT("%", ("Trying to match [%s]",edge?edge->CE_NAME:"(nil)"));
1212 /* First we match the target sets, if this fails then we don't have to
1213 * bother with the prerequisite sets. The targets sets are sorted.
1214 * this makes life very simple. */
1215 /* ce_dir is handled per member target, no check needed for the
1216 * master target. */
1218 /* FIXME: We already checked above that there is only one target
1219 * name. Remove the comparisons for following names. */
1220 l1 = CeMeToo(target); /* Used by .UPDATEALL !!! */
1221 l2 = CeMeToo(edge);
1222 while(l1 && l2 && l1->cl_prq == l2->cl_prq) {
1223 l1=l1->cl_next;
1224 l2=l2->cl_next;
1226 /* If both l1 and l2 are NULL we had a match. */
1227 if (l1 || l2)
1228 continue;
1230 /* target sets match, so check prerequisites. */
1231 if( (!edge->ce_prq && !prereq) /* matches both empty - separate this. */
1232 || ( edge->ce_prq
1233 && ( edge->ce_dir == _sv_setdir
1234 || ( edge->ce_dir
1235 && _sv_setdir
1236 && !strcmp(edge->ce_dir,strchr(_sv_setdir,'=')+1)
1241 LINKPTR prql;
1243 /* this is a really gross way to compare two sets, it's n^2 but
1244 * since the sets are assumed to always be tiny, it should be ok. */
1245 for(tpq=prereq; tpq; tpq=tpq->ce_link) {
1246 for(prql=edge->ce_prq;prql;prql=prql->cl_next)
1247 if (prql->cl_prq == tpq)
1248 break;
1250 if(prql == NIL(LINK))
1251 break;
1253 prql->cl_prq->ce_flag |= F_MARK;
1256 if (tpq == NIL(CELL)) {
1257 for(prql=edge->ce_prq;prql;prql=prql->cl_next)
1258 if(!(prql->cl_prq->ce_flag & F_MARK))
1259 break;
1261 if(prql == NIL(LINK))
1262 match = TRUE;
1265 /* clean up the mark bits. */
1266 for(prql=edge->ce_prq;prql;prql=prql->cl_next)
1267 prql->cl_prq->ce_flag &= ~F_MARK;
1271 if( match ) {
1272 /* match is TRUE hence, we found an edge joining the target and the
1273 * prerequisite so reset the new edge so that new values replace it. */
1274 DB_PRINT( "%", ("It's an old edge") );
1276 edge->ce_dir = NIL(char);
1277 edge->ce_flag &= (F_PERCENT|F_MAGIC|F_DFA);
1278 edge->ce_attr &= A_NOINFER;
1280 else {
1281 DB_PRINT( "%", ("Adding a new edge") );
1283 edge = _make_multi(target);
1285 /* FIXME: There can be only one %-target. */
1286 for(edl=CeMeToo(target);edl;edl=edl->cl_next) {
1287 if( !((tpq=edl->cl_prq)->ce_flag & F_DFA) ) {
1288 Add_nfa( tpq->CE_NAME );
1289 tpq->ce_flag |= F_DFA;
1292 edl->cl_prq->ce_set = edge;
1295 edge->ce_all = target->ce_all;
1296 target->ce_all.cl_next = NIL(LINK);
1297 target->ce_set = NIL(CELL);
1299 /* Add all prerequisites to edge. */
1300 for(tpq=prereq; tpq; tpq=tpq->ce_link)
1301 Add_prerequisite(edge, tpq, FALSE, TRUE);
1304 if( op & R_OP_DCL )
1305 Warning("'::' operator for meta-target '%s' ignored, ':' operator assumed.",
1306 target->CE_NAME );
1308 /* If edge was already added we're in BIG trouble. */
1309 /* Re-use cur as temporary variable. */
1310 for( cur=_sv_edgel; cur != NIL(CELL); cur=cur->ce_link ) {
1311 if( cur == edge )
1312 Fatal( "Internal Error: edge already in _sv_edgel." );
1315 edge->ce_link = _sv_edgel;
1316 _sv_edgel = edge;
1317 _sv_globprq_only = 0;
1319 DB_RETURN(NIL(CELL));
1323 static CELLPTR
1324 _make_multi( tg )/*
1325 ===================
1326 This function is called to convert tg into an F_MULTI target.
1327 Return a pointer to the new member cell.
1328 I don't know what the author intended but the ce_index entry is only
1329 used in this function (set to 0 for added targets) and undefined otherwise!
1330 The undefined value is hopefully set to 0 by the C compiler as each added
1331 target sets its ce_count to ++ce_index (==1). (FIXME) */
1332 CELLPTR tg;
1334 CELLPTR cp;
1336 /* This creates a new master F_MULTI target if tg existed before as a normal
1337 * target with prerequisites or recipes. */
1338 if( !(tg->ce_flag & F_MULTI) && (tg->ce_prq || tg->ce_recipe) ) {
1339 /* Allocate a new master cell. */
1340 TALLOC(cp, 1, CELL);
1341 *cp = *tg;
1343 /* F_MULTI master */
1344 tg->ce_prq = NIL(LINK);
1345 tg->ce_flag |= F_RULES|F_MULTI|F_TARGET;
1346 tg->ce_attr |= A_SEQ;
1347 tg->ce_recipe = NIL(STRING);
1348 tg->ce_dir = NIL(char);
1350 /* F_MULTI member for preexisting elements */
1351 cp->ce_count = ++tg->ce_index;
1352 cp->ce_cond = NIL(STRING);
1353 cp->ce_set = NIL(CELL);
1354 cp->ce_all.cl_prq = cp;
1355 CeNotMe(cp) = NIL(LINK);
1357 Add_prerequisite(tg, cp, FALSE, TRUE);
1360 /* Alocate memory for new member of F_MULTI target */
1361 TALLOC(cp, 1, CELL);
1362 *cp = *tg;
1364 /* This is reached if the target already exists, but without having
1365 * prerequisites or recepies. Morph it into a F_MULTI master cell. */
1366 if( !(tg->ce_flag & F_MULTI) ) {
1367 tg->ce_prq = NIL(LINK);
1368 tg->ce_flag |= F_RULES|F_MULTI|F_TARGET;
1369 tg->ce_attr |= A_SEQ;
1370 tg->ce_recipe = NIL(STRING);
1371 tg->ce_dir = NIL(char);
1372 cp->ce_cond = NIL(STRING);
1374 /* This handles the case of adding an additional target as a
1375 * prerequisite to a F_MULTI target. */
1376 else {
1377 cp->ce_flag &= ~(F_RULES|F_MULTI);
1378 cp->ce_attr &= ~A_SEQ;
1379 cp->ce_prq = NIL(LINK);
1380 cp->ce_index = 0;
1381 cp->ce_cond = NIL(STRING);
1383 cp->ce_count = ++tg->ce_index;
1384 cp->ce_flag |= F_TARGET;
1385 cp->ce_set = NIL(CELL);
1386 cp->ce_all.cl_prq = cp;
1387 CeNotMe(cp) = NIL(LINK);
1389 Add_prerequisite(tg, cp, FALSE, TRUE);
1390 return(cp);
1394 static void
1395 _add_indirect_prereq( pq )/*
1396 ==========================
1397 Prerequisite is an indirect prerequisite for a %-target, add it to
1398 the target's list of indirect prerequsites to add on match. */
1399 CELLPTR pq;
1401 register LINKPTR ln;
1403 /* Only add to list of indirect prerequsites if it is not in already. */
1404 for(ln=_sv_ind_prq; ln; ln=ln->cl_next)
1405 if(strcmp(ln->cl_prq->CE_NAME,pq->CE_NAME) == 0)
1406 return;
1408 /* Not in, add it. */
1409 TALLOC( ln, 1, LINK );
1410 ln->cl_next = _sv_ind_prq;
1411 ln->cl_prq = pq;
1412 _sv_ind_prq = ln;
1417 static void
1418 _set_attributes( attr, set_dir, cp )/*
1419 ======================================
1420 Set the appropriate attributes for a cell */
1421 t_attr attr;
1422 char *set_dir;
1423 CELLPTR cp;
1425 char *dir = 0;
1427 DB_ENTER( "_set_attributes" );
1429 /* If .SETDIR attribute is set then we have at least .SETDIR= in the
1430 * set_dir string. So go and fishout what is at the end of the =.
1431 * If not set and not NULL then propagate it to the target cell. */
1433 if( attr & A_SETDIR ) {
1434 char *p;
1435 if( (p = strchr( set_dir, '=' )) != NULL )
1436 dir = p + 1;
1438 if( cp->ce_dir )
1439 Warning( "Multiple .SETDIR for %s ignored", cp->CE_NAME );
1440 else if( *dir )
1441 cp->ce_dir = DmStrDup(dir);
1443 cp->ce_attr |= attr; /* set rest of attributes for target */
1445 DB_VOID_RETURN;
1450 static void
1451 _set_global_attr( attr )/*
1452 ==========================
1453 Handle the setting of the global attribute functions based on
1454 The attribute flags set in attr. */
1455 t_attr attr;
1457 t_attr flag;
1459 /* Some compilers can't handle a switch on a long, and t_attr is now a long
1460 * integer on some systems. foey! */
1461 for( flag = MAX_ATTR; flag; flag >>= 1 )
1462 if( flag & attr ) {
1463 if( flag == A_PRECIOUS) Def_macro(".PRECIOUS", "y", M_EXPANDED);
1464 else if( flag == A_SILENT) Def_macro(".SILENT", "y", M_EXPANDED);
1465 else if( flag == A_IGNORE ) Def_macro(".IGNORE", "y", M_EXPANDED);
1466 else if( flag == A_EPILOG ) Def_macro(".EPILOG", "y", M_EXPANDED);
1467 else if( flag == A_PROLOG ) Def_macro(".PROLOG", "y", M_EXPANDED);
1468 else if( flag == A_NOINFER ) Def_macro(".NOINFER", "y", M_EXPANDED);
1469 else if( flag == A_SEQ ) Def_macro(".SEQUENTIAL","y", M_EXPANDED);
1470 else if( flag == A_SHELL ) Def_macro(".USESHELL", "y", M_EXPANDED);
1471 else if( flag == A_MKSARGS ) Def_macro(".MKSARGS", "y", M_EXPANDED);
1472 #if !defined(__CYGWIN__)
1473 else if( flag == A_SWAP ) Def_macro(".SWAP", "y", M_EXPANDED);
1474 #else
1475 else if( flag == A_WINPATH ) Def_macro(".WINPATH", "y", M_EXPANDED);
1476 #endif
1479 attr &= ~A_GLOB;
1480 if( attr ) Warning( "Non global attribute(s) ignored" );
1485 static void
1486 _stick_at_head( cp, pq )/*
1487 ==========================
1488 Add the prerequisite list to the head of the existing prerequisite
1489 list */
1491 CELLPTR cp; /* cell for target node */
1492 CELLPTR pq; /* list of prerequisites to add */
1494 DB_ENTER( "_stick_at_head" );
1496 if( pq->ce_link != NIL(CELL) ) _stick_at_head( cp, pq->ce_link );
1497 Add_prerequisite( cp, pq, TRUE, FALSE );
1499 DB_VOID_RETURN;
1504 static t_attr
1505 _is_attribute( name )/*
1506 =======================
1507 Check the passed name against the list of valid attributes and return the
1508 attribute index if it is, else return 0, indicating the name is not a valid
1509 attribute. The present attributes are defined in dmake.h as A_xxx #defines,
1510 with the corresponding makefile specification: (note they must be named
1511 exactly as defined below)
1513 Valid attributes are: .IGNORE, .SETDIR=, .SILENT, .PRECIOUS, .LIBRARY,
1514 .EPILOG, .PROLOG, .LIBRARYM, .SYMBOL, .UPDATEALL,
1515 .USESHELL, .NOINFER, .PHONY, .SWAP/.WINPATH, .SEQUENTIAL
1516 .NOSTATE, .MKSARGS, .IGNOREGROUP, .GROUP, .FIRST
1517 .EXECUTE, .ERRREMOVE
1519 NOTE: The strcmp's are OK since at most three are ever executed for any
1520 one attribute check, and that happens only when we can be fairly
1521 certain we have an attribute. */
1522 char *name;
1524 t_attr attr = 0;
1526 DB_ENTER( "_is_attribute" );
1528 if( *name++ == '.' )
1529 switch( *name )
1531 case 'E':
1532 if( !strcmp(name, "EPILOG") ) attr = A_EPILOG;
1533 else if( !strcmp(name, "EXECUTE")) attr = A_EXECUTE;
1534 else if( !strcmp(name, "ERRREMOVE")) attr = A_ERRREMOVE;
1535 else attr = 0;
1536 break;
1538 /* A_FIRST implies A_IGNORE, handled in ST_INCLUDE */
1539 case 'F':
1540 attr = (strcmp(name, "FIRST")) ? 0 : A_FIRST;
1541 break;
1543 case 'G': attr = (strcmp(name, "GROUP")) ? 0 : A_GROUP; break;
1544 case 'L': attr = (strcmp(name, "LIBRARY")) ? 0 : A_LIBRARY; break;
1545 case 'M': attr = (strcmp(name, "MKSARGS")) ? 0 : A_MKSARGS; break;
1547 case 'I':
1548 if( !strcmp(name, "IGNORE") ) attr = A_IGNORE;
1549 else if( !strcmp(name, "IGNOREGROUP")) attr = A_IGNOREGROUP;
1550 else attr = 0;
1551 break;
1553 case 'N':
1554 if( !strcmp(name, "NOINFER") ) attr = A_NOINFER;
1555 else if( !strcmp(name, "NOSTATE")) attr = A_NOSTATE;
1556 else attr = 0;
1557 break;
1559 case 'U':
1560 if( !strcmp(name, "UPDATEALL") ) attr = A_UPDATEALL;
1561 else if( !strcmp(name, "USESHELL")) attr = A_SHELL;
1562 else attr = 0;
1563 break;
1565 case 'P':
1566 if( !strcmp(name, "PRECIOUS") ) attr = A_PRECIOUS;
1567 else if( !strcmp(name, "PROLOG") ) attr = A_PROLOG;
1568 else if( !strcmp(name, "PHONY") ) attr = A_PHONY;
1569 else attr = 0;
1570 break;
1572 case 'S':
1573 if( !strncmp(name, "SETDIR=", 7) ) attr = A_SETDIR;
1574 else if( !strcmp(name, "SILENT") ) attr = A_SILENT;
1575 else if( !strcmp(name, "SYMBOL") ) attr = A_SYMBOL;
1576 else if( !strcmp(name, "SEQUENTIAL")) attr = A_SEQ;
1577 /* A_SWAP has no meaning except for MSDOS. */
1578 else if( !strcmp(name, "SWAP")) attr = A_SWAP;
1579 else attr = 0;
1580 break;
1582 case 'W': attr = (strcmp(name, "WINPATH")) ? 0 : A_WINPATH; break;
1585 DB_RETURN( attr );
1590 static int
1591 _is_special( tg )/*
1592 ===================
1593 This function returns TRUE if the name passed in represents a special
1594 target, otherwise it returns false. A special target is one that has
1595 a special meaning to dmake, and may require processing at the time that
1596 it is parsed.
1598 Current Special targets are:
1599 .GROUPPROLOG .GROUPEPILOG .INCLUDE .IMPORT
1600 .EXPORT .SOURCE .SUFFIXES .ERROR .EXIT
1601 .INCLUDEDIRS .MAKEFILES .REMOVE .KEEP_STATE
1602 .TARGETS .ROOT
1604 char *tg;
1606 DB_ENTER( "_is_special" );
1608 if( *tg++ != '.' ) DB_RETURN( 0 );
1610 switch( *tg )
1612 case 'E':
1613 if( !strcmp( tg, "ERROR" ) ) DB_RETURN( ST_REST );
1614 else if( !strcmp( tg, "EXPORT" ) ) DB_RETURN( ST_EXPORT );
1615 else if( !strcmp( tg, "EXIT" ) ) DB_RETURN( ST_EXIT );
1616 break;
1618 case 'G':
1619 if( !strcmp( tg, "GROUPPROLOG" )) DB_RETURN( ST_REST );
1620 else if( !strcmp( tg, "GROUPEPILOG" )) DB_RETURN( ST_REST );
1621 break;
1623 case 'I':
1624 if( !strcmp( tg, "IMPORT" ) ) DB_RETURN( ST_IMPORT );
1625 else if( !strcmp( tg, "INCLUDE" ) ) DB_RETURN( ST_INCLUDE );
1626 else if( !strcmp( tg, "INCLUDEDIRS" )) DB_RETURN( ST_REST );
1627 break;
1629 case 'K':
1630 if( !strcmp( tg, "KEEP_STATE" ) ) DB_RETURN( ST_KEEP );
1631 break;
1633 case 'M':
1634 if( !strcmp( tg, "MAKEFILES" ) ) DB_RETURN( ST_REST );
1635 break;
1637 case 'R':
1638 if( !strcmp( tg, "REMOVE" ) ) DB_RETURN( ST_REST );
1639 else if( !strcmp( tg, "ROOT" ) ) DB_RETURN( ST_REST );
1640 break;
1642 case 'S':
1643 if( !strncmp( tg, "SOURCE", 6 ) ) DB_RETURN( ST_SOURCE );
1644 else if( !strncmp(tg, "SUFFIXES", 8 )) {
1645 if (Verbose & V_WARNALL)
1646 Warning( "The .SUFFIXES target has no special meaning and is deprecated." );
1647 DB_RETURN( ST_SOURCE );
1649 break;
1651 case 'T':
1652 if( !strcmp( tg, "TARGETS" ) ) DB_RETURN( ST_REST );
1653 break;
1656 DB_RETURN( 0 );
1661 static int
1662 _is_percent( np )/*
1663 ===================
1664 return TRUE if np points at a string containing a % sign */
1665 char *np;
1667 return( (strchr(np,'%') && (*np != '\'' && np[strlen(np)-1] != '\'')) ?
1668 TRUE : FALSE );
1672 static char *
1673 _is_magic( np )/*
1674 =================
1675 return NULL if np does not points at a string of the form
1676 .<chars>.<chars> or .<chars>
1677 where chars are "visible characters" for the current locale. If np is of the
1678 first form we return a pointer to the second '.' and for the second form we
1679 return a pointer to the '.'.
1681 NOTE: reject target if it contains / or begins with ..
1682 reject also .INIT and .DONE because they are mentioned in the
1683 man page. */
1684 char *np;
1686 register char *n;
1688 n = np;
1689 if( *n != '.' ) return( NIL(char) );
1690 if (strchr(DirBrkStr, *(n+1))!=NULL || *(n+1) == '.' )
1691 return (NIL(char));
1692 if( !strcmp( n+1, "INIT" ) || !strcmp( n+1, "DONE" ) )
1693 return (NIL(char));
1695 for( n++; isgraph(*n) && (*n != '.'); n++ );
1697 if( *n != '\0' ) {
1698 if( *n != '.' ) return( NIL(char) );
1699 for( np = n++; isgraph( *n ) && (*n != '.'); n++ );
1700 if( *n != '\0' ) return( NIL(char) );
1702 /* Until dmake 4.5 a .<suffix> target was ignored when AUGMAKE was
1703 * set and evaluated as a meta target if unset (also for -A).
1704 * To keep maximum compatibility accept this regardles of the AUGMAKE
1705 * status. */
1707 /* np points at the second . of .<chars>.<chars> string.
1708 * if the special target is of the form .<chars> then np points at the
1709 * first . in the token. */
1711 return( np );
1715 static int
1716 _add_root(tg)/*
1717 ===============
1718 Adds "tg" to the prerequisits list of "Targets" if "Target" is not TRUE,
1719 i.e. to the list of targets that are to be build.
1720 Instead io setting "Target" to TRUE, TRUE is returned as more targets
1721 might be defined in the current makefile line and they all have to be
1722 add to "Targets" in this case. */
1724 CELLPTR tg;
1726 int res = FALSE;
1728 if(tg == Targets)
1729 return(TRUE);
1731 if( !Target && !(tg->ce_flag & (F_SPECIAL|F_PERCENT)) ) {
1732 Add_prerequisite(Targets, tg, FALSE, TRUE);
1734 tg->ce_flag |= F_TARGET;
1735 tg->ce_attr |= A_FRINGE;
1736 res = TRUE;
1739 return(res);