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