Fix up indenting
This commit is contained in:
parent
d7fe39b587
commit
d4185b0e15
137
editors/sed.c
137
editors/sed.c
@ -165,7 +165,7 @@ static void destroy_cmd_strs(void)
|
|||||||
* a backslash ('\').
|
* a backslash ('\').
|
||||||
*/
|
*/
|
||||||
static int index_of_next_unescaped_regexp_delim(const char delimiter,
|
static int index_of_next_unescaped_regexp_delim(const char delimiter,
|
||||||
const char *str)
|
const char *str)
|
||||||
{
|
{
|
||||||
int bracket = -1;
|
int bracket = -1;
|
||||||
int escaped = 0;
|
int escaped = 0;
|
||||||
@ -174,8 +174,8 @@ static int index_of_next_unescaped_regexp_delim(const char delimiter,
|
|||||||
|
|
||||||
for (; (ch = str[idx]); idx++) {
|
for (; (ch = str[idx]); idx++) {
|
||||||
if (bracket != -1) {
|
if (bracket != -1) {
|
||||||
if (ch == ']' && !(bracket == idx - 1 ||
|
if (ch == ']' && !(bracket == idx - 1 || (bracket == idx - 2
|
||||||
(bracket == idx - 2 && str[idx - 1] == '^')))
|
&& str[idx - 1] == '^')))
|
||||||
bracket = -1;
|
bracket = -1;
|
||||||
} else if (escaped)
|
} else if (escaped)
|
||||||
escaped = 0;
|
escaped = 0;
|
||||||
@ -392,7 +392,7 @@ static int parse_edit_cmd(sed_cmd_t * sed_cmd, const char *editstr)
|
|||||||
/* store the edit line text */
|
/* store the edit line text */
|
||||||
sed_cmd->editline = xmalloc(strlen(&editstr[2]) + 2);
|
sed_cmd->editline = xmalloc(strlen(&editstr[2]) + 2);
|
||||||
for (i = 2, j = 0;
|
for (i = 2, j = 0;
|
||||||
editstr[i] != '\0' && strchr("\r\n", editstr[i]) == NULL; i++, j++) {
|
editstr[i] != '\0' && strchr("\r\n", editstr[i]) == NULL; i++, j++) {
|
||||||
if ((editstr[i] == '\\') && strchr("\n\r", editstr[i + 1]) != NULL) {
|
if ((editstr[i] == '\\') && strchr("\n\r", editstr[i + 1]) != NULL) {
|
||||||
sed_cmd->editline[j] = '\n';
|
sed_cmd->editline[j] = '\n';
|
||||||
i++;
|
i++;
|
||||||
@ -449,7 +449,7 @@ static int parse_file_cmd(sed_cmd_t * sed_cmd, const char *filecmdstr)
|
|||||||
/*
|
/*
|
||||||
* Process the commands arguments
|
* Process the commands arguments
|
||||||
*/
|
*/
|
||||||
static char *parse_cmd_str(sed_cmd_t *sed_cmd, char *cmdstr)
|
static char *parse_cmd_str(sed_cmd_t * sed_cmd, char *cmdstr)
|
||||||
{
|
{
|
||||||
/* handle (s)ubstitution command */
|
/* handle (s)ubstitution command */
|
||||||
if (sed_cmd->cmd == 's') {
|
if (sed_cmd->cmd == 's') {
|
||||||
@ -536,7 +536,7 @@ static char *add_cmd(sed_cmd_t * sed_cmd, char *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 == 0) {
|
if (idx == 0) {
|
||||||
bb_error_msg_and_die("get_address: no address found in string\n"
|
bb_error_msg_and_die("get_address: no address found in string\n"
|
||||||
"\t(you probably didn't check the string you passed me)");
|
"\t(you probably didn't check the string you passed me)");
|
||||||
}
|
}
|
||||||
cmdstr += idx;
|
cmdstr += idx;
|
||||||
}
|
}
|
||||||
@ -637,8 +637,8 @@ static void load_cmd_file(char *filename)
|
|||||||
while ((line = bb_get_line_from_file(cmdfile)) != NULL) {
|
while ((line = bb_get_line_from_file(cmdfile)) != NULL) {
|
||||||
/* if a line ends with '\' it needs the next line appended to it */
|
/* if a line ends with '\' it needs the next line appended to it */
|
||||||
while (((e = last_char_is(line, '\n')) != NULL)
|
while (((e = last_char_is(line, '\n')) != NULL)
|
||||||
&& (e > line) && (e[-1] == '\\')
|
&& (e > line) && (e[-1] == '\\')
|
||||||
&& ((nextline = bb_get_line_from_file(cmdfile)) != NULL)) {
|
&& ((nextline = bb_get_line_from_file(cmdfile)) != NULL)) {
|
||||||
line = xrealloc(line, (e - line) + 1 + strlen(nextline) + 1);
|
line = xrealloc(line, (e - line) + 1 + strlen(nextline) + 1);
|
||||||
strcat(line, nextline);
|
strcat(line, nextline);
|
||||||
free(nextline);
|
free(nextline);
|
||||||
@ -686,9 +686,7 @@ void pipe_putc(struct pipeline *const pipeline, char c)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
static void print_subst_w_backrefs(const char *line, const char *replace,
|
static void print_subst_w_backrefs(const char *line, const char *replace,
|
||||||
regmatch_t * regmatch,
|
regmatch_t * regmatch, struct pipeline *const pipeline, int matches)
|
||||||
struct pipeline *const pipeline,
|
|
||||||
int matches)
|
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@ -707,7 +705,7 @@ static void print_subst_w_backrefs(const char *line, const char *replace,
|
|||||||
/* print out the text held in regmatch[backref] */
|
/* print out the text held in regmatch[backref] */
|
||||||
if (backref <= matches && regmatch[backref].rm_so != -1)
|
if (backref <= matches && regmatch[backref].rm_so != -1)
|
||||||
for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo;
|
for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo;
|
||||||
j++)
|
j++)
|
||||||
pipeputc(line[j]);
|
pipeputc(line[j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -723,6 +721,7 @@ static void print_subst_w_backrefs(const char *line, const char *replace,
|
|||||||
* purpose...) */
|
* purpose...) */
|
||||||
else if (replace[i] == '&') {
|
else if (replace[i] == '&') {
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
for (j = regmatch[0].rm_so; j < regmatch[0].rm_eo; j++)
|
for (j = regmatch[0].rm_so; j < regmatch[0].rm_eo; j++)
|
||||||
pipeputc(line[j]);
|
pipeputc(line[j]);
|
||||||
}
|
}
|
||||||
@ -766,9 +765,9 @@ 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
|
/* and now, as long as we've got a line to try matching and if we can match
|
||||||
* the search string, we make substitutions */
|
* the search string, we make substitutions */
|
||||||
while ((*hackline || !altered) && (regexec(current_regex, hackline,
|
while ((*hackline || !altered)
|
||||||
sed_cmd->num_backrefs + 1,
|
&& (regexec(current_regex, hackline, sed_cmd->num_backrefs + 1,
|
||||||
regmatch, 0) != REG_NOMATCH)) {
|
regmatch, 0) != REG_NOMATCH)) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* print everything before the match */
|
/* print everything before the match */
|
||||||
@ -776,8 +775,8 @@ static int do_subst_command(sed_cmd_t * sed_cmd, char **line)
|
|||||||
pipeputc(hackline[i]);
|
pipeputc(hackline[i]);
|
||||||
|
|
||||||
/* then print the substitution string */
|
/* then print the substitution string */
|
||||||
print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch,
|
print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch, pipeline,
|
||||||
pipeline, sed_cmd->num_backrefs);
|
sed_cmd->num_backrefs);
|
||||||
|
|
||||||
/* advance past the match */
|
/* advance past the match */
|
||||||
hackline += regmatch[0].rm_eo;
|
hackline += regmatch[0].rm_eo;
|
||||||
@ -805,6 +804,7 @@ static int do_subst_command(sed_cmd_t * sed_cmd, char **line)
|
|||||||
static sed_cmd_t *branch_to(const char *label)
|
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 = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
|
||||||
if ((sed_cmd->label) && (strcmp(sed_cmd->label, label) == 0)) {
|
if ((sed_cmd->label) && (strcmp(sed_cmd->label, label) == 0)) {
|
||||||
break;
|
break;
|
||||||
@ -843,31 +843,26 @@ static void process_file(FILE * file)
|
|||||||
force_print = 0;
|
force_print = 0;
|
||||||
|
|
||||||
/* 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;
|
for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
|
||||||
sed_cmd = sed_cmd->next) {
|
|
||||||
int deleted = 0;
|
int deleted = 0;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* entry point into sedding...
|
* entry point into sedding...
|
||||||
*/
|
*/
|
||||||
int matched = (
|
int matched = (
|
||||||
/* no range necessary */
|
/* no range necessary */
|
||||||
(sed_cmd->beg_line == 0
|
(sed_cmd->beg_line == 0 && sed_cmd->end_line == 0
|
||||||
&& sed_cmd->end_line == 0
|
&& sed_cmd->beg_match == NULL
|
||||||
&& sed_cmd->beg_match == NULL
|
&& sed_cmd->end_match == NULL) ||
|
||||||
&& sed_cmd->end_match == NULL) ||
|
/* this line number is the first address we're looking for */
|
||||||
/* this line number is the first address we're looking for */
|
(sed_cmd->beg_line && (sed_cmd->beg_line == linenum)) ||
|
||||||
(sed_cmd->beg_line
|
/* this line matches our first address regex */
|
||||||
&& (sed_cmd->beg_line == linenum)) ||
|
(sed_cmd->beg_match
|
||||||
/* this line matches our first address regex */
|
&& (regexec(sed_cmd->beg_match, pattern_space, 0, NULL,
|
||||||
(sed_cmd->beg_match
|
0) == 0)) ||
|
||||||
&&
|
/* we are currently within the beginning & ending address range */
|
||||||
(regexec
|
still_in_range || ((sed_cmd->beg_line == -1)
|
||||||
(sed_cmd->beg_match, pattern_space, 0, NULL,
|
&& (next_line == NULL))
|
||||||
0) == 0)) ||
|
|
||||||
/* we are currently within the beginning & ending address range */
|
|
||||||
still_in_range || ((sed_cmd->beg_line == -1)
|
|
||||||
&& (next_line == NULL))
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (sed_cmd->invert ^ matched) {
|
if (sed_cmd->invert ^ matched) {
|
||||||
@ -927,7 +922,7 @@ static void process_file(FILE * file)
|
|||||||
|
|
||||||
pattern_space =
|
pattern_space =
|
||||||
xrealloc(pattern_space,
|
xrealloc(pattern_space,
|
||||||
strlen(pattern_space) + 2);
|
strlen(pattern_space) + 2);
|
||||||
tmp = strchr(pattern_space + offset, '\n');
|
tmp = strchr(pattern_space + offset, '\n');
|
||||||
memmove(tmp + 1, tmp, strlen(tmp) + 1);
|
memmove(tmp + 1, tmp, strlen(tmp) + 1);
|
||||||
tmp[0] = '\\';
|
tmp[0] = '\\';
|
||||||
@ -952,8 +947,7 @@ static void process_file(FILE * file)
|
|||||||
#endif
|
#endif
|
||||||
altered |= substituted;
|
altered |= substituted;
|
||||||
if (!be_quiet && altered && ((sed_cmd->next == NULL)
|
if (!be_quiet && altered && ((sed_cmd->next == NULL)
|
||||||
|| (sed_cmd->next->cmd !=
|
|| (sed_cmd->next->cmd != 's'))) {
|
||||||
's'))) {
|
|
||||||
force_print = 1;
|
force_print = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -979,10 +973,8 @@ static void process_file(FILE * file)
|
|||||||
/* multi-address case */
|
/* multi-address case */
|
||||||
/* - matching text */
|
/* - matching text */
|
||||||
|| (sed_cmd->end_match
|
|| (sed_cmd->end_match
|
||||||
&&
|
&& (regexec(sed_cmd->end_match, pattern_space, 0,
|
||||||
(regexec
|
NULL, 0) == 0))
|
||||||
(sed_cmd->end_match, pattern_space, 0, NULL,
|
|
||||||
0) == 0))
|
|
||||||
/* - matching line numbers */
|
/* - matching line numbers */
|
||||||
|| (sed_cmd->end_line > 0
|
|| (sed_cmd->end_line > 0
|
||||||
&& sed_cmd->end_line == linenum)) {
|
&& sed_cmd->end_line == linenum)) {
|
||||||
@ -994,11 +986,17 @@ static void process_file(FILE * file)
|
|||||||
|
|
||||||
case 'r':{
|
case 'r':{
|
||||||
FILE *outfile;
|
FILE *outfile;
|
||||||
|
|
||||||
outfile = fopen(sed_cmd->filename, "r");
|
outfile = fopen(sed_cmd->filename, "r");
|
||||||
if (outfile) {
|
if (outfile) {
|
||||||
char *line;
|
char *line;
|
||||||
while ((line = bb_get_chomped_line_from_file(outfile)) != NULL) {
|
|
||||||
pattern_space = xrealloc(pattern_space, strlen(line) + strlen(pattern_space) + 2);
|
while ((line =
|
||||||
|
bb_get_chomped_line_from_file(outfile)) !=
|
||||||
|
NULL) {
|
||||||
|
pattern_space =
|
||||||
|
xrealloc(pattern_space,
|
||||||
|
strlen(line) + strlen(pattern_space) + 2);
|
||||||
strcat(pattern_space, "\n");
|
strcat(pattern_space, "\n");
|
||||||
strcat(pattern_space, line);
|
strcat(pattern_space, line);
|
||||||
}
|
}
|
||||||
@ -1023,8 +1021,7 @@ static void process_file(FILE * file)
|
|||||||
if (next_line) {
|
if (next_line) {
|
||||||
pattern_space =
|
pattern_space =
|
||||||
realloc(pattern_space,
|
realloc(pattern_space,
|
||||||
strlen(pattern_space) +
|
strlen(pattern_space) + strlen(next_line) + 2);
|
||||||
strlen(next_line) + 2);
|
|
||||||
strcat(pattern_space, "\n");
|
strcat(pattern_space, "\n");
|
||||||
strcat(pattern_space, next_line);
|
strcat(pattern_space, next_line);
|
||||||
next_line = bb_get_chomped_line_from_file(file);
|
next_line = bb_get_chomped_line_from_file(file);
|
||||||
@ -1077,39 +1074,23 @@ static void process_file(FILE * file)
|
|||||||
*/
|
*/
|
||||||
if (matched) {
|
if (matched) {
|
||||||
if (
|
if (
|
||||||
/* this is a single-address command or... */
|
/* 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 */
|
||||||
/* If only one address */
|
/* we were in the middle of our address range (this
|
||||||
/* we were in the middle of our address range (this
|
* isn't the first time through) and.. */
|
||||||
* isn't the first time through) and.. */
|
|| ((still_in_range == 1)
|
||||||
(still_in_range == 1) && (
|
/* this line number is the last address we're looking for or... */
|
||||||
/* this line number is the last address we're looking for or... */
|
&& ((sed_cmd->end_line
|
||||||
(sed_cmd->
|
&& (sed_cmd->end_line == linenum))
|
||||||
end_line
|
/* this line matches our last address regex */
|
||||||
&& (sed_cmd->
|
|| (sed_cmd->end_match
|
||||||
end_line ==
|
&& (regexec(sed_cmd->end_match, pattern_space,
|
||||||
linenum))
|
0, NULL, 0) == 0))))) {
|
||||||
||
|
|
||||||
/* this line matches our last address regex */
|
|
||||||
(sed_cmd->
|
|
||||||
end_match
|
|
||||||
&&
|
|
||||||
(regexec
|
|
||||||
(sed_cmd->
|
|
||||||
end_match,
|
|
||||||
pattern_space,
|
|
||||||
0, NULL,
|
|
||||||
0) == 0))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
/* we're out of our address range */
|
/* we're out of our address range */
|
||||||
still_in_range = 0;
|
still_in_range = 0;
|
||||||
}
|
} else {
|
||||||
|
/* didn't hit the exit? then we're still in the middle of an address range */
|
||||||
/* didn't hit the exit? then we're still in the middle of an address range */
|
|
||||||
else {
|
|
||||||
still_in_range = 1;
|
still_in_range = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user