Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 2 | /* |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 3 | * sed.c - very minimalist version of sed |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley |
| 6 | * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org> |
Matt Kraai | 5ed78ad | 2002-01-03 21:12:34 +0000 | [diff] [blame] | 7 | * Copyright (C) 2002 Matt Kraai |
Glenn L McGrath | c6992fe | 2004-04-25 05:11:19 +0000 | [diff] [blame] | 8 | * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au> |
Rob Landley | 25d8239 | 2004-04-01 09:23:30 +0000 | [diff] [blame] | 9 | * Copyright (C) 2003,2004 by Rob Landley <rob@landley.net> |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 10 | * |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 11 | * MAINTAINER: Rob Landley <rob@landley.net> |
| 12 | * |
Rob Landley | fae1dc8 | 2005-11-20 07:44:35 +0000 | [diff] [blame] | 13 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 16 | /* Code overview. |
| 17 | |
| 18 | Files are laid out to avoid unnecessary function declarations. So for |
| 19 | example, every function add_cmd calls occurs before add_cmd in this file. |
| 20 | |
| 21 | add_cmd() is called on each line of sed command text (from a file or from |
| 22 | the command line). It calls get_address() and parse_cmd_args(). The |
| 23 | resulting sed_cmd_t structures are appended to a linked list |
| 24 | (sed_cmd_head/sed_cmd_tail). |
| 25 | |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 26 | add_input_file() adds a FILE * to the list of input files. We need to |
Rob Landley | fae1dc8 | 2005-11-20 07:44:35 +0000 | [diff] [blame] | 27 | know all input sources ahead of time to find the last line for the $ match. |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 28 | |
| 29 | process_files() does actual sedding, reading data lines from each input FILE * |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 30 | (which could be stdin) and applying the sed command list (sed_cmd_head) to |
| 31 | each of the resulting lines. |
| 32 | |
| 33 | sed_main() is where external code calls into this, with a command line. |
| 34 | */ |
| 35 | |
| 36 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 37 | /* |
| 38 | Supported features and commands in this version of sed: |
| 39 | |
| 40 | - comments ('#') |
Mark Whitley | 94074a9 | 2000-07-14 00:00:15 +0000 | [diff] [blame] | 41 | - address matching: num|/matchstr/[,num|/matchstr/|$]command |
| 42 | - commands: (p)rint, (d)elete, (s)ubstitue (with g & I flags) |
| 43 | - edit commands: (a)ppend, (i)nsert, (c)hange |
Mark Whitley | 1f3b9f2 | 2001-05-11 22:27:13 +0000 | [diff] [blame] | 44 | - file commands: (r)ead |
Rob Landley | c63fe91 | 2005-10-30 10:08:13 +0000 | [diff] [blame] | 45 | - backreferences in substitution expressions (\0, \1, \2...\9) |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 46 | - grouped commands: {cmd1;cmd2} |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 47 | - transliteration (y/source-chars/dest-chars/) |
| 48 | - pattern space hold space storing / swapping (g, h, x) |
Rob Landley | 93850a5 | 2005-05-18 06:34:37 +0000 | [diff] [blame] | 49 | - labels / branching (: label, b, t, T) |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 50 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 51 | (Note: Specifying an address (range) to match is *optional*; commands |
| 52 | default to the whole pattern space if no specific address match was |
| 53 | requested.) |
| 54 | |
Glenn L McGrath | 2570b43 | 2003-09-16 05:25:43 +0000 | [diff] [blame] | 55 | Todo: |
Glenn L McGrath | 2570b43 | 2003-09-16 05:25:43 +0000 | [diff] [blame] | 56 | - Create a wrapper around regex to make libc's regex conform with sed |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 57 | |
Glenn L McGrath | d5eadea | 2003-03-09 02:39:29 +0000 | [diff] [blame] | 58 | Reference http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 59 | */ |
| 60 | |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 61 | #include <stdio.h> |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 62 | #include <unistd.h> /* for getopt() */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 63 | #include <string.h> /* for strdup() */ |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 64 | #include <errno.h> |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 65 | #include <ctype.h> /* for isspace() */ |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 66 | #include <stdlib.h> |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 67 | #include "busybox.h" |
"Vladimir N. Oleynik" | 23f62fc | 2005-09-14 16:59:11 +0000 | [diff] [blame] | 68 | #include "xregex.h" |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 69 | |
Glenn L McGrath | e7a8bc9 | 2003-03-09 10:23:57 +0000 | [diff] [blame] | 70 | typedef struct sed_cmd_s { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 71 | /* Ordered by alignment requirements: currently 36 bytes on x86 */ |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 72 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 73 | /* address storage */ |
| 74 | regex_t *beg_match; /* sed -e '/match/cmd' */ |
| 75 | regex_t *end_match; /* sed -e '/match/,/end_match/cmd' */ |
| 76 | regex_t *sub_match; /* For 's/sub_match/string/' */ |
| 77 | int beg_line; /* 'sed 1p' 0 == apply commands to all lines */ |
| 78 | int end_line; /* 'sed 1,3p' 0 == one line only. -1 = last line ($) */ |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 79 | |
| 80 | FILE *file; /* File (sw) command writes to, -1 for none. */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 81 | char *string; /* Data string for (saicytb) commands. */ |
Glenn L McGrath | fc4cb4d | 2003-04-12 16:10:42 +0000 | [diff] [blame] | 82 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 83 | unsigned short which_match; /* (s) Which match to replace (0 for all) */ |
Glenn L McGrath | fc4cb4d | 2003-04-12 16:10:42 +0000 | [diff] [blame] | 84 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 85 | /* Bitfields (gcc won't group them if we don't) */ |
| 86 | unsigned int invert:1; /* the '!' after the address */ |
| 87 | unsigned int in_match:1; /* Next line also included in match? */ |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 88 | unsigned int no_newline:1; /* Last line written by (sw) had no '\n' */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 89 | unsigned int sub_p:1; /* (s) print option */ |
Glenn L McGrath | c18ce37 | 2003-09-13 06:57:39 +0000 | [diff] [blame] | 90 | |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 91 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 92 | /* GENERAL FIELDS */ |
| 93 | char cmd; /* The command char: abcdDgGhHilnNpPqrstwxy:={} */ |
| 94 | struct sed_cmd_s *next; /* Next command (linked list, NULL terminated) */ |
Glenn L McGrath | e7a8bc9 | 2003-03-09 10:23:57 +0000 | [diff] [blame] | 95 | } sed_cmd_t; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 96 | |
| 97 | /* globals */ |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 98 | /* options */ |
Rob Landley | ce4f0e9 | 2004-10-30 06:54:19 +0000 | [diff] [blame] | 99 | static int be_quiet, in_place, regex_type; |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 100 | static FILE *nonstdout; |
| 101 | static char *outname,*hold_space; |
Rob Landley | 53302f8 | 2004-02-18 09:54:15 +0000 | [diff] [blame] | 102 | |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 103 | /* List of input files */ |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 104 | static int input_file_count,current_input_file; |
| 105 | static FILE **input_file_list; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 106 | |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 107 | static const char bad_format_in_subst[] = |
| 108 | "bad format in substitution expression"; |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 109 | static const char *const semicolon_whitespace = "; \n\r\t\v"; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 110 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 111 | static regmatch_t regmatch[10]; |
Rob Landley | ce4f0e9 | 2004-10-30 06:54:19 +0000 | [diff] [blame] | 112 | static regex_t *previous_regex_ptr; |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 113 | |
Glenn L McGrath | c949bfa | 2003-03-28 03:53:31 +0000 | [diff] [blame] | 114 | /* linked list of sed commands */ |
| 115 | static sed_cmd_t sed_cmd_head; |
| 116 | static sed_cmd_t *sed_cmd_tail = &sed_cmd_head; |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 117 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 118 | /* Linked list of append lines */ |
Rob Landley | 5797c7f | 2005-05-18 05:56:16 +0000 | [diff] [blame] | 119 | struct append_list { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 120 | char *string; |
| 121 | struct append_list *next; |
| 122 | }; |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 123 | static struct append_list *append_head=NULL, *append_tail=NULL; |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 124 | |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 125 | void free_and_close_stuff(void); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 126 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 127 | static void free_and_close_stuff(void) |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 128 | { |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 129 | sed_cmd_t *sed_cmd = sed_cmd_head.next; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 130 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 131 | while(append_head) { |
| 132 | append_tail=append_head->next; |
| 133 | free(append_head->string); |
| 134 | free(append_head); |
| 135 | append_head=append_tail; |
| 136 | } |
| 137 | |
Glenn L McGrath | 56c633c | 2003-03-28 04:23:23 +0000 | [diff] [blame] | 138 | while (sed_cmd) { |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 139 | sed_cmd_t *sed_cmd_next = sed_cmd->next; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 140 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 141 | if(sed_cmd->file) |
| 142 | bb_xprint_and_close_file(sed_cmd->file); |
| 143 | |
Glenn L McGrath | 56c633c | 2003-03-28 04:23:23 +0000 | [diff] [blame] | 144 | if (sed_cmd->beg_match) { |
| 145 | regfree(sed_cmd->beg_match); |
| 146 | free(sed_cmd->beg_match); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | } |
Glenn L McGrath | 56c633c | 2003-03-28 04:23:23 +0000 | [diff] [blame] | 148 | if (sed_cmd->end_match) { |
| 149 | regfree(sed_cmd->end_match); |
| 150 | free(sed_cmd->end_match); |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 151 | } |
Glenn L McGrath | 56c633c | 2003-03-28 04:23:23 +0000 | [diff] [blame] | 152 | if (sed_cmd->sub_match) { |
| 153 | regfree(sed_cmd->sub_match); |
| 154 | free(sed_cmd->sub_match); |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 155 | } |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 156 | free(sed_cmd->string); |
Glenn L McGrath | 56c633c | 2003-03-28 04:23:23 +0000 | [diff] [blame] | 157 | free(sed_cmd); |
| 158 | sed_cmd = sed_cmd_next; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 159 | } |
Rob Landley | ce4f0e9 | 2004-10-30 06:54:19 +0000 | [diff] [blame] | 160 | |
| 161 | if(hold_space) free(hold_space); |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 162 | |
| 163 | while(current_input_file<input_file_count) |
| 164 | fclose(input_file_list[current_input_file++]); |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 165 | } |
Mark Whitley | c41e8c8 | 2000-07-12 23:35:21 +0000 | [diff] [blame] | 166 | #endif |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 167 | |
Rob Landley | 53302f8 | 2004-02-18 09:54:15 +0000 | [diff] [blame] | 168 | /* If something bad happens during -i operation, delete temp file */ |
| 169 | |
| 170 | static void cleanup_outname(void) |
| 171 | { |
| 172 | if(outname) unlink(outname); |
| 173 | } |
| 174 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 175 | /* strdup, replacing "\n" with '\n', and "\delimiter" with 'delimiter' */ |
| 176 | |
| 177 | static void parse_escapes(char *dest, const char *string, int len, char from, char to) |
| 178 | { |
| 179 | int i=0; |
| 180 | |
| 181 | while(i<len) { |
| 182 | if(string[i] == '\\') { |
Glenn L McGrath | 738fb33 | 2003-10-01 06:45:11 +0000 | [diff] [blame] | 183 | if(!to || string[i+1] == from) { |
| 184 | *(dest++) = to ? to : string[i+1]; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 185 | i+=2; |
| 186 | continue; |
| 187 | } else *(dest++)=string[i++]; |
| 188 | } |
| 189 | *(dest++) = string[i++]; |
| 190 | } |
| 191 | *dest=0; |
| 192 | } |
| 193 | |
Rob Landley | fae1dc8 | 2005-11-20 07:44:35 +0000 | [diff] [blame] | 194 | static char *copy_parsing_escapes(const char *string, int len) |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 195 | { |
| 196 | char *dest=xmalloc(len+1); |
| 197 | |
| 198 | parse_escapes(dest,string,len,'n','\n'); |
| 199 | return dest; |
| 200 | } |
| 201 | |
| 202 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 203 | /* |
Eric Andersen | 28b3c53 | 2001-01-02 11:01:31 +0000 | [diff] [blame] | 204 | * index_of_next_unescaped_regexp_delim - walks left to right through a string |
| 205 | * beginning at a specified index and returns the index of the next regular |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 206 | * expression delimiter (typically a forward * slash ('/')) not preceded by |
Eric Andersen | 28b3c53 | 2001-01-02 11:01:31 +0000 | [diff] [blame] | 207 | * a backslash ('\'). |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 208 | */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 209 | static int index_of_next_unescaped_regexp_delim(const char delimiter, |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 210 | const char *str) |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 211 | { |
Matt Kraai | a0065d5 | 2001-08-24 14:45:50 +0000 | [diff] [blame] | 212 | int bracket = -1; |
| 213 | int escaped = 0; |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 214 | int idx = 0; |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 215 | char ch; |
Matt Kraai | a0065d5 | 2001-08-24 14:45:50 +0000 | [diff] [blame] | 216 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 217 | for (; (ch = str[idx]); idx++) { |
Matt Kraai | a0065d5 | 2001-08-24 14:45:50 +0000 | [diff] [blame] | 218 | if (bracket != -1) { |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 219 | if (ch == ']' && !(bracket == idx - 1 || (bracket == idx - 2 |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 220 | && str[idx - 1] == '^'))) |
Matt Kraai | a0065d5 | 2001-08-24 14:45:50 +0000 | [diff] [blame] | 221 | bracket = -1; |
| 222 | } else if (escaped) |
| 223 | escaped = 0; |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 224 | else if (ch == '\\') |
Matt Kraai | a0065d5 | 2001-08-24 14:45:50 +0000 | [diff] [blame] | 225 | escaped = 1; |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 226 | else if (ch == '[') |
Matt Kraai | a0065d5 | 2001-08-24 14:45:50 +0000 | [diff] [blame] | 227 | bracket = idx; |
Glenn L McGrath | 1fb4467 | 2003-03-09 08:44:49 +0000 | [diff] [blame] | 228 | else if (ch == delimiter) |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 229 | return idx; |
| 230 | } |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 231 | |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 232 | /* if we make it to here, we've hit the end of the string */ |
| 233 | return -1; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 236 | /* |
| 237 | * Returns the index of the third delimiter |
| 238 | */ |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 239 | static int parse_regex_delim(const char *cmdstr, char **match, char **replace) |
| 240 | { |
| 241 | const char *cmdstr_ptr = cmdstr; |
| 242 | char delimiter; |
| 243 | int idx = 0; |
| 244 | |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 245 | /* verify that the 's' or 'y' is followed by something. That something |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 246 | * (typically a 'slash') is now our regexp delimiter... */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 247 | if (*cmdstr == '\0') bb_error_msg_and_die(bad_format_in_subst); |
| 248 | delimiter = *(cmdstr_ptr++); |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 249 | |
| 250 | /* save the match string */ |
| 251 | idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr); |
| 252 | if (idx == -1) { |
Glenn L McGrath | 9a52bb6 | 2003-03-30 09:38:40 +0000 | [diff] [blame] | 253 | bb_error_msg_and_die(bad_format_in_subst); |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 254 | } |
Rob Landley | fae1dc8 | 2005-11-20 07:44:35 +0000 | [diff] [blame] | 255 | *match = copy_parsing_escapes(cmdstr_ptr, idx); |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 256 | |
| 257 | /* save the replacement string */ |
| 258 | cmdstr_ptr += idx + 1; |
| 259 | idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr); |
| 260 | if (idx == -1) { |
Glenn L McGrath | 9a52bb6 | 2003-03-30 09:38:40 +0000 | [diff] [blame] | 261 | bb_error_msg_and_die(bad_format_in_subst); |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 262 | } |
Rob Landley | fae1dc8 | 2005-11-20 07:44:35 +0000 | [diff] [blame] | 263 | *replace = copy_parsing_escapes(cmdstr_ptr, idx); |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 264 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 265 | return ((cmdstr_ptr - cmdstr) + idx); |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 268 | /* |
| 269 | * returns the index in the string just past where the address ends. |
| 270 | */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 271 | static int get_address(char *my_str, int *linenum, regex_t ** regex) |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 272 | { |
Glenn L McGrath | e3e28d3 | 2003-09-15 09:22:04 +0000 | [diff] [blame] | 273 | char *pos = my_str; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 274 | |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 275 | if (isdigit(*my_str)) { |
| 276 | *linenum = strtol(my_str, &pos, 10); |
Glenn L McGrath | 1fb4467 | 2003-03-09 08:44:49 +0000 | [diff] [blame] | 277 | /* endstr shouldnt ever equal NULL */ |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 278 | } else if (*my_str == '$') { |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 279 | *linenum = -1; |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 280 | pos++; |
| 281 | } else if (*my_str == '/' || *my_str == '\\') { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 282 | int next; |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 283 | char delimiter; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 284 | char *temp; |
Glenn L McGrath | 1fb4467 | 2003-03-09 08:44:49 +0000 | [diff] [blame] | 285 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 286 | if (*my_str == '\\') delimiter = *(++pos); |
| 287 | else delimiter = '/'; |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 288 | next = index_of_next_unescaped_regexp_delim(delimiter, ++pos); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 289 | if (next == -1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 290 | bb_error_msg_and_die("unterminated match expression"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 291 | |
Rob Landley | fae1dc8 | 2005-11-20 07:44:35 +0000 | [diff] [blame] | 292 | temp=copy_parsing_escapes(pos,next); |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 293 | *regex = (regex_t *) xmalloc(sizeof(regex_t)); |
Eric Andersen | 9855548 | 2004-05-26 10:03:33 +0000 | [diff] [blame] | 294 | xregcomp(*regex, temp, regex_type|REG_NEWLINE); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 295 | free(temp); |
| 296 | /* Move position to next character after last delimiter */ |
| 297 | pos+=(next+1); |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 298 | } |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 299 | return pos - my_str; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 300 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 301 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 302 | /* Grab a filename. Whitespace at start is skipped, then goes to EOL. */ |
| 303 | static int parse_file_cmd(sed_cmd_t * sed_cmd, const char *filecmdstr, char **retval) |
| 304 | { |
| 305 | int start = 0, idx, hack=0; |
| 306 | |
| 307 | /* Skip whitespace, then grab filename to end of line */ |
| 308 | while (isspace(filecmdstr[start])) start++; |
| 309 | idx=start; |
| 310 | while(filecmdstr[idx] && filecmdstr[idx]!='\n') idx++; |
| 311 | /* If lines glued together, put backslash back. */ |
| 312 | if(filecmdstr[idx]=='\n') hack=1; |
| 313 | if(idx==start) bb_error_msg_and_die("Empty filename"); |
| 314 | *retval = bb_xstrndup(filecmdstr+start, idx-start+hack+1); |
| 315 | if(hack) *(idx+*retval)='\\'; |
| 316 | |
| 317 | return idx; |
| 318 | } |
| 319 | |
Eric Andersen | 638da75 | 2003-10-09 08:18:36 +0000 | [diff] [blame] | 320 | static int parse_subst_cmd(sed_cmd_t * const sed_cmd, char *substr) |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 321 | { |
Eric Andersen | 9855548 | 2004-05-26 10:03:33 +0000 | [diff] [blame] | 322 | int cflags = regex_type; |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 323 | char *match; |
| 324 | int idx = 0; |
| 325 | |
| 326 | /* |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 327 | * A substitution command should look something like this: |
| 328 | * s/match/replace/ #gIpw |
Mark Whitley | 0e4cec0 | 2000-08-21 21:29:20 +0000 | [diff] [blame] | 329 | * || | ||| |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 330 | * mandatory optional |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 331 | */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 332 | idx = parse_regex_delim(substr, &match, &sed_cmd->string); |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 333 | |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 334 | /* determine the number of back references in the match string */ |
| 335 | /* Note: we compute this here rather than in the do_subst_command() |
| 336 | * function to save processor time, at the expense of a little more memory |
| 337 | * (4 bits) per sed_cmd */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 338 | |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 339 | /* process the flags */ |
Mark Whitley | 70705d7 | 2000-07-14 19:06:30 +0000 | [diff] [blame] | 340 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 341 | sed_cmd->which_match=1; |
| 342 | while (substr[++idx]) { |
| 343 | /* Parse match number */ |
| 344 | if(isdigit(substr[idx])) { |
| 345 | if(match[0]!='^') { |
| 346 | /* Match 0 treated as all, multiple matches we take the last one. */ |
| 347 | char *pos=substr+idx; |
| 348 | sed_cmd->which_match=(unsigned short)strtol(substr+idx,&pos,10); |
| 349 | idx=pos-substr; |
| 350 | } |
| 351 | continue; |
| 352 | } |
Rob Landley | 40ec4ae | 2004-01-04 06:42:14 +0000 | [diff] [blame] | 353 | /* Skip spaces */ |
| 354 | if(isspace(substr[idx])) continue; |
| 355 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 356 | switch (substr[idx]) { |
| 357 | /* Replace all occurrences */ |
| 358 | case 'g': |
| 359 | if (match[0] != '^') sed_cmd->which_match = 0; |
| 360 | break; |
| 361 | /* Print pattern space */ |
| 362 | case 'p': |
| 363 | sed_cmd->sub_p = 1; |
| 364 | break; |
Rob Landley | fae1dc8 | 2005-11-20 07:44:35 +0000 | [diff] [blame] | 365 | /* Write to file */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 366 | case 'w': |
| 367 | { |
| 368 | char *temp; |
| 369 | idx+=parse_file_cmd(sed_cmd,substr+idx,&temp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 370 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 371 | break; |
| 372 | } |
| 373 | /* Ignore case (gnu exension) */ |
| 374 | case 'I': |
| 375 | cflags |= REG_ICASE; |
| 376 | break; |
Rob Landley | fae1dc8 | 2005-11-20 07:44:35 +0000 | [diff] [blame] | 377 | /* Comment */ |
| 378 | case '#': |
| 379 | while(substr[++idx]); |
| 380 | /* Fall through */ |
| 381 | /* End of command */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 382 | case ';': |
| 383 | case '}': |
| 384 | goto out; |
| 385 | default: |
| 386 | bb_error_msg_and_die("bad option in substitution expression"); |
| 387 | } |
| 388 | } |
Glenn L McGrath | 2570b43 | 2003-09-16 05:25:43 +0000 | [diff] [blame] | 389 | out: |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 390 | /* compile the match string into a regex */ |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 391 | if (*match != '\0') { |
| 392 | /* If match is empty, we use last regex used at runtime */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 393 | sed_cmd->sub_match = (regex_t *) xmalloc(sizeof(regex_t)); |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 394 | xregcomp(sed_cmd->sub_match, match, cflags); |
| 395 | } |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 396 | free(match); |
Mark Whitley | 70705d7 | 2000-07-14 19:06:30 +0000 | [diff] [blame] | 397 | |
| 398 | return idx; |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Glenn L McGrath | 2971ef1 | 2003-03-18 01:19:23 +0000 | [diff] [blame] | 401 | /* |
| 402 | * Process the commands arguments |
| 403 | */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 404 | static char *parse_cmd_args(sed_cmd_t *sed_cmd, char *cmdstr) |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 405 | { |
Glenn L McGrath | 2f8a401 | 2003-03-09 02:44:49 +0000 | [diff] [blame] | 406 | /* handle (s)ubstitution command */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 407 | if (sed_cmd->cmd == 's') cmdstr += parse_subst_cmd(sed_cmd, cmdstr); |
Glenn L McGrath | 2f8a401 | 2003-03-09 02:44:49 +0000 | [diff] [blame] | 408 | /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */ |
| 409 | else if (strchr("aic", sed_cmd->cmd)) { |
| 410 | if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c') |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 411 | bb_error_msg_and_die |
| 412 | ("only a beginning address can be specified for edit commands"); |
Rob Landley | 25d8239 | 2004-04-01 09:23:30 +0000 | [diff] [blame] | 413 | for(;;) { |
| 414 | if(*cmdstr=='\n' || *cmdstr=='\\') { |
| 415 | cmdstr++; |
| 416 | break; |
| 417 | } else if(isspace(*cmdstr)) cmdstr++; |
| 418 | else break; |
| 419 | } |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 420 | sed_cmd->string = bb_xstrdup(cmdstr); |
Glenn L McGrath | 738fb33 | 2003-10-01 06:45:11 +0000 | [diff] [blame] | 421 | parse_escapes(sed_cmd->string,sed_cmd->string,strlen(cmdstr),0,0); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 422 | cmdstr += strlen(cmdstr); |
Glenn L McGrath | 2f8a401 | 2003-03-09 02:44:49 +0000 | [diff] [blame] | 423 | /* handle file cmds: (r)ead */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 424 | } else if(strchr("rw", sed_cmd->cmd)) { |
Glenn L McGrath | 2f8a401 | 2003-03-09 02:44:49 +0000 | [diff] [blame] | 425 | if (sed_cmd->end_line || sed_cmd->end_match) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 426 | bb_error_msg_and_die("Command only uses one address"); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 427 | cmdstr += parse_file_cmd(sed_cmd, cmdstr, &sed_cmd->string); |
| 428 | if(sed_cmd->cmd=='w') |
| 429 | sed_cmd->file=bb_xfopen(sed_cmd->string,"w"); |
Glenn L McGrath | 961c6c1 | 2003-03-28 04:43:39 +0000 | [diff] [blame] | 430 | /* handle branch commands */ |
Rob Landley | 93850a5 | 2005-05-18 06:34:37 +0000 | [diff] [blame] | 431 | } else if (strchr(":btT", sed_cmd->cmd)) { |
Glenn L McGrath | 961c6c1 | 2003-03-28 04:43:39 +0000 | [diff] [blame] | 432 | int length; |
| 433 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 434 | while(isspace(*cmdstr)) cmdstr++; |
Glenn L McGrath | f452356 | 2003-09-14 06:01:14 +0000 | [diff] [blame] | 435 | length = strcspn(cmdstr, semicolon_whitespace); |
| 436 | if (length) { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 437 | sed_cmd->string = strndup(cmdstr, length); |
Glenn L McGrath | f452356 | 2003-09-14 06:01:14 +0000 | [diff] [blame] | 438 | cmdstr += length; |
| 439 | } |
Glenn L McGrath | 961c6c1 | 2003-03-28 04:43:39 +0000 | [diff] [blame] | 440 | } |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 441 | /* translation command */ |
| 442 | else if (sed_cmd->cmd == 'y') { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 443 | char *match, *replace; |
| 444 | int i=cmdstr[0]; |
| 445 | |
| 446 | cmdstr+=parse_regex_delim(cmdstr, &match, &replace)+1; |
| 447 | /* \n already parsed, but \delimiter needs unescaping. */ |
| 448 | parse_escapes(match,match,strlen(match),i,i); |
| 449 | parse_escapes(replace,replace,strlen(replace),i,i); |
| 450 | |
| 451 | sed_cmd->string = xcalloc(1, (strlen(match) + 1) * 2); |
| 452 | for (i = 0; match[i] && replace[i]; i++) { |
| 453 | sed_cmd->string[i * 2] = match[i]; |
| 454 | sed_cmd->string[(i * 2) + 1] = replace[i]; |
| 455 | } |
| 456 | free(match); |
| 457 | free(replace); |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 458 | } |
Glenn L McGrath | 2971ef1 | 2003-03-18 01:19:23 +0000 | [diff] [blame] | 459 | /* if it wasnt a single-letter command that takes no arguments |
| 460 | * then it must be an invalid command. |
| 461 | */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 462 | else if (strchr("dDgGhHlnNpPqx={}", sed_cmd->cmd) == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 463 | bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd); |
Mark Whitley | 1f3b9f2 | 2001-05-11 22:27:13 +0000 | [diff] [blame] | 464 | } |
Mark Whitley | 70705d7 | 2000-07-14 19:06:30 +0000 | [diff] [blame] | 465 | |
| 466 | /* give back whatever's left over */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 467 | return (cmdstr); |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 468 | } |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 469 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 470 | |
| 471 | /* Parse address+command sets, skipping comment lines. */ |
| 472 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 473 | static void add_cmd(char *cmdstr) |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 474 | { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 475 | static char *add_cmd_line=NULL; |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 476 | sed_cmd_t *sed_cmd; |
Glenn L McGrath | 586d86c | 2003-10-09 07:22:59 +0000 | [diff] [blame] | 477 | int temp; |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 478 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 479 | /* Append this line to any unfinished line from last time. */ |
| 480 | if(add_cmd_line) { |
| 481 | int lastlen=strlen(add_cmd_line); |
Eric Andersen | 638da75 | 2003-10-09 08:18:36 +0000 | [diff] [blame] | 482 | char *tmp=xmalloc(lastlen+strlen(cmdstr)+2); |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 483 | |
Eric Andersen | 638da75 | 2003-10-09 08:18:36 +0000 | [diff] [blame] | 484 | memcpy(tmp,add_cmd_line,lastlen); |
| 485 | tmp[lastlen]='\n'; |
| 486 | strcpy(tmp+lastlen+1,cmdstr); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 487 | free(add_cmd_line); |
Eric Andersen | 638da75 | 2003-10-09 08:18:36 +0000 | [diff] [blame] | 488 | cmdstr=add_cmd_line=tmp; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 489 | } else add_cmd_line=NULL; |
| 490 | |
| 491 | /* If this line ends with backslash, request next line. */ |
Glenn L McGrath | 586d86c | 2003-10-09 07:22:59 +0000 | [diff] [blame] | 492 | temp=strlen(cmdstr); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 493 | if(temp && cmdstr[temp-1]=='\\') { |
| 494 | if(!add_cmd_line) add_cmd_line=strdup(cmdstr); |
| 495 | add_cmd_line[temp-1]=0; |
| 496 | return; |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 497 | } |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 498 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 499 | /* Loop parsing all commands in this line. */ |
| 500 | while(*cmdstr) { |
| 501 | /* Skip leading whitespace and semicolons */ |
| 502 | cmdstr += strspn(cmdstr, semicolon_whitespace); |
| 503 | |
| 504 | /* If no more commands, exit. */ |
| 505 | if(!*cmdstr) break; |
| 506 | |
| 507 | /* if this is a comment, jump past it and keep going */ |
| 508 | if (*cmdstr == '#') { |
| 509 | /* "#n" is the same as using -n on the command line */ |
| 510 | if (cmdstr[1] == 'n') be_quiet++; |
| 511 | if(!(cmdstr=strpbrk(cmdstr, "\n\r"))) break; |
| 512 | continue; |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 513 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 514 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 515 | /* parse the command |
| 516 | * format is: [addr][,addr][!]cmd |
| 517 | * |----||-----||-| |
| 518 | * part1 part2 part3 |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 519 | */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 520 | |
| 521 | sed_cmd = xcalloc(1, sizeof(sed_cmd_t)); |
| 522 | |
| 523 | /* first part (if present) is an address: either a '$', a number or a /regex/ */ |
| 524 | cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match); |
| 525 | |
| 526 | /* second part (if present) will begin with a comma */ |
| 527 | if (*cmdstr == ',') { |
| 528 | int idx; |
| 529 | |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 530 | cmdstr++; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 531 | idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match); |
| 532 | if (!idx) bb_error_msg_and_die("get_address: no address found in string\n"); |
| 533 | cmdstr += idx; |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 534 | } |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 535 | |
| 536 | /* skip whitespace before the command */ |
| 537 | while (isspace(*cmdstr)) cmdstr++; |
| 538 | |
| 539 | /* Check for inversion flag */ |
| 540 | if (*cmdstr == '!') { |
| 541 | sed_cmd->invert = 1; |
| 542 | cmdstr++; |
| 543 | |
| 544 | /* skip whitespace before the command */ |
| 545 | while (isspace(*cmdstr)) cmdstr++; |
| 546 | } |
| 547 | |
| 548 | /* last part (mandatory) will be a command */ |
| 549 | if (!*cmdstr) bb_error_msg_and_die("missing command"); |
| 550 | sed_cmd->cmd = *(cmdstr++); |
| 551 | cmdstr = parse_cmd_args(sed_cmd, cmdstr); |
| 552 | |
| 553 | /* Add the command to the command array */ |
| 554 | sed_cmd_tail->next = sed_cmd; |
| 555 | sed_cmd_tail = sed_cmd_tail->next; |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 558 | /* If we glued multiple lines together, free the memory. */ |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame^] | 559 | free(add_cmd_line); |
| 560 | add_cmd_line=NULL; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 563 | /* Append to a string, reallocating memory as necessary. */ |
| 564 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 565 | static struct pipeline { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 566 | char *buf; /* Space to hold string */ |
| 567 | int idx; /* Space used */ |
| 568 | int len; /* Space allocated */ |
| 569 | } pipeline; |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 570 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 571 | #define PIPE_GROW 64 |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 572 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 573 | static void pipe_putc(char c) |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 574 | { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 575 | if(pipeline.idx==pipeline.len) { |
| 576 | pipeline.buf = xrealloc(pipeline.buf, pipeline.len + PIPE_GROW); |
| 577 | pipeline.len+=PIPE_GROW; |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 578 | } |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 579 | pipeline.buf[pipeline.idx++] = (c); |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 582 | static void do_subst_w_backrefs(const char *line, const char *replace) |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 583 | { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 584 | int i,j; |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 585 | |
| 586 | /* go through the replacement string */ |
| 587 | for (i = 0; replace[i]; i++) { |
| 588 | /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */ |
Rob Landley | c63fe91 | 2005-10-30 10:08:13 +0000 | [diff] [blame] | 589 | if (replace[i] == '\\' && replace[i+1]>='0' && replace[i+1]<='9') { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 590 | int backref=replace[++i]-'0'; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 591 | |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 592 | /* print out the text held in regmatch[backref] */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 593 | if(regmatch[backref].rm_so != -1) |
| 594 | for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++) |
| 595 | pipe_putc(line[j]); |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Mark Whitley | 83e85f6 | 2000-07-25 20:48:44 +0000 | [diff] [blame] | 598 | /* if we find a backslash escaped character, print the character */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 599 | else if (replace[i] == '\\') pipe_putc(replace[++i]); |
Mark Whitley | 83e85f6 | 2000-07-25 20:48:44 +0000 | [diff] [blame] | 600 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 601 | /* if we find an unescaped '&' print out the whole matched text. */ |
| 602 | else if (replace[i] == '&') |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 603 | for (j = regmatch[0].rm_so; j < regmatch[0].rm_eo; j++) |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 604 | pipe_putc(line[j]); |
| 605 | /* Otherwise just output the character. */ |
| 606 | else pipe_putc(replace[i]); |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 610 | static int do_subst_command(sed_cmd_t * sed_cmd, char **line) |
Mark Whitley | 4f7fe77 | 2000-07-13 20:01:58 +0000 | [diff] [blame] | 611 | { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 612 | char *oldline = *line; |
Mark Whitley | 4f7fe77 | 2000-07-13 20:01:58 +0000 | [diff] [blame] | 613 | int altered = 0; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 614 | int match_count=0; |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 615 | regex_t *current_regex; |
| 616 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 617 | /* Handle empty regex. */ |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 618 | if (sed_cmd->sub_match == NULL) { |
| 619 | current_regex = previous_regex_ptr; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 620 | if(!current_regex) |
| 621 | bb_error_msg_and_die("No previous regexp."); |
| 622 | } else previous_regex_ptr = current_regex = sed_cmd->sub_match; |
Mark Whitley | 4f7fe77 | 2000-07-13 20:01:58 +0000 | [diff] [blame] | 623 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 624 | /* Find the first match */ |
| 625 | if(REG_NOMATCH==regexec(current_regex, oldline, 10, regmatch, 0)) |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 626 | return 0; |
| 627 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 628 | /* Initialize temporary output buffer. */ |
| 629 | pipeline.buf=xmalloc(PIPE_GROW); |
| 630 | pipeline.len=PIPE_GROW; |
| 631 | pipeline.idx=0; |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 632 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 633 | /* Now loop through, substituting for matches */ |
| 634 | do { |
Mark Whitley | 4f7fe77 | 2000-07-13 20:01:58 +0000 | [diff] [blame] | 635 | int i; |
Mark Whitley | 4f7fe77 | 2000-07-13 20:01:58 +0000 | [diff] [blame] | 636 | |
Eric Andersen | c06f568 | 2004-02-04 10:57:46 +0000 | [diff] [blame] | 637 | /* Work around bug in glibc regexec, demonstrated by: |
| 638 | echo " a.b" | busybox sed 's [^ .]* x g' |
| 639 | The match_count check is so not to break |
| 640 | echo "hi" | busybox sed 's/^/!/g' */ |
| 641 | if(!regmatch[0].rm_so && !regmatch[0].rm_eo && match_count) { |
| 642 | pipe_putc(*(oldline++)); |
| 643 | continue; |
| 644 | } |
| 645 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 646 | match_count++; |
| 647 | |
| 648 | /* If we aren't interested in this match, output old line to |
| 649 | end of match and continue */ |
| 650 | if(sed_cmd->which_match && sed_cmd->which_match!=match_count) { |
| 651 | for(i=0;i<regmatch[0].rm_eo;i++) |
| 652 | pipe_putc(oldline[i]); |
| 653 | continue; |
| 654 | } |
| 655 | |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 656 | /* print everything before the match */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 657 | for (i = 0; i < regmatch[0].rm_so; i++) pipe_putc(oldline[i]); |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 658 | |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 659 | /* then print the substitution string */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 660 | do_subst_w_backrefs(oldline, sed_cmd->string); |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 661 | |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 662 | /* advance past the match */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 663 | oldline += regmatch[0].rm_eo; |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 664 | /* flag that something has changed */ |
| 665 | altered++; |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 666 | |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 667 | /* if we're not doing this globally, get out now */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 668 | if (sed_cmd->which_match) break; |
| 669 | } while (*oldline && (regexec(current_regex, oldline, 10, regmatch, 0) != REG_NOMATCH)); |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 670 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 671 | /* Copy rest of string into output pipeline */ |
| 672 | |
| 673 | while(*oldline) pipe_putc(*(oldline++)); |
| 674 | pipe_putc(0); |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 675 | |
Eric Andersen | b76cb68 | 2001-08-22 05:58:16 +0000 | [diff] [blame] | 676 | free(*line); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 677 | *line = pipeline.buf; |
Mark Whitley | 4f7fe77 | 2000-07-13 20:01:58 +0000 | [diff] [blame] | 678 | return altered; |
| 679 | } |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 680 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 681 | /* Set command pointer to point to this label. (Does not handle null label.) */ |
Glenn L McGrath | 961c6c1 | 2003-03-28 04:43:39 +0000 | [diff] [blame] | 682 | static sed_cmd_t *branch_to(const char *label) |
| 683 | { |
| 684 | sed_cmd_t *sed_cmd; |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 685 | |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 686 | for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 687 | if ((sed_cmd->cmd == ':') && (sed_cmd->string) && (strcmp(sed_cmd->string, label) == 0)) { |
Glenn L McGrath | f452356 | 2003-09-14 06:01:14 +0000 | [diff] [blame] | 688 | return (sed_cmd); |
Glenn L McGrath | 961c6c1 | 2003-03-28 04:43:39 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
Glenn L McGrath | f452356 | 2003-09-14 06:01:14 +0000 | [diff] [blame] | 691 | bb_error_msg_and_die("Can't find label for jump to `%s'", label); |
Glenn L McGrath | 961c6c1 | 2003-03-28 04:43:39 +0000 | [diff] [blame] | 692 | } |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 693 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 694 | /* Append copy of string to append buffer */ |
| 695 | static void append(char *s) |
| 696 | { |
| 697 | struct append_list *temp=calloc(1,sizeof(struct append_list)); |
| 698 | |
| 699 | if(append_head) |
| 700 | append_tail=(append_tail->next=temp); |
| 701 | else append_head=append_tail=temp; |
| 702 | temp->string=strdup(s); |
| 703 | } |
| 704 | |
| 705 | static void flush_append(void) |
| 706 | { |
| 707 | /* Output appended lines. */ |
| 708 | while(append_head) { |
Rob Landley | 53302f8 | 2004-02-18 09:54:15 +0000 | [diff] [blame] | 709 | fprintf(nonstdout,"%s\n",append_head->string); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 710 | append_tail=append_head->next; |
| 711 | free(append_head->string); |
| 712 | free(append_head); |
| 713 | append_head=append_tail; |
| 714 | } |
| 715 | append_head=append_tail=NULL; |
| 716 | } |
| 717 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 718 | static void add_input_file(FILE *file) |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 719 | { |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 720 | input_file_list=xrealloc(input_file_list,(input_file_count+1)*sizeof(FILE *)); |
| 721 | input_file_list[input_file_count++]=file; |
| 722 | } |
| 723 | |
| 724 | /* Get next line of input from input_file_list, flushing append buffer and |
| 725 | * noting if we ran out of files without a newline on the last line we read. |
| 726 | */ |
| 727 | static char *get_next_line(int *no_newline) |
| 728 | { |
| 729 | char *temp=NULL; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 730 | int len; |
| 731 | |
| 732 | flush_append(); |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 733 | while (current_input_file<input_file_count) { |
| 734 | temp = bb_get_chunk_from_file(input_file_list[current_input_file],&len); |
| 735 | if (temp) { |
| 736 | *no_newline = !(len && temp[len-1]=='\n'); |
| 737 | if (!*no_newline) temp[len-1] = 0; |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 738 | break; |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 739 | // Close this file and advance to next one |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 740 | } else fclose(input_file_list[current_input_file++]); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | return temp; |
| 744 | } |
| 745 | |
| 746 | /* Output line of text. missing_newline means the last line output did not |
| 747 | end with a newline. no_newline means this line does not end with a |
| 748 | newline. */ |
| 749 | |
| 750 | static int puts_maybe_newline(char *s, FILE *file, int missing_newline, int no_newline) |
| 751 | { |
| 752 | if(missing_newline) fputc('\n',file); |
| 753 | fputs(s,file); |
| 754 | if(!no_newline) fputc('\n',file); |
| 755 | |
Rob Landley | 53302f8 | 2004-02-18 09:54:15 +0000 | [diff] [blame] | 756 | if(ferror(file)) { |
| 757 | fprintf(stderr,"Write failed.\n"); |
| 758 | exit(4); /* It's what gnu sed exits with... */ |
| 759 | } |
| 760 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 761 | return no_newline; |
| 762 | } |
| 763 | |
Rob Landley | 53302f8 | 2004-02-18 09:54:15 +0000 | [diff] [blame] | 764 | #define sed_puts(s,n) missing_newline=puts_maybe_newline(s,nonstdout,missing_newline,n) |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 765 | |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 766 | /* Process all the lines in all the files */ |
| 767 | |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 768 | static void process_files(void) |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 769 | { |
Rob Landley | ce4f0e9 | 2004-10-30 06:54:19 +0000 | [diff] [blame] | 770 | char *pattern_space, *next_line; |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 771 | int linenum = 0, missing_newline=0; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 772 | int no_newline,next_no_newline=0; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 773 | |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 774 | /* Prime the pump */ |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 775 | next_line = get_next_line(&next_no_newline); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 776 | |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 777 | /* go through every line in each file */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 778 | for(;;) { |
Glenn L McGrath | c949bfa | 2003-03-28 03:53:31 +0000 | [diff] [blame] | 779 | sed_cmd_t *sed_cmd; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 780 | int substituted=0; |
| 781 | |
| 782 | /* Advance to next line. Stop if out of lines. */ |
| 783 | if(!(pattern_space=next_line)) break; |
| 784 | no_newline=next_no_newline; |
Glenn L McGrath | 505bd0f | 2003-03-08 05:21:02 +0000 | [diff] [blame] | 785 | |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 786 | /* Read one line in advance so we can act on the last line, |
| 787 | * the '$' address */ |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 788 | next_line = get_next_line(&next_no_newline); |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 789 | linenum++; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 790 | restart: |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 791 | /* for every line, go through all the commands */ |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 792 | for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) { |
Eric Andersen | 52a3c27 | 2003-12-23 08:53:51 +0000 | [diff] [blame] | 793 | int old_matched, matched; |
| 794 | |
| 795 | old_matched = sed_cmd->in_match; |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 796 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 797 | /* Determine if this command matches this line: */ |
| 798 | |
| 799 | /* Are we continuing a previous multi-line match? */ |
| 800 | |
| 801 | sed_cmd->in_match = sed_cmd->in_match |
| 802 | |
| 803 | /* Or is no range necessary? */ |
| 804 | || (!sed_cmd->beg_line && !sed_cmd->end_line |
| 805 | && !sed_cmd->beg_match && !sed_cmd->end_match) |
| 806 | |
| 807 | /* Or did we match the start of a numerical range? */ |
| 808 | || (sed_cmd->beg_line > 0 && (sed_cmd->beg_line == linenum)) |
| 809 | |
| 810 | /* Or does this line match our begin address regex? */ |
| 811 | || (sed_cmd->beg_match && |
| 812 | !regexec(sed_cmd->beg_match, pattern_space, 0, NULL, 0)) |
| 813 | |
| 814 | /* Or did we match last line of input? */ |
| 815 | || (sed_cmd->beg_line == -1 && next_line == NULL); |
| 816 | |
| 817 | /* Snapshot the value */ |
| 818 | |
| 819 | matched = sed_cmd->in_match; |
| 820 | |
| 821 | /* Is this line the end of the current match? */ |
| 822 | |
| 823 | if(matched) { |
| 824 | sed_cmd->in_match = !( |
| 825 | /* has the ending line come, or is this a single address command? */ |
| 826 | (sed_cmd->end_line ? |
| 827 | sed_cmd->end_line==-1 ? |
| 828 | !next_line |
| 829 | : sed_cmd->end_line<=linenum |
| 830 | : !sed_cmd->end_match) |
| 831 | /* or does this line matches our last address regex */ |
Eric Andersen | 52a3c27 | 2003-12-23 08:53:51 +0000 | [diff] [blame] | 832 | || (sed_cmd->end_match && old_matched && (regexec(sed_cmd->end_match, pattern_space, 0, NULL, 0) == 0)) |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 833 | ); |
Glenn L McGrath | fc4cb4d | 2003-04-12 16:10:42 +0000 | [diff] [blame] | 834 | } |
Glenn L McGrath | fc4cb4d | 2003-04-12 16:10:42 +0000 | [diff] [blame] | 835 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 836 | /* Skip blocks of commands we didn't match. */ |
| 837 | if (sed_cmd->cmd == '{') { |
| 838 | if(sed_cmd->invert ? matched : !matched) |
| 839 | while(sed_cmd && sed_cmd->cmd!='}') sed_cmd=sed_cmd->next; |
| 840 | if(!sed_cmd) bb_error_msg_and_die("Unterminated {"); |
| 841 | continue; |
| 842 | } |
| 843 | |
| 844 | /* Okay, so did this line match? */ |
| 845 | if (sed_cmd->invert ? !matched : matched) { |
| 846 | /* Update last used regex in case a blank substitute BRE is found */ |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 847 | if (sed_cmd->beg_match) { |
| 848 | previous_regex_ptr = sed_cmd->beg_match; |
| 849 | } |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 850 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 851 | /* actual sedding */ |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 852 | switch (sed_cmd->cmd) { |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 853 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 854 | /* Print line number */ |
| 855 | case '=': |
Rob Landley | 53302f8 | 2004-02-18 09:54:15 +0000 | [diff] [blame] | 856 | fprintf(nonstdout,"%d\n", linenum); |
Glenn L McGrath | 2eed0e2 | 2003-09-15 06:28:45 +0000 | [diff] [blame] | 857 | break; |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 858 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 859 | /* Write the current pattern space up to the first newline */ |
| 860 | case 'P': |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 861 | { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 862 | char *tmp = strchr(pattern_space, '\n'); |
Glenn L McGrath | 0c51832 | 2003-03-30 03:41:53 +0000 | [diff] [blame] | 863 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 864 | if (tmp) { |
| 865 | *tmp = '\0'; |
| 866 | sed_puts(pattern_space,1); |
| 867 | *tmp = '\n'; |
| 868 | break; |
| 869 | } |
| 870 | /* Fall Through */ |
| 871 | } |
| 872 | |
| 873 | /* Write the current pattern space to output */ |
| 874 | case 'p': |
| 875 | sed_puts(pattern_space,no_newline); |
| 876 | break; |
| 877 | /* Delete up through first newline */ |
| 878 | case 'D': |
| 879 | { |
| 880 | char *tmp = strchr(pattern_space,'\n'); |
| 881 | |
| 882 | if(tmp) { |
| 883 | tmp=bb_xstrdup(tmp+1); |
| 884 | free(pattern_space); |
| 885 | pattern_space=tmp; |
| 886 | goto restart; |
Glenn L McGrath | 0c51832 | 2003-03-30 03:41:53 +0000 | [diff] [blame] | 887 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 888 | } |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 889 | /* discard this line. */ |
| 890 | case 'd': |
| 891 | goto discard_line; |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 892 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 893 | /* Substitute with regex */ |
| 894 | case 's': |
| 895 | if(do_subst_command(sed_cmd, &pattern_space)) { |
| 896 | substituted|=1; |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 897 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 898 | /* handle p option */ |
| 899 | if(sed_cmd->sub_p) |
| 900 | sed_puts(pattern_space,no_newline); |
| 901 | /* handle w option */ |
| 902 | if(sed_cmd->file) |
| 903 | sed_cmd->no_newline=puts_maybe_newline(pattern_space, sed_cmd->file, sed_cmd->no_newline, no_newline); |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 904 | |
Glenn L McGrath | d7fe39b | 2003-04-09 15:52:32 +0000 | [diff] [blame] | 905 | } |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 906 | break; |
| 907 | |
| 908 | /* Append line to linked list to be printed later */ |
| 909 | case 'a': |
| 910 | { |
| 911 | append(sed_cmd->string); |
| 912 | break; |
Glenn L McGrath | d87a7ac | 2003-04-09 15:26:14 +0000 | [diff] [blame] | 913 | } |
Glenn L McGrath | d7fe39b | 2003-04-09 15:52:32 +0000 | [diff] [blame] | 914 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 915 | /* Insert text before this line */ |
| 916 | case 'i': |
| 917 | sed_puts(sed_cmd->string,1); |
Glenn L McGrath | 2570b43 | 2003-09-16 05:25:43 +0000 | [diff] [blame] | 918 | break; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 919 | |
| 920 | /* Cut and paste text (replace) */ |
| 921 | case 'c': |
| 922 | /* Only triggers on last line of a matching range. */ |
Rob Landley | ce4f0e9 | 2004-10-30 06:54:19 +0000 | [diff] [blame] | 923 | if (!sed_cmd->in_match) sed_puts(sed_cmd->string,0); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 924 | goto discard_line; |
| 925 | |
| 926 | /* Read file, append contents to output */ |
| 927 | case 'r': |
Glenn L McGrath | f452356 | 2003-09-14 06:01:14 +0000 | [diff] [blame] | 928 | { |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 929 | FILE *rfile; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 930 | |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 931 | rfile = fopen(sed_cmd->string, "r"); |
| 932 | if (rfile) { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 933 | char *line; |
| 934 | |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 935 | while ((line = bb_get_chomped_line_from_file(rfile)) |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 936 | != NULL) |
| 937 | append(line); |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 938 | bb_xprint_and_close_file(rfile); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | break; |
| 942 | } |
| 943 | |
| 944 | /* Write pattern space to file. */ |
| 945 | case 'w': |
| 946 | sed_cmd->no_newline=puts_maybe_newline(pattern_space,sed_cmd->file, sed_cmd->no_newline,no_newline); |
| 947 | break; |
| 948 | |
| 949 | /* Read next line from input */ |
| 950 | case 'n': |
| 951 | if (!be_quiet) |
| 952 | sed_puts(pattern_space,no_newline); |
| 953 | if (next_line) { |
| 954 | free(pattern_space); |
| 955 | pattern_space = next_line; |
| 956 | no_newline=next_no_newline; |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 957 | next_line = get_next_line(&next_no_newline); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 958 | linenum++; |
| 959 | break; |
| 960 | } |
| 961 | /* fall through */ |
| 962 | |
| 963 | /* Quit. End of script, end of input. */ |
| 964 | case 'q': |
| 965 | /* Exit the outer while loop */ |
| 966 | free(next_line); |
| 967 | next_line = NULL; |
| 968 | goto discard_commands; |
| 969 | |
| 970 | /* Append the next line to the current line */ |
| 971 | case 'N': |
| 972 | { |
| 973 | /* If no next line, jump to end of script and exit. */ |
| 974 | if (next_line == NULL) { |
| 975 | /* Jump to end of script and exit */ |
| 976 | free(next_line); |
| 977 | next_line = NULL; |
| 978 | goto discard_line; |
| 979 | /* append next_line, read new next_line. */ |
Glenn L McGrath | f452356 | 2003-09-14 06:01:14 +0000 | [diff] [blame] | 980 | } else { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 981 | int len=strlen(pattern_space); |
| 982 | |
| 983 | pattern_space = realloc(pattern_space, len + strlen(next_line) + 2); |
| 984 | pattern_space[len]='\n'; |
| 985 | strcpy(pattern_space+len+1, next_line); |
| 986 | no_newline=next_no_newline; |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 987 | next_line = get_next_line(&next_no_newline); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 988 | linenum++; |
Glenn L McGrath | f452356 | 2003-09-14 06:01:14 +0000 | [diff] [blame] | 989 | } |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 990 | break; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 991 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 992 | |
Rob Landley | 93850a5 | 2005-05-18 06:34:37 +0000 | [diff] [blame] | 993 | /* Test/branch if substitution occurred */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 994 | case 't': |
Rob Landley | 93850a5 | 2005-05-18 06:34:37 +0000 | [diff] [blame] | 995 | if(!substituted) break; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 996 | substituted=0; |
Rob Landley | 93850a5 | 2005-05-18 06:34:37 +0000 | [diff] [blame] | 997 | /* Fall through */ |
| 998 | /* Test/branch if substitution didn't occur */ |
| 999 | case 'T': |
| 1000 | if (substituted) break; |
| 1001 | /* Fall through */ |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1002 | /* Branch to label */ |
| 1003 | case 'b': |
| 1004 | if (!sed_cmd->string) goto discard_commands; |
| 1005 | else sed_cmd = branch_to(sed_cmd->string); |
| 1006 | break; |
| 1007 | /* Transliterate characters */ |
| 1008 | case 'y': |
| 1009 | { |
| 1010 | int i; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1011 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1012 | for (i = 0; pattern_space[i]; i++) { |
| 1013 | int j; |
| 1014 | |
| 1015 | for (j = 0; sed_cmd->string[j]; j += 2) { |
| 1016 | if (pattern_space[i] == sed_cmd->string[j]) { |
| 1017 | pattern_space[i] = sed_cmd->string[j + 1]; |
Rob Landley | babd3fb | 2005-09-02 00:10:06 +0000 | [diff] [blame] | 1018 | break; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1019 | } |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 1020 | } |
| 1021 | } |
Glenn L McGrath | 294d113 | 2003-09-14 16:28:08 +0000 | [diff] [blame] | 1022 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1023 | break; |
Glenn L McGrath | 91e1978 | 2003-04-26 07:40:07 +0000 | [diff] [blame] | 1024 | } |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1025 | case 'g': /* Replace pattern space with hold space */ |
| 1026 | free(pattern_space); |
Rob Landley | ce4f0e9 | 2004-10-30 06:54:19 +0000 | [diff] [blame] | 1027 | pattern_space = strdup(hold_space ? hold_space : ""); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1028 | break; |
| 1029 | case 'G': /* Append newline and hold space to pattern space */ |
| 1030 | { |
| 1031 | int pattern_space_size = 2; |
| 1032 | int hold_space_size = 0; |
| 1033 | |
| 1034 | if (pattern_space) |
| 1035 | pattern_space_size += strlen(pattern_space); |
| 1036 | if (hold_space) hold_space_size = strlen(hold_space); |
| 1037 | pattern_space = xrealloc(pattern_space, pattern_space_size + hold_space_size); |
| 1038 | if (pattern_space_size == 2) pattern_space[0]=0; |
Glenn L McGrath | 977451e | 2003-09-15 12:07:48 +0000 | [diff] [blame] | 1039 | strcat(pattern_space, "\n"); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1040 | if (hold_space) strcat(pattern_space, hold_space); |
| 1041 | no_newline=0; |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 1042 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1043 | break; |
Glenn L McGrath | 91e1978 | 2003-04-26 07:40:07 +0000 | [diff] [blame] | 1044 | } |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1045 | case 'h': /* Replace hold space with pattern space */ |
| 1046 | free(hold_space); |
| 1047 | hold_space = strdup(pattern_space); |
| 1048 | break; |
| 1049 | case 'H': /* Append newline and pattern space to hold space */ |
| 1050 | { |
| 1051 | int hold_space_size = 2; |
| 1052 | int pattern_space_size = 0; |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 1053 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1054 | if (hold_space) hold_space_size += strlen(hold_space); |
| 1055 | if (pattern_space) |
| 1056 | pattern_space_size = strlen(pattern_space); |
| 1057 | hold_space = xrealloc(hold_space, |
| 1058 | hold_space_size + pattern_space_size); |
| 1059 | |
| 1060 | if (hold_space_size == 2) hold_space[0]=0; |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 1061 | strcat(hold_space, "\n"); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1062 | if (pattern_space) strcat(hold_space, pattern_space); |
| 1063 | |
| 1064 | break; |
Glenn L McGrath | 4dc1d25 | 2003-09-14 01:25:31 +0000 | [diff] [blame] | 1065 | } |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1066 | case 'x': /* Exchange hold and pattern space */ |
| 1067 | { |
| 1068 | char *tmp = pattern_space; |
| 1069 | pattern_space = hold_space; |
| 1070 | no_newline=0; |
| 1071 | hold_space = tmp; |
| 1072 | break; |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 1073 | } |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 1074 | } |
Robert Griebl | 47abc49 | 2002-06-11 23:43:27 +0000 | [diff] [blame] | 1075 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1076 | } |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 1077 | |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1078 | /* |
| 1079 | * exit point from sedding... |
| 1080 | */ |
| 1081 | discard_commands: |
| 1082 | /* we will print the line unless we were told to be quiet ('-n') |
| 1083 | or if the line was suppressed (ala 'd'elete) */ |
| 1084 | if (!be_quiet) sed_puts(pattern_space,no_newline); |
| 1085 | |
| 1086 | /* Delete and such jump here. */ |
| 1087 | discard_line: |
| 1088 | flush_append(); |
Glenn L McGrath | c6adada | 2003-04-07 12:24:44 +0000 | [diff] [blame] | 1089 | free(pattern_space); |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1090 | } |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Glenn L McGrath | 42c2573 | 2003-10-04 05:27:56 +0000 | [diff] [blame] | 1093 | /* It is possible to have a command line argument with embedded |
| 1094 | newlines. This counts as multiple command lines. */ |
| 1095 | |
| 1096 | static void add_cmd_block(char *cmdstr) |
| 1097 | { |
| 1098 | int go=1; |
| 1099 | char *temp=bb_xstrdup(cmdstr),*temp2=temp; |
| 1100 | |
| 1101 | while(go) { |
| 1102 | int len=strcspn(temp2,"\n"); |
| 1103 | if(!temp2[len]) go=0; |
| 1104 | else temp2[len]=0; |
| 1105 | add_cmd(temp2); |
| 1106 | temp2+=len+1; |
| 1107 | } |
| 1108 | free(temp); |
| 1109 | } |
| 1110 | |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 1111 | extern int sed_main(int argc, char **argv) |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 1112 | { |
Rob Landley | ce4f0e9 | 2004-10-30 06:54:19 +0000 | [diff] [blame] | 1113 | int status = EXIT_SUCCESS, opt, getpat = 1; |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 1114 | |
Mark Whitley | 858c1ad | 2000-07-11 21:38:47 +0000 | [diff] [blame] | 1115 | /* destroy command strings on exit */ |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 1116 | if (ENABLE_FEATURE_CLEAN_UP && atexit(free_and_close_stuff) == -1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1117 | bb_perror_msg_and_die("atexit"); |
Mark Whitley | 858c1ad | 2000-07-11 21:38:47 +0000 | [diff] [blame] | 1118 | |
Rob Landley | fae1dc8 | 2005-11-20 07:44:35 +0000 | [diff] [blame] | 1119 | /* Lie to autoconf when it starts asking stupid questions. */ |
Eric Andersen | c06f568 | 2004-02-04 10:57:46 +0000 | [diff] [blame] | 1120 | if(argc==2 && !strcmp(argv[1],"--version")) { |
| 1121 | printf("This is not GNU sed version 4.0\n"); |
| 1122 | exit(0); |
| 1123 | } |
Eric Andersen | c06f568 | 2004-02-04 10:57:46 +0000 | [diff] [blame] | 1124 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1125 | /* do normal option parsing */ |
Eric Andersen | 9855548 | 2004-05-26 10:03:33 +0000 | [diff] [blame] | 1126 | while ((opt = getopt(argc, argv, "irne:f:")) > 0) { |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1127 | switch (opt) { |
Rob Landley | 53302f8 | 2004-02-18 09:54:15 +0000 | [diff] [blame] | 1128 | case 'i': |
| 1129 | in_place++; |
| 1130 | atexit(cleanup_outname); |
| 1131 | break; |
Eric Andersen | 9855548 | 2004-05-26 10:03:33 +0000 | [diff] [blame] | 1132 | case 'r': |
| 1133 | regex_type|=REG_EXTENDED; |
| 1134 | break; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1135 | case 'n': |
| 1136 | be_quiet++; |
| 1137 | break; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1138 | case 'e': |
Glenn L McGrath | 42c2573 | 2003-10-04 05:27:56 +0000 | [diff] [blame] | 1139 | add_cmd_block(optarg); |
Eric Andersen | faa7d86 | 2004-04-21 00:56:22 +0000 | [diff] [blame] | 1140 | getpat=0; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1141 | break; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1142 | case 'f': |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1143 | { |
| 1144 | FILE *cmdfile; |
| 1145 | char *line; |
| 1146 | |
| 1147 | cmdfile = bb_xfopen(optarg, "r"); |
| 1148 | |
Rob Landley | fae1dc8 | 2005-11-20 07:44:35 +0000 | [diff] [blame] | 1149 | while ((line = bb_get_chomped_line_from_file(cmdfile)) != NULL) { |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1150 | add_cmd(line); |
Eric Andersen | faa7d86 | 2004-04-21 00:56:22 +0000 | [diff] [blame] | 1151 | getpat=0; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1152 | free(line); |
| 1153 | } |
| 1154 | bb_xprint_and_close_file(cmdfile); |
| 1155 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1156 | break; |
Glenn L McGrath | aa5a602 | 2003-10-01 03:06:16 +0000 | [diff] [blame] | 1157 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1158 | default: |
| 1159 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1160 | } |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 1161 | } |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1162 | |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 1163 | /* if we didn't get a pattern from -e or -f, use argv[optind] */ |
Eric Andersen | faa7d86 | 2004-04-21 00:56:22 +0000 | [diff] [blame] | 1164 | if(getpat) { |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1165 | if (argv[optind] == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1166 | bb_show_usage(); |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 1167 | else |
Glenn L McGrath | 42c2573 | 2003-10-04 05:27:56 +0000 | [diff] [blame] | 1168 | add_cmd_block(argv[optind++]); |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1169 | } |
Glenn L McGrath | 42c2573 | 2003-10-04 05:27:56 +0000 | [diff] [blame] | 1170 | /* Flush any unfinished commands. */ |
| 1171 | add_cmd(""); |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1172 | |
Rob Landley | 53302f8 | 2004-02-18 09:54:15 +0000 | [diff] [blame] | 1173 | /* By default, we write to stdout */ |
| 1174 | nonstdout=stdout; |
| 1175 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1176 | /* argv[(optind)..(argc-1)] should be names of file to process. If no |
| 1177 | * files were specified or '-' was specified, take input from stdin. |
| 1178 | * Otherwise, we process all the files specified. */ |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 1179 | if (argv[optind] == NULL) { |
Rob Landley | 5797c7f | 2005-05-18 05:56:16 +0000 | [diff] [blame] | 1180 | if(in_place) bb_error_msg_and_die("Filename required for -i"); |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 1181 | add_input_file(stdin); |
| 1182 | process_files(); |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1183 | } else { |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1184 | int i; |
| 1185 | FILE *file; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1186 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1187 | for (i = optind; i < argc; i++) { |
Rob Landley | 53302f8 | 2004-02-18 09:54:15 +0000 | [diff] [blame] | 1188 | if(!strcmp(argv[i], "-") && !in_place) { |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 1189 | add_input_file(stdin); |
| 1190 | process_files(); |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 1191 | } else { |
| 1192 | file = bb_wfopen(argv[i], "r"); |
| 1193 | if (file) { |
Rob Landley | 53302f8 | 2004-02-18 09:54:15 +0000 | [diff] [blame] | 1194 | if(in_place) { |
| 1195 | struct stat statbuf; |
Rob Landley | 5797c7f | 2005-05-18 05:56:16 +0000 | [diff] [blame] | 1196 | int nonstdoutfd; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1197 | |
Rob Landley | 53302f8 | 2004-02-18 09:54:15 +0000 | [diff] [blame] | 1198 | outname=bb_xstrndup(argv[i],strlen(argv[i])+6); |
| 1199 | strcat(outname,"XXXXXX"); |
Rob Landley | 5797c7f | 2005-05-18 05:56:16 +0000 | [diff] [blame] | 1200 | if(-1==(nonstdoutfd=mkstemp(outname))) |
| 1201 | bb_error_msg_and_die("no temp file"); |
| 1202 | nonstdout=fdopen(nonstdoutfd,"w"); |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 1203 | |
Rob Landley | 53302f8 | 2004-02-18 09:54:15 +0000 | [diff] [blame] | 1204 | /* Set permissions of output file */ |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 1205 | |
Rob Landley | 53302f8 | 2004-02-18 09:54:15 +0000 | [diff] [blame] | 1206 | fstat(fileno(file),&statbuf); |
Rob Landley | 5797c7f | 2005-05-18 05:56:16 +0000 | [diff] [blame] | 1207 | fchmod(nonstdoutfd,statbuf.st_mode); |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 1208 | add_input_file(file); |
| 1209 | process_files(); |
Rob Landley | 53302f8 | 2004-02-18 09:54:15 +0000 | [diff] [blame] | 1210 | fclose(nonstdout); |
Rob Landley | 2b26fd5 | 2006-02-24 02:30:39 +0000 | [diff] [blame] | 1211 | |
Rob Landley | 53302f8 | 2004-02-18 09:54:15 +0000 | [diff] [blame] | 1212 | nonstdout=stdout; |
| 1213 | unlink(argv[i]); |
| 1214 | rename(outname,argv[i]); |
| 1215 | free(outname); |
| 1216 | outname=0; |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 1217 | } else add_input_file(file); |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 1218 | } else { |
| 1219 | status = EXIT_FAILURE; |
| 1220 | } |
| 1221 | } |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1222 | } |
Rob Landley | dcc2866 | 2004-11-25 07:21:47 +0000 | [diff] [blame] | 1223 | if(input_file_count>current_input_file) process_files(); |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1224 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1225 | |
Matt Kraai | a5f09c6 | 2001-11-12 16:44:55 +0000 | [diff] [blame] | 1226 | return status; |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 1227 | } |