replace.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* PREPROCESSOR: MACRO-TEXT REPLACEMENT ROUTINES */
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "debug.h" /* UF */
  10. #include "pathlength.h" /* UF */
  11. #include "textsize.h" /* UF */
  12. #include <alloc.h>
  13. #include <assert.h>
  14. #include "idf.h"
  15. #include "input.h"
  16. #include "macro.h"
  17. #include "LLlex.h"
  18. #include "class.h"
  19. #include "interface.h"
  20. char *strcpy(), *strcat();
  21. char *long2str();
  22. extern int InputLevel;
  23. PRIVATE struct mlist *ReplList; /* list of currently active macros */
  24. EXPORT int
  25. replace(idef)
  26. register struct idf *idef;
  27. {
  28. /* replace() is called by the lexical analyzer to perform
  29. macro replacement. "idef" is the description of the
  30. identifier which leads to the replacement. If the
  31. optional actual parameters of the macro are OK, the text
  32. of the macro is prepared to serve as an input buffer,
  33. which is pushed onto the input stack.
  34. replace() returns 1 if the replacement succeeded and 0 if
  35. some error has occurred.
  36. */
  37. register struct macro *mac = idef->id_macro;
  38. register char c;
  39. char **actpars, **getactuals();
  40. char *reptext, *macro2buffer();
  41. register struct mlist *repl;
  42. int size;
  43. if (mac->mc_flag & NOREPLACE) {
  44. warning("macro %s is recursive", idef->id_text);
  45. return 0;
  46. }
  47. if (mac->mc_nps != -1) { /* with parameter list */
  48. if (mac->mc_flag & FUNC) {
  49. /* must be "defined".
  50. Unfortunately, the next assertion
  51. will not compile ...
  52. assert( ! strcmp("defined", idef->id_text));
  53. */
  54. if (! AccDefined)
  55. return 0;
  56. }
  57. if (++mac->mc_count > 100) {
  58. /* 100 must be some number in Parameters */
  59. warning("macro %s is assumed recursive",
  60. idef->id_text);
  61. return 0;
  62. }
  63. LoadChar(c);
  64. c = skipspaces(c,! (mac->mc_flag & FUNC));
  65. if (c != '(') { /* no replacement if no () */
  66. PushBack();
  67. if (! (mac->mc_flag & FUNC)) {
  68. warning("macro %s needs arguments",
  69. idef->id_text);
  70. return 0;
  71. }
  72. }
  73. if (mac->mc_flag & FUNC) {
  74. struct idf *param;
  75. char *nam;
  76. extern char *GetIdentifier();
  77. UnknownIdIsZero = 0;
  78. nam = GetIdentifier();
  79. if (nam) {
  80. param = findidf(nam);
  81. }
  82. else param = 0;
  83. UnknownIdIsZero = 1;
  84. if (c == '(') {
  85. LoadChar(c);
  86. c = skipspaces(c, 0);
  87. if (c != ')') error(") missing");
  88. }
  89. if (! nam) {
  90. error("identifier missing");
  91. }
  92. repl = new_mlist();
  93. if (param && param->id_macro)
  94. reptext = "1 ";
  95. else
  96. reptext = "0 ";
  97. InsertText(reptext, 2);
  98. InputLevel++;
  99. repl->m_level = InputLevel;
  100. repl->next = ReplList;
  101. ReplList = repl;
  102. repl->m_mac = mac;
  103. if (nam) free(nam);
  104. return 1;
  105. }
  106. actpars = getactuals(idef); /* get act.param. list */
  107. }
  108. repl = new_mlist();
  109. repl->m_mac = mac;
  110. if (mac->mc_flag & FUNC) /* this macro leads to special action */
  111. macro_func(idef);
  112. if (mac->mc_nps <= 0) {
  113. reptext = mac->mc_text;
  114. size = mac->mc_length;
  115. mac->mc_flag |= NOREPLACE; /* a file called __FILE__ ??? */
  116. }
  117. else {
  118. reptext = macro2buffer(idef, actpars, &size); /* create input buffer */
  119. repl->m_repl = reptext;
  120. }
  121. InsertText(reptext, size);
  122. InputLevel++;
  123. repl->m_level = InputLevel;
  124. repl->next = ReplList;
  125. ReplList = repl;
  126. return 1;
  127. }
  128. char FilNamBuf[PATHLENGTH];
  129. PRIVATE
  130. macro_func(idef)
  131. register struct idf *idef;
  132. {
  133. /* macro_func() performs the special actions needed with some
  134. macros. These macros are __FILE__ and __LINE__ which
  135. replacement texts must be evaluated at the time they are
  136. used.
  137. */
  138. register struct macro *mac = idef->id_macro;
  139. switch (idef->id_text[2]) { /* This switch is very blunt... */
  140. case 'F' : /* __FILE__ */
  141. mac->mc_length = strlen(FileName) + 2;
  142. mac->mc_text = FilNamBuf;
  143. mac->mc_text[0] = '"';
  144. strcpy(&(mac->mc_text[1]), FileName);
  145. strcat(mac->mc_text, "\"");
  146. break;
  147. case 'L' : /* __LINE__ */
  148. {
  149. mac->mc_text = long2str((long) LineNumber, 10);
  150. mac->mc_length = strlen(mac->mc_text);
  151. break;
  152. }
  153. default :
  154. crash("(macro_func)");
  155. }
  156. }
  157. PRIVATE char *
  158. macro2buffer(idef, actpars, siztext)
  159. struct idf *idef;
  160. char **actpars;
  161. int *siztext;
  162. {
  163. /* Macro2buffer() turns the macro replacement text, as it is
  164. stored, into an input buffer, while each occurrence of the
  165. non-ascii formal parameter mark is replaced by its
  166. corresponding actual parameter specified in the actual
  167. parameter list actpars. A pointer to the beginning of the
  168. constructed text is returned, while *siztext is filled
  169. with its length.
  170. If there are no parameters, this function behaves
  171. the same as strcpy().
  172. */
  173. register unsigned int size = idef->id_macro->mc_length + ITEXTSIZE;
  174. register char *text = Malloc(size);
  175. register int pos = 0;
  176. register char *ptr = idef->id_macro->mc_text;
  177. while (*ptr) {
  178. if (*ptr & FORMALP) { /* non-asc formal param. mark */
  179. register int n = *ptr++ & 0177;
  180. register char *p;
  181. assert(n != 0);
  182. /* copy the text of the actual parameter
  183. into the replacement text
  184. */
  185. for (p = actpars[n - 1]; *p; p++) {
  186. text[pos++] = *p;
  187. if (pos == size)
  188. text = Realloc(text, size <<= 1);
  189. }
  190. }
  191. else {
  192. text[pos++] = *ptr++;
  193. if (pos == size)
  194. text = Realloc(text, size <<= 1);
  195. }
  196. }
  197. text[pos] = '\0';
  198. *siztext = pos;
  199. return Realloc(text, pos+1);
  200. }
  201. EXPORT
  202. DoUnstack()
  203. {
  204. Unstacked = 1;
  205. }
  206. EXPORT
  207. EnableMacros()
  208. {
  209. register struct mlist *p = ReplList, *prev = 0;
  210. assert(Unstacked > 0);
  211. while (p) {
  212. struct mlist *nxt = p->next;
  213. if (p->m_level > InputLevel) {
  214. p->m_mac->mc_flag &= ~NOREPLACE;
  215. if (p->m_mac->mc_count) p->m_mac->mc_count--;
  216. if (p->m_repl) free(p->m_repl);
  217. if (! prev) ReplList = nxt;
  218. else prev->next = nxt;
  219. free_mlist(p);
  220. }
  221. else prev = p;
  222. p = nxt;
  223. }
  224. Unstacked = 0;
  225. }