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 * 3. 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 * @(#) Copyright (c) 1992, 1993 The Regents of the University of California. All rights reserved. 34 * @(#)main.c 8.2 (Berkeley) 1/3/94 35 * $FreeBSD: src/usr.bin/sed/main.c,v 1.41 2008/02/09 09:12:02 dwmalone Exp $ 36 * $DragonFly: src/usr.bin/sed/main.c,v 1.4 2008/04/08 13:23:38 swildner Exp $ 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 <regex.h> 51 #include <stddef.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 57 #include "defs.h" 58 #include "extern.h" 59 60 /* 61 * Linked list of units (strings and files) to be compiled 62 */ 63 struct s_compunit { 64 struct s_compunit *next; 65 enum e_cut {CU_FILE, CU_STRING} type; 66 char *s; /* Pointer to string or fname */ 67 }; 68 69 /* 70 * Linked list pointer to compilation units and pointer to current 71 * next pointer. 72 */ 73 static struct s_compunit *script, **cu_nextp = &script; 74 75 /* 76 * Linked list of files to be processed 77 */ 78 struct s_flist { 79 char *fname; 80 struct s_flist *next; 81 }; 82 83 /* 84 * Linked list pointer to files and pointer to current 85 * next pointer. 86 */ 87 static struct s_flist *files, **fl_nextp = &files; 88 89 FILE *infile; /* Current input file */ 90 FILE *outfile; /* Current output file */ 91 92 int aflag, eflag, nflag; 93 int rflags = 0; 94 static int rval; /* Exit status */ 95 96 static int ispan; /* Whether inplace editing spans across files */ 97 98 /* 99 * Current file and line number; line numbers restart across compilation 100 * units, but span across input files. The latter is optional if editing 101 * in place. 102 */ 103 const char *fname; /* File name. */ 104 const char *outfname; /* Output file name */ 105 static char oldfname[PATH_MAX]; /* Old file name (for in-place editing) */ 106 static char tmpfname[PATH_MAX]; /* Temporary file name (for in-place editing) */ 107 static const char *inplace; /* Inplace edit file extension. */ 108 u_long linenum; 109 110 static void add_compunit(enum e_cut, char *); 111 static void add_file(char *); 112 static void usage(void); 113 114 int 115 main(int argc, char *argv[]) 116 { 117 int c, fflag; 118 char *temp_arg; 119 120 (void) setlocale(LC_ALL, ""); 121 122 fflag = 0; 123 inplace = NULL; 124 125 while ((c = getopt(argc, argv, "EI:ae:f:i:lnr")) != -1) 126 switch (c) { 127 case 'r': /* GNU sed compat */ 128 case 'E': 129 rflags = REG_EXTENDED; 130 break; 131 case 'I': 132 inplace = optarg; 133 ispan = 1; /* span across input files */ 134 break; 135 case 'a': 136 aflag = 1; 137 break; 138 case 'e': 139 eflag = 1; 140 if ((temp_arg = malloc(strlen(optarg) + 2)) == NULL) 141 err(1, "malloc"); 142 strcpy(temp_arg, optarg); 143 strcat(temp_arg, "\n"); 144 add_compunit(CU_STRING, temp_arg); 145 break; 146 case 'f': 147 fflag = 1; 148 add_compunit(CU_FILE, optarg); 149 break; 150 case 'i': 151 inplace = optarg; 152 ispan = 0; /* don't span across input files */ 153 break; 154 case 'l': 155 if(setlinebuf(stdout) != 0) 156 warnx("setlinebuf() failed"); 157 break; 158 case 'n': 159 nflag = 1; 160 break; 161 default: 162 case '?': 163 usage(); 164 } 165 argc -= optind; 166 argv += optind; 167 168 /* First usage case; script is the first arg */ 169 if (!eflag && !fflag && *argv) { 170 add_compunit(CU_STRING, *argv); 171 argv++; 172 } 173 174 compile(); 175 176 /* Continue with first and start second usage */ 177 if (*argv) 178 for (; *argv; argv++) 179 add_file(*argv); 180 else 181 add_file(NULL); 182 process(); 183 cfclose(prog, NULL); 184 if (fclose(stdout)) 185 err(1, "stdout"); 186 exit(rval); 187 } 188 189 static void 190 usage(void) 191 { 192 (void)fprintf(stderr, "%s\n%s\n", 193 "usage: sed script [-Ealn] [-i extension] [file ...]", 194 " sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]"); 195 exit(1); 196 } 197 198 /* 199 * Like fgets, but go through the chain of compilation units chaining them 200 * together. Empty strings and files are ignored. 201 */ 202 char * 203 cu_fgets(char *buf, int n, int *more) 204 { 205 static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF; 206 static FILE *f; /* Current open file */ 207 static char *s; /* Current pointer inside string */ 208 static char string_ident[30]; 209 char *p; 210 211 again: 212 switch (state) { 213 case ST_EOF: 214 if (script == NULL) { 215 if (more != NULL) 216 *more = 0; 217 return (NULL); 218 } 219 linenum = 0; 220 switch (script->type) { 221 case CU_FILE: 222 if ((f = fopen(script->s, "r")) == NULL) 223 err(1, "%s", script->s); 224 fname = script->s; 225 state = ST_FILE; 226 goto again; 227 case CU_STRING: 228 if (((size_t)snprintf(string_ident, 229 sizeof(string_ident), "\"%s\"", script->s)) >= 230 sizeof(string_ident) - 1) 231 (void)strcpy(string_ident + 232 sizeof(string_ident) - 6, " ...\""); 233 fname = string_ident; 234 s = script->s; 235 state = ST_STRING; 236 goto again; 237 } 238 case ST_FILE: 239 if ((p = fgets(buf, n, f)) != NULL) { 240 linenum++; 241 if (linenum == 1 && buf[0] == '#' && buf[1] == 'n') 242 nflag = 1; 243 if (more != NULL) 244 *more = !feof(f); 245 return (p); 246 } 247 script = script->next; 248 (void)fclose(f); 249 state = ST_EOF; 250 goto again; 251 case ST_STRING: 252 if (linenum == 0 && s[0] == '#' && s[1] == 'n') 253 nflag = 1; 254 p = buf; 255 for (;;) { 256 if (n-- <= 1) { 257 *p = '\0'; 258 linenum++; 259 if (more != NULL) 260 *more = 1; 261 return (buf); 262 } 263 switch (*s) { 264 case '\0': 265 state = ST_EOF; 266 if (s == script->s) { 267 script = script->next; 268 goto again; 269 } else { 270 script = script->next; 271 *p = '\0'; 272 linenum++; 273 if (more != NULL) 274 *more = 0; 275 return (buf); 276 } 277 case '\n': 278 *p++ = '\n'; 279 *p = '\0'; 280 s++; 281 linenum++; 282 if (more != NULL) 283 *more = 0; 284 return (buf); 285 default: 286 *p++ = *s++; 287 } 288 } 289 } 290 /* NOTREACHED */ 291 return (NULL); 292 } 293 294 /* 295 * Like fgets, but go through the list of files chaining them together. 296 * Set len to the length of the line. 297 */ 298 int 299 mf_fgets(SPACE *sp, enum e_spflag spflag) 300 { 301 struct stat sb; 302 size_t len; 303 char *p; 304 int c; 305 static int firstfile; 306 307 if (infile == NULL) { 308 /* stdin? */ 309 if (files->fname == NULL) { 310 if (inplace != NULL) 311 errx(1, "-I or -i may not be used with stdin"); 312 infile = stdin; 313 fname = "stdin"; 314 outfile = stdout; 315 outfname = "stdout"; 316 } 317 firstfile = 1; 318 } 319 320 for (;;) { 321 if (infile != NULL && (c = getc(infile)) != EOF) { 322 (void)ungetc(c, infile); 323 break; 324 } 325 /* If we are here then either eof or no files are open yet */ 326 if (infile == stdin) { 327 sp->len = 0; 328 return (0); 329 } 330 if (infile != NULL) { 331 fclose(infile); 332 if (*oldfname != '\0') { 333 if (rename(fname, oldfname) != 0) { 334 warn("rename()"); 335 unlink(tmpfname); 336 exit(1); 337 } 338 *oldfname = '\0'; 339 } 340 if (*tmpfname != '\0') { 341 if (outfile != NULL && outfile != stdout) 342 fclose(outfile); 343 outfile = NULL; 344 rename(tmpfname, fname); 345 *tmpfname = '\0'; 346 } 347 outfname = NULL; 348 } 349 if (firstfile == 0) 350 files = files->next; 351 else 352 firstfile = 0; 353 if (files == NULL) { 354 sp->len = 0; 355 return (0); 356 } 357 fname = files->fname; 358 if (inplace != NULL) { 359 if (lstat(fname, &sb) != 0) 360 err(1, "%s", fname); 361 if (!(sb.st_mode & S_IFREG)) 362 errx(1, "%s: %s %s", fname, 363 "in-place editing only", 364 "works for regular files"); 365 if (*inplace != '\0') { 366 strlcpy(oldfname, fname, 367 sizeof(oldfname)); 368 len = strlcat(oldfname, inplace, 369 sizeof(oldfname)); 370 if (len > sizeof(oldfname)) 371 errx(1, "%s: name too long", fname); 372 } 373 len = snprintf(tmpfname, sizeof(tmpfname), 374 "%s/.!%ld!%s", dirname(fname), (long)getpid(), 375 basename(fname)); 376 if (len >= sizeof(tmpfname)) 377 errx(1, "%s: name too long", fname); 378 unlink(tmpfname); 379 if ((outfile = fopen(tmpfname, "w")) == NULL) 380 err(1, "%s", fname); 381 fchown(fileno(outfile), sb.st_uid, sb.st_gid); 382 fchmod(fileno(outfile), sb.st_mode & ALLPERMS); 383 outfname = tmpfname; 384 if (!ispan) { 385 linenum = 0; 386 resetstate(); 387 } 388 } else { 389 outfile = stdout; 390 outfname = "stdout"; 391 } 392 if ((infile = fopen(fname, "r")) == NULL) { 393 warn("%s", fname); 394 rval = 1; 395 continue; 396 } 397 } 398 /* 399 * We are here only when infile is open and we still have something 400 * to read from it. 401 * 402 * Use fgetln so that we can handle essentially infinite input data. 403 * Can't use the pointer into the stdio buffer as the process space 404 * because the ungetc() can cause it to move. 405 */ 406 p = fgetln(infile, &len); 407 if (ferror(infile)) 408 errx(1, "%s: %s", fname, strerror(errno ? errno : EIO)); 409 if (len != 0 && p[len - 1] == '\n') 410 len--; 411 cspace(sp, p, len, spflag); 412 413 linenum++; 414 415 return (1); 416 } 417 418 /* 419 * Add a compilation unit to the linked list 420 */ 421 static void 422 add_compunit(enum e_cut type, char *s) 423 { 424 struct s_compunit *cu; 425 426 if ((cu = malloc(sizeof(struct s_compunit))) == NULL) 427 err(1, "malloc"); 428 cu->type = type; 429 cu->s = s; 430 cu->next = NULL; 431 *cu_nextp = cu; 432 cu_nextp = &cu->next; 433 } 434 435 /* 436 * Add a file to the linked list 437 */ 438 static void 439 add_file(char *s) 440 { 441 struct s_flist *fp; 442 443 if ((fp = malloc(sizeof(struct s_flist))) == NULL) 444 err(1, "malloc"); 445 fp->next = NULL; 446 *fl_nextp = fp; 447 fp->fname = s; 448 fl_nextp = &fp->next; 449 } 450 451 int 452 lastline(void) 453 { 454 int ch; 455 456 if (files->next != NULL && (inplace == NULL || ispan)) 457 return (0); 458 if ((ch = getc(infile)) == EOF) 459 return (1); 460 ungetc(ch, infile); 461 return (0); 462 } 463