preprocess.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 DRIVER */
  7. #include <system.h>
  8. #include "input.h"
  9. #include "obufsize.h"
  10. #include "LLlex.h"
  11. #include "class.h"
  12. #include "idf.h"
  13. #include "idfsize.h"
  14. #include "bits.h"
  15. #include "line_prefix.h"
  16. #ifdef DOBITS
  17. char bits[128];
  18. #endif
  19. char _obuf[OBUFSIZE];
  20. extern int do_preprocess;
  21. void Xflush()
  22. {
  23. if (do_preprocess) sys_write(STDOUT, _obuf, OBUFSIZE);
  24. }
  25. void preprocess(char *fn)
  26. {
  27. int c;
  28. char *op = _obuf;
  29. char *ob = &_obuf[OBUFSIZE];
  30. char Xbuf[256];
  31. int lineno = 0;
  32. extern char options[];
  33. #define flush(X) (! do_preprocess || sys_write(STDOUT,_obuf,X))
  34. #define echo(ch) if (op == ob) { Xflush(); op = _obuf; } *op++ = (ch);
  35. #define newline() echo('\n')
  36. if (! options['P']) {
  37. /* Generate a line directive communicating the
  38. source filename
  39. */
  40. char *p = Xbuf;
  41. sprint(p, "%s 1 \"%s\"\n",
  42. LINE_PREFIX,
  43. FileName);
  44. while (*p) {
  45. echo(*p++);
  46. }
  47. }
  48. for (;;) {
  49. LineNumber++;
  50. lineno++;
  51. LoadChar(c);
  52. while (c == '#') {
  53. domacro();
  54. lineno++;
  55. newline();
  56. LoadChar(c);
  57. }
  58. if (lineno != LineNumber || fn != FileName) {
  59. fn = FileName;
  60. lineno = LineNumber;
  61. if (! options['P']) {
  62. char *p = Xbuf;
  63. sprint(p, "%s %d \"%s\"\n",
  64. LINE_PREFIX,
  65. LineNumber,
  66. FileName);
  67. while (*p) {
  68. echo(*p++);
  69. }
  70. }
  71. }
  72. for (;;) {
  73. if (c & 0200) {
  74. if (c == EOI) {
  75. newline();
  76. flush(op-_obuf);
  77. return;
  78. }
  79. fatal("non-ascii character read");
  80. }
  81. if (c == '/') {
  82. LoadChar(c);
  83. if (c == '*') {
  84. NoUnstack++;
  85. if (options['C']) {
  86. echo('/');
  87. echo('*');
  88. }
  89. for (;;) {
  90. LoadChar(c);
  91. if (c == '\n') {
  92. ++LineNumber;
  93. ++lineno;
  94. echo(c);
  95. }
  96. else if (c == EOI) {
  97. newline();
  98. flush(op - _obuf);
  99. return;
  100. }
  101. else if (c == '*') {
  102. if (options['C']) {
  103. echo(c);
  104. }
  105. LoadChar(c);
  106. if (c == '/') {
  107. if (options['C']) {
  108. echo(c);
  109. }
  110. break;
  111. }
  112. else {
  113. PushBack();
  114. }
  115. }
  116. else if (options['C']) {
  117. echo(c);
  118. }
  119. }
  120. NoUnstack--;
  121. LoadChar(c);
  122. continue;
  123. }
  124. echo('/');
  125. continue;
  126. }
  127. switch(class(c)) {
  128. case STNL:
  129. echo(c);
  130. break;
  131. case STSTR:
  132. case STCHAR: {
  133. int stopc = c;
  134. int escaped;
  135. do {
  136. escaped = 0;
  137. echo(c);
  138. LoadChar(c);
  139. if (c == '\n') {
  140. break;
  141. }
  142. else if (c == EOI) {
  143. newline();
  144. flush(op-_obuf);
  145. return;
  146. }
  147. if (c == '\\') {
  148. echo(c);
  149. LoadChar(c);
  150. if (c == '\n') {
  151. ++LineNumber;
  152. lineno++;
  153. }
  154. else escaped = 1;
  155. }
  156. } while (escaped || c != stopc);
  157. echo(c);
  158. if (c == '\n')
  159. break; /* Don't eat # */
  160. LoadChar(c);
  161. continue;
  162. }
  163. case STNUM:
  164. #define getdec(c) do { echo(c); LoadChar(c);} while (is_dig(c))
  165. #define gethex(c) do { echo(c); LoadChar(c);} while (is_hex(c))
  166. if (c != '0') {
  167. getdec(c);
  168. if (c == '.') getdec(c);
  169. if (c == 'e') {
  170. echo(c);
  171. LoadChar(c);
  172. if (c == '+' || c == '-') {
  173. echo(c);
  174. LoadChar(c);
  175. }
  176. if (is_dig(c)) getdec(c);
  177. }
  178. }
  179. else {
  180. echo(c);
  181. LoadChar(c);
  182. if (c == 'x' || c == 'X') gethex(c);
  183. else if (is_dig(c)) getdec(c);
  184. }
  185. continue;
  186. case STIDF: {
  187. extern int idfsize; /* ??? */
  188. char buf[IDFSIZE + 1];
  189. char *tg = &buf[0];
  190. char *maxpos = &buf[idfsize];
  191. struct idf *idef;
  192. #define tstmac(bx) if (!(bits[c] & bx)) goto nomac
  193. #define cpy if (Unstacked) EnableMacros(); *tg++ = c
  194. #define load LoadChar(c); if (!in_idf(c)) goto endidf
  195. #ifdef DOBITS
  196. cpy; tstmac(bit0); load;
  197. cpy; tstmac(bit1); load;
  198. cpy; tstmac(bit2); load;
  199. cpy; tstmac(bit3); load;
  200. cpy; tstmac(bit4); load;
  201. cpy; tstmac(bit5); load;
  202. cpy; tstmac(bit6); load;
  203. cpy; tstmac(bit7); load;
  204. #endif
  205. for(;;) {
  206. if (tg < maxpos) {
  207. cpy;
  208. }
  209. else break;
  210. load;
  211. }
  212. endidf:
  213. PushBack();
  214. *tg = '\0'; /* mark the end of the identifier */
  215. idef = findidf(buf);
  216. if (idef && idef->id_macro) {
  217. do {
  218. LoadChar(c);
  219. } while (in_idf(c));
  220. PushBack();
  221. if (replace(idef)) {
  222. LoadChar(c);
  223. continue;
  224. }
  225. }
  226. nomac:
  227. *tg = 0;
  228. tg = buf;
  229. while (*tg) {
  230. echo(*tg++);
  231. }
  232. LoadChar(c);
  233. while (in_idf(c)) {
  234. echo(c);
  235. LoadChar(c);
  236. }
  237. continue;
  238. }
  239. default:
  240. echo(c);
  241. LoadChar(c);
  242. continue;
  243. }
  244. break;
  245. }
  246. }
  247. /*NOTREACHED*/
  248. }