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