diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2003-04-07 16:04:14 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2003-04-07 16:04:14 +0000 |
commit | 7ce9e247f025af4ca217a89d914626dda24566d9 (patch) | |
tree | 3cded3628da03b9db42d4cf777d716dd3c635cbb | |
parent | c6adada1584df76a85ed08ba7bbfa9a8c3302f99 (diff) | |
download | busybox-w32-7ce9e247f025af4ca217a89d914626dda24566d9.tar.gz busybox-w32-7ce9e247f025af4ca217a89d914626dda24566d9.tar.bz2 busybox-w32-7ce9e247f025af4ca217a89d914626dda24566d9.zip |
Add hold space commands 'g', 'h', 'x'
-rw-r--r-- | editors/sed.c | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/editors/sed.c b/editors/sed.c index 834f06638..1673ee2d4 100644 --- a/editors/sed.c +++ b/editors/sed.c | |||
@@ -95,7 +95,7 @@ typedef struct sed_cmd_s { | |||
95 | /* Note: GNU/POSIX sed does not save more than nine backrefs, so | 95 | /* Note: GNU/POSIX sed does not save more than nine backrefs, so |
96 | * we only use 4 bits to hold the number */ | 96 | * we only use 4 bits to hold the number */ |
97 | unsigned int sub_g:1; /* sed -e 's/foo/bar/g' (global) */ | 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) */ | 98 | unsigned int sub_p:1; /* sed -e 's/foo/bar/p' (print substitution) */ |
99 | 99 | ||
100 | /* TRANSLATE COMMAND */ | 100 | /* TRANSLATE COMMAND */ |
101 | char *translate; | 101 | char *translate; |
@@ -267,7 +267,6 @@ static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr) | |||
267 | * | 267 | * |
268 | * (all three of the '/' slashes are mandatory) | 268 | * (all three of the '/' slashes are mandatory) |
269 | */ | 269 | */ |
270 | |||
271 | idx = parse_regex_delim(substr, &match, &sed_cmd->replace); | 270 | idx = parse_regex_delim(substr, &match, &sed_cmd->replace); |
272 | 271 | ||
273 | /* determine the number of back references in the match string */ | 272 | /* determine the number of back references in the match string */ |
@@ -473,7 +472,7 @@ static char *parse_cmd_str(sed_cmd_t * const sed_cmd, char *cmdstr) | |||
473 | /* if it wasnt a single-letter command that takes no arguments | 472 | /* if it wasnt a single-letter command that takes no arguments |
474 | * then it must be an invalid command. | 473 | * then it must be an invalid command. |
475 | */ | 474 | */ |
476 | else if (strchr("nNpPqd=", sed_cmd->cmd) == 0) { | 475 | else if (strchr("dghnNpPqx=", sed_cmd->cmd) == 0) { |
477 | bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd); | 476 | bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd); |
478 | } | 477 | } |
479 | 478 | ||
@@ -743,10 +742,10 @@ static int do_subst_command(const sed_cmd_t *sed_cmd, char **line) | |||
743 | altered++; | 742 | altered++; |
744 | 743 | ||
745 | /* if we're not doing this globally, get out now */ | 744 | /* if we're not doing this globally, get out now */ |
746 | if (!sed_cmd->sub_g) | 745 | if (!sed_cmd->sub_g) { |
747 | break; | 746 | break; |
747 | } | ||
748 | } | 748 | } |
749 | |||
750 | for (; *hackline; hackline++) pipeputc(*hackline); | 749 | for (; *hackline; hackline++) pipeputc(*hackline); |
751 | if (thepipe.buf[thepipe.idx] == PIPE_MAGIC) thepipe.buf[thepipe.idx] = 0; | 750 | if (thepipe.buf[thepipe.idx] == PIPE_MAGIC) thepipe.buf[thepipe.idx] = 0; |
752 | 751 | ||
@@ -774,6 +773,7 @@ static sed_cmd_t *branch_to(const char *label) | |||
774 | static void process_file(FILE *file) | 773 | static void process_file(FILE *file) |
775 | { | 774 | { |
776 | char *pattern_space; /* Posix requires it be able to hold at least 8192 bytes */ | 775 | char *pattern_space; /* Posix requires it be able to hold at least 8192 bytes */ |
776 | char *hold_space = NULL; /* 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 */ | 777 | static int linenum = 0; /* GNU sed does not restart counting lines at EOF */ |
778 | unsigned int still_in_range = 0; | 778 | unsigned int still_in_range = 0; |
779 | int altered; | 779 | int altered; |
@@ -975,6 +975,20 @@ static void process_file(FILE *file) | |||
975 | } | 975 | } |
976 | } | 976 | } |
977 | break; | 977 | break; |
978 | case 'g': /* Replace pattern space with hold space */ | ||
979 | free(pattern_space); | ||
980 | pattern_space = strdup(hold_space); | ||
981 | break; | ||
982 | case 'h': /* Replace hold space with pattern space */ | ||
983 | free(hold_space); | ||
984 | hold_space = strdup(pattern_space); | ||
985 | break; | ||
986 | case 'x': { /* Swap hold and pattern space */ | ||
987 | char *tmp; | ||
988 | tmp = pattern_space; | ||
989 | pattern_space = hold_space; | ||
990 | hold_space = tmp; | ||
991 | } | ||
978 | } | 992 | } |
979 | } | 993 | } |
980 | 994 | ||