diff options
Diffstat (limited to 'busybox/editors')
-rw-r--r-- | busybox/editors/awk.c | 3 | ||||
-rw-r--r-- | busybox/editors/sed.c | 110 | ||||
-rw-r--r-- | busybox/editors/vi.c | 12 |
3 files changed, 69 insertions, 56 deletions
diff --git a/busybox/editors/awk.c b/busybox/editors/awk.c index c1cb2a2..ed8b0f2 100644 --- a/busybox/editors/awk.c +++ b/busybox/editors/awk.c @@ -1084,7 +1084,7 @@ static node *parse_expr(unsigned long iexp) { cn->a.n = vn; xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP; if (tc & (TC_OPERAND | TC_REGEXP)) { - xtc = TC_UOPPRE | TC_BINOP | TC_OPERAND | iexp; + xtc = TC_UOPPRE | TC_UOPPOST | TC_BINOP | TC_OPERAND | iexp; /* one should be very careful with switch on tclass - * only simple tclasses should be used! */ switch (tc) { @@ -1101,7 +1101,6 @@ static node *parse_expr(unsigned long iexp) { cn->info |= xS; cn->r.n = parse_expr(TC_ARRTERM); } - xtc = TC_UOPPOST | TC_UOPPRE | TC_BINOP | TC_OPERAND | iexp; break; case TC_NUMBER: diff --git a/busybox/editors/sed.c b/busybox/editors/sed.c index 3d68716..992e571 100644 --- a/busybox/editors/sed.c +++ b/busybox/editors/sed.c @@ -34,7 +34,10 @@ resulting sed_cmd_t structures are appended to a linked list (sed_cmd_head/sed_cmd_tail). - process_file() does actual sedding, reading data lines from an input FILE * + add_input_file() adds a FILE * to the list of input files. We need to + know them all ahead of time to find the last line for the $ match. + + process_files() does actual sedding, reading data lines from each input FILE * (which could be stdin) and applying the sed command list (sed_cmd_head) to each of the resulting lines. @@ -112,17 +115,20 @@ typedef struct sed_cmd_s { /* globals */ /* options */ -static int be_quiet = 0, in_place=0, regex_type=0; +static int be_quiet, in_place, regex_type; FILE *nonstdout; -char *outname; +char *outname,*hold_space; +/* List of input files */ +int input_file_count,current_input_file; +FILE **input_file_list; static const char bad_format_in_subst[] = "bad format in substitution expression"; const char *const semicolon_whitespace = "; \n\r\t\v"; regmatch_t regmatch[10]; -static regex_t *previous_regex_ptr = NULL; +static regex_t *previous_regex_ptr; /* linked list of sed commands */ static sed_cmd_t sed_cmd_head; @@ -169,6 +175,11 @@ static void free_and_close_stuff(void) free(sed_cmd); sed_cmd = sed_cmd_next; } + + if(hold_space) free(hold_space); + + while(current_input_file<input_file_count) + fclose(input_file_list[current_input_file++]); } #endif @@ -563,6 +574,8 @@ void add_cmd(char *cmdstr) } } +/* Append to a string, reallocating memory as necessary. */ + struct pipeline { char *buf; /* Space to hold string */ int idx; /* Space used */ @@ -716,20 +729,29 @@ static void flush_append(void) append_head=append_tail=NULL; } -/* Get next line of input, flushing append buffer and noting if we hit EOF - * without a newline on the last line. +void add_input_file(FILE *file) +{ + input_file_list=xrealloc(input_file_list,(input_file_count+1)*sizeof(FILE *)); + input_file_list[input_file_count++]=file; +} + +/* Get next line of input from input_file_list, flushing append buffer and + * noting if we ran out of files without a newline on the last line we read. */ -static char *get_next_line(FILE * file, int *no_newline) +static char *get_next_line(int *no_newline) { - char *temp; + char *temp=NULL; int len; flush_append(); - temp=bb_get_line_from_file(file); - if(temp) { - len=strlen(temp); - if(len && temp[len-1]=='\n') temp[len-1]=0; - else *no_newline=1; + while(current_input_file<input_file_count) { + temp=bb_get_line_from_file(input_file_list[current_input_file]); + if(temp) { + len=strlen(temp); + *no_newline=!(len && temp[len-1]=='\n'); + if(!*no_newline) temp[len-1]=0; + break; + } else fclose(input_file_list[current_input_file++]); } return temp; @@ -755,15 +777,15 @@ static int puts_maybe_newline(char *s, FILE *file, int missing_newline, int no_n #define sed_puts(s,n) missing_newline=puts_maybe_newline(s,nonstdout,missing_newline,n) -static void process_file(FILE *file) +static void process_files(void) { - char *pattern_space, *next_line, *hold_space=NULL; - static int linenum = 0, missing_newline=0; + char *pattern_space, *next_line; + int linenum = 0, missing_newline=0; int no_newline,next_no_newline=0; - next_line = get_next_line(file,&next_no_newline); + next_line = get_next_line(&next_no_newline); - /* go through every line in the file */ + /* go through every line in each file */ for(;;) { sed_cmd_t *sed_cmd; int substituted=0; @@ -773,7 +795,7 @@ static void process_file(FILE *file) no_newline=next_no_newline; /* Read one line in advance so we can act on the last line, the '$' address */ - next_line = get_next_line(file,&next_no_newline); + next_line = get_next_line(&next_no_newline); linenum++; restart: /* for every line, go through all the commands */ @@ -908,7 +930,7 @@ restart: /* Cut and paste text (replace) */ case 'c': /* Only triggers on last line of a matching range. */ - if (!sed_cmd->in_match) sed_puts(sed_cmd->string,1); + if (!sed_cmd->in_match) sed_puts(sed_cmd->string,0); goto discard_line; /* Read file, append contents to output */ @@ -942,7 +964,7 @@ restart: free(pattern_space); pattern_space = next_line; no_newline=next_no_newline; - next_line = get_next_line(file,&next_no_newline); + next_line = get_next_line(&next_no_newline); linenum++; break; } @@ -972,7 +994,7 @@ restart: pattern_space[len]='\n'; strcpy(pattern_space+len+1, next_line); no_newline=next_no_newline; - next_line = get_next_line(file,&next_no_newline); + next_line = get_next_line(&next_no_newline); linenum++; } break; @@ -1007,10 +1029,7 @@ restart: } case 'g': /* Replace pattern space with hold space */ free(pattern_space); - if (hold_space) { - pattern_space = strdup(hold_space); - no_newline=0; - } + pattern_space = strdup(hold_space ? hold_space : ""); break; case 'G': /* Append newline and hold space to pattern space */ { @@ -1096,9 +1115,7 @@ static void add_cmd_block(char *cmdstr) extern int sed_main(int argc, char **argv) { - int status = EXIT_SUCCESS; - int opt; - uint8_t getpat = 1; + int status = EXIT_SUCCESS, opt, getpat = 1; #ifdef CONFIG_FEATURE_CLEAN_UP /* destroy command strings on exit */ @@ -1153,8 +1170,7 @@ extern int sed_main(int argc, char **argv) } } - /* if we didn't get a pattern from a -e and no command file was specified, - * argv[optind] should be the pattern. no pattern, no worky */ + /* if we didn't get a pattern from -e or -f, use argv[optind] */ if(getpat) { if (argv[optind] == NULL) bb_show_usage(); @@ -1171,49 +1187,47 @@ extern int sed_main(int argc, char **argv) * files were specified or '-' was specified, take input from stdin. * Otherwise, we process all the files specified. */ if (argv[optind] == NULL) { - if(in_place) { - fprintf(stderr,"sed: Filename required for -i\n"); - exit(1); - } - process_file(stdin); + if(in_place) bb_error_msg_and_die("Filename required for -i"); + add_input_file(stdin); + process_files(); } else { int i; FILE *file; for (i = optind; i < argc; i++) { if(!strcmp(argv[i], "-") && !in_place) { - process_file(stdin); + add_input_file(stdin); + process_files(); } else { file = bb_wfopen(argv[i], "r"); if (file) { if(in_place) { struct stat statbuf; + int nonstdoutfd; + outname=bb_xstrndup(argv[i],strlen(argv[i])+6); strcat(outname,"XXXXXX"); + if(-1==(nonstdoutfd=mkstemp(outname))) + bb_error_msg_and_die("no temp file"); + nonstdout=fdopen(nonstdoutfd,"w"); /* Set permissions of output file */ fstat(fileno(file),&statbuf); - mkstemp(outname); - nonstdout=bb_wfopen(outname,"w"); - /* Set permissions of output file */ - fstat(fileno(file),&statbuf); - fchmod(fileno(nonstdout),statbuf.st_mode); - atexit(cleanup_outname); - } - process_file(file); - fclose(file); - if(in_place) { + fchmod(nonstdoutfd,statbuf.st_mode); + add_input_file(file); + process_files(); fclose(nonstdout); nonstdout=stdout; unlink(argv[i]); rename(outname,argv[i]); free(outname); outname=0; - } + } else add_input_file(file); } else { status = EXIT_FAILURE; } } } + if(input_file_count>current_input_file) process_files(); } return status; diff --git a/busybox/editors/vi.c b/busybox/editors/vi.c index cd6cf0e..5a47f5a 100644 --- a/busybox/editors/vi.c +++ b/busybox/editors/vi.c @@ -2340,7 +2340,7 @@ static Byte readit(void) // read (maybe cursor) key from stdin } //----- IO Routines -------------------------------------------- -static Byte get_one_char() +static Byte get_one_char(void) { static Byte c; @@ -2600,25 +2600,25 @@ static void place_cursor(int row, int col, int opti) } //----- Erase from cursor to end of line ----------------------- -static void clear_to_eol() +static void clear_to_eol(void) { write1(Ceol); // Erase from cursor to end of line } //----- Erase from cursor to end of screen ----------------------- -static void clear_to_eos() +static void clear_to_eos(void) { write1(Ceos); // Erase from cursor to end of screen } //----- Start standout mode ------------------------------------ -static void standout_start() // send "start reverse video" sequence +static void standout_start(void) // send "start reverse video" sequence { write1(SOs); // Start reverse video mode } //----- End standout mode -------------------------------------- -static void standout_end() // send "end reverse video" sequence +static void standout_end(void) // send "end reverse video" sequence { write1(SOn); // End reverse video mode } @@ -2648,7 +2648,7 @@ static void Indicate_Error(void) //----- Screen[] Routines -------------------------------------- //----- Erase the Screen[] memory ------------------------------ -static void screen_erase() +static void screen_erase(void) { memset(screen, ' ', screensize); // clear new screen } |