mirror of
https://github.com/sheumann/hush.git
synced 2024-07-06 19:28:59 +00:00
sed: style fixes
This commit is contained in:
parent
b15b7f7a4a
commit
2f8f71b20d
@ -91,10 +91,9 @@ typedef struct sed_cmd_s {
|
||||
|
||||
static const char *const semicolon_whitespace = "; \n\r\t\v";
|
||||
|
||||
struct sed_globals
|
||||
{
|
||||
struct sed_globals {
|
||||
/* options */
|
||||
int be_quiet, in_place, regex_type;
|
||||
int be_quiet, regex_type;
|
||||
FILE *nonstdout;
|
||||
char *outname, *hold_space;
|
||||
|
||||
@ -123,7 +122,7 @@ struct sed_globals
|
||||
|
||||
void sed_free_and_close_stuff(void);
|
||||
#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;
|
||||
|
||||
@ -209,7 +208,7 @@ static int index_of_next_unescaped_regexp_delim(int delimiter, char *str)
|
||||
|
||||
if (delimiter < 0) {
|
||||
bracket--;
|
||||
delimiter *= -1;
|
||||
delimiter = -delimiter;
|
||||
}
|
||||
|
||||
for (; (ch = str[idx]); idx++) {
|
||||
@ -337,6 +336,7 @@ static int parse_subst_cmd(sed_cmd_t *sed_cmd, char *substr)
|
||||
if (match[0] != '^') {
|
||||
/* Match 0 treated as all, multiple matches we take the last one. */
|
||||
char *pos = substr + idx;
|
||||
/* FIXME: error check? */
|
||||
sed_cmd->which_match = (unsigned short)strtol(substr+idx, &pos, 10);
|
||||
idx = pos - substr;
|
||||
}
|
||||
@ -801,13 +801,14 @@ static void process_files(void)
|
||||
next_line = get_next_line(&next_last_char);
|
||||
|
||||
/* go through every line in each file */
|
||||
for (;;) {
|
||||
again:
|
||||
|
||||
sed_cmd_t *sed_cmd;
|
||||
int substituted = 0;
|
||||
|
||||
/* Advance to next line. Stop if out of lines. */
|
||||
pattern_space = next_line;
|
||||
if (!pattern_space) break;
|
||||
if (!pattern_space) return;
|
||||
last_char = next_last_char;
|
||||
|
||||
/* 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 == -1 ?
|
||||
!next_line
|
||||
: sed_cmd->end_line<=linenum
|
||||
: !sed_cmd->end_match)
|
||||
: (sed_cmd->end_line <= linenum)
|
||||
: !sed_cmd->end_match
|
||||
)
|
||||
/* 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 */
|
||||
case 'N':
|
||||
{
|
||||
int len;
|
||||
/* If no next line, jump to end of script and exit. */
|
||||
if (next_line == NULL) {
|
||||
/* Jump to end of script and exit */
|
||||
@ -1002,16 +1007,14 @@ restart:
|
||||
next_line = NULL;
|
||||
goto discard_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[len] = '\n';
|
||||
strcpy(pattern_space + len+1, next_line);
|
||||
last_char = next_last_char;
|
||||
next_line = get_next_line(&next_last_char);
|
||||
linenum++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@ -1032,11 +1035,9 @@ restart:
|
||||
/* Transliterate characters */
|
||||
case 'y':
|
||||
{
|
||||
int i;
|
||||
int i, j;
|
||||
|
||||
for (i = 0; pattern_space[i]; i++) {
|
||||
int j;
|
||||
|
||||
for (j = 0; sed_cmd->string[j]; j += 2) {
|
||||
if (pattern_space[i] == sed_cmd->string[j]) {
|
||||
pattern_space[i] = sed_cmd->string[j + 1];
|
||||
@ -1119,7 +1120,8 @@ discard_commands:
|
||||
discard_line:
|
||||
flush_append();
|
||||
free(pattern_space);
|
||||
}
|
||||
|
||||
goto again;
|
||||
}
|
||||
|
||||
/* 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)
|
||||
{
|
||||
enum {
|
||||
OPT_in_place = 1 << 0,
|
||||
};
|
||||
unsigned opt;
|
||||
llist_t *opt_e, *opt_f;
|
||||
int status = EXIT_SUCCESS;
|
||||
@ -1176,20 +1181,23 @@ int sed_main(int argc, char **argv)
|
||||
|
||||
/* Lie to autoconf when it starts asking stupid questions. */
|
||||
if (argc == 2 && !strcmp(argv[1], "--version")) {
|
||||
printf("This is not GNU sed version 4.0\n");
|
||||
exit(0);
|
||||
puts("This is not GNU sed version 4.0");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* do normal option parsing */
|
||||
opt_e = opt_f = NULL;
|
||||
opt_complementary = "e::f::"; /* can occur multiple times */
|
||||
opt = getopt32(argc, argv, "irne:f:", &opt_e, &opt_f);
|
||||
if (opt & 0x1) { // -i
|
||||
bbg.in_place++;
|
||||
opt_complementary = "e::f::" /* can occur multiple times */
|
||||
"nn"; /* count -n */
|
||||
opt = getopt32(argc, argv, "irne:f:", &opt_e, &opt_f,
|
||||
&bbg.be_quiet); /* counter for -n */
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
if (opt & OPT_in_place) { // -i
|
||||
atexit(cleanup_outname);
|
||||
}
|
||||
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
|
||||
/* getopt32 reverses order of arguments, handle it */
|
||||
add_cmds_link(opt_e);
|
||||
@ -1198,12 +1206,12 @@ int sed_main(int argc, char **argv)
|
||||
/* getopt32 reverses order of arguments, handle it */
|
||||
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 (argv[optind] == NULL)
|
||||
if (!argc)
|
||||
bb_show_usage();
|
||||
else
|
||||
add_cmd_block(argv[optind++]);
|
||||
add_cmd_block(*argv++);
|
||||
argc--;
|
||||
}
|
||||
/* Flush any unfinished commands. */
|
||||
add_cmd("");
|
||||
@ -1211,11 +1219,11 @@ int sed_main(int argc, char **argv)
|
||||
/* By default, we write to 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.
|
||||
* Otherwise, we process all the files specified. */
|
||||
if (argv[optind] == NULL) {
|
||||
if (bbg.in_place)
|
||||
if (argv[0] == NULL) {
|
||||
if (opt & OPT_in_place)
|
||||
bb_error_msg_and_die(bb_msg_requires_arg, "-i");
|
||||
add_input_file(stdin);
|
||||
process_files();
|
||||
@ -1223,11 +1231,13 @@ int sed_main(int argc, char **argv)
|
||||
int i;
|
||||
FILE *file;
|
||||
|
||||
for (i = optind; i < argc; i++) {
|
||||
for (i = 0; i < argc; i++) {
|
||||
struct stat statbuf;
|
||||
int nonstdoutfd;
|
||||
|
||||
if (!strcmp(argv[i], "-") && !bbg.in_place) {
|
||||
if (argv[i][0] == '-' && !argv[i][1]
|
||||
&& !(opt & OPT_in_place)
|
||||
) {
|
||||
add_input_file(stdin);
|
||||
process_files();
|
||||
continue;
|
||||
@ -1237,7 +1247,7 @@ int sed_main(int argc, char **argv)
|
||||
status = EXIT_FAILURE;
|
||||
continue;
|
||||
}
|
||||
if (!bbg.in_place) {
|
||||
if (!(opt & OPT_in_place)) {
|
||||
add_input_file(file);
|
||||
continue;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user