aboutsummaryrefslogtreecommitdiff
path: root/sed.c
diff options
context:
space:
mode:
authorMark Whitley <markw@lineo.com>2000-07-11 21:38:47 +0000
committerMark Whitley <markw@lineo.com>2000-07-11 21:38:47 +0000
commit858c1adf2b1ce4829eb8994381412c3cb12e7250 (patch)
treec23d8533a54c14c1d41c2f5b172380c89ae64ebd /sed.c
parent7719008a85c69db66e2f948d6779b6a45352018d (diff)
downloadbusybox-w32-858c1adf2b1ce4829eb8994381412c3cb12e7250.tar.gz
busybox-w32-858c1adf2b1ce4829eb8994381412c3cb12e7250.tar.bz2
busybox-w32-858c1adf2b1ce4829eb8994381412c3cb12e7250.zip
Applied patch from Matt Kraai to call destroy_cmd_strs in atexit(), rather
than peppering it throughout the code.
Diffstat (limited to 'sed.c')
-rw-r--r--sed.c39
1 files changed, 17 insertions, 22 deletions
diff --git a/sed.c b/sed.c
index 2fb243fb9..b6bfcdb68 100644
--- a/sed.c
+++ b/sed.c
@@ -126,14 +126,6 @@ static void destroy_cmd_strs()
126 sed_cmds = NULL; 126 sed_cmds = NULL;
127} 127}
128 128
129static void exit_sed(int retcode, const char *message)
130{
131 destroy_cmd_strs();
132 if (message)
133 fputs(message, stderr);
134 exit(retcode);
135}
136
137/* 129/*
138 * trim_str - trims leading and trailing space from a string 130 * trim_str - trims leading and trailing space from a string
139 * 131 *
@@ -204,12 +196,12 @@ static int get_address(const char *str, int *line, regex_t **regex)
204 else if (my_str[idx] == '/') { 196 else if (my_str[idx] == '/') {
205 idx = index_of_next_unescaped_slash(idx, my_str); 197 idx = index_of_next_unescaped_slash(idx, my_str);
206 if (idx == -1) 198 if (idx == -1)
207 exit_sed(1, "sed: unterminated match expression\n"); 199 fatalError("sed: unterminated match expression\n");
208 my_str[idx] = '\0'; 200 my_str[idx] = '\0';
209 *regex = (regex_t *)xmalloc(sizeof(regex_t)); 201 *regex = (regex_t *)xmalloc(sizeof(regex_t));
210 if (bb_regcomp(*regex, my_str+1, REG_NEWLINE) != 0) { 202 if (bb_regcomp(*regex, my_str+1, REG_NEWLINE) != 0) {
211 free(my_str); 203 free(my_str);
212 exit_sed(1, NULL); 204 exit(1);
213 } 205 }
214 } 206 }
215 else { 207 else {
@@ -251,9 +243,9 @@ static void parse_cmd_str(struct sed_cmd *sed_cmd, const char *cmdstr)
251 243
252 /* last part (mandatory) will be a command */ 244 /* last part (mandatory) will be a command */
253 if (cmdstr[idx] == '\0') 245 if (cmdstr[idx] == '\0')
254 exit_sed(1, "sed: missing command\n"); 246 fatalError("sed: missing command\n");
255 if (!strchr("pds", cmdstr[idx])) /* <-- XXX add new commands here */ 247 if (!strchr("pds", cmdstr[idx])) /* <-- XXX add new commands here */
256 exit_sed(1, "sed: invalid command\n"); 248 fatalError("sed: invalid command\n");
257 sed_cmd->cmd = cmdstr[idx]; 249 sed_cmd->cmd = cmdstr[idx];
258 /* special-case handling for 's' */ 250 /* special-case handling for 's' */
259 if (sed_cmd->cmd == 's') { 251 if (sed_cmd->cmd == 's') {
@@ -267,20 +259,20 @@ static void parse_cmd_str(struct sed_cmd *sed_cmd, const char *cmdstr)
267 259
268 /* verify that we have an 's' followed by a 'slash' */ 260 /* verify that we have an 's' followed by a 'slash' */
269 if (cmdstr[++idx] != '/') 261 if (cmdstr[++idx] != '/')
270 exit_sed(1, "sed: bad format in substitution expression\n"); 262 fatalError("sed: bad format in substitution expression\n");
271 263
272 /* save the match string */ 264 /* save the match string */
273 oldidx = idx+1; 265 oldidx = idx+1;
274 idx = index_of_next_unescaped_slash(idx, cmdstr); 266 idx = index_of_next_unescaped_slash(idx, cmdstr);
275 if (idx == -1) 267 if (idx == -1)
276 exit_sed(1, "sed: bad format in substitution expression\n"); 268 fatalError("sed: bad format in substitution expression\n");
277 match = strdup_substr(cmdstr, oldidx, idx); 269 match = strdup_substr(cmdstr, oldidx, idx);
278 270
279 /* save the replacement string */ 271 /* save the replacement string */
280 oldidx = idx+1; 272 oldidx = idx+1;
281 idx = index_of_next_unescaped_slash(idx, cmdstr); 273 idx = index_of_next_unescaped_slash(idx, cmdstr);
282 if (idx == -1) 274 if (idx == -1)
283 exit_sed(1, "sed: bad format in substitution expression\n"); 275 fatalError("sed: bad format in substitution expression\n");
284 sed_cmd->replace = strdup_substr(cmdstr, oldidx, idx); 276 sed_cmd->replace = strdup_substr(cmdstr, oldidx, idx);
285 277
286 /* process the flags */ 278 /* process the flags */
@@ -293,7 +285,7 @@ static void parse_cmd_str(struct sed_cmd *sed_cmd, const char *cmdstr)
293 cflags |= REG_ICASE; 285 cflags |= REG_ICASE;
294 break; 286 break;
295 default: 287 default:
296 exit_sed(1, "sed: bad option in substitution expression\n"); 288 fatalError("sed: bad option in substitution expression\n");
297 } 289 }
298 } 290 }
299 291
@@ -301,7 +293,7 @@ static void parse_cmd_str(struct sed_cmd *sed_cmd, const char *cmdstr)
301 sed_cmd->sub_match = (regex_t *)xmalloc(sizeof(regex_t)); 293 sed_cmd->sub_match = (regex_t *)xmalloc(sizeof(regex_t));
302 if (bb_regcomp(sed_cmd->sub_match, match, cflags) != 0) { 294 if (bb_regcomp(sed_cmd->sub_match, match, cflags) != 0) {
303 free(match); 295 free(match);
304 exit_sed(1, NULL); 296 exit(1);
305 } 297 }
306 free(match); 298 free(match);
307 } 299 }
@@ -333,7 +325,7 @@ static void load_cmd_file(char *filename)
333 325
334 cmdfile = fopen(filename, "r"); 326 cmdfile = fopen(filename, "r");
335 if (cmdfile == NULL) 327 if (cmdfile == NULL)
336 exit_sed(1, strerror(errno)); 328 fatalError(strerror(errno));
337 329
338 while ((line = get_line_from_file(cmdfile)) != NULL) { 330 while ((line = get_line_from_file(cmdfile)) != NULL) {
339 line[strlen(line)-1] = 0; /* eat newline */ 331 line[strlen(line)-1] = 0; /* eat newline */
@@ -464,10 +456,16 @@ extern int sed_main(int argc, char **argv)
464{ 456{
465 int opt; 457 int opt;
466 458
467 /* do special-case option parsing */ 459 /* do special-case option parsing */
468 if (argv[1] && (strcmp(argv[1], "--help") == 0)) 460 if (argv[1] && (strcmp(argv[1], "--help") == 0))
469 usage(sed_usage); 461 usage(sed_usage);
470 462
463 /* destroy command strings on exit */
464 if (atexit(destroy_cmd_strs) == -1) {
465 perror("sed");
466 exit(1);
467 }
468
471 /* do normal option parsing */ 469 /* do normal option parsing */
472 while ((opt = getopt(argc, argv, "Vhne:f:")) > 0) { 470 while ((opt = getopt(argc, argv, "Vhne:f:")) > 0) {
473 switch (opt) { 471 switch (opt) {
@@ -522,8 +520,5 @@ extern int sed_main(int argc, char **argv)
522 } 520 }
523 } 521 }
524 522
525 exit_sed(0, NULL);
526
527 /* not reached */
528 return 0; 523 return 0;
529} 524}