home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / DMAKE38A.ZIP / EXPAND.C < prev    next >
C/C++ Source or Header  |  1992-01-23  |  25KB  |  922 lines

  1. /* RCS      -- $Header: /u2/dvadura/src/generic/dmake/src/RCS/expand.c,v 1.1 1992/01/24 03:27:03 dvadura Exp $
  2. -- SYNOPSIS -- macro expansion code.
  3. -- 
  4. -- DESCRIPTION
  5. --
  6. --    This routine handles all the necessary junk that deals with macro
  7. --    expansion.  It understands the following syntax.  If a macro is
  8. --    not defined it expands to NULL, and {} are synonyms for ().
  9. --
  10. --        $$      - expands to $
  11. --        {{      - expands to {
  12. --            }}      - expands to }
  13. --        $A      - expands to whatever the macro A is defined as
  14. --        $(AA)   - expands to whatever the macro AA is defined as
  15. --        $($(A)) - represents macro indirection
  16. --        <+...+> - get mapped to $(mktmp ...)
  17. --    
  18. --        following macro is recognized
  19. --        
  20. --                string1{ token_list }string2
  21. --                
  22. --        and expands to string1 prepended to each element of token_list and
  23. --        string2 appended to each of the resulting tokens from the first
  24. --        operation.  If string2 is of the form above then the result is
  25. --        the cross product of the specified (possibly modified) token_lists.
  26. --        
  27. --        The folowing macro modifiers are defined and expanded:
  28. --        
  29. --               $(macro:modifier_list:modifier_list:...)
  30. --               
  31. --        where modifier_list a combination of:
  32. --        
  33. --               D or d      - Directory portion of token including separator
  34. --               F or f      - File portion of token including suffix
  35. --               B or b      - basename portion of token not including suffix
  36. --         T or t      - for tokenization
  37. --
  38. --      or a single
  39. --               S or s      - pattern substitution (simple)
  40. --               
  41. --        NOTE:  Modifiers are applied once the macro value has been found.
  42. --               Thus the construct $($(test):s/joe/mary/) is defined and
  43. --               modifies the value of $($(test))
  44. --
  45. --           Also the construct $(m:d:f) is not the same as $(m:df)
  46. --           the first applies d to the value of $(m) and then
  47. --           applies f to the value of that whereas the second form
  48. --           applies df to the value of $(m).
  49. --
  50. -- AUTHOR
  51. --      Dennis Vadura, dvadura@watdragon.uwaterloo.ca
  52. --      CS DEPT, University of Waterloo, Waterloo, Ont., Canada
  53. --
  54. -- COPYRIGHT
  55. --      Copyright (c) 1990 by Dennis Vadura.  All rights reserved.
  56. -- 
  57. --      This program is free software; you can redistribute it and/or
  58. --      modify it under the terms of the GNU General Public License
  59. --      (version 1), as published by the Free Software Foundation, and
  60. --      found in the file 'LICENSE' included with this distribution.
  61. -- 
  62. --      This program is distributed in the hope that it will be useful,
  63. --      but WITHOUT ANY WARRANTY; without even the implied warrant of
  64. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  65. --      GNU General Public License for more details.
  66. -- 
  67. --      You should have received a copy of the GNU General Public License
  68. --      along with this program;  if not, write to the Free Software
  69. --      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  70. --
  71. -- LOG
  72. --     $Log: expand.c,v $
  73.  * Revision 1.1  1992/01/24  03:27:03  dvadura
  74.  * dmake Version 3.8, Initial revision
  75.  *
  76. */
  77.  
  78. #include "extern.h"
  79.  
  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(_MSC_VER)
  84. #include "optoff.h"
  85. #endif
  86.  
  87. static    char*    _scan_token ANSI((char*, char**));
  88. static    char*    _scan_macro ANSI((char*, char**));
  89. static    char*    _scan_brace ANSI((char*, char**, int*));
  90. static    char*    _cross_prod ANSI((char*, char*));
  91.  
  92.  
  93. PUBLIC char *
  94. Expand( src )/*
  95. ===============
  96.       This is the driver routine for the expansion, it identifies non-white
  97.       space tokens and gets the _scan_token routine to figure out if they should
  98.       be treated in a special way. */
  99.  
  100. char *src;                    /* pointer to source string  */
  101. {
  102.    char  *tmp;              /* pointer to temporary str  */
  103.    char  *res;                /* pointer to result string  */
  104.    char  *start;              /* pointer to start of token */
  105.    
  106.    DB_ENTER( "Expand" );
  107.    DB_PRINT( "exp", ("Expanding [%s]", src) );
  108.  
  109.    res = _strdup( "" );
  110.    if( src == NIL(char) ) DB_RETURN( res );
  111.  
  112.    while( *src ) {
  113.       char *ks, *ke;
  114.  
  115.       /* Here we find the next non white space token in the string
  116.        * and find it's end, with respect to non-significant white space. */
  117.       
  118.       start = _strspn( src, " \t\n" );
  119.       res   = _strjoin( res, src, start-src, TRUE );
  120.       if( !(*start) ) break;
  121.  
  122.       /* START <+...+> KLUDGE */
  123.       if( (ks=_strstr(start,"<+")) && (ke=_strstr(ks,"+>")) ){
  124.      char *t1, *t2;
  125.  
  126.      res = _strjoin( res, t2=Expand(t1=_substr(start,ks)), -1, TRUE);
  127.      FREE(t1); FREE(t2);
  128.  
  129.      t1 = _substr(ks+2, ke+1); t1[ke-ks-2] = ')';
  130.      t2 = _strjoin( "$(mktmp ", t1, -1,FALSE);
  131.      FREE(t1);
  132.      res = _strjoin( res, t2=Expand(t2), -1, TRUE);
  133.      FREE(t2);
  134.      src = ke+2;
  135.       }
  136.       /* END <+...+> KLUDGE */
  137.       else {
  138.      res   = _strjoin( res, tmp = _scan_token( start, &src ), -1, TRUE );
  139.      FREE( tmp );
  140.       }
  141.    }
  142.    
  143.    DB_PRINT( "exp", ("Returning [%s]", res) );
  144.    DB_RETURN( res );
  145. }
  146.  
  147.  
  148. PUBLIC char *
  149. Apply_edit( src, pat, subst, fr, anchor )/*
  150. ===========================================
  151.    Take the src string and apply the pattern substitution.  ie. look for
  152.    occurrences of pat in src and replace each occurrence with subst.  This is
  153.    NOT a regular expressions pattern substitution, it's just not worth it.
  154.    
  155.    if anchor == TRUE then the src pattern match must be at the end of a token.
  156.    ie. this is for SYSV compatibility and is only used for substitutions of
  157.    the caused by $(macro:pat=sub).  So if src = "fre.o.k june.o" then
  158.    $(src:.o=.a) results in "fre.o.k june.a", and $(src:s/.o/.a) results in
  159.    "fre.a.k june.a" */
  160.  
  161. char *src;            /* the source string */
  162. char *pat;            /* pattern to find   */
  163. char *subst;            /* substitute string */
  164. int   fr;            /* if TRUE free src  */
  165. int   anchor;            /* if TRUE anchor    */
  166. {
  167.    char *res;
  168.    char *p;
  169.    char *s;
  170.    int   l;
  171.  
  172.    DB_ENTER( "Apply_edit" );
  173.    
  174.    if( !*pat ) DB_RETURN( src );        /* do nothing if pat is NULL */
  175.  
  176.    DB_PRINT( "mod", ("Source str:  [%s]", src) );
  177.    DB_PRINT( "mod", ("Replacing [%s], with [%s]", pat, subst) );
  178.  
  179.    s   = src;
  180.    l   = strlen( pat );
  181.    if( (p = _strstr( s, pat )) != NIL(char) ) {
  182.       res = _strdup( "" );
  183.       do {
  184.      if( anchor )
  185.         if( !*(p+l) || (strchr(" \t", *(p+l)) != NIL(char)) )
  186.            res = _strjoin( _strjoin( res, s, p-s, TRUE ), subst, -1, TRUE );
  187.         else
  188.            res = _strjoin( res, s, p+l-s, TRUE );
  189.      else
  190.         res = _strjoin( _strjoin( res, s, p-s, TRUE ), subst, -1, TRUE );
  191.  
  192.      s   = p + l;
  193.       }
  194.       while( (p = _strstr( s, pat )) != NIL(char) );
  195.  
  196.       res = _strjoin( res, s, -1, TRUE );
  197.       if( fr ) FREE( src );
  198.    }
  199.    else
  200.       res = src;
  201.  
  202.  
  203.    DB_PRINT( "mod", ("Result [%s]", res) );
  204.    DB_RETURN( res );
  205. }
  206.  
  207.  
  208. PUBLIC void
  209. Map_esc( tok )/*
  210. ================
  211.    Map an escape sequence and replace it by it's corresponding character
  212.    value.  It is assumed that tok points at the initial \, the esc
  213.    sequence in the original string is replaced and the value of tok
  214.    is not modified. */
  215. char *tok;
  216. {
  217.    if( strchr( "\"\\vantbrf01234567", tok[1] ) ) {
  218.       switch( tok[1] ) {
  219.      case 'a' : *tok = 0x07; break;
  220.      case 'b' : *tok = '\b'; break;
  221.      case 'f' : *tok = '\f'; break;
  222.      case 'n' : *tok = '\n'; break;
  223.      case 'r' : *tok = '\r'; break;
  224.      case 't' : *tok = '\t'; break;
  225.      case 'v' : *tok = 0x0b; break;
  226.      case '\\': *tok = '\\'; break;
  227.      case '\"': *tok = '\"'; break;
  228.  
  229.      default: {
  230.         register int i = 0;
  231.         register int j = 0;
  232.         for( ; i<2 && isdigit(tok[2]); i++ ) {
  233.            j = (j << 3) + (tok[1] - '0');
  234.            strcpy( tok+1, tok+2 );
  235.         }
  236.         j = (j << 3) + (tok[1] - '0');
  237.         *tok = j;
  238.      }
  239.       }
  240.       strcpy( tok+1, tok+2 );
  241.    }
  242. }
  243.  
  244.  
  245. PUBLIC char*
  246. Apply_modifiers( mod, src )/*
  247. =============================
  248.    This routine applies the appropriate modifiers to the string src
  249.    and returns the proper result string */
  250.  
  251. int  mod;
  252. char *src;
  253. {
  254.    char       *s;
  255.    char    *e;
  256.    TKSTR   str;
  257.  
  258.    DB_ENTER( "Apply_modifiers" );
  259.  
  260.    if( mod == (SUFFIX_FLAG | DIRECTORY_FLAG | FILE_FLAG) )
  261.       DB_RETURN( src );
  262.  
  263.    SET_TOKEN( &str, src );
  264.    DB_PRINT( "mod", ("Source string [%s]", src) );
  265.  
  266.    while( *(s = Get_token( &str, "", FALSE )) != '\0' ) {
  267.       /* search for the directory portion of the filename.  If the
  268.        * DIRECTORY_FLAG is set, then we want to keep the directory portion
  269.        * othewise throw it away and blank out to the end of the token */
  270.  
  271.       if( (e = basename(s)) != s)
  272.      if( !(mod & DIRECTORY_FLAG) ) {
  273.         strcpy(s, e);
  274.         e = s+(str.tk_str-e);
  275.         for(; e != str.tk_str; e++)
  276.                *e = ' ';
  277.      }
  278.      else
  279.         s = e;
  280.  
  281.       /* search for the suffix, if there is none, treat it as a NULL suffix.
  282.        * if no file name treat it as a NULL file name.  same copy op as
  283.        * for directory case above */
  284.  
  285.       e = strrchr( s, '.' );            /* NULL suffix if e=0 */
  286.       if( e == NIL(char) ) e = s+strlen(s);
  287.  
  288.       if( !(mod & FILE_FLAG) ) {
  289.      strcpy( s, e );
  290.      e = s+(str.tk_str-e);
  291.      for( ; e != str.tk_str; e++ ) *e = ' ';
  292.       }
  293.       else
  294.      s = e;
  295.  
  296.       /* The last and final part.  This is the suffix case, if we don't want
  297.        * it then just erase to the end of the token. */
  298.  
  299.       if( s != NIL(char) )
  300.      if( !(mod & SUFFIX_FLAG) )
  301.         for( ; s != str.tk_str; s++ ) *s = ' ';
  302.    }
  303.  
  304.    /* delete the extra white space, it looks ugly */
  305.    for( s = src, e = NIL(char); *s; s++ )
  306.       if( *s == ' ' || *s == '\t' || *s == '\n' ) {
  307.      if( e == NIL(char) )
  308.         e = s;
  309.       }
  310.       else {
  311.      if( e != NIL(char) ) {
  312.         if( e+1 < s ) {
  313.            strcpy( e+1, s );
  314.            s = e+1;
  315.            *e = ' ';
  316.         }
  317.         e = NIL(char);
  318.      }
  319.       }
  320.  
  321.    if( e != NIL(char) )
  322.       if( e < s )
  323.      strcpy( e, s );
  324.  
  325.    DB_PRINT( "mod", ("Result string [%s]", src) );
  326.    DB_RETURN( src );
  327. }
  328.  
  329.  
  330. PUBLIC char*
  331. Tokenize( src, separator )/*
  332. ============================
  333.     Tokenize the input of src and join each token found together with
  334.     the next token separated by the separator string.
  335.  
  336.     When doing the tokenization, <sp>, <tab>, <nl>, and \<nl> all
  337.     constitute white space. */
  338.  
  339. char *src;
  340. char *separator;
  341. {
  342.    TKSTR    tokens;
  343.    char        *tok;
  344.    char        *res;
  345.    int        first = TRUE;
  346.  
  347.    DB_ENTER( "Tokenize" );
  348.  
  349.    SET_TOKEN( &tokens, src );
  350.  
  351.  
  352.    /* map the escape codes in the separator string first */
  353.  
  354.    for(tok=separator; (tok = strchr(tok,ESCAPE_CHAR)) != NIL(char); tok++)
  355.       Map_esc( tok );
  356.  
  357.    DB_PRINT( "exp", ("Separator [%s]", separator) );
  358.  
  359.    /* Build the token list */
  360.    res = _strdup( "" );
  361.    while( *(tok = Get_token( &tokens, "", FALSE )) != '\0' ) {
  362.       DB_PRINT( "exp", ("Tokenizing [%s]", tok) );
  363.  
  364.       if( first ) {
  365.      FREE( res );
  366.      res   = _strdup( tok );
  367.      first = FALSE;
  368.       }
  369.       else {
  370.            char *x;
  371.      res = _strjoin(res, x =_strjoin(separator, tok, -1, FALSE), -1, TRUE);
  372.      FREE( x );
  373.       }
  374.    }
  375.  
  376.    FREE( src );
  377.    DB_RETURN( res );
  378. }
  379.  
  380.  
  381. static char*
  382. _scan_token( s, ps )/*
  383. ======================
  384.       This routine scans the token characters one at a time and identifies
  385.       macros starting with $( and ${ and calls _scan_macro to expand their
  386.       value.   the string1{ token_list }string2 expansion is also handled.
  387.       In this case a temporary result is maintained so that we can take it's
  388.       cross product with any other token_lists that may possibly appear. */
  389.       
  390. char *s;        /* pointer to start of src string */
  391. char **ps;        /* pointer to start pointer      */
  392. {
  393.    char *res;                 /* pointer to result          */
  394.    char *start;               /* pointer to start of prefix */
  395.    int  crossproduct = 0;     /* if 1 then computing X-prod */
  396.  
  397.    start = s;
  398.    res   = _strdup( "" );
  399.    while( 1 )
  400.       switch( *s ) {
  401.          /* Termination, We halt at seeing a space or a tab or end of string.
  402.           * We return the value of the result with any new macro's we scanned
  403.           * or if we were computing cross_products then we return the new
  404.           * cross_product.
  405.           * NOTE:  Once we start computing cross products it is impossible to
  406.           *        stop.  ie. the semantics are such that once a {} pair is
  407.           *        seen we compute cross products until termination. */
  408.  
  409.          case ' ':
  410.          case '\t':
  411.      case '\n':
  412.          case '\0': 
  413.      {
  414.         char *tmp;
  415.  
  416.         *ps = s;
  417.         if( !crossproduct )
  418.            tmp = _strjoin( res, start, (s-start), TRUE );
  419.         else
  420.         {
  421.            tmp = _substr( start, s );
  422.            tmp = _cross_prod( res, tmp );
  423.         }
  424.         return( tmp );
  425.      }
  426.          
  427.          case '$':
  428.          case '{':
  429.      {
  430.         /* Handle if it's a macro or if it's a {} construct.
  431.          * The results of a macro expansion are handled differently based
  432.          * on whether we have seen a {} beforehand. */
  433.         
  434.         char *tmp;
  435.         tmp = _substr( start, s );          /* save the prefix */
  436.  
  437.         if( *s == '$' ) {
  438.            start = _scan_macro( s+1, &s );
  439.  
  440.            if( crossproduct )
  441.           res = _cross_prod( res, _strjoin( tmp, start, -1, TRUE ) );
  442.            else {
  443.           res = _strjoin(res,tmp = _strjoin(tmp,start,-1,TRUE),-1,TRUE);
  444.           FREE( tmp );
  445.            }
  446.            FREE( start );
  447.         }
  448.         else if( strchr("{ \t",s[1]) == NIL(char) ){
  449.            int ok;
  450.            start = _scan_brace( s+1, &s, &ok );
  451.           
  452.            if( ok ) {
  453.           res = _cross_prod( res, _cross_prod(tmp, start) );
  454.           crossproduct = TRUE;
  455.            }
  456.            else {
  457.           res =_strjoin(res,tmp=_strjoin(tmp,start,-1,TRUE),-1,TRUE);
  458.           FREE( start );
  459.           FREE( tmp   );
  460.            }
  461.         }
  462.         else {    /* handle the {{ case */
  463.            res = _strjoin( res, start, (s-start+1), TRUE );
  464.            s  += (s[1]=='{')?2:1;
  465.            FREE( tmp );
  466.         }
  467.  
  468.         start = s;
  469.      }
  470.      break;
  471.  
  472.      case '}':
  473.         if( s[1] != '}' ) {
  474.            /* error malformed macro expansion */
  475.            s++;
  476.         }
  477.         else {    /* handle the }} case */
  478.            res = _strjoin( res, start, (s-start+1), TRUE );
  479.            s += 2;
  480.            start = s;
  481.         }
  482.         break;
  483.          
  484.          default: s++;
  485.       }
  486. }
  487.  
  488.  
  489. static char*
  490. _scan_macro( s, ps )/*
  491. ======================
  492.     This routine scans a macro use and expands it to the value.  It
  493.     returns the macro's expanded value and modifies the pointer into the
  494.     src string to point at the first character after the macro use.
  495.     The types of uses recognized are:
  496.  
  497.         $$        - expands to $
  498.         $(name)        - expands to value of name
  499.         ${name}        - same as above
  500.         $($(name))    - recurses on macro names (any level)
  501.     and
  502.         $(func[,args ...] [data])
  503.     and 
  504.             $(name:modifier_list:modifier_list:...)
  505.         
  506.     see comment for Expand for description of valid modifiers.
  507.  
  508.     NOTE that once a macro name bounded by ( or { is found only
  509.     the appropriate terminator (ie. ( or } is searched for. */
  510.  
  511. char *s;        /* pointer to start of src string   */
  512. char **ps;        /* pointer to start pointer        */
  513. {
  514.    char sdelim;         /* start of macro delimiter         */
  515.    char edelim;         /* corresponding end macro delim    */
  516.    char *start;         /* start of prefix                  */
  517.    char *macro_name;    /* temporary macro name             */
  518.    char *recurse_name;  /* recursive macro name             */
  519.    char *result;    /* result for macro expansion        */
  520.    int  bflag = 0;      /* brace flag, ==0 => $A type macro */
  521.    int  done  = 0;      /* != 0 => done macro search        */
  522.    int  lev   = 0;      /* brace level                      */
  523.    int  mflag = 0;      /* != 0 => modifiers present in mac */
  524.    int  fflag = 0;    /* != 0 => GNU style function         */
  525.    HASHPTR hp;        /* hash table pointer for macros    */
  526.    
  527.    DB_ENTER( "_scan_macro" );
  528.  
  529.    /* Check for the simple $ at end of line case */
  530.    if( !*s ) {
  531.       *ps = s;
  532.       DB_RETURN( _strdup("") );
  533.    }
  534.  
  535.    if( *s == '$' ) {    /* Take care of the simple $$ case. */
  536.       *ps = s+1;
  537.       DB_RETURN( _strdup("$") );
  538.    }
  539.  
  540.    sdelim = *s;         /* set and remember start/end delim */
  541.    if( sdelim == '(' )
  542.       edelim = ')';
  543.    else
  544.       edelim = '}';
  545.  
  546.    start = s;           /* build up macro name, find its end*/
  547.    while( !done ) {
  548.       switch( *s ) {
  549.          case '(':                /* open macro brace */
  550.          case '{':
  551.         if( *s == sdelim ) {
  552.            lev++;
  553.            bflag++;
  554.         }
  555.         break;
  556.          
  557.          case ':':                              /* halt at modifier */
  558.             if( lev == 1 ) {
  559.                done = TRUE;
  560.                mflag = 1;
  561.             }
  562.             break;
  563.  
  564.      case ' ':
  565.      case '\t':
  566.      case '\n':
  567.         fflag = 1;
  568.         break;
  569.             
  570.      case '\0':                /* check for null */
  571.         *ps = s;
  572.         if( lev ) {
  573.            done  = TRUE;
  574.            bflag = 0;
  575.            s     = start;
  576.         }
  577.         break;
  578.          
  579.          case ')':                /* close macro brace */
  580.          case '}':
  581.         if( *s == edelim && lev ) --lev;
  582.         /*FALLTHROUGH*/
  583.  
  584.          default:
  585.         done = !lev;
  586.       }
  587.       s++;
  588.    }
  589.  
  590.    /* Check if this is a $A type macro.  If so then we have to
  591.     * handle it a little differently. */
  592.    if( bflag )
  593.       macro_name = _substr( start+1, s-1 );
  594.    else
  595.       macro_name = _substr( start, s );
  596.  
  597.    /* Check to see if the macro name contains spaces, if so then treat it
  598.     * as a GNU style function invocation and call the function mapper to
  599.     * deal with it. */
  600.    if( fflag )
  601.       result = Exec_function(macro_name);
  602.    else {
  603.       /* Check if the macro is a recursive macro name, if so then
  604.        * EXPAND the name before expanding the value */
  605.       if( strchr( macro_name, '$' ) != NIL(char) ) {
  606.      recurse_name = Expand( macro_name );
  607.      FREE( macro_name );
  608.      macro_name = recurse_name;
  609.       }
  610.  
  611.       /* Code to do value expansion goes here, NOTE:  macros whose assign bit
  612.      is one have been evaluated and assigned, they contain no further
  613.      expansions and thus do not need their values expanded again. */
  614.  
  615.       if( (hp = GET_MACRO( macro_name )) != NIL(HASH) ) {
  616.      if( hp->ht_flag & M_MARK )
  617.         Fatal( "Detected circular macro [%s]", hp->ht_name );
  618.  
  619.      /* for M_MULTI macro variable assignments */
  620.      If_multi = hp->ht_flag & M_MULTI;
  621.  
  622.      if( !(hp->ht_flag & M_EXPANDED) ) {
  623.         hp->ht_flag |= M_MARK;
  624.         result = Expand( hp->ht_value );
  625.         hp->ht_flag ^= M_MARK;
  626.      }
  627.      else if( hp->ht_value != NIL(char) )
  628.         result = _strdup( hp->ht_value );
  629.      else
  630.         result = _strdup( "" );
  631.  
  632.      /*
  633.       * Mark macros as used only if we are not expanding them for
  634.       * the purpose of a .IF test, so we can warn about redef after use*/
  635.  
  636.      if( !If_expand ) hp->ht_flag |= M_USED;
  637.       }
  638.       else
  639.      result = _strdup( "" );
  640.    }
  641.  
  642.    if( mflag ) {
  643.       char separator;
  644.       int  modifier_list = 0;
  645.       int  aug_mod       = FALSE;
  646.       char *pat1;
  647.       char *pat2;
  648.       char *p;
  649.  
  650.       /* Yet another brain damaged AUGMAKE kludge.  We should accept the 
  651.        * AUGMAKE bullshit of $(f:pat=sub) form of macro expansion.  In
  652.        * order to do this we will forgo the normal processing if the
  653.        * AUGMAKE solution pans out, otherwise we will try to process the
  654.        * modifiers ala dmake.
  655.        *
  656.        * So we look for = in modifier string.
  657.        * If found we process it and not do the normal stuff */
  658.  
  659.       for( p=s; *p && *p != '=' && *p != edelim; p++ );
  660.  
  661.       if( *p == '=' ) {
  662.      pat1 = _substr( s, p );
  663.      for( s=p=p+1; (*p != edelim); p++ );
  664.  
  665.      pat2 = _substr( s, p );
  666.  
  667.      if( !Augmake ) {
  668.         char *tmp = pat2;
  669.         pat2 = Expand(pat2);
  670.         FREE(tmp);
  671.      }
  672.  
  673.      result = Apply_edit( result, pat1, pat2, TRUE, TRUE );
  674.      FREE( pat1 );
  675.      FREE( pat2 );
  676.      s = p;
  677.      aug_mod = TRUE;
  678.       }
  679.  
  680.       if( !aug_mod )
  681.      while( *s && *s != edelim ) {        /* while not at end of macro */
  682.         switch( *s++ ) {
  683.            case 'b':
  684.            case 'B': modifier_list |= FILE_FLAG;            break;
  685.  
  686.            case 'd':
  687.            case 'D': modifier_list |= DIRECTORY_FLAG;         break;
  688.  
  689.            case 'f':
  690.            case 'F': modifier_list |= FILE_FLAG | SUFFIX_FLAG; break;
  691.  
  692.            case 'S':
  693.            case 's':
  694.           if( modifier_list ) {
  695.              Warning( "Edit modifier must appear alone, ignored");
  696.              modifier_list = 0;
  697.           }
  698.           else {
  699.              separator = *s++;
  700.              for( p=s; *p != separator && *p != edelim; p++ );
  701.  
  702.              if( *p == edelim )
  703.                 Warning("Syntax error in edit pattern, ignored");
  704.              else {
  705.             char *t1, *t2;
  706.             pat1 = _substr( s, p );
  707.             for(s=p=p+1; (*p != separator) && (*p != edelim); p++ );
  708.             pat2 = _substr( s, p );
  709.             t1 = Expand(pat1); FREE(pat1);
  710.             t2 = Expand(pat2); FREE(pat2);
  711.             result = Apply_edit( result, t1, t2, TRUE, FALSE );
  712.             FREE( t1 );
  713.             FREE( t2 );
  714.              }
  715.              s = p;
  716.           }
  717.           /* find the end of the macro spec, or the start of a new
  718.            * modifier list for further processing of the result */
  719.  
  720.           for( ; (*s != edelim) && (*s != ':'); s++ );
  721.           if( *s == ':' ) s++;
  722.           break;
  723.  
  724.            case 'T':
  725.            case 't':
  726.           if( modifier_list ) {
  727.              Warning( "Tokenize modifier must appear alone, ignored");
  728.              modifier_list = 0;
  729.           }
  730.           else {
  731.              char *msg = "Separator string must be quoted";
  732.  
  733.              separator = *s++;
  734.  
  735.              if( separator != '\"' )
  736.             Warning( msg );
  737.              else {
  738.             /* we change the semantics to allow $(v:t")") */
  739.             for (p = s; *p && *p != separator; p++)
  740.                if (*p == '\\')
  741.                   if (p[1] == '\\' || p[1] == '"')
  742.                  p++;
  743.             if( *p == 0 )
  744.                Fatal( "Unterminated separator string" );
  745.             else {
  746.                pat1 = _substr( s, p );
  747.                result = Tokenize( result, pat1 );
  748.                FREE( pat1 );
  749.             }
  750.             s = p;
  751.              }
  752.  
  753.              /* find the end of the macro spec, or the start of a new
  754.               * modifier list for further processing of the result */
  755.  
  756.              for( ; (*s != edelim) && (*s != ':'); s++ );
  757.              if( *s == ':' ) s++;
  758.           }
  759.           break;
  760.  
  761.            case ':':
  762.           if( modifier_list ) {
  763.              result = Apply_modifiers( modifier_list, result );
  764.              modifier_list = 0;
  765.           }
  766.           break;
  767.  
  768.            default:
  769.           Warning( "Illegal modifier in macro, ignored" );
  770.           break;
  771.         }
  772.      }
  773.  
  774.       if( modifier_list ) /* apply modifier */
  775.          result = Apply_modifiers( modifier_list, result );
  776.       
  777.       s++;
  778.    }
  779.  
  780.    *ps = s;
  781.    FREE( macro_name );
  782.    DB_RETURN( result );
  783. }
  784.  
  785.  
  786. static char*
  787. _scan_brace( s, ps, flag )/*
  788. ============================
  789.       This routine scans for { token_list } pairs.  It expands the value of
  790.       token_list by calling Expand on it.  Token_list may be anything at all.
  791.       Note that the routine count's ballanced parentheses.  This means you
  792.       cannot have something like { fred { joe }, if that is what you really
  793.       need the write it as { fred {{ joe }, flag is set to 1 if all ok
  794.       and to 0 if the braces were unballanced. */
  795.       
  796. char *s;
  797. char **ps;
  798. int  *flag;
  799. {
  800.    char *t;
  801.    char *start;
  802.    char *res;
  803.    int  lev  = 1;
  804.    int  done = 0;
  805.    
  806.    DB_ENTER( "_scan_brace" );
  807.  
  808.    start = s;
  809.    while( !done )
  810.       switch( *s++ ) {
  811.          case '{': 
  812.             if( *s == '{' ) break;              /* ignore {{ */
  813.             lev++;
  814.             break;
  815.             
  816.          case '}': 
  817.             if( *s == '}' ) break;              /* ignore }} */
  818.         if( lev )
  819.            if( --lev == 0 ) done = TRUE;
  820.         break;
  821.  
  822.      case '$':
  823.         if( *s == '{' || *s == '}' ) {
  824.           if( (t = strchr(s,'}')) != NIL(char) )
  825.              s = t;
  826.           s++;
  827.         }
  828.         break;
  829.          
  830.          case '\0':
  831.         if( lev ) {
  832.            done = TRUE;
  833.            s--;
  834.            /* error malformed macro expansion */
  835.         }
  836.         break;
  837.       }
  838.  
  839.    start = _substr( start, (lev) ? s : s-1 );
  840.  
  841.    if( lev ) {
  842.       /* Braces were not ballanced so just return the string.
  843.        * Do not expand it. */
  844.        
  845.       res   = _strjoin( "{", start, -1, FALSE );
  846.       *flag = 0;
  847.    }
  848.    else {
  849.       *flag = 1;
  850.       res   = Expand( start );
  851.  
  852.       if( (t = _strspn( res, " \t" )) != res ) strcpy( res, t );
  853.    }
  854.  
  855.    FREE( start );       /* this is ok! start is assigned a _substr above */
  856.    *ps = s;
  857.  
  858.    DB_RETURN( res );
  859. }
  860.  
  861.  
  862. static char*
  863. _cross_prod( x, y )/*
  864. =====================
  865.       Given two strings x and y compute the cross-product of the tokens found
  866.       in each string.  ie. if x = "a b" and y = "c d" return "ac ad bc bd".
  867.  
  868.          NOTE:  buf will continue to grow until it is big enough to handle
  869.                 all cross product requests.  It is never freed!  (maybe I
  870.             will fix this someday) */
  871.       
  872. char *x;
  873. char *y;
  874. {
  875.    static char *buf;
  876.    static int  buf_siz = 0;
  877.    char *brkx;
  878.    char *brky;
  879.    char *cy;
  880.    char *cx;
  881.    char *res;
  882.    int  i;
  883.  
  884.    if( *x && *y ) {
  885.       res = _strdup( "" ); cx = x;
  886.       while( *cx ) {
  887.      cy = y;
  888.          brkx = _strpbrk( cx, " \t\n" );
  889.      if( (brkx-cx == 2) && *cx == '\"' && *(cx+1) == '\"' ) cx = brkx;
  890.  
  891.      while( *cy ) {
  892.         brky = _strpbrk( cy, " \t\n" );
  893.         if( (brky-cy == 2) && *cy == '\"' && *(cy+1) == '\"' ) cy = brky;
  894.         i    = brkx-cx + brky-cy + 2;
  895.  
  896.         if( i > buf_siz ) {        /* grow buf to the correct size */
  897.            if( buf != NIL(char) ) FREE( buf );
  898.            if( (buf = MALLOC( i, char )) == NIL(char))  No_ram();
  899.            buf_siz = i;
  900.         }
  901.  
  902.         strncpy( buf, cx, (i = brkx-cx) );
  903.         buf[i] = '\0';
  904.         if (brky-cy > 0) strncat( buf, cy, brky-cy );
  905.         buf[i+(brky-cy)] = '\0';
  906.         strcat( buf, " " );
  907.         res = _strjoin( res, buf, -1, TRUE );
  908.         cy = _strspn( brky, " \t\n" );
  909.      }
  910.      cx = _strspn( brkx, " \t\n" );
  911.       }
  912.  
  913.       FREE( x );
  914.       res[ strlen(res)-1 ] = '\0';
  915.    }
  916.    else
  917.       res = _strjoin( x, y, -1, TRUE );
  918.  
  919.    FREE( y );
  920.    return( res );
  921. }
  922.