replace.c 20 KB

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