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