aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2003-03-28 04:43:39 +0000
committerbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2003-03-28 04:43:39 +0000
commit65c8de04a3756b4ca48c78cb4444852822efc99c (patch)
tree65516cdaea81c7cbf67ea404d01a87a6615e6950
parent649196f6ab42c653796612e0bf412e4b0782748f (diff)
downloadbusybox-w32-65c8de04a3756b4ca48c78cb4444852822efc99c.tar.gz
busybox-w32-65c8de04a3756b4ca48c78cb4444852822efc99c.tar.bz2
busybox-w32-65c8de04a3756b4ca48c78cb4444852822efc99c.zip
Add basic branching support, sed ':' and 'b' commands
git-svn-id: svn://busybox.net/trunk/busybox@6762 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--editors/sed.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/editors/sed.c b/editors/sed.c
index 037d2a8ac..5fe435622 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -55,6 +55,9 @@
55#include <stdlib.h> 55#include <stdlib.h>
56#include "busybox.h" 56#include "busybox.h"
57 57
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
60
58/* externs */ 61/* externs */
59extern void xregcomp(regex_t *preg, const char *regex, int cflags); 62extern void xregcomp(regex_t *preg, const char *regex, int cflags);
60extern int optind; /* in unistd.h */ 63extern int optind; /* in unistd.h */
@@ -103,6 +106,9 @@ typedef struct sed_cmd_s {
103 /* inversion flag */ 106 /* inversion flag */
104 int invert; /* the '!' after the address */ 107 int invert; /* the '!' after the address */
105 108
109 /* Branch commands */
110 char label[SED_LABEL_LENGTH + 1];
111
106 /* next command in list (sequential list of specified commands) */ 112 /* next command in list (sequential list of specified commands) */
107 struct sed_cmd_s *linear; 113 struct sed_cmd_s *linear;
108 114
@@ -399,6 +405,18 @@ static char *parse_cmd_str(sed_cmd_t * const sed_cmd, char *cmdstr)
399 bb_error_msg_and_die("Command only uses one address"); 405 bb_error_msg_and_die("Command only uses one address");
400 cmdstr += parse_file_cmd(sed_cmd, cmdstr); 406 cmdstr += parse_file_cmd(sed_cmd, cmdstr);
401 } 407 }
408 /* handle branch commands */
409 else if (strchr(":b", sed_cmd->cmd)) {
410 int length;
411
412 cmdstr += strspn(cmdstr, " ");
413 length = strcspn(cmdstr, "; ");
414 if (length > SED_LABEL_LENGTH) {
415 length = SED_LABEL_LENGTH;
416 }
417 strncpy(sed_cmd->label, cmdstr, length);
418 cmdstr += length;
419 }
402 /* if it wasnt a single-letter command that takes no arguments 420 /* if it wasnt a single-letter command that takes no arguments
403 * then it must be an invalid command. 421 * then it must be an invalid command.
404 */ 422 */
@@ -677,6 +695,18 @@ static int do_subst_command(const sed_cmd_t *sed_cmd, char **line)
677 return altered; 695 return altered;
678} 696}
679 697
698static sed_cmd_t *branch_to(const char *label)
699{
700 sed_cmd_t *sed_cmd;
701 for(sed_cmd = sed_cmd_head.linear; sed_cmd; sed_cmd = sed_cmd->linear) {
702 if (strcmp(sed_cmd->label, label) == 0) {
703 break;
704 }
705 }
706
707 /* If no match returns last command */
708 return(sed_cmd);
709}
680 710
681static void process_file(FILE *file) 711static void process_file(FILE *file)
682{ 712{
@@ -832,6 +862,12 @@ static void process_file(FILE *file)
832 strcat(line, next_line); 862 strcat(line, next_line);
833 next_line = bb_get_chomped_line_from_file(file); 863 next_line = bb_get_chomped_line_from_file(file);
834 linenum++; 864 linenum++;
865 break;
866 case 'b':
867 sed_cmd = branch_to(sed_cmd->label);
868 break;
869// case ':':
870// break;
835 } 871 }
836 } 872 }
837 873