replace.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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. /* M A C R O R E P L A C E M E N T */
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "nopp.h"
  10. #ifndef NOPP
  11. #include "debug.h"
  12. #include "pathlength.h"
  13. #include "strsize.h"
  14. #include "nparams.h"
  15. #include "idfsize.h"
  16. #include "numsize.h"
  17. #include "arith.h"
  18. #include <alloc.h>
  19. #include "idf.h"
  20. #include "idf_loc.h"
  21. #include "input.h"
  22. #include "macro.h"
  23. #include "LLlex.h"
  24. #include "class.h"
  25. #include "assert.h"
  26. #include "static.h"
  27. #include "macbuf.h"
  28. #include "replace.h"
  29. #include "replace_loc.h"
  30. #include "error.h"
  31. #include "skip.h"
  32. #include "domacro.h"
  33. #include "replace_loc.h"
  34. extern int InputLevel;
  35. struct repl *ReplaceList; /* list of currently active macros */
  36. int replace(struct idf *idf)
  37. {
  38. /* replace is called by the lexical analyzer to perform
  39. macro replacement. The routine actualy functions as a
  40. higher interface to the real thing: expand_macro().
  41. */
  42. struct repl *repl;
  43. if (!(idf->id_macro)) return 0;
  44. if (idf->id_macro->mc_flag & NOREPLACE)
  45. return 0;
  46. repl = new_repl();
  47. repl->r_ptr = repl->r_text = Malloc(repl->r_size = LAPBUF);
  48. repl->r_args = new_args();
  49. repl->r_idf = idf;
  50. if (!expand_macro(repl, idf))
  51. return 0;
  52. InputLevel++;
  53. InsertText(repl->r_text, (int)(repl->r_ptr - repl->r_text));
  54. idf->id_macro->mc_flag |= NOREPLACE;
  55. repl->r_level = InputLevel;
  56. repl->next = ReplaceList;
  57. ReplaceList = repl;
  58. return 1;
  59. }
  60. void unstackrepl()
  61. {
  62. Unstacked++;
  63. }
  64. void freeargs(struct args *args)
  65. {
  66. int i;
  67. /* We must don't know how many parameters were specified, so be
  68. * prepared to free all NPARAMS parameters.
  69. * When an expvec is !0, the rawvec will also be !0.
  70. * When an expvec is 0, all remaining vectors will also be 0.
  71. */
  72. for (i = 0; i < NPARAMS; i++) {
  73. if (args->a_expvec[i]) {
  74. free(args->a_expvec[i]);
  75. free(args->a_rawvec[i]);
  76. } else break;
  77. }
  78. free_args(args);
  79. }
  80. void EnableMacros()
  81. {
  82. struct repl *r = ReplaceList, *prev = 0;
  83. ASSERT(Unstacked > 0);
  84. while(r) {
  85. struct repl *nxt = r->next;
  86. if (r->r_level > InputLevel) {
  87. r->r_idf->id_macro->mc_flag &= ~NOREPLACE;
  88. if (!prev) ReplaceList = nxt;
  89. else prev->next = nxt;
  90. free(r->r_text);
  91. freeargs(r->r_args);
  92. free_repl(r);
  93. }
  94. else prev = r;
  95. r = nxt;
  96. }
  97. Unstacked = 0;
  98. }
  99. int expand_macro(struct repl *repl, struct idf *idf)
  100. {
  101. /* expand_macro() does the actual macro replacement.
  102. "idf" is a description of the identifier which
  103. caused the replacement.
  104. If the identifier represents a function-like macro
  105. call, the number of actual parameters is checked
  106. against the number of formal parameters. Note that
  107. in ANSI C the parameters are expanded first;
  108. this is done by calling getactuals().
  109. When the possible parameters are expanded, the replace-
  110. ment list associated with "idf" is expanded.
  111. expand_macro() returns 1 if the replacement succeeded
  112. and 0 if some error occurred.
  113. A special case is "defined". This acts as a unary operator
  114. on a single, unexpanded identifier, which may be surrounded
  115. by parenthesis. The function expand_defined() handles this.
  116. */
  117. struct macro *mac = idf->id_macro;
  118. struct args *args = repl->r_args;
  119. int ch;
  120. if (mac->mc_nps != -1) { /* with parameter list */
  121. if (mac->mc_flag & FUNC) {
  122. /* the following assertion won't compile:
  123. ASSERT(!strcmp("defined", idf->id_text));
  124. expand the assert macro by hand (??? dirty, temporary)
  125. */
  126. #ifdef DEBUG
  127. if (strcmp("defined", idf->id_text))
  128. crash("in %s, %u: assertion %s failed",
  129. __FILE__, __LINE__ - 2,
  130. "strcmp(\"defined\", idf->id_text)");
  131. #endif
  132. if (!AccDefined) return 0;
  133. expand_defined(repl);
  134. return 1;
  135. }
  136. ch = GetChar();
  137. ch = skipspaces(ch,1);
  138. if (ch != '(') { /* no replacement if no () */
  139. ChPushBack(ch);
  140. return 0;
  141. } else
  142. getactuals(repl, idf);
  143. }
  144. if (mac->mc_flag & FUNC) /* this macro leads to special action */
  145. macro_func(idf);
  146. macro2buffer(repl, idf, args);
  147. /* According to the ANSI definition:
  148. #define a +
  149. a+b; --> + + b ;
  150. 'a' must be substituded, but the result should be
  151. three tokens: + + ID. Therefore a token separator is
  152. inserted after the replacement.
  153. */
  154. if (repl->r_text == repl->r_ptr || *(repl->r_ptr - 1) != TOKSEP) {
  155. add2repl(repl, TOKSEP);
  156. }
  157. return 1;
  158. }
  159. void expand_defined(struct repl *repl)
  160. {
  161. int ch = GetChar();
  162. struct idf *id;
  163. int parens = 0;
  164. ch = skipspaces(ch, 0);
  165. if (ch == '(') {
  166. parens++;
  167. ch = GetChar();
  168. ch = skipspaces(ch, 0);
  169. }
  170. if ((class(ch) != STIDF) && (class(ch) != STELL)) {
  171. error("identifier missing");
  172. if (parens && ch != ')') error(") missing");
  173. if (!parens || ch != ')') ChPushBack(ch);
  174. add2repl(repl, '0');
  175. return;
  176. }
  177. ChPushBack(ch);
  178. id = GetIdentifier(0);
  179. ASSERT(id || class(ch) == STELL);
  180. ch = GetChar();
  181. ch = skipspaces(ch, 0);
  182. if (parens && ch != ')') error(") missing");
  183. if (!parens || ch != ')') ChPushBack(ch);
  184. add2repl(repl, (id && id->id_macro) ? '1' : '0');
  185. add2repl(repl, ' ');
  186. }
  187. void newarg(struct args *args)
  188. {
  189. args->a_expptr = args->a_expbuf = Malloc(args->a_expsize = ARGBUF);
  190. args->a_rawptr = args->a_rawbuf = Malloc(args->a_rawsize = ARGBUF);
  191. }
  192. void getactuals(struct repl *repl, struct idf *idf)
  193. {
  194. /* Get the actual parameters from the input stream.
  195. The hard part is done by actual(), only comma's and
  196. other syntactic trivialities are checked here.
  197. */
  198. struct args *args = repl->r_args;
  199. int nps = idf->id_macro->mc_nps;
  200. int argcnt;
  201. int ch;
  202. argcnt = 0;
  203. newarg(args);
  204. if ((ch = GetChar()) != ')') {
  205. UnGetChar();
  206. while ((ch = actual(repl)) != ')' ) {
  207. if (ch != ',') {
  208. lexerror("illegal macro call");
  209. return;
  210. }
  211. stash(repl, '\0', 1);
  212. args->a_expvec[argcnt] = args->a_expbuf;
  213. args->a_rawvec[argcnt] = args->a_rawbuf;
  214. ++argcnt;
  215. if (argcnt == STDC_NPARAMS)
  216. lexstrict("number of parameters exceeds ANSI standard");
  217. if (argcnt >= NPARAMS)
  218. fatal("argument vector overflow");
  219. newarg(args);
  220. }
  221. stash(repl, '\0', 1);
  222. args->a_expvec[argcnt] = args->a_expbuf;
  223. args->a_rawvec[argcnt] = args->a_rawbuf;
  224. ++argcnt;
  225. }
  226. if (argcnt < nps)
  227. lexerror("too few macro arguments");
  228. else if (argcnt > nps)
  229. lexerror("too many macro arguments");
  230. }
  231. void saveraw(struct repl *repl)
  232. {
  233. struct repl *nrepl = ReplaceList;
  234. struct args *ap = nrepl->r_args;
  235. char *p;
  236. /* stash identifier name */
  237. for (p = nrepl->r_idf->id_text; *p != '\0'; p++)
  238. stash(repl, *p, -1);
  239. /* The following code deals with expanded function
  240. like macro calls. It makes the following code
  241. work:
  242. #define def(a,b) x(a,b)
  243. #define glue(a,b) a ## b
  244. glue(abc,def(a,b))
  245. Results in:
  246. abcdef(a,b);
  247. */
  248. if (ap->a_rawvec[0]) {
  249. /* stash arguments */
  250. int i;
  251. for (i = 0; ap->a_rawvec[i] != (char *)0; i++) {
  252. if (i == 0) stash(repl, '(', -1);
  253. else stash(repl, ',', -1);
  254. for (p = ap->a_rawvec[i]; *p != '\0'; p++)
  255. stash(repl, *p, -1);
  256. }
  257. stash(repl, ')', -1);
  258. }
  259. }
  260. int actual(struct repl *repl)
  261. {
  262. /* This routine deals with the scanning of an actual parameter.
  263. It keeps in account the opening and closing brackets,
  264. preprocessor numbers, strings and character constants.
  265. */
  266. int ch = 0;
  267. int level = 0, nostashraw = 0;
  268. int lastch;
  269. static int Unstacked_missed;
  270. while (1) {
  271. lastch = ch;
  272. ch = GetChar();
  273. if (nostashraw
  274. && nostashraw >= Unstacked_missed) {
  275. nostashraw -= Unstacked_missed;
  276. Unstacked_missed = 0;
  277. }
  278. if (Unstacked) {
  279. nostashraw -= Unstacked;
  280. if (nostashraw < 0) {
  281. Unstacked_missed = -nostashraw;
  282. nostashraw = 0;
  283. }
  284. EnableMacros();
  285. }
  286. if (class(ch) == STIDF || class(ch) == STELL) {
  287. /* Scan a preprocessor identifier token. If the
  288. token is a macro, it is expanded first.
  289. */
  290. char buf[(IDFSIZE > NUMSIZE ? IDFSIZE : NUMSIZE) + 1];
  291. char *p = buf;
  292. struct idf *idef;
  293. int pos = -1;
  294. extern int idfsize;
  295. int NoExpandMacro;
  296. if (ch == NOEXPM) {
  297. NoExpandMacro= 1;
  298. ch = GetChar();
  299. } else NoExpandMacro = 0;
  300. do {
  301. if (++pos < idfsize) {
  302. *p++ = ch;
  303. }
  304. ch = GetChar();
  305. } while (in_idf(ch));
  306. *p++ = '\0';
  307. ch = '\0'; /* It could be an unstashed TOKSEP */
  308. UnGetChar();
  309. /* When the identifier has an associated macro
  310. replacement list, it's expanded.
  311. */
  312. idef = findidf(buf);
  313. if (!idef || NoExpandMacro || !replace(idef)) {
  314. if (NoExpandMacro
  315. || (idef && idef->id_macro
  316. && (idef->id_macro->mc_flag & NOREPLACE)))
  317. stash(repl, NOEXPM, !nostashraw);
  318. for (p = buf; *p != '\0'; p++)
  319. stash(repl, *p, !nostashraw);
  320. } else {
  321. if (!nostashraw) saveraw(repl);
  322. nostashraw++;
  323. }
  324. } else if (class(ch) == STNUM) {
  325. /* a preprocessing number has the following
  326. regular expression:
  327. [0-9|"."[0-9]]{[0-9"."a-zA-Z_]|{[Ee][+-]}}*
  328. */
  329. stash(repl, ch, !nostashraw);
  330. if (ch == '.') {
  331. ch = GetChar();
  332. if (class(ch) != STNUM) {
  333. ch = '\0'; /* It could be an unstashed TOKSEP */
  334. UnGetChar();
  335. continue;
  336. }
  337. else stash(repl, ch, !nostashraw);
  338. }
  339. ch = GetChar();
  340. while (in_idf(ch) || ch == '.') {
  341. stash(repl, ch, !nostashraw);
  342. if ((ch = GetChar()) == 'e' || ch == 'E') {
  343. stash(repl, ch, !nostashraw);
  344. ch = GetChar();
  345. if (ch == '+' || ch == '-') {
  346. stash(repl, ch, !nostashraw);
  347. ch = GetChar();
  348. }
  349. }
  350. }
  351. ch = '\0'; /* It could be an unstashed TOKSEP */
  352. UnGetChar();
  353. } else if (ch == '(') {
  354. /* a comma may occur between parentheses */
  355. level++;
  356. stash(repl, ch, !nostashraw);
  357. } else if (ch == ')') {
  358. level--;
  359. /* closing parenthesis of macro call */
  360. if (level < 0) return ')';
  361. stash(repl, ch, !nostashraw);
  362. } else if (ch == ',') {
  363. if (level <= 0) { /* comma separator for next argument */
  364. if (level)
  365. lexerror("unbalanced parenthesis");
  366. if (!nostashraw)
  367. return ','; /* ??? */
  368. }
  369. stash(repl, ch, !nostashraw);
  370. } else if (ch == '\n') {
  371. /* newlines are accepted as white spaces */
  372. LineNumber++;
  373. /* This piece of code needs some explanation:
  374. consider the call of a macro defined as:
  375. #define sum(a,b) (a+b)
  376. in the following form:
  377. sum(
  378. /_* comment *_/ #include phone_number
  379. ,2);
  380. in which case the include must be handled
  381. interpreted as such.
  382. */
  383. a_new_line: ch = GetChar();
  384. while (class(ch) == STSKIP || ch == '/') {
  385. if (ch == '/') {
  386. if ((ch = GetChar()) == '*' && !InputLevel) {
  387. skipcomment();
  388. stash(repl, ' ', !nostashraw);
  389. ch = GetChar();
  390. continue;
  391. } else {
  392. UnGetChar();
  393. ch = '/';
  394. }
  395. stash(repl, '/', !nostashraw);
  396. break;
  397. } else ch = GetChar();
  398. }
  399. if (ch == '#') {
  400. domacro();
  401. /* Clear File_Inserted since domacro could
  402. * be called again, which calls GetToken().
  403. */
  404. File_Inserted = 0;
  405. goto a_new_line;
  406. } else if (ch == EOI) {
  407. lexerror("unterminated macro call");
  408. return ')';
  409. }
  410. if (ch != '/') {
  411. UnGetChar();
  412. ch = ' ';
  413. stash(repl, ' ', !nostashraw);
  414. }
  415. } else if (ch == '/') {
  416. /* comments are treated as one white space token */
  417. if ((ch = GetChar()) == '*' && !InputLevel) {
  418. skipcomment();
  419. stash(repl, ' ', !nostashraw);
  420. } else {
  421. UnGetChar();
  422. ch = '/';
  423. stash(repl, '/', !nostashraw);
  424. }
  425. } else if (ch == '\'' || ch == '"') {
  426. /* Strings are considered as ONE token, thus no
  427. replacement within strings.
  428. */
  429. int match = ch;
  430. stash(repl, ch, !nostashraw);
  431. while ((ch = GetChar()) != EOI) {
  432. if (ch == match)
  433. break;
  434. if (ch == '\\') {
  435. stash(repl, ch, !nostashraw);
  436. ch = GetChar();
  437. } else if (ch == '\n') {
  438. lexerror("newline in string");
  439. LineNumber++;
  440. stash(repl, match, !nostashraw);
  441. break;
  442. }
  443. stash(repl, ch, !nostashraw);
  444. }
  445. if (ch != match) {
  446. lexerror("unterminated macro call");
  447. return ')';
  448. }
  449. stash(repl, ch, !nostashraw);
  450. } else {
  451. if (lastch == TOKSEP && ch == TOKSEP) continue;
  452. stash(repl, ch, !nostashraw);
  453. }
  454. }
  455. }
  456. void macro_func(struct idf *idef)
  457. {
  458. /* macro_func() performs the special actions needed with some
  459. macros. These macros are __FILE__ and __LINE__ which
  460. replacement texts must be evaluated at the time they are
  461. used.
  462. */
  463. struct macro *mac = idef->id_macro;
  464. static char FilNamBuf[PATHLENGTH];
  465. char *long2str();
  466. switch (idef->id_text[2]) {
  467. case 'F': /* __FILE__ */
  468. FilNamBuf[0] = '"';
  469. strcpy(&FilNamBuf[1], FileName);
  470. strcat(FilNamBuf, "\"");
  471. mac->mc_text = FilNamBuf;
  472. mac->mc_length = strlen(FilNamBuf);
  473. break;
  474. case 'L': /* __LINE__ */
  475. mac->mc_text = long2str((long)LineNumber, 10);
  476. mac->mc_length = strlen(mac->mc_text);
  477. break;
  478. default:
  479. crash("(macro_func)");
  480. /*NOTREACHED*/
  481. }
  482. }
  483. void macro2buffer(struct repl *repl, struct idf *idf, struct args *args)
  484. {
  485. /* macro2buffer expands the replacement list and places the
  486. result onto the replacement buffer. It deals with the #
  487. and ## operators, and inserts the actual parameters.
  488. The argument buffer contains the raw argument (needed
  489. for the ## operator), and the expanded argument (for
  490. all other parameter substitutions).
  491. The grammar of the replacement list is:
  492. repl_list: TOKEN repl_list
  493. | PARAMETER repl_list
  494. | '#' PARAMETER
  495. | TOKEN '##' TOKEN
  496. | PARAMETER '##' TOKEN
  497. | TOKEN '##' PARAMETER
  498. | PARAMETER '##' PARAMETER
  499. ;
  500. As the grammar indicates, we could make a DFA and
  501. use this finite state machine for the replacement
  502. list parsing (inserting the arguments, etc.).
  503. Currently we go through the replacement list in a
  504. linear fashion. This is VERY expensive, something
  505. smarter should be done (but even a DFA is O(|s|)).
  506. */
  507. char *ptr = idf->id_macro->mc_text;
  508. int err = 0;
  509. int func = idf->id_macro->mc_nps != -1;
  510. ASSERT(ptr[idf->id_macro->mc_length] == '\0');
  511. while (*ptr) {
  512. if (*ptr == '\'' || *ptr == '"') {
  513. int delim = *ptr;
  514. do {
  515. add2repl(repl, *ptr);
  516. if (*ptr == '\\')
  517. add2repl(repl, *++ptr);
  518. if (*ptr == '\0') {
  519. lexerror("unterminated string");
  520. return;
  521. }
  522. ptr++;
  523. } while (*ptr != delim || *ptr == '\0');
  524. add2repl(repl, *ptr++);
  525. } else if (*ptr == '#' && (func || *(ptr+1) == '#')) {
  526. if (*++ptr == '#') {
  527. int tmpindex;
  528. /* ## - paste operator */
  529. ptr++;
  530. /* trim the actual replacement list */
  531. --repl->r_ptr;
  532. while (repl->r_ptr >= repl->r_text
  533. && is_wsp(*(unsigned char *)repl->r_ptr))
  534. --repl->r_ptr;
  535. /* ## occurred at the beginning of the replacement list.
  536. */
  537. if (repl->r_ptr < repl->r_text) {
  538. err = 1;
  539. break;
  540. }
  541. if (repl->r_ptr >= repl->r_text
  542. && *repl->r_ptr == TOKSEP)
  543. --repl->r_ptr;
  544. ++repl->r_ptr;
  545. tmpindex = repl->r_ptr - repl->r_text;
  546. /* tmpindex can be 0 */
  547. /* skip space in macro replacement list */
  548. while ((*ptr & FORMALP) == 0 && is_wsp(*(unsigned char *)ptr))
  549. ptr++;
  550. /* ## occurred at the end of the replacement list.
  551. */
  552. if (*ptr & FORMALP) {
  553. int n = *ptr++ & 0177;
  554. char *p;
  555. ASSERT(n > 0);
  556. p = args->a_rawvec[n-1];
  557. if (p) { /* else macro argument missing */
  558. while (is_wsp(*(unsigned char *)p)) p++;
  559. if (*p == NOEXPM) p++;
  560. while (*p)
  561. add2repl(repl, *p++);
  562. }
  563. while (tmpindex > 0
  564. && in_idf((unsigned char)repl->r_text[tmpindex]))
  565. tmpindex--;
  566. if (tmpindex >= 0
  567. && repl->r_text[tmpindex] == NOEXPM)
  568. repl->r_text[tmpindex] = TOKSEP;
  569. } else if (*ptr == '\0') {
  570. err = 1;
  571. break;
  572. } else {
  573. if (in_idf(*(unsigned char *)ptr)) {
  574. tmpindex--;
  575. while (tmpindex > 0
  576. && in_idf((unsigned char)repl->r_text[tmpindex]))
  577. tmpindex--;
  578. if (tmpindex >= 0
  579. && repl->r_text[tmpindex] == NOEXPM)
  580. repl->r_text[tmpindex] = TOKSEP;
  581. }
  582. }
  583. } else { /* # operator */
  584. ptr = stringify(repl, ptr, args);
  585. }
  586. } else if (*ptr & FORMALP) {
  587. /* insert actual parameter */
  588. int n = *ptr++ & 0177;
  589. char *p, *q;
  590. ASSERT(n > 0);
  591. /* This is VERY dirty, we look ahead for the
  592. ## operator. If it's found we use the raw
  593. argument buffer instead of the expanded
  594. one.
  595. */
  596. for (p = ptr; (*p & FORMALP) == 0 && is_wsp(*(unsigned char *)p); p++)
  597. /* EMPTY */;
  598. if (*p == '#' && p[1] == '#')
  599. q = args->a_rawvec[n-1];
  600. else
  601. q = args->a_expvec[n-1];
  602. if (q) /* else macro argument missing */
  603. while (*q)
  604. add2repl(repl, *q++);
  605. if (repl->r_text == repl->r_ptr || *(repl->r_ptr - 1) != TOKSEP)
  606. add2repl(repl, TOKSEP);
  607. } else {
  608. add2repl(repl, *ptr++);
  609. }
  610. }
  611. if (err)
  612. lexerror("illegal use of the ## operator");
  613. }
  614. char *stringify(struct repl *repl, char *ptr, struct args *args)
  615. {
  616. /* If a parameter is immediately preceded by a # token
  617. both are replaced by a single string literal that
  618. contains the spelling of the token sequence for the
  619. corresponding argument.
  620. Each occurrence of white space between the argument's
  621. tokens become a single space character in the string
  622. literal. White spaces before the first token and after
  623. the last token comprising the argument are deleted.
  624. To retain the original spelling we insert backslashes
  625. as appropriate. We only escape backslashes if they
  626. occure within string tokens.
  627. */
  628. int space = 1; /* skip leading spaces */
  629. int delim = 0; /* string or character constant delim */
  630. int backslash = 0; /* last character was a \ */
  631. /* skip spaces macro replacement list */
  632. while ((*ptr & FORMALP) == 0 && is_wsp(*(unsigned char *)ptr))
  633. ptr++;
  634. if (*ptr & FORMALP) {
  635. int n = *ptr++ & 0177;
  636. char *p;
  637. ASSERT(n != 0);
  638. p = args->a_rawvec[n-1];
  639. add2repl(repl, '"');
  640. while (*p) {
  641. if (is_wsp(*(unsigned char *)p)) {
  642. if (!space) {
  643. space = 1;
  644. add2repl(repl, ' ');
  645. }
  646. p++;
  647. continue;
  648. }
  649. space = 0;
  650. if (!delim && (*p == '"' || *p == '\''))
  651. delim = *p;
  652. else if (*p == delim && !backslash)
  653. delim = 0;
  654. backslash = *p == '\\';
  655. if (*p == '"' || (delim && *p == '\\'))
  656. add2repl(repl, '\\');
  657. if (*p == TOKSEP || *p == NOEXPM) p++;
  658. else add2repl(repl, *p++);
  659. }
  660. /* trim spaces in the replacement list */
  661. for (--repl->r_ptr; is_wsp(*(unsigned char *)repl->r_ptr); repl->r_ptr--)
  662. /* EMPTY */;
  663. ++repl->r_ptr; /* oops, one to far */
  664. add2repl(repl, '"');
  665. } else
  666. error("illegal use of # operator");
  667. return ptr;
  668. }
  669. /* The following routine is also called from domacro.c.
  670. */
  671. void add2repl(struct repl *repl, int ch)
  672. {
  673. int index = repl->r_ptr - repl->r_text;
  674. ASSERT(index < repl->r_size);
  675. if (index + 2 >= repl->r_size) {
  676. repl->r_text = Realloc(repl->r_text, (unsigned) (repl->r_size <<= 1));
  677. repl->r_ptr = repl->r_text + index;
  678. }
  679. *repl->r_ptr++ = ch;
  680. *repl->r_ptr = '\0';
  681. }
  682. /* If the variable stashraw is negative, we must only stash into the raw
  683. * buffer. If the variable is zero, we must only stash into the expanded
  684. * buffer. Otherwise, we must use both buffers.
  685. */
  686. void stash(struct repl *repl ,int ch, int stashraw)
  687. {
  688. /* Stash characters into the macro expansion buffer.
  689. */
  690. struct args *args = repl->r_args;
  691. int index = args->a_expptr - args->a_expbuf;
  692. if (stashraw >= 0) {
  693. ASSERT(index < args->a_expsize);
  694. if (index + 1 >= args->a_expsize) {
  695. args->a_expbuf = Realloc(args->a_expbuf,
  696. (unsigned) (args->a_expsize <<= 1));
  697. args->a_expptr = args->a_expbuf + index;
  698. }
  699. *args->a_expptr++ = ch;
  700. }
  701. if (stashraw) {
  702. index = args->a_rawptr - args->a_rawbuf;
  703. ASSERT(index < args->a_rawsize);
  704. if (index + 1 >= args->a_rawsize) {
  705. args->a_rawbuf = Realloc(args->a_rawbuf,
  706. (unsigned)(args->a_rawsize <<= 1));
  707. args->a_rawptr = args->a_rawbuf + index;
  708. }
  709. *args->a_rawptr++ = ch;
  710. }
  711. }
  712. #endif /* NOPP */