home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d045 / make.lha / Make / macro.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-12-03  |  6.3 KB  |  225 lines

  1. #include "make.h"
  2.  
  3. char *stpchr();
  4.  
  5. ExpandMacros( source, target, maxlen, tnode, newer)
  6. char *source;
  7. char *target;
  8. int maxlen;
  9. TNODE *tnode;
  10. STRNODE *newer;
  11.    {
  12.    char *subs, *sp, tbuf[MAXBUFF], *p;
  13.    STRNODE *strptr;
  14.    char macrotype, specialpart, *lastdot;
  15.    int len , tocopy;
  16.  
  17.    while( (p=stpchr(source, ESCAPE)) != NULL)
  18.       {
  19.       tstbreak();
  20.  
  21.       tocopy = p - source;
  22.       if (tocopy > maxlen) tocopy = maxlen;
  23.       movmem(source, target, tocopy);
  24.       maxlen -= tocopy;
  25.       target += tocopy;
  26.  
  27.       source = p + 1;
  28.  
  29.       macrotype = 0;
  30.       specialpart = 0;
  31.  
  32.       if (stpchr("(*@<?%+", *source) != NULL)
  33.          {
  34.          if ((macrotype = *source) == '(')
  35.             {
  36.             /* special modifier macro? */
  37.             if (stpchr("*@<?%+", *++source) != NULL)
  38.                {
  39.                macrotype = *source++;
  40.                if (*source != ')')
  41.                   {
  42.                   specialpart = *source++;
  43.                   if ((specialpart != 'D' && specialpart != 'F') ||
  44.                       *source != ')' || macrotype == '?')
  45.                      err("invalid macro call '%s'\n",source-4);
  46.                   }
  47.                }
  48.             else
  49.                {
  50.                /* just a normal macro name, so locate it */
  51.                if ( (p=stpchr(source, ')')) == NULL)
  52.                   err("Missing ')' on macro '%s'", source-2);
  53.                }
  54.             }
  55.  
  56.          /* now process the macro we have */
  57. #ifdef DEBUG
  58.          if (DebugMode)
  59.             {
  60.             msg("Macro expanpanding macro type '%lc'\n", macrotype);
  61.             if (macrotype == '(')
  62.                {
  63.                *p = 0;
  64.                msg("      for '%s'\n", source);
  65.                *p = ')';
  66.                }
  67.             }
  68. #endif
  69.          switch( macrotype )
  70.             {
  71.             case '(':  /* normal macro expansion */
  72.                *p=0;    /* temporarily null out end of macro */
  73.                if ((subs = (char *)find(source, Macros)) != NULL)
  74.                   {
  75.                   ExpandMacros(subs, target, maxlen, tnode, newer);
  76.                   len = strlen(target);
  77.                   target += len;
  78.                   maxlen -= len;
  79.                   }
  80.                *p=')';  /* restore the parenthesis we nulled out */
  81.                source = p;
  82.                break;
  83.  
  84.             case '@':  /* name of current target */
  85.                /* expand any macros in the name over */
  86.                /* change so we pass the name later on */
  87.                SimpleMacro(tnode->targets->string, tbuf, MAXBUFF);
  88.  
  89.                /* copy over the result to the targetut string */
  90.                for (sp=tbuf; *sp && maxlen; *target++=*sp++, maxlen--);
  91.                break;
  92.  
  93.             case '*':  /* root of current target */
  94.                /* this is the same as $@ without the .c or whatever */
  95.                /* expand any macros in the name over */
  96.                /* change so we pass the name later on */
  97.                SimpleMacro(tnode->targets->string, tbuf, MAXBUFF);
  98.  
  99.                /* copy over the result to the target string */
  100.                lastdot = NULL;
  101.                for (sp=tbuf; *sp && maxlen; *target++=*sp++, maxlen--)
  102.                   if (*sp == '.')
  103.                      lastdot = target;
  104.  
  105.                /* back it up to the last . found to strip off the suffix */
  106.                if (lastdot != NULL)
  107.                   {
  108.                   maxlen += (target-lastdot);
  109.                   target = lastdot;
  110.                   }
  111.                break;
  112.  
  113.             case '<':  /* current constructed target */
  114.                /* expand any macros in the name over */
  115.                /* change so we pass the name later on */
  116.                SimpleMacro(tnode->depends->string, tbuf, MAXBUFF);
  117.  
  118.                /* copy over the result to the targetut string */
  119.                for (sp=tbuf; *sp && maxlen; *target++=*sp++, maxlen--);
  120.                break;
  121.  
  122.             case '?':  /* all modified sources for target */
  123.             case '+':  /* all sources for the current rule */
  124.                strptr = (macrotype == '?') ? newer : tnode->depends;
  125.  
  126.                /* copy over the sources one at a time */
  127.                for (; strptr != NULL && maxlen>0; strptr=strptr->next)
  128.                   {
  129.                   SimpleMacro(strptr->string, tbuf, MAXBUFF);
  130.                   for (sp=tbuf; *sp && maxlen; *target++=*sp++, maxlen--);
  131.  
  132.                   if (maxlen>0)
  133.                      {
  134.                      maxlen--;
  135.                      *target++ = ' ';
  136.                      }
  137.                   }
  138.                break;
  139.             default:
  140.                err("Invalid macro expansion '%lc'", macrotype);
  141.             }
  142.          source++;
  143.          }
  144.       else
  145.          {
  146.          /* copy over the single escaped character */
  147.          if (maxlen > 0)
  148.             {
  149.             *target++ = *source++;
  150.             maxlen--;
  151.             }
  152.          }
  153.       }
  154.  
  155.    /* copy over the remainder of the string */
  156.    stccpy(target, source, maxlen);
  157.    }
  158.  
  159.  
  160. /*-------------------------------------------------------------------------*/
  161. int SimpleMacro( source, target, bufflen )
  162. char *source;
  163. char *target;
  164. int  bufflen;
  165.    {
  166.    char *p, *subs;
  167.    int tocopy;
  168.    int len, maxlen;
  169.  
  170.  
  171.    maxlen = bufflen;
  172.  
  173. #ifdef DEBUG
  174.    if (DebugMode)
  175.       msg("Expanding '%s'\n", source);
  176. #endif
  177.    tstbreak();
  178.  
  179.    while ( (p=stpchr(source, ESCAPE)) != NULL)
  180.       {
  181.       tstbreak();
  182.  
  183.       tocopy = p - source;
  184.       if (tocopy > maxlen) tocopy = maxlen;
  185.       movmem(source,target,tocopy);
  186.       maxlen -= tocopy;
  187.       target += tocopy;
  188.  
  189.       source = p+1;
  190.  
  191.       if (*source == '(')
  192.          {
  193.          if ( (p=stpchr(++source, ')')) == NULL)
  194.             err("Missing ')' on macro '%s'", source-2);
  195.  
  196.          if (p != source)
  197.             {
  198.             *p=0;    /* temporarily null out end of macro */
  199.             if ((subs = (char *)find(source, Macros)) != NULL)
  200.                {
  201.                len = SimpleMacro(subs, target, maxlen);
  202.                target += len;
  203.                maxlen -= len;
  204.                }
  205.             *p=')';  /* restore the parenthesis we nulled out */
  206.             }
  207.          }
  208.       else
  209.          {
  210.          /* copy over the single escaped character */
  211.          if (maxlen > 0)
  212.             {
  213.             *target++ = *++p;
  214.             maxlen--;
  215.             }
  216.          }
  217.       source = p+1;
  218.       }
  219.  
  220.    /* copy over the remainder of the string */
  221.    maxlen -= (stccpy(target, source, maxlen) - 1);
  222.  
  223.    return(bufflen-maxlen);
  224.    }
  225.