source: EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c@ 439

Last change on this file since 439 was 439, checked in by coas-nagasima, 4 years ago

mrubyを2.1.1に更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 2.8 KB
Line 
1#include <mruby.h>
2
3#ifdef MRB_DISABLE_STDIO
4# error mruby-bin-strip conflicts 'MRB_DISABLE_STDIO' configuration in your 'build_config.rb'
5#endif
6
7#include <stdlib.h>
8#include <string.h>
9#include <mruby/irep.h>
10#include <mruby/dump.h>
11
12struct strip_args {
13 int argc_start;
14 int argc;
15 char **argv;
16 mrb_bool lvar;
17};
18
19static void
20print_usage(const char *f)
21{
22 printf("Usage: %s [switches] irepfiles\n", f);
23 printf("switches:\n");
24 printf(" -l, --lvar remove LVAR section too.\n");
25}
26
27static int
28parse_args(int argc, char **argv, struct strip_args *args)
29{
30 int i;
31
32 args->argc_start = 0;
33 args->argc = argc;
34 args->argv = argv;
35 args->lvar = FALSE;
36
37 for (i = 1; i < argc; ++i) {
38 const size_t len = strlen(argv[i]);
39 if (len >= 2 && argv[i][0] == '-') {
40 switch (argv[i][1]) {
41 case 'l':
42 args->lvar = TRUE;
43 break;
44 case '-':
45 if (strncmp((*argv) + 2, "lvar", len) == 0) {
46 args->lvar = TRUE;
47 break;
48 }
49 default:
50 return -1;
51 }
52 }
53 else {
54 break;
55 }
56 }
57
58 args->argc_start = i;
59 return i;
60}
61
62static int
63strip(mrb_state *mrb, struct strip_args *args)
64{
65 int i;
66
67 for (i = args->argc_start; i < args->argc; ++i) {
68 char *filename;
69 FILE *rfile;
70 mrb_irep *irep;
71 FILE *wfile;
72 int dump_result;
73
74 filename = args->argv[i];
75 rfile = fopen(filename, "rb");
76 if (rfile == NULL) {
77 fprintf(stderr, "can't open file for reading %s\n", filename);
78 return EXIT_FAILURE;
79 }
80
81 irep = mrb_read_irep_file(mrb, rfile);
82 fclose(rfile);
83 if (irep == NULL) {
84 fprintf(stderr, "can't read irep file %s\n", filename);
85 return EXIT_FAILURE;
86 }
87
88 /* clear lv if --lvar is enabled */
89 if (args->lvar) {
90 mrb_irep_remove_lv(mrb, irep);
91 }
92
93 wfile = fopen(filename, "wb");
94 if (wfile == NULL) {
95 fprintf(stderr, "can't open file for writing %s\n", filename);
96 mrb_irep_decref(mrb, irep);
97 return EXIT_FAILURE;
98 }
99
100 /* debug flag must always be false */
101 dump_result = mrb_dump_irep_binary(mrb, irep, FALSE, wfile);
102
103 fclose(wfile);
104 mrb_irep_decref(mrb, irep);
105
106 if (dump_result != MRB_DUMP_OK) {
107 fprintf(stderr, "error occurred during dumping %s\n", filename);
108 return EXIT_FAILURE;
109 }
110 }
111 return EXIT_SUCCESS;
112}
113
114int
115main(int argc, char **argv)
116{
117 struct strip_args args;
118 int args_result;
119 mrb_state *mrb;
120 int ret;
121
122 if (argc <= 1) {
123 printf("no files to strip\n");
124 print_usage(argv[0]);
125 return EXIT_FAILURE;
126 }
127
128 args_result = parse_args(argc, argv, &args);
129 if (args_result < 0) {
130 print_usage(argv[0]);
131 return EXIT_FAILURE;
132 }
133 mrb = mrb_open_core(mrb_default_allocf, NULL);
134 if (mrb == NULL) {
135 fputs("Invalid mrb_state, exiting mruby-strip\n", stderr);
136 return EXIT_FAILURE;
137 }
138
139 ret = strip(mrb, &args);
140
141 mrb_close(mrb);
142 return ret;
143}
Note: See TracBrowser for help on using the repository browser.