blob: 71542d2ac86e2c72a480c9fc9945dd93d7b12f6c [file] [log] [blame]
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001/* vi: set sw=4 ts=4: */
Eric Andersen6b6b3f61999-10-28 16:06:25 +00002/*
Mark Whitley6315ce62000-07-10 22:55:51 +00003 * sed.c - very minimalist version of sed
Eric Andersen6b6b3f61999-10-28 16:06:25 +00004 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
6 * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org>
Matt Kraai5ed78ad2002-01-03 21:12:34 +00007 * Copyright (C) 2002 Matt Kraai
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00008 * Copyright (C) 2003 by Glenn McGrath <bug1@iinet.net.au>
Rob Landley25d82392004-04-01 09:23:30 +00009 * Copyright (C) 2003,2004 by Rob Landley <rob@landley.net>
Erik Andersen1266a131999-12-29 22:19:46 +000010 *
Rob Landley2b26fd52006-02-24 02:30:39 +000011 * MAINTAINER: Rob Landley <rob@landley.net>
Bernhard Reutner-Fischerb7f39732006-03-01 20:14:16 +000012 *
Rob Landleyfae1dc82005-11-20 07:44:35 +000013 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen6b6b3f61999-10-28 16:06:25 +000014 */
15
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000016/* Code overview.
17
18 Files are laid out to avoid unnecessary function declarations. So for
19 example, every function add_cmd calls occurs before add_cmd in this file.
20
21 add_cmd() is called on each line of sed command text (from a file or from
22 the command line). It calls get_address() and parse_cmd_args(). The
23 resulting sed_cmd_t structures are appended to a linked list
Rob Landleye3f5a3f2006-05-09 03:53:55 +000024 (bbg.sed_cmd_head/bbg.sed_cmd_tail).
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000025
Rob Landleydcc28662004-11-25 07:21:47 +000026 add_input_file() adds a FILE * to the list of input files. We need to
Rob Landleyfae1dc82005-11-20 07:44:35 +000027 know all input sources ahead of time to find the last line for the $ match.
Rob Landleydcc28662004-11-25 07:21:47 +000028
29 process_files() does actual sedding, reading data lines from each input FILE *
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000030 (which could be stdin) and applying the sed command list (sed_cmd_head) to
31 each of the resulting lines.
32
33 sed_main() is where external code calls into this, with a command line.
34*/
35
36
Mark Whitley6315ce62000-07-10 22:55:51 +000037/*
38 Supported features and commands in this version of sed:
39
40 - comments ('#')
Mark Whitley94074a92000-07-14 00:00:15 +000041 - address matching: num|/matchstr/[,num|/matchstr/|$]command
42 - commands: (p)rint, (d)elete, (s)ubstitue (with g & I flags)
43 - edit commands: (a)ppend, (i)nsert, (c)hange
Mark Whitley1f3b9f22001-05-11 22:27:13 +000044 - file commands: (r)ead
Rob Landleyc63fe912005-10-30 10:08:13 +000045 - backreferences in substitution expressions (\0, \1, \2...\9)
Glenn L McGrathf50ce312003-03-09 15:12:24 +000046 - grouped commands: {cmd1;cmd2}
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000047 - transliteration (y/source-chars/dest-chars/)
48 - pattern space hold space storing / swapping (g, h, x)
Rob Landley93850a52005-05-18 06:34:37 +000049 - labels / branching (: label, b, t, T)
Glenn L McGrathf50ce312003-03-09 15:12:24 +000050
Mark Whitley6315ce62000-07-10 22:55:51 +000051 (Note: Specifying an address (range) to match is *optional*; commands
52 default to the whole pattern space if no specific address match was
53 requested.)
54
Glenn L McGrath2570b432003-09-16 05:25:43 +000055 Todo:
Glenn L McGrath2570b432003-09-16 05:25:43 +000056 - Create a wrapper around regex to make libc's regex conform with sed
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000057
Glenn L McGrathd5eadea2003-03-09 02:39:29 +000058 Reference http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html
Mark Whitley6315ce62000-07-10 22:55:51 +000059*/
60
Eric Andersen6b6b3f61999-10-28 16:06:25 +000061#include <stdio.h>
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000062#include <unistd.h> /* for getopt() */
Eric Andersen6b6b3f61999-10-28 16:06:25 +000063#include <errno.h>
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000064#include <ctype.h> /* for isspace() */
Eric Andersened3ef502001-01-27 08:24:39 +000065#include <stdlib.h>
Rob Landleye3f5a3f2006-05-09 03:53:55 +000066#include <string.h>
Eric Andersen3570a342000-09-25 21:45:58 +000067#include "busybox.h"
"Vladimir N. Oleynik"23f62fc2005-09-14 16:59:11 +000068#include "xregex.h"
Mark Whitley6315ce62000-07-10 22:55:51 +000069
Rob Landleye3f5a3f2006-05-09 03:53:55 +000070/* Each sed command turns into one of these structures. */
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +000071typedef struct sed_cmd_s {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000072 /* Ordered by alignment requirements: currently 36 bytes on x86 */
Mark Whitley2dc192f2000-11-03 19:47:00 +000073
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000074 /* address storage */
75 regex_t *beg_match; /* sed -e '/match/cmd' */
76 regex_t *end_match; /* sed -e '/match/,/end_match/cmd' */
77 regex_t *sub_match; /* For 's/sub_match/string/' */
78 int beg_line; /* 'sed 1p' 0 == apply commands to all lines */
79 int end_line; /* 'sed 1,3p' 0 == one line only. -1 = last line ($) */
Bernhard Reutner-Fischerb7f39732006-03-01 20:14:16 +000080
Rob Landley2b26fd52006-02-24 02:30:39 +000081 FILE *file; /* File (sw) command writes to, -1 for none. */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000082 char *string; /* Data string for (saicytb) commands. */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +000083
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000084 unsigned short which_match; /* (s) Which match to replace (0 for all) */
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +000085
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000086 /* Bitfields (gcc won't group them if we don't) */
87 unsigned int invert:1; /* the '!' after the address */
88 unsigned int in_match:1; /* Next line also included in match? */
Rob Landley2b26fd52006-02-24 02:30:39 +000089 unsigned int no_newline:1; /* Last line written by (sw) had no '\n' */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000090 unsigned int sub_p:1; /* (s) print option */
Glenn L McGrathc18ce372003-09-13 06:57:39 +000091
Glenn L McGrathaa5a6022003-10-01 03:06:16 +000092 /* GENERAL FIELDS */
93 char cmd; /* The command char: abcdDgGhHilnNpPqrstwxy:={} */
94 struct sed_cmd_s *next; /* Next command (linked list, NULL terminated) */
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +000095} sed_cmd_t;
Mark Whitley6315ce62000-07-10 22:55:51 +000096
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000097static const char bad_format_in_subst[] =
98 "bad format in substitution expression";
Eric Andersen14f5c8d2005-04-16 19:39:00 +000099static const char *const semicolon_whitespace = "; \n\r\t\v";
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000100
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000101struct sed_globals
102{
103 /* options */
104 int be_quiet, in_place, regex_type;
105 FILE *nonstdout;
106 char *outname, *hold_space;
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000107
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000108 /* List of input files */
109 int input_file_count,current_input_file;
110 FILE **input_file_list;
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000111
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000112 regmatch_t regmatch[10];
113 regex_t *previous_regex_ptr;
114
115 /* linked list of sed commands */
116 sed_cmd_t sed_cmd_head, *sed_cmd_tail;
117
118 /* Linked list of append lines */
119 llist_t *append_head;
120
121 char *add_cmd_line;
122
123 struct pipeline {
124 char *buf; /* Space to hold string */
125 int idx; /* Space used */
126 int len; /* Space allocated */
127 } pipeline;
128} bbg;
129
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000130
Bernhard Reutner-Fischerc0bb3082006-03-02 10:06:22 +0000131void sed_free_and_close_stuff(void);
Bernhard Reutner-Fischerb7f39732006-03-01 20:14:16 +0000132#if ENABLE_FEATURE_CLEAN_UP
Bernhard Reutner-Fischerc0bb3082006-03-02 10:06:22 +0000133void sed_free_and_close_stuff(void)
Mark Whitley6315ce62000-07-10 22:55:51 +0000134{
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000135 sed_cmd_t *sed_cmd = bbg.sed_cmd_head.next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000136
Rob Landley5fc467e2006-05-24 17:45:47 +0000137 llist_free(bbg.append_head, free);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000138
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000139 while (sed_cmd) {
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000140 sed_cmd_t *sed_cmd_next = sed_cmd->next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000141
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000142 if(sed_cmd->file)
143 bb_xprint_and_close_file(sed_cmd->file);
144
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000145 if (sed_cmd->beg_match) {
146 regfree(sed_cmd->beg_match);
147 free(sed_cmd->beg_match);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000149 if (sed_cmd->end_match) {
150 regfree(sed_cmd->end_match);
151 free(sed_cmd->end_match);
Mark Whitley6315ce62000-07-10 22:55:51 +0000152 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000153 if (sed_cmd->sub_match) {
154 regfree(sed_cmd->sub_match);
155 free(sed_cmd->sub_match);
Mark Whitley6315ce62000-07-10 22:55:51 +0000156 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000157 free(sed_cmd->string);
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000158 free(sed_cmd);
159 sed_cmd = sed_cmd_next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000160 }
Rob Landleyce4f0e92004-10-30 06:54:19 +0000161
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000162 if(bbg.hold_space) free(bbg.hold_space);
Rob Landleydcc28662004-11-25 07:21:47 +0000163
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000164 while(bbg.current_input_file<bbg.input_file_count)
165 fclose(bbg.input_file_list[bbg.current_input_file++]);
Mark Whitley6315ce62000-07-10 22:55:51 +0000166}
Mark Whitleyc41e8c82000-07-12 23:35:21 +0000167#endif
Mark Whitley6315ce62000-07-10 22:55:51 +0000168
Rob Landley53302f82004-02-18 09:54:15 +0000169/* If something bad happens during -i operation, delete temp file */
170
171static void cleanup_outname(void)
172{
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000173 if(bbg.outname) unlink(bbg.outname);
Rob Landley53302f82004-02-18 09:54:15 +0000174}
175
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000176/* strdup, replacing "\n" with '\n', and "\delimiter" with 'delimiter' */
177
178static void parse_escapes(char *dest, const char *string, int len, char from, char to)
179{
180 int i=0;
181
182 while(i<len) {
183 if(string[i] == '\\') {
Glenn L McGrath738fb332003-10-01 06:45:11 +0000184 if(!to || string[i+1] == from) {
185 *(dest++) = to ? to : string[i+1];
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000186 i+=2;
187 continue;
188 } else *(dest++)=string[i++];
189 }
190 *(dest++) = string[i++];
191 }
192 *dest=0;
193}
194
Rob Landleyfae1dc82005-11-20 07:44:35 +0000195static char *copy_parsing_escapes(const char *string, int len)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000196{
197 char *dest=xmalloc(len+1);
198
199 parse_escapes(dest,string,len,'n','\n');
200 return dest;
201}
202
203
Mark Whitley6315ce62000-07-10 22:55:51 +0000204/*
Eric Andersen28b3c532001-01-02 11:01:31 +0000205 * index_of_next_unescaped_regexp_delim - walks left to right through a string
206 * beginning at a specified index and returns the index of the next regular
Eric Andersenaff114c2004-04-14 17:51:38 +0000207 * expression delimiter (typically a forward * slash ('/')) not preceded by
Eric Andersen28b3c532001-01-02 11:01:31 +0000208 * a backslash ('\').
Mark Whitley6315ce62000-07-10 22:55:51 +0000209 */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000210static int index_of_next_unescaped_regexp_delim(const char delimiter,
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000211 const char *str)
Mark Whitley6315ce62000-07-10 22:55:51 +0000212{
Matt Kraaia0065d52001-08-24 14:45:50 +0000213 int bracket = -1;
214 int escaped = 0;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000215 int idx = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000216 char ch;
Matt Kraaia0065d52001-08-24 14:45:50 +0000217
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000218 for (; (ch = str[idx]); idx++) {
Matt Kraaia0065d52001-08-24 14:45:50 +0000219 if (bracket != -1) {
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000220 if (ch == ']' && !(bracket == idx - 1 || (bracket == idx - 2
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000221 && str[idx - 1] == '^')))
Matt Kraaia0065d52001-08-24 14:45:50 +0000222 bracket = -1;
223 } else if (escaped)
224 escaped = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000225 else if (ch == '\\')
Matt Kraaia0065d52001-08-24 14:45:50 +0000226 escaped = 1;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000227 else if (ch == '[')
Matt Kraaia0065d52001-08-24 14:45:50 +0000228 bracket = idx;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000229 else if (ch == delimiter)
Mark Whitley97562bd2000-07-17 20:06:42 +0000230 return idx;
231 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000232
Mark Whitley97562bd2000-07-17 20:06:42 +0000233 /* if we make it to here, we've hit the end of the string */
234 return -1;
Mark Whitley6315ce62000-07-10 22:55:51 +0000235}
236
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000237/*
238 * Returns the index of the third delimiter
239 */
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000240static int parse_regex_delim(const char *cmdstr, char **match, char **replace)
241{
242 const char *cmdstr_ptr = cmdstr;
243 char delimiter;
244 int idx = 0;
245
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000246 /* verify that the 's' or 'y' is followed by something. That something
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000247 * (typically a 'slash') is now our regexp delimiter... */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000248 if (*cmdstr == '\0') bb_error_msg_and_die(bad_format_in_subst);
249 delimiter = *(cmdstr_ptr++);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000250
251 /* save the match string */
252 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
253 if (idx == -1) {
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000254 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000255 }
Rob Landleyfae1dc82005-11-20 07:44:35 +0000256 *match = copy_parsing_escapes(cmdstr_ptr, idx);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000257
258 /* save the replacement string */
259 cmdstr_ptr += idx + 1;
260 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
261 if (idx == -1) {
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000262 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000263 }
Rob Landleyfae1dc82005-11-20 07:44:35 +0000264 *replace = copy_parsing_escapes(cmdstr_ptr, idx);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000265
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000266 return ((cmdstr_ptr - cmdstr) + idx);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000267}
268
Mark Whitley6315ce62000-07-10 22:55:51 +0000269/*
270 * returns the index in the string just past where the address ends.
271 */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000272static int get_address(char *my_str, int *linenum, regex_t ** regex)
Mark Whitley6315ce62000-07-10 22:55:51 +0000273{
Glenn L McGrathe3e28d32003-09-15 09:22:04 +0000274 char *pos = my_str;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000275
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000276 if (isdigit(*my_str)) {
277 *linenum = strtol(my_str, &pos, 10);
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000278 /* endstr shouldnt ever equal NULL */
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000279 } else if (*my_str == '$') {
Mark Whitley0915c4b2001-06-11 23:50:06 +0000280 *linenum = -1;
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000281 pos++;
282 } else if (*my_str == '/' || *my_str == '\\') {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000283 int next;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000284 char delimiter;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000285 char *temp;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000286
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000287 if (*my_str == '\\') delimiter = *(++pos);
288 else delimiter = '/';
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000289 next = index_of_next_unescaped_regexp_delim(delimiter, ++pos);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000290 if (next == -1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000291 bb_error_msg_and_die("unterminated match expression");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000292
Rob Landleyfae1dc82005-11-20 07:44:35 +0000293 temp=copy_parsing_escapes(pos,next);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000294 *regex = (regex_t *) xmalloc(sizeof(regex_t));
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000295 xregcomp(*regex, temp, bbg.regex_type|REG_NEWLINE);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000296 free(temp);
297 /* Move position to next character after last delimiter */
298 pos+=(next+1);
Mark Whitley6315ce62000-07-10 22:55:51 +0000299 }
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000300 return pos - my_str;
Mark Whitley6315ce62000-07-10 22:55:51 +0000301}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000302
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000303/* Grab a filename. Whitespace at start is skipped, then goes to EOL. */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000304static int parse_file_cmd(sed_cmd_t *sed_cmd, const char *filecmdstr, char **retval)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000305{
306 int start = 0, idx, hack=0;
307
308 /* Skip whitespace, then grab filename to end of line */
309 while (isspace(filecmdstr[start])) start++;
310 idx=start;
311 while(filecmdstr[idx] && filecmdstr[idx]!='\n') idx++;
312 /* If lines glued together, put backslash back. */
313 if(filecmdstr[idx]=='\n') hack=1;
314 if(idx==start) bb_error_msg_and_die("Empty filename");
315 *retval = bb_xstrndup(filecmdstr+start, idx-start+hack+1);
316 if(hack) *(idx+*retval)='\\';
317
318 return idx;
319}
320
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000321static int parse_subst_cmd(sed_cmd_t *const sed_cmd, char *substr)
Mark Whitley06f35292000-07-13 19:58:04 +0000322{
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000323 int cflags = bbg.regex_type;
Mark Whitley06f35292000-07-13 19:58:04 +0000324 char *match;
325 int idx = 0;
326
327 /*
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000328 * A substitution command should look something like this:
329 * s/match/replace/ #gIpw
Mark Whitley0e4cec02000-08-21 21:29:20 +0000330 * || | |||
Mark Whitley06f35292000-07-13 19:58:04 +0000331 * mandatory optional
Mark Whitley06f35292000-07-13 19:58:04 +0000332 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000333 idx = parse_regex_delim(substr, &match, &sed_cmd->string);
Mark Whitley06f35292000-07-13 19:58:04 +0000334
Mark Whitley97562bd2000-07-17 20:06:42 +0000335 /* determine the number of back references in the match string */
336 /* Note: we compute this here rather than in the do_subst_command()
337 * function to save processor time, at the expense of a little more memory
338 * (4 bits) per sed_cmd */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000339
Mark Whitley06f35292000-07-13 19:58:04 +0000340 /* process the flags */
Mark Whitley70705d72000-07-14 19:06:30 +0000341
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000342 sed_cmd->which_match=1;
343 while (substr[++idx]) {
344 /* Parse match number */
345 if(isdigit(substr[idx])) {
346 if(match[0]!='^') {
347 /* Match 0 treated as all, multiple matches we take the last one. */
348 char *pos=substr+idx;
349 sed_cmd->which_match=(unsigned short)strtol(substr+idx,&pos,10);
350 idx=pos-substr;
351 }
352 continue;
353 }
Rob Landley40ec4ae2004-01-04 06:42:14 +0000354 /* Skip spaces */
355 if(isspace(substr[idx])) continue;
356
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000357 switch (substr[idx]) {
358 /* Replace all occurrences */
359 case 'g':
360 if (match[0] != '^') sed_cmd->which_match = 0;
361 break;
362 /* Print pattern space */
363 case 'p':
364 sed_cmd->sub_p = 1;
365 break;
Rob Landleyfae1dc82005-11-20 07:44:35 +0000366 /* Write to file */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000367 case 'w':
368 {
369 char *temp;
370 idx+=parse_file_cmd(sed_cmd,substr+idx,&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000371
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000372 break;
373 }
374 /* Ignore case (gnu exension) */
375 case 'I':
376 cflags |= REG_ICASE;
377 break;
Rob Landleyfae1dc82005-11-20 07:44:35 +0000378 /* Comment */
379 case '#':
380 while(substr[++idx]);
381 /* Fall through */
382 /* End of command */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000383 case ';':
384 case '}':
385 goto out;
386 default:
387 bb_error_msg_and_die("bad option in substitution expression");
388 }
389 }
Glenn L McGrath2570b432003-09-16 05:25:43 +0000390out:
Mark Whitley97562bd2000-07-17 20:06:42 +0000391 /* compile the match string into a regex */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000392 if (*match != '\0') {
393 /* If match is empty, we use last regex used at runtime */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000394 sed_cmd->sub_match = (regex_t *) xmalloc(sizeof(regex_t));
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000395 xregcomp(sed_cmd->sub_match, match, cflags);
396 }
Mark Whitley06f35292000-07-13 19:58:04 +0000397 free(match);
Mark Whitley70705d72000-07-14 19:06:30 +0000398
399 return idx;
Mark Whitley06f35292000-07-13 19:58:04 +0000400}
401
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000402/*
403 * Process the commands arguments
404 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000405static char *parse_cmd_args(sed_cmd_t *sed_cmd, char *cmdstr)
Mark Whitley6315ce62000-07-10 22:55:51 +0000406{
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000407 /* handle (s)ubstitution command */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000408 if (sed_cmd->cmd == 's') cmdstr += parse_subst_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000409 /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */
410 else if (strchr("aic", sed_cmd->cmd)) {
411 if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000412 bb_error_msg_and_die
413 ("only a beginning address can be specified for edit commands");
Rob Landley25d82392004-04-01 09:23:30 +0000414 for(;;) {
415 if(*cmdstr=='\n' || *cmdstr=='\\') {
416 cmdstr++;
417 break;
418 } else if(isspace(*cmdstr)) cmdstr++;
419 else break;
420 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000421 sed_cmd->string = bb_xstrdup(cmdstr);
Glenn L McGrath738fb332003-10-01 06:45:11 +0000422 parse_escapes(sed_cmd->string,sed_cmd->string,strlen(cmdstr),0,0);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000423 cmdstr += strlen(cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000424 /* handle file cmds: (r)ead */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000425 } else if(strchr("rw", sed_cmd->cmd)) {
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000426 if (sed_cmd->end_line || sed_cmd->end_match)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000427 bb_error_msg_and_die("Command only uses one address");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000428 cmdstr += parse_file_cmd(sed_cmd, cmdstr, &sed_cmd->string);
429 if(sed_cmd->cmd=='w')
430 sed_cmd->file=bb_xfopen(sed_cmd->string,"w");
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000431 /* handle branch commands */
Rob Landley93850a52005-05-18 06:34:37 +0000432 } else if (strchr(":btT", sed_cmd->cmd)) {
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000433 int length;
434
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000435 while(isspace(*cmdstr)) cmdstr++;
Glenn L McGrathf4523562003-09-14 06:01:14 +0000436 length = strcspn(cmdstr, semicolon_whitespace);
437 if (length) {
Rob Landley1f305dc2006-03-09 22:21:20 +0000438 sed_cmd->string = bb_xstrndup(cmdstr, length);
Glenn L McGrathf4523562003-09-14 06:01:14 +0000439 cmdstr += length;
440 }
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000441 }
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000442 /* translation command */
443 else if (sed_cmd->cmd == 'y') {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000444 char *match, *replace;
445 int i=cmdstr[0];
446
447 cmdstr+=parse_regex_delim(cmdstr, &match, &replace)+1;
448 /* \n already parsed, but \delimiter needs unescaping. */
449 parse_escapes(match,match,strlen(match),i,i);
450 parse_escapes(replace,replace,strlen(replace),i,i);
451
Rob Landley9ffd4232006-05-21 18:30:35 +0000452 sed_cmd->string = xzalloc((strlen(match) + 1) * 2);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000453 for (i = 0; match[i] && replace[i]; i++) {
454 sed_cmd->string[i * 2] = match[i];
455 sed_cmd->string[(i * 2) + 1] = replace[i];
456 }
457 free(match);
458 free(replace);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000459 }
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000460 /* if it wasnt a single-letter command that takes no arguments
461 * then it must be an invalid command.
462 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000463 else if (strchr("dDgGhHlnNpPqx={}", sed_cmd->cmd) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000464 bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd);
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000465 }
Mark Whitley70705d72000-07-14 19:06:30 +0000466
467 /* give back whatever's left over */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000468 return (cmdstr);
Eric Andersen50d63601999-11-09 01:47:36 +0000469}
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000470
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000471
472/* Parse address+command sets, skipping comment lines. */
473
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000474static void add_cmd(char *cmdstr)
Erik Andersen1266a131999-12-29 22:19:46 +0000475{
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000476 sed_cmd_t *sed_cmd;
Glenn L McGrath586d86c2003-10-09 07:22:59 +0000477 int temp;
Glenn L McGrath8aac05b2003-09-14 04:06:12 +0000478
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000479 /* Append this line to any unfinished line from last time. */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000480 if (bbg.add_cmd_line) {
481 cmdstr = bb_xasprintf("%s\n%s", bbg.add_cmd_line, cmdstr);
482 free(bbg.add_cmd_line);
483 bbg.add_cmd_line = cmdstr;
Rob Landley12d87552006-06-05 17:32:44 +0000484 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000485
486 /* If this line ends with backslash, request next line. */
Glenn L McGrath586d86c2003-10-09 07:22:59 +0000487 temp=strlen(cmdstr);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000488 if(temp && cmdstr[temp-1]=='\\') {
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000489 if (!bbg.add_cmd_line) bbg.add_cmd_line = bb_xstrdup(cmdstr);
490 bbg.add_cmd_line[temp-1] = 0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000491 return;
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000492 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000493
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000494 /* Loop parsing all commands in this line. */
495 while(*cmdstr) {
496 /* Skip leading whitespace and semicolons */
497 cmdstr += strspn(cmdstr, semicolon_whitespace);
498
499 /* If no more commands, exit. */
500 if(!*cmdstr) break;
501
502 /* if this is a comment, jump past it and keep going */
503 if (*cmdstr == '#') {
504 /* "#n" is the same as using -n on the command line */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000505 if (cmdstr[1] == 'n') bbg.be_quiet++;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000506 if(!(cmdstr=strpbrk(cmdstr, "\n\r"))) break;
507 continue;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000508 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000509
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000510 /* parse the command
511 * format is: [addr][,addr][!]cmd
512 * |----||-----||-|
513 * part1 part2 part3
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000514 */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000515
Rob Landley9ffd4232006-05-21 18:30:35 +0000516 sed_cmd = xzalloc(sizeof(sed_cmd_t));
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000517
518 /* first part (if present) is an address: either a '$', a number or a /regex/ */
519 cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match);
520
521 /* second part (if present) will begin with a comma */
522 if (*cmdstr == ',') {
523 int idx;
524
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000525 cmdstr++;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000526 idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000527 if (!idx) bb_error_msg_and_die("no address after comma\n");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000528 cmdstr += idx;
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000529 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000530
531 /* skip whitespace before the command */
532 while (isspace(*cmdstr)) cmdstr++;
533
534 /* Check for inversion flag */
535 if (*cmdstr == '!') {
536 sed_cmd->invert = 1;
537 cmdstr++;
538
539 /* skip whitespace before the command */
540 while (isspace(*cmdstr)) cmdstr++;
541 }
542
543 /* last part (mandatory) will be a command */
544 if (!*cmdstr) bb_error_msg_and_die("missing command");
545 sed_cmd->cmd = *(cmdstr++);
546 cmdstr = parse_cmd_args(sed_cmd, cmdstr);
547
548 /* Add the command to the command array */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000549 bbg.sed_cmd_tail->next = sed_cmd;
550 bbg.sed_cmd_tail = bbg.sed_cmd_tail->next;
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000551 }
552
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000553 /* If we glued multiple lines together, free the memory. */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000554 free(bbg.add_cmd_line);
555 bbg.add_cmd_line = NULL;
Mark Whitley6315ce62000-07-10 22:55:51 +0000556}
557
Rob Landleydcc28662004-11-25 07:21:47 +0000558/* Append to a string, reallocating memory as necessary. */
559
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000560#define PIPE_GROW 64
Eric Andersenc52a6b02001-11-10 10:49:42 +0000561
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000562static void pipe_putc(char c)
Eric Andersenc52a6b02001-11-10 10:49:42 +0000563{
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000564 if(bbg.pipeline.idx==bbg.pipeline.len) {
565 bbg.pipeline.buf = xrealloc(bbg.pipeline.buf,
566 bbg.pipeline.len + PIPE_GROW);
567 bbg.pipeline.len+=PIPE_GROW;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000568 }
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000569 bbg.pipeline.buf[bbg.pipeline.idx++] = c;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000570}
571
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000572static void do_subst_w_backrefs(const char *line, const char *replace)
Mark Whitley97562bd2000-07-17 20:06:42 +0000573{
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000574 int i,j;
Mark Whitley97562bd2000-07-17 20:06:42 +0000575
576 /* go through the replacement string */
577 for (i = 0; replace[i]; i++) {
578 /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */
Rob Landleyc63fe912005-10-30 10:08:13 +0000579 if (replace[i] == '\\' && replace[i+1]>='0' && replace[i+1]<='9') {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000580 int backref=replace[++i]-'0';
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000581
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000582 /* print out the text held in bbg.regmatch[backref] */
583 if(bbg.regmatch[backref].rm_so != -1)
584 for (j = bbg.regmatch[backref].rm_so;
585 j < bbg.regmatch[backref].rm_eo; j++) pipe_putc(line[j]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000586 }
587
Mark Whitley83e85f62000-07-25 20:48:44 +0000588 /* if we find a backslash escaped character, print the character */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000589 else if (replace[i] == '\\') pipe_putc(replace[++i]);
Mark Whitley83e85f62000-07-25 20:48:44 +0000590
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000591 /* if we find an unescaped '&' print out the whole matched text. */
592 else if (replace[i] == '&')
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000593 for (j = bbg.regmatch[0].rm_so; j < bbg.regmatch[0].rm_eo; j++)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000594 pipe_putc(line[j]);
595 /* Otherwise just output the character. */
596 else pipe_putc(replace[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000597 }
598}
599
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000600static int do_subst_command(sed_cmd_t *sed_cmd, char **line)
Mark Whitley4f7fe772000-07-13 20:01:58 +0000601{
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000602 char *oldline = *line;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000603 int altered = 0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000604 int match_count=0;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000605 regex_t *current_regex;
606
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000607 /* Handle empty regex. */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000608 if (sed_cmd->sub_match == NULL) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000609 current_regex = bbg.previous_regex_ptr;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000610 if(!current_regex)
611 bb_error_msg_and_die("No previous regexp.");
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000612 } else bbg.previous_regex_ptr = current_regex = sed_cmd->sub_match;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000613
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000614 /* Find the first match */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000615 if(REG_NOMATCH==regexec(current_regex, oldline, 10, bbg.regmatch, 0))
Mark Whitley2dc192f2000-11-03 19:47:00 +0000616 return 0;
617
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000618 /* Initialize temporary output buffer. */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000619 bbg.pipeline.buf=xmalloc(PIPE_GROW);
620 bbg.pipeline.len=PIPE_GROW;
621 bbg.pipeline.idx=0;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000622
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000623 /* Now loop through, substituting for matches */
624 do {
Mark Whitley4f7fe772000-07-13 20:01:58 +0000625 int i;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000626
Eric Andersenc06f5682004-02-04 10:57:46 +0000627 /* Work around bug in glibc regexec, demonstrated by:
628 echo " a.b" | busybox sed 's [^ .]* x g'
629 The match_count check is so not to break
630 echo "hi" | busybox sed 's/^/!/g' */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000631 if(!bbg.regmatch[0].rm_so && !bbg.regmatch[0].rm_eo && match_count) {
Eric Andersenc06f5682004-02-04 10:57:46 +0000632 pipe_putc(*(oldline++));
633 continue;
634 }
635
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000636 match_count++;
637
638 /* If we aren't interested in this match, output old line to
639 end of match and continue */
640 if(sed_cmd->which_match && sed_cmd->which_match!=match_count) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000641 for(i=0;i<bbg.regmatch[0].rm_eo;i++)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000642 pipe_putc(oldline[i]);
643 continue;
644 }
645
Mark Whitley2dc192f2000-11-03 19:47:00 +0000646 /* print everything before the match */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000647 for (i = 0; i < bbg.regmatch[0].rm_so; i++) pipe_putc(oldline[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000648
Mark Whitley2dc192f2000-11-03 19:47:00 +0000649 /* then print the substitution string */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000650 do_subst_w_backrefs(oldline, sed_cmd->string);
Mark Whitley97562bd2000-07-17 20:06:42 +0000651
Mark Whitley2dc192f2000-11-03 19:47:00 +0000652 /* advance past the match */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000653 oldline += bbg.regmatch[0].rm_eo;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000654 /* flag that something has changed */
655 altered++;
Mark Whitley97562bd2000-07-17 20:06:42 +0000656
Mark Whitley2dc192f2000-11-03 19:47:00 +0000657 /* if we're not doing this globally, get out now */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000658 if (sed_cmd->which_match) break;
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000659 } while (*oldline && (regexec(current_regex, oldline, 10, bbg.regmatch, 0) != REG_NOMATCH));
Mark Whitley2dc192f2000-11-03 19:47:00 +0000660
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000661 /* Copy rest of string into output pipeline */
662
663 while(*oldline) pipe_putc(*(oldline++));
664 pipe_putc(0);
Mark Whitley2dc192f2000-11-03 19:47:00 +0000665
Eric Andersenb76cb682001-08-22 05:58:16 +0000666 free(*line);
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000667 *line = bbg.pipeline.buf;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000668 return altered;
669}
Mark Whitley6315ce62000-07-10 22:55:51 +0000670
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000671/* Set command pointer to point to this label. (Does not handle null label.) */
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000672static sed_cmd_t *branch_to(const char *label)
673{
674 sed_cmd_t *sed_cmd;
Glenn L McGrathd4185b02003-04-11 17:10:23 +0000675
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000676 for (sed_cmd = bbg.sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000677 if ((sed_cmd->cmd == ':') && (sed_cmd->string) && (strcmp(sed_cmd->string, label) == 0)) {
Glenn L McGrathf4523562003-09-14 06:01:14 +0000678 return (sed_cmd);
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000679 }
680 }
Glenn L McGrathf4523562003-09-14 06:01:14 +0000681 bb_error_msg_and_die("Can't find label for jump to `%s'", label);
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000682}
Mark Whitley6315ce62000-07-10 22:55:51 +0000683
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000684static void append(char *s)
685{
Rob Landley8bb50782006-05-26 23:44:51 +0000686 llist_add_to_end(&bbg.append_head, bb_xstrdup(s));
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000687}
688
689static void flush_append(void)
690{
Rob Landley0b656282006-05-08 22:17:23 +0000691 char *data;
692
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000693 /* Output appended lines. */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000694 while((data = (char *)llist_pop(&bbg.append_head))) {
695 fprintf(bbg.nonstdout,"%s\n",data);
Rob Landley0b656282006-05-08 22:17:23 +0000696 free(data);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000697 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000698}
699
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000700static void add_input_file(FILE *file)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000701{
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000702 bbg.input_file_list=xrealloc(bbg.input_file_list,
703 (bbg.input_file_count + 1) * sizeof(FILE *));
704 bbg.input_file_list[bbg.input_file_count++] = file;
Rob Landleydcc28662004-11-25 07:21:47 +0000705}
706
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000707/* Get next line of input from bbg.input_file_list, flushing append buffer and
Rob Landleydcc28662004-11-25 07:21:47 +0000708 * noting if we ran out of files without a newline on the last line we read.
709 */
710static char *get_next_line(int *no_newline)
711{
712 char *temp=NULL;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000713 int len;
714
715 flush_append();
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000716 while (bbg.current_input_file<bbg.input_file_count) {
717 temp = bb_get_chunk_from_file(bbg.input_file_list[bbg.current_input_file],&len);
Rob Landley2b26fd52006-02-24 02:30:39 +0000718 if (temp) {
719 *no_newline = !(len && temp[len-1]=='\n');
720 if (!*no_newline) temp[len-1] = 0;
Rob Landleydcc28662004-11-25 07:21:47 +0000721 break;
Rob Landley2b26fd52006-02-24 02:30:39 +0000722 // Close this file and advance to next one
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000723 } else fclose(bbg.input_file_list[bbg.current_input_file++]);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000724 }
725
726 return temp;
727}
728
729/* Output line of text. missing_newline means the last line output did not
730 end with a newline. no_newline means this line does not end with a
731 newline. */
732
733static int puts_maybe_newline(char *s, FILE *file, int missing_newline, int no_newline)
734{
735 if(missing_newline) fputc('\n',file);
736 fputs(s,file);
737 if(!no_newline) fputc('\n',file);
738
Rob Landley53302f82004-02-18 09:54:15 +0000739 if(ferror(file)) {
Bernhard Reutner-Fischera3d4bf32006-06-03 21:40:11 +0000740 bb_default_error_retval = 4; /* It's what gnu sed exits with... */
741 bb_error_msg_and_die(bb_msg_write_error);
Rob Landley53302f82004-02-18 09:54:15 +0000742 }
743
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000744 return no_newline;
745}
746
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000747#define sed_puts(s,n) missing_newline=puts_maybe_newline(s,bbg.nonstdout,missing_newline,n)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000748
Rob Landley2b26fd52006-02-24 02:30:39 +0000749/* Process all the lines in all the files */
750
Rob Landleydcc28662004-11-25 07:21:47 +0000751static void process_files(void)
Mark Whitley6315ce62000-07-10 22:55:51 +0000752{
Rob Landleyce4f0e92004-10-30 06:54:19 +0000753 char *pattern_space, *next_line;
Rob Landleydcc28662004-11-25 07:21:47 +0000754 int linenum = 0, missing_newline=0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000755 int no_newline,next_no_newline=0;
Mark Whitley6315ce62000-07-10 22:55:51 +0000756
Rob Landley2b26fd52006-02-24 02:30:39 +0000757 /* Prime the pump */
Rob Landleydcc28662004-11-25 07:21:47 +0000758 next_line = get_next_line(&next_no_newline);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000759
Rob Landleydcc28662004-11-25 07:21:47 +0000760 /* go through every line in each file */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000761 for(;;) {
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000762 sed_cmd_t *sed_cmd;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000763 int substituted=0;
764
765 /* Advance to next line. Stop if out of lines. */
766 if(!(pattern_space=next_line)) break;
767 no_newline=next_no_newline;
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000768
Rob Landley2b26fd52006-02-24 02:30:39 +0000769 /* Read one line in advance so we can act on the last line,
770 * the '$' address */
Rob Landleydcc28662004-11-25 07:21:47 +0000771 next_line = get_next_line(&next_no_newline);
Mark Whitley6315ce62000-07-10 22:55:51 +0000772 linenum++;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000773restart:
Mark Whitley6315ce62000-07-10 22:55:51 +0000774 /* for every line, go through all the commands */
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000775 for (sed_cmd = bbg.sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next)
776 {
Eric Andersen52a3c272003-12-23 08:53:51 +0000777 int old_matched, matched;
778
779 old_matched = sed_cmd->in_match;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000780
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000781 /* Determine if this command matches this line: */
782
783 /* Are we continuing a previous multi-line match? */
784
785 sed_cmd->in_match = sed_cmd->in_match
786
787 /* Or is no range necessary? */
788 || (!sed_cmd->beg_line && !sed_cmd->end_line
789 && !sed_cmd->beg_match && !sed_cmd->end_match)
790
791 /* Or did we match the start of a numerical range? */
792 || (sed_cmd->beg_line > 0 && (sed_cmd->beg_line == linenum))
793
794 /* Or does this line match our begin address regex? */
795 || (sed_cmd->beg_match &&
796 !regexec(sed_cmd->beg_match, pattern_space, 0, NULL, 0))
797
798 /* Or did we match last line of input? */
799 || (sed_cmd->beg_line == -1 && next_line == NULL);
800
801 /* Snapshot the value */
802
803 matched = sed_cmd->in_match;
804
805 /* Is this line the end of the current match? */
806
807 if(matched) {
808 sed_cmd->in_match = !(
809 /* has the ending line come, or is this a single address command? */
810 (sed_cmd->end_line ?
811 sed_cmd->end_line==-1 ?
812 !next_line
813 : sed_cmd->end_line<=linenum
814 : !sed_cmd->end_match)
815 /* or does this line matches our last address regex */
Eric Andersen52a3c272003-12-23 08:53:51 +0000816 || (sed_cmd->end_match && old_matched && (regexec(sed_cmd->end_match, pattern_space, 0, NULL, 0) == 0))
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000817 );
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000818 }
Glenn L McGrathfc4cb4d2003-04-12 16:10:42 +0000819
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000820 /* Skip blocks of commands we didn't match. */
821 if (sed_cmd->cmd == '{') {
822 if(sed_cmd->invert ? matched : !matched)
823 while(sed_cmd && sed_cmd->cmd!='}') sed_cmd=sed_cmd->next;
824 if(!sed_cmd) bb_error_msg_and_die("Unterminated {");
825 continue;
826 }
827
828 /* Okay, so did this line match? */
829 if (sed_cmd->invert ? !matched : matched) {
830 /* Update last used regex in case a blank substitute BRE is found */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000831 if (sed_cmd->beg_match) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000832 bbg.previous_regex_ptr = sed_cmd->beg_match;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000833 }
Mark Whitley0915c4b2001-06-11 23:50:06 +0000834
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000835 /* actual sedding */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000836 switch (sed_cmd->cmd) {
Mark Whitley0915c4b2001-06-11 23:50:06 +0000837
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000838 /* Print line number */
839 case '=':
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000840 fprintf(bbg.nonstdout,"%d\n", linenum);
Glenn L McGrath2eed0e22003-09-15 06:28:45 +0000841 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000842
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000843 /* Write the current pattern space up to the first newline */
844 case 'P':
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000845 {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000846 char *tmp = strchr(pattern_space, '\n');
Glenn L McGrath0c518322003-03-30 03:41:53 +0000847
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000848 if (tmp) {
849 *tmp = '\0';
850 sed_puts(pattern_space,1);
851 *tmp = '\n';
852 break;
853 }
854 /* Fall Through */
855 }
856
857 /* Write the current pattern space to output */
858 case 'p':
859 sed_puts(pattern_space,no_newline);
860 break;
861 /* Delete up through first newline */
862 case 'D':
863 {
864 char *tmp = strchr(pattern_space,'\n');
865
866 if(tmp) {
867 tmp=bb_xstrdup(tmp+1);
868 free(pattern_space);
869 pattern_space=tmp;
870 goto restart;
Glenn L McGrath0c518322003-03-30 03:41:53 +0000871 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000872 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000873 /* discard this line. */
874 case 'd':
875 goto discard_line;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000876
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000877 /* Substitute with regex */
878 case 's':
879 if(do_subst_command(sed_cmd, &pattern_space)) {
880 substituted|=1;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000881
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000882 /* handle p option */
883 if(sed_cmd->sub_p)
884 sed_puts(pattern_space,no_newline);
885 /* handle w option */
886 if(sed_cmd->file)
887 sed_cmd->no_newline=puts_maybe_newline(pattern_space, sed_cmd->file, sed_cmd->no_newline, no_newline);
Mark Whitley0915c4b2001-06-11 23:50:06 +0000888
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000889 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000890 break;
891
892 /* Append line to linked list to be printed later */
893 case 'a':
894 {
895 append(sed_cmd->string);
896 break;
Glenn L McGrathd87a7ac2003-04-09 15:26:14 +0000897 }
Glenn L McGrathd7fe39b2003-04-09 15:52:32 +0000898
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000899 /* Insert text before this line */
900 case 'i':
901 sed_puts(sed_cmd->string,1);
Glenn L McGrath2570b432003-09-16 05:25:43 +0000902 break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000903
904 /* Cut and paste text (replace) */
905 case 'c':
906 /* Only triggers on last line of a matching range. */
Rob Landleyce4f0e92004-10-30 06:54:19 +0000907 if (!sed_cmd->in_match) sed_puts(sed_cmd->string,0);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000908 goto discard_line;
909
910 /* Read file, append contents to output */
911 case 'r':
Glenn L McGrathf4523562003-09-14 06:01:14 +0000912 {
Rob Landley2b26fd52006-02-24 02:30:39 +0000913 FILE *rfile;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000914
Rob Landley2b26fd52006-02-24 02:30:39 +0000915 rfile = fopen(sed_cmd->string, "r");
916 if (rfile) {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000917 char *line;
918
Rob Landley2b26fd52006-02-24 02:30:39 +0000919 while ((line = bb_get_chomped_line_from_file(rfile))
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000920 != NULL)
921 append(line);
Rob Landley2b26fd52006-02-24 02:30:39 +0000922 bb_xprint_and_close_file(rfile);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000923 }
924
925 break;
926 }
927
928 /* Write pattern space to file. */
929 case 'w':
930 sed_cmd->no_newline=puts_maybe_newline(pattern_space,sed_cmd->file, sed_cmd->no_newline,no_newline);
931 break;
932
933 /* Read next line from input */
934 case 'n':
Rob Landleye3f5a3f2006-05-09 03:53:55 +0000935 if (!bbg.be_quiet)
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000936 sed_puts(pattern_space,no_newline);
937 if (next_line) {
938 free(pattern_space);
939 pattern_space = next_line;
940 no_newline=next_no_newline;
Rob Landleydcc28662004-11-25 07:21:47 +0000941 next_line = get_next_line(&next_no_newline);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000942 linenum++;
943 break;
944 }
945 /* fall through */
946
947 /* Quit. End of script, end of input. */
948 case 'q':
949 /* Exit the outer while loop */
950 free(next_line);
951 next_line = NULL;
952 goto discard_commands;
953
954 /* Append the next line to the current line */
955 case 'N':
956 {
957 /* If no next line, jump to end of script and exit. */
958 if (next_line == NULL) {
959 /* Jump to end of script and exit */
960 free(next_line);
961 next_line = NULL;
962 goto discard_line;
963 /* append next_line, read new next_line. */
Glenn L McGrathf4523562003-09-14 06:01:14 +0000964 } else {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000965 int len=strlen(pattern_space);
966
967 pattern_space = realloc(pattern_space, len + strlen(next_line) + 2);
968 pattern_space[len]='\n';
969 strcpy(pattern_space+len+1, next_line);
970 no_newline=next_no_newline;
Rob Landleydcc28662004-11-25 07:21:47 +0000971 next_line = get_next_line(&next_no_newline);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000972 linenum++;
Glenn L McGrathf4523562003-09-14 06:01:14 +0000973 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000974 break;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000975 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000976
Rob Landley93850a52005-05-18 06:34:37 +0000977 /* Test/branch if substitution occurred */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000978 case 't':
Rob Landley93850a52005-05-18 06:34:37 +0000979 if(!substituted) break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000980 substituted=0;
Rob Landley93850a52005-05-18 06:34:37 +0000981 /* Fall through */
982 /* Test/branch if substitution didn't occur */
983 case 'T':
984 if (substituted) break;
985 /* Fall through */
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000986 /* Branch to label */
987 case 'b':
988 if (!sed_cmd->string) goto discard_commands;
989 else sed_cmd = branch_to(sed_cmd->string);
990 break;
991 /* Transliterate characters */
992 case 'y':
993 {
994 int i;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000995
Glenn L McGrathaa5a6022003-10-01 03:06:16 +0000996 for (i = 0; pattern_space[i]; i++) {
997 int j;
998
999 for (j = 0; sed_cmd->string[j]; j += 2) {
1000 if (pattern_space[i] == sed_cmd->string[j]) {
1001 pattern_space[i] = sed_cmd->string[j + 1];
Rob Landleybabd3fb2005-09-02 00:10:06 +00001002 break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001003 }
Glenn L McGrathf01b46d2003-03-30 08:02:18 +00001004 }
1005 }
Glenn L McGrath294d1132003-09-14 16:28:08 +00001006
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001007 break;
Glenn L McGrath91e19782003-04-26 07:40:07 +00001008 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001009 case 'g': /* Replace pattern space with hold space */
1010 free(pattern_space);
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001011 pattern_space = bb_xstrdup(bbg.hold_space ? bbg.hold_space : "");
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001012 break;
1013 case 'G': /* Append newline and hold space to pattern space */
1014 {
1015 int pattern_space_size = 2;
1016 int hold_space_size = 0;
1017
1018 if (pattern_space)
1019 pattern_space_size += strlen(pattern_space);
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001020 if (bbg.hold_space)
1021 hold_space_size = strlen(bbg.hold_space);
1022 pattern_space = xrealloc(pattern_space,
1023 pattern_space_size + hold_space_size);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001024 if (pattern_space_size == 2) pattern_space[0]=0;
Glenn L McGrath977451e2003-09-15 12:07:48 +00001025 strcat(pattern_space, "\n");
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001026 if (bbg.hold_space)
1027 strcat(pattern_space, bbg.hold_space);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001028 no_newline=0;
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001029
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001030 break;
Glenn L McGrath91e19782003-04-26 07:40:07 +00001031 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001032 case 'h': /* Replace hold space with pattern space */
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001033 free(bbg.hold_space);
1034 bbg.hold_space = bb_xstrdup(pattern_space);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001035 break;
1036 case 'H': /* Append newline and pattern space to hold space */
1037 {
1038 int hold_space_size = 2;
1039 int pattern_space_size = 0;
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001040
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001041 if (bbg.hold_space)
1042 hold_space_size += strlen(bbg.hold_space);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001043 if (pattern_space)
1044 pattern_space_size = strlen(pattern_space);
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001045 bbg.hold_space = xrealloc(bbg.hold_space,
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001046 hold_space_size + pattern_space_size);
1047
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001048 if (hold_space_size == 2) *bbg.hold_space=0;
1049 strcat(bbg.hold_space, "\n");
1050 if (pattern_space) strcat(bbg.hold_space, pattern_space);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001051
1052 break;
Glenn L McGrath4dc1d252003-09-14 01:25:31 +00001053 }
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001054 case 'x': /* Exchange hold and pattern space */
1055 {
1056 char *tmp = pattern_space;
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001057 pattern_space = bbg.hold_space;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001058 no_newline=0;
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001059 bbg.hold_space = tmp;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001060 break;
Glenn L McGrath8417c8c2003-09-14 15:24:18 +00001061 }
Mark Whitley0915c4b2001-06-11 23:50:06 +00001062 }
Robert Griebl47abc492002-06-11 23:43:27 +00001063 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001064 }
Erik Andersen1266a131999-12-29 22:19:46 +00001065
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001066 /*
1067 * exit point from sedding...
1068 */
1069discard_commands:
1070 /* we will print the line unless we were told to be quiet ('-n')
1071 or if the line was suppressed (ala 'd'elete) */
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001072 if (!bbg.be_quiet) sed_puts(pattern_space,no_newline);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001073
1074 /* Delete and such jump here. */
1075discard_line:
1076 flush_append();
Glenn L McGrathc6adada2003-04-07 12:24:44 +00001077 free(pattern_space);
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001078 }
Erik Andersen1266a131999-12-29 22:19:46 +00001079}
1080
Glenn L McGrath42c25732003-10-04 05:27:56 +00001081/* It is possible to have a command line argument with embedded
1082 newlines. This counts as multiple command lines. */
1083
1084static void add_cmd_block(char *cmdstr)
1085{
1086 int go=1;
1087 char *temp=bb_xstrdup(cmdstr),*temp2=temp;
1088
1089 while(go) {
1090 int len=strcspn(temp2,"\n");
1091 if(!temp2[len]) go=0;
1092 else temp2[len]=0;
1093 add_cmd(temp2);
1094 temp2+=len+1;
1095 }
1096 free(temp);
1097}
1098
Rob Landleydfba7412006-03-06 20:47:33 +00001099int sed_main(int argc, char **argv)
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001100{
Rob Landleyce4f0e92004-10-30 06:54:19 +00001101 int status = EXIT_SUCCESS, opt, getpat = 1;
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001102
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001103 bbg.sed_cmd_tail=&bbg.sed_cmd_head;
1104
Mark Whitley858c1ad2000-07-11 21:38:47 +00001105 /* destroy command strings on exit */
Rob Landley0b656282006-05-08 22:17:23 +00001106 if (ENABLE_FEATURE_CLEAN_UP) atexit(sed_free_and_close_stuff);
Mark Whitley858c1ad2000-07-11 21:38:47 +00001107
Rob Landleyfae1dc82005-11-20 07:44:35 +00001108 /* Lie to autoconf when it starts asking stupid questions. */
Eric Andersenc06f5682004-02-04 10:57:46 +00001109 if(argc==2 && !strcmp(argv[1],"--version")) {
1110 printf("This is not GNU sed version 4.0\n");
1111 exit(0);
1112 }
Eric Andersenc06f5682004-02-04 10:57:46 +00001113
Mark Whitley6315ce62000-07-10 22:55:51 +00001114 /* do normal option parsing */
Eric Andersen98555482004-05-26 10:03:33 +00001115 while ((opt = getopt(argc, argv, "irne:f:")) > 0) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001116 switch (opt) {
Rob Landley53302f82004-02-18 09:54:15 +00001117 case 'i':
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001118 bbg.in_place++;
Rob Landley53302f82004-02-18 09:54:15 +00001119 atexit(cleanup_outname);
1120 break;
Eric Andersen98555482004-05-26 10:03:33 +00001121 case 'r':
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001122 bbg.regex_type|=REG_EXTENDED;
Eric Andersen98555482004-05-26 10:03:33 +00001123 break;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001124 case 'n':
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001125 bbg.be_quiet++;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001126 break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001127 case 'e':
Glenn L McGrath42c25732003-10-04 05:27:56 +00001128 add_cmd_block(optarg);
Eric Andersenfaa7d862004-04-21 00:56:22 +00001129 getpat=0;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001130 break;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001131 case 'f':
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001132 {
1133 FILE *cmdfile;
1134 char *line;
1135
1136 cmdfile = bb_xfopen(optarg, "r");
1137
Rob Landleyfae1dc82005-11-20 07:44:35 +00001138 while ((line = bb_get_chomped_line_from_file(cmdfile)) != NULL) {
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001139 add_cmd(line);
Eric Andersenfaa7d862004-04-21 00:56:22 +00001140 getpat=0;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001141 free(line);
1142 }
1143 bb_xprint_and_close_file(cmdfile);
1144
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001145 break;
Glenn L McGrathaa5a6022003-10-01 03:06:16 +00001146 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001147 default:
1148 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001149 }
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001150 }
Mark Whitley6315ce62000-07-10 22:55:51 +00001151
Rob Landleydcc28662004-11-25 07:21:47 +00001152 /* if we didn't get a pattern from -e or -f, use argv[optind] */
Eric Andersenfaa7d862004-04-21 00:56:22 +00001153 if(getpat) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001154 if (argv[optind] == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001155 bb_show_usage();
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001156 else
Glenn L McGrath42c25732003-10-04 05:27:56 +00001157 add_cmd_block(argv[optind++]);
Mark Whitley6315ce62000-07-10 22:55:51 +00001158 }
Glenn L McGrath42c25732003-10-04 05:27:56 +00001159 /* Flush any unfinished commands. */
1160 add_cmd("");
Mark Whitley6315ce62000-07-10 22:55:51 +00001161
Rob Landley53302f82004-02-18 09:54:15 +00001162 /* By default, we write to stdout */
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001163 bbg.nonstdout=stdout;
Rob Landley53302f82004-02-18 09:54:15 +00001164
Mark Whitley6315ce62000-07-10 22:55:51 +00001165 /* argv[(optind)..(argc-1)] should be names of file to process. If no
1166 * files were specified or '-' was specified, take input from stdin.
1167 * Otherwise, we process all the files specified. */
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001168 if (argv[optind] == NULL) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001169 if(bbg.in_place) bb_error_msg_and_die("Filename required for -i");
Rob Landleydcc28662004-11-25 07:21:47 +00001170 add_input_file(stdin);
1171 process_files();
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001172 } else {
Mark Whitley6315ce62000-07-10 22:55:51 +00001173 int i;
1174 FILE *file;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001175
Mark Whitley6315ce62000-07-10 22:55:51 +00001176 for (i = optind; i < argc; i++) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001177 if(!strcmp(argv[i], "-") && !bbg.in_place) {
Rob Landleydcc28662004-11-25 07:21:47 +00001178 add_input_file(stdin);
1179 process_files();
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001180 } else {
1181 file = bb_wfopen(argv[i], "r");
1182 if (file) {
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001183 if(bbg.in_place) {
Rob Landley53302f82004-02-18 09:54:15 +00001184 struct stat statbuf;
Rob Landley5797c7f2005-05-18 05:56:16 +00001185 int nonstdoutfd;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001186
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001187 bbg.outname=bb_xstrndup(argv[i],strlen(argv[i])+6);
1188 strcat(bbg.outname,"XXXXXX");
1189 if(-1==(nonstdoutfd=mkstemp(bbg.outname)))
Rob Landley5797c7f2005-05-18 05:56:16 +00001190 bb_error_msg_and_die("no temp file");
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001191 bbg.nonstdout=fdopen(nonstdoutfd,"w");
Bernhard Reutner-Fischerb7f39732006-03-01 20:14:16 +00001192
Rob Landley53302f82004-02-18 09:54:15 +00001193 /* Set permissions of output file */
Bernhard Reutner-Fischerb7f39732006-03-01 20:14:16 +00001194
Rob Landley53302f82004-02-18 09:54:15 +00001195 fstat(fileno(file),&statbuf);
Rob Landley5797c7f2005-05-18 05:56:16 +00001196 fchmod(nonstdoutfd,statbuf.st_mode);
Rob Landleydcc28662004-11-25 07:21:47 +00001197 add_input_file(file);
1198 process_files();
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001199 fclose(bbg.nonstdout);
Bernhard Reutner-Fischerb7f39732006-03-01 20:14:16 +00001200
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001201 bbg.nonstdout=stdout;
Rob Landley53302f82004-02-18 09:54:15 +00001202 unlink(argv[i]);
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001203 rename(bbg.outname,argv[i]);
1204 free(bbg.outname);
1205 bbg.outname=0;
Rob Landleydcc28662004-11-25 07:21:47 +00001206 } else add_input_file(file);
Glenn L McGrath8aac05b2003-09-14 04:06:12 +00001207 } else {
1208 status = EXIT_FAILURE;
1209 }
1210 }
Mark Whitley6315ce62000-07-10 22:55:51 +00001211 }
Rob Landleye3f5a3f2006-05-09 03:53:55 +00001212 if(bbg.input_file_count>bbg.current_input_file) process_files();
Mark Whitley6315ce62000-07-10 22:55:51 +00001213 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001214
Matt Kraaia5f09c62001-11-12 16:44:55 +00001215 return status;
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001216}