1 /* $OpenBSD: main.c,v 1.44 2023/02/08 08:18:11 tb Exp $ */ 2 3 /*- 4 * Copyright (c) 1992 Diomidis Spinellis. 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Diomidis Spinellis of Imperial College, University of London. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/types.h> 37 #include <sys/ioctl.h> 38 #include <sys/stat.h> 39 40 #include <ctype.h> 41 #include <errno.h> 42 #include <fcntl.h> 43 #include <limits.h> 44 #include <regex.h> 45 #include <stddef.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 #include <libgen.h> 51 52 #include "defs.h" 53 #include "extern.h" 54 55 /* 56 * Linked list of units (strings and files) to be compiled 57 */ 58 struct s_compunit { 59 struct s_compunit *next; 60 enum e_cut {CU_FILE, CU_STRING} type; 61 char *s; /* Pointer to string or fname */ 62 }; 63 64 /* 65 * Linked list pointer to compilation units and pointer to current 66 * next pointer. 67 */ 68 static struct s_compunit *script, **cu_nextp = &script; 69 70 /* 71 * Linked list of files to be processed 72 */ 73 struct s_flist { 74 char *fname; 75 struct s_flist *next; 76 }; 77 78 /* 79 * Linked list pointer to files and pointer to current 80 * next pointer. 81 */ 82 static struct s_flist *files, **fl_nextp = &files; 83 84 FILE *infile; /* Current input file */ 85 FILE *outfile; /* Current output file */ 86 87 int Eflag, aflag, eflag, nflag; 88 static int rval; /* Exit status */ 89 90 /* 91 * Current file and line number; line numbers restart across compilation 92 * units, but span across input files. The latter is optional if editing 93 * in place. 94 */ 95 const char *fname; /* File name. */ 96 const char *outfname; /* Output file name */ 97 static char oldfname[PATH_MAX]; /* Old file name (for in-place editing) */ 98 static char tmpfname[PATH_MAX]; /* Temporary file name (for in-place editing) */ 99 char *inplace; /* Inplace edit file extension */ 100 u_long linenum; 101 102 static void add_compunit(enum e_cut, char *); 103 static void add_file(char *); 104 static int next_files_have_lines(void); 105 106 int termwidth; 107 108 int pledge_wpath, pledge_rpath; 109 110 int 111 main(int argc, char *argv[]) 112 { 113 struct winsize win; 114 int c, fflag; 115 char *p; 116 117 fflag = 0; 118 inplace = NULL; 119 while ((c = getopt(argc, argv, "Eae:f:i::nru")) != -1) 120 switch (c) { 121 case 'E': 122 case 'r': 123 Eflag = 1; 124 break; 125 case 'a': 126 aflag = 1; 127 break; 128 case 'e': 129 eflag = 1; 130 add_compunit(CU_STRING, optarg); 131 break; 132 case 'f': 133 fflag = 1; 134 add_compunit(CU_FILE, optarg); 135 break; 136 case 'i': 137 inplace = optarg ? optarg : ""; 138 break; 139 case 'n': 140 nflag = 1; 141 break; 142 case 'u': 143 setvbuf(stdout, NULL, _IOLBF, 0); 144 break; 145 default: 146 (void)fprintf(stderr, 147 "usage: sed [-aEnru] [-i[extension]] command [file ...]\n" 148 " sed [-aEnru] [-e command] [-f command_file] [-i[extension]] [file ...]\n"); 149 exit(1); 150 } 151 argc -= optind; 152 argv += optind; 153 154 termwidth = 0; 155 if ((p = getenv("COLUMNS")) != NULL) 156 termwidth = strtonum(p, 0, INT_MAX, NULL); 157 if (termwidth == 0 && ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 && 158 win.ws_col > 0) 159 termwidth = win.ws_col; 160 if (termwidth == 0) 161 termwidth = 80; 162 if (termwidth <= 8) 163 termwidth = 1; 164 else 165 termwidth -= 8; 166 167 if (inplace != NULL) { 168 if (pledge("stdio rpath wpath cpath fattr chown", NULL) == -1) 169 error(FATAL, "pledge: %s", strerror(errno)); 170 } else { 171 if (pledge("stdio rpath wpath cpath", NULL) == -1) 172 error(FATAL, "pledge: %s", strerror(errno)); 173 } 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 if (!pledge_wpath && inplace == NULL) { 186 if (pledge("stdio rpath", NULL) == -1) 187 error(FATAL, "pledge: %s", strerror(errno)); 188 } 189 for (; *argv; argv++) 190 add_file(*argv); 191 } else { 192 if (!pledge_wpath && !pledge_rpath) { 193 if (pledge("stdio", NULL) == -1) 194 error(FATAL, "pledge: %s", strerror(errno)); 195 } else if (pledge_rpath) { 196 if (pledge("stdio rpath", NULL) == -1) 197 error(FATAL, "pledge: %s", strerror(errno)); 198 } else if (pledge_wpath) { 199 if (pledge("stdio wpath cpath", NULL) == -1) 200 error(FATAL, "pledge: %s", strerror(errno)); 201 } 202 add_file(NULL); 203 } 204 process(); 205 cfclose(prog, NULL); 206 if (fclose(stdout)) 207 error(FATAL, "stdout: %s", strerror(errno)); 208 exit (rval); 209 } 210 211 /* 212 * Like fgets, but go through the chain of compilation units chaining them 213 * together. Empty strings and files are ignored. 214 */ 215 char * 216 cu_fgets(char **outbuf, size_t *outsize) 217 { 218 static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF; 219 static FILE *f; /* Current open file */ 220 static char *s; /* Current pointer inside string */ 221 static char string_ident[30]; 222 size_t len; 223 char *p; 224 225 if (*outbuf == NULL) 226 *outsize = 0; 227 228 again: 229 switch (state) { 230 case ST_EOF: 231 if (script == NULL) 232 return (NULL); 233 linenum = 0; 234 switch (script->type) { 235 case CU_FILE: 236 if ((f = fopen(script->s, "r")) == NULL) 237 error(FATAL, 238 "%s: %s", script->s, strerror(errno)); 239 fname = script->s; 240 state = ST_FILE; 241 goto again; 242 case CU_STRING: 243 len = snprintf(string_ident, sizeof(string_ident), 244 "\"%s\"", script->s); 245 if (len >= sizeof(string_ident)) 246 strlcpy(string_ident + 247 sizeof(string_ident) - 6, " ...\"", 5); 248 fname = string_ident; 249 s = script->s; 250 state = ST_STRING; 251 goto again; 252 } 253 case ST_FILE: 254 if (getline(outbuf, outsize, f) != -1) { 255 p = *outbuf; 256 linenum++; 257 if (linenum == 1 && p[0] == '#' && p[1] == 'n') 258 nflag = 1; 259 return (*outbuf); 260 } 261 script = script->next; 262 (void)fclose(f); 263 state = ST_EOF; 264 goto again; 265 case ST_STRING: 266 if (linenum == 0 && s[0] == '#' && s[1] == 'n') 267 nflag = 1; 268 p = *outbuf; 269 len = *outsize; 270 for (;;) { 271 if (len <= 1) { 272 *outbuf = xrealloc(*outbuf, 273 *outsize + _POSIX2_LINE_MAX); 274 p = *outbuf + *outsize - len; 275 len += _POSIX2_LINE_MAX; 276 *outsize += _POSIX2_LINE_MAX; 277 } 278 switch (*s) { 279 case '\0': 280 state = ST_EOF; 281 if (s == script->s) { 282 script = script->next; 283 goto again; 284 } else { 285 script = script->next; 286 *p = '\0'; 287 linenum++; 288 return (*outbuf); 289 } 290 case '\n': 291 *p++ = '\n'; 292 *p = '\0'; 293 s++; 294 linenum++; 295 return (*outbuf); 296 default: 297 *p++ = *s++; 298 len--; 299 } 300 } 301 } 302 303 return (NULL); 304 } 305 306 void 307 finish_file(void) 308 { 309 if (infile != NULL) { 310 fclose(infile); 311 if (*oldfname != '\0') { 312 if (rename(fname, oldfname) != 0) { 313 warning("rename()"); 314 unlink(tmpfname); 315 exit(1); 316 } 317 *oldfname = '\0'; 318 } 319 if (*tmpfname != '\0') { 320 if (outfile != NULL && outfile != stdout) 321 fclose(outfile); 322 outfile = NULL; 323 rename(tmpfname, fname); 324 *tmpfname = '\0'; 325 } 326 outfname = NULL; 327 } 328 } 329 330 /* 331 * Like fgets, but go through the list of files chaining them together. 332 * Set len to the length of the line. 333 */ 334 int 335 mf_fgets(SPACE *sp, enum e_spflag spflag) 336 { 337 struct stat sb; 338 size_t len; 339 char dirbuf[PATH_MAX]; 340 static char *p; 341 static size_t psize; 342 int c, fd; 343 static int firstfile; 344 345 if (infile == NULL) { 346 /* stdin? */ 347 if (files->fname == NULL) { 348 if (inplace != NULL) 349 error(FATAL, "-i may not be used with stdin"); 350 infile = stdin; 351 fname = "stdin"; 352 outfile = stdout; 353 outfname = "stdout"; 354 } 355 356 firstfile = 1; 357 } 358 359 for (;;) { 360 if (infile != NULL && (c = getc(infile)) != EOF) { 361 (void)ungetc(c, infile); 362 break; 363 } 364 /* If we are here then either eof or no files are open yet */ 365 if (infile == stdin) { 366 sp->len = 0; 367 return (0); 368 } 369 finish_file(); 370 if (firstfile == 0) 371 files = files->next; 372 else 373 firstfile = 0; 374 if (files == NULL) { 375 sp->len = 0; 376 return (0); 377 } 378 fname = files->fname; 379 if (inplace != NULL) { 380 if (lstat(fname, &sb) != 0) 381 error(FATAL, "%s: %s", fname, 382 strerror(errno ? errno : EIO)); 383 if (!S_ISREG(sb.st_mode)) 384 error(FATAL, "%s: %s %s", fname, 385 "in-place editing only", 386 "works for regular files"); 387 if (*inplace != '\0') { 388 strlcpy(oldfname, fname, 389 sizeof(oldfname)); 390 len = strlcat(oldfname, inplace, 391 sizeof(oldfname)); 392 if (len > sizeof(oldfname)) 393 error(FATAL, "%s: name too long", fname); 394 } 395 strlcpy(dirbuf, fname, sizeof(dirbuf)); 396 len = snprintf(tmpfname, sizeof(tmpfname), 397 "%s/sedXXXXXXXXXX", dirname(dirbuf)); 398 if (len >= sizeof(tmpfname)) 399 error(FATAL, "%s: name too long", fname); 400 if ((fd = mkstemp(tmpfname)) == -1) 401 error(FATAL, "%s: %s", fname, strerror(errno)); 402 if ((outfile = fdopen(fd, "w")) == NULL) { 403 unlink(tmpfname); 404 error(FATAL, "%s", fname); 405 } 406 fchown(fileno(outfile), sb.st_uid, sb.st_gid); 407 fchmod(fileno(outfile), sb.st_mode & ALLPERMS); 408 outfname = tmpfname; 409 linenum = 0; 410 resetstate(); 411 } else { 412 outfile = stdout; 413 outfname = "stdout"; 414 } 415 if ((infile = fopen(fname, "r")) == NULL) { 416 warning("%s", strerror(errno)); 417 rval = 1; 418 continue; 419 } 420 } 421 422 /* 423 * We are here only when infile is open and we still have something 424 * to read from it. 425 * 426 * Use getline() so that we can handle essentially infinite input 427 * data. The p and psize are static so each invocation gives 428 * getline() the same buffer which is expanded as needed. 429 */ 430 len = getline(&p, &psize, infile); 431 if ((ssize_t)len == -1) 432 error(FATAL, "%s: %s", fname, strerror(errno)); 433 if (len != 0 && p[len - 1] == '\n') { 434 sp->append_newline = 1; 435 len--; 436 } else if (!lastline()) { 437 sp->append_newline = 1; 438 } else { 439 sp->append_newline = 0; 440 } 441 cspace(sp, p, len, spflag); 442 443 linenum++; 444 445 return (1); 446 } 447 448 /* 449 * Add a compilation unit to the linked list 450 */ 451 static void 452 add_compunit(enum e_cut type, char *s) 453 { 454 struct s_compunit *cu; 455 456 cu = xmalloc(sizeof(struct s_compunit)); 457 cu->type = type; 458 cu->s = s; 459 cu->next = NULL; 460 *cu_nextp = cu; 461 cu_nextp = &cu->next; 462 } 463 464 /* 465 * Add a file to the linked list 466 */ 467 static void 468 add_file(char *s) 469 { 470 struct s_flist *fp; 471 472 fp = xmalloc(sizeof(struct s_flist)); 473 fp->next = NULL; 474 *fl_nextp = fp; 475 fp->fname = s; 476 fl_nextp = &fp->next; 477 } 478 479 480 static int 481 next_files_have_lines(void) 482 { 483 struct s_flist *file; 484 FILE *file_fd; 485 int ch; 486 487 file = files; 488 while ((file = file->next) != NULL) { 489 if ((file_fd = fopen(file->fname, "r")) == NULL) 490 continue; 491 492 if ((ch = getc(file_fd)) != EOF) { 493 /* 494 * This next file has content, therefore current 495 * file doesn't contains the last line. 496 */ 497 ungetc(ch, file_fd); 498 fclose(file_fd); 499 return (1); 500 } 501 fclose(file_fd); 502 } 503 return (0); 504 } 505 506 int 507 lastline(void) 508 { 509 int ch; 510 511 if (feof(infile)) { 512 return !( 513 (inplace == NULL) && 514 next_files_have_lines()); 515 } 516 if ((ch = getc(infile)) == EOF) { 517 return !( 518 (inplace == NULL) && 519 next_files_have_lines()); 520 } 521 ungetc(ch, infile); 522 return (0); 523 } 524