Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mrbc.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "mruby.h"
5 #include "mruby/compile.h"
6 #include "mruby/dump.h"
7 #include "mruby/proc.h"
8 
9 #define RITEBIN_EXT ".mrb"
10 #define C_EXT ".c"
11 
14 void parser_dump(mrb_state*, struct mrb_ast_node*, int);
15 void codedump_all(mrb_state*, int);
16 
17 struct mrbc_args {
18  int argc;
19  char **argv;
20  int idx;
21  const char *prog;
22  const char *outfile;
23  const char *initname;
27 };
28 
29 static void
30 usage(const char *name)
31 {
32  static const char *const usage_msg[] = {
33  "switches:",
34  "-c check syntax only",
35  "-o<outfile> place the output into <outfile>",
36  "-v print version number, then turn on verbose mode",
37  "-g produce debugging information",
38  "-B<symbol> binary <symbol> output in C language format",
39  "--verbose run at verbose mode",
40  "--version print the version",
41  "--copyright print the copyright",
42  NULL
43  };
44  const char *const *p = usage_msg;
45 
46  printf("Usage: %s [switches] programfile\n", name);
47  while (*p)
48  printf(" %s\n", *p++);
49 }
50 
51 static char *
52 get_outfilename(mrb_state *mrb, char *infile, char *ext)
53 {
54  size_t infilelen;
55  size_t extlen;
56  char *outfile;
57  char *p;
58 
59  infilelen = strlen(infile);
60  extlen = strlen(ext);
61  outfile = (char*)mrb_malloc(mrb, infilelen + extlen + 1);
62  memcpy(outfile, infile, infilelen + 1);
63  if (*ext) {
64  if ((p = strrchr(outfile, '.')) == NULL)
65  p = outfile + infilelen;
66  memcpy(p, ext, extlen + 1);
67  }
68 
69  return outfile;
70 }
71 
72 static int
73 parse_args(mrb_state *mrb, int argc, char **argv, struct mrbc_args *args)
74 {
75  char *outfile = NULL;
76  static const struct mrbc_args args_zero = { 0 };
77  int i;
78 
79  *args = args_zero;
80  args->argc = argc;
81  args->argv = argv;
82  args->prog = argv[0];
83 
84  for (i=1; i<argc; i++) {
85  if (argv[i][0] == '-') {
86  switch ((argv[i])[1]) {
87  case 'o':
88  if (args->outfile) {
89  fprintf(stderr, "%s: an output file is already specified. (%s)\n",
90  args->prog, outfile);
91  return -1;
92  }
93  if (argv[i][2] == '\0' && argv[i+1]) {
94  i++;
95  args->outfile = get_outfilename(mrb, argv[i], "");
96  }
97  else {
98  args->outfile = get_outfilename(mrb, argv[i] + 2, "");
99  }
100  break;
101  case 'B':
102  if (argv[i][2] == '\0' && argv[i+1]) {
103  i++;
104  args->initname = argv[i];
105  }
106  else {
107  args->initname = argv[i]+2;
108  }
109  if (*args->initname == '\0') {
110  fprintf(stderr, "%s: function name is not specified.\n", args->prog);
111  return -1;
112  }
113  break;
114  case 'c':
115  args->check_syntax = 1;
116  break;
117  case 'v':
118  if (!args->verbose) mrb_show_version(mrb);
119  args->verbose = 1;
120  break;
121  case 'g':
122  args->debug_info = 1;
123  break;
124  case 'h':
125  return -1;
126  case '-':
127  if (argv[i][1] == '\n') {
128  return i;
129  }
130  if (strcmp(argv[i] + 2, "version") == 0) {
131  mrb_show_version(mrb);
132  exit(EXIT_SUCCESS);
133  }
134  else if (strcmp(argv[i] + 2, "verbose") == 0) {
135  args->verbose = 1;
136  break;
137  }
138  else if (strcmp(argv[i] + 2, "copyright") == 0) {
139  mrb_show_copyright(mrb);
140  exit(EXIT_SUCCESS);
141  }
142  return -1;
143  default:
144  return i;
145  }
146  }
147  else {
148  break;
149  }
150  }
151  return i;
152 }
153 
154 static void
155 cleanup(mrb_state *mrb, struct mrbc_args *args)
156 {
157  if (args->outfile)
158  mrb_free(mrb, (void*)args->outfile);
159  mrb_close(mrb);
160 }
161 
162 static int
163 partial_hook(struct mrb_parser_state *p)
164 {
165  mrbc_context *c = p->cxt;
166  struct mrbc_args *args = (struct mrbc_args *)c->partial_data;
167  const char *fn;
168 
169  if (p->f) fclose(p->f);
170  if (args->idx >= args->argc) {
171  p->f = NULL;
172  return -1;
173  }
174  fn = args->argv[args->idx++];
175  p->f = fopen(fn, "r");
176  if (p->f == NULL) {
177  fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, fn);
178  return -1;
179  }
181  return 0;
182 }
183 
184 static int
185 load_file(mrb_state *mrb, struct mrbc_args *args)
186 {
187  mrbc_context *c;
188  mrb_value result;
189  char *input = args->argv[args->idx];
190  FILE *infile;
191 
192  c = mrbc_context_new(mrb);
193  if (args->verbose)
194  c->dump_result = 1;
195  c->no_exec = 1;
196  if (input[0] == '-' && input[1] == '\0') {
197  infile = stdin;
198  }
199  else if ((infile = fopen(input, "r")) == NULL) {
200  fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, input);
201  return EXIT_FAILURE;
202  }
203  mrbc_filename(mrb, c, input);
204  args->idx++;
205  if (args->idx < args->argc) {
206  mrbc_partial_hook(mrb, c, partial_hook, (void*)args);
207  }
208 
209  result = mrb_load_file_cxt(mrb, infile, c);
210  if (mrb_undef_p(result) || mrb_fixnum(result) < 0) {
211  mrbc_context_free(mrb, c);
212  return EXIT_FAILURE;
213  }
214  mrbc_context_free(mrb, c);
215  return EXIT_SUCCESS;
216 }
217 
218 static int
219 dump_file(mrb_state *mrb, FILE *wfp, const char *outfile, struct mrbc_args *args)
220 {
221  int n = MRB_DUMP_OK;
222 
223  if (args->initname) {
224  n = mrb_dump_irep_cfunc(mrb, 0, args->debug_info, wfp, args->initname);
225  if (n == MRB_DUMP_INVALID_ARGUMENT) {
226  fprintf(stderr, "%s: invalid C language symbol name\n", args->initname);
227  }
228  }
229  else {
230  n = mrb_dump_irep_binary(mrb, 0, args->debug_info, wfp);
231  }
232  if (n != MRB_DUMP_OK) {
233  fprintf(stderr, "%s: error in mrb dump (%s) %d\n", args->prog, outfile, n);
234  }
235  return n;
236 }
237 
238 int
239 main(int argc, char **argv)
240 {
241  mrb_state *mrb = mrb_open();
242  int n, result;
243  struct mrbc_args args;
244  FILE *wfp;
245 
246  if (mrb == NULL) {
247  fputs("Invalid mrb_state, exiting mrbc\n", stderr);
248  return EXIT_FAILURE;
249  }
250 
251  n = parse_args(mrb, argc, argv, &args);
252  if (n < 0) {
253  cleanup(mrb, &args);
254  usage(argv[0]);
255  return EXIT_FAILURE;
256  }
257  if (n == argc) {
258  fprintf(stderr, "%s: no program file given\n", args.prog);
259  return EXIT_FAILURE;
260  }
261  if (args.outfile == NULL) {
262  if (n + 1 == argc) {
263  args.outfile = get_outfilename(mrb, argv[n], args.initname ? C_EXT : RITEBIN_EXT);
264  }
265  else {
266  fprintf(stderr, "%s: output file should be specified to compile multiple files\n", args.prog);
267  return EXIT_FAILURE;
268  }
269  }
270 
271  args.idx = n;
272  if (load_file(mrb, &args) == EXIT_FAILURE) {
273  cleanup(mrb, &args);
274  return EXIT_FAILURE;
275  }
276  if (args.check_syntax) {
277  printf("%s:%s:Syntax OK", args.prog, argv[n]);
278  }
279 
280  if (args.check_syntax) {
281  cleanup(mrb, &args);
282  return EXIT_SUCCESS;
283  }
284 
285  if (args.outfile) {
286  if (strcmp("-", args.outfile) == 0) {
287  wfp = stdout;
288  }
289  else if ((wfp = fopen(args.outfile, "wb")) == NULL) {
290  fprintf(stderr, "%s: cannot open output file:(%s)\n", args.prog, args.outfile);
291  return EXIT_FAILURE;
292  }
293  }
294  else {
295  fprintf(stderr, "Output file is required\n");
296  return EXIT_FAILURE;
297  }
298  result = dump_file(mrb, wfp, args.outfile, &args);
299  fclose(wfp);
300  cleanup(mrb, &args);
301  if (result != MRB_DUMP_OK) {
302  return EXIT_FAILURE;
303  }
304  return EXIT_SUCCESS;
305 }
306 
307 void
309 {
310 }
311 
312 #ifndef DISABLE_GEMS
313 void
315 {
316 }
317 
318 void
320 {
321 }
322 #endif