sed: mostly style fixes, very small changes in actual code

This commit is contained in:
Denis Vlasenko 2006-10-25 12:46:46 +00:00
parent d18a3a20db
commit 8077850330

View File

@ -243,7 +243,7 @@ static int parse_regex_delim(char *cmdstr, char **match, char **replace)
* (typically a 'slash') is now our regexp delimiter... */
if (*cmdstr == '\0')
bb_error_msg_and_die("bad format in substitution expression");
delimiter = *(cmdstr_ptr++);
delimiter = *cmdstr_ptr++;
/* save the match string */
idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
@ -275,11 +275,11 @@ static int get_address(char *my_str, int *linenum, regex_t ** regex)
char delimiter;
char *temp;
if (*my_str == '\\') delimiter = *(++pos);
else delimiter = '/';
delimiter = '/';
if (*my_str == '\\') delimiter = *++pos;
next = index_of_next_unescaped_regexp_delim(delimiter, ++pos);
temp = copy_parsing_escapes(pos,next);
*regex = (regex_t *) xmalloc(sizeof(regex_t));
*regex = xmalloc(sizeof(regex_t));
xregcomp(*regex, temp, bbg.regex_type|REG_NEWLINE);
free(temp);
/* Move position to next character after last delimiter */
@ -297,11 +297,13 @@ static int parse_file_cmd(sed_cmd_t *sed_cmd, char *filecmdstr, char **retval)
while (isspace(filecmdstr[start])) start++;
idx = start;
while (filecmdstr[idx] && filecmdstr[idx] != '\n') idx++;
/* If lines glued together, put backslash back. */
if (filecmdstr[idx] == '\n') hack = 1;
if(idx==start) bb_error_msg_and_die("Empty filename");
if (idx == start)
bb_error_msg_and_die("empty filename");
*retval = xstrndup(filecmdstr+start, idx-start+hack+1);
if(hack) *(idx+*retval)='\\';
if (hack) (*retval)[idx] = '\\';
return idx;
}
@ -365,7 +367,7 @@ static int parse_subst_cmd(sed_cmd_t *sed_cmd, char *substr)
break;
/* Comment */
case '#':
while(substr[++idx]);
while (substr[++idx]) /*skip all*/;
/* Fall through */
/* End of command */
case ';':
@ -393,7 +395,8 @@ out:
static char *parse_cmd_args(sed_cmd_t *sed_cmd, char *cmdstr)
{
/* handle (s)ubstitution command */
if (sed_cmd->cmd == 's') cmdstr += parse_subst_cmd(sed_cmd, cmdstr);
if (sed_cmd->cmd == 's')
cmdstr += parse_subst_cmd(sed_cmd, 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')
@ -403,8 +406,10 @@ static char *parse_cmd_args(sed_cmd_t *sed_cmd, char *cmdstr)
if (*cmdstr == '\n' || *cmdstr == '\\') {
cmdstr++;
break;
} else if(isspace(*cmdstr)) cmdstr++;
else break;
} else if (isspace(*cmdstr))
cmdstr++;
else
break;
}
sed_cmd->string = xstrdup(cmdstr);
parse_escapes(sed_cmd->string,sed_cmd->string,strlen(cmdstr),0,0);
@ -412,7 +417,7 @@ static char *parse_cmd_args(sed_cmd_t *sed_cmd, char *cmdstr)
/* handle file cmds: (r)ead */
} else if (strchr("rw", sed_cmd->cmd)) {
if (sed_cmd->end_line || sed_cmd->end_match)
bb_error_msg_and_die("Command only uses one address");
bb_error_msg_and_die("command only uses one address");
cmdstr += parse_file_cmd(sed_cmd, cmdstr, &sed_cmd->string);
if (sed_cmd->cmd == 'w')
sed_cmd->file = xfopen(sed_cmd->string,"w");
@ -440,7 +445,7 @@ static char *parse_cmd_args(sed_cmd_t *sed_cmd, char *cmdstr)
sed_cmd->string = xzalloc((strlen(match) + 1) * 2);
for (i = 0; match[i] && replace[i]; i++) {
sed_cmd->string[i*2] = match[i];
sed_cmd->string[(i * 2) + 1] = replace[i];
sed_cmd->string[i*2+1] = replace[i];
}
free(match);
free(replace);
@ -449,11 +454,11 @@ static char *parse_cmd_args(sed_cmd_t *sed_cmd, char *cmdstr)
* then it must be an invalid command.
*/
else if (strchr("dDgGhHlnNpPqx={}", sed_cmd->cmd) == 0) {
bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd);
bb_error_msg_and_die("unsupported command %c", sed_cmd->cmd);
}
/* give back whatever's left over */
return (cmdstr);
return cmdstr;
}
@ -474,7 +479,8 @@ static void add_cmd(char *cmdstr)
/* If this line ends with backslash, request next line. */
temp = strlen(cmdstr);
if (temp && cmdstr[temp-1] == '\\') {
if (!bbg.add_cmd_line) bbg.add_cmd_line = xstrdup(cmdstr);
if (!bbg.add_cmd_line)
bbg.add_cmd_line = xstrdup(cmdstr);
bbg.add_cmd_line[temp-1] = 0;
return;
}
@ -490,8 +496,10 @@ static void add_cmd(char *cmdstr)
/* if this is a comment, jump past it and keep going */
if (*cmdstr == '#') {
/* "#n" is the same as using -n on the command line */
if (cmdstr[1] == 'n') bbg.be_quiet++;
if(!(cmdstr=strpbrk(cmdstr, "\n\r"))) break;
if (cmdstr[1] == 'n')
bbg.be_quiet++;
cmdstr = strpbrk(cmdstr, "\n\r");
if (!cmdstr) break;
continue;
}
@ -512,7 +520,8 @@ static void add_cmd(char *cmdstr)
cmdstr++;
idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
if (!idx) bb_error_msg_and_die("no address after comma");
if (!idx)
bb_error_msg_and_die("no address after comma");
cmdstr += idx;
}
@ -529,7 +538,8 @@ static void add_cmd(char *cmdstr)
}
/* last part (mandatory) will be a command */
if (!*cmdstr) bb_error_msg_and_die("missing command");
if (!*cmdstr)
bb_error_msg_and_die("missing command");
sed_cmd->cmd = *(cmdstr++);
cmdstr = parse_cmd_args(sed_cmd, cmdstr);
@ -568,18 +578,22 @@ static void do_subst_w_backrefs(char *line, char *replace)
int backref = replace[++i]-'0';
/* 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 (bbg.regmatch[backref].rm_so != -1) {
j = bbg.regmatch[backref].rm_so;
while (j < bbg.regmatch[backref].rm_eo)
pipe_putc(line[j++]);
}
}
/* if we find a backslash escaped character, print the character */
else if (replace[i] == '\\') pipe_putc(replace[++i]);
/* if we find an unescaped '&' print out the whole matched text. */
else if (replace[i] == '&')
for (j = bbg.regmatch[0].rm_so; j < bbg.regmatch[0].rm_eo; j++)
pipe_putc(line[j]);
else if (replace[i] == '&') {
j = bbg.regmatch[0].rm_so;
while (j < bbg.regmatch[0].rm_eo)
pipe_putc(line[j++]);
}
/* Otherwise just output the character. */
else pipe_putc(replace[i]);
}
@ -597,7 +611,8 @@ static int do_subst_command(sed_cmd_t *sed_cmd, char **line)
current_regex = bbg.previous_regex_ptr;
if (!current_regex)
bb_error_msg_and_die("No previous regexp.");
} else bbg.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, bbg.regmatch, 0))
@ -617,7 +632,7 @@ static int do_subst_command(sed_cmd_t *sed_cmd, char **line)
The match_count check is so not to break
echo "hi" | busybox sed 's/^/!/g' */
if (!bbg.regmatch[0].rm_so && !bbg.regmatch[0].rm_eo && match_count) {
pipe_putc(*(oldline++));
pipe_putc(*oldline++);
continue;
}
@ -627,12 +642,13 @@ static int do_subst_command(sed_cmd_t *sed_cmd, char **line)
end of match and continue */
if (sed_cmd->which_match && sed_cmd->which_match!=match_count) {
for (i = 0; i < bbg.regmatch[0].rm_eo; i++)
pipe_putc(*(oldline++));
pipe_putc(*oldline++);
continue;
}
/* print everything before the match */
for (i = 0; i < bbg.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 */
do_subst_w_backrefs(oldline, sed_cmd->string);
@ -648,7 +664,8 @@ static int do_subst_command(sed_cmd_t *sed_cmd, char **line)
/* Copy rest of string into output pipeline */
while(*oldline) pipe_putc(*(oldline++));
while (*oldline)
pipe_putc(*oldline++);
pipe_putc(0);
free(*line);
@ -662,11 +679,11 @@ static sed_cmd_t *branch_to(char *label)
sed_cmd_t *sed_cmd;
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);
if (sed_cmd->cmd == ':' && sed_cmd->string && !strcmp(sed_cmd->string, label)) {
return sed_cmd;
}
}
bb_error_msg_and_die("Can't find label for jump to `%s'", label);
bb_error_msg_and_die("can't find label for jump to '%s'", label);
}
static void append(char *s)
@ -708,7 +725,8 @@ static char *get_next_line(int *no_newline)
if (!*no_newline) temp[len-1] = 0;
break;
// Close this file and advance to next one
} else fclose(bbg.input_file_list[bbg.current_input_file++]);
} else
fclose(bbg.input_file_list[bbg.current_input_file++]);
}
return temp;
@ -751,7 +769,8 @@ static void process_files(void)
int substituted = 0;
/* Advance to next line. Stop if out of lines. */
if(!(pattern_space=next_line)) break;
pattern_space = next_line;
if (!pattern_space) break;
no_newline = next_no_newline;
/* Read one line in advance so we can act on the last line,
@ -760,8 +779,7 @@ static void process_files(void)
linenum++;
restart:
/* for every line, go through all the commands */
for (sed_cmd = bbg.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;
old_matched = sed_cmd->in_match;
@ -769,20 +787,15 @@ restart:
/* Determine if this command matches this line: */
/* Are we continuing a previous multi-line match? */
sed_cmd->in_match = sed_cmd->in_match
/* Or is no range necessary? */
|| (!sed_cmd->beg_line && !sed_cmd->end_line
&& !sed_cmd->beg_match && !sed_cmd->end_match)
/* Or did we match the start of a numerical range? */
|| (sed_cmd->beg_line > 0 && (sed_cmd->beg_line == linenum))
/* Or does this line match our begin address regex? */
|| (sed_cmd->beg_match &&
!regexec(sed_cmd->beg_match, pattern_space, 0, NULL, 0))
/* Or did we match last line of input? */
|| (sed_cmd->beg_line == -1 && next_line == NULL);
@ -808,8 +821,9 @@ restart:
/* Skip blocks of commands we didn't match. */
if (sed_cmd->cmd == '{') {
if (sed_cmd->invert ? matched : !matched)
while(sed_cmd && sed_cmd->cmd!='}') sed_cmd=sed_cmd->next;
if(!sed_cmd) bb_error_msg_and_die("Unterminated {");
while (sed_cmd && sed_cmd->cmd != '}')
sed_cmd = sed_cmd->next;
if (!sed_cmd) bb_error_msg_and_die("unterminated {");
continue;
}
@ -892,7 +906,8 @@ restart:
/* Cut and paste text (replace) */
case 'c':
/* Only triggers on last line of a matching range. */
if (!sed_cmd->in_match) sed_puts(sed_cmd->string,0);
if (!sed_cmd->in_match)
sed_puts(sed_cmd->string,0);
goto discard_line;
/* Read file, append contents to output */
@ -1009,7 +1024,8 @@ restart:
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");
if (bbg.hold_space)
strcat(pattern_space, bbg.hold_space);
@ -1033,9 +1049,11 @@ restart:
bbg.hold_space = xrealloc(bbg.hold_space,
hold_space_size + pattern_space_size);
if (hold_space_size == 2) *bbg.hold_space=0;
if (hold_space_size == 2)
*bbg.hold_space = 0;
strcat(bbg.hold_space, "\n");
if (pattern_space) strcat(bbg.hold_space, pattern_space);
if (pattern_space)
strcat(bbg.hold_space, pattern_space);
break;
}
@ -1159,7 +1177,8 @@ int sed_main(int argc, char **argv)
* files were specified or '-' was specified, take input from stdin.
* Otherwise, we process all the files specified. */
if (argv[optind] == NULL) {
if(bbg.in_place) bb_error_msg_and_die(bb_msg_requires_arg, "-i");
if (bbg.in_place)
bb_error_msg_and_die(bb_msg_requires_arg, "-i");
add_input_file(stdin);
process_files();
} else {
@ -1185,9 +1204,9 @@ int sed_main(int argc, char **argv)
continue;
}
bbg.outname=xstrndup(argv[i],strlen(argv[i])+6);
strcat(bbg.outname,"XXXXXX");
if(-1==(nonstdoutfd=mkstemp(bbg.outname)))
bbg.outname = xasprintf("%sXXXXXX", argv[i]);
nonstdoutfd = mkstemp(bbg.outname);
if (-1 == nonstdoutfd)
bb_error_msg_and_die("no temp file");
bbg.nonstdout = fdopen(nonstdoutfd,"w");
@ -1200,12 +1219,14 @@ int sed_main(int argc, char **argv)
fclose(bbg.nonstdout);
bbg.nonstdout = stdout;
unlink(argv[i]);
/* unlink(argv[i]); */
// FIXME: error check / message?
rename(bbg.outname,argv[i]);
free(bbg.outname);
bbg.outname = 0;
}
if(bbg.input_file_count>bbg.current_input_file) process_files();
if (bbg.input_file_count > bbg.current_input_file)
process_files();
}
return status;