domacro.c 20 KB

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