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