domacro.c 19 KB

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