sed: style fixes

This commit is contained in:
Denis Vlasenko 2006-12-10 02:09:12 +00:00
parent b15b7f7a4a
commit 2f8f71b20d

View File

@ -91,10 +91,9 @@ typedef struct sed_cmd_s {
static const char *const semicolon_whitespace = "; \n\r\t\v"; static const char *const semicolon_whitespace = "; \n\r\t\v";
struct sed_globals struct sed_globals {
{
/* options */ /* options */
int be_quiet, in_place, regex_type; int be_quiet, regex_type;
FILE *nonstdout; FILE *nonstdout;
char *outname, *hold_space; char *outname, *hold_space;
@ -123,7 +122,7 @@ struct sed_globals
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) static void sed_free_and_close_stuff(void)
{ {
sed_cmd_t *sed_cmd = bbg.sed_cmd_head.next; sed_cmd_t *sed_cmd = bbg.sed_cmd_head.next;
@ -209,7 +208,7 @@ static int index_of_next_unescaped_regexp_delim(int delimiter, char *str)
if (delimiter < 0) { if (delimiter < 0) {
bracket--; bracket--;
delimiter *= -1; delimiter = -delimiter;
} }
for (; (ch = str[idx]); idx++) { for (; (ch = str[idx]); idx++) {
@ -337,6 +336,7 @@ static int parse_subst_cmd(sed_cmd_t *sed_cmd, char *substr)
if (match[0] != '^') { if (match[0] != '^') {
/* Match 0 treated as all, multiple matches we take the last one. */ /* Match 0 treated as all, multiple matches we take the last one. */
char *pos = substr + idx; char *pos = substr + idx;
/* FIXME: error check? */
sed_cmd->which_match = (unsigned short)strtol(substr+idx, &pos, 10); sed_cmd->which_match = (unsigned short)strtol(substr+idx, &pos, 10);
idx = pos - substr; idx = pos - substr;
} }
@ -801,13 +801,14 @@ static void process_files(void)
next_line = get_next_line(&next_last_char); next_line = get_next_line(&next_last_char);
/* go through every line in each file */ /* go through every line in each file */
for (;;) { again:
sed_cmd_t *sed_cmd; sed_cmd_t *sed_cmd;
int substituted = 0; int substituted = 0;
/* Advance to next line. Stop if out of lines. */ /* Advance to next line. Stop if out of lines. */
pattern_space = next_line; pattern_space = next_line;
if (!pattern_space) break; if (!pattern_space) return;
last_char = next_last_char; last_char = next_last_char;
/* Read one line in advance so we can act on the last line, /* Read one line in advance so we can act on the last line,
@ -848,10 +849,13 @@ restart:
(sed_cmd->end_line ? (sed_cmd->end_line ?
sed_cmd->end_line == -1 ? sed_cmd->end_line == -1 ?
!next_line !next_line
: sed_cmd->end_line<=linenum : (sed_cmd->end_line <= linenum)
: !sed_cmd->end_match) : !sed_cmd->end_match
)
/* or does this line matches our last address regex */ /* or does this line matches our last address regex */
|| (sed_cmd->end_match && old_matched && (regexec(sed_cmd->end_match, pattern_space, 0, NULL, 0) == 0)) || (sed_cmd->end_match && old_matched
&& (regexec(sed_cmd->end_match,
pattern_space, 0, NULL, 0) == 0))
); );
} }
@ -995,6 +999,7 @@ restart:
/* Append the next line to the current line */ /* Append the next line to the current line */
case 'N': case 'N':
{ {
int len;
/* If no next line, jump to end of script and exit. */ /* If no next line, jump to end of script and exit. */
if (next_line == NULL) { if (next_line == NULL) {
/* Jump to end of script and exit */ /* Jump to end of script and exit */
@ -1002,16 +1007,14 @@ restart:
next_line = NULL; next_line = NULL;
goto discard_line; goto discard_line;
/* append next_line, read new next_line. */ /* append next_line, read new next_line. */
} else { }
int len = strlen(pattern_space); len = strlen(pattern_space);
pattern_space = realloc(pattern_space, len + strlen(next_line) + 2); pattern_space = realloc(pattern_space, len + strlen(next_line) + 2);
pattern_space[len] = '\n'; pattern_space[len] = '\n';
strcpy(pattern_space + len+1, next_line); strcpy(pattern_space + len+1, next_line);
last_char = next_last_char; last_char = next_last_char;
next_line = get_next_line(&next_last_char); next_line = get_next_line(&next_last_char);
linenum++; linenum++;
}
break; break;
} }
@ -1032,11 +1035,9 @@ restart:
/* Transliterate characters */ /* Transliterate characters */
case 'y': case 'y':
{ {
int i; int i, j;
for (i = 0; pattern_space[i]; i++) { for (i = 0; pattern_space[i]; i++) {
int j;
for (j = 0; sed_cmd->string[j]; j += 2) { for (j = 0; sed_cmd->string[j]; j += 2) {
if (pattern_space[i] == sed_cmd->string[j]) { if (pattern_space[i] == sed_cmd->string[j]) {
pattern_space[i] = sed_cmd->string[j + 1]; pattern_space[i] = sed_cmd->string[j + 1];
@ -1119,7 +1120,8 @@ discard_commands:
discard_line: discard_line:
flush_append(); flush_append();
free(pattern_space); free(pattern_space);
}
goto again;
} }
/* It is possible to have a command line argument with embedded /* It is possible to have a command line argument with embedded
@ -1165,6 +1167,9 @@ static void add_files_link(llist_t *opt_f)
int sed_main(int argc, char **argv) int sed_main(int argc, char **argv)
{ {
enum {
OPT_in_place = 1 << 0,
};
unsigned opt; unsigned opt;
llist_t *opt_e, *opt_f; llist_t *opt_e, *opt_f;
int status = EXIT_SUCCESS; int status = EXIT_SUCCESS;
@ -1176,20 +1181,23 @@ int sed_main(int argc, char **argv)
/* Lie to autoconf when it starts asking stupid questions. */ /* Lie to autoconf when it starts asking stupid questions. */
if (argc == 2 && !strcmp(argv[1], "--version")) { if (argc == 2 && !strcmp(argv[1], "--version")) {
printf("This is not GNU sed version 4.0\n"); puts("This is not GNU sed version 4.0");
exit(0); return 0;
} }
/* do normal option parsing */ /* do normal option parsing */
opt_e = opt_f = NULL; opt_e = opt_f = NULL;
opt_complementary = "e::f::"; /* can occur multiple times */ opt_complementary = "e::f::" /* can occur multiple times */
opt = getopt32(argc, argv, "irne:f:", &opt_e, &opt_f); "nn"; /* count -n */
if (opt & 0x1) { // -i opt = getopt32(argc, argv, "irne:f:", &opt_e, &opt_f,
bbg.in_place++; &bbg.be_quiet); /* counter for -n */
argc -= optind;
argv += optind;
if (opt & OPT_in_place) { // -i
atexit(cleanup_outname); atexit(cleanup_outname);
} }
if (opt & 0x2) bbg.regex_type |= REG_EXTENDED; // -r if (opt & 0x2) bbg.regex_type |= REG_EXTENDED; // -r
if (opt & 0x4) bbg.be_quiet++; // -n //if (opt & 0x4) bbg.be_quiet++; // -n
if (opt & 0x8) { // -e if (opt & 0x8) { // -e
/* getopt32 reverses order of arguments, handle it */ /* getopt32 reverses order of arguments, handle it */
add_cmds_link(opt_e); add_cmds_link(opt_e);
@ -1198,12 +1206,12 @@ int sed_main(int argc, char **argv)
/* getopt32 reverses order of arguments, handle it */ /* getopt32 reverses order of arguments, handle it */
add_files_link(opt_f); add_files_link(opt_f);
} }
/* if we didn't get a pattern from -e or -f, use argv[optind] */ /* if we didn't get a pattern from -e or -f, use argv[0] */
if (!(opt & 0x18)) { if (!(opt & 0x18)) {
if (argv[optind] == NULL) if (!argc)
bb_show_usage(); bb_show_usage();
else add_cmd_block(*argv++);
add_cmd_block(argv[optind++]); argc--;
} }
/* Flush any unfinished commands. */ /* Flush any unfinished commands. */
add_cmd(""); add_cmd("");
@ -1211,11 +1219,11 @@ int sed_main(int argc, char **argv)
/* By default, we write to stdout */ /* By default, we write to stdout */
bbg.nonstdout = stdout; bbg.nonstdout = stdout;
/* argv[(optind)..(argc-1)] should be names of file to process. If no /* argv[0..(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[0] == NULL) {
if (bbg.in_place) if (opt & OPT_in_place)
bb_error_msg_and_die(bb_msg_requires_arg, "-i"); bb_error_msg_and_die(bb_msg_requires_arg, "-i");
add_input_file(stdin); add_input_file(stdin);
process_files(); process_files();
@ -1223,11 +1231,13 @@ int sed_main(int argc, char **argv)
int i; int i;
FILE *file; FILE *file;
for (i = optind; i < argc; i++) { for (i = 0; i < argc; i++) {
struct stat statbuf; struct stat statbuf;
int nonstdoutfd; int nonstdoutfd;
if (!strcmp(argv[i], "-") && !bbg.in_place) { if (argv[i][0] == '-' && !argv[i][1]
&& !(opt & OPT_in_place)
) {
add_input_file(stdin); add_input_file(stdin);
process_files(); process_files();
continue; continue;
@ -1237,7 +1247,7 @@ int sed_main(int argc, char **argv)
status = EXIT_FAILURE; status = EXIT_FAILURE;
continue; continue;
} }
if (!bbg.in_place) { if (!(opt & OPT_in_place)) {
add_input_file(file); add_input_file(file);
continue; continue;
} }