diff options
author | Manuel Novoa III <mjn3@codepoet.org> | 2003-03-19 09:13:01 +0000 |
---|---|---|
committer | Manuel Novoa III <mjn3@codepoet.org> | 2003-03-19 09:13:01 +0000 |
commit | cad5364599eb5062d59e0c397ed638ddd61a8d5d (patch) | |
tree | a318d0f03aa076c74b576ea45dc543a5669e8e91 /editors/sed.c | |
parent | e01f9662a5bd5d91be4f6b3941b57fff73cd5af1 (diff) | |
download | busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.tar.gz busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.tar.bz2 busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.zip |
Major coreutils update.
Diffstat (limited to 'editors/sed.c')
-rw-r--r-- | editors/sed.c | 62 |
1 files changed, 30 insertions, 32 deletions
diff --git a/editors/sed.c b/editors/sed.c index 8bd627a9c..2ff028498 100644 --- a/editors/sed.c +++ b/editors/sed.c | |||
@@ -200,7 +200,7 @@ static int get_address(char *delimiter, char *my_str, int *linenum, regex_t **re | |||
200 | } | 200 | } |
201 | idx = index_of_next_unescaped_regexp_delim(*delimiter, my_str, ++idx); | 201 | idx = index_of_next_unescaped_regexp_delim(*delimiter, my_str, ++idx); |
202 | if (idx == -1) { | 202 | if (idx == -1) { |
203 | error_msg_and_die("unterminated match expression"); | 203 | bb_error_msg_and_die("unterminated match expression"); |
204 | } | 204 | } |
205 | my_str[idx] = '\0'; | 205 | my_str[idx] = '\0'; |
206 | *regex = (regex_t *)xmalloc(sizeof(regex_t)); | 206 | *regex = (regex_t *)xmalloc(sizeof(regex_t)); |
@@ -230,7 +230,7 @@ static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr) | |||
230 | /* verify that the 's' is followed by something. That something | 230 | /* verify that the 's' is followed by something. That something |
231 | * (typically a 'slash') is now our regexp delimiter... */ | 231 | * (typically a 'slash') is now our regexp delimiter... */ |
232 | if (substr[idx] == '\0') | 232 | if (substr[idx] == '\0') |
233 | error_msg_and_die("bad format in substitution expression"); | 233 | bb_error_msg_and_die("bad format in substitution expression"); |
234 | else | 234 | else |
235 | sed_cmd->delimiter=substr[idx]; | 235 | sed_cmd->delimiter=substr[idx]; |
236 | 236 | ||
@@ -238,8 +238,8 @@ static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr) | |||
238 | oldidx = idx+1; | 238 | oldidx = idx+1; |
239 | idx = index_of_next_unescaped_regexp_delim(sed_cmd->delimiter, substr, ++idx); | 239 | idx = index_of_next_unescaped_regexp_delim(sed_cmd->delimiter, substr, ++idx); |
240 | if (idx == -1) | 240 | if (idx == -1) |
241 | error_msg_and_die("bad format in substitution expression"); | 241 | bb_error_msg_and_die("bad format in substitution expression"); |
242 | match = xstrndup(substr + oldidx, idx - oldidx); | 242 | match = bb_xstrndup(substr + oldidx, idx - oldidx); |
243 | 243 | ||
244 | /* determine the number of back references in the match string */ | 244 | /* determine the number of back references in the match string */ |
245 | /* Note: we compute this here rather than in the do_subst_command() | 245 | /* Note: we compute this here rather than in the do_subst_command() |
@@ -257,8 +257,8 @@ static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr) | |||
257 | oldidx = idx+1; | 257 | oldidx = idx+1; |
258 | idx = index_of_next_unescaped_regexp_delim(sed_cmd->delimiter, substr, ++idx); | 258 | idx = index_of_next_unescaped_regexp_delim(sed_cmd->delimiter, substr, ++idx); |
259 | if (idx == -1) | 259 | if (idx == -1) |
260 | error_msg_and_die("bad format in substitution expression"); | 260 | bb_error_msg_and_die("bad format in substitution expression"); |
261 | sed_cmd->replace = xstrndup(substr + oldidx, idx - oldidx); | 261 | sed_cmd->replace = bb_xstrndup(substr + oldidx, idx - oldidx); |
262 | 262 | ||
263 | /* process the flags */ | 263 | /* process the flags */ |
264 | while (substr[++idx]) { | 264 | while (substr[++idx]) { |
@@ -278,7 +278,7 @@ static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr) | |||
278 | if (strchr(semicolon_whitespace, substr[idx])) | 278 | if (strchr(semicolon_whitespace, substr[idx])) |
279 | goto out; | 279 | goto out; |
280 | /* else */ | 280 | /* else */ |
281 | error_msg_and_die("bad option in substitution expression"); | 281 | bb_error_msg_and_die("bad option in substitution expression"); |
282 | } | 282 | } |
283 | } | 283 | } |
284 | 284 | ||
@@ -317,7 +317,7 @@ static int parse_edit_cmd(sed_cmd_t *sed_cmd, const char *editstr) | |||
317 | * | 317 | * |
318 | */ | 318 | */ |
319 | if ((*editstr != '\\') || ((editstr[1] != '\n') && (editstr[1] != '\r'))) { | 319 | if ((*editstr != '\\') || ((editstr[1] != '\n') && (editstr[1] != '\r'))) { |
320 | error_msg_and_die("bad format in edit expression"); | 320 | bb_error_msg_and_die("bad format in edit expression"); |
321 | } | 321 | } |
322 | 322 | ||
323 | /* store the edit line text */ | 323 | /* store the edit line text */ |
@@ -390,20 +390,20 @@ static char *parse_cmd_str(sed_cmd_t * const sed_cmd, char *cmdstr) | |||
390 | /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */ | 390 | /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */ |
391 | else if (strchr("aic", sed_cmd->cmd)) { | 391 | else if (strchr("aic", sed_cmd->cmd)) { |
392 | if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c') | 392 | if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c') |
393 | error_msg_and_die("only a beginning address can be specified for edit commands"); | 393 | bb_error_msg_and_die("only a beginning address can be specified for edit commands"); |
394 | cmdstr += parse_edit_cmd(sed_cmd, cmdstr); | 394 | cmdstr += parse_edit_cmd(sed_cmd, cmdstr); |
395 | } | 395 | } |
396 | /* handle file cmds: (r)ead */ | 396 | /* handle file cmds: (r)ead */ |
397 | else if (sed_cmd->cmd == 'r') { | 397 | else if (sed_cmd->cmd == 'r') { |
398 | if (sed_cmd->end_line || sed_cmd->end_match) | 398 | if (sed_cmd->end_line || sed_cmd->end_match) |
399 | error_msg_and_die("Command only uses one address"); | 399 | bb_error_msg_and_die("Command only uses one address"); |
400 | cmdstr += parse_file_cmd(sed_cmd, cmdstr); | 400 | cmdstr += parse_file_cmd(sed_cmd, cmdstr); |
401 | } | 401 | } |
402 | /* if it wasnt a single-letter command that takes no arguments | 402 | /* if it wasnt a single-letter command that takes no arguments |
403 | * then it must be an invalid command. | 403 | * then it must be an invalid command. |
404 | */ | 404 | */ |
405 | else if (strchr("nNpPqd=", sed_cmd->cmd) == 0) { | 405 | else if (strchr("nNpPqd=", sed_cmd->cmd) == 0) { |
406 | error_msg_and_die("Unsupported command %c", sed_cmd->cmd); | 406 | bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd); |
407 | } | 407 | } |
408 | 408 | ||
409 | /* give back whatever's left over */ | 409 | /* give back whatever's left over */ |
@@ -442,7 +442,7 @@ static char *add_cmd(sed_cmd_t *sed_cmd, char *cmdstr) | |||
442 | cmdstr++; | 442 | cmdstr++; |
443 | idx = get_address(&(sed_cmd->delimiter), cmdstr, &sed_cmd->end_line, &sed_cmd->end_match); | 443 | idx = get_address(&(sed_cmd->delimiter), cmdstr, &sed_cmd->end_line, &sed_cmd->end_match); |
444 | if (idx == 0) { | 444 | if (idx == 0) { |
445 | error_msg_and_die("get_address: no address found in string\n" | 445 | bb_error_msg_and_die("get_address: no address found in string\n" |
446 | "\t(you probably didn't check the string you passed me)"); | 446 | "\t(you probably didn't check the string you passed me)"); |
447 | } | 447 | } |
448 | cmdstr += idx; | 448 | cmdstr += idx; |
@@ -465,7 +465,7 @@ static char *add_cmd(sed_cmd_t *sed_cmd, char *cmdstr) | |||
465 | * with <blank>s. | 465 | * with <blank>s. |
466 | */ | 466 | */ |
467 | if (isblank(cmdstr[idx]) { | 467 | if (isblank(cmdstr[idx]) { |
468 | error_msg_and_die("blank follows '!'"); | 468 | bb_error_msg_and_die("blank follows '!'"); |
469 | } | 469 | } |
470 | #else | 470 | #else |
471 | /* skip whitespace before the command */ | 471 | /* skip whitespace before the command */ |
@@ -478,7 +478,7 @@ static char *add_cmd(sed_cmd_t *sed_cmd, char *cmdstr) | |||
478 | 478 | ||
479 | /* last part (mandatory) will be a command */ | 479 | /* last part (mandatory) will be a command */ |
480 | if (*cmdstr == '\0') | 480 | if (*cmdstr == '\0') |
481 | error_msg_and_die("missing command"); | 481 | bb_error_msg_and_die("missing command"); |
482 | 482 | ||
483 | sed_cmd->cmd = *cmdstr; | 483 | sed_cmd->cmd = *cmdstr; |
484 | cmdstr++; | 484 | cmdstr++; |
@@ -517,14 +517,16 @@ static void load_cmd_file(char *filename) | |||
517 | FILE *cmdfile; | 517 | FILE *cmdfile; |
518 | char *line; | 518 | char *line; |
519 | char *nextline; | 519 | char *nextline; |
520 | char *e; | ||
520 | 521 | ||
521 | cmdfile = xfopen(filename, "r"); | 522 | cmdfile = bb_xfopen(filename, "r"); |
522 | 523 | ||
523 | while ((line = get_line_from_file(cmdfile)) != NULL) { | 524 | while ((line = bb_get_line_from_file(cmdfile)) != NULL) { |
524 | /* if a line ends with '\' it needs the next line appended to it */ | 525 | /* if a line ends with '\' it needs the next line appended to it */ |
525 | while (line[strlen(line)-2] == '\\' && | 526 | while (((e = last_char_is(line, '\n')) != NULL) |
526 | (nextline = get_line_from_file(cmdfile)) != NULL) { | 527 | && (e > line) && (e[-1] == '\\') |
527 | line = xrealloc(line, strlen(line) + strlen(nextline) + 1); | 528 | && ((nextline = bb_get_line_from_file(cmdfile)) != NULL)) { |
529 | line = xrealloc(line, (e - line) + 1 + strlen(nextline) + 1); | ||
528 | strcat(line, nextline); | 530 | strcat(line, nextline); |
529 | free(nextline); | 531 | free(nextline); |
530 | } | 532 | } |
@@ -677,20 +679,18 @@ static void process_file(FILE *file) | |||
677 | int altered; | 679 | int altered; |
678 | int i; | 680 | int i; |
679 | 681 | ||
680 | line = get_line_from_file(file); | 682 | line = bb_get_chomped_line_from_file(file); |
681 | if (line == NULL) { | 683 | if (line == NULL) { |
682 | return; | 684 | return; |
683 | } | 685 | } |
684 | chomp(line); | ||
685 | 686 | ||
686 | /* go through every line in the file */ | 687 | /* go through every line in the file */ |
687 | do { | 688 | do { |
688 | char *next_line; | 689 | char *next_line; |
689 | 690 | ||
690 | /* Read one line in advance so we can act on the last line, the '$' address */ | 691 | /* Read one line in advance so we can act on the last line, the '$' address */ |
691 | next_line = get_line_from_file(file); | 692 | next_line = bb_get_chomped_line_from_file(file); |
692 | 693 | ||
693 | chomp(next_line); | ||
694 | linenum++; | 694 | linenum++; |
695 | altered = 0; | 695 | altered = 0; |
696 | 696 | ||
@@ -805,7 +805,7 @@ static void process_file(FILE *file) | |||
805 | puts(line); | 805 | puts(line); |
806 | outfile = fopen(sed_cmd->filename, "r"); | 806 | outfile = fopen(sed_cmd->filename, "r"); |
807 | if (outfile) | 807 | if (outfile) |
808 | print_file(outfile); | 808 | bb_xprint_and_close_file(outfile); |
809 | /* else if we couldn't open the output file, | 809 | /* else if we couldn't open the output file, |
810 | * no biggie, just don't print anything */ | 810 | * no biggie, just don't print anything */ |
811 | altered++; | 811 | altered++; |
@@ -817,16 +817,14 @@ static void process_file(FILE *file) | |||
817 | case 'n': /* Read next line from input */ | 817 | case 'n': /* Read next line from input */ |
818 | free(line); | 818 | free(line); |
819 | line = next_line; | 819 | line = next_line; |
820 | next_line = get_line_from_file(file); | 820 | next_line = bb_get_chomped_line_from_file(file); |
821 | chomp(next_line); | ||
822 | linenum++; | 821 | linenum++; |
823 | break; | 822 | break; |
824 | case 'N': /* Append the next line to the current line */ | 823 | case 'N': /* Append the next line to the current line */ |
825 | line = realloc(line, strlen(line) + strlen(next_line) + 2); | 824 | line = realloc(line, strlen(line) + strlen(next_line) + 2); |
826 | strcat(line, "\n"); | 825 | strcat(line, "\n"); |
827 | strcat(line, next_line); | 826 | strcat(line, next_line); |
828 | next_line = get_line_from_file(file); | 827 | next_line = bb_get_chomped_line_from_file(file); |
829 | chomp(next_line); | ||
830 | linenum++; | 828 | linenum++; |
831 | } | 829 | } |
832 | } | 830 | } |
@@ -880,7 +878,7 @@ extern int sed_main(int argc, char **argv) | |||
880 | #ifdef CONFIG_FEATURE_CLEAN_UP | 878 | #ifdef CONFIG_FEATURE_CLEAN_UP |
881 | /* destroy command strings on exit */ | 879 | /* destroy command strings on exit */ |
882 | if (atexit(destroy_cmd_strs) == -1) | 880 | if (atexit(destroy_cmd_strs) == -1) |
883 | perror_msg_and_die("atexit"); | 881 | bb_perror_msg_and_die("atexit"); |
884 | #endif | 882 | #endif |
885 | 883 | ||
886 | /* do normal option parsing */ | 884 | /* do normal option parsing */ |
@@ -896,7 +894,7 @@ extern int sed_main(int argc, char **argv) | |||
896 | load_cmd_file(optarg); | 894 | load_cmd_file(optarg); |
897 | break; | 895 | break; |
898 | default: | 896 | default: |
899 | show_usage(); | 897 | bb_show_usage(); |
900 | } | 898 | } |
901 | } | 899 | } |
902 | 900 | ||
@@ -904,7 +902,7 @@ extern int sed_main(int argc, char **argv) | |||
904 | * argv[optind] should be the pattern. no pattern, no worky */ | 902 | * argv[optind] should be the pattern. no pattern, no worky */ |
905 | if (ncmds == 0) { | 903 | if (ncmds == 0) { |
906 | if (argv[optind] == NULL) | 904 | if (argv[optind] == NULL) |
907 | show_usage(); | 905 | bb_show_usage(); |
908 | else { | 906 | else { |
909 | add_cmd_str(argv[optind]); | 907 | add_cmd_str(argv[optind]); |
910 | optind++; | 908 | optind++; |
@@ -921,7 +919,7 @@ extern int sed_main(int argc, char **argv) | |||
921 | int i; | 919 | int i; |
922 | FILE *file; | 920 | FILE *file; |
923 | for (i = optind; i < argc; i++) { | 921 | for (i = optind; i < argc; i++) { |
924 | file = wfopen(argv[i], "r"); | 922 | file = bb_wfopen(argv[i], "r"); |
925 | if (file) { | 923 | if (file) { |
926 | process_file(file); | 924 | process_file(file); |
927 | fclose(file); | 925 | fclose(file); |