blob: f8e3720b598047e9599864b3eb6c759d435464ea [file] [log] [blame]
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001/* vi: set sw=4 ts=4: */
Eric Andersen6b6b3f61999-10-28 16:06:25 +00002/*
Mark Whitley6315ce62000-07-10 22:55:51 +00003 * sed.c - very minimalist version of sed
Eric Andersen6b6b3f61999-10-28 16:06:25 +00004 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
6 * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org>
Matt Kraai5ed78ad2002-01-03 21:12:34 +00007 * Copyright (C) 2002 Matt Kraai
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00008 * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au>
Rob Landley25d82392004-04-01 09:23:30 +00009 * Copyright (C) 2003,2004 by Rob Landley <rob@landley.net>
Erik Andersen1266a131999-12-29 22:19:46 +000010 *
Rob Landley2b26fd52006-02-24 02:30:39 +000011 * MAINTAINER: Rob Landley <rob@landley.net>
Bernhard Reutner-Fischerb7f39732006-03-01 20:14:16 +000012 *
Rob Landleyfae1dc82005-11-20 07:44:35 +000013 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen6b6b3f61999-10-28 16:06:25 +000014 */
15
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000016/* 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 Landleye3f5a3f2006-05-09 03:53:55 +000024 (bbg.sed_cmd_head/bbg.sed_cmd_tail).
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000025
Rob Landleydcc28662004-11-25 07:21:47 +000026 add_input_file() adds a FILE * to the list of input files. We need to
Rob Landleyfae1dc82005-11-20 07:44:35 +000027 know all input sources ahead of time to find the last line for the $ match.
Rob Landleydcc28662004-11-25 07:21:47 +000028
29 process_files() does actual sedding, reading data lines from each input FILE *
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000030 (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 Whitley6315ce62000-07-10 22:55:51 +000037/*
38 Supported features and commands in this version of sed:
39
40 - comments ('#')
Mark Whitley94074a92000-07-14 00:00:15 +000041 - 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 Whitley1f3b9f22001-05-11 22:27:13 +000044 - file commands: (r)ead
Rob Landleyc63fe912005-10-30 10:08:13 +000045 - backreferences in substitution expressions (\0, \1, \2...\9)
Glenn L McGrathf50ce312003-03-09 15:12:24 +000046 - grouped commands: {cmd1;cmd2}
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000047 - transliteration (y/source-chars/dest-chars/)
48 - pattern space hold space storing / swapping (g, h, x)
Rob Landley93850a52005-05-18 06:34:37 +000049 - labels / branching (: label, b, t, T)
Glenn L McGrathf50ce312003-03-09 15:12:24 +000050
Mark Whitley6315ce62000-07-10 22:55:51 +000051 (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 McGrath2570b432003-09-16 05:25:43 +000055 Todo:
Glenn L McGrath2570b432003-09-16 05:25:43 +000056 - Create a wrapper around regex to make libc's regex conform with sed
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000057
Glenn L McGrathd5eadea2003-03-09 02:39:29 +000058 Reference http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html
Mark Whitley6315ce62000-07-10 22:55:51 +000059*/
60
Eric Andersen3570a342000-09-25 21:45:58 +000061#include "busybox.h"
"Vladimir N. Oleynik"23f62fc2005-09-14 16:59:11 +000062#include "xregex.h"
Mark Whitley6315ce62000-07-10 22:55:51 +000063
Rob Landleye3f5a3f2006-05-09 03:53:55 +000064/* Each sed command turns into one of these structures. */
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +000065typedef struct sed_cmd_s {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000066 /* Ordered by alignment requirements: currently 36 bytes on x86 */
Mark Whitley2dc192f2000-11-03 19:47:00 +000067
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000068 /* address storage */
69 regex_t *beg_match; /* sed -e '/match/cmd' */
70 regex_t *end_match; /* sed -e '/match/,/end_match/cmd' */
71 regex_t *sub_match; /* For 's/sub_match/string/' */
72 int beg_line; /* 'sed 1p' 0 == apply commands to all lines */
73 int end_line; /* 'sed 1,3p' 0 == one line only. -1 = last line ($) */
Bernhard Reutner-Fischerb7f39732006-03-01 20:14:16 +000074
Rob Landley2b26fd52006-02-24 02:30:39 +000075 FILE *file; /* File (sw) command writes to, -1 for none. */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000076 char *string; /* Data string for (saicytb) commands. */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +000077
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000078 unsigned short which_match; /* (s) Which match to replace (0 for all) */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +000079
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000080 /* Bitfields (gcc won't group them if we don't) */
81 unsigned int invert:1; /* the '!' after the address */
82 unsigned int in_match:1; /* Next line also included in match? */
Rob Landley2b26fd52006-02-24 02:30:39 +000083 unsigned int no_newline:1; /* Last line written by (sw) had no '\n' */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000084 unsigned int sub_p:1; /* (s) print option */
Glenn L McGrathc18ce372003-09-13 06:57:39 +000085
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000086 /* GENERAL FIELDS */
87 char cmd; /* The command char: abcdDgGhHilnNpPqrstwxy:={} */
88 struct sed_cmd_s *next; /* Next command (linked list, NULL terminated) */
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +000089} sed_cmd_t;
Mark Whitley6315ce62000-07-10 22:55:51 +000090
Eric Andersen14f5c8d2005-04-16 19:39:00 +000091static const char *const semicolon_whitespace = "; \n\r\t\v";
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000092
Rob Landleye3f5a3f2006-05-09 03:53:55 +000093struct sed_globals
94{
95 /* options */
96 int be_quiet, in_place, regex_type;
97 FILE *nonstdout;
98 char *outname, *hold_space;
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000099
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000100 /* List of input files */
101 int input_file_count,current_input_file;
102 FILE **input_file_list;
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000103
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000104 regmatch_t regmatch[10];
105 regex_t *previous_regex_ptr;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000106
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000107 /* linked list of sed commands */
108 sed_cmd_t sed_cmd_head, *sed_cmd_tail;
109
110 /* Linked list of append lines */
111 llist_t *append_head;
112
113 char *add_cmd_line;
114
115 struct pipeline {
116 char *buf; /* Space to hold string */
117 int idx; /* Space used */
118 int len; /* Space allocated */
119 } pipeline;
120} bbg;
121
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000122
Bernhard Reutner-Fischerc0bb3082006-03-02 10:06:22 +0000123void sed_free_and_close_stuff(void);
Bernhard Reutner-Fischerb7f39732006-03-01 20:14:16 +0000124#if ENABLE_FEATURE_CLEAN_UP
Bernhard Reutner-Fischerc0bb3082006-03-02 10:06:22 +0000125void sed_free_and_close_stuff(void)
Mark Whitley6315ce62000-07-10 22:55:51 +0000126{
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000127 sed_cmd_t *sed_cmd = bbg.sed_cmd_head.next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000128
Rob Landley5fc467e2006-05-24 17:45:47 +0000129 llist_free(bbg.append_head, free);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000130
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000131 while (sed_cmd) {
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000132 sed_cmd_t *sed_cmd_next = sed_cmd->next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000133
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000134 if(sed_cmd->file)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000135 xprint_and_close_file(sed_cmd->file);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000136
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000137 if (sed_cmd->beg_match) {
138 regfree(sed_cmd->beg_match);
139 free(sed_cmd->beg_match);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000141 if (sed_cmd->end_match) {
142 regfree(sed_cmd->end_match);
143 free(sed_cmd->end_match);
Mark Whitley6315ce62000-07-10 22:55:51 +0000144 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000145 if (sed_cmd->sub_match) {
146 regfree(sed_cmd->sub_match);
147 free(sed_cmd->sub_match);
Mark Whitley6315ce62000-07-10 22:55:51 +0000148 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000149 free(sed_cmd->string);
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000150 free(sed_cmd);
151 sed_cmd = sed_cmd_next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000152 }
Rob Landleyce4f0e92004-10-30 06:54:19 +0000153
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000154 if(bbg.hold_space) free(bbg.hold_space);
Rob Landleydcc28662004-11-25 07:21:47 +0000155
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000156 while(bbg.current_input_file<bbg.input_file_count)
157 fclose(bbg.input_file_list[bbg.current_input_file++]);
Mark Whitley6315ce62000-07-10 22:55:51 +0000158}
Mark Whitleyc41e8c82000-07-12 23:35:21 +0000159#endif
Mark Whitley6315ce62000-07-10 22:55:51 +0000160
Rob Landley53302f82004-02-18 09:54:15 +0000161/* If something bad happens during -i operation, delete temp file */
162
163static void cleanup_outname(void)
164{
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000165 if(bbg.outname) unlink(bbg.outname);
Rob Landley53302f82004-02-18 09:54:15 +0000166}
167
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000168/* strdup, replacing "\n" with '\n', and "\delimiter" with 'delimiter' */
169
Rob Landley4795e4e2006-07-26 17:25:08 +0000170static void parse_escapes(char *dest, char *string, int len, char from, char to)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000171{
172 int i=0;
173
174 while(i<len) {
175 if(string[i] == '\\') {
Glenn L McGrath738fb332003-10-01 06:45:11 +0000176 if(!to || string[i+1] == from) {
177 *(dest++) = to ? to : string[i+1];
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000178 i+=2;
179 continue;
180 } else *(dest++)=string[i++];
181 }
182 *(dest++) = string[i++];
183 }
184 *dest=0;
185}
186
Rob Landley4795e4e2006-07-26 17:25:08 +0000187static char *copy_parsing_escapes(char *string, int len)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000188{
189 char *dest=xmalloc(len+1);
190
191 parse_escapes(dest,string,len,'n','\n');
192 return dest;
193}
194
195
Mark Whitley6315ce62000-07-10 22:55:51 +0000196/*
Eric Andersen28b3c532001-01-02 11:01:31 +0000197 * index_of_next_unescaped_regexp_delim - walks left to right through a string
198 * beginning at a specified index and returns the index of the next regular
Eric Andersenaff114c2004-04-14 17:51:38 +0000199 * expression delimiter (typically a forward * slash ('/')) not preceded by
Rob Landley4795e4e2006-07-26 17:25:08 +0000200 * a backslash ('\'). A negative delimiter disables square bracket checking.
Mark Whitley6315ce62000-07-10 22:55:51 +0000201 */
Rob Landley4795e4e2006-07-26 17:25:08 +0000202static int index_of_next_unescaped_regexp_delim(int delimiter, char *str)
Mark Whitley6315ce62000-07-10 22:55:51 +0000203{
Matt Kraaia0065d52001-08-24 14:45:50 +0000204 int bracket = -1;
205 int escaped = 0;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000206 int idx = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000207 char ch;
Matt Kraaia0065d52001-08-24 14:45:50 +0000208
Rob Landley4795e4e2006-07-26 17:25:08 +0000209 if (delimiter < 0) {
210 bracket--;
211 delimiter *= -1;
212 }
213
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000214 for (; (ch = str[idx]); idx++) {
Rob Landley4795e4e2006-07-26 17:25:08 +0000215 if (bracket >= 0) {
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000216 if (ch == ']' && !(bracket == idx - 1 || (bracket == idx - 2
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000217 && str[idx - 1] == '^')))
Matt Kraaia0065d52001-08-24 14:45:50 +0000218 bracket = -1;
219 } else if (escaped)
220 escaped = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000221 else if (ch == '\\')
Matt Kraaia0065d52001-08-24 14:45:50 +0000222 escaped = 1;
Rob Landley4795e4e2006-07-26 17:25:08 +0000223 else if (bracket == -1 && ch == '[')
Matt Kraaia0065d52001-08-24 14:45:50 +0000224 bracket = idx;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000225 else if (ch == delimiter)
Mark Whitley97562bd2000-07-17 20:06:42 +0000226 return idx;
227 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000228
Mark Whitley97562bd2000-07-17 20:06:42 +0000229 /* if we make it to here, we've hit the end of the string */
Rob Landley4795e4e2006-07-26 17:25:08 +0000230 bb_error_msg_and_die("unmatched '%c'",delimiter);
Mark Whitley6315ce62000-07-10 22:55:51 +0000231}
232
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000233/*
234 * Returns the index of the third delimiter
235 */
Rob Landley4795e4e2006-07-26 17:25:08 +0000236static int parse_regex_delim(char *cmdstr, char **match, char **replace)
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000237{
Rob Landley4795e4e2006-07-26 17:25:08 +0000238 char *cmdstr_ptr = cmdstr;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000239 char delimiter;
240 int idx = 0;
241
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000242 /* verify that the 's' or 'y' is followed by something. That something
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000243 * (typically a 'slash') is now our regexp delimiter... */
Rob Landley4795e4e2006-07-26 17:25:08 +0000244 if (*cmdstr == '\0')
245 bb_error_msg_and_die("bad format in substitution expression");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000246 delimiter = *(cmdstr_ptr++);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000247
248 /* save the match string */
249 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
Rob Landleyfae1dc82005-11-20 07:44:35 +0000250 *match = copy_parsing_escapes(cmdstr_ptr, idx);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000251
252 /* save the replacement string */
253 cmdstr_ptr += idx + 1;
Rob Landley4795e4e2006-07-26 17:25:08 +0000254 idx = index_of_next_unescaped_regexp_delim(-delimiter, cmdstr_ptr);
Rob Landleyfae1dc82005-11-20 07:44:35 +0000255 *replace = copy_parsing_escapes(cmdstr_ptr, idx);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000256
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000257 return ((cmdstr_ptr - cmdstr) + idx);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000258}
259
Mark Whitley6315ce62000-07-10 22:55:51 +0000260/*
261 * returns the index in the string just past where the address ends.
262 */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000263static int get_address(char *my_str, int *linenum, regex_t ** regex)
Mark Whitley6315ce62000-07-10 22:55:51 +0000264{
Glenn L McGrathe3e28d32003-09-15 09:22:04 +0000265 char *pos = my_str;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000266
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000267 if (isdigit(*my_str)) {
268 *linenum = strtol(my_str, &pos, 10);
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000269 /* endstr shouldnt ever equal NULL */
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000270 } else if (*my_str == '$') {
Mark Whitley0915c4b2001-06-11 23:50:06 +0000271 *linenum = -1;
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000272 pos++;
273 } else if (*my_str == '/' || *my_str == '\\') {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000274 int next;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000275 char delimiter;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000276 char *temp;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000277
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000278 if (*my_str == '\\') delimiter = *(++pos);
279 else delimiter = '/';
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000280 next = index_of_next_unescaped_regexp_delim(delimiter, ++pos);
Rob Landley4795e4e2006-07-26 17:25:08 +0000281 temp = copy_parsing_escapes(pos,next);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000282 *regex = (regex_t *) xmalloc(sizeof(regex_t));
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000283 xregcomp(*regex, temp, bbg.regex_type|REG_NEWLINE);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000284 free(temp);
285 /* Move position to next character after last delimiter */
Rob Landley4795e4e2006-07-26 17:25:08 +0000286 pos += (next+1);
Mark Whitley6315ce62000-07-10 22:55:51 +0000287 }
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000288 return pos - my_str;
Mark Whitley6315ce62000-07-10 22:55:51 +0000289}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000290
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000291/* Grab a filename. Whitespace at start is skipped, then goes to EOL. */
Rob Landley4795e4e2006-07-26 17:25:08 +0000292static int parse_file_cmd(sed_cmd_t *sed_cmd, char *filecmdstr, char **retval)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000293{
294 int start = 0, idx, hack=0;
295
296 /* Skip whitespace, then grab filename to end of line */
297 while (isspace(filecmdstr[start])) start++;
298 idx=start;
299 while(filecmdstr[idx] && filecmdstr[idx]!='\n') idx++;
300 /* If lines glued together, put backslash back. */
301 if(filecmdstr[idx]=='\n') hack=1;
302 if(idx==start) bb_error_msg_and_die("Empty filename");
Rob Landleyd921b2e2006-08-03 15:41:12 +0000303 *retval = xstrndup(filecmdstr+start, idx-start+hack+1);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000304 if(hack) *(idx+*retval)='\\';
305
306 return idx;
307}
308
Rob Landley4795e4e2006-07-26 17:25:08 +0000309static int parse_subst_cmd(sed_cmd_t *sed_cmd, char *substr)
Mark Whitley06f35292000-07-13 19:58:04 +0000310{
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000311 int cflags = bbg.regex_type;
Mark Whitley06f35292000-07-13 19:58:04 +0000312 char *match;
313 int idx = 0;
314
315 /*
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000316 * A substitution command should look something like this:
317 * s/match/replace/ #gIpw
Mark Whitley0e4cec02000-08-21 21:29:20 +0000318 * || | |||
Mark Whitley06f35292000-07-13 19:58:04 +0000319 * mandatory optional
Mark Whitley06f35292000-07-13 19:58:04 +0000320 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000321 idx = parse_regex_delim(substr, &match, &sed_cmd->string);
Mark Whitley06f35292000-07-13 19:58:04 +0000322
Mark Whitley97562bd2000-07-17 20:06:42 +0000323 /* determine the number of back references in the match string */
324 /* Note: we compute this here rather than in the do_subst_command()
325 * function to save processor time, at the expense of a little more memory
326 * (4 bits) per sed_cmd */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000327
Mark Whitley06f35292000-07-13 19:58:04 +0000328 /* process the flags */
Mark Whitley70705d72000-07-14 19:06:30 +0000329
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000330 sed_cmd->which_match=1;
331 while (substr[++idx]) {
332 /* Parse match number */
333 if(isdigit(substr[idx])) {
334 if(match[0]!='^') {
335 /* Match 0 treated as all, multiple matches we take the last one. */
336 char *pos=substr+idx;
337 sed_cmd->which_match=(unsigned short)strtol(substr+idx,&pos,10);
338 idx=pos-substr;
339 }
340 continue;
341 }
Rob Landley40ec4ae2004-01-04 06:42:14 +0000342 /* Skip spaces */
343 if(isspace(substr[idx])) continue;
344
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000345 switch (substr[idx]) {
346 /* Replace all occurrences */
347 case 'g':
348 if (match[0] != '^') sed_cmd->which_match = 0;
349 break;
350 /* Print pattern space */
351 case 'p':
352 sed_cmd->sub_p = 1;
353 break;
Rob Landleyfae1dc82005-11-20 07:44:35 +0000354 /* Write to file */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000355 case 'w':
356 {
357 char *temp;
358 idx+=parse_file_cmd(sed_cmd,substr+idx,&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000359
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000360 break;
361 }
362 /* Ignore case (gnu exension) */
363 case 'I':
364 cflags |= REG_ICASE;
365 break;
Rob Landleyfae1dc82005-11-20 07:44:35 +0000366 /* Comment */
367 case '#':
368 while(substr[++idx]);
369 /* Fall through */
370 /* End of command */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000371 case ';':
372 case '}':
373 goto out;
374 default:
375 bb_error_msg_and_die("bad option in substitution expression");
376 }
377 }
Glenn L McGrath2570b432003-09-16 05:25:43 +0000378out:
Mark Whitley97562bd2000-07-17 20:06:42 +0000379 /* compile the match string into a regex */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000380 if (*match != '\0') {
381 /* If match is empty, we use last regex used at runtime */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000382 sed_cmd->sub_match = (regex_t *) xmalloc(sizeof(regex_t));
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000383 xregcomp(sed_cmd->sub_match, match, cflags);
384 }
Mark Whitley06f35292000-07-13 19:58:04 +0000385 free(match);
Mark Whitley70705d72000-07-14 19:06:30 +0000386
387 return idx;
Mark Whitley06f35292000-07-13 19:58:04 +0000388}
389
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000390/*
391 * Process the commands arguments
392 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000393static char *parse_cmd_args(sed_cmd_t *sed_cmd, char *cmdstr)
Mark Whitley6315ce62000-07-10 22:55:51 +0000394{
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000395 /* handle (s)ubstitution command */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000396 if (sed_cmd->cmd == 's') cmdstr += parse_subst_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000397 /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */
398 else if (strchr("aic", sed_cmd->cmd)) {
399 if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000400 bb_error_msg_and_die
401 ("only a beginning address can be specified for edit commands");
Rob Landley25d82392004-04-01 09:23:30 +0000402 for(;;) {
403 if(*cmdstr=='\n' || *cmdstr=='\\') {
404 cmdstr++;
405 break;
406 } else if(isspace(*cmdstr)) cmdstr++;
407 else break;
408 }
Rob Landleyd921b2e2006-08-03 15:41:12 +0000409 sed_cmd->string = xstrdup(cmdstr);
Glenn L McGrath738fb332003-10-01 06:45:11 +0000410 parse_escapes(sed_cmd->string,sed_cmd->string,strlen(cmdstr),0,0);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000411 cmdstr += strlen(cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000412 /* handle file cmds: (r)ead */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000413 } else if(strchr("rw", sed_cmd->cmd)) {
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000414 if (sed_cmd->end_line || sed_cmd->end_match)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000415 bb_error_msg_and_die("Command only uses one address");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000416 cmdstr += parse_file_cmd(sed_cmd, cmdstr, &sed_cmd->string);
417 if(sed_cmd->cmd=='w')
Rob Landleyd921b2e2006-08-03 15:41:12 +0000418 sed_cmd->file=xfopen(sed_cmd->string,"w");
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000419 /* handle branch commands */
Rob Landley93850a52005-05-18 06:34:37 +0000420 } else if (strchr(":btT", sed_cmd->cmd)) {
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000421 int length;
422
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000423 while(isspace(*cmdstr)) cmdstr++;
Glenn L McGrathf4523562003-09-14 06:01:14 +0000424 length = strcspn(cmdstr, semicolon_whitespace);
425 if (length) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000426 sed_cmd->string = xstrndup(cmdstr, length);
Glenn L McGrathf4523562003-09-14 06:01:14 +0000427 cmdstr += length;
428 }
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000429 }
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000430 /* translation command */
431 else if (sed_cmd->cmd == 'y') {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000432 char *match, *replace;
433 int i=cmdstr[0];
434
435 cmdstr+=parse_regex_delim(cmdstr, &match, &replace)+1;
436 /* \n already parsed, but \delimiter needs unescaping. */
437 parse_escapes(match,match,strlen(match),i,i);
438 parse_escapes(replace,replace,strlen(replace),i,i);
439
Rob Landley9ffd4232006-05-21 18:30:35 +0000440 sed_cmd->string = xzalloc((strlen(match) + 1) * 2);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000441 for (i = 0; match[i] && replace[i]; i++) {
442 sed_cmd->string[i * 2] = match[i];
443 sed_cmd->string[(i * 2) + 1] = replace[i];
444 }
445 free(match);
446 free(replace);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000447 }
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000448 /* if it wasnt a single-letter command that takes no arguments
449 * then it must be an invalid command.
450 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000451 else if (strchr("dDgGhHlnNpPqx={}", sed_cmd->cmd) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000452 bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd);
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000453 }
Mark Whitley70705d72000-07-14 19:06:30 +0000454
455 /* give back whatever's left over */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000456 return (cmdstr);
Eric Andersen50d63601999-11-09 01:47:36 +0000457}
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000458
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000459
460/* Parse address+command sets, skipping comment lines. */
461
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000462static void add_cmd(char *cmdstr)
Erik Andersen1266a131999-12-29 22:19:46 +0000463{
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000464 sed_cmd_t *sed_cmd;
Glenn L McGrath586d86c2003-10-09 07:22:59 +0000465 int temp;
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000466
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000467 /* Append this line to any unfinished line from last time. */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000468 if (bbg.add_cmd_line) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000469 cmdstr = xasprintf("%s\n%s", bbg.add_cmd_line, cmdstr);
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000470 free(bbg.add_cmd_line);
471 bbg.add_cmd_line = cmdstr;
Rob Landley12d87552006-06-05 17:32:44 +0000472 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000473
474 /* If this line ends with backslash, request next line. */
Glenn L McGrath586d86c2003-10-09 07:22:59 +0000475 temp=strlen(cmdstr);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000476 if(temp && cmdstr[temp-1]=='\\') {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000477 if (!bbg.add_cmd_line) bbg.add_cmd_line = xstrdup(cmdstr);
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000478 bbg.add_cmd_line[temp-1] = 0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000479 return;
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000480 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000481
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000482 /* Loop parsing all commands in this line. */
483 while(*cmdstr) {
484 /* Skip leading whitespace and semicolons */
485 cmdstr += strspn(cmdstr, semicolon_whitespace);
486
487 /* If no more commands, exit. */
488 if(!*cmdstr) break;
489
490 /* if this is a comment, jump past it and keep going */
491 if (*cmdstr == '#') {
492 /* "#n" is the same as using -n on the command line */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000493 if (cmdstr[1] == 'n') bbg.be_quiet++;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000494 if(!(cmdstr=strpbrk(cmdstr, "\n\r"))) break;
495 continue;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000496 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000497
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000498 /* parse the command
499 * format is: [addr][,addr][!]cmd
500 * |----||-----||-|
501 * part1 part2 part3
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000502 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000503
Rob Landley9ffd4232006-05-21 18:30:35 +0000504 sed_cmd = xzalloc(sizeof(sed_cmd_t));
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000505
506 /* first part (if present) is an address: either a '$', a number or a /regex/ */
507 cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match);
508
509 /* second part (if present) will begin with a comma */
510 if (*cmdstr == ',') {
511 int idx;
512
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000513 cmdstr++;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000514 idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
Denis Vlasenko6d655be2006-09-06 19:02:46 +0000515 if (!idx) bb_error_msg_and_die("no address after comma");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000516 cmdstr += idx;
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000517 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000518
519 /* skip whitespace before the command */
520 while (isspace(*cmdstr)) cmdstr++;
521
522 /* Check for inversion flag */
523 if (*cmdstr == '!') {
524 sed_cmd->invert = 1;
525 cmdstr++;
526
527 /* skip whitespace before the command */
528 while (isspace(*cmdstr)) cmdstr++;
529 }
530
531 /* last part (mandatory) will be a command */
532 if (!*cmdstr) bb_error_msg_and_die("missing command");
533 sed_cmd->cmd = *(cmdstr++);
534 cmdstr = parse_cmd_args(sed_cmd, cmdstr);
535
536 /* Add the command to the command array */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000537 bbg.sed_cmd_tail->next = sed_cmd;
538 bbg.sed_cmd_tail = bbg.sed_cmd_tail->next;
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000539 }
540
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000541 /* If we glued multiple lines together, free the memory. */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000542 free(bbg.add_cmd_line);
543 bbg.add_cmd_line = NULL;
Mark Whitley6315ce62000-07-10 22:55:51 +0000544}
545
Rob Landleydcc28662004-11-25 07:21:47 +0000546/* Append to a string, reallocating memory as necessary. */
547
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000548#define PIPE_GROW 64
Eric Andersenc52a6b02001-11-10 10:49:42 +0000549
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000550static void pipe_putc(char c)
Eric Andersenc52a6b02001-11-10 10:49:42 +0000551{
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000552 if(bbg.pipeline.idx==bbg.pipeline.len) {
553 bbg.pipeline.buf = xrealloc(bbg.pipeline.buf,
554 bbg.pipeline.len + PIPE_GROW);
555 bbg.pipeline.len+=PIPE_GROW;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000556 }
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000557 bbg.pipeline.buf[bbg.pipeline.idx++] = c;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000558}
559
Rob Landley4795e4e2006-07-26 17:25:08 +0000560static void do_subst_w_backrefs(char *line, char *replace)
Mark Whitley97562bd2000-07-17 20:06:42 +0000561{
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000562 int i,j;
Mark Whitley97562bd2000-07-17 20:06:42 +0000563
564 /* go through the replacement string */
565 for (i = 0; replace[i]; i++) {
566 /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */
Rob Landleyc63fe912005-10-30 10:08:13 +0000567 if (replace[i] == '\\' && replace[i+1]>='0' && replace[i+1]<='9') {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000568 int backref=replace[++i]-'0';
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000569
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000570 /* print out the text held in bbg.regmatch[backref] */
571 if(bbg.regmatch[backref].rm_so != -1)
572 for (j = bbg.regmatch[backref].rm_so;
573 j < bbg.regmatch[backref].rm_eo; j++) pipe_putc(line[j]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000574 }
575
Mark Whitley83e85f62000-07-25 20:48:44 +0000576 /* if we find a backslash escaped character, print the character */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000577 else if (replace[i] == '\\') pipe_putc(replace[++i]);
Mark Whitley83e85f62000-07-25 20:48:44 +0000578
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000579 /* if we find an unescaped '&' print out the whole matched text. */
580 else if (replace[i] == '&')
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000581 for (j = bbg.regmatch[0].rm_so; j < bbg.regmatch[0].rm_eo; j++)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000582 pipe_putc(line[j]);
583 /* Otherwise just output the character. */
584 else pipe_putc(replace[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000585 }
586}
587
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000588static int do_subst_command(sed_cmd_t *sed_cmd, char **line)
Mark Whitley4f7fe772000-07-13 20:01:58 +0000589{
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000590 char *oldline = *line;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000591 int altered = 0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000592 int match_count=0;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000593 regex_t *current_regex;
594
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000595 /* Handle empty regex. */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000596 if (sed_cmd->sub_match == NULL) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000597 current_regex = bbg.previous_regex_ptr;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000598 if(!current_regex)
599 bb_error_msg_and_die("No previous regexp.");
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000600 } else bbg.previous_regex_ptr = current_regex = sed_cmd->sub_match;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000601
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000602 /* Find the first match */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000603 if(REG_NOMATCH==regexec(current_regex, oldline, 10, bbg.regmatch, 0))
Mark Whitley2dc192f2000-11-03 19:47:00 +0000604 return 0;
605
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000606 /* Initialize temporary output buffer. */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000607 bbg.pipeline.buf=xmalloc(PIPE_GROW);
608 bbg.pipeline.len=PIPE_GROW;
609 bbg.pipeline.idx=0;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000610
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000611 /* Now loop through, substituting for matches */
612 do {
Mark Whitley4f7fe772000-07-13 20:01:58 +0000613 int i;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000614
Eric Andersenc06f5682004-02-04 10:57:46 +0000615 /* Work around bug in glibc regexec, demonstrated by:
616 echo " a.b" | busybox sed 's [^ .]* x g'
617 The match_count check is so not to break
618 echo "hi" | busybox sed 's/^/!/g' */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000619 if(!bbg.regmatch[0].rm_so && !bbg.regmatch[0].rm_eo && match_count) {
Eric Andersenc06f5682004-02-04 10:57:46 +0000620 pipe_putc(*(oldline++));
621 continue;
622 }
623
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000624 match_count++;
625
626 /* If we aren't interested in this match, output old line to
627 end of match and continue */
628 if(sed_cmd->which_match && sed_cmd->which_match!=match_count) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000629 for(i=0;i<bbg.regmatch[0].rm_eo;i++)
Rob Landleyd35c0ef2006-09-08 00:01:02 +0000630 pipe_putc(*(oldline++));
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000631 continue;
632 }
633
Mark Whitley2dc192f2000-11-03 19:47:00 +0000634 /* print everything before the match */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000635 for (i = 0; i < bbg.regmatch[0].rm_so; i++) pipe_putc(oldline[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000636
Mark Whitley2dc192f2000-11-03 19:47:00 +0000637 /* then print the substitution string */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000638 do_subst_w_backrefs(oldline, sed_cmd->string);
Mark Whitley97562bd2000-07-17 20:06:42 +0000639
Mark Whitley2dc192f2000-11-03 19:47:00 +0000640 /* advance past the match */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000641 oldline += bbg.regmatch[0].rm_eo;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000642 /* flag that something has changed */
643 altered++;
Mark Whitley97562bd2000-07-17 20:06:42 +0000644
Mark Whitley2dc192f2000-11-03 19:47:00 +0000645 /* if we're not doing this globally, get out now */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000646 if (sed_cmd->which_match) break;
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000647 } while (*oldline && (regexec(current_regex, oldline, 10, bbg.regmatch, 0) != REG_NOMATCH));
Mark Whitley2dc192f2000-11-03 19:47:00 +0000648
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000649 /* Copy rest of string into output pipeline */
650
651 while(*oldline) pipe_putc(*(oldline++));
652 pipe_putc(0);
Mark Whitley2dc192f2000-11-03 19:47:00 +0000653
Eric Andersenb76cb682001-08-22 05:58:16 +0000654 free(*line);
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000655 *line = bbg.pipeline.buf;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000656 return altered;
657}
Mark Whitley6315ce62000-07-10 22:55:51 +0000658
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000659/* Set command pointer to point to this label. (Does not handle null label.) */
Rob Landley4795e4e2006-07-26 17:25:08 +0000660static sed_cmd_t *branch_to(char *label)
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000661{
662 sed_cmd_t *sed_cmd;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000663
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000664 for (sed_cmd = bbg.sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000665 if ((sed_cmd->cmd == ':') && (sed_cmd->string) && (strcmp(sed_cmd->string, label) == 0)) {
Glenn L McGrathf4523562003-09-14 06:01:14 +0000666 return (sed_cmd);
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000667 }
668 }
Glenn L McGrathf4523562003-09-14 06:01:14 +0000669 bb_error_msg_and_die("Can't find label for jump to `%s'", label);
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000670}
Mark Whitley6315ce62000-07-10 22:55:51 +0000671
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000672static void append(char *s)
673{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000674 llist_add_to_end(&bbg.append_head, xstrdup(s));
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000675}
676
677static void flush_append(void)
678{
Rob Landley0b656282006-05-08 22:17:23 +0000679 char *data;
680
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000681 /* Output appended lines. */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000682 while((data = (char *)llist_pop(&bbg.append_head))) {
683 fprintf(bbg.nonstdout,"%s\n",data);
Rob Landley0b656282006-05-08 22:17:23 +0000684 free(data);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000685 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000686}
687
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000688static void add_input_file(FILE *file)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000689{
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000690 bbg.input_file_list=xrealloc(bbg.input_file_list,
691 (bbg.input_file_count + 1) * sizeof(FILE *));
692 bbg.input_file_list[bbg.input_file_count++] = file;
Rob Landleydcc28662004-11-25 07:21:47 +0000693}
694
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000695/* Get next line of input from bbg.input_file_list, flushing append buffer and
Rob Landleydcc28662004-11-25 07:21:47 +0000696 * noting if we ran out of files without a newline on the last line we read.
697 */
698static char *get_next_line(int *no_newline)
699{
700 char *temp=NULL;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000701 int len;
702
703 flush_append();
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000704 while (bbg.current_input_file<bbg.input_file_count) {
705 temp = bb_get_chunk_from_file(bbg.input_file_list[bbg.current_input_file],&len);
Rob Landley2b26fd52006-02-24 02:30:39 +0000706 if (temp) {
707 *no_newline = !(len && temp[len-1]=='\n');
708 if (!*no_newline) temp[len-1] = 0;
Rob Landleydcc28662004-11-25 07:21:47 +0000709 break;
Rob Landley2b26fd52006-02-24 02:30:39 +0000710 // Close this file and advance to next one
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000711 } else fclose(bbg.input_file_list[bbg.current_input_file++]);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000712 }
713
714 return temp;
715}
716
717/* Output line of text. missing_newline means the last line output did not
718 end with a newline. no_newline means this line does not end with a
719 newline. */
720
721static int puts_maybe_newline(char *s, FILE *file, int missing_newline, int no_newline)
722{
723 if(missing_newline) fputc('\n',file);
724 fputs(s,file);
725 if(!no_newline) fputc('\n',file);
726
Rob Landley53302f82004-02-18 09:54:15 +0000727 if(ferror(file)) {
Bernhard Reutner-Fischera3d4bf32006-06-03 21:40:11 +0000728 bb_default_error_retval = 4; /* It's what gnu sed exits with... */
729 bb_error_msg_and_die(bb_msg_write_error);
Rob Landley53302f82004-02-18 09:54:15 +0000730 }
731
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000732 return no_newline;
733}
734
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000735#define sed_puts(s,n) missing_newline=puts_maybe_newline(s,bbg.nonstdout,missing_newline,n)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000736
Rob Landley2b26fd52006-02-24 02:30:39 +0000737/* Process all the lines in all the files */
738
Rob Landleydcc28662004-11-25 07:21:47 +0000739static void process_files(void)
Mark Whitley6315ce62000-07-10 22:55:51 +0000740{
Rob Landleyce4f0e92004-10-30 06:54:19 +0000741 char *pattern_space, *next_line;
Rob Landleydcc28662004-11-25 07:21:47 +0000742 int linenum = 0, missing_newline=0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000743 int no_newline,next_no_newline=0;
Mark Whitley6315ce62000-07-10 22:55:51 +0000744
Rob Landley2b26fd52006-02-24 02:30:39 +0000745 /* Prime the pump */
Rob Landleydcc28662004-11-25 07:21:47 +0000746 next_line = get_next_line(&next_no_newline);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000747
Rob Landleydcc28662004-11-25 07:21:47 +0000748 /* go through every line in each file */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000749 for(;;) {
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000750 sed_cmd_t *sed_cmd;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000751 int substituted=0;
752
753 /* Advance to next line. Stop if out of lines. */
754 if(!(pattern_space=next_line)) break;
755 no_newline=next_no_newline;
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000756
Rob Landley2b26fd52006-02-24 02:30:39 +0000757 /* Read one line in advance so we can act on the last line,
758 * the '$' address */
Rob Landleydcc28662004-11-25 07:21:47 +0000759 next_line = get_next_line(&next_no_newline);
Mark Whitley6315ce62000-07-10 22:55:51 +0000760 linenum++;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000761restart:
Mark Whitley6315ce62000-07-10 22:55:51 +0000762 /* for every line, go through all the commands */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000763 for (sed_cmd = bbg.sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next)
764 {
Eric Andersen52a3c272003-12-23 08:53:51 +0000765 int old_matched, matched;
766
767 old_matched = sed_cmd->in_match;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000768
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000769 /* Determine if this command matches this line: */
770
771 /* Are we continuing a previous multi-line match? */
772
773 sed_cmd->in_match = sed_cmd->in_match
774
775 /* Or is no range necessary? */
776 || (!sed_cmd->beg_line && !sed_cmd->end_line
777 && !sed_cmd->beg_match && !sed_cmd->end_match)
778
779 /* Or did we match the start of a numerical range? */
780 || (sed_cmd->beg_line > 0 && (sed_cmd->beg_line == linenum))
781
782 /* Or does this line match our begin address regex? */
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000783 || (sed_cmd->beg_match &&
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000784 !regexec(sed_cmd->beg_match, pattern_space, 0, NULL, 0))
785
786 /* Or did we match last line of input? */
787 || (sed_cmd->beg_line == -1 && next_line == NULL);
788
789 /* Snapshot the value */
790
791 matched = sed_cmd->in_match;
792
793 /* Is this line the end of the current match? */
794
795 if(matched) {
796 sed_cmd->in_match = !(
797 /* has the ending line come, or is this a single address command? */
798 (sed_cmd->end_line ?
799 sed_cmd->end_line==-1 ?
800 !next_line
801 : sed_cmd->end_line<=linenum
802 : !sed_cmd->end_match)
803 /* or does this line matches our last address regex */
Eric Andersen52a3c272003-12-23 08:53:51 +0000804 || (sed_cmd->end_match && old_matched && (regexec(sed_cmd->end_match, pattern_space, 0, NULL, 0) == 0))
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000805 );
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000806 }
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000807
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000808 /* Skip blocks of commands we didn't match. */
809 if (sed_cmd->cmd == '{') {
810 if(sed_cmd->invert ? matched : !matched)
811 while(sed_cmd && sed_cmd->cmd!='}') sed_cmd=sed_cmd->next;
812 if(!sed_cmd) bb_error_msg_and_die("Unterminated {");
813 continue;
814 }
815
816 /* Okay, so did this line match? */
817 if (sed_cmd->invert ? !matched : matched) {
818 /* Update last used regex in case a blank substitute BRE is found */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000819 if (sed_cmd->beg_match) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000820 bbg.previous_regex_ptr = sed_cmd->beg_match;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000821 }
Mark Whitley0915c4b2001-06-11 23:50:06 +0000822
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000823 /* actual sedding */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000824 switch (sed_cmd->cmd) {
Mark Whitley0915c4b2001-06-11 23:50:06 +0000825
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000826 /* Print line number */
827 case '=':
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000828 fprintf(bbg.nonstdout,"%d\n", linenum);
Glenn L McGrath2eed0e22003-09-15 06:28:45 +0000829 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000830
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000831 /* Write the current pattern space up to the first newline */
832 case 'P':
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000833 {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000834 char *tmp = strchr(pattern_space, '\n');
Glenn L McGrath0c518322003-03-30 03:41:53 +0000835
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000836 if (tmp) {
837 *tmp = '\0';
838 sed_puts(pattern_space,1);
839 *tmp = '\n';
840 break;
841 }
842 /* Fall Through */
843 }
844
845 /* Write the current pattern space to output */
846 case 'p':
847 sed_puts(pattern_space,no_newline);
848 break;
849 /* Delete up through first newline */
850 case 'D':
851 {
852 char *tmp = strchr(pattern_space,'\n');
853
854 if(tmp) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000855 tmp=xstrdup(tmp+1);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000856 free(pattern_space);
857 pattern_space=tmp;
858 goto restart;
Glenn L McGrath0c518322003-03-30 03:41:53 +0000859 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000860 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000861 /* discard this line. */
862 case 'd':
863 goto discard_line;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000864
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000865 /* Substitute with regex */
866 case 's':
867 if(do_subst_command(sed_cmd, &pattern_space)) {
868 substituted|=1;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000869
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000870 /* handle p option */
871 if(sed_cmd->sub_p)
872 sed_puts(pattern_space,no_newline);
873 /* handle w option */
874 if(sed_cmd->file)
875 sed_cmd->no_newline=puts_maybe_newline(pattern_space, sed_cmd->file, sed_cmd->no_newline, no_newline);
Mark Whitley0915c4b2001-06-11 23:50:06 +0000876
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000877 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000878 break;
879
880 /* Append line to linked list to be printed later */
881 case 'a':
882 {
883 append(sed_cmd->string);
884 break;
Glenn L McGrathd87a7ac2003-04-09 15:26:14 +0000885 }
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000886
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000887 /* Insert text before this line */
888 case 'i':
889 sed_puts(sed_cmd->string,1);
Glenn L McGrath2570b432003-09-16 05:25:43 +0000890 break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000891
892 /* Cut and paste text (replace) */
893 case 'c':
894 /* Only triggers on last line of a matching range. */
Rob Landleyce4f0e92004-10-30 06:54:19 +0000895 if (!sed_cmd->in_match) sed_puts(sed_cmd->string,0);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000896 goto discard_line;
897
898 /* Read file, append contents to output */
899 case 'r':
Glenn L McGrathf4523562003-09-14 06:01:14 +0000900 {
Rob Landley2b26fd52006-02-24 02:30:39 +0000901 FILE *rfile;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000902
Rob Landley2b26fd52006-02-24 02:30:39 +0000903 rfile = fopen(sed_cmd->string, "r");
904 if (rfile) {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000905 char *line;
906
Rob Landley2b26fd52006-02-24 02:30:39 +0000907 while ((line = bb_get_chomped_line_from_file(rfile))
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000908 != NULL)
909 append(line);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000910 xprint_and_close_file(rfile);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000911 }
912
913 break;
914 }
915
916 /* Write pattern space to file. */
917 case 'w':
918 sed_cmd->no_newline=puts_maybe_newline(pattern_space,sed_cmd->file, sed_cmd->no_newline,no_newline);
919 break;
920
921 /* Read next line from input */
922 case 'n':
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000923 if (!bbg.be_quiet)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000924 sed_puts(pattern_space,no_newline);
925 if (next_line) {
926 free(pattern_space);
927 pattern_space = next_line;
928 no_newline=next_no_newline;
Rob Landleydcc28662004-11-25 07:21:47 +0000929 next_line = get_next_line(&next_no_newline);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000930 linenum++;
931 break;
932 }
933 /* fall through */
934
935 /* Quit. End of script, end of input. */
936 case 'q':
937 /* Exit the outer while loop */
938 free(next_line);
939 next_line = NULL;
940 goto discard_commands;
941
942 /* Append the next line to the current line */
943 case 'N':
944 {
945 /* If no next line, jump to end of script and exit. */
946 if (next_line == NULL) {
947 /* Jump to end of script and exit */
948 free(next_line);
949 next_line = NULL;
950 goto discard_line;
951 /* append next_line, read new next_line. */
Glenn L McGrathf4523562003-09-14 06:01:14 +0000952 } else {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000953 int len=strlen(pattern_space);
954
955 pattern_space = realloc(pattern_space, len + strlen(next_line) + 2);
956 pattern_space[len]='\n';
957 strcpy(pattern_space+len+1, next_line);
958 no_newline=next_no_newline;
Rob Landleydcc28662004-11-25 07:21:47 +0000959 next_line = get_next_line(&next_no_newline);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000960 linenum++;
Glenn L McGrathf4523562003-09-14 06:01:14 +0000961 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000962 break;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000963 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000964
Rob Landley93850a52005-05-18 06:34:37 +0000965 /* Test/branch if substitution occurred */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000966 case 't':
Rob Landley93850a52005-05-18 06:34:37 +0000967 if(!substituted) break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000968 substituted=0;
Rob Landley93850a52005-05-18 06:34:37 +0000969 /* Fall through */
970 /* Test/branch if substitution didn't occur */
971 case 'T':
972 if (substituted) break;
973 /* Fall through */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000974 /* Branch to label */
975 case 'b':
976 if (!sed_cmd->string) goto discard_commands;
977 else sed_cmd = branch_to(sed_cmd->string);
978 break;
979 /* Transliterate characters */
980 case 'y':
981 {
982 int i;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000983
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000984 for (i = 0; pattern_space[i]; i++) {
985 int j;
986
987 for (j = 0; sed_cmd->string[j]; j += 2) {
988 if (pattern_space[i] == sed_cmd->string[j]) {
989 pattern_space[i] = sed_cmd->string[j + 1];
Rob Landleybabd3fb2005-09-02 00:10:06 +0000990 break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000991 }
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000992 }
993 }
Glenn L McGrath294d1132003-09-14 16:28:08 +0000994
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000995 break;
Glenn L McGrath91e19782003-04-26 07:40:07 +0000996 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000997 case 'g': /* Replace pattern space with hold space */
998 free(pattern_space);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000999 pattern_space = xstrdup(bbg.hold_space ? bbg.hold_space : "");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001000 break;
1001 case 'G': /* Append newline and hold space to pattern space */
1002 {
1003 int pattern_space_size = 2;
1004 int hold_space_size = 0;
1005
1006 if (pattern_space)
1007 pattern_space_size += strlen(pattern_space);
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001008 if (bbg.hold_space)
1009 hold_space_size = strlen(bbg.hold_space);
1010 pattern_space = xrealloc(pattern_space,
1011 pattern_space_size + hold_space_size);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001012 if (pattern_space_size == 2) pattern_space[0]=0;
Glenn L McGrath977451e2003-09-15 12:07:48 +00001013 strcat(pattern_space, "\n");
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001014 if (bbg.hold_space)
1015 strcat(pattern_space, bbg.hold_space);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001016 no_newline=0;
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001017
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001018 break;
Glenn L McGrath91e19782003-04-26 07:40:07 +00001019 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001020 case 'h': /* Replace hold space with pattern space */
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001021 free(bbg.hold_space);
Rob Landleyd921b2e2006-08-03 15:41:12 +00001022 bbg.hold_space = xstrdup(pattern_space);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001023 break;
1024 case 'H': /* Append newline and pattern space to hold space */
1025 {
1026 int hold_space_size = 2;
1027 int pattern_space_size = 0;
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001028
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001029 if (bbg.hold_space)
1030 hold_space_size += strlen(bbg.hold_space);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001031 if (pattern_space)
1032 pattern_space_size = strlen(pattern_space);
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001033 bbg.hold_space = xrealloc(bbg.hold_space,
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001034 hold_space_size + pattern_space_size);
1035
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001036 if (hold_space_size == 2) *bbg.hold_space=0;
1037 strcat(bbg.hold_space, "\n");
1038 if (pattern_space) strcat(bbg.hold_space, pattern_space);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001039
1040 break;
Glenn L McGrath4dc1d252003-09-14 01:25:31 +00001041 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001042 case 'x': /* Exchange hold and pattern space */
1043 {
1044 char *tmp = pattern_space;
Rob Landleyea7c1852006-06-28 13:50:59 +00001045 pattern_space = bbg.hold_space ? : xzalloc(1);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001046 no_newline=0;
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001047 bbg.hold_space = tmp;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001048 break;
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001049 }
Mark Whitley0915c4b2001-06-11 23:50:06 +00001050 }
Robert Griebl47abc492002-06-11 23:43:27 +00001051 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001052 }
Erik Andersen1266a131999-12-29 22:19:46 +00001053
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001054 /*
1055 * exit point from sedding...
1056 */
1057discard_commands:
1058 /* we will print the line unless we were told to be quiet ('-n')
1059 or if the line was suppressed (ala 'd'elete) */
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001060 if (!bbg.be_quiet) sed_puts(pattern_space,no_newline);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001061
1062 /* Delete and such jump here. */
1063discard_line:
1064 flush_append();
Glenn L McGrathc6adada2003-04-07 12:24:44 +00001065 free(pattern_space);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001066 }
Erik Andersen1266a131999-12-29 22:19:46 +00001067}
1068
Glenn L McGrath42c25732003-10-04 05:27:56 +00001069/* It is possible to have a command line argument with embedded
1070 newlines. This counts as multiple command lines. */
1071
1072static void add_cmd_block(char *cmdstr)
1073{
1074 int go=1;
Rob Landleyd921b2e2006-08-03 15:41:12 +00001075 char *temp=xstrdup(cmdstr),*temp2=temp;
Glenn L McGrath42c25732003-10-04 05:27:56 +00001076
1077 while(go) {
1078 int len=strcspn(temp2,"\n");
1079 if(!temp2[len]) go=0;
1080 else temp2[len]=0;
1081 add_cmd(temp2);
1082 temp2+=len+1;
1083 }
1084 free(temp);
1085}
1086
Rob Landleydfba7412006-03-06 20:47:33 +00001087int sed_main(int argc, char **argv)
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001088{
Rob Landleyce4f0e92004-10-30 06:54:19 +00001089 int status = EXIT_SUCCESS, opt, getpat = 1;
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001090
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001091 bbg.sed_cmd_tail=&bbg.sed_cmd_head;
1092
Mark Whitley858c1ad2000-07-11 21:38:47 +00001093 /* destroy command strings on exit */
Rob Landley0b656282006-05-08 22:17:23 +00001094 if (ENABLE_FEATURE_CLEAN_UP) atexit(sed_free_and_close_stuff);
Mark Whitley858c1ad2000-07-11 21:38:47 +00001095
Rob Landleyfae1dc82005-11-20 07:44:35 +00001096 /* Lie to autoconf when it starts asking stupid questions. */
Eric Andersenc06f5682004-02-04 10:57:46 +00001097 if(argc==2 && !strcmp(argv[1],"--version")) {
1098 printf("This is not GNU sed version 4.0\n");
1099 exit(0);
1100 }
Eric Andersenc06f5682004-02-04 10:57:46 +00001101
Mark Whitley6315ce62000-07-10 22:55:51 +00001102 /* do normal option parsing */
Eric Andersen98555482004-05-26 10:03:33 +00001103 while ((opt = getopt(argc, argv, "irne:f:")) > 0) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001104 switch (opt) {
Rob Landley53302f82004-02-18 09:54:15 +00001105 case 'i':
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001106 bbg.in_place++;
Rob Landley53302f82004-02-18 09:54:15 +00001107 atexit(cleanup_outname);
1108 break;
Eric Andersen98555482004-05-26 10:03:33 +00001109 case 'r':
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001110 bbg.regex_type|=REG_EXTENDED;
Eric Andersen98555482004-05-26 10:03:33 +00001111 break;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001112 case 'n':
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001113 bbg.be_quiet++;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001114 break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001115 case 'e':
Glenn L McGrath42c25732003-10-04 05:27:56 +00001116 add_cmd_block(optarg);
Eric Andersenfaa7d862004-04-21 00:56:22 +00001117 getpat=0;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001118 break;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001119 case 'f':
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001120 {
1121 FILE *cmdfile;
1122 char *line;
1123
Rob Landleyd921b2e2006-08-03 15:41:12 +00001124 cmdfile = xfopen(optarg, "r");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001125
Rob Landleyfae1dc82005-11-20 07:44:35 +00001126 while ((line = bb_get_chomped_line_from_file(cmdfile)) != NULL) {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001127 add_cmd(line);
Eric Andersenfaa7d862004-04-21 00:56:22 +00001128 getpat=0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001129 free(line);
1130 }
Rob Landleyd921b2e2006-08-03 15:41:12 +00001131 xprint_and_close_file(cmdfile);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001132
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001133 break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001134 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001135 default:
1136 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001137 }
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001138 }
Mark Whitley6315ce62000-07-10 22:55:51 +00001139
Rob Landleydcc28662004-11-25 07:21:47 +00001140 /* if we didn't get a pattern from -e or -f, use argv[optind] */
Eric Andersenfaa7d862004-04-21 00:56:22 +00001141 if(getpat) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001142 if (argv[optind] == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001143 bb_show_usage();
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001144 else
Glenn L McGrath42c25732003-10-04 05:27:56 +00001145 add_cmd_block(argv[optind++]);
Mark Whitley6315ce62000-07-10 22:55:51 +00001146 }
Glenn L McGrath42c25732003-10-04 05:27:56 +00001147 /* Flush any unfinished commands. */
1148 add_cmd("");
Mark Whitley6315ce62000-07-10 22:55:51 +00001149
Rob Landley53302f82004-02-18 09:54:15 +00001150 /* By default, we write to stdout */
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001151 bbg.nonstdout=stdout;
Rob Landley53302f82004-02-18 09:54:15 +00001152
Mark Whitley6315ce62000-07-10 22:55:51 +00001153 /* argv[(optind)..(argc-1)] should be names of file to process. If no
1154 * files were specified or '-' was specified, take input from stdin.
1155 * Otherwise, we process all the files specified. */
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001156 if (argv[optind] == NULL) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +00001157 if(bbg.in_place) bb_error_msg_and_die(bb_msg_requires_arg, "-i");
Rob Landleydcc28662004-11-25 07:21:47 +00001158 add_input_file(stdin);
1159 process_files();
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001160 } else {
Mark Whitley6315ce62000-07-10 22:55:51 +00001161 int i;
1162 FILE *file;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001163
Mark Whitley6315ce62000-07-10 22:55:51 +00001164 for (i = optind; i < argc; i++) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001165 if(!strcmp(argv[i], "-") && !bbg.in_place) {
Rob Landleydcc28662004-11-25 07:21:47 +00001166 add_input_file(stdin);
1167 process_files();
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001168 } else {
1169 file = bb_wfopen(argv[i], "r");
1170 if (file) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001171 if(bbg.in_place) {
Rob Landley53302f82004-02-18 09:54:15 +00001172 struct stat statbuf;
Rob Landley5797c7f2005-05-18 05:56:16 +00001173 int nonstdoutfd;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001174
Rob Landleyd921b2e2006-08-03 15:41:12 +00001175 bbg.outname=xstrndup(argv[i],strlen(argv[i])+6);
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001176 strcat(bbg.outname,"XXXXXX");
1177 if(-1==(nonstdoutfd=mkstemp(bbg.outname)))
Rob Landley5797c7f2005-05-18 05:56:16 +00001178 bb_error_msg_and_die("no temp file");
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001179 bbg.nonstdout=fdopen(nonstdoutfd,"w");
Bernhard Reutner-Fischerb7f39732006-03-01 20:14:16 +00001180
Rob Landley53302f82004-02-18 09:54:15 +00001181 /* Set permissions of output file */
Bernhard Reutner-Fischerb7f39732006-03-01 20:14:16 +00001182
Rob Landley53302f82004-02-18 09:54:15 +00001183 fstat(fileno(file),&statbuf);
Rob Landley5797c7f2005-05-18 05:56:16 +00001184 fchmod(nonstdoutfd,statbuf.st_mode);
Rob Landleydcc28662004-11-25 07:21:47 +00001185 add_input_file(file);
1186 process_files();
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001187 fclose(bbg.nonstdout);
Bernhard Reutner-Fischerb7f39732006-03-01 20:14:16 +00001188
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001189 bbg.nonstdout=stdout;
Rob Landley53302f82004-02-18 09:54:15 +00001190 unlink(argv[i]);
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001191 rename(bbg.outname,argv[i]);
1192 free(bbg.outname);
1193 bbg.outname=0;
Rob Landleydcc28662004-11-25 07:21:47 +00001194 } else add_input_file(file);
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001195 } else {
1196 status = EXIT_FAILURE;
1197 }
1198 }
Mark Whitley6315ce62000-07-10 22:55:51 +00001199 }
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001200 if(bbg.input_file_count>bbg.current_input_file) process_files();
Mark Whitley6315ce62000-07-10 22:55:51 +00001201 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001202
Matt Kraaia5f09c62001-11-12 16:44:55 +00001203 return status;
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001204}