Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / dmake / expand.c
blob4256f17d49fb573af50338090631c8a28d99ae8f
1 /*
2 --
3 -- SYNOPSIS
4 -- Macro expansion code.
5 --
6 -- DESCRIPTION
7 --
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 ().
12 -- $$ - expands to $
13 -- {{ - expands to {
14 -- }} - expands to }
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).
61 -- AUTHOR
62 -- Dennis Vadura, dvadura@dmake.wticorp.com
64 -- WWW
65 -- http://dmake.wticorp.com/
67 -- COPYRIGHT
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.
74 -- LOG
75 -- Use cvs log to obtain detailed change logs.
78 #include "extern.h"
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
84 #include "optoff.h"
85 #endif
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));
93 #else
94 static char* _scan_ballanced_parens ANSI((char*, int));
95 #endif
98 PUBLIC char *
99 Expand( src )/*
100 ===============
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 );
117 while( *src ) {
118 char *ks, *ke;
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" );
125 #else
126 start = DmStrSpn( src, " \t\r\n" );
127 #endif
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) ) {
135 char *t1, *t2;
137 res = DmStrJoin( res, t2=Expand(t1=DmSubStr(start,ks)), -1, TRUE);
138 FREE(t1); FREE(t2);
140 t1 = DmSubStr(ks+2, ke+1); t1[ke-ks-2] = ')';
141 t2 = DmStrJoin( "$(mktmp ", t1, -1,FALSE);
142 FREE(t1);
143 res = DmStrJoin( res, t1=Expand(t2), -1, TRUE);
144 FREE(t1); FREE(t2);
145 src = ke+2;
147 /* END <+...+> KLUDGE */
148 else {
149 res = DmStrJoin( res, tmp = ScanToken(start,&src,TRUE), -1, TRUE );
150 FREE( tmp );
154 DB_PRINT( "exp", ("Returning [%s]", res) );
155 DB_RETURN( res );
159 PUBLIC char *
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
170 "fre.a.k june.a" */
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 */
178 char *res;
179 char *p;
180 char *s;
181 int l;
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 */
192 s = src;
193 l = strlen( pat );
194 if( (p = DmStrStr( s, pat )) != NIL(char) ) {
195 res = DmStrDup( "" );
196 do {
197 if( anchor )
198 if( !*(p+l) || (strchr(" \t", *(p+l)) != NIL(char)) )
199 res = DmStrJoin( DmStrJoin(res,s,p-s,TRUE), subst, -1, TRUE );
200 else
201 res = DmStrJoin( res, s, p+l-s, TRUE );
202 else
203 res = DmStrJoin( DmStrJoin(res,s,p-s,TRUE), subst, -1, TRUE );
205 s = p + l;
207 while( (p = DmStrStr( s, pat )) != NIL(char) );
209 res = DmStrJoin( res, s, -1, TRUE );
210 if( fr ) FREE( src );
212 else
213 res = src;
216 DB_PRINT( "mod", ("Result [%s]", res) );
217 DB_RETURN( res );
221 PUBLIC void
222 Map_esc( tok )/*
223 ================
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
227 is not modified. */
228 char *tok;
230 if( strchr( "\"\\vantbrf01234567", tok[1] ) ) {
231 size_t len;
232 switch( 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;
243 default: {
244 register int i = 0;
245 register int j = 0;
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');
252 *tok = j;
255 len = strlen(tok+2)+1;
256 memmove( tok+1, tok+2, len );
261 PUBLIC char*
262 Apply_modifiers( mod, src )/*
263 =============================
264 This routine applies the appropriate modifiers to the string src
265 and returns the proper result string */
267 int mod;
268 char *src;
270 char *s;
271 char *e;
272 char *res;
273 TKSTR str;
275 DB_ENTER( "Apply_modifiers" );
277 if ( mod & INFNAME_FLAG ) {
278 SET_TOKEN( &str, src );
279 e = NIL(char);
281 while( *(s = Get_token( &str, "", FALSE )) != '\0' ) {
282 HASHPTR hp;
284 if ( (hp = Get_name(normalize_path(s), Defs, FALSE)) != NIL(HASH)
285 && hp->CP_OWNR
286 && hp->CP_OWNR->ce_fname
288 res = hp->CP_OWNR->ce_fname;
290 else
291 res = s;
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);
299 } else {
300 e = DmStrApp(e, res);
305 FREE(src);
306 src = e;
307 mod &= ~INFNAME_FLAG;
310 if ( mod & NORMPATH_FLAG ) {
311 e = exec_normpath(src);
313 FREE(src);
314 src = e;
315 mod &= ~NORMPATH_FLAG;
318 if(mod & (TOLOWER_FLAG|TOUPPER_FLAG) ) {
319 int lower;
320 lower = mod & TOLOWER_FLAG;
322 for (s=src; *s; s++)
323 if ( isalpha(*s) )
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) {
334 s--;
336 e = DmStrDup(s);
337 /* Add trailing quote. */
338 if(str.tk_quote == 0) {
339 e = DmStrJoin(e, "\"", 1, TRUE);
342 CLEAR_TOKEN(&str);
343 FREE(src);
344 src = e;
346 else {
347 CLEAR_TOKEN(&str);
349 mod &= ~JUST_FIRST_FLAG;
352 if( !mod || mod == (SUFFIX_FLAG | DIRECTORY_FLAG | FILE_FLAG) )
353 DB_RETURN( src );
355 SET_TOKEN( &str, src );
356 DB_PRINT( "mod", ("Source string [%s]", src) );
357 res = DmStrDup("");
359 while( *(s = Get_token( &str, "", FALSE )) != '\0' ) {
360 char *tokstart = s;
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;
370 memmove(s, e, len);
372 else
373 s = e;
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;
388 memmove(s, e, len);
390 else
391 s = e;
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. */
399 if( s != NIL(char) )
400 if( !(mod & SUFFIX_FLAG) && s != str.tk_str )
401 *s = '\0';
404 /* only keep non-empty tokens. (This also discards empty quoted ""
405 * tokens.) */
406 if( strlen(tokstart) ) {
407 /* Recycle the quote at the beginning. */
408 if(str.tk_quote == 0) {
409 tokstart--;
411 res = DmStrApp(res, tokstart);
412 /* Add trailing quote. */
413 if(str.tk_quote == 0) {
414 res = DmStrJoin(res, "\"", 1, TRUE);
419 FREE(src);
420 src = res;
423 DB_PRINT( "mod", ("Result string [%s]", src) );
424 DB_RETURN( src );
428 PUBLIC char*
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. */
437 char *src;
438 char *separator;
439 char op;
440 int mapesc;
442 TKSTR tokens;
443 char *tok;
444 char *res;
445 int first = (op == 't' || op == 'T');
447 DB_ENTER( "Tokenize" );
449 /* map the escape codes in the separator string first */
450 if ( mapesc )
451 for(tok=separator; (tok = strchr(tok,ESCAPE_CHAR)) != NIL(char); tok++)
452 Map_esc( 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' ) {
462 char *x;
464 if( first ) {
465 FREE( res );
466 res = DmStrDup( tok );
467 first = FALSE;
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);
475 else {
476 res = DmStrJoin(res, x =DmStrJoin(separator, tok, -1, FALSE),
477 -1, TRUE);
478 FREE( x );
481 DB_PRINT( "exp", ("Tokenizing [%s] --> [%s]", tok, res) );
484 FREE( src );
485 DB_RETURN( res );
489 static char*
490 _scan_ballanced_parens(p, delim)
491 char *p;
492 char delim;
494 int pcount = 0;
495 int bcount = 0;
497 if ( p ) {
498 do {
499 if (delim)
500 if( !(bcount || pcount) && *p == delim) {
501 return(p);
504 if ( *p == '(' ) pcount++;
505 else if ( *p == '{' ) bcount++;
506 else if ( *p == ')' && pcount ) pcount--;
507 else if ( *p == '}' && bcount ) bcount--;
509 p++;
511 while (*p && (pcount || bcount || delim));
514 return(p);
518 PUBLIC char*
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 */
529 int doexpand;
531 char *res; /* pointer to result */
532 char *start; /* pointer to start of prefix */
533 int crossproduct = 0; /* if 1 then computing X-prod */
535 start = s;
536 res = DmStrDup( "" );
537 while( 1 ) {
538 switch( *s ) {
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
542 * cross_product.
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. */
547 case ' ':
548 case '\t':
549 case '\n':
550 case '\r':
551 case '\0':
553 char *tmp;
555 *ps = s;
556 if( !crossproduct )
557 tmp = DmStrJoin( res, start, (s-start), TRUE );
558 else
560 tmp = DmSubStr( start, s );
561 tmp = _cross_prod( res, tmp );
563 return( tmp );
566 case '$':
567 case '{':
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. */
573 char *tmp;
574 tmp = DmSubStr( start, s ); /* save the prefix */
576 if( *s == '$' ) {
577 start = _scan_macro( s+1, &s, doexpand );
579 if( crossproduct ) {
580 res = _cross_prod( res, DmStrJoin( tmp, start, -1, TRUE ) );
582 else {
583 res = DmStrJoin(res,tmp=DmStrJoin(tmp,start,-1,TRUE),-1,TRUE);
584 FREE( tmp );
586 FREE( start );
588 else if( strchr("{ \t",s[1]) == NIL(char) ){
589 int ok;
590 start = _scan_brace( s+1, &s, &ok );
592 if( ok ) {
593 if ( crossproduct ) {
594 res = _cross_prod(res,_cross_prod(tmp,start));
596 else {
597 char *freeres;
598 res = Tokenize(start,
599 freeres=DmStrJoin(res,tmp,-1,TRUE),
600 '^', FALSE);
601 FREE(freeres);
602 FREE(tmp);
604 crossproduct = TRUE;
606 else {
607 res =DmStrJoin(res,tmp=DmStrJoin(tmp,start,-1,TRUE),-1,TRUE);
608 FREE( start );
609 FREE( tmp );
612 else { /* handle the {{ case */
613 res = DmStrJoin( res, start, (s-start+1), TRUE );
614 s += (s[1]=='{')?2:1;
615 FREE( tmp );
618 start = s;
620 break;
622 case '}':
623 if( s[1] != '}' ) {
624 /* error malformed macro expansion */
625 s++;
627 else { /* handle the }} case */
628 res = DmStrJoin( res, start, (s-start+1), TRUE );
629 s += 2;
630 start = s;
632 break;
634 default: s++;
640 static char*
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)) {
684 *ps = s;
685 DB_RETURN( DmStrDup("") );
688 if( *s == '$' ) { /* Take care of the simple $$ case. */
689 *ps = s+1;
690 DB_RETURN( DmStrDup("$") );
693 sdelim = *s; /* set and remember start/end delim */
694 if( sdelim == '(' )
695 edelim = ')';
696 else
697 edelim = '}';
699 start = s; /* build up macro name, find its end */
700 while( !done ) {
701 switch( *s ) {
702 case '(': /* open macro brace */
703 case '{':
704 if( *s == sdelim ) {
705 lev++;
706 bflag++;
708 break;
710 case ':': /* halt at modifier */
711 if( lev == 1 && !fflag && doexpand ) {
712 done = TRUE;
713 mflag = 1;
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 */
719 break;
721 case '\n': /* Not possible because of the
722 * following case. */
723 Fatal( "DEBUG: No standalone '\n' [%s].\n", start );
724 break;
726 case '\\': /* Transform \<nl> -> ' '. */
727 if( s[1] != '\n' ) {
728 done = !lev;
729 break;
730 } else {
731 size_t len;
732 s[1] = ' ';
733 len = strlen(s+1)+1;
734 memmove( s, s+1, len );
736 /*FALLTHRU*/
737 case ' ':
738 case '\t':
739 if ( lev == 1 ) fflag = 1;
740 break;
742 case '\0': /* check for null */
743 *ps = s;
744 done = TRUE;
745 if( lev ) { /* catch $( or ${ without closing bracket */
746 Fatal( "Syntax error in macro [$%s]. The closing bracket [%c] is missing.\n", start, edelim );
747 } else
748 Fatal( "DEBUG: This cannot occur! [%s].\n", start );
749 break;
751 case ')': /* close macro brace */
752 case '}':
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;
756 /*FALLTHRU*/
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 */
761 /* was found. */
763 s++;
766 /* Check if this is a $A type macro. If so then we have to
767 * handle it a little differently. */
768 if( bflag )
769 macro_name = DmSubStr( start+1, s-1 );
770 else
771 macro_name = DmSubStr( start, s );
773 /* If we don't have to expand the macro we're done. */
774 if (!doexpand) {
775 *ps = s;
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);
786 else {
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 );
791 FREE( 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 );
810 else
811 result = DmStrDup( "" );
814 else {
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;
830 if( mflag ) {
831 char separator;
832 int modifier_list = 0;
833 int aug_mod = FALSE;
834 char *pat1;
835 char *pat2;
836 char *p;
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' ) {
843 size_t len;
844 p[1] = ' ';
845 len = strlen(p+1)+1;
846 memmove( p, p+1, len );
849 if( !*p )
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++ );
863 if( *p == '=' ) {
864 char *tmp;
866 pat1 = Expand(tmp = DmSubStr(s,p)); FREE(tmp);
867 s = p+1;
868 p = _scan_ballanced_parens(s+1, edelim);
870 if ( !*p ) {
871 Fatal( "Incomplete macro expression [%s]", s );
872 p = s+1;
874 pat2 = Expand(tmp = DmSubStr(s,p)); FREE(tmp);
876 result = Apply_edit( result, pat1, pat2, TRUE, TRUE );
877 FREE( pat1 );
878 FREE( pat2 );
879 s = p;
880 aug_mod = TRUE;
883 if( !aug_mod )
884 while( *s && *s != edelim ) { /* while not at end of macro */
885 char switch_char;
887 switch( switch_char = *s++ ) {
888 case '1': modifier_list |= JUST_FIRST_FLAG; break;
890 case 'b':
891 case 'B': modifier_list |= FILE_FLAG; break;
893 case 'd':
894 case 'D': modifier_list |= DIRECTORY_FLAG; break;
896 case 'f':
897 case 'F': modifier_list |= FILE_FLAG | SUFFIX_FLAG; break;
899 case 'e':
900 case 'E': modifier_list |= SUFFIX_FLAG; break;
902 case 'l':
903 case 'L': modifier_list |= TOLOWER_FLAG; break;
905 case 'i':
906 case 'I': modifier_list |= INFNAME_FLAG; break;
908 case 'u':
909 case 'U': modifier_list |= TOUPPER_FLAG; break;
911 case 'm':
912 case 'M':
913 if( modifier_list || ( (*s != edelim) && (*s != ':') ) ) {
914 Warning( "Map escape modifier must appear alone, ignored");
915 modifier_list = 0;
917 else {
918 /* map the escape codes in the separator string first */
919 for(p=result; (p = strchr(p,ESCAPE_CHAR)) != NIL(char); p++)
920 Map_esc( 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++ );
926 if( !*s )
927 Fatal( "Syntax error in macro. [$%s].\n", start );
928 if( *s == ':' ) s++;
929 break;
931 case 'n':
932 case 'N': modifier_list |= NORMPATH_FLAG; break;
934 case 'S':
935 case 's':
936 if( modifier_list ) {
937 Warning( "Edit modifier must appear alone, ignored");
938 modifier_list = 0;
940 else {
941 separator = *s++;
942 for( p=s; *p != separator && *p; p++ );
944 if( !*p )
945 Fatal( "Syntax error in subst macro. [$%s].\n", start );
946 else {
947 char *t1, *t2;
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. */
957 if( !*p ) {
958 if( *(p-1) == edelim ) {
959 p--;
960 Warning( "Syntax error in subst macro. Bracket found, but third delimiter [%c] missing in [$%s].\n", separator, start );
962 else {
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 );
970 FREE( t1 );
971 FREE( t2 );
973 s = p;
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++ );
979 if( !*s )
980 Fatal( "Syntax error in macro. [$%s].\n", start );
981 if( *s == ':' ) s++;
982 break;
984 case 'T':
985 case 't':
986 case '^':
987 case '+':
988 if( modifier_list ) {
989 Warning( "Tokenize modifier must appear alone, ignored");
990 modifier_list = 0;
992 else {
993 separator = *s++;
995 if( separator == '$' ) {
996 p = _scan_ballanced_parens(s,'\0');
998 if ( *p ) {
999 char *tmp;
1000 pat1 = Expand(tmp = DmSubStr(s-1,p));
1001 FREE(tmp);
1002 result = Tokenize(result, pat1, switch_char, TRUE);
1003 FREE(pat1);
1005 else {
1006 Warning( "Incomplete macro expression [%s]", s );
1008 s = p;
1010 else if ( separator == '\"' ) {
1011 /* we change the semantics to allow $(v:t")") */
1012 for (p = s; *p && *p != separator; p++)
1013 if (*p == '\\')
1014 if (p[1] == '\\' || p[1] == '"')
1015 p++;
1017 if( *p == 0 )
1018 Fatal( "Unterminated separator string" );
1019 else {
1020 pat1 = DmSubStr( s, p );
1021 result = Tokenize( result, pat1, switch_char, TRUE);
1022 FREE( pat1 );
1024 s = p;
1026 else {
1027 Warning(
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++;
1037 break;
1039 case ':':
1040 if( modifier_list ) {
1041 result = Apply_modifiers( modifier_list, result );
1042 modifier_list = 0;
1044 break;
1046 default:
1047 Warning( "Illegal modifier in macro, ignored" );
1048 break;
1052 if( modifier_list ) /* apply modifier */
1053 result = Apply_modifiers( modifier_list, result );
1055 s++;
1058 *ps = s;
1059 FREE( macro_name );
1060 DB_RETURN( result );
1064 static char*
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. */
1074 char *s;
1075 char **ps;
1076 int *flag;
1078 char *t;
1079 char *start;
1080 char *res;
1081 int lev = 1;
1082 int done = 0;
1084 DB_ENTER( "_scan_brace" );
1086 start = s;
1087 while( !done )
1088 switch( *s++ ) {
1089 case '{':
1090 if( *s == '{' ) break; /* ignore {{ */
1091 lev++;
1092 break;
1094 case '}':
1095 if( *s == '}' ) break; /* ignore }} */
1096 if( lev )
1097 if( --lev == 0 ) done = TRUE;
1098 break;
1100 case '$':
1101 if( *s == '{' || *s == '}' ) {
1102 if( (t = strchr(s,'}')) != NIL(char) )
1103 s = t;
1104 s++;
1106 break;
1108 case '\0':
1109 if( lev ) {
1110 done = TRUE;
1111 s--;
1112 /* error malformed macro expansion */
1114 break;
1117 start = DmSubStr( start, (lev) ? s : s-1 );
1119 if( lev ) {
1120 /* Braces were not ballanced so just return the string.
1121 * Do not expand it. */
1123 res = DmStrJoin( "{", start, -1, FALSE );
1124 *flag = 0;
1126 else {
1127 *flag = 1;
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 */
1137 *ps = s;
1139 DB_RETURN( res );
1143 static char*
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) */
1153 char *x;
1154 char *y;
1156 static char *buf = NULL;
1157 static int buf_siz = 0;
1158 char *brkx;
1159 char *brky;
1160 char *cy;
1161 char *cx;
1162 char *res;
1163 int i;
1165 if( *x && *y ) {
1166 res = DmStrDup( "" ); cx = x;
1167 while( *cx ) {
1168 cy = y;
1169 brkx = DmStrPbrk( cx, " \t\n" );
1170 if( (brkx-cx == 2) && *cx == '\"' && *(cx+1) == '\"' ) cx = brkx;
1172 while( *cy ) {
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();
1180 buf_siz = i;
1183 strncpy( buf, cx, (i = brkx-cx) );
1184 buf[i] = '\0';
1185 if (brky-cy > 0) strncat( buf, cy, brky-cy );
1186 buf[i+(brky-cy)] = '\0';
1187 strcat( buf, " " );
1188 res = DmStrJoin( res, buf, -1, TRUE );
1189 cy = DmStrSpn( brky, " \t\n" );
1191 cx = DmStrSpn( brkx, " \t\n" );
1194 FREE( x );
1195 res[ strlen(res)-1 ] = '\0';
1197 else
1198 res = DmStrJoin( x, y, -1, TRUE );
1200 FREE( y );
1201 return( res );