mirror of https://github.com/mirror/busybox.git
Run through indent
parent
c1d95076f0
commit
8d6395d41a
155
editors/sed.c
155
editors/sed.c
|
@ -65,7 +65,8 @@ extern char *optarg; /* ditto */
|
|||
|
||||
/* options */
|
||||
static int be_quiet = 0;
|
||||
static const char bad_format_in_subst[] = "bad format in substitution expression";
|
||||
static const char bad_format_in_subst[] =
|
||||
"bad format in substitution expression";
|
||||
|
||||
typedef struct sed_cmd_s {
|
||||
/* Order by alignment requirements */
|
||||
|
@ -158,7 +159,8 @@ static void destroy_cmd_strs(void)
|
|||
* expression delimiter (typically a forward * slash ('/')) not preceeded by
|
||||
* a backslash ('\').
|
||||
*/
|
||||
static int index_of_next_unescaped_regexp_delim(const char delimiter, const char *str)
|
||||
static int index_of_next_unescaped_regexp_delim(const char delimiter,
|
||||
const char *str)
|
||||
{
|
||||
int bracket = -1;
|
||||
int escaped = 0;
|
||||
|
@ -223,17 +225,17 @@ static int parse_regex_delim(const char *cmdstr, char **match, char **replace)
|
|||
static int get_address(char *my_str, int *linenum, regex_t ** regex)
|
||||
{
|
||||
int idx = 0;
|
||||
|
||||
if (isdigit(my_str[idx])) {
|
||||
char *endstr;
|
||||
|
||||
*linenum = strtol(my_str, &endstr, 10);
|
||||
/* endstr shouldnt ever equal NULL */
|
||||
idx = endstr - my_str;
|
||||
}
|
||||
else if (my_str[idx] == '$') {
|
||||
} else if (my_str[idx] == '$') {
|
||||
*linenum = -1;
|
||||
idx++;
|
||||
}
|
||||
else if (my_str[idx] == '/' || my_str[idx] == '\\') {
|
||||
} else if (my_str[idx] == '/' || my_str[idx] == '\\') {
|
||||
int idx_start = 1;
|
||||
char delimiter;
|
||||
|
||||
|
@ -281,7 +283,8 @@ static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr)
|
|||
/* sed_cmd->num_backrefs = 0; *//* XXX: not needed? --apparently not */
|
||||
for (j = 0; match[j]; j++) {
|
||||
/* GNU/POSIX sed does not save more than nine backrefs */
|
||||
if (match[j] == '\\' && match[j+1] == '(' && sed_cmd->num_backrefs <= 9)
|
||||
if (match[j] == '\\' && match[j + 1] == '('
|
||||
&& sed_cmd->num_backrefs <= 9)
|
||||
sed_cmd->num_backrefs++;
|
||||
}
|
||||
|
||||
|
@ -383,8 +386,8 @@ static int parse_edit_cmd(sed_cmd_t *sed_cmd, const char *editstr)
|
|||
|
||||
/* store the edit line text */
|
||||
sed_cmd->editline = xmalloc(strlen(&editstr[2]) + 2);
|
||||
for (i = 2, j = 0; editstr[i] != '\0' && strchr("\r\n", editstr[i]) == NULL;
|
||||
i++, j++) {
|
||||
for (i = 2, j = 0;
|
||||
editstr[i] != '\0' && strchr("\r\n", editstr[i]) == NULL; i++, j++) {
|
||||
if ((editstr[i] == '\\') && strchr("\n\r", editstr[i + 1]) != NULL) {
|
||||
sed_cmd->editline[j] = '\n';
|
||||
i++;
|
||||
|
@ -427,8 +430,8 @@ static int parse_file_cmd(sed_cmd_t *sed_cmd, const char *filecmdstr)
|
|||
*/
|
||||
|
||||
/* the file command may be followed by whitespace; move past it. */
|
||||
while (isspace(filecmdstr[++idx]))
|
||||
{ ; }
|
||||
while (isspace(filecmdstr[++idx])) {;
|
||||
}
|
||||
|
||||
/* the first non-whitespace we get is a filename. the filename ends when we
|
||||
* hit a normal sed command terminator or end of string */
|
||||
|
@ -451,7 +454,8 @@ static char *parse_cmd_str(sed_cmd_t * const sed_cmd, char *cmdstr)
|
|||
/* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */
|
||||
else if (strchr("aic", sed_cmd->cmd)) {
|
||||
if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
|
||||
bb_error_msg_and_die("only a beginning address can be specified for edit commands");
|
||||
bb_error_msg_and_die
|
||||
("only a beginning address can be specified for edit commands");
|
||||
cmdstr += parse_edit_cmd(sed_cmd, cmdstr);
|
||||
}
|
||||
/* handle file cmds: (r)ead */
|
||||
|
@ -526,6 +530,7 @@ static char *add_cmd(sed_cmd_t *sed_cmd, char *cmdstr)
|
|||
/* second part (if present) will begin with a comma */
|
||||
if (*cmdstr == ',') {
|
||||
int idx;
|
||||
|
||||
cmdstr++;
|
||||
idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
|
||||
if (idx == 0) {
|
||||
|
@ -552,8 +557,7 @@ static char *add_cmd(sed_cmd_t *sed_cmd, char *cmdstr)
|
|||
* with <blank>s.
|
||||
*/
|
||||
if (isblank(cmdstr[idx]) {
|
||||
bb_error_msg_and_die("blank follows '!'");
|
||||
}
|
||||
bb_error_msg_and_die("blank follows '!'");}
|
||||
#else
|
||||
/* skip whitespace before the command */
|
||||
while (isspace(*cmdstr)) {
|
||||
|
@ -613,6 +617,7 @@ static void add_cmd_str(char *cmdstr)
|
|||
#endif
|
||||
do {
|
||||
sed_cmd_t *sed_cmd;
|
||||
|
||||
sed_cmd = xcalloc(1, sizeof(sed_cmd_t));
|
||||
cmdstr = add_cmd(sed_cmd, cmdstr);
|
||||
} while (cmdstr && strlen(cmdstr));
|
||||
|
@ -657,8 +662,7 @@ struct pipeline {
|
|||
void pipe_putc(struct pipeline *const pipeline, char c)
|
||||
{
|
||||
if (pipeline->buf[pipeline->idx] == PIPE_MAGIC) {
|
||||
pipeline->buf =
|
||||
xrealloc(pipeline->buf, pipeline->len + PIPE_GROW);
|
||||
pipeline->buf = xrealloc(pipeline->buf, pipeline->len + PIPE_GROW);
|
||||
memset(pipeline->buf + pipeline->len, 0, PIPE_GROW);
|
||||
pipeline->len += PIPE_GROW;
|
||||
pipeline->buf[pipeline->len - 1] = PIPE_MAGIC;
|
||||
|
@ -669,16 +673,21 @@ void pipe_putc(struct pipeline *const pipeline, char c)
|
|||
#define pipeputc(c) pipe_putc(pipeline, c)
|
||||
|
||||
#if 0
|
||||
{ if (pipeline[pipeline_idx] == PIPE_MAGIC) { \
|
||||
pipeline = xrealloc(pipeline, pipeline_len+PIPE_GROW); \
|
||||
memset(pipeline+pipeline_len, 0, PIPE_GROW); \
|
||||
pipeline_len += PIPE_GROW; \
|
||||
pipeline[pipeline_len-1] = PIPE_MAGIC; } \
|
||||
pipeline[pipeline_idx++] = (c); }
|
||||
{
|
||||
if (pipeline[pipeline_idx] == PIPE_MAGIC) {
|
||||
pipeline = xrealloc(pipeline, pipeline_len + PIPE_GROW);
|
||||
memset(pipeline + pipeline_len, 0, PIPE_GROW);
|
||||
pipeline_len += PIPE_GROW;
|
||||
pipeline[pipeline_len - 1] = PIPE_MAGIC;
|
||||
}
|
||||
pipeline[pipeline_idx++] = (c);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void print_subst_w_backrefs(const char *line, const char *replace,
|
||||
regmatch_t *regmatch, struct pipeline *const pipeline, int matches)
|
||||
regmatch_t * regmatch,
|
||||
struct pipeline *const pipeline,
|
||||
int matches)
|
||||
{
|
||||
int i;
|
||||
|
||||
|
@ -689,13 +698,15 @@ static void print_subst_w_backrefs(const char *line, const char *replace,
|
|||
int j;
|
||||
char tmpstr[2];
|
||||
int backref;
|
||||
|
||||
++i; /* i now indexes the backref number, instead of the leading slash */
|
||||
tmpstr[0] = replace[i];
|
||||
tmpstr[1] = 0;
|
||||
backref = atoi(tmpstr);
|
||||
/* print out the text held in regmatch[backref] */
|
||||
if (backref <= matches && regmatch[backref].rm_so != -1)
|
||||
for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++)
|
||||
for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo;
|
||||
j++)
|
||||
pipeputc(line[j]);
|
||||
}
|
||||
|
||||
|
@ -711,6 +722,7 @@ static void print_subst_w_backrefs(const char *line, const char *replace,
|
|||
* purpose...) */
|
||||
else if (replace[i] == '&' && replace[i - 1] != '\\') {
|
||||
int j;
|
||||
|
||||
for (j = regmatch[0].rm_so; j < regmatch[0].rm_eo; j++)
|
||||
pipeputc(line[j]);
|
||||
}
|
||||
|
@ -755,7 +767,8 @@ static int do_subst_command(sed_cmd_t *sed_cmd, char **line)
|
|||
/* and now, as long as we've got a line to try matching and if we can match
|
||||
* the search string, we make substitutions */
|
||||
while ((*hackline || !altered) && (regexec(current_regex, hackline,
|
||||
sed_cmd->num_backrefs+1, regmatch, 0) != REG_NOMATCH) ) {
|
||||
sed_cmd->num_backrefs + 1,
|
||||
regmatch, 0) != REG_NOMATCH)) {
|
||||
int i;
|
||||
|
||||
/* print everything before the match */
|
||||
|
@ -776,8 +789,10 @@ static int do_subst_command(sed_cmd_t *sed_cmd, char **line)
|
|||
break;
|
||||
}
|
||||
}
|
||||
for (; *hackline; hackline++) pipeputc(*hackline);
|
||||
if (thepipe.buf[thepipe.idx] == PIPE_MAGIC) thepipe.buf[thepipe.idx] = 0;
|
||||
for (; *hackline; hackline++)
|
||||
pipeputc(*hackline);
|
||||
if (thepipe.buf[thepipe.idx] == PIPE_MAGIC)
|
||||
thepipe.buf[thepipe.idx] = 0;
|
||||
|
||||
/* cleanup */
|
||||
free(regmatch);
|
||||
|
@ -790,6 +805,7 @@ static int do_subst_command(sed_cmd_t *sed_cmd, char **line)
|
|||
static sed_cmd_t *branch_to(const char *label)
|
||||
{
|
||||
sed_cmd_t *sed_cmd;
|
||||
|
||||
for (sed_cmd = sed_cmd_head.linear; sed_cmd; sed_cmd = sed_cmd->linear) {
|
||||
if (strcmp(sed_cmd->label, label) == 0) {
|
||||
break;
|
||||
|
@ -828,7 +844,8 @@ static void process_file(FILE *file)
|
|||
force_print = 0;
|
||||
|
||||
/* for every line, go through all the commands */
|
||||
for (sed_cmd = sed_cmd_head.linear; sed_cmd; sed_cmd = sed_cmd->linear) {
|
||||
for (sed_cmd = sed_cmd_head.linear; sed_cmd;
|
||||
sed_cmd = sed_cmd->linear) {
|
||||
int deleted = 0;
|
||||
|
||||
/*
|
||||
|
@ -836,15 +853,22 @@ static void process_file(FILE *file)
|
|||
*/
|
||||
int matched = (
|
||||
/* no range necessary */
|
||||
(sed_cmd->beg_line == 0 && sed_cmd->end_line == 0 &&
|
||||
sed_cmd->beg_match == NULL &&
|
||||
sed_cmd->end_match == NULL) ||
|
||||
(sed_cmd->beg_line == 0
|
||||
&& sed_cmd->end_line == 0
|
||||
&& sed_cmd->beg_match == NULL
|
||||
&& sed_cmd->end_match == NULL) ||
|
||||
/* this line number is the first address we're looking for */
|
||||
(sed_cmd->beg_line && (sed_cmd->beg_line == linenum)) ||
|
||||
(sed_cmd->beg_line
|
||||
&& (sed_cmd->beg_line == linenum)) ||
|
||||
/* this line matches our first address regex */
|
||||
(sed_cmd->beg_match && (regexec(sed_cmd->beg_match, pattern_space, 0, NULL, 0) == 0)) ||
|
||||
(sed_cmd->beg_match
|
||||
&&
|
||||
(regexec
|
||||
(sed_cmd->beg_match, pattern_space, 0, NULL,
|
||||
0) == 0)) ||
|
||||
/* we are currently within the beginning & ending address range */
|
||||
still_in_range || ((sed_cmd->beg_line == -1) && (next_line == NULL))
|
||||
still_in_range || ((sed_cmd->beg_line == -1)
|
||||
&& (next_line == NULL))
|
||||
);
|
||||
|
||||
if (sed_cmd->invert ^ matched) {
|
||||
|
@ -860,8 +884,10 @@ static void process_file(FILE *file)
|
|||
case '=':
|
||||
printf("%d\n", linenum);
|
||||
break;
|
||||
case 'P': { /* Write the current pattern space upto the first newline */
|
||||
case 'P':{
|
||||
/* Write the current pattern space upto the first newline */
|
||||
char *tmp = strchr(pattern_space, '\n');
|
||||
|
||||
if (tmp) {
|
||||
*tmp = '\0';
|
||||
}
|
||||
|
@ -896,9 +922,13 @@ static void process_file(FILE *file)
|
|||
/* HACK: escape newlines twice so regex can match them */
|
||||
{
|
||||
int offset = 0;
|
||||
|
||||
while (strchr(pattern_space + offset, '\n') != NULL) {
|
||||
char *tmp;
|
||||
pattern_space = xrealloc(pattern_space, strlen(pattern_space) + 2);
|
||||
|
||||
pattern_space =
|
||||
xrealloc(pattern_space,
|
||||
strlen(pattern_space) + 2);
|
||||
tmp = strchr(pattern_space + offset, '\n');
|
||||
memmove(tmp + 1, tmp, strlen(tmp) + 1);
|
||||
tmp[0] = '\\';
|
||||
|
@ -922,7 +952,9 @@ static void process_file(FILE *file)
|
|||
}
|
||||
#endif
|
||||
altered |= substituted;
|
||||
if (!be_quiet && altered && ((sed_cmd->linear == NULL) || (sed_cmd->linear->cmd != 's'))) {
|
||||
if (!be_quiet && altered && ((sed_cmd->linear == NULL)
|
||||
|| (sed_cmd->linear->cmd !=
|
||||
's'))) {
|
||||
force_print = 1;
|
||||
}
|
||||
|
||||
|
@ -947,10 +979,14 @@ static void process_file(FILE *file)
|
|||
if ((sed_cmd->end_match == NULL && sed_cmd->end_line == 0)
|
||||
/* multi-address case */
|
||||
/* - matching text */
|
||||
|| (sed_cmd->end_match && (regexec(sed_cmd->end_match, pattern_space, 0, NULL, 0) == 0))
|
||||
|| (sed_cmd->end_match
|
||||
&&
|
||||
(regexec
|
||||
(sed_cmd->end_match, pattern_space, 0, NULL,
|
||||
0) == 0))
|
||||
/* - matching line numbers */
|
||||
|| (sed_cmd->end_line > 0 && sed_cmd->end_line == linenum))
|
||||
{
|
||||
|| (sed_cmd->end_line > 0
|
||||
&& sed_cmd->end_line == linenum)) {
|
||||
fputs(sed_cmd->editline, stdout);
|
||||
}
|
||||
altered++;
|
||||
|
@ -959,6 +995,7 @@ static void process_file(FILE *file)
|
|||
|
||||
case 'r':{
|
||||
FILE *outfile;
|
||||
|
||||
puts(pattern_space);
|
||||
outfile = fopen(sed_cmd->filename, "r");
|
||||
if (outfile)
|
||||
|
@ -982,7 +1019,10 @@ static void process_file(FILE *file)
|
|||
break;
|
||||
case 'N': /* Append the next line to the current line */
|
||||
if (next_line) {
|
||||
pattern_space = realloc(pattern_space, strlen(pattern_space) + strlen(next_line) + 2);
|
||||
pattern_space =
|
||||
realloc(pattern_space,
|
||||
strlen(pattern_space) +
|
||||
strlen(next_line) + 2);
|
||||
strcat(pattern_space, "\n");
|
||||
strcat(pattern_space, next_line);
|
||||
next_line = bb_get_chomped_line_from_file(file);
|
||||
|
@ -999,8 +1039,10 @@ static void process_file(FILE *file)
|
|||
break;
|
||||
case 'y':{
|
||||
int i;
|
||||
|
||||
for (i = 0; pattern_space[i] != 0; i++) {
|
||||
int j;
|
||||
|
||||
for (j = 0; sed_cmd->translate[j]; j += 2) {
|
||||
if (pattern_space[i] == sed_cmd->translate[j]) {
|
||||
pattern_space[i] = sed_cmd->translate[j + 1];
|
||||
|
@ -1017,8 +1059,10 @@ static void process_file(FILE *file)
|
|||
free(hold_space);
|
||||
hold_space = strdup(pattern_space);
|
||||
break;
|
||||
case 'x': { /* Swap hold and pattern space */
|
||||
case 'x':{
|
||||
/* Swap hold and pattern space */
|
||||
char *tmp;
|
||||
|
||||
tmp = pattern_space;
|
||||
pattern_space = hold_space;
|
||||
hold_space = tmp;
|
||||
|
@ -1032,16 +1076,29 @@ static void process_file(FILE *file)
|
|||
if (matched) {
|
||||
if (
|
||||
/* this is a single-address command or... */
|
||||
(sed_cmd->end_line == 0 && sed_cmd->end_match == NULL) || (
|
||||
(sed_cmd->end_line == 0 && sed_cmd->end_match == NULL)
|
||||
|| (
|
||||
/* If only one address */
|
||||
|
||||
/* we were in the middle of our address range (this
|
||||
* isn't the first time through) and.. */
|
||||
(still_in_range == 1) && (
|
||||
/* this line number is the last address we're looking for or... */
|
||||
(sed_cmd->end_line && (sed_cmd->end_line == linenum)) ||
|
||||
(sed_cmd->
|
||||
end_line
|
||||
&& (sed_cmd->
|
||||
end_line ==
|
||||
linenum))
|
||||
||
|
||||
/* this line matches our last address regex */
|
||||
(sed_cmd->end_match && (regexec(sed_cmd->end_match, pattern_space, 0, NULL, 0) == 0))
|
||||
(sed_cmd->
|
||||
end_match
|
||||
&&
|
||||
(regexec
|
||||
(sed_cmd->
|
||||
end_match,
|
||||
pattern_space,
|
||||
0, NULL,
|
||||
0) == 0))
|
||||
)
|
||||
)
|
||||
) {
|
||||
|
@ -1088,6 +1145,7 @@ extern int sed_main(int argc, char **argv)
|
|||
break;
|
||||
case 'e':{
|
||||
char *str_cmd = strdup(optarg);
|
||||
|
||||
add_cmd_str(str_cmd);
|
||||
free(str_cmd);
|
||||
break;
|
||||
|
@ -1107,6 +1165,7 @@ extern int sed_main(int argc, char **argv)
|
|||
bb_show_usage();
|
||||
else {
|
||||
char *str_cmd = strdup(argv[optind]);
|
||||
|
||||
add_cmd_str(strdup(str_cmd));
|
||||
free(str_cmd);
|
||||
optind++;
|
||||
|
@ -1118,10 +1177,10 @@ extern int sed_main(int argc, char **argv)
|
|||
* Otherwise, we process all the files specified. */
|
||||
if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) {
|
||||
process_file(stdin);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
int i;
|
||||
FILE *file;
|
||||
|
||||
for (i = optind; i < argc; i++) {
|
||||
file = bb_wfopen(argv[i], "r");
|
||||
if (file) {
|
||||
|
|
Loading…
Reference in New Issue