Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 1 | /* |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 2 | * sed.c - very minimalist version of sed |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 3 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 4 | * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley |
| 5 | * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org> |
Matt Kraai | 5ed78ad | 2002-01-03 21:12:34 +0000 | [diff] [blame] | 6 | * Copyright (C) 2002 Matt Kraai |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 7 | * Copyright (C) 2003 by Glenn McGrath <bug1@optushome.com.au> |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 8 | * |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 25 | /* |
| 26 | Supported features and commands in this version of sed: |
| 27 | |
| 28 | - comments ('#') |
Mark Whitley | 94074a9 | 2000-07-14 00:00:15 +0000 | [diff] [blame] | 29 | - address matching: num|/matchstr/[,num|/matchstr/|$]command |
| 30 | - commands: (p)rint, (d)elete, (s)ubstitue (with g & I flags) |
| 31 | - edit commands: (a)ppend, (i)nsert, (c)hange |
Mark Whitley | 1f3b9f2 | 2001-05-11 22:27:13 +0000 | [diff] [blame] | 32 | - file commands: (r)ead |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 33 | - backreferences in substitution expressions (\1, \2...\9) |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 34 | - grouped commands: {cmd1;cmd2} |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 35 | - transliteration (y/source-chars/dest-chars/) |
| 36 | - pattern space hold space storing / swapping (g, h, x) |
| 37 | - labels / branching (: label, b, t) |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 38 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 39 | (Note: Specifying an address (range) to match is *optional*; commands |
| 40 | default to the whole pattern space if no specific address match was |
| 41 | requested.) |
| 42 | |
| 43 | Unsupported features: |
| 44 | |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 45 | - GNU extensions |
Glenn L McGrath | f36635c | 2003-09-13 15:12:22 +0000 | [diff] [blame] | 46 | - and more. |
Glenn L McGrath | d5eadea | 2003-03-09 02:39:29 +0000 | [diff] [blame] | 47 | |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 48 | Bugs: |
| 49 | |
Glenn L McGrath | f36635c | 2003-09-13 15:12:22 +0000 | [diff] [blame] | 50 | - lots |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 51 | |
Glenn L McGrath | d5eadea | 2003-03-09 02:39:29 +0000 | [diff] [blame] | 52 | Reference http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 53 | */ |
| 54 | |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 55 | #include <stdio.h> |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 56 | #include <unistd.h> /* for getopt() */ |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 57 | #include <regex.h> |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 58 | #include <string.h> /* for strdup() */ |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 59 | #include <errno.h> |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 60 | #include <ctype.h> /* for isspace() */ |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 61 | #include <stdlib.h> |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 62 | #include "busybox.h" |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 63 | |
Glenn L McGrath | e7a8bc9 | 2003-03-09 10:23:57 +0000 | [diff] [blame] | 64 | typedef struct sed_cmd_s { |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 65 | /* Order by alignment requirements */ |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 66 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 67 | /* address storage */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 68 | regex_t *beg_match; /* sed -e '/match/cmd' */ |
| 69 | regex_t *end_match; /* sed -e '/match/,/end_match/cmd' */ |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 70 | |
Glenn L McGrath | fc4cb4d | 2003-04-12 16:10:42 +0000 | [diff] [blame] | 71 | int beg_line; /* 'sed 1p' 0 == no begining line, apply commands to all lines */ |
| 72 | int end_line; /* 'sed 1,3p' 0 == no end line, use only beginning. -1 == $ */ |
| 73 | |
| 74 | /* inversion flag */ |
| 75 | int invert; /* the '!' after the address */ |
Glenn L McGrath | fc4cb4d | 2003-04-12 16:10:42 +0000 | [diff] [blame] | 76 | |
Glenn L McGrath | c18ce37 | 2003-09-13 06:57:39 +0000 | [diff] [blame] | 77 | /* Runtime flag no not if the current command match's */ |
| 78 | int still_in_range; |
| 79 | |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 80 | /* SUBSTITUTION COMMAND SPECIFIC FIELDS */ |
| 81 | |
| 82 | /* sed -e 's/sub_match/replace/' */ |
| 83 | regex_t *sub_match; |
| 84 | char *replace; |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 85 | |
| 86 | /* EDIT COMMAND (a,i,c) SPECIFIC FIELDS */ |
| 87 | char *editline; |
| 88 | |
| 89 | /* FILE COMMAND (r) SPECIFIC FIELDS */ |
| 90 | char *filename; |
| 91 | |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 92 | /* SUBSTITUTION COMMAND SPECIFIC FIELDS */ |
| 93 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 94 | unsigned int num_backrefs:4; /* how many back references (\1..\9) */ |
| 95 | /* Note: GNU/POSIX sed does not save more than nine backrefs, so |
| 96 | * we only use 4 bits to hold the number */ |
| 97 | unsigned int sub_g:1; /* sed -e 's/foo/bar/g' (global) */ |
| 98 | unsigned int sub_p:1; /* sed -e 's/foo/bar/p' (print substitution) */ |
Mark Whitley | 94074a9 | 2000-07-14 00:00:15 +0000 | [diff] [blame] | 99 | |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 100 | /* TRANSLATE COMMAND */ |
| 101 | char *translate; |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 102 | |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 103 | /* GENERAL FIELDS */ |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 104 | /* the command */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 105 | char cmd; /* p,d,s (add more at your leisure :-) */ |
Robert Griebl | 47abc49 | 2002-06-11 23:43:27 +0000 | [diff] [blame] | 106 | |
Glenn L McGrath | 961c6c1 | 2003-03-28 04:43:39 +0000 | [diff] [blame] | 107 | /* Branch commands */ |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 108 | char *label; |
Glenn L McGrath | 961c6c1 | 2003-03-28 04:43:39 +0000 | [diff] [blame] | 109 | |
Glenn L McGrath | c949bfa | 2003-03-28 03:53:31 +0000 | [diff] [blame] | 110 | /* next command in list (sequential list of specified commands) */ |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 111 | struct sed_cmd_s *next; |
Glenn L McGrath | c949bfa | 2003-03-28 03:53:31 +0000 | [diff] [blame] | 112 | |
Glenn L McGrath | e7a8bc9 | 2003-03-09 10:23:57 +0000 | [diff] [blame] | 113 | } sed_cmd_t; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 114 | |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 115 | |
| 116 | /* externs */ |
| 117 | extern void xregcomp(regex_t * preg, const char *regex, int cflags); |
| 118 | extern int optind; /* in unistd.h */ |
| 119 | extern char *optarg; /* ditto */ |
| 120 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 121 | /* globals */ |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 122 | /* options */ |
| 123 | static int be_quiet = 0; |
| 124 | static const char bad_format_in_subst[] = |
| 125 | "bad format in substitution expression"; |
| 126 | |
Glenn L McGrath | c949bfa | 2003-03-28 03:53:31 +0000 | [diff] [blame] | 127 | /* linked list of sed commands */ |
| 128 | static sed_cmd_t sed_cmd_head; |
| 129 | static sed_cmd_t *sed_cmd_tail = &sed_cmd_head; |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 130 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 131 | const char *const semicolon_whitespace = "; \n\r\t\v\0"; |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 132 | static regex_t *previous_regex_ptr = NULL; |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 133 | |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 134 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 135 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Matt Kraai | 0c390a7 | 2001-11-20 16:00:19 +0000 | [diff] [blame] | 136 | static void destroy_cmd_strs(void) |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 137 | { |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 138 | sed_cmd_t *sed_cmd = sed_cmd_head.next; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 139 | |
Glenn L McGrath | 56c633c | 2003-03-28 04:23:23 +0000 | [diff] [blame] | 140 | while (sed_cmd) { |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 141 | sed_cmd_t *sed_cmd_next = sed_cmd->next; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 142 | |
Glenn L McGrath | 56c633c | 2003-03-28 04:23:23 +0000 | [diff] [blame] | 143 | if (sed_cmd->beg_match) { |
| 144 | regfree(sed_cmd->beg_match); |
| 145 | free(sed_cmd->beg_match); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 146 | } |
Glenn L McGrath | 56c633c | 2003-03-28 04:23:23 +0000 | [diff] [blame] | 147 | if (sed_cmd->end_match) { |
| 148 | regfree(sed_cmd->end_match); |
| 149 | free(sed_cmd->end_match); |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 150 | } |
Glenn L McGrath | 56c633c | 2003-03-28 04:23:23 +0000 | [diff] [blame] | 151 | if (sed_cmd->sub_match) { |
| 152 | regfree(sed_cmd->sub_match); |
| 153 | free(sed_cmd->sub_match); |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 154 | } |
Glenn L McGrath | 56c633c | 2003-03-28 04:23:23 +0000 | [diff] [blame] | 155 | free(sed_cmd->replace); |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 156 | free(sed_cmd->editline); |
| 157 | free(sed_cmd->filename); |
| 158 | free(sed_cmd->translate); |
| 159 | free(sed_cmd->label); |
Glenn L McGrath | 56c633c | 2003-03-28 04:23:23 +0000 | [diff] [blame] | 160 | free(sed_cmd); |
| 161 | sed_cmd = sed_cmd_next; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 162 | } |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 163 | } |
Mark Whitley | c41e8c8 | 2000-07-12 23:35:21 +0000 | [diff] [blame] | 164 | #endif |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 165 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 166 | /* |
Eric Andersen | 28b3c53 | 2001-01-02 11:01:31 +0000 | [diff] [blame] | 167 | * index_of_next_unescaped_regexp_delim - walks left to right through a string |
| 168 | * beginning at a specified index and returns the index of the next regular |
| 169 | * expression delimiter (typically a forward * slash ('/')) not preceeded by |
| 170 | * a backslash ('\'). |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 171 | */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 172 | static int index_of_next_unescaped_regexp_delim(const char delimiter, |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 173 | const char *str) |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 174 | { |
Matt Kraai | a0065d5 | 2001-08-24 14:45:50 +0000 | [diff] [blame] | 175 | int bracket = -1; |
| 176 | int escaped = 0; |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 177 | int idx = 0; |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 178 | char ch; |
Matt Kraai | a0065d5 | 2001-08-24 14:45:50 +0000 | [diff] [blame] | 179 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 180 | for (; (ch = str[idx]); idx++) { |
Matt Kraai | a0065d5 | 2001-08-24 14:45:50 +0000 | [diff] [blame] | 181 | if (bracket != -1) { |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 182 | if (ch == ']' && !(bracket == idx - 1 || (bracket == idx - 2 |
| 183 | && str[idx - 1] == '^'))) |
Matt Kraai | a0065d5 | 2001-08-24 14:45:50 +0000 | [diff] [blame] | 184 | bracket = -1; |
| 185 | } else if (escaped) |
| 186 | escaped = 0; |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 187 | else if (ch == '\\') |
Matt Kraai | a0065d5 | 2001-08-24 14:45:50 +0000 | [diff] [blame] | 188 | escaped = 1; |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 189 | else if (ch == '[') |
Matt Kraai | a0065d5 | 2001-08-24 14:45:50 +0000 | [diff] [blame] | 190 | bracket = idx; |
Glenn L McGrath | 1fb4467 | 2003-03-09 08:44:49 +0000 | [diff] [blame] | 191 | else if (ch == delimiter) |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 192 | return idx; |
| 193 | } |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 194 | |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 195 | /* if we make it to here, we've hit the end of the string */ |
| 196 | return -1; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 199 | /* |
| 200 | * Returns the index of the third delimiter |
| 201 | */ |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 202 | static int parse_regex_delim(const char *cmdstr, char **match, char **replace) |
| 203 | { |
| 204 | const char *cmdstr_ptr = cmdstr; |
| 205 | char delimiter; |
| 206 | int idx = 0; |
| 207 | |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 208 | /* 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] | 209 | * (typically a 'slash') is now our regexp delimiter... */ |
| 210 | if (*cmdstr == '\0') |
Glenn L McGrath | 9a52bb6 | 2003-03-30 09:38:40 +0000 | [diff] [blame] | 211 | bb_error_msg_and_die(bad_format_in_subst); |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 212 | else |
| 213 | delimiter = *cmdstr_ptr; |
| 214 | |
| 215 | cmdstr_ptr++; |
| 216 | |
| 217 | /* save the match string */ |
| 218 | idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr); |
| 219 | if (idx == -1) { |
Glenn L McGrath | 9a52bb6 | 2003-03-30 09:38:40 +0000 | [diff] [blame] | 220 | bb_error_msg_and_die(bad_format_in_subst); |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 221 | } |
| 222 | *match = bb_xstrndup(cmdstr_ptr, idx); |
| 223 | |
| 224 | /* save the replacement string */ |
| 225 | cmdstr_ptr += idx + 1; |
| 226 | idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr); |
| 227 | if (idx == -1) { |
Glenn L McGrath | 9a52bb6 | 2003-03-30 09:38:40 +0000 | [diff] [blame] | 228 | bb_error_msg_and_die(bad_format_in_subst); |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 229 | } |
| 230 | *replace = bb_xstrndup(cmdstr_ptr, idx); |
| 231 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 232 | return ((cmdstr_ptr - cmdstr) + idx); |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 235 | /* |
| 236 | * returns the index in the string just past where the address ends. |
| 237 | */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 238 | static int get_address(char *my_str, int *linenum, regex_t ** regex) |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 239 | { |
Glenn L McGrath | e3e28d3 | 2003-09-15 09:22:04 +0000 | [diff] [blame] | 240 | char *pos = my_str; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 241 | |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 242 | if (isdigit(*my_str)) { |
| 243 | *linenum = strtol(my_str, &pos, 10); |
Glenn L McGrath | 1fb4467 | 2003-03-09 08:44:49 +0000 | [diff] [blame] | 244 | /* endstr shouldnt ever equal NULL */ |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 245 | } else if (*my_str == '$') { |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 246 | *linenum = -1; |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 247 | pos++; |
| 248 | } else if (*my_str == '/' || *my_str == '\\') { |
| 249 | int next, idx_start = 1; |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 250 | char delimiter; |
Glenn L McGrath | 1fb4467 | 2003-03-09 08:44:49 +0000 | [diff] [blame] | 251 | |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 252 | delimiter = '/'; |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 253 | if (*my_str == '\\') { |
Robert Griebl | 00f5ecb | 2002-08-06 23:13:31 +0000 | [diff] [blame] | 254 | idx_start++; |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 255 | delimiter = *(++pos); |
Robert Griebl | 7940147 | 2002-08-06 21:07:17 +0000 | [diff] [blame] | 256 | } |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 257 | next = index_of_next_unescaped_regexp_delim(delimiter, ++pos); |
| 258 | if (next == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 259 | bb_error_msg_and_die("unterminated match expression"); |
Glenn L McGrath | 1fb4467 | 2003-03-09 08:44:49 +0000 | [diff] [blame] | 260 | } |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 261 | pos += next; |
| 262 | *pos = '\0'; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 263 | |
| 264 | *regex = (regex_t *) xmalloc(sizeof(regex_t)); |
| 265 | xregcomp(*regex, my_str + idx_start, REG_NEWLINE); |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 266 | pos++; /* so it points to the next character after the last '/' */ |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 267 | } |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 268 | return pos - my_str; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 269 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 270 | |
Glenn L McGrath | e7a8bc9 | 2003-03-09 10:23:57 +0000 | [diff] [blame] | 271 | static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr) |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 272 | { |
Glenn L McGrath | e01f966 | 2003-03-18 08:37:57 +0000 | [diff] [blame] | 273 | int cflags = 0; |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 274 | char *match; |
| 275 | int idx = 0; |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 276 | int j; |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 277 | |
| 278 | /* |
| 279 | * the string that gets passed to this function should look like this: |
Mark Whitley | 0e4cec0 | 2000-08-21 21:29:20 +0000 | [diff] [blame] | 280 | * s/match/replace/gIp |
| 281 | * || | ||| |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 282 | * mandatory optional |
| 283 | * |
| 284 | * (all three of the '/' slashes are mandatory) |
| 285 | */ |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 286 | idx = parse_regex_delim(substr, &match, &sed_cmd->replace); |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 287 | |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 288 | /* determine the number of back references in the match string */ |
| 289 | /* Note: we compute this here rather than in the do_subst_command() |
| 290 | * function to save processor time, at the expense of a little more memory |
| 291 | * (4 bits) per sed_cmd */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 292 | |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 293 | for (j = 0; match[j]; j++) { |
| 294 | /* GNU/POSIX sed does not save more than nine backrefs */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 295 | if (match[j] == '\\' && match[j + 1] == '(' |
| 296 | && sed_cmd->num_backrefs <= 9) |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 297 | sed_cmd->num_backrefs++; |
| 298 | } |
| 299 | |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 300 | /* process the flags */ |
| 301 | while (substr[++idx]) { |
| 302 | switch (substr[idx]) { |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 303 | case 'g': |
Glenn L McGrath | f36635c | 2003-09-13 15:12:22 +0000 | [diff] [blame] | 304 | if (match[0] != '^') { |
| 305 | sed_cmd->sub_g = 1; |
| 306 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 307 | break; |
Glenn L McGrath | bed4033 | 2003-03-10 02:21:14 +0000 | [diff] [blame] | 308 | /* Hmm, i dont see the I option mentioned in the standard */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 309 | case 'I': |
| 310 | cflags |= REG_ICASE; |
| 311 | break; |
| 312 | case 'p': |
| 313 | sed_cmd->sub_p = 1; |
| 314 | break; |
| 315 | default: |
| 316 | /* any whitespace or semicolon trailing after a s/// is ok */ |
| 317 | if (strchr(semicolon_whitespace, substr[idx])) |
| 318 | goto out; |
| 319 | /* else */ |
| 320 | bb_error_msg_and_die("bad option in substitution expression"); |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
Mark Whitley | 70705d7 | 2000-07-14 19:06:30 +0000 | [diff] [blame] | 323 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 324 | out: |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 325 | /* compile the match string into a regex */ |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 326 | if (*match != '\0') { |
| 327 | /* If match is empty, we use last regex used at runtime */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 328 | sed_cmd->sub_match = (regex_t *) xmalloc(sizeof(regex_t)); |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 329 | xregcomp(sed_cmd->sub_match, match, cflags); |
| 330 | } |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 331 | free(match); |
Mark Whitley | 70705d7 | 2000-07-14 19:06:30 +0000 | [diff] [blame] | 332 | |
| 333 | return idx; |
Mark Whitley | 06f3529 | 2000-07-13 19:58:04 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 336 | static void replace_slash_n(char *string) |
| 337 | { |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 338 | char *dest; |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 339 | |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 340 | for (dest = string; *string; string++, dest++) { |
| 341 | if ((string[0] == '\\') && (string[1] == 'n')) { |
| 342 | *dest = '\n'; |
| 343 | string++; |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 344 | } else { |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 345 | *dest = *string; |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 348 | *dest=0; |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static int parse_translate_cmd(sed_cmd_t * const sed_cmd, const char *cmdstr) |
| 352 | { |
| 353 | char *match; |
| 354 | char *replace; |
| 355 | int idx; |
| 356 | int i; |
| 357 | |
| 358 | idx = parse_regex_delim(cmdstr, &match, &replace); |
| 359 | replace_slash_n(match); |
| 360 | replace_slash_n(replace); |
| 361 | sed_cmd->translate = xcalloc(1, (strlen(match) + 1) * 2); |
| 362 | for (i = 0; (match[i] != 0) && (replace[i] != 0); i++) { |
| 363 | sed_cmd->translate[i * 2] = match[i]; |
| 364 | sed_cmd->translate[(i * 2) + 1] = replace[i]; |
| 365 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 366 | return (idx + 1); |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 369 | static int parse_edit_cmd(sed_cmd_t * sed_cmd, const char *editstr) |
Mark Whitley | 94074a9 | 2000-07-14 00:00:15 +0000 | [diff] [blame] | 370 | { |
Matt Kraai | 5ed78ad | 2002-01-03 21:12:34 +0000 | [diff] [blame] | 371 | int i, j; |
Mark Whitley | 94074a9 | 2000-07-14 00:00:15 +0000 | [diff] [blame] | 372 | |
| 373 | /* |
| 374 | * the string that gets passed to this function should look like this: |
| 375 | * |
| 376 | * need one of these |
| 377 | * | |
| 378 | * | this backslash (immediately following the edit command) is mandatory |
| 379 | * | | |
| 380 | * [aic]\ |
| 381 | * TEXT1\ |
| 382 | * TEXT2\ |
| 383 | * TEXTN |
| 384 | * |
| 385 | * as soon as we hit a TEXT line that has no trailing '\', we're done. |
| 386 | * this means a command like: |
| 387 | * |
| 388 | * i\ |
| 389 | * INSERTME |
| 390 | * |
| 391 | * is a-ok. |
| 392 | * |
| 393 | */ |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 394 | if ((*editstr != '\\') || ((editstr[1] != '\n') && (editstr[1] != '\r'))) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 395 | bb_error_msg_and_die("bad format in edit expression"); |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 396 | } |
Mark Whitley | 94074a9 | 2000-07-14 00:00:15 +0000 | [diff] [blame] | 397 | |
| 398 | /* store the edit line text */ |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 399 | sed_cmd->editline = xmalloc(strlen(&editstr[2]) + 2); |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 400 | for (i = 2, j = 0; |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 401 | editstr[i] != '\0' && strchr("\r\n", editstr[i]) == NULL; i++, j++) { |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 402 | if ((editstr[i] == '\\') && strchr("\n\r", editstr[i + 1]) != NULL) { |
Matt Kraai | 5ed78ad | 2002-01-03 21:12:34 +0000 | [diff] [blame] | 403 | sed_cmd->editline[j] = '\n'; |
| 404 | i++; |
| 405 | } else |
| 406 | sed_cmd->editline[j] = editstr[i]; |
Mark Whitley | 94074a9 | 2000-07-14 00:00:15 +0000 | [diff] [blame] | 407 | } |
Mark Whitley | 70705d7 | 2000-07-14 19:06:30 +0000 | [diff] [blame] | 408 | |
Mark Whitley | 56c14a6 | 2001-04-20 23:41:44 +0000 | [diff] [blame] | 409 | /* figure out if we need to add a newline */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 410 | if (sed_cmd->editline[j - 1] != '\n') |
Matt Kraai | 5ed78ad | 2002-01-03 21:12:34 +0000 | [diff] [blame] | 411 | sed_cmd->editline[j++] = '\n'; |
Mark Whitley | 56c14a6 | 2001-04-20 23:41:44 +0000 | [diff] [blame] | 412 | |
| 413 | /* terminate string */ |
Matt Kraai | 5ed78ad | 2002-01-03 21:12:34 +0000 | [diff] [blame] | 414 | sed_cmd->editline[j] = '\0'; |
Mark Whitley | 464c5de | 2000-07-14 23:24:00 +0000 | [diff] [blame] | 415 | |
Matt Kraai | 5ed78ad | 2002-01-03 21:12:34 +0000 | [diff] [blame] | 416 | return i; |
Mark Whitley | 94074a9 | 2000-07-14 00:00:15 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Mark Whitley | 1f3b9f2 | 2001-05-11 22:27:13 +0000 | [diff] [blame] | 419 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 420 | static int parse_file_cmd(sed_cmd_t * sed_cmd, const char *filecmdstr) |
Mark Whitley | 1f3b9f2 | 2001-05-11 22:27:13 +0000 | [diff] [blame] | 421 | { |
| 422 | int idx = 0; |
| 423 | int filenamelen = 0; |
| 424 | |
| 425 | /* |
| 426 | * the string that gets passed to this function should look like this: |
| 427 | * '[ ]filename' |
| 428 | * | | |
| 429 | * | a filename |
| 430 | * | |
| 431 | * optional whitespace |
| 432 | |
| 433 | * re: the file to be read, the GNU manual says the following: "Note that |
| 434 | * if filename cannot be read, it is treated as if it were an empty file, |
| 435 | * without any error indication." Thus, all of the following commands are |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 436 | * perfectly legal: |
Mark Whitley | 1f3b9f2 | 2001-05-11 22:27:13 +0000 | [diff] [blame] | 437 | * |
| 438 | * sed -e '1r noexist' |
| 439 | * sed -e '1r ;' |
| 440 | * sed -e '1r' |
| 441 | */ |
| 442 | |
| 443 | /* the file command may be followed by whitespace; move past it. */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 444 | while (isspace(filecmdstr[++idx])) {; |
| 445 | } |
| 446 | |
Mark Whitley | 1f3b9f2 | 2001-05-11 22:27:13 +0000 | [diff] [blame] | 447 | /* the first non-whitespace we get is a filename. the filename ends when we |
| 448 | * hit a normal sed command terminator or end of string */ |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 449 | filenamelen = strcspn(&filecmdstr[idx], semicolon_whitespace); |
Matt Kraai | 6e9e136 | 2001-05-27 14:11:52 +0000 | [diff] [blame] | 450 | sed_cmd->filename = xmalloc(filenamelen + 1); |
| 451 | safe_strncpy(sed_cmd->filename, &filecmdstr[idx], filenamelen + 1); |
Mark Whitley | 1f3b9f2 | 2001-05-11 22:27:13 +0000 | [diff] [blame] | 452 | return idx + filenamelen; |
| 453 | } |
| 454 | |
Glenn L McGrath | 2971ef1 | 2003-03-18 01:19:23 +0000 | [diff] [blame] | 455 | /* |
| 456 | * Process the commands arguments |
| 457 | */ |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 458 | static char *parse_cmd_str(sed_cmd_t * sed_cmd, char *cmdstr) |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 459 | { |
Glenn L McGrath | 2f8a401 | 2003-03-09 02:44:49 +0000 | [diff] [blame] | 460 | /* handle (s)ubstitution command */ |
Glenn L McGrath | 2971ef1 | 2003-03-18 01:19:23 +0000 | [diff] [blame] | 461 | if (sed_cmd->cmd == 's') { |
Glenn L McGrath | 4c6523a | 2003-03-09 11:06:38 +0000 | [diff] [blame] | 462 | cmdstr += parse_subst_cmd(sed_cmd, cmdstr); |
Glenn L McGrath | 2f8a401 | 2003-03-09 02:44:49 +0000 | [diff] [blame] | 463 | } |
| 464 | /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */ |
| 465 | else if (strchr("aic", sed_cmd->cmd)) { |
| 466 | 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] | 467 | bb_error_msg_and_die |
| 468 | ("only a beginning address can be specified for edit commands"); |
Glenn L McGrath | 4c6523a | 2003-03-09 11:06:38 +0000 | [diff] [blame] | 469 | cmdstr += parse_edit_cmd(sed_cmd, cmdstr); |
Glenn L McGrath | 2f8a401 | 2003-03-09 02:44:49 +0000 | [diff] [blame] | 470 | } |
| 471 | /* handle file cmds: (r)ead */ |
| 472 | else if (sed_cmd->cmd == 'r') { |
| 473 | if (sed_cmd->end_line || sed_cmd->end_match) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 474 | bb_error_msg_and_die("Command only uses one address"); |
Glenn L McGrath | 4c6523a | 2003-03-09 11:06:38 +0000 | [diff] [blame] | 475 | cmdstr += parse_file_cmd(sed_cmd, cmdstr); |
Glenn L McGrath | 2f8a401 | 2003-03-09 02:44:49 +0000 | [diff] [blame] | 476 | } |
Glenn L McGrath | 961c6c1 | 2003-03-28 04:43:39 +0000 | [diff] [blame] | 477 | /* handle branch commands */ |
Glenn L McGrath | 595a6a4 | 2003-03-28 08:36:52 +0000 | [diff] [blame] | 478 | else if (strchr(":bt", sed_cmd->cmd)) { |
Glenn L McGrath | 961c6c1 | 2003-03-28 04:43:39 +0000 | [diff] [blame] | 479 | int length; |
| 480 | |
| 481 | cmdstr += strspn(cmdstr, " "); |
Glenn L McGrath | f452356 | 2003-09-14 06:01:14 +0000 | [diff] [blame] | 482 | length = strcspn(cmdstr, semicolon_whitespace); |
| 483 | if (length) { |
| 484 | sed_cmd->label = strndup(cmdstr, length); |
| 485 | cmdstr += length; |
| 486 | } |
Glenn L McGrath | 961c6c1 | 2003-03-28 04:43:39 +0000 | [diff] [blame] | 487 | } |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 488 | /* translation command */ |
| 489 | else if (sed_cmd->cmd == 'y') { |
| 490 | cmdstr += parse_translate_cmd(sed_cmd, cmdstr); |
| 491 | } |
Glenn L McGrath | 2971ef1 | 2003-03-18 01:19:23 +0000 | [diff] [blame] | 492 | /* if it wasnt a single-letter command that takes no arguments |
| 493 | * then it must be an invalid command. |
| 494 | */ |
Glenn L McGrath | 91e1978 | 2003-04-26 07:40:07 +0000 | [diff] [blame] | 495 | else if (strchr("dgGhHnNpPqx={}", sed_cmd->cmd) == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 496 | bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd); |
Mark Whitley | 1f3b9f2 | 2001-05-11 22:27:13 +0000 | [diff] [blame] | 497 | } |
Mark Whitley | 70705d7 | 2000-07-14 19:06:30 +0000 | [diff] [blame] | 498 | |
| 499 | /* give back whatever's left over */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 500 | return (cmdstr); |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 501 | } |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 502 | |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 503 | static char *add_cmd(char *cmdstr) |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 504 | { |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 505 | sed_cmd_t *sed_cmd; |
| 506 | |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 507 | /* Skip over leading whitespace and semicolons */ |
| 508 | cmdstr += strspn(cmdstr, semicolon_whitespace); |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 509 | |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 510 | /* if we ate the whole thing, that means there was just trailing |
| 511 | * whitespace or a final / no-op semicolon. either way, get out */ |
| 512 | if (*cmdstr == '\0') { |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 513 | return (NULL); |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 514 | } |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 515 | |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 516 | /* if this is a comment, jump past it and keep going */ |
| 517 | if (*cmdstr == '#') { |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 518 | /* "#n" is the same as using -n on the command line */ |
| 519 | if (cmdstr[1] == 'n') { |
| 520 | be_quiet++; |
| 521 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 522 | return (strpbrk(cmdstr, "\n\r")); |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 523 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 524 | |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 525 | /* parse the command |
| 526 | * format is: [addr][,addr]cmd |
| 527 | * |----||-----||-| |
| 528 | * part1 part2 part3 |
| 529 | */ |
| 530 | |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 531 | sed_cmd = xcalloc(1, sizeof(sed_cmd_t)); |
| 532 | |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 533 | /* first part (if present) is an address: either a '$', a number or a /regex/ */ |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 534 | cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match); |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 535 | |
| 536 | /* second part (if present) will begin with a comma */ |
| 537 | if (*cmdstr == ',') { |
| 538 | int idx; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 539 | |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 540 | cmdstr++; |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 541 | idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match); |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 542 | if (idx == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 543 | bb_error_msg_and_die("get_address: no address found in string\n" |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 544 | "\t(you probably didn't check the string you passed me)"); |
Mark Whitley | 70705d7 | 2000-07-14 19:06:30 +0000 | [diff] [blame] | 545 | } |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 546 | cmdstr += idx; |
| 547 | } |
Mark Whitley | 70705d7 | 2000-07-14 19:06:30 +0000 | [diff] [blame] | 548 | |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 549 | /* skip whitespace before the command */ |
| 550 | while (isspace(*cmdstr)) { |
| 551 | cmdstr++; |
| 552 | } |
| 553 | |
| 554 | /* there my be the inversion flag between part2 and part3 */ |
| 555 | if (*cmdstr == '!') { |
| 556 | sed_cmd->invert = 1; |
| 557 | cmdstr++; |
| 558 | |
| 559 | #ifdef SED_FEATURE_STRICT_CHECKING |
| 560 | /* According to the spec |
| 561 | * It is unspecified whether <blank>s can follow a '!' character, |
| 562 | * and conforming applications shall not follow a '!' character |
| 563 | * with <blank>s. |
| 564 | */ |
| 565 | if (isblank(cmdstr[idx]) { |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 566 | bb_error_msg_and_die("blank follows '!'");} |
| 567 | #else |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 568 | /* skip whitespace before the command */ |
| 569 | while (isspace(*cmdstr)) { |
| 570 | cmdstr++; |
| 571 | } |
| 572 | #endif |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* last part (mandatory) will be a command */ |
| 576 | if (*cmdstr == '\0') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 577 | bb_error_msg_and_die("missing command"); |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 578 | |
| 579 | sed_cmd->cmd = *cmdstr; |
| 580 | cmdstr++; |
| 581 | |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 582 | cmdstr = parse_cmd_str(sed_cmd, cmdstr); |
| 583 | |
| 584 | /* Add the command to the command array */ |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 585 | sed_cmd_tail->next = sed_cmd; |
| 586 | sed_cmd_tail = sed_cmd_tail->next; |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 587 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 588 | return (cmdstr); |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Glenn L McGrath | e3e28d3 | 2003-09-15 09:22:04 +0000 | [diff] [blame] | 591 | static void add_cmd_str(const char *cmdstr) |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 592 | { |
Glenn L McGrath | e3e28d3 | 2003-09-15 09:22:04 +0000 | [diff] [blame] | 593 | char *cmdstr_expanded = strdup(cmdstr); |
| 594 | char *cmdstr_ptr; |
Glenn L McGrath | 0c51832 | 2003-03-30 03:41:53 +0000 | [diff] [blame] | 595 | |
Glenn L McGrath | e3e28d3 | 2003-09-15 09:22:04 +0000 | [diff] [blame] | 596 | #ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE |
| 597 | cmdstr_ptr = cmdstr_expanded; |
Glenn L McGrath | 0c51832 | 2003-03-30 03:41:53 +0000 | [diff] [blame] | 598 | /* HACK: convert "\n" to match tranlated '\n' string */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 599 | while ((cmdstr_ptr = strstr(cmdstr_ptr, "\\n")) != NULL) { |
Glenn L McGrath | e3e28d3 | 2003-09-15 09:22:04 +0000 | [diff] [blame] | 600 | int length = strlen(cmdstr) + 2; |
| 601 | cmdstr_expanded = realloc(cmdstr_expanded, length); |
| 602 | cmdstr_ptr = strstr(cmdstr_expanded, "\\n"); |
Glenn L McGrath | 0c51832 | 2003-03-30 03:41:53 +0000 | [diff] [blame] | 603 | memmove(cmdstr_ptr + 1, cmdstr_ptr, strlen(cmdstr_ptr) + 1); |
| 604 | cmdstr_ptr[0] = '\\'; |
| 605 | cmdstr_ptr += 3; |
| 606 | } |
| 607 | #endif |
Glenn L McGrath | e3e28d3 | 2003-09-15 09:22:04 +0000 | [diff] [blame] | 608 | cmdstr_ptr = cmdstr_expanded; |
Glenn L McGrath | f50ce31 | 2003-03-09 15:12:24 +0000 | [diff] [blame] | 609 | do { |
Glenn L McGrath | e3e28d3 | 2003-09-15 09:22:04 +0000 | [diff] [blame] | 610 | cmdstr_ptr = add_cmd(cmdstr_ptr); |
| 611 | } while (cmdstr_ptr && strlen(cmdstr_ptr)); |
| 612 | |
| 613 | free(cmdstr_expanded); |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | |
Glenn L McGrath | e3e28d3 | 2003-09-15 09:22:04 +0000 | [diff] [blame] | 617 | static void load_cmd_file(const char *filename) |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 618 | { |
| 619 | FILE *cmdfile; |
| 620 | char *line; |
Mark Whitley | 94074a9 | 2000-07-14 00:00:15 +0000 | [diff] [blame] | 621 | char *nextline; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 622 | char *e; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 623 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 624 | cmdfile = bb_xfopen(filename, "r"); |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 625 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 626 | while ((line = bb_get_line_from_file(cmdfile)) != NULL) { |
Mark Whitley | 94074a9 | 2000-07-14 00:00:15 +0000 | [diff] [blame] | 627 | /* if a line ends with '\' it needs the next line appended to it */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 628 | while (((e = last_char_is(line, '\n')) != NULL) |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 629 | && (e > line) && (e[-1] == '\\') |
| 630 | && ((nextline = bb_get_line_from_file(cmdfile)) != NULL)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 631 | line = xrealloc(line, (e - line) + 1 + strlen(nextline) + 1); |
Mark Whitley | 94074a9 | 2000-07-14 00:00:15 +0000 | [diff] [blame] | 632 | strcat(line, nextline); |
Mark Whitley | 464c5de | 2000-07-14 23:24:00 +0000 | [diff] [blame] | 633 | free(nextline); |
Mark Whitley | 94074a9 | 2000-07-14 00:00:15 +0000 | [diff] [blame] | 634 | } |
Mark Whitley | 464c5de | 2000-07-14 23:24:00 +0000 | [diff] [blame] | 635 | /* eat trailing newline (if any) --if I don't do this, edit commands |
| 636 | * (aic) will print an extra newline */ |
Matt Kraai | 05e782d | 2001-02-01 16:49:30 +0000 | [diff] [blame] | 637 | chomp(line); |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 638 | add_cmd_str(line); |
| 639 | free(line); |
| 640 | } |
| 641 | } |
| 642 | |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 643 | struct pipeline { |
| 644 | char *buf; |
| 645 | int idx; |
| 646 | int len; |
| 647 | }; |
| 648 | |
Eric Andersen | b76cb68 | 2001-08-22 05:58:16 +0000 | [diff] [blame] | 649 | #define PIPE_MAGIC 0x7f |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 650 | #define PIPE_GROW 64 |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 651 | |
| 652 | void pipe_putc(struct pipeline *const pipeline, char c) |
| 653 | { |
| 654 | if (pipeline->buf[pipeline->idx] == PIPE_MAGIC) { |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 655 | pipeline->buf = xrealloc(pipeline->buf, pipeline->len + PIPE_GROW); |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 656 | memset(pipeline->buf + pipeline->len, 0, PIPE_GROW); |
| 657 | pipeline->len += PIPE_GROW; |
| 658 | pipeline->buf[pipeline->len - 1] = PIPE_MAGIC; |
| 659 | } |
| 660 | pipeline->buf[pipeline->idx++] = (c); |
| 661 | } |
| 662 | |
| 663 | #define pipeputc(c) pipe_putc(pipeline, c) |
| 664 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 665 | static void print_subst_w_backrefs(const char *line, const char *replace, |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 666 | regmatch_t * regmatch, struct pipeline *const pipeline, int matches) |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 667 | { |
| 668 | int i; |
| 669 | |
| 670 | /* go through the replacement string */ |
| 671 | for (i = 0; replace[i]; i++) { |
| 672 | /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 673 | if (replace[i] == '\\' && isdigit(replace[i + 1])) { |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 674 | int j; |
| 675 | char tmpstr[2]; |
| 676 | int backref; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 677 | |
| 678 | ++i; /* i now indexes the backref number, instead of the leading slash */ |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 679 | tmpstr[0] = replace[i]; |
| 680 | tmpstr[1] = 0; |
| 681 | backref = atoi(tmpstr); |
| 682 | /* print out the text held in regmatch[backref] */ |
Matt Kraai | a3e4f45 | 2001-08-20 21:21:06 +0000 | [diff] [blame] | 683 | if (backref <= matches && regmatch[backref].rm_so != -1) |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 684 | for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 685 | j++) |
Eric Andersen | b76cb68 | 2001-08-22 05:58:16 +0000 | [diff] [blame] | 686 | pipeputc(line[j]); |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Mark Whitley | 83e85f6 | 2000-07-25 20:48:44 +0000 | [diff] [blame] | 689 | /* if we find a backslash escaped character, print the character */ |
| 690 | else if (replace[i] == '\\') { |
| 691 | ++i; |
Eric Andersen | b76cb68 | 2001-08-22 05:58:16 +0000 | [diff] [blame] | 692 | pipeputc(replace[i]); |
Mark Whitley | 83e85f6 | 2000-07-25 20:48:44 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 695 | /* if we find an unescaped '&' print out the whole matched text. |
| 696 | * fortunately, regmatch[0] contains the indicies to the whole matched |
| 697 | * expression (kinda seems like it was designed for just such a |
| 698 | * purpose...) */ |
Glenn L McGrath | 2410386 | 2003-04-09 07:51:43 +0000 | [diff] [blame] | 699 | else if (replace[i] == '&') { |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 700 | int j; |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 701 | |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 702 | for (j = regmatch[0].rm_so; j < regmatch[0].rm_eo; j++) |
Eric Andersen | b76cb68 | 2001-08-22 05:58:16 +0000 | [diff] [blame] | 703 | pipeputc(line[j]); |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 704 | } |
| 705 | /* nothing special, just print this char of the replacement string to stdout */ |
| 706 | else |
Eric Andersen | b76cb68 | 2001-08-22 05:58:16 +0000 | [diff] [blame] | 707 | pipeputc(replace[i]); |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 711 | static int do_subst_command(sed_cmd_t * sed_cmd, char **line) |
Mark Whitley | 4f7fe77 | 2000-07-13 20:01:58 +0000 | [diff] [blame] | 712 | { |
Eric Andersen | b76cb68 | 2001-08-22 05:58:16 +0000 | [diff] [blame] | 713 | char *hackline = *line; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 714 | struct pipeline thepipe = { NULL, 0, 0 }; |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 715 | struct pipeline *const pipeline = &thepipe; |
Mark Whitley | 4f7fe77 | 2000-07-13 20:01:58 +0000 | [diff] [blame] | 716 | int altered = 0; |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 717 | int result; |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 718 | regmatch_t *regmatch = NULL; |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 719 | regex_t *current_regex; |
| 720 | |
| 721 | if (sed_cmd->sub_match == NULL) { |
| 722 | current_regex = previous_regex_ptr; |
| 723 | } else { |
| 724 | previous_regex_ptr = current_regex = sed_cmd->sub_match; |
| 725 | } |
| 726 | result = regexec(current_regex, hackline, 0, NULL, 0); |
Mark Whitley | 4f7fe77 | 2000-07-13 20:01:58 +0000 | [diff] [blame] | 727 | |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 728 | /* we only proceed if the substitution 'search' expression matches */ |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 729 | if (result == REG_NOMATCH) { |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 730 | return 0; |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 731 | } |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 732 | |
| 733 | /* whaddaya know, it matched. get the number of back references */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 734 | regmatch = xmalloc(sizeof(regmatch_t) * (sed_cmd->num_backrefs + 1)); |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 735 | |
Eric Andersen | b76cb68 | 2001-08-22 05:58:16 +0000 | [diff] [blame] | 736 | /* allocate more PIPE_GROW bytes |
| 737 | if replaced string is larger than original */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 738 | thepipe.len = strlen(hackline) + PIPE_GROW; |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 739 | thepipe.buf = xcalloc(1, thepipe.len); |
Eric Andersen | b76cb68 | 2001-08-22 05:58:16 +0000 | [diff] [blame] | 740 | /* buffer magic */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 741 | thepipe.buf[thepipe.len - 1] = PIPE_MAGIC; |
Eric Andersen | b76cb68 | 2001-08-22 05:58:16 +0000 | [diff] [blame] | 742 | |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 743 | /* and now, as long as we've got a line to try matching and if we can match |
| 744 | * the search string, we make substitutions */ |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 745 | while ((*hackline || !altered) |
| 746 | && (regexec(current_regex, hackline, sed_cmd->num_backrefs + 1, |
| 747 | regmatch, 0) != REG_NOMATCH)) { |
Mark Whitley | 4f7fe77 | 2000-07-13 20:01:58 +0000 | [diff] [blame] | 748 | int i; |
Mark Whitley | 4f7fe77 | 2000-07-13 20:01:58 +0000 | [diff] [blame] | 749 | |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 750 | /* print everything before the match */ |
| 751 | for (i = 0; i < regmatch[0].rm_so; i++) |
Eric Andersen | b76cb68 | 2001-08-22 05:58:16 +0000 | [diff] [blame] | 752 | pipeputc(hackline[i]); |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 753 | |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 754 | /* then print the substitution string */ |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 755 | print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch, pipeline, |
| 756 | sed_cmd->num_backrefs); |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 757 | |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 758 | /* advance past the match */ |
| 759 | hackline += regmatch[0].rm_eo; |
| 760 | /* flag that something has changed */ |
| 761 | altered++; |
Mark Whitley | 97562bd | 2000-07-17 20:06:42 +0000 | [diff] [blame] | 762 | |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 763 | /* if we're not doing this globally, get out now */ |
Glenn L McGrath | 7ce9e24 | 2003-04-07 16:04:14 +0000 | [diff] [blame] | 764 | if (!sed_cmd->sub_g) { |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 765 | break; |
Glenn L McGrath | 7ce9e24 | 2003-04-07 16:04:14 +0000 | [diff] [blame] | 766 | } |
Mark Whitley | 4f7fe77 | 2000-07-13 20:01:58 +0000 | [diff] [blame] | 767 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 768 | for (; *hackline; hackline++) |
| 769 | pipeputc(*hackline); |
| 770 | if (thepipe.buf[thepipe.idx] == PIPE_MAGIC) |
| 771 | thepipe.buf[thepipe.idx] = 0; |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 772 | |
| 773 | /* cleanup */ |
| 774 | free(regmatch); |
| 775 | |
Eric Andersen | b76cb68 | 2001-08-22 05:58:16 +0000 | [diff] [blame] | 776 | free(*line); |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 777 | *line = thepipe.buf; |
Mark Whitley | 4f7fe77 | 2000-07-13 20:01:58 +0000 | [diff] [blame] | 778 | return altered; |
| 779 | } |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 780 | |
Glenn L McGrath | 961c6c1 | 2003-03-28 04:43:39 +0000 | [diff] [blame] | 781 | static sed_cmd_t *branch_to(const char *label) |
| 782 | { |
| 783 | sed_cmd_t *sed_cmd; |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 784 | |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 785 | for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) { |
Glenn L McGrath | f452356 | 2003-09-14 06:01:14 +0000 | [diff] [blame] | 786 | if ((sed_cmd->cmd == ':') && (sed_cmd->label) && (strcmp(sed_cmd->label, label) == 0)) { |
| 787 | return (sed_cmd); |
Glenn L McGrath | 961c6c1 | 2003-03-28 04:43:39 +0000 | [diff] [blame] | 788 | } |
| 789 | } |
Glenn L McGrath | f452356 | 2003-09-14 06:01:14 +0000 | [diff] [blame] | 790 | 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] | 791 | } |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 792 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 793 | static void process_file(FILE * file) |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 794 | { |
Glenn L McGrath | c6adada | 2003-04-07 12:24:44 +0000 | [diff] [blame] | 795 | char *pattern_space; /* Posix requires it be able to hold at least 8192 bytes */ |
Glenn L McGrath | 7ce9e24 | 2003-04-07 16:04:14 +0000 | [diff] [blame] | 796 | char *hold_space = NULL; /* Posix requires it be able to hold at least 8192 bytes */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 797 | static int linenum = 0; /* GNU sed does not restart counting lines at EOF */ |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 798 | int altered; |
Glenn L McGrath | a0f0f0c | 2003-03-28 14:11:34 +0000 | [diff] [blame] | 799 | int force_print; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 800 | |
Glenn L McGrath | c6adada | 2003-04-07 12:24:44 +0000 | [diff] [blame] | 801 | pattern_space = bb_get_chomped_line_from_file(file); |
| 802 | if (pattern_space == NULL) { |
Glenn L McGrath | 505bd0f | 2003-03-08 05:21:02 +0000 | [diff] [blame] | 803 | return; |
| 804 | } |
Glenn L McGrath | fc4cb4d | 2003-04-12 16:10:42 +0000 | [diff] [blame] | 805 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 806 | /* go through every line in the file */ |
Glenn L McGrath | 505bd0f | 2003-03-08 05:21:02 +0000 | [diff] [blame] | 807 | do { |
| 808 | char *next_line; |
Glenn L McGrath | c949bfa | 2003-03-28 03:53:31 +0000 | [diff] [blame] | 809 | sed_cmd_t *sed_cmd; |
Glenn L McGrath | 595a6a4 | 2003-03-28 08:36:52 +0000 | [diff] [blame] | 810 | int substituted = 0; |
Glenn L McGrath | fc4cb4d | 2003-04-12 16:10:42 +0000 | [diff] [blame] | 811 | /* This enables whole blocks of commands to be mask'ed out if the lead address doesnt match */ |
| 812 | int block_mask = 1; |
Glenn L McGrath | 505bd0f | 2003-03-08 05:21:02 +0000 | [diff] [blame] | 813 | |
| 814 | /* Read one line in advance so we can act on the last line, the '$' address */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 815 | next_line = bb_get_chomped_line_from_file(file); |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 816 | linenum++; |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 817 | altered = 0; |
Glenn L McGrath | a0f0f0c | 2003-03-28 14:11:34 +0000 | [diff] [blame] | 818 | force_print = 0; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 819 | |
| 820 | /* for every line, go through all the commands */ |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 821 | for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) { |
Robert Griebl | 47abc49 | 2002-06-11 23:43:27 +0000 | [diff] [blame] | 822 | int deleted = 0; |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 823 | |
| 824 | /* |
| 825 | * entry point into sedding... |
| 826 | */ |
Robert Griebl | 47abc49 | 2002-06-11 23:43:27 +0000 | [diff] [blame] | 827 | int matched = ( |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 828 | /* no range necessary */ |
| 829 | (sed_cmd->beg_line == 0 && sed_cmd->end_line == 0 |
| 830 | && sed_cmd->beg_match == NULL |
| 831 | && sed_cmd->end_match == NULL) || |
| 832 | /* this line number is the first address we're looking for */ |
Glenn L McGrath | fc4cb4d | 2003-04-12 16:10:42 +0000 | [diff] [blame] | 833 | (sed_cmd->beg_line > 0 && (sed_cmd->beg_line == linenum)) || |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 834 | /* this line matches our first address regex */ |
| 835 | (sed_cmd->beg_match |
| 836 | && (regexec(sed_cmd->beg_match, pattern_space, 0, NULL, |
| 837 | 0) == 0)) || |
| 838 | /* we are currently within the beginning & ending address range */ |
Glenn L McGrath | c18ce37 | 2003-09-13 06:57:39 +0000 | [diff] [blame] | 839 | sed_cmd->still_in_range || ((sed_cmd->beg_line == -1) |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 840 | && (next_line == NULL)) |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 841 | ); |
Glenn L McGrath | fc4cb4d | 2003-04-12 16:10:42 +0000 | [diff] [blame] | 842 | if (sed_cmd->cmd == '{') { |
| 843 | block_mask = block_mask & matched; |
| 844 | } |
| 845 | // matched &= block_mask; |
| 846 | |
| 847 | if (sed_cmd->invert ^ (matched & block_mask)) { |
Glenn L McGrath | c1d9507 | 2003-04-08 06:42:45 +0000 | [diff] [blame] | 848 | /* Update last used regex incase a blank substitute BRE is found */ |
| 849 | if (sed_cmd->beg_match) { |
| 850 | previous_regex_ptr = sed_cmd->beg_match; |
| 851 | } |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 852 | |
| 853 | /* |
| 854 | * actual sedding |
| 855 | */ |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 856 | switch (sed_cmd->cmd) { |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 857 | case '=': |
| 858 | printf("%d\n", linenum); |
| 859 | break; |
| 860 | case 'P':{ |
| 861 | /* Write the current pattern space upto the first newline */ |
| 862 | char *tmp = strchr(pattern_space, '\n'); |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 863 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 864 | if (tmp) { |
| 865 | *tmp = '\0'; |
Glenn L McGrath | 2eed0e2 | 2003-09-15 06:28:45 +0000 | [diff] [blame] | 866 | puts(pattern_space); |
| 867 | *tmp = '\n'; |
| 868 | break; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 869 | } |
Glenn L McGrath | 2eed0e2 | 2003-09-15 06:28:45 +0000 | [diff] [blame] | 870 | /* Fall Through */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 871 | } |
| 872 | case 'p': /* Write the current pattern space to output */ |
| 873 | puts(pattern_space); |
| 874 | break; |
| 875 | case 'd': |
| 876 | altered++; |
| 877 | deleted = 1; |
Glenn L McGrath | 294d113 | 2003-09-14 16:28:08 +0000 | [diff] [blame] | 878 | force_print = 0; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 879 | break; |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 880 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 881 | case 's': |
| 882 | |
| 883 | /* |
| 884 | * Some special cases for 's' printing to make it compliant with |
| 885 | * GNU sed printing behavior (aka "The -n | s///p Matrix"): |
| 886 | * |
| 887 | * -n ONLY = never print anything regardless of any successful |
| 888 | * substitution |
| 889 | * |
| 890 | * s///p ONLY = always print successful substitutions, even if |
| 891 | * the pattern_space is going to be printed anyway (pattern_space |
| 892 | * will be printed twice). |
| 893 | * |
| 894 | * -n AND s///p = print ONLY a successful substitution ONE TIME; |
| 895 | * no other lines are printed - this is the reason why the 'p' |
| 896 | * flag exists in the first place. |
| 897 | */ |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 898 | |
Glenn L McGrath | 0c51832 | 2003-03-30 03:41:53 +0000 | [diff] [blame] | 899 | #ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 900 | /* HACK: escape newlines twice so regex can match them */ |
| 901 | { |
| 902 | int offset = 0; |
Glenn L McGrath | e3e28d3 | 2003-09-15 09:22:04 +0000 | [diff] [blame] | 903 | char *tmp = strchr(pattern_space + offset, '\n'); |
| 904 | while ((tmp = strchr(pattern_space + offset, '\n')) != NULL) { |
| 905 | offset = tmp - pattern_space; |
| 906 | pattern_space = xrealloc(pattern_space, strlen(pattern_space) + 2); |
| 907 | tmp = pattern_space + offset; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 908 | memmove(tmp + 1, tmp, strlen(tmp) + 1); |
| 909 | tmp[0] = '\\'; |
| 910 | tmp[1] = 'n'; |
Glenn L McGrath | e3e28d3 | 2003-09-15 09:22:04 +0000 | [diff] [blame] | 911 | offset += 2; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 912 | } |
| 913 | } |
Glenn L McGrath | 0c51832 | 2003-03-30 03:41:53 +0000 | [diff] [blame] | 914 | #endif |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 915 | /* we print the pattern_space once, unless we were told to be quiet */ |
Glenn L McGrath | 3fe4756 | 2003-09-14 07:59:28 +0000 | [diff] [blame] | 916 | substituted |= do_subst_command(sed_cmd, &pattern_space); |
Glenn L McGrath | 0c51832 | 2003-03-30 03:41:53 +0000 | [diff] [blame] | 917 | |
| 918 | #ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 919 | /* undo HACK: escape newlines twice so regex can match them */ |
| 920 | { |
| 921 | char *tmp = pattern_space; |
Glenn L McGrath | 0c51832 | 2003-03-30 03:41:53 +0000 | [diff] [blame] | 922 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 923 | while ((tmp = strstr(tmp, "\\n")) != NULL) { |
| 924 | memmove(tmp, tmp + 1, strlen(tmp + 1) + 1); |
| 925 | tmp[0] = '\n'; |
Glenn L McGrath | 0c51832 | 2003-03-30 03:41:53 +0000 | [diff] [blame] | 926 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 927 | } |
Glenn L McGrath | 0c51832 | 2003-03-30 03:41:53 +0000 | [diff] [blame] | 928 | #endif |
Glenn L McGrath | edc388c | 2003-09-14 08:52:53 +0000 | [diff] [blame] | 929 | if (!be_quiet && substituted && ((sed_cmd->next == NULL) |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 930 | || (sed_cmd->next->cmd != 's'))) { |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 931 | force_print = 1; |
| 932 | } |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 933 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 934 | /* we also print the line if we were given the 'p' flag |
| 935 | * (this is quite possibly the second printing) */ |
| 936 | if ((sed_cmd->sub_p) && altered) { |
Glenn L McGrath | c6adada | 2003-04-07 12:24:44 +0000 | [diff] [blame] | 937 | puts(pattern_space); |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 938 | } |
| 939 | break; |
| 940 | case 'a': |
| 941 | puts(pattern_space); |
| 942 | fputs(sed_cmd->editline, stdout); |
| 943 | altered++; |
| 944 | break; |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 945 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 946 | case 'i': |
| 947 | fputs(sed_cmd->editline, stdout); |
| 948 | break; |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 949 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 950 | case 'c': |
| 951 | /* single-address case */ |
| 952 | if ((sed_cmd->end_match == NULL && sed_cmd->end_line == 0) |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 953 | /* multi-address case */ |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 954 | /* - matching text */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 955 | || (sed_cmd->end_match |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 956 | && (regexec(sed_cmd->end_match, pattern_space, 0, |
| 957 | NULL, 0) == 0)) |
Eric Andersen | c52a6b0 | 2001-11-10 10:49:42 +0000 | [diff] [blame] | 958 | /* - matching line numbers */ |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 959 | || (sed_cmd->end_line > 0 |
| 960 | && sed_cmd->end_line == linenum)) { |
| 961 | fputs(sed_cmd->editline, stdout); |
| 962 | } |
| 963 | altered++; |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 964 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 965 | break; |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 966 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 967 | case 'r':{ |
| 968 | FILE *outfile; |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 969 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 970 | outfile = fopen(sed_cmd->filename, "r"); |
Glenn L McGrath | d87a7ac | 2003-04-09 15:26:14 +0000 | [diff] [blame] | 971 | if (outfile) { |
Glenn L McGrath | d7fe39b | 2003-04-09 15:52:32 +0000 | [diff] [blame] | 972 | char *line; |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 973 | |
| 974 | while ((line = |
| 975 | bb_get_chomped_line_from_file(outfile)) != |
| 976 | NULL) { |
| 977 | pattern_space = |
| 978 | xrealloc(pattern_space, |
| 979 | strlen(line) + strlen(pattern_space) + 2); |
Glenn L McGrath | d7fe39b | 2003-04-09 15:52:32 +0000 | [diff] [blame] | 980 | strcat(pattern_space, "\n"); |
| 981 | strcat(pattern_space, line); |
| 982 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 983 | bb_xprint_and_close_file(outfile); |
Glenn L McGrath | d87a7ac | 2003-04-09 15:26:14 +0000 | [diff] [blame] | 984 | } |
Glenn L McGrath | d7fe39b | 2003-04-09 15:52:32 +0000 | [diff] [blame] | 985 | |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 986 | } |
| 987 | break; |
| 988 | case 'q': /* Branch to end of script and quit */ |
| 989 | deleted = 1; |
| 990 | /* Exit the outer while loop */ |
| 991 | free(next_line); |
| 992 | next_line = NULL; |
| 993 | break; |
| 994 | case 'n': /* Read next line from input */ |
Glenn L McGrath | 6e5687a | 2003-09-15 06:12:53 +0000 | [diff] [blame] | 995 | if (!be_quiet) { |
| 996 | puts(pattern_space); |
| 997 | } |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 998 | if (next_line) { |
| 999 | free(pattern_space); |
| 1000 | pattern_space = next_line; |
| 1001 | next_line = bb_get_chomped_line_from_file(file); |
| 1002 | linenum++; |
Glenn L McGrath | 6e5687a | 2003-09-15 06:12:53 +0000 | [diff] [blame] | 1003 | } else { |
| 1004 | /* Jump to end of script and exist */ |
| 1005 | deleted = 1; |
| 1006 | free(next_line); |
| 1007 | next_line = NULL; |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 1008 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1009 | break; |
| 1010 | case 'N': /* Append the next line to the current line */ |
| 1011 | if (next_line) { |
| 1012 | pattern_space = |
| 1013 | realloc(pattern_space, |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 1014 | strlen(pattern_space) + strlen(next_line) + 2); |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1015 | strcat(pattern_space, "\n"); |
| 1016 | strcat(pattern_space, next_line); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1017 | next_line = bb_get_chomped_line_from_file(file); |
Glenn L McGrath | 4157a8a | 2003-03-10 04:12:35 +0000 | [diff] [blame] | 1018 | linenum++; |
Glenn L McGrath | 7311631 | 2003-09-15 05:42:05 +0000 | [diff] [blame] | 1019 | } else { |
| 1020 | /* Jump to end of script and exist */ |
| 1021 | deleted = 1; |
| 1022 | free(next_line); |
| 1023 | next_line = NULL; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1024 | } |
| 1025 | break; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1026 | case 't': |
Glenn L McGrath | f452356 | 2003-09-14 06:01:14 +0000 | [diff] [blame] | 1027 | if (substituted) |
| 1028 | /* Fall through */ |
| 1029 | case 'b': |
| 1030 | { |
| 1031 | if (sed_cmd->label == NULL) { |
| 1032 | /* Jump to end of script */ |
| 1033 | deleted = 1; |
| 1034 | } else { |
| 1035 | sed_cmd = branch_to(sed_cmd->label); |
| 1036 | } |
Glenn L McGrath | 640c1f5 | 2003-09-15 04:55:29 +0000 | [diff] [blame] | 1037 | /* Reset the substitution flag */ |
| 1038 | substituted = 0; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1039 | } |
| 1040 | break; |
| 1041 | case 'y':{ |
| 1042 | int i; |
| 1043 | |
| 1044 | for (i = 0; pattern_space[i] != 0; i++) { |
| 1045 | int j; |
| 1046 | |
| 1047 | for (j = 0; sed_cmd->translate[j]; j += 2) { |
| 1048 | if (pattern_space[i] == sed_cmd->translate[j]) { |
| 1049 | pattern_space[i] = sed_cmd->translate[j + 1]; |
Glenn L McGrath | f01b46d | 2003-03-30 08:02:18 +0000 | [diff] [blame] | 1050 | } |
| 1051 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1052 | } |
| 1053 | } |
| 1054 | break; |
| 1055 | case 'g': /* Replace pattern space with hold space */ |
| 1056 | free(pattern_space); |
Glenn L McGrath | 294d113 | 2003-09-14 16:28:08 +0000 | [diff] [blame] | 1057 | if (hold_space) { |
| 1058 | pattern_space = strdup(hold_space); |
| 1059 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1060 | break; |
Glenn L McGrath | 91e1978 | 2003-04-26 07:40:07 +0000 | [diff] [blame] | 1061 | case 'G': { /* Append newline and hold space to pattern space */ |
Glenn L McGrath | 294d113 | 2003-09-14 16:28:08 +0000 | [diff] [blame] | 1062 | int pattern_space_size = 2; |
| 1063 | int hold_space_size = 0; |
| 1064 | |
Glenn L McGrath | 91e1978 | 2003-04-26 07:40:07 +0000 | [diff] [blame] | 1065 | if (pattern_space) { |
Glenn L McGrath | 294d113 | 2003-09-14 16:28:08 +0000 | [diff] [blame] | 1066 | pattern_space_size += strlen(pattern_space); |
Glenn L McGrath | 91e1978 | 2003-04-26 07:40:07 +0000 | [diff] [blame] | 1067 | } |
Glenn L McGrath | 294d113 | 2003-09-14 16:28:08 +0000 | [diff] [blame] | 1068 | if (hold_space) { |
| 1069 | hold_space_size = strlen(hold_space); |
| 1070 | } |
| 1071 | pattern_space = xrealloc(pattern_space, pattern_space_size + hold_space_size); |
| 1072 | if (pattern_space_size == 2) { |
| 1073 | strcat(pattern_space, "\n"); |
| 1074 | } else { |
| 1075 | strcpy(pattern_space, "\n"); |
| 1076 | } |
| 1077 | if (hold_space) { |
| 1078 | strcat(pattern_space, hold_space); |
| 1079 | } |
Glenn L McGrath | 91e1978 | 2003-04-26 07:40:07 +0000 | [diff] [blame] | 1080 | break; |
| 1081 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1082 | case 'h': /* Replace hold space with pattern space */ |
| 1083 | free(hold_space); |
| 1084 | hold_space = strdup(pattern_space); |
| 1085 | break; |
Glenn L McGrath | 91e1978 | 2003-04-26 07:40:07 +0000 | [diff] [blame] | 1086 | case 'H': { /* Append newline and pattern space to hold space */ |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 1087 | int hold_space_size = 2; |
| 1088 | int pattern_space_size = 0; |
| 1089 | |
Glenn L McGrath | 91e1978 | 2003-04-26 07:40:07 +0000 | [diff] [blame] | 1090 | if (hold_space) { |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 1091 | hold_space_size += strlen(hold_space); |
Glenn L McGrath | 91e1978 | 2003-04-26 07:40:07 +0000 | [diff] [blame] | 1092 | } |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 1093 | if (pattern_space) { |
| 1094 | pattern_space_size = strlen(pattern_space); |
| 1095 | } |
| 1096 | hold_space = xrealloc(hold_space, hold_space_size + pattern_space_size); |
| 1097 | |
| 1098 | if (hold_space_size == 2) { |
| 1099 | strcpy(hold_space, "\n"); |
Glenn L McGrath | 4dc1d25 | 2003-09-14 01:25:31 +0000 | [diff] [blame] | 1100 | } else { |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 1101 | strcat(hold_space, "\n"); |
Glenn L McGrath | 4dc1d25 | 2003-09-14 01:25:31 +0000 | [diff] [blame] | 1102 | } |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 1103 | if (pattern_space) { |
| 1104 | strcat(hold_space, pattern_space); |
| 1105 | } |
Glenn L McGrath | 91e1978 | 2003-04-26 07:40:07 +0000 | [diff] [blame] | 1106 | break; |
| 1107 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1108 | case 'x':{ |
| 1109 | /* Swap hold and pattern space */ |
Glenn L McGrath | fc4cb4d | 2003-04-12 16:10:42 +0000 | [diff] [blame] | 1110 | char *tmp = pattern_space; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1111 | pattern_space = hold_space; |
| 1112 | hold_space = tmp; |
Glenn L McGrath | fc4cb4d | 2003-04-12 16:10:42 +0000 | [diff] [blame] | 1113 | break; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1114 | } |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 1115 | } |
Robert Griebl | 47abc49 | 2002-06-11 23:43:27 +0000 | [diff] [blame] | 1116 | } |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 1117 | |
Robert Griebl | 47abc49 | 2002-06-11 23:43:27 +0000 | [diff] [blame] | 1118 | /* |
| 1119 | * exit point from sedding... |
| 1120 | */ |
| 1121 | if (matched) { |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 1122 | if ( |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 1123 | /* this is a single-address command or... */ |
| 1124 | (sed_cmd->end_line == 0 && sed_cmd->end_match == NULL) |
| 1125 | /* If only one address */ |
| 1126 | /* we were in the middle of our address range (this |
| 1127 | * isn't the first time through) and.. */ |
Glenn L McGrath | c18ce37 | 2003-09-13 06:57:39 +0000 | [diff] [blame] | 1128 | || ((sed_cmd->still_in_range == 1) |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 1129 | /* this line number is the last address we're looking for or... */ |
Glenn L McGrath | fc4cb4d | 2003-04-12 16:10:42 +0000 | [diff] [blame] | 1130 | && ((sed_cmd->end_line > 0 |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 1131 | && (sed_cmd->end_line == linenum)) |
| 1132 | /* this line matches our last address regex */ |
| 1133 | || (sed_cmd->end_match |
| 1134 | && (regexec(sed_cmd->end_match, pattern_space, |
| 1135 | 0, NULL, 0) == 0))))) { |
Mark Whitley | 0915c4b | 2001-06-11 23:50:06 +0000 | [diff] [blame] | 1136 | /* we're out of our address range */ |
Glenn L McGrath | c18ce37 | 2003-09-13 06:57:39 +0000 | [diff] [blame] | 1137 | sed_cmd->still_in_range = 0; |
Glenn L McGrath | d4185b0 | 2003-04-11 17:10:23 +0000 | [diff] [blame] | 1138 | } else { |
| 1139 | /* didn't hit the exit? then we're still in the middle of an address range */ |
Glenn L McGrath | c18ce37 | 2003-09-13 06:57:39 +0000 | [diff] [blame] | 1140 | sed_cmd->still_in_range = 1; |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1141 | } |
| 1142 | } |
Robert Griebl | 47abc49 | 2002-06-11 23:43:27 +0000 | [diff] [blame] | 1143 | |
Glenn L McGrath | fc4cb4d | 2003-04-12 16:10:42 +0000 | [diff] [blame] | 1144 | if (sed_cmd->cmd == '}') { |
| 1145 | block_mask = 1; |
| 1146 | } |
| 1147 | |
Robert Griebl | 47abc49 | 2002-06-11 23:43:27 +0000 | [diff] [blame] | 1148 | if (deleted) |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1149 | break; |
Glenn L McGrath | 4dc1d25 | 2003-09-14 01:25:31 +0000 | [diff] [blame] | 1150 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1151 | } |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 1152 | |
Mark Whitley | 2dc192f | 2000-11-03 19:47:00 +0000 | [diff] [blame] | 1153 | /* we will print the line unless we were told to be quiet or if the |
| 1154 | * line was altered (via a 'd'elete or 's'ubstitution), in which case |
| 1155 | * the altered line was already printed */ |
Glenn L McGrath | edc388c | 2003-09-14 08:52:53 +0000 | [diff] [blame] | 1156 | if ((!be_quiet && !altered && !substituted) || force_print) { |
Glenn L McGrath | c6adada | 2003-04-07 12:24:44 +0000 | [diff] [blame] | 1157 | puts(pattern_space); |
Glenn L McGrath | a0f0f0c | 2003-03-28 14:11:34 +0000 | [diff] [blame] | 1158 | } |
Glenn L McGrath | c6adada | 2003-04-07 12:24:44 +0000 | [diff] [blame] | 1159 | free(pattern_space); |
| 1160 | pattern_space = next_line; |
| 1161 | } while (pattern_space); |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | extern int sed_main(int argc, char **argv) |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 1165 | { |
Matt Kraai | a5f09c6 | 2001-11-12 16:44:55 +0000 | [diff] [blame] | 1166 | int opt, status = EXIT_SUCCESS; |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 1167 | |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 1168 | #if 0 /* This doesnt seem to be working */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1169 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Mark Whitley | 858c1ad | 2000-07-11 21:38:47 +0000 | [diff] [blame] | 1170 | /* destroy command strings on exit */ |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 1171 | if (atexit(destroy_cmd_strs) == -1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1172 | bb_perror_msg_and_die("atexit"); |
Mark Whitley | c41e8c8 | 2000-07-12 23:35:21 +0000 | [diff] [blame] | 1173 | #endif |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 1174 | #endif |
Mark Whitley | 858c1ad | 2000-07-11 21:38:47 +0000 | [diff] [blame] | 1175 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1176 | /* do normal option parsing */ |
Eric Andersen | b50da53 | 2001-02-17 16:52:35 +0000 | [diff] [blame] | 1177 | while ((opt = getopt(argc, argv, "ne:f:")) > 0) { |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1178 | switch (opt) { |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1179 | case 'n': |
| 1180 | be_quiet++; |
| 1181 | break; |
| 1182 | case 'e':{ |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 1183 | add_cmd_str(optarg); |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1184 | break; |
| 1185 | } |
| 1186 | case 'f': |
| 1187 | load_cmd_file(optarg); |
| 1188 | break; |
| 1189 | default: |
| 1190 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1191 | } |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 1192 | } |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1193 | |
| 1194 | /* if we didn't get a pattern from a -e and no command file was specified, |
| 1195 | * argv[optind] should be the pattern. no pattern, no worky */ |
Glenn L McGrath | bd9b32b | 2003-04-09 01:43:54 +0000 | [diff] [blame] | 1196 | if (sed_cmd_head.next == NULL) { |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1197 | if (argv[optind] == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1198 | bb_show_usage(); |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 1199 | else |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 1200 | add_cmd_str(argv[optind++]); |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1203 | /* argv[(optind)..(argc-1)] should be names of file to process. If no |
| 1204 | * files were specified or '-' was specified, take input from stdin. |
| 1205 | * Otherwise, we process all the files specified. */ |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 1206 | if (argv[optind] == NULL) { |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1207 | process_file(stdin); |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1208 | } else { |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1209 | int i; |
| 1210 | FILE *file; |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1211 | |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1212 | for (i = optind; i < argc; i++) { |
Glenn L McGrath | 8aac05b | 2003-09-14 04:06:12 +0000 | [diff] [blame] | 1213 | if(!strcmp(argv[i], "-")) { |
| 1214 | process_file(stdin); |
| 1215 | } else { |
| 1216 | file = bb_wfopen(argv[i], "r"); |
| 1217 | if (file) { |
| 1218 | process_file(file); |
| 1219 | fclose(file); |
| 1220 | } else { |
| 1221 | status = EXIT_FAILURE; |
| 1222 | } |
| 1223 | } |
Mark Whitley | 6315ce6 | 2000-07-10 22:55:51 +0000 | [diff] [blame] | 1224 | } |
| 1225 | } |
Glenn L McGrath | 8d6395d | 2003-04-08 11:56:11 +0000 | [diff] [blame] | 1226 | |
Glenn L McGrath | 8417c8c | 2003-09-14 15:24:18 +0000 | [diff] [blame] | 1227 | #ifdef CONFIG_FEATURE_CLEAN_UP |
| 1228 | destroy_cmd_strs(); |
| 1229 | #endif |
Matt Kraai | a5f09c6 | 2001-11-12 16:44:55 +0000 | [diff] [blame] | 1230 | return status; |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 1231 | } |