summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2014-07-10 08:45:21 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2014-07-10 08:45:21 +0900
commite79478d0b45d84957eebb23c268a7c5a474c1085 (patch)
treec4aa512bb24515fe3160ae0dee5c6d04f7e53e78
parent835da83ca16029f1d97aa926159b3edbee1f6957 (diff)
parent10bd78e85ddf3ba6238e7a37cef4ccad08dee51f (diff)
downloadmruby-e79478d0b45d84957eebb23c268a7c5a474c1085.tar.gz
mruby-e79478d0b45d84957eebb23c268a7c5a474c1085.zip
Merge pull request #2457 from cremno/mruby-strip-rewrite
mruby-bin-strip rewrite
-rw-r--r--mrbgems/mruby-bin-strip/bintest/mruby-strip.rb2
-rw-r--r--mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c118
2 files changed, 72 insertions, 48 deletions
diff --git a/mrbgems/mruby-bin-strip/bintest/mruby-strip.rb b/mrbgems/mruby-bin-strip/bintest/mruby-strip.rb
index 770ea0638..e74a74f10 100644
--- a/mrbgems/mruby-bin-strip/bintest/mruby-strip.rb
+++ b/mrbgems/mruby-bin-strip/bintest/mruby-strip.rb
@@ -9,7 +9,7 @@ end
assert('file not found') do
o = `bin/mruby-strip not_found.mrb 2>&1`
assert_equal 1, $?.exitstatus
- assert_equal "can't open file not_found.mrb\n", o
+ assert_equal "can't open file for reading not_found.mrb\n", o
end
assert('not irep file') do
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 f209fa060..75d6d49fd 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;
};
@@ -36,15 +39,17 @@ print_usage(const char *f)
static int
parse_args(int argc, char **argv, struct strip_args *args)
{
- static const struct strip_args initial_args = {0};
int i;
- *args = initial_args;
+ args->argc_start = 0;
+ args->argc = argc;
+ args->argv = argv;
+ args->lvar = FALSE;
for (i = 1; i < argc; ++i) {
- size_t const len = strlen(argv[i]);
+ const size_t len = strlen(argv[i]);
if (len >= 2 && argv[i][0] == '-') {
- switch(argv[i][1]) {
+ switch (argv[i][1]) {
case 'l':
args->lvar = TRUE;
break;
@@ -56,22 +61,75 @@ parse_args(int argc, char **argv, struct strip_args *args)
default:
return -1;
}
- } else {
+ }
+ else {
break;
}
}
+ args->argc_start = i;
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");
@@ -84,48 +142,14 @@ main(int argc, char **argv)
print_usage(argv[0]);
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;
- }
- }
-
mrb = mrb_open();
-
- 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;
- }
+ if (mrb == NULL) {
+ fputs("Invalid mrb_state, exiting mruby-strip\n", stderr);
+ 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;
}