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