diff options
author | Mark Whitley <markw@lineo.com> | 2000-07-13 20:01:58 +0000 |
---|---|---|
committer | Mark Whitley <markw@lineo.com> | 2000-07-13 20:01:58 +0000 |
commit | 4f7fe77d07294586687382597d04fb46a24092e5 (patch) | |
tree | 596c63765bf27c51c8a4cec40849fd5769202213 | |
parent | 06f3529ada8c564673eb2d551b9979b3d65a7475 (diff) | |
download | busybox-w32-4f7fe77d07294586687382597d04fb46a24092e5.tar.gz busybox-w32-4f7fe77d07294586687382597d04fb46a24092e5.tar.bz2 busybox-w32-4f7fe77d07294586687382597d04fb46a24092e5.zip |
(Something I should have done in the previous checkin...) Also broke out
substitution command execution from do_sed_command() and put it in it's own
do_subst_command() function.
-rw-r--r-- | editors/sed.c | 80 | ||||
-rw-r--r-- | sed.c | 80 |
2 files changed, 86 insertions, 74 deletions
diff --git a/editors/sed.c b/editors/sed.c index 8ae34f8c7..22d642ee9 100644 --- a/editors/sed.c +++ b/editors/sed.c | |||
@@ -340,6 +340,47 @@ static void load_cmd_file(char *filename) | |||
340 | } | 340 | } |
341 | } | 341 | } |
342 | 342 | ||
343 | static int do_subst_command(const struct sed_cmd *sed_cmd, const char *line) | ||
344 | { | ||
345 | int altered = 0; | ||
346 | |||
347 | /* we only substitute if the substitution 'search' expression matches */ | ||
348 | if (regexec(sed_cmd->sub_match, line, 0, NULL, 0) == 0) { | ||
349 | regmatch_t regmatch; | ||
350 | int i; | ||
351 | char *ptr = (char *)line; | ||
352 | |||
353 | while (*ptr) { | ||
354 | /* if we can match the search string... */ | ||
355 | if (regexec(sed_cmd->sub_match, ptr, 1, ®match, 0) == 0) { | ||
356 | /* print everything before the match, */ | ||
357 | for (i = 0; i < regmatch.rm_so; i++) | ||
358 | fputc(ptr[i], stdout); | ||
359 | /* then print the substitution in its place */ | ||
360 | fputs(sed_cmd->replace, stdout); | ||
361 | /* then advance past the match */ | ||
362 | ptr += regmatch.rm_eo; | ||
363 | /* and let the calling function know that something | ||
364 | * has been changed */ | ||
365 | altered++; | ||
366 | |||
367 | /* if we're not doing this globally... */ | ||
368 | if (!sed_cmd->sub_g) | ||
369 | break; | ||
370 | } | ||
371 | /* if we COULD NOT match the search string (meaning we've gone past | ||
372 | * all previous instances), get out */ | ||
373 | else | ||
374 | break; | ||
375 | } | ||
376 | |||
377 | /* is there anything left to print? */ | ||
378 | if (*ptr) | ||
379 | fputs(ptr, stdout); | ||
380 | } | ||
381 | |||
382 | return altered; | ||
383 | } | ||
343 | 384 | ||
344 | static int do_sed_command(const struct sed_cmd *sed_cmd, const char *line) | 385 | static int do_sed_command(const struct sed_cmd *sed_cmd, const char *line) |
345 | { | 386 | { |
@@ -355,43 +396,8 @@ static int do_sed_command(const struct sed_cmd *sed_cmd, const char *line) | |||
355 | altered++; | 396 | altered++; |
356 | break; | 397 | break; |
357 | 398 | ||
358 | case 's': /* oo, a fun one :-) */ | 399 | case 's': |
359 | 400 | altered = do_subst_command(sed_cmd, line); | |
360 | /* we only substitute if the substitution 'search' expression matches */ | ||
361 | if (regexec(sed_cmd->sub_match, line, 0, NULL, 0) == 0) { | ||
362 | regmatch_t regmatch; | ||
363 | int i; | ||
364 | char *ptr = (char *)line; | ||
365 | |||
366 | while (*ptr) { | ||
367 | /* if we can match the search string... */ | ||
368 | if (regexec(sed_cmd->sub_match, ptr, 1, ®match, 0) == 0) { | ||
369 | /* print everything before the match, */ | ||
370 | for (i = 0; i < regmatch.rm_so; i++) | ||
371 | fputc(ptr[i], stdout); | ||
372 | /* then print the substitution in its place */ | ||
373 | fputs(sed_cmd->replace, stdout); | ||
374 | /* then advance past the match */ | ||
375 | ptr += regmatch.rm_eo; | ||
376 | /* and let the calling function know that something | ||
377 | * has been changed */ | ||
378 | altered++; | ||
379 | |||
380 | /* if we're not doing this globally... */ | ||
381 | if (!sed_cmd->sub_g) | ||
382 | break; | ||
383 | } | ||
384 | /* if we COULD NOT match the search string (meaning we've gone past | ||
385 | * all previous instances), get out */ | ||
386 | else | ||
387 | break; | ||
388 | } | ||
389 | |||
390 | /* is there anything left to print? */ | ||
391 | if (*ptr) | ||
392 | fputs(ptr, stdout); | ||
393 | } | ||
394 | |||
395 | break; | 401 | break; |
396 | } | 402 | } |
397 | 403 | ||
@@ -340,6 +340,47 @@ static void load_cmd_file(char *filename) | |||
340 | } | 340 | } |
341 | } | 341 | } |
342 | 342 | ||
343 | static int do_subst_command(const struct sed_cmd *sed_cmd, const char *line) | ||
344 | { | ||
345 | int altered = 0; | ||
346 | |||
347 | /* we only substitute if the substitution 'search' expression matches */ | ||
348 | if (regexec(sed_cmd->sub_match, line, 0, NULL, 0) == 0) { | ||
349 | regmatch_t regmatch; | ||
350 | int i; | ||
351 | char *ptr = (char *)line; | ||
352 | |||
353 | while (*ptr) { | ||
354 | /* if we can match the search string... */ | ||
355 | if (regexec(sed_cmd->sub_match, ptr, 1, ®match, 0) == 0) { | ||
356 | /* print everything before the match, */ | ||
357 | for (i = 0; i < regmatch.rm_so; i++) | ||
358 | fputc(ptr[i], stdout); | ||
359 | /* then print the substitution in its place */ | ||
360 | fputs(sed_cmd->replace, stdout); | ||
361 | /* then advance past the match */ | ||
362 | ptr += regmatch.rm_eo; | ||
363 | /* and let the calling function know that something | ||
364 | * has been changed */ | ||
365 | altered++; | ||
366 | |||
367 | /* if we're not doing this globally... */ | ||
368 | if (!sed_cmd->sub_g) | ||
369 | break; | ||
370 | } | ||
371 | /* if we COULD NOT match the search string (meaning we've gone past | ||
372 | * all previous instances), get out */ | ||
373 | else | ||
374 | break; | ||
375 | } | ||
376 | |||
377 | /* is there anything left to print? */ | ||
378 | if (*ptr) | ||
379 | fputs(ptr, stdout); | ||
380 | } | ||
381 | |||
382 | return altered; | ||
383 | } | ||
343 | 384 | ||
344 | static int do_sed_command(const struct sed_cmd *sed_cmd, const char *line) | 385 | static int do_sed_command(const struct sed_cmd *sed_cmd, const char *line) |
345 | { | 386 | { |
@@ -355,43 +396,8 @@ static int do_sed_command(const struct sed_cmd *sed_cmd, const char *line) | |||
355 | altered++; | 396 | altered++; |
356 | break; | 397 | break; |
357 | 398 | ||
358 | case 's': /* oo, a fun one :-) */ | 399 | case 's': |
359 | 400 | altered = do_subst_command(sed_cmd, line); | |
360 | /* we only substitute if the substitution 'search' expression matches */ | ||
361 | if (regexec(sed_cmd->sub_match, line, 0, NULL, 0) == 0) { | ||
362 | regmatch_t regmatch; | ||
363 | int i; | ||
364 | char *ptr = (char *)line; | ||
365 | |||
366 | while (*ptr) { | ||
367 | /* if we can match the search string... */ | ||
368 | if (regexec(sed_cmd->sub_match, ptr, 1, ®match, 0) == 0) { | ||
369 | /* print everything before the match, */ | ||
370 | for (i = 0; i < regmatch.rm_so; i++) | ||
371 | fputc(ptr[i], stdout); | ||
372 | /* then print the substitution in its place */ | ||
373 | fputs(sed_cmd->replace, stdout); | ||
374 | /* then advance past the match */ | ||
375 | ptr += regmatch.rm_eo; | ||
376 | /* and let the calling function know that something | ||
377 | * has been changed */ | ||
378 | altered++; | ||
379 | |||
380 | /* if we're not doing this globally... */ | ||
381 | if (!sed_cmd->sub_g) | ||
382 | break; | ||
383 | } | ||
384 | /* if we COULD NOT match the search string (meaning we've gone past | ||
385 | * all previous instances), get out */ | ||
386 | else | ||
387 | break; | ||
388 | } | ||
389 | |||
390 | /* is there anything left to print? */ | ||
391 | if (*ptr) | ||
392 | fputs(ptr, stdout); | ||
393 | } | ||
394 | |||
395 | break; | 401 | break; |
396 | } | 402 | } |
397 | 403 | ||