diff options
| author | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2003-03-28 03:53:31 +0000 |
|---|---|---|
| committer | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2003-03-28 03:53:31 +0000 |
| commit | 706be371504b7be1ca4db0cd8bbeea42384706f2 (patch) | |
| tree | 57c1c30fb74f674552f42fd8bded6050dafb96b4 | |
| parent | 448db6218913e1eabe9cdc49e16c49b639b8458c (diff) | |
| download | busybox-w32-706be371504b7be1ca4db0cd8bbeea42384706f2.tar.gz busybox-w32-706be371504b7be1ca4db0cd8bbeea42384706f2.tar.bz2 busybox-w32-706be371504b7be1ca4db0cd8bbeea42384706f2.zip | |
Use a linked list for sed commands in preparation for branching support
git-svn-id: svn://busybox.net/trunk/busybox@6760 69ca8d6d-28ef-0310-b511-8ec308f3f277
| -rw-r--r-- | editors/sed.c | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/editors/sed.c b/editors/sed.c index 2ff028498..85eb9f68d 100644 --- a/editors/sed.c +++ b/editors/sed.c | |||
| @@ -101,14 +101,17 @@ typedef struct sed_cmd_s { | |||
| 101 | char cmd; /* p,d,s (add more at your leisure :-) */ | 101 | char cmd; /* p,d,s (add more at your leisure :-) */ |
| 102 | 102 | ||
| 103 | /* inversion flag */ | 103 | /* inversion flag */ |
| 104 | int invert; /* the '!' after the address */ | 104 | int invert; /* the '!' after the address */ |
| 105 | |||
| 106 | /* next command in list (sequential list of specified commands) */ | ||
| 107 | struct sed_cmd_s *linear; | ||
| 108 | |||
| 105 | } sed_cmd_t; | 109 | } sed_cmd_t; |
| 106 | 110 | ||
| 107 | /* globals */ | 111 | /* globals */ |
| 108 | static sed_cmd_t **sed_cmds = NULL; /* growable arrary holding a sequence of sed cmds */ | 112 | /* linked list of sed commands */ |
| 109 | static int ncmds = 0; /* number of sed commands */ | 113 | static sed_cmd_t sed_cmd_head; |
| 110 | 114 | static sed_cmd_t *sed_cmd_tail = &sed_cmd_head; | |
| 111 | /*static char *cur_file = NULL;*/ /* file currently being processed XXX: do I need this? */ | ||
| 112 | 115 | ||
| 113 | const char * const semicolon_whitespace = "; \n\r\t\v\0"; | 116 | const char * const semicolon_whitespace = "; \n\r\t\v\0"; |
| 114 | 117 | ||
| @@ -485,19 +488,26 @@ static char *add_cmd(sed_cmd_t *sed_cmd, char *cmdstr) | |||
| 485 | 488 | ||
| 486 | if (sed_cmd->cmd == '{') { | 489 | if (sed_cmd->cmd == '{') { |
| 487 | do { | 490 | do { |
| 491 | sed_cmd_t *sed_cmd_new; | ||
| 488 | char *end_ptr = strpbrk(cmdstr, ";}"); | 492 | char *end_ptr = strpbrk(cmdstr, ";}"); |
| 493 | |||
| 489 | *end_ptr = '\0'; | 494 | *end_ptr = '\0'; |
| 490 | add_cmd(sed_cmd, cmdstr); | 495 | sed_cmd_new = xcalloc(1, sizeof(sed_cmd_t)); |
| 496 | sed_cmd_new->beg_match = sed_cmd->beg_match; | ||
| 497 | sed_cmd_new->end_match = sed_cmd->end_match; | ||
| 498 | sed_cmd_new->beg_line = sed_cmd->beg_line; | ||
| 499 | sed_cmd_new->end_line = sed_cmd->end_line; | ||
| 500 | sed_cmd_new->invert = sed_cmd->invert; | ||
| 501 | |||
| 502 | add_cmd(sed_cmd_new, cmdstr); | ||
| 491 | cmdstr = end_ptr + 1; | 503 | cmdstr = end_ptr + 1; |
| 492 | } while (*cmdstr != '\0'); | 504 | } while (*cmdstr != '\0'); |
| 493 | } else { | 505 | } else { |
| 494 | |||
| 495 | cmdstr = parse_cmd_str(sed_cmd, cmdstr); | 506 | cmdstr = parse_cmd_str(sed_cmd, cmdstr); |
| 496 | 507 | ||
| 497 | /* Add the command to the command array */ | 508 | /* Add the command to the command array */ |
| 498 | sed_cmds = xrealloc(sed_cmds, sizeof(sed_cmd_t) * (++ncmds)); | 509 | sed_cmd_tail->linear = sed_cmd; |
| 499 | sed_cmds[ncmds-1] = xmalloc(sizeof(sed_cmd_t)); | 510 | sed_cmd_tail = sed_cmd_tail->linear; |
| 500 | memcpy(sed_cmds[ncmds-1], sed_cmd, sizeof(sed_cmd_t)); | ||
| 501 | } | 511 | } |
| 502 | return(cmdstr); | 512 | return(cmdstr); |
| 503 | } | 513 | } |
| @@ -677,7 +687,6 @@ static void process_file(FILE *file) | |||
| 677 | static int linenum = 0; /* GNU sed does not restart counting lines at EOF */ | 687 | static int linenum = 0; /* GNU sed does not restart counting lines at EOF */ |
| 678 | unsigned int still_in_range = 0; | 688 | unsigned int still_in_range = 0; |
| 679 | int altered; | 689 | int altered; |
| 680 | int i; | ||
| 681 | 690 | ||
| 682 | line = bb_get_chomped_line_from_file(file); | 691 | line = bb_get_chomped_line_from_file(file); |
| 683 | if (line == NULL) { | 692 | if (line == NULL) { |
| @@ -687,6 +696,7 @@ static void process_file(FILE *file) | |||
| 687 | /* go through every line in the file */ | 696 | /* go through every line in the file */ |
| 688 | do { | 697 | do { |
| 689 | char *next_line; | 698 | char *next_line; |
| 699 | sed_cmd_t *sed_cmd; | ||
| 690 | 700 | ||
| 691 | /* Read one line in advance so we can act on the last line, the '$' address */ | 701 | /* Read one line in advance so we can act on the last line, the '$' address */ |
| 692 | next_line = bb_get_chomped_line_from_file(file); | 702 | next_line = bb_get_chomped_line_from_file(file); |
| @@ -695,8 +705,7 @@ static void process_file(FILE *file) | |||
| 695 | altered = 0; | 705 | altered = 0; |
| 696 | 706 | ||
| 697 | /* for every line, go through all the commands */ | 707 | /* for every line, go through all the commands */ |
| 698 | for (i = 0; i < ncmds; i++) { | 708 | for (sed_cmd = sed_cmd_head.linear; sed_cmd; sed_cmd = sed_cmd->linear) { |
| 699 | sed_cmd_t *sed_cmd = sed_cmds[i]; | ||
| 700 | int deleted = 0; | 709 | int deleted = 0; |
| 701 | 710 | ||
| 702 | /* | 711 | /* |
| @@ -770,7 +779,7 @@ static void process_file(FILE *file) | |||
| 770 | * (this is quite possibly the second printing) */ | 779 | * (this is quite possibly the second printing) */ |
| 771 | if (sed_cmd->sub_p) | 780 | if (sed_cmd->sub_p) |
| 772 | altered |= do_subst_command(sed_cmd, &line); | 781 | altered |= do_subst_command(sed_cmd, &line); |
| 773 | if (altered && (i+1 >= ncmds || sed_cmds[i+1]->cmd != 's')) | 782 | if (altered && ((sed_cmd->linear == NULL) || (sed_cmd->linear->cmd != 's'))) |
| 774 | puts(line); | 783 | puts(line); |
| 775 | 784 | ||
| 776 | break; | 785 | break; |
| @@ -900,7 +909,7 @@ extern int sed_main(int argc, char **argv) | |||
| 900 | 909 | ||
| 901 | /* if we didn't get a pattern from a -e and no command file was specified, | 910 | /* if we didn't get a pattern from a -e and no command file was specified, |
| 902 | * argv[optind] should be the pattern. no pattern, no worky */ | 911 | * argv[optind] should be the pattern. no pattern, no worky */ |
| 903 | if (ncmds == 0) { | 912 | if (sed_cmd_head.linear == NULL) { |
| 904 | if (argv[optind] == NULL) | 913 | if (argv[optind] == NULL) |
| 905 | bb_show_usage(); | 914 | bb_show_usage(); |
| 906 | else { | 915 | else { |
