1 /* 2 * Copyright 2010 Nexenta Systems, Inc. All rights reserved. 3 * Copyright (c) 1992 Diomidis Spinellis. 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Diomidis Spinellis of Imperial College, University of London. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/types.h> 36 #include <sys/mman.h> 37 #include <sys/param.h> 38 #include <sys/stat.h> 39 40 #include <err.h> 41 #include <errno.h> 42 #include <fcntl.h> 43 #include <libgen.h> 44 #include <limits.h> 45 #include <locale.h> 46 #include <regex.h> 47 #include <stddef.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 #include <libintl.h> 53 54 #include "defs.h" 55 #include "extern.h" 56 57 /* 58 * Linked list of units (strings and files) to be compiled 59 */ 60 struct s_compunit { 61 struct s_compunit *next; 62 enum e_cut {CU_FILE, CU_STRING} type; 63 char *s; /* Pointer to string or fname */ 64 }; 65 66 /* 67 * Linked list pointer to compilation units and pointer to current 68 * next pointer. 69 */ 70 static struct s_compunit *script, **cu_nextp = &script; 71 72 /* 73 * Linked list of files to be processed 74 */ 75 struct s_flist { 76 char *fname; 77 struct s_flist *next; 78 }; 79 80 /* 81 * Linked list pointer to files and pointer to current 82 * next pointer. 83 */ 84 static struct s_flist *files, **fl_nextp = &files; 85 86 FILE *infile; /* Current input file */ 87 FILE *outfile; /* Current output file */ 88 89 int aflag, eflag, nflag; 90 int rflags = 0; 91 static int rval; /* Exit status */ 92 93 static int ispan; /* Whether inplace editing spans across files */ 94 95 /* 96 * Current file and line number; line numbers restart across compilation 97 * units, but span across input files. The latter is optional if editing 98 * in place. 99 */ 100 const char *fname; /* File name. */ 101 const char *outfname; /* Output file name */ 102 static char oldfname[PATH_MAX]; /* Old file name (for in-place editing) */ 103 static char tmpfname[PATH_MAX]; /* Temporary file name (for in-place editing) */ 104 static const char *inplace; /* Inplace edit file extension. */ 105 ulong_t linenum; 106 107 static void add_compunit(enum e_cut, char *); 108 static void add_file(char *); 109 static void usage(void); 110 static char *getln(FILE *, size_t *); 111 112 113 int 114 main(int argc, char *argv[]) 115 { 116 int c, fflag; 117 char *temp_arg; 118 119 (void) setlocale(LC_ALL, ""); 120 121 #ifndef TEXT_DOMAIN 122 #define TEXT_DOMAIN "SYS_TEST" 123 #endif 124 (void) textdomain(TEXT_DOMAIN); 125 126 fflag = 0; 127 inplace = NULL; 128 129 while ((c = getopt(argc, argv, "EI:ae:f:i:lnr")) != -1) 130 switch (c) { 131 case 'r': /* Gnu sed compat */ 132 case 'E': 133 rflags = REG_EXTENDED; 134 break; 135 case 'I': 136 inplace = optarg; 137 ispan = 1; /* span across input files */ 138 break; 139 case 'a': 140 aflag = 1; 141 break; 142 case 'e': 143 eflag = 1; 144 if (asprintf(&temp_arg, "%s\n", optarg) <= 1) 145 err(1, "asprintf"); 146 add_compunit(CU_STRING, temp_arg); 147 break; 148 case 'f': 149 fflag = 1; 150 add_compunit(CU_FILE, optarg); 151 break; 152 case 'i': 153 inplace = optarg; 154 ispan = 0; /* don't span across input files */ 155 break; 156 case 'l': 157 /* On SunOS, setlinebuf "returns no useful value */ 158 (void) setlinebuf(stdout); 159 break; 160 case 'n': 161 nflag = 1; 162 break; 163 default: 164 case '?': 165 usage(); 166 } 167 argc -= optind; 168 argv += optind; 169 170 /* First usage case; script is the first arg */ 171 if (!eflag && !fflag && *argv) { 172 add_compunit(CU_STRING, *argv); 173 argv++; 174 } 175 176 compile(); 177 178 /* Continue with first and start second usage */ 179 if (*argv) 180 for (; *argv; argv++) 181 add_file(*argv); 182 else 183 add_file(NULL); 184 process(); 185 cfclose(prog, NULL); 186 if (fclose(stdout)) 187 err(1, "stdout"); 188 return (rval); 189 } 190 191 static void 192 usage(void) 193 { 194 (void) fputs(_("usage: sed script [-Ealn] [-i extension] [file ...]\n" 195 " sed [-Ealn] [-i extension] [-e script] ... " 196 "[-f script_file] ... [file ...]"), 197 stderr); 198 exit(1); 199 } 200 201 /* 202 * Like fgets, but go through the chain of compilation units chaining them 203 * together. Empty strings and files are ignored. 204 */ 205 char * 206 cu_fgets(char *buf, int n, int *more) 207 { 208 static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF; 209 static FILE *f; /* Current open file */ 210 static char *s; /* Current pointer inside string */ 211 static char string_ident[30]; 212 char *p; 213 214 again: 215 switch (state) { 216 case ST_EOF: 217 if (script == NULL) { 218 if (more != NULL) 219 *more = 0; 220 return (NULL); 221 } 222 linenum = 0; 223 switch (script->type) { 224 case CU_FILE: 225 if ((f = fopen(script->s, "r")) == NULL) 226 err(1, "%s", script->s); 227 fname = script->s; 228 state = ST_FILE; 229 goto again; 230 case CU_STRING: 231 if (((size_t)snprintf(string_ident, 232 sizeof (string_ident), "\"%s\"", script->s)) >= 233 sizeof (string_ident) - 1) 234 (void) strcpy(string_ident + 235 sizeof (string_ident) - 6, " ...\""); 236 fname = string_ident; 237 s = script->s; 238 state = ST_STRING; 239 goto again; 240 } 241 /*NOTREACHED*/ 242 243 case ST_FILE: 244 if ((p = fgets(buf, n, f)) != NULL) { 245 linenum++; 246 if (linenum == 1 && buf[0] == '#' && buf[1] == 'n') 247 nflag = 1; 248 if (more != NULL) 249 *more = !feof(f); 250 return (p); 251 } 252 script = script->next; 253 (void) fclose(f); 254 state = ST_EOF; 255 goto again; 256 case ST_STRING: 257 if (linenum == 0 && s[0] == '#' && s[1] == 'n') 258 nflag = 1; 259 p = buf; 260 for (;;) { 261 if (n-- <= 1) { 262 *p = '\0'; 263 linenum++; 264 if (more != NULL) 265 *more = 1; 266 return (buf); 267 } 268 switch (*s) { 269 case '\0': 270 state = ST_EOF; 271 if (s == script->s) { 272 script = script->next; 273 goto again; 274 } else { 275 script = script->next; 276 *p = '\0'; 277 linenum++; 278 if (more != NULL) 279 *more = 0; 280 return (buf); 281 } 282 case '\n': 283 *p++ = '\n'; 284 *p = '\0'; 285 s++; 286 linenum++; 287 if (more != NULL) 288 *more = 0; 289 return (buf); 290 default: 291 *p++ = *s++; 292 } 293 } 294 } 295 /* NOTREACHED */ 296 return (NULL); 297 } 298 299 /* 300 * Like fgets, but go through the list of files chaining them together. 301 * Set len to the length of the line. 302 */ 303 int 304 mf_fgets(SPACE *sp, enum e_spflag spflag) 305 { 306 struct stat sb; 307 size_t len; 308 char *p; 309 int c; 310 static int firstfile; 311 312 if (infile == NULL) { 313 /* stdin? */ 314 if (files->fname == NULL) { 315 if (inplace != NULL) 316 errx(1, 317 _("-I or -i may not be used with stdin")); 318 infile = stdin; 319 fname = "stdin"; 320 outfile = stdout; 321 outfname = "stdout"; 322 } 323 firstfile = 1; 324 } 325 326 for (;;) { 327 if (infile != NULL && (c = getc(infile)) != EOF) { 328 (void) ungetc(c, infile); 329 break; 330 } 331 /* If we are here then either eof or no files are open yet */ 332 if (infile == stdin) { 333 sp->len = 0; 334 return (0); 335 } 336 if (infile != NULL) { 337 (void) fclose(infile); 338 if (*oldfname != '\0') { 339 if (link(fname, oldfname) != 0) { 340 warn("link()"); 341 (void) unlink(tmpfname); 342 exit(1); 343 } 344 *oldfname = '\0'; 345 } 346 if (*tmpfname != '\0') { 347 if (outfile != NULL && outfile != stdout) 348 if (fclose(outfile) != 0) { 349 warn("fclose()"); 350 (void) unlink(tmpfname); 351 exit(1); 352 } 353 outfile = NULL; 354 if (rename(tmpfname, fname) != 0) { 355 /* this should not happen really! */ 356 warn("rename()"); 357 (void) unlink(tmpfname); 358 exit(1); 359 } 360 *tmpfname = '\0'; 361 } 362 outfname = NULL; 363 } 364 if (firstfile == 0) 365 files = files->next; 366 else 367 firstfile = 0; 368 if (files == NULL) { 369 sp->len = 0; 370 return (0); 371 } 372 fname = files->fname; 373 if (inplace != NULL) { 374 char bn[PATH_MAX]; 375 char dn[PATH_MAX]; 376 (void) strlcpy(bn, fname, sizeof (bn)); 377 (void) strlcpy(dn, fname, sizeof (dn)); 378 if (lstat(fname, &sb) != 0) 379 err(1, "%s", fname); 380 if (!(sb.st_mode & S_IFREG)) 381 fatal(_("in-place editing only " 382 "works for regular files")); 383 if (*inplace != '\0') { 384 (void) strlcpy(oldfname, fname, 385 sizeof (oldfname)); 386 len = strlcat(oldfname, inplace, 387 sizeof (oldfname)); 388 if (len > sizeof (oldfname)) 389 fatal(_("name too long")); 390 } 391 len = snprintf(tmpfname, sizeof (tmpfname), 392 "%s/.!%ld!%s", dirname(dn), (long)getpid(), 393 basename(bn)); 394 if (len >= sizeof (tmpfname)) 395 fatal(_("name too long")); 396 (void) unlink(tmpfname); 397 if ((outfile = fopen(tmpfname, "w")) == NULL) 398 err(1, "%s", fname); 399 if (fchown(fileno(outfile), sb.st_uid, sb.st_gid) != 0) 400 warn("fchown()"); 401 if (fchmod(fileno(outfile), sb.st_mode & 07777) != 0) 402 warn("fchmod()"); 403 outfname = tmpfname; 404 if (!ispan) { 405 linenum = 0; 406 resetstate(); 407 } 408 } else { 409 outfile = stdout; 410 outfname = "stdout"; 411 } 412 if ((infile = fopen(fname, "r")) == NULL) { 413 warn("%s", fname); 414 rval = 1; 415 continue; 416 } 417 } 418 /* 419 * We are here only when infile is open and we still have something 420 * to read from it. 421 * 422 * Use fgetln so that we can handle essentially infinite input data. 423 * Can't use the pointer into the stdio buffer as the process space 424 * because the ungetc() can cause it to move. 425 */ 426 p = getln(infile, &len); 427 if (ferror(infile)) 428 errx(1, "%s: %s", fname, strerror(errno ? errno : EIO)); 429 if (len != 0 && p[len - 1] == '\n') 430 len--; 431 cspace(sp, p, len, spflag); 432 433 linenum++; 434 435 return (1); 436 } 437 438 /* 439 * Add a compilation unit to the linked list 440 */ 441 static void 442 add_compunit(enum e_cut type, char *s) 443 { 444 struct s_compunit *cu; 445 446 if ((cu = malloc(sizeof (struct s_compunit))) == NULL) 447 err(1, "malloc"); 448 cu->type = type; 449 cu->s = s; 450 cu->next = NULL; 451 *cu_nextp = cu; 452 cu_nextp = &cu->next; 453 } 454 455 /* 456 * Add a file to the linked list 457 */ 458 static void 459 add_file(char *s) 460 { 461 struct s_flist *fp; 462 463 if ((fp = malloc(sizeof (struct s_flist))) == NULL) 464 err(1, "malloc"); 465 fp->next = NULL; 466 *fl_nextp = fp; 467 fp->fname = s; 468 fl_nextp = &fp->next; 469 } 470 471 int 472 lastline(void) 473 { 474 int ch; 475 476 if (files->next != NULL && (inplace == NULL || ispan)) 477 return (0); 478 if ((ch = getc(infile)) == EOF) 479 return (1); 480 (void) ungetc(ch, infile); 481 return (0); 482 } 483 484 char * 485 getln(FILE *in, size_t *lenp) 486 { 487 static char *buffer = NULL; 488 static size_t sz = 0; 489 490 size_t len = 0; 491 492 for (;;) { 493 if (sz <= (len + 1)) { 494 char *nb; 495 if ((nb = realloc(buffer, sz + LINE_MAX)) == NULL) { 496 err(1, "realloc"); 497 } 498 buffer = nb; 499 sz += LINE_MAX; 500 } 501 502 buffer[len] = 0; 503 504 if (fgets(buffer + len, sz - len, in) == NULL) { 505 /* END OF FILE */ 506 *lenp = len; 507 break; 508 } 509 510 len += strlen(buffer + len); 511 512 if (buffer[len - 1] == '\n') { 513 /* got the new line */ 514 *lenp = len; 515 break; 516 } 517 } 518 519 return (buffer); 520 } 521