preprocess.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Header$ */
  6. /* PREPROCESSOR DRIVER */
  7. #include <system.h>
  8. #include "input.h"
  9. #include "obufsize.h"
  10. #include "arith.h"
  11. #include "LLlex.h"
  12. #include "class.h"
  13. #include "macro.h"
  14. #include "idf.h"
  15. #include "idfsize.h"
  16. #include "bits.h"
  17. #include "line_prefix.h"
  18. char _obuf[OBUFSIZE];
  19. #ifdef DOBITS
  20. char bits[128];
  21. #endif
  22. extern int InputLevel;
  23. Xflush()
  24. {
  25. sys_write(STDOUT, _obuf, OBUFSIZE);
  26. }
  27. preprocess(fn)
  28. char *fn;
  29. {
  30. register int c;
  31. register char *op = _obuf;
  32. register char *ob = &_obuf[OBUFSIZE];
  33. char Xbuf[256];
  34. int lineno = 0;
  35. extern char options[];
  36. #define flush(X) (sys_write(STDOUT,_obuf,X))
  37. #define echo(ch) if (op == ob) { Xflush(); op = _obuf; } *op++ = (ch);
  38. #define newline() echo('\n')
  39. if (!options['P']) {
  40. /* Generate a line directive communicating the
  41. source filename
  42. */
  43. register char *p = Xbuf;
  44. sprint(p, "%s 1 \"%s\"\n",
  45. LINE_PREFIX,
  46. FileName);
  47. while (*p) {
  48. echo(*p++);
  49. }
  50. }
  51. #define do_line(lineno, fn) \
  52. if (lineno != LineNumber || fn != FileName) { \
  53. fn = FileName; \
  54. lineno = LineNumber; \
  55. if (! options['P']) { \
  56. register char *p = Xbuf; \
  57. \
  58. sprint(p, "%s %d \"%s\"\n", \
  59. LINE_PREFIX, \
  60. LineNumber, \
  61. FileName); \
  62. while (*p) { \
  63. echo(*p++); \
  64. } \
  65. } \
  66. }
  67. for (;;) {
  68. LineNumber++;
  69. lineno++;
  70. c = GetChar();
  71. while (class(c) == STSKIP) {
  72. echo(c);
  73. c = GetChar();
  74. }
  75. while (c == '#') {
  76. if (!domacro()) { /* pass pragma's to compiler */
  77. register char *p = "#pragma";
  78. do_line(lineno, fn);
  79. while(*p) {
  80. echo(*p++);
  81. }
  82. while ((c = GetChar()) != EOI) {
  83. if (class(c) == STNL) break;
  84. echo(c);
  85. }
  86. }
  87. lineno++;
  88. newline();
  89. c = GetChar();
  90. while (class(c) == STSKIP) {
  91. echo(c);
  92. c = GetChar();
  93. }
  94. }
  95. do_line(lineno, fn);
  96. for (;;) {
  97. /* illegal character */
  98. if (c & 0200) {
  99. if (c == EOI) {
  100. newline();
  101. flush(op-_obuf);
  102. return;
  103. }
  104. fatal("non-ascii character read");
  105. }
  106. /* comments */
  107. if (c == '/' && !InputLevel) {
  108. c = GetChar();
  109. if (c == '*') {
  110. NoUnstack++;
  111. if (options['C']) {
  112. echo('/');
  113. echo('*');
  114. }
  115. for (;;) {
  116. c = GetChar();
  117. if (c == '\n') {
  118. ++LineNumber;
  119. ++lineno;
  120. echo(c);
  121. }
  122. else if (c == EOI) {
  123. newline();
  124. flush(op - _obuf);
  125. return;
  126. }
  127. else if (c == '*') {
  128. if (options['C']) {
  129. echo(c);
  130. }
  131. c = GetChar();
  132. if (c == '/') {
  133. if (options['C']) {
  134. echo(c);
  135. }
  136. break;
  137. }
  138. else {
  139. UnGetChar();
  140. }
  141. }
  142. else if (options['C']) {
  143. echo(c);
  144. }
  145. }
  146. NoUnstack--;
  147. c = GetChar();
  148. continue;
  149. }
  150. echo('/');
  151. continue;
  152. }
  153. /* switch on character */
  154. switch(class(c)) {
  155. case STNL:
  156. echo(c);
  157. break;
  158. case STSTR:
  159. case STCHAR:
  160. {
  161. register int stopc = c;
  162. int escaped;
  163. do {
  164. escaped = 0;
  165. echo(c);
  166. c = GetChar();
  167. if (c == '\n') {
  168. break;
  169. }
  170. else if (c == EOI) {
  171. newline();
  172. flush(op-_obuf);
  173. return;
  174. }
  175. if (c == '\\') {
  176. echo(c);
  177. c = GetChar();
  178. if (c == '\n') {
  179. ++LineNumber;
  180. lineno++;
  181. }
  182. else if (c == '\'') escaped = 1;
  183. }
  184. } while (escaped || c != stopc);
  185. echo(c);
  186. if (c == '\n')
  187. break; /* Don't eat # */
  188. c = GetChar();
  189. continue;
  190. }
  191. case STNUM:
  192. echo(c);
  193. if (c == '.') {
  194. c = GetChar();
  195. if (c == '.') {
  196. if ((c = GetChar()) == '.') {
  197. echo('.'); echo('.');
  198. continue;
  199. }
  200. UnGetChar();
  201. c = '.';
  202. continue;
  203. } else if (!is_dig(c)) {
  204. continue;
  205. }
  206. }
  207. c = GetChar();
  208. while (in_idf(c) || c == '.') {
  209. echo(c);
  210. if (c == 'e' || c == 'E') {
  211. c = GetChar();
  212. if (c == '+' || c == '-') {
  213. echo(c);
  214. c = GetChar();
  215. }
  216. } else c = GetChar();
  217. }
  218. continue;
  219. case STELL:
  220. if (c == '"' || c == '\'') {
  221. echo(c);
  222. continue;
  223. }
  224. UnGetChar();
  225. c = 'L';
  226. case STIDF: {
  227. extern int idfsize; /* ??? */
  228. char buf[IDFSIZE + 1];
  229. register char *tg = &buf[0];
  230. register char *maxpos = &buf[idfsize];
  231. register struct idf *idef;
  232. int NoExpandNext = 0;
  233. #define tstmac(bx) if (!(bits[c] & bx)) goto nomac
  234. #define cpy *tg++ = c
  235. #define load c = GetChar(); if (!in_idf(c)) goto endidf
  236. /* unstack macro's when allowed. */
  237. if (Unstacked)
  238. EnableMacros();
  239. if (c == NOEXPM) {
  240. NoExpandNext = 1;
  241. c = GetChar();
  242. }
  243. #ifdef DOBITS
  244. cpy; tstmac(bit0); load;
  245. cpy; tstmac(bit1); load;
  246. cpy; tstmac(bit2); load;
  247. cpy; tstmac(bit3); load;
  248. cpy; tstmac(bit4); load;
  249. cpy; tstmac(bit5); load;
  250. cpy; tstmac(bit6); load;
  251. cpy; tstmac(bit7); load;
  252. #endif
  253. for(;;) {
  254. if (tg < maxpos) {
  255. cpy;
  256. }
  257. load;
  258. }
  259. endidf:
  260. if (c != EOF) UnGetChar();
  261. *tg = '\0'; /* mark the end of the identifier */
  262. if ((idef = findidf(buf))
  263. && idef->id_macro
  264. && ReplaceMacros && !NoExpandNext) {
  265. if (replace(idef)) {
  266. c = GetChar();
  267. continue;
  268. }
  269. tg = buf;
  270. while (*tg) {
  271. echo(*tg++);
  272. }
  273. c = GetChar();
  274. if (in_idf(c)) echo(' ');
  275. continue;
  276. }
  277. nomac:
  278. *tg = '\0';
  279. tg = buf;
  280. while (*tg) {
  281. echo(*tg++);
  282. }
  283. c = GetChar();
  284. while (in_idf(c)) {
  285. echo(c);
  286. c = GetChar();
  287. }
  288. continue;
  289. }
  290. case STMSPEC:
  291. if (InputLevel) {
  292. echo(' '); /* seperate tokens */
  293. c = GetChar();
  294. continue;
  295. }
  296. /* else fallthrough */
  297. default:
  298. echo(c);
  299. c = GetChar();
  300. continue;
  301. }
  302. break;
  303. }
  304. }
  305. /*NOTREACHED*/
  306. }