Index: EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
===================================================================
--- EcnlProtoTool/trunk/mruby-1.2.0/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c	(revision 321)
+++ EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c	(revision 331)
@@ -11,4 +11,7 @@
 #include <stdio.h>
 #include <ctype.h>
+
+#include <signal.h>
+#include <setjmp.h>
 
 #ifdef ENABLE_READLINE
@@ -30,9 +33,19 @@
 #endif
 
-#include "mruby.h"
-#include "mruby/array.h"
-#include "mruby/proc.h"
-#include "mruby/compile.h"
-#include "mruby/string.h"
+#ifndef _WIN32
+#define MIRB_SIGSETJMP(env) sigsetjmp(env, 1)
+#define MIRB_SIGLONGJMP(env, val) siglongjmp(env, val)
+#define SIGJMP_BUF sigjmp_buf
+#else
+#define MIRB_SIGSETJMP(env) setjmp(env)
+#define MIRB_SIGLONGJMP(env, val) longjmp(env, val)
+#define SIGJMP_BUF jmp_buf
+#endif
+
+#include <mruby.h>
+#include <mruby/array.h>
+#include <mruby/proc.h>
+#include <mruby/compile.h>
+#include <mruby/string.h>
 
 #ifdef ENABLE_READLINE
@@ -195,4 +208,5 @@
 
 struct _args {
+  FILE *rfp;
   mrb_bool verbose      : 1;
   int argc;
@@ -252,4 +266,19 @@
     }
   }
+
+  if (args->rfp == NULL) {
+    if (*argv != NULL) {
+      args->rfp = fopen(argv[0], "r");
+      if (args->rfp == NULL) {
+        printf("Cannot open program file. (%s)\n", *argv);
+        return EXIT_FAILURE;
+      }
+      argc--; argv++;
+    }
+  }
+  args->argv = (char **)mrb_realloc(mrb, args->argv, sizeof(char*) * (argc + 1));
+  memcpy(args->argv, argv, (argc+1) * sizeof(char*));
+  args->argc = argc;
+
   return EXIT_SUCCESS;
 }
@@ -258,4 +287,7 @@
 cleanup(mrb_state *mrb, struct _args *args)
 {
+  if (args->rfp)
+    fclose(args->rfp);
+  mrb_free(mrb, args->argv);
   mrb_close(mrb);
 }
@@ -308,14 +340,32 @@
 }
 
+
+#ifndef ENABLE_READLINE
+volatile sig_atomic_t input_canceled = 0;
+void
+ctrl_c_handler(int signo)
+{
+  input_canceled = 1;
+}
+#else
+SIGJMP_BUF ctrl_c_buf;
+void
+ctrl_c_handler(int signo)
+{
+  MIRB_SIGLONGJMP(ctrl_c_buf, 1);
+}
+#endif
+
 int
 main(int argc, char **argv)
 {
-  char ruby_code[1024] = { 0 };
+  char ruby_code[4096] = { 0 };
   char last_code_line[1024] = { 0 };
 #ifndef ENABLE_READLINE
   int last_char;
-  int char_index;
+  size_t char_index;
 #else
   char *history_path;
+  char* line;
 #endif
   mrbc_context *cxt;
@@ -324,5 +374,7 @@
   mrb_value result;
   struct _args args;
+  mrb_value ARGV;
   int n;
+  int i;
   mrb_bool code_block_open = FALSE;
   int ai;
@@ -335,5 +387,4 @@
     return EXIT_FAILURE;
   }
-  mrb_define_global_const(mrb, "ARGV", mrb_ary_new_capa(mrb, 0));
 
   n = parse_args(mrb, argc, argv, &args);
@@ -343,4 +394,14 @@
     return n;
   }
+
+  ARGV = mrb_ary_new_capa(mrb, args.argc);
+  for (i = 0; i < args.argc; i++) {
+    char* utf8 = mrb_utf8_from_locale(args.argv[i], -1);
+    if (utf8) {
+      mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, utf8));
+      mrb_utf8_free(utf8);
+    }
+  }
+  mrb_define_global_const(mrb, "ARGV", ARGV);
 
 #ifdef ENABLE_READLINE
@@ -369,15 +430,31 @@
     char *utf8;
 
+    if (args.rfp) {
+      if (fgets(last_code_line, sizeof(last_code_line)-1, args.rfp) != NULL)
+        goto done;
+      break;
+    }
+
 #ifndef ENABLE_READLINE
     print_cmdline(code_block_open);
 
+    signal(SIGINT, ctrl_c_handler);
     char_index = 0;
     while ((last_char = getchar()) != '\n') {
       if (last_char == EOF) break;
-      if (char_index > sizeof(last_code_line)-2) {
+      if (char_index >= sizeof(last_code_line)-2) {
         fputs("input string too long\n", stderr);
         continue;
       }
       last_code_line[char_index++] = last_char;
+    }
+    signal(SIGINT, SIG_DFL);
+    if (input_canceled) {
+      ruby_code[0] = '\0';
+      last_code_line[0] = '\0';
+      code_block_open = FALSE;
+      puts("^C");
+      input_canceled = 0;
+      continue;
     }
     if (last_char == EOF) {
@@ -389,5 +466,17 @@
     last_code_line[char_index] = '\0';
 #else
-    char* line = MIRB_READLINE(code_block_open ? "* " : "> ");
+    if (MIRB_SIGSETJMP(ctrl_c_buf) == 0) {
+      ;
+    }
+    else {
+      ruby_code[0] = '\0';
+      last_code_line[0] = '\0';
+      code_block_open = FALSE;
+      puts("^C");
+    }
+    signal(SIGINT, ctrl_c_handler);
+    line = MIRB_READLINE(code_block_open ? "* " : "> ");
+    signal(SIGINT, SIG_DFL);
+
     if (line == NULL) {
       printf("\n");
@@ -403,4 +492,6 @@
     free(line);
 #endif
+
+done:
 
     if (code_block_open) {
@@ -454,7 +545,7 @@
           mrb_codedump_all(mrb, proc);
         }
-        /* pass a proc for evaulation */
+        /* pass a proc for evaluation */
         /* evaluate the bytecode */
-        result = mrb_context_run(mrb,
+        result = mrb_vm_run(mrb,
             proc,
             mrb_top_self(mrb),
