macro.c 2.6 KB

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