blob: db364e3eeb5754567d1bf87422ae7a9797032480 [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 *
Eric Andersen6b6b3f61999-10-28 16:06:25 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000027/* Code overview.
28
29 Files are laid out to avoid unnecessary function declarations. So for
30 example, every function add_cmd calls occurs before add_cmd in this file.
31
32 add_cmd() is called on each line of sed command text (from a file or from
33 the command line). It calls get_address() and parse_cmd_args(). The
34 resulting sed_cmd_t structures are appended to a linked list
35 (sed_cmd_head/sed_cmd_tail).
36
Rob Landleydcc28662004-11-25 07:21:47 +000037 add_input_file() adds a FILE * to the list of input files. We need to
38 know them all ahead of time to find the last line for the $ match.
39
40 process_files() does actual sedding, reading data lines from each input FILE *
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000041 (which could be stdin) and applying the sed command list (sed_cmd_head) to
42 each of the resulting lines.
43
44 sed_main() is where external code calls into this, with a command line.
45*/
46
47
Mark Whitley6315ce62000-07-10 22:55:51 +000048/*
49 Supported features and commands in this version of sed:
50
51 - comments ('#')
Mark Whitley94074a92000-07-14 00:00:15 +000052 - address matching: num|/matchstr/[,num|/matchstr/|$]command
53 - commands: (p)rint, (d)elete, (s)ubstitue (with g & I flags)
54 - edit commands: (a)ppend, (i)nsert, (c)hange
Mark Whitley1f3b9f22001-05-11 22:27:13 +000055 - file commands: (r)ead
Mark Whitley97562bd2000-07-17 20:06:42 +000056 - backreferences in substitution expressions (\1, \2...\9)
Glenn L McGrathf50ce312003-03-09 15:12:24 +000057 - grouped commands: {cmd1;cmd2}
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000058 - transliteration (y/source-chars/dest-chars/)
59 - pattern space hold space storing / swapping (g, h, x)
60 - labels / branching (: label, b, t)
Glenn L McGrathf50ce312003-03-09 15:12:24 +000061
Mark Whitley6315ce62000-07-10 22:55:51 +000062 (Note: Specifying an address (range) to match is *optional*; commands
63 default to the whole pattern space if no specific address match was
64 requested.)
65
66 Unsupported features:
67
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000068 - GNU extensions
Glenn L McGrathf36635c2003-09-13 15:12:22 +000069 - and more.
Glenn L McGrathd5eadea2003-03-09 02:39:29 +000070
Glenn L McGrath2570b432003-09-16 05:25:43 +000071 Todo:
72
73 - Create a wrapper around regex to make libc's regex conform with sed
74 - Fix bugs
75
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000076
Glenn L McGrathd5eadea2003-03-09 02:39:29 +000077 Reference http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html
Mark Whitley6315ce62000-07-10 22:55:51 +000078*/
79
Eric Andersen6b6b3f61999-10-28 16:06:25 +000080#include <stdio.h>
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000081#include <unistd.h> /* for getopt() */
Mark Whitley6315ce62000-07-10 22:55:51 +000082#include <regex.h>
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000083#include <string.h> /* for strdup() */
Eric Andersen6b6b3f61999-10-28 16:06:25 +000084#include <errno.h>
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000085#include <ctype.h> /* for isspace() */
Eric Andersened3ef502001-01-27 08:24:39 +000086#include <stdlib.h>
Eric Andersen3570a342000-09-25 21:45:58 +000087#include "busybox.h"
Mark Whitley6315ce62000-07-10 22:55:51 +000088
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +000089typedef struct sed_cmd_s {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000090 /* Ordered by alignment requirements: currently 36 bytes on x86 */
Mark Whitley2dc192f2000-11-03 19:47:00 +000091
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000092 /* address storage */
93 regex_t *beg_match; /* sed -e '/match/cmd' */
94 regex_t *end_match; /* sed -e '/match/,/end_match/cmd' */
95 regex_t *sub_match; /* For 's/sub_match/string/' */
96 int beg_line; /* 'sed 1p' 0 == apply commands to all lines */
97 int end_line; /* 'sed 1,3p' 0 == one line only. -1 = last line ($) */
Mark Whitley6315ce62000-07-10 22:55:51 +000098
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000099 FILE *file; /* File (sr) command writes to, -1 for none. */
100 char *string; /* Data string for (saicytb) commands. */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000101
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000102 unsigned short which_match; /* (s) Which match to replace (0 for all) */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000103
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000104 /* Bitfields (gcc won't group them if we don't) */
105 unsigned int invert:1; /* the '!' after the address */
106 unsigned int in_match:1; /* Next line also included in match? */
107 unsigned int no_newline:1; /* Last line written by (sr) had no '\n' */
108 unsigned int sub_p:1; /* (s) print option */
Glenn L McGrathc18ce372003-09-13 06:57:39 +0000109
Mark Whitley2dc192f2000-11-03 19:47:00 +0000110
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000111 /* GENERAL FIELDS */
112 char cmd; /* The command char: abcdDgGhHilnNpPqrstwxy:={} */
113 struct sed_cmd_s *next; /* Next command (linked list, NULL terminated) */
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +0000114} sed_cmd_t;
Mark Whitley6315ce62000-07-10 22:55:51 +0000115
116/* globals */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000117/* options */
Rob Landleyce4f0e92004-10-30 06:54:19 +0000118static int be_quiet, in_place, regex_type;
Rob Landley53302f82004-02-18 09:54:15 +0000119FILE *nonstdout;
Rob Landleyce4f0e92004-10-30 06:54:19 +0000120char *outname,*hold_space;
Rob Landley53302f82004-02-18 09:54:15 +0000121
Rob Landleydcc28662004-11-25 07:21:47 +0000122/* List of input files */
123int input_file_count,current_input_file;
124FILE **input_file_list;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000125
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000126static const char bad_format_in_subst[] =
127 "bad format in substitution expression";
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000128const char *const semicolon_whitespace = "; \n\r\t\v";
129
130regmatch_t regmatch[10];
Rob Landleyce4f0e92004-10-30 06:54:19 +0000131static regex_t *previous_regex_ptr;
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000132
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000133/* linked list of sed commands */
134static sed_cmd_t sed_cmd_head;
135static sed_cmd_t *sed_cmd_tail = &sed_cmd_head;
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000136
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000137/* Linked list of append lines */
138struct append_list {
139 char *string;
140 struct append_list *next;
141};
142struct append_list *append_head=NULL, *append_tail=NULL;
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000143
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000144#ifdef CONFIG_FEATURE_CLEAN_UP
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000145static void free_and_close_stuff(void)
Mark Whitley6315ce62000-07-10 22:55:51 +0000146{
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000147 sed_cmd_t *sed_cmd = sed_cmd_head.next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000148
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000149 while(append_head) {
150 append_tail=append_head->next;
151 free(append_head->string);
152 free(append_head);
153 append_head=append_tail;
154 }
155
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000156 while (sed_cmd) {
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000157 sed_cmd_t *sed_cmd_next = sed_cmd->next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000158
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000159 if(sed_cmd->file)
160 bb_xprint_and_close_file(sed_cmd->file);
161
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000162 if (sed_cmd->beg_match) {
163 regfree(sed_cmd->beg_match);
164 free(sed_cmd->beg_match);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000165 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000166 if (sed_cmd->end_match) {
167 regfree(sed_cmd->end_match);
168 free(sed_cmd->end_match);
Mark Whitley6315ce62000-07-10 22:55:51 +0000169 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000170 if (sed_cmd->sub_match) {
171 regfree(sed_cmd->sub_match);
172 free(sed_cmd->sub_match);
Mark Whitley6315ce62000-07-10 22:55:51 +0000173 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000174 free(sed_cmd->string);
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000175 free(sed_cmd);
176 sed_cmd = sed_cmd_next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000177 }
Rob Landleyce4f0e92004-10-30 06:54:19 +0000178
179 if(hold_space) free(hold_space);
Rob Landleydcc28662004-11-25 07:21:47 +0000180
181 while(current_input_file<input_file_count)
182 fclose(input_file_list[current_input_file++]);
Mark Whitley6315ce62000-07-10 22:55:51 +0000183}
Mark Whitleyc41e8c82000-07-12 23:35:21 +0000184#endif
Mark Whitley6315ce62000-07-10 22:55:51 +0000185
Rob Landley53302f82004-02-18 09:54:15 +0000186/* If something bad happens during -i operation, delete temp file */
187
188static void cleanup_outname(void)
189{
190 if(outname) unlink(outname);
191}
192
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000193/* strdup, replacing "\n" with '\n', and "\delimiter" with 'delimiter' */
194
195static void parse_escapes(char *dest, const char *string, int len, char from, char to)
196{
197 int i=0;
198
199 while(i<len) {
200 if(string[i] == '\\') {
Glenn L McGrath738fb332003-10-01 06:45:11 +0000201 if(!to || string[i+1] == from) {
202 *(dest++) = to ? to : string[i+1];
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000203 i+=2;
204 continue;
205 } else *(dest++)=string[i++];
206 }
207 *(dest++) = string[i++];
208 }
209 *dest=0;
210}
211
212static char *copy_parsing_slashn(const char *string, int len)
213{
214 char *dest=xmalloc(len+1);
215
216 parse_escapes(dest,string,len,'n','\n');
217 return dest;
218}
219
220
Mark Whitley6315ce62000-07-10 22:55:51 +0000221/*
Eric Andersen28b3c532001-01-02 11:01:31 +0000222 * index_of_next_unescaped_regexp_delim - walks left to right through a string
223 * beginning at a specified index and returns the index of the next regular
Eric Andersenaff114c2004-04-14 17:51:38 +0000224 * expression delimiter (typically a forward * slash ('/')) not preceded by
Eric Andersen28b3c532001-01-02 11:01:31 +0000225 * a backslash ('\').
Mark Whitley6315ce62000-07-10 22:55:51 +0000226 */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000227static int index_of_next_unescaped_regexp_delim(const char delimiter,
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000228 const char *str)
Mark Whitley6315ce62000-07-10 22:55:51 +0000229{
Matt Kraaia0065d52001-08-24 14:45:50 +0000230 int bracket = -1;
231 int escaped = 0;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000232 int idx = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000233 char ch;
Matt Kraaia0065d52001-08-24 14:45:50 +0000234
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000235 for (; (ch = str[idx]); idx++) {
Matt Kraaia0065d52001-08-24 14:45:50 +0000236 if (bracket != -1) {
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000237 if (ch == ']' && !(bracket == idx - 1 || (bracket == idx - 2
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000238 && str[idx - 1] == '^')))
Matt Kraaia0065d52001-08-24 14:45:50 +0000239 bracket = -1;
240 } else if (escaped)
241 escaped = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000242 else if (ch == '\\')
Matt Kraaia0065d52001-08-24 14:45:50 +0000243 escaped = 1;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000244 else if (ch == '[')
Matt Kraaia0065d52001-08-24 14:45:50 +0000245 bracket = idx;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000246 else if (ch == delimiter)
Mark Whitley97562bd2000-07-17 20:06:42 +0000247 return idx;
248 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000249
Mark Whitley97562bd2000-07-17 20:06:42 +0000250 /* if we make it to here, we've hit the end of the string */
251 return -1;
Mark Whitley6315ce62000-07-10 22:55:51 +0000252}
253
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000254/*
255 * Returns the index of the third delimiter
256 */
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000257static int parse_regex_delim(const char *cmdstr, char **match, char **replace)
258{
259 const char *cmdstr_ptr = cmdstr;
260 char delimiter;
261 int idx = 0;
262
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000263 /* verify that the 's' or 'y' is followed by something. That something
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000264 * (typically a 'slash') is now our regexp delimiter... */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000265 if (*cmdstr == '\0') bb_error_msg_and_die(bad_format_in_subst);
266 delimiter = *(cmdstr_ptr++);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000267
268 /* save the match string */
269 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
270 if (idx == -1) {
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000271 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000272 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000273 *match = copy_parsing_slashn(cmdstr_ptr, idx);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000274
275 /* save the replacement string */
276 cmdstr_ptr += idx + 1;
277 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
278 if (idx == -1) {
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000279 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000280 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000281 *replace = copy_parsing_slashn(cmdstr_ptr, idx);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000282
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000283 return ((cmdstr_ptr - cmdstr) + idx);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000284}
285
Mark Whitley6315ce62000-07-10 22:55:51 +0000286/*
287 * returns the index in the string just past where the address ends.
288 */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000289static int get_address(char *my_str, int *linenum, regex_t ** regex)
Mark Whitley6315ce62000-07-10 22:55:51 +0000290{
Glenn L McGrathe3e28d32003-09-15 09:22:04 +0000291 char *pos = my_str;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000292
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000293 if (isdigit(*my_str)) {
294 *linenum = strtol(my_str, &pos, 10);
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000295 /* endstr shouldnt ever equal NULL */
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000296 } else if (*my_str == '$') {
Mark Whitley0915c4b2001-06-11 23:50:06 +0000297 *linenum = -1;
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000298 pos++;
299 } else if (*my_str == '/' || *my_str == '\\') {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000300 int next;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000301 char delimiter;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000302 char *temp;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000303
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000304 if (*my_str == '\\') delimiter = *(++pos);
305 else delimiter = '/';
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000306 next = index_of_next_unescaped_regexp_delim(delimiter, ++pos);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000307 if (next == -1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000308 bb_error_msg_and_die("unterminated match expression");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000309
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000310 temp=copy_parsing_slashn(pos,next);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000311 *regex = (regex_t *) xmalloc(sizeof(regex_t));
Eric Andersen98555482004-05-26 10:03:33 +0000312 xregcomp(*regex, temp, regex_type|REG_NEWLINE);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000313 free(temp);
314 /* Move position to next character after last delimiter */
315 pos+=(next+1);
Mark Whitley6315ce62000-07-10 22:55:51 +0000316 }
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000317 return pos - my_str;
Mark Whitley6315ce62000-07-10 22:55:51 +0000318}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000319
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000320/* Grab a filename. Whitespace at start is skipped, then goes to EOL. */
321static int parse_file_cmd(sed_cmd_t * sed_cmd, const char *filecmdstr, char **retval)
322{
323 int start = 0, idx, hack=0;
324
325 /* Skip whitespace, then grab filename to end of line */
326 while (isspace(filecmdstr[start])) start++;
327 idx=start;
328 while(filecmdstr[idx] && filecmdstr[idx]!='\n') idx++;
329 /* If lines glued together, put backslash back. */
330 if(filecmdstr[idx]=='\n') hack=1;
331 if(idx==start) bb_error_msg_and_die("Empty filename");
332 *retval = bb_xstrndup(filecmdstr+start, idx-start+hack+1);
333 if(hack) *(idx+*retval)='\\';
334
335 return idx;
336}
337
Eric Andersen638da752003-10-09 08:18:36 +0000338static int parse_subst_cmd(sed_cmd_t * const sed_cmd, char *substr)
Mark Whitley06f35292000-07-13 19:58:04 +0000339{
Eric Andersen98555482004-05-26 10:03:33 +0000340 int cflags = regex_type;
Mark Whitley06f35292000-07-13 19:58:04 +0000341 char *match;
342 int idx = 0;
343
344 /*
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000345 * A substitution command should look something like this:
346 * s/match/replace/ #gIpw
Mark Whitley0e4cec02000-08-21 21:29:20 +0000347 * || | |||
Mark Whitley06f35292000-07-13 19:58:04 +0000348 * mandatory optional
Mark Whitley06f35292000-07-13 19:58:04 +0000349 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000350 idx = parse_regex_delim(substr, &match, &sed_cmd->string);
Mark Whitley06f35292000-07-13 19:58:04 +0000351
Mark Whitley97562bd2000-07-17 20:06:42 +0000352 /* determine the number of back references in the match string */
353 /* Note: we compute this here rather than in the do_subst_command()
354 * function to save processor time, at the expense of a little more memory
355 * (4 bits) per sed_cmd */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000356
Mark Whitley06f35292000-07-13 19:58:04 +0000357 /* process the flags */
Mark Whitley70705d72000-07-14 19:06:30 +0000358
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000359 sed_cmd->which_match=1;
360 while (substr[++idx]) {
361 /* Parse match number */
362 if(isdigit(substr[idx])) {
363 if(match[0]!='^') {
364 /* Match 0 treated as all, multiple matches we take the last one. */
365 char *pos=substr+idx;
366 sed_cmd->which_match=(unsigned short)strtol(substr+idx,&pos,10);
367 idx=pos-substr;
368 }
369 continue;
370 }
Rob Landley40ec4ae2004-01-04 06:42:14 +0000371 /* Skip spaces */
372 if(isspace(substr[idx])) continue;
373
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000374 switch (substr[idx]) {
375 /* Replace all occurrences */
376 case 'g':
377 if (match[0] != '^') sed_cmd->which_match = 0;
378 break;
379 /* Print pattern space */
380 case 'p':
381 sed_cmd->sub_p = 1;
382 break;
383 case 'w':
384 {
385 char *temp;
386 idx+=parse_file_cmd(sed_cmd,substr+idx,&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000387
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000388 break;
389 }
390 /* Ignore case (gnu exension) */
391 case 'I':
392 cflags |= REG_ICASE;
393 break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000394 case ';':
395 case '}':
396 goto out;
397 default:
398 bb_error_msg_and_die("bad option in substitution expression");
399 }
400 }
Glenn L McGrath2570b432003-09-16 05:25:43 +0000401out:
Mark Whitley97562bd2000-07-17 20:06:42 +0000402 /* compile the match string into a regex */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000403 if (*match != '\0') {
404 /* If match is empty, we use last regex used at runtime */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000405 sed_cmd->sub_match = (regex_t *) xmalloc(sizeof(regex_t));
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000406 xregcomp(sed_cmd->sub_match, match, cflags);
407 }
Mark Whitley06f35292000-07-13 19:58:04 +0000408 free(match);
Mark Whitley70705d72000-07-14 19:06:30 +0000409
410 return idx;
Mark Whitley06f35292000-07-13 19:58:04 +0000411}
412
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000413/*
414 * Process the commands arguments
415 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000416static char *parse_cmd_args(sed_cmd_t *sed_cmd, char *cmdstr)
Mark Whitley6315ce62000-07-10 22:55:51 +0000417{
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000418 /* handle (s)ubstitution command */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000419 if (sed_cmd->cmd == 's') cmdstr += parse_subst_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000420 /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */
421 else if (strchr("aic", sed_cmd->cmd)) {
422 if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000423 bb_error_msg_and_die
424 ("only a beginning address can be specified for edit commands");
Rob Landley25d82392004-04-01 09:23:30 +0000425 for(;;) {
426 if(*cmdstr=='\n' || *cmdstr=='\\') {
427 cmdstr++;
428 break;
429 } else if(isspace(*cmdstr)) cmdstr++;
430 else break;
431 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000432 sed_cmd->string = bb_xstrdup(cmdstr);
Glenn L McGrath738fb332003-10-01 06:45:11 +0000433 parse_escapes(sed_cmd->string,sed_cmd->string,strlen(cmdstr),0,0);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000434 cmdstr += strlen(cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000435 /* handle file cmds: (r)ead */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000436 } else if(strchr("rw", sed_cmd->cmd)) {
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000437 if (sed_cmd->end_line || sed_cmd->end_match)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000438 bb_error_msg_and_die("Command only uses one address");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000439 cmdstr += parse_file_cmd(sed_cmd, cmdstr, &sed_cmd->string);
440 if(sed_cmd->cmd=='w')
441 sed_cmd->file=bb_xfopen(sed_cmd->string,"w");
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000442 /* handle branch commands */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000443 } else if (strchr(":bt", sed_cmd->cmd)) {
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000444 int length;
445
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000446 while(isspace(*cmdstr)) cmdstr++;
Glenn L McGrathf4523562003-09-14 06:01:14 +0000447 length = strcspn(cmdstr, semicolon_whitespace);
448 if (length) {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000449 sed_cmd->string = strndup(cmdstr, length);
Glenn L McGrathf4523562003-09-14 06:01:14 +0000450 cmdstr += length;
451 }
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000452 }
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000453 /* translation command */
454 else if (sed_cmd->cmd == 'y') {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000455 char *match, *replace;
456 int i=cmdstr[0];
457
458 cmdstr+=parse_regex_delim(cmdstr, &match, &replace)+1;
459 /* \n already parsed, but \delimiter needs unescaping. */
460 parse_escapes(match,match,strlen(match),i,i);
461 parse_escapes(replace,replace,strlen(replace),i,i);
462
463 sed_cmd->string = xcalloc(1, (strlen(match) + 1) * 2);
464 for (i = 0; match[i] && replace[i]; i++) {
465 sed_cmd->string[i * 2] = match[i];
466 sed_cmd->string[(i * 2) + 1] = replace[i];
467 }
468 free(match);
469 free(replace);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000470 }
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000471 /* if it wasnt a single-letter command that takes no arguments
472 * then it must be an invalid command.
473 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000474 else if (strchr("dDgGhHlnNpPqx={}", sed_cmd->cmd) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000475 bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd);
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000476 }
Mark Whitley70705d72000-07-14 19:06:30 +0000477
478 /* give back whatever's left over */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000479 return (cmdstr);
Eric Andersen50d63601999-11-09 01:47:36 +0000480}
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000481
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000482
483/* Parse address+command sets, skipping comment lines. */
484
485void add_cmd(char *cmdstr)
Erik Andersen1266a131999-12-29 22:19:46 +0000486{
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000487 static char *add_cmd_line=NULL;
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000488 sed_cmd_t *sed_cmd;
Glenn L McGrath586d86c2003-10-09 07:22:59 +0000489 int temp;
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000490
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000491 /* Append this line to any unfinished line from last time. */
492 if(add_cmd_line) {
493 int lastlen=strlen(add_cmd_line);
Eric Andersen638da752003-10-09 08:18:36 +0000494 char *tmp=xmalloc(lastlen+strlen(cmdstr)+2);
Erik Andersen1266a131999-12-29 22:19:46 +0000495
Eric Andersen638da752003-10-09 08:18:36 +0000496 memcpy(tmp,add_cmd_line,lastlen);
497 tmp[lastlen]='\n';
498 strcpy(tmp+lastlen+1,cmdstr);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000499 free(add_cmd_line);
Eric Andersen638da752003-10-09 08:18:36 +0000500 cmdstr=add_cmd_line=tmp;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000501 } else add_cmd_line=NULL;
502
503 /* If this line ends with backslash, request next line. */
Glenn L McGrath586d86c2003-10-09 07:22:59 +0000504 temp=strlen(cmdstr);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000505 if(temp && cmdstr[temp-1]=='\\') {
506 if(!add_cmd_line) add_cmd_line=strdup(cmdstr);
507 add_cmd_line[temp-1]=0;
508 return;
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000509 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000510
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000511 /* Loop parsing all commands in this line. */
512 while(*cmdstr) {
513 /* Skip leading whitespace and semicolons */
514 cmdstr += strspn(cmdstr, semicolon_whitespace);
515
516 /* If no more commands, exit. */
517 if(!*cmdstr) break;
518
519 /* if this is a comment, jump past it and keep going */
520 if (*cmdstr == '#') {
521 /* "#n" is the same as using -n on the command line */
522 if (cmdstr[1] == 'n') be_quiet++;
523 if(!(cmdstr=strpbrk(cmdstr, "\n\r"))) break;
524 continue;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000525 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000526
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000527 /* parse the command
528 * format is: [addr][,addr][!]cmd
529 * |----||-----||-|
530 * part1 part2 part3
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000531 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000532
533 sed_cmd = xcalloc(1, sizeof(sed_cmd_t));
534
535 /* first part (if present) is an address: either a '$', a number or a /regex/ */
536 cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match);
537
538 /* second part (if present) will begin with a comma */
539 if (*cmdstr == ',') {
540 int idx;
541
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000542 cmdstr++;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000543 idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
544 if (!idx) bb_error_msg_and_die("get_address: no address found in string\n");
545 cmdstr += idx;
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000546 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000547
548 /* skip whitespace before the command */
549 while (isspace(*cmdstr)) cmdstr++;
550
551 /* Check for inversion flag */
552 if (*cmdstr == '!') {
553 sed_cmd->invert = 1;
554 cmdstr++;
555
556 /* skip whitespace before the command */
557 while (isspace(*cmdstr)) cmdstr++;
558 }
559
560 /* last part (mandatory) will be a command */
561 if (!*cmdstr) bb_error_msg_and_die("missing command");
562 sed_cmd->cmd = *(cmdstr++);
563 cmdstr = parse_cmd_args(sed_cmd, cmdstr);
564
565 /* Add the command to the command array */
566 sed_cmd_tail->next = sed_cmd;
567 sed_cmd_tail = sed_cmd_tail->next;
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000568 }
569
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000570 /* If we glued multiple lines together, free the memory. */
571 if(add_cmd_line) {
572 free(add_cmd_line);
573 add_cmd_line=NULL;
Mark Whitley6315ce62000-07-10 22:55:51 +0000574 }
575}
576
Rob Landleydcc28662004-11-25 07:21:47 +0000577/* Append to a string, reallocating memory as necessary. */
578
Eric Andersenc52a6b02001-11-10 10:49:42 +0000579struct pipeline {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000580 char *buf; /* Space to hold string */
581 int idx; /* Space used */
582 int len; /* Space allocated */
583} pipeline;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000584
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000585#define PIPE_GROW 64
Eric Andersenc52a6b02001-11-10 10:49:42 +0000586
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000587void pipe_putc(char c)
Eric Andersenc52a6b02001-11-10 10:49:42 +0000588{
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000589 if(pipeline.idx==pipeline.len) {
590 pipeline.buf = xrealloc(pipeline.buf, pipeline.len + PIPE_GROW);
591 pipeline.len+=PIPE_GROW;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000592 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000593 pipeline.buf[pipeline.idx++] = (c);
Eric Andersenc52a6b02001-11-10 10:49:42 +0000594}
595
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000596static void do_subst_w_backrefs(const char *line, const char *replace)
Mark Whitley97562bd2000-07-17 20:06:42 +0000597{
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000598 int i,j;
Mark Whitley97562bd2000-07-17 20:06:42 +0000599
600 /* go through the replacement string */
601 for (i = 0; replace[i]; i++) {
602 /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */
Glenn L McGrath0ad4daa2003-10-01 10:26:23 +0000603 if (replace[i] == '\\' && replace[i+1]>'0' && replace[i+1]<='9') {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000604 int backref=replace[++i]-'0';
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000605
Mark Whitley97562bd2000-07-17 20:06:42 +0000606 /* print out the text held in regmatch[backref] */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000607 if(regmatch[backref].rm_so != -1)
608 for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++)
609 pipe_putc(line[j]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000610 }
611
Mark Whitley83e85f62000-07-25 20:48:44 +0000612 /* if we find a backslash escaped character, print the character */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000613 else if (replace[i] == '\\') pipe_putc(replace[++i]);
Mark Whitley83e85f62000-07-25 20:48:44 +0000614
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000615 /* if we find an unescaped '&' print out the whole matched text. */
616 else if (replace[i] == '&')
Mark Whitley97562bd2000-07-17 20:06:42 +0000617 for (j = regmatch[0].rm_so; j < regmatch[0].rm_eo; j++)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000618 pipe_putc(line[j]);
619 /* Otherwise just output the character. */
620 else pipe_putc(replace[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000621 }
622}
623
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000624static int do_subst_command(sed_cmd_t * sed_cmd, char **line)
Mark Whitley4f7fe772000-07-13 20:01:58 +0000625{
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000626 char *oldline = *line;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000627 int altered = 0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000628 int match_count=0;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000629 regex_t *current_regex;
630
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000631 /* Handle empty regex. */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000632 if (sed_cmd->sub_match == NULL) {
633 current_regex = previous_regex_ptr;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000634 if(!current_regex)
635 bb_error_msg_and_die("No previous regexp.");
636 } else previous_regex_ptr = current_regex = sed_cmd->sub_match;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000637
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000638 /* Find the first match */
639 if(REG_NOMATCH==regexec(current_regex, oldline, 10, regmatch, 0))
Mark Whitley2dc192f2000-11-03 19:47:00 +0000640 return 0;
641
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000642 /* Initialize temporary output buffer. */
643 pipeline.buf=xmalloc(PIPE_GROW);
644 pipeline.len=PIPE_GROW;
645 pipeline.idx=0;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000646
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000647 /* Now loop through, substituting for matches */
648 do {
Mark Whitley4f7fe772000-07-13 20:01:58 +0000649 int i;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000650
Eric Andersenc06f5682004-02-04 10:57:46 +0000651 /* Work around bug in glibc regexec, demonstrated by:
652 echo " a.b" | busybox sed 's [^ .]* x g'
653 The match_count check is so not to break
654 echo "hi" | busybox sed 's/^/!/g' */
655 if(!regmatch[0].rm_so && !regmatch[0].rm_eo && match_count) {
656 pipe_putc(*(oldline++));
657 continue;
658 }
659
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000660 match_count++;
661
662 /* If we aren't interested in this match, output old line to
663 end of match and continue */
664 if(sed_cmd->which_match && sed_cmd->which_match!=match_count) {
665 for(i=0;i<regmatch[0].rm_eo;i++)
666 pipe_putc(oldline[i]);
667 continue;
668 }
669
Mark Whitley2dc192f2000-11-03 19:47:00 +0000670 /* print everything before the match */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000671 for (i = 0; i < regmatch[0].rm_so; i++) pipe_putc(oldline[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000672
Mark Whitley2dc192f2000-11-03 19:47:00 +0000673 /* then print the substitution string */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000674 do_subst_w_backrefs(oldline, sed_cmd->string);
Mark Whitley97562bd2000-07-17 20:06:42 +0000675
Mark Whitley2dc192f2000-11-03 19:47:00 +0000676 /* advance past the match */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000677 oldline += regmatch[0].rm_eo;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000678 /* flag that something has changed */
679 altered++;
Mark Whitley97562bd2000-07-17 20:06:42 +0000680
Mark Whitley2dc192f2000-11-03 19:47:00 +0000681 /* if we're not doing this globally, get out now */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000682 if (sed_cmd->which_match) break;
683 } while (*oldline && (regexec(current_regex, oldline, 10, regmatch, 0) != REG_NOMATCH));
Mark Whitley2dc192f2000-11-03 19:47:00 +0000684
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000685 /* Copy rest of string into output pipeline */
686
687 while(*oldline) pipe_putc(*(oldline++));
688 pipe_putc(0);
Mark Whitley2dc192f2000-11-03 19:47:00 +0000689
Eric Andersenb76cb682001-08-22 05:58:16 +0000690 free(*line);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000691 *line = pipeline.buf;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000692 return altered;
693}
Mark Whitley6315ce62000-07-10 22:55:51 +0000694
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000695/* Set command pointer to point to this label. (Does not handle null label.) */
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000696static sed_cmd_t *branch_to(const char *label)
697{
698 sed_cmd_t *sed_cmd;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000699
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000700 for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000701 if ((sed_cmd->cmd == ':') && (sed_cmd->string) && (strcmp(sed_cmd->string, label) == 0)) {
Glenn L McGrathf4523562003-09-14 06:01:14 +0000702 return (sed_cmd);
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000703 }
704 }
Glenn L McGrathf4523562003-09-14 06:01:14 +0000705 bb_error_msg_and_die("Can't find label for jump to `%s'", label);
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000706}
Mark Whitley6315ce62000-07-10 22:55:51 +0000707
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000708/* Append copy of string to append buffer */
709static void append(char *s)
710{
711 struct append_list *temp=calloc(1,sizeof(struct append_list));
712
713 if(append_head)
714 append_tail=(append_tail->next=temp);
715 else append_head=append_tail=temp;
716 temp->string=strdup(s);
717}
718
719static void flush_append(void)
720{
721 /* Output appended lines. */
722 while(append_head) {
Rob Landley53302f82004-02-18 09:54:15 +0000723 fprintf(nonstdout,"%s\n",append_head->string);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000724 append_tail=append_head->next;
725 free(append_head->string);
726 free(append_head);
727 append_head=append_tail;
728 }
729 append_head=append_tail=NULL;
730}
731
Rob Landleydcc28662004-11-25 07:21:47 +0000732void add_input_file(FILE *file)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000733{
Rob Landleydcc28662004-11-25 07:21:47 +0000734 input_file_list=xrealloc(input_file_list,(input_file_count+1)*sizeof(FILE *));
735 input_file_list[input_file_count++]=file;
736}
737
738/* Get next line of input from input_file_list, flushing append buffer and
739 * noting if we ran out of files without a newline on the last line we read.
740 */
741static char *get_next_line(int *no_newline)
742{
743 char *temp=NULL;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000744 int len;
745
746 flush_append();
Rob Landleydcc28662004-11-25 07:21:47 +0000747 while(current_input_file<input_file_count) {
748 temp=bb_get_line_from_file(input_file_list[current_input_file]);
749 if(temp) {
750 len=strlen(temp);
751 *no_newline=!(len && temp[len-1]=='\n');
752 if(!*no_newline) temp[len-1]=0;
753 break;
754 } else fclose(input_file_list[current_input_file++]);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000755 }
756
757 return temp;
758}
759
760/* Output line of text. missing_newline means the last line output did not
761 end with a newline. no_newline means this line does not end with a
762 newline. */
763
764static int puts_maybe_newline(char *s, FILE *file, int missing_newline, int no_newline)
765{
766 if(missing_newline) fputc('\n',file);
767 fputs(s,file);
768 if(!no_newline) fputc('\n',file);
769
Rob Landley53302f82004-02-18 09:54:15 +0000770 if(ferror(file)) {
771 fprintf(stderr,"Write failed.\n");
772 exit(4); /* It's what gnu sed exits with... */
773 }
774
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000775 return no_newline;
776}
777
Rob Landley53302f82004-02-18 09:54:15 +0000778#define sed_puts(s,n) missing_newline=puts_maybe_newline(s,nonstdout,missing_newline,n)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000779
Rob Landleydcc28662004-11-25 07:21:47 +0000780static void process_files(void)
Mark Whitley6315ce62000-07-10 22:55:51 +0000781{
Rob Landleyce4f0e92004-10-30 06:54:19 +0000782 char *pattern_space, *next_line;
Rob Landleydcc28662004-11-25 07:21:47 +0000783 int linenum = 0, missing_newline=0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000784 int no_newline,next_no_newline=0;
Mark Whitley6315ce62000-07-10 22:55:51 +0000785
Rob Landleydcc28662004-11-25 07:21:47 +0000786 next_line = get_next_line(&next_no_newline);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000787
Rob Landleydcc28662004-11-25 07:21:47 +0000788 /* go through every line in each file */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000789 for(;;) {
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000790 sed_cmd_t *sed_cmd;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000791 int substituted=0;
792
793 /* Advance to next line. Stop if out of lines. */
794 if(!(pattern_space=next_line)) break;
795 no_newline=next_no_newline;
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000796
797 /* Read one line in advance so we can act on the last line, the '$' address */
Rob Landleydcc28662004-11-25 07:21:47 +0000798 next_line = get_next_line(&next_no_newline);
Mark Whitley6315ce62000-07-10 22:55:51 +0000799 linenum++;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000800restart:
Mark Whitley6315ce62000-07-10 22:55:51 +0000801 /* for every line, go through all the commands */
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000802 for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
Eric Andersen52a3c272003-12-23 08:53:51 +0000803 int old_matched, matched;
804
805 old_matched = sed_cmd->in_match;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000806
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000807 /* Determine if this command matches this line: */
808
809 /* Are we continuing a previous multi-line match? */
810
811 sed_cmd->in_match = sed_cmd->in_match
812
813 /* Or is no range necessary? */
814 || (!sed_cmd->beg_line && !sed_cmd->end_line
815 && !sed_cmd->beg_match && !sed_cmd->end_match)
816
817 /* Or did we match the start of a numerical range? */
818 || (sed_cmd->beg_line > 0 && (sed_cmd->beg_line == linenum))
819
820 /* Or does this line match our begin address regex? */
821 || (sed_cmd->beg_match &&
822 !regexec(sed_cmd->beg_match, pattern_space, 0, NULL, 0))
823
824 /* Or did we match last line of input? */
825 || (sed_cmd->beg_line == -1 && next_line == NULL);
826
827 /* Snapshot the value */
828
829 matched = sed_cmd->in_match;
830
831 /* Is this line the end of the current match? */
832
833 if(matched) {
834 sed_cmd->in_match = !(
835 /* has the ending line come, or is this a single address command? */
836 (sed_cmd->end_line ?
837 sed_cmd->end_line==-1 ?
838 !next_line
839 : sed_cmd->end_line<=linenum
840 : !sed_cmd->end_match)
841 /* or does this line matches our last address regex */
Eric Andersen52a3c272003-12-23 08:53:51 +0000842 || (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 +0000843 );
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000844 }
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000845
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000846 /* Skip blocks of commands we didn't match. */
847 if (sed_cmd->cmd == '{') {
848 if(sed_cmd->invert ? matched : !matched)
849 while(sed_cmd && sed_cmd->cmd!='}') sed_cmd=sed_cmd->next;
850 if(!sed_cmd) bb_error_msg_and_die("Unterminated {");
851 continue;
852 }
853
854 /* Okay, so did this line match? */
855 if (sed_cmd->invert ? !matched : matched) {
856 /* Update last used regex in case a blank substitute BRE is found */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000857 if (sed_cmd->beg_match) {
858 previous_regex_ptr = sed_cmd->beg_match;
859 }
Mark Whitley0915c4b2001-06-11 23:50:06 +0000860
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000861 /* actual sedding */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000862 switch (sed_cmd->cmd) {
Mark Whitley0915c4b2001-06-11 23:50:06 +0000863
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000864 /* Print line number */
865 case '=':
Rob Landley53302f82004-02-18 09:54:15 +0000866 fprintf(nonstdout,"%d\n", linenum);
Glenn L McGrath2eed0e22003-09-15 06:28:45 +0000867 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000868
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000869 /* Write the current pattern space up to the first newline */
870 case 'P':
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000871 {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000872 char *tmp = strchr(pattern_space, '\n');
Glenn L McGrath0c518322003-03-30 03:41:53 +0000873
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000874 if (tmp) {
875 *tmp = '\0';
876 sed_puts(pattern_space,1);
877 *tmp = '\n';
878 break;
879 }
880 /* Fall Through */
881 }
882
883 /* Write the current pattern space to output */
884 case 'p':
885 sed_puts(pattern_space,no_newline);
886 break;
887 /* Delete up through first newline */
888 case 'D':
889 {
890 char *tmp = strchr(pattern_space,'\n');
891
892 if(tmp) {
893 tmp=bb_xstrdup(tmp+1);
894 free(pattern_space);
895 pattern_space=tmp;
896 goto restart;
Glenn L McGrath0c518322003-03-30 03:41:53 +0000897 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000898 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000899 /* discard this line. */
900 case 'd':
901 goto discard_line;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000902
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000903 /* Substitute with regex */
904 case 's':
905 if(do_subst_command(sed_cmd, &pattern_space)) {
906 substituted|=1;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000907
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000908 /* handle p option */
909 if(sed_cmd->sub_p)
910 sed_puts(pattern_space,no_newline);
911 /* handle w option */
912 if(sed_cmd->file)
913 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 +0000914
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000915 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000916 break;
917
918 /* Append line to linked list to be printed later */
919 case 'a':
920 {
921 append(sed_cmd->string);
922 break;
Glenn L McGrathd87a7ac2003-04-09 15:26:14 +0000923 }
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000924
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000925 /* Insert text before this line */
926 case 'i':
927 sed_puts(sed_cmd->string,1);
Glenn L McGrath2570b432003-09-16 05:25:43 +0000928 break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000929
930 /* Cut and paste text (replace) */
931 case 'c':
932 /* Only triggers on last line of a matching range. */
Rob Landleyce4f0e92004-10-30 06:54:19 +0000933 if (!sed_cmd->in_match) sed_puts(sed_cmd->string,0);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000934 goto discard_line;
935
936 /* Read file, append contents to output */
937 case 'r':
Glenn L McGrathf4523562003-09-14 06:01:14 +0000938 {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000939 FILE *outfile;
940
941 outfile = fopen(sed_cmd->string, "r");
942 if (outfile) {
943 char *line;
944
945 while ((line = bb_get_chomped_line_from_file(outfile))
946 != NULL)
947 append(line);
948 bb_xprint_and_close_file(outfile);
949 }
950
951 break;
952 }
953
954 /* Write pattern space to file. */
955 case 'w':
956 sed_cmd->no_newline=puts_maybe_newline(pattern_space,sed_cmd->file, sed_cmd->no_newline,no_newline);
957 break;
958
959 /* Read next line from input */
960 case 'n':
961 if (!be_quiet)
962 sed_puts(pattern_space,no_newline);
963 if (next_line) {
964 free(pattern_space);
965 pattern_space = next_line;
966 no_newline=next_no_newline;
Rob Landleydcc28662004-11-25 07:21:47 +0000967 next_line = get_next_line(&next_no_newline);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000968 linenum++;
969 break;
970 }
971 /* fall through */
972
973 /* Quit. End of script, end of input. */
974 case 'q':
975 /* Exit the outer while loop */
976 free(next_line);
977 next_line = NULL;
978 goto discard_commands;
979
980 /* Append the next line to the current line */
981 case 'N':
982 {
983 /* If no next line, jump to end of script and exit. */
984 if (next_line == NULL) {
985 /* Jump to end of script and exit */
986 free(next_line);
987 next_line = NULL;
988 goto discard_line;
989 /* append next_line, read new next_line. */
Glenn L McGrathf4523562003-09-14 06:01:14 +0000990 } else {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000991 int len=strlen(pattern_space);
992
993 pattern_space = realloc(pattern_space, len + strlen(next_line) + 2);
994 pattern_space[len]='\n';
995 strcpy(pattern_space+len+1, next_line);
996 no_newline=next_no_newline;
Rob Landleydcc28662004-11-25 07:21:47 +0000997 next_line = get_next_line(&next_no_newline);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000998 linenum++;
Glenn L McGrathf4523562003-09-14 06:01:14 +0000999 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001000 break;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001001 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001002
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001003 /* Test if substition worked, branch if so. */
1004 case 't':
1005 if (!substituted) break;
1006 substituted=0;
1007 /* Fall through */
1008 /* Branch to label */
1009 case 'b':
1010 if (!sed_cmd->string) goto discard_commands;
1011 else sed_cmd = branch_to(sed_cmd->string);
1012 break;
1013 /* Transliterate characters */
1014 case 'y':
1015 {
1016 int i;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001017
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001018 for (i = 0; pattern_space[i]; i++) {
1019 int j;
1020
1021 for (j = 0; sed_cmd->string[j]; j += 2) {
1022 if (pattern_space[i] == sed_cmd->string[j]) {
1023 pattern_space[i] = sed_cmd->string[j + 1];
1024 }
Glenn L McGrathf01b46d2003-03-30 08:02:18 +00001025 }
1026 }
Glenn L McGrath294d1132003-09-14 16:28:08 +00001027
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001028 break;
Glenn L McGrath91e19782003-04-26 07:40:07 +00001029 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001030 case 'g': /* Replace pattern space with hold space */
1031 free(pattern_space);
Rob Landleyce4f0e92004-10-30 06:54:19 +00001032 pattern_space = strdup(hold_space ? hold_space : "");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001033 break;
1034 case 'G': /* Append newline and hold space to pattern space */
1035 {
1036 int pattern_space_size = 2;
1037 int hold_space_size = 0;
1038
1039 if (pattern_space)
1040 pattern_space_size += strlen(pattern_space);
1041 if (hold_space) hold_space_size = strlen(hold_space);
1042 pattern_space = xrealloc(pattern_space, pattern_space_size + hold_space_size);
1043 if (pattern_space_size == 2) pattern_space[0]=0;
Glenn L McGrath977451e2003-09-15 12:07:48 +00001044 strcat(pattern_space, "\n");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001045 if (hold_space) strcat(pattern_space, hold_space);
1046 no_newline=0;
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001047
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001048 break;
Glenn L McGrath91e19782003-04-26 07:40:07 +00001049 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001050 case 'h': /* Replace hold space with pattern space */
1051 free(hold_space);
1052 hold_space = strdup(pattern_space);
1053 break;
1054 case 'H': /* Append newline and pattern space to hold space */
1055 {
1056 int hold_space_size = 2;
1057 int pattern_space_size = 0;
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001058
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001059 if (hold_space) hold_space_size += strlen(hold_space);
1060 if (pattern_space)
1061 pattern_space_size = strlen(pattern_space);
1062 hold_space = xrealloc(hold_space,
1063 hold_space_size + pattern_space_size);
1064
1065 if (hold_space_size == 2) hold_space[0]=0;
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001066 strcat(hold_space, "\n");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001067 if (pattern_space) strcat(hold_space, pattern_space);
1068
1069 break;
Glenn L McGrath4dc1d252003-09-14 01:25:31 +00001070 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001071 case 'x': /* Exchange hold and pattern space */
1072 {
1073 char *tmp = pattern_space;
1074 pattern_space = hold_space;
1075 no_newline=0;
1076 hold_space = tmp;
1077 break;
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001078 }
Mark Whitley0915c4b2001-06-11 23:50:06 +00001079 }
Robert Griebl47abc492002-06-11 23:43:27 +00001080 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001081 }
Erik Andersen1266a131999-12-29 22:19:46 +00001082
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001083 /*
1084 * exit point from sedding...
1085 */
1086discard_commands:
1087 /* we will print the line unless we were told to be quiet ('-n')
1088 or if the line was suppressed (ala 'd'elete) */
1089 if (!be_quiet) sed_puts(pattern_space,no_newline);
1090
1091 /* Delete and such jump here. */
1092discard_line:
1093 flush_append();
Glenn L McGrathc6adada2003-04-07 12:24:44 +00001094 free(pattern_space);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001095 }
Erik Andersen1266a131999-12-29 22:19:46 +00001096}
1097
Glenn L McGrath42c25732003-10-04 05:27:56 +00001098/* It is possible to have a command line argument with embedded
1099 newlines. This counts as multiple command lines. */
1100
1101static void add_cmd_block(char *cmdstr)
1102{
1103 int go=1;
1104 char *temp=bb_xstrdup(cmdstr),*temp2=temp;
1105
1106 while(go) {
1107 int len=strcspn(temp2,"\n");
1108 if(!temp2[len]) go=0;
1109 else temp2[len]=0;
1110 add_cmd(temp2);
1111 temp2+=len+1;
1112 }
1113 free(temp);
1114}
1115
Erik Andersen1266a131999-12-29 22:19:46 +00001116extern int sed_main(int argc, char **argv)
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001117{
Rob Landleyce4f0e92004-10-30 06:54:19 +00001118 int status = EXIT_SUCCESS, opt, getpat = 1;
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001119
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001120#ifdef CONFIG_FEATURE_CLEAN_UP
Mark Whitley858c1ad2000-07-11 21:38:47 +00001121 /* destroy command strings on exit */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001122 if (atexit(free_and_close_stuff) == -1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001123 bb_perror_msg_and_die("atexit");
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001124#endif
Mark Whitley858c1ad2000-07-11 21:38:47 +00001125
Eric Andersenc06f5682004-02-04 10:57:46 +00001126#define LIE_TO_AUTOCONF
1127#ifdef LIE_TO_AUTOCONF
1128 if(argc==2 && !strcmp(argv[1],"--version")) {
1129 printf("This is not GNU sed version 4.0\n");
1130 exit(0);
1131 }
1132#endif
1133
Mark Whitley6315ce62000-07-10 22:55:51 +00001134 /* do normal option parsing */
Eric Andersen98555482004-05-26 10:03:33 +00001135 while ((opt = getopt(argc, argv, "irne:f:")) > 0) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001136 switch (opt) {
Rob Landley53302f82004-02-18 09:54:15 +00001137 case 'i':
1138 in_place++;
1139 atexit(cleanup_outname);
1140 break;
Eric Andersen98555482004-05-26 10:03:33 +00001141 case 'r':
1142 regex_type|=REG_EXTENDED;
1143 break;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001144 case 'n':
1145 be_quiet++;
1146 break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001147 case 'e':
Glenn L McGrath42c25732003-10-04 05:27:56 +00001148 add_cmd_block(optarg);
Eric Andersenfaa7d862004-04-21 00:56:22 +00001149 getpat=0;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001150 break;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001151 case 'f':
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001152 {
1153 FILE *cmdfile;
1154 char *line;
1155
1156 cmdfile = bb_xfopen(optarg, "r");
1157
1158 while ((line = bb_get_chomped_line_from_file(cmdfile))
1159 != NULL) {
1160 add_cmd(line);
Eric Andersenfaa7d862004-04-21 00:56:22 +00001161 getpat=0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001162 free(line);
1163 }
1164 bb_xprint_and_close_file(cmdfile);
1165
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001166 break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001167 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001168 default:
1169 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001170 }
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001171 }
Mark Whitley6315ce62000-07-10 22:55:51 +00001172
Rob Landleydcc28662004-11-25 07:21:47 +00001173 /* if we didn't get a pattern from -e or -f, use argv[optind] */
Eric Andersenfaa7d862004-04-21 00:56:22 +00001174 if(getpat) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001175 if (argv[optind] == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001176 bb_show_usage();
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001177 else
Glenn L McGrath42c25732003-10-04 05:27:56 +00001178 add_cmd_block(argv[optind++]);
Mark Whitley6315ce62000-07-10 22:55:51 +00001179 }
Glenn L McGrath42c25732003-10-04 05:27:56 +00001180 /* Flush any unfinished commands. */
1181 add_cmd("");
Mark Whitley6315ce62000-07-10 22:55:51 +00001182
Rob Landley53302f82004-02-18 09:54:15 +00001183 /* By default, we write to stdout */
1184 nonstdout=stdout;
1185
Mark Whitley6315ce62000-07-10 22:55:51 +00001186 /* argv[(optind)..(argc-1)] should be names of file to process. If no
1187 * files were specified or '-' was specified, take input from stdin.
1188 * Otherwise, we process all the files specified. */
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001189 if (argv[optind] == NULL) {
Rob Landley53302f82004-02-18 09:54:15 +00001190 if(in_place) {
1191 fprintf(stderr,"sed: Filename required for -i\n");
1192 exit(1);
1193 }
Rob Landleydcc28662004-11-25 07:21:47 +00001194 add_input_file(stdin);
1195 process_files();
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001196 } else {
Mark Whitley6315ce62000-07-10 22:55:51 +00001197 int i;
1198 FILE *file;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001199
Mark Whitley6315ce62000-07-10 22:55:51 +00001200 for (i = optind; i < argc; i++) {
Rob Landley53302f82004-02-18 09:54:15 +00001201 if(!strcmp(argv[i], "-") && !in_place) {
Rob Landleydcc28662004-11-25 07:21:47 +00001202 add_input_file(stdin);
1203 process_files();
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001204 } else {
1205 file = bb_wfopen(argv[i], "r");
1206 if (file) {
Rob Landley53302f82004-02-18 09:54:15 +00001207 if(in_place) {
1208 struct stat statbuf;
1209 outname=bb_xstrndup(argv[i],strlen(argv[i])+6);
1210 strcat(outname,"XXXXXX");
Rob Landley53302f82004-02-18 09:54:15 +00001211 mkstemp(outname);
1212 nonstdout=bb_wfopen(outname,"w");
1213 /* Set permissions of output file */
1214 fstat(fileno(file),&statbuf);
Eric Andersenb9466952004-04-21 00:57:14 +00001215 fchmod(fileno(nonstdout),statbuf.st_mode);
Rob Landley53302f82004-02-18 09:54:15 +00001216 atexit(cleanup_outname);
Rob Landleydcc28662004-11-25 07:21:47 +00001217 add_input_file(file);
1218 process_files();
Rob Landley53302f82004-02-18 09:54:15 +00001219 fclose(nonstdout);
1220 nonstdout=stdout;
1221 unlink(argv[i]);
1222 rename(outname,argv[i]);
1223 free(outname);
1224 outname=0;
Rob Landleydcc28662004-11-25 07:21:47 +00001225 } else add_input_file(file);
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001226 } else {
1227 status = EXIT_FAILURE;
1228 }
1229 }
Mark Whitley6315ce62000-07-10 22:55:51 +00001230 }
Rob Landleydcc28662004-11-25 07:21:47 +00001231 if(input_file_count>current_input_file) process_files();
Mark Whitley6315ce62000-07-10 22:55:51 +00001232 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001233
Matt Kraaia5f09c62001-11-12 16:44:55 +00001234 return status;
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001235}