blob: 912318c04c81a502a5fd98ee5c9a3741e29630ec [file] [log] [blame]
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001/*
Mark Whitley6315ce62000-07-10 22:55:51 +00002 * sed.c - very minimalist version of sed
Eric Andersen6b6b3f61999-10-28 16:06:25 +00003 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00004 * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
5 * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org>
Matt Kraai5ed78ad2002-01-03 21:12:34 +00006 * Copyright (C) 2002 Matt Kraai
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +00007 * Copyright (C) 2003 by Glenn McGrath <bug1@optushome.com.au>
Erik Andersen1266a131999-12-29 22:19:46 +00008 *
Eric Andersen6b6b3f61999-10-28 16:06:25 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24
Mark Whitley6315ce62000-07-10 22:55:51 +000025/*
26 Supported features and commands in this version of sed:
27
28 - comments ('#')
Mark Whitley94074a92000-07-14 00:00:15 +000029 - address matching: num|/matchstr/[,num|/matchstr/|$]command
30 - commands: (p)rint, (d)elete, (s)ubstitue (with g & I flags)
31 - edit commands: (a)ppend, (i)nsert, (c)hange
Mark Whitley1f3b9f22001-05-11 22:27:13 +000032 - file commands: (r)ead
Mark Whitley97562bd2000-07-17 20:06:42 +000033 - backreferences in substitution expressions (\1, \2...\9)
Glenn L McGrathf50ce312003-03-09 15:12:24 +000034 - grouped commands: {cmd1;cmd2}
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000035 - transliteration (y/source-chars/dest-chars/)
36 - pattern space hold space storing / swapping (g, h, x)
37 - labels / branching (: label, b, t)
Glenn L McGrathf50ce312003-03-09 15:12:24 +000038
Mark Whitley6315ce62000-07-10 22:55:51 +000039 (Note: Specifying an address (range) to match is *optional*; commands
40 default to the whole pattern space if no specific address match was
41 requested.)
42
43 Unsupported features:
44
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000045 - GNU extensions
Mark Whitley6315ce62000-07-10 22:55:51 +000046 - and lots, lots more.
Glenn L McGrathd5eadea2003-03-09 02:39:29 +000047
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +000048 Bugs:
49
50 - Cant subst globally using ^ or $ in regex, eg. "aah" | sed 's/^a/b/g'
51
Glenn L McGrathd5eadea2003-03-09 02:39:29 +000052 Reference http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html
Mark Whitley6315ce62000-07-10 22:55:51 +000053*/
54
Eric Andersen6b6b3f61999-10-28 16:06:25 +000055#include <stdio.h>
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000056#include <unistd.h> /* for getopt() */
Mark Whitley6315ce62000-07-10 22:55:51 +000057#include <regex.h>
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000058#include <string.h> /* for strdup() */
Eric Andersen6b6b3f61999-10-28 16:06:25 +000059#include <errno.h>
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000060#include <ctype.h> /* for isspace() */
Eric Andersened3ef502001-01-27 08:24:39 +000061#include <stdlib.h>
Eric Andersen3570a342000-09-25 21:45:58 +000062#include "busybox.h"
Mark Whitley6315ce62000-07-10 22:55:51 +000063
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +000064typedef struct sed_cmd_s {
Eric Andersenc52a6b02001-11-10 10:49:42 +000065 /* Order by alignment requirements */
Mark Whitley2dc192f2000-11-03 19:47:00 +000066
Mark Whitley6315ce62000-07-10 22:55:51 +000067 /* address storage */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000068 regex_t *beg_match; /* sed -e '/match/cmd' */
69 regex_t *end_match; /* sed -e '/match/,/end_match/cmd' */
Mark Whitley6315ce62000-07-10 22:55:51 +000070
Mark Whitley2dc192f2000-11-03 19:47:00 +000071 /* SUBSTITUTION COMMAND SPECIFIC FIELDS */
72
73 /* sed -e 's/sub_match/replace/' */
74 regex_t *sub_match;
75 char *replace;
Eric Andersenc52a6b02001-11-10 10:49:42 +000076
77 /* EDIT COMMAND (a,i,c) SPECIFIC FIELDS */
78 char *editline;
79
80 /* FILE COMMAND (r) SPECIFIC FIELDS */
81 char *filename;
82
83 /* address storage */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000084 int beg_line; /* 'sed 1p' 0 == no begining line, apply commands to all lines */
85 int end_line; /* 'sed 1,3p' 0 == no end line, use only beginning. -1 == $ */
Eric Andersenc52a6b02001-11-10 10:49:42 +000086 /* SUBSTITUTION COMMAND SPECIFIC FIELDS */
87
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000088 unsigned int num_backrefs:4; /* how many back references (\1..\9) */
89 /* Note: GNU/POSIX sed does not save more than nine backrefs, so
90 * we only use 4 bits to hold the number */
91 unsigned int sub_g:1; /* sed -e 's/foo/bar/g' (global) */
92 unsigned int sub_p:1; /* sed -e 's/foo/bar/p' (print substitution) */
Mark Whitley94074a92000-07-14 00:00:15 +000093
Glenn L McGrathf01b46d2003-03-30 08:02:18 +000094 /* TRANSLATE COMMAND */
95 char *translate;
Mark Whitley2dc192f2000-11-03 19:47:00 +000096
Glenn L McGrathf01b46d2003-03-30 08:02:18 +000097 /* GENERAL FIELDS */
Eric Andersenc52a6b02001-11-10 10:49:42 +000098 /* the command */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +000099 char cmd; /* p,d,s (add more at your leisure :-) */
Robert Griebl47abc492002-06-11 23:43:27 +0000100
101 /* inversion flag */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000102 int invert; /* the '!' after the address */
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000103
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000104 /* Branch commands */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000105 char *label;
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000106
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000107 /* next command in list (sequential list of specified commands) */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000108 struct sed_cmd_s *next;
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000109
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +0000110} sed_cmd_t;
Mark Whitley6315ce62000-07-10 22:55:51 +0000111
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000112
113/* externs */
114extern void xregcomp(regex_t * preg, const char *regex, int cflags);
115extern int optind; /* in unistd.h */
116extern char *optarg; /* ditto */
117
Mark Whitley6315ce62000-07-10 22:55:51 +0000118/* globals */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000119/* options */
120static int be_quiet = 0;
121static const char bad_format_in_subst[] =
122 "bad format in substitution expression";
123
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000124/* linked list of sed commands */
125static sed_cmd_t sed_cmd_head;
126static sed_cmd_t *sed_cmd_tail = &sed_cmd_head;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000127static sed_cmd_t *block_cmd;
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000128
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000129static int in_block = 0;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000130const char *const semicolon_whitespace = "; \n\r\t\v\0";
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000131static regex_t *previous_regex_ptr = NULL;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000132
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000133
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000134#ifdef CONFIG_FEATURE_CLEAN_UP
Matt Kraai0c390a72001-11-20 16:00:19 +0000135static void destroy_cmd_strs(void)
Mark Whitley6315ce62000-07-10 22:55:51 +0000136{
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000137 sed_cmd_t *sed_cmd = sed_cmd_head.next;
Mark Whitley6315ce62000-07-10 22:55:51 +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 McGrath56c633c2003-03-28 04:23:23 +0000142 if (sed_cmd->beg_match) {
143 regfree(sed_cmd->beg_match);
144 free(sed_cmd->beg_match);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000145 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000146 if (sed_cmd->end_match) {
147 regfree(sed_cmd->end_match);
148 free(sed_cmd->end_match);
Mark Whitley6315ce62000-07-10 22:55:51 +0000149 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000150 if (sed_cmd->sub_match) {
151 regfree(sed_cmd->sub_match);
152 free(sed_cmd->sub_match);
Mark Whitley6315ce62000-07-10 22:55:51 +0000153 }
Glenn L McGrath56c633c2003-03-28 04:23:23 +0000154 free(sed_cmd->replace);
155 free(sed_cmd);
156 sed_cmd = sed_cmd_next;
Mark Whitley6315ce62000-07-10 22:55:51 +0000157 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000158}
Mark Whitleyc41e8c82000-07-12 23:35:21 +0000159#endif
Mark Whitley6315ce62000-07-10 22:55:51 +0000160
Mark Whitley6315ce62000-07-10 22:55:51 +0000161/*
Eric Andersen28b3c532001-01-02 11:01:31 +0000162 * index_of_next_unescaped_regexp_delim - walks left to right through a string
163 * beginning at a specified index and returns the index of the next regular
164 * expression delimiter (typically a forward * slash ('/')) not preceeded by
165 * a backslash ('\').
Mark Whitley6315ce62000-07-10 22:55:51 +0000166 */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000167static int index_of_next_unescaped_regexp_delim(const char delimiter,
168 const char *str)
Mark Whitley6315ce62000-07-10 22:55:51 +0000169{
Matt Kraaia0065d52001-08-24 14:45:50 +0000170 int bracket = -1;
171 int escaped = 0;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000172 int idx = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000173 char ch;
Matt Kraaia0065d52001-08-24 14:45:50 +0000174
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000175 for (; (ch = str[idx]); idx++) {
Matt Kraaia0065d52001-08-24 14:45:50 +0000176 if (bracket != -1) {
Eric Andersenc52a6b02001-11-10 10:49:42 +0000177 if (ch == ']' && !(bracket == idx - 1 ||
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000178 (bracket == idx - 2 && str[idx - 1] == '^')))
Matt Kraaia0065d52001-08-24 14:45:50 +0000179 bracket = -1;
180 } else if (escaped)
181 escaped = 0;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000182 else if (ch == '\\')
Matt Kraaia0065d52001-08-24 14:45:50 +0000183 escaped = 1;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000184 else if (ch == '[')
Matt Kraaia0065d52001-08-24 14:45:50 +0000185 bracket = idx;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000186 else if (ch == delimiter)
Mark Whitley97562bd2000-07-17 20:06:42 +0000187 return idx;
188 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000189
Mark Whitley97562bd2000-07-17 20:06:42 +0000190 /* if we make it to here, we've hit the end of the string */
191 return -1;
Mark Whitley6315ce62000-07-10 22:55:51 +0000192}
193
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000194static int parse_regex_delim(const char *cmdstr, char **match, char **replace)
195{
196 const char *cmdstr_ptr = cmdstr;
197 char delimiter;
198 int idx = 0;
199
200 /* verify that the 's' is followed by something. That something
201 * (typically a 'slash') is now our regexp delimiter... */
202 if (*cmdstr == '\0')
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000203 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000204 else
205 delimiter = *cmdstr_ptr;
206
207 cmdstr_ptr++;
208
209 /* save the match string */
210 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
211 if (idx == -1) {
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000212 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000213 }
214 *match = bb_xstrndup(cmdstr_ptr, idx);
215
216 /* save the replacement string */
217 cmdstr_ptr += idx + 1;
218 idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
219 if (idx == -1) {
Glenn L McGrath9a52bb62003-03-30 09:38:40 +0000220 bb_error_msg_and_die(bad_format_in_subst);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000221 }
222 *replace = bb_xstrndup(cmdstr_ptr, idx);
223
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000224 return ((cmdstr_ptr - cmdstr) + idx);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000225}
226
Mark Whitley6315ce62000-07-10 22:55:51 +0000227/*
228 * returns the index in the string just past where the address ends.
229 */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000230static int get_address(char *my_str, int *linenum, regex_t ** regex)
Mark Whitley6315ce62000-07-10 22:55:51 +0000231{
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000232 int idx = 0;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000233
Mark Whitley6315ce62000-07-10 22:55:51 +0000234 if (isdigit(my_str[idx])) {
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000235 char *endstr;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000236
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000237 *linenum = strtol(my_str, &endstr, 10);
238 /* endstr shouldnt ever equal NULL */
239 idx = endstr - my_str;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000240 } else if (my_str[idx] == '$') {
Mark Whitley0915c4b2001-06-11 23:50:06 +0000241 *linenum = -1;
Mark Whitley6315ce62000-07-10 22:55:51 +0000242 idx++;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000243 } else if (my_str[idx] == '/' || my_str[idx] == '\\') {
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000244 int idx_start = 1;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000245 char delimiter;
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000246
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000247 delimiter = '/';
Robert Griebl79401472002-08-06 21:07:17 +0000248 if (my_str[idx] == '\\') {
Robert Griebl00f5ecb2002-08-06 23:13:31 +0000249 idx_start++;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000250 delimiter = my_str[++idx];
Robert Griebl79401472002-08-06 21:07:17 +0000251 }
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000252 idx++;
253 idx += index_of_next_unescaped_regexp_delim(delimiter, my_str + idx);
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000254 if (idx == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000255 bb_error_msg_and_die("unterminated match expression");
Glenn L McGrath1fb44672003-03-09 08:44:49 +0000256 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +0000257 my_str[idx] = '\0';
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000258
259 *regex = (regex_t *) xmalloc(sizeof(regex_t));
260 xregcomp(*regex, my_str + idx_start, REG_NEWLINE);
261 idx++; /* so it points to the next character after the last '/' */
Mark Whitley6315ce62000-07-10 22:55:51 +0000262 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000263 return idx;
264}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000265
Glenn L McGrathe7a8bc92003-03-09 10:23:57 +0000266static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr)
Mark Whitley06f35292000-07-13 19:58:04 +0000267{
Glenn L McGrathe01f9662003-03-18 08:37:57 +0000268 int cflags = 0;
Mark Whitley06f35292000-07-13 19:58:04 +0000269 char *match;
270 int idx = 0;
Mark Whitley97562bd2000-07-17 20:06:42 +0000271 int j;
Mark Whitley06f35292000-07-13 19:58:04 +0000272
273 /*
274 * the string that gets passed to this function should look like this:
Mark Whitley0e4cec02000-08-21 21:29:20 +0000275 * s/match/replace/gIp
276 * || | |||
Mark Whitley06f35292000-07-13 19:58:04 +0000277 * mandatory optional
278 *
279 * (all three of the '/' slashes are mandatory)
280 */
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000281 idx = parse_regex_delim(substr, &match, &sed_cmd->replace);
Mark Whitley06f35292000-07-13 19:58:04 +0000282
Mark Whitley97562bd2000-07-17 20:06:42 +0000283 /* determine the number of back references in the match string */
284 /* Note: we compute this here rather than in the do_subst_command()
285 * function to save processor time, at the expense of a little more memory
286 * (4 bits) per sed_cmd */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000287
288 /* sed_cmd->num_backrefs = 0; *//* XXX: not needed? --apparently not */
Mark Whitley97562bd2000-07-17 20:06:42 +0000289 for (j = 0; match[j]; j++) {
290 /* GNU/POSIX sed does not save more than nine backrefs */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000291 if (match[j] == '\\' && match[j + 1] == '('
292 && sed_cmd->num_backrefs <= 9)
Mark Whitley97562bd2000-07-17 20:06:42 +0000293 sed_cmd->num_backrefs++;
294 }
295
Mark Whitley06f35292000-07-13 19:58:04 +0000296 /* process the flags */
297 while (substr[++idx]) {
298 switch (substr[idx]) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000299 case 'g':
300 sed_cmd->sub_g = 1;
301 break;
Glenn L McGrathbed40332003-03-10 02:21:14 +0000302 /* Hmm, i dont see the I option mentioned in the standard */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000303 case 'I':
304 cflags |= REG_ICASE;
305 break;
306 case 'p':
307 sed_cmd->sub_p = 1;
308 break;
309 default:
310 /* any whitespace or semicolon trailing after a s/// is ok */
311 if (strchr(semicolon_whitespace, substr[idx]))
312 goto out;
313 /* else */
314 bb_error_msg_and_die("bad option in substitution expression");
Mark Whitley06f35292000-07-13 19:58:04 +0000315 }
316 }
Mark Whitley70705d72000-07-14 19:06:30 +0000317
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000318 out:
Mark Whitley97562bd2000-07-17 20:06:42 +0000319 /* compile the match string into a regex */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000320 if (*match != '\0') {
321 /* If match is empty, we use last regex used at runtime */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000322 sed_cmd->sub_match = (regex_t *) xmalloc(sizeof(regex_t));
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000323 xregcomp(sed_cmd->sub_match, match, cflags);
324 }
Mark Whitley06f35292000-07-13 19:58:04 +0000325 free(match);
Mark Whitley70705d72000-07-14 19:06:30 +0000326
327 return idx;
Mark Whitley06f35292000-07-13 19:58:04 +0000328}
329
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000330static void replace_slash_n(char *string)
331{
332 int i;
333 int remaining = strlen(string);
334
335 for (i = 0; string[i]; i++) {
336 if ((string[i] == '\\') && (string[i + 1] == 'n')) {
337 string[i] = '\n';
338 memmove(string + i + 1, string + i + 1, remaining - 1);
339 } else {
340 remaining--;
341 }
342 }
343}
344
345static int parse_translate_cmd(sed_cmd_t * const sed_cmd, const char *cmdstr)
346{
347 char *match;
348 char *replace;
349 int idx;
350 int i;
351
352 idx = parse_regex_delim(cmdstr, &match, &replace);
353 replace_slash_n(match);
354 replace_slash_n(replace);
355 sed_cmd->translate = xcalloc(1, (strlen(match) + 1) * 2);
356 for (i = 0; (match[i] != 0) && (replace[i] != 0); i++) {
357 sed_cmd->translate[i * 2] = match[i];
358 sed_cmd->translate[(i * 2) + 1] = replace[i];
359 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000360 return (idx + 1);
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000361}
362
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000363static int parse_edit_cmd(sed_cmd_t * sed_cmd, const char *editstr)
Mark Whitley94074a92000-07-14 00:00:15 +0000364{
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000365 int i, j;
Mark Whitley94074a92000-07-14 00:00:15 +0000366
367 /*
368 * the string that gets passed to this function should look like this:
369 *
370 * need one of these
371 * |
372 * | this backslash (immediately following the edit command) is mandatory
373 * | |
374 * [aic]\
375 * TEXT1\
376 * TEXT2\
377 * TEXTN
378 *
379 * as soon as we hit a TEXT line that has no trailing '\', we're done.
380 * this means a command like:
381 *
382 * i\
383 * INSERTME
384 *
385 * is a-ok.
386 *
387 */
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000388 if ((*editstr != '\\') || ((editstr[1] != '\n') && (editstr[1] != '\r'))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000389 bb_error_msg_and_die("bad format in edit expression");
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000390 }
Mark Whitley94074a92000-07-14 00:00:15 +0000391
392 /* store the edit line text */
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000393 sed_cmd->editline = xmalloc(strlen(&editstr[2]) + 2);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000394 for (i = 2, j = 0;
395 editstr[i] != '\0' && strchr("\r\n", editstr[i]) == NULL; i++, j++) {
396 if ((editstr[i] == '\\') && strchr("\n\r", editstr[i + 1]) != NULL) {
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000397 sed_cmd->editline[j] = '\n';
398 i++;
399 } else
400 sed_cmd->editline[j] = editstr[i];
Mark Whitley94074a92000-07-14 00:00:15 +0000401 }
Mark Whitley70705d72000-07-14 19:06:30 +0000402
Mark Whitley56c14a62001-04-20 23:41:44 +0000403 /* figure out if we need to add a newline */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000404 if (sed_cmd->editline[j - 1] != '\n')
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000405 sed_cmd->editline[j++] = '\n';
Mark Whitley56c14a62001-04-20 23:41:44 +0000406
407 /* terminate string */
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000408 sed_cmd->editline[j] = '\0';
Mark Whitley464c5de2000-07-14 23:24:00 +0000409
Matt Kraai5ed78ad2002-01-03 21:12:34 +0000410 return i;
Mark Whitley94074a92000-07-14 00:00:15 +0000411}
412
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000413
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000414static int parse_file_cmd(sed_cmd_t * sed_cmd, const char *filecmdstr)
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000415{
416 int idx = 0;
417 int filenamelen = 0;
418
419 /*
420 * the string that gets passed to this function should look like this:
421 * '[ ]filename'
422 * | |
423 * | a filename
424 * |
425 * optional whitespace
426
427 * re: the file to be read, the GNU manual says the following: "Note that
428 * if filename cannot be read, it is treated as if it were an empty file,
429 * without any error indication." Thus, all of the following commands are
430 * perfectly leagal:
431 *
432 * sed -e '1r noexist'
433 * sed -e '1r ;'
434 * sed -e '1r'
435 */
436
437 /* the file command may be followed by whitespace; move past it. */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000438 while (isspace(filecmdstr[++idx])) {;
439 }
440
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000441 /* the first non-whitespace we get is a filename. the filename ends when we
442 * hit a normal sed command terminator or end of string */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000443 filenamelen = strcspn(&filecmdstr[idx], semicolon_whitespace);
Matt Kraai6e9e1362001-05-27 14:11:52 +0000444 sed_cmd->filename = xmalloc(filenamelen + 1);
445 safe_strncpy(sed_cmd->filename, &filecmdstr[idx], filenamelen + 1);
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000446
447 return idx + filenamelen;
448}
449
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000450/*
451 * Process the commands arguments
452 */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000453static char *parse_cmd_str(sed_cmd_t *sed_cmd, char *cmdstr)
Mark Whitley6315ce62000-07-10 22:55:51 +0000454{
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000455 /* handle (s)ubstitution command */
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000456 if (sed_cmd->cmd == 's') {
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000457 cmdstr += parse_subst_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000458 }
459 /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */
460 else if (strchr("aic", sed_cmd->cmd)) {
461 if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000462 bb_error_msg_and_die
463 ("only a beginning address can be specified for edit commands");
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000464 cmdstr += parse_edit_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000465 }
466 /* handle file cmds: (r)ead */
467 else if (sed_cmd->cmd == 'r') {
468 if (sed_cmd->end_line || sed_cmd->end_match)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000469 bb_error_msg_and_die("Command only uses one address");
Glenn L McGrath4c6523a2003-03-09 11:06:38 +0000470 cmdstr += parse_file_cmd(sed_cmd, cmdstr);
Glenn L McGrath2f8a4012003-03-09 02:44:49 +0000471 }
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000472 /* handle branch commands */
Glenn L McGrath595a6a42003-03-28 08:36:52 +0000473 else if (strchr(":bt", sed_cmd->cmd)) {
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000474 int length;
475
476 cmdstr += strspn(cmdstr, " ");
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000477 length = strcspn(cmdstr, "; \n");
478 sed_cmd->label = strndup(cmdstr, length);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000479 cmdstr += length;
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000480 }
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000481 /* translation command */
482 else if (sed_cmd->cmd == 'y') {
483 cmdstr += parse_translate_cmd(sed_cmd, cmdstr);
484 }
Glenn L McGrath2971ef12003-03-18 01:19:23 +0000485 /* if it wasnt a single-letter command that takes no arguments
486 * then it must be an invalid command.
487 */
Glenn L McGrath7ce9e242003-04-07 16:04:14 +0000488 else if (strchr("dghnNpPqx=", sed_cmd->cmd) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000489 bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd);
Mark Whitley1f3b9f22001-05-11 22:27:13 +0000490 }
Mark Whitley70705d72000-07-14 19:06:30 +0000491
492 /* give back whatever's left over */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000493 return (cmdstr);
Eric Andersen50d63601999-11-09 01:47:36 +0000494}
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000495
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000496static char *add_cmd(sed_cmd_t * sed_cmd, char *cmdstr)
Erik Andersen1266a131999-12-29 22:19:46 +0000497{
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000498 /* Skip over leading whitespace and semicolons */
499 cmdstr += strspn(cmdstr, semicolon_whitespace);
Erik Andersen1266a131999-12-29 22:19:46 +0000500
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000501 /* if we ate the whole thing, that means there was just trailing
502 * whitespace or a final / no-op semicolon. either way, get out */
503 if (*cmdstr == '\0') {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000504 return (NULL);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000505 }
Mark Whitley6315ce62000-07-10 22:55:51 +0000506
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000507 /* if this is a comment, jump past it and keep going */
508 if (*cmdstr == '#') {
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000509 /* "#n" is the same as using -n on the command line */
510 if (cmdstr[1] == 'n') {
511 be_quiet++;
512 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000513 return (strpbrk(cmdstr, "\n\r"));
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000514 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000515
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000516 /* Test for end of block */
517 if (*cmdstr == '}') {
518 in_block = 0;
519 cmdstr++;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000520 return (cmdstr);
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000521 }
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000522
523 /* parse the command
524 * format is: [addr][,addr]cmd
525 * |----||-----||-|
526 * part1 part2 part3
527 */
528
529 /* first part (if present) is an address: either a '$', a number or a /regex/ */
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000530 cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000531
532 /* second part (if present) will begin with a comma */
533 if (*cmdstr == ',') {
534 int idx;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000535
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000536 cmdstr++;
Glenn L McGrathf01b46d2003-03-30 08:02:18 +0000537 idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000538 if (idx == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000539 bb_error_msg_and_die("get_address: no address found in string\n"
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000540 "\t(you probably didn't check the string you passed me)");
Mark Whitley70705d72000-07-14 19:06:30 +0000541 }
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000542 cmdstr += idx;
543 }
Mark Whitley70705d72000-07-14 19:06:30 +0000544
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000545 /* skip whitespace before the command */
546 while (isspace(*cmdstr)) {
547 cmdstr++;
548 }
549
550 /* there my be the inversion flag between part2 and part3 */
551 if (*cmdstr == '!') {
552 sed_cmd->invert = 1;
553 cmdstr++;
554
555#ifdef SED_FEATURE_STRICT_CHECKING
556 /* According to the spec
557 * It is unspecified whether <blank>s can follow a '!' character,
558 * and conforming applications shall not follow a '!' character
559 * with <blank>s.
560 */
561 if (isblank(cmdstr[idx]) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000562 bb_error_msg_and_die("blank follows '!'");}
563#else
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000564 /* skip whitespace before the command */
565 while (isspace(*cmdstr)) {
566 cmdstr++;
567 }
568#endif
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000569 }
570
571 /* last part (mandatory) will be a command */
572 if (*cmdstr == '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000573 bb_error_msg_and_die("missing command");
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000574
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000575 /* This is the start of a block of commands */
576 if (*cmdstr == '{') {
577 if (in_block != 0) {
578 bb_error_msg_and_die("cant handle sub-blocks");
579 }
580 in_block = 1;
581 block_cmd = sed_cmd;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000582
583 return (cmdstr + 1);
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000584 }
585
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000586 sed_cmd->cmd = *cmdstr;
587 cmdstr++;
588
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000589 if (in_block == 1) {
590 sed_cmd->beg_match = block_cmd->beg_match;
591 sed_cmd->end_match = block_cmd->end_match;
592 sed_cmd->beg_line = block_cmd->beg_line;
593 sed_cmd->end_line = block_cmd->end_line;
594 sed_cmd->invert = block_cmd->invert;
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000595 }
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000596
597 cmdstr = parse_cmd_str(sed_cmd, cmdstr);
598
599 /* Add the command to the command array */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000600 sed_cmd_tail->next = sed_cmd;
601 sed_cmd_tail = sed_cmd_tail->next;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000602
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000603 return (cmdstr);
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000604}
605
606static void add_cmd_str(char *cmdstr)
607{
Glenn L McGrath0c518322003-03-30 03:41:53 +0000608#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
609 char *cmdstr_ptr = cmdstr;
610
611 /* HACK: convert "\n" to match tranlated '\n' string */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000612 while ((cmdstr_ptr = strstr(cmdstr_ptr, "\\n")) != NULL) {
Glenn L McGrath0c518322003-03-30 03:41:53 +0000613 cmdstr = xrealloc(cmdstr, strlen(cmdstr) + 2);
614 cmdstr_ptr = strstr(cmdstr, "\\n");
615 memmove(cmdstr_ptr + 1, cmdstr_ptr, strlen(cmdstr_ptr) + 1);
616 cmdstr_ptr[0] = '\\';
617 cmdstr_ptr += 3;
618 }
619#endif
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000620 do {
621 sed_cmd_t *sed_cmd;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000622
Glenn L McGrathf50ce312003-03-09 15:12:24 +0000623 sed_cmd = xcalloc(1, sizeof(sed_cmd_t));
624 cmdstr = add_cmd(sed_cmd, cmdstr);
625 } while (cmdstr && strlen(cmdstr));
Mark Whitley6315ce62000-07-10 22:55:51 +0000626}
627
628
629static void load_cmd_file(char *filename)
630{
631 FILE *cmdfile;
632 char *line;
Mark Whitley94074a92000-07-14 00:00:15 +0000633 char *nextline;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000634 char *e;
Mark Whitley6315ce62000-07-10 22:55:51 +0000635
Manuel Novoa III cad53642003-03-19 09:13:01 +0000636 cmdfile = bb_xfopen(filename, "r");
Mark Whitley6315ce62000-07-10 22:55:51 +0000637
Manuel Novoa III cad53642003-03-19 09:13:01 +0000638 while ((line = bb_get_line_from_file(cmdfile)) != NULL) {
Mark Whitley94074a92000-07-14 00:00:15 +0000639 /* if a line ends with '\' it needs the next line appended to it */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000640 while (((e = last_char_is(line, '\n')) != NULL)
641 && (e > line) && (e[-1] == '\\')
642 && ((nextline = bb_get_line_from_file(cmdfile)) != NULL)) {
643 line = xrealloc(line, (e - line) + 1 + strlen(nextline) + 1);
Mark Whitley94074a92000-07-14 00:00:15 +0000644 strcat(line, nextline);
Mark Whitley464c5de2000-07-14 23:24:00 +0000645 free(nextline);
Mark Whitley94074a92000-07-14 00:00:15 +0000646 }
Mark Whitley464c5de2000-07-14 23:24:00 +0000647 /* eat trailing newline (if any) --if I don't do this, edit commands
648 * (aic) will print an extra newline */
Matt Kraai05e782d2001-02-01 16:49:30 +0000649 chomp(line);
Mark Whitley6315ce62000-07-10 22:55:51 +0000650 add_cmd_str(line);
651 free(line);
652 }
653}
654
Eric Andersenc52a6b02001-11-10 10:49:42 +0000655struct pipeline {
656 char *buf;
657 int idx;
658 int len;
659};
660
Eric Andersenb76cb682001-08-22 05:58:16 +0000661#define PIPE_MAGIC 0x7f
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000662#define PIPE_GROW 64
Eric Andersenc52a6b02001-11-10 10:49:42 +0000663
664void pipe_putc(struct pipeline *const pipeline, char c)
665{
666 if (pipeline->buf[pipeline->idx] == PIPE_MAGIC) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000667 pipeline->buf = xrealloc(pipeline->buf, pipeline->len + PIPE_GROW);
Eric Andersenc52a6b02001-11-10 10:49:42 +0000668 memset(pipeline->buf + pipeline->len, 0, PIPE_GROW);
669 pipeline->len += PIPE_GROW;
670 pipeline->buf[pipeline->len - 1] = PIPE_MAGIC;
671 }
672 pipeline->buf[pipeline->idx++] = (c);
673}
674
675#define pipeputc(c) pipe_putc(pipeline, c)
676
677#if 0
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000678{
679 if (pipeline[pipeline_idx] == PIPE_MAGIC) {
680 pipeline = xrealloc(pipeline, pipeline_len + PIPE_GROW);
681 memset(pipeline + pipeline_len, 0, PIPE_GROW);
682 pipeline_len += PIPE_GROW;
683 pipeline[pipeline_len - 1] = PIPE_MAGIC;
684 }
685 pipeline[pipeline_idx++] = (c);
686}
Eric Andersenc52a6b02001-11-10 10:49:42 +0000687#endif
Eric Andersenb76cb682001-08-22 05:58:16 +0000688
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000689static void print_subst_w_backrefs(const char *line, const char *replace,
690 regmatch_t * regmatch,
691 struct pipeline *const pipeline,
692 int matches)
Mark Whitley97562bd2000-07-17 20:06:42 +0000693{
694 int i;
695
696 /* go through the replacement string */
697 for (i = 0; replace[i]; i++) {
698 /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000699 if (replace[i] == '\\' && isdigit(replace[i + 1])) {
Mark Whitley97562bd2000-07-17 20:06:42 +0000700 int j;
701 char tmpstr[2];
702 int backref;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000703
704 ++i; /* i now indexes the backref number, instead of the leading slash */
Mark Whitley97562bd2000-07-17 20:06:42 +0000705 tmpstr[0] = replace[i];
706 tmpstr[1] = 0;
707 backref = atoi(tmpstr);
708 /* print out the text held in regmatch[backref] */
Matt Kraaia3e4f452001-08-20 21:21:06 +0000709 if (backref <= matches && regmatch[backref].rm_so != -1)
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000710 for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo;
711 j++)
Eric Andersenb76cb682001-08-22 05:58:16 +0000712 pipeputc(line[j]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000713 }
714
Mark Whitley83e85f62000-07-25 20:48:44 +0000715 /* if we find a backslash escaped character, print the character */
716 else if (replace[i] == '\\') {
717 ++i;
Eric Andersenb76cb682001-08-22 05:58:16 +0000718 pipeputc(replace[i]);
Mark Whitley83e85f62000-07-25 20:48:44 +0000719 }
720
Mark Whitley97562bd2000-07-17 20:06:42 +0000721 /* if we find an unescaped '&' print out the whole matched text.
722 * fortunately, regmatch[0] contains the indicies to the whole matched
723 * expression (kinda seems like it was designed for just such a
724 * purpose...) */
Glenn L McGrath24103862003-04-09 07:51:43 +0000725 else if (replace[i] == '&') {
Mark Whitley97562bd2000-07-17 20:06:42 +0000726 int j;
727 for (j = regmatch[0].rm_so; j < regmatch[0].rm_eo; j++)
Eric Andersenb76cb682001-08-22 05:58:16 +0000728 pipeputc(line[j]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000729 }
730 /* nothing special, just print this char of the replacement string to stdout */
731 else
Eric Andersenb76cb682001-08-22 05:58:16 +0000732 pipeputc(replace[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000733 }
734}
735
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000736static int do_subst_command(sed_cmd_t * sed_cmd, char **line)
Mark Whitley4f7fe772000-07-13 20:01:58 +0000737{
Eric Andersenb76cb682001-08-22 05:58:16 +0000738 char *hackline = *line;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000739 struct pipeline thepipe = { NULL, 0, 0 };
Eric Andersenc52a6b02001-11-10 10:49:42 +0000740 struct pipeline *const pipeline = &thepipe;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000741 int altered = 0;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000742 int result;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000743 regmatch_t *regmatch = NULL;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000744 regex_t *current_regex;
745
746 if (sed_cmd->sub_match == NULL) {
747 current_regex = previous_regex_ptr;
748 } else {
749 previous_regex_ptr = current_regex = sed_cmd->sub_match;
750 }
751 result = regexec(current_regex, hackline, 0, NULL, 0);
Mark Whitley4f7fe772000-07-13 20:01:58 +0000752
Mark Whitley2dc192f2000-11-03 19:47:00 +0000753 /* we only proceed if the substitution 'search' expression matches */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000754 if (result == REG_NOMATCH) {
Mark Whitley2dc192f2000-11-03 19:47:00 +0000755 return 0;
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000756 }
Mark Whitley2dc192f2000-11-03 19:47:00 +0000757
758 /* whaddaya know, it matched. get the number of back references */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000759 regmatch = xmalloc(sizeof(regmatch_t) * (sed_cmd->num_backrefs + 1));
Mark Whitley2dc192f2000-11-03 19:47:00 +0000760
Eric Andersenb76cb682001-08-22 05:58:16 +0000761 /* allocate more PIPE_GROW bytes
762 if replaced string is larger than original */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000763 thepipe.len = strlen(hackline) + PIPE_GROW;
Eric Andersenc52a6b02001-11-10 10:49:42 +0000764 thepipe.buf = xcalloc(1, thepipe.len);
Eric Andersenb76cb682001-08-22 05:58:16 +0000765 /* buffer magic */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000766 thepipe.buf[thepipe.len - 1] = PIPE_MAGIC;
Eric Andersenb76cb682001-08-22 05:58:16 +0000767
Mark Whitley2dc192f2000-11-03 19:47:00 +0000768 /* and now, as long as we've got a line to try matching and if we can match
769 * the search string, we make substitutions */
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000770 while ((*hackline || !altered) && (regexec(current_regex, hackline,
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000771 sed_cmd->num_backrefs + 1,
772 regmatch, 0) != REG_NOMATCH)) {
Mark Whitley4f7fe772000-07-13 20:01:58 +0000773 int i;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000774
Mark Whitley2dc192f2000-11-03 19:47:00 +0000775 /* print everything before the match */
776 for (i = 0; i < regmatch[0].rm_so; i++)
Eric Andersenb76cb682001-08-22 05:58:16 +0000777 pipeputc(hackline[i]);
Mark Whitley97562bd2000-07-17 20:06:42 +0000778
Mark Whitley2dc192f2000-11-03 19:47:00 +0000779 /* then print the substitution string */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000780 print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch,
781 pipeline, sed_cmd->num_backrefs);
Mark Whitley97562bd2000-07-17 20:06:42 +0000782
Mark Whitley2dc192f2000-11-03 19:47:00 +0000783 /* advance past the match */
784 hackline += regmatch[0].rm_eo;
785 /* flag that something has changed */
786 altered++;
Mark Whitley97562bd2000-07-17 20:06:42 +0000787
Mark Whitley2dc192f2000-11-03 19:47:00 +0000788 /* if we're not doing this globally, get out now */
Glenn L McGrath7ce9e242003-04-07 16:04:14 +0000789 if (!sed_cmd->sub_g) {
Mark Whitley2dc192f2000-11-03 19:47:00 +0000790 break;
Glenn L McGrath7ce9e242003-04-07 16:04:14 +0000791 }
Mark Whitley4f7fe772000-07-13 20:01:58 +0000792 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000793 for (; *hackline; hackline++)
794 pipeputc(*hackline);
795 if (thepipe.buf[thepipe.idx] == PIPE_MAGIC)
796 thepipe.buf[thepipe.idx] = 0;
Mark Whitley2dc192f2000-11-03 19:47:00 +0000797
798 /* cleanup */
799 free(regmatch);
800
Eric Andersenb76cb682001-08-22 05:58:16 +0000801 free(*line);
Eric Andersenc52a6b02001-11-10 10:49:42 +0000802 *line = thepipe.buf;
Mark Whitley4f7fe772000-07-13 20:01:58 +0000803 return altered;
804}
Mark Whitley6315ce62000-07-10 22:55:51 +0000805
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000806static sed_cmd_t *branch_to(const char *label)
807{
808 sed_cmd_t *sed_cmd;
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000809 for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
810 if ((sed_cmd->label) && (strcmp(sed_cmd->label, label) == 0)) {
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000811 break;
812 }
813 }
814
815 /* If no match returns last command */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000816 return (sed_cmd);
Glenn L McGrath961c6c12003-03-28 04:43:39 +0000817}
Mark Whitley6315ce62000-07-10 22:55:51 +0000818
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000819static void process_file(FILE * file)
Mark Whitley6315ce62000-07-10 22:55:51 +0000820{
Glenn L McGrathc6adada2003-04-07 12:24:44 +0000821 char *pattern_space; /* Posix requires it be able to hold at least 8192 bytes */
Glenn L McGrath7ce9e242003-04-07 16:04:14 +0000822 char *hold_space = NULL; /* Posix requires it be able to hold at least 8192 bytes */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000823 static int linenum = 0; /* GNU sed does not restart counting lines at EOF */
Mark Whitley6315ce62000-07-10 22:55:51 +0000824 unsigned int still_in_range = 0;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000825 int altered;
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +0000826 int force_print;
Mark Whitley6315ce62000-07-10 22:55:51 +0000827
Glenn L McGrathc6adada2003-04-07 12:24:44 +0000828 pattern_space = bb_get_chomped_line_from_file(file);
829 if (pattern_space == NULL) {
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000830 return;
831 }
832
Mark Whitley6315ce62000-07-10 22:55:51 +0000833 /* go through every line in the file */
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000834 do {
835 char *next_line;
Glenn L McGrathc949bfa2003-03-28 03:53:31 +0000836 sed_cmd_t *sed_cmd;
Glenn L McGrath595a6a42003-03-28 08:36:52 +0000837 int substituted = 0;
Glenn L McGrath505bd0f2003-03-08 05:21:02 +0000838
839 /* Read one line in advance so we can act on the last line, the '$' address */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000840 next_line = bb_get_chomped_line_from_file(file);
Mark Whitley6315ce62000-07-10 22:55:51 +0000841
842 linenum++;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000843 altered = 0;
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +0000844 force_print = 0;
Mark Whitley6315ce62000-07-10 22:55:51 +0000845
846 /* for every line, go through all the commands */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000847 for (sed_cmd = sed_cmd_head.next; sed_cmd;
848 sed_cmd = sed_cmd->next) {
Robert Griebl47abc492002-06-11 23:43:27 +0000849 int deleted = 0;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000850
851 /*
852 * entry point into sedding...
853 */
Robert Griebl47abc492002-06-11 23:43:27 +0000854 int matched = (
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000855 /* no range necessary */
856 (sed_cmd->beg_line == 0
857 && sed_cmd->end_line == 0
858 && sed_cmd->beg_match == NULL
859 && sed_cmd->end_match == NULL) ||
860 /* this line number is the first address we're looking for */
861 (sed_cmd->beg_line
862 && (sed_cmd->beg_line == linenum)) ||
863 /* this line matches our first address regex */
864 (sed_cmd->beg_match
865 &&
866 (regexec
867 (sed_cmd->beg_match, pattern_space, 0, NULL,
868 0) == 0)) ||
869 /* we are currently within the beginning & ending address range */
870 still_in_range || ((sed_cmd->beg_line == -1)
871 && (next_line == NULL))
872 );
Robert Griebl47abc492002-06-11 23:43:27 +0000873
874 if (sed_cmd->invert ^ matched) {
Glenn L McGrathc1d95072003-04-08 06:42:45 +0000875 /* Update last used regex incase a blank substitute BRE is found */
876 if (sed_cmd->beg_match) {
877 previous_regex_ptr = sed_cmd->beg_match;
878 }
Mark Whitley0915c4b2001-06-11 23:50:06 +0000879
880 /*
881 * actual sedding
882 */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000883 switch (sed_cmd->cmd) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000884 case '=':
885 printf("%d\n", linenum);
886 break;
887 case 'P':{
888 /* Write the current pattern space upto the first newline */
889 char *tmp = strchr(pattern_space, '\n');
Mark Whitley0915c4b2001-06-11 23:50:06 +0000890
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000891 if (tmp) {
892 *tmp = '\0';
893 }
894 }
895 case 'p': /* Write the current pattern space to output */
896 puts(pattern_space);
897 break;
898 case 'd':
899 altered++;
900 deleted = 1;
901 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000902
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000903 case 's':
904
905 /*
906 * Some special cases for 's' printing to make it compliant with
907 * GNU sed printing behavior (aka "The -n | s///p Matrix"):
908 *
909 * -n ONLY = never print anything regardless of any successful
910 * substitution
911 *
912 * s///p ONLY = always print successful substitutions, even if
913 * the pattern_space is going to be printed anyway (pattern_space
914 * will be printed twice).
915 *
916 * -n AND s///p = print ONLY a successful substitution ONE TIME;
917 * no other lines are printed - this is the reason why the 'p'
918 * flag exists in the first place.
919 */
Mark Whitley0915c4b2001-06-11 23:50:06 +0000920
Glenn L McGrath0c518322003-03-30 03:41:53 +0000921#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000922 /* HACK: escape newlines twice so regex can match them */
923 {
924 int offset = 0;
925
926 while (strchr(pattern_space + offset, '\n') != NULL) {
927 char *tmp;
928
929 pattern_space =
930 xrealloc(pattern_space,
931 strlen(pattern_space) + 2);
932 tmp = strchr(pattern_space + offset, '\n');
933 memmove(tmp + 1, tmp, strlen(tmp) + 1);
934 tmp[0] = '\\';
935 tmp[1] = 'n';
936 offset = tmp - pattern_space + 2;
937 }
938 }
Glenn L McGrath0c518322003-03-30 03:41:53 +0000939#endif
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000940 /* we print the pattern_space once, unless we were told to be quiet */
941 substituted = do_subst_command(sed_cmd, &pattern_space);
Glenn L McGrath0c518322003-03-30 03:41:53 +0000942
943#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000944 /* undo HACK: escape newlines twice so regex can match them */
945 {
946 char *tmp = pattern_space;
Glenn L McGrath0c518322003-03-30 03:41:53 +0000947
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000948 while ((tmp = strstr(tmp, "\\n")) != NULL) {
949 memmove(tmp, tmp + 1, strlen(tmp + 1) + 1);
950 tmp[0] = '\n';
Glenn L McGrath0c518322003-03-30 03:41:53 +0000951 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000952 }
Glenn L McGrath0c518322003-03-30 03:41:53 +0000953#endif
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000954 altered |= substituted;
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +0000955 if (!be_quiet && altered && ((sed_cmd->next == NULL)
956 || (sed_cmd->next->cmd !=
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000957 's'))) {
958 force_print = 1;
959 }
Mark Whitley0915c4b2001-06-11 23:50:06 +0000960
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000961 /* we also print the line if we were given the 'p' flag
962 * (this is quite possibly the second printing) */
963 if ((sed_cmd->sub_p) && altered) {
Glenn L McGrathc6adada2003-04-07 12:24:44 +0000964 puts(pattern_space);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000965 }
966 break;
967 case 'a':
968 puts(pattern_space);
969 fputs(sed_cmd->editline, stdout);
970 altered++;
971 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000972
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000973 case 'i':
974 fputs(sed_cmd->editline, stdout);
975 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000976
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000977 case 'c':
978 /* single-address case */
979 if ((sed_cmd->end_match == NULL && sed_cmd->end_line == 0)
Mark Whitley0915c4b2001-06-11 23:50:06 +0000980 /* multi-address case */
Eric Andersenc52a6b02001-11-10 10:49:42 +0000981 /* - matching text */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000982 || (sed_cmd->end_match
983 &&
984 (regexec
985 (sed_cmd->end_match, pattern_space, 0, NULL,
986 0) == 0))
Eric Andersenc52a6b02001-11-10 10:49:42 +0000987 /* - matching line numbers */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000988 || (sed_cmd->end_line > 0
989 && sed_cmd->end_line == linenum)) {
990 fputs(sed_cmd->editline, stdout);
991 }
992 altered++;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000993
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000994 break;
Mark Whitley0915c4b2001-06-11 23:50:06 +0000995
Glenn L McGrath8d6395d2003-04-08 11:56:11 +0000996 case 'r':{
997 FILE *outfile;
998
999 puts(pattern_space);
1000 outfile = fopen(sed_cmd->filename, "r");
1001 if (outfile)
1002 bb_xprint_and_close_file(outfile);
1003 /* else if we couldn't open the output file,
1004 * no biggie, just don't print anything */
1005 altered++;
1006 }
1007 break;
1008 case 'q': /* Branch to end of script and quit */
1009 deleted = 1;
1010 /* Exit the outer while loop */
1011 free(next_line);
1012 next_line = NULL;
1013 break;
1014 case 'n': /* Read next line from input */
1015 free(pattern_space);
1016 pattern_space = next_line;
1017 next_line = bb_get_chomped_line_from_file(file);
1018 linenum++;
1019 break;
1020 case 'N': /* Append the next line to the current line */
1021 if (next_line) {
1022 pattern_space =
1023 realloc(pattern_space,
1024 strlen(pattern_space) +
1025 strlen(next_line) + 2);
1026 strcat(pattern_space, "\n");
1027 strcat(pattern_space, next_line);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001028 next_line = bb_get_chomped_line_from_file(file);
Glenn L McGrath4157a8a2003-03-10 04:12:35 +00001029 linenum++;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001030 }
1031 break;
1032 case 'b':
1033 sed_cmd = branch_to(sed_cmd->label);
1034 break;
1035 case 't':
1036 if (substituted) {
Glenn L McGrath961c6c12003-03-28 04:43:39 +00001037 sed_cmd = branch_to(sed_cmd->label);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001038 }
1039 break;
1040 case 'y':{
1041 int i;
1042
1043 for (i = 0; pattern_space[i] != 0; i++) {
1044 int j;
1045
1046 for (j = 0; sed_cmd->translate[j]; j += 2) {
1047 if (pattern_space[i] == sed_cmd->translate[j]) {
1048 pattern_space[i] = sed_cmd->translate[j + 1];
Glenn L McGrathf01b46d2003-03-30 08:02:18 +00001049 }
1050 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001051 }
1052 }
1053 break;
1054 case 'g': /* Replace pattern space with hold space */
1055 free(pattern_space);
1056 pattern_space = strdup(hold_space);
1057 break;
1058 case 'h': /* Replace hold space with pattern space */
1059 free(hold_space);
1060 hold_space = strdup(pattern_space);
1061 break;
1062 case 'x':{
1063 /* Swap hold and pattern space */
1064 char *tmp;
1065
1066 tmp = pattern_space;
1067 pattern_space = hold_space;
1068 hold_space = tmp;
1069 }
Mark Whitley0915c4b2001-06-11 23:50:06 +00001070 }
Robert Griebl47abc492002-06-11 23:43:27 +00001071 }
Mark Whitley0915c4b2001-06-11 23:50:06 +00001072
Robert Griebl47abc492002-06-11 23:43:27 +00001073 /*
1074 * exit point from sedding...
1075 */
1076 if (matched) {
Mark Whitley0915c4b2001-06-11 23:50:06 +00001077 if (
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001078 /* this is a single-address command or... */
1079 (sed_cmd->end_line == 0 && sed_cmd->end_match == NULL)
1080 || (
1081 /* If only one address */
1082 /* we were in the middle of our address range (this
1083 * isn't the first time through) and.. */
1084 (still_in_range == 1) && (
1085 /* this line number is the last address we're looking for or... */
1086 (sed_cmd->
1087 end_line
1088 && (sed_cmd->
1089 end_line ==
1090 linenum))
1091 ||
1092 /* this line matches our last address regex */
1093 (sed_cmd->
1094 end_match
1095 &&
1096 (regexec
1097 (sed_cmd->
1098 end_match,
1099 pattern_space,
1100 0, NULL,
1101 0) == 0))
1102 )
1103 )
1104 ) {
Mark Whitley0915c4b2001-06-11 23:50:06 +00001105 /* we're out of our address range */
1106 still_in_range = 0;
1107 }
1108
1109 /* didn't hit the exit? then we're still in the middle of an address range */
1110 else {
1111 still_in_range = 1;
Mark Whitley6315ce62000-07-10 22:55:51 +00001112 }
1113 }
Robert Griebl47abc492002-06-11 23:43:27 +00001114
1115 if (deleted)
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001116 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001117 }
Erik Andersen1266a131999-12-29 22:19:46 +00001118
Mark Whitley2dc192f2000-11-03 19:47:00 +00001119 /* we will print the line unless we were told to be quiet or if the
1120 * line was altered (via a 'd'elete or 's'ubstitution), in which case
1121 * the altered line was already printed */
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001122 if ((!be_quiet && !altered) || force_print) {
Glenn L McGrathc6adada2003-04-07 12:24:44 +00001123 puts(pattern_space);
Glenn L McGratha0f0f0c2003-03-28 14:11:34 +00001124 }
Glenn L McGrathc6adada2003-04-07 12:24:44 +00001125 free(pattern_space);
1126 pattern_space = next_line;
1127 } while (pattern_space);
Erik Andersen1266a131999-12-29 22:19:46 +00001128}
1129
1130extern int sed_main(int argc, char **argv)
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001131{
Matt Kraaia5f09c62001-11-12 16:44:55 +00001132 int opt, status = EXIT_SUCCESS;
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001133
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001134#ifdef CONFIG_FEATURE_CLEAN_UP
Mark Whitley858c1ad2000-07-11 21:38:47 +00001135 /* destroy command strings on exit */
Matt Kraaia9819b22000-12-22 01:48:07 +00001136 if (atexit(destroy_cmd_strs) == -1)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001137 bb_perror_msg_and_die("atexit");
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001138#endif
Mark Whitley858c1ad2000-07-11 21:38:47 +00001139
Mark Whitley6315ce62000-07-10 22:55:51 +00001140 /* do normal option parsing */
Eric Andersenb50da532001-02-17 16:52:35 +00001141 while ((opt = getopt(argc, argv, "ne:f:")) > 0) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001142 switch (opt) {
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001143 case 'n':
1144 be_quiet++;
1145 break;
1146 case 'e':{
1147 char *str_cmd = strdup(optarg);
1148
1149 add_cmd_str(str_cmd);
1150 free(str_cmd);
1151 break;
1152 }
1153 case 'f':
1154 load_cmd_file(optarg);
1155 break;
1156 default:
1157 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001158 }
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001159 }
Mark Whitley6315ce62000-07-10 22:55:51 +00001160
1161 /* if we didn't get a pattern from a -e and no command file was specified,
1162 * argv[optind] should be the pattern. no pattern, no worky */
Glenn L McGrathbd9b32b2003-04-09 01:43:54 +00001163 if (sed_cmd_head.next == NULL) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001164 if (argv[optind] == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001165 bb_show_usage();
Mark Whitley6315ce62000-07-10 22:55:51 +00001166 else {
Glenn L McGrath0c518322003-03-30 03:41:53 +00001167 char *str_cmd = strdup(argv[optind]);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001168
Glenn L McGrath0c518322003-03-30 03:41:53 +00001169 add_cmd_str(strdup(str_cmd));
1170 free(str_cmd);
Mark Whitley6315ce62000-07-10 22:55:51 +00001171 optind++;
1172 }
1173 }
1174
Mark Whitley6315ce62000-07-10 22:55:51 +00001175 /* argv[(optind)..(argc-1)] should be names of file to process. If no
1176 * files were specified or '-' was specified, take input from stdin.
1177 * Otherwise, we process all the files specified. */
1178 if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) {
1179 process_file(stdin);
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001180 } else {
Mark Whitley6315ce62000-07-10 22:55:51 +00001181 int i;
1182 FILE *file;
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001183
Mark Whitley6315ce62000-07-10 22:55:51 +00001184 for (i = optind; i < argc; i++) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001185 file = bb_wfopen(argv[i], "r");
Eric Andersen9c6b5fc2001-11-17 07:23:46 +00001186 if (file) {
Mark Whitley6315ce62000-07-10 22:55:51 +00001187 process_file(file);
1188 fclose(file);
Matt Kraaia5f09c62001-11-12 16:44:55 +00001189 } else
1190 status = EXIT_FAILURE;
Mark Whitley6315ce62000-07-10 22:55:51 +00001191 }
1192 }
Glenn L McGrath8d6395d2003-04-08 11:56:11 +00001193
Matt Kraaia5f09c62001-11-12 16:44:55 +00001194 return status;
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001195}