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