blob: c039249178838a55324838cd7b88a71af370ccab [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
Erik Andersen1266a131999-12-29 22:19:46 +00007 *
Eric Andersen6b6b3f61999-10-28 16:06:25 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Mark Whitley6315ce62000-07-10 22:55:51 +000024/*
25 Supported features and commands in this version of sed:
26
27 - comments ('#')
Mark Whitley94074a92000-07-14 00:00:15 +000028 - address matching: num|/matchstr/[,num|/matchstr/|$]command
29 - commands: (p)rint, (d)elete, (s)ubstitue (with g & I flags)
30 - edit commands: (a)ppend, (i)nsert, (c)hange
Mark Whitley1f3b9f22001-05-11 22:27:13 +000031 - file commands: (r)ead
Mark Whitley97562bd2000-07-17 20:06:42 +000032 - backreferences in substitution expressions (\1, \2...\9)
Glenn L McGrathf50ce312003-03-09 15:12:24 +000033 - grouped commands: {cmd1;cmd2}
34
Mark Whitley6315ce62000-07-10 22:55:51 +000035 (Note: Specifying an address (range) to match is *optional*; commands
36 default to the whole pattern space if no specific address match was
37 requested.)
38
39 Unsupported features:
40
41 - transliteration (y/source-chars/dest-chars/) (use 'tr')
Mark Whitley6315ce62000-07-10 22:55:51 +000042 - no pattern space hold space storing / swapping (x, etc.)
43 - no labels / branching (: label, b, t, and friends)
44 - and lots, lots more.
Glenn L McGrathd5eadea2003-03-09 02:39:29 +000045
46 Reference http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html
Mark Whitley6315ce62000-07-10 22:55:51 +000047*/
48
Eric Andersen6b6b3f61999-10-28 16:06:25 +000049#include <stdio.h>
Mark Whitley6315ce62000-07-10 22:55:51 +000050#include <unistd.h> /* for getopt() */
51#include <regex.h>
52#include <string.h> /* for strdup() */
Eric Andersen6b6b3f61999-10-28 16:06:25 +000053#include <errno.h>
Mark Whitley6315ce62000-07-10 22:55:51 +000054#include <ctype.h> /* for isspace() */
Eric Andersened3ef502001-01-27 08:24:39 +000055#include <stdlib.h>
Eric Andersen3570a342000-09-25 21:45:58 +000056#include "busybox.h"
Mark Whitley6315ce62000-07-10 22:55:51 +000057
Glenn L McGrath961c6c12003-03-28 04:43:39 +000058/* the spec says label must be at least 8 chars, behavious is unspecified if more than 8 chars */
59#define SED_LABEL_LENGTH 8
60
Mark Whitley6315ce62000-07-10 22:55:51 +000061/* externs */
Eric Andersened3ef502001-01-27 08:24:39 +000062extern void xregcomp(regex_t *preg, const char *regex, int cflags);
Mark Whitley6315ce62000-07-10 22:55:51 +000063extern int optind; /* in unistd.h */
64extern char *optarg; /* ditto */
65
66/* options */
67static int be_quiet = 0;
Glenn L McGrath9a52bb62003-03-30 09:38:40 +000068static const char bad_format_in_subst[] = "bad format in substitution expression";
Mark Whitley0e4cec02000-08-21 21:29:20 +000069
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +000070typedef struct sed_cmd_s {
Eric Andersenc52a6b02001-11-10 10:49:42 +000071 /* Order by alignment requirements */
Mark Whitley2dc192f2000-11-03 19:47:00 +000072
Mark Whitley6315ce62000-07-10 22:55:51 +000073 /* address storage */
Mark Whitley6315ce62000-07-10 22:55:51 +000074 regex_t *beg_match; /* sed -e '/match/cmd' */
75 regex_t *end_match; /* sed -e '/match/,/end_match/cmd' */
76
Mark Whitley2dc192f2000-11-03 19:47:00 +000077 /* SUBSTITUTION COMMAND SPECIFIC FIELDS */
78
79 /* sed -e 's/sub_match/replace/' */
80 regex_t *sub_match;
81 char *replace;
Eric Andersenc52a6b02001-11-10 10:49:42 +000082
83 /* EDIT COMMAND (a,i,c) SPECIFIC FIELDS */
84 char *editline;
85
86 /* FILE COMMAND (r) SPECIFIC FIELDS */
87 char *filename;
88
89 /* address storage */
90 int beg_line; /* 'sed 1p' 0 == no begining line, apply commands to all lines */
91 int end_line; /* 'sed 1,3p' 0 == no end line, use only beginning. -1 == $ */
92 /* SUBSTITUTION COMMAND SPECIFIC FIELDS */
93
Mark Whitley97562bd2000-07-17 20:06:42 +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 */
Mark Whitley2dc192f2000-11-03 19:47:00 +000097 unsigned int sub_g:1; /* sed -e 's/foo/bar/g' (global) */
98 unsigned int sub_p:2; /* 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 */
105 char cmd; /* p,d,s (add more at your leisure :-) */
Robert Griebl47abc492002-06-11 23:43:27 +0000106
107 /* inversion flag */
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000108 int invert; /* the '!' after the address */
109
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000110 /* Branch commands */
111 char label[SED_LABEL_LENGTH + 1];
112
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000113 /* next command in list (sequential list of specified commands) */
114 struct sed_cmd_s *linear;
115
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +0000116} sed_cmd_t;
Mark Whitley6315ce62000-07-10 22:55:51 +0000117
118/* globals */
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000119/* linked list of sed commands */
120static sed_cmd_t sed_cmd_head;
121static sed_cmd_t *sed_cmd_tail = &sed_cmd_head;
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000122
Eric Andersenc52a6b02001-11-10 10:49:42 +0000123const char * const semicolon_whitespace = "; \n\r\t\v\0";
124
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000125#ifdef CONFIG_FEATURE_CLEAN_UP
Matt Kraai0c390a72001-11-20 16:00:19 +0000126static void destroy_cmd_strs(void)
Mark Whitley6315ce62000-07-10 22:55:51 +0000127{
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000128 sed_cmd_t *sed_cmd = sed_cmd_head.linear;
Mark Whitley6315ce62000-07-10 22:55:51 +0000129
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000130 while (sed_cmd) {
131 sed_cmd_t *sed_cmd_next = sed_cmd->linear;
Mark Whitley6315ce62000-07-10 22:55:51 +0000132
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000133 if (sed_cmd->beg_match) {
134 regfree(sed_cmd->beg_match);
135 free(sed_cmd->beg_match);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000137 if (sed_cmd->end_match) {
138 regfree(sed_cmd->end_match);
139 free(sed_cmd->end_match);
Mark Whitley6315ce62000-07-10 22:55:51 +0000140 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000141 if (sed_cmd->sub_match) {
142 regfree(sed_cmd->sub_match);
143 free(sed_cmd->sub_match);
Mark Whitley6315ce62000-07-10 22:55:51 +0000144 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000145 free(sed_cmd->replace);
146 free(sed_cmd);
147 sed_cmd = sed_cmd_next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000148 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000149}
Mark Whitleyc41e8c82000-07-12 23:35:21 +0000150#endif
Mark Whitley6315ce62000-07-10 22:55:51 +0000151
Mark Whitley6315ce62000-07-10 22:55:51 +0000152/*
Eric Andersen28b3c532001-01-02 11:01:31 +0000153 * index_of_next_unescaped_regexp_delim - walks left to right through a string
154 * beginning at a specified index and returns the index of the next regular
155 * expression delimiter (typically a forward * slash ('/')) not preceeded by
156 * a backslash ('\').
Mark Whitley6315ce62000-07-10 22:55:51 +0000157 */
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000158static int index_of_next_unescaped_regexp_delim(const char delimiter, const char *str)
Mark Whitley6315ce62000-07-10 22:55:51 +0000159{
Matt Kraaia0065d52001-08-24 14:45:50 +0000160 int bracket = -1;
161 int escaped = 0;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000162 int idx = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000163 char ch;
Matt Kraaia0065d52001-08-24 14:45:50 +0000164
Eric Andersenc52a6b02001-11-10 10:49:42 +0000165 for ( ; (ch = str[idx]); idx++) {
Matt Kraaia0065d52001-08-24 14:45:50 +0000166 if (bracket != -1) {
Eric Andersenc52a6b02001-11-10 10:49:42 +0000167 if (ch == ']' && !(bracket == idx - 1 ||
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000168 (bracket == idx - 2 && str[idx-1] == '^')))
Matt Kraaia0065d52001-08-24 14:45:50 +0000169 bracket = -1;
170 } else if (escaped)
171 escaped = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000172 else if (ch == '\\')
Matt Kraaia0065d52001-08-24 14:45:50 +0000173 escaped = 1;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000174 else if (ch == '[')
Matt Kraaia0065d52001-08-24 14:45:50 +0000175 bracket = idx;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000176 else if (ch == delimiter)
Mark Whitley97562bd2000-07-17 20:06:42 +0000177 return idx;
178 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000179
Mark Whitley97562bd2000-07-17 20:06:42 +0000180 /* if we make it to here, we've hit the end of the string */
181 return -1;
Mark Whitley6315ce62000-07-10 22:55:51 +0000182}
183
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000184static int parse_regex_delim(const char *cmdstr, char **match, char **replace)
185{
186 const char *cmdstr_ptr = cmdstr;
187 char delimiter;
188 int idx = 0;
189
190 /* verify that the 's' is followed by something. That something
191 * (typically a 'slash') is now our regexp delimiter... */
192 if (*cmdstr == '\0')
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000193 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000194 else
195 delimiter = *cmdstr_ptr;
196
197 cmdstr_ptr++;
198
199 /* save the match string */
200 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
201 if (idx == -1) {
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000202 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000203 }
204 *match = bb_xstrndup(cmdstr_ptr, idx);
205
206 /* save the replacement string */
207 cmdstr_ptr += idx + 1;
208 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
209 if (idx == -1) {
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000210 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000211 }
212 *replace = bb_xstrndup(cmdstr_ptr, idx);
213
214 return((cmdstr_ptr - cmdstr) + idx);
215}
216
Mark Whitley6315ce62000-07-10 22:55:51 +0000217/*
218 * returns the index in the string just past where the address ends.
219 */
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000220static int get_address(char *my_str, int *linenum, regex_t **regex)
Mark Whitley6315ce62000-07-10 22:55:51 +0000221{
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000222 int idx = 0;
Mark Whitley6315ce62000-07-10 22:55:51 +0000223 if (isdigit(my_str[idx])) {
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000224 char *endstr;
225 *linenum = strtol(my_str, &endstr, 10);
226 /* endstr shouldnt ever equal NULL */
227 idx = endstr - my_str;
Mark Whitley6315ce62000-07-10 22:55:51 +0000228 }
229 else if (my_str[idx] == '$') {
Mark Whitley0915c4b2001-06-11 23:50:06 +0000230 *linenum = -1;
Mark Whitley6315ce62000-07-10 22:55:51 +0000231 idx++;
232 }
Robert Griebl79401472002-08-06 21:07:17 +0000233 else if (my_str[idx] == '/' || my_str[idx] == '\\') {
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000234 int idx_start = 1;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000235 char delimiter;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000236
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000237 delimiter = '/';
Robert Griebl79401472002-08-06 21:07:17 +0000238 if (my_str[idx] == '\\') {
Robert Griebl00f5ecb2002-08-06 23:13:31 +0000239 idx_start++;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000240 delimiter = my_str[++idx];
Robert Griebl79401472002-08-06 21:07:17 +0000241 }
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000242 idx++;
243 idx += index_of_next_unescaped_regexp_delim(delimiter, my_str + idx);
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000244 if (idx == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000245 bb_error_msg_and_die("unterminated match expression");
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000246 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +0000247 my_str[idx] = '\0';
248 *regex = (regex_t *)xmalloc(sizeof(regex_t));
Robert Griebl00f5ecb2002-08-06 23:13:31 +0000249 xregcomp(*regex, my_str+idx_start, REG_NEWLINE);
Mark Whitley496e33f2000-07-13 22:52:02 +0000250 idx++; /* so it points to the next character after the last '/' */
Mark Whitley6315ce62000-07-10 22:55:51 +0000251 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000252 return idx;
253}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000254
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +0000255static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr)
Mark Whitley06f35292000-07-13 19:58:04 +0000256{
Glenn L McGrathe01f9662003-03-18 08:37:57 +0000257 int cflags = 0;
Mark Whitley06f35292000-07-13 19:58:04 +0000258 char *match;
259 int idx = 0;
Mark Whitley97562bd2000-07-17 20:06:42 +0000260 int j;
Mark Whitley06f35292000-07-13 19:58:04 +0000261
262 /*
263 * the string that gets passed to this function should look like this:
Mark Whitley0e4cec02000-08-21 21:29:20 +0000264 * s/match/replace/gIp
265 * || | |||
Mark Whitley06f35292000-07-13 19:58:04 +0000266 * mandatory optional
267 *
268 * (all three of the '/' slashes are mandatory)
269 */
270
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000271 idx = parse_regex_delim(substr, &match, &sed_cmd->replace);
Mark Whitley06f35292000-07-13 19:58:04 +0000272
Mark Whitley97562bd2000-07-17 20:06:42 +0000273 /* determine the number of back references in the match string */
274 /* Note: we compute this here rather than in the do_subst_command()
275 * function to save processor time, at the expense of a little more memory
276 * (4 bits) per sed_cmd */
277
278 /* sed_cmd->num_backrefs = 0; */ /* XXX: not needed? --apparently not */
279 for (j = 0; match[j]; j++) {
280 /* GNU/POSIX sed does not save more than nine backrefs */
Mark Whitley2dc192f2000-11-03 19:47:00 +0000281 if (match[j] == '\\' && match[j+1] == '(' && sed_cmd->num_backrefs <= 9)
Mark Whitley97562bd2000-07-17 20:06:42 +0000282 sed_cmd->num_backrefs++;
283 }
284
Mark Whitley06f35292000-07-13 19:58:04 +0000285 /* process the flags */
286 while (substr[++idx]) {
287 switch (substr[idx]) {
Mark Whitley70705d72000-07-14 19:06:30 +0000288 case 'g':
Mark Whitley2dc192f2000-11-03 19:47:00 +0000289 sed_cmd->sub_g = 1;
Mark Whitley70705d72000-07-14 19:06:30 +0000290 break;
Glenn L McGrathbed40332003-03-10 02:21:14 +0000291 /* Hmm, i dont see the I option mentioned in the standard */
Mark Whitley70705d72000-07-14 19:06:30 +0000292 case 'I':
293 cflags |= REG_ICASE;
294 break;
Mark Whitley0e4cec02000-08-21 21:29:20 +0000295 case 'p':
Mark Whitley2dc192f2000-11-03 19:47:00 +0000296 sed_cmd->sub_p = 1;
Mark Whitley0e4cec02000-08-21 21:29:20 +0000297 break;
Mark Whitley70705d72000-07-14 19:06:30 +0000298 default:
299 /* any whitespace or semicolon trailing after a s/// is ok */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000300 if (strchr(semicolon_whitespace, substr[idx]))
Mark Whitley70705d72000-07-14 19:06:30 +0000301 goto out;
302 /* else */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000303 bb_error_msg_and_die("bad option in substitution expression");
Mark Whitley06f35292000-07-13 19:58:04 +0000304 }
305 }
Mark Whitley70705d72000-07-14 19:06:30 +0000306
307out:
Mark Whitley97562bd2000-07-17 20:06:42 +0000308 /* compile the match string into a regex */
Mark Whitley06f35292000-07-13 19:58:04 +0000309 sed_cmd->sub_match = (regex_t *)xmalloc(sizeof(regex_t));
310 xregcomp(sed_cmd->sub_match, match, cflags);
311 free(match);
Mark Whitley70705d72000-07-14 19:06:30 +0000312
313 return idx;
Mark Whitley06f35292000-07-13 19:58:04 +0000314}
315
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000316static void replace_slash_n(char *string)
317{
318 int i;
319 int remaining = strlen(string);
320
321 for (i = 0; string[i]; i++) {
322 if ((string[i] == '\\') && (string[i + 1] == 'n')) {
323 string[i] = '\n';
324 memmove(string + i + 1, string + i + 1, remaining - 1);
325 } else {
326 remaining--;
327 }
328 }
329}
330
331static int parse_translate_cmd(sed_cmd_t * const sed_cmd, const char *cmdstr)
332{
333 char *match;
334 char *replace;
335 int idx;
336 int i;
337
338 idx = parse_regex_delim(cmdstr, &match, &replace);
339 replace_slash_n(match);
340 replace_slash_n(replace);
341 sed_cmd->translate = xcalloc(1, (strlen(match) + 1) * 2);
342 for (i = 0; (match[i] != 0) && (replace[i] != 0); i++) {
343 sed_cmd->translate[i * 2] = match[i];
344 sed_cmd->translate[(i * 2) + 1] = replace[i];
345 }
Glenn L McGrath30b47df2003-03-30 08:40:09 +0000346 return(idx + 1);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000347}
348
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +0000349static int parse_edit_cmd(sed_cmd_t *sed_cmd, const char *editstr)
Mark Whitley94074a92000-07-14 00:00:15 +0000350{
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000351 int i, j;
Mark Whitley94074a92000-07-14 00:00:15 +0000352
353 /*
354 * the string that gets passed to this function should look like this:
355 *
356 * need one of these
357 * |
358 * | this backslash (immediately following the edit command) is mandatory
359 * | |
360 * [aic]\
361 * TEXT1\
362 * TEXT2\
363 * TEXTN
364 *
365 * as soon as we hit a TEXT line that has no trailing '\', we're done.
366 * this means a command like:
367 *
368 * i\
369 * INSERTME
370 *
371 * is a-ok.
372 *
373 */
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000374 if ((*editstr != '\\') || ((editstr[1] != '\n') && (editstr[1] != '\r'))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000375 bb_error_msg_and_die("bad format in edit expression");
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000376 }
Mark Whitley94074a92000-07-14 00:00:15 +0000377
378 /* store the edit line text */
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000379 sed_cmd->editline = xmalloc(strlen(&editstr[2]) + 2);
380 for (i = 2, j = 0; editstr[i] != '\0' && strchr("\r\n", editstr[i]) == NULL;
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000381 i++, j++) {
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000382 if ((editstr[i] == '\\') && strchr("\n\r", editstr[i+1]) != NULL) {
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000383 sed_cmd->editline[j] = '\n';
384 i++;
385 } else
386 sed_cmd->editline[j] = editstr[i];
Mark Whitley94074a92000-07-14 00:00:15 +0000387 }
Mark Whitley70705d72000-07-14 19:06:30 +0000388
Mark Whitley56c14a62001-04-20 23:41:44 +0000389 /* figure out if we need to add a newline */
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000390 if (sed_cmd->editline[j-1] != '\n')
391 sed_cmd->editline[j++] = '\n';
Mark Whitley56c14a62001-04-20 23:41:44 +0000392
393 /* terminate string */
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000394 sed_cmd->editline[j] = '\0';
Mark Whitley464c5de2000-07-14 23:24:00 +0000395
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000396 return i;
Mark Whitley94074a92000-07-14 00:00:15 +0000397}
398
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000399
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +0000400static int parse_file_cmd(sed_cmd_t *sed_cmd, const char *filecmdstr)
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000401{
402 int idx = 0;
403 int filenamelen = 0;
404
405 /*
406 * the string that gets passed to this function should look like this:
407 * '[ ]filename'
408 * | |
409 * | a filename
410 * |
411 * optional whitespace
412
413 * re: the file to be read, the GNU manual says the following: "Note that
414 * if filename cannot be read, it is treated as if it were an empty file,
415 * without any error indication." Thus, all of the following commands are
416 * perfectly leagal:
417 *
418 * sed -e '1r noexist'
419 * sed -e '1r ;'
420 * sed -e '1r'
421 */
422
423 /* the file command may be followed by whitespace; move past it. */
424 while (isspace(filecmdstr[++idx]))
425 { ; }
426
427 /* the first non-whitespace we get is a filename. the filename ends when we
428 * hit a normal sed command terminator or end of string */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000429 filenamelen = strcspn(&filecmdstr[idx], semicolon_whitespace);
Matt Kraai6e9e1362001-05-27 14:11:52 +0000430 sed_cmd->filename = xmalloc(filenamelen + 1);
431 safe_strncpy(sed_cmd->filename, &filecmdstr[idx], filenamelen + 1);
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000432
433 return idx + filenamelen;
434}
435
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000436/*
437 * Process the commands arguments
438 */
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +0000439static char *parse_cmd_str(sed_cmd_t * const sed_cmd, char *cmdstr)
Mark Whitley6315ce62000-07-10 22:55:51 +0000440{
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000441 /* handle (s)ubstitution command */
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000442 if (sed_cmd->cmd == 's') {
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000443 cmdstr += parse_subst_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000444 }
445 /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */
446 else if (strchr("aic", sed_cmd->cmd)) {
447 if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000448 bb_error_msg_and_die("only a beginning address can be specified for edit commands");
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000449 cmdstr += parse_edit_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000450 }
451 /* handle file cmds: (r)ead */
452 else if (sed_cmd->cmd == 'r') {
453 if (sed_cmd->end_line || sed_cmd->end_match)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000454 bb_error_msg_and_die("Command only uses one address");
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000455 cmdstr += parse_file_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000456 }
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000457 /* handle branch commands */
Glenn L McGrath595a6a42003-03-28 08:36:52 +0000458 else if (strchr(":bt", sed_cmd->cmd)) {
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000459 int length;
460
461 cmdstr += strspn(cmdstr, " ");
462 length = strcspn(cmdstr, "; ");
463 if (length > SED_LABEL_LENGTH) {
464 length = SED_LABEL_LENGTH;
465 }
466 strncpy(sed_cmd->label, cmdstr, length);
467 cmdstr += length;
468 }
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000469 /* translation command */
470 else if (sed_cmd->cmd == 'y') {
471 cmdstr += parse_translate_cmd(sed_cmd, cmdstr);
472 }
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000473 /* if it wasnt a single-letter command that takes no arguments
474 * then it must be an invalid command.
475 */
476 else if (strchr("nNpPqd=", sed_cmd->cmd) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000477 bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd);
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000478 }
Mark Whitley70705d72000-07-14 19:06:30 +0000479
480 /* give back whatever's left over */
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000481 return(cmdstr);
Eric Andersen50d63601999-11-09 01:47:36 +0000482}
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000483
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000484static char *add_cmd(sed_cmd_t *sed_cmd, char *cmdstr)
Erik Andersen1266a131999-12-29 22:19:46 +0000485{
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000486 /* Skip over leading whitespace and semicolons */
487 cmdstr += strspn(cmdstr, semicolon_whitespace);
Erik Andersen1266a131999-12-29 22:19:46 +0000488
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000489 /* if we ate the whole thing, that means there was just trailing
490 * whitespace or a final / no-op semicolon. either way, get out */
491 if (*cmdstr == '\0') {
492 return(NULL);
493 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000494
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000495 /* if this is a comment, jump past it and keep going */
496 if (*cmdstr == '#') {
497 return(strpbrk(cmdstr, "\n\r"));
498 }
499
500 /* parse the command
501 * format is: [addr][,addr]cmd
502 * |----||-----||-|
503 * part1 part2 part3
504 */
505
506 /* first part (if present) is an address: either a '$', a number or a /regex/ */
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000507 cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000508
509 /* second part (if present) will begin with a comma */
510 if (*cmdstr == ',') {
511 int idx;
512 cmdstr++;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000513 idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000514 if (idx == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000515 bb_error_msg_and_die("get_address: no address found in string\n"
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000516 "\t(you probably didn't check the string you passed me)");
Mark Whitley70705d72000-07-14 19:06:30 +0000517 }
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000518 cmdstr += idx;
519 }
Mark Whitley70705d72000-07-14 19:06:30 +0000520
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000521 /* skip whitespace before the command */
522 while (isspace(*cmdstr)) {
523 cmdstr++;
524 }
525
526 /* there my be the inversion flag between part2 and part3 */
527 if (*cmdstr == '!') {
528 sed_cmd->invert = 1;
529 cmdstr++;
530
531#ifdef SED_FEATURE_STRICT_CHECKING
532 /* According to the spec
533 * It is unspecified whether <blank>s can follow a '!' character,
534 * and conforming applications shall not follow a '!' character
535 * with <blank>s.
536 */
537 if (isblank(cmdstr[idx]) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000538 bb_error_msg_and_die("blank follows '!'");
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000539 }
540#else
541 /* skip whitespace before the command */
542 while (isspace(*cmdstr)) {
543 cmdstr++;
544 }
545#endif
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000546 }
547
548 /* last part (mandatory) will be a command */
549 if (*cmdstr == '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000550 bb_error_msg_and_die("missing command");
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000551
552 sed_cmd->cmd = *cmdstr;
553 cmdstr++;
554
555 if (sed_cmd->cmd == '{') {
556 do {
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000557 sed_cmd_t *sed_cmd_new;
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000558 char *end_ptr = strpbrk(cmdstr, ";}");
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000559
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000560 *end_ptr = '\0';
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000561 sed_cmd_new = xcalloc(1, sizeof(sed_cmd_t));
562 sed_cmd_new->beg_match = sed_cmd->beg_match;
563 sed_cmd_new->end_match = sed_cmd->end_match;
564 sed_cmd_new->beg_line = sed_cmd->beg_line;
565 sed_cmd_new->end_line = sed_cmd->end_line;
566 sed_cmd_new->invert = sed_cmd->invert;
567
568 add_cmd(sed_cmd_new, cmdstr);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000569 cmdstr = end_ptr + 1;
570 } while (*cmdstr != '\0');
571 } else {
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000572 cmdstr = parse_cmd_str(sed_cmd, cmdstr);
573
574 /* Add the command to the command array */
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000575 sed_cmd_tail->linear = sed_cmd;
576 sed_cmd_tail = sed_cmd_tail->linear;
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000577 }
578 return(cmdstr);
579}
580
581static void add_cmd_str(char *cmdstr)
582{
Glenn L McGrath0c518322003-03-30 03:41:53 +0000583#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
584 char *cmdstr_ptr = cmdstr;
585
586 /* HACK: convert "\n" to match tranlated '\n' string */
587 while((cmdstr_ptr = strstr(cmdstr_ptr, "\\n")) != NULL) {
588 cmdstr = xrealloc(cmdstr, strlen(cmdstr) + 2);
589 cmdstr_ptr = strstr(cmdstr, "\\n");
590 memmove(cmdstr_ptr + 1, cmdstr_ptr, strlen(cmdstr_ptr) + 1);
591 cmdstr_ptr[0] = '\\';
592 cmdstr_ptr += 3;
593 }
594#endif
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000595 do {
596 sed_cmd_t *sed_cmd;
597 sed_cmd = xcalloc(1, sizeof(sed_cmd_t));
598 cmdstr = add_cmd(sed_cmd, cmdstr);
599 } while (cmdstr && strlen(cmdstr));
Mark Whitley6315ce62000-07-10 22:55:51 +0000600}
601
602
603static void load_cmd_file(char *filename)
604{
605 FILE *cmdfile;
606 char *line;
Mark Whitley94074a92000-07-14 00:00:15 +0000607 char *nextline;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000608 char *e;
Mark Whitley6315ce62000-07-10 22:55:51 +0000609
Manuel Novoa III cad53642003-03-19 09:13:01 +0000610 cmdfile = bb_xfopen(filename, "r");
Mark Whitley6315ce62000-07-10 22:55:51 +0000611
Manuel Novoa III cad53642003-03-19 09:13:01 +0000612 while ((line = bb_get_line_from_file(cmdfile)) != NULL) {
Mark Whitley94074a92000-07-14 00:00:15 +0000613 /* if a line ends with '\' it needs the next line appended to it */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000614 while (((e = last_char_is(line, '\n')) != NULL)
615 && (e > line) && (e[-1] == '\\')
616 && ((nextline = bb_get_line_from_file(cmdfile)) != NULL)) {
617 line = xrealloc(line, (e - line) + 1 + strlen(nextline) + 1);
Mark Whitley94074a92000-07-14 00:00:15 +0000618 strcat(line, nextline);
Mark Whitley464c5de2000-07-14 23:24:00 +0000619 free(nextline);
Mark Whitley94074a92000-07-14 00:00:15 +0000620 }
Mark Whitley464c5de2000-07-14 23:24:00 +0000621 /* eat trailing newline (if any) --if I don't do this, edit commands
622 * (aic) will print an extra newline */
Matt Kraai05e782d2001-02-01 16:49:30 +0000623 chomp(line);
Mark Whitley6315ce62000-07-10 22:55:51 +0000624 add_cmd_str(line);
625 free(line);
626 }
627}
628
Eric Andersenc52a6b02001-11-10 10:49:42 +0000629struct pipeline {
630 char *buf;
631 int idx;
632 int len;
633};
634
Eric Andersenb76cb682001-08-22 05:58:16 +0000635#define PIPE_MAGIC 0x7f
636#define PIPE_GROW 64
Eric Andersenc52a6b02001-11-10 10:49:42 +0000637
638void pipe_putc(struct pipeline *const pipeline, char c)
639{
640 if (pipeline->buf[pipeline->idx] == PIPE_MAGIC) {
641 pipeline->buf =
642 xrealloc(pipeline->buf, pipeline->len + PIPE_GROW);
643 memset(pipeline->buf + pipeline->len, 0, PIPE_GROW);
644 pipeline->len += PIPE_GROW;
645 pipeline->buf[pipeline->len - 1] = PIPE_MAGIC;
646 }
647 pipeline->buf[pipeline->idx++] = (c);
648}
649
650#define pipeputc(c) pipe_putc(pipeline, c)
651
652#if 0
Eric Andersenb76cb682001-08-22 05:58:16 +0000653{ if (pipeline[pipeline_idx] == PIPE_MAGIC) { \
654 pipeline = xrealloc(pipeline, pipeline_len+PIPE_GROW); \
655 memset(pipeline+pipeline_len, 0, PIPE_GROW); \
656 pipeline_len += PIPE_GROW; \
657 pipeline[pipeline_len-1] = PIPE_MAGIC; } \
658 pipeline[pipeline_idx++] = (c); }
Eric Andersenc52a6b02001-11-10 10:49:42 +0000659#endif
Eric Andersenb76cb682001-08-22 05:58:16 +0000660
661static void print_subst_w_backrefs(const char *line, const char *replace,
Eric Andersenc52a6b02001-11-10 10:49:42 +0000662 regmatch_t *regmatch, struct pipeline *const pipeline, int matches)
Mark Whitley97562bd2000-07-17 20:06:42 +0000663{
664 int i;
665
666 /* go through the replacement string */
667 for (i = 0; replace[i]; i++) {
668 /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */
669 if (replace[i] == '\\' && isdigit(replace[i+1])) {
670 int j;
671 char tmpstr[2];
672 int backref;
673 ++i; /* i now indexes the backref number, instead of the leading slash */
674 tmpstr[0] = replace[i];
675 tmpstr[1] = 0;
676 backref = atoi(tmpstr);
677 /* print out the text held in regmatch[backref] */
Matt Kraaia3e4f452001-08-20 21:21:06 +0000678 if (backref <= matches && regmatch[backref].rm_so != -1)
679 for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; 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...) */
693 else if (replace[i] == '&' && replace[i-1] != '\\') {
694 int j;
695 for (j = regmatch[0].rm_so; j < regmatch[0].rm_eo; j++)
Eric Andersenb76cb682001-08-22 05:58:16 +0000696 pipeputc(line[j]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000697 }
698 /* nothing special, just print this char of the replacement string to stdout */
699 else
Eric Andersenb76cb682001-08-22 05:58:16 +0000700 pipeputc(replace[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000701 }
702}
703
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +0000704static int do_subst_command(const sed_cmd_t *sed_cmd, char **line)
Mark Whitley4f7fe772000-07-13 20:01:58 +0000705{
Eric Andersenb76cb682001-08-22 05:58:16 +0000706 char *hackline = *line;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000707 struct pipeline thepipe = { NULL, 0 , 0};
708 struct pipeline *const pipeline = &thepipe;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000709 int altered = 0;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000710 regmatch_t *regmatch = NULL;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000711
Mark Whitley2dc192f2000-11-03 19:47:00 +0000712 /* we only proceed if the substitution 'search' expression matches */
Eric Andersenb76cb682001-08-22 05:58:16 +0000713 if (regexec(sed_cmd->sub_match, hackline, 0, NULL, 0) == REG_NOMATCH)
Mark Whitley2dc192f2000-11-03 19:47:00 +0000714 return 0;
715
716 /* whaddaya know, it matched. get the number of back references */
717 regmatch = xmalloc(sizeof(regmatch_t) * (sed_cmd->num_backrefs+1));
718
Eric Andersenb76cb682001-08-22 05:58:16 +0000719 /* allocate more PIPE_GROW bytes
720 if replaced string is larger than original */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000721 thepipe.len = strlen(hackline)+PIPE_GROW;
722 thepipe.buf = xcalloc(1, thepipe.len);
Eric Andersenb76cb682001-08-22 05:58:16 +0000723 /* buffer magic */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000724 thepipe.buf[thepipe.len-1] = PIPE_MAGIC;
Eric Andersenb76cb682001-08-22 05:58:16 +0000725
Mark Whitley2dc192f2000-11-03 19:47:00 +0000726 /* and now, as long as we've got a line to try matching and if we can match
727 * the search string, we make substitutions */
Matt Kraai8470b9a2001-10-23 21:12:07 +0000728 while ((*hackline || !altered) && (regexec(sed_cmd->sub_match, hackline,
729 sed_cmd->num_backrefs+1, regmatch, 0) != REG_NOMATCH) ) {
Mark Whitley4f7fe772000-07-13 20:01:58 +0000730 int i;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000731
Mark Whitley2dc192f2000-11-03 19:47:00 +0000732 /* print everything before the match */
733 for (i = 0; i < regmatch[0].rm_so; i++)
Eric Andersenb76cb682001-08-22 05:58:16 +0000734 pipeputc(hackline[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000735
Mark Whitley2dc192f2000-11-03 19:47:00 +0000736 /* then print the substitution string */
Eric Andersenb76cb682001-08-22 05:58:16 +0000737 print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch,
Eric Andersenc52a6b02001-11-10 10:49:42 +0000738 pipeline, sed_cmd->num_backrefs);
Mark Whitley97562bd2000-07-17 20:06:42 +0000739
Mark Whitley2dc192f2000-11-03 19:47:00 +0000740 /* advance past the match */
741 hackline += regmatch[0].rm_eo;
742 /* flag that something has changed */
743 altered++;
Mark Whitley97562bd2000-07-17 20:06:42 +0000744
Mark Whitley2dc192f2000-11-03 19:47:00 +0000745 /* if we're not doing this globally, get out now */
746 if (!sed_cmd->sub_g)
747 break;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000748 }
749
Eric Andersenb76cb682001-08-22 05:58:16 +0000750 for (; *hackline; hackline++) pipeputc(*hackline);
Eric Andersenc52a6b02001-11-10 10:49:42 +0000751 if (thepipe.buf[thepipe.idx] == PIPE_MAGIC) thepipe.buf[thepipe.idx] = 0;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000752
753 /* cleanup */
754 free(regmatch);
755
Eric Andersenb76cb682001-08-22 05:58:16 +0000756 free(*line);
Eric Andersenc52a6b02001-11-10 10:49:42 +0000757 *line = thepipe.buf;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000758 return altered;
759}
Mark Whitley6315ce62000-07-10 22:55:51 +0000760
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000761static sed_cmd_t *branch_to(const char *label)
762{
763 sed_cmd_t *sed_cmd;
764 for(sed_cmd = sed_cmd_head.linear; sed_cmd; sed_cmd = sed_cmd->linear) {
765 if (strcmp(sed_cmd->label, label) == 0) {
766 break;
767 }
768 }
769
770 /* If no match returns last command */
771 return(sed_cmd);
772}
Mark Whitley6315ce62000-07-10 22:55:51 +0000773
774static void process_file(FILE *file)
775{
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000776 char *line;
Mark Whitley6315ce62000-07-10 22:55:51 +0000777 static int linenum = 0; /* GNU sed does not restart counting lines at EOF */
778 unsigned int still_in_range = 0;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000779 int altered;
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +0000780 int force_print;
Mark Whitley6315ce62000-07-10 22:55:51 +0000781
Manuel Novoa III cad53642003-03-19 09:13:01 +0000782 line = bb_get_chomped_line_from_file(file);
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000783 if (line == NULL) {
784 return;
785 }
786
Mark Whitley6315ce62000-07-10 22:55:51 +0000787 /* go through every line in the file */
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000788 do {
789 char *next_line;
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000790 sed_cmd_t *sed_cmd;
Glenn L McGrath595a6a42003-03-28 08:36:52 +0000791 int substituted = 0;
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000792
793 /* Read one line in advance so we can act on the last line, the '$' address */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000794 next_line = bb_get_chomped_line_from_file(file);
Mark Whitley6315ce62000-07-10 22:55:51 +0000795
796 linenum++;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000797 altered = 0;
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +0000798 force_print = 0;
Mark Whitley6315ce62000-07-10 22:55:51 +0000799
800 /* for every line, go through all the commands */
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000801 for (sed_cmd = sed_cmd_head.linear; sed_cmd; sed_cmd = sed_cmd->linear) {
Robert Griebl47abc492002-06-11 23:43:27 +0000802 int deleted = 0;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000803
804 /*
805 * entry point into sedding...
806 */
Robert Griebl47abc492002-06-11 23:43:27 +0000807 int matched = (
Matt Kraai02c40a72001-06-21 13:57:51 +0000808 /* no range necessary */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000809 (sed_cmd->beg_line == 0 && sed_cmd->end_line == 0 &&
810 sed_cmd->beg_match == NULL &&
811 sed_cmd->end_match == NULL) ||
Mark Whitley0915c4b2001-06-11 23:50:06 +0000812 /* this line number is the first address we're looking for */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000813 (sed_cmd->beg_line && (sed_cmd->beg_line == linenum)) ||
Mark Whitley0915c4b2001-06-11 23:50:06 +0000814 /* this line matches our first address regex */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000815 (sed_cmd->beg_match && (regexec(sed_cmd->beg_match, line, 0, NULL, 0) == 0)) ||
Mark Whitley0915c4b2001-06-11 23:50:06 +0000816 /* we are currently within the beginning & ending address range */
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000817 still_in_range || ((sed_cmd->beg_line == -1) && (next_line == NULL))
Robert Griebl47abc492002-06-11 23:43:27 +0000818 );
819
820 if (sed_cmd->invert ^ matched) {
Mark Whitley0915c4b2001-06-11 23:50:06 +0000821
822 /*
823 * actual sedding
824 */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000825 switch (sed_cmd->cmd) {
Glenn L McGrath0a65e192002-12-23 10:16:12 +0000826 case '=':
827 printf("%d\n", linenum);
828 break;
Glenn L McGrath4157a8a2003-03-10 04:12:35 +0000829 case 'P': { /* Write the current pattern space upto the first newline */
830 char *tmp = strchr(line, '\n');
831 if (tmp) {
832 *tmp = '\0';
833 }
834 }
835 case 'p': /* Write the current pattern space to output */
Mark Whitley0915c4b2001-06-11 23:50:06 +0000836 puts(line);
837 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000838 case 'd':
839 altered++;
Matt Kraai5c69cd82002-04-01 16:17:37 +0000840 deleted = 1;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000841 break;
842
843 case 's':
844
845 /*
846 * Some special cases for 's' printing to make it compliant with
847 * GNU sed printing behavior (aka "The -n | s///p Matrix"):
848 *
849 * -n ONLY = never print anything regardless of any successful
850 * substitution
851 *
852 * s///p ONLY = always print successful substitutions, even if
853 * the line is going to be printed anyway (line will be printed
854 * twice).
855 *
856 * -n AND s///p = print ONLY a successful substitution ONE TIME;
857 * no other lines are printed - this is the reason why the 'p'
858 * flag exists in the first place.
859 */
860
Glenn L McGrath0c518322003-03-30 03:41:53 +0000861#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
862 /* HACK: escape newlines twice so regex can match them */
863 {
864 int offset = 0;
865 while(strchr(line + offset, '\n') != NULL) {
866 char *tmp;
867 line = xrealloc(line, strlen(line) + 2);
868 tmp = strchr(line + offset, '\n');
869 memmove(tmp + 1, tmp, strlen(tmp) + 1);
870 tmp[0] = '\\';
871 tmp[1] = 'n';
872 offset = tmp - line + 2;
873 }
874 }
875#endif
Mark Whitley0915c4b2001-06-11 23:50:06 +0000876 /* we print the line once, unless we were told to be quiet */
Glenn L McGrath595a6a42003-03-28 08:36:52 +0000877 substituted = do_subst_command(sed_cmd, &line);
Glenn L McGrath0c518322003-03-30 03:41:53 +0000878
879#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
880 /* undo HACK: escape newlines twice so regex can match them */
881 {
882 char *tmp = line;
883
884 while((tmp = strstr(tmp, "\\n")) != NULL) {
885 memmove(tmp, tmp + 1, strlen(tmp + 1) + 1);
886 tmp[0] = '\n';
887 }
888 }
889#endif
Glenn L McGrath595a6a42003-03-28 08:36:52 +0000890 altered |= substituted;
891 if (!be_quiet && altered && ((sed_cmd->linear == NULL) || (sed_cmd->linear->cmd != 's'))) {
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +0000892 force_print = 1;
Glenn L McGrathccd43a82003-03-28 07:44:03 +0000893 }
Mark Whitley0915c4b2001-06-11 23:50:06 +0000894
895 /* we also print the line if we were given the 'p' flag
896 * (this is quite possibly the second printing) */
Glenn L McGrath595a6a42003-03-28 08:36:52 +0000897 if ((sed_cmd->sub_p) && altered) {
898 puts(line);
Glenn L McGrathccd43a82003-03-28 07:44:03 +0000899 }
Mark Whitley0915c4b2001-06-11 23:50:06 +0000900 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000901 case 'a':
902 puts(line);
Eric Andersenc52a6b02001-11-10 10:49:42 +0000903 fputs(sed_cmd->editline, stdout);
Mark Whitley0915c4b2001-06-11 23:50:06 +0000904 altered++;
905 break;
906
907 case 'i':
Eric Andersenc52a6b02001-11-10 10:49:42 +0000908 fputs(sed_cmd->editline, stdout);
Mark Whitley0915c4b2001-06-11 23:50:06 +0000909 break;
910
911 case 'c':
912 /* single-address case */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000913 if ((sed_cmd->end_match == NULL && sed_cmd->end_line == 0)
Mark Whitley0915c4b2001-06-11 23:50:06 +0000914 /* multi-address case */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000915 /* - matching text */
916 || (sed_cmd->end_match && (regexec(sed_cmd->end_match, line, 0, NULL, 0) == 0))
917 /* - matching line numbers */
918 || (sed_cmd->end_line > 0 && sed_cmd->end_line == linenum))
919 {
920 fputs(sed_cmd->editline, stdout);
Mark Whitley0915c4b2001-06-11 23:50:06 +0000921 }
922 altered++;
923
924 break;
925
926 case 'r': {
Glenn L McGrathbed40332003-03-10 02:21:14 +0000927 FILE *outfile;
928 puts(line);
929 outfile = fopen(sed_cmd->filename, "r");
930 if (outfile)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000931 bb_xprint_and_close_file(outfile);
Glenn L McGrathbed40332003-03-10 02:21:14 +0000932 /* else if we couldn't open the output file,
933 * no biggie, just don't print anything */
934 altered++;
935 }
936 break;
937 case 'q': /* Branch to end of script and quit */
Glenn L McGrathb08e3e82003-03-28 04:57:52 +0000938 deleted = 1;
939 /* Exit the outer while loop */
940 free(next_line);
941 next_line = NULL;
942 break;
Glenn L McGrathff724fb2003-03-10 02:56:56 +0000943 case 'n': /* Read next line from input */
Glenn L McGrath4157a8a2003-03-10 04:12:35 +0000944 free(line);
945 line = next_line;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000946 next_line = bb_get_chomped_line_from_file(file);
Glenn L McGrath4157a8a2003-03-10 04:12:35 +0000947 linenum++;
Glenn L McGrathff724fb2003-03-10 02:56:56 +0000948 break;
Glenn L McGrath4157a8a2003-03-10 04:12:35 +0000949 case 'N': /* Append the next line to the current line */
Glenn L McGrath0c518322003-03-30 03:41:53 +0000950 if (next_line) {
951 line = realloc(line, strlen(line) + strlen(next_line) + 2);
952 strcat(line, "\n");
953 strcat(line, next_line);
954 next_line = bb_get_chomped_line_from_file(file);
955 linenum++;
956 }
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000957 break;
958 case 'b':
959 sed_cmd = branch_to(sed_cmd->label);
960 break;
Glenn L McGrath595a6a42003-03-28 08:36:52 +0000961 case 't':
962 if (substituted) {
963 sed_cmd = branch_to(sed_cmd->label);
964 }
965 break;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000966 case 'y': {
967 int i;
968 for (i = 0; line[i] != 0; i++) {
969 int j;
970 for (j = 0; sed_cmd->translate[j] ;j += 2) {
971 if (line[i] == sed_cmd->translate[j]) {
972 line[i] = sed_cmd->translate[j + 1];
973 }
974 }
975 }
976 }
977 break;
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000978// case ':':
979// break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000980 }
Robert Griebl47abc492002-06-11 23:43:27 +0000981 }
Mark Whitley0915c4b2001-06-11 23:50:06 +0000982
Robert Griebl47abc492002-06-11 23:43:27 +0000983 /*
984 * exit point from sedding...
985 */
986 if (matched) {
Mark Whitley0915c4b2001-06-11 23:50:06 +0000987 if (
988 /* this is a single-address command or... */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000989 (sed_cmd->end_line == 0 && sed_cmd->end_match == NULL) || (
Mark Whitley0915c4b2001-06-11 23:50:06 +0000990 /* we were in the middle of our address range (this
991 * isn't the first time through) and.. */
992 (still_in_range == 1) && (
993 /* this line number is the last address we're looking for or... */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000994 (sed_cmd->end_line && (sed_cmd->end_line == linenum)) ||
Mark Whitley0915c4b2001-06-11 23:50:06 +0000995 /* this line matches our last address regex */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000996 (sed_cmd->end_match && (regexec(sed_cmd->end_match, line, 0, NULL, 0) == 0))
Mark Whitley0915c4b2001-06-11 23:50:06 +0000997 )
998 )
999 ) {
1000 /* we're out of our address range */
1001 still_in_range = 0;
1002 }
1003
1004 /* didn't hit the exit? then we're still in the middle of an address range */
1005 else {
1006 still_in_range = 1;
Mark Whitley6315ce62000-07-10 22:55:51 +00001007 }
1008 }
Robert Griebl47abc492002-06-11 23:43:27 +00001009
1010 if (deleted)
1011 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001012 }
Erik Andersen1266a131999-12-29 22:19:46 +00001013
Mark Whitley2dc192f2000-11-03 19:47:00 +00001014 /* we will print the line unless we were told to be quiet or if the
1015 * line was altered (via a 'd'elete or 's'ubstitution), in which case
1016 * the altered line was already printed */
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +00001017 if ((!be_quiet && !altered) || force_print){
Mark Whitleydd527d32001-05-14 19:53:08 +00001018 puts(line);
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +00001019 }
Mark Whitley6315ce62000-07-10 22:55:51 +00001020 free(line);
Glenn L McGrath505bd0f2003-03-08 05:21:02 +00001021 line = next_line;
1022 } while (line);
Erik Andersen1266a131999-12-29 22:19:46 +00001023}
1024
1025extern int sed_main(int argc, char **argv)
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001026{
Matt Kraaia5f09c62001-11-12 16:44:55 +00001027 int opt, status = EXIT_SUCCESS;
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001028
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001029#ifdef CONFIG_FEATURE_CLEAN_UP
Mark Whitley858c1ad2000-07-11 21:38:47 +00001030 /* destroy command strings on exit */
Matt Kraaia9819b22000-12-22 01:48:07 +00001031 if (atexit(destroy_cmd_strs) == -1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001032 bb_perror_msg_and_die("atexit");
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001033#endif
Mark Whitley858c1ad2000-07-11 21:38:47 +00001034
Mark Whitley6315ce62000-07-10 22:55:51 +00001035 /* do normal option parsing */
Eric Andersenb50da532001-02-17 16:52:35 +00001036 while ((opt = getopt(argc, argv, "ne:f:")) > 0) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001037 switch (opt) {
Erik Andersene916d242000-03-06 19:20:35 +00001038 case 'n':
Mark Whitley6315ce62000-07-10 22:55:51 +00001039 be_quiet++;
Erik Andersene916d242000-03-06 19:20:35 +00001040 break;
Glenn L McGrath0c518322003-03-30 03:41:53 +00001041 case 'e': {
1042 char *str_cmd = strdup(optarg);
1043 add_cmd_str(str_cmd);
1044 free(str_cmd);
Erik Andersene916d242000-03-06 19:20:35 +00001045 break;
Glenn L McGrath0c518322003-03-30 03:41:53 +00001046 }
Mark Whitley6315ce62000-07-10 22:55:51 +00001047 case 'f':
1048 load_cmd_file(optarg);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001049 break;
Eric Andersenb50da532001-02-17 16:52:35 +00001050 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001051 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001052 }
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001053 }
Mark Whitley6315ce62000-07-10 22:55:51 +00001054
1055 /* if we didn't get a pattern from a -e and no command file was specified,
1056 * argv[optind] should be the pattern. no pattern, no worky */
Glenn L McGrathc949bfa2003-03-28 03:53:31 +00001057 if (sed_cmd_head.linear == NULL) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001058 if (argv[optind] == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001059 bb_show_usage();
Mark Whitley6315ce62000-07-10 22:55:51 +00001060 else {
Glenn L McGrath0c518322003-03-30 03:41:53 +00001061 char *str_cmd = strdup(argv[optind]);
1062 add_cmd_str(strdup(str_cmd));
1063 free(str_cmd);
Mark Whitley6315ce62000-07-10 22:55:51 +00001064 optind++;
1065 }
1066 }
1067
Mark Whitley6315ce62000-07-10 22:55:51 +00001068 /* argv[(optind)..(argc-1)] should be names of file to process. If no
1069 * files were specified or '-' was specified, take input from stdin.
1070 * Otherwise, we process all the files specified. */
1071 if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) {
1072 process_file(stdin);
1073 }
1074 else {
1075 int i;
1076 FILE *file;
1077 for (i = optind; i < argc; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001078 file = bb_wfopen(argv[i], "r");
Eric Andersen9c6b5fc2001-11-17 07:23:46 +00001079 if (file) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001080 process_file(file);
1081 fclose(file);
Matt Kraaia5f09c62001-11-12 16:44:55 +00001082 } else
1083 status = EXIT_FAILURE;
Mark Whitley6315ce62000-07-10 22:55:51 +00001084 }
1085 }
1086
Matt Kraaia5f09c62001-11-12 16:44:55 +00001087 return status;
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001088}