Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1628 lines
43 KiB

  1. /************************************************************************/
  2. /* */
  3. /* RCPP - Resource Compiler Pre-Processor for NT system */
  4. /* */
  5. /* P0MACROS.C - Preprocessor Macros definitions */
  6. /* */
  7. /* 27-Nov-90 w-BrianM Update for NT from PM SDK RCPP */
  8. /* */
  9. /************************************************************************/
  10. #include <windows.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <ctype.h>
  14. #include <malloc.h>
  15. #include "rcpptype.h"
  16. #include "rcppdecl.h"
  17. #include "rcppext.h"
  18. #include "p0defs.h"
  19. #include "charmap.h"
  20. /************************************************************************
  21. **
  22. ** WARNING: gather_chars() depends ELIMIT being the boundary of
  23. ** Macro_buffer.
  24. ************************************************************************/
  25. #define ACT_BUFFER &Macro_buffer[0]
  26. #define EXP_BUFFER &Macro_buffer[BIG_BUFFER * 2]
  27. #define EXP_PAD 5
  28. #define ALIMIT &Macro_buffer[BIG_BUFFER * 2]
  29. #define ELIMIT (&Macro_buffer[BIG_BUFFER * 4] - EXP_PAD)
  30. /************************************************************************
  31. ** actual argument lists are length preceeded strings which are copied
  32. ** into ACT_BUFFER. the first argument is pt'd to by exp_actuals in the
  33. ** expansion_t construct. the next actual is obtained by adding the length
  34. ** of the current actual to the start of the current actual.
  35. ************************************************************************/
  36. #define ACTUAL_SIZE(P) (*(short *)(P))
  37. #define ACTUAL_TEXT(P) ((P) + sizeof(short))
  38. #define ACTUAL_NEXT(P) ((P) + ACTUAL_SIZE(P))
  39. /************************************************************************
  40. ** the formals are copied into the buffer similar to the actuals, except
  41. ** the size is denoted by an unsigned char, instead of short
  42. ************************************************************************/
  43. #define FORMAL_SIZE(P) (*(ptext_t)(P))
  44. #define FORMAL_TEXT(P) ((P) + sizeof(UCHAR))
  45. #define FORMAL_NEXT(P) ((P) + FORMAL_SIZE(P))
  46. expansion_t Macro_expansion[LIMIT_MACRO_DEPTH];
  47. ptext_t P_defn_start;
  48. int N_formals;
  49. pdefn_t Defn_level_0[LEVEL_0 + 1];
  50. /************************************************************************
  51. ** These are needed by p0scanner (Exp_ptr,Tiny_lexer_nesting)
  52. ************************************************************************/
  53. ptext_t Exp_ptr = EXP_BUFFER; /* ptr to free exp space */
  54. int Tiny_lexer_nesting; /* stay in tiny lexer or back to main */
  55. static ptext_t Act_ptr = ACT_BUFFER; /* ptr to free actuals space */
  56. static ptext_t Save_Exp_ptr = EXP_BUFFER; /* for buffering unbal parens */
  57. static ptext_t P_actuals; /* actuals for this (level) macro */
  58. static int N_actuals; /* number of actuals in invocation */
  59. static int Macro_line; /* where we started the macro */
  60. /************************************************************************/
  61. /* Local Function Prototypes */
  62. /************************************************************************/
  63. void chkbuf(ptext_t);
  64. ptext_t do_strformal(void);
  65. ptext_t do_macformal(int *);
  66. void expand_actual(UCHAR);
  67. void expand_definition(void);
  68. void expand_macro(void);
  69. void fatal_in_macro(int);
  70. ptext_t gather_chars(ptext_t, UCHAR);
  71. void get_actuals(pdefn_t, int);
  72. int get_definition(void);
  73. void get_formals(void);
  74. int is_macro_arg(ptext_t);
  75. void move_to_actual(ptext_t, ptext_t);
  76. void move_to_exp(ptext_t);
  77. void move_to_exp_esc(int, ptext_t);
  78. int post_paste(void);
  79. void push_macro(pdefn_t);
  80. int redefn (ptext_t, ptext_t, int);
  81. int rescan_expansion(void);
  82. /************************************************************************
  83. ** UNDEFINE - remove a symbol from the symbol table
  84. ** No noise is made if the programmer attempts to undefine a predefined
  85. ** macro, but it is not done.
  86. ************************************************************************/
  87. void undefine(void)
  88. {
  89. pdefn_t pdef;
  90. pdefn_t prev;
  91. prev = NULL;
  92. pdef = Defn_level_0[Reuse_1_hash & LEVEL_0];
  93. while(pdef) {
  94. if(memcmp (Reuse_1, DEFN_IDENT(pdef), Reuse_1_length) == 0) {
  95. if(PRE_DEFINED(pdef)) {
  96. Msg_Temp = GET_MSG (4112);
  97. SET_MSG (Msg_Text, Msg_Temp, Reuse_1, "#undef");
  98. warning(4112);
  99. break;
  100. }
  101. if(prev == NULL) /* match at head of list */
  102. Defn_level_0[Reuse_1_hash & LEVEL_0] = DEFN_NEXT(pdef);
  103. else
  104. DEFN_NEXT(prev) = DEFN_NEXT(pdef);
  105. break;
  106. }
  107. prev = pdef;
  108. pdef = DEFN_NEXT(pdef);
  109. }
  110. }
  111. /************************************************************************
  112. ** BEGIN DEFINE A MACRO {
  113. ************************************************************************/
  114. void define(void)
  115. {
  116. UCHAR c;
  117. if (! (LX_IS_IDENT(c = skip_cwhite())) ) {
  118. Msg_Temp = GET_MSG (2007);
  119. SET_MSG (Msg_Text, Msg_Temp);
  120. error (2007); /* #define syntax */
  121. skip_cnew();
  122. return;
  123. }
  124. getid(c);
  125. N_formals = 0;
  126. P_defn_start = Macro_buffer;
  127. /*
  128. ** the next character must be white space or an open paren
  129. */
  130. first_switch:
  131. switch(CHARMAP(c = GETCH())) {
  132. case LX_OPAREN: /* we have formal parameters */
  133. get_formals(); /* changes N_formals and fills Macro_buffer */
  134. if(N_formals == 0) {/* empty formal list */
  135. /*
  136. ** we must special case this since the expand() reads in the
  137. ** actual arguments iff there are formal parameters. thus if we
  138. ** #define foo() bar()
  139. ** . . .
  140. ** foo()
  141. ** will expand as
  142. ** bar()()
  143. ** we put the right paren in to fool the expander into looking
  144. ** for actuals.
  145. */
  146. N_formals = -1;
  147. }
  148. break;
  149. case LX_WHITE:
  150. break;
  151. case LX_CR:
  152. goto first_switch;
  153. case LX_SLASH:
  154. if( ! skip_comment()) {
  155. Msg_Temp = GET_MSG (2008);
  156. SET_MSG (Msg_Text, Msg_Temp, '/');
  157. error (2008);
  158. }
  159. break;
  160. case LX_NL: /* no definition */
  161. UNGETCH();
  162. definstall((ptext_t)0, 0, 0);
  163. return;
  164. break;
  165. case LX_EOS:
  166. if(handle_eos() != BACKSLASH_EOS) {
  167. goto first_switch;
  168. }
  169. /* got BACKSLASH_EOS */
  170. /*
  171. ** FALLTHROUGH
  172. */
  173. default:
  174. Msg_Temp = GET_MSG (2008);
  175. SET_MSG (Msg_Text, Msg_Temp, c);
  176. error (2008); /* unexpected character in macro definition */
  177. }
  178. definstall(P_defn_start, get_definition(), N_formals);
  179. }
  180. /************************************************************************
  181. ** get_formals : collect comma separated idents until the first closing paren
  182. ** (the openning paren has already been read)
  183. ** since we can't be in a macro when we're asked for this, we can be assured
  184. ** that we can use a single buffer to collect all the formal names.
  185. ************************************************************************/
  186. void get_formals(void)
  187. {
  188. UCHAR c;
  189. ptext_t p_stop;
  190. ptext_t p_id;
  191. p_id = p_stop = FORMAL_TEXT(P_defn_start);
  192. for(;;) {
  193. switch(CHARMAP(c = skip_cwhite())) {
  194. case LX_ID:
  195. if( p_id != p_stop ) {
  196. Msg_Temp = GET_MSG (2010);
  197. SET_MSG (Msg_Text, Msg_Temp, c);
  198. error (2010);
  199. }
  200. *p_stop++ = c;
  201. for(;;) {
  202. while(LXC_IS_IDENT(c = GETCH())) { /* while an id char */
  203. *p_stop++ = c; /* collect it */
  204. } if(c == EOS_CHAR) {
  205. /*
  206. ** found end of buffer marker, make sure it is,
  207. ** then handle it.
  208. */
  209. if(io_eob()) { /* end of buffer in here is bad */
  210. Msg_Temp = GET_MSG (1004);
  211. SET_MSG (Msg_Text, Msg_Temp);
  212. fatal (1004);
  213. }
  214. continue;
  215. }
  216. if((c == '\\') && (checknl())) {
  217. continue;
  218. }
  219. UNGETCH();
  220. break;
  221. }
  222. *p_stop++ = '\0';
  223. break;
  224. case LX_COMMA:
  225. case LX_CPAREN:
  226. if( p_stop > p_id ) {
  227. /* make sure an identifier was read */
  228. if((p_stop - p_id) >= TINY_BUFFER) {
  229. p_id[TINY_BUFFER - 1] = '\0';
  230. Msg_Temp = GET_MSG (4111);
  231. SET_MSG (Msg_Text, Msg_Temp, p_id);
  232. warning(4011); /* id truncated */
  233. p_stop = p_id + TINY_BUFFER;
  234. }
  235. if(is_macro_arg(p_id) >= 1) {
  236. Msg_Temp = GET_MSG (2009);
  237. SET_MSG (Msg_Text, Msg_Temp, p_id);
  238. error(2009); /* reuse of formal */
  239. }
  240. else {
  241. FORMAL_SIZE(P_defn_start) = (UCHAR)(p_stop - P_defn_start);
  242. P_defn_start = p_stop;
  243. N_formals++;
  244. }
  245. }
  246. else {
  247. if( (CHARMAP(c) == LX_COMMA) || (N_formals > 0) ) {
  248. Msg_Temp = GET_MSG (2010);
  249. SET_MSG (Msg_Text, Msg_Temp, c);
  250. error(2010);
  251. }
  252. }
  253. if( CHARMAP(c) == LX_CPAREN ) {
  254. return;
  255. }
  256. p_id = p_stop = FORMAL_TEXT(P_defn_start);
  257. break;
  258. default:
  259. Msg_Temp = GET_MSG (2010);
  260. SET_MSG (Msg_Text, Msg_Temp, c);
  261. error(2010); /* unexpected char in formal list */
  262. break;
  263. }
  264. }
  265. }
  266. /************************************************************************
  267. ** definstall - Install a new definition. id is in Reuse_1.
  268. ** p_text : ptr to the definition
  269. ** n : number of bytes in the definition (may contain embedded nulls)
  270. ** number : number of formals
  271. ************************************************************************/
  272. void definstall(ptext_t p_text, int n, int number)
  273. {
  274. pdefn_t p;
  275. if(n == 0) {
  276. p_text = NULL;
  277. }
  278. if( strcmp (Reuse_1, "defined") == 0) {
  279. Msg_Temp = GET_MSG (4112);
  280. SET_MSG (Msg_Text, Msg_Temp, Reuse_1, "#define");
  281. warning(4112);/* name reserved */
  282. return;
  283. }
  284. if((p = get_defined()) != 0) {
  285. if(PRE_DEFINED(p)) {
  286. Msg_Temp = GET_MSG (4112);
  287. SET_MSG (Msg_Text, Msg_Temp, Reuse_1, "#define");
  288. warning(4112);/* name reserved */
  289. return;
  290. }
  291. else {
  292. if(redefn(p_text, DEFN_TEXT(p), n)) {
  293. Msg_Temp = GET_MSG (4005);
  294. SET_MSG (Msg_Text, Msg_Temp, Reuse_1);
  295. warning(4005);/* redefinition */
  296. }
  297. else {
  298. return;
  299. }
  300. }
  301. }
  302. else {
  303. hln_t ident;
  304. HLN_NAME(ident) = Reuse_1;
  305. HLN_HASH(ident) = Reuse_1_hash;
  306. HLN_LENGTH(ident) = (UCHAR)Reuse_1_length;
  307. p = malloc(sizeof(defn_t));
  308. if (p == NULL) {
  309. Msg_Temp = GET_MSG (1002);
  310. SET_MSG (Msg_Text, Msg_Temp);
  311. error(1002);
  312. return;
  313. }
  314. DEFN_IDENT(p) = HLN_TO_NAME(&ident);
  315. DEFN_NEXT(p) = Defn_level_0[Reuse_1_hash & LEVEL_0];
  316. DEFN_TEXT(p) = (char*)NULL;
  317. DEFN_EXPANDING(p) = 0;
  318. Defn_level_0[Reuse_1_hash & LEVEL_0] = p;
  319. }
  320. if(n != 0) {
  321. DEFN_TEXT(p) = pstrndup(p_text, n);
  322. if(number == FROM_COMMAND) { /* special case from cmd line */
  323. *(DEFN_TEXT(p) + n - 1) = EOS_DEFINITION; /* for handle_eos */
  324. }
  325. }
  326. DEFN_NFORMALS(p) = (char)((number != FROM_COMMAND) ? number : 0);
  327. }
  328. /************************************************************************
  329. ** get_defined : is the given id in the macro symbol table?
  330. ** return a ptr to it if so, NULL if not.
  331. ************************************************************************/
  332. pdefn_t get_defined(void)
  333. {
  334. pdefn_t pdef;
  335. for( pdef = Defn_level_0[Reuse_1_hash & LEVEL_0]; pdef;
  336. pdef = DEFN_NEXT(pdef)) {
  337. if(memcmp (Reuse_1, DEFN_IDENT(pdef), Reuse_1_length) == 0) {
  338. return(pdef);
  339. }
  340. }
  341. return(NULL);
  342. }
  343. /************************************************************************
  344. ** redefn : are the two definitions the same?
  345. ************************************************************************/
  346. int redefn(REG ptext_t p_new, ptext_t p_old, int n)
  347. {
  348. if(p_old && p_new) {
  349. if(strncmp(p_new, p_old, n) == 0) { /* strings are exact */
  350. return(FALSE);
  351. }
  352. return(TRUE);
  353. }
  354. return((p_old != NULL) || (p_new != NULL));
  355. }
  356. /************************************************************************
  357. ** get_definition : accumulate the macro definition, stops when it finds
  358. ** a newline (it uses it). returns a ptr to the end of the string it builds.
  359. ** builds the string in Macro_buffer. (given the start in P_defn_start)
  360. ************************************************************************/
  361. int get_definition(void)
  362. {
  363. REG ptext_t p;
  364. UCHAR c;
  365. int stringize = FALSE;
  366. int charize = FALSE;
  367. p = P_defn_start;
  368. c = skip_cwhite();
  369. for(;;) {
  370. chkbuf(p);
  371. switch(CHARMAP(c)) {
  372. case LX_EOS:
  373. if(handle_eos() == BACKSLASH_EOS) {
  374. /* got backslash EOS */
  375. /* \<anything else> goes out as is. The <anything else>
  376. * character must be emitted now, so that
  377. * #define FOO(name) \name
  378. * . . .
  379. * FOO(bar)
  380. *
  381. * does NOT see occurence of name in the definition as an
  382. * occurence of the formal param and emit \bar when it is
  383. * expanded later,but if the definition is \nname it will
  384. * find name as a formal paramater and emit \nbar
  385. */
  386. *p++ = c; /* put in backslash, break'll add new char */
  387. c = get_non_eof();
  388. }
  389. else {
  390. c = GETCH();
  391. continue;
  392. }
  393. break;
  394. case LX_NL: /* only way out */
  395. UNGETCH();
  396. if(p == P_defn_start) {
  397. return(0);
  398. }
  399. chkbuf(p);
  400. *p++ = EOS_CHAR;
  401. *p++ = EOS_DEFINITION; /* tells handle_eos defn finished */
  402. return((int)(p - P_defn_start));/* p's last incr counts the 0*/
  403. break;
  404. case LX_DQUOTE:
  405. case LX_SQUOTE:
  406. p = gather_chars(p, c);
  407. c = GETCH();
  408. continue;
  409. break;
  410. case LX_POUND:
  411. split_op:
  412. switch(CHARMAP(GETCH())) {
  413. case LX_POUND:
  414. /*
  415. ** handle ## processing. cant be the first or the last.
  416. */
  417. if(p == P_defn_start) {
  418. Msg_Temp = GET_MSG (2160);
  419. SET_MSG (Msg_Text, Msg_Temp);
  420. error(2160); /* ## not allowed as first entry */
  421. continue;
  422. }
  423. if(*(p - 1) == ' ') { /* hose the last blank */
  424. p--;
  425. }
  426. if(CHARMAP(c = skip_cwhite()) == LX_NL) {
  427. UNGETCH();
  428. Msg_Temp = GET_MSG (2161);
  429. SET_MSG (Msg_Text, Msg_Temp);
  430. error(2161);
  431. continue;
  432. }
  433. /* this case does *not* fall through to LX_ID */
  434. continue;
  435. break;
  436. case LX_EACH:
  437. charize = TRUE;
  438. break;
  439. case LX_EOS:
  440. if( handle_eos() != BACKSLASH_EOS ) {
  441. goto split_op;
  442. }
  443. /*
  444. ** FALLTHROUGH
  445. */
  446. default:
  447. UNGETCH();
  448. stringize = TRUE;
  449. break;
  450. }
  451. if(CHARMAP(c = skip_cwhite()) != LX_ID) {
  452. Msg_Temp = GET_MSG (2162);
  453. SET_MSG (Msg_Text, Msg_Temp);
  454. error(2162); /* must have id following */
  455. continue;
  456. }
  457. /*
  458. ** FALLTHROUGH
  459. */
  460. case LX_ID:
  461. {
  462. /* we have the start of an identifier - check it to see if
  463. * its an occurence of a formal parameter name.
  464. * we gather the id ourselves (instead of getid()) since this
  465. * wil save us from having to copy it to our string if it's
  466. * not a formal parameter.
  467. */
  468. int n;
  469. ptext_t p_macformal;
  470. p_macformal = p;
  471. do {
  472. chkbuf(p);
  473. *p++ = c;
  474. get_more_id:
  475. c = GETCH();
  476. } while(LXC_IS_IDENT(c));
  477. if(CHARMAP(c) == LX_EOS) {
  478. if(handle_eos() != BACKSLASH_EOS) {
  479. goto get_more_id;
  480. }
  481. }
  482. *p = '\0'; /* term. string, but do not advance ptr */
  483. if((n = is_macro_arg(p_macformal)) >= 1) {
  484. /*
  485. ** this is an occurance of formal 'n', replace the id with
  486. ** the special MAC character.
  487. */
  488. p = p_macformal;
  489. if(stringize) {
  490. *p++ = LX_FORMALSTR;
  491. }
  492. else {
  493. if(charize) {
  494. *p++ = LX_FORMALCHAR;
  495. }
  496. else {
  497. *p++ = LX_FORMALMARK;
  498. }
  499. }
  500. *p++ = (UCHAR) n;
  501. }
  502. else if(charize || stringize) {
  503. Msg_Temp = GET_MSG (2162);
  504. SET_MSG (Msg_Text, Msg_Temp);
  505. error(2162);
  506. }
  507. stringize = FALSE;
  508. charize = FALSE;
  509. continue; /* we broke out of the loop with a new char */
  510. }
  511. case LX_SLASH:
  512. if( ! skip_comment() ) { /* really is a slash */
  513. break;
  514. }
  515. /*
  516. ** FALLTHROUGH
  517. */
  518. case LX_CR:
  519. case LX_WHITE:
  520. /*
  521. ** this is white space, all contiguous whitespace is transformed
  522. ** to 1 blank. (hence the skip_cwhite() and the continue).
  523. */
  524. if(CHARMAP(c = skip_cwhite()) != LX_NL) {
  525. *p++ = ' ';
  526. }
  527. continue; /* restart loop */
  528. case LX_ILL:
  529. Msg_Temp = GET_MSG (2018);
  530. SET_MSG (Msg_Text, Msg_Temp, c);
  531. error(2018);
  532. c = GETCH();
  533. continue;
  534. }
  535. *p++ = c;
  536. c = GETCH();
  537. }
  538. }
  539. /************************************************************************/
  540. /* is_macro_arg () */
  541. /************************************************************************/
  542. int is_macro_arg(ptext_t name)
  543. {
  544. REG int i;
  545. REG ptext_t p;
  546. p = Macro_buffer;
  547. for(i = 1; i <= N_formals; i++) {
  548. if( strcmp(name, FORMAL_TEXT(p)) == 0) {
  549. return(i);
  550. }
  551. p = FORMAL_NEXT(p);
  552. }
  553. return(-1);
  554. }
  555. /************************************************************************/
  556. /* chkbuf () */
  557. /************************************************************************/
  558. void chkbuf(ptext_t p)
  559. {
  560. if( p >= ELIMIT ) {
  561. Msg_Temp = GET_MSG (1065);
  562. SET_MSG (Msg_Text, Msg_Temp, Reuse_1);
  563. fatal (1065);
  564. }
  565. }
  566. /************************************************************************
  567. ** gather_chars : collect chars until a matching one is found.
  568. ** skip backslashed chars. moves the chars into the buffer,
  569. ** returns a ptr past the last char copied.
  570. ************************************************************************/
  571. ptext_t gather_chars(REG ptext_t p, UCHAR match_c)
  572. {
  573. UCHAR c;
  574. *p++ = match_c;
  575. for(;;) {
  576. if(p > ELIMIT) {
  577. return(ELIMIT);
  578. }
  579. switch(CHARMAP(c = GETCH())) {
  580. case LX_NL:
  581. Msg_Temp = GET_MSG (2001);
  582. SET_MSG (Msg_Text, Msg_Temp);
  583. error(2001);
  584. UNGETCH();
  585. c = match_c;
  586. /*
  587. ** FALLTHROUGH
  588. */
  589. case LX_DQUOTE:
  590. case LX_SQUOTE:
  591. if(c == match_c) {
  592. *p++ = c;
  593. return(p); /* only way out */
  594. }
  595. break;
  596. case LX_EOS:
  597. if(handle_eos() != BACKSLASH_EOS) {
  598. continue;
  599. }
  600. else {
  601. /* got backslash */
  602. *p++ = '\\';
  603. c = get_non_eof();
  604. if((c == '\\') && (checknl())) {
  605. continue;
  606. }
  607. }
  608. break;
  609. case LX_LEADBYTE:
  610. *p++ = c;
  611. c = get_non_eof();
  612. break;
  613. }
  614. *p++ = c;
  615. }
  616. }
  617. /************************************************************************
  618. ** END DEFINING MACROS }
  619. ************************************************************************/
  620. /************************************************************************
  621. ** BEGIN EXPANDING MACROS {
  622. ************************************************************************/
  623. /************************************************************************
  624. ** can_expand: tries to expand the macro passed to it - returns
  625. ** true if it succeeded in expanding it. It will only return FALSE
  626. ** if a macro name was found, a paren was expected, and a paren was
  627. ** not the next non white character.
  628. ************************************************************************/
  629. int can_expand(pdefn_t pdef)
  630. {
  631. UCHAR c;
  632. int n_formals;
  633. int return_value = FALSE;
  634. Tiny_lexer_nesting = 0;
  635. Save_Exp_ptr = Exp_ptr; /* not necessarily EXP_BUFFER */
  636. Macro_line = Linenumber;
  637. expand_name:
  638. P_actuals = Act_ptr;
  639. N_actuals = 0;
  640. n_formals = DEFN_NFORMALS(pdef);
  641. if( PRE_DEFINED(pdef) ) {
  642. push_macro(pdef);
  643. DEFN_EXPANDING(CURRENT_MACRO)++;
  644. if(rescan_expansion()) {
  645. return(TRUE); /* could expand macro */
  646. }
  647. }
  648. else if( n_formals == 0 ) {
  649. return_value = TRUE;
  650. if(DEFN_TEXT(pdef)) {
  651. push_macro(pdef);
  652. expand_definition();
  653. }
  654. else {
  655. /*
  656. ** Macro expands to nothing (no definition). Since it
  657. ** didn't have any actuals, Act_ptr is already correct.
  658. ** Exp_ptr must be changed however to delete the
  659. ** identifier from the expanded text.
  660. */
  661. Exp_ptr = Save_Exp_ptr;
  662. }
  663. }
  664. else {
  665. if( n_formals == -1 ) {
  666. n_formals = 0;
  667. }
  668. name_comment_paren:
  669. if( can_get_non_white()) {
  670. if(CHARMAP(CHECKCH()) == LX_SLASH) {
  671. SKIPCH();
  672. if(skip_comment()) {
  673. goto name_comment_paren;
  674. }
  675. else {
  676. UNGETCH();
  677. }
  678. }
  679. if(CHARMAP(CHECKCH())==LX_OPAREN) {
  680. SKIPCH();
  681. return_value = TRUE;
  682. get_actuals(pdef, n_formals);
  683. }
  684. else {
  685. /*
  686. ** #define xx(a) a
  687. ** xx bar();
  688. ** don't lose white space between "xx" and "bar"
  689. */
  690. ptext_t p = Exp_ptr;
  691. push_macro(pdef);
  692. DEFN_EXPANDING(CURRENT_MACRO)++;
  693. Exp_ptr = p;
  694. if( rescan_expansion() ) {
  695. return(FALSE);
  696. }
  697. }
  698. }
  699. else {
  700. }
  701. }
  702. /*
  703. ** makes sure a macro is being worked on. At this point, there will
  704. ** be a macro to expand, unless the macro expand_the_named_macro was
  705. ** passed had no definition text. If it had no defintion text,
  706. ** Tiny_lexer_nesting was not incremented.
  707. */
  708. while(Tiny_lexer_nesting != 0) {
  709. if(Exp_ptr >= ELIMIT) {
  710. fatal_in_macro(10056);
  711. }
  712. switch(CHARMAP(c = GETCH())) {
  713. case LX_ID:
  714. case LX_MACFORMAL:
  715. Save_Exp_ptr = Exp_ptr;
  716. if(tl_getid(c) && ((pdef = get_defined())!= 0)) {
  717. if(DEFN_EXPANDING(pdef)) {
  718. /*
  719. ** the macro is already being expanded, so just
  720. ** write the do not expand marker and the
  721. ** identifier to the expand area. The do not
  722. ** expand marker is necessary so this macro
  723. ** doesn't get expanded on the rescan
  724. */
  725. int len = Reuse_1_length - 1;
  726. *Exp_ptr++ = LX_NOEXPANDMARK;
  727. *Exp_ptr++ = ((UCHAR)len);
  728. }
  729. else {
  730. /*
  731. ** a legal identifier was read, it is defined, and
  732. ** it is not currently being expanded. This means
  733. ** there is reason to believe it can be expanded.
  734. */
  735. goto expand_name;
  736. }
  737. }
  738. if(InIf &&(memcmp(Reuse_1, "defined", 8) ==0)) {
  739. do_defined(Reuse_1);
  740. }
  741. continue;
  742. break;
  743. case LX_NUMBER:
  744. /* getnum with Prep on to keep leading 0x on number */
  745. {
  746. int Save_prep = Prep;
  747. Prep = TRUE;
  748. getnum(c);
  749. Prep = Save_prep;
  750. }
  751. continue;
  752. break;
  753. case LX_DOT:
  754. *Exp_ptr++ = '.';
  755. dot_switch:
  756. switch(CHARMAP(c = GETCH())) {
  757. case LX_EOS:
  758. if(handle_eos() != BACKSLASH_EOS) {
  759. if(Tiny_lexer_nesting > 0) {
  760. goto dot_switch;
  761. }
  762. continue;
  763. }
  764. break;
  765. case LX_DOT:
  766. *Exp_ptr++ = '.';
  767. if( ! checkop('.')) {
  768. break; /* error will be caught on rescan */
  769. }
  770. *Exp_ptr++ = '.';
  771. continue;
  772. break;
  773. case LX_NUMBER:
  774. *Exp_ptr++ = c;
  775. get_real(Exp_ptr);
  776. continue;
  777. }
  778. UNGETCH();
  779. continue;
  780. case LX_CHARFORMAL:
  781. move_to_exp_esc('\'', do_strformal());
  782. continue;
  783. break;
  784. case LX_STRFORMAL:
  785. move_to_exp_esc('"', do_strformal());
  786. continue;
  787. break;
  788. case LX_DQUOTE:
  789. case LX_SQUOTE:
  790. /*
  791. ** gather_chars is called even though the error reported
  792. ** on overflow may need to be changed.
  793. */
  794. Exp_ptr = gather_chars(Exp_ptr, c);
  795. continue;
  796. break;
  797. case LX_WHITE:
  798. while(LXC_IS_WHITE(GETCH())) {
  799. ;
  800. }
  801. UNGETCH();
  802. c = ' ';
  803. break;
  804. case LX_EOS:
  805. if(handle_eos() == BACKSLASH_EOS) {
  806. *Exp_ptr++ = c;
  807. c = GETCH();
  808. break;
  809. }
  810. continue;
  811. break;
  812. }
  813. *Exp_ptr++ = c;
  814. }
  815. return(return_value);
  816. }
  817. /************************************************************************
  818. ** get_actuals : Paren must already be found. If all the actuals can
  819. ** be read, the macro is pushed and expansion begins. Otherwise,
  820. ** this function is quickly exited and lets the tiny lexer take
  821. ** care of rescanning.
  822. ************************************************************************/
  823. void get_actuals(pdefn_t pdef, int n_formals)
  824. {
  825. /*
  826. ** The only concern with this is that a rescan could finish while
  827. ** this is trying to collect actuals. When a rescan finishes, it
  828. ** may reset Act_ptr and Exp_ptr. Unless these are saved before the
  829. ** end of rescan is handled, the part of the actual collected so far
  830. ** would be lost.
  831. */
  832. REG ptext_t start;
  833. UCHAR c;
  834. ptext_t actuals_start;
  835. int paste;
  836. int level;
  837. *Exp_ptr++ = PREVCH(); /* must be oparen */
  838. level = 0;
  839. actuals_start = Act_ptr;
  840. while( level >= 0) {
  841. if(Exp_ptr >= ELIMIT) {
  842. fatal_in_macro(10056);
  843. }
  844. more_white:
  845. if( ! can_get_non_white()) {
  846. return;
  847. }
  848. if(CHARMAP(CHECKCH()) == LX_SLASH) {
  849. SKIPCH();
  850. if(skip_comment()) {
  851. goto more_white;
  852. }
  853. else {
  854. start = Exp_ptr;
  855. *Exp_ptr++ = '/';
  856. }
  857. }
  858. else {
  859. start = Exp_ptr;
  860. }
  861. paste = FALSE;
  862. for(;;) {
  863. switch(CHARMAP(c = GETCH())) {
  864. case LX_CPAREN:
  865. if(--level < 0) {
  866. goto leave_loop;
  867. }
  868. break;
  869. case LX_COMMA:
  870. /*
  871. ** if the comma is not at level == 0, it is part of
  872. ** a parenthesized list and not a delimiter
  873. */
  874. if(level == 0) {
  875. goto leave_loop;
  876. }
  877. break;
  878. case LX_SLASH:
  879. if( ! skip_comment()) {
  880. break;
  881. }
  882. if(*(Exp_ptr - 1) == ' ') {
  883. continue;
  884. }
  885. c = ' ';
  886. break;
  887. case LX_CR:
  888. case LX_NL:
  889. case LX_WHITE:
  890. UNGETCH(); /* This char is valid white space */
  891. if( ! can_get_non_white()) {
  892. return;
  893. }
  894. continue;
  895. break;
  896. case LX_OPAREN:
  897. ++level;
  898. break;
  899. case LX_DQUOTE:
  900. case LX_SQUOTE:
  901. Exp_ptr = gather_chars(Exp_ptr, c);
  902. continue;
  903. break;
  904. case LX_ID:
  905. *Exp_ptr++ = c;
  906. while(LXC_IS_IDENT(c = GETCH())) {
  907. if(Exp_ptr >= ELIMIT) {
  908. fatal_in_macro(10056);
  909. }
  910. *Exp_ptr++ = c;
  911. }
  912. if(CHARMAP(c) != LX_MACFORMAL) {
  913. UNGETCH();
  914. continue;
  915. }
  916. paste = TRUE;
  917. /*
  918. ** FALLTHROUGH
  919. */
  920. case LX_MACFORMAL:
  921. move_to_exp(do_macformal(&paste));
  922. continue;
  923. break;
  924. case LX_STRFORMAL:
  925. move_to_exp_esc('"', do_strformal());
  926. continue;
  927. break;
  928. case LX_CHARFORMAL:
  929. move_to_exp_esc('\'', do_strformal());
  930. continue;
  931. break;
  932. case LX_EOS:
  933. /*
  934. ** Will saving this pointers create dead space in the
  935. ** buffers? Yes, but only temporarily.
  936. **
  937. ** handle_eos() may reset Act_ptr and Exp_ptr to the
  938. ** beginning of the buffers if a rescan is finishing
  939. ** and Macro_depth is going to be 0. ANSI allows
  940. ** actuals to start within a macro defintion and be
  941. ** completed (further actuals and closing paren) later
  942. ** in the text.
  943. **
  944. ** These buffer pointers will eventually be reset to
  945. ** the beginnings of their respective buffers when the
  946. ** macro for the actuals being collected right now
  947. ** finish rescan
  948. **
  949. ** This is special handling for folks who use
  950. ** unbalanced parens in macro definitions
  951. */
  952. {
  953. ptext_t Exp_save;
  954. ptext_t Act_save;
  955. int eos_res;
  956. Exp_save = Exp_ptr;
  957. Act_save = Act_ptr;
  958. if((eos_res = handle_eos()) & (ACTUAL_EOS | RESCAN_EOS)) {
  959. return;
  960. }
  961. Act_ptr = Act_save;
  962. Exp_ptr = Exp_save;
  963. if(eos_res == BACKSLASH_EOS) { /* ??? DFP QUESTION */
  964. *Exp_ptr++ = c; /* save the \ */
  965. c = get_non_eof(); /* get char following \ */
  966. break;
  967. }
  968. }
  969. continue;
  970. break;
  971. }
  972. *Exp_ptr++ = c;
  973. }
  974. leave_loop:
  975. /*
  976. ** if the last character was whitespace, hose it
  977. */
  978. if(CHARMAP(*(Exp_ptr - 1)) == LX_WHITE) {
  979. Exp_ptr--;
  980. }
  981. /*
  982. ** if Exp_ptr <= start, foo() was read, don't incr N_actuals
  983. */
  984. if(Exp_ptr > start) {
  985. N_actuals++;
  986. move_to_actual(start, Exp_ptr);
  987. }
  988. *Exp_ptr++ = c;
  989. }
  990. P_actuals = actuals_start;
  991. if(n_formals < N_actuals) {
  992. Msg_Temp = GET_MSG (4002);
  993. SET_MSG (Msg_Text, Msg_Temp, Reuse_1);
  994. warning(4002);
  995. }
  996. else if(n_formals > N_actuals) {
  997. Msg_Temp = GET_MSG (4003);
  998. SET_MSG (Msg_Text, Msg_Temp, Reuse_1);
  999. warning(4003);
  1000. }
  1001. if(DEFN_TEXT(pdef)) {
  1002. push_macro(pdef);
  1003. expand_macro();
  1004. }
  1005. else {
  1006. /*
  1007. ** the macro expands to nothing (no definition)
  1008. ** This essentially means delete the macro and its actuals
  1009. ** from the expanded text
  1010. */
  1011. Act_ptr = P_actuals; /* reset pointer to get rid of actuals */
  1012. Exp_ptr = Save_Exp_ptr; /* delete macro & actuals from exp text */
  1013. }
  1014. }
  1015. /************************************************************************
  1016. ** rescan_expansion: pops a level off of tiny lexer. If this is the
  1017. ** original macro called, the rescan is set up, otherwise the MACRO
  1018. ** (not only the tiny lexer level) is popped.
  1019. ************************************************************************/
  1020. int rescan_expansion(void)
  1021. {
  1022. if(--Tiny_lexer_nesting == 0) {
  1023. if(Exp_ptr >= ELIMIT) {
  1024. fatal_in_macro(10056);
  1025. }
  1026. *Exp_ptr++ = EOS_CHAR;
  1027. *Exp_ptr++ = EOS_RESCAN;
  1028. Current_char = CURRENT_TEXT;
  1029. return(TRUE); /* rescan the expanded text */
  1030. }
  1031. else {
  1032. /* reset Current_char, pop the macro */
  1033. Current_char = CURRENT_STRING;
  1034. Act_ptr = CURRENT_ACTUALS; /* don't need its actuals */
  1035. DEFN_EXPANDING(CURRENT_MACRO)--;
  1036. --Macro_depth;
  1037. return(FALSE); /* do not rescan expanded text */
  1038. }
  1039. }
  1040. /************************************************************************
  1041. ** move_to_actual: moves the string located between start and finish
  1042. ** inclusive to the current location in ACT_BUFFER as a new actual.
  1043. ************************************************************************/
  1044. void move_to_actual(ptext_t start, ptext_t finish)
  1045. {
  1046. REG ptext_t p;
  1047. REG int len;
  1048. len = (int)(finish - start);
  1049. if(Act_ptr + len >= ALIMIT - 2) {
  1050. fatal_in_macro(10056);
  1051. }
  1052. strncpy(ACTUAL_TEXT(Act_ptr), start, len);
  1053. p = ACTUAL_TEXT(Act_ptr) + len;
  1054. if ((((ULONG_PTR)p) & 1) == 0) {
  1055. *p++ = EOS_CHAR;
  1056. *p++ = EOS_ACTUAL;
  1057. }
  1058. else {
  1059. *p++ = EOS_CHAR;
  1060. *p++ = EOS_PAD;
  1061. *p++ = EOS_ACTUAL;
  1062. }
  1063. ACTUAL_SIZE(Act_ptr) = (short)(p - Act_ptr);
  1064. Act_ptr = p;
  1065. }
  1066. /************************************************************************
  1067. ** move_to_exp_esc: moves zero terminated string starting at source to
  1068. ** the current position in EXP_BUFFER, with quotes placed around the
  1069. ** string and interior backslashes and dquotes are escaped with a
  1070. ** backslash. The terminating null should not be copied. The null
  1071. ** does not come from the property of a string, but rather is the
  1072. ** marker used to indicate there is no more actual.
  1073. ************************************************************************/
  1074. void move_to_exp_esc(int quote_char, REG ptext_t source)
  1075. {
  1076. int mapped_c;
  1077. int mapped_quote;
  1078. int in_quoted = FALSE;
  1079. if( ! source ) {
  1080. return;
  1081. }
  1082. *Exp_ptr++ = (char)quote_char;
  1083. for(;;) {
  1084. if(Exp_ptr >= ELIMIT) {
  1085. fatal_in_macro(10056);
  1086. }
  1087. switch(mapped_c = CHARMAP(*source)) {
  1088. case LX_EOS:
  1089. if(*source == EOS_CHAR) {
  1090. goto leave_move_stringize;
  1091. }
  1092. /* got BACKSLASH */
  1093. /* but it can't be backslash-newline combination because
  1094. ** we are reprocessing text already read in
  1095. */
  1096. if(in_quoted) {
  1097. *Exp_ptr++ = '\\';
  1098. }
  1099. break;
  1100. break;
  1101. case LX_DQUOTE:
  1102. if(CHARMAP(quote_char) == LX_DQUOTE) {
  1103. *Exp_ptr++ = '\\';
  1104. }
  1105. /*
  1106. ** FALLTHROUGH
  1107. */
  1108. case LX_SQUOTE:
  1109. if(CHARMAP(quote_char) == LX_SQUOTE) {
  1110. break;
  1111. }
  1112. if(in_quoted ) {
  1113. if(mapped_c == mapped_quote) {
  1114. in_quoted = FALSE;
  1115. }
  1116. }
  1117. else {
  1118. in_quoted = TRUE;
  1119. mapped_quote = mapped_c;
  1120. }
  1121. break;
  1122. case LX_LEADBYTE:
  1123. *Exp_ptr++ = *source++;
  1124. break;
  1125. }
  1126. *Exp_ptr++ = *source++;
  1127. }
  1128. leave_move_stringize:
  1129. *Exp_ptr++ = (char)quote_char;
  1130. }
  1131. /************************************************************************
  1132. ** move_to_exp: moves zero terminated string starting at source to
  1133. ** the current position in EXP_BUFFER. The terminating null should
  1134. ** not be copied.
  1135. ************************************************************************/
  1136. void move_to_exp(REG ptext_t source)
  1137. {
  1138. if( ! source ) {
  1139. return;
  1140. }
  1141. while( *source ) {
  1142. if(Exp_ptr >= ELIMIT) {
  1143. fatal_in_macro(10056);
  1144. }
  1145. *Exp_ptr++ = *source++;
  1146. }
  1147. }
  1148. /************************************************************************
  1149. ** push_macro: pushes macro information onto the macro stack.
  1150. ** Information such as the current location in the Exp and Act buffers
  1151. ** will be used by whatever macros this one may call.
  1152. ************************************************************************/
  1153. void push_macro(pdefn_t pdef)
  1154. {
  1155. /*
  1156. ** note that increment leaves element 0 of the macro stack unused.
  1157. ** this element can be reserved for links to dynamically allocated
  1158. ** macro expansion stacks, if they become desirable
  1159. */
  1160. if(++Macro_depth > LIMIT_MACRO_DEPTH) {
  1161. Msg_Temp = GET_MSG (1009);
  1162. SET_MSG (Msg_Text, Msg_Temp, Reuse_1);
  1163. fatal (1009);
  1164. }
  1165. Tiny_lexer_nesting++;
  1166. CURRENT_MACRO = pdef;
  1167. CURRENT_ACTUALS = P_actuals;
  1168. CURRENT_NACTUALS = (UCHAR)N_actuals;
  1169. CURRENT_NACTSEXPANDED = 0;
  1170. CURRENT_STRING = Current_char;
  1171. CURRENT_TEXT = Exp_ptr = Save_Exp_ptr;
  1172. }
  1173. /************************************************************************
  1174. **expand_definition: sets the input stream to start reading from
  1175. ** the macro definition. Also marks the macro as in the process of
  1176. ** expanding so if it eventually invokes itself, it will not expand
  1177. ** the new occurence.
  1178. ************************************************************************/
  1179. void expand_definition(void)
  1180. {
  1181. Current_char = DEFN_TEXT(CURRENT_MACRO);
  1182. DEFN_EXPANDING(CURRENT_MACRO)++;
  1183. }
  1184. /************************************************************************
  1185. **expand_actual: sets the input stream to start reading from
  1186. ** the actual specified in actual.
  1187. ************************************************************************/
  1188. void expand_actual(UCHAR actual)
  1189. {
  1190. ptext_t p;
  1191. p = CURRENT_ACTUALS;
  1192. while(--actual) {
  1193. p = ACTUAL_NEXT(p);
  1194. }
  1195. Current_char = ACTUAL_TEXT(p);
  1196. }
  1197. /************************************************************************
  1198. ** expand_macro: if there are still actuals for this macro to be
  1199. ** expanded, the next one is set up, otherwise this sets up to
  1200. ** expand the macro definition
  1201. ************************************************************************/
  1202. void expand_macro(void)
  1203. {
  1204. if(CURRENT_NACTUALS > CURRENT_NACTSEXPANDED) {
  1205. expand_actual(++CURRENT_NACTSEXPANDED);
  1206. }
  1207. else {
  1208. expand_definition();
  1209. }
  1210. }
  1211. /************************************************************************
  1212. **post_paste: looks ahead one character to find out if a paste has
  1213. ** been requested immediately after this identifier. If the next
  1214. ** character can continue an identifier, or is the macformal marker,
  1215. ** a paste should be done. This is called after a macformal is found
  1216. ** to find out if the expanded or unexpanded actual should be used.
  1217. ************************************************************************/
  1218. int post_paste(void)
  1219. {
  1220. UCHAR c;
  1221. if((CHARMAP(c = GETCH()) == LX_MACFORMAL) || (LXC_IS_IDENT(c))) {
  1222. UNGETCH();
  1223. return(TRUE);
  1224. }
  1225. UNGETCH();
  1226. return(FALSE);
  1227. }
  1228. /************************************************************************
  1229. **do_macformal: This function is called after a macformal marker is
  1230. ** found. It reads the next character to find out which macformal is
  1231. ** wanted. Then it checks to see if a paste is wanted, to find out
  1232. ** if the expanded or unexpanded actual should be used. The return
  1233. ** value is a pointer to the text of the actual wanted, or NULL if the
  1234. ** actual asked for was not provided.
  1235. ************************************************************************/
  1236. ptext_t do_macformal(int *pre_paste)
  1237. {
  1238. UCHAR n;
  1239. ptext_t p;
  1240. int temp_paste;
  1241. p = CURRENT_ACTUALS;
  1242. n = GETCH();
  1243. if(n > CURRENT_NACTUALS) {
  1244. return(NULL); /* already output warning */
  1245. }
  1246. temp_paste = post_paste();
  1247. if(( ! (*pre_paste)) && ( ! temp_paste) ) {
  1248. /*
  1249. ** if the programmer provided x actuals, actuals x+1 to 2x are
  1250. ** those actuals expanded
  1251. */
  1252. n += CURRENT_NACTUALS;
  1253. }
  1254. *pre_paste = temp_paste;
  1255. if (n != 0)
  1256. while(--n) {
  1257. p = ACTUAL_NEXT(p);
  1258. }
  1259. return(ACTUAL_TEXT(p));
  1260. }
  1261. /************************************************************************
  1262. **tl_getid: This function reads an identifier for the tiny lexer
  1263. ** into EXP_BUFFER. if macformal is found, the text of that actual
  1264. ** (expanded or not) is appended to the identifier. It is possible
  1265. ** that this text will contain characters that are not legal
  1266. ** identifiers so return value is whether checking to see if the
  1267. ** "identifier" is defined is worth the bother.
  1268. ************************************************************************/
  1269. int tl_getid(UCHAR c)
  1270. {
  1271. UCHAR *p;
  1272. int paste;
  1273. int legal_identifier;
  1274. int length = 0;
  1275. p = Exp_ptr;
  1276. paste = FALSE;
  1277. legal_identifier = TRUE;
  1278. do_handle_macformal:
  1279. if(CHARMAP(c) == LX_MACFORMAL) {
  1280. ptext_t p_buf;
  1281. if((p_buf = do_macformal(&paste)) != 0) {
  1282. while( *p_buf ) {
  1283. if( ! LXC_IS_IDENT(*p_buf)) {
  1284. legal_identifier = FALSE;
  1285. }
  1286. if(Exp_ptr >= ELIMIT) {
  1287. fatal_in_macro(10056);
  1288. }
  1289. *Exp_ptr++ = *p_buf++;
  1290. }
  1291. }
  1292. }
  1293. else {
  1294. *Exp_ptr++ = c;
  1295. }
  1296. do_handle_eos:
  1297. while(LXC_IS_IDENT(c = GETCH())) {
  1298. if(Exp_ptr >= ELIMIT) {
  1299. fatal_in_macro(10056);
  1300. }
  1301. *Exp_ptr++ = c;
  1302. }
  1303. if(CHARMAP(c) == LX_NOEXPAND) {
  1304. length = (int)GETCH(); /* just skip length */
  1305. goto do_handle_eos;
  1306. }
  1307. if(CHARMAP(c) == LX_MACFORMAL) {
  1308. paste = TRUE;
  1309. goto do_handle_macformal;
  1310. }
  1311. UNGETCH();
  1312. if(legal_identifier && (length == (Exp_ptr - p))) {
  1313. legal_identifier = FALSE;
  1314. }
  1315. if(legal_identifier) {
  1316. if(((Exp_ptr - p) > LIMIT_ID_LENGTH) && ( ! Prep)) {
  1317. Exp_ptr = &p[LIMIT_ID_LENGTH];
  1318. *Exp_ptr = '\0'; /* terminates identifier for warning */
  1319. Msg_Temp = GET_MSG (4011);
  1320. SET_MSG (Msg_Text, Msg_Temp, p);
  1321. warning(4011); /* id truncated */
  1322. }
  1323. else {
  1324. *Exp_ptr = '\0'; /* terminates identifier for expandable check */
  1325. }
  1326. /*
  1327. ** Whether or not we are doing Prep output, we still have to make
  1328. ** sure the identifier will fit in Reuse_1
  1329. */
  1330. if((Exp_ptr - p) > sizeof(Reuse_1)) {
  1331. Exp_ptr = &p[LIMIT_ID_LENGTH];
  1332. *Exp_ptr = '\0';
  1333. Msg_Temp = GET_MSG (4011);
  1334. SET_MSG (Msg_Text, Msg_Temp, p);
  1335. warning(4011);
  1336. }
  1337. /*
  1338. ** copy into Reuse_1 for warnings about mismatched number of
  1339. ** formals/actuals, and in case it's not expandable
  1340. */
  1341. memcpy(Reuse_1, p, (int)((Exp_ptr - p) + 1));
  1342. Reuse_1_hash = local_c_hash(Reuse_1);
  1343. /*
  1344. ** the characters from Exp_ptr to p inclusive do not include the
  1345. ** the hash character, the length character, and the terminating
  1346. ** null.
  1347. */
  1348. Reuse_1_length = (UCHAR)((Exp_ptr - p) + 1);
  1349. }
  1350. return(legal_identifier);
  1351. }
  1352. /************************************************************************
  1353. ** do_strformal: returns pointer to the actual requested without
  1354. ** checking for paste (a legal token is not possible, so if a paste
  1355. ** is being done on a strformal, the behavior is undefined
  1356. ************************************************************************/
  1357. ptext_t do_strformal(void)
  1358. {
  1359. UCHAR n;
  1360. ptext_t p;
  1361. /* use unexpanded actual */
  1362. p = CURRENT_ACTUALS;
  1363. n = GETCH();
  1364. if(n > CURRENT_NACTUALS) {
  1365. return(NULL); /* already output warning */
  1366. }
  1367. if (n != 0)
  1368. while(--n) {
  1369. p = ACTUAL_NEXT(p);
  1370. }
  1371. return(ACTUAL_TEXT(p));
  1372. }
  1373. /************************************************************************
  1374. ** can_get_non_white: tries to get the next non white character
  1375. ** using P1 rules for white space (NL included). If the end of
  1376. ** an actual, or a rescan is found, this returns FALSE, so control
  1377. ** can drop into one of the lexers.
  1378. ************************************************************************/
  1379. int can_get_non_white(void)
  1380. {
  1381. int return_value = FALSE;
  1382. int white_found = FALSE;
  1383. for(;;) {
  1384. switch(CHARMAP(GETCH())) {
  1385. case LX_NL:
  1386. if(On_pound_line) {
  1387. UNGETCH();
  1388. goto leave_cgnw;
  1389. }
  1390. Linenumber++;
  1391. /*
  1392. ** FALLTHROUGH
  1393. */
  1394. case LX_WHITE:
  1395. case LX_CR:
  1396. white_found = TRUE;
  1397. break;
  1398. case LX_EOS:
  1399. {
  1400. int eos_res;
  1401. if((eos_res = handle_eos()) & (ACTUAL_EOS | RESCAN_EOS)) {
  1402. goto leave_cgnw;
  1403. }
  1404. if(eos_res != BACKSLASH_EOS) {
  1405. break;
  1406. }
  1407. }
  1408. /*
  1409. ** FALLTHROUGH
  1410. */
  1411. default:
  1412. UNGETCH();
  1413. return_value = TRUE;
  1414. goto leave_cgnw;
  1415. break;
  1416. }
  1417. }
  1418. leave_cgnw:
  1419. if(white_found) {
  1420. if(Exp_ptr >= ELIMIT) {
  1421. fatal_in_macro(10056);
  1422. }
  1423. if(*(Exp_ptr - 1) != ' ') {
  1424. *Exp_ptr++ = ' ';
  1425. }
  1426. }
  1427. return(return_value); /* could you get next non white? */
  1428. }
  1429. /************************************************************************/
  1430. /* fatal_in_macro () */
  1431. /************************************************************************/
  1432. void fatal_in_macro(int e)
  1433. {
  1434. Linenumber = Macro_line;
  1435. Msg_Temp = GET_MSG(e);
  1436. SET_MSG (Msg_Text, Msg_Temp);
  1437. fatal (e);
  1438. }
  1439. /************************************************************************
  1440. ** handle_eos : handle the end of a string.
  1441. ************************************************************************/
  1442. int handle_eos(void)
  1443. {
  1444. if(PREVCH() == '\\') {
  1445. if(checknl()) {
  1446. return(FILE_EOS);
  1447. }
  1448. else {
  1449. return(BACKSLASH_EOS);
  1450. }
  1451. }
  1452. if(Macro_depth == 0) { /* found end of file buffer or backslash */
  1453. if(io_eob()) { /* end of buffer in here is bad */
  1454. Msg_Temp = GET_MSG(1004);
  1455. SET_MSG (Msg_Text, Msg_Temp);
  1456. fatal (1004);
  1457. }
  1458. return(FILE_EOS);
  1459. }
  1460. again:
  1461. switch(GETCH()) {
  1462. case EOS_PAD:
  1463. goto again;
  1464. case EOS_ACTUAL:
  1465. /*
  1466. ** Just finished expanding actual. Check to see if there are
  1467. ** any more actuals to be expanded. If there are, set up to
  1468. ** expand them and return. Otherwise, set up to expand defn
  1469. */
  1470. /* move expanded text of this actual to act_buffer */
  1471. move_to_actual(CURRENT_TEXT, Exp_ptr);
  1472. /* reset Exp_ptr for more expansions at this macro depth */
  1473. Exp_ptr = CURRENT_TEXT;
  1474. /* expand next actual if there, otherwise expand definition */
  1475. expand_macro();
  1476. return(ACTUAL_EOS);
  1477. break;
  1478. case EOS_DEFINITION:
  1479. if(rescan_expansion()) {
  1480. return(RESCAN_EOS);
  1481. }
  1482. else {
  1483. return(DEFINITION_EOS);
  1484. }
  1485. break;
  1486. case EOS_RESCAN:
  1487. /*
  1488. ** Reset Current_char, Exp_ptr and Act_ptr, pop the macro
  1489. */
  1490. /* get input from the previous stream */
  1491. Current_char = CURRENT_STRING;
  1492. /* mark this macro as not expanding */
  1493. DEFN_EXPANDING(CURRENT_MACRO)--;
  1494. /*
  1495. ** if looking for the actuals of a macro, these pointers
  1496. ** should really not be reset, however, it is cleaner to
  1497. ** save them before calling handle_eos, and restore them
  1498. ** upon returning, than check a static variable here.
  1499. */
  1500. if(Macro_depth == 1) {
  1501. Act_ptr = ACT_BUFFER;
  1502. Exp_ptr = EXP_BUFFER;
  1503. }
  1504. --Macro_depth;
  1505. return(DEFINITION_EOS);
  1506. break;
  1507. /* the following conditional compile is so brackets match */
  1508. DEFAULT_UNREACHABLE;
  1509. }
  1510. }
  1511. /************************************************************************
  1512. ** END EXPANDING MACRO }
  1513. ************************************************************************/