blob: 00ed208920e971959d60728dd72c087a008a1680 [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);
Glenn L McGrath8417c8c2003-09-14 15:24:18 +0000156 free(sed_cmd->editline);
157 free(sed_cmd->filename);
158 free(sed_cmd->translate);
159 free(sed_cmd->label);
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000160 free(sed_cmd);
161 sed_cmd = sed_cmd_next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000162 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000163}
Mark Whitleyc41e8c82000-07-12 23:35:21 +0000164#endif
Mark Whitley6315ce62000-07-10 22:55:51 +0000165
Mark Whitley6315ce62000-07-10 22:55:51 +0000166/*
Eric Andersen28b3c532001-01-02 11:01:31 +0000167 * index_of_next_unescaped_regexp_delim - walks left to right through a string
168 * beginning at a specified index and returns the index of the next regular
169 * expression delimiter (typically a forward * slash ('/')) not preceeded by
170 * a backslash ('\').
Mark Whitley6315ce62000-07-10 22:55:51 +0000171 */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000172static int index_of_next_unescaped_regexp_delim(const char delimiter,
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000173 const char *str)
Mark Whitley6315ce62000-07-10 22:55:51 +0000174{
Matt Kraaia0065d52001-08-24 14:45:50 +0000175 int bracket = -1;
176 int escaped = 0;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000177 int idx = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000178 char ch;
Matt Kraaia0065d52001-08-24 14:45:50 +0000179
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000180 for (; (ch = str[idx]); idx++) {
Matt Kraaia0065d52001-08-24 14:45:50 +0000181 if (bracket != -1) {
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000182 if (ch == ']' && !(bracket == idx - 1 || (bracket == idx - 2
183 && str[idx - 1] == '^')))
Matt Kraaia0065d52001-08-24 14:45:50 +0000184 bracket = -1;
185 } else if (escaped)
186 escaped = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000187 else if (ch == '\\')
Matt Kraaia0065d52001-08-24 14:45:50 +0000188 escaped = 1;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000189 else if (ch == '[')
Matt Kraaia0065d52001-08-24 14:45:50 +0000190 bracket = idx;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000191 else if (ch == delimiter)
Mark Whitley97562bd2000-07-17 20:06:42 +0000192 return idx;
193 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000194
Mark Whitley97562bd2000-07-17 20:06:42 +0000195 /* if we make it to here, we've hit the end of the string */
196 return -1;
Mark Whitley6315ce62000-07-10 22:55:51 +0000197}
198
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000199/*
200 * Returns the index of the third delimiter
201 */
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000202static int parse_regex_delim(const char *cmdstr, char **match, char **replace)
203{
204 const char *cmdstr_ptr = cmdstr;
205 char delimiter;
206 int idx = 0;
207
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000208 /* verify that the 's' or 'y' is followed by something. That something
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000209 * (typically a 'slash') is now our regexp delimiter... */
210 if (*cmdstr == '\0')
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 else
213 delimiter = *cmdstr_ptr;
214
215 cmdstr_ptr++;
216
217 /* save the match string */
218 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
219 if (idx == -1) {
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000220 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000221 }
222 *match = bb_xstrndup(cmdstr_ptr, idx);
223
224 /* save the replacement string */
225 cmdstr_ptr += idx + 1;
226 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
227 if (idx == -1) {
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000228 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000229 }
230 *replace = bb_xstrndup(cmdstr_ptr, idx);
231
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000232 return ((cmdstr_ptr - cmdstr) + idx);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000233}
234
Mark Whitley6315ce62000-07-10 22:55:51 +0000235/*
236 * returns the index in the string just past where the address ends.
237 */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000238static int get_address(char *my_str, int *linenum, regex_t ** regex)
Mark Whitley6315ce62000-07-10 22:55:51 +0000239{
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000240 char *pos=my_str;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000241
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000242 if (isdigit(*my_str)) {
243 *linenum = strtol(my_str, &pos, 10);
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000244 /* endstr shouldnt ever equal NULL */
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000245 } else if (*my_str == '$') {
Mark Whitley0915c4b2001-06-11 23:50:06 +0000246 *linenum = -1;
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000247 pos++;
248 } else if (*my_str == '/' || *my_str == '\\') {
249 int next, idx_start = 1;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000250 char delimiter;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000251
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000252 delimiter = '/';
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000253 if (*my_str == '\\') {
Robert Griebl00f5ecb2002-08-06 23:13:31 +0000254 idx_start++;
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000255 delimiter = *(++pos);
Robert Griebl79401472002-08-06 21:07:17 +0000256 }
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000257 next = index_of_next_unescaped_regexp_delim(delimiter, ++pos);
258 if (next == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000259 bb_error_msg_and_die("unterminated match expression");
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000260 }
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000261 pos += next;
262 *pos = '\0';
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000263
264 *regex = (regex_t *) xmalloc(sizeof(regex_t));
265 xregcomp(*regex, my_str + idx_start, REG_NEWLINE);
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000266 pos++; /* so it points to the next character after the last '/' */
Mark Whitley6315ce62000-07-10 22:55:51 +0000267 }
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000268 return pos - my_str;
Mark Whitley6315ce62000-07-10 22:55:51 +0000269}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000270
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +0000271static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr)
Mark Whitley06f35292000-07-13 19:58:04 +0000272{
Glenn L McGrathe01f9662003-03-18 08:37:57 +0000273 int cflags = 0;
Mark Whitley06f35292000-07-13 19:58:04 +0000274 char *match;
275 int idx = 0;
Mark Whitley97562bd2000-07-17 20:06:42 +0000276 int j;
Mark Whitley06f35292000-07-13 19:58:04 +0000277
278 /*
279 * the string that gets passed to this function should look like this:
Mark Whitley0e4cec02000-08-21 21:29:20 +0000280 * s/match/replace/gIp
281 * || | |||
Mark Whitley06f35292000-07-13 19:58:04 +0000282 * mandatory optional
283 *
284 * (all three of the '/' slashes are mandatory)
285 */
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000286 idx = parse_regex_delim(substr, &match, &sed_cmd->replace);
Mark Whitley06f35292000-07-13 19:58:04 +0000287
Mark Whitley97562bd2000-07-17 20:06:42 +0000288 /* determine the number of back references in the match string */
289 /* Note: we compute this here rather than in the do_subst_command()
290 * function to save processor time, at the expense of a little more memory
291 * (4 bits) per sed_cmd */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000292
Mark Whitley97562bd2000-07-17 20:06:42 +0000293 for (j = 0; match[j]; j++) {
294 /* GNU/POSIX sed does not save more than nine backrefs */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000295 if (match[j] == '\\' && match[j + 1] == '('
296 && sed_cmd->num_backrefs <= 9)
Mark Whitley97562bd2000-07-17 20:06:42 +0000297 sed_cmd->num_backrefs++;
298 }
299
Mark Whitley06f35292000-07-13 19:58:04 +0000300 /* process the flags */
301 while (substr[++idx]) {
302 switch (substr[idx]) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000303 case 'g':
Glenn L McGrathf36635c2003-09-13 15:12:22 +0000304 if (match[0] != '^') {
305 sed_cmd->sub_g = 1;
306 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000307 break;
Glenn L McGrathbed40332003-03-10 02:21:14 +0000308 /* Hmm, i dont see the I option mentioned in the standard */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000309 case 'I':
310 cflags |= REG_ICASE;
311 break;
312 case 'p':
313 sed_cmd->sub_p = 1;
314 break;
315 default:
316 /* any whitespace or semicolon trailing after a s/// is ok */
317 if (strchr(semicolon_whitespace, substr[idx]))
318 goto out;
319 /* else */
320 bb_error_msg_and_die("bad option in substitution expression");
Mark Whitley06f35292000-07-13 19:58:04 +0000321 }
322 }
Mark Whitley70705d72000-07-14 19:06:30 +0000323
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000324 out:
Mark Whitley97562bd2000-07-17 20:06:42 +0000325 /* compile the match string into a regex */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000326 if (*match != '\0') {
327 /* If match is empty, we use last regex used at runtime */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000328 sed_cmd->sub_match = (regex_t *) xmalloc(sizeof(regex_t));
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000329 xregcomp(sed_cmd->sub_match, match, cflags);
330 }
Mark Whitley06f35292000-07-13 19:58:04 +0000331 free(match);
Mark Whitley70705d72000-07-14 19:06:30 +0000332
333 return idx;
Mark Whitley06f35292000-07-13 19:58:04 +0000334}
335
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000336static void replace_slash_n(char *string)
337{
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000338 char *dest;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000339
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000340 for (dest = string; *string; string++, dest++) {
341 if ((string[0] == '\\') && (string[1] == 'n')) {
342 *dest = '\n';
343 string++;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000344 } else {
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000345 *dest = *string;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000346 }
347 }
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000348 *dest=0;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000349}
350
351static int parse_translate_cmd(sed_cmd_t * const sed_cmd, const char *cmdstr)
352{
353 char *match;
354 char *replace;
355 int idx;
356 int i;
357
358 idx = parse_regex_delim(cmdstr, &match, &replace);
359 replace_slash_n(match);
360 replace_slash_n(replace);
361 sed_cmd->translate = xcalloc(1, (strlen(match) + 1) * 2);
362 for (i = 0; (match[i] != 0) && (replace[i] != 0); i++) {
363 sed_cmd->translate[i * 2] = match[i];
364 sed_cmd->translate[(i * 2) + 1] = replace[i];
365 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000366 return (idx + 1);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000367}
368
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000369static int parse_edit_cmd(sed_cmd_t * sed_cmd, const char *editstr)
Mark Whitley94074a92000-07-14 00:00:15 +0000370{
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000371 int i, j;
Mark Whitley94074a92000-07-14 00:00:15 +0000372
373 /*
374 * the string that gets passed to this function should look like this:
375 *
376 * need one of these
377 * |
378 * | this backslash (immediately following the edit command) is mandatory
379 * | |
380 * [aic]\
381 * TEXT1\
382 * TEXT2\
383 * TEXTN
384 *
385 * as soon as we hit a TEXT line that has no trailing '\', we're done.
386 * this means a command like:
387 *
388 * i\
389 * INSERTME
390 *
391 * is a-ok.
392 *
393 */
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000394 if ((*editstr != '\\') || ((editstr[1] != '\n') && (editstr[1] != '\r'))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000395 bb_error_msg_and_die("bad format in edit expression");
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000396 }
Mark Whitley94074a92000-07-14 00:00:15 +0000397
398 /* store the edit line text */
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000399 sed_cmd->editline = xmalloc(strlen(&editstr[2]) + 2);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000400 for (i = 2, j = 0;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000401 editstr[i] != '\0' && strchr("\r\n", editstr[i]) == NULL; i++, j++) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000402 if ((editstr[i] == '\\') && strchr("\n\r", editstr[i + 1]) != NULL) {
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000403 sed_cmd->editline[j] = '\n';
404 i++;
405 } else
406 sed_cmd->editline[j] = editstr[i];
Mark Whitley94074a92000-07-14 00:00:15 +0000407 }
Mark Whitley70705d72000-07-14 19:06:30 +0000408
Mark Whitley56c14a62001-04-20 23:41:44 +0000409 /* figure out if we need to add a newline */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000410 if (sed_cmd->editline[j - 1] != '\n')
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000411 sed_cmd->editline[j++] = '\n';
Mark Whitley56c14a62001-04-20 23:41:44 +0000412
413 /* terminate string */
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000414 sed_cmd->editline[j] = '\0';
Mark Whitley464c5de2000-07-14 23:24:00 +0000415
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000416 return i;
Mark Whitley94074a92000-07-14 00:00:15 +0000417}
418
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000419
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000420static int parse_file_cmd(sed_cmd_t * sed_cmd, const char *filecmdstr)
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000421{
422 int idx = 0;
423 int filenamelen = 0;
424
425 /*
426 * the string that gets passed to this function should look like this:
427 * '[ ]filename'
428 * | |
429 * | a filename
430 * |
431 * optional whitespace
432
433 * re: the file to be read, the GNU manual says the following: "Note that
434 * if filename cannot be read, it is treated as if it were an empty file,
435 * without any error indication." Thus, all of the following commands are
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000436 * perfectly legal:
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000437 *
438 * sed -e '1r noexist'
439 * sed -e '1r ;'
440 * sed -e '1r'
441 */
442
443 /* the file command may be followed by whitespace; move past it. */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000444 while (isspace(filecmdstr[++idx])) {;
445 }
446
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000447 /* the first non-whitespace we get is a filename. the filename ends when we
448 * hit a normal sed command terminator or end of string */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000449 filenamelen = strcspn(&filecmdstr[idx], semicolon_whitespace);
Matt Kraai6e9e1362001-05-27 14:11:52 +0000450 sed_cmd->filename = xmalloc(filenamelen + 1);
451 safe_strncpy(sed_cmd->filename, &filecmdstr[idx], filenamelen + 1);
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000452 return idx + filenamelen;
453}
454
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000455/*
456 * Process the commands arguments
457 */
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000458static char *parse_cmd_str(sed_cmd_t * sed_cmd, char *cmdstr)
Mark Whitley6315ce62000-07-10 22:55:51 +0000459{
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000460 /* handle (s)ubstitution command */
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000461 if (sed_cmd->cmd == 's') {
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000462 cmdstr += parse_subst_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000463 }
464 /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */
465 else if (strchr("aic", sed_cmd->cmd)) {
466 if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000467 bb_error_msg_and_die
468 ("only a beginning address can be specified for edit commands");
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000469 cmdstr += parse_edit_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000470 }
471 /* handle file cmds: (r)ead */
472 else if (sed_cmd->cmd == 'r') {
473 if (sed_cmd->end_line || sed_cmd->end_match)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000474 bb_error_msg_and_die("Command only uses one address");
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000475 cmdstr += parse_file_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000476 }
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000477 /* handle branch commands */
Glenn L McGrath595a6a42003-03-28 08:36:52 +0000478 else if (strchr(":bt", sed_cmd->cmd)) {
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000479 int length;
480
481 cmdstr += strspn(cmdstr, " ");
Glenn L McGrathf4523562003-09-14 06:01:14 +0000482 length = strcspn(cmdstr, semicolon_whitespace);
483 if (length) {
484 sed_cmd->label = strndup(cmdstr, length);
485 cmdstr += length;
486 }
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000487 }
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000488 /* translation command */
489 else if (sed_cmd->cmd == 'y') {
490 cmdstr += parse_translate_cmd(sed_cmd, cmdstr);
491 }
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000492 /* if it wasnt a single-letter command that takes no arguments
493 * then it must be an invalid command.
494 */
Glenn L McGrath91e19782003-04-26 07:40:07 +0000495 else if (strchr("dgGhHnNpPqx={}", sed_cmd->cmd) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000496 bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd);
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000497 }
Mark Whitley70705d72000-07-14 19:06:30 +0000498
499 /* give back whatever's left over */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000500 return (cmdstr);
Eric Andersen50d63601999-11-09 01:47:36 +0000501}
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000502
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000503static char *add_cmd(char *cmdstr)
Erik Andersen1266a131999-12-29 22:19:46 +0000504{
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000505 sed_cmd_t *sed_cmd;
506
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000507 /* Skip over leading whitespace and semicolons */
508 cmdstr += strspn(cmdstr, semicolon_whitespace);
Erik Andersen1266a131999-12-29 22:19:46 +0000509
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000510 /* if we ate the whole thing, that means there was just trailing
511 * whitespace or a final / no-op semicolon. either way, get out */
512 if (*cmdstr == '\0') {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000513 return (NULL);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000514 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000515
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000516 /* if this is a comment, jump past it and keep going */
517 if (*cmdstr == '#') {
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000518 /* "#n" is the same as using -n on the command line */
519 if (cmdstr[1] == 'n') {
520 be_quiet++;
521 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000522 return (strpbrk(cmdstr, "\n\r"));
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000523 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000524
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000525 /* parse the command
526 * format is: [addr][,addr]cmd
527 * |----||-----||-|
528 * part1 part2 part3
529 */
530
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000531 sed_cmd = xcalloc(1, sizeof(sed_cmd_t));
532
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000533 /* first part (if present) is an address: either a '$', a number or a /regex/ */
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000534 cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000535
536 /* second part (if present) will begin with a comma */
537 if (*cmdstr == ',') {
538 int idx;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000539
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000540 cmdstr++;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000541 idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000542 if (idx == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000543 bb_error_msg_and_die("get_address: no address found in string\n"
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000544 "\t(you probably didn't check the string you passed me)");
Mark Whitley70705d72000-07-14 19:06:30 +0000545 }
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000546 cmdstr += idx;
547 }
Mark Whitley70705d72000-07-14 19:06:30 +0000548
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000549 /* skip whitespace before the command */
550 while (isspace(*cmdstr)) {
551 cmdstr++;
552 }
553
554 /* there my be the inversion flag between part2 and part3 */
555 if (*cmdstr == '!') {
556 sed_cmd->invert = 1;
557 cmdstr++;
558
559#ifdef SED_FEATURE_STRICT_CHECKING
560 /* According to the spec
561 * It is unspecified whether <blank>s can follow a '!' character,
562 * and conforming applications shall not follow a '!' character
563 * with <blank>s.
564 */
565 if (isblank(cmdstr[idx]) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000566 bb_error_msg_and_die("blank follows '!'");}
567#else
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000568 /* skip whitespace before the command */
569 while (isspace(*cmdstr)) {
570 cmdstr++;
571 }
572#endif
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000573 }
574
575 /* last part (mandatory) will be a command */
576 if (*cmdstr == '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000577 bb_error_msg_and_die("missing command");
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000578
579 sed_cmd->cmd = *cmdstr;
580 cmdstr++;
581
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000582 cmdstr = parse_cmd_str(sed_cmd, cmdstr);
583
584 /* Add the command to the command array */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000585 sed_cmd_tail->next = sed_cmd;
586 sed_cmd_tail = sed_cmd_tail->next;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000587
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000588 return (cmdstr);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000589}
590
591static void add_cmd_str(char *cmdstr)
592{
Glenn L McGrath0c518322003-03-30 03:41:53 +0000593#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
594 char *cmdstr_ptr = cmdstr;
595
596 /* HACK: convert "\n" to match tranlated '\n' string */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000597 while ((cmdstr_ptr = strstr(cmdstr_ptr, "\\n")) != NULL) {
Glenn L McGrath0c518322003-03-30 03:41:53 +0000598 cmdstr = xrealloc(cmdstr, strlen(cmdstr) + 2);
599 cmdstr_ptr = strstr(cmdstr, "\\n");
600 memmove(cmdstr_ptr + 1, cmdstr_ptr, strlen(cmdstr_ptr) + 1);
601 cmdstr_ptr[0] = '\\';
602 cmdstr_ptr += 3;
603 }
604#endif
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000605 do {
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000606 cmdstr = add_cmd(cmdstr);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000607 } while (cmdstr && strlen(cmdstr));
Mark Whitley6315ce62000-07-10 22:55:51 +0000608}
609
610
611static void load_cmd_file(char *filename)
612{
613 FILE *cmdfile;
614 char *line;
Mark Whitley94074a92000-07-14 00:00:15 +0000615 char *nextline;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000616 char *e;
Mark Whitley6315ce62000-07-10 22:55:51 +0000617
Manuel Novoa III cad53642003-03-19 09:13:01 +0000618 cmdfile = bb_xfopen(filename, "r");
Mark Whitley6315ce62000-07-10 22:55:51 +0000619
Manuel Novoa III cad53642003-03-19 09:13:01 +0000620 while ((line = bb_get_line_from_file(cmdfile)) != NULL) {
Mark Whitley94074a92000-07-14 00:00:15 +0000621 /* if a line ends with '\' it needs the next line appended to it */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000622 while (((e = last_char_is(line, '\n')) != NULL)
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000623 && (e > line) && (e[-1] == '\\')
624 && ((nextline = bb_get_line_from_file(cmdfile)) != NULL)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000625 line = xrealloc(line, (e - line) + 1 + strlen(nextline) + 1);
Mark Whitley94074a92000-07-14 00:00:15 +0000626 strcat(line, nextline);
Mark Whitley464c5de2000-07-14 23:24:00 +0000627 free(nextline);
Mark Whitley94074a92000-07-14 00:00:15 +0000628 }
Mark Whitley464c5de2000-07-14 23:24:00 +0000629 /* eat trailing newline (if any) --if I don't do this, edit commands
630 * (aic) will print an extra newline */
Matt Kraai05e782d2001-02-01 16:49:30 +0000631 chomp(line);
Mark Whitley6315ce62000-07-10 22:55:51 +0000632 add_cmd_str(line);
633 free(line);
634 }
635}
636
Eric Andersenc52a6b02001-11-10 10:49:42 +0000637struct pipeline {
638 char *buf;
639 int idx;
640 int len;
641};
642
Eric Andersenb76cb682001-08-22 05:58:16 +0000643#define PIPE_MAGIC 0x7f
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000644#define PIPE_GROW 64
Eric Andersenc52a6b02001-11-10 10:49:42 +0000645
646void pipe_putc(struct pipeline *const pipeline, char c)
647{
648 if (pipeline->buf[pipeline->idx] == PIPE_MAGIC) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000649 pipeline->buf = xrealloc(pipeline->buf, pipeline->len + PIPE_GROW);
Eric Andersenc52a6b02001-11-10 10:49:42 +0000650 memset(pipeline->buf + pipeline->len, 0, PIPE_GROW);
651 pipeline->len += PIPE_GROW;
652 pipeline->buf[pipeline->len - 1] = PIPE_MAGIC;
653 }
654 pipeline->buf[pipeline->idx++] = (c);
655}
656
657#define pipeputc(c) pipe_putc(pipeline, c)
658
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000659static void print_subst_w_backrefs(const char *line, const char *replace,
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000660 regmatch_t * regmatch, struct pipeline *const pipeline, int matches)
Mark Whitley97562bd2000-07-17 20:06:42 +0000661{
662 int i;
663
664 /* go through the replacement string */
665 for (i = 0; replace[i]; i++) {
666 /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000667 if (replace[i] == '\\' && isdigit(replace[i + 1])) {
Mark Whitley97562bd2000-07-17 20:06:42 +0000668 int j;
669 char tmpstr[2];
670 int backref;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000671
672 ++i; /* i now indexes the backref number, instead of the leading slash */
Mark Whitley97562bd2000-07-17 20:06:42 +0000673 tmpstr[0] = replace[i];
674 tmpstr[1] = 0;
675 backref = atoi(tmpstr);
676 /* print out the text held in regmatch[backref] */
Matt Kraaia3e4f452001-08-20 21:21:06 +0000677 if (backref <= matches && regmatch[backref].rm_so != -1)
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000678 for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000679 j++)
Eric Andersenb76cb682001-08-22 05:58:16 +0000680 pipeputc(line[j]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000681 }
682
Mark Whitley83e85f62000-07-25 20:48:44 +0000683 /* if we find a backslash escaped character, print the character */
684 else if (replace[i] == '\\') {
685 ++i;
Eric Andersenb76cb682001-08-22 05:58:16 +0000686 pipeputc(replace[i]);
Mark Whitley83e85f62000-07-25 20:48:44 +0000687 }
688
Mark Whitley97562bd2000-07-17 20:06:42 +0000689 /* if we find an unescaped '&' print out the whole matched text.
690 * fortunately, regmatch[0] contains the indicies to the whole matched
691 * expression (kinda seems like it was designed for just such a
692 * purpose...) */
Glenn L McGrath24103862003-04-09 07:51:43 +0000693 else if (replace[i] == '&') {
Mark Whitley97562bd2000-07-17 20:06:42 +0000694 int j;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000695
Mark Whitley97562bd2000-07-17 20:06:42 +0000696 for (j = regmatch[0].rm_so; j < regmatch[0].rm_eo; j++)
Eric Andersenb76cb682001-08-22 05:58:16 +0000697 pipeputc(line[j]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000698 }
699 /* nothing special, just print this char of the replacement string to stdout */
700 else
Eric Andersenb76cb682001-08-22 05:58:16 +0000701 pipeputc(replace[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000702 }
703}
704
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000705static int do_subst_command(sed_cmd_t * sed_cmd, char **line)
Mark Whitley4f7fe772000-07-13 20:01:58 +0000706{
Eric Andersenb76cb682001-08-22 05:58:16 +0000707 char *hackline = *line;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000708 struct pipeline thepipe = { NULL, 0, 0 };
Eric Andersenc52a6b02001-11-10 10:49:42 +0000709 struct pipeline *const pipeline = &thepipe;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000710 int altered = 0;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000711 int result;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000712 regmatch_t *regmatch = NULL;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000713 regex_t *current_regex;
714
715 if (sed_cmd->sub_match == NULL) {
716 current_regex = previous_regex_ptr;
717 } else {
718 previous_regex_ptr = current_regex = sed_cmd->sub_match;
719 }
720 result = regexec(current_regex, hackline, 0, NULL, 0);
Mark Whitley4f7fe772000-07-13 20:01:58 +0000721
Mark Whitley2dc192f2000-11-03 19:47:00 +0000722 /* we only proceed if the substitution 'search' expression matches */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000723 if (result == REG_NOMATCH) {
Mark Whitley2dc192f2000-11-03 19:47:00 +0000724 return 0;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000725 }
Mark Whitley2dc192f2000-11-03 19:47:00 +0000726
727 /* whaddaya know, it matched. get the number of back references */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000728 regmatch = xmalloc(sizeof(regmatch_t) * (sed_cmd->num_backrefs + 1));
Mark Whitley2dc192f2000-11-03 19:47:00 +0000729
Eric Andersenb76cb682001-08-22 05:58:16 +0000730 /* allocate more PIPE_GROW bytes
731 if replaced string is larger than original */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000732 thepipe.len = strlen(hackline) + PIPE_GROW;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000733 thepipe.buf = xcalloc(1, thepipe.len);
Eric Andersenb76cb682001-08-22 05:58:16 +0000734 /* buffer magic */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000735 thepipe.buf[thepipe.len - 1] = PIPE_MAGIC;
Eric Andersenb76cb682001-08-22 05:58:16 +0000736
Mark Whitley2dc192f2000-11-03 19:47:00 +0000737 /* and now, as long as we've got a line to try matching and if we can match
738 * the search string, we make substitutions */
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000739 while ((*hackline || !altered)
740 && (regexec(current_regex, hackline, sed_cmd->num_backrefs + 1,
741 regmatch, 0) != REG_NOMATCH)) {
Mark Whitley4f7fe772000-07-13 20:01:58 +0000742 int i;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000743
Mark Whitley2dc192f2000-11-03 19:47:00 +0000744 /* print everything before the match */
745 for (i = 0; i < regmatch[0].rm_so; i++)
Eric Andersenb76cb682001-08-22 05:58:16 +0000746 pipeputc(hackline[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000747
Mark Whitley2dc192f2000-11-03 19:47:00 +0000748 /* then print the substitution string */
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000749 print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch, pipeline,
750 sed_cmd->num_backrefs);
Mark Whitley97562bd2000-07-17 20:06:42 +0000751
Mark Whitley2dc192f2000-11-03 19:47:00 +0000752 /* advance past the match */
753 hackline += regmatch[0].rm_eo;
754 /* flag that something has changed */
755 altered++;
Mark Whitley97562bd2000-07-17 20:06:42 +0000756
Mark Whitley2dc192f2000-11-03 19:47:00 +0000757 /* if we're not doing this globally, get out now */
Glenn L McGrath7ce9e242003-04-07 16:04:14 +0000758 if (!sed_cmd->sub_g) {
Mark Whitley2dc192f2000-11-03 19:47:00 +0000759 break;
Glenn L McGrath7ce9e242003-04-07 16:04:14 +0000760 }
Mark Whitley4f7fe772000-07-13 20:01:58 +0000761 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000762 for (; *hackline; hackline++)
763 pipeputc(*hackline);
764 if (thepipe.buf[thepipe.idx] == PIPE_MAGIC)
765 thepipe.buf[thepipe.idx] = 0;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000766
767 /* cleanup */
768 free(regmatch);
769
Eric Andersenb76cb682001-08-22 05:58:16 +0000770 free(*line);
Eric Andersenc52a6b02001-11-10 10:49:42 +0000771 *line = thepipe.buf;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000772 return altered;
773}
Mark Whitley6315ce62000-07-10 22:55:51 +0000774
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000775static sed_cmd_t *branch_to(const char *label)
776{
777 sed_cmd_t *sed_cmd;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000778
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000779 for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
Glenn L McGrathf4523562003-09-14 06:01:14 +0000780 if ((sed_cmd->cmd == ':') && (sed_cmd->label) && (strcmp(sed_cmd->label, label) == 0)) {
781 return (sed_cmd);
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000782 }
783 }
Glenn L McGrathf4523562003-09-14 06:01:14 +0000784 bb_error_msg_and_die("Can't find label for jump to `%s'", label);
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000785}
Mark Whitley6315ce62000-07-10 22:55:51 +0000786
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000787static void process_file(FILE * file)
Mark Whitley6315ce62000-07-10 22:55:51 +0000788{
Glenn L McGrathc6adada2003-04-07 12:24:44 +0000789 char *pattern_space; /* Posix requires it be able to hold at least 8192 bytes */
Glenn L McGrath7ce9e242003-04-07 16:04:14 +0000790 char *hold_space = NULL; /* Posix requires it be able to hold at least 8192 bytes */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000791 static int linenum = 0; /* GNU sed does not restart counting lines at EOF */
Mark Whitley0915c4b2001-06-11 23:50:06 +0000792 int altered;
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +0000793 int force_print;
Mark Whitley6315ce62000-07-10 22:55:51 +0000794
Glenn L McGrathc6adada2003-04-07 12:24:44 +0000795 pattern_space = bb_get_chomped_line_from_file(file);
796 if (pattern_space == NULL) {
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000797 return;
798 }
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000799
Mark Whitley6315ce62000-07-10 22:55:51 +0000800 /* go through every line in the file */
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000801 do {
802 char *next_line;
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000803 sed_cmd_t *sed_cmd;
Glenn L McGrath595a6a42003-03-28 08:36:52 +0000804 int substituted = 0;
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000805 /* This enables whole blocks of commands to be mask'ed out if the lead address doesnt match */
806 int block_mask = 1;
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000807
808 /* Read one line in advance so we can act on the last line, the '$' address */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000809 next_line = bb_get_chomped_line_from_file(file);
Mark Whitley6315ce62000-07-10 22:55:51 +0000810 linenum++;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000811 altered = 0;
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +0000812 force_print = 0;
Mark Whitley6315ce62000-07-10 22:55:51 +0000813
814 /* for every line, go through all the commands */
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000815 for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
Robert Griebl47abc492002-06-11 23:43:27 +0000816 int deleted = 0;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000817
818 /*
819 * entry point into sedding...
820 */
Robert Griebl47abc492002-06-11 23:43:27 +0000821 int matched = (
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000822 /* no range necessary */
823 (sed_cmd->beg_line == 0 && sed_cmd->end_line == 0
824 && sed_cmd->beg_match == NULL
825 && sed_cmd->end_match == NULL) ||
826 /* this line number is the first address we're looking for */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000827 (sed_cmd->beg_line > 0 && (sed_cmd->beg_line == linenum)) ||
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000828 /* this line matches our first address regex */
829 (sed_cmd->beg_match
830 && (regexec(sed_cmd->beg_match, pattern_space, 0, NULL,
831 0) == 0)) ||
832 /* we are currently within the beginning & ending address range */
Glenn L McGrathc18ce372003-09-13 06:57:39 +0000833 sed_cmd->still_in_range || ((sed_cmd->beg_line == -1)
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000834 && (next_line == NULL))
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000835 );
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000836 if (sed_cmd->cmd == '{') {
837 block_mask = block_mask & matched;
838 }
839// matched &= block_mask;
840
841 if (sed_cmd->invert ^ (matched & block_mask)) {
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000842 /* Update last used regex incase a blank substitute BRE is found */
843 if (sed_cmd->beg_match) {
844 previous_regex_ptr = sed_cmd->beg_match;
845 }
Mark Whitley0915c4b2001-06-11 23:50:06 +0000846
847 /*
848 * actual sedding
849 */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000850 switch (sed_cmd->cmd) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000851 case '=':
852 printf("%d\n", linenum);
853 break;
854 case 'P':{
855 /* Write the current pattern space upto the first newline */
856 char *tmp = strchr(pattern_space, '\n');
Mark Whitley0915c4b2001-06-11 23:50:06 +0000857
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000858 if (tmp) {
859 *tmp = '\0';
860 }
861 }
862 case 'p': /* Write the current pattern space to output */
863 puts(pattern_space);
864 break;
865 case 'd':
866 altered++;
867 deleted = 1;
Glenn L McGrath294d1132003-09-14 16:28:08 +0000868 force_print = 0;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000869 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000870
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000871 case 's':
872
873 /*
874 * Some special cases for 's' printing to make it compliant with
875 * GNU sed printing behavior (aka "The -n | s///p Matrix"):
876 *
877 * -n ONLY = never print anything regardless of any successful
878 * substitution
879 *
880 * s///p ONLY = always print successful substitutions, even if
881 * the pattern_space is going to be printed anyway (pattern_space
882 * will be printed twice).
883 *
884 * -n AND s///p = print ONLY a successful substitution ONE TIME;
885 * no other lines are printed - this is the reason why the 'p'
886 * flag exists in the first place.
887 */
Mark Whitley0915c4b2001-06-11 23:50:06 +0000888
Glenn L McGrath0c518322003-03-30 03:41:53 +0000889#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000890 /* HACK: escape newlines twice so regex can match them */
891 {
892 int offset = 0;
893
894 while (strchr(pattern_space + offset, '\n') != NULL) {
895 char *tmp;
896
897 pattern_space =
898 xrealloc(pattern_space,
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000899 strlen(pattern_space) + 2);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000900 tmp = strchr(pattern_space + offset, '\n');
901 memmove(tmp + 1, tmp, strlen(tmp) + 1);
902 tmp[0] = '\\';
903 tmp[1] = 'n';
904 offset = tmp - pattern_space + 2;
905 }
906 }
Glenn L McGrath0c518322003-03-30 03:41:53 +0000907#endif
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000908 /* we print the pattern_space once, unless we were told to be quiet */
Glenn L McGrath3fe47562003-09-14 07:59:28 +0000909 substituted |= do_subst_command(sed_cmd, &pattern_space);
Glenn L McGrath0c518322003-03-30 03:41:53 +0000910
911#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000912 /* undo HACK: escape newlines twice so regex can match them */
913 {
914 char *tmp = pattern_space;
Glenn L McGrath0c518322003-03-30 03:41:53 +0000915
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000916 while ((tmp = strstr(tmp, "\\n")) != NULL) {
917 memmove(tmp, tmp + 1, strlen(tmp + 1) + 1);
918 tmp[0] = '\n';
Glenn L McGrath0c518322003-03-30 03:41:53 +0000919 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000920 }
Glenn L McGrath0c518322003-03-30 03:41:53 +0000921#endif
Glenn L McGrathedc388c2003-09-14 08:52:53 +0000922 if (!be_quiet && substituted && ((sed_cmd->next == NULL)
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000923 || (sed_cmd->next->cmd != 's'))) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000924 force_print = 1;
925 }
Mark Whitley0915c4b2001-06-11 23:50:06 +0000926
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000927 /* we also print the line if we were given the 'p' flag
928 * (this is quite possibly the second printing) */
929 if ((sed_cmd->sub_p) && altered) {
Glenn L McGrathc6adada2003-04-07 12:24:44 +0000930 puts(pattern_space);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000931 }
932 break;
933 case 'a':
934 puts(pattern_space);
935 fputs(sed_cmd->editline, stdout);
936 altered++;
937 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000938
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000939 case 'i':
940 fputs(sed_cmd->editline, stdout);
941 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000942
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000943 case 'c':
944 /* single-address case */
945 if ((sed_cmd->end_match == NULL && sed_cmd->end_line == 0)
Mark Whitley0915c4b2001-06-11 23:50:06 +0000946 /* multi-address case */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000947 /* - matching text */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000948 || (sed_cmd->end_match
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000949 && (regexec(sed_cmd->end_match, pattern_space, 0,
950 NULL, 0) == 0))
Eric Andersenc52a6b02001-11-10 10:49:42 +0000951 /* - matching line numbers */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000952 || (sed_cmd->end_line > 0
953 && sed_cmd->end_line == linenum)) {
954 fputs(sed_cmd->editline, stdout);
955 }
956 altered++;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000957
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000958 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000959
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000960 case 'r':{
961 FILE *outfile;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000962
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000963 outfile = fopen(sed_cmd->filename, "r");
Glenn L McGrathd87a7ac2003-04-09 15:26:14 +0000964 if (outfile) {
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000965 char *line;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000966
967 while ((line =
968 bb_get_chomped_line_from_file(outfile)) !=
969 NULL) {
970 pattern_space =
971 xrealloc(pattern_space,
972 strlen(line) + strlen(pattern_space) + 2);
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000973 strcat(pattern_space, "\n");
974 strcat(pattern_space, line);
975 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000976 bb_xprint_and_close_file(outfile);
Glenn L McGrathd87a7ac2003-04-09 15:26:14 +0000977 }
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000978
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000979 }
980 break;
981 case 'q': /* Branch to end of script and quit */
982 deleted = 1;
983 /* Exit the outer while loop */
984 free(next_line);
985 next_line = NULL;
986 break;
987 case 'n': /* Read next line from input */
Glenn L McGrath8417c8c2003-09-14 15:24:18 +0000988 if (next_line) {
989 free(pattern_space);
990 pattern_space = next_line;
991 next_line = bb_get_chomped_line_from_file(file);
992 linenum++;
993 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000994 break;
995 case 'N': /* Append the next line to the current line */
996 if (next_line) {
997 pattern_space =
998 realloc(pattern_space,
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000999 strlen(pattern_space) + strlen(next_line) + 2);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001000 strcat(pattern_space, "\n");
1001 strcat(pattern_space, next_line);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001002 next_line = bb_get_chomped_line_from_file(file);
Glenn L McGrath4157a8a2003-03-10 04:12:35 +00001003 linenum++;
Glenn L McGrath73116312003-09-15 05:42:05 +00001004 } else {
1005 /* Jump to end of script and exist */
1006 deleted = 1;
1007 free(next_line);
1008 next_line = NULL;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001009 }
1010 break;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001011 case 't':
Glenn L McGrathf4523562003-09-14 06:01:14 +00001012 if (substituted)
1013 /* Fall through */
1014 case 'b':
1015 {
1016 if (sed_cmd->label == NULL) {
1017 /* Jump to end of script */
1018 deleted = 1;
1019 } else {
1020 sed_cmd = branch_to(sed_cmd->label);
1021 }
Glenn L McGrath640c1f52003-09-15 04:55:29 +00001022 /* Reset the substitution flag */
1023 substituted = 0;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001024 }
1025 break;
1026 case 'y':{
1027 int i;
1028
1029 for (i = 0; pattern_space[i] != 0; i++) {
1030 int j;
1031
1032 for (j = 0; sed_cmd->translate[j]; j += 2) {
1033 if (pattern_space[i] == sed_cmd->translate[j]) {
1034 pattern_space[i] = sed_cmd->translate[j + 1];
Glenn L McGrathf01b46d2003-03-30 08:02:18 +00001035 }
1036 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001037 }
1038 }
1039 break;
1040 case 'g': /* Replace pattern space with hold space */
1041 free(pattern_space);
Glenn L McGrath294d1132003-09-14 16:28:08 +00001042 if (hold_space) {
1043 pattern_space = strdup(hold_space);
1044 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001045 break;
Glenn L McGrath91e19782003-04-26 07:40:07 +00001046 case 'G': { /* Append newline and hold space to pattern space */
Glenn L McGrath294d1132003-09-14 16:28:08 +00001047 int pattern_space_size = 2;
1048 int hold_space_size = 0;
1049
Glenn L McGrath91e19782003-04-26 07:40:07 +00001050 if (pattern_space) {
Glenn L McGrath294d1132003-09-14 16:28:08 +00001051 pattern_space_size += strlen(pattern_space);
Glenn L McGrath91e19782003-04-26 07:40:07 +00001052 }
Glenn L McGrath294d1132003-09-14 16:28:08 +00001053 if (hold_space) {
1054 hold_space_size = strlen(hold_space);
1055 }
1056 pattern_space = xrealloc(pattern_space, pattern_space_size + hold_space_size);
1057 if (pattern_space_size == 2) {
1058 strcat(pattern_space, "\n");
1059 } else {
1060 strcpy(pattern_space, "\n");
1061 }
1062 if (hold_space) {
1063 strcat(pattern_space, hold_space);
1064 }
Glenn L McGrath91e19782003-04-26 07:40:07 +00001065 break;
1066 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001067 case 'h': /* Replace hold space with pattern space */
1068 free(hold_space);
1069 hold_space = strdup(pattern_space);
1070 break;
Glenn L McGrath91e19782003-04-26 07:40:07 +00001071 case 'H': { /* Append newline and pattern space to hold space */
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001072 int hold_space_size = 2;
1073 int pattern_space_size = 0;
1074
Glenn L McGrath91e19782003-04-26 07:40:07 +00001075 if (hold_space) {
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001076 hold_space_size += strlen(hold_space);
Glenn L McGrath91e19782003-04-26 07:40:07 +00001077 }
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001078 if (pattern_space) {
1079 pattern_space_size = strlen(pattern_space);
1080 }
1081 hold_space = xrealloc(hold_space, hold_space_size + pattern_space_size);
1082
1083 if (hold_space_size == 2) {
1084 strcpy(hold_space, "\n");
Glenn L McGrath4dc1d252003-09-14 01:25:31 +00001085 } else {
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001086 strcat(hold_space, "\n");
Glenn L McGrath4dc1d252003-09-14 01:25:31 +00001087 }
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001088 if (pattern_space) {
1089 strcat(hold_space, pattern_space);
1090 }
Glenn L McGrath91e19782003-04-26 07:40:07 +00001091 break;
1092 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001093 case 'x':{
1094 /* Swap hold and pattern space */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +00001095 char *tmp = pattern_space;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001096 pattern_space = hold_space;
1097 hold_space = tmp;
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +00001098 break;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001099 }
Mark Whitley0915c4b2001-06-11 23:50:06 +00001100 }
Robert Griebl47abc492002-06-11 23:43:27 +00001101 }
Mark Whitley0915c4b2001-06-11 23:50:06 +00001102
Robert Griebl47abc492002-06-11 23:43:27 +00001103 /*
1104 * exit point from sedding...
1105 */
1106 if (matched) {
Mark Whitley0915c4b2001-06-11 23:50:06 +00001107 if (
Glenn L McGrathd4185b02003-04-11 17:10:23 +00001108 /* this is a single-address command or... */
1109 (sed_cmd->end_line == 0 && sed_cmd->end_match == NULL)
1110 /* If only one address */
1111 /* we were in the middle of our address range (this
1112 * isn't the first time through) and.. */
Glenn L McGrathc18ce372003-09-13 06:57:39 +00001113 || ((sed_cmd->still_in_range == 1)
Glenn L McGrathd4185b02003-04-11 17:10:23 +00001114 /* this line number is the last address we're looking for or... */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +00001115 && ((sed_cmd->end_line > 0
Glenn L McGrathd4185b02003-04-11 17:10:23 +00001116 && (sed_cmd->end_line == linenum))
1117 /* this line matches our last address regex */
1118 || (sed_cmd->end_match
1119 && (regexec(sed_cmd->end_match, pattern_space,
1120 0, NULL, 0) == 0))))) {
Mark Whitley0915c4b2001-06-11 23:50:06 +00001121 /* we're out of our address range */
Glenn L McGrathc18ce372003-09-13 06:57:39 +00001122 sed_cmd->still_in_range = 0;
Glenn L McGrathd4185b02003-04-11 17:10:23 +00001123 } else {
1124 /* didn't hit the exit? then we're still in the middle of an address range */
Glenn L McGrathc18ce372003-09-13 06:57:39 +00001125 sed_cmd->still_in_range = 1;
Mark Whitley6315ce62000-07-10 22:55:51 +00001126 }
1127 }
Robert Griebl47abc492002-06-11 23:43:27 +00001128
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +00001129 if (sed_cmd->cmd == '}') {
1130 block_mask = 1;
1131 }
1132
Robert Griebl47abc492002-06-11 23:43:27 +00001133 if (deleted)
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001134 break;
Glenn L McGrath4dc1d252003-09-14 01:25:31 +00001135
Erik Andersene49d5ec2000-02-08 19:58:47 +00001136 }
Erik Andersen1266a131999-12-29 22:19:46 +00001137
Mark Whitley2dc192f2000-11-03 19:47:00 +00001138 /* we will print the line unless we were told to be quiet or if the
1139 * line was altered (via a 'd'elete or 's'ubstitution), in which case
1140 * the altered line was already printed */
Glenn L McGrathedc388c2003-09-14 08:52:53 +00001141 if ((!be_quiet && !altered && !substituted) || force_print) {
Glenn L McGrathc6adada2003-04-07 12:24:44 +00001142 puts(pattern_space);
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +00001143 }
Glenn L McGrathc6adada2003-04-07 12:24:44 +00001144 free(pattern_space);
1145 pattern_space = next_line;
1146 } while (pattern_space);
Erik Andersen1266a131999-12-29 22:19:46 +00001147}
1148
1149extern int sed_main(int argc, char **argv)
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001150{
Matt Kraaia5f09c62001-11-12 16:44:55 +00001151 int opt, status = EXIT_SUCCESS;
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001152
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001153#if 0 /* This doesnt seem to be working */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001154#ifdef CONFIG_FEATURE_CLEAN_UP
Mark Whitley858c1ad2000-07-11 21:38:47 +00001155 /* destroy command strings on exit */
Matt Kraaia9819b22000-12-22 01:48:07 +00001156 if (atexit(destroy_cmd_strs) == -1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001157 bb_perror_msg_and_die("atexit");
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001158#endif
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001159#endif
Mark Whitley858c1ad2000-07-11 21:38:47 +00001160
Mark Whitley6315ce62000-07-10 22:55:51 +00001161 /* do normal option parsing */
Eric Andersenb50da532001-02-17 16:52:35 +00001162 while ((opt = getopt(argc, argv, "ne:f:")) > 0) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001163 switch (opt) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001164 case 'n':
1165 be_quiet++;
1166 break;
1167 case 'e':{
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001168 add_cmd_str(optarg);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001169 break;
1170 }
1171 case 'f':
1172 load_cmd_file(optarg);
1173 break;
1174 default:
1175 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001176 }
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001177 }
Mark Whitley6315ce62000-07-10 22:55:51 +00001178
1179 /* if we didn't get a pattern from a -e and no command file was specified,
1180 * argv[optind] should be the pattern. no pattern, no worky */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +00001181 if (sed_cmd_head.next == NULL) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001182 if (argv[optind] == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001183 bb_show_usage();
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001184 else
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001185 add_cmd_str(argv[optind++]);
Mark Whitley6315ce62000-07-10 22:55:51 +00001186 }
1187
Mark Whitley6315ce62000-07-10 22:55:51 +00001188 /* argv[(optind)..(argc-1)] should be names of file to process. If no
1189 * files were specified or '-' was specified, take input from stdin.
1190 * Otherwise, we process all the files specified. */
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001191 if (argv[optind] == NULL) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001192 process_file(stdin);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001193 } else {
Mark Whitley6315ce62000-07-10 22:55:51 +00001194 int i;
1195 FILE *file;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001196
Mark Whitley6315ce62000-07-10 22:55:51 +00001197 for (i = optind; i < argc; i++) {
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001198 if(!strcmp(argv[i], "-")) {
1199 process_file(stdin);
1200 } else {
1201 file = bb_wfopen(argv[i], "r");
1202 if (file) {
1203 process_file(file);
1204 fclose(file);
1205 } else {
1206 status = EXIT_FAILURE;
1207 }
1208 }
Mark Whitley6315ce62000-07-10 22:55:51 +00001209 }
1210 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001211
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001212#ifdef CONFIG_FEATURE_CLEAN_UP
1213 destroy_cmd_strs();
1214#endif
Matt Kraaia5f09c62001-11-12 16:44:55 +00001215 return status;
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001216}