diff options
Diffstat (limited to 'editors')
-rw-r--r-- | editors/sed.c | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/editors/sed.c b/editors/sed.c index dcc2ca06b..b08eae4dc 100644 --- a/editors/sed.c +++ b/editors/sed.c | |||
@@ -8,6 +8,8 @@ | |||
8 | * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au> | 8 | * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au> |
9 | * Copyright (C) 2003,2004 by Rob Landley <rob@landley.net> | 9 | * Copyright (C) 2003,2004 by Rob Landley <rob@landley.net> |
10 | * | 10 | * |
11 | * MAINTAINER: Rob Landley <rob@landley.net> | ||
12 | * | ||
11 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | 13 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
12 | */ | 14 | */ |
13 | 15 | ||
@@ -74,8 +76,8 @@ typedef struct sed_cmd_s { | |||
74 | regex_t *sub_match; /* For 's/sub_match/string/' */ | 76 | regex_t *sub_match; /* For 's/sub_match/string/' */ |
75 | int beg_line; /* 'sed 1p' 0 == apply commands to all lines */ | 77 | int beg_line; /* 'sed 1p' 0 == apply commands to all lines */ |
76 | int end_line; /* 'sed 1,3p' 0 == one line only. -1 = last line ($) */ | 78 | int end_line; /* 'sed 1,3p' 0 == one line only. -1 = last line ($) */ |
77 | 79 | ||
78 | FILE *file; /* File (sr) command writes to, -1 for none. */ | 80 | FILE *file; /* File (sw) command writes to, -1 for none. */ |
79 | char *string; /* Data string for (saicytb) commands. */ | 81 | char *string; /* Data string for (saicytb) commands. */ |
80 | 82 | ||
81 | unsigned short which_match; /* (s) Which match to replace (0 for all) */ | 83 | unsigned short which_match; /* (s) Which match to replace (0 for all) */ |
@@ -83,7 +85,7 @@ typedef struct sed_cmd_s { | |||
83 | /* Bitfields (gcc won't group them if we don't) */ | 85 | /* Bitfields (gcc won't group them if we don't) */ |
84 | unsigned int invert:1; /* the '!' after the address */ | 86 | unsigned int invert:1; /* the '!' after the address */ |
85 | unsigned int in_match:1; /* Next line also included in match? */ | 87 | unsigned int in_match:1; /* Next line also included in match? */ |
86 | unsigned int no_newline:1; /* Last line written by (sr) had no '\n' */ | 88 | unsigned int no_newline:1; /* Last line written by (sw) had no '\n' */ |
87 | unsigned int sub_p:1; /* (s) print option */ | 89 | unsigned int sub_p:1; /* (s) print option */ |
88 | 90 | ||
89 | 91 | ||
@@ -120,6 +122,7 @@ struct append_list { | |||
120 | }; | 122 | }; |
121 | static struct append_list *append_head=NULL, *append_tail=NULL; | 123 | static struct append_list *append_head=NULL, *append_tail=NULL; |
122 | 124 | ||
125 | void free_and_close_stuff(void); | ||
123 | #ifdef CONFIG_FEATURE_CLEAN_UP | 126 | #ifdef CONFIG_FEATURE_CLEAN_UP |
124 | static void free_and_close_stuff(void) | 127 | static void free_and_close_stuff(void) |
125 | { | 128 | { |
@@ -729,13 +732,13 @@ static char *get_next_line(int *no_newline) | |||
729 | int len; | 732 | int len; |
730 | 733 | ||
731 | flush_append(); | 734 | flush_append(); |
732 | while(current_input_file<input_file_count) { | 735 | while (current_input_file<input_file_count) { |
733 | temp=bb_get_line_from_file(input_file_list[current_input_file]); | 736 | temp = bb_get_chunk_from_file(input_file_list[current_input_file],&len); |
734 | if(temp) { | 737 | if (temp) { |
735 | len=strlen(temp); | 738 | *no_newline = !(len && temp[len-1]=='\n'); |
736 | *no_newline=!(len && temp[len-1]=='\n'); | 739 | if (!*no_newline) temp[len-1] = 0; |
737 | if(!*no_newline) temp[len-1]=0; | ||
738 | break; | 740 | break; |
741 | // Close this file and advance to next one | ||
739 | } else fclose(input_file_list[current_input_file++]); | 742 | } else fclose(input_file_list[current_input_file++]); |
740 | } | 743 | } |
741 | 744 | ||
@@ -762,12 +765,15 @@ static int puts_maybe_newline(char *s, FILE *file, int missing_newline, int no_n | |||
762 | 765 | ||
763 | #define sed_puts(s,n) missing_newline=puts_maybe_newline(s,nonstdout,missing_newline,n) | 766 | #define sed_puts(s,n) missing_newline=puts_maybe_newline(s,nonstdout,missing_newline,n) |
764 | 767 | ||
768 | /* Process all the lines in all the files */ | ||
769 | |||
765 | static void process_files(void) | 770 | static void process_files(void) |
766 | { | 771 | { |
767 | char *pattern_space, *next_line; | 772 | char *pattern_space, *next_line; |
768 | int linenum = 0, missing_newline=0; | 773 | int linenum = 0, missing_newline=0; |
769 | int no_newline,next_no_newline=0; | 774 | int no_newline,next_no_newline=0; |
770 | 775 | ||
776 | /* Prime the pump */ | ||
771 | next_line = get_next_line(&next_no_newline); | 777 | next_line = get_next_line(&next_no_newline); |
772 | 778 | ||
773 | /* go through every line in each file */ | 779 | /* go through every line in each file */ |
@@ -779,7 +785,8 @@ static void process_files(void) | |||
779 | if(!(pattern_space=next_line)) break; | 785 | if(!(pattern_space=next_line)) break; |
780 | no_newline=next_no_newline; | 786 | no_newline=next_no_newline; |
781 | 787 | ||
782 | /* Read one line in advance so we can act on the last line, the '$' address */ | 788 | /* Read one line in advance so we can act on the last line, |
789 | * the '$' address */ | ||
783 | next_line = get_next_line(&next_no_newline); | 790 | next_line = get_next_line(&next_no_newline); |
784 | linenum++; | 791 | linenum++; |
785 | restart: | 792 | restart: |
@@ -921,16 +928,16 @@ restart: | |||
921 | /* Read file, append contents to output */ | 928 | /* Read file, append contents to output */ |
922 | case 'r': | 929 | case 'r': |
923 | { | 930 | { |
924 | FILE *outfile; | 931 | FILE *rfile; |
925 | 932 | ||
926 | outfile = fopen(sed_cmd->string, "r"); | 933 | rfile = fopen(sed_cmd->string, "r"); |
927 | if (outfile) { | 934 | if (rfile) { |
928 | char *line; | 935 | char *line; |
929 | 936 | ||
930 | while ((line = bb_get_chomped_line_from_file(outfile)) | 937 | while ((line = bb_get_chomped_line_from_file(rfile)) |
931 | != NULL) | 938 | != NULL) |
932 | append(line); | 939 | append(line); |
933 | bb_xprint_and_close_file(outfile); | 940 | bb_xprint_and_close_file(rfile); |
934 | } | 941 | } |
935 | 942 | ||
936 | break; | 943 | break; |
@@ -1107,11 +1114,9 @@ extern int sed_main(int argc, char **argv) | |||
1107 | { | 1114 | { |
1108 | int status = EXIT_SUCCESS, opt, getpat = 1; | 1115 | int status = EXIT_SUCCESS, opt, getpat = 1; |
1109 | 1116 | ||
1110 | #ifdef CONFIG_FEATURE_CLEAN_UP | ||
1111 | /* destroy command strings on exit */ | 1117 | /* destroy command strings on exit */ |
1112 | if (atexit(free_and_close_stuff) == -1) | 1118 | if (ENABLE_FEATURE_CLEAN_UP && atexit(free_and_close_stuff) == -1) |
1113 | bb_perror_msg_and_die("atexit"); | 1119 | bb_perror_msg_and_die("atexit"); |
1114 | #endif | ||
1115 | 1120 | ||
1116 | /* Lie to autoconf when it starts asking stupid questions. */ | 1121 | /* Lie to autoconf when it starts asking stupid questions. */ |
1117 | if(argc==2 && !strcmp(argv[1],"--version")) { | 1122 | if(argc==2 && !strcmp(argv[1],"--version")) { |
@@ -1197,12 +1202,15 @@ extern int sed_main(int argc, char **argv) | |||
1197 | if(-1==(nonstdoutfd=mkstemp(outname))) | 1202 | if(-1==(nonstdoutfd=mkstemp(outname))) |
1198 | bb_error_msg_and_die("no temp file"); | 1203 | bb_error_msg_and_die("no temp file"); |
1199 | nonstdout=fdopen(nonstdoutfd,"w"); | 1204 | nonstdout=fdopen(nonstdoutfd,"w"); |
1205 | |||
1200 | /* Set permissions of output file */ | 1206 | /* Set permissions of output file */ |
1207 | |||
1201 | fstat(fileno(file),&statbuf); | 1208 | fstat(fileno(file),&statbuf); |
1202 | fchmod(nonstdoutfd,statbuf.st_mode); | 1209 | fchmod(nonstdoutfd,statbuf.st_mode); |
1203 | add_input_file(file); | 1210 | add_input_file(file); |
1204 | process_files(); | 1211 | process_files(); |
1205 | fclose(nonstdout); | 1212 | fclose(nonstdout); |
1213 | |||
1206 | nonstdout=stdout; | 1214 | nonstdout=stdout; |
1207 | unlink(argv[i]); | 1215 | unlink(argv[i]); |
1208 | rename(outname,argv[i]); | 1216 | rename(outname,argv[i]); |