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 /* 105 * Current file and line number; line numbers restart across compilation 106 * units, but span across input files. 107 */ 108 const char *fname; /* File name. */ 109 const char *outfname; /* Output file name */ 110 static char oldfname[PATH_MAX]; /* Old file name (for in-place editing) */ 111 static char tmpfname[PATH_MAX]; /* Temporary file name (for in-place editing) */ 112 const char *inplace; /* Inplace edit file extension. */ 113 u_long linenum; 114 115 static void add_compunit(enum e_cut, char *); 116 static void add_file(char *); 117 static int inplace_edit(char **); 118 static void usage(void); 119 120 int 121 main(int argc, char *argv[]) 122 { 123 int c, fflag; 124 char *temp_arg; 125 126 (void) setlocale(LC_ALL, ""); 127 128 fflag = 0; 129 inplace = NULL; 130 131 while ((c = getopt(argc, argv, "Eae:f:i:ln")) != -1) 132 switch (c) { 133 case 'E': 134 rflags = REG_EXTENDED; 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 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 ((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 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 } else { 385 outfile = stdout; 386 outfname = "stdout"; 387 } 388 if ((infile = fopen(fname, "r")) == NULL) { 389 warn("%s", fname); 390 rval = 1; 391 continue; 392 } 393 } 394 /* 395 * We are here only when infile is open and we still have something 396 * to read from it. 397 * 398 * Use fgetln so that we can handle essentially infinite input data. 399 * Can't use the pointer into the stdio buffer as the process space 400 * because the ungetc() can cause it to move. 401 */ 402 p = fgetln(infile, &len); 403 if (ferror(infile)) 404 errx(1, "%s: %s", fname, strerror(errno ? errno : EIO)); 405 if (len != 0 && p[len - 1] == '\n') 406 len--; 407 cspace(sp, p, len, spflag); 408 409 linenum++; 410 411 return (1); 412 } 413 414 /* 415 * Add a compilation unit to the linked list 416 */ 417 static void 418 add_compunit(enum e_cut type, char *s) 419 { 420 struct s_compunit *cu; 421 422 if ((cu = malloc(sizeof(struct s_compunit))) == NULL) 423 err(1, "malloc"); 424 cu->type = type; 425 cu->s = s; 426 cu->next = NULL; 427 *cu_nextp = cu; 428 cu_nextp = &cu->next; 429 } 430 431 /* 432 * Add a file to the linked list 433 */ 434 static void 435 add_file(char *s) 436 { 437 struct s_flist *fp; 438 439 if ((fp = malloc(sizeof(struct s_flist))) == NULL) 440 err(1, "malloc"); 441 fp->next = NULL; 442 *fl_nextp = fp; 443 fp->fname = s; 444 fl_nextp = &fp->next; 445 } 446 447 int 448 lastline(void) 449 { 450 int ch; 451 452 if (files->next != NULL) 453 return (0); 454 if ((ch = getc(infile)) == EOF) 455 return (1); 456 ungetc(ch, infile); 457 return (0); 458 } 459