fix n, add N, P

This commit is contained in:
Glenn L McGrath 2003-03-10 04:12:35 +00:00
parent ff724fb076
commit 4157a8a71e

View File

@ -382,7 +382,7 @@ static char *parse_cmd_str(sed_cmd_t * const sed_cmd, char *cmdstr)
{ {
/* if it was a single-letter command that takes no arguments (such as 'p' /* if it was a single-letter command that takes no arguments (such as 'p'
* or 'd') all we need to do is increment the index past that command */ * or 'd') all we need to do is increment the index past that command */
if (strchr("npqd=", sed_cmd->cmd)) { if (strchr("nNpPqd=", sed_cmd->cmd)) {
cmdstr++; cmdstr++;
} }
/* handle (s)ubstitution command */ /* handle (s)ubstitution command */
@ -680,6 +680,7 @@ static void process_file(FILE *file)
if (line == NULL) { if (line == NULL) {
return; return;
} }
chomp(line);
/* go through every line in the file */ /* go through every line in the file */
do { do {
@ -688,7 +689,7 @@ static void process_file(FILE *file)
/* Read one line in advance so we can act on the last line, the '$' address */ /* Read one line in advance so we can act on the last line, the '$' address */
next_line = get_line_from_file(file); next_line = get_line_from_file(file);
chomp(line); chomp(next_line);
linenum++; linenum++;
altered = 0; altered = 0;
@ -722,10 +723,15 @@ static void process_file(FILE *file)
case '=': case '=':
printf("%d\n", linenum); printf("%d\n", linenum);
break; break;
case 'p': case 'P': { /* Write the current pattern space upto the first newline */
char *tmp = strchr(line, '\n');
if (tmp) {
*tmp = '\0';
}
}
case 'p': /* Write the current pattern space to output */
puts(line); puts(line);
break; break;
case 'd': case 'd':
altered++; altered++;
deleted = 1; deleted = 1;
@ -808,8 +814,19 @@ static void process_file(FILE *file)
free(line); free(line);
return; return;
case 'n': /* Read next line from input */ case 'n': /* Read next line from input */
i = ncmds; free(line);
line = next_line;
next_line = get_line_from_file(file);
chomp(next_line);
linenum++;
break; break;
case 'N': /* Append the next line to the current line */
line = realloc(line, strlen(line) + strlen(next_line) + 2);
strcat(line, "\n");
strcat(line, next_line);
next_line = get_line_from_file(file);
chomp(next_line);
linenum++;
} }
} }