1 /*- 2 * Copyright (c) 2013 Johann 'Myrkraverk' Oskarsson. 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 * 3. 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 * @(#) Copyright (c) 1992, 1993 The Regents of the University of California. All rights reserved. 35 * @(#)main.c 8.2 (Berkeley) 1/3/94 36 * $FreeBSD: head/usr.bin/sed/main.c 277811 2015-01-27 19:46:19Z pfg $ 37 */ 38 39 #include <sys/types.h> 40 #include <sys/mman.h> 41 #include <sys/param.h> 42 #include <sys/stat.h> 43 44 #include <err.h> 45 #include <errno.h> 46 #include <fcntl.h> 47 #include <libgen.h> 48 #include <limits.h> 49 #include <locale.h> 50 #include <stddef.h> 51 #define _WITH_GETLINE 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 #include <regex.h> 57 58 #include "defs.h" 59 #include "extern.h" 60 61 /* 62 * Linked list of units (strings and files) to be compiled 63 */ 64 struct s_compunit { 65 struct s_compunit *next; 66 enum e_cut {CU_FILE, CU_STRING} type; 67 char *s; /* Pointer to string or fname */ 68 }; 69 70 /* 71 * Linked list pointer to compilation units and pointer to current 72 * next pointer. 73 */ 74 static struct s_compunit *script, **cu_nextp = &script; 75 76 /* 77 * Linked list of files to be processed 78 */ 79 struct s_flist { 80 char *fname; 81 struct s_flist *next; 82 }; 83 84 /* 85 * Linked list pointer to files and pointer to current 86 * next pointer. 87 */ 88 static struct s_flist *files, **fl_nextp = &files; 89 90 FILE *infile; /* Current input file */ 91 FILE *outfile; /* Current output file */ 92 93 int aflag, eflag, nflag; 94 int rflags = 0; 95 static int rval; /* Exit status */ 96 97 static int ispan; /* Whether inplace editing spans across files */ 98 99 /* 100 * Current file and line number; line numbers restart across compilation 101 * units, but span across input files. The latter is optional if editing 102 * in place. 103 */ 104 const char *fname; /* File name. */ 105 const char *outfname; /* Output file name */ 106 static char oldfname[PATH_MAX]; /* Old file name (for in-place editing) */ 107 static char tmpfname[PATH_MAX]; /* Temporary file name (for in-place editing) */ 108 static const char *inplace; /* Inplace edit file extension. */ 109 u_long linenum; 110 111 static void add_compunit(enum e_cut, char *); 112 static void add_file(char *); 113 static void usage(void); 114 115 int 116 main(int argc, char *argv[]) 117 { 118 int c, fflag; 119 char *temp_arg; 120 121 (void) setlocale(LC_ALL, ""); 122 123 fflag = 0; 124 inplace = NULL; 125 126 while ((c = getopt(argc, argv, "EI:ae:f:i:lnru")) != -1) 127 switch (c) { 128 case 'r': /* GNU sed compat */ 129 case 'E': 130 rflags = REG_EXTENDED; 131 break; 132 case 'I': 133 inplace = optarg; 134 ispan = 1; /* span across input files */ 135 break; 136 case 'a': 137 aflag = 1; 138 break; 139 case 'e': 140 eflag = 1; 141 if ((temp_arg = malloc(strlen(optarg) + 2)) == NULL) 142 err(1, "malloc"); 143 strcpy(temp_arg, optarg); 144 strcat(temp_arg, "\n"); 145 add_compunit(CU_STRING, temp_arg); 146 break; 147 case 'f': 148 fflag = 1; 149 add_compunit(CU_FILE, optarg); 150 break; 151 case 'i': 152 inplace = optarg; 153 ispan = 0; /* don't span across input files */ 154 break; 155 case 'l': 156 if(setvbuf(stdout, NULL, _IOLBF, 0) != 0) 157 warnx("setting line buffered output failed"); 158 break; 159 case 'n': 160 nflag = 1; 161 break; 162 case 'u': 163 if(setvbuf(stdout, NULL, _IONBF, 0) != 0) 164 warnx("setting unbuffered output failed"); 165 break; 166 default: 167 case '?': 168 usage(); 169 } 170 argc -= optind; 171 argv += optind; 172 173 /* First usage case; script is the first arg */ 174 if (!eflag && !fflag && *argv) { 175 add_compunit(CU_STRING, *argv); 176 argv++; 177 } 178 179 compile(); 180 181 /* Continue with first and start second usage */ 182 if (*argv) 183 for (; *argv; argv++) 184 add_file(*argv); 185 else 186 add_file(NULL); 187 process(); 188 cfclose(prog, NULL); 189 if (fclose(stdout)) 190 err(1, "stdout"); 191 exit(rval); 192 } 193 194 static void 195 usage(void) 196 { 197 (void)fprintf(stderr, 198 "usage: %s script [-Ealnru] [-i extension] [file ...]\n" 199 "\t%s [-Ealnu] [-i extension] [-e script] ... [-f script_file]" 200 " ... [file ...]\n", getprogname(), getprogname()); 201 exit(1); 202 } 203 204 /* 205 * Like fgets, but go through the chain of compilation units chaining them 206 * together. Empty strings and files are ignored. 207 */ 208 char * 209 cu_fgets(char *buf, int n, int *more) 210 { 211 static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF; 212 static FILE *f; /* Current open file */ 213 static char *s; /* Current pointer inside string */ 214 static char string_ident[30]; 215 char *p; 216 217 again: 218 switch (state) { 219 case ST_EOF: 220 if (script == NULL) { 221 if (more != NULL) 222 *more = 0; 223 return (NULL); 224 } 225 linenum = 0; 226 switch (script->type) { 227 case CU_FILE: 228 if ((f = fopen(script->s, "r")) == NULL) 229 err(1, "%s", script->s); 230 fname = script->s; 231 state = ST_FILE; 232 goto again; 233 case CU_STRING: 234 if (((size_t)snprintf(string_ident, 235 sizeof(string_ident), "\"%s\"", script->s)) >= 236 sizeof(string_ident) - 1) 237 (void)strcpy(string_ident + 238 sizeof(string_ident) - 6, " ...\""); 239 fname = string_ident; 240 s = script->s; 241 state = ST_STRING; 242 goto again; 243 } 244 case ST_FILE: 245 if ((p = fgets(buf, n, f)) != NULL) { 246 linenum++; 247 if (linenum == 1 && buf[0] == '#' && buf[1] == 'n') 248 nflag = 1; 249 if (more != NULL) 250 *more = !feof(f); 251 return (p); 252 } 253 script = script->next; 254 (void)fclose(f); 255 state = ST_EOF; 256 goto again; 257 case ST_STRING: 258 if (linenum == 0 && s[0] == '#' && s[1] == 'n') 259 nflag = 1; 260 p = buf; 261 for (;;) { 262 if (n-- <= 1) { 263 *p = '\0'; 264 linenum++; 265 if (more != NULL) 266 *more = 1; 267 return (buf); 268 } 269 switch (*s) { 270 case '\0': 271 state = ST_EOF; 272 if (s == script->s) { 273 script = script->next; 274 goto again; 275 } else { 276 script = script->next; 277 *p = '\0'; 278 linenum++; 279 if (more != NULL) 280 *more = 0; 281 return (buf); 282 } 283 case '\n': 284 *p++ = '\n'; 285 *p = '\0'; 286 s++; 287 linenum++; 288 if (more != NULL) 289 *more = 0; 290 return (buf); 291 default: 292 *p++ = *s++; 293 } 294 } 295 } 296 /* NOTREACHED */ 297 return (NULL); 298 } 299 300 /* 301 * Like fgets, but go through the list of files chaining them together. 302 * Set len to the length of the line. 303 */ 304 int 305 mf_fgets(SPACE *sp, enum e_spflag spflag) 306 { 307 struct stat sb; 308 ssize_t len; 309 char *dirbuf, *basebuf; 310 static char *p = NULL; 311 static size_t plen = 0; 312 int c; 313 static int firstfile; 314 315 if (infile == NULL) { 316 /* stdin? */ 317 if (files->fname == NULL) { 318 if (inplace != NULL) 319 errx(1, "-I or -i may not be used with stdin"); 320 infile = stdin; 321 fname = "stdin"; 322 outfile = stdout; 323 outfname = "stdout"; 324 } 325 firstfile = 1; 326 } 327 328 for (;;) { 329 if (infile != NULL && (c = getc(infile)) != EOF) { 330 (void)ungetc(c, infile); 331 break; 332 } 333 /* If we are here then either eof or no files are open yet */ 334 if (infile == stdin) { 335 sp->len = 0; 336 return (0); 337 } 338 if (infile != NULL) { 339 fclose(infile); 340 if (*oldfname != '\0') { 341 /* if there was a backup file, remove it */ 342 unlink(oldfname); 343 /* 344 * Backup the original. Note that hard links 345 * are not supported on all filesystems. 346 */ 347 if ((link(fname, oldfname) != 0) && 348 (rename(fname, oldfname) != 0)) { 349 warn("rename()"); 350 if (*tmpfname) 351 unlink(tmpfname); 352 exit(1); 353 } 354 *oldfname = '\0'; 355 } 356 if (*tmpfname != '\0') { 357 if (outfile != NULL && outfile != stdout) 358 if (fclose(outfile) != 0) { 359 warn("fclose()"); 360 unlink(tmpfname); 361 exit(1); 362 } 363 outfile = NULL; 364 if (rename(tmpfname, fname) != 0) { 365 /* this should not happen really! */ 366 warn("rename()"); 367 unlink(tmpfname); 368 exit(1); 369 } 370 *tmpfname = '\0'; 371 } 372 outfname = NULL; 373 } 374 if (firstfile == 0) 375 files = files->next; 376 else 377 firstfile = 0; 378 if (files == NULL) { 379 sp->len = 0; 380 return (0); 381 } 382 fname = files->fname; 383 if (inplace != NULL) { 384 if (lstat(fname, &sb) != 0) 385 err(1, "%s", fname); 386 if (!(sb.st_mode & S_IFREG)) 387 errx(1, "%s: %s %s", fname, 388 "in-place editing only", 389 "works for regular files"); 390 if (*inplace != '\0') { 391 strlcpy(oldfname, fname, 392 sizeof(oldfname)); 393 len = strlcat(oldfname, inplace, 394 sizeof(oldfname)); 395 if (len > (ssize_t)sizeof(oldfname)) 396 errx(1, "%s: name too long", fname); 397 } 398 if ((dirbuf = strdup(fname)) == NULL || 399 (basebuf = strdup(fname)) == NULL) 400 err(1, "strdup"); 401 len = snprintf(tmpfname, sizeof(tmpfname), 402 "%s/.!%ld!%s", dirname(dirbuf), (long)getpid(), 403 basename(basebuf)); 404 free(dirbuf); 405 free(basebuf); 406 if (len >= (ssize_t)sizeof(tmpfname)) 407 errx(1, "%s: name too long", fname); 408 unlink(tmpfname); 409 if (outfile != NULL && outfile != stdout) 410 fclose(outfile); 411 if ((outfile = fopen(tmpfname, "w")) == NULL) 412 err(1, "%s", fname); 413 fchown(fileno(outfile), sb.st_uid, sb.st_gid); 414 fchmod(fileno(outfile), sb.st_mode & ALLPERMS); 415 outfname = tmpfname; 416 if (!ispan) { 417 linenum = 0; 418 resetstate(); 419 } 420 } else { 421 outfile = stdout; 422 outfname = "stdout"; 423 } 424 if ((infile = fopen(fname, "r")) == NULL) { 425 warn("%s", fname); 426 rval = 1; 427 continue; 428 } 429 } 430 /* 431 * We are here only when infile is open and we still have something 432 * to read from it. 433 * 434 * Use getline() so that we can handle essentially infinite input 435 * data. The p and plen are static so each invocation gives 436 * getline() the same buffer which is expanded as needed. 437 */ 438 len = getline(&p, &plen, infile); 439 if (len == -1) 440 err(1, "%s", fname); 441 if (len != 0 && p[len - 1] == '\n') { 442 sp->append_newline = 1; 443 len--; 444 } else if (!lastline()) { 445 sp->append_newline = 1; 446 } else { 447 sp->append_newline = 0; 448 } 449 cspace(sp, p, len, spflag); 450 451 linenum++; 452 453 return (1); 454 } 455 456 /* 457 * Add a compilation unit to the linked list 458 */ 459 static void 460 add_compunit(enum e_cut type, char *s) 461 { 462 struct s_compunit *cu; 463 464 if ((cu = malloc(sizeof(struct s_compunit))) == NULL) 465 err(1, "malloc"); 466 cu->type = type; 467 cu->s = s; 468 cu->next = NULL; 469 *cu_nextp = cu; 470 cu_nextp = &cu->next; 471 } 472 473 /* 474 * Add a file to the linked list 475 */ 476 static void 477 add_file(char *s) 478 { 479 struct s_flist *fp; 480 481 if ((fp = malloc(sizeof(struct s_flist))) == NULL) 482 err(1, "malloc"); 483 fp->next = NULL; 484 *fl_nextp = fp; 485 fp->fname = s; 486 fl_nextp = &fp->next; 487 } 488 489 static int 490 next_files_have_lines(void) 491 { 492 struct s_flist *file; 493 FILE *file_fd; 494 int ch; 495 496 file = files; 497 while ((file = file->next) != NULL) { 498 if ((file_fd = fopen(file->fname, "r")) == NULL) 499 continue; 500 501 if ((ch = getc(file_fd)) != EOF) { 502 /* 503 * This next file has content, therefore current 504 * file doesn't contains the last line. 505 */ 506 ungetc(ch, file_fd); 507 fclose(file_fd); 508 return (1); 509 } 510 511 fclose(file_fd); 512 } 513 514 return (0); 515 } 516 517 int 518 lastline(void) 519 { 520 int ch; 521 522 if (feof(infile)) { 523 return !( 524 (inplace == NULL || ispan) && 525 next_files_have_lines()); 526 } 527 if ((ch = getc(infile)) == EOF) { 528 return !( 529 (inplace == NULL || ispan) && 530 next_files_have_lines()); 531 } 532 ungetc(ch, infile); 533 return (0); 534 } 535