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. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#) Copyright (c) 1992, 1993 The Regents of the University of California. All rights reserved. 38 * @(#)main.c 8.2 (Berkeley) 1/3/94 39 * $FreeBSD: src/usr.bin/sed/main.c,v 1.9.2.7 2002/08/06 10:03:29 fanf Exp $ 40 * $DragonFly: src/usr.bin/sed/main.c,v 1.3 2003/10/04 20:36:50 hmp Exp $ 41 */ 42 43 #include <sys/types.h> 44 #include <sys/mman.h> 45 #include <sys/param.h> 46 #include <sys/stat.h> 47 48 #include <err.h> 49 #include <errno.h> 50 #include <fcntl.h> 51 #include <locale.h> 52 #include <regex.h> 53 #include <stddef.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <unistd.h> 58 59 #include "defs.h" 60 #include "extern.h" 61 62 /* 63 * Linked list of units (strings and files) to be compiled 64 */ 65 struct s_compunit { 66 struct s_compunit *next; 67 enum e_cut {CU_FILE, CU_STRING} type; 68 char *s; /* Pointer to string or fname */ 69 }; 70 71 /* 72 * Linked list pointer to compilation units and pointer to current 73 * next pointer. 74 */ 75 static struct s_compunit *script, **cu_nextp = &script; 76 77 /* 78 * Linked list of files to be processed 79 */ 80 struct s_flist { 81 char *fname; 82 struct s_flist *next; 83 }; 84 85 /* 86 * Linked list pointer to files and pointer to current 87 * next pointer. 88 */ 89 static struct s_flist *files, **fl_nextp = &files; 90 91 static FILE *curfile; /* Current open file */ 92 93 int aflag, eflag, nflag; 94 int rflags = 0; 95 static int rval; /* Exit status */ 96 97 /* 98 * Current file and line number; line numbers restart across compilation 99 * units, but span across input files. 100 */ 101 const char *fname; /* File name. */ 102 const char *inplace; /* Inplace edit file extension. */ 103 u_long linenum; 104 105 static void add_compunit(enum e_cut, char *); 106 static void add_file(char *); 107 static int inplace_edit(char **); 108 static void usage(void); 109 110 int 111 main(int argc, char **argv) 112 { 113 int c, fflag; 114 char *temp_arg; 115 116 (void) setlocale(LC_ALL, ""); 117 118 fflag = 0; 119 inplace = NULL; 120 121 while ((c = getopt(argc, argv, "Eae:f:i:n")) != -1) 122 switch (c) { 123 case 'E': 124 rflags = REG_EXTENDED; 125 break; 126 case 'a': 127 aflag = 1; 128 break; 129 case 'e': 130 eflag = 1; 131 if ((temp_arg = malloc(strlen(optarg) + 2)) == NULL) 132 err(1, "malloc"); 133 strcpy(temp_arg, optarg); 134 strcat(temp_arg, "\n"); 135 add_compunit(CU_STRING, temp_arg); 136 break; 137 case 'f': 138 fflag = 1; 139 add_compunit(CU_FILE, optarg); 140 break; 141 case 'i': 142 inplace = optarg; 143 break; 144 case 'n': 145 nflag = 1; 146 break; 147 default: 148 case '?': 149 usage(); 150 } 151 argc -= optind; 152 argv += optind; 153 154 /* First usage case; script is the first arg */ 155 if (!eflag && !fflag && *argv) { 156 add_compunit(CU_STRING, *argv); 157 argv++; 158 } 159 160 compile(); 161 162 /* Continue with first and start second usage */ 163 if (*argv) 164 for (; *argv; argv++) 165 add_file(*argv); 166 else 167 add_file(NULL); 168 process(); 169 cfclose(prog, NULL); 170 if (fclose(stdout)) 171 err(1, "stdout"); 172 exit(rval); 173 } 174 175 static void 176 usage(void) 177 { 178 (void)fprintf(stderr, "%s\n%s\n", 179 "usage: sed script [-Ean] [-i extension] [file ...]", 180 " sed [-an] [-i extension] [-e script] ... [-f script_file] ... [file ...]"); 181 exit(1); 182 } 183 184 /* 185 * Like fgets, but go through the chain of compilation units chaining them 186 * together. Empty strings and files are ignored. 187 */ 188 char * 189 cu_fgets(char *buf, int n, int *more) 190 { 191 static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF; 192 static FILE *f; /* Current open file */ 193 static char *s; /* Current pointer inside string */ 194 static char string_ident[30]; 195 char *p; 196 197 again: 198 switch (state) { 199 case ST_EOF: 200 if (script == NULL) { 201 if (more != NULL) 202 *more = 0; 203 return (NULL); 204 } 205 linenum = 0; 206 switch (script->type) { 207 case CU_FILE: 208 if ((f = fopen(script->s, "r")) == NULL) 209 err(1, "%s", script->s); 210 fname = script->s; 211 state = ST_FILE; 212 goto again; 213 case CU_STRING: 214 if ((snprintf(string_ident, 215 sizeof(string_ident), "\"%s\"", script->s)) >= 216 sizeof(string_ident) - 1) 217 (void)strcpy(string_ident + 218 sizeof(string_ident) - 6, " ...\""); 219 fname = string_ident; 220 s = script->s; 221 state = ST_STRING; 222 goto again; 223 } 224 case ST_FILE: 225 if ((p = fgets(buf, n, f)) != NULL) { 226 linenum++; 227 if (linenum == 1 && buf[0] == '#' && buf[1] == 'n') 228 nflag = 1; 229 if (more != NULL) 230 *more = !feof(f); 231 return (p); 232 } 233 script = script->next; 234 (void)fclose(f); 235 state = ST_EOF; 236 goto again; 237 case ST_STRING: 238 if (linenum == 0 && s[0] == '#' && s[1] == 'n') 239 nflag = 1; 240 p = buf; 241 for (;;) { 242 if (n-- <= 1) { 243 *p = '\0'; 244 linenum++; 245 if (more != NULL) 246 *more = 1; 247 return (buf); 248 } 249 switch (*s) { 250 case '\0': 251 state = ST_EOF; 252 if (s == script->s) { 253 script = script->next; 254 goto again; 255 } else { 256 script = script->next; 257 *p = '\0'; 258 linenum++; 259 if (more != NULL) 260 *more = 0; 261 return (buf); 262 } 263 case '\n': 264 *p++ = '\n'; 265 *p = '\0'; 266 s++; 267 linenum++; 268 if (more != NULL) 269 *more = 0; 270 return (buf); 271 default: 272 *p++ = *s++; 273 } 274 } 275 } 276 /* NOTREACHED */ 277 return (NULL); 278 } 279 280 /* 281 * Like fgets, but go through the list of files chaining them together. 282 * Set len to the length of the line. 283 */ 284 int 285 mf_fgets(SPACE *sp, enum e_spflag spflag) 286 { 287 size_t len; 288 char *p; 289 int c; 290 static int firstfile; 291 292 if (curfile == NULL) { 293 /* stdin? */ 294 if (files->fname == NULL) { 295 if (inplace != NULL) 296 errx(1, "-i may not be used with stdin"); 297 curfile = stdin; 298 fname = "stdin"; 299 } 300 firstfile = 1; 301 } 302 303 for (;;) { 304 if (curfile != NULL && (c = getc(curfile)) != EOF) { 305 (void)ungetc(c, curfile); 306 break; 307 } 308 /* If we are here then either eof or no files are open yet */ 309 if (curfile == stdin) { 310 sp->len = 0; 311 return (0); 312 } 313 if (curfile != NULL) { 314 fclose(curfile); 315 } 316 if (firstfile == 0) { 317 files = files->next; 318 } else 319 firstfile = 0; 320 if (files == NULL) { 321 sp->len = 0; 322 return (0); 323 } 324 if (inplace != NULL) { 325 if (inplace_edit(&files->fname) == -1) 326 continue; 327 } 328 fname = files->fname; 329 if ((curfile = fopen(fname, "r")) == NULL) { 330 warn("%s", fname); 331 rval = 1; 332 continue; 333 } 334 if (inplace != NULL && *inplace == '\0') 335 unlink(fname); 336 } 337 /* 338 * We are here only when curfile is open and we still have something 339 * to read from it. 340 * 341 * Use fgetln so that we can handle essentially infinite input data. 342 * Can't use the pointer into the stdio buffer as the process space 343 * because the ungetc() can cause it to move. 344 */ 345 p = fgetln(curfile, &len); 346 if (ferror(curfile)) 347 errx(1, "%s: %s", fname, strerror(errno ? errno : EIO)); 348 if (len != 0 && p[len - 1] == '\n') 349 len--; 350 cspace(sp, p, len, spflag); 351 352 linenum++; 353 354 return (1); 355 } 356 357 /* 358 * Add a compilation unit to the linked list 359 */ 360 static void 361 add_compunit(enum e_cut type, char *s) 362 { 363 struct s_compunit *cu; 364 365 if ((cu = malloc(sizeof(struct s_compunit))) == NULL) 366 err(1, "malloc"); 367 cu->type = type; 368 cu->s = s; 369 cu->next = NULL; 370 *cu_nextp = cu; 371 cu_nextp = &cu->next; 372 } 373 374 /* 375 * Add a file to the linked list 376 */ 377 static void 378 add_file(char *s) 379 { 380 struct s_flist *fp; 381 382 if ((fp = malloc(sizeof(struct s_flist))) == NULL) 383 err(1, "malloc"); 384 fp->next = NULL; 385 *fl_nextp = fp; 386 fp->fname = s; 387 fl_nextp = &fp->next; 388 } 389 390 /* 391 * Modify a pointer to a filename for inplace editing and reopen stdout 392 */ 393 static int 394 inplace_edit(char **filename) 395 { 396 struct stat orig; 397 char backup[MAXPATHLEN]; 398 399 if (lstat(*filename, &orig) == -1) 400 err(1, "lstat"); 401 if ((orig.st_mode & S_IFREG) == 0) { 402 warnx("cannot inplace edit %s, not a regular file", *filename); 403 return -1; 404 } 405 406 if (*inplace == '\0') { 407 /* 408 * This is a bit of a hack: we use mkstemp() to avoid the 409 * mktemp() link-time warning, although mktemp() would fit in 410 * this context much better. We're only interested in getting 411 * a name for use in the rename(); there aren't any security 412 * issues here that don't already exist in relation to the 413 * original file and its directory. 414 */ 415 int fd; 416 strlcpy(backup, *filename, sizeof(backup)); 417 strlcat(backup, ".XXXXXXXXXX", sizeof(backup)); 418 fd = mkstemp(backup); 419 if (fd == -1) 420 errx(1, "could not create backup of %s", *filename); 421 else 422 close(fd); 423 } else { 424 strlcpy(backup, *filename, sizeof(backup)); 425 strlcat(backup, inplace, sizeof(backup)); 426 } 427 428 if (rename(*filename, backup) == -1) 429 err(1, "rename(\"%s\", \"%s\")", *filename, backup); 430 if (freopen(*filename, "w", stdout) == NULL) 431 err(1, "open(\"%s\")", *filename); 432 if (fchmod(fileno(stdout), orig.st_mode) == -1) 433 err(1, "chmod(\"%s\")", *filename); 434 *filename = strdup(backup); 435 if (*filename == NULL) 436 err(1, "malloc"); 437 return 0; 438 } 439 440 int 441 lastline(void) 442 { 443 int ch; 444 445 if (files->next != NULL) 446 return (0); 447 if ((ch = getc(curfile)) == EOF) 448 return (1); 449 ungetc(ch, curfile); 450 return (0); 451 } 452