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