home *** CD-ROM | disk | FTP | other *** search
/ Total Destruction / Total_Destruction.iso / addons / Lccwin32.exe / Lccwin32 / lccpub / make / MACRO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-11  |  3.3 KB  |  182 lines

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