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