compile.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /* $OpenBSD: compile.c,v 1.42 2017/08/01 18:05:53 martijn Exp $ */
  2. /*-
  3. * Copyright (c) 2016
  4. * mirabilos <m@mirbsd.org>
  5. * Copyright (c) 1992 Diomidis Spinellis.
  6. * Copyright (c) 1992, 1993
  7. * The Regents of the University of California. All rights reserved.
  8. *
  9. * This code is derived from software contributed to Berkeley by
  10. * Diomidis Spinellis of Imperial College, University of London.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. */
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <ctype.h>
  39. #include <errno.h>
  40. #include <fcntl.h>
  41. #include <limits.h>
  42. #include <regex.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include "defs.h"
  47. #include "extern.h"
  48. __RCSID("$MirOS: src/usr.bin/sed/compile.c,v 1.3 2017/11/20 01:23:56 tg Exp $");
  49. #define LHSZ 128
  50. #define LHMASK (LHSZ - 1)
  51. static struct labhash {
  52. struct labhash *lh_next;
  53. u_int lh_hash;
  54. struct s_command *lh_cmd;
  55. int lh_ref;
  56. } *labels[LHSZ];
  57. static char *compile_addr(char *, struct s_addr *);
  58. static char *compile_ccl(char **, char *);
  59. static char *compile_delimited(char *, char *, int);
  60. static char *compile_flags(char *, struct s_subst *);
  61. static char *compile_re(char *, regex_t **);
  62. static char *compile_subst(char *, struct s_subst *);
  63. static char *compile_text(void);
  64. static char *compile_tr(char *, char **);
  65. static struct s_command
  66. **compile_stream(struct s_command **);
  67. static char *duptoeol(char *, const char *, char **);
  68. static void enterlabel(struct s_command *);
  69. static struct s_command
  70. *findlabel(char *);
  71. static void fixuplabel(struct s_command *, struct s_command *);
  72. static void uselabel(void);
  73. /*
  74. * Command specification. This is used to drive the command parser.
  75. */
  76. struct s_format {
  77. char code; /* Command code */
  78. int naddr; /* Number of address args */
  79. enum e_args args; /* Argument type */
  80. };
  81. static struct s_format cmd_fmts[] = {
  82. {'{', 2, GROUP},
  83. {'}', 0, ENDGROUP},
  84. {'a', 1, TEXT},
  85. {'b', 2, BRANCH},
  86. {'c', 2, TEXT},
  87. {'d', 2, EMPTY},
  88. {'D', 2, EMPTY},
  89. {'g', 2, EMPTY},
  90. {'G', 2, EMPTY},
  91. {'h', 2, EMPTY},
  92. {'H', 2, EMPTY},
  93. {'i', 1, TEXT},
  94. {'l', 2, EMPTY},
  95. {'n', 2, EMPTY},
  96. {'N', 2, EMPTY},
  97. {'p', 2, EMPTY},
  98. {'P', 2, EMPTY},
  99. {'q', 1, EMPTY},
  100. {'r', 1, RFILE},
  101. {'s', 2, SUBST},
  102. {'t', 2, BRANCH},
  103. {'w', 2, WFILE},
  104. {'x', 2, EMPTY},
  105. {'y', 2, TR},
  106. {'!', 2, NONSEL},
  107. {':', 0, LABEL},
  108. {'#', 0, COMMENT},
  109. {'=', 1, EMPTY},
  110. {'\0', 0, COMMENT},
  111. };
  112. /* The compiled program. */
  113. struct s_command *prog;
  114. /*
  115. * Compile the program into prog.
  116. * Initialise appends.
  117. */
  118. void
  119. compile(void)
  120. {
  121. *compile_stream(&prog) = NULL;
  122. fixuplabel(prog, NULL);
  123. uselabel();
  124. appends = xreallocarray(NULL, appendnum, sizeof(struct s_appends));
  125. match = xreallocarray(NULL, maxnsub + 1, sizeof(regmatch_t));
  126. }
  127. #define EATSPACE() do { \
  128. if (p) \
  129. while (isascii((unsigned char)*p) && \
  130. isspace((unsigned char)*p)) \
  131. p++; \
  132. } while (0)
  133. static struct s_command **
  134. compile_stream(struct s_command **link)
  135. {
  136. char *p;
  137. static char *lbuf; /* To avoid excessive malloc calls */
  138. static size_t bufsize;
  139. struct s_command *cmd, *cmd2, *stack;
  140. struct s_format *fp;
  141. int naddr; /* Number of addresses */
  142. stack = 0;
  143. for (;;) {
  144. if ((p = cu_fgets(&lbuf, &bufsize)) == NULL) {
  145. if (stack != 0)
  146. error(COMPILE, "unexpected EOF (pending }'s)");
  147. return (link);
  148. }
  149. semicolon: EATSPACE();
  150. if (*p == '#' || *p == '\0')
  151. continue;
  152. if (*p == ';') {
  153. p++;
  154. goto semicolon;
  155. }
  156. *link = cmd = xmalloc(sizeof(struct s_command));
  157. link = &cmd->next;
  158. cmd->nonsel = cmd->inrange = 0;
  159. /* First parse the addresses */
  160. naddr = 0;
  161. /* Valid characters to start an address */
  162. #define addrchar(c) (strchr("0123456789/\\$", (c)))
  163. if (addrchar(*p)) {
  164. naddr++;
  165. cmd->a1 = xmalloc(sizeof(struct s_addr));
  166. p = compile_addr(p, cmd->a1);
  167. EATSPACE(); /* EXTENSION */
  168. if (*p == ',') {
  169. p++;
  170. EATSPACE(); /* EXTENSION */
  171. naddr++;
  172. cmd->a2 = xmalloc(sizeof(struct s_addr));
  173. p = compile_addr(p, cmd->a2);
  174. EATSPACE();
  175. } else {
  176. cmd->a2 = 0;
  177. }
  178. } else {
  179. cmd->a1 = cmd->a2 = 0;
  180. }
  181. nonsel: /* Now parse the command */
  182. if (!*p)
  183. error(COMPILE, "command expected");
  184. cmd->code = *p;
  185. for (fp = cmd_fmts; fp->code; fp++)
  186. if (fp->code == *p)
  187. break;
  188. if (!fp->code)
  189. error(COMPILE, "invalid command code %c", *p);
  190. if (naddr > fp->naddr)
  191. error(COMPILE,
  192. "command %c expects up to %d address(es), found %d",
  193. *p, fp->naddr, naddr);
  194. switch (fp->args) {
  195. case NONSEL: /* ! */
  196. p++;
  197. EATSPACE();
  198. cmd->nonsel = 1;
  199. goto nonsel;
  200. case GROUP: /* { */
  201. p++;
  202. EATSPACE();
  203. cmd->next = stack;
  204. stack = cmd;
  205. link = &cmd->u.c;
  206. if (*p)
  207. goto semicolon;
  208. break;
  209. case ENDGROUP:
  210. /*
  211. * Short-circuit command processing, since end of
  212. * group is really just a noop.
  213. */
  214. cmd->nonsel = 1;
  215. if (stack == 0)
  216. error(COMPILE, "unexpected }");
  217. cmd2 = stack;
  218. stack = cmd2->next;
  219. cmd2->next = cmd;
  220. /*FALLTHROUGH*/
  221. case EMPTY: /* d D g G h H l n N p P q x = \0 */
  222. p++;
  223. EATSPACE();
  224. if (*p == ';') {
  225. p++;
  226. link = &cmd->next;
  227. goto semicolon;
  228. }
  229. if (*p)
  230. error(COMPILE,
  231. "extra characters at the end of %c command", cmd->code);
  232. break;
  233. case TEXT: /* a c i */
  234. p++;
  235. EATSPACE();
  236. if (*p != '\\')
  237. error(COMPILE, "command %c expects \\ followed by"
  238. " text", cmd->code);
  239. p++;
  240. EATSPACE();
  241. if (*p)
  242. error(COMPILE, "extra characters after \\ at the"
  243. " end of %c command", cmd->code);
  244. cmd->t = compile_text();
  245. break;
  246. case COMMENT: /* \0 # */
  247. break;
  248. case WFILE: /* w */
  249. p++;
  250. EATSPACE();
  251. if (*p == '\0')
  252. error(COMPILE, "filename expected");
  253. cmd->t = duptoeol(p, "w command", NULL);
  254. if (aflag) {
  255. cmd->u.fd = -1;
  256. pledge_wpath = 1;
  257. }
  258. else if ((cmd->u.fd = open(p,
  259. O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
  260. DEFFILEMODE)) == -1)
  261. error(FATAL, "%s: %s", p, strerror(errno));
  262. break;
  263. case RFILE: /* r */
  264. pledge_rpath = 1;
  265. p++;
  266. EATSPACE();
  267. cmd->t = duptoeol(p, "read command", NULL);
  268. break;
  269. case BRANCH: /* b t */
  270. p++;
  271. EATSPACE();
  272. if (*p == '\0')
  273. cmd->t = NULL;
  274. else
  275. cmd->t = duptoeol(p, "branch", &p);
  276. if (*p == ';') {
  277. p++;
  278. goto semicolon;
  279. }
  280. break;
  281. case LABEL: /* : */
  282. p++;
  283. EATSPACE();
  284. cmd->t = duptoeol(p, "label", &p);
  285. if (strlen(cmd->t) == 0)
  286. error(COMPILE, "empty label");
  287. enterlabel(cmd);
  288. if (*p == ';') {
  289. p++;
  290. goto semicolon;
  291. }
  292. break;
  293. case SUBST: /* s */
  294. p++;
  295. if (*p == '\0' || *p == '\\')
  296. error(COMPILE, "substitute pattern can not be"
  297. " delimited by newline or backslash");
  298. cmd->u.s = xmalloc(sizeof(struct s_subst));
  299. p = compile_re(p, &cmd->u.s->re);
  300. if (p == NULL)
  301. error(COMPILE, "unterminated substitute pattern");
  302. --p;
  303. p = compile_subst(p, cmd->u.s);
  304. p = compile_flags(p, cmd->u.s);
  305. EATSPACE();
  306. if (*p == ';') {
  307. p++;
  308. link = &cmd->next;
  309. goto semicolon;
  310. }
  311. break;
  312. case TR: /* y */
  313. p++;
  314. p = compile_tr(p, (char **)&cmd->u.y);
  315. EATSPACE();
  316. if (*p == ';') {
  317. p++;
  318. link = &cmd->next;
  319. goto semicolon;
  320. }
  321. if (*p)
  322. error(COMPILE, "extra text at the end of a"
  323. " transform command");
  324. break;
  325. }
  326. }
  327. }
  328. /*
  329. * Get a delimited string. P points to the delimeter of the string; d points
  330. * to a buffer area. Newline and delimiter escapes are processed; other
  331. * escapes are ignored.
  332. *
  333. * Returns a pointer to the first character after the final delimiter or NULL
  334. * in the case of a non-terminated string. The character array d is filled
  335. * with the processed string.
  336. */
  337. static char *
  338. compile_delimited(char *p, char *d, int is_tr)
  339. {
  340. char c;
  341. c = *p++;
  342. if (c == '\0')
  343. return (NULL);
  344. else if (c == '\\')
  345. error(COMPILE, "\\ can not be used as a string delimiter");
  346. else if (c == '\n')
  347. error(COMPILE, "newline can not be used as a string delimiter");
  348. while (*p) {
  349. if (*p == '[' && *p != c) {
  350. if ((d = compile_ccl(&p, d)) == NULL)
  351. error(COMPILE, "unbalanced brackets ([])");
  352. continue;
  353. } else if (*p == '\\' && p[1] == '[') {
  354. *d++ = *p++;
  355. } else if (*p == '\\' && p[1] == c) {
  356. p++;
  357. } else if (*p == '\\' && p[1] == 'n') {
  358. *d++ = '\n';
  359. p += 2;
  360. continue;
  361. } else if (*p == '\\' && p[1] == '\\') {
  362. if (is_tr)
  363. p++;
  364. else
  365. *d++ = *p++;
  366. } else if (*p == c) {
  367. *d = '\0';
  368. return (p + 1);
  369. }
  370. *d++ = *p++;
  371. }
  372. return (NULL);
  373. }
  374. /* compile_ccl: expand a POSIX character class */
  375. static char *
  376. compile_ccl(char **sp, char *t)
  377. {
  378. int c, d;
  379. char *s = *sp;
  380. *t++ = *s++;
  381. if (*s == '^')
  382. *t++ = *s++;
  383. if (*s == ']')
  384. *t++ = *s++;
  385. for (; *s && (*t = *s) != ']'; s++, t++)
  386. if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
  387. *++t = *++s, t++, s++;
  388. for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
  389. if ((c = *s) == '\0')
  390. return NULL;
  391. } else if (*s == '\\' && s[1] == 'n') {
  392. *t = '\n';
  393. s++;
  394. }
  395. if (*s == ']') {
  396. *sp = ++s;
  397. return (++t);
  398. } else {
  399. return (NULL);
  400. }
  401. }
  402. /*
  403. * Get a regular expression. P points to the delimiter of the regular
  404. * expression; repp points to the address of a regexp pointer. Newline
  405. * and delimiter escapes are processed; other escapes are ignored.
  406. * Returns a pointer to the first character after the final delimiter
  407. * or NULL in the case of a non terminated regular expression. The regexp
  408. * pointer is set to the compiled regular expression.
  409. * Cflags are passed to regcomp.
  410. */
  411. static char *
  412. compile_re(char *p, regex_t **repp)
  413. {
  414. int eval;
  415. char *re;
  416. re = xmalloc(strlen(p) + 1); /* strlen(re) <= strlen(p) */
  417. p = compile_delimited(p, re, 0);
  418. if (p && strlen(re) == 0) {
  419. *repp = NULL;
  420. free(re);
  421. return (p);
  422. }
  423. *repp = xmalloc(sizeof(regex_t));
  424. if (p && (eval = regcomp(*repp, re, Eflag ? REG_EXTENDED : 0)) != 0)
  425. error(COMPILE, "RE error: %s", strregerror(eval, *repp));
  426. if (maxnsub < (*repp)->re_nsub)
  427. maxnsub = (*repp)->re_nsub;
  428. free(re);
  429. return (p);
  430. }
  431. /*
  432. * Compile the substitution string of a regular expression and set res to
  433. * point to a saved copy of it. Nsub is the number of parenthesized regular
  434. * expressions.
  435. */
  436. static char *
  437. compile_subst(char *p, struct s_subst *s)
  438. {
  439. static char *lbuf;
  440. static size_t bufsize;
  441. size_t asize, ref, size;
  442. char c, *text, *op, *sp;
  443. int sawesc = 0;
  444. c = *p++; /* Terminator character */
  445. if (c == '\0')
  446. return (NULL);
  447. s->maxbref = 0;
  448. s->linenum = linenum;
  449. text = NULL;
  450. asize = size = 0;
  451. do {
  452. size_t len = ROUNDLEN(strlen(p) + 1);
  453. if (asize - size < len) {
  454. do {
  455. asize += len;
  456. } while (asize - size < len);
  457. text = xrealloc(text, asize);
  458. }
  459. op = sp = text + size;
  460. for (; *p; p++) {
  461. if (*p == '\\' || sawesc) {
  462. /*
  463. * If this is a continuation from the last
  464. * buffer, we won't have a character to
  465. * skip over.
  466. */
  467. if (sawesc)
  468. sawesc = 0;
  469. else
  470. p++;
  471. if (*p == '\0') {
  472. /*
  473. * This escaped character is continued
  474. * in the next part of the line. Note
  475. * this fact, then cause the loop to
  476. * exit w/ normal EOL case and reenter
  477. * above with the new buffer.
  478. */
  479. sawesc = 1;
  480. p--;
  481. continue;
  482. } else if (strchr("123456789", *p) != NULL) {
  483. *sp++ = '\\';
  484. ref = *p - '0';
  485. if (s->re != NULL &&
  486. ref > s->re->re_nsub)
  487. error(COMPILE,
  488. "\\%c not defined in the RE", *p);
  489. if (s->maxbref < ref)
  490. s->maxbref = ref;
  491. } else if (*p == '&' || *p == '\\')
  492. *sp++ = '\\';
  493. } else if (*p == c) {
  494. p++;
  495. *sp++ = '\0';
  496. size += sp - op;
  497. s->new = xrealloc(text, size);
  498. return (p);
  499. } else if (*p == '\n') {
  500. error(COMPILE,
  501. "unescaped newline inside substitute pattern");
  502. }
  503. *sp++ = *p;
  504. }
  505. size += sp - op;
  506. } while ((p = cu_fgets(&lbuf, &bufsize)));
  507. error(COMPILE, "unterminated substitute in regular expression");
  508. }
  509. /*
  510. * Compile the flags of the s command
  511. */
  512. static char *
  513. compile_flags(char *p, struct s_subst *s)
  514. {
  515. int gn; /* True if we have seen g or n */
  516. long l;
  517. char wfile[PATH_MAX], *q, *eq;
  518. s->n = 1; /* Default */
  519. s->p = 0;
  520. s->wfile = NULL;
  521. s->wfd = -1;
  522. for (gn = 0;;) {
  523. EATSPACE(); /* EXTENSION */
  524. switch (*p) {
  525. case 'g':
  526. if (gn)
  527. error(COMPILE, "more than one number or 'g' in"
  528. " substitute flags");
  529. gn = 1;
  530. s->n = 0;
  531. break;
  532. case '\0':
  533. case '\n':
  534. case ';':
  535. return (p);
  536. case 'p':
  537. s->p = 1;
  538. break;
  539. case '1': case '2': case '3':
  540. case '4': case '5': case '6':
  541. case '7': case '8': case '9':
  542. if (gn)
  543. error(COMPILE, "more than one number or 'g' in"
  544. " substitute flags");
  545. gn = 1;
  546. l = strtol(p, &p, 10);
  547. if (l <= 0 || l >= INT_MAX)
  548. error(COMPILE,
  549. "number in substitute flags out of range");
  550. s->n = (int)l;
  551. continue;
  552. case 'w':
  553. p++;
  554. #ifdef HISTORIC_PRACTICE
  555. if (*p != ' ') {
  556. warning("space missing before w wfile");
  557. return (p);
  558. }
  559. #endif
  560. EATSPACE();
  561. q = wfile;
  562. eq = wfile + sizeof(wfile) - 1;
  563. while (*p) {
  564. if (*p == '\n')
  565. break;
  566. if (q >= eq)
  567. error(COMPILE, "wfile too long");
  568. *q++ = *p++;
  569. }
  570. *q = '\0';
  571. if (q == wfile)
  572. error(COMPILE, "no wfile specified");
  573. s->wfile = strdup(wfile);
  574. if (aflag)
  575. pledge_wpath = 1;
  576. else if ((s->wfd = open(wfile,
  577. O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
  578. DEFFILEMODE)) == -1)
  579. error(FATAL, "%s: %s", wfile, strerror(errno));
  580. return (p);
  581. default:
  582. error(COMPILE,
  583. "bad flag in substitute command: '%c'", *p);
  584. break;
  585. }
  586. p++;
  587. }
  588. }
  589. /*
  590. * Compile a translation set of strings into a lookup table.
  591. */
  592. static char *
  593. compile_tr(char *p, char **transtab)
  594. {
  595. int i;
  596. char *lt, *op, *np;
  597. char *old = NULL, *new = NULL;
  598. if (*p == '\0' || *p == '\\')
  599. error(COMPILE,
  600. "transform pattern can not be delimited by newline or backslash");
  601. old = xmalloc(strlen(p) + 1);
  602. p = compile_delimited(p, old, 1);
  603. if (p == NULL) {
  604. error(COMPILE, "unterminated transform source string");
  605. goto bad;
  606. }
  607. new = xmalloc(strlen(p) + 1);
  608. p = compile_delimited(--p, new, 1);
  609. if (p == NULL) {
  610. error(COMPILE, "unterminated transform target string");
  611. goto bad;
  612. }
  613. EATSPACE();
  614. if (strlen(new) != strlen(old)) {
  615. error(COMPILE, "transform strings are not the same length");
  616. goto bad;
  617. }
  618. /* We assume characters are 8 bits */
  619. lt = xmalloc(UCHAR_MAX + 1);
  620. for (i = 0; i <= UCHAR_MAX; i++)
  621. lt[i] = (char)i;
  622. for (op = old, np = new; *op; op++, np++)
  623. lt[(u_char)*op] = *np;
  624. *transtab = lt;
  625. free(old);
  626. free(new);
  627. return (p);
  628. bad:
  629. free(old);
  630. free(new);
  631. return (NULL);
  632. }
  633. /*
  634. * Compile the text following an a, c, or i command.
  635. */
  636. static char *
  637. compile_text(void)
  638. {
  639. size_t asize, size;
  640. int esc_nl;
  641. char *lbuf, *text, *p, *op, *s;
  642. size_t bufsize;
  643. lbuf = text = NULL;
  644. asize = size = 0;
  645. while ((p = cu_fgets(&lbuf, &bufsize))) {
  646. size_t len = ROUNDLEN(strlen(p) + 1);
  647. if (asize - size < len) {
  648. do {
  649. asize += len;
  650. } while (asize - size < len);
  651. text = xrealloc(text, asize);
  652. }
  653. op = s = text + size;
  654. for (esc_nl = 0; *p != '\0'; p++) {
  655. if (*p == '\\' && p[1] != '\0' && *++p == '\n')
  656. esc_nl = 1;
  657. *s++ = *p;
  658. }
  659. size += s - op;
  660. if (!esc_nl) {
  661. *s = '\0';
  662. break;
  663. }
  664. }
  665. free(lbuf);
  666. text = xrealloc(text, size + 1);
  667. text[size] = '\0';
  668. return (text);
  669. }
  670. /*
  671. * Get an address and return a pointer to the first character after
  672. * it. Fill the structure pointed to according to the address.
  673. */
  674. static char *
  675. compile_addr(char *p, struct s_addr *a)
  676. {
  677. char *end;
  678. switch (*p) {
  679. case '\\': /* Context address */
  680. ++p;
  681. /* FALLTHROUGH */
  682. case '/': /* Context address */
  683. p = compile_re(p, &a->u.r);
  684. if (p == NULL)
  685. error(COMPILE, "unterminated regular expression");
  686. a->type = AT_RE;
  687. return (p);
  688. case '$': /* Last line */
  689. a->type = AT_LAST;
  690. return (p + 1);
  691. /* Line number */
  692. case '0': case '1': case '2': case '3': case '4':
  693. case '5': case '6': case '7': case '8': case '9':
  694. a->type = AT_LINE;
  695. a->u.l = strtoul(p, &end, 10);
  696. return (end);
  697. default:
  698. error(COMPILE, "expected context address");
  699. return (NULL);
  700. }
  701. }
  702. /*
  703. * duptoeol --
  704. * Return a copy of all the characters up to \n or \0.
  705. */
  706. static char *
  707. duptoeol(char *s, const char *ctype, char **semi)
  708. {
  709. size_t len;
  710. int ws;
  711. char *start;
  712. ws = 0;
  713. if (semi) {
  714. for (start = s; *s != '\0' && *s != '\n' && *s != ';'; ++s)
  715. ws = isspace((unsigned char)*s);
  716. } else {
  717. for (start = s; *s != '\0' && *s != '\n'; ++s)
  718. ws = isspace((unsigned char)*s);
  719. *s = '\0';
  720. }
  721. if (ws)
  722. warning("whitespace after %s", ctype);
  723. len = s - start + 1;
  724. if (semi)
  725. *semi = s;
  726. s = xmalloc(len);
  727. strlcpy(s, start, len);
  728. return (s);
  729. }
  730. /*
  731. * Convert goto label names to addresses, and count a and r commands, in
  732. * the given subset of the script. Free the memory used by labels in b
  733. * and t commands (but not by :).
  734. *
  735. * TODO: Remove } nodes
  736. */
  737. static void
  738. fixuplabel(struct s_command *cp, struct s_command *end)
  739. {
  740. for (; cp != end; cp = cp->next)
  741. switch (cp->code) {
  742. case 'a':
  743. case 'r':
  744. appendnum++;
  745. break;
  746. case 'b':
  747. case 't':
  748. /* Resolve branch target. */
  749. if (cp->t == NULL) {
  750. cp->u.c = NULL;
  751. break;
  752. }
  753. if ((cp->u.c = findlabel(cp->t)) == NULL)
  754. error(COMPILE, "undefined label '%s'", cp->t);
  755. free(cp->t);
  756. break;
  757. case '{':
  758. /* Do interior commands. */
  759. fixuplabel(cp->u.c, cp->next);
  760. break;
  761. }
  762. }
  763. /*
  764. * Associate the given command label for later lookup.
  765. */
  766. static void
  767. enterlabel(struct s_command *cp)
  768. {
  769. struct labhash **lhp, *lh;
  770. u_char *p;
  771. u_int h, c;
  772. for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
  773. h = (h << 5) + h + c;
  774. lhp = &labels[h & LHMASK];
  775. for (lh = *lhp; lh != NULL; lh = lh->lh_next)
  776. if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
  777. error(COMPILE, "duplicate label '%s'", cp->t);
  778. lh = xmalloc(sizeof *lh);
  779. lh->lh_next = *lhp;
  780. lh->lh_hash = h;
  781. lh->lh_cmd = cp;
  782. lh->lh_ref = 0;
  783. *lhp = lh;
  784. }
  785. /*
  786. * Find the label contained in the command l in the command linked
  787. * list cp. L is excluded from the search. Return NULL if not found.
  788. */
  789. static struct s_command *
  790. findlabel(char *name)
  791. {
  792. struct labhash *lh;
  793. u_char *p;
  794. u_int h, c;
  795. for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
  796. h = (h << 5) + h + c;
  797. for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
  798. if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
  799. lh->lh_ref = 1;
  800. return (lh->lh_cmd);
  801. }
  802. }
  803. return (NULL);
  804. }
  805. /*
  806. * Warn about any unused labels. As a side effect, release the label hash
  807. * table space.
  808. */
  809. static void
  810. uselabel(void)
  811. {
  812. struct labhash *lh, *next;
  813. int i;
  814. for (i = 0; i < LHSZ; i++) {
  815. for (lh = labels[i]; lh != NULL; lh = next) {
  816. next = lh->lh_next;
  817. if (!lh->lh_ref)
  818. warning("unused label '%s'",
  819. lh->lh_cmd->t);
  820. free(lh);
  821. }
  822. }
  823. }