diff options
| author | cremno <[email protected]> | 2014-07-09 15:32:13 +0200 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-07-12 20:36:44 +0900 |
| commit | 85bb1f92ef74a85488700c556b60e6ac7933e822 (patch) | |
| tree | 8b2cf0783e8b4b24af57de23df6a5d537f53d75f /mrbgems/mruby-bin-strip | |
| parent | c9b7cee2f6b0cf7029237444da819d0783ea78ba (diff) | |
| download | mruby-85bb1f92ef74a85488700c556b60e6ac7933e822.tar.gz mruby-85bb1f92ef74a85488700c556b60e6ac7933e822.zip | |
rewrite stripping
Previous version ignored some errors, and didn't free memory and close files.
Now no memory will be dynamically allocated to simplify error handling.
This commit also fixes a wrong check:
files[i] = fopen(argv[i], "wb");
if (!ireps[i]) {
Improve error messages a bit and add missing newline to one.
Diffstat (limited to 'mrbgems/mruby-bin-strip')
| -rw-r--r-- | mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c | 103 |
1 files changed, 62 insertions, 41 deletions
diff --git a/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c b/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c index b89b3d32d..f4b821d4b 100644 --- a/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c +++ b/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c @@ -6,6 +6,9 @@ #include "mruby/dump.h" struct strip_args { + int argc_start; + int argc; + char **argv; mrb_bool lvar; }; @@ -64,14 +67,65 @@ parse_args(int argc, char **argv, struct strip_args *args) return i; } +static int +strip(mrb_state *mrb, struct strip_args *args) +{ + int i; + + for (i = args->argc_start; i < args->argc; ++i) { + char *filename; + FILE *rfile; + mrb_irep *irep; + FILE *wfile; + int dump_result; + + filename = args->argv[i]; + rfile = fopen(filename, "rb"); + if (rfile == NULL) { + fprintf(stderr, "can't open file for reading %s\n", filename); + return EXIT_FAILURE; + } + + irep = mrb_read_irep_file(mrb, rfile); + fclose(rfile); + if (irep == NULL) { + fprintf(stderr, "can't read irep file %s\n", filename); + return EXIT_FAILURE; + } + + /* clear lv if --lvar is enabled */ + if (args->lvar) { + irep_remove_lv(mrb, irep); + } + + wfile = fopen(filename, "wb"); + if (wfile == NULL) { + fprintf(stderr, "can't open file for writing %s\n", filename); + mrb_irep_decref(mrb, irep); + return EXIT_FAILURE; + } + + /* debug flag must always be false */ + dump_result = mrb_dump_irep_binary(mrb, irep, FALSE, wfile); + + fclose(wfile); + mrb_irep_decref(mrb, irep); + + if (dump_result != MRB_DUMP_OK) { + fprintf(stderr, "error occurred during dumping %s\n", filename); + return EXIT_FAILURE; + } + } + return EXIT_SUCCESS; +} + int main(int argc, char **argv) { struct strip_args args; - int args_result, i, dump_result; - FILE **files; - mrb_irep **ireps; + int args_result; mrb_state *mrb; + int ret; if (argc <= 1) { printf("no files to strip\n"); @@ -85,15 +139,9 @@ main(int argc, char **argv) return EXIT_FAILURE; } - files = (FILE**)malloc(sizeof(FILE*) * argc); - for (i = args_result; i < argc; ++i) { - files[i] = fopen(argv[i], "rb"); - - if (!files[i]) { - fprintf(stderr, "can't open file %s\n", argv[i]); - return EXIT_FAILURE; - } - } + args.argc_start = args_result; + args.argc = argc; + args.argv = argv; mrb = mrb_open(); if (mrb == NULL) { @@ -101,35 +149,8 @@ main(int argc, char **argv) return EXIT_FAILURE; } - ireps = (mrb_irep**)malloc(sizeof(mrb_irep*) * argc); - for (i = args_result; i < argc; ++i) { - ireps[i] = mrb_read_irep_file(mrb, files[i]); - if (!ireps[i]) { - fprintf(stderr, "can't read irep file %s\n", argv[i]); - return EXIT_FAILURE; - } - fclose(files[i]); - files[i] = fopen(argv[i], "wb"); - if (!ireps[i]) { - fprintf(stderr, "can't reopen irep file %s\n", argv[i]); - return EXIT_FAILURE; - } - } - - for (i = args_result; i < argc; ++i) { - /* clear lv if --lvar is enabled */ - if (args.lvar) { - irep_remove_lv(mrb, ireps[i]); - } - - /* debug flag must be alway false */ - dump_result = mrb_dump_irep_binary(mrb, ireps[i], FALSE, files[i]); - if (dump_result != MRB_DUMP_OK) { - fprintf(stderr, "error occur when dumping %s", argv[i]); - return EXIT_FAILURE; - } - } + ret = strip(mrb, &args); mrb_close(mrb); - return EXIT_SUCCESS; + return ret; } |
