From e3f5a3fd82bcdc2ecef034d6e45863d5e4b92770 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 9 May 2006 03:53:55 +0000 Subject: Convert sed's static variables to a single global structure. --- editors/sed.c | 253 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 130 insertions(+), 123 deletions(-) diff --git a/editors/sed.c b/editors/sed.c index a3b825028..893931153 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -21,7 +21,7 @@ add_cmd() is called on each line of sed command text (from a file or from the command line). It calls get_address() and parse_cmd_args(). The resulting sed_cmd_t structures are appended to a linked list - (sed_cmd_head/sed_cmd_tail). + (bbg.sed_cmd_head/bbg.sed_cmd_tail). add_input_file() adds a FILE * to the list of input files. We need to know all input sources ahead of time to find the last line for the $ match. @@ -60,13 +60,14 @@ #include #include /* for getopt() */ -#include /* for strdup() */ #include #include /* for isspace() */ #include +#include #include "busybox.h" #include "xregex.h" +/* Each sed command turns into one of these structures. */ typedef struct sed_cmd_s { /* Ordered by alignment requirements: currently 36 bytes on x86 */ @@ -88,43 +89,52 @@ typedef struct sed_cmd_s { unsigned int no_newline:1; /* Last line written by (sw) had no '\n' */ unsigned int sub_p:1; /* (s) print option */ - /* GENERAL FIELDS */ char cmd; /* The command char: abcdDgGhHilnNpPqrstwxy:={} */ struct sed_cmd_s *next; /* Next command (linked list, NULL terminated) */ } sed_cmd_t; -/* globals */ -/* options */ -static int be_quiet, in_place, regex_type; -static FILE *nonstdout; -static char *outname,*hold_space; - -/* List of input files */ -static int input_file_count,current_input_file; -static FILE **input_file_list; - static const char bad_format_in_subst[] = "bad format in substitution expression"; static const char *const semicolon_whitespace = "; \n\r\t\v"; -static regmatch_t regmatch[10]; -static regex_t *previous_regex_ptr; +struct sed_globals +{ + /* options */ + int be_quiet, in_place, regex_type; + FILE *nonstdout; + char *outname, *hold_space; + + /* List of input files */ + int input_file_count,current_input_file; + FILE **input_file_list; + + regmatch_t regmatch[10]; + regex_t *previous_regex_ptr; + + /* linked list of sed commands */ + sed_cmd_t sed_cmd_head, *sed_cmd_tail; -/* linked list of sed commands */ -static sed_cmd_t sed_cmd_head; -static sed_cmd_t *sed_cmd_tail = &sed_cmd_head; + /* Linked list of append lines */ + llist_t *append_head; + + char *add_cmd_line; + + struct pipeline { + char *buf; /* Space to hold string */ + int idx; /* Space used */ + int len; /* Space allocated */ + } pipeline; +} bbg; -/* Linked list of append lines */ -static llist_t *append_head=NULL; void sed_free_and_close_stuff(void); #if ENABLE_FEATURE_CLEAN_UP void sed_free_and_close_stuff(void) { - sed_cmd_t *sed_cmd = sed_cmd_head.next; + sed_cmd_t *sed_cmd = bbg.sed_cmd_head.next; - llist_free_contents(append_head); + llist_free_contents(bbg.append_head); while (sed_cmd) { sed_cmd_t *sed_cmd_next = sed_cmd->next; @@ -149,10 +159,10 @@ void sed_free_and_close_stuff(void) sed_cmd = sed_cmd_next; } - if(hold_space) free(hold_space); + if(bbg.hold_space) free(bbg.hold_space); - while(current_input_fileend_line, &sed_cmd->end_match); - if (!idx) bb_error_msg_and_die("get_address: no address found in string\n"); + if (!idx) bb_error_msg_and_die("no address after comma\n"); cmdstr += idx; } @@ -542,32 +546,27 @@ static void add_cmd(char *cmdstr) cmdstr = parse_cmd_args(sed_cmd, cmdstr); /* Add the command to the command array */ - sed_cmd_tail->next = sed_cmd; - sed_cmd_tail = sed_cmd_tail->next; + bbg.sed_cmd_tail->next = sed_cmd; + bbg.sed_cmd_tail = bbg.sed_cmd_tail->next; } /* If we glued multiple lines together, free the memory. */ - free(add_cmd_line); - add_cmd_line=NULL; + free(bbg.add_cmd_line); + bbg.add_cmd_line = NULL; } /* Append to a string, reallocating memory as necessary. */ -static struct pipeline { - char *buf; /* Space to hold string */ - int idx; /* Space used */ - int len; /* Space allocated */ -} pipeline; - #define PIPE_GROW 64 static void pipe_putc(char c) { - if(pipeline.idx==pipeline.len) { - pipeline.buf = xrealloc(pipeline.buf, pipeline.len + PIPE_GROW); - pipeline.len+=PIPE_GROW; + if(bbg.pipeline.idx==bbg.pipeline.len) { + bbg.pipeline.buf = xrealloc(bbg.pipeline.buf, + bbg.pipeline.len + PIPE_GROW); + bbg.pipeline.len+=PIPE_GROW; } - pipeline.buf[pipeline.idx++] = (c); + bbg.pipeline.buf[bbg.pipeline.idx++] = c; } static void do_subst_w_backrefs(const char *line, const char *replace) @@ -580,10 +579,10 @@ static void do_subst_w_backrefs(const char *line, const char *replace) if (replace[i] == '\\' && replace[i+1]>='0' && replace[i+1]<='9') { int backref=replace[++i]-'0'; - /* print out the text held in regmatch[backref] */ - if(regmatch[backref].rm_so != -1) - for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++) - pipe_putc(line[j]); + /* print out the text held in bbg.regmatch[backref] */ + if(bbg.regmatch[backref].rm_so != -1) + for (j = bbg.regmatch[backref].rm_so; + j < bbg.regmatch[backref].rm_eo; j++) pipe_putc(line[j]); } /* if we find a backslash escaped character, print the character */ @@ -591,14 +590,14 @@ static void do_subst_w_backrefs(const char *line, const char *replace) /* if we find an unescaped '&' print out the whole matched text. */ else if (replace[i] == '&') - for (j = regmatch[0].rm_so; j < regmatch[0].rm_eo; j++) + for (j = bbg.regmatch[0].rm_so; j < bbg.regmatch[0].rm_eo; j++) pipe_putc(line[j]); /* Otherwise just output the character. */ else pipe_putc(replace[i]); } } -static int do_subst_command(sed_cmd_t * sed_cmd, char **line) +static int do_subst_command(sed_cmd_t *sed_cmd, char **line) { char *oldline = *line; int altered = 0; @@ -607,19 +606,19 @@ static int do_subst_command(sed_cmd_t * sed_cmd, char **line) /* Handle empty regex. */ if (sed_cmd->sub_match == NULL) { - current_regex = previous_regex_ptr; + current_regex = bbg.previous_regex_ptr; if(!current_regex) bb_error_msg_and_die("No previous regexp."); - } else previous_regex_ptr = current_regex = sed_cmd->sub_match; + } else bbg.previous_regex_ptr = current_regex = sed_cmd->sub_match; /* Find the first match */ - if(REG_NOMATCH==regexec(current_regex, oldline, 10, regmatch, 0)) + if(REG_NOMATCH==regexec(current_regex, oldline, 10, bbg.regmatch, 0)) return 0; /* Initialize temporary output buffer. */ - pipeline.buf=xmalloc(PIPE_GROW); - pipeline.len=PIPE_GROW; - pipeline.idx=0; + bbg.pipeline.buf=xmalloc(PIPE_GROW); + bbg.pipeline.len=PIPE_GROW; + bbg.pipeline.idx=0; /* Now loop through, substituting for matches */ do { @@ -629,7 +628,7 @@ static int do_subst_command(sed_cmd_t * sed_cmd, char **line) echo " a.b" | busybox sed 's [^ .]* x g' The match_count check is so not to break echo "hi" | busybox sed 's/^/!/g' */ - if(!regmatch[0].rm_so && !regmatch[0].rm_eo && match_count) { + if(!bbg.regmatch[0].rm_so && !bbg.regmatch[0].rm_eo && match_count) { pipe_putc(*(oldline++)); continue; } @@ -639,25 +638,25 @@ static int do_subst_command(sed_cmd_t * sed_cmd, char **line) /* If we aren't interested in this match, output old line to end of match and continue */ if(sed_cmd->which_match && sed_cmd->which_match!=match_count) { - for(i=0;istring); /* advance past the match */ - oldline += regmatch[0].rm_eo; + oldline += bbg.regmatch[0].rm_eo; /* flag that something has changed */ altered++; /* if we're not doing this globally, get out now */ if (sed_cmd->which_match) break; - } while (*oldline && (regexec(current_regex, oldline, 10, regmatch, 0) != REG_NOMATCH)); + } while (*oldline && (regexec(current_regex, oldline, 10, bbg.regmatch, 0) != REG_NOMATCH)); /* Copy rest of string into output pipeline */ @@ -665,7 +664,7 @@ static int do_subst_command(sed_cmd_t * sed_cmd, char **line) pipe_putc(0); free(*line); - *line = pipeline.buf; + *line = bbg.pipeline.buf; return altered; } @@ -674,7 +673,7 @@ static sed_cmd_t *branch_to(const char *label) { sed_cmd_t *sed_cmd; - for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) { + for (sed_cmd = bbg.sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) { if ((sed_cmd->cmd == ':') && (sed_cmd->string) && (strcmp(sed_cmd->string, label) == 0)) { return (sed_cmd); } @@ -684,7 +683,7 @@ static sed_cmd_t *branch_to(const char *label) static void append(char *s) { - append_head=llist_add_to_end(append_head, bb_xstrdup(s)); + bbg.append_head = llist_add_to_end(bbg.append_head, bb_xstrdup(s)); } static void flush_append(void) @@ -692,19 +691,20 @@ static void flush_append(void) char *data; /* Output appended lines. */ - while((data = (char *)llist_pop(&append_head))) { - fprintf(nonstdout,"%s\n",data); + while((data = (char *)llist_pop(&bbg.append_head))) { + fprintf(bbg.nonstdout,"%s\n",data); free(data); } } static 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; + bbg.input_file_list=xrealloc(bbg.input_file_list, + (bbg.input_file_count + 1) * sizeof(FILE *)); + bbg.input_file_list[bbg.input_file_count++] = file; } -/* Get next line of input from input_file_list, flushing append buffer and +/* Get next line of input from bbg.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(int *no_newline) @@ -713,14 +713,14 @@ static char *get_next_line(int *no_newline) int len; flush_append(); - while (current_input_filenext) { + for (sed_cmd = bbg.sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) + { int old_matched, matched; old_matched = sed_cmd->in_match; @@ -828,7 +829,7 @@ restart: if (sed_cmd->invert ? !matched : matched) { /* Update last used regex in case a blank substitute BRE is found */ if (sed_cmd->beg_match) { - previous_regex_ptr = sed_cmd->beg_match; + bbg.previous_regex_ptr = sed_cmd->beg_match; } /* actual sedding */ @@ -836,7 +837,7 @@ restart: /* Print line number */ case '=': - fprintf(nonstdout,"%d\n", linenum); + fprintf(bbg.nonstdout,"%d\n", linenum); break; /* Write the current pattern space up to the first newline */ @@ -931,7 +932,7 @@ restart: /* Read next line from input */ case 'n': - if (!be_quiet) + if (!bbg.be_quiet) sed_puts(pattern_space,no_newline); if (next_line) { free(pattern_space); @@ -1007,7 +1008,7 @@ restart: } case 'g': /* Replace pattern space with hold space */ free(pattern_space); - pattern_space = strdup(hold_space ? hold_space : ""); + pattern_space = bb_xstrdup(bbg.hold_space ? bbg.hold_space : ""); break; case 'G': /* Append newline and hold space to pattern space */ { @@ -1016,42 +1017,46 @@ restart: if (pattern_space) pattern_space_size += strlen(pattern_space); - if (hold_space) hold_space_size = strlen(hold_space); - pattern_space = xrealloc(pattern_space, pattern_space_size + hold_space_size); + if (bbg.hold_space) + hold_space_size = strlen(bbg.hold_space); + pattern_space = xrealloc(pattern_space, + pattern_space_size + hold_space_size); if (pattern_space_size == 2) pattern_space[0]=0; strcat(pattern_space, "\n"); - if (hold_space) strcat(pattern_space, hold_space); + if (bbg.hold_space) + strcat(pattern_space, bbg.hold_space); no_newline=0; break; } case 'h': /* Replace hold space with pattern space */ - free(hold_space); - hold_space = strdup(pattern_space); + free(bbg.hold_space); + bbg.hold_space = bb_xstrdup(pattern_space); break; case 'H': /* Append newline and pattern space to hold space */ { int hold_space_size = 2; int pattern_space_size = 0; - if (hold_space) hold_space_size += strlen(hold_space); + if (bbg.hold_space) + hold_space_size += strlen(bbg.hold_space); if (pattern_space) pattern_space_size = strlen(pattern_space); - hold_space = xrealloc(hold_space, + bbg.hold_space = xrealloc(bbg.hold_space, hold_space_size + pattern_space_size); - if (hold_space_size == 2) hold_space[0]=0; - strcat(hold_space, "\n"); - if (pattern_space) strcat(hold_space, pattern_space); + if (hold_space_size == 2) *bbg.hold_space=0; + strcat(bbg.hold_space, "\n"); + if (pattern_space) strcat(bbg.hold_space, pattern_space); break; } case 'x': /* Exchange hold and pattern space */ { char *tmp = pattern_space; - pattern_space = hold_space; + pattern_space = bbg.hold_space; no_newline=0; - hold_space = tmp; + bbg.hold_space = tmp; break; } } @@ -1064,7 +1069,7 @@ restart: discard_commands: /* we will print the line unless we were told to be quiet ('-n') or if the line was suppressed (ala 'd'elete) */ - if (!be_quiet) sed_puts(pattern_space,no_newline); + if (!bbg.be_quiet) sed_puts(pattern_space,no_newline); /* Delete and such jump here. */ discard_line: @@ -1095,6 +1100,8 @@ int sed_main(int argc, char **argv) { int status = EXIT_SUCCESS, opt, getpat = 1; + bbg.sed_cmd_tail=&bbg.sed_cmd_head; + /* destroy command strings on exit */ if (ENABLE_FEATURE_CLEAN_UP) atexit(sed_free_and_close_stuff); @@ -1108,14 +1115,14 @@ int sed_main(int argc, char **argv) while ((opt = getopt(argc, argv, "irne:f:")) > 0) { switch (opt) { case 'i': - in_place++; + bbg.in_place++; atexit(cleanup_outname); break; case 'r': - regex_type|=REG_EXTENDED; + bbg.regex_type|=REG_EXTENDED; break; case 'n': - be_quiet++; + bbg.be_quiet++; break; case 'e': add_cmd_block(optarg); @@ -1153,13 +1160,13 @@ int sed_main(int argc, char **argv) add_cmd(""); /* By default, we write to stdout */ - nonstdout=stdout; + bbg.nonstdout=stdout; /* argv[(optind)..(argc-1)] should be names of file to process. If no * files were specified or '-' was specified, take input from stdin. * Otherwise, we process all the files specified. */ if (argv[optind] == NULL) { - if(in_place) bb_error_msg_and_die("Filename required for -i"); + if(bbg.in_place) bb_error_msg_and_die("Filename required for -i"); add_input_file(stdin); process_files(); } else { @@ -1167,21 +1174,21 @@ int sed_main(int argc, char **argv) FILE *file; for (i = optind; i < argc; i++) { - if(!strcmp(argv[i], "-") && !in_place) { + if(!strcmp(argv[i], "-") && !bbg.in_place) { add_input_file(stdin); process_files(); } else { file = bb_wfopen(argv[i], "r"); if (file) { - if(in_place) { + if(bbg.in_place) { struct stat statbuf; int nonstdoutfd; - outname=bb_xstrndup(argv[i],strlen(argv[i])+6); - strcat(outname,"XXXXXX"); - if(-1==(nonstdoutfd=mkstemp(outname))) + bbg.outname=bb_xstrndup(argv[i],strlen(argv[i])+6); + strcat(bbg.outname,"XXXXXX"); + if(-1==(nonstdoutfd=mkstemp(bbg.outname))) bb_error_msg_and_die("no temp file"); - nonstdout=fdopen(nonstdoutfd,"w"); + bbg.nonstdout=fdopen(nonstdoutfd,"w"); /* Set permissions of output file */ @@ -1189,20 +1196,20 @@ int sed_main(int argc, char **argv) fchmod(nonstdoutfd,statbuf.st_mode); add_input_file(file); process_files(); - fclose(nonstdout); + fclose(bbg.nonstdout); - nonstdout=stdout; + bbg.nonstdout=stdout; unlink(argv[i]); - rename(outname,argv[i]); - free(outname); - outname=0; + rename(bbg.outname,argv[i]); + free(bbg.outname); + bbg.outname=0; } else add_input_file(file); } else { status = EXIT_FAILURE; } } } - if(input_file_count>current_input_file) process_files(); + if(bbg.input_file_count>bbg.current_input_file) process_files(); } return status; -- cgit v1.2.3