19b50d902SRodney W. Grimes /*- 21a2a4fc8SPedro F. Giffuni * Copyright (c) 2013 Johann 'Myrkraverk' Oskarsson. 39b50d902SRodney W. Grimes * Copyright (c) 1992 Diomidis Spinellis. 49b50d902SRodney W. Grimes * Copyright (c) 1992, 1993 59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 69b50d902SRodney W. Grimes * 79b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by 89b50d902SRodney W. Grimes * Diomidis Spinellis of Imperial College, University of London. 99b50d902SRodney W. Grimes * 109b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 119b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 129b50d902SRodney W. Grimes * are met: 139b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 159b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 169b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 179b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 189b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 199b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 209b50d902SRodney W. Grimes * without specific prior written permission. 219b50d902SRodney W. Grimes * 229b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 239b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 249b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 259b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 269b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 279b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 289b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 299b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 309b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 319b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 329b50d902SRodney W. Grimes * SUCH DAMAGE. 339b50d902SRodney W. Grimes */ 349b50d902SRodney W. Grimes 35e74bf75fSMark Murray #include <sys/cdefs.h> 36e74bf75fSMark Murray __FBSDID("$FreeBSD$"); 37e74bf75fSMark Murray 389b50d902SRodney W. Grimes #ifndef lint 3973a08bb2SPhilippe Charnier static const char copyright[] = 409b50d902SRodney W. Grimes "@(#) Copyright (c) 1992, 1993\n\ 419b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 42e74bf75fSMark Murray #endif 439b50d902SRodney W. Grimes 449b50d902SRodney W. Grimes #ifndef lint 45e74bf75fSMark Murray static const char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/3/94"; 4673a08bb2SPhilippe Charnier #endif 479b50d902SRodney W. Grimes 488701af62SMaxim Sobolev #include <sys/types.h> 498701af62SMaxim Sobolev #include <sys/mman.h> 50839af0c1SJuli Mallett #include <sys/param.h> 51839af0c1SJuli Mallett #include <sys/stat.h> 529b50d902SRodney W. Grimes 5373a08bb2SPhilippe Charnier #include <err.h> 549b50d902SRodney W. Grimes #include <errno.h> 55821df508SXin LI #include <fcntl.h> 56a3eb24c6SStefan Farfeleder #include <libgen.h> 5781a8648aSTim J. Robbins #include <limits.h> 58726aebe5SAndrey A. Chernov #include <locale.h> 599b50d902SRodney W. Grimes #include <regex.h> 60821df508SXin LI #include <stddef.h> 619b50d902SRodney W. Grimes #include <stdio.h> 629b50d902SRodney W. Grimes #include <stdlib.h> 639b50d902SRodney W. Grimes #include <string.h> 649b50d902SRodney W. Grimes #include <unistd.h> 659b50d902SRodney W. Grimes 669b50d902SRodney W. Grimes #include "defs.h" 679b50d902SRodney W. Grimes #include "extern.h" 689b50d902SRodney W. Grimes 699b50d902SRodney W. Grimes /* 709b50d902SRodney W. Grimes * Linked list of units (strings and files) to be compiled 719b50d902SRodney W. Grimes */ 729b50d902SRodney W. Grimes struct s_compunit { 739b50d902SRodney W. Grimes struct s_compunit *next; 749b50d902SRodney W. Grimes enum e_cut {CU_FILE, CU_STRING} type; 75b4932d18SPedro F. Giffuni const char *s; /* Pointer to string or fname */ 769b50d902SRodney W. Grimes }; 779b50d902SRodney W. Grimes 789b50d902SRodney W. Grimes /* 799b50d902SRodney W. Grimes * Linked list pointer to compilation units and pointer to current 809b50d902SRodney W. Grimes * next pointer. 819b50d902SRodney W. Grimes */ 829b50d902SRodney W. Grimes static struct s_compunit *script, **cu_nextp = &script; 839b50d902SRodney W. Grimes 849b50d902SRodney W. Grimes /* 859b50d902SRodney W. Grimes * Linked list of files to be processed 869b50d902SRodney W. Grimes */ 879b50d902SRodney W. Grimes struct s_flist { 88b4932d18SPedro F. Giffuni const char *fname; 899b50d902SRodney W. Grimes struct s_flist *next; 909b50d902SRodney W. Grimes }; 919b50d902SRodney W. Grimes 929b50d902SRodney W. Grimes /* 939b50d902SRodney W. Grimes * Linked list pointer to files and pointer to current 949b50d902SRodney W. Grimes * next pointer. 959b50d902SRodney W. Grimes */ 969b50d902SRodney W. Grimes static struct s_flist *files, **fl_nextp = &files; 979b50d902SRodney W. Grimes 98f54cda14SDag-Erling Smørgrav FILE *infile; /* Current input file */ 99f54cda14SDag-Erling Smørgrav FILE *outfile; /* Current output file */ 1008523e9a6STim J. Robbins 1019b50d902SRodney W. Grimes int aflag, eflag, nflag; 102175de1e6SBrian Feldman int rflags = 0; 1033af4dcb2STim J. Robbins static int rval; /* Exit status */ 1049b50d902SRodney W. Grimes 105f6703c9cSYaroslav Tykhiy static int ispan; /* Whether inplace editing spans across files */ 106f6703c9cSYaroslav Tykhiy 1079b50d902SRodney W. Grimes /* 1089b50d902SRodney W. Grimes * Current file and line number; line numbers restart across compilation 109f6703c9cSYaroslav Tykhiy * units, but span across input files. The latter is optional if editing 110f6703c9cSYaroslav Tykhiy * in place. 1119b50d902SRodney W. Grimes */ 112e74bf75fSMark Murray const char *fname; /* File name. */ 113f54cda14SDag-Erling Smørgrav const char *outfname; /* Output file name */ 114f54cda14SDag-Erling Smørgrav static char oldfname[PATH_MAX]; /* Old file name (for in-place editing) */ 115f54cda14SDag-Erling Smørgrav static char tmpfname[PATH_MAX]; /* Temporary file name (for in-place editing) */ 116a511e639SDiomidis Spinellis static const char *inplace; /* Inplace edit file extension. */ 1179b50d902SRodney W. Grimes u_long linenum; 1189b50d902SRodney W. Grimes 119b4932d18SPedro F. Giffuni static void add_compunit(enum e_cut, const char *); 120b4932d18SPedro F. Giffuni static void add_file(const char *); 1213f330d7dSWarner Losh static void usage(void); 1229b50d902SRodney W. Grimes 1239b50d902SRodney W. Grimes int 124e6478125SDag-Erling Smørgrav main(int argc, char *argv[]) 1259b50d902SRodney W. Grimes { 126*637a1a74SEnji Cooper char *temp_arg; 1279b50d902SRodney W. Grimes int c, fflag; 1289b50d902SRodney W. Grimes 129726aebe5SAndrey A. Chernov (void) setlocale(LC_ALL, ""); 130726aebe5SAndrey A. Chernov 1319b50d902SRodney W. Grimes fflag = 0; 132839af0c1SJuli Mallett inplace = NULL; 133839af0c1SJuli Mallett 134fc39ce9eSPedro F. Giffuni while ((c = getopt(argc, argv, "EI:ae:f:i:lnru")) != -1) 1359b50d902SRodney W. Grimes switch (c) { 136701d73b6SWarner Losh case 'r': /* Gnu sed compat */ 137175de1e6SBrian Feldman case 'E': 138175de1e6SBrian Feldman rflags = REG_EXTENDED; 139175de1e6SBrian Feldman break; 140f6703c9cSYaroslav Tykhiy case 'I': 141f6703c9cSYaroslav Tykhiy inplace = optarg; 142f6703c9cSYaroslav Tykhiy ispan = 1; /* span across input files */ 143f6703c9cSYaroslav Tykhiy break; 1449b50d902SRodney W. Grimes case 'a': 1459b50d902SRodney W. Grimes aflag = 1; 1469b50d902SRodney W. Grimes break; 1479b50d902SRodney W. Grimes case 'e': 1489b50d902SRodney W. Grimes eflag = 1; 149*637a1a74SEnji Cooper asprintf(&temp_arg, "%s\n", optarg); 150*637a1a74SEnji Cooper if (temp_arg == NULL) 151*637a1a74SEnji Cooper errx(1, "Couldn't allocate temporary buffer"); 152*637a1a74SEnji Cooper add_compunit(CU_STRING, temp_arg); 1539b50d902SRodney W. Grimes break; 1549b50d902SRodney W. Grimes case 'f': 1559b50d902SRodney W. Grimes fflag = 1; 1569b50d902SRodney W. Grimes add_compunit(CU_FILE, optarg); 1579b50d902SRodney W. Grimes break; 158839af0c1SJuli Mallett case 'i': 159839af0c1SJuli Mallett inplace = optarg; 160f6703c9cSYaroslav Tykhiy ispan = 0; /* don't span across input files */ 161839af0c1SJuli Mallett break; 1621ed86f6cSGleb Smirnoff case 'l': 16390d81f30SPedro F. Giffuni if(setvbuf(stdout, NULL, _IOLBF, 0) != 0) 16490d81f30SPedro F. Giffuni warnx("setting line buffered output failed"); 1651ed86f6cSGleb Smirnoff break; 1669b50d902SRodney W. Grimes case 'n': 1679b50d902SRodney W. Grimes nflag = 1; 1689b50d902SRodney W. Grimes break; 169fc39ce9eSPedro F. Giffuni case 'u': 17090d81f30SPedro F. Giffuni if(setvbuf(stdout, NULL, _IONBF, 0) != 0) 171fc39ce9eSPedro F. Giffuni warnx("setting unbuffered output failed"); 172fc39ce9eSPedro F. Giffuni break; 1739b50d902SRodney W. Grimes default: 1749b50d902SRodney W. Grimes case '?': 17573a08bb2SPhilippe Charnier usage(); 1769b50d902SRodney W. Grimes } 1779b50d902SRodney W. Grimes argc -= optind; 1789b50d902SRodney W. Grimes argv += optind; 1799b50d902SRodney W. Grimes 1809b50d902SRodney W. Grimes /* First usage case; script is the first arg */ 1819b50d902SRodney W. Grimes if (!eflag && !fflag && *argv) { 1829b50d902SRodney W. Grimes add_compunit(CU_STRING, *argv); 1839b50d902SRodney W. Grimes argv++; 1849b50d902SRodney W. Grimes } 1859b50d902SRodney W. Grimes 1869b50d902SRodney W. Grimes compile(); 1879b50d902SRodney W. Grimes 1889b50d902SRodney W. Grimes /* Continue with first and start second usage */ 1899b50d902SRodney W. Grimes if (*argv) 1909b50d902SRodney W. Grimes for (; *argv; argv++) 1919b50d902SRodney W. Grimes add_file(*argv); 1929b50d902SRodney W. Grimes else 1939b50d902SRodney W. Grimes add_file(NULL); 1949b50d902SRodney W. Grimes process(); 1959b50d902SRodney W. Grimes cfclose(prog, NULL); 1969b50d902SRodney W. Grimes if (fclose(stdout)) 19773a08bb2SPhilippe Charnier err(1, "stdout"); 1983af4dcb2STim J. Robbins exit(rval); 1999b50d902SRodney W. Grimes } 2009b50d902SRodney W. Grimes 20173a08bb2SPhilippe Charnier static void 202e6478125SDag-Erling Smørgrav usage(void) 20373a08bb2SPhilippe Charnier { 204fc39ce9eSPedro F. Giffuni (void)fprintf(stderr, 205f2431000SPedro F. Giffuni "usage: %s script [-Ealnru] [-i extension] [file ...]\n" 2069b788a2eSPedro F. Giffuni "\t%s [-Ealnu] [-i extension] [-e script] ... [-f script_file]" 207fc39ce9eSPedro F. Giffuni " ... [file ...]\n", getprogname(), getprogname()); 20873a08bb2SPhilippe Charnier exit(1); 20973a08bb2SPhilippe Charnier } 21073a08bb2SPhilippe Charnier 2119b50d902SRodney W. Grimes /* 2129b50d902SRodney W. Grimes * Like fgets, but go through the chain of compilation units chaining them 2139b50d902SRodney W. Grimes * together. Empty strings and files are ignored. 2149b50d902SRodney W. Grimes */ 215b4932d18SPedro F. Giffuni const char * 216b4932d18SPedro F. Giffuni cu_fgets(int *more) 2179b50d902SRodney W. Grimes { 2189b50d902SRodney W. Grimes static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF; 2199b50d902SRodney W. Grimes static FILE *f; /* Current open file */ 220b4932d18SPedro F. Giffuni static const char *s; /* Current pointer inside string */ 221b4932d18SPedro F. Giffuni static char string_ident[30], *lastresult; 222b4932d18SPedro F. Giffuni static size_t lastsize; 2239b50d902SRodney W. Grimes char *p; 224b4932d18SPedro F. Giffuni const char *start; 2259b50d902SRodney W. Grimes 2269b50d902SRodney W. Grimes again: 2279b50d902SRodney W. Grimes switch (state) { 2289b50d902SRodney W. Grimes case ST_EOF: 229c56690efSArchie Cobbs if (script == NULL) { 230c56690efSArchie Cobbs if (more != NULL) 231c56690efSArchie Cobbs *more = 0; 2329b50d902SRodney W. Grimes return (NULL); 233c56690efSArchie Cobbs } 2349b50d902SRodney W. Grimes linenum = 0; 2359b50d902SRodney W. Grimes switch (script->type) { 2369b50d902SRodney W. Grimes case CU_FILE: 2379b50d902SRodney W. Grimes if ((f = fopen(script->s, "r")) == NULL) 23873a08bb2SPhilippe Charnier err(1, "%s", script->s); 2399b50d902SRodney W. Grimes fname = script->s; 2409b50d902SRodney W. Grimes state = ST_FILE; 2419b50d902SRodney W. Grimes goto again; 2429b50d902SRodney W. Grimes case CU_STRING: 243d3e5e11cSDavid Malone if (((size_t)snprintf(string_ident, 2449b50d902SRodney W. Grimes sizeof(string_ident), "\"%s\"", script->s)) >= 2459b50d902SRodney W. Grimes sizeof(string_ident) - 1) 2469b50d902SRodney W. Grimes (void)strcpy(string_ident + 2479b50d902SRodney W. Grimes sizeof(string_ident) - 6, " ...\""); 2489b50d902SRodney W. Grimes fname = string_ident; 2499b50d902SRodney W. Grimes s = script->s; 2509b50d902SRodney W. Grimes state = ST_STRING; 2519b50d902SRodney W. Grimes goto again; 2529b50d902SRodney W. Grimes } 2539b50d902SRodney W. Grimes case ST_FILE: 254b4932d18SPedro F. Giffuni p = lastresult; 255b4932d18SPedro F. Giffuni if (getline(&p, &lastsize, f) != -1) { 2569b50d902SRodney W. Grimes linenum++; 257b4932d18SPedro F. Giffuni if (linenum == 1 && p[0] == '#' && p[1] == 'n') 2589b50d902SRodney W. Grimes nflag = 1; 259c56690efSArchie Cobbs if (more != NULL) 260c56690efSArchie Cobbs *more = !feof(f); 261b4932d18SPedro F. Giffuni return (lastresult = p); 262b4932d18SPedro F. Giffuni } else if (ferror(f)) 263b4932d18SPedro F. Giffuni err(1, "%s", script->s); 2649b50d902SRodney W. Grimes script = script->next; 2659b50d902SRodney W. Grimes (void)fclose(f); 2669b50d902SRodney W. Grimes state = ST_EOF; 2679b50d902SRodney W. Grimes goto again; 2689b50d902SRodney W. Grimes case ST_STRING: 2699b50d902SRodney W. Grimes if (linenum == 0 && s[0] == '#' && s[1] == 'n') 2709b50d902SRodney W. Grimes nflag = 1; 271b4932d18SPedro F. Giffuni else if (s[0] == '\0') { 272b4932d18SPedro F. Giffuni state = ST_EOF; 273b4932d18SPedro F. Giffuni script = script->next; 274b4932d18SPedro F. Giffuni goto again; 2759b50d902SRodney W. Grimes } 276b4932d18SPedro F. Giffuni start = s; 277b4932d18SPedro F. Giffuni for (;;) { 2789b50d902SRodney W. Grimes switch (*s) { 2799b50d902SRodney W. Grimes case '\0': 2809b50d902SRodney W. Grimes state = ST_EOF; 2819b50d902SRodney W. Grimes script = script->next; 282b4932d18SPedro F. Giffuni /* FALLTHROUGH */ 2839b50d902SRodney W. Grimes case '\n': 2849b50d902SRodney W. Grimes s++; 2859b50d902SRodney W. Grimes linenum++; 286c56690efSArchie Cobbs if (more != NULL) 287c56690efSArchie Cobbs *more = 0; 288b4932d18SPedro F. Giffuni return (start); 2899b50d902SRodney W. Grimes default: 290b4932d18SPedro F. Giffuni s++; 2919b50d902SRodney W. Grimes } 2929b50d902SRodney W. Grimes } 2939b50d902SRodney W. Grimes } 2949b50d902SRodney W. Grimes /* NOTREACHED */ 29573a08bb2SPhilippe Charnier return (NULL); 2969b50d902SRodney W. Grimes } 2979b50d902SRodney W. Grimes 2989b50d902SRodney W. Grimes /* 2999b50d902SRodney W. Grimes * Like fgets, but go through the list of files chaining them together. 3009b50d902SRodney W. Grimes * Set len to the length of the line. 3019b50d902SRodney W. Grimes */ 3029b50d902SRodney W. Grimes int 303e6478125SDag-Erling Smørgrav mf_fgets(SPACE *sp, enum e_spflag spflag) 3049b50d902SRodney W. Grimes { 305f54cda14SDag-Erling Smørgrav struct stat sb; 3061a2a4fc8SPedro F. Giffuni ssize_t len; 3076718ba18SEd Schouten char *dirbuf, *basebuf; 3081a2a4fc8SPedro F. Giffuni static char *p = NULL; 3091a2a4fc8SPedro F. Giffuni static size_t plen = 0; 3105f4cf81eSWolfram Schneider int c; 3118701af62SMaxim Sobolev static int firstfile; 3129b50d902SRodney W. Grimes 313f54cda14SDag-Erling Smørgrav if (infile == NULL) { 3148701af62SMaxim Sobolev /* stdin? */ 3159b50d902SRodney W. Grimes if (files->fname == NULL) { 316839af0c1SJuli Mallett if (inplace != NULL) 317f6703c9cSYaroslav Tykhiy errx(1, "-I or -i may not be used with stdin"); 318f54cda14SDag-Erling Smørgrav infile = stdin; 3199b50d902SRodney W. Grimes fname = "stdin"; 320f54cda14SDag-Erling Smørgrav outfile = stdout; 321f54cda14SDag-Erling Smørgrav outfname = "stdout"; 3228701af62SMaxim Sobolev } 3238701af62SMaxim Sobolev firstfile = 1; 3248701af62SMaxim Sobolev } 3258701af62SMaxim Sobolev 3268701af62SMaxim Sobolev for (;;) { 327f54cda14SDag-Erling Smørgrav if (infile != NULL && (c = getc(infile)) != EOF) { 328f54cda14SDag-Erling Smørgrav (void)ungetc(c, infile); 3298701af62SMaxim Sobolev break; 3308701af62SMaxim Sobolev } 3318701af62SMaxim Sobolev /* If we are here then either eof or no files are open yet */ 332f54cda14SDag-Erling Smørgrav if (infile == stdin) { 333254fac85STim J. Robbins sp->len = 0; 3348701af62SMaxim Sobolev return (0); 3358701af62SMaxim Sobolev } 336f54cda14SDag-Erling Smørgrav if (infile != NULL) { 337f54cda14SDag-Erling Smørgrav fclose(infile); 3387bbdbe14SBrian Somers if (*oldfname != '\0') { 339919e14f0SJilles Tjoelker /* if there was a backup file, remove it */ 340919e14f0SJilles Tjoelker unlink(oldfname); 341919e14f0SJilles Tjoelker /* 342919e14f0SJilles Tjoelker * Backup the original. Note that hard links 343919e14f0SJilles Tjoelker * are not supported on all filesystems. 344919e14f0SJilles Tjoelker */ 345919e14f0SJilles Tjoelker if ((link(fname, oldfname) != 0) && 346919e14f0SJilles Tjoelker (rename(fname, oldfname) != 0)) { 347f54cda14SDag-Erling Smørgrav warn("rename()"); 348919e14f0SJilles Tjoelker if (*tmpfname) 349f54cda14SDag-Erling Smørgrav unlink(tmpfname); 350f54cda14SDag-Erling Smørgrav exit(1); 3518701af62SMaxim Sobolev } 3527bbdbe14SBrian Somers *oldfname = '\0'; 3537bbdbe14SBrian Somers } 3547bbdbe14SBrian Somers if (*tmpfname != '\0') { 3557bbdbe14SBrian Somers if (outfile != NULL && outfile != stdout) 356919e14f0SJilles Tjoelker if (fclose(outfile) != 0) { 357919e14f0SJilles Tjoelker warn("fclose()"); 358919e14f0SJilles Tjoelker unlink(tmpfname); 359919e14f0SJilles Tjoelker exit(1); 360919e14f0SJilles Tjoelker } 3617bbdbe14SBrian Somers outfile = NULL; 362919e14f0SJilles Tjoelker if (rename(tmpfname, fname) != 0) { 363919e14f0SJilles Tjoelker /* this should not happen really! */ 364919e14f0SJilles Tjoelker warn("rename()"); 365919e14f0SJilles Tjoelker unlink(tmpfname); 366919e14f0SJilles Tjoelker exit(1); 367919e14f0SJilles Tjoelker } 3687bbdbe14SBrian Somers *tmpfname = '\0'; 3697bbdbe14SBrian Somers } 370f54cda14SDag-Erling Smørgrav outfname = NULL; 371f54cda14SDag-Erling Smørgrav } 372f54cda14SDag-Erling Smørgrav if (firstfile == 0) 3738701af62SMaxim Sobolev files = files->next; 374f54cda14SDag-Erling Smørgrav else 3758701af62SMaxim Sobolev firstfile = 0; 3768701af62SMaxim Sobolev if (files == NULL) { 377254fac85STim J. Robbins sp->len = 0; 3788701af62SMaxim Sobolev return (0); 3798701af62SMaxim Sobolev } 3809b50d902SRodney W. Grimes fname = files->fname; 381f54cda14SDag-Erling Smørgrav if (inplace != NULL) { 382f54cda14SDag-Erling Smørgrav if (lstat(fname, &sb) != 0) 383f54cda14SDag-Erling Smørgrav err(1, "%s", fname); 384f54cda14SDag-Erling Smørgrav if (!(sb.st_mode & S_IFREG)) 385f54cda14SDag-Erling Smørgrav errx(1, "%s: %s %s", fname, 386f54cda14SDag-Erling Smørgrav "in-place editing only", 387f54cda14SDag-Erling Smørgrav "works for regular files"); 388f54cda14SDag-Erling Smørgrav if (*inplace != '\0') { 389f54cda14SDag-Erling Smørgrav strlcpy(oldfname, fname, 390f54cda14SDag-Erling Smørgrav sizeof(oldfname)); 391f54cda14SDag-Erling Smørgrav len = strlcat(oldfname, inplace, 392f54cda14SDag-Erling Smørgrav sizeof(oldfname)); 393b4932d18SPedro F. Giffuni if ((size_t)len > sizeof(oldfname)) 394f54cda14SDag-Erling Smørgrav errx(1, "%s: name too long", fname); 395f54cda14SDag-Erling Smørgrav } 3966718ba18SEd Schouten if ((dirbuf = strdup(fname)) == NULL || 3976718ba18SEd Schouten (basebuf = strdup(fname)) == NULL) 3986718ba18SEd Schouten err(1, "strdup"); 399f54cda14SDag-Erling Smørgrav len = snprintf(tmpfname, sizeof(tmpfname), 4006718ba18SEd Schouten "%s/.!%ld!%s", dirname(dirbuf), (long)getpid(), 4016718ba18SEd Schouten basename(basebuf)); 4026718ba18SEd Schouten free(dirbuf); 4036718ba18SEd Schouten free(basebuf); 404b4932d18SPedro F. Giffuni if ((size_t)len >= sizeof(tmpfname)) 405f54cda14SDag-Erling Smørgrav errx(1, "%s: name too long", fname); 406f54cda14SDag-Erling Smørgrav unlink(tmpfname); 407fc04dce0SPedro F. Giffuni if (outfile != NULL && outfile != stdout) 408fc04dce0SPedro F. Giffuni fclose(outfile); 40976c76d54SPedro F. Giffuni if ((outfile = fopen(tmpfname, "w")) == NULL) 41076c76d54SPedro F. Giffuni err(1, "%s", fname); 411f54cda14SDag-Erling Smørgrav fchown(fileno(outfile), sb.st_uid, sb.st_gid); 412f54cda14SDag-Erling Smørgrav fchmod(fileno(outfile), sb.st_mode & ALLPERMS); 413f54cda14SDag-Erling Smørgrav outfname = tmpfname; 414f6703c9cSYaroslav Tykhiy if (!ispan) { 415f6703c9cSYaroslav Tykhiy linenum = 0; 41626a5710cSYaroslav Tykhiy resetstate(); 417f6703c9cSYaroslav Tykhiy } 418f54cda14SDag-Erling Smørgrav } else { 419f54cda14SDag-Erling Smørgrav outfile = stdout; 420f54cda14SDag-Erling Smørgrav outfname = "stdout"; 421f54cda14SDag-Erling Smørgrav } 422f54cda14SDag-Erling Smørgrav if ((infile = fopen(fname, "r")) == NULL) { 4233af4dcb2STim J. Robbins warn("%s", fname); 4246689fb2bSTim J. Robbins rval = 1; 4253af4dcb2STim J. Robbins continue; 4263af4dcb2STim J. Robbins } 4279b50d902SRodney W. Grimes } 4289b50d902SRodney W. Grimes /* 429f54cda14SDag-Erling Smørgrav * We are here only when infile is open and we still have something 4308523e9a6STim J. Robbins * to read from it. 4318701af62SMaxim Sobolev * 4321a2a4fc8SPedro F. Giffuni * Use getline() so that we can handle essentially infinite input 4331a2a4fc8SPedro F. Giffuni * data. The p and plen are static so each invocation gives 4341a2a4fc8SPedro F. Giffuni * getline() the same buffer which is expanded as needed. 4359b50d902SRodney W. Grimes */ 4361a2a4fc8SPedro F. Giffuni len = getline(&p, &plen, infile); 4371a2a4fc8SPedro F. Giffuni if (len == -1) 4381a2a4fc8SPedro F. Giffuni err(1, "%s", fname); 43996d68291SJean-Sébastien Pédron if (len != 0 && p[len - 1] == '\n') { 44096d68291SJean-Sébastien Pédron sp->append_newline = 1; 441ed92199dSTim J. Robbins len--; 44296d68291SJean-Sébastien Pédron } else if (!lastline()) { 44396d68291SJean-Sébastien Pédron sp->append_newline = 1; 44496d68291SJean-Sébastien Pédron } else { 44596d68291SJean-Sébastien Pédron sp->append_newline = 0; 44696d68291SJean-Sébastien Pédron } 4479b50d902SRodney W. Grimes cspace(sp, p, len, spflag); 4489b50d902SRodney W. Grimes 4499b50d902SRodney W. Grimes linenum++; 4508701af62SMaxim Sobolev 4519b50d902SRodney W. Grimes return (1); 4529b50d902SRodney W. Grimes } 4539b50d902SRodney W. Grimes 4549b50d902SRodney W. Grimes /* 4559b50d902SRodney W. Grimes * Add a compilation unit to the linked list 4569b50d902SRodney W. Grimes */ 4579b50d902SRodney W. Grimes static void 458b4932d18SPedro F. Giffuni add_compunit(enum e_cut type, const char *s) 4599b50d902SRodney W. Grimes { 4609b50d902SRodney W. Grimes struct s_compunit *cu; 4619b50d902SRodney W. Grimes 4628e33c0a0SDavid E. O'Brien if ((cu = malloc(sizeof(struct s_compunit))) == NULL) 4638e33c0a0SDavid E. O'Brien err(1, "malloc"); 4649b50d902SRodney W. Grimes cu->type = type; 4659b50d902SRodney W. Grimes cu->s = s; 4669b50d902SRodney W. Grimes cu->next = NULL; 4679b50d902SRodney W. Grimes *cu_nextp = cu; 4689b50d902SRodney W. Grimes cu_nextp = &cu->next; 4699b50d902SRodney W. Grimes } 4709b50d902SRodney W. Grimes 4719b50d902SRodney W. Grimes /* 4729b50d902SRodney W. Grimes * Add a file to the linked list 4739b50d902SRodney W. Grimes */ 4749b50d902SRodney W. Grimes static void 475b4932d18SPedro F. Giffuni add_file(const char *s) 4769b50d902SRodney W. Grimes { 4779b50d902SRodney W. Grimes struct s_flist *fp; 4789b50d902SRodney W. Grimes 4798e33c0a0SDavid E. O'Brien if ((fp = malloc(sizeof(struct s_flist))) == NULL) 4808e33c0a0SDavid E. O'Brien err(1, "malloc"); 4819b50d902SRodney W. Grimes fp->next = NULL; 4829b50d902SRodney W. Grimes *fl_nextp = fp; 4839b50d902SRodney W. Grimes fp->fname = s; 4849b50d902SRodney W. Grimes fl_nextp = &fp->next; 4859b50d902SRodney W. Grimes } 486839af0c1SJuli Mallett 48796d68291SJean-Sébastien Pédron static int 488707c9cc5SPedro F. Giffuni next_files_have_lines(void) 48996d68291SJean-Sébastien Pédron { 49096d68291SJean-Sébastien Pédron struct s_flist *file; 49196d68291SJean-Sébastien Pédron FILE *file_fd; 49296d68291SJean-Sébastien Pédron int ch; 49396d68291SJean-Sébastien Pédron 49496d68291SJean-Sébastien Pédron file = files; 49596d68291SJean-Sébastien Pédron while ((file = file->next) != NULL) { 49696d68291SJean-Sébastien Pédron if ((file_fd = fopen(file->fname, "r")) == NULL) 49796d68291SJean-Sébastien Pédron continue; 49896d68291SJean-Sébastien Pédron 49996d68291SJean-Sébastien Pédron if ((ch = getc(file_fd)) != EOF) { 50096d68291SJean-Sébastien Pédron /* 50196d68291SJean-Sébastien Pédron * This next file has content, therefore current 50296d68291SJean-Sébastien Pédron * file doesn't contains the last line. 50396d68291SJean-Sébastien Pédron */ 50496d68291SJean-Sébastien Pédron ungetc(ch, file_fd); 50596d68291SJean-Sébastien Pédron fclose(file_fd); 50696d68291SJean-Sébastien Pédron return (1); 50796d68291SJean-Sébastien Pédron } 50896d68291SJean-Sébastien Pédron 50996d68291SJean-Sébastien Pédron fclose(file_fd); 51096d68291SJean-Sébastien Pédron } 51196d68291SJean-Sébastien Pédron 51296d68291SJean-Sébastien Pédron return (0); 51396d68291SJean-Sébastien Pédron } 51496d68291SJean-Sébastien Pédron 5158523e9a6STim J. Robbins int 5168523e9a6STim J. Robbins lastline(void) 5178523e9a6STim J. Robbins { 5188523e9a6STim J. Robbins int ch; 5198523e9a6STim J. Robbins 52096d68291SJean-Sébastien Pédron if (feof(infile)) { 52196d68291SJean-Sébastien Pédron return !( 52296d68291SJean-Sébastien Pédron (inplace == NULL || ispan) && 52396d68291SJean-Sébastien Pédron next_files_have_lines()); 52496d68291SJean-Sébastien Pédron } 52596d68291SJean-Sébastien Pédron if ((ch = getc(infile)) == EOF) { 52696d68291SJean-Sébastien Pédron return !( 52796d68291SJean-Sébastien Pédron (inplace == NULL || ispan) && 52896d68291SJean-Sébastien Pédron next_files_have_lines()); 52996d68291SJean-Sébastien Pédron } 530f54cda14SDag-Erling Smørgrav ungetc(ch, infile); 5318523e9a6STim J. Robbins return (0); 5328523e9a6STim J. Robbins } 533