2 * sed.c - very minimalist version of sed
4 * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
5 * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org>
6 * Copyright (C) 2002 Matt Kraai
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 Supported features and commands in this version of sed:
28 - address matching: num|/matchstr/[,num|/matchstr/|$]command
29 - commands: (p)rint, (d)elete, (s)ubstitue (with g & I flags)
30 - edit commands: (a)ppend, (i)nsert, (c)hange
31 - file commands: (r)ead
32 - backreferences in substitution expressions (\1, \2...\9)
33 - grouped commands: {cmd1;cmd2}
35 (Note: Specifying an address (range) to match is *optional*; commands
36 default to the whole pattern space if no specific address match was
41 - transliteration (y/source-chars/dest-chars/) (use 'tr')
42 - no pattern space hold space storing / swapping (x, etc.)
43 - no labels / branching (: label, b, t, and friends)
44 - and lots, lots more.
46 Reference http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html
50 #include <unistd.h> /* for getopt() */
52 #include <string.h> /* for strdup() */
54 #include <ctype.h> /* for isspace() */
58 /* the spec says label must be at least 8 chars, behavious is unspecified if more than 8 chars */
59 #define SED_LABEL_LENGTH 8
62 extern void xregcomp(regex_t *preg, const char *regex, int cflags);
63 extern int optind; /* in unistd.h */
64 extern char *optarg; /* ditto */
67 static int be_quiet = 0;
68 static const char bad_format_in_subst[] = "bad format in substitution expression";
70 typedef struct sed_cmd_s {
71 /* Order by alignment requirements */
74 regex_t *beg_match; /* sed -e '/match/cmd' */
75 regex_t *end_match; /* sed -e '/match/,/end_match/cmd' */
77 /* SUBSTITUTION COMMAND SPECIFIC FIELDS */
79 /* sed -e 's/sub_match/replace/' */
83 /* EDIT COMMAND (a,i,c) SPECIFIC FIELDS */
86 /* FILE COMMAND (r) SPECIFIC FIELDS */
90 int beg_line; /* 'sed 1p' 0 == no begining line, apply commands to all lines */
91 int end_line; /* 'sed 1,3p' 0 == no end line, use only beginning. -1 == $ */
92 /* SUBSTITUTION COMMAND SPECIFIC FIELDS */
94 unsigned int num_backrefs:4; /* how many back references (\1..\9) */
95 /* Note: GNU/POSIX sed does not save more than nine backrefs, so
96 * we only use 4 bits to hold the number */
97 unsigned int sub_g:1; /* sed -e 's/foo/bar/g' (global) */
98 unsigned int sub_p:2; /* sed -e 's/foo/bar/p' (print substitution) */
100 /* TRANSLATE COMMAND */
105 char cmd; /* p,d,s (add more at your leisure :-) */
108 int invert; /* the '!' after the address */
110 /* Branch commands */
111 char label[SED_LABEL_LENGTH + 1];
113 /* next command in list (sequential list of specified commands) */
114 struct sed_cmd_s *linear;
119 /* linked list of sed commands */
120 static sed_cmd_t sed_cmd_head;
121 static sed_cmd_t *sed_cmd_tail = &sed_cmd_head;
123 const char * const semicolon_whitespace = "; \n\r\t\v\0";
125 #ifdef CONFIG_FEATURE_CLEAN_UP
126 static void destroy_cmd_strs(void)
128 sed_cmd_t *sed_cmd = sed_cmd_head.linear;
131 sed_cmd_t *sed_cmd_next = sed_cmd->linear;
133 if (sed_cmd->beg_match) {
134 regfree(sed_cmd->beg_match);
135 free(sed_cmd->beg_match);
137 if (sed_cmd->end_match) {
138 regfree(sed_cmd->end_match);
139 free(sed_cmd->end_match);
141 if (sed_cmd->sub_match) {
142 regfree(sed_cmd->sub_match);
143 free(sed_cmd->sub_match);
145 free(sed_cmd->replace);
147 sed_cmd = sed_cmd_next;
153 * index_of_next_unescaped_regexp_delim - walks left to right through a string
154 * beginning at a specified index and returns the index of the next regular
155 * expression delimiter (typically a forward * slash ('/')) not preceeded by
158 static int index_of_next_unescaped_regexp_delim(const char delimiter, const char *str)
165 for ( ; (ch = str[idx]); idx++) {
167 if (ch == ']' && !(bracket == idx - 1 ||
168 (bracket == idx - 2 && str[idx-1] == '^')))
176 else if (ch == delimiter)
180 /* if we make it to here, we've hit the end of the string */
184 static int parse_regex_delim(const char *cmdstr, char **match, char **replace)
186 const char *cmdstr_ptr = cmdstr;
190 /* verify that the 's' is followed by something. That something
191 * (typically a 'slash') is now our regexp delimiter... */
193 bb_error_msg_and_die(bad_format_in_subst);
195 delimiter = *cmdstr_ptr;
199 /* save the match string */
200 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
202 bb_error_msg_and_die(bad_format_in_subst);
204 *match = bb_xstrndup(cmdstr_ptr, idx);
206 /* save the replacement string */
207 cmdstr_ptr += idx + 1;
208 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
210 bb_error_msg_and_die(bad_format_in_subst);
212 *replace = bb_xstrndup(cmdstr_ptr, idx);
214 return((cmdstr_ptr - cmdstr) + idx);
218 * returns the index in the string just past where the address ends.
220 static int get_address(char *my_str, int *linenum, regex_t **regex)
223 if (isdigit(my_str[idx])) {
225 *linenum = strtol(my_str, &endstr, 10);
226 /* endstr shouldnt ever equal NULL */
227 idx = endstr - my_str;
229 else if (my_str[idx] == '$') {
233 else if (my_str[idx] == '/' || my_str[idx] == '\\') {
238 if (my_str[idx] == '\\') {
240 delimiter = my_str[++idx];
243 idx += index_of_next_unescaped_regexp_delim(delimiter, my_str + idx);
245 bb_error_msg_and_die("unterminated match expression");
248 *regex = (regex_t *)xmalloc(sizeof(regex_t));
249 xregcomp(*regex, my_str+idx_start, REG_NEWLINE);
250 idx++; /* so it points to the next character after the last '/' */
255 static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr)
263 * the string that gets passed to this function should look like this:
264 * s/match/replace/gIp
268 * (all three of the '/' slashes are mandatory)
271 idx = parse_regex_delim(substr, &match, &sed_cmd->replace);
273 /* determine the number of back references in the match string */
274 /* Note: we compute this here rather than in the do_subst_command()
275 * function to save processor time, at the expense of a little more memory
276 * (4 bits) per sed_cmd */
278 /* sed_cmd->num_backrefs = 0; */ /* XXX: not needed? --apparently not */
279 for (j = 0; match[j]; j++) {
280 /* GNU/POSIX sed does not save more than nine backrefs */
281 if (match[j] == '\\' && match[j+1] == '(' && sed_cmd->num_backrefs <= 9)
282 sed_cmd->num_backrefs++;
285 /* process the flags */
286 while (substr[++idx]) {
287 switch (substr[idx]) {
291 /* Hmm, i dont see the I option mentioned in the standard */
299 /* any whitespace or semicolon trailing after a s/// is ok */
300 if (strchr(semicolon_whitespace, substr[idx]))
303 bb_error_msg_and_die("bad option in substitution expression");
308 /* compile the match string into a regex */
309 sed_cmd->sub_match = (regex_t *)xmalloc(sizeof(regex_t));
310 xregcomp(sed_cmd->sub_match, match, cflags);
316 static void replace_slash_n(char *string)
319 int remaining = strlen(string);
321 for (i = 0; string[i]; i++) {
322 if ((string[i] == '\\') && (string[i + 1] == 'n')) {
324 memmove(string + i + 1, string + i + 1, remaining - 1);
331 static int parse_translate_cmd(sed_cmd_t * const sed_cmd, const char *cmdstr)
338 idx = parse_regex_delim(cmdstr, &match, &replace);
339 replace_slash_n(match);
340 replace_slash_n(replace);
341 sed_cmd->translate = xcalloc(1, (strlen(match) + 1) * 2);
342 for (i = 0; (match[i] != 0) && (replace[i] != 0); i++) {
343 sed_cmd->translate[i * 2] = match[i];
344 sed_cmd->translate[(i * 2) + 1] = replace[i];
349 static int parse_edit_cmd(sed_cmd_t *sed_cmd, const char *editstr)
354 * the string that gets passed to this function should look like this:
358 * | this backslash (immediately following the edit command) is mandatory
365 * as soon as we hit a TEXT line that has no trailing '\', we're done.
366 * this means a command like:
374 if ((*editstr != '\\') || ((editstr[1] != '\n') && (editstr[1] != '\r'))) {
375 bb_error_msg_and_die("bad format in edit expression");
378 /* store the edit line text */
379 sed_cmd->editline = xmalloc(strlen(&editstr[2]) + 2);
380 for (i = 2, j = 0; editstr[i] != '\0' && strchr("\r\n", editstr[i]) == NULL;
382 if ((editstr[i] == '\\') && strchr("\n\r", editstr[i+1]) != NULL) {
383 sed_cmd->editline[j] = '\n';
386 sed_cmd->editline[j] = editstr[i];
389 /* figure out if we need to add a newline */
390 if (sed_cmd->editline[j-1] != '\n')
391 sed_cmd->editline[j++] = '\n';
393 /* terminate string */
394 sed_cmd->editline[j] = '\0';
400 static int parse_file_cmd(sed_cmd_t *sed_cmd, const char *filecmdstr)
406 * the string that gets passed to this function should look like this:
411 * optional whitespace
413 * re: the file to be read, the GNU manual says the following: "Note that
414 * if filename cannot be read, it is treated as if it were an empty file,
415 * without any error indication." Thus, all of the following commands are
418 * sed -e '1r noexist'
423 /* the file command may be followed by whitespace; move past it. */
424 while (isspace(filecmdstr[++idx]))
427 /* the first non-whitespace we get is a filename. the filename ends when we
428 * hit a normal sed command terminator or end of string */
429 filenamelen = strcspn(&filecmdstr[idx], semicolon_whitespace);
430 sed_cmd->filename = xmalloc(filenamelen + 1);
431 safe_strncpy(sed_cmd->filename, &filecmdstr[idx], filenamelen + 1);
433 return idx + filenamelen;
437 * Process the commands arguments
439 static char *parse_cmd_str(sed_cmd_t * const sed_cmd, char *cmdstr)
441 /* handle (s)ubstitution command */
442 if (sed_cmd->cmd == 's') {
443 cmdstr += parse_subst_cmd(sed_cmd, cmdstr);
445 /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */
446 else if (strchr("aic", sed_cmd->cmd)) {
447 if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
448 bb_error_msg_and_die("only a beginning address can be specified for edit commands");
449 cmdstr += parse_edit_cmd(sed_cmd, cmdstr);
451 /* handle file cmds: (r)ead */
452 else if (sed_cmd->cmd == 'r') {
453 if (sed_cmd->end_line || sed_cmd->end_match)
454 bb_error_msg_and_die("Command only uses one address");
455 cmdstr += parse_file_cmd(sed_cmd, cmdstr);
457 /* handle branch commands */
458 else if (strchr(":bt", sed_cmd->cmd)) {
461 cmdstr += strspn(cmdstr, " ");
462 length = strcspn(cmdstr, "; ");
463 if (length > SED_LABEL_LENGTH) {
464 length = SED_LABEL_LENGTH;
466 strncpy(sed_cmd->label, cmdstr, length);
469 /* translation command */
470 else if (sed_cmd->cmd == 'y') {
471 cmdstr += parse_translate_cmd(sed_cmd, cmdstr);
473 /* if it wasnt a single-letter command that takes no arguments
474 * then it must be an invalid command.
476 else if (strchr("nNpPqd=", sed_cmd->cmd) == 0) {
477 bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd);
480 /* give back whatever's left over */
484 static char *add_cmd(sed_cmd_t *sed_cmd, char *cmdstr)
486 /* Skip over leading whitespace and semicolons */
487 cmdstr += strspn(cmdstr, semicolon_whitespace);
489 /* if we ate the whole thing, that means there was just trailing
490 * whitespace or a final / no-op semicolon. either way, get out */
491 if (*cmdstr == '\0') {
495 /* if this is a comment, jump past it and keep going */
496 if (*cmdstr == '#') {
497 return(strpbrk(cmdstr, "\n\r"));
501 * format is: [addr][,addr]cmd
506 /* first part (if present) is an address: either a '$', a number or a /regex/ */
507 cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match);
509 /* second part (if present) will begin with a comma */
510 if (*cmdstr == ',') {
513 idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
515 bb_error_msg_and_die("get_address: no address found in string\n"
516 "\t(you probably didn't check the string you passed me)");
521 /* skip whitespace before the command */
522 while (isspace(*cmdstr)) {
526 /* there my be the inversion flag between part2 and part3 */
527 if (*cmdstr == '!') {
531 #ifdef SED_FEATURE_STRICT_CHECKING
532 /* According to the spec
533 * It is unspecified whether <blank>s can follow a '!' character,
534 * and conforming applications shall not follow a '!' character
537 if (isblank(cmdstr[idx]) {
538 bb_error_msg_and_die("blank follows '!'");
541 /* skip whitespace before the command */
542 while (isspace(*cmdstr)) {
548 /* last part (mandatory) will be a command */
550 bb_error_msg_and_die("missing command");
552 sed_cmd->cmd = *cmdstr;
555 if (sed_cmd->cmd == '{') {
557 sed_cmd_t *sed_cmd_new;
558 char *end_ptr = strpbrk(cmdstr, ";}");
561 sed_cmd_new = xcalloc(1, sizeof(sed_cmd_t));
562 sed_cmd_new->beg_match = sed_cmd->beg_match;
563 sed_cmd_new->end_match = sed_cmd->end_match;
564 sed_cmd_new->beg_line = sed_cmd->beg_line;
565 sed_cmd_new->end_line = sed_cmd->end_line;
566 sed_cmd_new->invert = sed_cmd->invert;
568 add_cmd(sed_cmd_new, cmdstr);
569 cmdstr = end_ptr + 1;
570 } while (*cmdstr != '\0');
572 cmdstr = parse_cmd_str(sed_cmd, cmdstr);
574 /* Add the command to the command array */
575 sed_cmd_tail->linear = sed_cmd;
576 sed_cmd_tail = sed_cmd_tail->linear;
581 static void add_cmd_str(char *cmdstr)
583 #ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
584 char *cmdstr_ptr = cmdstr;
586 /* HACK: convert "\n" to match tranlated '\n' string */
587 while((cmdstr_ptr = strstr(cmdstr_ptr, "\\n")) != NULL) {
588 cmdstr = xrealloc(cmdstr, strlen(cmdstr) + 2);
589 cmdstr_ptr = strstr(cmdstr, "\\n");
590 memmove(cmdstr_ptr + 1, cmdstr_ptr, strlen(cmdstr_ptr) + 1);
591 cmdstr_ptr[0] = '\\';
597 sed_cmd = xcalloc(1, sizeof(sed_cmd_t));
598 cmdstr = add_cmd(sed_cmd, cmdstr);
599 } while (cmdstr && strlen(cmdstr));
603 static void load_cmd_file(char *filename)
610 cmdfile = bb_xfopen(filename, "r");
612 while ((line = bb_get_line_from_file(cmdfile)) != NULL) {
613 /* if a line ends with '\' it needs the next line appended to it */
614 while (((e = last_char_is(line, '\n')) != NULL)
615 && (e > line) && (e[-1] == '\\')
616 && ((nextline = bb_get_line_from_file(cmdfile)) != NULL)) {
617 line = xrealloc(line, (e - line) + 1 + strlen(nextline) + 1);
618 strcat(line, nextline);
621 /* eat trailing newline (if any) --if I don't do this, edit commands
622 * (aic) will print an extra newline */
635 #define PIPE_MAGIC 0x7f
638 void pipe_putc(struct pipeline *const pipeline, char c)
640 if (pipeline->buf[pipeline->idx] == PIPE_MAGIC) {
642 xrealloc(pipeline->buf, pipeline->len + PIPE_GROW);
643 memset(pipeline->buf + pipeline->len, 0, PIPE_GROW);
644 pipeline->len += PIPE_GROW;
645 pipeline->buf[pipeline->len - 1] = PIPE_MAGIC;
647 pipeline->buf[pipeline->idx++] = (c);
650 #define pipeputc(c) pipe_putc(pipeline, c)
653 { if (pipeline[pipeline_idx] == PIPE_MAGIC) { \
654 pipeline = xrealloc(pipeline, pipeline_len+PIPE_GROW); \
655 memset(pipeline+pipeline_len, 0, PIPE_GROW); \
656 pipeline_len += PIPE_GROW; \
657 pipeline[pipeline_len-1] = PIPE_MAGIC; } \
658 pipeline[pipeline_idx++] = (c); }
661 static void print_subst_w_backrefs(const char *line, const char *replace,
662 regmatch_t *regmatch, struct pipeline *const pipeline, int matches)
666 /* go through the replacement string */
667 for (i = 0; replace[i]; i++) {
668 /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */
669 if (replace[i] == '\\' && isdigit(replace[i+1])) {
673 ++i; /* i now indexes the backref number, instead of the leading slash */
674 tmpstr[0] = replace[i];
676 backref = atoi(tmpstr);
677 /* print out the text held in regmatch[backref] */
678 if (backref <= matches && regmatch[backref].rm_so != -1)
679 for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++)
683 /* if we find a backslash escaped character, print the character */
684 else if (replace[i] == '\\') {
686 pipeputc(replace[i]);
689 /* if we find an unescaped '&' print out the whole matched text.
690 * fortunately, regmatch[0] contains the indicies to the whole matched
691 * expression (kinda seems like it was designed for just such a
693 else if (replace[i] == '&' && replace[i-1] != '\\') {
695 for (j = regmatch[0].rm_so; j < regmatch[0].rm_eo; j++)
698 /* nothing special, just print this char of the replacement string to stdout */
700 pipeputc(replace[i]);
704 static int do_subst_command(const sed_cmd_t *sed_cmd, char **line)
706 char *hackline = *line;
707 struct pipeline thepipe = { NULL, 0 , 0};
708 struct pipeline *const pipeline = &thepipe;
710 regmatch_t *regmatch = NULL;
712 /* we only proceed if the substitution 'search' expression matches */
713 if (regexec(sed_cmd->sub_match, hackline, 0, NULL, 0) == REG_NOMATCH)
716 /* whaddaya know, it matched. get the number of back references */
717 regmatch = xmalloc(sizeof(regmatch_t) * (sed_cmd->num_backrefs+1));
719 /* allocate more PIPE_GROW bytes
720 if replaced string is larger than original */
721 thepipe.len = strlen(hackline)+PIPE_GROW;
722 thepipe.buf = xcalloc(1, thepipe.len);
724 thepipe.buf[thepipe.len-1] = PIPE_MAGIC;
726 /* and now, as long as we've got a line to try matching and if we can match
727 * the search string, we make substitutions */
728 while ((*hackline || !altered) && (regexec(sed_cmd->sub_match, hackline,
729 sed_cmd->num_backrefs+1, regmatch, 0) != REG_NOMATCH) ) {
732 /* print everything before the match */
733 for (i = 0; i < regmatch[0].rm_so; i++)
734 pipeputc(hackline[i]);
736 /* then print the substitution string */
737 print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch,
738 pipeline, sed_cmd->num_backrefs);
740 /* advance past the match */
741 hackline += regmatch[0].rm_eo;
742 /* flag that something has changed */
745 /* if we're not doing this globally, get out now */
750 for (; *hackline; hackline++) pipeputc(*hackline);
751 if (thepipe.buf[thepipe.idx] == PIPE_MAGIC) thepipe.buf[thepipe.idx] = 0;
761 static sed_cmd_t *branch_to(const char *label)
764 for(sed_cmd = sed_cmd_head.linear; sed_cmd; sed_cmd = sed_cmd->linear) {
765 if (strcmp(sed_cmd->label, label) == 0) {
770 /* If no match returns last command */
774 static void process_file(FILE *file)
776 char *pattern_space; /* Posix requires it be able to hold at least 8192 bytes */
777 static int linenum = 0; /* GNU sed does not restart counting lines at EOF */
778 unsigned int still_in_range = 0;
782 pattern_space = bb_get_chomped_line_from_file(file);
783 if (pattern_space == NULL) {
787 /* go through every line in the file */
793 /* Read one line in advance so we can act on the last line, the '$' address */
794 next_line = bb_get_chomped_line_from_file(file);
800 /* for every line, go through all the commands */
801 for (sed_cmd = sed_cmd_head.linear; sed_cmd; sed_cmd = sed_cmd->linear) {
805 * entry point into sedding...
808 /* no range necessary */
809 (sed_cmd->beg_line == 0 && sed_cmd->end_line == 0 &&
810 sed_cmd->beg_match == NULL &&
811 sed_cmd->end_match == NULL) ||
812 /* this line number is the first address we're looking for */
813 (sed_cmd->beg_line && (sed_cmd->beg_line == linenum)) ||
814 /* this line matches our first address regex */
815 (sed_cmd->beg_match && (regexec(sed_cmd->beg_match, pattern_space, 0, NULL, 0) == 0)) ||
816 /* we are currently within the beginning & ending address range */
817 still_in_range || ((sed_cmd->beg_line == -1) && (next_line == NULL))
820 if (sed_cmd->invert ^ matched) {
825 switch (sed_cmd->cmd) {
827 printf("%d\n", linenum);
829 case 'P': { /* Write the current pattern space upto the first newline */
830 char *tmp = strchr(pattern_space, '\n');
835 case 'p': /* Write the current pattern space to output */
846 * Some special cases for 's' printing to make it compliant with
847 * GNU sed printing behavior (aka "The -n | s///p Matrix"):
849 * -n ONLY = never print anything regardless of any successful
852 * s///p ONLY = always print successful substitutions, even if
853 * the pattern_space is going to be printed anyway (pattern_space
854 * will be printed twice).
856 * -n AND s///p = print ONLY a successful substitution ONE TIME;
857 * no other lines are printed - this is the reason why the 'p'
858 * flag exists in the first place.
861 #ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
862 /* HACK: escape newlines twice so regex can match them */
865 while(strchr(pattern_space + offset, '\n') != NULL) {
867 pattern_space = xrealloc(pattern_space, strlen(pattern_space) + 2);
868 tmp = strchr(pattern_space + offset, '\n');
869 memmove(tmp + 1, tmp, strlen(tmp) + 1);
872 offset = tmp - pattern_space + 2;
876 /* we print the pattern_space once, unless we were told to be quiet */
877 substituted = do_subst_command(sed_cmd, &pattern_space);
879 #ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
880 /* undo HACK: escape newlines twice so regex can match them */
882 char *tmp = pattern_space;
884 while((tmp = strstr(tmp, "\\n")) != NULL) {
885 memmove(tmp, tmp + 1, strlen(tmp + 1) + 1);
890 altered |= substituted;
891 if (!be_quiet && altered && ((sed_cmd->linear == NULL) || (sed_cmd->linear->cmd != 's'))) {
895 /* we also print the line if we were given the 'p' flag
896 * (this is quite possibly the second printing) */
897 if ((sed_cmd->sub_p) && altered) {
903 fputs(sed_cmd->editline, stdout);
908 fputs(sed_cmd->editline, stdout);
912 /* single-address case */
913 if ((sed_cmd->end_match == NULL && sed_cmd->end_line == 0)
914 /* multi-address case */
915 /* - matching text */
916 || (sed_cmd->end_match && (regexec(sed_cmd->end_match, pattern_space, 0, NULL, 0) == 0))
917 /* - matching line numbers */
918 || (sed_cmd->end_line > 0 && sed_cmd->end_line == linenum))
920 fputs(sed_cmd->editline, stdout);
929 outfile = fopen(sed_cmd->filename, "r");
931 bb_xprint_and_close_file(outfile);
932 /* else if we couldn't open the output file,
933 * no biggie, just don't print anything */
937 case 'q': /* Branch to end of script and quit */
939 /* Exit the outer while loop */
943 case 'n': /* Read next line from input */
945 pattern_space = next_line;
946 next_line = bb_get_chomped_line_from_file(file);
949 case 'N': /* Append the next line to the current line */
951 pattern_space = realloc(pattern_space, strlen(pattern_space) + strlen(next_line) + 2);
952 strcat(pattern_space, "\n");
953 strcat(pattern_space, next_line);
954 next_line = bb_get_chomped_line_from_file(file);
959 sed_cmd = branch_to(sed_cmd->label);
963 sed_cmd = branch_to(sed_cmd->label);
968 for (i = 0; pattern_space[i] != 0; i++) {
970 for (j = 0; sed_cmd->translate[j] ;j += 2) {
971 if (pattern_space[i] == sed_cmd->translate[j]) {
972 pattern_space[i] = sed_cmd->translate[j + 1];
982 * exit point from sedding...
986 /* this is a single-address command or... */
987 (sed_cmd->end_line == 0 && sed_cmd->end_match == NULL) || (
988 /* we were in the middle of our address range (this
989 * isn't the first time through) and.. */
990 (still_in_range == 1) && (
991 /* this line number is the last address we're looking for or... */
992 (sed_cmd->end_line && (sed_cmd->end_line == linenum)) ||
993 /* this line matches our last address regex */
994 (sed_cmd->end_match && (regexec(sed_cmd->end_match, pattern_space, 0, NULL, 0) == 0))
998 /* we're out of our address range */
1002 /* didn't hit the exit? then we're still in the middle of an address range */
1012 /* we will print the line unless we were told to be quiet or if the
1013 * line was altered (via a 'd'elete or 's'ubstitution), in which case
1014 * the altered line was already printed */
1015 if ((!be_quiet && !altered) || force_print){
1016 puts(pattern_space);
1018 free(pattern_space);
1019 pattern_space = next_line;
1020 } while (pattern_space);
1023 extern int sed_main(int argc, char **argv)
1025 int opt, status = EXIT_SUCCESS;
1027 #ifdef CONFIG_FEATURE_CLEAN_UP
1028 /* destroy command strings on exit */
1029 if (atexit(destroy_cmd_strs) == -1)
1030 bb_perror_msg_and_die("atexit");
1033 /* do normal option parsing */
1034 while ((opt = getopt(argc, argv, "ne:f:")) > 0) {
1040 char *str_cmd = strdup(optarg);
1041 add_cmd_str(str_cmd);
1046 load_cmd_file(optarg);
1053 /* if we didn't get a pattern from a -e and no command file was specified,
1054 * argv[optind] should be the pattern. no pattern, no worky */
1055 if (sed_cmd_head.linear == NULL) {
1056 if (argv[optind] == NULL)
1059 char *str_cmd = strdup(argv[optind]);
1060 add_cmd_str(strdup(str_cmd));
1066 /* argv[(optind)..(argc-1)] should be names of file to process. If no
1067 * files were specified or '-' was specified, take input from stdin.
1068 * Otherwise, we process all the files specified. */
1069 if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) {
1070 process_file(stdin);
1075 for (i = optind; i < argc; i++) {
1076 file = bb_wfopen(argv[i], "r");
1081 status = EXIT_FAILURE;