home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / tools / make / make_pd / macro.c < prev    next >
C/C++ Source or Header  |  1987-01-10  |  3KB  |  157 lines

  1. /*
  2.  *    Macro control for make
  3.  */
  4.  
  5.  
  6. #include "h.h"
  7.  
  8.  
  9. struct macro *        macrohead;
  10.  
  11.  
  12. struct macro *
  13. getmp(name)
  14. char *            name;
  15. {
  16.     register struct macro *    rp;
  17.  
  18.     for (rp = macrohead; rp; rp = rp->m_next)
  19.         if (strcmp(name, rp->m_name) == 0)
  20.             return rp;
  21.     return (struct macro *)0;
  22. }
  23.  
  24.  
  25. char *
  26. getmacro(name)
  27. char *            name;
  28. {
  29.     struct macro *        mp;
  30.  
  31.     if (mp = getmp(name))
  32.         return mp->m_val;
  33.     else
  34.         return "";
  35. }
  36.  
  37.  
  38. struct macro *
  39. setmacro(name, val)
  40. char *            name;
  41. char *            val;
  42. {
  43.     register struct macro *    rp;
  44.     register char *        cp;
  45.  
  46.  
  47.             /*  Replace macro definition if it exists  */
  48.     for (rp = macrohead; rp; rp = rp->m_next)
  49.         if (strcmp(name, rp->m_name) == 0)
  50.         {
  51.             free(rp->m_val);    /*  Free space from old  */
  52.             break;
  53.         }
  54.  
  55.     if (!rp)        /*  If not defined, allocate space for new  */
  56.     {
  57.         if ((rp = (struct macro *)malloc(sizeof (struct macro)))
  58.                      == (struct macro *)0)
  59.             fatal("No memory for macro");
  60.  
  61.         rp->m_next = macrohead;
  62.         macrohead = rp;
  63.         rp->m_flag = FALSE;
  64.  
  65.         if ((cp = malloc(strlen(name)+1)) == (char *)0)
  66.             fatal("No memory for macro");
  67.         strcpy(cp, name);
  68.         rp->m_name = cp;
  69.     }
  70.  
  71.     if ((cp = malloc(strlen(val)+1)) == (char *)0)
  72.         fatal("No memory for macro");
  73.     strcpy(cp, val);        /*  Copy in new value  */
  74.     rp->m_val = cp;
  75.  
  76.     return rp;
  77. }
  78.  
  79.  
  80. /*
  81.  *    Do the dirty work for expand
  82.  */
  83. void
  84. doexp(to, from, len, buf)
  85. char **            to;
  86. char *            from;
  87. int *            len;
  88. char *            buf;
  89. {
  90.     register char *        rp;
  91.     register char *        p;
  92.     register char *        q;
  93.     register struct macro *    mp;
  94.  
  95.  
  96.     rp = from;
  97.     p = *to;
  98.     while (*rp)
  99.     {
  100.         if (*rp != '$')
  101.         {
  102.             *p++ = *rp++;
  103.             (*len)--;
  104.         }
  105.         else
  106.         {
  107.             q = buf;
  108.             if (*++rp == '{')
  109.                 while (*++rp && *rp != '}')
  110.                     *q++ = *rp;
  111.             else if (*rp == '(')
  112.                 while (*++rp && *rp != ')')
  113.                     *q++ = *rp;
  114.             else if (!*rp)
  115.             {
  116.                 *p++ = '$';
  117.                 break;
  118.             }
  119.             else
  120.                 *q++ = *rp;
  121.             *q = '\0';
  122.             if (*rp)
  123.                 rp++;
  124.             if (!(mp = getmp(buf)))
  125.                 mp = setmacro(buf, "");
  126.             if (mp->m_flag)
  127.                 fatal("Infinitely recursive macro %s", mp->m_name);
  128.             mp->m_flag = TRUE;
  129.             *to = p;
  130.             doexp(to, mp->m_val, len, buf);
  131.             p = *to;
  132.             mp->m_flag = FALSE;
  133.         }
  134.         if (*len <= 0)
  135.             error("Expanded line too line");
  136.     }
  137.     *p = '\0';
  138.     *to = p;
  139. }
  140.  
  141.  
  142. /*
  143.  *    Expand any macros in str.
  144.  */
  145. void
  146. expand(str)
  147. char *        str;
  148. {
  149.     static char        a[LZ];
  150.     static char        b[LZ];
  151.     char *            p = str;
  152.     int            len = LZ-1;
  153.  
  154.     strcpy(a, str);
  155.     doexp(&p, a, &len, b);
  156. }
  157.