5 #include <mruby/irep.h>
6 #include <mruby/dump.h>
16 print_usage(const char *f)
18 printf("Usage: %s [switches] irepfiles\n", f);
19 printf("switches:\n");
20 printf(" -l, --lvar remove LVAR section too.\n");
24 parse_args(int argc, char **argv, struct strip_args *args)
33 for (i = 1; i < argc; ++i) {
34 const size_t len = strlen(argv[i]);
35 if (len >= 2 && argv[i][0] == '-') {
41 if (strncmp((*argv) + 2, "lvar", len) == 0) {
59 strip(mrb_state *mrb, struct strip_args *args)
63 for (i = args->argc_start; i < args->argc; ++i) {
70 filename = args->argv[i];
71 rfile = fopen(filename, "rb");
73 fprintf(stderr, "can't open file for reading %s\n", filename);
77 irep = mrb_read_irep_file(mrb, rfile);
80 fprintf(stderr, "can't read irep file %s\n", filename);
84 /* clear lv if --lvar is enabled */
86 mrb_irep_remove_lv(mrb, irep);
89 wfile = fopen(filename, "wb");
91 fprintf(stderr, "can't open file for writing %s\n", filename);
92 mrb_irep_decref(mrb, irep);
96 /* debug flag must always be false */
97 dump_result = mrb_dump_irep_binary(mrb, irep, FALSE, wfile);
100 mrb_irep_decref(mrb, irep);
102 if (dump_result != MRB_DUMP_OK) {
103 fprintf(stderr, "error occurred during dumping %s\n", filename);
111 main(int argc, char **argv)
113 struct strip_args args;
119 printf("no files to strip\n");
120 print_usage(argv[0]);
124 args_result = parse_args(argc, argv, &args);
125 if (args_result < 0) {
126 print_usage(argv[0]);
129 mrb = mrb_open_core(mrb_default_allocf, NULL);
131 fputs("Invalid mrb_state, exiting mruby-strip\n", stderr);
135 ret = strip(mrb, &args);