domacro.c 20 KB

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