*
* Licensed under GPLv2, see file LICENSE in this source tree.
*/
-
/* Code overview.
*
* Files are laid out to avoid unnecessary function declarations. So for
*
* sed_main() is where external code calls into this, with a command line.
*/
-
/* Supported features and commands in this version of sed:
*
* - comments ('#')
* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html
* http://sed.sourceforge.net/sedfaq3.html
*/
-
//config:config SED
//config: bool "sed (12 kb)"
//config: default y
//config: sed is used to perform text transformations on a file
//config: or input from a pipeline.
-//kbuild:lib-$(CONFIG_SED) += sed.o
-
//applet:IF_SED(APPLET(sed, BB_DIR_BIN, BB_SUID_DROP))
+//kbuild:lib-$(CONFIG_SED) += sed.o
+
//usage:#define sed_trivial_usage
//usage: "[-i[SFX]] [-nrE] [-f FILE]... [-e CMD]... [FILE]...\n"
//usage: "or: sed [-i[SFX]] [-nrE] CMD [FILE]..."
if (sed_cmd->sw_file)
fclose(sed_cmd->sw_file);
- if (sed_cmd->beg_match) {
- regfree(sed_cmd->beg_match);
- free(sed_cmd->beg_match);
- }
- if (sed_cmd->end_match) {
- regfree(sed_cmd->end_match);
- free(sed_cmd->end_match);
- }
- if (sed_cmd->sub_match) {
- regfree(sed_cmd->sub_match);
- free(sed_cmd->sub_match);
- }
+ /* Used to free regexps, but now there is code
+ * in get_address() which can reuse a regexp
+ * for constructs as /regexp/cmd1;//cmd2
+ * leading to double-frees here:
+ */
+ //if (sed_cmd->beg_match) {
+ // regfree(sed_cmd->beg_match);
+ // free(sed_cmd->beg_match);
+ //}
+ //if (sed_cmd->end_match) {
+ // regfree(sed_cmd->end_match);
+ // free(sed_cmd->end_match);
+ //}
+ //if (sed_cmd->sub_match) {
+ // regfree(sed_cmd->sub_match);
+ // free(sed_cmd->sub_match);
+ //}
free(sed_cmd->string);
free(sed_cmd);
sed_cmd = sed_cmd_next;
/* verify that the 's' or 'y' is followed by something. That something
* (typically a 'slash') is now our regexp delimiter... */
if (*cmdstr == '\0')
- bb_error_msg_and_die("bad format in substitution expression");
+ bb_simple_error_msg_and_die("bad format in substitution expression");
delimiter = *cmdstr_ptr++;
/* save the match string */
if (*my_str == '\\')
delimiter = *++pos;
next = index_of_next_unescaped_regexp_delim(delimiter, ++pos);
- temp = copy_parsing_escapes(pos, next);
- *regex = xzalloc(sizeof(regex_t));
- xregcomp(*regex, temp, G.regex_type);
- free(temp);
+ if (next != 0) {
+ temp = copy_parsing_escapes(pos, next);
+ G.previous_regex_ptr = *regex = xzalloc(sizeof(regex_t));
+ xregcomp(*regex, temp, G.regex_type);
+ free(temp);
+ } else {
+ *regex = G.previous_regex_ptr;
+ if (!G.previous_regex_ptr)
+ bb_simple_error_msg_and_die("no previous regexp");
+ }
/* Move position to next character after last delimiter */
pos += (next+1);
}
/* Grab a filename. Whitespace at start is skipped, then goes to EOL. */
static int parse_file_cmd(/*sed_cmd_t *sed_cmd,*/ const char *filecmdstr, char **retval)
{
- int start = 0, idx, hack = 0;
+ const char *start;
+ const char *eol;
/* Skip whitespace, then grab filename to end of line */
- while (isspace(filecmdstr[start]))
- start++;
- idx = start;
- while (filecmdstr[idx] && filecmdstr[idx] != '\n')
- idx++;
-
- /* If lines glued together, put backslash back. */
- if (filecmdstr[idx] == '\n')
- hack = 1;
- if (idx == start)
- bb_error_msg_and_die("empty filename");
- *retval = xstrndup(filecmdstr+start, idx-start+hack+1);
- if (hack)
- (*retval)[idx] = '\\';
+ start = skip_whitespace(filecmdstr);
+ eol = strchrnul(start, '\n');
+ if (eol == start)
+ bb_simple_error_msg_and_die("empty filename");
+
+ if (*eol) {
+ /* If lines glued together, put backslash back. */
+ *retval = xstrndup(start, eol-start + 1);
+ (*retval)[eol-start] = '\\';
+ } else {
+ /* eol is NUL */
+ *retval = xstrdup(start);
+ }
- return idx;
+ return eol - filecmdstr;
}
static int parse_subst_cmd(sed_cmd_t *sed_cmd, const char *substr)
goto out;
default:
dbg("s bad flags:'%s'", substr + idx);
- bb_error_msg_and_die("bad option in substitution expression");
+ bb_simple_error_msg_and_die("bad option in substitution expression");
}
}
out:
idx--; /* if 0, trigger error check below */
}
if (idx < 0)
- bb_error_msg_and_die("no address after comma");
+ bb_simple_error_msg_and_die("no address after comma");
sed_cmd->end_line_orig = sed_cmd->end_line;
}
/* last part (mandatory) will be a command */
if (!*cmdstr)
- bb_error_msg_and_die("missing command");
+ bb_simple_error_msg_and_die("missing command");
sed_cmd->cmd = *cmdstr++;
cmdstr = parse_cmd_args(sed_cmd, cmdstr);
if (!current_regex) {
current_regex = G.previous_regex_ptr;
if (!current_regex)
- bb_error_msg_and_die("no previous regexp");
+ bb_simple_error_msg_and_die("no previous regexp");
}
G.previous_regex_ptr = current_regex;
if (ferror(file)) {
xfunc_error_retval = 4; /* It's what gnu sed exits with... */
- bb_error_msg_and_die(bb_msg_write_error);
+ bb_simple_error_msg_and_die(bb_msg_write_error);
}
*last_puts_char = lpc;
}
static char *get_next_line(char *gets_char, char *last_puts_char)
{
char *temp = NULL;
- int len;
+ size_t len;
char gc;
flush_append(last_puts_char);
int old_matched, matched;
old_matched = sed_cmd->in_match;
+ if (!old_matched)
+ sed_cmd->end_line = sed_cmd->end_line_orig;
/* Determine if this command matches this line: */
}
sed_cmd = sed_cmd->next;
if (!sed_cmd)
- bb_error_msg_and_die("unterminated {");
+ bb_simple_error_msg_and_die("unterminated {");
}
}
continue;