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