domacro.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. /* $Id$ */
  6. /* PREPROCESSOR: CONTROLLINE INTERPRETER */
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "arith.h"
  10. #include "LLlex.h"
  11. #include "Lpars.h"
  12. #include "idf.h"
  13. #include "input.h"
  14. #include "ifdepth.h"
  15. #include "botch_free.h"
  16. #include "nparams.h"
  17. #include "parbufsize.h"
  18. #include "textsize.h"
  19. #include "idfsize.h"
  20. #include "debug.h"
  21. #include <assert.h>
  22. #include <alloc.h>
  23. #include "class.h"
  24. #include "macro.h"
  25. #include "bits.h"
  26. #include "macbuf.h"
  27. #include "replace.h"
  28. char *getwdir(char *fn); /* /util/cpp/input.c */
  29. extern char options[];
  30. extern char **inctable; /* list of include directories */
  31. char ifstack[IFDEPTH]; /* if-stack: the content of an entry is */
  32. /* 1 if a corresponding ELSE has been */
  33. /* encountered. */
  34. void do_include();
  35. void do_define();
  36. void do_elif();
  37. void push_if();
  38. void do_else();
  39. void do_endif();
  40. void do_if();
  41. void do_ifdef(int how);
  42. void do_undef(char *argstr);
  43. void do_error();
  44. void do_warning();
  45. void do_line(unsigned int l);
  46. void macro_def(struct idf *id, char *text, int nformals, int length, int flags);
  47. int nestlevel = -1;
  48. int svnestlevel[30] = {-1};
  49. int nestcount;
  50. extern int do_preprocess;
  51. /* skiponerr => skip the rest of the line on error */
  52. char *GetIdentifier(int skiponerr)
  53. {
  54. /* Returns a pointer to the identifier that is read from the
  55. input stream. When the input does not contain an
  56. identifier, the rest of the line is skipped when skiponerr
  57. is on, and a null-pointer is returned.
  58. The substitution of macros is disabled.
  59. Remember that on end-of-line EOF is returned.
  60. */
  61. int tmp = UnknownIdIsZero;
  62. int tok;
  63. struct token tk;
  64. UnknownIdIsZero = ReplaceMacros = 0;
  65. tok = GetToken(&tk);
  66. ReplaceMacros = 1;
  67. UnknownIdIsZero = tmp;
  68. if (tok != IDENTIFIER) {
  69. if (skiponerr && tok != EOF) SkipToNewLine();
  70. return (char *)0;
  71. }
  72. return tk.tk_str;
  73. }
  74. /* domacro() is the control line interpreter. The '#' has already
  75. been read by the lexical analyzer by which domacro() is called.
  76. The token appearing directly after the '#' is obtained by calling
  77. the basic lexical analyzing function GetToken() and is interpreted
  78. to perform the action belonging to that token.
  79. An error message is produced when the token is not recognized.
  80. Pragma's are handled by do_pragma(). They are passed on to the
  81. compiler.
  82. */
  83. void domacro()
  84. {
  85. struct token tk; /* the token itself */
  86. register struct idf *id;
  87. int toknum;
  88. ReplaceMacros = 0;
  89. toknum = GetToken(&tk);
  90. ReplaceMacros = 1;
  91. switch(toknum) { /* select control line action */
  92. case IDENTIFIER: /* is it a macro keyword? */
  93. id = findidf(tk.tk_str);
  94. if (!id) {
  95. error("%s: unknown control", tk.tk_str);
  96. SkipToNewLine();
  97. free(tk.tk_str);
  98. break;
  99. }
  100. free(tk.tk_str);
  101. switch (id->id_resmac) {
  102. case K_DEFINE: /* "define" */
  103. do_define();
  104. break;
  105. case K_ELIF: /* "elif" */
  106. do_elif();
  107. break;
  108. case K_ELSE: /* "else" */
  109. do_else();
  110. break;
  111. case K_ENDIF: /* "endif" */
  112. do_endif();
  113. break;
  114. case K_IF: /* "if" */
  115. do_if();
  116. break;
  117. case K_IFDEF: /* "ifdef" */
  118. do_ifdef(1);
  119. break;
  120. case K_IFNDEF: /* "ifndef" */
  121. do_ifdef(0);
  122. break;
  123. case K_INCLUDE: /* "include" */
  124. do_include();
  125. break;
  126. case K_LINE: /* "line" */
  127. /* set LineNumber and FileName according to
  128. the arguments.
  129. */
  130. if (GetToken(&tk) != INTEGER) {
  131. error("bad #line syntax");
  132. SkipToNewLine();
  133. }
  134. else
  135. do_line((unsigned int)tk.tk_val);
  136. break;
  137. case K_WARNING: /* "warning" */
  138. do_warning();
  139. break;
  140. case K_ERROR: /* "error" */
  141. do_error();
  142. break;
  143. case K_PRAGMA: /* "pragma" */
  144. do_pragma();
  145. break;
  146. case K_UNDEF: /* "undef" */
  147. do_undef((char *)0);
  148. break;
  149. default:
  150. /* invalid word seen after the '#' */
  151. error("%s: unknown control", id->id_text);
  152. SkipToNewLine();
  153. }
  154. break;
  155. case INTEGER: /* # <integer> [<filespecifier>]? */
  156. do_line((unsigned int)tk.tk_val);
  157. break;
  158. case EOF: /* only `#' on this line: do nothing, ignore */
  159. break;
  160. default: /* invalid token following '#' */
  161. error("illegal # line");
  162. SkipToNewLine();
  163. }
  164. }
  165. void skip_block(int to_endif)
  166. {
  167. /* skip_block() skips the input from
  168. 1) a false #if, #ifdef, #ifndef or #elif until the
  169. corresponding #elif (resulting in true), #else or
  170. #endif is read.
  171. 2) a #else corresponding to a true #if, #ifdef,
  172. #ifndef or #elif until the corresponding #endif is
  173. seen.
  174. */
  175. register int ch;
  176. register int skiplevel = nestlevel; /* current nesting level */
  177. struct token tk;
  178. int toknum;
  179. struct idf *id;
  180. NoUnstack++;
  181. for (;;) {
  182. ch = GetChar(); /* read first character after newline */
  183. while (class(ch) == STSKIP)
  184. ch = GetChar();
  185. if (ch != '#') {
  186. if (ch == EOI) {
  187. NoUnstack--;
  188. return;
  189. }
  190. if (ch == '/') {
  191. if (ch != '*') UnGetChar();
  192. else {
  193. skipcomment();
  194. continue;
  195. }
  196. } else UnGetChar();
  197. SkipToNewLine();
  198. continue;
  199. }
  200. ReplaceMacros = 0;
  201. toknum = GetToken(&tk);
  202. ReplaceMacros = 1;
  203. if (toknum != IDENTIFIER) {
  204. if (toknum != INTEGER) {
  205. error("illegal # line");
  206. }
  207. SkipToNewLine();
  208. continue;
  209. }
  210. /* an IDENTIFIER: look for #if, #ifdef and #ifndef
  211. without interpreting them.
  212. Interpret #else, #elif and #endif if they occur
  213. on the same level.
  214. */
  215. id = findidf(tk.tk_str);
  216. if (id == (struct idf *)0) {
  217. /* invalid word seen after the '#' */
  218. warning("%s: unknown control", tk.tk_str);
  219. }
  220. free(tk.tk_str);
  221. if (id == (struct idf *)0) continue;
  222. switch(id->id_resmac) {
  223. case K_DEFINE:
  224. case K_ERROR:
  225. case K_INCLUDE:
  226. case K_LINE:
  227. case K_PRAGMA:
  228. case K_UNDEF:
  229. case K_FILE:
  230. SkipToNewLine();
  231. break;
  232. case K_IF:
  233. case K_IFDEF:
  234. case K_IFNDEF:
  235. push_if();
  236. SkipToNewLine();
  237. break;
  238. case K_ELIF:
  239. if (ifstack[nestlevel])
  240. error("#elif after #else");
  241. if (!to_endif && nestlevel == skiplevel) {
  242. nestlevel--;
  243. push_if();
  244. if (ifexpr()) {
  245. NoUnstack--;
  246. return;
  247. }
  248. }
  249. else SkipToNewLine(); /* otherwise done in ifexpr() */
  250. break;
  251. case K_ELSE:
  252. if (ifstack[nestlevel])
  253. error("#else after #else");
  254. ++(ifstack[nestlevel]);
  255. if (!to_endif && nestlevel == skiplevel) {
  256. if (SkipToNewLine()) {
  257. if (!options['o'])
  258. strict("garbage following #else");
  259. }
  260. NoUnstack--;
  261. return;
  262. }
  263. else SkipToNewLine();
  264. break;
  265. case K_ENDIF:
  266. assert(nestlevel > svnestlevel[nestcount]);
  267. if (nestlevel == skiplevel) {
  268. if (SkipToNewLine()) {
  269. if (!options['o'])
  270. strict("garbage following #endif");
  271. }
  272. nestlevel--;
  273. NoUnstack--;
  274. return;
  275. }
  276. else SkipToNewLine();
  277. nestlevel--;
  278. break;
  279. }
  280. }
  281. }
  282. int ifexpr()
  283. {
  284. /* ifexpr() returns whether the restricted constant
  285. expression following #if or #elif evaluates to true. This
  286. is done by calling the LLgen generated subparser for
  287. constant expressions. The result of this expression will
  288. be given in the extern long variable "ifval".
  289. */
  290. extern arith ifval;
  291. int errors = err_occurred;
  292. ifval = (arith)0;
  293. AccDefined = 1;
  294. UnknownIdIsZero = 1;
  295. DOT = 0; /* tricky */
  296. If_expr(); /* invoke constant expression parser */
  297. AccDefined = 0;
  298. UnknownIdIsZero = 0;
  299. return (errors == err_occurred) && (ifval != (arith)0);
  300. }
  301. void do_include()
  302. {
  303. /* do_include() performs the inclusion of a file.
  304. */
  305. char *filenm;
  306. char *result;
  307. int tok;
  308. struct token tk;
  309. AccFileSpecifier = 1;
  310. if (((tok = GetToken(&tk)) == FILESPECIFIER) || tok == STRING)
  311. filenm = tk.tk_str;
  312. else {
  313. error("bad include syntax");
  314. filenm = (char *)0;
  315. }
  316. AccFileSpecifier = 0;
  317. if (SkipToNewLine()) {
  318. error("bad include syntax");
  319. }
  320. inctable[0] = WorkingDir;
  321. if (filenm) {
  322. if (!InsertFile(filenm, &inctable[tok==FILESPECIFIER],&result)){
  323. if (do_preprocess) error("cannot open include file \"%s\"", filenm);
  324. else warning("cannot open include file \"%s\"", filenm);
  325. add_dependency(filenm);
  326. }
  327. else {
  328. add_dependency(result);
  329. WorkingDir = getwdir(result);
  330. svnestlevel[++nestcount] = nestlevel;
  331. FileName = result;
  332. LineNumber = 1;
  333. }
  334. }
  335. }
  336. void do_define()
  337. {
  338. /* do_define() interprets a #define control line.
  339. */
  340. register char *str; /* the #defined identifier's descriptor */
  341. int nformals = -1; /* keep track of the number of formals */
  342. char *formals[NPARAMS]; /* pointers to the names of the formals */
  343. char parbuf[PARBUFSIZE]; /* names of formals */
  344. char *repl_text; /* start of the replacement text */
  345. int length; /* length of the replacement text */
  346. register ch;
  347. char *get_text();
  348. /* read the #defined macro's name */
  349. if (!(str = GetIdentifier(1))) {
  350. error("#define: illegal macro name");
  351. return;
  352. }
  353. /* there is a formal parameter list if the identifier is
  354. followed immediately by a '('.
  355. */
  356. ch = GetChar();
  357. if (ch == '(') {
  358. if ((nformals = getparams(formals, parbuf)) == -1) {
  359. SkipToNewLine();
  360. free(str);
  361. return; /* an error occurred */
  362. }
  363. ch = GetChar();
  364. }
  365. /* read the replacement text if there is any */
  366. ch = skipspaces(ch,0); /* find first character of the text */
  367. assert(ch != EOI);
  368. /* UnGetChar() is not right when replacement starts with a '/' */
  369. ChPushBack(ch);
  370. repl_text = get_text((nformals > 0) ? formals : 0, &length);
  371. macro_def(str2idf(str, 0), repl_text, nformals, length, NOFLAG);
  372. LineNumber++;
  373. }
  374. void push_if()
  375. {
  376. if (nestlevel >= IFDEPTH)
  377. fatal("too many nested #if/#ifdef/#ifndef");
  378. else
  379. ifstack[++nestlevel] = 0;
  380. }
  381. void do_elif()
  382. {
  383. if (nestlevel <= svnestlevel[nestcount]) {
  384. error("#elif without corresponding #if");
  385. SkipToNewLine();
  386. }
  387. else { /* restart at this level as if a #if is detected. */
  388. if (ifstack[nestlevel]) {
  389. error("#elif after #else");
  390. SkipToNewLine();
  391. }
  392. nestlevel--;
  393. push_if();
  394. skip_block(1);
  395. }
  396. }
  397. void do_else()
  398. {
  399. if (SkipToNewLine()) {
  400. if (!options['o'])
  401. strict("garbage following #else");
  402. }
  403. if (nestlevel <= svnestlevel[nestcount])
  404. error("#else without corresponding #if");
  405. else { /* mark this level as else-d */
  406. if (ifstack[nestlevel]) {
  407. error("#else after #else");
  408. }
  409. ++(ifstack[nestlevel]);
  410. skip_block(1);
  411. }
  412. }
  413. void do_endif()
  414. {
  415. if (SkipToNewLine()) {
  416. if (!options['o'])
  417. strict("garbage following #endif");
  418. }
  419. if (nestlevel <= svnestlevel[nestcount]) {
  420. error("#endif without corresponding #if");
  421. }
  422. else nestlevel--;
  423. }
  424. void do_if()
  425. {
  426. push_if();
  427. if (!ifexpr()) /* a false #if/#elif expression */
  428. skip_block(0);
  429. }
  430. void do_ifdef(int how)
  431. {
  432. register struct idf *id;
  433. register char *str;
  434. /* how == 1 : ifdef; how == 0 : ifndef
  435. */
  436. push_if();
  437. if (!(str = GetIdentifier(1))) {
  438. error("illegal #ifdef construction");
  439. id = (struct idf *)0;
  440. } else {
  441. id = findidf(str);
  442. free(str);
  443. }
  444. if (SkipToNewLine()) {
  445. if (str && !options['o'])
  446. strict("garbage following #%s <identifier>",
  447. how ? "ifdef" : "ifndef");
  448. }
  449. /* The next test is a shorthand for:
  450. (how && !id->id_macro) || (!how && id->id_macro)
  451. */
  452. if (how ^ (id && id->id_macro != 0))
  453. skip_block(0);
  454. }
  455. /* argstr != NULL when the undef came from a -U option */
  456. void do_undef(char *argstr)
  457. {
  458. register struct idf *id;
  459. register char *str = argstr;
  460. /* Forget a macro definition. */
  461. if (str || (str = GetIdentifier(1))) {
  462. if ((id = findidf(str)) && id->id_macro) {
  463. if (id->id_macro->mc_flag & NOUNDEF) {
  464. error("it is not allowed to #undef %s", str);
  465. } else {
  466. free(id->id_macro->mc_text);
  467. free_macro(id->id_macro);
  468. id->id_macro = (struct macro *) 0;
  469. }
  470. } /* else: don't complain */
  471. if (!argstr){
  472. free(str);
  473. if (SkipToNewLine()) {
  474. if (!options['o'])
  475. strict("garbage following #undef");
  476. }
  477. }
  478. }
  479. else
  480. error("illegal #undef construction");
  481. }
  482. void do_warning()
  483. {
  484. int len;
  485. char *get_text();
  486. char *bp = get_text((char **) 0, &len);
  487. warning("user warning: %s", bp);
  488. free(bp);
  489. LineNumber++;
  490. }
  491. void do_error()
  492. {
  493. int len;
  494. char *get_text();
  495. char *bp = get_text((char **) 0, &len);
  496. error("user error: %s", bp);
  497. free(bp);
  498. LineNumber++;
  499. }
  500. int getparams(char *buf[], char parbuf[])
  501. {
  502. /* getparams() reads the formal parameter list of a macro
  503. definition.
  504. The number of parameters is returned.
  505. As a formal parameter list is expected when calling this
  506. routine, -1 is returned if an error is detected, for
  507. example:
  508. #define one(1), where 1 is not an identifier.
  509. Note that the '(' has already been eaten.
  510. The names of the formal parameters are stored into parbuf.
  511. */
  512. register char **pbuf = &buf[0];
  513. register int c;
  514. register char *ptr = &parbuf[0];
  515. register char **pbuf2;
  516. c = GetChar();
  517. c = skipspaces(c,0);
  518. if (c == ')') { /* no parameters: #define name() */
  519. *pbuf = (char *) 0;
  520. return 0;
  521. }
  522. for (;;) { /* eat the formal parameter list */
  523. if (class(c) != STIDF && class(c) != STELL) {
  524. error("#define: bad formal parameter");
  525. return -1;
  526. }
  527. *pbuf = ptr; /* name of the formal */
  528. *ptr++ = c;
  529. if (ptr >= &parbuf[PARBUFSIZE])
  530. fatal("formal parameter buffer overflow");
  531. do { /* eat the identifier name */
  532. c = GetChar();
  533. *ptr++ = c;
  534. if (ptr >= &parbuf[PARBUFSIZE])
  535. fatal("formal parameter buffer overflow");
  536. } while (in_idf(c));
  537. *(ptr - 1) = '\0'; /* mark end of the name */
  538. /* Check if this formal parameter is already used.
  539. Usually, macros do not have many parameters, so ...
  540. */
  541. for (pbuf2 = pbuf - 1; pbuf2 >= &buf[0]; pbuf2--) {
  542. if (!strcmp(*pbuf2, *pbuf)) {
  543. warning("formal parameter \"%s\" already used",
  544. *pbuf);
  545. }
  546. }
  547. pbuf++;
  548. c = skipspaces(c,0);
  549. if (c == ')') { /* end of the formal parameter list */
  550. *pbuf = (char *) 0;
  551. return pbuf - buf;
  552. }
  553. if (c != ',') {
  554. error("#define: bad formal parameter list");
  555. return -1;
  556. }
  557. c = GetChar();
  558. c = skipspaces(c,0);
  559. }
  560. /*NOTREACHED*/
  561. return -1;
  562. }
  563. void macro_def(struct idf *id, char *text, int nformals, int length, int flags)
  564. {
  565. struct macro *newdef = id->id_macro;
  566. /* macro_def() puts the contents and information of a macro
  567. definition into a structure and stores it into the symbol
  568. table entry belonging to the name of the macro.
  569. An error is given if there was already a definition
  570. */
  571. if (newdef) { /* is there a redefinition? */
  572. if (newdef->mc_flag & NOUNDEF) {
  573. error("it is not allowed to redefine %s", id->id_text);
  574. } else if (!macroeq(newdef->mc_text, text))
  575. error("illegal redefine of \"%s\"", id->id_text);
  576. free(text);
  577. return;
  578. } else {
  579. #ifdef DOBITS
  580. unsigned char *p = (unsigned char *)id->id_text;
  581. #define setbit(bx) if (!*p) goto go_on; bits[*p++] |= (bx)
  582. setbit(bit0);
  583. setbit(bit1);
  584. setbit(bit2);
  585. setbit(bit3);
  586. setbit(bit4);
  587. setbit(bit5);
  588. setbit(bit6);
  589. setbit(bit7);
  590. go_on:
  591. #endif
  592. id->id_macro = newdef = new_macro();
  593. }
  594. newdef->mc_text = text; /* replacement text */
  595. newdef->mc_nps = nformals; /* nr of formals */
  596. newdef->mc_length = length; /* length of repl. text */
  597. newdef->mc_flag = flags; /* special flags */
  598. }
  599. int find_name(char *nm, char *index[])
  600. {
  601. /* find_name() returns the index of "nm" in the namelist
  602. "index" if it can be found there. 0 is returned if it is
  603. not there.
  604. */
  605. register char **ip = &index[0];
  606. while (*ip)
  607. if (strcmp(nm, *ip++) == 0)
  608. return ip - &index[0];
  609. /* arrived here, nm is not in the name list. */
  610. return 0;
  611. }
  612. #define BLANK(ch) ((ch == ' ') || (ch == '\t'))
  613. char *get_text(char *formals[], int *length)
  614. {
  615. /* get_text() copies the replacement text of a macro
  616. definition with zero, one or more parameters, thereby
  617. substituting each formal parameter by a special character
  618. (non-ascii: 0200 & (order-number in the formal parameter
  619. list)) in order to substitute this character later by the
  620. actual parameter. The replacement text is copied into
  621. itself because the copied text will contain fewer or the
  622. same amount of characters. The length of the replacement
  623. text is returned.
  624. Implementation:
  625. finite automaton : we are interested in
  626. 1- white space, sequences must be mapped onto 1 single
  627. blank.
  628. 2- identifiers, since they might be replaced by some
  629. actual parameter.
  630. 3- strings and character constants, since replacing
  631. variables within them is illegal, and white-space is
  632. significant.
  633. 4- comment, same as for 1
  634. Other tokens will not be seen as such.
  635. */
  636. register int c;
  637. struct repl repls;
  638. register struct repl *repl = &repls;
  639. int blank = 0;
  640. c = GetChar();
  641. repl->r_ptr = repl->r_text = Malloc((unsigned)(repl->r_size = ITEXTSIZE));
  642. *repl->r_ptr = '\0';
  643. while ((c != EOI) && (class(c) != STNL)) {
  644. if (BLANK(c)) {
  645. blank++;
  646. c = GetChar();
  647. continue;
  648. }
  649. if (c == '\'' || c == '"') {
  650. register int delim = c;
  651. if (blank) {
  652. blank = 0;
  653. add2repl(repl, ' ');
  654. }
  655. do {
  656. add2repl(repl, c);
  657. if (c == '\\') add2repl(repl, GetChar());
  658. c = GetChar();
  659. } while (c != delim && c != EOI && class(c) != STNL);
  660. if (c != delim) {
  661. strict("unclosed opening %c", delim);
  662. break;
  663. }
  664. add2repl(repl, c);
  665. c = GetChar();
  666. } else if (c == '/') {
  667. c = GetChar();
  668. if (c == '*') {
  669. skipcomment();
  670. blank++;
  671. c = GetChar();
  672. continue;
  673. }
  674. if (blank) {
  675. blank = 0;
  676. add2repl(repl, ' ');
  677. }
  678. add2repl(repl, '/');
  679. } else if (formals
  680. && (class(c) == STIDF || class(c) == STELL)) {
  681. char id_buf[IDFSIZE + 1];
  682. register char *idp = id_buf;
  683. int n;
  684. /* read identifier: it may be a formal parameter */
  685. *idp++ = c;
  686. do {
  687. c = GetChar();
  688. if (idp <= &id_buf[IDFSIZE])
  689. *idp++ = c;
  690. } while (in_idf(c));
  691. *--idp = '\0';
  692. if (blank) {
  693. blank = 0;
  694. add2repl(repl, ' ');
  695. }
  696. /* construct the formal parameter mark or identifier */
  697. if ( (n = find_name(id_buf, formals)) )
  698. add2repl(repl, FORMALP | (char) n);
  699. else {
  700. idp = id_buf;
  701. while (*idp) add2repl(repl, *idp++);
  702. }
  703. } else if (class(c) == STNUM) {
  704. if (blank) {
  705. blank = 0;
  706. add2repl(repl, ' ');
  707. }
  708. add2repl(repl, c);
  709. if (c == '.') {
  710. c = GetChar();
  711. if (class(c) != STNUM) {
  712. continue;
  713. }
  714. add2repl(repl, c);
  715. }
  716. c = GetChar();
  717. while(in_idf(c) || c == '.') {
  718. add2repl(repl, c);
  719. if((c = GetChar()) == 'e' || c == 'E') {
  720. add2repl(repl, c);
  721. c = GetChar();
  722. if (c == '+' || c == '-') {
  723. add2repl(repl, c);
  724. c = GetChar();
  725. }
  726. }
  727. }
  728. } else {
  729. if (blank) {
  730. blank = 0;
  731. add2repl(repl, ' ');
  732. }
  733. add2repl(repl, c);
  734. c = GetChar();
  735. }
  736. }
  737. *length = repl->r_ptr - repl->r_text;
  738. return Realloc(repl->r_text, (unsigned)(repl->r_ptr - repl->r_text +1));
  739. }
  740. /* macroeq() decides whether two macro replacement texts are
  741. identical. This version compares the texts, which occur
  742. as strings, without taking care of the leading and trailing
  743. blanks (spaces and tabs).
  744. */
  745. int macroeq(char *s, char *t)
  746. {
  747. /* skip leading spaces */
  748. while (BLANK(*s)) s++;
  749. while (BLANK(*t)) t++;
  750. /* first non-blank encountered in both strings */
  751. /* The actual comparison loop: */
  752. while (*s && *s == *t)
  753. s++, t++;
  754. /* two cases are possible when arrived here: */
  755. if (*s == '\0') { /* *s == '\0' */
  756. while (BLANK(*t)) t++;
  757. return *t == '\0';
  758. }
  759. else { /* *s != *t */
  760. while (BLANK(*s)) s++;
  761. while (BLANK(*t)) t++;
  762. return (*s == '\0') && (*t == '\0');
  763. }
  764. }
  765. void do_line(unsigned int l)
  766. {
  767. struct token tk;
  768. int t = GetToken(&tk);
  769. if (t != EOF) SkipToNewLine();
  770. LineNumber = l; /* the number of the next input line */
  771. if (t == STRING) /* is there a filespecifier? */
  772. FileName = tk.tk_str;
  773. }