use skip_whitespace where appropriate

This commit is contained in:
Denis Vlasenko 2006-10-25 12:46:03 +00:00
parent c8400a2162
commit d18a3a20db
8 changed files with 98 additions and 105 deletions

View File

@ -1470,7 +1470,7 @@ static int awk_split(char *s, node *spl, char **slist)
} }
} else { /* space split */ } else { /* space split */
while (*s) { while (*s) {
while (isspace(*s)) s++; s = skip_whitespace(s);
if (! *s) break; if (! *s) break;
n++; n++;
while (*s && !isspace(*s)) while (*s && !isspace(*s))

View File

@ -63,29 +63,29 @@
/* Each sed command turns into one of these structures. */ /* Each sed command turns into one of these structures. */
typedef struct sed_cmd_s { typedef struct sed_cmd_s {
/* Ordered by alignment requirements: currently 36 bytes on x86 */ /* Ordered by alignment requirements: currently 36 bytes on x86 */
/* address storage */ /* address storage */
regex_t *beg_match; /* sed -e '/match/cmd' */ regex_t *beg_match; /* sed -e '/match/cmd' */
regex_t *end_match; /* sed -e '/match/,/end_match/cmd' */ regex_t *end_match; /* sed -e '/match/,/end_match/cmd' */
regex_t *sub_match; /* For 's/sub_match/string/' */ regex_t *sub_match; /* For 's/sub_match/string/' */
int beg_line; /* 'sed 1p' 0 == apply commands to all lines */ int beg_line; /* 'sed 1p' 0 == apply commands to all lines */
int end_line; /* 'sed 1,3p' 0 == one line only. -1 = last line ($) */ int end_line; /* 'sed 1,3p' 0 == one line only. -1 = last line ($) */
FILE *file; /* File (sw) command writes to, -1 for none. */ FILE *file; /* File (sw) command writes to, -1 for none. */
char *string; /* Data string for (saicytb) commands. */ char *string; /* Data string for (saicytb) commands. */
unsigned short which_match; /* (s) Which match to replace (0 for all) */ unsigned short which_match; /* (s) Which match to replace (0 for all) */
/* Bitfields (gcc won't group them if we don't) */ /* Bitfields (gcc won't group them if we don't) */
unsigned int invert:1; /* the '!' after the address */ unsigned int invert:1; /* the '!' after the address */
unsigned int in_match:1; /* Next line also included in match? */ unsigned int in_match:1; /* Next line also included in match? */
unsigned int no_newline:1; /* Last line written by (sw) had no '\n' */ unsigned int no_newline:1; /* Last line written by (sw) had no '\n' */
unsigned int sub_p:1; /* (s) print option */ unsigned int sub_p:1; /* (s) print option */
/* GENERAL FIELDS */ /* GENERAL FIELDS */
char cmd; /* The command char: abcdDgGhHilnNpPqrstwxy:={} */ char cmd; /* The command char: abcdDgGhHilnNpPqrstwxy:={} */
struct sed_cmd_s *next; /* Next command (linked list, NULL terminated) */ struct sed_cmd_s *next; /* Next command (linked list, NULL terminated) */
} sed_cmd_t; } sed_cmd_t;
static const char *const semicolon_whitespace = "; \n\r\t\v"; static const char *const semicolon_whitespace = "; \n\r\t\v";
@ -162,7 +162,7 @@ void sed_free_and_close_stuff(void)
static void cleanup_outname(void) static void cleanup_outname(void)
{ {
if(bbg.outname) unlink(bbg.outname); if(bbg.outname) unlink(bbg.outname);
} }
/* strdup, replacing "\n" with '\n', and "\delimiter" with 'delimiter' */ /* strdup, replacing "\n" with '\n', and "\delimiter" with 'delimiter' */
@ -343,36 +343,36 @@ static int parse_subst_cmd(sed_cmd_t *sed_cmd, char *substr)
if(isspace(substr[idx])) continue; if(isspace(substr[idx])) continue;
switch (substr[idx]) { switch (substr[idx]) {
/* Replace all occurrences */ /* Replace all occurrences */
case 'g': case 'g':
if (match[0] != '^') sed_cmd->which_match = 0; if (match[0] != '^') sed_cmd->which_match = 0;
break; break;
/* Print pattern space */ /* Print pattern space */
case 'p': case 'p':
sed_cmd->sub_p = 1; sed_cmd->sub_p = 1;
break; break;
/* Write to file */ /* Write to file */
case 'w': case 'w':
{ {
char *temp; char *temp;
idx+=parse_file_cmd(sed_cmd,substr+idx,&temp); idx+=parse_file_cmd(sed_cmd,substr+idx,&temp);
break; break;
} }
/* Ignore case (gnu exension) */ /* Ignore case (gnu exension) */
case 'I': case 'I':
cflags |= REG_ICASE; cflags |= REG_ICASE;
break; break;
/* Comment */ /* Comment */
case '#': case '#':
while(substr[++idx]); while(substr[++idx]);
/* Fall through */ /* Fall through */
/* End of command */ /* End of command */
case ';': case ';':
case '}': case '}':
goto out; goto out;
default: default:
bb_error_msg_and_die("bad option in substitution expression"); bb_error_msg_and_die("bad option in substitution expression");
} }
} }
out: out:
@ -420,7 +420,7 @@ static char *parse_cmd_args(sed_cmd_t *sed_cmd, char *cmdstr)
} else if (strchr(":btT", sed_cmd->cmd)) { } else if (strchr(":btT", sed_cmd->cmd)) {
int length; int length;
while(isspace(*cmdstr)) cmdstr++; cmdstr = skip_whitespace(cmdstr);
length = strcspn(cmdstr, semicolon_whitespace); length = strcspn(cmdstr, semicolon_whitespace);
if (length) { if (length) {
sed_cmd->string = xstrndup(cmdstr, length); sed_cmd->string = xstrndup(cmdstr, length);
@ -517,7 +517,7 @@ static void add_cmd(char *cmdstr)
} }
/* skip whitespace before the command */ /* skip whitespace before the command */
while (isspace(*cmdstr)) cmdstr++; cmdstr = skip_whitespace(cmdstr);
/* Check for inversion flag */ /* Check for inversion flag */
if (*cmdstr == '!') { if (*cmdstr == '!') {
@ -525,7 +525,7 @@ static void add_cmd(char *cmdstr)
cmdstr++; cmdstr++;
/* skip whitespace before the command */ /* skip whitespace before the command */
while (isspace(*cmdstr)) cmdstr++; cmdstr = skip_whitespace(cmdstr);
} }
/* last part (mandatory) will be a command */ /* last part (mandatory) will be a command */
@ -724,7 +724,7 @@ static int puts_maybe_newline(char *s, FILE *file, int missing_newline, int no_n
fputs(s,file); fputs(s,file);
if(!no_newline) fputc('\n',file); if(!no_newline) fputc('\n',file);
if(ferror(file)) { if(ferror(file)) {
xfunc_error_retval = 4; /* It's what gnu sed exits with... */ xfunc_error_retval = 4; /* It's what gnu sed exits with... */
bb_error_msg_and_die(bb_msg_write_error); bb_error_msg_and_die(bb_msg_write_error);
} }
@ -1167,40 +1167,43 @@ int sed_main(int argc, char **argv)
FILE *file; FILE *file;
for (i = optind; i < argc; i++) { for (i = optind; i < argc; i++) {
struct stat statbuf;
int nonstdoutfd;
if(!strcmp(argv[i], "-") && !bbg.in_place) { if(!strcmp(argv[i], "-") && !bbg.in_place) {
add_input_file(stdin); add_input_file(stdin);
process_files(); process_files();
} else { continue;
file = bb_wfopen(argv[i], "r");
if (file) {
if(bbg.in_place) {
struct stat statbuf;
int nonstdoutfd;
bbg.outname=xstrndup(argv[i],strlen(argv[i])+6);
strcat(bbg.outname,"XXXXXX");
if(-1==(nonstdoutfd=mkstemp(bbg.outname)))
bb_error_msg_and_die("no temp file");
bbg.nonstdout=fdopen(nonstdoutfd,"w");
/* Set permissions of output file */
fstat(fileno(file),&statbuf);
fchmod(nonstdoutfd,statbuf.st_mode);
add_input_file(file);
process_files();
fclose(bbg.nonstdout);
bbg.nonstdout=stdout;
unlink(argv[i]);
rename(bbg.outname,argv[i]);
free(bbg.outname);
bbg.outname=0;
} else add_input_file(file);
} else {
status = EXIT_FAILURE;
}
} }
file = bb_wfopen(argv[i], "r");
if (!file) {
status = EXIT_FAILURE;
continue;
}
if(!bbg.in_place) {
add_input_file(file);
continue;
}
bbg.outname=xstrndup(argv[i],strlen(argv[i])+6);
strcat(bbg.outname,"XXXXXX");
if(-1==(nonstdoutfd=mkstemp(bbg.outname)))
bb_error_msg_and_die("no temp file");
bbg.nonstdout=fdopen(nonstdoutfd,"w");
/* Set permissions of output file */
fstat(fileno(file),&statbuf);
fchmod(nonstdoutfd,statbuf.st_mode);
add_input_file(file);
process_files();
fclose(bbg.nonstdout);
bbg.nonstdout=stdout;
unlink(argv[i]);
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();
} }

View File

@ -179,10 +179,10 @@ static void stack_machine(const char *argument)
*/ */
static char *get_token(char **buffer) static char *get_token(char **buffer)
{ {
char *start = NULL; char *start = NULL;
char *current = *buffer; char *current;
while (isspace(*current)) { current++; } current = skip_whitespace(*buffer);
if (*current != 0) { if (*current != 0) {
start = current; start = current;
while (!isspace(*current) && *current != 0) { current++; } while (!isspace(*current) && *current != 0) { current++; }

View File

@ -76,15 +76,13 @@ static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
{ {
char *tag, *value; char *tag, *value;
while ( isspace ( *buffer )) buffer = skip_whitespace ( buffer );
buffer++;
tag = value = buffer; tag = value = buffer;
while ( !isspace ( *value )) while ( !isspace ( *value ))
if (!*value) return 0; if (!*value) return 0;
else value++; else value++;
*value++ = 0; *value++ = 0;
while ( isspace ( *value )) value = skip_whitespace ( value );
value++;
if (!*value) return 0; if (!*value) return 0;
*ptag = tag; *ptag = tag;
@ -311,11 +309,9 @@ static void include_conf ( struct dep_t **first, struct dep_t **current, char *b
} }
} }
else if (( strncmp ( buffer, "include", 7 ) == 0 ) && isspace ( buffer [7] )) { else if (( strncmp ( buffer, "include", 7 ) == 0 ) && isspace ( buffer [7] )) {
int fdi; char *filename;
int fdi; char *filename = buffer + 8; filename = skip_whitespace ( buffer + 8 );
while ( isspace ( *filename ))
filename++;
if (( fdi = open ( filename, O_RDONLY )) >= 0 ) { if (( fdi = open ( filename, O_RDONLY )) >= 0 ) {
include_conf(first, current, buffer, buflen, fdi); include_conf(first, current, buffer, buflen, fdi);

View File

@ -572,10 +572,7 @@ static char *next_word(char **buf)
} }
/* Skip over leading whitespace */ /* Skip over leading whitespace */
word = *buf; word = skip_whitespace(*buf);
while (isspace(*word)) {
++word;
}
/* Skip over comments */ /* Skip over comments */
if (*word == '#') { if (*word == '#') {
@ -712,9 +709,7 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
} }
/* ship any trailing whitespace */ /* ship any trailing whitespace */
while (isspace(*buf_ptr)) { buf_ptr = skip_whitespace(buf_ptr);
++buf_ptr;
}
if (buf_ptr[0] != '\0') { if (buf_ptr[0] != '\0') {
bb_error_msg("too many parameters \"%s\"", buf); bb_error_msg("too many parameters \"%s\"", buf);

View File

@ -302,7 +302,7 @@ int wget_main(int argc, char **argv)
s = buf; s = buf;
while (*s != '\0' && !isspace(*s)) ++s; while (*s != '\0' && !isspace(*s)) ++s;
while (isspace(*s)) ++s; s = skip_whitespace(s);
// FIXME: no error check // FIXME: no error check
// xatou wouldn't work: "200 OK" // xatou wouldn't work: "200 OK"
status = atoi(s); status = atoi(s);

View File

@ -119,7 +119,7 @@ static char *parse_pipeline(char *cmdline, struct pipeline *line)
char *end; char *end;
// Skip leading whitespace and detect end of line. // Skip leading whitespace and detect end of line.
while (isspace(*start)) start++; start = skip_whitespace(start);
if (!*start || *start=='#') { if (!*start || *start=='#') {
if (ENABLE_BBSH_JOBCTL) line->cmdlinelen = start-cmdline; if (ENABLE_BBSH_JOBCTL) line->cmdlinelen = start-cmdline;
return 0; return 0;

View File

@ -203,8 +203,7 @@ static llist_t *get_block_backed_filesystems(void)
while ((buf = xmalloc_getline(f)) != 0) { while ((buf = xmalloc_getline(f)) != 0) {
if (!strncmp(buf, "nodev", 5) && isspace(buf[5])) if (!strncmp(buf, "nodev", 5) && isspace(buf[5]))
continue; continue;
fs = buf; fs = skip_whitespace(buf);
while (isspace(*fs)) fs++;
if (*fs=='#' || *fs=='*' || !*fs) continue; if (*fs=='#' || *fs=='*' || !*fs) continue;
llist_add_to_end(&list, xstrdup(fs)); llist_add_to_end(&list, xstrdup(fs));