mirror of
https://github.com/sheumann/hush.git
synced 2025-01-20 05:30:25 +00:00
Convert sed's static variables to a single global structure.
This commit is contained in:
parent
0b65628395
commit
e3f5a3fd82
243
editors/sed.c
243
editors/sed.c
@ -21,7 +21,7 @@
|
|||||||
add_cmd() is called on each line of sed command text (from a file or from
|
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
|
the command line). It calls get_address() and parse_cmd_args(). The
|
||||||
resulting sed_cmd_t structures are appended to a linked list
|
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
|
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.
|
know all input sources ahead of time to find the last line for the $ match.
|
||||||
@ -60,13 +60,14 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h> /* for getopt() */
|
#include <unistd.h> /* for getopt() */
|
||||||
#include <string.h> /* for strdup() */
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <ctype.h> /* for isspace() */
|
#include <ctype.h> /* for isspace() */
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include "busybox.h"
|
#include "busybox.h"
|
||||||
#include "xregex.h"
|
#include "xregex.h"
|
||||||
|
|
||||||
|
/* Each sed command turns into one of these structures. */
|
||||||
typedef struct sed_cmd_s {
|
typedef struct sed_cmd_s {
|
||||||
/* Ordered by alignment requirements: currently 36 bytes on x86 */
|
/* 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 no_newline:1; /* Last line written by (sw) had no '\n' */
|
||||||
unsigned int sub_p:1; /* (s) print option */
|
unsigned int sub_p:1; /* (s) print option */
|
||||||
|
|
||||||
|
|
||||||
/* GENERAL FIELDS */
|
/* GENERAL FIELDS */
|
||||||
char cmd; /* The command char: abcdDgGhHilnNpPqrstwxy:={} */
|
char cmd; /* The command char: abcdDgGhHilnNpPqrstwxy:={} */
|
||||||
struct sed_cmd_s *next; /* Next command (linked list, NULL terminated) */
|
struct sed_cmd_s *next; /* Next command (linked list, NULL terminated) */
|
||||||
} sed_cmd_t;
|
} 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[] =
|
static const char bad_format_in_subst[] =
|
||||||
"bad format in substitution expression";
|
"bad format in substitution expression";
|
||||||
static const char *const semicolon_whitespace = "; \n\r\t\v";
|
static const char *const semicolon_whitespace = "; \n\r\t\v";
|
||||||
|
|
||||||
static regmatch_t regmatch[10];
|
struct sed_globals
|
||||||
static regex_t *previous_regex_ptr;
|
{
|
||||||
|
/* 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 */
|
/* linked list of sed commands */
|
||||||
static sed_cmd_t sed_cmd_head;
|
sed_cmd_t sed_cmd_head, *sed_cmd_tail;
|
||||||
static sed_cmd_t *sed_cmd_tail = &sed_cmd_head;
|
|
||||||
|
|
||||||
/* Linked list of append lines */
|
/* Linked list of append lines */
|
||||||
static llist_t *append_head=NULL;
|
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;
|
||||||
|
|
||||||
|
|
||||||
void sed_free_and_close_stuff(void);
|
void sed_free_and_close_stuff(void);
|
||||||
#if ENABLE_FEATURE_CLEAN_UP
|
#if ENABLE_FEATURE_CLEAN_UP
|
||||||
void sed_free_and_close_stuff(void)
|
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) {
|
while (sed_cmd) {
|
||||||
sed_cmd_t *sed_cmd_next = sed_cmd->next;
|
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;
|
sed_cmd = sed_cmd_next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(hold_space) free(hold_space);
|
if(bbg.hold_space) free(bbg.hold_space);
|
||||||
|
|
||||||
while(current_input_file<input_file_count)
|
while(bbg.current_input_file<bbg.input_file_count)
|
||||||
fclose(input_file_list[current_input_file++]);
|
fclose(bbg.input_file_list[bbg.current_input_file++]);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -160,7 +170,7 @@ void sed_free_and_close_stuff(void)
|
|||||||
|
|
||||||
static void cleanup_outname(void)
|
static void cleanup_outname(void)
|
||||||
{
|
{
|
||||||
if(outname) unlink(outname);
|
if(bbg.outname) unlink(bbg.outname);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* strdup, replacing "\n" with '\n', and "\delimiter" with 'delimiter' */
|
/* strdup, replacing "\n" with '\n', and "\delimiter" with 'delimiter' */
|
||||||
@ -282,7 +292,7 @@ static int get_address(char *my_str, int *linenum, regex_t ** regex)
|
|||||||
|
|
||||||
temp=copy_parsing_escapes(pos,next);
|
temp=copy_parsing_escapes(pos,next);
|
||||||
*regex = (regex_t *) xmalloc(sizeof(regex_t));
|
*regex = (regex_t *) xmalloc(sizeof(regex_t));
|
||||||
xregcomp(*regex, temp, regex_type|REG_NEWLINE);
|
xregcomp(*regex, temp, bbg.regex_type|REG_NEWLINE);
|
||||||
free(temp);
|
free(temp);
|
||||||
/* Move position to next character after last delimiter */
|
/* Move position to next character after last delimiter */
|
||||||
pos+=(next+1);
|
pos+=(next+1);
|
||||||
@ -310,7 +320,7 @@ static int parse_file_cmd(sed_cmd_t * sed_cmd, const char *filecmdstr, char **re
|
|||||||
|
|
||||||
static int parse_subst_cmd(sed_cmd_t *const sed_cmd, char *substr)
|
static int parse_subst_cmd(sed_cmd_t *const sed_cmd, char *substr)
|
||||||
{
|
{
|
||||||
int cflags = regex_type;
|
int cflags = bbg.regex_type;
|
||||||
char *match;
|
char *match;
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
|
|
||||||
@ -463,27 +473,21 @@ static char *parse_cmd_args(sed_cmd_t *sed_cmd, char *cmdstr)
|
|||||||
|
|
||||||
static void add_cmd(char *cmdstr)
|
static void add_cmd(char *cmdstr)
|
||||||
{
|
{
|
||||||
static char *add_cmd_line=NULL;
|
|
||||||
sed_cmd_t *sed_cmd;
|
sed_cmd_t *sed_cmd;
|
||||||
int temp;
|
int temp;
|
||||||
|
|
||||||
/* Append this line to any unfinished line from last time. */
|
/* Append this line to any unfinished line from last time. */
|
||||||
if(add_cmd_line) {
|
if (bbg.add_cmd_line) {
|
||||||
int lastlen=strlen(add_cmd_line);
|
cmdstr = bb_xasprintf("%s\n%s", bbg.add_cmd_line, cmdstr);
|
||||||
char *tmp=xmalloc(lastlen+strlen(cmdstr)+2);
|
free(bbg.add_cmd_line);
|
||||||
|
bbg.add_cmd_line = cmdstr;
|
||||||
memcpy(tmp,add_cmd_line,lastlen);
|
} else bbg.add_cmd_line=NULL;
|
||||||
tmp[lastlen]='\n';
|
|
||||||
strcpy(tmp+lastlen+1,cmdstr);
|
|
||||||
free(add_cmd_line);
|
|
||||||
cmdstr=add_cmd_line=tmp;
|
|
||||||
} else add_cmd_line=NULL;
|
|
||||||
|
|
||||||
/* If this line ends with backslash, request next line. */
|
/* If this line ends with backslash, request next line. */
|
||||||
temp=strlen(cmdstr);
|
temp=strlen(cmdstr);
|
||||||
if(temp && cmdstr[temp-1]=='\\') {
|
if(temp && cmdstr[temp-1]=='\\') {
|
||||||
if(!add_cmd_line) add_cmd_line=strdup(cmdstr);
|
if (!bbg.add_cmd_line) bbg.add_cmd_line = bb_xstrdup(cmdstr);
|
||||||
add_cmd_line[temp-1]=0;
|
bbg.add_cmd_line[temp-1] = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -498,7 +502,7 @@ static void add_cmd(char *cmdstr)
|
|||||||
/* if this is a comment, jump past it and keep going */
|
/* if this is a comment, jump past it and keep going */
|
||||||
if (*cmdstr == '#') {
|
if (*cmdstr == '#') {
|
||||||
/* "#n" is the same as using -n on the command line */
|
/* "#n" is the same as using -n on the command line */
|
||||||
if (cmdstr[1] == 'n') be_quiet++;
|
if (cmdstr[1] == 'n') bbg.be_quiet++;
|
||||||
if(!(cmdstr=strpbrk(cmdstr, "\n\r"))) break;
|
if(!(cmdstr=strpbrk(cmdstr, "\n\r"))) break;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -520,7 +524,7 @@ static void add_cmd(char *cmdstr)
|
|||||||
|
|
||||||
cmdstr++;
|
cmdstr++;
|
||||||
idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
|
idx = get_address(cmdstr, &sed_cmd->end_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;
|
cmdstr += idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -542,32 +546,27 @@ static void add_cmd(char *cmdstr)
|
|||||||
cmdstr = parse_cmd_args(sed_cmd, cmdstr);
|
cmdstr = parse_cmd_args(sed_cmd, cmdstr);
|
||||||
|
|
||||||
/* Add the command to the command array */
|
/* Add the command to the command array */
|
||||||
sed_cmd_tail->next = sed_cmd;
|
bbg.sed_cmd_tail->next = sed_cmd;
|
||||||
sed_cmd_tail = sed_cmd_tail->next;
|
bbg.sed_cmd_tail = bbg.sed_cmd_tail->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If we glued multiple lines together, free the memory. */
|
/* If we glued multiple lines together, free the memory. */
|
||||||
free(add_cmd_line);
|
free(bbg.add_cmd_line);
|
||||||
add_cmd_line=NULL;
|
bbg.add_cmd_line = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Append to a string, reallocating memory as necessary. */
|
/* 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
|
#define PIPE_GROW 64
|
||||||
|
|
||||||
static void pipe_putc(char c)
|
static void pipe_putc(char c)
|
||||||
{
|
{
|
||||||
if(pipeline.idx==pipeline.len) {
|
if(bbg.pipeline.idx==bbg.pipeline.len) {
|
||||||
pipeline.buf = xrealloc(pipeline.buf, pipeline.len + PIPE_GROW);
|
bbg.pipeline.buf = xrealloc(bbg.pipeline.buf,
|
||||||
pipeline.len+=PIPE_GROW;
|
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)
|
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') {
|
if (replace[i] == '\\' && replace[i+1]>='0' && replace[i+1]<='9') {
|
||||||
int backref=replace[++i]-'0';
|
int backref=replace[++i]-'0';
|
||||||
|
|
||||||
/* print out the text held in regmatch[backref] */
|
/* print out the text held in bbg.regmatch[backref] */
|
||||||
if(regmatch[backref].rm_so != -1)
|
if(bbg.regmatch[backref].rm_so != -1)
|
||||||
for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++)
|
for (j = bbg.regmatch[backref].rm_so;
|
||||||
pipe_putc(line[j]);
|
j < bbg.regmatch[backref].rm_eo; j++) pipe_putc(line[j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if we find a backslash escaped character, print the character */
|
/* if we find a backslash escaped character, print the character */
|
||||||
@ -591,7 +590,7 @@ static void do_subst_w_backrefs(const char *line, const char *replace)
|
|||||||
|
|
||||||
/* if we find an unescaped '&' print out the whole matched text. */
|
/* if we find an unescaped '&' print out the whole matched text. */
|
||||||
else if (replace[i] == '&')
|
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]);
|
pipe_putc(line[j]);
|
||||||
/* Otherwise just output the character. */
|
/* Otherwise just output the character. */
|
||||||
else pipe_putc(replace[i]);
|
else pipe_putc(replace[i]);
|
||||||
@ -607,19 +606,19 @@ static int do_subst_command(sed_cmd_t * sed_cmd, char **line)
|
|||||||
|
|
||||||
/* Handle empty regex. */
|
/* Handle empty regex. */
|
||||||
if (sed_cmd->sub_match == NULL) {
|
if (sed_cmd->sub_match == NULL) {
|
||||||
current_regex = previous_regex_ptr;
|
current_regex = bbg.previous_regex_ptr;
|
||||||
if(!current_regex)
|
if(!current_regex)
|
||||||
bb_error_msg_and_die("No previous regexp.");
|
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 */
|
/* 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;
|
return 0;
|
||||||
|
|
||||||
/* Initialize temporary output buffer. */
|
/* Initialize temporary output buffer. */
|
||||||
pipeline.buf=xmalloc(PIPE_GROW);
|
bbg.pipeline.buf=xmalloc(PIPE_GROW);
|
||||||
pipeline.len=PIPE_GROW;
|
bbg.pipeline.len=PIPE_GROW;
|
||||||
pipeline.idx=0;
|
bbg.pipeline.idx=0;
|
||||||
|
|
||||||
/* Now loop through, substituting for matches */
|
/* Now loop through, substituting for matches */
|
||||||
do {
|
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'
|
echo " a.b" | busybox sed 's [^ .]* x g'
|
||||||
The match_count check is so not to break
|
The match_count check is so not to break
|
||||||
echo "hi" | busybox sed 's/^/!/g' */
|
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++));
|
pipe_putc(*(oldline++));
|
||||||
continue;
|
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
|
/* If we aren't interested in this match, output old line to
|
||||||
end of match and continue */
|
end of match and continue */
|
||||||
if(sed_cmd->which_match && sed_cmd->which_match!=match_count) {
|
if(sed_cmd->which_match && sed_cmd->which_match!=match_count) {
|
||||||
for(i=0;i<regmatch[0].rm_eo;i++)
|
for(i=0;i<bbg.regmatch[0].rm_eo;i++)
|
||||||
pipe_putc(oldline[i]);
|
pipe_putc(oldline[i]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* print everything before the match */
|
/* print everything before the match */
|
||||||
for (i = 0; i < regmatch[0].rm_so; i++) pipe_putc(oldline[i]);
|
for (i = 0; i < bbg.regmatch[0].rm_so; i++) pipe_putc(oldline[i]);
|
||||||
|
|
||||||
/* then print the substitution string */
|
/* then print the substitution string */
|
||||||
do_subst_w_backrefs(oldline, sed_cmd->string);
|
do_subst_w_backrefs(oldline, sed_cmd->string);
|
||||||
|
|
||||||
/* advance past the match */
|
/* advance past the match */
|
||||||
oldline += regmatch[0].rm_eo;
|
oldline += bbg.regmatch[0].rm_eo;
|
||||||
/* flag that something has changed */
|
/* flag that something has changed */
|
||||||
altered++;
|
altered++;
|
||||||
|
|
||||||
/* if we're not doing this globally, get out now */
|
/* if we're not doing this globally, get out now */
|
||||||
if (sed_cmd->which_match) break;
|
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 */
|
/* 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);
|
pipe_putc(0);
|
||||||
|
|
||||||
free(*line);
|
free(*line);
|
||||||
*line = pipeline.buf;
|
*line = bbg.pipeline.buf;
|
||||||
return altered;
|
return altered;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -674,7 +673,7 @@ static sed_cmd_t *branch_to(const char *label)
|
|||||||
{
|
{
|
||||||
sed_cmd_t *sed_cmd;
|
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)) {
|
if ((sed_cmd->cmd == ':') && (sed_cmd->string) && (strcmp(sed_cmd->string, label) == 0)) {
|
||||||
return (sed_cmd);
|
return (sed_cmd);
|
||||||
}
|
}
|
||||||
@ -684,7 +683,7 @@ static sed_cmd_t *branch_to(const char *label)
|
|||||||
|
|
||||||
static void append(char *s)
|
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)
|
static void flush_append(void)
|
||||||
@ -692,19 +691,20 @@ static void flush_append(void)
|
|||||||
char *data;
|
char *data;
|
||||||
|
|
||||||
/* Output appended lines. */
|
/* Output appended lines. */
|
||||||
while((data = (char *)llist_pop(&append_head))) {
|
while((data = (char *)llist_pop(&bbg.append_head))) {
|
||||||
fprintf(nonstdout,"%s\n",data);
|
fprintf(bbg.nonstdout,"%s\n",data);
|
||||||
free(data);
|
free(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_input_file(FILE *file)
|
static void add_input_file(FILE *file)
|
||||||
{
|
{
|
||||||
input_file_list=xrealloc(input_file_list,(input_file_count+1)*sizeof(FILE *));
|
bbg.input_file_list=xrealloc(bbg.input_file_list,
|
||||||
input_file_list[input_file_count++]=file;
|
(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.
|
* noting if we ran out of files without a newline on the last line we read.
|
||||||
*/
|
*/
|
||||||
static char *get_next_line(int *no_newline)
|
static char *get_next_line(int *no_newline)
|
||||||
@ -713,14 +713,14 @@ static char *get_next_line(int *no_newline)
|
|||||||
int len;
|
int len;
|
||||||
|
|
||||||
flush_append();
|
flush_append();
|
||||||
while (current_input_file<input_file_count) {
|
while (bbg.current_input_file<bbg.input_file_count) {
|
||||||
temp = bb_get_chunk_from_file(input_file_list[current_input_file],&len);
|
temp = bb_get_chunk_from_file(bbg.input_file_list[bbg.current_input_file],&len);
|
||||||
if (temp) {
|
if (temp) {
|
||||||
*no_newline = !(len && temp[len-1]=='\n');
|
*no_newline = !(len && temp[len-1]=='\n');
|
||||||
if (!*no_newline) temp[len-1] = 0;
|
if (!*no_newline) temp[len-1] = 0;
|
||||||
break;
|
break;
|
||||||
// Close this file and advance to next one
|
// Close this file and advance to next one
|
||||||
} else fclose(input_file_list[current_input_file++]);
|
} else fclose(bbg.input_file_list[bbg.current_input_file++]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return temp;
|
return temp;
|
||||||
@ -744,7 +744,7 @@ static int puts_maybe_newline(char *s, FILE *file, int missing_newline, int no_n
|
|||||||
return no_newline;
|
return no_newline;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define sed_puts(s,n) missing_newline=puts_maybe_newline(s,nonstdout,missing_newline,n)
|
#define sed_puts(s,n) missing_newline=puts_maybe_newline(s,bbg.nonstdout,missing_newline,n)
|
||||||
|
|
||||||
/* Process all the lines in all the files */
|
/* Process all the lines in all the files */
|
||||||
|
|
||||||
@ -772,7 +772,8 @@ static void process_files(void)
|
|||||||
linenum++;
|
linenum++;
|
||||||
restart:
|
restart:
|
||||||
/* for every line, go through all the commands */
|
/* for every line, go through all the commands */
|
||||||
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)
|
||||||
|
{
|
||||||
int old_matched, matched;
|
int old_matched, matched;
|
||||||
|
|
||||||
old_matched = sed_cmd->in_match;
|
old_matched = sed_cmd->in_match;
|
||||||
@ -828,7 +829,7 @@ restart:
|
|||||||
if (sed_cmd->invert ? !matched : matched) {
|
if (sed_cmd->invert ? !matched : matched) {
|
||||||
/* Update last used regex in case a blank substitute BRE is found */
|
/* Update last used regex in case a blank substitute BRE is found */
|
||||||
if (sed_cmd->beg_match) {
|
if (sed_cmd->beg_match) {
|
||||||
previous_regex_ptr = sed_cmd->beg_match;
|
bbg.previous_regex_ptr = sed_cmd->beg_match;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* actual sedding */
|
/* actual sedding */
|
||||||
@ -836,7 +837,7 @@ restart:
|
|||||||
|
|
||||||
/* Print line number */
|
/* Print line number */
|
||||||
case '=':
|
case '=':
|
||||||
fprintf(nonstdout,"%d\n", linenum);
|
fprintf(bbg.nonstdout,"%d\n", linenum);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* Write the current pattern space up to the first newline */
|
/* Write the current pattern space up to the first newline */
|
||||||
@ -931,7 +932,7 @@ restart:
|
|||||||
|
|
||||||
/* Read next line from input */
|
/* Read next line from input */
|
||||||
case 'n':
|
case 'n':
|
||||||
if (!be_quiet)
|
if (!bbg.be_quiet)
|
||||||
sed_puts(pattern_space,no_newline);
|
sed_puts(pattern_space,no_newline);
|
||||||
if (next_line) {
|
if (next_line) {
|
||||||
free(pattern_space);
|
free(pattern_space);
|
||||||
@ -1007,7 +1008,7 @@ restart:
|
|||||||
}
|
}
|
||||||
case 'g': /* Replace pattern space with hold space */
|
case 'g': /* Replace pattern space with hold space */
|
||||||
free(pattern_space);
|
free(pattern_space);
|
||||||
pattern_space = strdup(hold_space ? hold_space : "");
|
pattern_space = bb_xstrdup(bbg.hold_space ? bbg.hold_space : "");
|
||||||
break;
|
break;
|
||||||
case 'G': /* Append newline and hold space to pattern space */
|
case 'G': /* Append newline and hold space to pattern space */
|
||||||
{
|
{
|
||||||
@ -1016,42 +1017,46 @@ restart:
|
|||||||
|
|
||||||
if (pattern_space)
|
if (pattern_space)
|
||||||
pattern_space_size += strlen(pattern_space);
|
pattern_space_size += strlen(pattern_space);
|
||||||
if (hold_space) hold_space_size = strlen(hold_space);
|
if (bbg.hold_space)
|
||||||
pattern_space = xrealloc(pattern_space, pattern_space_size + hold_space_size);
|
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;
|
if (pattern_space_size == 2) pattern_space[0]=0;
|
||||||
strcat(pattern_space, "\n");
|
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;
|
no_newline=0;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'h': /* Replace hold space with pattern space */
|
case 'h': /* Replace hold space with pattern space */
|
||||||
free(hold_space);
|
free(bbg.hold_space);
|
||||||
hold_space = strdup(pattern_space);
|
bbg.hold_space = bb_xstrdup(pattern_space);
|
||||||
break;
|
break;
|
||||||
case 'H': /* Append newline and pattern space to hold space */
|
case 'H': /* Append newline and pattern space to hold space */
|
||||||
{
|
{
|
||||||
int hold_space_size = 2;
|
int hold_space_size = 2;
|
||||||
int pattern_space_size = 0;
|
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)
|
if (pattern_space)
|
||||||
pattern_space_size = strlen(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);
|
hold_space_size + pattern_space_size);
|
||||||
|
|
||||||
if (hold_space_size == 2) hold_space[0]=0;
|
if (hold_space_size == 2) *bbg.hold_space=0;
|
||||||
strcat(hold_space, "\n");
|
strcat(bbg.hold_space, "\n");
|
||||||
if (pattern_space) strcat(hold_space, pattern_space);
|
if (pattern_space) strcat(bbg.hold_space, pattern_space);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'x': /* Exchange hold and pattern space */
|
case 'x': /* Exchange hold and pattern space */
|
||||||
{
|
{
|
||||||
char *tmp = pattern_space;
|
char *tmp = pattern_space;
|
||||||
pattern_space = hold_space;
|
pattern_space = bbg.hold_space;
|
||||||
no_newline=0;
|
no_newline=0;
|
||||||
hold_space = tmp;
|
bbg.hold_space = tmp;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1064,7 +1069,7 @@ restart:
|
|||||||
discard_commands:
|
discard_commands:
|
||||||
/* we will print the line unless we were told to be quiet ('-n')
|
/* we will print the line unless we were told to be quiet ('-n')
|
||||||
or if the line was suppressed (ala 'd'elete) */
|
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. */
|
/* Delete and such jump here. */
|
||||||
discard_line:
|
discard_line:
|
||||||
@ -1095,6 +1100,8 @@ int sed_main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
int status = EXIT_SUCCESS, opt, getpat = 1;
|
int status = EXIT_SUCCESS, opt, getpat = 1;
|
||||||
|
|
||||||
|
bbg.sed_cmd_tail=&bbg.sed_cmd_head;
|
||||||
|
|
||||||
/* destroy command strings on exit */
|
/* destroy command strings on exit */
|
||||||
if (ENABLE_FEATURE_CLEAN_UP) atexit(sed_free_and_close_stuff);
|
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) {
|
while ((opt = getopt(argc, argv, "irne:f:")) > 0) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'i':
|
case 'i':
|
||||||
in_place++;
|
bbg.in_place++;
|
||||||
atexit(cleanup_outname);
|
atexit(cleanup_outname);
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
regex_type|=REG_EXTENDED;
|
bbg.regex_type|=REG_EXTENDED;
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
be_quiet++;
|
bbg.be_quiet++;
|
||||||
break;
|
break;
|
||||||
case 'e':
|
case 'e':
|
||||||
add_cmd_block(optarg);
|
add_cmd_block(optarg);
|
||||||
@ -1153,13 +1160,13 @@ int sed_main(int argc, char **argv)
|
|||||||
add_cmd("");
|
add_cmd("");
|
||||||
|
|
||||||
/* By default, we write to stdout */
|
/* By default, we write to stdout */
|
||||||
nonstdout=stdout;
|
bbg.nonstdout=stdout;
|
||||||
|
|
||||||
/* argv[(optind)..(argc-1)] should be names of file to process. If no
|
/* argv[(optind)..(argc-1)] should be names of file to process. If no
|
||||||
* files were specified or '-' was specified, take input from stdin.
|
* files were specified or '-' was specified, take input from stdin.
|
||||||
* Otherwise, we process all the files specified. */
|
* Otherwise, we process all the files specified. */
|
||||||
if (argv[optind] == NULL) {
|
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);
|
add_input_file(stdin);
|
||||||
process_files();
|
process_files();
|
||||||
} else {
|
} else {
|
||||||
@ -1167,21 +1174,21 @@ int sed_main(int argc, char **argv)
|
|||||||
FILE *file;
|
FILE *file;
|
||||||
|
|
||||||
for (i = optind; i < argc; i++) {
|
for (i = optind; i < argc; i++) {
|
||||||
if(!strcmp(argv[i], "-") && !in_place) {
|
if(!strcmp(argv[i], "-") && !bbg.in_place) {
|
||||||
add_input_file(stdin);
|
add_input_file(stdin);
|
||||||
process_files();
|
process_files();
|
||||||
} else {
|
} else {
|
||||||
file = bb_wfopen(argv[i], "r");
|
file = bb_wfopen(argv[i], "r");
|
||||||
if (file) {
|
if (file) {
|
||||||
if(in_place) {
|
if(bbg.in_place) {
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
int nonstdoutfd;
|
int nonstdoutfd;
|
||||||
|
|
||||||
outname=bb_xstrndup(argv[i],strlen(argv[i])+6);
|
bbg.outname=bb_xstrndup(argv[i],strlen(argv[i])+6);
|
||||||
strcat(outname,"XXXXXX");
|
strcat(bbg.outname,"XXXXXX");
|
||||||
if(-1==(nonstdoutfd=mkstemp(outname)))
|
if(-1==(nonstdoutfd=mkstemp(bbg.outname)))
|
||||||
bb_error_msg_and_die("no temp file");
|
bb_error_msg_and_die("no temp file");
|
||||||
nonstdout=fdopen(nonstdoutfd,"w");
|
bbg.nonstdout=fdopen(nonstdoutfd,"w");
|
||||||
|
|
||||||
/* Set permissions of output file */
|
/* Set permissions of output file */
|
||||||
|
|
||||||
@ -1189,20 +1196,20 @@ int sed_main(int argc, char **argv)
|
|||||||
fchmod(nonstdoutfd,statbuf.st_mode);
|
fchmod(nonstdoutfd,statbuf.st_mode);
|
||||||
add_input_file(file);
|
add_input_file(file);
|
||||||
process_files();
|
process_files();
|
||||||
fclose(nonstdout);
|
fclose(bbg.nonstdout);
|
||||||
|
|
||||||
nonstdout=stdout;
|
bbg.nonstdout=stdout;
|
||||||
unlink(argv[i]);
|
unlink(argv[i]);
|
||||||
rename(outname,argv[i]);
|
rename(bbg.outname,argv[i]);
|
||||||
free(outname);
|
free(bbg.outname);
|
||||||
outname=0;
|
bbg.outname=0;
|
||||||
} else add_input_file(file);
|
} else add_input_file(file);
|
||||||
} else {
|
} else {
|
||||||
status = EXIT_FAILURE;
|
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;
|
return status;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user