blob: a1602394523f73dd3b88757a5bba157d982cd7a4 [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 Landleye9a7a622006-09-22 02:52:41 +000013 * Licensed under GPL version 2, 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 {
Denis Vlasenkod18a3a22006-10-25 12:46:03 +000066 /* Ordered by alignment requirements: currently 36 bytes on x86 */
Mark Whitley2dc192f2000-11-03 19:47:00 +000067
Denis Vlasenkod18a3a22006-10-25 12:46:03 +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
Denis Vlasenkod18a3a22006-10-25 12:46:03 +000075 FILE *file; /* File (sw) command writes to, -1 for none. */
76 char *string; /* Data string for (saicytb) commands. */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +000077
Denis Vlasenkod18a3a22006-10-25 12:46:03 +000078 unsigned short which_match; /* (s) Which match to replace (0 for all) */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +000079
Denis Vlasenkod18a3a22006-10-25 12:46:03 +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? */
83 unsigned int no_newline:1; /* Last line written by (sw) had no '\n' */
84 unsigned int sub_p:1; /* (s) print option */
Glenn L McGrathc18ce372003-09-13 06:57:39 +000085
Denis Vlasenkod18a3a22006-10-25 12:46:03 +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
Denis Vlasenko80778502006-10-25 12:46:46 +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
Denis Vlasenko80778502006-10-25 12:46:46 +0000154 if (bbg.hold_space) free(bbg.hold_space);
Rob Landleydcc28662004-11-25 07:21:47 +0000155
Denis Vlasenko80778502006-10-25 12:46:46 +0000156 while (bbg.current_input_file < bbg.input_file_count)
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000157 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{
Denis Vlasenko80778502006-10-25 12:46:46 +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{
Denis Vlasenko80778502006-10-25 12:46:46 +0000172 int i = 0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000173
Denis Vlasenko80778502006-10-25 12:46:46 +0000174 while (i < len) {
175 if (string[i] == '\\') {
176 if (!to || string[i+1] == from) {
Glenn L McGrath738fb332003-10-01 06:45:11 +0000177 *(dest++) = to ? to : string[i+1];
Denis Vlasenko80778502006-10-25 12:46:46 +0000178 i += 2;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000179 continue;
Denis Vlasenko80778502006-10-25 12:46:46 +0000180 } else *(dest++) = string[i++];
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000181 }
182 *(dest++) = string[i++];
183 }
Denis Vlasenko80778502006-10-25 12:46:46 +0000184 *dest = 0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000185}
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{
Denis Vlasenko80778502006-10-25 12:46:46 +0000189 char *dest = xmalloc(len+1);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000190
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");
Denis Vlasenko80778502006-10-25 12:46:46 +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
Denis Vlasenko80778502006-10-25 12:46:46 +0000278 delimiter = '/';
279 if (*my_str == '\\') delimiter = *++pos;
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);
Denis Vlasenko80778502006-10-25 12:46:46 +0000282 *regex = 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{
Denis Vlasenko80778502006-10-25 12:46:46 +0000294 int start = 0, idx, hack = 0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000295
296 /* Skip whitespace, then grab filename to end of line */
297 while (isspace(filecmdstr[start])) start++;
Denis Vlasenko80778502006-10-25 12:46:46 +0000298 idx = start;
299 while (filecmdstr[idx] && filecmdstr[idx] != '\n') idx++;
300
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000301 /* If lines glued together, put backslash back. */
Denis Vlasenko80778502006-10-25 12:46:46 +0000302 if (filecmdstr[idx] == '\n') hack = 1;
303 if (idx == start)
304 bb_error_msg_and_die("empty filename");
Rob Landleyd921b2e2006-08-03 15:41:12 +0000305 *retval = xstrndup(filecmdstr+start, idx-start+hack+1);
Denis Vlasenko80778502006-10-25 12:46:46 +0000306 if (hack) (*retval)[idx] = '\\';
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000307
308 return idx;
309}
310
Rob Landley4795e4e2006-07-26 17:25:08 +0000311static int parse_subst_cmd(sed_cmd_t *sed_cmd, char *substr)
Mark Whitley06f35292000-07-13 19:58:04 +0000312{
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000313 int cflags = bbg.regex_type;
Mark Whitley06f35292000-07-13 19:58:04 +0000314 char *match;
315 int idx = 0;
316
317 /*
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000318 * A substitution command should look something like this:
319 * s/match/replace/ #gIpw
Mark Whitley0e4cec02000-08-21 21:29:20 +0000320 * || | |||
Mark Whitley06f35292000-07-13 19:58:04 +0000321 * mandatory optional
Mark Whitley06f35292000-07-13 19:58:04 +0000322 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000323 idx = parse_regex_delim(substr, &match, &sed_cmd->string);
Mark Whitley06f35292000-07-13 19:58:04 +0000324
Mark Whitley97562bd2000-07-17 20:06:42 +0000325 /* determine the number of back references in the match string */
326 /* Note: we compute this here rather than in the do_subst_command()
327 * function to save processor time, at the expense of a little more memory
328 * (4 bits) per sed_cmd */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000329
Mark Whitley06f35292000-07-13 19:58:04 +0000330 /* process the flags */
Mark Whitley70705d72000-07-14 19:06:30 +0000331
Denis Vlasenko80778502006-10-25 12:46:46 +0000332 sed_cmd->which_match = 1;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000333 while (substr[++idx]) {
334 /* Parse match number */
Denis Vlasenko80778502006-10-25 12:46:46 +0000335 if (isdigit(substr[idx])) {
336 if (match[0] != '^') {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000337 /* Match 0 treated as all, multiple matches we take the last one. */
Denis Vlasenko80778502006-10-25 12:46:46 +0000338 char *pos = substr+idx;
339 sed_cmd->which_match = (unsigned short)strtol(substr+idx,&pos,10);
340 idx = pos-substr;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000341 }
342 continue;
343 }
Rob Landley40ec4ae2004-01-04 06:42:14 +0000344 /* Skip spaces */
Denis Vlasenko80778502006-10-25 12:46:46 +0000345 if (isspace(substr[idx])) continue;
Rob Landley40ec4ae2004-01-04 06:42:14 +0000346
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000347 switch (substr[idx]) {
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000348 /* Replace all occurrences */
349 case 'g':
350 if (match[0] != '^') sed_cmd->which_match = 0;
351 break;
352 /* Print pattern space */
353 case 'p':
354 sed_cmd->sub_p = 1;
355 break;
356 /* Write to file */
357 case 'w':
358 {
359 char *temp;
Denis Vlasenko80778502006-10-25 12:46:46 +0000360 idx += parse_file_cmd(sed_cmd,substr+idx,&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000361
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000362 break;
363 }
364 /* Ignore case (gnu exension) */
365 case 'I':
366 cflags |= REG_ICASE;
367 break;
368 /* Comment */
369 case '#':
Denis Vlasenko80778502006-10-25 12:46:46 +0000370 while (substr[++idx]) /*skip all*/;
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000371 /* Fall through */
372 /* End of command */
373 case ';':
374 case '}':
375 goto out;
376 default:
377 bb_error_msg_and_die("bad option in substitution expression");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000378 }
379 }
Glenn L McGrath2570b432003-09-16 05:25:43 +0000380out:
Mark Whitley97562bd2000-07-17 20:06:42 +0000381 /* compile the match string into a regex */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000382 if (*match != '\0') {
383 /* If match is empty, we use last regex used at runtime */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000384 sed_cmd->sub_match = (regex_t *) xmalloc(sizeof(regex_t));
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000385 xregcomp(sed_cmd->sub_match, match, cflags);
386 }
Mark Whitley06f35292000-07-13 19:58:04 +0000387 free(match);
Mark Whitley70705d72000-07-14 19:06:30 +0000388
389 return idx;
Mark Whitley06f35292000-07-13 19:58:04 +0000390}
391
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000392/*
393 * Process the commands arguments
394 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000395static char *parse_cmd_args(sed_cmd_t *sed_cmd, char *cmdstr)
Mark Whitley6315ce62000-07-10 22:55:51 +0000396{
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000397 /* handle (s)ubstitution command */
Denis Vlasenko80778502006-10-25 12:46:46 +0000398 if (sed_cmd->cmd == 's')
399 cmdstr += parse_subst_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000400 /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */
401 else if (strchr("aic", sed_cmd->cmd)) {
402 if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000403 bb_error_msg_and_die
404 ("only a beginning address can be specified for edit commands");
Denis Vlasenko80778502006-10-25 12:46:46 +0000405 for (;;) {
406 if (*cmdstr == '\n' || *cmdstr == '\\') {
Rob Landley25d82392004-04-01 09:23:30 +0000407 cmdstr++;
408 break;
Denis Vlasenko80778502006-10-25 12:46:46 +0000409 } else if (isspace(*cmdstr))
410 cmdstr++;
411 else
412 break;
Rob Landley25d82392004-04-01 09:23:30 +0000413 }
Rob Landleyd921b2e2006-08-03 15:41:12 +0000414 sed_cmd->string = xstrdup(cmdstr);
Glenn L McGrath738fb332003-10-01 06:45:11 +0000415 parse_escapes(sed_cmd->string,sed_cmd->string,strlen(cmdstr),0,0);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000416 cmdstr += strlen(cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000417 /* handle file cmds: (r)ead */
Denis Vlasenko80778502006-10-25 12:46:46 +0000418 } else if (strchr("rw", sed_cmd->cmd)) {
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000419 if (sed_cmd->end_line || sed_cmd->end_match)
Denis Vlasenko80778502006-10-25 12:46:46 +0000420 bb_error_msg_and_die("command only uses one address");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000421 cmdstr += parse_file_cmd(sed_cmd, cmdstr, &sed_cmd->string);
Denis Vlasenko80778502006-10-25 12:46:46 +0000422 if (sed_cmd->cmd == 'w')
423 sed_cmd->file = xfopen(sed_cmd->string,"w");
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000424 /* handle branch commands */
Rob Landley93850a52005-05-18 06:34:37 +0000425 } else if (strchr(":btT", sed_cmd->cmd)) {
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000426 int length;
427
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000428 cmdstr = skip_whitespace(cmdstr);
Glenn L McGrathf4523562003-09-14 06:01:14 +0000429 length = strcspn(cmdstr, semicolon_whitespace);
430 if (length) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000431 sed_cmd->string = xstrndup(cmdstr, length);
Glenn L McGrathf4523562003-09-14 06:01:14 +0000432 cmdstr += length;
433 }
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000434 }
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000435 /* translation command */
436 else if (sed_cmd->cmd == 'y') {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000437 char *match, *replace;
Denis Vlasenko80778502006-10-25 12:46:46 +0000438 int i = cmdstr[0];
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000439
Denis Vlasenko80778502006-10-25 12:46:46 +0000440 cmdstr += parse_regex_delim(cmdstr, &match, &replace)+1;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000441 /* \n already parsed, but \delimiter needs unescaping. */
442 parse_escapes(match,match,strlen(match),i,i);
443 parse_escapes(replace,replace,strlen(replace),i,i);
444
Rob Landley9ffd4232006-05-21 18:30:35 +0000445 sed_cmd->string = xzalloc((strlen(match) + 1) * 2);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000446 for (i = 0; match[i] && replace[i]; i++) {
Denis Vlasenko80778502006-10-25 12:46:46 +0000447 sed_cmd->string[i*2] = match[i];
448 sed_cmd->string[i*2+1] = replace[i];
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000449 }
450 free(match);
451 free(replace);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000452 }
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000453 /* if it wasnt a single-letter command that takes no arguments
454 * then it must be an invalid command.
455 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000456 else if (strchr("dDgGhHlnNpPqx={}", sed_cmd->cmd) == 0) {
Denis Vlasenko80778502006-10-25 12:46:46 +0000457 bb_error_msg_and_die("unsupported command %c", sed_cmd->cmd);
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000458 }
Mark Whitley70705d72000-07-14 19:06:30 +0000459
460 /* give back whatever's left over */
Denis Vlasenko80778502006-10-25 12:46:46 +0000461 return cmdstr;
Eric Andersen50d63601999-11-09 01:47:36 +0000462}
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000463
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000464
465/* Parse address+command sets, skipping comment lines. */
466
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000467static void add_cmd(char *cmdstr)
Erik Andersen1266a131999-12-29 22:19:46 +0000468{
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000469 sed_cmd_t *sed_cmd;
Glenn L McGrath586d86c2003-10-09 07:22:59 +0000470 int temp;
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000471
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000472 /* Append this line to any unfinished line from last time. */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000473 if (bbg.add_cmd_line) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000474 cmdstr = xasprintf("%s\n%s", bbg.add_cmd_line, cmdstr);
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000475 free(bbg.add_cmd_line);
476 bbg.add_cmd_line = cmdstr;
Rob Landley12d87552006-06-05 17:32:44 +0000477 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000478
479 /* If this line ends with backslash, request next line. */
Denis Vlasenko80778502006-10-25 12:46:46 +0000480 temp = strlen(cmdstr);
481 if (temp && cmdstr[temp-1] == '\\') {
482 if (!bbg.add_cmd_line)
483 bbg.add_cmd_line = xstrdup(cmdstr);
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000484 bbg.add_cmd_line[temp-1] = 0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000485 return;
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000486 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000487
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000488 /* Loop parsing all commands in this line. */
Denis Vlasenko80778502006-10-25 12:46:46 +0000489 while (*cmdstr) {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000490 /* Skip leading whitespace and semicolons */
491 cmdstr += strspn(cmdstr, semicolon_whitespace);
492
493 /* If no more commands, exit. */
Denis Vlasenko80778502006-10-25 12:46:46 +0000494 if (!*cmdstr) break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000495
496 /* if this is a comment, jump past it and keep going */
497 if (*cmdstr == '#') {
498 /* "#n" is the same as using -n on the command line */
Denis Vlasenko80778502006-10-25 12:46:46 +0000499 if (cmdstr[1] == 'n')
500 bbg.be_quiet++;
501 cmdstr = strpbrk(cmdstr, "\n\r");
502 if (!cmdstr) break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000503 continue;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000504 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000505
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000506 /* parse the command
507 * format is: [addr][,addr][!]cmd
508 * |----||-----||-|
509 * part1 part2 part3
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000510 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000511
Rob Landley9ffd4232006-05-21 18:30:35 +0000512 sed_cmd = xzalloc(sizeof(sed_cmd_t));
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000513
514 /* first part (if present) is an address: either a '$', a number or a /regex/ */
515 cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match);
516
517 /* second part (if present) will begin with a comma */
518 if (*cmdstr == ',') {
519 int idx;
520
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000521 cmdstr++;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000522 idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
Denis Vlasenko80778502006-10-25 12:46:46 +0000523 if (!idx)
524 bb_error_msg_and_die("no address after comma");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000525 cmdstr += idx;
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000526 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000527
528 /* skip whitespace before the command */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000529 cmdstr = skip_whitespace(cmdstr);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000530
531 /* Check for inversion flag */
532 if (*cmdstr == '!') {
533 sed_cmd->invert = 1;
534 cmdstr++;
535
536 /* skip whitespace before the command */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000537 cmdstr = skip_whitespace(cmdstr);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000538 }
539
540 /* last part (mandatory) will be a command */
Denis Vlasenko80778502006-10-25 12:46:46 +0000541 if (!*cmdstr)
542 bb_error_msg_and_die("missing command");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000543 sed_cmd->cmd = *(cmdstr++);
544 cmdstr = parse_cmd_args(sed_cmd, cmdstr);
545
546 /* Add the command to the command array */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000547 bbg.sed_cmd_tail->next = sed_cmd;
548 bbg.sed_cmd_tail = bbg.sed_cmd_tail->next;
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000549 }
550
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000551 /* If we glued multiple lines together, free the memory. */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000552 free(bbg.add_cmd_line);
553 bbg.add_cmd_line = NULL;
Mark Whitley6315ce62000-07-10 22:55:51 +0000554}
555
Rob Landleydcc28662004-11-25 07:21:47 +0000556/* Append to a string, reallocating memory as necessary. */
557
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000558#define PIPE_GROW 64
Eric Andersenc52a6b02001-11-10 10:49:42 +0000559
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000560static void pipe_putc(char c)
Eric Andersenc52a6b02001-11-10 10:49:42 +0000561{
Denis Vlasenko80778502006-10-25 12:46:46 +0000562 if (bbg.pipeline.idx == bbg.pipeline.len) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000563 bbg.pipeline.buf = xrealloc(bbg.pipeline.buf,
Denis Vlasenko80778502006-10-25 12:46:46 +0000564 bbg.pipeline.len + PIPE_GROW);
565 bbg.pipeline.len += PIPE_GROW;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000566 }
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000567 bbg.pipeline.buf[bbg.pipeline.idx++] = c;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000568}
569
Rob Landley4795e4e2006-07-26 17:25:08 +0000570static void do_subst_w_backrefs(char *line, char *replace)
Mark Whitley97562bd2000-07-17 20:06:42 +0000571{
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000572 int i,j;
Mark Whitley97562bd2000-07-17 20:06:42 +0000573
574 /* go through the replacement string */
575 for (i = 0; replace[i]; i++) {
576 /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */
Denis Vlasenko80778502006-10-25 12:46:46 +0000577 if (replace[i] == '\\' && replace[i+1] >= '0' && replace[i+1] <= '9') {
578 int backref = replace[++i]-'0';
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000579
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000580 /* print out the text held in bbg.regmatch[backref] */
Denis Vlasenko80778502006-10-25 12:46:46 +0000581 if (bbg.regmatch[backref].rm_so != -1) {
582 j = bbg.regmatch[backref].rm_so;
583 while (j < bbg.regmatch[backref].rm_eo)
584 pipe_putc(line[j++]);
585 }
Mark Whitley97562bd2000-07-17 20:06:42 +0000586 }
587
Mark Whitley83e85f62000-07-25 20:48:44 +0000588 /* if we find a backslash escaped character, print the character */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000589 else if (replace[i] == '\\') pipe_putc(replace[++i]);
Mark Whitley83e85f62000-07-25 20:48:44 +0000590
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000591 /* if we find an unescaped '&' print out the whole matched text. */
Denis Vlasenko80778502006-10-25 12:46:46 +0000592 else if (replace[i] == '&') {
593 j = bbg.regmatch[0].rm_so;
594 while (j < bbg.regmatch[0].rm_eo)
595 pipe_putc(line[j++]);
596 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000597 /* Otherwise just output the character. */
598 else pipe_putc(replace[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000599 }
600}
601
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000602static int do_subst_command(sed_cmd_t *sed_cmd, char **line)
Mark Whitley4f7fe772000-07-13 20:01:58 +0000603{
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000604 char *oldline = *line;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000605 int altered = 0;
Denis Vlasenko80778502006-10-25 12:46:46 +0000606 int match_count = 0;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000607 regex_t *current_regex;
608
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000609 /* Handle empty regex. */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000610 if (sed_cmd->sub_match == NULL) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000611 current_regex = bbg.previous_regex_ptr;
Denis Vlasenko80778502006-10-25 12:46:46 +0000612 if (!current_regex)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000613 bb_error_msg_and_die("no previous regexp");
Denis Vlasenko80778502006-10-25 12:46:46 +0000614 } else
615 bbg.previous_regex_ptr = current_regex = sed_cmd->sub_match;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000616
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000617 /* Find the first match */
Denis Vlasenko80778502006-10-25 12:46:46 +0000618 if (REG_NOMATCH == regexec(current_regex, oldline, 10, bbg.regmatch, 0))
Mark Whitley2dc192f2000-11-03 19:47:00 +0000619 return 0;
620
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000621 /* Initialize temporary output buffer. */
Denis Vlasenko80778502006-10-25 12:46:46 +0000622 bbg.pipeline.buf = xmalloc(PIPE_GROW);
623 bbg.pipeline.len = PIPE_GROW;
624 bbg.pipeline.idx = 0;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000625
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000626 /* Now loop through, substituting for matches */
627 do {
Mark Whitley4f7fe772000-07-13 20:01:58 +0000628 int i;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000629
Eric Andersenc06f5682004-02-04 10:57:46 +0000630 /* Work around bug in glibc regexec, demonstrated by:
631 echo " a.b" | busybox sed 's [^ .]* x g'
632 The match_count check is so not to break
633 echo "hi" | busybox sed 's/^/!/g' */
Denis Vlasenko80778502006-10-25 12:46:46 +0000634 if (!bbg.regmatch[0].rm_so && !bbg.regmatch[0].rm_eo && match_count) {
635 pipe_putc(*oldline++);
Eric Andersenc06f5682004-02-04 10:57:46 +0000636 continue;
637 }
638
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000639 match_count++;
640
641 /* If we aren't interested in this match, output old line to
642 end of match and continue */
Denis Vlasenko80778502006-10-25 12:46:46 +0000643 if (sed_cmd->which_match && sed_cmd->which_match!=match_count) {
644 for (i = 0; i < bbg.regmatch[0].rm_eo; i++)
645 pipe_putc(*oldline++);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000646 continue;
647 }
648
Mark Whitley2dc192f2000-11-03 19:47:00 +0000649 /* print everything before the match */
Denis Vlasenko80778502006-10-25 12:46:46 +0000650 for (i = 0; i < bbg.regmatch[0].rm_so; i++)
651 pipe_putc(oldline[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000652
Mark Whitley2dc192f2000-11-03 19:47:00 +0000653 /* then print the substitution string */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000654 do_subst_w_backrefs(oldline, sed_cmd->string);
Mark Whitley97562bd2000-07-17 20:06:42 +0000655
Mark Whitley2dc192f2000-11-03 19:47:00 +0000656 /* advance past the match */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000657 oldline += bbg.regmatch[0].rm_eo;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000658 /* flag that something has changed */
659 altered++;
Mark Whitley97562bd2000-07-17 20:06:42 +0000660
Mark Whitley2dc192f2000-11-03 19:47:00 +0000661 /* if we're not doing this globally, get out now */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000662 if (sed_cmd->which_match) break;
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000663 } while (*oldline && (regexec(current_regex, oldline, 10, bbg.regmatch, 0) != REG_NOMATCH));
Mark Whitley2dc192f2000-11-03 19:47:00 +0000664
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000665 /* Copy rest of string into output pipeline */
666
Denis Vlasenko80778502006-10-25 12:46:46 +0000667 while (*oldline)
668 pipe_putc(*oldline++);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000669 pipe_putc(0);
Mark Whitley2dc192f2000-11-03 19:47:00 +0000670
Eric Andersenb76cb682001-08-22 05:58:16 +0000671 free(*line);
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000672 *line = bbg.pipeline.buf;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000673 return altered;
674}
Mark Whitley6315ce62000-07-10 22:55:51 +0000675
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000676/* Set command pointer to point to this label. (Does not handle null label.) */
Rob Landley4795e4e2006-07-26 17:25:08 +0000677static sed_cmd_t *branch_to(char *label)
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000678{
679 sed_cmd_t *sed_cmd;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000680
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000681 for (sed_cmd = bbg.sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
Denis Vlasenko80778502006-10-25 12:46:46 +0000682 if (sed_cmd->cmd == ':' && sed_cmd->string && !strcmp(sed_cmd->string, label)) {
683 return sed_cmd;
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000684 }
685 }
Denis Vlasenko80778502006-10-25 12:46:46 +0000686 bb_error_msg_and_die("can't find label for jump to '%s'", label);
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000687}
Mark Whitley6315ce62000-07-10 22:55:51 +0000688
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000689static void append(char *s)
690{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000691 llist_add_to_end(&bbg.append_head, xstrdup(s));
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000692}
693
694static void flush_append(void)
695{
Rob Landley0b656282006-05-08 22:17:23 +0000696 char *data;
697
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000698 /* Output appended lines. */
Denis Vlasenko80778502006-10-25 12:46:46 +0000699 while ((data = (char *)llist_pop(&bbg.append_head))) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000700 fprintf(bbg.nonstdout,"%s\n",data);
Rob Landley0b656282006-05-08 22:17:23 +0000701 free(data);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000702 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000703}
704
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000705static void add_input_file(FILE *file)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000706{
Denis Vlasenko80778502006-10-25 12:46:46 +0000707 bbg.input_file_list = xrealloc(bbg.input_file_list,
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000708 (bbg.input_file_count + 1) * sizeof(FILE *));
709 bbg.input_file_list[bbg.input_file_count++] = file;
Rob Landleydcc28662004-11-25 07:21:47 +0000710}
711
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000712/* Get next line of input from bbg.input_file_list, flushing append buffer and
Rob Landleydcc28662004-11-25 07:21:47 +0000713 * noting if we ran out of files without a newline on the last line we read.
714 */
715static char *get_next_line(int *no_newline)
716{
Denis Vlasenko80778502006-10-25 12:46:46 +0000717 char *temp = NULL;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000718 int len;
719
720 flush_append();
Denis Vlasenko80778502006-10-25 12:46:46 +0000721 while (bbg.current_input_file < bbg.input_file_count) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000722 temp = bb_get_chunk_from_file(bbg.input_file_list[bbg.current_input_file],&len);
Rob Landley2b26fd52006-02-24 02:30:39 +0000723 if (temp) {
Denis Vlasenko80778502006-10-25 12:46:46 +0000724 *no_newline = !(len && temp[len-1] == '\n');
Rob Landley2b26fd52006-02-24 02:30:39 +0000725 if (!*no_newline) temp[len-1] = 0;
Rob Landleydcc28662004-11-25 07:21:47 +0000726 break;
Rob Landley2b26fd52006-02-24 02:30:39 +0000727 // Close this file and advance to next one
Denis Vlasenko80778502006-10-25 12:46:46 +0000728 } else
729 fclose(bbg.input_file_list[bbg.current_input_file++]);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000730 }
731
732 return temp;
733}
734
735/* Output line of text. missing_newline means the last line output did not
736 end with a newline. no_newline means this line does not end with a
737 newline. */
738
739static int puts_maybe_newline(char *s, FILE *file, int missing_newline, int no_newline)
740{
Denis Vlasenko80778502006-10-25 12:46:46 +0000741 if (missing_newline) fputc('\n',file);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000742 fputs(s,file);
Denis Vlasenko80778502006-10-25 12:46:46 +0000743 if (!no_newline) fputc('\n',file);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000744
Denis Vlasenko80778502006-10-25 12:46:46 +0000745 if (ferror(file)) {
Denis Vlasenko40920822006-10-03 20:28:06 +0000746 xfunc_error_retval = 4; /* It's what gnu sed exits with... */
Bernhard Reutner-Fischera3d4bf32006-06-03 21:40:11 +0000747 bb_error_msg_and_die(bb_msg_write_error);
Rob Landley53302f82004-02-18 09:54:15 +0000748 }
749
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000750 return no_newline;
751}
752
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000753#define sed_puts(s,n) missing_newline=puts_maybe_newline(s,bbg.nonstdout,missing_newline,n)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000754
Rob Landley2b26fd52006-02-24 02:30:39 +0000755/* Process all the lines in all the files */
756
Rob Landleydcc28662004-11-25 07:21:47 +0000757static void process_files(void)
Mark Whitley6315ce62000-07-10 22:55:51 +0000758{
Rob Landleyce4f0e92004-10-30 06:54:19 +0000759 char *pattern_space, *next_line;
Denis Vlasenko80778502006-10-25 12:46:46 +0000760 int linenum = 0, missing_newline = 0;
761 int no_newline,next_no_newline = 0;
Mark Whitley6315ce62000-07-10 22:55:51 +0000762
Rob Landley2b26fd52006-02-24 02:30:39 +0000763 /* Prime the pump */
Rob Landleydcc28662004-11-25 07:21:47 +0000764 next_line = get_next_line(&next_no_newline);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000765
Rob Landleydcc28662004-11-25 07:21:47 +0000766 /* go through every line in each file */
Denis Vlasenko80778502006-10-25 12:46:46 +0000767 for (;;) {
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000768 sed_cmd_t *sed_cmd;
Denis Vlasenko80778502006-10-25 12:46:46 +0000769 int substituted = 0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000770
771 /* Advance to next line. Stop if out of lines. */
Denis Vlasenko80778502006-10-25 12:46:46 +0000772 pattern_space = next_line;
773 if (!pattern_space) break;
774 no_newline = next_no_newline;
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000775
Rob Landley2b26fd52006-02-24 02:30:39 +0000776 /* Read one line in advance so we can act on the last line,
777 * the '$' address */
Rob Landleydcc28662004-11-25 07:21:47 +0000778 next_line = get_next_line(&next_no_newline);
Mark Whitley6315ce62000-07-10 22:55:51 +0000779 linenum++;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000780restart:
Mark Whitley6315ce62000-07-10 22:55:51 +0000781 /* for every line, go through all the commands */
Denis Vlasenko80778502006-10-25 12:46:46 +0000782 for (sed_cmd = bbg.sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
Eric Andersen52a3c272003-12-23 08:53:51 +0000783 int old_matched, matched;
784
785 old_matched = sed_cmd->in_match;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000786
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000787 /* Determine if this command matches this line: */
788
789 /* Are we continuing a previous multi-line match? */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000790 sed_cmd->in_match = sed_cmd->in_match
Denis Vlasenko80778502006-10-25 12:46:46 +0000791 /* Or is no range necessary? */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000792 || (!sed_cmd->beg_line && !sed_cmd->end_line
793 && !sed_cmd->beg_match && !sed_cmd->end_match)
Denis Vlasenko80778502006-10-25 12:46:46 +0000794 /* Or did we match the start of a numerical range? */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000795 || (sed_cmd->beg_line > 0 && (sed_cmd->beg_line == linenum))
Denis Vlasenko80778502006-10-25 12:46:46 +0000796 /* Or does this line match our begin address regex? */
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000797 || (sed_cmd->beg_match &&
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000798 !regexec(sed_cmd->beg_match, pattern_space, 0, NULL, 0))
Denis Vlasenko80778502006-10-25 12:46:46 +0000799 /* Or did we match last line of input? */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000800 || (sed_cmd->beg_line == -1 && next_line == NULL);
801
802 /* Snapshot the value */
803
804 matched = sed_cmd->in_match;
805
806 /* Is this line the end of the current match? */
807
Denis Vlasenko80778502006-10-25 12:46:46 +0000808 if (matched) {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000809 sed_cmd->in_match = !(
810 /* has the ending line come, or is this a single address command? */
811 (sed_cmd->end_line ?
812 sed_cmd->end_line==-1 ?
813 !next_line
814 : sed_cmd->end_line<=linenum
815 : !sed_cmd->end_match)
816 /* or does this line matches our last address regex */
Eric Andersen52a3c272003-12-23 08:53:51 +0000817 || (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 +0000818 );
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000819 }
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000820
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000821 /* Skip blocks of commands we didn't match. */
822 if (sed_cmd->cmd == '{') {
Denis Vlasenko80778502006-10-25 12:46:46 +0000823 if (sed_cmd->invert ? matched : !matched)
824 while (sed_cmd && sed_cmd->cmd != '}')
825 sed_cmd = sed_cmd->next;
826 if (!sed_cmd) bb_error_msg_and_die("unterminated {");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000827 continue;
828 }
829
830 /* Okay, so did this line match? */
831 if (sed_cmd->invert ? !matched : matched) {
832 /* Update last used regex in case a blank substitute BRE is found */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000833 if (sed_cmd->beg_match) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000834 bbg.previous_regex_ptr = sed_cmd->beg_match;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000835 }
Mark Whitley0915c4b2001-06-11 23:50:06 +0000836
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000837 /* actual sedding */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000838 switch (sed_cmd->cmd) {
Mark Whitley0915c4b2001-06-11 23:50:06 +0000839
Denis Vlasenko80778502006-10-25 12:46:46 +0000840 /* Print line number */
841 case '=':
842 fprintf(bbg.nonstdout, "%d\n", linenum);
843 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000844
Denis Vlasenko80778502006-10-25 12:46:46 +0000845 /* Write the current pattern space up to the first newline */
846 case 'P':
847 {
848 char *tmp = strchr(pattern_space, '\n');
Glenn L McGrath0c518322003-03-30 03:41:53 +0000849
Denis Vlasenko80778502006-10-25 12:46:46 +0000850 if (tmp) {
851 *tmp = '\0';
852 sed_puts(pattern_space,1);
853 *tmp = '\n';
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000854 break;
Glenn L McGrathd87a7ac2003-04-09 15:26:14 +0000855 }
Denis Vlasenko80778502006-10-25 12:46:46 +0000856 /* Fall Through */
857 }
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000858
Denis Vlasenko80778502006-10-25 12:46:46 +0000859 /* Write the current pattern space to output */
860 case 'p':
861 sed_puts(pattern_space,no_newline);
862 break;
863 /* Delete up through first newline */
864 case 'D':
865 {
866 char *tmp = strchr(pattern_space,'\n');
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000867
Denis Vlasenko80778502006-10-25 12:46:46 +0000868 if (tmp) {
869 tmp = xstrdup(tmp+1);
870 free(pattern_space);
871 pattern_space = tmp;
872 goto restart;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000873 }
Denis Vlasenko80778502006-10-25 12:46:46 +0000874 }
875 /* discard this line. */
876 case 'd':
877 goto discard_line;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000878
Denis Vlasenko80778502006-10-25 12:46:46 +0000879 /* Substitute with regex */
880 case 's':
881 if (do_subst_command(sed_cmd, &pattern_space)) {
882 substituted |= 1;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000883
Denis Vlasenko80778502006-10-25 12:46:46 +0000884 /* handle p option */
885 if (sed_cmd->sub_p)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000886 sed_puts(pattern_space,no_newline);
Denis Vlasenko80778502006-10-25 12:46:46 +0000887 /* handle w option */
888 if (sed_cmd->file)
889 sed_cmd->no_newline = puts_maybe_newline(pattern_space, sed_cmd->file, sed_cmd->no_newline, no_newline);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000890
Denis Vlasenko80778502006-10-25 12:46:46 +0000891 }
892 break;
893
894 /* Append line to linked list to be printed later */
895 case 'a':
896 {
897 append(sed_cmd->string);
898 break;
899 }
900
901 /* Insert text before this line */
902 case 'i':
903 sed_puts(sed_cmd->string,1);
904 break;
905
906 /* Cut and paste text (replace) */
907 case 'c':
908 /* Only triggers on last line of a matching range. */
909 if (!sed_cmd->in_match)
910 sed_puts(sed_cmd->string,0);
911 goto discard_line;
912
913 /* Read file, append contents to output */
914 case 'r':
915 {
916 FILE *rfile;
917
918 rfile = fopen(sed_cmd->string, "r");
919 if (rfile) {
920 char *line;
921
922 while ((line = xmalloc_getline(rfile))
923 != NULL)
924 append(line);
925 xprint_and_close_file(rfile);
926 }
927
928 break;
929 }
930
931 /* Write pattern space to file. */
932 case 'w':
933 sed_cmd->no_newline = puts_maybe_newline(pattern_space,sed_cmd->file, sed_cmd->no_newline,no_newline);
934 break;
935
936 /* Read next line from input */
937 case 'n':
938 if (!bbg.be_quiet)
939 sed_puts(pattern_space,no_newline);
940 if (next_line) {
941 free(pattern_space);
942 pattern_space = next_line;
943 no_newline = next_no_newline;
944 next_line = get_next_line(&next_no_newline);
945 linenum++;
946 break;
947 }
948 /* fall through */
949
950 /* Quit. End of script, end of input. */
951 case 'q':
952 /* Exit the outer while loop */
953 free(next_line);
954 next_line = NULL;
955 goto discard_commands;
956
957 /* Append the next line to the current line */
958 case 'N':
959 {
960 /* If no next line, jump to end of script and exit. */
961 if (next_line == NULL) {
962 /* Jump to end of script and exit */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000963 free(next_line);
964 next_line = NULL;
Denis Vlasenko80778502006-10-25 12:46:46 +0000965 goto discard_line;
966 /* append next_line, read new next_line. */
967 } else {
968 int len = strlen(pattern_space);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000969
Denis Vlasenko80778502006-10-25 12:46:46 +0000970 pattern_space = realloc(pattern_space, len + strlen(next_line) + 2);
971 pattern_space[len] = '\n';
972 strcpy(pattern_space + len+1, next_line);
973 no_newline = next_no_newline;
974 next_line = get_next_line(&next_no_newline);
975 linenum++;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000976 }
Denis Vlasenko80778502006-10-25 12:46:46 +0000977 break;
978 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000979
Denis Vlasenko80778502006-10-25 12:46:46 +0000980 /* Test/branch if substitution occurred */
981 case 't':
982 if (!substituted) break;
983 substituted = 0;
984 /* Fall through */
985 /* Test/branch if substitution didn't occur */
986 case 'T':
987 if (substituted) break;
988 /* Fall through */
989 /* Branch to label */
990 case 'b':
991 if (!sed_cmd->string) goto discard_commands;
992 else sed_cmd = branch_to(sed_cmd->string);
993 break;
994 /* Transliterate characters */
995 case 'y':
996 {
997 int i;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000998
Denis Vlasenko80778502006-10-25 12:46:46 +0000999 for (i = 0; pattern_space[i]; i++) {
1000 int j;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001001
Denis Vlasenko80778502006-10-25 12:46:46 +00001002 for (j = 0; sed_cmd->string[j]; j += 2) {
1003 if (pattern_space[i] == sed_cmd->string[j]) {
1004 pattern_space[i] = sed_cmd->string[j + 1];
1005 break;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +00001006 }
1007 }
Glenn L McGrath91e19782003-04-26 07:40:07 +00001008 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001009
Denis Vlasenko80778502006-10-25 12:46:46 +00001010 break;
1011 }
1012 case 'g': /* Replace pattern space with hold space */
1013 free(pattern_space);
1014 pattern_space = xstrdup(bbg.hold_space ? bbg.hold_space : "");
1015 break;
1016 case 'G': /* Append newline and hold space to pattern space */
1017 {
1018 int pattern_space_size = 2;
1019 int hold_space_size = 0;
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001020
Denis Vlasenko80778502006-10-25 12:46:46 +00001021 if (pattern_space)
1022 pattern_space_size += strlen(pattern_space);
1023 if (bbg.hold_space)
1024 hold_space_size = strlen(bbg.hold_space);
1025 pattern_space = xrealloc(pattern_space,
1026 pattern_space_size + hold_space_size);
1027 if (pattern_space_size == 2)
1028 pattern_space[0] = 0;
1029 strcat(pattern_space, "\n");
1030 if (bbg.hold_space)
1031 strcat(pattern_space, bbg.hold_space);
1032 no_newline = 0;
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001033
Denis Vlasenko80778502006-10-25 12:46:46 +00001034 break;
1035 }
1036 case 'h': /* Replace hold space with pattern space */
1037 free(bbg.hold_space);
1038 bbg.hold_space = xstrdup(pattern_space);
1039 break;
1040 case 'H': /* Append newline and pattern space to hold space */
1041 {
1042 int hold_space_size = 2;
1043 int pattern_space_size = 0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001044
Denis Vlasenko80778502006-10-25 12:46:46 +00001045 if (bbg.hold_space)
1046 hold_space_size += strlen(bbg.hold_space);
1047 if (pattern_space)
1048 pattern_space_size = strlen(pattern_space);
1049 bbg.hold_space = xrealloc(bbg.hold_space,
1050 hold_space_size + pattern_space_size);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001051
Denis Vlasenko80778502006-10-25 12:46:46 +00001052 if (hold_space_size == 2)
1053 *bbg.hold_space = 0;
1054 strcat(bbg.hold_space, "\n");
1055 if (pattern_space)
1056 strcat(bbg.hold_space, pattern_space);
1057
1058 break;
1059 }
1060 case 'x': /* Exchange hold and pattern space */
1061 {
1062 char *tmp = pattern_space;
1063 pattern_space = bbg.hold_space ? : xzalloc(1);
1064 no_newline = 0;
1065 bbg.hold_space = tmp;
1066 break;
1067 }
Mark Whitley0915c4b2001-06-11 23:50:06 +00001068 }
Robert Griebl47abc492002-06-11 23:43:27 +00001069 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001070 }
Erik Andersen1266a131999-12-29 22:19:46 +00001071
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001072 /*
1073 * exit point from sedding...
1074 */
1075discard_commands:
1076 /* we will print the line unless we were told to be quiet ('-n')
1077 or if the line was suppressed (ala 'd'elete) */
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001078 if (!bbg.be_quiet) sed_puts(pattern_space,no_newline);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001079
1080 /* Delete and such jump here. */
1081discard_line:
1082 flush_append();
Glenn L McGrathc6adada2003-04-07 12:24:44 +00001083 free(pattern_space);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001084 }
Erik Andersen1266a131999-12-29 22:19:46 +00001085}
1086
Glenn L McGrath42c25732003-10-04 05:27:56 +00001087/* It is possible to have a command line argument with embedded
1088 newlines. This counts as multiple command lines. */
1089
1090static void add_cmd_block(char *cmdstr)
1091{
Denis Vlasenko80778502006-10-25 12:46:46 +00001092 int go = 1;
1093 char *temp = xstrdup(cmdstr), *temp2 = temp;
Glenn L McGrath42c25732003-10-04 05:27:56 +00001094
Denis Vlasenko80778502006-10-25 12:46:46 +00001095 while (go) {
1096 int len = strcspn(temp2,"\n");
1097 if (!temp2[len]) go = 0;
1098 else temp2[len] = 0;
Glenn L McGrath42c25732003-10-04 05:27:56 +00001099 add_cmd(temp2);
Denis Vlasenko80778502006-10-25 12:46:46 +00001100 temp2 += len+1;
Glenn L McGrath42c25732003-10-04 05:27:56 +00001101 }
1102 free(temp);
1103}
1104
Denis Vlasenkoe2016e12006-10-01 21:37:40 +00001105static void add_cmds_link(llist_t *opt_e)
1106{
1107 if (!opt_e) return;
1108 add_cmds_link(opt_e->link);
1109 add_cmd_block(opt_e->data);
1110 free(opt_e);
1111}
1112
1113static void add_files_link(llist_t *opt_f)
1114{
1115 char *line;
1116 FILE *cmdfile;
1117 if (!opt_f) return;
1118 add_files_link(opt_f->link);
1119 cmdfile = xfopen(opt_f->data, "r");
Denis Vlasenko2d5ca602006-10-12 22:43:20 +00001120 while ((line = xmalloc_getline(cmdfile)) != NULL) {
Denis Vlasenkoe2016e12006-10-01 21:37:40 +00001121 add_cmd(line);
1122 free(line);
1123 }
1124 xprint_and_close_file(cmdfile);
1125 free(opt_f);
1126}
1127
Rob Landleydfba7412006-03-06 20:47:33 +00001128int sed_main(int argc, char **argv)
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001129{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001130 unsigned opt;
Denis Vlasenkob97c9842006-10-01 21:05:12 +00001131 llist_t *opt_e, *opt_f;
1132 int status = EXIT_SUCCESS;
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001133
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001134 bbg.sed_cmd_tail=&bbg.sed_cmd_head;
1135
Mark Whitley858c1ad2000-07-11 21:38:47 +00001136 /* destroy command strings on exit */
Rob Landley0b656282006-05-08 22:17:23 +00001137 if (ENABLE_FEATURE_CLEAN_UP) atexit(sed_free_and_close_stuff);
Mark Whitley858c1ad2000-07-11 21:38:47 +00001138
Rob Landleyfae1dc82005-11-20 07:44:35 +00001139 /* Lie to autoconf when it starts asking stupid questions. */
Denis Vlasenko80778502006-10-25 12:46:46 +00001140 if (argc==2 && !strcmp(argv[1],"--version")) {
Eric Andersenc06f5682004-02-04 10:57:46 +00001141 printf("This is not GNU sed version 4.0\n");
1142 exit(0);
1143 }
Eric Andersenc06f5682004-02-04 10:57:46 +00001144
Mark Whitley6315ce62000-07-10 22:55:51 +00001145 /* do normal option parsing */
Denis Vlasenkob97c9842006-10-01 21:05:12 +00001146 opt_e = opt_f = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001147 opt_complementary = "e::f::"; /* can occur multiple times */
1148 opt = getopt32(argc, argv, "irne:f:", &opt_e, &opt_f);
Denis Vlasenko750fc6d2006-09-22 08:56:03 +00001149 if (opt & 0x1) { // -i
1150 bbg.in_place++;
1151 atexit(cleanup_outname);
1152 }
1153 if (opt & 0x2) bbg.regex_type|=REG_EXTENDED; // -r
1154 if (opt & 0x4) bbg.be_quiet++; // -n
1155 if (opt & 0x8) { // -e
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001156 /* getopt32 reverses order of arguments, handle it */
Denis Vlasenkoe2016e12006-10-01 21:37:40 +00001157 add_cmds_link(opt_e);
Denis Vlasenko750fc6d2006-09-22 08:56:03 +00001158 }
1159 if (opt & 0x10) { // -f
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001160 /* getopt32 reverses order of arguments, handle it */
Denis Vlasenkoe2016e12006-10-01 21:37:40 +00001161 add_files_link(opt_f);
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001162 }
Rob Landleydcc28662004-11-25 07:21:47 +00001163 /* if we didn't get a pattern from -e or -f, use argv[optind] */
Denis Vlasenko80778502006-10-25 12:46:46 +00001164 if (!(opt & 0x18)) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001165 if (argv[optind] == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001166 bb_show_usage();
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001167 else
Glenn L McGrath42c25732003-10-04 05:27:56 +00001168 add_cmd_block(argv[optind++]);
Mark Whitley6315ce62000-07-10 22:55:51 +00001169 }
Glenn L McGrath42c25732003-10-04 05:27:56 +00001170 /* Flush any unfinished commands. */
1171 add_cmd("");
Mark Whitley6315ce62000-07-10 22:55:51 +00001172
Rob Landley53302f82004-02-18 09:54:15 +00001173 /* By default, we write to stdout */
Denis Vlasenkob97c9842006-10-01 21:05:12 +00001174 bbg.nonstdout = stdout;
Rob Landley53302f82004-02-18 09:54:15 +00001175
Mark Whitley6315ce62000-07-10 22:55:51 +00001176 /* argv[(optind)..(argc-1)] should be names of file to process. If no
1177 * files were specified or '-' was specified, take input from stdin.
1178 * Otherwise, we process all the files specified. */
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001179 if (argv[optind] == NULL) {
Denis Vlasenko80778502006-10-25 12:46:46 +00001180 if (bbg.in_place)
1181 bb_error_msg_and_die(bb_msg_requires_arg, "-i");
Rob Landleydcc28662004-11-25 07:21:47 +00001182 add_input_file(stdin);
1183 process_files();
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001184 } else {
Mark Whitley6315ce62000-07-10 22:55:51 +00001185 int i;
1186 FILE *file;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001187
Mark Whitley6315ce62000-07-10 22:55:51 +00001188 for (i = optind; i < argc; i++) {
Denis Vlasenkod18a3a22006-10-25 12:46:03 +00001189 struct stat statbuf;
1190 int nonstdoutfd;
1191
Denis Vlasenko80778502006-10-25 12:46:46 +00001192 if (!strcmp(argv[i], "-") && !bbg.in_place) {
Rob Landleydcc28662004-11-25 07:21:47 +00001193 add_input_file(stdin);
1194 process_files();
Denis Vlasenkod18a3a22006-10-25 12:46:03 +00001195 continue;
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001196 }
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00001197 file = fopen_or_warn(argv[i], "r");
Denis Vlasenkod18a3a22006-10-25 12:46:03 +00001198 if (!file) {
1199 status = EXIT_FAILURE;
1200 continue;
1201 }
Denis Vlasenko80778502006-10-25 12:46:46 +00001202 if (!bbg.in_place) {
Denis Vlasenkod18a3a22006-10-25 12:46:03 +00001203 add_input_file(file);
1204 continue;
1205 }
1206
Denis Vlasenko80778502006-10-25 12:46:46 +00001207 bbg.outname = xasprintf("%sXXXXXX", argv[i]);
1208 nonstdoutfd = mkstemp(bbg.outname);
1209 if (-1 == nonstdoutfd)
Denis Vlasenkod18a3a22006-10-25 12:46:03 +00001210 bb_error_msg_and_die("no temp file");
Denis Vlasenko80778502006-10-25 12:46:46 +00001211 bbg.nonstdout = fdopen(nonstdoutfd,"w");
Denis Vlasenkod18a3a22006-10-25 12:46:03 +00001212
1213 /* Set permissions of output file */
1214
1215 fstat(fileno(file),&statbuf);
1216 fchmod(nonstdoutfd,statbuf.st_mode);
1217 add_input_file(file);
1218 process_files();
1219 fclose(bbg.nonstdout);
1220
Denis Vlasenko80778502006-10-25 12:46:46 +00001221 bbg.nonstdout = stdout;
1222 /* unlink(argv[i]); */
1223 // FIXME: error check / message?
Denis Vlasenkod18a3a22006-10-25 12:46:03 +00001224 rename(bbg.outname,argv[i]);
1225 free(bbg.outname);
Denis Vlasenko80778502006-10-25 12:46:46 +00001226 bbg.outname = 0;
Mark Whitley6315ce62000-07-10 22:55:51 +00001227 }
Denis Vlasenko80778502006-10-25 12:46:46 +00001228 if (bbg.input_file_count > bbg.current_input_file)
1229 process_files();
Mark Whitley6315ce62000-07-10 22:55:51 +00001230 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001231
Matt Kraaia5f09c62001-11-12 16:44:55 +00001232 return status;
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001233}