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