4 -- Macro expansion code.
8 -- This routine handles all the necessary junk that deals with macro
9 -- expansion. It understands the following syntax. If a macro is
10 -- not defined it expands to NULL, and {} are synonyms for ().
15 -- $A - expands to whatever the macro A is defined as
16 -- $(AA) - expands to whatever the macro AA is defined as
17 -- $($(A)) - represents macro indirection
18 -- <+...+> - get mapped to $(mktmp ...)
20 -- following macro is recognized
22 -- string1{ token_list }string2
24 -- and expands to string1 prepended to each element of token_list and
25 -- string2 appended to each of the resulting tokens from the first
26 -- operation. If string2 is of the form above then the result is
27 -- the cross product of the specified (possibly modified) token_lists.
29 -- The folowing macro modifiers are defined and expanded:
31 -- $(macro:modifier_list:modifier_list:...)
33 -- where modifier_list a combination of:
35 -- D or d - Directory portion of token including separator
36 -- F or f - File portion of token including suffix
37 -- B or b - basename portion of token not including suffix
38 -- E or e - Suffix portion of name
39 -- L or l - translate to lower case
40 -- U or u - translate to upper case
41 -- I or i - return inferred names
42 -- N or n - return normalized paths
43 -- 1 - return the first white space separated token
45 -- or a single one of:
46 -- M or m - map escape codes
47 -- S or s - pattern substitution (simple)
48 -- T or t - for tokenization
49 -- ^ - prepend a prefix to each token
50 -- + - append a suffix to each token
52 -- NOTE: Modifiers are applied once the macro value has been found.
53 -- Thus the construct $($(test):s/joe/mary/) is defined and
54 -- modifies the value of $($(test))
56 -- Also the construct $(m:d:f) is not the same as $(m:df)
57 -- the first applies d to the value of $(m) and then
58 -- applies f to the value of that whereas the second form
59 -- applies df to the value of $(m).
62 -- Dennis Vadura, dvadura@dmake.wticorp.com
65 -- http://dmake.wticorp.com/
68 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
70 -- This program is NOT free software; you can redistribute it and/or
71 -- modify it under the terms of the Software License Agreement Provided
72 -- in the file <distribution-root>/readme/license.txt.
75 -- Use cvs log to obtain detailed change logs.
80 /* Microsoft BRAINDAMAGE ALERT!!!!
81 * This #ifdef is here only to satisfy stupid bugs in MSC5.0 and MSC5.1
82 * it isn't needed for anything else. It turns loop optimization off. */
83 #if defined(_MSV_VER) && _MSC_VER < 600
87 static char* _scan_macro
ANSI((char*, char**, int));
88 static char* _scan_brace
ANSI((char*, char**, int*));
89 static char* _cross_prod
ANSI((char*, char*));
91 #if !defined(__GNUC__) && !defined(__IBMC__)
92 static char* _scan_ballanced_parens
ANSI((char*, char));
94 static char* _scan_ballanced_parens
ANSI((char*, int));
101 This is the driver routine for the expansion, it identifies non-white
102 space tokens and gets the ScanToken routine to figure out if they should
103 be treated in a special way. */
105 char *src
; /* pointer to source string */
107 char *tmp
; /* pointer to temporary str */
108 char *res
; /* pointer to result string */
109 char *start
; /* pointer to start of token */
111 DB_ENTER( "Expand" );
112 DB_PRINT( "exp", ("Expanding [%s]", src
) );
114 res
= DmStrDup( "" );
115 if( src
== NIL(char) ) DB_RETURN( res
);
120 /* Here we find the next non white space token in the string
121 * and find it's end, with respect to non-significant white space. */
123 #if !defined( _MPW) && !defined(__EMX__)
124 start
= DmStrSpn( src
, " \t\n" );
126 start
= DmStrSpn( src
, " \t\r\n" );
129 res
= DmStrJoin( res
, src
, start
-src
, TRUE
);
130 if( !(*start
) ) break;
132 /* START <+...+> KLUDGE */
133 if( (ks
=DmStrStr(start
,"<+")) != NIL(char)
134 && (ke
=DmStrStr(ks
,"+>")) != NIL(char) ) {
137 res
= DmStrJoin( res
, t2
=Expand(t1
=DmSubStr(start
,ks
)), -1, TRUE
);
140 t1
= DmSubStr(ks
+2, ke
+1); t1
[ke
-ks
-2] = ')';
141 t2
= DmStrJoin( "$(mktmp ", t1
, -1,FALSE
);
143 res
= DmStrJoin( res
, t1
=Expand(t2
), -1, TRUE
);
147 /* END <+...+> KLUDGE */
149 res
= DmStrJoin( res
, tmp
= ScanToken(start
,&src
,TRUE
), -1, TRUE
);
154 DB_PRINT( "exp", ("Returning [%s]", res
) );
160 Apply_edit( src
, pat
, subst
, fr
, anchor
)/*
161 ===========================================
162 Take the src string and apply the pattern substitution. ie. look for
163 occurrences of pat in src and replace each occurrence with subst. This is
164 NOT a regular expressions pattern substitution, it's just not worth it.
166 if anchor == TRUE then the src pattern match must be at the end of a token.
167 ie. this is for SYSV compatibility and is only used for substitutions of
168 the caused by $(macro:pat=sub). So if src = "fre.o.k june.o" then
169 $(src:.o=.a) results in "fre.o.k june.a", and $(src:s/.o/.a) results in
172 char *src
; /* the source string */
173 char *pat
; /* pattern to find */
174 char *subst
; /* substitute string */
175 int fr
; /* if TRUE free src */
176 int anchor
; /* if TRUE anchor */
183 DB_ENTER( "Apply_edit" );
185 /* do nothing if pat is NULL or pat and subst are equal */
186 if( !*pat
|| !strcmp(pat
,subst
) ) DB_RETURN( src
);
188 DB_PRINT( "mod", ("Source str: [%s]", src
) );
189 DB_PRINT( "mod", ("Replacing [%s], with [%s]", pat
, subst
) );
191 /* FIXME: This routine is used frequently and has room for optimizations */
194 if( (p
= DmStrStr( s
, pat
)) != NIL(char) ) {
195 res
= DmStrDup( "" );
198 if( !*(p
+l
) || (strchr(" \t", *(p
+l
)) != NIL(char)) )
199 res
= DmStrJoin( DmStrJoin(res
,s
,p
-s
,TRUE
), subst
, -1, TRUE
);
201 res
= DmStrJoin( res
, s
, p
+l
-s
, TRUE
);
203 res
= DmStrJoin( DmStrJoin(res
,s
,p
-s
,TRUE
), subst
, -1, TRUE
);
207 while( (p
= DmStrStr( s
, pat
)) != NIL(char) );
209 res
= DmStrJoin( res
, s
, -1, TRUE
);
210 if( fr
) FREE( src
);
216 DB_PRINT( "mod", ("Result [%s]", res
) );
224 Map an escape sequence and replace it by it's corresponding character
225 value. It is assumed that tok points at the initial \, the esc
226 sequence in the original string is replaced and the value of tok
230 if( strchr( "\"\\vantbrf01234567", tok
[1] ) ) {
233 case 'a' : *tok
= 0x07; break;
234 case 'b' : *tok
= '\b'; break;
235 case 'f' : *tok
= '\f'; break;
236 case 'n' : *tok
= '\n'; break;
237 case 'r' : *tok
= '\r'; break;
238 case 't' : *tok
= '\t'; break;
239 case 'v' : *tok
= 0x0b; break;
240 case '\\': *tok
= '\\'; break;
241 case '\"': *tok
= '\"'; break;
246 for( ; i
<2 && isdigit(tok
[2]); i
++ ) {
247 j
= (j
<< 3) + (tok
[1] - '0');
248 len
= strlen(tok
+2)+1;
249 memmove( tok
+1, tok
+2, len
);
251 j
= (j
<< 3) + (tok
[1] - '0');
255 len
= strlen(tok
+2)+1;
256 memmove( tok
+1, tok
+2, len
);
262 Apply_modifiers( mod
, src
)/*
263 =============================
264 This routine applies the appropriate modifiers to the string src
265 and returns the proper result string */
275 DB_ENTER( "Apply_modifiers" );
277 if ( mod
& INFNAME_FLAG
) {
278 SET_TOKEN( &str
, src
);
281 while( *(s
= Get_token( &str
, "", FALSE
)) != '\0' ) {
284 if ( (hp
= Get_name(normalize_path(s
), Defs
, FALSE
)) != NIL(HASH
)
286 && hp
->CP_OWNR
->ce_fname
288 res
= hp
->CP_OWNR
->ce_fname
;
293 if(str
.tk_quote
== 0) {
294 /* Add leading quote. */
295 e
= DmStrApp(e
, "\"");
296 e
= DmStrJoin(e
, res
, -1, TRUE
);
297 /* Append the trailing quote. */
298 e
= DmStrJoin(e
, "\"", 1, TRUE
);
300 e
= DmStrApp(e
, res
);
307 mod
&= ~INFNAME_FLAG
;
310 if ( mod
& NORMPATH_FLAG
) {
311 e
= exec_normpath(src
);
315 mod
&= ~NORMPATH_FLAG
;
318 if(mod
& (TOLOWER_FLAG
|TOUPPER_FLAG
) ) {
320 lower
= mod
& TOLOWER_FLAG
;
324 *s
= ((lower
) ? tolower(*s
) : toupper(*s
));
326 mod
&= ~(TOLOWER_FLAG
|TOUPPER_FLAG
);
329 if (mod
& JUST_FIRST_FLAG
) {
330 SET_TOKEN(&str
, src
);
331 if ((s
= Get_token(&str
,"",FALSE
)) != '\0') {
332 /* Recycle the quote at the beginning. */
333 if(str
.tk_quote
== 0) {
337 /* Add trailing quote. */
338 if(str
.tk_quote
== 0) {
339 e
= DmStrJoin(e
, "\"", 1, TRUE
);
349 mod
&= ~JUST_FIRST_FLAG
;
352 if( !mod
|| mod
== (SUFFIX_FLAG
| DIRECTORY_FLAG
| FILE_FLAG
) )
355 SET_TOKEN( &str
, src
);
356 DB_PRINT( "mod", ("Source string [%s]", src
) );
359 while( *(s
= Get_token( &str
, "", FALSE
)) != '\0' ) {
362 /* search for the directory portion of the filename. If the
363 * DIRECTORY_FLAG is set, then we want to keep the directory portion
364 * othewise throw it away and blank out to the end of the token */
366 if( (e
= Basename(s
)) != s
) {
367 if( !(mod
& DIRECTORY_FLAG
) ) {
368 /* Move the basename to the start. */
369 size_t len
= strlen(e
)+1;
375 /* s now points to the start of the basename. */
378 /* search for the suffix, if there is none, treat it as a NULL suffix.
379 * if no file name treat it as a NULL file name. same copy op as
380 * for directory case above */
382 e
= strrchr( s
, '.' ); /* NULL suffix if e=0 */
383 if( e
== NIL(char) ) e
= s
+strlen(s
);
385 if( !(mod
& FILE_FLAG
) ) {
386 /* Move the suffix to the start. */
387 size_t len
= strlen(e
)+1;
393 /* s now points to the start of the suffix. */
396 /* The last and final part. This is the suffix case, if we don't want
397 * it then just erase it. */
400 if( !(mod
& SUFFIX_FLAG
) && s
!= str
.tk_str
)
404 /* only keep non-empty tokens. (This also discards empty quoted ""
406 if( strlen(tokstart
) ) {
407 /* Recycle the quote at the beginning. */
408 if(str
.tk_quote
== 0) {
411 res
= DmStrApp(res
, tokstart
);
412 /* Add trailing quote. */
413 if(str
.tk_quote
== 0) {
414 res
= DmStrJoin(res
, "\"", 1, TRUE
);
423 DB_PRINT( "mod", ("Result string [%s]", src
) );
429 Tokenize( src
, separator
, op
, mapesc
)/*
430 ========================================
431 Tokenize the input of src and join each token found together with
432 the next token separated by the separator string.
434 When doing the tokenization, <sp>, <tab>, <nl>, and \<nl> all
435 constitute white space. */
445 int first
= (op
== 't' || op
== 'T');
447 DB_ENTER( "Tokenize" );
449 /* map the escape codes in the separator string first */
451 for(tok
=separator
; (tok
= strchr(tok
,ESCAPE_CHAR
)) != NIL(char); tok
++)
454 DB_PRINT( "exp", ("Separator [%s]", separator
) );
456 /* By default we return an empty string */
457 res
= DmStrDup( "" );
459 /* Build the token list */
460 SET_TOKEN( &tokens
, src
);
461 while( *(tok
= Get_token( &tokens
, "", FALSE
)) != '\0' ) {
466 res
= DmStrDup( tok
);
469 else if (op
== '^') {
470 res
= DmStrAdd(res
, DmStrJoin(separator
, tok
, -1, FALSE
), TRUE
);
472 else if (op
== '+') {
473 res
= DmStrAdd(res
, DmStrJoin(tok
, separator
, -1, FALSE
), TRUE
);
476 res
= DmStrJoin(res
, x
=DmStrJoin(separator
, tok
, -1, FALSE
),
481 DB_PRINT( "exp", ("Tokenizing [%s] --> [%s]", tok
, res
) );
490 _scan_ballanced_parens(p
, delim
)
500 if( !(bcount
|| pcount
) && *p
== delim
) {
504 if ( *p
== '(' ) pcount
++;
505 else if ( *p
== '{' ) bcount
++;
506 else if ( *p
== ')' && pcount
) pcount
--;
507 else if ( *p
== '}' && bcount
) bcount
--;
511 while (*p
&& (pcount
|| bcount
|| delim
));
519 ScanToken( s
, ps
, doexpand
)/*
520 ==============================
521 This routine scans the token characters one at a time and identifies
522 macros starting with $( and ${ and calls _scan_macro to expand their
523 value. the string1{ token_list }string2 expansion is also handled.
524 In this case a temporary result is maintained so that we can take it's
525 cross product with any other token_lists that may possibly appear. */
527 char *s
; /* pointer to start of src string */
528 char **ps
; /* pointer to start pointer */
531 char *res
; /* pointer to result */
532 char *start
; /* pointer to start of prefix */
533 int crossproduct
= 0; /* if 1 then computing X-prod */
536 res
= DmStrDup( "" );
539 /* Termination, We halt at seeing a space or a tab or end of string.
540 * We return the value of the result with any new macro's we scanned
541 * or if we were computing cross_products then we return the new
543 * NOTE: Once we start computing cross products it is impossible to
544 * stop. ie. the semantics are such that once a {} pair is
545 * seen we compute cross products until termination. */
557 tmp
= DmStrJoin( res
, start
, (s
-start
), TRUE
);
560 tmp
= DmSubStr( start
, s
);
561 tmp
= _cross_prod( res
, tmp
);
569 /* Handle if it's a macro or if it's a {} construct.
570 * The results of a macro expansion are handled differently based
571 * on whether we have seen a {} beforehand. */
574 tmp
= DmSubStr( start
, s
); /* save the prefix */
577 start
= _scan_macro( s
+1, &s
, doexpand
);
580 res
= _cross_prod( res
, DmStrJoin( tmp
, start
, -1, TRUE
) );
583 res
= DmStrJoin(res
,tmp
=DmStrJoin(tmp
,start
,-1,TRUE
),-1,TRUE
);
588 else if( strchr("{ \t",s
[1]) == NIL(char) ){
590 start
= _scan_brace( s
+1, &s
, &ok
);
593 if ( crossproduct
) {
594 res
= _cross_prod(res
,_cross_prod(tmp
,start
));
598 res
= Tokenize(start
,
599 freeres
=DmStrJoin(res
,tmp
,-1,TRUE
),
607 res
=DmStrJoin(res
,tmp
=DmStrJoin(tmp
,start
,-1,TRUE
),-1,TRUE
);
612 else { /* handle the {{ case */
613 res
= DmStrJoin( res
, start
, (s
-start
+1), TRUE
);
614 s
+= (s
[1]=='{')?2:1;
624 /* error malformed macro expansion */
627 else { /* handle the }} case */
628 res
= DmStrJoin( res
, start
, (s
-start
+1), TRUE
);
641 _scan_macro( s
, ps
, doexpand
)/*
642 ================================
643 This routine scans a macro use and expands it to the value. It
644 returns the macro's expanded value and modifies the pointer into the
645 src string to point at the first character after the macro use.
646 The types of uses recognized are:
648 $$ and $<sp> - expands to $
649 $(name) - expands to value of name
650 ${name} - same as above
651 $($(name)) - recurses on macro names (any level)
653 $(func[,args ...] [data])
655 $(name:modifier_list:modifier_list:...)
657 see comment for Expand for description of valid modifiers.
659 NOTE that once a macro name bounded by ( or { is found only
660 the appropriate terminator (ie. ( or } is searched for. */
662 char *s
; /* pointer to start of src string */
663 char **ps
; /* pointer to start pointer */
664 int doexpand
; /* If TRUE enables macro expansion */
666 char sdelim
; /* start of macro delimiter */
667 char edelim
; /* corresponding end macro delim */
668 char *start
; /* start of prefix */
669 char *macro_name
; /* temporary macro name */
670 char *recurse_name
; /* recursive macro name */
671 char *result
; /* result for macro expansion */
672 int bflag
= 0; /* brace flag, ==0 => $A type macro */
673 int done
= 0; /* != 0 => done macro search */
674 int lev
= 0; /* brace level */
675 int mflag
= 0; /* != 0 => modifiers present in mac */
676 int fflag
= 0; /* != 0 => GNU style function */
677 HASHPTR hp
; /* hash table pointer for macros */
679 DB_ENTER( "_scan_macro" );
681 /* Check for $ at end of line, or $ followed by white space */
682 /* FIXME: Shouldn't a single '$' be an error? */
683 if( !*s
|| strchr(" \t", *s
) != NIL(char)) {
685 DB_RETURN( DmStrDup("") );
688 if( *s
== '$' ) { /* Take care of the simple $$ case. */
690 DB_RETURN( DmStrDup("$") );
693 sdelim
= *s
; /* set and remember start/end delim */
699 start
= s
; /* build up macro name, find its end */
702 case '(': /* open macro brace */
710 case ':': /* halt at modifier */
711 if( lev
== 1 && !fflag
&& doexpand
) {
715 else if( !lev
) /* must be $: */
716 Fatal( "Syntax error in macro [$%s]. A colon [:] cannot be a macro name.\n", start
);
718 /* continue if a colon is found but lev > 1 */
721 case '\n': /* Not possible because of the
723 Fatal( "DEBUG: No standalone '\n' [%s].\n", start
);
726 case '\\': /* Transform \<nl> -> ' '. */
734 memmove( s
, s
+1, len
);
739 if ( lev
== 1 ) fflag
= 1;
742 case '\0': /* check for null */
745 if( lev
) { /* catch $( or ${ without closing bracket */
746 Fatal( "Syntax error in macro [$%s]. The closing bracket [%c] is missing.\n", start
, edelim
);
748 Fatal( "DEBUG: This cannot occur! [%s].\n", start
);
751 case ')': /* close macro brace */
753 if( !lev
) /* A closing bracket without an .. */
754 Fatal("Syntax error in macro [$%s]. Closing bracket [%c] cannot be a macro name.\n", start
, *s
);
755 else if( *s
== edelim
) --lev
;
758 default: /* Done when lev == 0. This means either no */
759 done
= !lev
; /* opening bracket (single letter macro) or */
760 /* a fully enclosed $(..) or ${..} macro */
766 /* Check if this is a $A type macro. If so then we have to
767 * handle it a little differently. */
769 macro_name
= DmSubStr( start
+1, s
-1 );
771 macro_name
= DmSubStr( start
, s
);
773 /* If we don't have to expand the macro we're done. */
776 DB_RETURN(macro_name
);
779 /* Check to see if the macro name contains spaces, if so then treat it
780 * as a GNU style function invocation and call the function mapper to
781 * deal with it. We do not call the function expander if the function
782 * invocation begins with a '$' */
783 if( fflag
&& *macro_name
!= '$' ) {
784 result
= Exec_function(macro_name
);
787 /* Check if the macro is a recursive macro name, if so then
788 * EXPAND the name before expanding the value */
789 if( strchr( macro_name
, '$' ) != NIL(char) ) {
790 recurse_name
= Expand( macro_name
);
792 macro_name
= recurse_name
;
795 /* Code to do value expansion goes here, NOTE: macros whose assign bit
796 is one have been evaluated and assigned, they contain no further
797 expansions and thus do not need their values expanded again. */
799 if( (hp
= GET_MACRO( macro_name
)) != NIL(HASH
) ) {
800 if( hp
->ht_flag
& M_MARK
)
801 Fatal( "Detected circular macro [%s]", hp
->ht_name
);
803 if( !(hp
->ht_flag
& M_EXPANDED
) ) {
804 hp
->ht_flag
|= M_MARK
;
805 result
= Expand( hp
->ht_value
);
806 hp
->ht_flag
^= M_MARK
;
808 else if( hp
->ht_value
!= NIL(char) )
809 result
= DmStrDup( hp
->ht_value
);
811 result
= DmStrDup( "" );
815 /* The use of an undefined macro implicitly defines it but
816 * leaves its value to NIL(char). */
817 hp
= Def_macro( macro_name
, NIL(char), M_EXPANDED
);
818 /* Setting M_INIT assures that this macro is treated unset like
819 * default internal macros. (Necessary for *= and *:=) */
820 hp
->ht_flag
|= M_INIT
;
822 result
= DmStrDup( "" );
824 /* Mark macros as used only if we are not expanding them for
825 * the purpose of a .IF test, so we can warn about redef after use*/
826 if( !If_expand
) hp
->ht_flag
|= M_USED
;
832 int modifier_list
= 0;
838 /* We are inside of a macro expansion. The "build up macro name,
839 * find its while loop above should have caught all \<nl> and
840 * converted them to a real space. Let's verify this. */
841 for( p
=s
; *p
&& *p
!= edelim
&& *p
; p
++ ) {
842 if( p
[0] == '\\' && p
[1] == '\n' ) {
846 memmove( p
, p
+1, len
);
850 Fatal( "Syntax error in macro modifier pattern [$%s]. The closing bracket [%c] is missing.\n", start
, edelim
);
852 /* Yet another brain damaged AUGMAKE kludge. We should accept the
853 * AUGMAKE bullshit of $(f:pat=sub) form of macro expansion. In
854 * order to do this we will forgo the normal processing if the
855 * AUGMAKE solution pans out, otherwise we will try to process the
856 * modifiers ala dmake.
858 * So we look for = in modifier string.
859 * If found we process it and not do the normal stuff */
861 for( p
=s
; *p
&& *p
!= '=' && *p
!= edelim
; p
++ );
866 pat1
= Expand(tmp
= DmSubStr(s
,p
)); FREE(tmp
);
868 p
= _scan_ballanced_parens(s
+1, edelim
);
871 Fatal( "Incomplete macro expression [%s]", s
);
874 pat2
= Expand(tmp
= DmSubStr(s
,p
)); FREE(tmp
);
876 result
= Apply_edit( result
, pat1
, pat2
, TRUE
, TRUE
);
884 while( *s
&& *s
!= edelim
) { /* while not at end of macro */
887 switch( switch_char
= *s
++ ) {
888 case '1': modifier_list
|= JUST_FIRST_FLAG
; break;
891 case 'B': modifier_list
|= FILE_FLAG
; break;
894 case 'D': modifier_list
|= DIRECTORY_FLAG
; break;
897 case 'F': modifier_list
|= FILE_FLAG
| SUFFIX_FLAG
; break;
900 case 'E': modifier_list
|= SUFFIX_FLAG
; break;
903 case 'L': modifier_list
|= TOLOWER_FLAG
; break;
906 case 'I': modifier_list
|= INFNAME_FLAG
; break;
909 case 'U': modifier_list
|= TOUPPER_FLAG
; break;
913 if( modifier_list
|| ( (*s
!= edelim
) && (*s
!= ':') ) ) {
914 Warning( "Map escape modifier must appear alone, ignored");
918 /* map the escape codes in the separator string first */
919 for(p
=result
; (p
= strchr(p
,ESCAPE_CHAR
)) != NIL(char); p
++)
922 /* find the end of the macro spec, or the start of a new
923 * modifier list for further processing of the result */
925 for( ; (*s
!= edelim
) && (*s
!= ':') && *s
; s
++ );
927 Fatal( "Syntax error in macro. [$%s].\n", start
);
932 case 'N': modifier_list
|= NORMPATH_FLAG
; break;
936 if( modifier_list
) {
937 Warning( "Edit modifier must appear alone, ignored");
942 for( p
=s
; *p
!= separator
&& *p
; p
++ );
945 Fatal( "Syntax error in subst macro. [$%s].\n", start
);
948 pat1
= DmSubStr( s
, p
);
949 for(s
=p
=p
+1; (*p
!= separator
) && *p
; p
++ );
950 /* Before the parsing fixes in iz36027 the :s macro modifier
951 * erroneously worked with patterns with missing pattern
952 * separator, i.e. $(XXX:s#pat#sub). This is an error because
953 * it prohibits the use of following macro modifiers.
954 * I.e. $(XXX:s#pat#sub:u) falsely replaces with "sub:u".
955 * ??? Remove this special case once OOo compiles without
956 * any of this warnings. */
958 if( *(p
-1) == edelim
) {
960 Warning( "Syntax error in subst macro. Bracket found, but third delimiter [%c] missing in [$%s].\n", separator
, start
);
963 Fatal( "Syntax error in subst macro. Third delimiter [%c] missing in [$%s].\n", separator
, start
);
966 pat2
= DmSubStr( s
, p
);
967 t1
= Expand(pat1
); FREE(pat1
);
968 t2
= Expand(pat2
); FREE(pat2
);
969 result
= Apply_edit( result
, t1
, t2
, TRUE
, FALSE
);
975 /* find the end of the macro spec, or the start of a new
976 * modifier list for further processing of the result */
978 for( ; (*s
!= edelim
) && (*s
!= ':') && *s
; s
++ );
980 Fatal( "Syntax error in macro. [$%s].\n", start
);
988 if( modifier_list
) {
989 Warning( "Tokenize modifier must appear alone, ignored");
995 if( separator
== '$' ) {
996 p
= _scan_ballanced_parens(s
,'\0');
1000 pat1
= Expand(tmp
= DmSubStr(s
-1,p
));
1002 result
= Tokenize(result
, pat1
, switch_char
, TRUE
);
1006 Warning( "Incomplete macro expression [%s]", s
);
1010 else if ( separator
== '\"' ) {
1011 /* we change the semantics to allow $(v:t")") */
1012 for (p
= s
; *p
&& *p
!= separator
; p
++)
1014 if (p
[1] == '\\' || p
[1] == '"')
1018 Fatal( "Unterminated separator string" );
1020 pat1
= DmSubStr( s
, p
);
1021 result
= Tokenize( result
, pat1
, switch_char
, TRUE
);
1028 "Separator must be a quoted string or macro expression");
1031 /* find the end of the macro spec, or the start of a new
1032 * modifier list for further processing of the result */
1034 for( ; (*s
!= edelim
) && (*s
!= ':'); s
++ );
1035 if( *s
== ':' ) s
++;
1040 if( modifier_list
) {
1041 result
= Apply_modifiers( modifier_list
, result
);
1047 Warning( "Illegal modifier in macro, ignored" );
1052 if( modifier_list
) /* apply modifier */
1053 result
= Apply_modifiers( modifier_list
, result
);
1060 DB_RETURN( result
);
1065 _scan_brace( s
, ps
, flag
)/*
1066 ============================
1067 This routine scans for { token_list } pairs. It expands the value of
1068 token_list by calling Expand on it. Token_list may be anything at all.
1069 Note that the routine count's ballanced parentheses. This means you
1070 cannot have something like { fred { joe }, if that is what you really
1071 need the write it as { fred {{ joe }, flag is set to 1 if all ok
1072 and to 0 if the braces were unballanced. */
1084 DB_ENTER( "_scan_brace" );
1090 if( *s
== '{' ) break; /* ignore {{ */
1095 if( *s
== '}' ) break; /* ignore }} */
1097 if( --lev
== 0 ) done
= TRUE
;
1101 if( *s
== '{' || *s
== '}' ) {
1102 if( (t
= strchr(s
,'}')) != NIL(char) )
1112 /* error malformed macro expansion */
1117 start
= DmSubStr( start
, (lev
) ? s
: s
-1 );
1120 /* Braces were not ballanced so just return the string.
1121 * Do not expand it. */
1123 res
= DmStrJoin( "{", start
, -1, FALSE
);
1128 res
= Expand( start
);
1130 if( (t
= DmStrSpn( res
, " \t" )) != res
) {
1131 size_t len
= strlen(t
)+1;
1132 memmove( res
, t
, len
);
1136 FREE( start
); /* this is ok! start is assigned a DmSubStr above */
1144 _cross_prod( x
, y
)/*
1145 =====================
1146 Given two strings x and y compute the cross-product of the tokens found
1147 in each string. ie. if x = "a b" and y = "c d" return "ac ad bc bd".
1149 NOTE: buf will continue to grow until it is big enough to handle
1150 all cross product requests. It is never freed! (maybe I
1151 will fix this someday) */
1156 static char *buf
= NULL
;
1157 static int buf_siz
= 0;
1166 res
= DmStrDup( "" ); cx
= x
;
1169 brkx
= DmStrPbrk( cx
, " \t\n" );
1170 if( (brkx
-cx
== 2) && *cx
== '\"' && *(cx
+1) == '\"' ) cx
= brkx
;
1173 brky
= DmStrPbrk( cy
, " \t\n" );
1174 if( (brky
-cy
== 2) && *cy
== '\"' && *(cy
+1) == '\"' ) cy
= brky
;
1175 i
= brkx
-cx
+ brky
-cy
+ 2;
1177 if( i
> buf_siz
) { /* grow buf to the correct size */
1178 if( buf
!= NIL(char) ) FREE( buf
);
1179 if( (buf
= MALLOC( i
, char )) == NIL(char)) No_ram();
1183 strncpy( buf
, cx
, (i
= brkx
-cx
) );
1185 if (brky
-cy
> 0) strncat( buf
, cy
, brky
-cy
);
1186 buf
[i
+(brky
-cy
)] = '\0';
1188 res
= DmStrJoin( res
, buf
, -1, TRUE
);
1189 cy
= DmStrSpn( brky
, " \t\n" );
1191 cx
= DmStrSpn( brkx
, " \t\n" );
1195 res
[ strlen(res
)-1 ] = '\0';
1198 res
= DmStrJoin( x
, y
, -1, TRUE
);