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