home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / sozobon / scsrc20 / make / macro.c < prev    next >
C/C++ Source or Header  |  1988-10-13  |  3KB  |  164 lines

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