home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / make_1 / !make_c_macro < prev    next >
Encoding:
Text File  |  1992-12-07  |  2.3 KB  |  149 lines

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