replace.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* $Header$ */
  2. /* PREPROCESSOR: MACRO-TEXT REPLACEMENT ROUTINES */
  3. #include "nopp.h"
  4. #ifndef NOPP
  5. #include "debug.h" /* UF */
  6. #include "pathlength.h" /* UF */
  7. #include "strsize.h" /* UF */
  8. #include "string.h"
  9. #include "alloc.h"
  10. #include "idf.h"
  11. #include "input.h"
  12. #include "macro.h"
  13. #include "arith.h"
  14. #include "LLlex.h"
  15. #include "class.h"
  16. #include "assert.h"
  17. #include "interface.h"
  18. EXPORT int
  19. replace(idef)
  20. struct idf *idef;
  21. {
  22. /* replace() is called by the lexical analyzer to perform
  23. macro replacement. "idef" is the description of the
  24. identifier which leads to the replacement. If the
  25. optional actual parameters of the macro are OK, the text
  26. of the macro is prepared to serve as an input buffer,
  27. which is pushed onto the input stack.
  28. replace() returns 1 if the replacement succeeded and 0 if
  29. some error has occurred.
  30. */
  31. register char c;
  32. register char flags = idef->id_macro->mc_flag;
  33. char **actpars, **getactuals();
  34. char *reptext, *macro2buffer();
  35. int size;
  36. if (idef->id_macro->mc_nps != -1) { /* with parameter list */
  37. LoadChar(c);
  38. c = skipspaces(c);
  39. if (c != '(') { /* no replacement if no () */
  40. lexerror("(warning) macro %s needs arguments",
  41. idef->id_text);
  42. PushBack();
  43. return 0;
  44. }
  45. actpars = getactuals(idef); /* get act.param. list */
  46. }
  47. if (flags & PREDEF) { /* don't replace this one... */
  48. return 0;
  49. }
  50. if (flags & FUNC) { /* this macro leads to special action */
  51. macro_func(idef);
  52. }
  53. /* create and input buffer */
  54. reptext = macro2buffer(idef, actpars, &size);
  55. InsertText(reptext, size);
  56. return 1;
  57. }
  58. PRIVATE
  59. macro_func(idef)
  60. struct idf *idef;
  61. {
  62. /* macro_func() performs the special actions needed with some
  63. macros. These macros are __FILE__ and __LINE__ which
  64. replacement texts must be evaluated at the time they are
  65. used.
  66. */
  67. static char FilNamBuf[PATHLENGTH];
  68. /* This switch is very blunt... */
  69. switch (idef->id_text[2]) {
  70. case 'F' : /* __FILE__ */
  71. FilNamBuf[0] = '"';
  72. strcpy(&FilNamBuf[1], FileName);
  73. strcat(FilNamBuf, "\"");
  74. idef->id_macro->mc_text = FilNamBuf;
  75. idef->id_macro->mc_length = strlen(FilNamBuf);
  76. break;
  77. case 'L' : /* __LINE__ */
  78. idef->id_macro->mc_text = itos(LineNumber);
  79. idef->id_macro->mc_length = 1;
  80. break;
  81. default :
  82. crash("(macro_func) illegal macro %s\n", idef->id_text);
  83. }
  84. }
  85. PRIVATE char *
  86. macro2buffer(idef, actpars, siztext)
  87. struct idf *idef;
  88. char **actpars;
  89. int *siztext;
  90. {
  91. /* Macro2buffer() turns the macro replacement text, as it is
  92. stored, into an input buffer, while each occurrence of the
  93. non-ascii formal parameter mark is replaced by its
  94. corresponding actual parameter specified in the actual
  95. parameter list actpars. A pointer to the beginning of the
  96. constructed text is returned, while *siztext is filled
  97. with its length.
  98. If there are no parameters, this function behaves
  99. the same as strcpy().
  100. */
  101. register int size = 8;
  102. register char *text = Malloc(size);
  103. register pos = 0;
  104. register char *ptr = idef->id_macro->mc_text;
  105. text[pos++] = '\0'; /* allow pushback */
  106. while (*ptr) {
  107. if (*ptr & FORMALP) { /* non-asc formal param. mark */
  108. register int n = *ptr++ & 0177;
  109. register char *p;
  110. ASSERT(n != 0);
  111. /* copy the text of the actual parameter
  112. into the replacement text
  113. */
  114. for (p = actpars[n - 1]; *p; p++) {
  115. text[pos++] = *p;
  116. if (pos == size) {
  117. text = Srealloc(text, size += RSTRSIZE);
  118. }
  119. }
  120. }
  121. else {
  122. text[pos++] = *ptr++;
  123. if (pos == size) {
  124. text = Srealloc(text, size += RSTRSIZE);
  125. }
  126. }
  127. }
  128. text[pos] = '\0';
  129. *siztext = pos;
  130. return text;
  131. }
  132. #endif NOPP