blob: aa3ce024ec8063fcce3c90f89c0df10b44cc7ac6 [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
Glenn L McGrathf36635c2003-09-13 15:12:22 +000046 - and more.
Glenn L McGrathd5eadea2003-03-09 02:39:29 +000047
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000048 Bugs:
49
Glenn L McGrathf36635c2003-09-13 15:12:22 +000050 - lots
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000051
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 */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +000076
Glenn L McGrathc18ce372003-09-13 06:57:39 +000077 /* Runtime flag no not if the current command match's */
78 int still_in_range;
79
Mark Whitley2dc192f2000-11-03 19:47:00 +000080 /* SUBSTITUTION COMMAND SPECIFIC FIELDS */
81
82 /* sed -e 's/sub_match/replace/' */
83 regex_t *sub_match;
84 char *replace;
Eric Andersenc52a6b02001-11-10 10:49:42 +000085
86 /* EDIT COMMAND (a,i,c) SPECIFIC FIELDS */
87 char *editline;
88
89 /* FILE COMMAND (r) SPECIFIC FIELDS */
90 char *filename;
91
Eric Andersenc52a6b02001-11-10 10:49:42 +000092 /* SUBSTITUTION COMMAND SPECIFIC FIELDS */
93
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000094 unsigned int num_backrefs:4; /* how many back references (\1..\9) */
95 /* Note: GNU/POSIX sed does not save more than nine backrefs, so
96 * we only use 4 bits to hold the number */
97 unsigned int sub_g:1; /* sed -e 's/foo/bar/g' (global) */
98 unsigned int sub_p:1; /* sed -e 's/foo/bar/p' (print substitution) */
Mark Whitley94074a92000-07-14 00:00:15 +000099
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000100 /* TRANSLATE COMMAND */
101 char *translate;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000102
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000103 /* GENERAL FIELDS */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000104 /* the command */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000105 char cmd; /* p,d,s (add more at your leisure :-) */
Robert Griebl47abc492002-06-11 23:43:27 +0000106
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000107 /* Branch commands */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000108 char *label;
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000109
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000110 /* next command in list (sequential list of specified commands) */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000111 struct sed_cmd_s *next;
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000112
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +0000113} sed_cmd_t;
Mark Whitley6315ce62000-07-10 22:55:51 +0000114
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000115
116/* externs */
117extern void xregcomp(regex_t * preg, const char *regex, int cflags);
118extern int optind; /* in unistd.h */
119extern char *optarg; /* ditto */
120
Mark Whitley6315ce62000-07-10 22:55:51 +0000121/* globals */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000122/* options */
123static int be_quiet = 0;
124static const char bad_format_in_subst[] =
125 "bad format in substitution expression";
126
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000127/* linked list of sed commands */
128static sed_cmd_t sed_cmd_head;
129static sed_cmd_t *sed_cmd_tail = &sed_cmd_head;
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000130
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000131const char *const semicolon_whitespace = "; \n\r\t\v\0";
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000132static regex_t *previous_regex_ptr = NULL;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000133
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000134
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000135#ifdef CONFIG_FEATURE_CLEAN_UP
Matt Kraai0c390a72001-11-20 16:00:19 +0000136static void destroy_cmd_strs(void)
Mark Whitley6315ce62000-07-10 22:55:51 +0000137{
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000138 sed_cmd_t *sed_cmd = sed_cmd_head.next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000139
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000140 while (sed_cmd) {
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000141 sed_cmd_t *sed_cmd_next = sed_cmd->next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000142
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000143 if (sed_cmd->beg_match) {
144 regfree(sed_cmd->beg_match);
145 free(sed_cmd->beg_match);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000146 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000147 if (sed_cmd->end_match) {
148 regfree(sed_cmd->end_match);
149 free(sed_cmd->end_match);
Mark Whitley6315ce62000-07-10 22:55:51 +0000150 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000151 if (sed_cmd->sub_match) {
152 regfree(sed_cmd->sub_match);
153 free(sed_cmd->sub_match);
Mark Whitley6315ce62000-07-10 22:55:51 +0000154 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000155 free(sed_cmd->replace);
156 free(sed_cmd);
157 sed_cmd = sed_cmd_next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000158 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000159}
Mark Whitleyc41e8c82000-07-12 23:35:21 +0000160#endif
Mark Whitley6315ce62000-07-10 22:55:51 +0000161
Mark Whitley6315ce62000-07-10 22:55:51 +0000162/*
Eric Andersen28b3c532001-01-02 11:01:31 +0000163 * index_of_next_unescaped_regexp_delim - walks left to right through a string
164 * beginning at a specified index and returns the index of the next regular
165 * expression delimiter (typically a forward * slash ('/')) not preceeded by
166 * a backslash ('\').
Mark Whitley6315ce62000-07-10 22:55:51 +0000167 */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000168static int index_of_next_unescaped_regexp_delim(const char delimiter,
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000169 const char *str)
Mark Whitley6315ce62000-07-10 22:55:51 +0000170{
Matt Kraaia0065d52001-08-24 14:45:50 +0000171 int bracket = -1;
172 int escaped = 0;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000173 int idx = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000174 char ch;
Matt Kraaia0065d52001-08-24 14:45:50 +0000175
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000176 for (; (ch = str[idx]); idx++) {
Matt Kraaia0065d52001-08-24 14:45:50 +0000177 if (bracket != -1) {
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000178 if (ch == ']' && !(bracket == idx - 1 || (bracket == idx - 2
179 && str[idx - 1] == '^')))
Matt Kraaia0065d52001-08-24 14:45:50 +0000180 bracket = -1;
181 } else if (escaped)
182 escaped = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000183 else if (ch == '\\')
Matt Kraaia0065d52001-08-24 14:45:50 +0000184 escaped = 1;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000185 else if (ch == '[')
Matt Kraaia0065d52001-08-24 14:45:50 +0000186 bracket = idx;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000187 else if (ch == delimiter)
Mark Whitley97562bd2000-07-17 20:06:42 +0000188 return idx;
189 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000190
Mark Whitley97562bd2000-07-17 20:06:42 +0000191 /* if we make it to here, we've hit the end of the string */
192 return -1;
Mark Whitley6315ce62000-07-10 22:55:51 +0000193}
194
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000195/*
196 * Returns the index of the third delimiter
197 */
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000198static int parse_regex_delim(const char *cmdstr, char **match, char **replace)
199{
200 const char *cmdstr_ptr = cmdstr;
201 char delimiter;
202 int idx = 0;
203
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000204 /* verify that the 's' or 'y' is followed by something. That something
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000205 * (typically a 'slash') is now our regexp delimiter... */
206 if (*cmdstr == '\0')
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000207 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000208 else
209 delimiter = *cmdstr_ptr;
210
211 cmdstr_ptr++;
212
213 /* save the match string */
214 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
215 if (idx == -1) {
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000216 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000217 }
218 *match = bb_xstrndup(cmdstr_ptr, idx);
219
220 /* save the replacement string */
221 cmdstr_ptr += idx + 1;
222 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
223 if (idx == -1) {
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000224 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000225 }
226 *replace = bb_xstrndup(cmdstr_ptr, idx);
227
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000228 return ((cmdstr_ptr - cmdstr) + idx);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000229}
230
Mark Whitley6315ce62000-07-10 22:55:51 +0000231/*
232 * returns the index in the string just past where the address ends.
233 */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000234static int get_address(char *my_str, int *linenum, regex_t ** regex)
Mark Whitley6315ce62000-07-10 22:55:51 +0000235{
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000236 char *pos=my_str;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000237
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000238 if (isdigit(*my_str)) {
239 *linenum = strtol(my_str, &pos, 10);
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000240 /* endstr shouldnt ever equal NULL */
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000241 } else if (*my_str == '$') {
Mark Whitley0915c4b2001-06-11 23:50:06 +0000242 *linenum = -1;
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000243 pos++;
244 } else if (*my_str == '/' || *my_str == '\\') {
245 int next, idx_start = 1;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000246 char delimiter;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000247
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000248 delimiter = '/';
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000249 if (*my_str == '\\') {
Robert Griebl00f5ecb2002-08-06 23:13:31 +0000250 idx_start++;
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000251 delimiter = *(++pos);
Robert Griebl79401472002-08-06 21:07:17 +0000252 }
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000253 next = index_of_next_unescaped_regexp_delim(delimiter, ++pos);
254 if (next == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000255 bb_error_msg_and_die("unterminated match expression");
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000256 }
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000257 pos += next;
258 *pos = '\0';
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000259
260 *regex = (regex_t *) xmalloc(sizeof(regex_t));
261 xregcomp(*regex, my_str + idx_start, REG_NEWLINE);
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000262 pos++; /* so it points to the next character after the last '/' */
Mark Whitley6315ce62000-07-10 22:55:51 +0000263 }
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000264 return pos - my_str;
Mark Whitley6315ce62000-07-10 22:55:51 +0000265}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +0000267static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr)
Mark Whitley06f35292000-07-13 19:58:04 +0000268{
Glenn L McGrathe01f9662003-03-18 08:37:57 +0000269 int cflags = 0;
Mark Whitley06f35292000-07-13 19:58:04 +0000270 char *match;
271 int idx = 0;
Mark Whitley97562bd2000-07-17 20:06:42 +0000272 int j;
Mark Whitley06f35292000-07-13 19:58:04 +0000273
274 /*
275 * the string that gets passed to this function should look like this:
Mark Whitley0e4cec02000-08-21 21:29:20 +0000276 * s/match/replace/gIp
277 * || | |||
Mark Whitley06f35292000-07-13 19:58:04 +0000278 * mandatory optional
279 *
280 * (all three of the '/' slashes are mandatory)
281 */
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000282 idx = parse_regex_delim(substr, &match, &sed_cmd->replace);
Mark Whitley06f35292000-07-13 19:58:04 +0000283
Mark Whitley97562bd2000-07-17 20:06:42 +0000284 /* determine the number of back references in the match string */
285 /* Note: we compute this here rather than in the do_subst_command()
286 * function to save processor time, at the expense of a little more memory
287 * (4 bits) per sed_cmd */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000288
Mark Whitley97562bd2000-07-17 20:06:42 +0000289 for (j = 0; match[j]; j++) {
290 /* GNU/POSIX sed does not save more than nine backrefs */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000291 if (match[j] == '\\' && match[j + 1] == '('
292 && sed_cmd->num_backrefs <= 9)
Mark Whitley97562bd2000-07-17 20:06:42 +0000293 sed_cmd->num_backrefs++;
294 }
295
Mark Whitley06f35292000-07-13 19:58:04 +0000296 /* process the flags */
297 while (substr[++idx]) {
298 switch (substr[idx]) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000299 case 'g':
Glenn L McGrathf36635c2003-09-13 15:12:22 +0000300 if (match[0] != '^') {
301 sed_cmd->sub_g = 1;
302 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000303 break;
Glenn L McGrathbed40332003-03-10 02:21:14 +0000304 /* Hmm, i dont see the I option mentioned in the standard */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000305 case 'I':
306 cflags |= REG_ICASE;
307 break;
308 case 'p':
309 sed_cmd->sub_p = 1;
310 break;
311 default:
312 /* any whitespace or semicolon trailing after a s/// is ok */
313 if (strchr(semicolon_whitespace, substr[idx]))
314 goto out;
315 /* else */
316 bb_error_msg_and_die("bad option in substitution expression");
Mark Whitley06f35292000-07-13 19:58:04 +0000317 }
318 }
Mark Whitley70705d72000-07-14 19:06:30 +0000319
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000320 out:
Mark Whitley97562bd2000-07-17 20:06:42 +0000321 /* compile the match string into a regex */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000322 if (*match != '\0') {
323 /* If match is empty, we use last regex used at runtime */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000324 sed_cmd->sub_match = (regex_t *) xmalloc(sizeof(regex_t));
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000325 xregcomp(sed_cmd->sub_match, match, cflags);
326 }
Mark Whitley06f35292000-07-13 19:58:04 +0000327 free(match);
Mark Whitley70705d72000-07-14 19:06:30 +0000328
329 return idx;
Mark Whitley06f35292000-07-13 19:58:04 +0000330}
331
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000332static void replace_slash_n(char *string)
333{
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000334 char *dest;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000335
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000336 for (dest = string; *string; string++, dest++) {
337 if ((string[0] == '\\') && (string[1] == 'n')) {
338 *dest = '\n';
339 string++;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000340 } else {
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000341 *dest = *string;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000342 }
343 }
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000344 *dest=0;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000345}
346
347static int parse_translate_cmd(sed_cmd_t * const sed_cmd, const char *cmdstr)
348{
349 char *match;
350 char *replace;
351 int idx;
352 int i;
353
354 idx = parse_regex_delim(cmdstr, &match, &replace);
355 replace_slash_n(match);
356 replace_slash_n(replace);
357 sed_cmd->translate = xcalloc(1, (strlen(match) + 1) * 2);
358 for (i = 0; (match[i] != 0) && (replace[i] != 0); i++) {
359 sed_cmd->translate[i * 2] = match[i];
360 sed_cmd->translate[(i * 2) + 1] = replace[i];
361 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000362 return (idx + 1);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000363}
364
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000365static int parse_edit_cmd(sed_cmd_t * sed_cmd, const char *editstr)
Mark Whitley94074a92000-07-14 00:00:15 +0000366{
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000367 int i, j;
Mark Whitley94074a92000-07-14 00:00:15 +0000368
369 /*
370 * the string that gets passed to this function should look like this:
371 *
372 * need one of these
373 * |
374 * | this backslash (immediately following the edit command) is mandatory
375 * | |
376 * [aic]\
377 * TEXT1\
378 * TEXT2\
379 * TEXTN
380 *
381 * as soon as we hit a TEXT line that has no trailing '\', we're done.
382 * this means a command like:
383 *
384 * i\
385 * INSERTME
386 *
387 * is a-ok.
388 *
389 */
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000390 if ((*editstr != '\\') || ((editstr[1] != '\n') && (editstr[1] != '\r'))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000391 bb_error_msg_and_die("bad format in edit expression");
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000392 }
Mark Whitley94074a92000-07-14 00:00:15 +0000393
394 /* store the edit line text */
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000395 sed_cmd->editline = xmalloc(strlen(&editstr[2]) + 2);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000396 for (i = 2, j = 0;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000397 editstr[i] != '\0' && strchr("\r\n", editstr[i]) == NULL; i++, j++) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000398 if ((editstr[i] == '\\') && strchr("\n\r", editstr[i + 1]) != NULL) {
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000399 sed_cmd->editline[j] = '\n';
400 i++;
401 } else
402 sed_cmd->editline[j] = editstr[i];
Mark Whitley94074a92000-07-14 00:00:15 +0000403 }
Mark Whitley70705d72000-07-14 19:06:30 +0000404
Mark Whitley56c14a62001-04-20 23:41:44 +0000405 /* figure out if we need to add a newline */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000406 if (sed_cmd->editline[j - 1] != '\n')
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000407 sed_cmd->editline[j++] = '\n';
Mark Whitley56c14a62001-04-20 23:41:44 +0000408
409 /* terminate string */
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000410 sed_cmd->editline[j] = '\0';
Mark Whitley464c5de2000-07-14 23:24:00 +0000411
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000412 return i;
Mark Whitley94074a92000-07-14 00:00:15 +0000413}
414
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000415
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000416static int parse_file_cmd(sed_cmd_t * sed_cmd, const char *filecmdstr)
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000417{
418 int idx = 0;
419 int filenamelen = 0;
420
421 /*
422 * the string that gets passed to this function should look like this:
423 * '[ ]filename'
424 * | |
425 * | a filename
426 * |
427 * optional whitespace
428
429 * re: the file to be read, the GNU manual says the following: "Note that
430 * if filename cannot be read, it is treated as if it were an empty file,
431 * without any error indication." Thus, all of the following commands are
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000432 * perfectly legal:
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000433 *
434 * sed -e '1r noexist'
435 * sed -e '1r ;'
436 * sed -e '1r'
437 */
438
439 /* the file command may be followed by whitespace; move past it. */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000440 while (isspace(filecmdstr[++idx])) {;
441 }
442
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000443 /* the first non-whitespace we get is a filename. the filename ends when we
444 * hit a normal sed command terminator or end of string */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000445 filenamelen = strcspn(&filecmdstr[idx], semicolon_whitespace);
Matt Kraai6e9e1362001-05-27 14:11:52 +0000446 sed_cmd->filename = xmalloc(filenamelen + 1);
447 safe_strncpy(sed_cmd->filename, &filecmdstr[idx], filenamelen + 1);
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000448 return idx + filenamelen;
449}
450
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000451/*
452 * Process the commands arguments
453 */
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000454static char *parse_cmd_str(sed_cmd_t * sed_cmd, char *cmdstr)
Mark Whitley6315ce62000-07-10 22:55:51 +0000455{
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000456 /* handle (s)ubstitution command */
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000457 if (sed_cmd->cmd == 's') {
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000458 cmdstr += parse_subst_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000459 }
460 /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */
461 else if (strchr("aic", sed_cmd->cmd)) {
462 if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000463 bb_error_msg_and_die
464 ("only a beginning address can be specified for edit commands");
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000465 cmdstr += parse_edit_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000466 }
467 /* handle file cmds: (r)ead */
468 else if (sed_cmd->cmd == 'r') {
469 if (sed_cmd->end_line || sed_cmd->end_match)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000470 bb_error_msg_and_die("Command only uses one address");
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000471 cmdstr += parse_file_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000472 }
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000473 /* handle branch commands */
Glenn L McGrath595a6a42003-03-28 08:36:52 +0000474 else if (strchr(":bt", sed_cmd->cmd)) {
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000475 int length;
476
477 cmdstr += strspn(cmdstr, " ");
Glenn L McGrathf4523562003-09-14 06:01:14 +0000478 length = strcspn(cmdstr, semicolon_whitespace);
479 if (length) {
480 sed_cmd->label = strndup(cmdstr, length);
481 cmdstr += length;
482 }
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000483 }
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000484 /* translation command */
485 else if (sed_cmd->cmd == 'y') {
486 cmdstr += parse_translate_cmd(sed_cmd, cmdstr);
487 }
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000488 /* if it wasnt a single-letter command that takes no arguments
489 * then it must be an invalid command.
490 */
Glenn L McGrath91e19782003-04-26 07:40:07 +0000491 else if (strchr("dgGhHnNpPqx={}", sed_cmd->cmd) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000492 bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd);
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000493 }
Mark Whitley70705d72000-07-14 19:06:30 +0000494
495 /* give back whatever's left over */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000496 return (cmdstr);
Eric Andersen50d63601999-11-09 01:47:36 +0000497}
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000498
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000499static char *add_cmd(char *cmdstr)
Erik Andersen1266a131999-12-29 22:19:46 +0000500{
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000501 sed_cmd_t *sed_cmd;
502
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000503 /* Skip over leading whitespace and semicolons */
504 cmdstr += strspn(cmdstr, semicolon_whitespace);
Erik Andersen1266a131999-12-29 22:19:46 +0000505
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000506 /* if we ate the whole thing, that means there was just trailing
507 * whitespace or a final / no-op semicolon. either way, get out */
508 if (*cmdstr == '\0') {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000509 return (NULL);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000510 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000511
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000512 /* if this is a comment, jump past it and keep going */
513 if (*cmdstr == '#') {
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000514 /* "#n" is the same as using -n on the command line */
515 if (cmdstr[1] == 'n') {
516 be_quiet++;
517 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000518 return (strpbrk(cmdstr, "\n\r"));
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000519 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000520
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000521 /* parse the command
522 * format is: [addr][,addr]cmd
523 * |----||-----||-|
524 * part1 part2 part3
525 */
526
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000527 sed_cmd = xcalloc(1, sizeof(sed_cmd_t));
528
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000529 /* first part (if present) is an address: either a '$', a number or a /regex/ */
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000530 cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000531
532 /* second part (if present) will begin with a comma */
533 if (*cmdstr == ',') {
534 int idx;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000535
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000536 cmdstr++;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000537 idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000538 if (idx == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000539 bb_error_msg_and_die("get_address: no address found in string\n"
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000540 "\t(you probably didn't check the string you passed me)");
Mark Whitley70705d72000-07-14 19:06:30 +0000541 }
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000542 cmdstr += idx;
543 }
Mark Whitley70705d72000-07-14 19:06:30 +0000544
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000545 /* skip whitespace before the command */
546 while (isspace(*cmdstr)) {
547 cmdstr++;
548 }
549
550 /* there my be the inversion flag between part2 and part3 */
551 if (*cmdstr == '!') {
552 sed_cmd->invert = 1;
553 cmdstr++;
554
555#ifdef SED_FEATURE_STRICT_CHECKING
556 /* According to the spec
557 * It is unspecified whether <blank>s can follow a '!' character,
558 * and conforming applications shall not follow a '!' character
559 * with <blank>s.
560 */
561 if (isblank(cmdstr[idx]) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000562 bb_error_msg_and_die("blank follows '!'");}
563#else
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000564 /* skip whitespace before the command */
565 while (isspace(*cmdstr)) {
566 cmdstr++;
567 }
568#endif
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000569 }
570
571 /* last part (mandatory) will be a command */
572 if (*cmdstr == '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000573 bb_error_msg_and_die("missing command");
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000574
575 sed_cmd->cmd = *cmdstr;
576 cmdstr++;
577
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000578 cmdstr = parse_cmd_str(sed_cmd, cmdstr);
579
580 /* Add the command to the command array */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000581 sed_cmd_tail->next = sed_cmd;
582 sed_cmd_tail = sed_cmd_tail->next;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000583
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000584 return (cmdstr);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000585}
586
587static void add_cmd_str(char *cmdstr)
588{
Glenn L McGrath0c518322003-03-30 03:41:53 +0000589#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
590 char *cmdstr_ptr = cmdstr;
591
592 /* HACK: convert "\n" to match tranlated '\n' string */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000593 while ((cmdstr_ptr = strstr(cmdstr_ptr, "\\n")) != NULL) {
Glenn L McGrath0c518322003-03-30 03:41:53 +0000594 cmdstr = xrealloc(cmdstr, strlen(cmdstr) + 2);
595 cmdstr_ptr = strstr(cmdstr, "\\n");
596 memmove(cmdstr_ptr + 1, cmdstr_ptr, strlen(cmdstr_ptr) + 1);
597 cmdstr_ptr[0] = '\\';
598 cmdstr_ptr += 3;
599 }
600#endif
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000601 do {
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000602 cmdstr = add_cmd(cmdstr);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000603 } while (cmdstr && strlen(cmdstr));
Mark Whitley6315ce62000-07-10 22:55:51 +0000604}
605
606
607static void load_cmd_file(char *filename)
608{
609 FILE *cmdfile;
610 char *line;
Mark Whitley94074a92000-07-14 00:00:15 +0000611 char *nextline;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000612 char *e;
Mark Whitley6315ce62000-07-10 22:55:51 +0000613
Manuel Novoa III cad53642003-03-19 09:13:01 +0000614 cmdfile = bb_xfopen(filename, "r");
Mark Whitley6315ce62000-07-10 22:55:51 +0000615
Manuel Novoa III cad53642003-03-19 09:13:01 +0000616 while ((line = bb_get_line_from_file(cmdfile)) != NULL) {
Mark Whitley94074a92000-07-14 00:00:15 +0000617 /* if a line ends with '\' it needs the next line appended to it */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000618 while (((e = last_char_is(line, '\n')) != NULL)
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000619 && (e > line) && (e[-1] == '\\')
620 && ((nextline = bb_get_line_from_file(cmdfile)) != NULL)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000621 line = xrealloc(line, (e - line) + 1 + strlen(nextline) + 1);
Mark Whitley94074a92000-07-14 00:00:15 +0000622 strcat(line, nextline);
Mark Whitley464c5de2000-07-14 23:24:00 +0000623 free(nextline);
Mark Whitley94074a92000-07-14 00:00:15 +0000624 }
Mark Whitley464c5de2000-07-14 23:24:00 +0000625 /* eat trailing newline (if any) --if I don't do this, edit commands
626 * (aic) will print an extra newline */
Matt Kraai05e782d2001-02-01 16:49:30 +0000627 chomp(line);
Mark Whitley6315ce62000-07-10 22:55:51 +0000628 add_cmd_str(line);
629 free(line);
630 }
631}
632
Eric Andersenc52a6b02001-11-10 10:49:42 +0000633struct pipeline {
634 char *buf;
635 int idx;
636 int len;
637};
638
Eric Andersenb76cb682001-08-22 05:58:16 +0000639#define PIPE_MAGIC 0x7f
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000640#define PIPE_GROW 64
Eric Andersenc52a6b02001-11-10 10:49:42 +0000641
642void pipe_putc(struct pipeline *const pipeline, char c)
643{
644 if (pipeline->buf[pipeline->idx] == PIPE_MAGIC) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000645 pipeline->buf = xrealloc(pipeline->buf, pipeline->len + PIPE_GROW);
Eric Andersenc52a6b02001-11-10 10:49:42 +0000646 memset(pipeline->buf + pipeline->len, 0, PIPE_GROW);
647 pipeline->len += PIPE_GROW;
648 pipeline->buf[pipeline->len - 1] = PIPE_MAGIC;
649 }
650 pipeline->buf[pipeline->idx++] = (c);
651}
652
653#define pipeputc(c) pipe_putc(pipeline, c)
654
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000655static void print_subst_w_backrefs(const char *line, const char *replace,
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000656 regmatch_t * regmatch, struct pipeline *const pipeline, int matches)
Mark Whitley97562bd2000-07-17 20:06:42 +0000657{
658 int i;
659
660 /* go through the replacement string */
661 for (i = 0; replace[i]; i++) {
662 /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000663 if (replace[i] == '\\' && isdigit(replace[i + 1])) {
Mark Whitley97562bd2000-07-17 20:06:42 +0000664 int j;
665 char tmpstr[2];
666 int backref;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000667
668 ++i; /* i now indexes the backref number, instead of the leading slash */
Mark Whitley97562bd2000-07-17 20:06:42 +0000669 tmpstr[0] = replace[i];
670 tmpstr[1] = 0;
671 backref = atoi(tmpstr);
672 /* print out the text held in regmatch[backref] */
Matt Kraaia3e4f452001-08-20 21:21:06 +0000673 if (backref <= matches && regmatch[backref].rm_so != -1)
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000674 for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000675 j++)
Eric Andersenb76cb682001-08-22 05:58:16 +0000676 pipeputc(line[j]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000677 }
678
Mark Whitley83e85f62000-07-25 20:48:44 +0000679 /* if we find a backslash escaped character, print the character */
680 else if (replace[i] == '\\') {
681 ++i;
Eric Andersenb76cb682001-08-22 05:58:16 +0000682 pipeputc(replace[i]);
Mark Whitley83e85f62000-07-25 20:48:44 +0000683 }
684
Mark Whitley97562bd2000-07-17 20:06:42 +0000685 /* if we find an unescaped '&' print out the whole matched text.
686 * fortunately, regmatch[0] contains the indicies to the whole matched
687 * expression (kinda seems like it was designed for just such a
688 * purpose...) */
Glenn L McGrath24103862003-04-09 07:51:43 +0000689 else if (replace[i] == '&') {
Mark Whitley97562bd2000-07-17 20:06:42 +0000690 int j;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000691
Mark Whitley97562bd2000-07-17 20:06:42 +0000692 for (j = regmatch[0].rm_so; j < regmatch[0].rm_eo; j++)
Eric Andersenb76cb682001-08-22 05:58:16 +0000693 pipeputc(line[j]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000694 }
695 /* nothing special, just print this char of the replacement string to stdout */
696 else
Eric Andersenb76cb682001-08-22 05:58:16 +0000697 pipeputc(replace[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000698 }
699}
700
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000701static int do_subst_command(sed_cmd_t * sed_cmd, char **line)
Mark Whitley4f7fe772000-07-13 20:01:58 +0000702{
Eric Andersenb76cb682001-08-22 05:58:16 +0000703 char *hackline = *line;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000704 struct pipeline thepipe = { NULL, 0, 0 };
Eric Andersenc52a6b02001-11-10 10:49:42 +0000705 struct pipeline *const pipeline = &thepipe;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000706 int altered = 0;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000707 int result;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000708 regmatch_t *regmatch = NULL;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000709 regex_t *current_regex;
710
711 if (sed_cmd->sub_match == NULL) {
712 current_regex = previous_regex_ptr;
713 } else {
714 previous_regex_ptr = current_regex = sed_cmd->sub_match;
715 }
716 result = regexec(current_regex, hackline, 0, NULL, 0);
Mark Whitley4f7fe772000-07-13 20:01:58 +0000717
Mark Whitley2dc192f2000-11-03 19:47:00 +0000718 /* we only proceed if the substitution 'search' expression matches */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000719 if (result == REG_NOMATCH) {
Mark Whitley2dc192f2000-11-03 19:47:00 +0000720 return 0;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000721 }
Mark Whitley2dc192f2000-11-03 19:47:00 +0000722
723 /* whaddaya know, it matched. get the number of back references */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000724 regmatch = xmalloc(sizeof(regmatch_t) * (sed_cmd->num_backrefs + 1));
Mark Whitley2dc192f2000-11-03 19:47:00 +0000725
Eric Andersenb76cb682001-08-22 05:58:16 +0000726 /* allocate more PIPE_GROW bytes
727 if replaced string is larger than original */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000728 thepipe.len = strlen(hackline) + PIPE_GROW;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000729 thepipe.buf = xcalloc(1, thepipe.len);
Eric Andersenb76cb682001-08-22 05:58:16 +0000730 /* buffer magic */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000731 thepipe.buf[thepipe.len - 1] = PIPE_MAGIC;
Eric Andersenb76cb682001-08-22 05:58:16 +0000732
Mark Whitley2dc192f2000-11-03 19:47:00 +0000733 /* and now, as long as we've got a line to try matching and if we can match
734 * the search string, we make substitutions */
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000735 while ((*hackline || !altered)
736 && (regexec(current_regex, hackline, sed_cmd->num_backrefs + 1,
737 regmatch, 0) != REG_NOMATCH)) {
Mark Whitley4f7fe772000-07-13 20:01:58 +0000738 int i;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000739
Mark Whitley2dc192f2000-11-03 19:47:00 +0000740 /* print everything before the match */
741 for (i = 0; i < regmatch[0].rm_so; i++)
Eric Andersenb76cb682001-08-22 05:58:16 +0000742 pipeputc(hackline[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000743
Mark Whitley2dc192f2000-11-03 19:47:00 +0000744 /* then print the substitution string */
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000745 print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch, pipeline,
746 sed_cmd->num_backrefs);
Mark Whitley97562bd2000-07-17 20:06:42 +0000747
Mark Whitley2dc192f2000-11-03 19:47:00 +0000748 /* advance past the match */
749 hackline += regmatch[0].rm_eo;
750 /* flag that something has changed */
751 altered++;
Mark Whitley97562bd2000-07-17 20:06:42 +0000752
Mark Whitley2dc192f2000-11-03 19:47:00 +0000753 /* if we're not doing this globally, get out now */
Glenn L McGrath7ce9e242003-04-07 16:04:14 +0000754 if (!sed_cmd->sub_g) {
Mark Whitley2dc192f2000-11-03 19:47:00 +0000755 break;
Glenn L McGrath7ce9e242003-04-07 16:04:14 +0000756 }
Mark Whitley4f7fe772000-07-13 20:01:58 +0000757 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000758 for (; *hackline; hackline++)
759 pipeputc(*hackline);
760 if (thepipe.buf[thepipe.idx] == PIPE_MAGIC)
761 thepipe.buf[thepipe.idx] = 0;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000762
763 /* cleanup */
764 free(regmatch);
765
Eric Andersenb76cb682001-08-22 05:58:16 +0000766 free(*line);
Eric Andersenc52a6b02001-11-10 10:49:42 +0000767 *line = thepipe.buf;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000768 return altered;
769}
Mark Whitley6315ce62000-07-10 22:55:51 +0000770
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000771static sed_cmd_t *branch_to(const char *label)
772{
773 sed_cmd_t *sed_cmd;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000774
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000775 for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
Glenn L McGrathf4523562003-09-14 06:01:14 +0000776 if ((sed_cmd->cmd == ':') && (sed_cmd->label) && (strcmp(sed_cmd->label, label) == 0)) {
777 return (sed_cmd);
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000778 }
779 }
Glenn L McGrathf4523562003-09-14 06:01:14 +0000780 bb_error_msg_and_die("Can't find label for jump to `%s'", label);
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000781}
Mark Whitley6315ce62000-07-10 22:55:51 +0000782
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000783static void process_file(FILE * file)
Mark Whitley6315ce62000-07-10 22:55:51 +0000784{
Glenn L McGrathc6adada2003-04-07 12:24:44 +0000785 char *pattern_space; /* Posix requires it be able to hold at least 8192 bytes */
Glenn L McGrath7ce9e242003-04-07 16:04:14 +0000786 char *hold_space = NULL; /* Posix requires it be able to hold at least 8192 bytes */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000787 static int linenum = 0; /* GNU sed does not restart counting lines at EOF */
Mark Whitley0915c4b2001-06-11 23:50:06 +0000788 int altered;
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +0000789 int force_print;
Mark Whitley6315ce62000-07-10 22:55:51 +0000790
Glenn L McGrathc6adada2003-04-07 12:24:44 +0000791 pattern_space = bb_get_chomped_line_from_file(file);
792 if (pattern_space == NULL) {
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000793 return;
794 }
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000795
Mark Whitley6315ce62000-07-10 22:55:51 +0000796 /* go through every line in the file */
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000797 do {
798 char *next_line;
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000799 sed_cmd_t *sed_cmd;
Glenn L McGrath595a6a42003-03-28 08:36:52 +0000800 int substituted = 0;
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000801 /* This enables whole blocks of commands to be mask'ed out if the lead address doesnt match */
802 int block_mask = 1;
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000803
804 /* Read one line in advance so we can act on the last line, the '$' address */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000805 next_line = bb_get_chomped_line_from_file(file);
Mark Whitley6315ce62000-07-10 22:55:51 +0000806
807 linenum++;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000808 altered = 0;
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +0000809 force_print = 0;
Mark Whitley6315ce62000-07-10 22:55:51 +0000810
811 /* for every line, go through all the commands */
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000812 for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
Robert Griebl47abc492002-06-11 23:43:27 +0000813 int deleted = 0;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000814
815 /*
816 * entry point into sedding...
817 */
Robert Griebl47abc492002-06-11 23:43:27 +0000818 int matched = (
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000819 /* no range necessary */
820 (sed_cmd->beg_line == 0 && sed_cmd->end_line == 0
821 && sed_cmd->beg_match == NULL
822 && sed_cmd->end_match == NULL) ||
823 /* this line number is the first address we're looking for */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000824 (sed_cmd->beg_line > 0 && (sed_cmd->beg_line == linenum)) ||
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000825 /* this line matches our first address regex */
826 (sed_cmd->beg_match
827 && (regexec(sed_cmd->beg_match, pattern_space, 0, NULL,
828 0) == 0)) ||
829 /* we are currently within the beginning & ending address range */
Glenn L McGrathc18ce372003-09-13 06:57:39 +0000830 sed_cmd->still_in_range || ((sed_cmd->beg_line == -1)
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000831 && (next_line == NULL))
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000832 );
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000833 if (sed_cmd->cmd == '{') {
834 block_mask = block_mask & matched;
835 }
836// matched &= block_mask;
837
838 if (sed_cmd->invert ^ (matched & block_mask)) {
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000839 /* Update last used regex incase a blank substitute BRE is found */
840 if (sed_cmd->beg_match) {
841 previous_regex_ptr = sed_cmd->beg_match;
842 }
Mark Whitley0915c4b2001-06-11 23:50:06 +0000843
844 /*
845 * actual sedding
846 */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000847 switch (sed_cmd->cmd) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000848 case '=':
849 printf("%d\n", linenum);
850 break;
851 case 'P':{
852 /* Write the current pattern space upto the first newline */
853 char *tmp = strchr(pattern_space, '\n');
Mark Whitley0915c4b2001-06-11 23:50:06 +0000854
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000855 if (tmp) {
856 *tmp = '\0';
857 }
858 }
859 case 'p': /* Write the current pattern space to output */
860 puts(pattern_space);
861 break;
862 case 'd':
863 altered++;
864 deleted = 1;
865 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000866
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000867 case 's':
868
869 /*
870 * Some special cases for 's' printing to make it compliant with
871 * GNU sed printing behavior (aka "The -n | s///p Matrix"):
872 *
873 * -n ONLY = never print anything regardless of any successful
874 * substitution
875 *
876 * s///p ONLY = always print successful substitutions, even if
877 * the pattern_space is going to be printed anyway (pattern_space
878 * will be printed twice).
879 *
880 * -n AND s///p = print ONLY a successful substitution ONE TIME;
881 * no other lines are printed - this is the reason why the 'p'
882 * flag exists in the first place.
883 */
Mark Whitley0915c4b2001-06-11 23:50:06 +0000884
Glenn L McGrath0c518322003-03-30 03:41:53 +0000885#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000886 /* HACK: escape newlines twice so regex can match them */
887 {
888 int offset = 0;
889
890 while (strchr(pattern_space + offset, '\n') != NULL) {
891 char *tmp;
892
893 pattern_space =
894 xrealloc(pattern_space,
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000895 strlen(pattern_space) + 2);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000896 tmp = strchr(pattern_space + offset, '\n');
897 memmove(tmp + 1, tmp, strlen(tmp) + 1);
898 tmp[0] = '\\';
899 tmp[1] = 'n';
900 offset = tmp - pattern_space + 2;
901 }
902 }
Glenn L McGrath0c518322003-03-30 03:41:53 +0000903#endif
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000904 /* we print the pattern_space once, unless we were told to be quiet */
905 substituted = do_subst_command(sed_cmd, &pattern_space);
Glenn L McGrath0c518322003-03-30 03:41:53 +0000906
907#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000908 /* undo HACK: escape newlines twice so regex can match them */
909 {
910 char *tmp = pattern_space;
Glenn L McGrath0c518322003-03-30 03:41:53 +0000911
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000912 while ((tmp = strstr(tmp, "\\n")) != NULL) {
913 memmove(tmp, tmp + 1, strlen(tmp + 1) + 1);
914 tmp[0] = '\n';
Glenn L McGrath0c518322003-03-30 03:41:53 +0000915 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000916 }
Glenn L McGrath0c518322003-03-30 03:41:53 +0000917#endif
Glenn L McGrath9b04f182003-08-30 04:35:07 +0000918 altered = substituted;
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000919 if (!be_quiet && altered && ((sed_cmd->next == NULL)
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000920 || (sed_cmd->next->cmd != 's'))) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000921 force_print = 1;
922 }
Mark Whitley0915c4b2001-06-11 23:50:06 +0000923
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000924 /* we also print the line if we were given the 'p' flag
925 * (this is quite possibly the second printing) */
926 if ((sed_cmd->sub_p) && altered) {
Glenn L McGrathc6adada2003-04-07 12:24:44 +0000927 puts(pattern_space);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000928 }
929 break;
930 case 'a':
931 puts(pattern_space);
932 fputs(sed_cmd->editline, stdout);
933 altered++;
934 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000935
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000936 case 'i':
937 fputs(sed_cmd->editline, stdout);
938 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000939
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000940 case 'c':
941 /* single-address case */
942 if ((sed_cmd->end_match == NULL && sed_cmd->end_line == 0)
Mark Whitley0915c4b2001-06-11 23:50:06 +0000943 /* multi-address case */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000944 /* - matching text */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000945 || (sed_cmd->end_match
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000946 && (regexec(sed_cmd->end_match, pattern_space, 0,
947 NULL, 0) == 0))
Eric Andersenc52a6b02001-11-10 10:49:42 +0000948 /* - matching line numbers */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000949 || (sed_cmd->end_line > 0
950 && sed_cmd->end_line == linenum)) {
951 fputs(sed_cmd->editline, stdout);
952 }
953 altered++;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000954
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000955 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000956
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000957 case 'r':{
958 FILE *outfile;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000959
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000960 outfile = fopen(sed_cmd->filename, "r");
Glenn L McGrathd87a7ac2003-04-09 15:26:14 +0000961 if (outfile) {
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000962 char *line;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000963
964 while ((line =
965 bb_get_chomped_line_from_file(outfile)) !=
966 NULL) {
967 pattern_space =
968 xrealloc(pattern_space,
969 strlen(line) + strlen(pattern_space) + 2);
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000970 strcat(pattern_space, "\n");
971 strcat(pattern_space, line);
972 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000973 bb_xprint_and_close_file(outfile);
Glenn L McGrathd87a7ac2003-04-09 15:26:14 +0000974 }
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000975
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000976 }
977 break;
978 case 'q': /* Branch to end of script and quit */
979 deleted = 1;
980 /* Exit the outer while loop */
981 free(next_line);
982 next_line = NULL;
983 break;
984 case 'n': /* Read next line from input */
985 free(pattern_space);
986 pattern_space = next_line;
987 next_line = bb_get_chomped_line_from_file(file);
988 linenum++;
989 break;
990 case 'N': /* Append the next line to the current line */
991 if (next_line) {
992 pattern_space =
993 realloc(pattern_space,
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000994 strlen(pattern_space) + strlen(next_line) + 2);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000995 strcat(pattern_space, "\n");
996 strcat(pattern_space, next_line);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000997 next_line = bb_get_chomped_line_from_file(file);
Glenn L McGrath4157a8a2003-03-10 04:12:35 +0000998 linenum++;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000999 }
1000 break;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001001 case 't':
Glenn L McGrathf4523562003-09-14 06:01:14 +00001002 if (substituted)
1003 /* Fall through */
1004 case 'b':
1005 {
1006 if (sed_cmd->label == NULL) {
1007 /* Jump to end of script */
1008 deleted = 1;
1009 } else {
1010 sed_cmd = branch_to(sed_cmd->label);
1011 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001012 }
1013 break;
1014 case 'y':{
1015 int i;
1016
1017 for (i = 0; pattern_space[i] != 0; i++) {
1018 int j;
1019
1020 for (j = 0; sed_cmd->translate[j]; j += 2) {
1021 if (pattern_space[i] == sed_cmd->translate[j]) {
1022 pattern_space[i] = sed_cmd->translate[j + 1];
Glenn L McGrathf01b46d2003-03-30 08:02:18 +00001023 }
1024 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001025 }
1026 }
1027 break;
1028 case 'g': /* Replace pattern space with hold space */
1029 free(pattern_space);
1030 pattern_space = strdup(hold_space);
1031 break;
Glenn L McGrath91e19782003-04-26 07:40:07 +00001032 case 'G': { /* Append newline and hold space to pattern space */
1033 int pattern_space_size = 0;
1034 if (pattern_space) {
1035 pattern_space_size = strlen(pattern_space);
1036 }
1037 pattern_space = xrealloc(pattern_space, pattern_space_size + strlen(hold_space) + 2);
1038 strcat(pattern_space, "\n");
1039 strcat(pattern_space, hold_space);
1040 break;
1041 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001042 case 'h': /* Replace hold space with pattern space */
1043 free(hold_space);
1044 hold_space = strdup(pattern_space);
1045 break;
Glenn L McGrath91e19782003-04-26 07:40:07 +00001046 case 'H': { /* Append newline and pattern space to hold space */
1047 int hold_space_size = 0;
1048 if (hold_space) {
1049 hold_space_size = strlen(hold_space);
1050 }
1051 hold_space = xrealloc(hold_space, hold_space_size + strlen(pattern_space) + 2);
Glenn L McGrath4dc1d252003-09-14 01:25:31 +00001052 if (hold_space_size) {
1053 strcat(hold_space, "\n");
1054 } else {
1055 hold_space[0] = '\n';
1056 }
Glenn L McGrath91e19782003-04-26 07:40:07 +00001057 strcat(hold_space, pattern_space);
1058 break;
1059 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001060 case 'x':{
1061 /* Swap hold and pattern space */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +00001062 char *tmp = pattern_space;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001063 pattern_space = hold_space;
1064 hold_space = tmp;
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +00001065 break;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001066 }
Mark Whitley0915c4b2001-06-11 23:50:06 +00001067 }
Robert Griebl47abc492002-06-11 23:43:27 +00001068 }
Mark Whitley0915c4b2001-06-11 23:50:06 +00001069
Robert Griebl47abc492002-06-11 23:43:27 +00001070 /*
1071 * exit point from sedding...
1072 */
1073 if (matched) {
Mark Whitley0915c4b2001-06-11 23:50:06 +00001074 if (
Glenn L McGrathd4185b02003-04-11 17:10:23 +00001075 /* this is a single-address command or... */
1076 (sed_cmd->end_line == 0 && sed_cmd->end_match == NULL)
1077 /* If only one address */
1078 /* we were in the middle of our address range (this
1079 * isn't the first time through) and.. */
Glenn L McGrathc18ce372003-09-13 06:57:39 +00001080 || ((sed_cmd->still_in_range == 1)
Glenn L McGrathd4185b02003-04-11 17:10:23 +00001081 /* this line number is the last address we're looking for or... */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +00001082 && ((sed_cmd->end_line > 0
Glenn L McGrathd4185b02003-04-11 17:10:23 +00001083 && (sed_cmd->end_line == linenum))
1084 /* this line matches our last address regex */
1085 || (sed_cmd->end_match
1086 && (regexec(sed_cmd->end_match, pattern_space,
1087 0, NULL, 0) == 0))))) {
Mark Whitley0915c4b2001-06-11 23:50:06 +00001088 /* we're out of our address range */
Glenn L McGrathc18ce372003-09-13 06:57:39 +00001089 sed_cmd->still_in_range = 0;
Glenn L McGrathd4185b02003-04-11 17:10:23 +00001090 } else {
1091 /* didn't hit the exit? then we're still in the middle of an address range */
Glenn L McGrathc18ce372003-09-13 06:57:39 +00001092 sed_cmd->still_in_range = 1;
Mark Whitley6315ce62000-07-10 22:55:51 +00001093 }
1094 }
Robert Griebl47abc492002-06-11 23:43:27 +00001095
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +00001096 if (sed_cmd->cmd == '}') {
1097 block_mask = 1;
1098 }
1099
Robert Griebl47abc492002-06-11 23:43:27 +00001100 if (deleted)
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001101 break;
Glenn L McGrath4dc1d252003-09-14 01:25:31 +00001102
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();
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001152 else
1153 add_cmd_str(strdup(argv[optind++]));
Mark Whitley6315ce62000-07-10 22:55:51 +00001154 }
1155
Mark Whitley6315ce62000-07-10 22:55:51 +00001156 /* argv[(optind)..(argc-1)] should be names of file to process. If no
1157 * files were specified or '-' was specified, take input from stdin.
1158 * Otherwise, we process all the files specified. */
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001159 if (argv[optind] == NULL) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001160 process_file(stdin);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001161 } else {
Mark Whitley6315ce62000-07-10 22:55:51 +00001162 int i;
1163 FILE *file;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001164
Mark Whitley6315ce62000-07-10 22:55:51 +00001165 for (i = optind; i < argc; i++) {
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001166 if(!strcmp(argv[i], "-")) {
1167 process_file(stdin);
1168 } else {
1169 file = bb_wfopen(argv[i], "r");
1170 if (file) {
1171 process_file(file);
1172 fclose(file);
1173 } else {
1174 status = EXIT_FAILURE;
1175 }
1176 }
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}