binutils: allow static linking
[openadk.git] / package / toolbox / src / sed / main.c
blobafa10d4df393b39f9978ebbf8fa7cdd847527e7b
1 /* $OpenBSD: main.c,v 1.35 2017/08/01 18:05:53 martijn Exp $ */
3 /*-
4 * Copyright (c) 2016, 2017
5 * mirabilos <m@mirbsd.org>
6 * Copyright (c) 1992 Diomidis Spinellis.
7 * Copyright (c) 1992, 1993
8 * The Regents of the University of California. All rights reserved.
10 * This code is derived from software contributed to Berkeley by
11 * Diomidis Spinellis of Imperial College, University of London.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
38 #include <sys/types.h>
39 #include <sys/ioctl.h>
40 #include <sys/stat.h>
42 #include <ctype.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <limits.h>
46 #include <regex.h>
47 #include <stddef.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <libgen.h>
54 #include "defs.h"
55 #include "extern.h"
57 __RCSID("$MirOS: src/usr.bin/sed/main.c,v 1.3 2017/11/20 01:23:57 tg Exp $");
60 * Linked list of units (strings and files) to be compiled
62 struct s_compunit {
63 struct s_compunit *next;
64 enum e_cut {CU_FILE, CU_STRING} type;
65 char *s; /* Pointer to string or fname */
69 * Linked list pointer to compilation units and pointer to current
70 * next pointer.
72 static struct s_compunit *script, **cu_nextp = &script;
75 * Linked list of files to be processed
77 struct s_flist {
78 char *fname;
79 struct s_flist *next;
83 * Linked list pointer to files and pointer to current
84 * next pointer.
86 static struct s_flist *files, **fl_nextp = &files;
88 FILE *infile; /* Current input file */
89 FILE *outfile; /* Current output file */
91 int Eflag, aflag, eflag, nflag;
92 static int rval; /* Exit status */
95 * Current file and line number; line numbers restart across compilation
96 * units, but span across input files. The latter is optional if editing
97 * in place.
99 const char *fname; /* File name. */
100 const char *outfname; /* Output file name */
101 static char oldfname[PATH_MAX]; /* Old file name (for in-place editing) */
102 static char tmpfname[PATH_MAX]; /* Temporary file name (for in-place editing) */
103 const char *inplace; /* Inplace edit file extension */
104 u_long linenum;
106 static void add_compunit(enum e_cut, char *);
107 static void add_file(char *);
108 static int next_files_have_lines(void);
110 int termwidth;
112 int pledge_wpath, pledge_rpath;
115 main(int argc, char *argv[])
117 int c, fflag;
119 fflag = 0;
120 inplace = NULL;
121 while ((c = getopt(argc, argv, "Eae:f:i::nru")) != -1)
122 switch (c) {
123 case 'E':
124 case 'r':
125 Eflag = 1;
126 break;
127 case 'a':
128 aflag = 1;
129 break;
130 case 'e':
131 eflag = 1;
132 add_compunit(CU_STRING, optarg);
133 break;
134 case 'f':
135 fflag = 1;
136 add_compunit(CU_FILE, optarg);
137 break;
138 case 'i':
139 inplace = optarg ? optarg : "";
140 break;
141 case 'n':
142 nflag = 1;
143 break;
144 case 'u':
145 setvbuf(stdout, NULL, _IOLBF, 0);
146 break;
147 default:
148 case '?':
149 (void)fprintf(stderr,
150 "usage: sed [-aEnru] [-i[extension]] command [file ...]\n"
151 " sed [-aEnru] [-e command] [-f command_file] [-i[extension]] [file ...]\n");
152 exit(1);
154 argc -= optind;
155 argv += optind;
157 termwidth = 60;
159 #if defined(__OpenBSD__) && !defined(__MirBSD__)
160 if (inplace != NULL) {
161 if (pledge("stdio rpath wpath cpath fattr chown", NULL) == -1)
162 error(FATAL, "pledge: %s", strerror(errno));
163 } else {
164 if (pledge("stdio rpath wpath cpath", NULL) == -1)
165 error(FATAL, "pledge: %s", strerror(errno));
167 #endif
169 /* First usage case; script is the first arg */
170 if (!eflag && !fflag && *argv) {
171 add_compunit(CU_STRING, *argv);
172 argv++;
175 compile();
177 /* Continue with first and start second usage */
178 if (*argv) {
179 #if defined(__OpenBSD__) && !defined(__MirBSD__)
180 if (!pledge_wpath && inplace == NULL) {
181 if (pledge("stdio rpath", NULL) == -1)
182 error(FATAL, "pledge: %s", strerror(errno));
184 #endif
185 for (; *argv; argv++)
186 add_file(*argv);
187 } else {
188 #if defined(__OpenBSD__) && !defined(__MirBSD__)
189 if (!pledge_wpath && !pledge_rpath) {
190 if (pledge("stdio", NULL) == -1)
191 error(FATAL, "pledge: %s", strerror(errno));
192 } else if (pledge_rpath) {
193 if (pledge("stdio rpath", NULL) == -1)
194 error(FATAL, "pledge: %s", strerror(errno));
195 } else if (pledge_wpath) {
196 if (pledge("stdio wpath cpath", NULL) == -1)
197 error(FATAL, "pledge: %s", strerror(errno));
199 #endif
200 add_file(NULL);
202 process();
203 cfclose(prog, NULL);
204 if (fclose(stdout))
205 error(FATAL, "stdout: %s", strerror(errno));
206 exit (rval);
210 * Like fgets, but go through the chain of compilation units chaining them
211 * together. Empty strings and files are ignored.
213 char *
214 cu_fgets(char **outbuf, size_t *outsize)
216 static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
217 static FILE *f; /* Current open file */
218 static char *s; /* Current pointer inside string */
219 static char string_ident[30];
220 size_t len;
221 char *p;
223 if (*outbuf == NULL)
224 *outsize = 0;
226 again:
227 switch (state) {
228 case ST_EOF:
229 if (script == NULL)
230 goto cu_fgets_nilreturn;
231 linenum = 0;
232 switch (script->type) {
233 case CU_FILE:
234 if ((f = fopen(script->s, "r")) == NULL)
235 error(FATAL,
236 "%s: %s", script->s, strerror(errno));
237 fname = script->s;
238 state = ST_FILE;
239 goto again;
240 case CU_STRING:
241 if (((size_t)snprintf(string_ident,
242 sizeof(string_ident), "\"%s\"", script->s)) >=
243 sizeof(string_ident))
244 strlcpy(string_ident +
245 sizeof(string_ident) - 6, " ...\"", 5);
246 fname = string_ident;
247 s = script->s;
248 state = ST_STRING;
249 goto again;
251 case ST_FILE:
252 if ((p = fgetln(f, &len)) != NULL) {
253 linenum++;
254 if (len >= *outsize) {
255 free(*outbuf);
256 *outsize = ROUNDLEN(len + 1);
257 *outbuf = xmalloc(*outsize);
259 memcpy(*outbuf, p, len);
260 (*outbuf)[len] = '\0';
261 if (linenum == 1 && p[0] == '#' && p[1] == 'n')
262 nflag = 1;
263 return (*outbuf);
265 script = script->next;
266 (void)fclose(f);
267 state = ST_EOF;
268 goto again;
269 case ST_STRING:
270 if (linenum == 0 && s[0] == '#' && s[1] == 'n')
271 nflag = 1;
272 p = *outbuf;
273 len = *outsize;
274 for (;;) {
275 if (len <= 1) {
276 *outbuf = xrealloc(*outbuf,
277 *outsize + _POSIX2_LINE_MAX);
278 p = *outbuf + *outsize - len;
279 len += _POSIX2_LINE_MAX;
280 *outsize += _POSIX2_LINE_MAX;
282 switch (*s) {
283 case '\0':
284 state = ST_EOF;
285 if (s == script->s) {
286 script = script->next;
287 goto again;
288 } else {
289 script = script->next;
290 *p = '\0';
291 linenum++;
292 return (*outbuf);
294 case '\n':
295 *p++ = '\n';
296 *p = '\0';
297 s++;
298 linenum++;
299 return (*outbuf);
300 default:
301 *p++ = *s++;
302 len--;
306 /* NOTREACHED */
307 /* but GCC doesn't care, so: */
308 cu_fgets_nilreturn:
309 return (NULL);
313 * Like fgets, but go through the list of files chaining them together.
314 * Set len to the length of the line.
317 mf_fgets(SPACE *sp, enum e_spflag spflag)
319 struct stat sb;
320 size_t len;
321 char *p;
322 int c, fd;
323 static int firstfile;
325 if (infile == NULL) {
326 /* stdin? */
327 if (files->fname == NULL) {
328 if (inplace != NULL)
329 error(FATAL, "-i may not be used with stdin");
330 infile = stdin;
331 fname = "stdin";
332 outfile = stdout;
333 outfname = "stdout";
336 firstfile = 1;
339 for (;;) {
340 if (infile != NULL && (c = getc(infile)) != EOF) {
341 (void)ungetc(c, infile);
342 break;
344 /* If we are here then either eof or no files are open yet */
345 if (infile == stdin) {
346 sp->len = 0;
347 return (0);
349 if (infile != NULL) {
350 fclose(infile);
351 if (*oldfname != '\0') {
352 if (rename(fname, oldfname) != 0) {
353 warning("rename()");
354 unlink(tmpfname);
355 exit(1);
357 *oldfname = '\0';
359 if (*tmpfname != '\0') {
360 if (outfile != NULL && outfile != stdout)
361 fclose(outfile);
362 outfile = NULL;
363 rename(tmpfname, fname);
364 *tmpfname = '\0';
366 outfname = NULL;
368 if (firstfile == 0)
369 files = files->next;
370 else
371 firstfile = 0;
372 if (files == NULL) {
373 sp->len = 0;
374 return (0);
376 fname = files->fname;
377 if (inplace != NULL) {
378 char *tmpdirname;
380 if (lstat(fname, &sb) != 0)
381 error(FATAL, "%s: %s", fname,
382 strerror(errno ? errno : EIO));
383 if (!S_ISREG(sb.st_mode))
384 error(FATAL, "%s: %s %s", fname,
385 "in-place editing only",
386 "works for regular files");
387 if (*inplace != '\0') {
388 strlcpy(oldfname, fname,
389 sizeof(oldfname));
390 len = strlcat(oldfname, inplace,
391 sizeof(oldfname));
392 if (len > sizeof(oldfname))
393 error(FATAL, "%s: name too long", fname);
395 len = snprintf(tmpfname, sizeof(tmpfname), "%s/sedXXXXXXXXXX",
396 dirname(tmpdirname = strdup(fname)));
397 free(tmpdirname);
398 if (len >= sizeof(tmpfname))
399 error(FATAL, "%s: name too long", fname);
400 if ((fd = mkstemp(tmpfname)) == -1)
401 error(FATAL, "%s: %s", fname, strerror(errno));
402 if ((outfile = fdopen(fd, "w")) == NULL) {
403 unlink(tmpfname);
404 error(FATAL, "%s", fname);
406 fchown(fileno(outfile), sb.st_uid, sb.st_gid);
407 fchmod(fileno(outfile), sb.st_mode & ALLPERMS);
408 outfname = tmpfname;
409 linenum = 0;
410 resetranges();
411 } else {
412 outfile = stdout;
413 outfname = "stdout";
415 if ((infile = fopen(fname, "r")) == NULL) {
416 warning("%s", strerror(errno));
417 rval = 1;
418 continue;
423 * We are here only when infile is open and we still have something
424 * to read from it.
426 * Use fgetln so that we can handle essentially infinite input data.
427 * Can't use the pointer into the stdio buffer as the process space
428 * because the ungetc() can cause it to move.
430 p = fgetln(infile, &len);
431 if (ferror(infile))
432 error(FATAL, "%s: %s", fname, strerror(errno ? errno : EIO));
433 if (len != 0 && p[len - 1] == '\n') {
434 sp->append_newline = 1;
435 len--;
436 } else if (!lastline()) {
437 sp->append_newline = 1;
438 } else {
439 sp->append_newline = 0;
441 cspace(sp, p, len, spflag);
443 linenum++;
445 return (1);
449 * Add a compilation unit to the linked list
451 static void
452 add_compunit(enum e_cut type, char *s)
454 struct s_compunit *cu;
456 cu = xmalloc(sizeof(struct s_compunit));
457 cu->type = type;
458 cu->s = s;
459 cu->next = NULL;
460 *cu_nextp = cu;
461 cu_nextp = &cu->next;
465 * Add a file to the linked list
467 static void
468 add_file(char *s)
470 struct s_flist *fp;
472 fp = xmalloc(sizeof(struct s_flist));
473 fp->next = NULL;
474 *fl_nextp = fp;
475 fp->fname = s;
476 fl_nextp = &fp->next;
480 static int
481 next_files_have_lines(void)
483 struct s_flist *file;
484 FILE *file_fd;
485 int ch;
487 file = files;
488 while ((file = file->next) != NULL) {
489 if ((file_fd = fopen(file->fname, "r")) == NULL)
490 continue;
492 if ((ch = getc(file_fd)) != EOF) {
494 * This next file has content, therefore current
495 * file doesn't contains the last line.
497 ungetc(ch, file_fd);
498 fclose(file_fd);
499 return (1);
501 fclose(file_fd);
503 return (0);
507 lastline(void)
509 int ch;
511 if (feof(infile)) {
512 return !(
513 (inplace == NULL) &&
514 next_files_have_lines());
516 if ((ch = getc(infile)) == EOF) {
517 return !(
518 (inplace == NULL) &&
519 next_files_have_lines());
521 ungetc(ch, infile);
522 return (0);