replace.c 19 KB

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