1 /* $RCSfile: expand.c,v $
3 -- last change: $Author: ihi $ $Date: 2007-10-15 15:38:46 $
6 -- Macro expansion code.
10 -- This routine handles all the necessary junk that deals with macro
11 -- expansion. It understands the following syntax. If a macro is
12 -- not defined it expands to NULL, and {} are synonyms for ().
17 -- $A - expands to whatever the macro A is defined as
18 -- $(AA) - expands to whatever the macro AA is defined as
19 -- $($(A)) - represents macro indirection
20 -- <+...+> - get mapped to $(mktmp ...)
22 -- following macro is recognized
24 -- string1{ token_list }string2
26 -- and expands to string1 prepended to each element of token_list and
27 -- string2 appended to each of the resulting tokens from the first
28 -- operation. If string2 is of the form above then the result is
29 -- the cross product of the specified (possibly modified) token_lists.
31 -- The folowing macro modifiers are defined and expanded:
33 -- $(macro:modifier_list:modifier_list:...)
35 -- where modifier_list a combination of:
37 -- D or d - Directory portion of token including separator
38 -- F or f - File portion of token including suffix
39 -- B or b - basename portion of token not including suffix
40 -- E or e - Suffix portion of name
41 -- L or l - translate to lower case
42 -- U or u - translate to upper case
43 -- I or i - return inferred names
44 -- N or n - return normalized paths
45 -- 1 - return the first white space separated token
47 -- or a single one of:
48 -- M or m - map escape codes
49 -- S or s - pattern substitution (simple)
50 -- T or t - for tokenization
51 -- ^ - prepend a prefix to each token
52 -- + - append a suffix to each token
54 -- NOTE: Modifiers are applied once the macro value has been found.
55 -- Thus the construct $($(test):s/joe/mary/) is defined and
56 -- modifies the value of $($(test))
58 -- Also the construct $(m:d:f) is not the same as $(m:df)
59 -- the first applies d to the value of $(m) and then
60 -- applies f to the value of that whereas the second form
61 -- applies df to the value of $(m).
64 -- Dennis Vadura, dvadura@dmake.wticorp.com
67 -- http://dmake.wticorp.com/
70 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
72 -- This program is NOT free software; you can redistribute it and/or
73 -- modify it under the terms of the Software License Agreement Provided
74 -- in the file <distribution-root>/readme/license.txt.
77 -- Use cvs log to obtain detailed change logs.
82 /* Microsoft BRAINDAMAGE ALERT!!!!
83 * This #ifdef is here only to satisfy stupid bugs in MSC5.0 and MSC5.1
84 * it isn't needed for anything else. It turns loop optimization off. */
85 #if defined(_MSV_VER) && _MSC_VER < 600
89 static char* _scan_macro
ANSI((char*, char**, int));
90 static char* _scan_brace
ANSI((char*, char**, int*));
91 static char* _cross_prod
ANSI((char*, char*));
93 #if !defined(__GNUC__) && !defined(__IBMC__)
94 static char* _scan_ballanced_parens
ANSI((char*, char));
96 static char* _scan_ballanced_parens
ANSI((char*, int));
103 This is the driver routine for the expansion, it identifies non-white
104 space tokens and gets the ScanToken routine to figure out if they should
105 be treated in a special way. */
107 char *src
; /* pointer to source string */
109 char *tmp
; /* pointer to temporary str */
110 char *res
; /* pointer to result string */
111 char *start
; /* pointer to start of token */
113 DB_ENTER( "Expand" );
114 DB_PRINT( "exp", ("Expanding [%s]", src
) );
116 res
= DmStrDup( "" );
117 if( src
== NIL(char) ) DB_RETURN( res
);
122 /* Here we find the next non white space token in the string
123 * and find it's end, with respect to non-significant white space. */
125 #if !defined( _MPW) && !defined(__EMX__)
126 start
= DmStrSpn( src
, " \t\n" );
128 start
= DmStrSpn( src
, " \t\r\n" );
131 res
= DmStrJoin( res
, src
, start
-src
, TRUE
);
132 if( !(*start
) ) break;
134 /* START <+...+> KLUDGE */
135 if( (ks
=DmStrStr(start
,"<+")) != NIL(char)
136 && (ke
=DmStrStr(ks
,"+>")) != NIL(char) ){
139 res
= DmStrJoin( res
, t2
=Expand(t1
=DmSubStr(start
,ks
)), -1, TRUE
);
142 t1
= DmSubStr(ks
+2, ke
+1); t1
[ke
-ks
-2] = ')';
143 t2
= DmStrJoin( "$(mktmp ", t1
, -1,FALSE
);
145 res
= DmStrJoin( res
, t2
=Expand(t2
), -1, TRUE
);
149 /* END <+...+> KLUDGE */
151 res
= DmStrJoin( res
, tmp
= ScanToken(start
,&src
,TRUE
), -1, TRUE
);
156 DB_PRINT( "exp", ("Returning [%s]", res
) );
162 Apply_edit( src
, pat
, subst
, fr
, anchor
)/*
163 ===========================================
164 Take the src string and apply the pattern substitution. ie. look for
165 occurrences of pat in src and replace each occurrence with subst. This is
166 NOT a regular expressions pattern substitution, it's just not worth it.
168 if anchor == TRUE then the src pattern match must be at the end of a token.
169 ie. this is for SYSV compatibility and is only used for substitutions of
170 the caused by $(macro:pat=sub). So if src = "fre.o.k june.o" then
171 $(src:.o=.a) results in "fre.o.k june.a", and $(src:s/.o/.a) results in
174 char *src
; /* the source string */
175 char *pat
; /* pattern to find */
176 char *subst
; /* substitute string */
177 int fr
; /* if TRUE free src */
178 int anchor
; /* if TRUE anchor */
185 DB_ENTER( "Apply_edit" );
187 /* do nothing if pat is NULL or pat and subst are equal */
188 if( !*pat
|| !strcmp(pat
,subst
) ) DB_RETURN( src
);
190 DB_PRINT( "mod", ("Source str: [%s]", src
) );
191 DB_PRINT( "mod", ("Replacing [%s], with [%s]", pat
, subst
) );
193 /* FIXME: This routine is used frequently and has room for optimizations */
196 if( (p
= DmStrStr( s
, pat
)) != NIL(char) ) {
197 res
= DmStrDup( "" );
200 if( !*(p
+l
) || (strchr(" \t", *(p
+l
)) != NIL(char)) )
201 res
= DmStrJoin( DmStrJoin(res
,s
,p
-s
,TRUE
), subst
, -1, TRUE
);
203 res
= DmStrJoin( res
, s
, p
+l
-s
, TRUE
);
205 res
= DmStrJoin( DmStrJoin(res
,s
,p
-s
,TRUE
), subst
, -1, TRUE
);
209 while( (p
= DmStrStr( s
, pat
)) != NIL(char) );
211 res
= DmStrJoin( res
, s
, -1, TRUE
);
212 if( fr
) FREE( src
);
218 DB_PRINT( "mod", ("Result [%s]", res
) );
226 Map an escape sequence and replace it by it's corresponding character
227 value. It is assumed that tok points at the initial \, the esc
228 sequence in the original string is replaced and the value of tok
232 if( strchr( "\"\\vantbrf01234567", tok
[1] ) ) {
234 case 'a' : *tok
= 0x07; break;
235 case 'b' : *tok
= '\b'; break;
236 case 'f' : *tok
= '\f'; break;
237 case 'n' : *tok
= '\n'; break;
238 case 'r' : *tok
= '\r'; break;
239 case 't' : *tok
= '\t'; break;
240 case 'v' : *tok
= 0x0b; break;
241 case '\\': *tok
= '\\'; break;
242 case '\"': *tok
= '\"'; break;
247 for( ; i
<2 && isdigit(tok
[2]); i
++ ) {
248 j
= (j
<< 3) + (tok
[1] - '0');
249 strcpy( tok
+1, tok
+2 );
251 j
= (j
<< 3) + (tok
[1] - '0');
255 strcpy( tok
+1, tok
+2 );
261 Apply_modifiers( mod
, src
)/*
262 =============================
263 This routine applies the appropriate modifiers to the string src
264 and returns the proper result string */
274 DB_ENTER( "Apply_modifiers" );
276 if ( mod
& INFNAME_FLAG
) {
277 SET_TOKEN( &str
, src
);
280 while( *(s
= Get_token( &str
, "", FALSE
)) != '\0' ) {
283 if ( (hp
= Get_name(normalize_path(s
), Defs
, FALSE
)) != NIL(HASH
)
285 && hp
->CP_OWNR
->ce_fname
287 res
= hp
->CP_OWNR
->ce_fname
;
292 if(str
.tk_quote
== 0) {
293 /* Add leading quote. */
294 e
= DmStrApp(e
, "\"");
295 e
= DmStrJoin(e
, res
, -1, TRUE
);
296 /* Append the trailing quote. */
297 e
= DmStrJoin(e
, "\"", 1, TRUE
);
299 e
= DmStrApp(e
, res
);
306 mod
&= ~INFNAME_FLAG
;
309 if ( mod
& NORMPATH_FLAG
) {
310 e
= exec_normpath(src
);
314 mod
&= ~NORMPATH_FLAG
;
317 if(mod
& (TOLOWER_FLAG
|TOUPPER_FLAG
) ) {
319 lower
= mod
& TOLOWER_FLAG
;
323 *s
= ((lower
) ? tolower(*s
) : toupper(*s
));
325 mod
&= ~(TOLOWER_FLAG
|TOUPPER_FLAG
);
328 if (mod
& JUST_FIRST_FLAG
) {
329 SET_TOKEN(&str
, src
);
330 if ((s
= Get_token(&str
,"",FALSE
)) != '\0') {
331 /* Recycle the quote at the beginning. */
332 if(str
.tk_quote
== 0) {
336 /* Add trailing quote. */
337 if(str
.tk_quote
== 0) {
338 e
= DmStrJoin(e
, "\"", 1, TRUE
);
348 mod
&= ~JUST_FIRST_FLAG
;
351 if( !mod
|| mod
== (SUFFIX_FLAG
| DIRECTORY_FLAG
| FILE_FLAG
) )
354 SET_TOKEN( &str
, src
);
355 DB_PRINT( "mod", ("Source string [%s]", src
) );
358 while( *(s
= Get_token( &str
, "", FALSE
)) != '\0' ) {
361 /* search for the directory portion of the filename. If the
362 * DIRECTORY_FLAG is set, then we want to keep the directory portion
363 * othewise throw it away and blank out to the end of the token */
365 if( (e
= Basename(s
)) != s
) {
366 if( !(mod
& DIRECTORY_FLAG
) ) {
367 /* Move the basename to the start. */
373 /* s now points to the start of the basename. */
376 /* search for the suffix, if there is none, treat it as a NULL suffix.
377 * if no file name treat it as a NULL file name. same copy op as
378 * for directory case above */
380 e
= strrchr( s
, '.' ); /* NULL suffix if e=0 */
381 if( e
== NIL(char) ) e
= s
+strlen(s
);
383 if( !(mod
& FILE_FLAG
) ) {
384 /* Move the suffix to the start. */
390 /* s now points to the start of the suffix. */
393 /* The last and final part. This is the suffix case, if we don't want
394 * it then just erase it. */
397 if( !(mod
& SUFFIX_FLAG
) && s
!= str
.tk_str
)
401 /* only keep non-empty tokens. (This also discards empty quoted ""
403 if( strlen(tokstart
) ) {
404 /* Recycle the quote at the beginning. */
405 if(str
.tk_quote
== 0) {
408 res
= DmStrApp(res
, tokstart
);
409 /* Add trailing quote. */
410 if(str
.tk_quote
== 0) {
411 res
= DmStrJoin(res
, "\"", 1, TRUE
);
420 DB_PRINT( "mod", ("Result string [%s]", src
) );
426 Tokenize( src
, separator
, op
, mapesc
)/*
427 ========================================
428 Tokenize the input of src and join each token found together with
429 the next token separated by the separator string.
431 When doing the tokenization, <sp>, <tab>, <nl>, and \<nl> all
432 constitute white space. */
442 int first
= (op
== 't' || op
== 'T');
444 DB_ENTER( "Tokenize" );
446 /* map the escape codes in the separator string first */
448 for(tok
=separator
; (tok
= strchr(tok
,ESCAPE_CHAR
)) != NIL(char); tok
++)
451 DB_PRINT( "exp", ("Separator [%s]", separator
) );
453 /* By default we return an empty string */
454 res
= DmStrDup( "" );
456 /* Build the token list */
457 SET_TOKEN( &tokens
, src
);
458 while( *(tok
= Get_token( &tokens
, "", FALSE
)) != '\0' ) {
463 res
= DmStrDup( tok
);
466 else if (op
== '^') {
467 res
= DmStrAdd(res
, DmStrJoin(separator
, tok
, -1, FALSE
), TRUE
);
469 else if (op
== '+') {
470 res
= DmStrAdd(res
, DmStrJoin(tok
, separator
, -1, FALSE
), TRUE
);
473 res
= DmStrJoin(res
, x
=DmStrJoin(separator
, tok
, -1, FALSE
),
478 DB_PRINT( "exp", ("Tokenizing [%s] --> [%s]", tok
, res
) );
487 _scan_ballanced_parens(p
, delim
)
497 if( !(bcount
|| pcount
) && *p
== delim
) {
501 if ( *p
== '(' ) pcount
++;
502 else if ( *p
== '{' ) bcount
++;
503 else if ( *p
== ')' && pcount
) pcount
--;
504 else if ( *p
== '}' && bcount
) bcount
--;
508 while (*p
&& (pcount
|| bcount
|| delim
));
516 ScanToken( s
, ps
, doexpand
)/*
517 ==============================
518 This routine scans the token characters one at a time and identifies
519 macros starting with $( and ${ and calls _scan_macro to expand their
520 value. the string1{ token_list }string2 expansion is also handled.
521 In this case a temporary result is maintained so that we can take it's
522 cross product with any other token_lists that may possibly appear. */
524 char *s
; /* pointer to start of src string */
525 char **ps
; /* pointer to start pointer */
528 char *res
; /* pointer to result */
529 char *start
; /* pointer to start of prefix */
530 int crossproduct
= 0; /* if 1 then computing X-prod */
533 res
= DmStrDup( "" );
536 /* Termination, We halt at seeing a space or a tab or end of string.
537 * We return the value of the result with any new macro's we scanned
538 * or if we were computing cross_products then we return the new
540 * NOTE: Once we start computing cross products it is impossible to
541 * stop. ie. the semantics are such that once a {} pair is
542 * seen we compute cross products until termination. */
554 tmp
= DmStrJoin( res
, start
, (s
-start
), TRUE
);
557 tmp
= DmSubStr( start
, s
);
558 tmp
= _cross_prod( res
, tmp
);
566 /* Handle if it's a macro or if it's a {} construct.
567 * The results of a macro expansion are handled differently based
568 * on whether we have seen a {} beforehand. */
571 tmp
= DmSubStr( start
, s
); /* save the prefix */
574 start
= _scan_macro( s
+1, &s
, doexpand
);
577 res
= _cross_prod( res
, DmStrJoin( tmp
, start
, -1, TRUE
) );
580 res
= DmStrJoin(res
,tmp
=DmStrJoin(tmp
,start
,-1,TRUE
),-1,TRUE
);
585 else if( strchr("{ \t",s
[1]) == NIL(char) ){
587 start
= _scan_brace( s
+1, &s
, &ok
);
590 if ( crossproduct
) {
591 res
= _cross_prod(res
,_cross_prod(tmp
,start
));
595 res
= Tokenize(start
,
596 freeres
=DmStrJoin(res
,tmp
,-1,TRUE
),
604 res
=DmStrJoin(res
,tmp
=DmStrJoin(tmp
,start
,-1,TRUE
),-1,TRUE
);
609 else { /* handle the {{ case */
610 res
= DmStrJoin( res
, start
, (s
-start
+1), TRUE
);
611 s
+= (s
[1]=='{')?2:1;
621 /* error malformed macro expansion */
624 else { /* handle the }} case */
625 res
= DmStrJoin( res
, start
, (s
-start
+1), TRUE
);
638 _scan_macro( s
, ps
, doexpand
)/*
639 ================================
640 This routine scans a macro use and expands it to the value. It
641 returns the macro's expanded value and modifies the pointer into the
642 src string to point at the first character after the macro use.
643 The types of uses recognized are:
645 $$ and $<sp> - expands to $
646 $(name) - expands to value of name
647 ${name} - same as above
648 $($(name)) - recurses on macro names (any level)
650 $(func[,args ...] [data])
652 $(name:modifier_list:modifier_list:...)
654 see comment for Expand for description of valid modifiers.
656 NOTE that once a macro name bounded by ( or { is found only
657 the appropriate terminator (ie. ( or } is searched for. */
659 char *s
; /* pointer to start of src string */
660 char **ps
; /* pointer to start pointer */
661 int doexpand
; /* If TRUE enables macro expansion */
663 char sdelim
; /* start of macro delimiter */
664 char edelim
; /* corresponding end macro delim */
665 char *start
; /* start of prefix */
666 char *macro_name
; /* temporary macro name */
667 char *recurse_name
; /* recursive macro name */
668 char *result
; /* result for macro expansion */
669 int bflag
= 0; /* brace flag, ==0 => $A type macro */
670 int done
= 0; /* != 0 => done macro search */
671 int lev
= 0; /* brace level */
672 int mflag
= 0; /* != 0 => modifiers present in mac */
673 int fflag
= 0; /* != 0 => GNU style function */
674 HASHPTR hp
; /* hash table pointer for macros */
676 DB_ENTER( "_scan_macro" );
678 /* Check for $ at end of line, or $ followed by white space */
679 /* FIXME: Shouldn't a single '$' be an error? */
680 if( !*s
|| strchr(" \t", *s
) != NIL(char)) {
682 DB_RETURN( DmStrDup("") );
685 if( *s
== '$' ) { /* Take care of the simple $$ case. */
687 DB_RETURN( DmStrDup("$") );
690 sdelim
= *s
; /* set and remember start/end delim */
696 start
= s
; /* build up macro name, find its end */
699 case '(': /* open macro brace */
707 case ':': /* halt at modifier */
708 if( lev
== 1 && !fflag
&& doexpand
) {
712 else if( !lev
) /* must be $: */
713 Fatal( "Syntax error in macro [$%s]. A colon [:] cannot be a macro name.\n", start
);
715 /* continue if a colon is found but lev > 1 */
718 case '\n': /* Not possible because of the
720 Fatal( "DEBUG: No standalone '\n' [%s].\n", start
);
723 case '\\': /* Transform \<nl> -> ' '. */
734 if ( lev
== 1 ) fflag
= 1;
737 case '\0': /* check for null */
740 if( lev
) { /* catch $( or ${ without closing bracket */
741 Fatal( "Syntax error in macro [$%s]. The closing bracket [%c] is missing.\n", start
, edelim
);
743 Fatal( "DEBUG: This cannot occur! [%s].\n", start
);
746 case ')': /* close macro brace */
748 if( !lev
) /* A closing bracket without an .. */
749 Fatal("Syntax error in macro [$%s]. Closing bracket [%c] cannot be a macro name.\n", start
, *s
);
750 else if( *s
== edelim
) --lev
;
753 default: /* Done when lev == 0. This means either no */
754 done
= !lev
; /* opening bracket (single letter macro) or */
755 /* a fully enclosed $(..) or ${..} macro */
761 /* Check if this is a $A type macro. If so then we have to
762 * handle it a little differently. */
764 macro_name
= DmSubStr( start
+1, s
-1 );
766 macro_name
= DmSubStr( start
, s
);
768 /* If we don't have to expand the macro we're done. */
771 DB_RETURN(macro_name
);
774 /* Check to see if the macro name contains spaces, if so then treat it
775 * as a GNU style function invocation and call the function mapper to
776 * deal with it. We do not call the function expander if the function
777 * invocation begins with a '$' */
778 if( fflag
&& *macro_name
!= '$' ) {
779 result
= Exec_function(macro_name
);
782 /* Check if the macro is a recursive macro name, if so then
783 * EXPAND the name before expanding the value */
784 if( strchr( macro_name
, '$' ) != NIL(char) ) {
785 recurse_name
= Expand( macro_name
);
787 macro_name
= recurse_name
;
790 /* Code to do value expansion goes here, NOTE: macros whose assign bit
791 is one have been evaluated and assigned, they contain no further
792 expansions and thus do not need their values expanded again. */
794 if( (hp
= GET_MACRO( macro_name
)) != NIL(HASH
) ) {
795 if( hp
->ht_flag
& M_MARK
)
796 Fatal( "Detected circular macro [%s]", hp
->ht_name
);
798 if( !(hp
->ht_flag
& M_EXPANDED
) ) {
799 hp
->ht_flag
|= M_MARK
;
800 result
= Expand( hp
->ht_value
);
801 hp
->ht_flag
^= M_MARK
;
803 else if( hp
->ht_value
!= NIL(char) )
804 result
= DmStrDup( hp
->ht_value
);
806 result
= DmStrDup( "" );
810 /* The use of an undefined macro implicitly defines it but
811 * leaves its value to NIL(char). */
812 hp
= Def_macro( macro_name
, NIL(char), M_EXPANDED
);
813 /* Setting M_INIT assures that this macro is treated unset like
814 * default internal macros. (Necessary for *= and *:=) */
815 hp
->ht_flag
|= M_INIT
;
817 result
= DmStrDup( "" );
819 /* Mark macros as used only if we are not expanding them for
820 * the purpose of a .IF test, so we can warn about redef after use*/
821 if( !If_expand
) hp
->ht_flag
|= M_USED
;
827 int modifier_list
= 0;
833 /* We are inside of a macro expansion. The "build up macro name,
834 * find its while loop above should have caught all \<nl> and
835 * converted them to a real space. Let's verify this. */
836 for( p
=s
; *p
&& *p
!= edelim
&& *p
; p
++ ) {
837 if( p
[0] == '\\' && p
[1] == '\n' ) {
843 Fatal( "Syntax error in macro modifier pattern [$%s]. The closing bracket [%c] is missing.\n", start
, edelim
);
845 /* Yet another brain damaged AUGMAKE kludge. We should accept the
846 * AUGMAKE bullshit of $(f:pat=sub) form of macro expansion. In
847 * order to do this we will forgo the normal processing if the
848 * AUGMAKE solution pans out, otherwise we will try to process the
849 * modifiers ala dmake.
851 * So we look for = in modifier string.
852 * If found we process it and not do the normal stuff */
854 for( p
=s
; *p
&& *p
!= '=' && *p
!= edelim
; p
++ );
859 pat1
= Expand(tmp
= DmSubStr(s
,p
)); FREE(tmp
);
861 p
= _scan_ballanced_parens(s
+1, edelim
);
864 Fatal( "Incomplete macro expression [%s]", s
);
867 pat2
= Expand(tmp
= DmSubStr(s
,p
)); FREE(tmp
);
869 result
= Apply_edit( result
, pat1
, pat2
, TRUE
, TRUE
);
877 while( *s
&& *s
!= edelim
) { /* while not at end of macro */
880 switch( switch_char
= *s
++ ) {
881 case '1': modifier_list
|= JUST_FIRST_FLAG
; break;
884 case 'B': modifier_list
|= FILE_FLAG
; break;
887 case 'D': modifier_list
|= DIRECTORY_FLAG
; break;
890 case 'F': modifier_list
|= FILE_FLAG
| SUFFIX_FLAG
; break;
893 case 'E': modifier_list
|= SUFFIX_FLAG
; break;
896 case 'L': modifier_list
|= TOLOWER_FLAG
; break;
899 case 'I': modifier_list
|= INFNAME_FLAG
; break;
902 case 'U': modifier_list
|= TOUPPER_FLAG
; break;
906 if( modifier_list
|| ( (*s
!= edelim
) && (*s
!= ':') ) ) {
907 Warning( "Map escape modifier must appear alone, ignored");
911 /* map the escape codes in the separator string first */
912 for(p
=result
; (p
= strchr(p
,ESCAPE_CHAR
)) != NIL(char); p
++)
915 /* find the end of the macro spec, or the start of a new
916 * modifier list for further processing of the result */
918 for( ; (*s
!= edelim
) && (*s
!= ':') && *s
; s
++ );
920 Fatal( "Syntax error in macro. [$%s].\n", start
);
925 case 'N': modifier_list
|= NORMPATH_FLAG
; break;
929 if( modifier_list
) {
930 Warning( "Edit modifier must appear alone, ignored");
935 for( p
=s
; *p
!= separator
&& *p
; p
++ );
938 Fatal( "Syntax error in subst macro. [$%s].\n", start
);
941 pat1
= DmSubStr( s
, p
);
942 for(s
=p
=p
+1; (*p
!= separator
) && *p
; p
++ );
943 /* Before the parsing fixes in iz36027 the :s macro modifier
944 * erroneously worked with patterns with missing pattern
945 * separator, i.e. $(XXX:s#pat#sub). This is an error because
946 * it prohibits the use of following macro modifiers.
947 * I.e. $(XXX:s#pat#sub:u) falsely replaces with "sub:u".
948 * ??? Remove this special case once OOo compiles without
949 * any of this warnings. */
951 if( *(p
-1) == edelim
) {
953 Warning( "Syntax error in subst macro. Bracket found, but third delimiter [%c] missing in [$%s].\n", separator
, start
);
956 Fatal( "Syntax error in subst macro. Third delimiter [%c] missing in [$%s].\n", separator
, start
);
959 pat2
= DmSubStr( s
, p
);
960 t1
= Expand(pat1
); FREE(pat1
);
961 t2
= Expand(pat2
); FREE(pat2
);
962 result
= Apply_edit( result
, t1
, t2
, TRUE
, FALSE
);
968 /* find the end of the macro spec, or the start of a new
969 * modifier list for further processing of the result */
971 for( ; (*s
!= edelim
) && (*s
!= ':') && *s
; s
++ );
973 Fatal( "Syntax error in macro. [$%s].\n", start
);
981 if( modifier_list
) {
982 Warning( "Tokenize modifier must appear alone, ignored");
988 if( separator
== '$' ) {
989 p
= _scan_ballanced_parens(s
,'\0');
993 pat1
= Expand(tmp
= DmSubStr(s
-1,p
));
995 result
= Tokenize(result
, pat1
, switch_char
, TRUE
);
999 Warning( "Incomplete macro expression [%s]", s
);
1003 else if ( separator
== '\"' ) {
1004 /* we change the semantics to allow $(v:t")") */
1005 for (p
= s
; *p
&& *p
!= separator
; p
++)
1007 if (p
[1] == '\\' || p
[1] == '"')
1011 Fatal( "Unterminated separator string" );
1013 pat1
= DmSubStr( s
, p
);
1014 result
= Tokenize( result
, pat1
, switch_char
, TRUE
);
1021 "Separator must be a quoted string or macro expression");
1024 /* find the end of the macro spec, or the start of a new
1025 * modifier list for further processing of the result */
1027 for( ; (*s
!= edelim
) && (*s
!= ':'); s
++ );
1028 if( *s
== ':' ) s
++;
1033 if( modifier_list
) {
1034 result
= Apply_modifiers( modifier_list
, result
);
1040 Warning( "Illegal modifier in macro, ignored" );
1045 if( modifier_list
) /* apply modifier */
1046 result
= Apply_modifiers( modifier_list
, result
);
1053 DB_RETURN( result
);
1058 _scan_brace( s
, ps
, flag
)/*
1059 ============================
1060 This routine scans for { token_list } pairs. It expands the value of
1061 token_list by calling Expand on it. Token_list may be anything at all.
1062 Note that the routine count's ballanced parentheses. This means you
1063 cannot have something like { fred { joe }, if that is what you really
1064 need the write it as { fred {{ joe }, flag is set to 1 if all ok
1065 and to 0 if the braces were unballanced. */
1077 DB_ENTER( "_scan_brace" );
1083 if( *s
== '{' ) break; /* ignore {{ */
1088 if( *s
== '}' ) break; /* ignore }} */
1090 if( --lev
== 0 ) done
= TRUE
;
1094 if( *s
== '{' || *s
== '}' ) {
1095 if( (t
= strchr(s
,'}')) != NIL(char) )
1105 /* error malformed macro expansion */
1110 start
= DmSubStr( start
, (lev
) ? s
: s
-1 );
1113 /* Braces were not ballanced so just return the string.
1114 * Do not expand it. */
1116 res
= DmStrJoin( "{", start
, -1, FALSE
);
1121 res
= Expand( start
);
1123 if( (t
= DmStrSpn( res
, " \t" )) != res
) strcpy( res
, t
);
1126 FREE( start
); /* this is ok! start is assigned a DmSubStr above */
1134 _cross_prod( x
, y
)/*
1135 =====================
1136 Given two strings x and y compute the cross-product of the tokens found
1137 in each string. ie. if x = "a b" and y = "c d" return "ac ad bc bd".
1139 NOTE: buf will continue to grow until it is big enough to handle
1140 all cross product requests. It is never freed! (maybe I
1141 will fix this someday) */
1146 static char *buf
= NULL
;
1147 static int buf_siz
= 0;
1156 res
= DmStrDup( "" ); cx
= x
;
1159 brkx
= DmStrPbrk( cx
, " \t\n" );
1160 if( (brkx
-cx
== 2) && *cx
== '\"' && *(cx
+1) == '\"' ) cx
= brkx
;
1163 brky
= DmStrPbrk( cy
, " \t\n" );
1164 if( (brky
-cy
== 2) && *cy
== '\"' && *(cy
+1) == '\"' ) cy
= brky
;
1165 i
= brkx
-cx
+ brky
-cy
+ 2;
1167 if( i
> buf_siz
) { /* grow buf to the correct size */
1168 if( buf
!= NIL(char) ) FREE( buf
);
1169 if( (buf
= MALLOC( i
, char )) == NIL(char)) No_ram();
1173 strncpy( buf
, cx
, (i
= brkx
-cx
) );
1175 if (brky
-cy
> 0) strncat( buf
, cy
, brky
-cy
);
1176 buf
[i
+(brky
-cy
)] = '\0';
1178 res
= DmStrJoin( res
, buf
, -1, TRUE
);
1179 cy
= DmStrSpn( brky
, " \t\n" );
1181 cx
= DmStrSpn( brkx
, " \t\n" );
1185 res
[ strlen(res
)-1 ] = '\0';
1188 res
= DmStrJoin( x
, y
, -1, TRUE
);