*
* MAINTAINER: Rob Landley <rob@landley.net>
*
- * Licensed under GPL version 2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
*/
/* Code overview.
#include "libbb.h"
#include "xregex.h"
+enum {
+ OPT_in_place = 1 << 0,
+};
+
/* Each sed command turns into one of these structures. */
typedef struct sed_cmd_s {
/* Ordered by alignment requirements: currently 36 bytes on x86 */
if (matched) {
/* once matched, "n,xxx" range is dead, disabling it */
- if (sed_cmd->beg_line > 0)
+ if (sed_cmd->beg_line > 0
+ && !(option_mask32 & OPT_in_place) /* but not for -i */
+ ) {
sed_cmd->beg_line = -2;
+ }
sed_cmd->in_match = !(
/* has the ending line come, or is this a single address command? */
(sed_cmd->end_line ?
}
/* actual sedding */
+ //bb_error_msg("pattern_space:'%s' next_line:'%s' cmd:%c",
+ //pattern_space, next_line, sed_cmd->cmd);
switch (sed_cmd->cmd) {
/* Print line number */
{
int len;
/* If no next line, jump to end of script and exit. */
+ /* http://www.gnu.org/software/sed/manual/sed.html:
+ * "Most versions of sed exit without printing anything
+ * when the N command is issued on the last line of
+ * a file. GNU sed prints pattern space before exiting
+ * unless of course the -n command switch has been
+ * specified. This choice is by design."
+ */
if (next_line == NULL) {
- free(next_line);
- next_line = NULL;
- goto discard_line;
+ //goto discard_line;
+ goto discard_commands; /* GNU behavior */
}
/* Append next_line, read new next_line. */
len = strlen(pattern_space);
int sed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int sed_main(int argc UNUSED_PARAM, char **argv)
{
- enum {
- OPT_in_place = 1 << 0,
- };
unsigned opt;
llist_t *opt_e, *opt_f;
int status = EXIT_SUCCESS;
opt_e = opt_f = NULL;
opt_complementary = "e::f::" /* can occur multiple times */
"nn"; /* count -n */
+ /* -i must be first, to match OPT_in_place definition */
opt = getopt32(argv, "irne:f:", &opt_e, &opt_f,
&G.be_quiet); /* counter for -n */
//argc -= optind;
if (opt & OPT_in_place)
bb_error_msg_and_die(bb_msg_requires_arg, "-i");
add_input_file(stdin);
- process_files();
} else {
int i;
FILE *file;
free(G.outname);
G.outname = NULL;
}
- if (G.input_file_count > G.current_input_file)
- process_files();
+ /* Here, to handle "sed 'cmds' nonexistent_file" case we did:
+ * if (G.current_input_file >= G.input_file_count)
+ * return status;
+ * but it's not needed since process_files() works correctly
+ * in this case too. */
}
+ process_files();
return status;
}