replace.c 19 KB

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