Rename "line" to pattern_space to be more descriptive and closer to the POSIX description

This commit is contained in:
Glenn L McGrath 2003-04-07 12:24:44 +00:00
parent 4957fc7a93
commit c6adada158

View File

@ -773,14 +773,14 @@ static sed_cmd_t *branch_to(const char *label)
static void process_file(FILE *file) static void process_file(FILE *file)
{ {
char *line; char *pattern_space; /* Posix requires it be able to hold at least 8192 bytes */
static int linenum = 0; /* GNU sed does not restart counting lines at EOF */ static int linenum = 0; /* GNU sed does not restart counting lines at EOF */
unsigned int still_in_range = 0; unsigned int still_in_range = 0;
int altered; int altered;
int force_print; int force_print;
line = bb_get_chomped_line_from_file(file); pattern_space = bb_get_chomped_line_from_file(file);
if (line == NULL) { if (pattern_space == NULL) {
return; return;
} }
@ -812,7 +812,7 @@ static void process_file(FILE *file)
/* 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, line, 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))
); );
@ -827,13 +827,13 @@ static void process_file(FILE *file)
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(line, '\n'); char *tmp = strchr(pattern_space, '\n');
if (tmp) { if (tmp) {
*tmp = '\0'; *tmp = '\0';
} }
} }
case 'p': /* Write the current pattern space to output */ case 'p': /* Write the current pattern space to output */
puts(line); puts(pattern_space);
break; break;
case 'd': case 'd':
altered++; altered++;
@ -850,8 +850,8 @@ static void process_file(FILE *file)
* substitution * substitution
* *
* s///p ONLY = always print successful substitutions, even if * s///p ONLY = always print successful substitutions, even if
* the line is going to be printed anyway (line will be printed * the pattern_space is going to be printed anyway (pattern_space
* twice). * will be printed twice).
* *
* -n AND s///p = print ONLY a successful substitution ONE TIME; * -n AND s///p = print ONLY a successful substitution ONE TIME;
* no other lines are printed - this is the reason why the 'p' * no other lines are printed - this is the reason why the 'p'
@ -862,24 +862,24 @@ 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(line + offset, '\n') != NULL) { while(strchr(pattern_space + offset, '\n') != NULL) {
char *tmp; char *tmp;
line = xrealloc(line, strlen(line) + 2); pattern_space = xrealloc(pattern_space, strlen(pattern_space) + 2);
tmp = strchr(line + 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] = '\\';
tmp[1] = 'n'; tmp[1] = 'n';
offset = tmp - line + 2; offset = tmp - pattern_space + 2;
} }
} }
#endif #endif
/* we print the line once, unless we were told to be quiet */ /* we print the pattern_space once, unless we were told to be quiet */
substituted = do_subst_command(sed_cmd, &line); substituted = do_subst_command(sed_cmd, &pattern_space);
#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE #ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
/* undo HACK: escape newlines twice so regex can match them */ /* undo HACK: escape newlines twice so regex can match them */
{ {
char *tmp = line; char *tmp = pattern_space;
while((tmp = strstr(tmp, "\\n")) != NULL) { while((tmp = strstr(tmp, "\\n")) != NULL) {
memmove(tmp, tmp + 1, strlen(tmp + 1) + 1); memmove(tmp, tmp + 1, strlen(tmp + 1) + 1);
@ -895,11 +895,11 @@ static void process_file(FILE *file)
/* we also print the line if we were given the 'p' flag /* we also print the line if we were given the 'p' flag
* (this is quite possibly the second printing) */ * (this is quite possibly the second printing) */
if ((sed_cmd->sub_p) && altered) { if ((sed_cmd->sub_p) && altered) {
puts(line); puts(pattern_space);
} }
break; break;
case 'a': case 'a':
puts(line); puts(pattern_space);
fputs(sed_cmd->editline, stdout); fputs(sed_cmd->editline, stdout);
altered++; altered++;
break; break;
@ -913,7 +913,7 @@ 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, line, 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))
{ {
@ -925,7 +925,7 @@ static void process_file(FILE *file)
case 'r': { case 'r': {
FILE *outfile; FILE *outfile;
puts(line); puts(pattern_space);
outfile = fopen(sed_cmd->filename, "r"); outfile = fopen(sed_cmd->filename, "r");
if (outfile) if (outfile)
bb_xprint_and_close_file(outfile); bb_xprint_and_close_file(outfile);
@ -941,16 +941,16 @@ static void process_file(FILE *file)
next_line = NULL; next_line = NULL;
break; break;
case 'n': /* Read next line from input */ case 'n': /* Read next line from input */
free(line); free(pattern_space);
line = next_line; pattern_space = next_line;
next_line = bb_get_chomped_line_from_file(file); next_line = bb_get_chomped_line_from_file(file);
linenum++; linenum++;
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) {
line = realloc(line, strlen(line) + strlen(next_line) + 2); pattern_space = realloc(pattern_space, strlen(pattern_space) + strlen(next_line) + 2);
strcat(line, "\n"); strcat(pattern_space, "\n");
strcat(line, 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);
linenum++; linenum++;
} }
@ -965,18 +965,16 @@ static void process_file(FILE *file)
break; break;
case 'y': { case 'y': {
int i; int i;
for (i = 0; line[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 (line[i] == sed_cmd->translate[j]) { if (pattern_space[i] == sed_cmd->translate[j]) {
line[i] = sed_cmd->translate[j + 1]; pattern_space[i] = sed_cmd->translate[j + 1];
} }
} }
} }
} }
break; break;
// case ':':
// break;
} }
} }
@ -993,7 +991,7 @@ static void process_file(FILE *file)
/* 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, line, 0, NULL, 0) == 0)) (sed_cmd->end_match && (regexec(sed_cmd->end_match, pattern_space, 0, NULL, 0) == 0))
) )
) )
) { ) {
@ -1015,11 +1013,11 @@ static void process_file(FILE *file)
* line was altered (via a 'd'elete or 's'ubstitution), in which case * line was altered (via a 'd'elete or 's'ubstitution), in which case
* the altered line was already printed */ * the altered line was already printed */
if ((!be_quiet && !altered) || force_print){ if ((!be_quiet && !altered) || force_print){
puts(line); puts(pattern_space);
} }
free(line); free(pattern_space);
line = next_line; pattern_space = next_line;
} while (line); } while (pattern_space);
} }
extern int sed_main(int argc, char **argv) extern int sed_main(int argc, char **argv)