blob: 6beba0661893edae23b60811193dda4cbee92164 [file] [log] [blame]
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001/*
Mark Whitley6315ce62000-07-10 22:55:51 +00002 * sed.c - very minimalist version of sed
Eric Andersen6b6b3f61999-10-28 16:06:25 +00003 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00004 * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
5 * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org>
Matt Kraai5ed78ad2002-01-03 21:12:34 +00006 * Copyright (C) 2002 Matt Kraai
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +00007 * Copyright (C) 2003 by Glenn McGrath <bug1@optushome.com.au>
Erik Andersen1266a131999-12-29 22:19:46 +00008 *
Eric Andersen6b6b3f61999-10-28 16:06:25 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24
Mark Whitley6315ce62000-07-10 22:55:51 +000025/*
26 Supported features and commands in this version of sed:
27
28 - comments ('#')
Mark Whitley94074a92000-07-14 00:00:15 +000029 - address matching: num|/matchstr/[,num|/matchstr/|$]command
30 - commands: (p)rint, (d)elete, (s)ubstitue (with g & I flags)
31 - edit commands: (a)ppend, (i)nsert, (c)hange
Mark Whitley1f3b9f22001-05-11 22:27:13 +000032 - file commands: (r)ead
Mark Whitley97562bd2000-07-17 20:06:42 +000033 - backreferences in substitution expressions (\1, \2...\9)
Glenn L McGrathf50ce312003-03-09 15:12:24 +000034 - grouped commands: {cmd1;cmd2}
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000035 - transliteration (y/source-chars/dest-chars/)
36 - pattern space hold space storing / swapping (g, h, x)
37 - labels / branching (: label, b, t)
Glenn L McGrathf50ce312003-03-09 15:12:24 +000038
Mark Whitley6315ce62000-07-10 22:55:51 +000039 (Note: Specifying an address (range) to match is *optional*; commands
40 default to the whole pattern space if no specific address match was
41 requested.)
42
43 Unsupported features:
44
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000045 - GNU extensions
Mark Whitley6315ce62000-07-10 22:55:51 +000046 - and lots, lots more.
Glenn L McGrathd5eadea2003-03-09 02:39:29 +000047
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000048 Bugs:
49
50 - Cant subst globally using ^ or $ in regex, eg. "aah" | sed 's/^a/b/g'
51
Glenn L McGrathd5eadea2003-03-09 02:39:29 +000052 Reference http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html
Mark Whitley6315ce62000-07-10 22:55:51 +000053*/
54
Eric Andersen6b6b3f61999-10-28 16:06:25 +000055#include <stdio.h>
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000056#include <unistd.h> /* for getopt() */
Mark Whitley6315ce62000-07-10 22:55:51 +000057#include <regex.h>
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000058#include <string.h> /* for strdup() */
Eric Andersen6b6b3f61999-10-28 16:06:25 +000059#include <errno.h>
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000060#include <ctype.h> /* for isspace() */
Eric Andersened3ef502001-01-27 08:24:39 +000061#include <stdlib.h>
Eric Andersen3570a342000-09-25 21:45:58 +000062#include "busybox.h"
Mark Whitley6315ce62000-07-10 22:55:51 +000063
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +000064typedef struct sed_cmd_s {
Eric Andersenc52a6b02001-11-10 10:49:42 +000065 /* Order by alignment requirements */
Mark Whitley2dc192f2000-11-03 19:47:00 +000066
Mark Whitley6315ce62000-07-10 22:55:51 +000067 /* address storage */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000068 regex_t *beg_match; /* sed -e '/match/cmd' */
69 regex_t *end_match; /* sed -e '/match/,/end_match/cmd' */
Mark Whitley6315ce62000-07-10 22:55:51 +000070
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +000071 int beg_line; /* 'sed 1p' 0 == no begining line, apply commands to all lines */
72 int end_line; /* 'sed 1,3p' 0 == no end line, use only beginning. -1 == $ */
73
74 /* inversion flag */
75 int invert; /* the '!' after the address */
76// int block_cmd; /* This command is part of a group that has a command address */
77
Mark Whitley2dc192f2000-11-03 19:47:00 +000078 /* SUBSTITUTION COMMAND SPECIFIC FIELDS */
79
80 /* sed -e 's/sub_match/replace/' */
81 regex_t *sub_match;
82 char *replace;
Eric Andersenc52a6b02001-11-10 10:49:42 +000083
84 /* EDIT COMMAND (a,i,c) SPECIFIC FIELDS */
85 char *editline;
86
87 /* FILE COMMAND (r) SPECIFIC FIELDS */
88 char *filename;
89
Eric Andersenc52a6b02001-11-10 10:49:42 +000090 /* SUBSTITUTION COMMAND SPECIFIC FIELDS */
91
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000092 unsigned int num_backrefs:4; /* how many back references (\1..\9) */
93 /* Note: GNU/POSIX sed does not save more than nine backrefs, so
94 * we only use 4 bits to hold the number */
95 unsigned int sub_g:1; /* sed -e 's/foo/bar/g' (global) */
96 unsigned int sub_p:1; /* sed -e 's/foo/bar/p' (print substitution) */
Mark Whitley94074a92000-07-14 00:00:15 +000097
Glenn L McGrathf01b46d2003-03-30 08:02:18 +000098 /* TRANSLATE COMMAND */
99 char *translate;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000100
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000101 /* GENERAL FIELDS */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000102 /* the command */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000103 char cmd; /* p,d,s (add more at your leisure :-) */
Robert Griebl47abc492002-06-11 23:43:27 +0000104
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000105 /* Branch commands */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000106 char *label;
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000107
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000108 /* next command in list (sequential list of specified commands) */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000109 struct sed_cmd_s *next;
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000110
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +0000111} sed_cmd_t;
Mark Whitley6315ce62000-07-10 22:55:51 +0000112
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000113
114/* externs */
115extern void xregcomp(regex_t * preg, const char *regex, int cflags);
116extern int optind; /* in unistd.h */
117extern char *optarg; /* ditto */
118
Mark Whitley6315ce62000-07-10 22:55:51 +0000119/* globals */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000120/* options */
121static int be_quiet = 0;
122static const char bad_format_in_subst[] =
123 "bad format in substitution expression";
124
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000125/* linked list of sed commands */
126static sed_cmd_t sed_cmd_head;
127static sed_cmd_t *sed_cmd_tail = &sed_cmd_head;
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000128
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000129const char *const semicolon_whitespace = "; \n\r\t\v\0";
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000130static regex_t *previous_regex_ptr = NULL;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000131
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000132
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000133#ifdef CONFIG_FEATURE_CLEAN_UP
Matt Kraai0c390a72001-11-20 16:00:19 +0000134static void destroy_cmd_strs(void)
Mark Whitley6315ce62000-07-10 22:55:51 +0000135{
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000136 sed_cmd_t *sed_cmd = sed_cmd_head.next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000137
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000138 while (sed_cmd) {
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000139 sed_cmd_t *sed_cmd_next = sed_cmd->next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000140
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000141 if (sed_cmd->beg_match) {
142 regfree(sed_cmd->beg_match);
143 free(sed_cmd->beg_match);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000144 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000145 if (sed_cmd->end_match) {
146 regfree(sed_cmd->end_match);
147 free(sed_cmd->end_match);
Mark Whitley6315ce62000-07-10 22:55:51 +0000148 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000149 if (sed_cmd->sub_match) {
150 regfree(sed_cmd->sub_match);
151 free(sed_cmd->sub_match);
Mark Whitley6315ce62000-07-10 22:55:51 +0000152 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000153 free(sed_cmd->replace);
154 free(sed_cmd);
155 sed_cmd = sed_cmd_next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000156 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000157}
Mark Whitleyc41e8c82000-07-12 23:35:21 +0000158#endif
Mark Whitley6315ce62000-07-10 22:55:51 +0000159
Mark Whitley6315ce62000-07-10 22:55:51 +0000160/*
Eric Andersen28b3c532001-01-02 11:01:31 +0000161 * index_of_next_unescaped_regexp_delim - walks left to right through a string
162 * beginning at a specified index and returns the index of the next regular
163 * expression delimiter (typically a forward * slash ('/')) not preceeded by
164 * a backslash ('\').
Mark Whitley6315ce62000-07-10 22:55:51 +0000165 */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000166static int index_of_next_unescaped_regexp_delim(const char delimiter,
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000167 const char *str)
Mark Whitley6315ce62000-07-10 22:55:51 +0000168{
Matt Kraaia0065d52001-08-24 14:45:50 +0000169 int bracket = -1;
170 int escaped = 0;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000171 int idx = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000172 char ch;
Matt Kraaia0065d52001-08-24 14:45:50 +0000173
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000174 for (; (ch = str[idx]); idx++) {
Matt Kraaia0065d52001-08-24 14:45:50 +0000175 if (bracket != -1) {
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000176 if (ch == ']' && !(bracket == idx - 1 || (bracket == idx - 2
177 && str[idx - 1] == '^')))
Matt Kraaia0065d52001-08-24 14:45:50 +0000178 bracket = -1;
179 } else if (escaped)
180 escaped = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000181 else if (ch == '\\')
Matt Kraaia0065d52001-08-24 14:45:50 +0000182 escaped = 1;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000183 else if (ch == '[')
Matt Kraaia0065d52001-08-24 14:45:50 +0000184 bracket = idx;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000185 else if (ch == delimiter)
Mark Whitley97562bd2000-07-17 20:06:42 +0000186 return idx;
187 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000188
Mark Whitley97562bd2000-07-17 20:06:42 +0000189 /* if we make it to here, we've hit the end of the string */
190 return -1;
Mark Whitley6315ce62000-07-10 22:55:51 +0000191}
192
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000193static int parse_regex_delim(const char *cmdstr, char **match, char **replace)
194{
195 const char *cmdstr_ptr = cmdstr;
196 char delimiter;
197 int idx = 0;
198
199 /* verify that the 's' is followed by something. That something
200 * (typically a 'slash') is now our regexp delimiter... */
201 if (*cmdstr == '\0')
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000202 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000203 else
204 delimiter = *cmdstr_ptr;
205
206 cmdstr_ptr++;
207
208 /* save the match string */
209 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
210 if (idx == -1) {
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000211 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000212 }
213 *match = bb_xstrndup(cmdstr_ptr, idx);
214
215 /* save the replacement string */
216 cmdstr_ptr += idx + 1;
217 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
218 if (idx == -1) {
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000219 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000220 }
221 *replace = bb_xstrndup(cmdstr_ptr, idx);
222
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000223 return ((cmdstr_ptr - cmdstr) + idx);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000224}
225
Mark Whitley6315ce62000-07-10 22:55:51 +0000226/*
227 * returns the index in the string just past where the address ends.
228 */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000229static int get_address(char *my_str, int *linenum, regex_t ** regex)
Mark Whitley6315ce62000-07-10 22:55:51 +0000230{
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000231 int idx = 0;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000232
Mark Whitley6315ce62000-07-10 22:55:51 +0000233 if (isdigit(my_str[idx])) {
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000234 char *endstr;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000235
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000236 *linenum = strtol(my_str, &endstr, 10);
237 /* endstr shouldnt ever equal NULL */
238 idx = endstr - my_str;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000239 } else if (my_str[idx] == '$') {
Mark Whitley0915c4b2001-06-11 23:50:06 +0000240 *linenum = -1;
Mark Whitley6315ce62000-07-10 22:55:51 +0000241 idx++;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000242 } else if (my_str[idx] == '/' || my_str[idx] == '\\') {
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000243 int idx_start = 1;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000244 char delimiter;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000245
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000246 delimiter = '/';
Robert Griebl79401472002-08-06 21:07:17 +0000247 if (my_str[idx] == '\\') {
Robert Griebl00f5ecb2002-08-06 23:13:31 +0000248 idx_start++;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000249 delimiter = my_str[++idx];
Robert Griebl79401472002-08-06 21:07:17 +0000250 }
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000251 idx++;
252 idx += index_of_next_unescaped_regexp_delim(delimiter, my_str + idx);
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000253 if (idx == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000254 bb_error_msg_and_die("unterminated match expression");
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000255 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +0000256 my_str[idx] = '\0';
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000257
258 *regex = (regex_t *) xmalloc(sizeof(regex_t));
259 xregcomp(*regex, my_str + idx_start, REG_NEWLINE);
260 idx++; /* so it points to the next character after the last '/' */
Mark Whitley6315ce62000-07-10 22:55:51 +0000261 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000262 return idx;
263}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000264
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +0000265static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr)
Mark Whitley06f35292000-07-13 19:58:04 +0000266{
Glenn L McGrathe01f9662003-03-18 08:37:57 +0000267 int cflags = 0;
Mark Whitley06f35292000-07-13 19:58:04 +0000268 char *match;
269 int idx = 0;
Mark Whitley97562bd2000-07-17 20:06:42 +0000270 int j;
Mark Whitley06f35292000-07-13 19:58:04 +0000271
272 /*
273 * the string that gets passed to this function should look like this:
Mark Whitley0e4cec02000-08-21 21:29:20 +0000274 * s/match/replace/gIp
275 * || | |||
Mark Whitley06f35292000-07-13 19:58:04 +0000276 * mandatory optional
277 *
278 * (all three of the '/' slashes are mandatory)
279 */
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000280 idx = parse_regex_delim(substr, &match, &sed_cmd->replace);
Mark Whitley06f35292000-07-13 19:58:04 +0000281
Mark Whitley97562bd2000-07-17 20:06:42 +0000282 /* determine the number of back references in the match string */
283 /* Note: we compute this here rather than in the do_subst_command()
284 * function to save processor time, at the expense of a little more memory
285 * (4 bits) per sed_cmd */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000286
287 /* sed_cmd->num_backrefs = 0; *//* XXX: not needed? --apparently not */
Mark Whitley97562bd2000-07-17 20:06:42 +0000288 for (j = 0; match[j]; j++) {
289 /* GNU/POSIX sed does not save more than nine backrefs */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000290 if (match[j] == '\\' && match[j + 1] == '('
291 && sed_cmd->num_backrefs <= 9)
Mark Whitley97562bd2000-07-17 20:06:42 +0000292 sed_cmd->num_backrefs++;
293 }
294
Mark Whitley06f35292000-07-13 19:58:04 +0000295 /* process the flags */
296 while (substr[++idx]) {
297 switch (substr[idx]) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000298 case 'g':
299 sed_cmd->sub_g = 1;
300 break;
Glenn L McGrathbed40332003-03-10 02:21:14 +0000301 /* Hmm, i dont see the I option mentioned in the standard */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000302 case 'I':
303 cflags |= REG_ICASE;
304 break;
305 case 'p':
306 sed_cmd->sub_p = 1;
307 break;
308 default:
309 /* any whitespace or semicolon trailing after a s/// is ok */
310 if (strchr(semicolon_whitespace, substr[idx]))
311 goto out;
312 /* else */
313 bb_error_msg_and_die("bad option in substitution expression");
Mark Whitley06f35292000-07-13 19:58:04 +0000314 }
315 }
Mark Whitley70705d72000-07-14 19:06:30 +0000316
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000317 out:
Mark Whitley97562bd2000-07-17 20:06:42 +0000318 /* compile the match string into a regex */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000319 if (*match != '\0') {
320 /* If match is empty, we use last regex used at runtime */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000321 sed_cmd->sub_match = (regex_t *) xmalloc(sizeof(regex_t));
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000322 xregcomp(sed_cmd->sub_match, match, cflags);
323 }
Mark Whitley06f35292000-07-13 19:58:04 +0000324 free(match);
Mark Whitley70705d72000-07-14 19:06:30 +0000325
326 return idx;
Mark Whitley06f35292000-07-13 19:58:04 +0000327}
328
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000329static void replace_slash_n(char *string)
330{
331 int i;
332 int remaining = strlen(string);
333
334 for (i = 0; string[i]; i++) {
335 if ((string[i] == '\\') && (string[i + 1] == 'n')) {
336 string[i] = '\n';
337 memmove(string + i + 1, string + i + 1, remaining - 1);
338 } else {
339 remaining--;
340 }
341 }
342}
343
344static int parse_translate_cmd(sed_cmd_t * const sed_cmd, const char *cmdstr)
345{
346 char *match;
347 char *replace;
348 int idx;
349 int i;
350
351 idx = parse_regex_delim(cmdstr, &match, &replace);
352 replace_slash_n(match);
353 replace_slash_n(replace);
354 sed_cmd->translate = xcalloc(1, (strlen(match) + 1) * 2);
355 for (i = 0; (match[i] != 0) && (replace[i] != 0); i++) {
356 sed_cmd->translate[i * 2] = match[i];
357 sed_cmd->translate[(i * 2) + 1] = replace[i];
358 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000359 return (idx + 1);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000360}
361
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000362static int parse_edit_cmd(sed_cmd_t * sed_cmd, const char *editstr)
Mark Whitley94074a92000-07-14 00:00:15 +0000363{
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000364 int i, j;
Mark Whitley94074a92000-07-14 00:00:15 +0000365
366 /*
367 * the string that gets passed to this function should look like this:
368 *
369 * need one of these
370 * |
371 * | this backslash (immediately following the edit command) is mandatory
372 * | |
373 * [aic]\
374 * TEXT1\
375 * TEXT2\
376 * TEXTN
377 *
378 * as soon as we hit a TEXT line that has no trailing '\', we're done.
379 * this means a command like:
380 *
381 * i\
382 * INSERTME
383 *
384 * is a-ok.
385 *
386 */
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000387 if ((*editstr != '\\') || ((editstr[1] != '\n') && (editstr[1] != '\r'))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000388 bb_error_msg_and_die("bad format in edit expression");
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000389 }
Mark Whitley94074a92000-07-14 00:00:15 +0000390
391 /* store the edit line text */
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000392 sed_cmd->editline = xmalloc(strlen(&editstr[2]) + 2);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000393 for (i = 2, j = 0;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000394 editstr[i] != '\0' && strchr("\r\n", editstr[i]) == NULL; i++, j++) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000395 if ((editstr[i] == '\\') && strchr("\n\r", editstr[i + 1]) != NULL) {
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000396 sed_cmd->editline[j] = '\n';
397 i++;
398 } else
399 sed_cmd->editline[j] = editstr[i];
Mark Whitley94074a92000-07-14 00:00:15 +0000400 }
Mark Whitley70705d72000-07-14 19:06:30 +0000401
Mark Whitley56c14a62001-04-20 23:41:44 +0000402 /* figure out if we need to add a newline */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000403 if (sed_cmd->editline[j - 1] != '\n')
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000404 sed_cmd->editline[j++] = '\n';
Mark Whitley56c14a62001-04-20 23:41:44 +0000405
406 /* terminate string */
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000407 sed_cmd->editline[j] = '\0';
Mark Whitley464c5de2000-07-14 23:24:00 +0000408
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000409 return i;
Mark Whitley94074a92000-07-14 00:00:15 +0000410}
411
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000412
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000413static int parse_file_cmd(sed_cmd_t * sed_cmd, const char *filecmdstr)
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000414{
415 int idx = 0;
416 int filenamelen = 0;
417
418 /*
419 * the string that gets passed to this function should look like this:
420 * '[ ]filename'
421 * | |
422 * | a filename
423 * |
424 * optional whitespace
425
426 * re: the file to be read, the GNU manual says the following: "Note that
427 * if filename cannot be read, it is treated as if it were an empty file,
428 * without any error indication." Thus, all of the following commands are
429 * perfectly leagal:
430 *
431 * sed -e '1r noexist'
432 * sed -e '1r ;'
433 * sed -e '1r'
434 */
435
436 /* the file command may be followed by whitespace; move past it. */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000437 while (isspace(filecmdstr[++idx])) {;
438 }
439
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000440 /* the first non-whitespace we get is a filename. the filename ends when we
441 * hit a normal sed command terminator or end of string */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000442 filenamelen = strcspn(&filecmdstr[idx], semicolon_whitespace);
Matt Kraai6e9e1362001-05-27 14:11:52 +0000443 sed_cmd->filename = xmalloc(filenamelen + 1);
444 safe_strncpy(sed_cmd->filename, &filecmdstr[idx], filenamelen + 1);
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000445 return idx + filenamelen;
446}
447
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000448/*
449 * Process the commands arguments
450 */
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000451static char *parse_cmd_str(sed_cmd_t * sed_cmd, char *cmdstr)
Mark Whitley6315ce62000-07-10 22:55:51 +0000452{
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000453 /* handle (s)ubstitution command */
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000454 if (sed_cmd->cmd == 's') {
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000455 cmdstr += parse_subst_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000456 }
457 /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */
458 else if (strchr("aic", sed_cmd->cmd)) {
459 if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000460 bb_error_msg_and_die
461 ("only a beginning address can be specified for edit commands");
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000462 cmdstr += parse_edit_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000463 }
464 /* handle file cmds: (r)ead */
465 else if (sed_cmd->cmd == 'r') {
466 if (sed_cmd->end_line || sed_cmd->end_match)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000467 bb_error_msg_and_die("Command only uses one address");
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000468 cmdstr += parse_file_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000469 }
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000470 /* handle branch commands */
Glenn L McGrath595a6a42003-03-28 08:36:52 +0000471 else if (strchr(":bt", sed_cmd->cmd)) {
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000472 int length;
473
474 cmdstr += strspn(cmdstr, " ");
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000475 length = strcspn(cmdstr, "; \n");
476 sed_cmd->label = strndup(cmdstr, length);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000477 cmdstr += length;
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000478 }
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000479 /* translation command */
480 else if (sed_cmd->cmd == 'y') {
481 cmdstr += parse_translate_cmd(sed_cmd, cmdstr);
482 }
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000483 /* if it wasnt a single-letter command that takes no arguments
484 * then it must be an invalid command.
485 */
Glenn L McGrath91e19782003-04-26 07:40:07 +0000486 else if (strchr("dgGhHnNpPqx={}", sed_cmd->cmd) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000487 bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd);
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000488 }
Mark Whitley70705d72000-07-14 19:06:30 +0000489
490 /* give back whatever's left over */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000491 return (cmdstr);
Eric Andersen50d63601999-11-09 01:47:36 +0000492}
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000493
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000494static char *add_cmd(sed_cmd_t * sed_cmd, char *cmdstr)
Erik Andersen1266a131999-12-29 22:19:46 +0000495{
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000496 /* Skip over leading whitespace and semicolons */
497 cmdstr += strspn(cmdstr, semicolon_whitespace);
Erik Andersen1266a131999-12-29 22:19:46 +0000498
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000499 /* if we ate the whole thing, that means there was just trailing
500 * whitespace or a final / no-op semicolon. either way, get out */
501 if (*cmdstr == '\0') {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000502 return (NULL);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000503 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000504
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000505 /* if this is a comment, jump past it and keep going */
506 if (*cmdstr == '#') {
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000507 /* "#n" is the same as using -n on the command line */
508 if (cmdstr[1] == 'n') {
509 be_quiet++;
510 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000511 return (strpbrk(cmdstr, "\n\r"));
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000512 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000513
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000514 /* parse the command
515 * format is: [addr][,addr]cmd
516 * |----||-----||-|
517 * part1 part2 part3
518 */
519
520 /* first part (if present) is an address: either a '$', a number or a /regex/ */
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000521 cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000522
523 /* second part (if present) will begin with a comma */
524 if (*cmdstr == ',') {
525 int idx;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000526
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000527 cmdstr++;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000528 idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000529 if (idx == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000530 bb_error_msg_and_die("get_address: no address found in string\n"
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000531 "\t(you probably didn't check the string you passed me)");
Mark Whitley70705d72000-07-14 19:06:30 +0000532 }
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000533 cmdstr += idx;
534 }
Mark Whitley70705d72000-07-14 19:06:30 +0000535
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000536 /* skip whitespace before the command */
537 while (isspace(*cmdstr)) {
538 cmdstr++;
539 }
540
541 /* there my be the inversion flag between part2 and part3 */
542 if (*cmdstr == '!') {
543 sed_cmd->invert = 1;
544 cmdstr++;
545
546#ifdef SED_FEATURE_STRICT_CHECKING
547 /* According to the spec
548 * It is unspecified whether <blank>s can follow a '!' character,
549 * and conforming applications shall not follow a '!' character
550 * with <blank>s.
551 */
552 if (isblank(cmdstr[idx]) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000553 bb_error_msg_and_die("blank follows '!'");}
554#else
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000555 /* skip whitespace before the command */
556 while (isspace(*cmdstr)) {
557 cmdstr++;
558 }
559#endif
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000560 }
561
562 /* last part (mandatory) will be a command */
563 if (*cmdstr == '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000564 bb_error_msg_and_die("missing command");
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000565
566 sed_cmd->cmd = *cmdstr;
567 cmdstr++;
568
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000569 cmdstr = parse_cmd_str(sed_cmd, cmdstr);
570
571 /* Add the command to the command array */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000572 sed_cmd_tail->next = sed_cmd;
573 sed_cmd_tail = sed_cmd_tail->next;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000574
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000575 return (cmdstr);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000576}
577
578static void add_cmd_str(char *cmdstr)
579{
Glenn L McGrath0c518322003-03-30 03:41:53 +0000580#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
581 char *cmdstr_ptr = cmdstr;
582
583 /* HACK: convert "\n" to match tranlated '\n' string */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000584 while ((cmdstr_ptr = strstr(cmdstr_ptr, "\\n")) != NULL) {
Glenn L McGrath0c518322003-03-30 03:41:53 +0000585 cmdstr = xrealloc(cmdstr, strlen(cmdstr) + 2);
586 cmdstr_ptr = strstr(cmdstr, "\\n");
587 memmove(cmdstr_ptr + 1, cmdstr_ptr, strlen(cmdstr_ptr) + 1);
588 cmdstr_ptr[0] = '\\';
589 cmdstr_ptr += 3;
590 }
591#endif
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000592 do {
593 sed_cmd_t *sed_cmd;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000594
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000595 sed_cmd = xcalloc(1, sizeof(sed_cmd_t));
596 cmdstr = add_cmd(sed_cmd, cmdstr);
597 } while (cmdstr && strlen(cmdstr));
Mark Whitley6315ce62000-07-10 22:55:51 +0000598}
599
600
601static void load_cmd_file(char *filename)
602{
603 FILE *cmdfile;
604 char *line;
Mark Whitley94074a92000-07-14 00:00:15 +0000605 char *nextline;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000606 char *e;
Mark Whitley6315ce62000-07-10 22:55:51 +0000607
Manuel Novoa III cad53642003-03-19 09:13:01 +0000608 cmdfile = bb_xfopen(filename, "r");
Mark Whitley6315ce62000-07-10 22:55:51 +0000609
Manuel Novoa III cad53642003-03-19 09:13:01 +0000610 while ((line = bb_get_line_from_file(cmdfile)) != NULL) {
Mark Whitley94074a92000-07-14 00:00:15 +0000611 /* if a line ends with '\' it needs the next line appended to it */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000612 while (((e = last_char_is(line, '\n')) != NULL)
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000613 && (e > line) && (e[-1] == '\\')
614 && ((nextline = bb_get_line_from_file(cmdfile)) != NULL)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000615 line = xrealloc(line, (e - line) + 1 + strlen(nextline) + 1);
Mark Whitley94074a92000-07-14 00:00:15 +0000616 strcat(line, nextline);
Mark Whitley464c5de2000-07-14 23:24:00 +0000617 free(nextline);
Mark Whitley94074a92000-07-14 00:00:15 +0000618 }
Mark Whitley464c5de2000-07-14 23:24:00 +0000619 /* eat trailing newline (if any) --if I don't do this, edit commands
620 * (aic) will print an extra newline */
Matt Kraai05e782d2001-02-01 16:49:30 +0000621 chomp(line);
Mark Whitley6315ce62000-07-10 22:55:51 +0000622 add_cmd_str(line);
623 free(line);
624 }
625}
626
Eric Andersenc52a6b02001-11-10 10:49:42 +0000627struct pipeline {
628 char *buf;
629 int idx;
630 int len;
631};
632
Eric Andersenb76cb682001-08-22 05:58:16 +0000633#define PIPE_MAGIC 0x7f
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000634#define PIPE_GROW 64
Eric Andersenc52a6b02001-11-10 10:49:42 +0000635
636void pipe_putc(struct pipeline *const pipeline, char c)
637{
638 if (pipeline->buf[pipeline->idx] == PIPE_MAGIC) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000639 pipeline->buf = xrealloc(pipeline->buf, pipeline->len + PIPE_GROW);
Eric Andersenc52a6b02001-11-10 10:49:42 +0000640 memset(pipeline->buf + pipeline->len, 0, PIPE_GROW);
641 pipeline->len += PIPE_GROW;
642 pipeline->buf[pipeline->len - 1] = PIPE_MAGIC;
643 }
644 pipeline->buf[pipeline->idx++] = (c);
645}
646
647#define pipeputc(c) pipe_putc(pipeline, c)
648
649#if 0
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000650{
651 if (pipeline[pipeline_idx] == PIPE_MAGIC) {
652 pipeline = xrealloc(pipeline, pipeline_len + PIPE_GROW);
653 memset(pipeline + pipeline_len, 0, PIPE_GROW);
654 pipeline_len += PIPE_GROW;
655 pipeline[pipeline_len - 1] = PIPE_MAGIC;
656 }
657 pipeline[pipeline_idx++] = (c);
658}
Eric Andersenc52a6b02001-11-10 10:49:42 +0000659#endif
Eric Andersenb76cb682001-08-22 05:58:16 +0000660
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000661static void print_subst_w_backrefs(const char *line, const char *replace,
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000662 regmatch_t * regmatch, struct pipeline *const pipeline, int matches)
Mark Whitley97562bd2000-07-17 20:06:42 +0000663{
664 int i;
665
666 /* go through the replacement string */
667 for (i = 0; replace[i]; i++) {
668 /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000669 if (replace[i] == '\\' && isdigit(replace[i + 1])) {
Mark Whitley97562bd2000-07-17 20:06:42 +0000670 int j;
671 char tmpstr[2];
672 int backref;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000673
674 ++i; /* i now indexes the backref number, instead of the leading slash */
Mark Whitley97562bd2000-07-17 20:06:42 +0000675 tmpstr[0] = replace[i];
676 tmpstr[1] = 0;
677 backref = atoi(tmpstr);
678 /* print out the text held in regmatch[backref] */
Matt Kraaia3e4f452001-08-20 21:21:06 +0000679 if (backref <= matches && regmatch[backref].rm_so != -1)
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000680 for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000681 j++)
Eric Andersenb76cb682001-08-22 05:58:16 +0000682 pipeputc(line[j]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000683 }
684
Mark Whitley83e85f62000-07-25 20:48:44 +0000685 /* if we find a backslash escaped character, print the character */
686 else if (replace[i] == '\\') {
687 ++i;
Eric Andersenb76cb682001-08-22 05:58:16 +0000688 pipeputc(replace[i]);
Mark Whitley83e85f62000-07-25 20:48:44 +0000689 }
690
Mark Whitley97562bd2000-07-17 20:06:42 +0000691 /* if we find an unescaped '&' print out the whole matched text.
692 * fortunately, regmatch[0] contains the indicies to the whole matched
693 * expression (kinda seems like it was designed for just such a
694 * purpose...) */
Glenn L McGrath24103862003-04-09 07:51:43 +0000695 else if (replace[i] == '&') {
Mark Whitley97562bd2000-07-17 20:06:42 +0000696 int j;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000697
Mark Whitley97562bd2000-07-17 20:06:42 +0000698 for (j = regmatch[0].rm_so; j < regmatch[0].rm_eo; j++)
Eric Andersenb76cb682001-08-22 05:58:16 +0000699 pipeputc(line[j]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000700 }
701 /* nothing special, just print this char of the replacement string to stdout */
702 else
Eric Andersenb76cb682001-08-22 05:58:16 +0000703 pipeputc(replace[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000704 }
705}
706
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000707static int do_subst_command(sed_cmd_t * sed_cmd, char **line)
Mark Whitley4f7fe772000-07-13 20:01:58 +0000708{
Eric Andersenb76cb682001-08-22 05:58:16 +0000709 char *hackline = *line;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000710 struct pipeline thepipe = { NULL, 0, 0 };
Eric Andersenc52a6b02001-11-10 10:49:42 +0000711 struct pipeline *const pipeline = &thepipe;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000712 int altered = 0;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000713 int result;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000714 regmatch_t *regmatch = NULL;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000715 regex_t *current_regex;
716
717 if (sed_cmd->sub_match == NULL) {
718 current_regex = previous_regex_ptr;
719 } else {
720 previous_regex_ptr = current_regex = sed_cmd->sub_match;
721 }
722 result = regexec(current_regex, hackline, 0, NULL, 0);
Mark Whitley4f7fe772000-07-13 20:01:58 +0000723
Mark Whitley2dc192f2000-11-03 19:47:00 +0000724 /* we only proceed if the substitution 'search' expression matches */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000725 if (result == REG_NOMATCH) {
Mark Whitley2dc192f2000-11-03 19:47:00 +0000726 return 0;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000727 }
Mark Whitley2dc192f2000-11-03 19:47:00 +0000728
729 /* whaddaya know, it matched. get the number of back references */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000730 regmatch = xmalloc(sizeof(regmatch_t) * (sed_cmd->num_backrefs + 1));
Mark Whitley2dc192f2000-11-03 19:47:00 +0000731
Eric Andersenb76cb682001-08-22 05:58:16 +0000732 /* allocate more PIPE_GROW bytes
733 if replaced string is larger than original */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000734 thepipe.len = strlen(hackline) + PIPE_GROW;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000735 thepipe.buf = xcalloc(1, thepipe.len);
Eric Andersenb76cb682001-08-22 05:58:16 +0000736 /* buffer magic */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000737 thepipe.buf[thepipe.len - 1] = PIPE_MAGIC;
Eric Andersenb76cb682001-08-22 05:58:16 +0000738
Mark Whitley2dc192f2000-11-03 19:47:00 +0000739 /* and now, as long as we've got a line to try matching and if we can match
740 * the search string, we make substitutions */
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000741 while ((*hackline || !altered)
742 && (regexec(current_regex, hackline, sed_cmd->num_backrefs + 1,
743 regmatch, 0) != REG_NOMATCH)) {
Mark Whitley4f7fe772000-07-13 20:01:58 +0000744 int i;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000745
Mark Whitley2dc192f2000-11-03 19:47:00 +0000746 /* print everything before the match */
747 for (i = 0; i < regmatch[0].rm_so; i++)
Eric Andersenb76cb682001-08-22 05:58:16 +0000748 pipeputc(hackline[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000749
Mark Whitley2dc192f2000-11-03 19:47:00 +0000750 /* then print the substitution string */
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000751 print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch, pipeline,
752 sed_cmd->num_backrefs);
Mark Whitley97562bd2000-07-17 20:06:42 +0000753
Mark Whitley2dc192f2000-11-03 19:47:00 +0000754 /* advance past the match */
755 hackline += regmatch[0].rm_eo;
756 /* flag that something has changed */
757 altered++;
Mark Whitley97562bd2000-07-17 20:06:42 +0000758
Mark Whitley2dc192f2000-11-03 19:47:00 +0000759 /* if we're not doing this globally, get out now */
Glenn L McGrath7ce9e242003-04-07 16:04:14 +0000760 if (!sed_cmd->sub_g) {
Mark Whitley2dc192f2000-11-03 19:47:00 +0000761 break;
Glenn L McGrath7ce9e242003-04-07 16:04:14 +0000762 }
Mark Whitley4f7fe772000-07-13 20:01:58 +0000763 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000764 for (; *hackline; hackline++)
765 pipeputc(*hackline);
766 if (thepipe.buf[thepipe.idx] == PIPE_MAGIC)
767 thepipe.buf[thepipe.idx] = 0;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000768
769 /* cleanup */
770 free(regmatch);
771
Eric Andersenb76cb682001-08-22 05:58:16 +0000772 free(*line);
Eric Andersenc52a6b02001-11-10 10:49:42 +0000773 *line = thepipe.buf;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000774 return altered;
775}
Mark Whitley6315ce62000-07-10 22:55:51 +0000776
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000777static sed_cmd_t *branch_to(const char *label)
778{
779 sed_cmd_t *sed_cmd;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000780
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000781 for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
782 if ((sed_cmd->label) && (strcmp(sed_cmd->label, label) == 0)) {
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000783 break;
784 }
785 }
786
787 /* If no match returns last command */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000788 return (sed_cmd);
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000789}
Mark Whitley6315ce62000-07-10 22:55:51 +0000790
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000791static void process_file(FILE * file)
Mark Whitley6315ce62000-07-10 22:55:51 +0000792{
Glenn L McGrathc6adada2003-04-07 12:24:44 +0000793 char *pattern_space; /* Posix requires it be able to hold at least 8192 bytes */
Glenn L McGrath7ce9e242003-04-07 16:04:14 +0000794 char *hold_space = NULL; /* Posix requires it be able to hold at least 8192 bytes */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000795 static int linenum = 0; /* GNU sed does not restart counting lines at EOF */
Mark Whitley6315ce62000-07-10 22:55:51 +0000796 unsigned int still_in_range = 0;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000797 int altered;
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +0000798 int force_print;
Mark Whitley6315ce62000-07-10 22:55:51 +0000799
Glenn L McGrathc6adada2003-04-07 12:24:44 +0000800 pattern_space = bb_get_chomped_line_from_file(file);
801 if (pattern_space == NULL) {
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000802 return;
803 }
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000804
Mark Whitley6315ce62000-07-10 22:55:51 +0000805 /* go through every line in the file */
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000806 do {
807 char *next_line;
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000808 sed_cmd_t *sed_cmd;
Glenn L McGrath595a6a42003-03-28 08:36:52 +0000809 int substituted = 0;
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000810 /* This enables whole blocks of commands to be mask'ed out if the lead address doesnt match */
811 int block_mask = 1;
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000812
813 /* Read one line in advance so we can act on the last line, the '$' address */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000814 next_line = bb_get_chomped_line_from_file(file);
Mark Whitley6315ce62000-07-10 22:55:51 +0000815
816 linenum++;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000817 altered = 0;
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +0000818 force_print = 0;
Mark Whitley6315ce62000-07-10 22:55:51 +0000819
820 /* for every line, go through all the commands */
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000821 for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
Robert Griebl47abc492002-06-11 23:43:27 +0000822 int deleted = 0;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000823
824 /*
825 * entry point into sedding...
826 */
Robert Griebl47abc492002-06-11 23:43:27 +0000827 int matched = (
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000828 /* no range necessary */
829 (sed_cmd->beg_line == 0 && sed_cmd->end_line == 0
830 && sed_cmd->beg_match == NULL
831 && sed_cmd->end_match == NULL) ||
832 /* this line number is the first address we're looking for */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000833 (sed_cmd->beg_line > 0 && (sed_cmd->beg_line == linenum)) ||
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000834 /* this line matches our first address regex */
835 (sed_cmd->beg_match
836 && (regexec(sed_cmd->beg_match, pattern_space, 0, NULL,
837 0) == 0)) ||
838 /* we are currently within the beginning & ending address range */
839 still_in_range || ((sed_cmd->beg_line == -1)
840 && (next_line == NULL))
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000841 );
Robert Griebl47abc492002-06-11 23:43:27 +0000842
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000843 if (sed_cmd->cmd == '{') {
844 block_mask = block_mask & matched;
845 }
846// matched &= block_mask;
847
848 if (sed_cmd->invert ^ (matched & block_mask)) {
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000849 /* Update last used regex incase a blank substitute BRE is found */
850 if (sed_cmd->beg_match) {
851 previous_regex_ptr = sed_cmd->beg_match;
852 }
Mark Whitley0915c4b2001-06-11 23:50:06 +0000853
854 /*
855 * actual sedding
856 */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000857 switch (sed_cmd->cmd) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000858 case '=':
859 printf("%d\n", linenum);
860 break;
861 case 'P':{
862 /* Write the current pattern space upto the first newline */
863 char *tmp = strchr(pattern_space, '\n');
Mark Whitley0915c4b2001-06-11 23:50:06 +0000864
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000865 if (tmp) {
866 *tmp = '\0';
867 }
868 }
869 case 'p': /* Write the current pattern space to output */
870 puts(pattern_space);
871 break;
872 case 'd':
873 altered++;
874 deleted = 1;
875 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000876
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000877 case 's':
878
879 /*
880 * Some special cases for 's' printing to make it compliant with
881 * GNU sed printing behavior (aka "The -n | s///p Matrix"):
882 *
883 * -n ONLY = never print anything regardless of any successful
884 * substitution
885 *
886 * s///p ONLY = always print successful substitutions, even if
887 * the pattern_space is going to be printed anyway (pattern_space
888 * will be printed twice).
889 *
890 * -n AND s///p = print ONLY a successful substitution ONE TIME;
891 * no other lines are printed - this is the reason why the 'p'
892 * flag exists in the first place.
893 */
Mark Whitley0915c4b2001-06-11 23:50:06 +0000894
Glenn L McGrath0c518322003-03-30 03:41:53 +0000895#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000896 /* HACK: escape newlines twice so regex can match them */
897 {
898 int offset = 0;
899
900 while (strchr(pattern_space + offset, '\n') != NULL) {
901 char *tmp;
902
903 pattern_space =
904 xrealloc(pattern_space,
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000905 strlen(pattern_space) + 2);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000906 tmp = strchr(pattern_space + offset, '\n');
907 memmove(tmp + 1, tmp, strlen(tmp) + 1);
908 tmp[0] = '\\';
909 tmp[1] = 'n';
910 offset = tmp - pattern_space + 2;
911 }
912 }
Glenn L McGrath0c518322003-03-30 03:41:53 +0000913#endif
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000914 /* we print the pattern_space once, unless we were told to be quiet */
915 substituted = do_subst_command(sed_cmd, &pattern_space);
Glenn L McGrath0c518322003-03-30 03:41:53 +0000916
917#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000918 /* undo HACK: escape newlines twice so regex can match them */
919 {
920 char *tmp = pattern_space;
Glenn L McGrath0c518322003-03-30 03:41:53 +0000921
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000922 while ((tmp = strstr(tmp, "\\n")) != NULL) {
923 memmove(tmp, tmp + 1, strlen(tmp + 1) + 1);
924 tmp[0] = '\n';
Glenn L McGrath0c518322003-03-30 03:41:53 +0000925 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000926 }
Glenn L McGrath0c518322003-03-30 03:41:53 +0000927#endif
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000928 altered |= substituted;
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000929 if (!be_quiet && altered && ((sed_cmd->next == NULL)
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000930 || (sed_cmd->next->cmd != 's'))) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000931 force_print = 1;
932 }
Mark Whitley0915c4b2001-06-11 23:50:06 +0000933
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000934 /* we also print the line if we were given the 'p' flag
935 * (this is quite possibly the second printing) */
936 if ((sed_cmd->sub_p) && altered) {
Glenn L McGrathc6adada2003-04-07 12:24:44 +0000937 puts(pattern_space);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000938 }
939 break;
940 case 'a':
941 puts(pattern_space);
942 fputs(sed_cmd->editline, stdout);
943 altered++;
944 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000945
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000946 case 'i':
947 fputs(sed_cmd->editline, stdout);
948 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000949
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000950 case 'c':
951 /* single-address case */
952 if ((sed_cmd->end_match == NULL && sed_cmd->end_line == 0)
Mark Whitley0915c4b2001-06-11 23:50:06 +0000953 /* multi-address case */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000954 /* - matching text */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000955 || (sed_cmd->end_match
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000956 && (regexec(sed_cmd->end_match, pattern_space, 0,
957 NULL, 0) == 0))
Eric Andersenc52a6b02001-11-10 10:49:42 +0000958 /* - matching line numbers */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000959 || (sed_cmd->end_line > 0
960 && sed_cmd->end_line == linenum)) {
961 fputs(sed_cmd->editline, stdout);
962 }
963 altered++;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000964
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000965 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000966
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000967 case 'r':{
968 FILE *outfile;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000969
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000970 outfile = fopen(sed_cmd->filename, "r");
Glenn L McGrathd87a7ac2003-04-09 15:26:14 +0000971 if (outfile) {
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000972 char *line;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000973
974 while ((line =
975 bb_get_chomped_line_from_file(outfile)) !=
976 NULL) {
977 pattern_space =
978 xrealloc(pattern_space,
979 strlen(line) + strlen(pattern_space) + 2);
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000980 strcat(pattern_space, "\n");
981 strcat(pattern_space, line);
982 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000983 bb_xprint_and_close_file(outfile);
Glenn L McGrathd87a7ac2003-04-09 15:26:14 +0000984 }
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000985
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000986 }
987 break;
988 case 'q': /* Branch to end of script and quit */
989 deleted = 1;
990 /* Exit the outer while loop */
991 free(next_line);
992 next_line = NULL;
993 break;
994 case 'n': /* Read next line from input */
995 free(pattern_space);
996 pattern_space = next_line;
997 next_line = bb_get_chomped_line_from_file(file);
998 linenum++;
999 break;
1000 case 'N': /* Append the next line to the current line */
1001 if (next_line) {
1002 pattern_space =
1003 realloc(pattern_space,
Glenn L McGrathd4185b02003-04-11 17:10:23 +00001004 strlen(pattern_space) + strlen(next_line) + 2);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001005 strcat(pattern_space, "\n");
1006 strcat(pattern_space, next_line);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001007 next_line = bb_get_chomped_line_from_file(file);
Glenn L McGrath4157a8a2003-03-10 04:12:35 +00001008 linenum++;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001009 }
1010 break;
1011 case 'b':
1012 sed_cmd = branch_to(sed_cmd->label);
1013 break;
1014 case 't':
1015 if (substituted) {
Glenn L McGrath961c6c12003-03-28 04:43:39 +00001016 sed_cmd = branch_to(sed_cmd->label);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001017 }
1018 break;
1019 case 'y':{
1020 int i;
1021
1022 for (i = 0; pattern_space[i] != 0; i++) {
1023 int j;
1024
1025 for (j = 0; sed_cmd->translate[j]; j += 2) {
1026 if (pattern_space[i] == sed_cmd->translate[j]) {
1027 pattern_space[i] = sed_cmd->translate[j + 1];
Glenn L McGrathf01b46d2003-03-30 08:02:18 +00001028 }
1029 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001030 }
1031 }
1032 break;
1033 case 'g': /* Replace pattern space with hold space */
1034 free(pattern_space);
1035 pattern_space = strdup(hold_space);
1036 break;
Glenn L McGrath91e19782003-04-26 07:40:07 +00001037 case 'G': { /* Append newline and hold space to pattern space */
1038 int pattern_space_size = 0;
1039 if (pattern_space) {
1040 pattern_space_size = strlen(pattern_space);
1041 }
1042 pattern_space = xrealloc(pattern_space, pattern_space_size + strlen(hold_space) + 2);
1043 strcat(pattern_space, "\n");
1044 strcat(pattern_space, hold_space);
1045 break;
1046 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001047 case 'h': /* Replace hold space with pattern space */
1048 free(hold_space);
1049 hold_space = strdup(pattern_space);
1050 break;
Glenn L McGrath91e19782003-04-26 07:40:07 +00001051 case 'H': { /* Append newline and pattern space to hold space */
1052 int hold_space_size = 0;
1053 if (hold_space) {
1054 hold_space_size = strlen(hold_space);
1055 }
1056 hold_space = xrealloc(hold_space, hold_space_size + strlen(pattern_space) + 2);
1057 strcat(hold_space, "\n");
1058 strcat(hold_space, pattern_space);
1059 break;
1060 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001061 case 'x':{
1062 /* Swap hold and pattern space */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +00001063 char *tmp = pattern_space;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001064 pattern_space = hold_space;
1065 hold_space = tmp;
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +00001066 break;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001067 }
Mark Whitley0915c4b2001-06-11 23:50:06 +00001068 }
Robert Griebl47abc492002-06-11 23:43:27 +00001069 }
Mark Whitley0915c4b2001-06-11 23:50:06 +00001070
Robert Griebl47abc492002-06-11 23:43:27 +00001071 /*
1072 * exit point from sedding...
1073 */
1074 if (matched) {
Mark Whitley0915c4b2001-06-11 23:50:06 +00001075 if (
Glenn L McGrathd4185b02003-04-11 17:10:23 +00001076 /* this is a single-address command or... */
1077 (sed_cmd->end_line == 0 && sed_cmd->end_match == NULL)
1078 /* If only one address */
1079 /* we were in the middle of our address range (this
1080 * isn't the first time through) and.. */
1081 || ((still_in_range == 1)
1082 /* this line number is the last address we're looking for or... */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +00001083 && ((sed_cmd->end_line > 0
Glenn L McGrathd4185b02003-04-11 17:10:23 +00001084 && (sed_cmd->end_line == linenum))
1085 /* this line matches our last address regex */
1086 || (sed_cmd->end_match
1087 && (regexec(sed_cmd->end_match, pattern_space,
1088 0, NULL, 0) == 0))))) {
Mark Whitley0915c4b2001-06-11 23:50:06 +00001089 /* we're out of our address range */
1090 still_in_range = 0;
Glenn L McGrathd4185b02003-04-11 17:10:23 +00001091 } else {
1092 /* didn't hit the exit? then we're still in the middle of an address range */
Mark Whitley0915c4b2001-06-11 23:50:06 +00001093 still_in_range = 1;
Mark Whitley6315ce62000-07-10 22:55:51 +00001094 }
1095 }
Robert Griebl47abc492002-06-11 23:43:27 +00001096
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +00001097 if (sed_cmd->cmd == '}') {
1098 block_mask = 1;
1099 }
1100
Robert Griebl47abc492002-06-11 23:43:27 +00001101 if (deleted)
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001102 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001103 }
Erik Andersen1266a131999-12-29 22:19:46 +00001104
Mark Whitley2dc192f2000-11-03 19:47:00 +00001105 /* we will print the line unless we were told to be quiet or if the
1106 * line was altered (via a 'd'elete or 's'ubstitution), in which case
1107 * the altered line was already printed */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001108 if ((!be_quiet && !altered) || force_print) {
Glenn L McGrathc6adada2003-04-07 12:24:44 +00001109 puts(pattern_space);
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +00001110 }
Glenn L McGrathc6adada2003-04-07 12:24:44 +00001111 free(pattern_space);
1112 pattern_space = next_line;
1113 } while (pattern_space);
Erik Andersen1266a131999-12-29 22:19:46 +00001114}
1115
1116extern int sed_main(int argc, char **argv)
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001117{
Matt Kraaia5f09c62001-11-12 16:44:55 +00001118 int opt, status = EXIT_SUCCESS;
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 */
Matt Kraaia9819b22000-12-22 01:48:07 +00001122 if (atexit(destroy_cmd_strs) == -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
Mark Whitley6315ce62000-07-10 22:55:51 +00001126 /* do normal option parsing */
Eric Andersenb50da532001-02-17 16:52:35 +00001127 while ((opt = getopt(argc, argv, "ne:f:")) > 0) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001128 switch (opt) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001129 case 'n':
1130 be_quiet++;
1131 break;
1132 case 'e':{
1133 char *str_cmd = strdup(optarg);
1134
1135 add_cmd_str(str_cmd);
1136 free(str_cmd);
1137 break;
1138 }
1139 case 'f':
1140 load_cmd_file(optarg);
1141 break;
1142 default:
1143 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001144 }
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001145 }
Mark Whitley6315ce62000-07-10 22:55:51 +00001146
1147 /* if we didn't get a pattern from a -e and no command file was specified,
1148 * argv[optind] should be the pattern. no pattern, no worky */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +00001149 if (sed_cmd_head.next == NULL) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001150 if (argv[optind] == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001151 bb_show_usage();
Mark Whitley6315ce62000-07-10 22:55:51 +00001152 else {
Glenn L McGrath0c518322003-03-30 03:41:53 +00001153 char *str_cmd = strdup(argv[optind]);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001154
Glenn L McGrath0c518322003-03-30 03:41:53 +00001155 add_cmd_str(strdup(str_cmd));
1156 free(str_cmd);
Mark Whitley6315ce62000-07-10 22:55:51 +00001157 optind++;
1158 }
1159 }
1160
Mark Whitley6315ce62000-07-10 22:55:51 +00001161 /* argv[(optind)..(argc-1)] should be names of file to process. If no
1162 * files were specified or '-' was specified, take input from stdin.
1163 * Otherwise, we process all the files specified. */
1164 if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) {
1165 process_file(stdin);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001166 } else {
Mark Whitley6315ce62000-07-10 22:55:51 +00001167 int i;
1168 FILE *file;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001169
Mark Whitley6315ce62000-07-10 22:55:51 +00001170 for (i = optind; i < argc; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001171 file = bb_wfopen(argv[i], "r");
Eric Andersen9c6b5fc2001-11-17 07:23:46 +00001172 if (file) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001173 process_file(file);
1174 fclose(file);
Matt Kraaia5f09c62001-11-12 16:44:55 +00001175 } else
1176 status = EXIT_FAILURE;
Mark Whitley6315ce62000-07-10 22:55:51 +00001177 }
1178 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001179
Matt Kraaia5f09c62001-11-12 16:44:55 +00001180 return status;
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001181}