Run through indent

This commit is contained in:
Glenn L McGrath 2003-04-08 11:56:11 +00:00
parent c1d95076f0
commit 8d6395d41a

View File

@ -65,7 +65,8 @@ extern char *optarg; /* ditto */
/* options */ /* options */
static int be_quiet = 0; 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 { typedef struct sed_cmd_s {
/* Order by alignment requirements */ /* Order by alignment requirements */
@ -158,7 +159,8 @@ static void destroy_cmd_strs(void)
* expression delimiter (typically a forward * slash ('/')) not preceeded by * expression delimiter (typically a forward * slash ('/')) not preceeded by
* a backslash ('\'). * 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 bracket = -1;
int escaped = 0; 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) static int get_address(char *my_str, int *linenum, regex_t ** regex)
{ {
int idx = 0; int idx = 0;
if (isdigit(my_str[idx])) { if (isdigit(my_str[idx])) {
char *endstr; char *endstr;
*linenum = strtol(my_str, &endstr, 10); *linenum = strtol(my_str, &endstr, 10);
/* endstr shouldnt ever equal NULL */ /* endstr shouldnt ever equal NULL */
idx = endstr - my_str; idx = endstr - my_str;
} } else if (my_str[idx] == '$') {
else if (my_str[idx] == '$') {
*linenum = -1; *linenum = -1;
idx++; idx++;
} } else if (my_str[idx] == '/' || my_str[idx] == '\\') {
else if (my_str[idx] == '/' || my_str[idx] == '\\') {
int idx_start = 1; int idx_start = 1;
char delimiter; 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 */ /* sed_cmd->num_backrefs = 0; *//* XXX: not needed? --apparently not */
for (j = 0; match[j]; j++) { for (j = 0; match[j]; j++) {
/* GNU/POSIX sed does not save more than nine backrefs */ /* 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++; 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 */ /* 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; editstr[i] != '\0' && strchr("\r\n", editstr[i]) == NULL; for (i = 2, j = 0;
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++;
@ -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. */ /* 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 /* the first non-whitespace we get is a filename. the filename ends when we
* hit a normal sed command terminator or end of string */ * 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 */ /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */
else if (strchr("aic", sed_cmd->cmd)) { else if (strchr("aic", sed_cmd->cmd)) {
if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c') 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); cmdstr += parse_edit_cmd(sed_cmd, cmdstr);
} }
/* handle file cmds: (r)ead */ /* 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 */ /* second part (if present) will begin with a comma */
if (*cmdstr == ',') { if (*cmdstr == ',') {
int idx; int idx;
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 == 0) { if (idx == 0) {
@ -552,8 +557,7 @@ static char *add_cmd(sed_cmd_t *sed_cmd, char *cmdstr)
* with <blank>s. * with <blank>s.
*/ */
if (isblank(cmdstr[idx]) { if (isblank(cmdstr[idx]) {
bb_error_msg_and_die("blank follows '!'"); bb_error_msg_and_die("blank follows '!'");}
}
#else #else
/* skip whitespace before the command */ /* skip whitespace before the command */
while (isspace(*cmdstr)) { while (isspace(*cmdstr)) {
@ -613,6 +617,7 @@ static void add_cmd_str(char *cmdstr)
#endif #endif
do { do {
sed_cmd_t *sed_cmd; sed_cmd_t *sed_cmd;
sed_cmd = xcalloc(1, sizeof(sed_cmd_t)); sed_cmd = xcalloc(1, sizeof(sed_cmd_t));
cmdstr = add_cmd(sed_cmd, cmdstr); cmdstr = add_cmd(sed_cmd, cmdstr);
} while (cmdstr && strlen(cmdstr)); } while (cmdstr && strlen(cmdstr));
@ -657,8 +662,7 @@ struct pipeline {
void pipe_putc(struct pipeline *const pipeline, char c) void pipe_putc(struct pipeline *const pipeline, char c)
{ {
if (pipeline->buf[pipeline->idx] == PIPE_MAGIC) { if (pipeline->buf[pipeline->idx] == PIPE_MAGIC) {
pipeline->buf = pipeline->buf = xrealloc(pipeline->buf, pipeline->len + PIPE_GROW);
xrealloc(pipeline->buf, pipeline->len + PIPE_GROW);
memset(pipeline->buf + pipeline->len, 0, PIPE_GROW); memset(pipeline->buf + pipeline->len, 0, PIPE_GROW);
pipeline->len += PIPE_GROW; pipeline->len += PIPE_GROW;
pipeline->buf[pipeline->len - 1] = PIPE_MAGIC; 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) #define pipeputc(c) pipe_putc(pipeline, c)
#if 0 #if 0
{ if (pipeline[pipeline_idx] == PIPE_MAGIC) { \ {
pipeline = xrealloc(pipeline, pipeline_len+PIPE_GROW); \ if (pipeline[pipeline_idx] == PIPE_MAGIC) {
memset(pipeline+pipeline_len, 0, PIPE_GROW); \ pipeline = xrealloc(pipeline, pipeline_len + PIPE_GROW);
pipeline_len += PIPE_GROW; \ memset(pipeline + pipeline_len, 0, PIPE_GROW);
pipeline[pipeline_len-1] = PIPE_MAGIC; } \ pipeline_len += PIPE_GROW;
pipeline[pipeline_idx++] = (c); } pipeline[pipeline_len - 1] = PIPE_MAGIC;
}
pipeline[pipeline_idx++] = (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, struct pipeline *const pipeline, int matches) regmatch_t * regmatch,
struct pipeline *const pipeline,
int matches)
{ {
int i; int i;
@ -689,13 +698,15 @@ static void print_subst_w_backrefs(const char *line, const char *replace,
int j; int j;
char tmpstr[2]; char tmpstr[2];
int backref; int backref;
++i; /* i now indexes the backref number, instead of the leading slash */ ++i; /* i now indexes the backref number, instead of the leading slash */
tmpstr[0] = replace[i]; tmpstr[0] = replace[i];
tmpstr[1] = 0; tmpstr[1] = 0;
backref = atoi(tmpstr); backref = atoi(tmpstr);
/* 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; j++) for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo;
j++)
pipeputc(line[j]); pipeputc(line[j]);
} }
@ -711,6 +722,7 @@ static void print_subst_w_backrefs(const char *line, const char *replace,
* purpose...) */ * purpose...) */
else if (replace[i] == '&' && replace[i - 1] != '\\') { else if (replace[i] == '&' && replace[i - 1] != '\\') {
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]);
} }
@ -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 /* 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) && (regexec(current_regex, hackline,
sed_cmd->num_backrefs+1, regmatch, 0) != REG_NOMATCH) ) { sed_cmd->num_backrefs + 1,
regmatch, 0) != REG_NOMATCH)) {
int i; int i;
/* print everything before the match */ /* print everything before the match */
@ -776,8 +789,10 @@ static int do_subst_command(sed_cmd_t *sed_cmd, char **line)
break; break;
} }
} }
for (; *hackline; hackline++) pipeputc(*hackline); for (; *hackline; hackline++)
if (thepipe.buf[thepipe.idx] == PIPE_MAGIC) thepipe.buf[thepipe.idx] = 0; pipeputc(*hackline);
if (thepipe.buf[thepipe.idx] == PIPE_MAGIC)
thepipe.buf[thepipe.idx] = 0;
/* cleanup */ /* cleanup */
free(regmatch); 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) 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.linear; sed_cmd; sed_cmd = sed_cmd->linear) { for (sed_cmd = sed_cmd_head.linear; sed_cmd; sed_cmd = sed_cmd->linear) {
if (strcmp(sed_cmd->label, label) == 0) { if (strcmp(sed_cmd->label, label) == 0) {
break; break;
@ -828,7 +844,8 @@ 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.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; int deleted = 0;
/* /*
@ -836,15 +853,22 @@ static void process_file(FILE *file)
*/ */
int matched = ( int matched = (
/* no range necessary */ /* no range necessary */
(sed_cmd->beg_line == 0 && sed_cmd->end_line == 0 && (sed_cmd->beg_line == 0
sed_cmd->beg_match == NULL && && sed_cmd->end_line == 0
sed_cmd->end_match == NULL) || && sed_cmd->beg_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
&& (sed_cmd->beg_line == linenum)) ||
/* this line matches our first address regex */ /* 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 */ /* 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) { if (sed_cmd->invert ^ matched) {
@ -860,8 +884,10 @@ static void process_file(FILE *file)
case '=': case '=':
printf("%d\n", linenum); printf("%d\n", linenum);
break; 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'); char *tmp = strchr(pattern_space, '\n');
if (tmp) { if (tmp) {
*tmp = '\0'; *tmp = '\0';
} }
@ -896,9 +922,13 @@ static void process_file(FILE *file)
/* HACK: escape newlines twice so regex can match them */ /* HACK: escape newlines twice so regex can match them */
{ {
int offset = 0; int offset = 0;
while (strchr(pattern_space + offset, '\n') != NULL) { while (strchr(pattern_space + offset, '\n') != NULL) {
char *tmp; 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'); tmp = strchr(pattern_space + offset, '\n');
memmove(tmp + 1, tmp, strlen(tmp) + 1); memmove(tmp + 1, tmp, strlen(tmp) + 1);
tmp[0] = '\\'; tmp[0] = '\\';
@ -922,7 +952,9 @@ static void process_file(FILE *file)
} }
#endif #endif
altered |= substituted; 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; force_print = 1;
} }
@ -947,10 +979,14 @@ static void process_file(FILE *file)
if ((sed_cmd->end_match == NULL && sed_cmd->end_line == 0) if ((sed_cmd->end_match == NULL && sed_cmd->end_line == 0)
/* multi-address case */ /* multi-address case */
/* - matching text */ /* - 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 */ /* - 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); fputs(sed_cmd->editline, stdout);
} }
altered++; altered++;
@ -959,6 +995,7 @@ static void process_file(FILE *file)
case 'r':{ case 'r':{
FILE *outfile; FILE *outfile;
puts(pattern_space); puts(pattern_space);
outfile = fopen(sed_cmd->filename, "r"); outfile = fopen(sed_cmd->filename, "r");
if (outfile) if (outfile)
@ -982,7 +1019,10 @@ static void process_file(FILE *file)
break; break;
case 'N': /* Append the next line to the current line */ case 'N': /* Append the next line to the current line */
if (next_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, "\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);
@ -999,8 +1039,10 @@ static void process_file(FILE *file)
break; break;
case 'y':{ case 'y':{
int i; int i;
for (i = 0; pattern_space[i] != 0; i++) { for (i = 0; pattern_space[i] != 0; i++) {
int j; int j;
for (j = 0; sed_cmd->translate[j]; j += 2) { for (j = 0; sed_cmd->translate[j]; j += 2) {
if (pattern_space[i] == sed_cmd->translate[j]) { if (pattern_space[i] == sed_cmd->translate[j]) {
pattern_space[i] = sed_cmd->translate[j + 1]; pattern_space[i] = sed_cmd->translate[j + 1];
@ -1017,8 +1059,10 @@ static void process_file(FILE *file)
free(hold_space); free(hold_space);
hold_space = strdup(pattern_space); hold_space = strdup(pattern_space);
break; break;
case 'x': { /* Swap hold and pattern space */ case 'x':{
/* Swap hold and pattern space */
char *tmp; char *tmp;
tmp = pattern_space; tmp = pattern_space;
pattern_space = hold_space; pattern_space = hold_space;
hold_space = tmp; hold_space = tmp;
@ -1032,16 +1076,29 @@ 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->end_line == linenum)) || (sed_cmd->
end_line
&& (sed_cmd->
end_line ==
linenum))
||
/* this line matches our last address regex */ /* 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; break;
case 'e':{ case 'e':{
char *str_cmd = strdup(optarg); char *str_cmd = strdup(optarg);
add_cmd_str(str_cmd); add_cmd_str(str_cmd);
free(str_cmd); free(str_cmd);
break; break;
@ -1107,6 +1165,7 @@ extern int sed_main(int argc, char **argv)
bb_show_usage(); bb_show_usage();
else { else {
char *str_cmd = strdup(argv[optind]); char *str_cmd = strdup(argv[optind]);
add_cmd_str(strdup(str_cmd)); add_cmd_str(strdup(str_cmd));
free(str_cmd); free(str_cmd);
optind++; optind++;
@ -1118,10 +1177,10 @@ extern int sed_main(int argc, char **argv)
* Otherwise, we process all the files specified. */ * Otherwise, we process all the files specified. */
if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) { if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) {
process_file(stdin); process_file(stdin);
} } else {
else {
int i; int i;
FILE *file; FILE *file;
for (i = optind; i < argc; i++) { for (i = optind; i < argc; i++) {
file = bb_wfopen(argv[i], "r"); file = bb_wfopen(argv[i], "r");
if (file) { if (file) {