replace.c 19 KB

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