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