* Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
* Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org>
* Copyright (C) 2002 Matt Kraai
- * Copyright (C) 2003 by Glenn McGrath <bug1@optushome.com.au>
+ * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au>
+ * Copyright (C) 2003,2004 by Rob Landley <rob@landley.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
/* globals */
/* options */
-static int be_quiet = 0;
+static int be_quiet = 0, in_place=0;
+FILE *nonstdout;
+char *outname;
+
static const char bad_format_in_subst[] =
"bad format in substitution expression";
}
#endif
+/* If something bad happens during -i operation, delete temp file */
+
+static void cleanup_outname(void)
+{
+ if(outname) unlink(outname);
+}
+
/* strdup, replacing "\n" with '\n', and "\delimiter" with 'delimiter' */
static void parse_escapes(char *dest, const char *string, int len, char from, char to)
/*
* index_of_next_unescaped_regexp_delim - walks left to right through a string
* beginning at a specified index and returns the index of the next regular
- * expression delimiter (typically a forward * slash ('/')) not preceeded by
+ * expression delimiter (typically a forward * slash ('/')) not preceded by
* a backslash ('\').
*/
static int index_of_next_unescaped_regexp_delim(const char delimiter,
next = index_of_next_unescaped_regexp_delim(delimiter, ++pos);
if (next == -1)
bb_error_msg_and_die("unterminated match expression");
-
+
temp=copy_parsing_slashn(pos,next);
*regex = (regex_t *) xmalloc(sizeof(regex_t));
xregcomp(*regex, temp, REG_NEWLINE);
{
char *temp;
idx+=parse_file_cmd(sed_cmd,substr+idx,&temp);
-
+
break;
}
/* Ignore case (gnu exension) */
if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
bb_error_msg_and_die
("only a beginning address can be specified for edit commands");
- while(isspace(*cmdstr)) cmdstr++;
+ for(;;) {
+ if(*cmdstr=='\n' || *cmdstr=='\\') {
+ cmdstr++;
+ break;
+ } else if(isspace(*cmdstr)) cmdstr++;
+ else break;
+ }
sed_cmd->string = bb_xstrdup(cmdstr);
parse_escapes(sed_cmd->string,sed_cmd->string,strlen(cmdstr),0,0);
cmdstr += strlen(cmdstr);
do {
int i;
+ /* Work around bug in glibc regexec, demonstrated by:
+ echo " a.b" | busybox sed 's [^ .]* x g'
+ The match_count check is so not to break
+ echo "hi" | busybox sed 's/^/!/g' */
+ if(!regmatch[0].rm_so && !regmatch[0].rm_eo && match_count) {
+ pipe_putc(*(oldline++));
+ continue;
+ }
+
match_count++;
/* If we aren't interested in this match, output old line to
{
/* Output appended lines. */
while(append_head) {
- puts(append_head->string);
+ fprintf(nonstdout,"%s\n",append_head->string);
append_tail=append_head->next;
free(append_head->string);
free(append_head);
fputs(s,file);
if(!no_newline) fputc('\n',file);
+ if(ferror(file)) {
+ fprintf(stderr,"Write failed.\n");
+ exit(4); /* It's what gnu sed exits with... */
+ }
+
return no_newline;
}
-#define sed_puts(s,n) missing_newline=puts_maybe_newline(s,stdout,missing_newline,n)
+#define sed_puts(s,n) missing_newline=puts_maybe_newline(s,nonstdout,missing_newline,n)
-static void process_file(FILE * file)
+static void process_file(FILE *file)
{
char *pattern_space, *next_line, *hold_space=NULL;
static int linenum = 0, missing_newline=0;
/* Print line number */
case '=':
- printf("%d\n", linenum);
+ fprintf(nonstdout,"%d\n", linenum);
break;
/* Write the current pattern space up to the first newline */
extern int sed_main(int argc, char **argv)
{
- int opt, status = EXIT_SUCCESS;
+ int status = EXIT_SUCCESS;
+ int opt;
+ uint8_t getpat = 1;
#ifdef CONFIG_FEATURE_CLEAN_UP
/* destroy command strings on exit */
bb_perror_msg_and_die("atexit");
#endif
+#define LIE_TO_AUTOCONF
+#ifdef LIE_TO_AUTOCONF
+ if(argc==2 && !strcmp(argv[1],"--version")) {
+ printf("This is not GNU sed version 4.0\n");
+ exit(0);
+ }
+#endif
+
/* do normal option parsing */
- while ((opt = getopt(argc, argv, "ne:f:")) > 0) {
+ while ((opt = getopt(argc, argv, "ine:f:")) > 0) {
switch (opt) {
+ case 'i':
+ in_place++;
+ atexit(cleanup_outname);
+ break;
case 'n':
be_quiet++;
break;
case 'e':
add_cmd_block(optarg);
+ getpat=0;
break;
case 'f':
{
while ((line = bb_get_chomped_line_from_file(cmdfile))
!= NULL) {
add_cmd(line);
+ getpat=0;
free(line);
}
bb_xprint_and_close_file(cmdfile);
/* 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 (sed_cmd_head.next == NULL) {
+ if(getpat) {
if (argv[optind] == NULL)
bb_show_usage();
else
/* Flush any unfinished commands. */
add_cmd("");
+ /* By default, we write to stdout */
+ nonstdout=stdout;
+
/* argv[(optind)..(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(in_place) {
+ fprintf(stderr,"sed: Filename required for -i\n");
+ exit(1);
+ }
process_file(stdin);
} else {
int i;
FILE *file;
for (i = optind; i < argc; i++) {
- if(!strcmp(argv[i], "-")) {
+ if(!strcmp(argv[i], "-") && !in_place) {
process_file(stdin);
} else {
file = bb_wfopen(argv[i], "r");
if (file) {
+ if(in_place) {
+ struct stat statbuf;
+ outname=bb_xstrndup(argv[i],strlen(argv[i])+6);
+ strcat(outname,"XXXXXX");
+ /* 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) {
+ fclose(nonstdout);
+ nonstdout=stdout;
+ unlink(argv[i]);
+ rename(outname,argv[i]);
+ free(outname);
+ outname=0;
+ }
} else {
status = EXIT_FAILURE;
}