resulting sed_cmd_t structures are appended to a linked list
(sed_cmd_head/sed_cmd_tail).
- process_file() does actual sedding, reading data lines from an input FILE *
+ add_input_file() adds a FILE * to the list of input files. We need to
+ know them all ahead of time to find the last line for the $ match.
+
+ process_files() does actual sedding, reading data lines from each input FILE *
(which could be stdin) and applying the sed command list (sed_cmd_head) to
each of the resulting lines.
- grouped commands: {cmd1;cmd2}
- transliteration (y/source-chars/dest-chars/)
- pattern space hold space storing / swapping (g, h, x)
- - labels / branching (: label, b, t)
+ - labels / branching (: label, b, t, T)
(Note: Specifying an address (range) to match is *optional*; commands
default to the whole pattern space if no specific address match was
Unsupported features:
- - GNU extensions
+ - most GNU extensions
- and more.
Todo:
#include <stdio.h>
#include <unistd.h> /* for getopt() */
-#include <regex.h>
#include <string.h> /* for strdup() */
#include <errno.h>
#include <ctype.h> /* for isspace() */
#include <stdlib.h>
#include "busybox.h"
+#include "xregex.h"
typedef struct sed_cmd_s {
/* Ordered by alignment requirements: currently 36 bytes on x86 */
/* globals */
/* options */
static int be_quiet, in_place, regex_type;
-FILE *nonstdout;
-char *outname,*hold_space;
+static FILE *nonstdout;
+static char *outname,*hold_space;
+/* List of input files */
+static int input_file_count,current_input_file;
+static FILE **input_file_list;
static const char bad_format_in_subst[] =
"bad format in substitution expression";
-const char *const semicolon_whitespace = "; \n\r\t\v";
+static const char *const semicolon_whitespace = "; \n\r\t\v";
-regmatch_t regmatch[10];
+static regmatch_t regmatch[10];
static regex_t *previous_regex_ptr;
/* linked list of sed commands */
char *string;
struct append_list *next;
};
-struct append_list *append_head=NULL, *append_tail=NULL;
+static struct append_list *append_head=NULL, *append_tail=NULL;
#ifdef CONFIG_FEATURE_CLEAN_UP
static void free_and_close_stuff(void)
}
if(hold_space) free(hold_space);
+
+ while(current_input_file<input_file_count)
+ fclose(input_file_list[current_input_file++]);
}
#endif
if(sed_cmd->cmd=='w')
sed_cmd->file=bb_xfopen(sed_cmd->string,"w");
/* handle branch commands */
- } else if (strchr(":bt", sed_cmd->cmd)) {
+ } else if (strchr(":btT", sed_cmd->cmd)) {
int length;
while(isspace(*cmdstr)) cmdstr++;
/* Parse address+command sets, skipping comment lines. */
-void add_cmd(char *cmdstr)
+static void add_cmd(char *cmdstr)
{
static char *add_cmd_line=NULL;
sed_cmd_t *sed_cmd;
}
}
-struct pipeline {
+/* Append to a string, reallocating memory as necessary. */
+
+static struct pipeline {
char *buf; /* Space to hold string */
int idx; /* Space used */
int len; /* Space allocated */
#define PIPE_GROW 64
-void pipe_putc(char c)
+static void pipe_putc(char c)
{
if(pipeline.idx==pipeline.len) {
pipeline.buf = xrealloc(pipeline.buf, pipeline.len + PIPE_GROW);
append_head=append_tail=NULL;
}
-/* Get next line of input, flushing append buffer and noting if we hit EOF
- * without a newline on the last line.
+static void add_input_file(FILE *file)
+{
+ input_file_list=xrealloc(input_file_list,(input_file_count+1)*sizeof(FILE *));
+ input_file_list[input_file_count++]=file;
+}
+
+/* Get next line of input from input_file_list, flushing append buffer and
+ * noting if we ran out of files without a newline on the last line we read.
*/
-static char *get_next_line(FILE * file, int *no_newline)
+static char *get_next_line(int *no_newline)
{
- char *temp;
+ char *temp=NULL;
int len;
flush_append();
- temp=bb_get_line_from_file(file);
- if(temp) {
- len=strlen(temp);
- if(len && temp[len-1]=='\n') temp[len-1]=0;
- else *no_newline=1;
+ while(current_input_file<input_file_count) {
+ temp=bb_get_line_from_file(input_file_list[current_input_file]);
+ if(temp) {
+ len=strlen(temp);
+ *no_newline=!(len && temp[len-1]=='\n');
+ if(!*no_newline) temp[len-1]=0;
+ break;
+ } else fclose(input_file_list[current_input_file++]);
}
return temp;
#define sed_puts(s,n) missing_newline=puts_maybe_newline(s,nonstdout,missing_newline,n)
-static void process_file(FILE *file)
+static void process_files(void)
{
char *pattern_space, *next_line;
- static int linenum = 0, missing_newline=0;
+ int linenum = 0, missing_newline=0;
int no_newline,next_no_newline=0;
- next_line = get_next_line(file,&next_no_newline);
+ next_line = get_next_line(&next_no_newline);
- /* go through every line in the file */
+ /* go through every line in each file */
for(;;) {
sed_cmd_t *sed_cmd;
int substituted=0;
no_newline=next_no_newline;
/* Read one line in advance so we can act on the last line, the '$' address */
- next_line = get_next_line(file,&next_no_newline);
+ next_line = get_next_line(&next_no_newline);
linenum++;
restart:
/* for every line, go through all the commands */
free(pattern_space);
pattern_space = next_line;
no_newline=next_no_newline;
- next_line = get_next_line(file,&next_no_newline);
+ next_line = get_next_line(&next_no_newline);
linenum++;
break;
}
pattern_space[len]='\n';
strcpy(pattern_space+len+1, next_line);
no_newline=next_no_newline;
- next_line = get_next_line(file,&next_no_newline);
+ next_line = get_next_line(&next_no_newline);
linenum++;
}
break;
}
- /* Test if substition worked, branch if so. */
+ /* Test/branch if substitution occurred */
case 't':
- if (!substituted) break;
+ if(!substituted) break;
substituted=0;
- /* Fall through */
+ /* Fall through */
+ /* Test/branch if substitution didn't occur */
+ case 'T':
+ if (substituted) break;
+ /* Fall through */
/* Branch to label */
case 'b':
if (!sed_cmd->string) goto discard_commands;
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];
+ break;
}
}
}
}
}
- /* if we didn't get a pattern from a -e and no command file was specified,
- * argv[optind] should be the pattern. no pattern, no worky */
+ /* if we didn't get a pattern from -e or -f, use argv[optind] */
if(getpat) {
if (argv[optind] == NULL)
bb_show_usage();
* files were specified or '-' was specified, take input from stdin.
* Otherwise, we process all the files specified. */
if (argv[optind] == NULL) {
- if(in_place) {
- fprintf(stderr,"sed: Filename required for -i\n");
- exit(1);
- }
- process_file(stdin);
+ if(in_place) bb_error_msg_and_die("Filename required for -i");
+ add_input_file(stdin);
+ process_files();
} else {
int i;
FILE *file;
for (i = optind; i < argc; i++) {
if(!strcmp(argv[i], "-") && !in_place) {
- process_file(stdin);
+ add_input_file(stdin);
+ process_files();
} else {
file = bb_wfopen(argv[i], "r");
if (file) {
if(in_place) {
struct stat statbuf;
+ int nonstdoutfd;
+
outname=bb_xstrndup(argv[i],strlen(argv[i])+6);
strcat(outname,"XXXXXX");
+ if(-1==(nonstdoutfd=mkstemp(outname)))
+ bb_error_msg_and_die("no temp file");
+ nonstdout=fdopen(nonstdoutfd,"w");
/* Set permissions of output file */
fstat(fileno(file),&statbuf);
- mkstemp(outname);
- nonstdout=bb_wfopen(outname,"w");
- /* Set permissions of output file */
- fstat(fileno(file),&statbuf);
- fchmod(fileno(nonstdout),statbuf.st_mode);
- atexit(cleanup_outname);
- }
- process_file(file);
- fclose(file);
- if(in_place) {
+ fchmod(nonstdoutfd,statbuf.st_mode);
+ add_input_file(file);
+ process_files();
fclose(nonstdout);
nonstdout=stdout;
unlink(argv[i]);
rename(outname,argv[i]);
free(outname);
outname=0;
- }
+ } else add_input_file(file);
} else {
status = EXIT_FAILURE;
}
}
}
+ if(input_file_count>current_input_file) process_files();
}
return status;