Index: EcnlProtoTool/trunk/prototool/.cproject
===================================================================
--- EcnlProtoTool/trunk/prototool/.cproject	(revision 438)
+++ EcnlProtoTool/trunk/prototool/.cproject	(revision 439)
@@ -81,5 +81,5 @@
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/musl-1.1.18/include}&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:}/onigmo-6.1.3/src&quot;"/>
-									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/mruby-1.3.0/include}&quot;"/>
+									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/mruby-2.1.1/include}&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/mbed_api/src}&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/src}&quot;"/>
@@ -88,6 +88,7 @@
 								</option>
 								<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.defs.165293534" name="Defined symbols (-D)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.defs" useByScannerDiscovery="true" valueType="definedSymbols">
-									<listOptionValue builtIn="false" value="MRB_GC_STRESS"/>
+									<listOptionValue builtIn="false" value="MRB_USE_CUSTOM_RO_DATA_P"/>
 									<listOptionValue builtIn="false" value="MRB_ENABLE_DEBUG_HOOK"/>
+									<listOptionValue builtIn="false" value="MRB_CONSTRAINED_BASELINE_PROFILE"/>
 									<listOptionValue builtIn="false" value="_DEFAULT_SOURCE"/>
 								</option>
@@ -149,5 +150,5 @@
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/onigmo-6.1.3/Debug}&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/mbed_api/Debug}&quot;"/>
-									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/mruby-1.3.0/build/arm-none-eabi/lib}&quot;"/>
+									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/mruby-2.1.1/build/arm-none-eabi/lib}&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${TCINSTALL}/lib/gcc/arm-none-eabi/${GCC_VERSION}/thumb/v7-ar/fpv3/hard&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${TCINSTALL}/arm-none-eabi/lib/thumb/v7-ar/fpv3/hard&quot;"/>
@@ -165,5 +166,5 @@
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/onigmo-6.1.3/Debug}/libonigmo.a&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/mbed_api/Debug}/libmbed_api.a&quot;"/>
-									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/mruby-1.3.0}/build/arm-none-eabi/lib/libmruby.a&quot;"/>
+									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/mruby-2.1.1}/build/arm-none-eabi/lib/libmruby.a&quot;"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/lib}/libmrbcode.a&quot;"/>
 								</option>
Index: EcnlProtoTool/trunk/prototool/.project
===================================================================
--- EcnlProtoTool/trunk/prototool/.project	(revision 438)
+++ EcnlProtoTool/trunk/prototool/.project	(revision 439)
@@ -6,5 +6,5 @@
 		<project>curl-7.57.0</project>
 		<project>mbed_api</project>
-		<project>mruby-1.3.0</project>
+		<project>mruby-2.1.1</project>
 		<project>ntshell</project>
 		<project>onigmo-6.1.3</project>
Index: EcnlProtoTool/trunk/prototool/debug_setting.txt
===================================================================
--- EcnlProtoTool/trunk/prototool/debug_setting.txt	(revision 438)
+++ EcnlProtoTool/trunk/prototool/debug_setting.txt	(revision 439)
@@ -1,1 +1,4 @@
+
+-f interface/cmsis-dap.cfg -f target/renesas_rza1h_swd.cfg
+
 add-symbol-file ../ntshell/Debug/ntshell.elf 0x18004000
Index: EcnlProtoTool/trunk/prototool/lib/makefile.mrbc
===================================================================
--- EcnlProtoTool/trunk/prototool/lib/makefile.mrbc	(revision 438)
+++ EcnlProtoTool/trunk/prototool/lib/makefile.mrbc	(revision 439)
@@ -2,7 +2,7 @@
 CC=arm-none-eabi-gcc
 AR=arm-none-eabi-ar
-MRBC=../../mruby-1.3.0/bin/mrbc
+MRBC=../../mruby-2.1.1/bin/mrbc
 
-CFLAGS=-mcpu=cortex-a9 -march=armv7-a -mlittle-endian -marm -mthumb-interwork -mfloat-abi=hard -mfpu=vfpv3 -I../../mruby-1.3.0/include
+CFLAGS=-mcpu=cortex-a9 -march=armv7-a -mlittle-endian -marm -mthumb-interwork -mfloat-abi=hard -mfpu=vfpv3 -I../../mruby-2.1.1/include
 COBJS=echo_server.o echo_client.o
 CSRCS=$(foreach file,$(COBJS),$(file:.o=.c))
Index: EcnlProtoTool/trunk/prototool/src/apibreak.c
===================================================================
--- EcnlProtoTool/trunk/prototool/src/apibreak.c	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/apibreak.c	(revision 439)
@@ -4,15 +4,13 @@
 */
 
-#include <stdlib.h>
 #include <string.h>
-
-#include "mruby.h"
-#include "mruby/irep.h"
+#include <mruby.h>
+#include <mruby/irep.h>
 #include "mrdb.h"
-#include "mruby/debug.h"
-#include "mruby/opcode.h"
-#include "mruby/class.h"
-#include "mruby/proc.h"
-#include "mruby/variable.h"
+#include <mruby/debug.h>
+#include <mruby/opcode.h>
+#include <mruby/class.h>
+#include <mruby/proc.h>
+#include <mruby/variable.h>
 #include "mrdberror.h"
 #include "apibreak.h"
@@ -25,23 +23,23 @@
 check_lineno(mrb_irep_debug_info_file *info_file, uint16_t lineno)
 {
-	uint32_t count = info_file->line_entry_count;
-	uint16_t l_idx;
-
-	if (info_file->line_type == mrb_debug_line_ary) {
-		for (l_idx = 0; l_idx < count; ++l_idx) {
-			if (lineno == info_file->lines.ary[l_idx]) {
-				return lineno;
-			}
-		}
-	}
-	else {
-		for (l_idx = 0; l_idx < count; ++l_idx) {
-			if (lineno == info_file->lines.flat_map[l_idx].line) {
-				return lineno;
-			}
-		}
-	}
-
-	return 0;
+  uint32_t count = info_file->line_entry_count;
+  uint16_t l_idx;
+
+  if (info_file->line_type == mrb_debug_line_ary) {
+    for (l_idx = 0; l_idx < count; ++l_idx) {
+      if (lineno == info_file->lines.ary[l_idx]) {
+        return lineno;
+      }
+    }
+  }
+  else {
+    for (l_idx = 0; l_idx < count; ++l_idx) {
+      if (lineno == info_file->lines.flat_map[l_idx].line) {
+        return lineno;
+      }
+    }
+  }
+
+  return 0;
 }
 
@@ -49,21 +47,21 @@
 get_break_index(mrb_debug_context *dbg, uint32_t bpno)
 {
-	uint32_t i;
-	int32_t index;
-	char hit = FALSE;
-
-	for (i = 0; i < dbg->bpnum; i++) {
-		if (dbg->bp[i].bpno == bpno) {
-			hit = TRUE;
-			index = i;
-			break;
-		}
-	}
-
-	if (hit == FALSE) {
-		return MRB_DEBUG_BREAK_INVALID_NO;
-	}
-
-	return index;
+  uint32_t i;
+  int32_t index;
+  char hit = FALSE;
+
+  for(i = 0 ; i < dbg->bpnum; i++) {
+    if (dbg->bp[i].bpno == bpno) {
+      hit = TRUE;
+      index = i;
+      break;
+    }
+  }
+
+  if (hit == FALSE) {
+    return MRB_DEBUG_BREAK_INVALID_NO;
+  }
+
+  return index;
 }
 
@@ -71,57 +69,48 @@
 free_breakpoint(mrb_state *mrb, mrb_debug_breakpoint *bp)
 {
-	switch (bp->type) {
-	case MRB_DEBUG_BPTYPE_LINE:
-		mrb_free(mrb, (void*)bp->point.linepoint.file);
-		break;
-	case MRB_DEBUG_BPTYPE_METHOD:
-		mrb_free(mrb, (void*)bp->point.methodpoint.method_name);
-		if (bp->point.methodpoint.class_name != NULL) {
-			mrb_free(mrb, (void*)bp->point.methodpoint.class_name);
-		}
-		break;
-	default:
-		break;
-	}
+  switch(bp->type) {
+    case MRB_DEBUG_BPTYPE_LINE:
+      mrb_free(mrb, (void*)bp->point.linepoint.file);
+      break;
+    case MRB_DEBUG_BPTYPE_METHOD:
+      mrb_free(mrb, (void*)bp->point.methodpoint.method_name);
+      if (bp->point.methodpoint.class_name != NULL) {
+        mrb_free(mrb, (void*)bp->point.methodpoint.class_name);
+      }
+      break;
+    default:
+      break;
+  }
 }
 
 static uint16_t
-check_file_lineno(struct mrb_irep *irep, const char *file, uint16_t lineno)
-{
-	mrb_irep_debug_info_file *info_file;
-	uint16_t result = 0;
-	uint16_t f_idx;
-	uint16_t fix_lineno;
-	uint16_t i;
-
-	for (f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) {
-		info_file = irep->debug_info->files[f_idx];
-		if (!strcmp(info_file->filename, file)) {
-			result = MRB_DEBUG_BP_FILE_OK;
-
-			fix_lineno = check_lineno(info_file, lineno);
-			if (fix_lineno != 0) {
-				return result | MRB_DEBUG_BP_LINENO_OK;
-			}
-		}
-		for (i = 0; i < irep->rlen; ++i) {
-			result |= check_file_lineno(irep->reps[i], file, lineno);
-			if (result == (MRB_DEBUG_BP_FILE_OK | MRB_DEBUG_BP_LINENO_OK)) {
-				return result;
-			}
-		}
-	}
-	return result;
-}
-
-static const char*
-get_class_name(mrb_state *mrb, struct RClass *class_obj)
-{
-	struct RClass *outer;
-	mrb_sym class_sym;
-
-	outer = mrb_class_outer_module(mrb, class_obj);
-	class_sym = mrb_class_sym(mrb, class_obj, outer);
-	return mrb_sym2name(mrb, class_sym);
+check_file_lineno(mrb_state *mrb, struct mrb_irep *irep, const char *file, uint16_t lineno)
+{
+  mrb_irep_debug_info_file *info_file;
+  uint16_t result = 0;
+  uint16_t f_idx;
+  uint16_t fix_lineno;
+  uint16_t i;
+
+  for (f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) {
+    const char *filename;
+    info_file = irep->debug_info->files[f_idx];
+    filename = mrb_sym_name_len(mrb, info_file->filename_sym, NULL);
+    if (!strcmp(filename, file)) {
+      result = MRB_DEBUG_BP_FILE_OK;
+
+      fix_lineno = check_lineno(info_file, lineno);
+      if (fix_lineno != 0) {
+        return result | MRB_DEBUG_BP_LINENO_OK;
+      }
+    }
+    for (i=0; i < irep->rlen; ++i) {
+      result  |= check_file_lineno(mrb, irep->reps[i], file, lineno);
+      if (result == (MRB_DEBUG_BP_FILE_OK | MRB_DEBUG_BP_LINENO_OK)) {
+        return result;
+      }
+    }
+  }
+  return result;
 }
 
@@ -129,52 +118,52 @@
 compare_break_method(mrb_state *mrb, mrb_debug_breakpoint *bp, struct RClass *class_obj, mrb_sym method_sym, mrb_bool* isCfunc)
 {
-	const char* class_name;
-	const char* method_name;
-	struct RProc* m;
-	struct RClass* sc;
-	const char* sn;
-	mrb_sym ssym;
-	mrb_debug_methodpoint *method_p;
-	mrb_bool is_defined;
-
-	method_name = mrb_sym2name(mrb, method_sym);
-
-	method_p = &bp->point.methodpoint;
-	if (strcmp(method_p->method_name, method_name) == 0) {
-		class_name = get_class_name(mrb, class_obj);
-		if (class_name == NULL) {
-			if (method_p->class_name == NULL) {
-				return bp->bpno;
-			}
-		}
-		else if (method_p->class_name != NULL) {
-			m = mrb_method_search_vm(mrb, &class_obj, method_sym);
-			if (m == NULL) {
-				return MRB_DEBUG_OK;
-			}
-			if (MRB_PROC_CFUNC_P(m)) {
-				*isCfunc = TRUE;
-			}
-
-			is_defined = mrb_class_defined(mrb, method_p->class_name);
-			if (is_defined == FALSE) {
-				return MRB_DEBUG_OK;
-			}
-
-			sc = mrb_class_get(mrb, method_p->class_name);
-			ssym = mrb_symbol(mrb_check_intern_cstr(mrb, method_p->method_name));
-			m = mrb_method_search_vm(mrb, &sc, ssym);
-			if (m == NULL) {
-				return MRB_DEBUG_OK;
-			}
-
-			class_name = get_class_name(mrb, class_obj);
-			sn = get_class_name(mrb, sc);
-			if (strcmp(sn, class_name) == 0) {
-				return bp->bpno;
-			}
-		}
-	}
-	return MRB_DEBUG_OK;
+  const char* class_name;
+  const char* method_name;
+  mrb_method_t m;
+  struct RClass* sc;
+  const char* sn;
+  mrb_sym ssym;
+  mrb_debug_methodpoint *method_p;
+  mrb_bool is_defined;
+
+  method_name = mrb_sym_name(mrb, method_sym);
+
+  method_p = &bp->point.methodpoint;
+  if (strcmp(method_p->method_name, method_name) == 0) {
+    class_name = mrb_class_name(mrb, class_obj);
+    if (class_name == NULL) {
+      if (method_p->class_name == NULL) {
+        return bp->bpno;
+      }
+    }
+    else if (method_p->class_name != NULL) {
+      m = mrb_method_search_vm(mrb, &class_obj, method_sym);
+      if (MRB_METHOD_UNDEF_P(m)) {
+        return MRB_DEBUG_OK;
+      }
+      if (MRB_METHOD_CFUNC_P(m)) {
+        *isCfunc = TRUE;
+      }
+
+      is_defined = mrb_class_defined(mrb, method_p->class_name);
+      if (is_defined == FALSE) {
+        return MRB_DEBUG_OK;
+      }
+
+      sc = mrb_class_get(mrb, method_p->class_name);
+      ssym = mrb_symbol(mrb_check_intern_cstr(mrb, method_p->method_name));
+      m = mrb_method_search_vm(mrb, &sc, ssym);
+      if (MRB_METHOD_UNDEF_P(m)) {
+        return MRB_DEBUG_OK;
+      }
+
+      class_name = mrb_class_name(mrb, class_obj);
+      sn = mrb_class_name(mrb, sc);
+      if (strcmp(sn, class_name) == 0) {
+        return bp->bpno;
+      }
+    }
+  }
+  return MRB_DEBUG_OK;
 }
 
@@ -182,45 +171,44 @@
 mrb_debug_set_break_line(mrb_state *mrb, mrb_debug_context *dbg, const char *file, uint16_t lineno)
 {
-	int32_t index;
-	char* set_file;
-	uint16_t result;
-
-	if ((mrb == NULL) || (dbg == NULL) || (file == NULL)) {
-		return MRB_DEBUG_INVALID_ARGUMENT;
-	}
-
-	if (dbg->bpnum >= MAX_BREAKPOINT) {
-		return MRB_DEBUG_BREAK_NUM_OVER;
-	}
-
-	if (dbg->next_bpno > MAX_BREAKPOINTNO) {
-		return MRB_DEBUG_BREAK_NO_OVER;
-	}
-
-	/* file and lineno check (line type mrb_debug_line_ary only.) */
-	result = check_file_lineno(dbg->root_irep, file, lineno);
-	if (result == 0) {
-		return MRB_DEBUG_BREAK_INVALID_FILE;
-	}
-	else if (result == MRB_DEBUG_BP_FILE_OK) {
-		return MRB_DEBUG_BREAK_INVALID_LINENO;
-	}
-
-	int len = strlen(file) + 1;
-	set_file = (char *)mrb_malloc(mrb, len);
-
-	index = dbg->bpnum;
-	dbg->bp[index].bpno = dbg->next_bpno;
-	dbg->next_bpno++;
-	dbg->bp[index].enable = TRUE;
-	dbg->bp[index].type = MRB_DEBUG_BPTYPE_LINE;
-	dbg->bp[index].point.linepoint.lineno = lineno;
-	dbg->bpnum++;
-
-	strlcpy(set_file, file, len);
-
-	dbg->bp[index].point.linepoint.file = set_file;
-
-	return dbg->bp[index].bpno;
+  int32_t index;
+  char* set_file;
+  uint16_t result;
+
+  if ((mrb == NULL)||(dbg == NULL)||(file == NULL)) {
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  if (dbg->bpnum >= MAX_BREAKPOINT) {
+    return MRB_DEBUG_BREAK_NUM_OVER;
+  }
+
+  if (dbg->next_bpno > MAX_BREAKPOINTNO) {
+    return MRB_DEBUG_BREAK_NO_OVER;
+  }
+
+  /* file and lineno check (line type mrb_debug_line_ary only.) */
+  result = check_file_lineno(mrb, dbg->root_irep, file, lineno);
+  if (result == 0) {
+    return MRB_DEBUG_BREAK_INVALID_FILE;
+  }
+  else if (result == MRB_DEBUG_BP_FILE_OK) {
+    return MRB_DEBUG_BREAK_INVALID_LINENO;
+  }
+
+  set_file = (char*)mrb_malloc(mrb, strlen(file) + 1);
+
+  index = dbg->bpnum;
+  dbg->bp[index].bpno = dbg->next_bpno;
+  dbg->next_bpno++;
+  dbg->bp[index].enable = TRUE;
+  dbg->bp[index].type = MRB_DEBUG_BPTYPE_LINE;
+  dbg->bp[index].point.linepoint.lineno = lineno;
+  dbg->bpnum++;
+
+  strncpy(set_file, file, strlen(file) + 1);
+
+  dbg->bp[index].point.linepoint.file = set_file;
+
+  return dbg->bp[index].bpno;
 }
 
@@ -228,45 +216,42 @@
 mrb_debug_set_break_method(mrb_state *mrb, mrb_debug_context *dbg, const char *class_name, const char *method_name)
 {
-	int32_t index;
-	char* set_class;
-	char* set_method;
-	int len;
-
-	if ((mrb == NULL) || (dbg == NULL) || (method_name == NULL)) {
-		return MRB_DEBUG_INVALID_ARGUMENT;
-	}
-
-	if (dbg->bpnum >= MAX_BREAKPOINT) {
-		return MRB_DEBUG_BREAK_NUM_OVER;
-	}
-
-	if (dbg->next_bpno > MAX_BREAKPOINTNO) {
-		return MRB_DEBUG_BREAK_NO_OVER;
-	}
-
-	if (class_name != NULL) {
-		len = strlen(class_name) + 1;
-		set_class = (char *)mrb_malloc(mrb, len);
-		strlcpy(set_class, class_name, len);
-	}
-	else {
-		set_class = NULL;
-	}
-
-	len = strlen(method_name) + 1;
-	set_method = (char *)mrb_malloc(mrb, len);
-
-	strlcpy(set_method, method_name, len);
-
-	index = dbg->bpnum;
-	dbg->bp[index].bpno = dbg->next_bpno;
-	dbg->next_bpno++;
-	dbg->bp[index].enable = TRUE;
-	dbg->bp[index].type = MRB_DEBUG_BPTYPE_METHOD;
-	dbg->bp[index].point.methodpoint.method_name = set_method;
-	dbg->bp[index].point.methodpoint.class_name = set_class;
-	dbg->bpnum++;
-
-	return dbg->bp[index].bpno;
+  int32_t index;
+  char* set_class;
+  char* set_method;
+
+  if ((mrb == NULL) || (dbg == NULL) || (method_name == NULL)) {
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  if (dbg->bpnum >= MAX_BREAKPOINT) {
+    return MRB_DEBUG_BREAK_NUM_OVER;
+  }
+
+  if (dbg->next_bpno > MAX_BREAKPOINTNO) {
+    return MRB_DEBUG_BREAK_NO_OVER;
+  }
+
+  if (class_name != NULL) {
+    set_class = (char*)mrb_malloc(mrb, strlen(class_name) + 1);
+    strncpy(set_class, class_name, strlen(class_name) + 1);
+  }
+  else {
+    set_class = NULL;
+  }
+
+  set_method = (char*)mrb_malloc(mrb, strlen(method_name) + 1);
+
+  strncpy(set_method, method_name, strlen(method_name) + 1);
+
+  index = dbg->bpnum;
+  dbg->bp[index].bpno = dbg->next_bpno;
+  dbg->next_bpno++;
+  dbg->bp[index].enable = TRUE;
+  dbg->bp[index].type = MRB_DEBUG_BPTYPE_METHOD;
+  dbg->bp[index].point.methodpoint.method_name = set_method;
+  dbg->bp[index].point.methodpoint.class_name = set_class;
+  dbg->bpnum++;
+
+  return dbg->bp[index].bpno;
 }
 
@@ -274,30 +259,30 @@
 mrb_debug_get_breaknum(mrb_state *mrb, mrb_debug_context *dbg)
 {
-	if ((mrb == NULL) || (dbg == NULL)) {
-		return MRB_DEBUG_INVALID_ARGUMENT;
-	}
-
-	return dbg->bpnum;
-}
-
-int32_t
-mrb_debug_get_break_all(mrb_state *mrb, mrb_debug_context *dbg, uint32_t size, mrb_debug_breakpoint bp[])
-{
-	uint32_t get_size = 0;
-
-	if ((mrb == NULL) || (dbg == NULL) || (bp == NULL)) {
-		return MRB_DEBUG_INVALID_ARGUMENT;
-	}
-
-	if (dbg->bpnum >= size) {
-		get_size = size;
-	}
-	else {
-		get_size = dbg->bpnum;
-	}
-
-	memcpy(bp, dbg->bp, sizeof(mrb_debug_breakpoint) * get_size);
-
-	return get_size;
+  if ((mrb == NULL) || (dbg == NULL)) {
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  return dbg->bpnum;
+}
+
+int32_t
+mrb_debug_get_break_all(mrb_state *mrb, mrb_debug_context *dbg, uint32_t size, mrb_debug_breakpoint *bp)
+{
+  uint32_t get_size = 0;
+
+  if ((mrb == NULL) || (dbg == NULL) || (bp == NULL)) {
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  if (dbg->bpnum >= size) {
+    get_size = size;
+  }
+  else {
+    get_size = dbg->bpnum;
+  }
+
+  memcpy(bp, dbg->bp, sizeof(mrb_debug_breakpoint) * get_size);
+
+  return get_size;
 }
 
@@ -307,19 +292,19 @@
   int32_t index;
 
-	if ((mrb == NULL) || (dbg == NULL) || (bp == NULL)) {
-		return MRB_DEBUG_INVALID_ARGUMENT;
-	}
-
-	index = get_break_index(dbg, bpno);
-	if (index == MRB_DEBUG_BREAK_INVALID_NO) {
-		return MRB_DEBUG_BREAK_INVALID_NO;
-	}
-
-	bp->bpno = dbg->bp[index].bpno;
-	bp->enable = dbg->bp[index].enable;
-	bp->point = dbg->bp[index].point;
-	bp->type = dbg->bp[index].type;
-
-	return 0;
+  if ((mrb == NULL) || (dbg == NULL) || (bp == NULL)) {
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  index = get_break_index(dbg, bpno);
+  if (index == MRB_DEBUG_BREAK_INVALID_NO) {
+    return MRB_DEBUG_BREAK_INVALID_NO;
+  }
+
+  bp->bpno = dbg->bp[index].bpno;
+  bp->enable = dbg->bp[index].enable;
+  bp->point = dbg->bp[index].point;
+  bp->type = dbg->bp[index].type;
+
+  return 0;
 }
 
@@ -327,30 +312,30 @@
 mrb_debug_delete_break(mrb_state *mrb, mrb_debug_context *dbg, uint32_t bpno)
 {
-	uint32_t i;
-	int32_t index;
-
-	if ((mrb == NULL) || (dbg == NULL)) {
-		return MRB_DEBUG_INVALID_ARGUMENT;
-	}
-
-	index = get_break_index(dbg, bpno);
-	if (index == MRB_DEBUG_BREAK_INVALID_NO) {
-		return MRB_DEBUG_BREAK_INVALID_NO;
-	}
-
-	free_breakpoint(mrb, &dbg->bp[index]);
-
-	for (i = index; i < dbg->bpnum; i++) {
-		if ((i + 1) == dbg->bpnum) {
-			memset(&dbg->bp[i], 0, sizeof(mrb_debug_breakpoint));
-		}
-		else {
-			memcpy(&dbg->bp[i], &dbg->bp[i + 1], sizeof(mrb_debug_breakpoint));
-		}
-	}
-
-	dbg->bpnum--;
-
-	return MRB_DEBUG_OK;
+  uint32_t i;
+  int32_t index;
+
+  if ((mrb == NULL) ||(dbg == NULL)) {
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  index = get_break_index(dbg, bpno);
+  if (index == MRB_DEBUG_BREAK_INVALID_NO) {
+    return MRB_DEBUG_BREAK_INVALID_NO;
+  }
+
+  free_breakpoint(mrb, &dbg->bp[index]);
+
+  for(i = index ; i < dbg->bpnum; i++) {
+    if ((i + 1) == dbg->bpnum) {
+      memset(&dbg->bp[i], 0, sizeof(mrb_debug_breakpoint));
+    }
+    else {
+      memcpy(&dbg->bp[i], &dbg->bp[i + 1], sizeof(mrb_debug_breakpoint));
+    }
+  }
+
+  dbg->bpnum--;
+
+  return MRB_DEBUG_OK;
 }
 
@@ -358,17 +343,17 @@
 mrb_debug_delete_break_all(mrb_state *mrb, mrb_debug_context *dbg)
 {
-	uint32_t i;
-
-	if ((mrb == NULL) || (dbg == NULL)) {
-		return MRB_DEBUG_INVALID_ARGUMENT;
-	}
-
-	for (i = 0; i < dbg->bpnum; i++) {
-		free_breakpoint(mrb, &dbg->bp[i]);
-	}
-
-	dbg->bpnum = 0;
-
-	return MRB_DEBUG_OK;
+  uint32_t i;
+
+  if ((mrb == NULL) || (dbg == NULL)) {
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  for(i = 0 ; i < dbg->bpnum ; i++) {
+    free_breakpoint(mrb, &dbg->bp[i]);
+  }
+
+  dbg->bpnum = 0;
+
+  return MRB_DEBUG_OK;
 }
 
@@ -376,18 +361,18 @@
 mrb_debug_enable_break(mrb_state *mrb, mrb_debug_context *dbg, uint32_t bpno)
 {
-	int32_t index = 0;
-
-	if ((mrb == NULL) || (dbg == NULL)) {
-		return MRB_DEBUG_INVALID_ARGUMENT;
-	}
-
-	index = get_break_index(dbg, bpno);
-	if (index == MRB_DEBUG_BREAK_INVALID_NO) {
-		return MRB_DEBUG_BREAK_INVALID_NO;
-	}
-
-	dbg->bp[index].enable = TRUE;
-
-	return MRB_DEBUG_OK;
+  int32_t index = 0;
+
+  if ((mrb == NULL) || (dbg == NULL)) {
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  index = get_break_index(dbg, bpno);
+  if (index == MRB_DEBUG_BREAK_INVALID_NO) {
+    return MRB_DEBUG_BREAK_INVALID_NO;
+  }
+
+  dbg->bp[index].enable = TRUE;
+
+  return MRB_DEBUG_OK;
 }
 
@@ -395,15 +380,15 @@
 mrb_debug_enable_break_all(mrb_state *mrb, mrb_debug_context *dbg)
 {
-	uint32_t i;
-
-	if ((mrb == NULL) || (dbg == NULL)) {
-		return MRB_DEBUG_INVALID_ARGUMENT;
-	}
-
-	for (i = 0; i < dbg->bpnum; i++) {
-		dbg->bp[i].enable = TRUE;
-	}
-
-	return MRB_DEBUG_OK;
+  uint32_t i;
+
+  if ((mrb == NULL) || (dbg == NULL)) {
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  for(i = 0 ; i < dbg->bpnum; i++) {
+    dbg->bp[i].enable = TRUE;
+  }
+
+  return MRB_DEBUG_OK;
 }
 
@@ -411,18 +396,18 @@
 mrb_debug_disable_break(mrb_state *mrb, mrb_debug_context *dbg, uint32_t bpno)
 {
-	int32_t index = 0;
-
-	if ((mrb == NULL) || (dbg == NULL)) {
-		return MRB_DEBUG_INVALID_ARGUMENT;
-	}
-
-	index = get_break_index(dbg, bpno);
-	if (index == MRB_DEBUG_BREAK_INVALID_NO) {
-		return MRB_DEBUG_BREAK_INVALID_NO;
-	}
-
-	dbg->bp[index].enable = FALSE;
-
-	return MRB_DEBUG_OK;
+  int32_t index = 0;
+
+  if ((mrb == NULL) || (dbg == NULL)) {
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  index = get_break_index(dbg, bpno);
+  if (index == MRB_DEBUG_BREAK_INVALID_NO) {
+    return MRB_DEBUG_BREAK_INVALID_NO;
+  }
+
+  dbg->bp[index].enable = FALSE;
+
+  return MRB_DEBUG_OK;
 }
 
@@ -430,26 +415,26 @@
 mrb_debug_disable_break_all(mrb_state *mrb, mrb_debug_context *dbg)
 {
-	uint32_t i;
-
-	if ((mrb == NULL) || (dbg == NULL)) {
-		return MRB_DEBUG_INVALID_ARGUMENT;
-	}
-
-	for (i = 0; i < dbg->bpnum; i++) {
-		dbg->bp[i].enable = FALSE;
-	}
-
-	return MRB_DEBUG_OK;
+  uint32_t i;
+
+  if ((mrb == NULL) || (dbg == NULL)) {
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  for(i = 0 ; i < dbg->bpnum; i++) {
+    dbg->bp[i].enable = FALSE;
+  }
+
+  return MRB_DEBUG_OK;
 }
 
 static mrb_bool
-check_start_pc_for_line(mrb_irep *irep, mrb_code *pc, uint16_t line)
-{
-	if (pc > irep->iseq) {
-		if (line == mrb_debug_get_line(irep, (uint32_t)(pc - irep->iseq - 1))) {
-			return FALSE;
-		}
-	}
-	return TRUE;
+check_start_pc_for_line(mrb_state *mrb, mrb_irep *irep, const mrb_code *pc, uint16_t line)
+{
+  if (pc > irep->iseq) {
+    if (line == mrb_debug_get_line(mrb, irep, pc - irep->iseq - 1)) {
+      return FALSE;
+    }
+  }
+  return TRUE;
 }
 
@@ -457,36 +442,36 @@
 mrb_debug_check_breakpoint_line(mrb_state *mrb, mrb_debug_context *dbg, const char *file, uint16_t line)
 {
-	mrb_debug_breakpoint *bp;
-	mrb_debug_linepoint *line_p;
-  uint32_t i;
-
-	if ((mrb == NULL) || (dbg == NULL) || (file == NULL) || (line <= 0)) {
-		return MRB_DEBUG_INVALID_ARGUMENT;
-	}
-
-	if (!check_start_pc_for_line(dbg->irep, dbg->pc, line)) {
-		return MRB_DEBUG_OK;
-	}
-
-	bp = dbg->bp;
-	for (i = 0; i < dbg->bpnum; i++) {
-		switch (bp->type) {
-		case MRB_DEBUG_BPTYPE_LINE:
-			if (bp->enable == TRUE) {
-				line_p = &bp->point.linepoint;
-				if ((strcmp(line_p->file, file) == 0) && (line_p->lineno == line)) {
-					return bp->bpno;
-				}
-			}
-			break;
-		case MRB_DEBUG_BPTYPE_METHOD:
-			break;
-		case MRB_DEBUG_BPTYPE_NONE:
-		default:
-			return MRB_DEBUG_OK;
-		}
-		bp++;
-	}
-	return MRB_DEBUG_OK;
+  mrb_debug_breakpoint *bp;
+  mrb_debug_linepoint *line_p;
+  uint32_t i;
+
+  if ((mrb == NULL) || (dbg == NULL) || (file == NULL) || (line <= 0)) {
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  if (!check_start_pc_for_line(mrb, dbg->irep, dbg->pc, line)) {
+    return MRB_DEBUG_OK;
+  }
+
+  bp = dbg->bp;
+  for(i=0; i<dbg->bpnum; i++) {
+    switch (bp->type) {
+      case MRB_DEBUG_BPTYPE_LINE:
+        if (bp->enable == TRUE) {
+          line_p = &bp->point.linepoint;
+          if ((strcmp(line_p->file, file) == 0) && (line_p->lineno == line)) {
+            return bp->bpno;
+          }
+        }
+        break;
+      case MRB_DEBUG_BPTYPE_METHOD:
+        break;
+      case MRB_DEBUG_BPTYPE_NONE:
+      default:
+        return MRB_DEBUG_OK;
+    }
+    bp++;
+  }
+  return MRB_DEBUG_OK;
 }
 
@@ -495,30 +480,28 @@
 mrb_debug_check_breakpoint_method(mrb_state *mrb, mrb_debug_context *dbg, struct RClass *class_obj, mrb_sym method_sym, mrb_bool* isCfunc)
 {
-	mrb_debug_breakpoint *bp;
-	int32_t bpno;
-  uint32_t i;
-
-	if ((mrb == NULL) || (dbg == NULL) || (class_obj == NULL)) {
-		return MRB_DEBUG_INVALID_ARGUMENT;
-	}
-
-	bp = dbg->bp;
-	for (i = 0; i < dbg->bpnum; i++) {
-		if (bp->type == MRB_DEBUG_BPTYPE_METHOD) {
-			if (bp->enable == TRUE) {
-				bpno = compare_break_method(mrb, bp, class_obj, method_sym, isCfunc);
-				if (bpno > 0) {
-					return bpno;
-				}
-			}
-		}
-		else if (bp->type == MRB_DEBUG_BPTYPE_NONE) {
-			break;
-		}
-		bp++;
-	}
-
-	return 0;
-}
-
-
+  mrb_debug_breakpoint *bp;
+  int32_t bpno;
+  uint32_t i;
+
+  if ((mrb == NULL) || (dbg == NULL) || (class_obj == NULL)) {
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  bp = dbg->bp;
+  for(i=0; i<dbg->bpnum; i++) {
+    if (bp->type == MRB_DEBUG_BPTYPE_METHOD) {
+      if (bp->enable == TRUE) {
+        bpno = compare_break_method(mrb, bp, class_obj, method_sym, isCfunc);
+        if (bpno > 0) {
+          return bpno;
+        }
+      }
+    }
+    else if (bp->type == MRB_DEBUG_BPTYPE_NONE) {
+      break;
+    }
+    bp++;
+  }
+
+  return 0;
+}
Index: EcnlProtoTool/trunk/prototool/src/apibreak.h
===================================================================
--- EcnlProtoTool/trunk/prototool/src/apibreak.h	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/apibreak.h	(revision 439)
@@ -7,20 +7,20 @@
 #define APIBREAK_H_
 
-#include "mruby.h"
+#include <mruby.h>
 #include "mrdb.h"
 
-int32_t mrb_debug_set_break_line( mrb_state *, mrb_debug_context *, const char *, uint16_t );
-int32_t mrb_debug_set_break_method( mrb_state *, mrb_debug_context *, const char *, const char * );
-int32_t mrb_debug_get_breaknum( mrb_state *, mrb_debug_context * );
-int32_t mrb_debug_get_break_all( mrb_state *, mrb_debug_context *, uint32_t, mrb_debug_breakpoint bp[]);
-int32_t mrb_debug_get_break( mrb_state *, mrb_debug_context *, uint32_t, mrb_debug_breakpoint * );
-int32_t mrb_debug_delete_break( mrb_state *, mrb_debug_context *, uint32_t );
-int32_t mrb_debug_delete_break_all( mrb_state *, mrb_debug_context * );
-int32_t mrb_debug_enable_break( mrb_state *, mrb_debug_context *, uint32_t );
-int32_t mrb_debug_enable_break_all( mrb_state *, mrb_debug_context * );
-int32_t mrb_debug_disable_break( mrb_state *, mrb_debug_context *, uint32_t );
-int32_t mrb_debug_disable_break_all( mrb_state *, mrb_debug_context * );
-int32_t mrb_debug_check_breakpoint_line( mrb_state *, mrb_debug_context *, const char *, uint16_t );
-int32_t mrb_debug_check_breakpoint_method( mrb_state *, mrb_debug_context *, struct RClass *, mrb_sym, mrb_bool* );
+int32_t mrb_debug_set_break_line(mrb_state *, mrb_debug_context *, const char *, uint16_t);
+int32_t mrb_debug_set_break_method(mrb_state *, mrb_debug_context *, const char *, const char *);
+int32_t mrb_debug_get_breaknum(mrb_state *, mrb_debug_context *);
+int32_t mrb_debug_get_break_all(mrb_state *, mrb_debug_context *, uint32_t, mrb_debug_breakpoint bp[]);
+int32_t mrb_debug_get_break(mrb_state *, mrb_debug_context *, uint32_t, mrb_debug_breakpoint *);
+int32_t mrb_debug_delete_break(mrb_state *, mrb_debug_context *, uint32_t);
+int32_t mrb_debug_delete_break_all(mrb_state *, mrb_debug_context *);
+int32_t mrb_debug_enable_break(mrb_state *, mrb_debug_context *, uint32_t);
+int32_t mrb_debug_enable_break_all(mrb_state *, mrb_debug_context *);
+int32_t mrb_debug_disable_break(mrb_state *, mrb_debug_context *, uint32_t);
+int32_t mrb_debug_disable_break_all(mrb_state *, mrb_debug_context *);
+int32_t mrb_debug_check_breakpoint_line(mrb_state *, mrb_debug_context *, const char *, uint16_t);
+int32_t mrb_debug_check_breakpoint_method(mrb_state *, mrb_debug_context *, struct RClass *, mrb_sym, mrb_bool*);
 
 #endif /* APIBREAK_H_ */
Index: EcnlProtoTool/trunk/prototool/src/apilist.c
===================================================================
--- EcnlProtoTool/trunk/prototool/src/apilist.c	(revision 439)
+++ EcnlProtoTool/trunk/prototool/src/apilist.c	(revision 439)
@@ -0,0 +1,240 @@
+/*
+ * apilist.c
+ */
+
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "mrdb.h"
+#include "mrdberror.h"
+#include "apilist.h"
+#include <mruby/compile.h>
+#include <mruby/irep.h>
+#include <mruby/debug.h>
+
+#define LINE_BUF_SIZE MAX_COMMAND_LINE
+
+typedef struct source_file {
+  char *path;
+  uint16_t lineno;
+  FILE *fp;
+} source_file;
+
+static void
+source_file_free(mrb_state *mrb, source_file *file)
+{
+  if (file != NULL) {
+    if (file->path != NULL) {
+      mrb_free(mrb, file->path);
+    }
+    if (file->fp != NULL) {
+      fclose(file->fp);
+      file->fp = NULL;
+    }
+    mrb_free(mrb, file);
+  }
+}
+
+static char*
+build_path(mrb_state *mrb, const char *dir, const char *base)
+{
+  int len;
+  char *path = NULL;
+
+  len = strlen(base) + 1;
+
+  if (strcmp(dir, ".")) {
+    len += strlen(dir) + sizeof("/") - 1;
+  }
+
+  path = (char*)mrb_malloc(mrb, len);
+  memset(path, 0, len);
+
+  if (strcmp(dir, ".")) {
+    strcat(path, dir);
+    strcat(path, "/");
+  }
+  strcat(path, base);
+
+  return path;
+}
+
+static char*
+dirname(mrb_state *mrb, const char *path)
+{
+  size_t len;
+  const char *p;
+  char *dir;
+
+  if (path == NULL) {
+    return NULL;
+  }
+
+  p = strrchr(path, '/');
+  len = p != NULL ? (size_t)(p - path) : strlen(path);
+
+  dir = (char*)mrb_malloc(mrb, len + 1);
+  strncpy(dir, path, len);
+  dir[len] = '\0';
+
+  return dir;
+}
+
+static source_file*
+source_file_new(mrb_state *mrb, mrb_debug_context *dbg, char *filename)
+{
+  source_file *file;
+
+  file = (source_file*)mrb_malloc(mrb, sizeof(source_file));
+
+  memset(file, '\0', sizeof(source_file));
+  file->fp = fopen(filename, "rb");
+
+  if (file->fp == NULL) {
+    source_file_free(mrb, file);
+    return NULL;
+  }
+
+  file->lineno = 1;
+  file->path = (char*)mrb_malloc(mrb, strlen(filename) + 1);
+  strcpy(file->path, filename);
+  return file;
+}
+
+static mrb_bool
+remove_newlines(char *s, FILE *fp)
+{
+  int c;
+  char *p;
+  size_t len;
+
+  if ((len = strlen(s)) == 0) {
+    return FALSE;
+  }
+
+  p = s + len - 1;
+
+  if (*p != '\r' && *p != '\n') {
+    return FALSE;
+  }
+
+  if (*p == '\r') {
+    /* peek the next character and skip '\n' */
+    if ((c = fgetc(fp)) != '\n') {
+      ungetc(c, fp);
+    }
+  }
+
+  /* remove trailing newline characters */
+  while (s <= p && (*p == '\r' || *p == '\n')) {
+    *p-- = '\0';
+  }
+
+  return TRUE;
+}
+
+static void
+show_lines(source_file *file, uint16_t line_min, uint16_t line_max)
+{
+  char buf[LINE_BUF_SIZE];
+  int show_lineno = 1, found_newline = 0, is_printed = 0;
+
+  if (file->fp == NULL) {
+    return;
+  }
+
+  while (fgets(buf, sizeof(buf), file->fp) != NULL) {
+    found_newline = remove_newlines(buf, file->fp);
+
+    if (line_min <= file->lineno) {
+      if (show_lineno) {
+        printf("%-8d", file->lineno);
+      }
+      show_lineno = found_newline;
+      printf(found_newline ? "%s\n" : "%s", buf);
+      is_printed = 1;
+    }
+
+    if (found_newline) {
+      if (line_max < ++file->lineno) {
+        break;
+      }
+    }
+  }
+
+  if (is_printed && !found_newline) {
+    printf("\n");
+  }
+}
+
+char*
+mrb_debug_get_source(mrb_state *mrb, mrdb_state *mrdb, const char *srcpath, const char *filename)
+{
+  int i;
+  FILE *fp;
+  const char *search_path[3];
+  char *path = NULL;
+  const char *srcname = strrchr(filename, '/');
+
+  if (srcname) srcname++;
+  else srcname = filename;
+
+  search_path[0] = srcpath;
+  search_path[1] = dirname(mrb, mrb_debug_get_filename(mrb, mrdb->dbg->irep, 0));
+  search_path[2] = ".";
+
+  for (i = 0; i < 3; i++) {
+    if (search_path[i] == NULL) {
+      continue;
+    }
+
+    if ((path = build_path(mrb, search_path[i], srcname)) == NULL) {
+      continue;
+    }
+
+    if ((fp = fopen(path, "rb")) == NULL) {
+      mrb_free(mrb, path);
+      path = NULL;
+      continue;
+    }
+    fclose(fp);
+    break;
+  }
+
+  mrb_free(mrb, (void *)search_path[1]);
+
+  return path;
+}
+
+int32_t
+mrb_debug_list(mrb_state *mrb, mrb_debug_context *dbg, char *filename, uint16_t line_min, uint16_t line_max)
+{
+  char *ext;
+  source_file *file;
+
+  if (mrb == NULL || dbg == NULL || filename == NULL) {
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  ext = strrchr(filename, '.');
+
+  if (ext == NULL || strcmp(ext, ".rb")) {
+    printf("List command only supports .rb file.\n");
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  if (line_min > line_max) {
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+
+  if ((file = source_file_new(mrb, dbg, filename)) != NULL) {
+    show_lines(file, line_min, line_max);
+    source_file_free(mrb, file);
+    return MRB_DEBUG_OK;
+  }
+  else {
+    printf("Invalid source file named %s.\n", filename);
+    return MRB_DEBUG_INVALID_ARGUMENT;
+  }
+}
Index: EcnlProtoTool/trunk/prototool/src/apilist.h
===================================================================
--- EcnlProtoTool/trunk/prototool/src/apilist.h	(revision 439)
+++ EcnlProtoTool/trunk/prototool/src/apilist.h	(revision 439)
@@ -0,0 +1,14 @@
+/*
+ * apilist.h
+ */
+
+#ifndef APILIST_H_
+#define APILIST_H_
+
+#include <mruby.h>
+#include "mrdb.h"
+
+int32_t mrb_debug_list(mrb_state *, mrb_debug_context *, char *, uint16_t, uint16_t);
+char* mrb_debug_get_source(mrb_state *, mrdb_state *, const char *, const char *);
+
+#endif /* APILIST_H_ */
Index: EcnlProtoTool/trunk/prototool/src/apiprint.c
===================================================================
--- EcnlProtoTool/trunk/prototool/src/apiprint.c	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/apiprint.c	(revision 439)
@@ -4,13 +4,12 @@
 */
 
-#include <stdlib.h>
-
+#include <string.h>
 #include "mrdb.h"
-#include "mruby/value.h"
-#include "mruby/class.h"
-#include "mruby/compile.h"
-#include "mruby/error.h"
-#include "mruby/numeric.h"
-#include "mruby/string.h"
+#include <mruby/value.h>
+#include <mruby/class.h>
+#include <mruby/compile.h>
+#include <mruby/error.h>
+#include <mruby/numeric.h>
+#include <mruby/string.h>
 #include "apiprint.h"
 
@@ -18,62 +17,67 @@
 mrdb_check_syntax(mrb_state *mrb, mrb_debug_context *dbg, const char *expr, size_t len)
 {
-	mrbc_context *c;
+  mrbc_context *c;
 
-	c = mrbc_context_new(mrb);
-	c->no_exec = TRUE;
-	c->capture_errors = TRUE;
-	c->filename = (char*)dbg->prvfile;
-	c->lineno = dbg->prvline;
+  c = mrbc_context_new(mrb);
+  c->no_exec = TRUE;
+  c->capture_errors = TRUE;
+  mrbc_filename(mrb, c, (const char*)dbg->prvfile);
+  c->lineno = dbg->prvline;
 
-	/* Load program */
-	mrb_load_nstring_cxt(mrb, expr, len, c);
+  /* Load program */
+  mrb_load_nstring_cxt(mrb, expr, len, c);
 
-	mrbc_context_free(mrb, c);
+  mrbc_context_free(mrb, c);
 }
 
 mrb_value
-mrb_debug_eval(mrb_state *mrb, mrb_debug_context *dbg, const char *expr, size_t len, mrb_bool *exc)
+mrb_debug_eval(mrb_state *mrb, mrb_debug_context *dbg, const char *expr, size_t len, mrb_bool *exc, int direct_eval)
 {
-	void(*tmp)(struct mrb_state *, struct mrb_irep *, mrb_code *, mrb_value *);
-	mrb_value ruby_code;
-	mrb_value s;
-	mrb_value v;
-	mrb_value recv;
+  void (*tmp)(struct mrb_state *, struct mrb_irep *, const mrb_code *, mrb_value *);
+  mrb_value ruby_code;
+  mrb_value s;
+  mrb_value v;
+  mrb_value recv;
 
-	/* disable code_fetch_hook */
-	tmp = mrb->code_fetch_hook;
-	mrb->code_fetch_hook = NULL;
+  /* disable code_fetch_hook */
+  tmp = mrb->code_fetch_hook;
+  mrb->code_fetch_hook = NULL;
 
-	mrdb_check_syntax(mrb, dbg, expr, len);
-	if (mrb->exc) {
-		v = mrb_obj_value(mrb->exc);
-		mrb->exc = 0;
-	}
-	else {
-		/*
-		 * begin
-		 *   expr
-		 * rescue => e
-		 *   e
-		 * end
-		 */
-		ruby_code = mrb_str_new_lit(mrb, "begin\n");
-		ruby_code = mrb_str_cat(mrb, ruby_code, expr, len);
-		ruby_code = mrb_str_cat_lit(mrb, ruby_code, "\nrescue => e\ne\nend");
+  mrdb_check_syntax(mrb, dbg, expr, len);
+  if (mrb->exc) {
+    v = mrb_obj_value(mrb->exc);
+    mrb->exc = 0;
+  }
+  else if (direct_eval) {
+    recv = dbg->regs[0];
 
-		recv = dbg->regs[0];
+    v = mrb_funcall(mrb, recv, expr, 0);
+  }
+  else {
+    /*
+     * begin
+     *   expr
+     * rescue => e
+     *   e
+     * end
+     */
+    ruby_code = mrb_str_new_lit(mrb, "begin\n");
+    ruby_code = mrb_str_cat(mrb, ruby_code, expr, len);
+    ruby_code = mrb_str_cat_lit(mrb, ruby_code, "\nrescue => e\ne\nend");
 
-		v = mrb_funcall(mrb, recv, "instance_eval", 1, ruby_code);
-	}
+    recv = dbg->regs[0];
 
-	if (exc) {
-		*exc = mrb_obj_is_kind_of(mrb, v, mrb->eException_class);
-	}
+    v =  mrb_funcall(mrb, recv, "instance_eval", 1, ruby_code);
+  }
 
-	s = mrb_funcall(mrb, v, "inspect", 0);
+  if (exc) {
+    *exc = mrb_obj_is_kind_of(mrb, v, mrb->eException_class);
+  }
 
-	/* enable code_fetch_hook */
-	mrb->code_fetch_hook = tmp;
+  s = mrb_inspect(mrb, v);
 
-	return s;
+  /* enable code_fetch_hook */
+  mrb->code_fetch_hook = tmp;
+
+  return s;
 }
Index: EcnlProtoTool/trunk/prototool/src/apiprint.h
===================================================================
--- EcnlProtoTool/trunk/prototool/src/apiprint.h	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/apiprint.h	(revision 439)
@@ -6,8 +6,8 @@
 #define APIPRINT_H_
 
-#include "mruby.h"
+#include <mruby.h>
 #include "mrdb.h"
 
-mrb_value mrb_debug_eval(mrb_state*, mrb_debug_context*, const char*, size_t, mrb_bool*);
+mrb_value mrb_debug_eval(mrb_state*, mrb_debug_context*, const char*, size_t, mrb_bool*, int);
 
 #endif /* APIPRINT_H_ */
Index: EcnlProtoTool/trunk/prototool/src/cmdbreak.c
===================================================================
--- EcnlProtoTool/trunk/prototool/src/cmdbreak.c	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/cmdbreak.c	(revision 439)
@@ -4,11 +4,10 @@
 */
 
-#include <stdlib.h>
+#include <ctype.h>
 #include <string.h>
-
-#include "mruby.h"
-#include "mruby/dump.h"
-#include "mruby/debug.h"
-#include "mruby/string.h"
+#include <mruby.h>
+#include <mruby/dump.h>
+#include <mruby/debug.h>
+#include <mruby/string.h>
 #include "mrdb.h"
 #include "mrdberror.h"
@@ -44,17 +43,17 @@
 #define BPNO_LETTER_NUM 9
 
-typedef int32_t(*all_command_func)(mrb_state *, mrb_debug_context *);
-typedef int32_t(*select_command_func)(mrb_state *, mrb_debug_context *, uint32_t);
+typedef int32_t (*all_command_func)(mrb_state *, mrb_debug_context *);
+typedef int32_t (*select_command_func)(mrb_state *, mrb_debug_context *, uint32_t);
 
 static void
 print_api_common_error(int32_t error)
 {
-	switch (error) {
-	case MRB_DEBUG_INVALID_ARGUMENT:
-		puts(BREAK_ERR_MSG_INVALIDARG);
-		break;
-	default:
-		break;
-	}
+  switch(error) {
+    case MRB_DEBUG_INVALID_ARGUMENT:
+      puts(BREAK_ERR_MSG_INVALIDARG);
+      break;
+    default:
+      break;
+  }
 }
 
@@ -69,20 +68,20 @@
 parse_breakpoint_no(char* args)
 {
-	char* ps = args;
-	uint32_t l;
-
-	if ((*ps == '0') || (strlen(ps) >= BPNO_LETTER_NUM)) {
-		return 0;
-	}
-
-	while (!(ISBLANK(*ps) || ISCNTRL(*ps))) {
-		if (!ISDIGIT(*ps)) {
-			return 0;
-		}
-		ps++;
-	}
-
-	STRTOUL(l, args);
-	return l;
+  char* ps = args;
+  uint32_t l;
+
+  if ((*ps == '0')||(strlen(ps) >= BPNO_LETTER_NUM)) {
+    return 0;
+  }
+
+  while (!(ISBLANK(*ps)||ISCNTRL(*ps))) {
+    if (!ISDIGIT(*ps)) {
+      return 0;
+    }
+    ps++;
+  }
+
+  STRTOUL(l, args);
+  return l;
 }
 
@@ -90,12 +89,12 @@
 exe_set_command_all(mrb_state *mrb, mrdb_state *mrdb, all_command_func func)
 {
-	int32_t ret = MRB_DEBUG_OK;
-
-	if (mrdb->wcnt == 1) {
-		ret = func(mrb, mrdb->dbg);
-		print_api_common_error(ret);
-		return TRUE;
-	}
-	return FALSE;
+  int32_t ret = MRB_DEBUG_OK;
+
+  if (mrdb->wcnt == 1) {
+    ret = func(mrb, mrdb->dbg);
+    print_api_common_error(ret);
+    return TRUE;
+  }
+  return FALSE;
 }
 
@@ -103,24 +102,24 @@
 exe_set_command_select(mrb_state *mrb, mrdb_state *mrdb, select_command_func func)
 {
-	char* ps;
-	int32_t ret = MRB_DEBUG_OK;
-	int32_t bpno = 0;
-	int32_t i;
-
-	for (i = 1; i < mrdb->wcnt; i++) {
-		ps = mrdb->words[i];
-		bpno = parse_breakpoint_no(ps);
-		if (bpno == 0) {
-			printf(BREAK_ERR_MSG_INVALIDBPNO, ps);
-			break;
-		}
-		ret = func(mrb, mrdb->dbg, (uint32_t)bpno);
-		if (ret == MRB_DEBUG_BREAK_INVALID_NO) {
-			printf(BREAK_ERR_MSG_NOBPNO, bpno);
-		}
-		else if (ret != MRB_DEBUG_OK) {
-			print_api_common_error(ret);
-		}
-	}
+  char* ps;
+  int32_t ret = MRB_DEBUG_OK;
+  int32_t bpno = 0;
+  int32_t i;
+
+  for(i=1; i<mrdb->wcnt; i++) {
+    ps = mrdb->words[i];
+    bpno = parse_breakpoint_no(ps);
+    if (bpno == 0) {
+      printf(BREAK_ERR_MSG_INVALIDBPNO, ps);
+      break;
+    }
+    ret = func(mrb, mrdb->dbg, (uint32_t)bpno);
+    if (ret == MRB_DEBUG_BREAK_INVALID_NO) {
+      printf(BREAK_ERR_MSG_NOBPNO, bpno);
+    }
+    else if (ret != MRB_DEBUG_OK) {
+      print_api_common_error(ret);
+    }
+  }
 }
 
@@ -128,29 +127,29 @@
 check_bptype(char* args)
 {
-	char* ps = args;
-
-	if (ISBLANK(*ps) || ISCNTRL(*ps)) {
-		puts(BREAK_ERR_MSG_BLANK);
-		return MRB_DEBUG_BPTYPE_NONE;
-	}
-
-	if (!ISDIGIT(*ps)) {
-		return MRB_DEBUG_BPTYPE_METHOD;
-	}
-
-	while (!(ISBLANK(*ps) || ISCNTRL(*ps))) {
-		if (!ISDIGIT(*ps)) {
-			printf(BREAK_ERR_MSG_INVALIDSTR, args);
-			return MRB_DEBUG_BPTYPE_NONE;
-		}
-		ps++;
-	}
-
-	if ((*args == '0') || (strlen(args) >= LINENO_MAX_DIGIT)) {
-		puts(BREAK_ERR_MSG_RANGEOVER);
-		return MRB_DEBUG_BPTYPE_NONE;
-	}
-
-	return MRB_DEBUG_BPTYPE_LINE;
+  char* ps = args;
+
+  if (ISBLANK(*ps)||ISCNTRL(*ps)) {
+    puts(BREAK_ERR_MSG_BLANK);
+    return MRB_DEBUG_BPTYPE_NONE;
+  }
+
+  if (!ISDIGIT(*ps)) {
+    return MRB_DEBUG_BPTYPE_METHOD;
+  }
+
+  while (!(ISBLANK(*ps)||ISCNTRL(*ps))) {
+    if (!ISDIGIT(*ps)) {
+      printf(BREAK_ERR_MSG_INVALIDSTR, args);
+      return MRB_DEBUG_BPTYPE_NONE;
+    }
+    ps++;
+  }
+
+  if ((*args == '0')||(strlen(args) >= LINENO_MAX_DIGIT)) {
+    puts(BREAK_ERR_MSG_RANGEOVER);
+    return MRB_DEBUG_BPTYPE_NONE;
+  }
+
+  return MRB_DEBUG_BPTYPE_LINE;
 }
 
@@ -158,20 +157,20 @@
 print_breakpoint(mrb_debug_breakpoint *bp)
 {
-	const char* enable_letter[] = { BREAK_INFO_MSG_DISABLE, BREAK_INFO_MSG_ENABLE };
-
-	if (bp->type == MRB_DEBUG_BPTYPE_LINE) {
-		printf(BREAK_INFO_MSG_LINEBREAK,
-			bp->bpno, enable_letter[bp->enable], bp->point.linepoint.file, bp->point.linepoint.lineno);
-	}
-	else {
-		if (bp->point.methodpoint.class_name == NULL) {
-			printf(BREAK_INFO_MSG_METHODBREAK_NOCLASS,
-				bp->bpno, enable_letter[bp->enable], bp->point.methodpoint.method_name);
-		}
-		else {
-			printf(BREAK_INFO_MSG_METHODBREAK,
-				bp->bpno, enable_letter[bp->enable], bp->point.methodpoint.class_name, bp->point.methodpoint.method_name);
-		}
-	}
+  const char* enable_letter[] = {BREAK_INFO_MSG_DISABLE, BREAK_INFO_MSG_ENABLE};
+
+  if (bp->type == MRB_DEBUG_BPTYPE_LINE) {
+    printf(BREAK_INFO_MSG_LINEBREAK,
+      bp->bpno, enable_letter[bp->enable], bp->point.linepoint.file, bp->point.linepoint.lineno);
+  }
+  else {
+    if (bp->point.methodpoint.class_name == NULL) {
+      printf(BREAK_INFO_MSG_METHODBREAK_NOCLASS,
+        bp->bpno, enable_letter[bp->enable], bp->point.methodpoint.method_name);
+    }
+    else {
+      printf(BREAK_INFO_MSG_METHODBREAK,
+        bp->bpno, enable_letter[bp->enable], bp->point.methodpoint.class_name, bp->point.methodpoint.method_name);
+    }
+  }
 }
 
@@ -179,31 +178,31 @@
 info_break_all(mrb_state *mrb, mrdb_state *mrdb)
 {
-	int32_t bpnum = 0;
-	int32_t i = 0;
-	int32_t ret = MRB_DEBUG_OK;
-	mrb_debug_breakpoint *bp_list;
-
-	bpnum = mrb_debug_get_breaknum(mrb, mrdb->dbg);
-	if (bpnum < 0) {
-		print_api_common_error(bpnum);
-		return;
-	}
-	else if (bpnum == 0) {
-		puts(BREAK_ERR_MSG_NOBPNO_INFOALL);
-		return;
-	}
-	bp_list = (mrb_debug_breakpoint*)mrb_malloc(mrb, bpnum * sizeof(mrb_debug_breakpoint));
-
-	ret = mrb_debug_get_break_all(mrb, mrdb->dbg, (uint32_t)bpnum, bp_list);
-	if (ret < 0) {
-		print_api_common_error(ret);
-		return;
-	}
-	puts(BREAK_INFO_MSG_HEADER);
-	for (i = 0; i < bpnum; i++) {
-		print_breakpoint(&bp_list[i]);
-	}
-
-	mrb_free(mrb, bp_list);
+  int32_t bpnum = 0;
+  int32_t i = 0;
+  int32_t ret = MRB_DEBUG_OK;
+  mrb_debug_breakpoint *bp_list;
+
+  bpnum = mrb_debug_get_breaknum(mrb, mrdb->dbg);
+  if (bpnum < 0) {
+    print_api_common_error(bpnum);
+    return;
+  }
+  else if (bpnum == 0) {
+    puts(BREAK_ERR_MSG_NOBPNO_INFOALL);
+    return;
+  }
+  bp_list = (mrb_debug_breakpoint*)mrb_malloc(mrb, bpnum * sizeof(mrb_debug_breakpoint));
+
+  ret = mrb_debug_get_break_all(mrb, mrdb->dbg, (uint32_t)bpnum, bp_list);
+  if (ret < 0) {
+    print_api_common_error(ret);
+    return;
+  }
+  puts(BREAK_INFO_MSG_HEADER);
+  for(i = 0 ; i < bpnum ; i++) {
+    print_breakpoint(&bp_list[i]);
+  }
+
+  mrb_free(mrb, bp_list);
 }
 
@@ -211,114 +210,114 @@
 info_break_select(mrb_state *mrb, mrdb_state *mrdb)
 {
-	int32_t ret = MRB_DEBUG_OK;
-	int32_t bpno = 0;
-	char* ps = mrdb->command;
-	mrb_debug_breakpoint bp;
-	mrb_bool isFirst = TRUE;
-	int32_t i;
-
-	for (i = 2; i < mrdb->wcnt; i++) {
-		ps = mrdb->words[i];
-		bpno = parse_breakpoint_no(ps);
-		if (bpno == 0) {
-			puts(BREAK_ERR_MSG_INVALIDBPNO_INFO);
-			break;
-		}
-
-		ret = mrb_debug_get_break(mrb, mrdb->dbg, bpno, &bp);
-		if (ret == MRB_DEBUG_BREAK_INVALID_NO) {
-			printf(BREAK_ERR_MSG_NOBPNO_INFO, bpno);
-			break;
-		}
-		else if (ret != MRB_DEBUG_OK) {
-			print_api_common_error(ret);
-			break;
-		}
-		else if (isFirst == TRUE) {
-			isFirst = FALSE;
-			puts(BREAK_INFO_MSG_HEADER);
-		}
-		print_breakpoint(&bp);
-	}
+  int32_t ret = MRB_DEBUG_OK;
+  int32_t bpno = 0;
+  char* ps = mrdb->command;
+  mrb_debug_breakpoint bp;
+  mrb_bool isFirst = TRUE;
+  int32_t i;
+
+  for(i=2; i<mrdb->wcnt; i++) {
+    ps = mrdb->words[i];
+    bpno = parse_breakpoint_no(ps);
+    if (bpno == 0) {
+      puts(BREAK_ERR_MSG_INVALIDBPNO_INFO);
+      break;
+    }
+
+    ret = mrb_debug_get_break(mrb, mrdb->dbg, bpno, &bp);
+    if (ret == MRB_DEBUG_BREAK_INVALID_NO) {
+      printf(BREAK_ERR_MSG_NOBPNO_INFO, bpno);
+      break;
+    }
+    else if (ret != MRB_DEBUG_OK) {
+      print_api_common_error(ret);
+      break;
+    }
+    else if (isFirst == TRUE) {
+      isFirst = FALSE;
+      puts(BREAK_INFO_MSG_HEADER);
+    }
+    print_breakpoint(&bp);
+  }
 }
 
 mrb_debug_bptype
-parse_breakcommand(mrdb_state *mrdb, const char **file, uint32_t *line, char **cname, char **method)
-{
-	mrb_debug_context *dbg = mrdb->dbg;
-	char *args;
-	char *body;
-	mrb_debug_bptype type;
-	uint32_t l;
-
-	if (mrdb->wcnt <= 1) {
-		puts(BREAK_ERR_MSG_BLANK);
-		return MRB_DEBUG_BPTYPE_NONE;
-	}
-
-	args = mrdb->words[1];
-	if ((body = strrchr(args, ':')) == NULL) {
-		body = args;
-		type = check_bptype(body);
-	}
-	else {
-		if (body == args) {
-			printf(BREAK_ERR_MSG_INVALIDSTR, args);
-			return MRB_DEBUG_BPTYPE_NONE;
-		}
-		*body = '\0';
-		type = check_bptype(++body);
-	}
-
-	switch (type) {
-	case MRB_DEBUG_BPTYPE_LINE:
-		STRTOUL(l, body);
-		if (l <= 65535) {
-			*line = l;
-			*file = (body == args) ? mrb_debug_get_filename(dbg->irep, (uint32_t)(dbg->pc - dbg->irep->iseq)) : args;
-		}
-		else {
-			puts(BREAK_ERR_MSG_RANGEOVER);
-			type = MRB_DEBUG_BPTYPE_NONE;
-		}
-		break;
-	case MRB_DEBUG_BPTYPE_METHOD:
-		if (body == args) {
-			/* method only */
-			if (ISUPPER(*body) || ISLOWER(*body) || (*body == '_')) {
-				*method = body;
-				*cname = NULL;
-			}
-			else {
-				printf(BREAK_ERR_MSG_INVALIDMETHOD, args);
-				type = MRB_DEBUG_BPTYPE_NONE;
-			}
-		}
-		else {
-			if (ISUPPER(*args)) {
-				switch (*body) {
-				case '@': case '$': case '?': case '.': case ',': case ':':
-				case ';': case '#': case '\\': case '\'': case '\"':
-					printf(BREAK_ERR_MSG_INVALIDMETHOD, body);
-					type = MRB_DEBUG_BPTYPE_NONE;
-					break;
-				default:
-					*method = body;
-					*cname = args;
-					break;
-				}
-			}
-			else {
-				printf(BREAK_ERR_MSG_INVALIDCLASS, args);
-				type = MRB_DEBUG_BPTYPE_NONE;
-			}
-		}
-		break;
-	case MRB_DEBUG_BPTYPE_NONE:
-	default:
-		break;
-	}
-
-	return type;
+parse_breakcommand(mrb_state *mrb, mrdb_state *mrdb, const char **file, uint32_t *line, char **cname, char **method)
+{
+  mrb_debug_context *dbg = mrdb->dbg;
+  char *args;
+  char *body;
+  mrb_debug_bptype type;
+  uint32_t l;
+
+  if (mrdb->wcnt <= 1) {
+    puts(BREAK_ERR_MSG_BLANK);
+    return MRB_DEBUG_BPTYPE_NONE;
+  }
+
+  args = mrdb->words[1];
+  if ((body = strrchr(args, ':')) == NULL) {
+    body = args;
+    type = check_bptype(body);
+  }
+  else {
+    if (body == args) {
+      printf(BREAK_ERR_MSG_INVALIDSTR, args);
+      return MRB_DEBUG_BPTYPE_NONE;
+    }
+    *body = '\0';
+    type = check_bptype(++body);
+  }
+
+  switch(type) {
+    case MRB_DEBUG_BPTYPE_LINE:
+      STRTOUL(l, body);
+      if (l <= 65535) {
+        *line = l;
+        *file = (body == args)? mrb_debug_get_filename(mrb, dbg->irep, dbg->pc - dbg->irep->iseq): args;
+      }
+      else {
+        puts(BREAK_ERR_MSG_RANGEOVER);
+        type = MRB_DEBUG_BPTYPE_NONE;
+      }
+      break;
+    case MRB_DEBUG_BPTYPE_METHOD:
+      if (body == args) {
+        /* method only */
+        if (ISUPPER(*body)||ISLOWER(*body)||(*body == '_')) {
+          *method = body;
+          *cname = NULL;
+        }
+        else {
+          printf(BREAK_ERR_MSG_INVALIDMETHOD, args);
+          type = MRB_DEBUG_BPTYPE_NONE;
+        }
+      }
+      else {
+        if (ISUPPER(*args)) {
+          switch(*body) {
+            case '@': case '$': case '?': case '.': case ',': case ':':
+            case ';': case '#': case '\\': case '\'': case '\"':
+            printf(BREAK_ERR_MSG_INVALIDMETHOD, body);
+            type = MRB_DEBUG_BPTYPE_NONE;
+            break;
+          default:
+            *method = body;
+            *cname = args;
+            break;
+          }
+        }
+        else {
+          printf(BREAK_ERR_MSG_INVALIDCLASS, args);
+          type = MRB_DEBUG_BPTYPE_NONE;
+        }
+      }
+      break;
+    case MRB_DEBUG_BPTYPE_NONE:
+    default:
+      break;
+  }
+
+  return type;
 }
 
@@ -326,62 +325,62 @@
 dbgcmd_break(mrb_state *mrb, mrdb_state *mrdb)
 {
-	mrb_debug_bptype type;
-	mrb_debug_context *dbg = mrdb->dbg;
-	const char *file = NULL;
-	uint32_t line = 0;
-	char *cname = NULL;
-	char *method = NULL;
-	int32_t ret;
-
-	type = parse_breakcommand(mrdb, &file, &line, &cname, &method);
-	switch (type) {
-	case MRB_DEBUG_BPTYPE_LINE:
-		ret = mrb_debug_set_break_line(mrb, dbg, file, line);
-		break;
-	case MRB_DEBUG_BPTYPE_METHOD:
-		ret = mrb_debug_set_break_method(mrb, dbg, cname, method);
-		break;
-	case MRB_DEBUG_BPTYPE_NONE:
-	default:
-		return DBGST_PROMPT;
-	}
-
-	if (ret >= 0) {
-		if (type == MRB_DEBUG_BPTYPE_LINE) {
-			printf(BREAK_SET_MSG_LINE, ret, file, line);
-		}
-		else if ((type == MRB_DEBUG_BPTYPE_METHOD) && (cname == NULL)) {
-			printf(BREAK_SET_MSG_METHOD, ret, method);
-		}
-		else {
-			printf(BREAK_SET_MSG_CLASS_METHOD, ret, cname, method);
-		}
-	}
-	else {
-		switch (ret) {
-		case MRB_DEBUG_BREAK_INVALID_LINENO:
-			printf(BREAK_ERR_MSG_INVALIDLINENO, line, file);
-			break;
-		case MRB_DEBUG_BREAK_INVALID_FILE:
-			printf(BREAK_ERR_MSG_INVALIDFILE, file);
-			break;
-		case MRB_DEBUG_BREAK_NUM_OVER:
-			puts(BREAK_ERR_MSG_NUMOVER);
-			break;
-		case MRB_DEBUG_BREAK_NO_OVER:
-			puts(BREAK_ERR_MSG_NOOVER);
-			break;
-		case MRB_DEBUG_INVALID_ARGUMENT:
-			puts(BREAK_ERR_MSG_INVALIDARG);
-			break;
-		case MRB_DEBUG_NOBUF:
-			puts("T.B.D.");
-			break;
-		default:
-			break;
-		}
-	}
-
-	return DBGST_PROMPT;
+  mrb_debug_bptype type;
+  mrb_debug_context *dbg = mrdb->dbg;
+  const char *file = NULL;
+  uint32_t line = 0;
+  char *cname = NULL;
+  char *method = NULL;
+  int32_t ret;
+
+  type = parse_breakcommand(mrb, mrdb, &file, &line, &cname, &method);
+  switch (type) {
+    case MRB_DEBUG_BPTYPE_LINE:
+      ret = mrb_debug_set_break_line(mrb, dbg, file, line);
+      break;
+    case MRB_DEBUG_BPTYPE_METHOD:
+      ret = mrb_debug_set_break_method(mrb, dbg, cname, method);
+      break;
+    case MRB_DEBUG_BPTYPE_NONE:
+    default:
+      return DBGST_PROMPT;
+  }
+
+  if (ret >= 0) {
+    if (type == MRB_DEBUG_BPTYPE_LINE) {
+      printf(BREAK_SET_MSG_LINE, ret, file, line);
+    }
+    else if ((type == MRB_DEBUG_BPTYPE_METHOD)&&(cname == NULL)) {
+      printf(BREAK_SET_MSG_METHOD, ret, method);
+    }
+    else {
+      printf(BREAK_SET_MSG_CLASS_METHOD, ret, cname, method);
+    }
+  }
+  else {
+    switch (ret) {
+      case MRB_DEBUG_BREAK_INVALID_LINENO:
+        printf(BREAK_ERR_MSG_INVALIDLINENO, line, file);
+        break;
+      case MRB_DEBUG_BREAK_INVALID_FILE:
+        printf(BREAK_ERR_MSG_INVALIDFILE, file);
+        break;
+      case MRB_DEBUG_BREAK_NUM_OVER:
+        puts(BREAK_ERR_MSG_NUMOVER);
+        break;
+      case MRB_DEBUG_BREAK_NO_OVER:
+        puts(BREAK_ERR_MSG_NOOVER);
+        break;
+      case MRB_DEBUG_INVALID_ARGUMENT:
+        puts(BREAK_ERR_MSG_INVALIDARG);
+        break;
+      case MRB_DEBUG_NOBUF:
+        puts("T.B.D.");
+        break;
+      default:
+        break;
+    }
+  }
+
+  return DBGST_PROMPT;
 }
 
@@ -389,12 +388,12 @@
 dbgcmd_info_break(mrb_state *mrb, mrdb_state *mrdb)
 {
-	if (mrdb->wcnt == 2) {
-		info_break_all(mrb, mrdb);
-	}
-	else {
-		info_break_select(mrb, mrdb);
-	}
-
-	return DBGST_PROMPT;
+  if (mrdb->wcnt == 2) {
+    info_break_all(mrb, mrdb);
+  }
+  else {
+    info_break_select(mrb, mrdb);
+  }
+
+  return DBGST_PROMPT;
 }
 
@@ -402,12 +401,12 @@
 dbgcmd_delete(mrb_state *mrb, mrdb_state *mrdb)
 {
-	mrb_bool ret = FALSE;
-
-	ret = exe_set_command_all(mrb, mrdb, mrb_debug_delete_break_all);
-	if (ret != TRUE) {
-		exe_set_command_select(mrb, mrdb, mrb_debug_delete_break);
-	}
-
-	return DBGST_PROMPT;
+  mrb_bool ret = FALSE;
+
+  ret = exe_set_command_all(mrb, mrdb, mrb_debug_delete_break_all);
+  if (ret != TRUE) {
+    exe_set_command_select(mrb, mrdb, mrb_debug_delete_break);
+  }
+
+  return DBGST_PROMPT;
 }
 
@@ -415,12 +414,12 @@
 dbgcmd_enable(mrb_state *mrb, mrdb_state *mrdb)
 {
-	mrb_bool ret = FALSE;
-
-	ret = exe_set_command_all(mrb, mrdb, mrb_debug_enable_break_all);
-	if (ret != TRUE) {
-		exe_set_command_select(mrb, mrdb, mrb_debug_enable_break);
-	}
-
-	return DBGST_PROMPT;
+  mrb_bool ret = FALSE;
+
+  ret = exe_set_command_all(mrb, mrdb, mrb_debug_enable_break_all);
+  if (ret != TRUE) {
+    exe_set_command_select(mrb, mrdb, mrb_debug_enable_break);
+  }
+
+  return DBGST_PROMPT;
 }
 
@@ -428,10 +427,10 @@
 dbgcmd_disable(mrb_state *mrb, mrdb_state *mrdb)
 {
-	mrb_bool ret = FALSE;
-
-	ret = exe_set_command_all(mrb, mrdb, mrb_debug_disable_break_all);
-	if (ret != TRUE) {
-		exe_set_command_select(mrb, mrdb, mrb_debug_disable_break);
-	}
-	return DBGST_PROMPT;
-}
+  mrb_bool ret = FALSE;
+
+  ret = exe_set_command_all(mrb, mrdb, mrb_debug_disable_break_all);
+  if (ret != TRUE) {
+    exe_set_command_select(mrb, mrdb, mrb_debug_disable_break);
+  }
+  return DBGST_PROMPT;
+}
Index: EcnlProtoTool/trunk/prototool/src/cmdmisc.c
===================================================================
--- EcnlProtoTool/trunk/prototool/src/cmdmisc.c	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/cmdmisc.c	(revision 439)
@@ -4,130 +4,135 @@
 */
 
+#include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include "mruby/compile.h"
-#include "mrdb.h"
-
-typedef struct help_msg
-{
-	const char *cmd1;
-	const char *cmd2;
-	const char *short_msg;
-	const char *long_msg;
+#include "apilist.h"
+#include <mruby/compile.h>
+
+typedef struct help_msg {
+  const char *cmd1;
+  const char *cmd2;
+  const char *short_msg;
+  const char *long_msg;
 } help_msg;
 
 static help_msg help_msg_list[] = {
   {
-	"b[reak]", NULL, "Set breakpoint",
-	"Usage: break [file:]line\n"
-	"       break [class:]method\n"
-	"\n"
-	"Set breakpoint at specified line or method.\n"
-	"If \'[file:]line\' is specified, break at start of code for that line (in a file).\n"
-	"If \'[class:]method\' is specified, break at start of code for that method (of the class).\n"
-  },
-  {
-	"c[ontinue]", NULL, "Continue program being debugged",
-	"Usage: continue [N]\n"
-	"\n"
-	"Continue program stopped by a breakpoint.\n"
-	"If N, which is non negative value, is passed,\n"
-	"proceed program until the N-th breakpoint is coming.\n"
-	"If N is not passed, N is assumed 1.\n"
-  },
-  {
-	"d[elete]", NULL, "Delete some breakpoints",
-	"Usage: delete [bpno1 [bpno2 [... [bpnoN]]]]\n"
-	"\n"
-	"Delete some breakpoints.\n"
-	"Arguments are breakpoint numbers with spaces in between.\n"
-	"To delete all breakpoints, give no argument.\n"
-  },
-  {
-	"dis[able]", NULL, "Disable some breakpoints",
-	"Usage: disable [bpno1 [bpno2 [... [bpnoN]]]]\n"
-	"\n"
-	"Disable some breakpoints.\n"
-	"Arguments are breakpoint numbers with spaces in between.\n"
-	"To disable all breakpoints, give no argument.\n"
-  },
-  {
-	"en[able]", NULL, "Enable some breakpoints",
-	"Usage: enable [bpno1 [bpno2 [... [bpnoN]]]]\n"
-	"\n"
-	"Enable some breakpoints.\n"
-	"Arguments are breakpoint numbers with spaces in between.\n"
-	"To enable all breakpoints, give no argument.\n"
-  },
-  {
-	"ev[al]", NULL, "Evaluate expression",
-	"Usage: eval expr\n"
-	"\n"
-	"It evaluates and prints the value of the mruby expression.\n"
-	"This is equivalent to the \'print\' command.\n"
-  },
-  {
-	"h[elp]", NULL, "Print this help",
-	"Usage: help [command]\n"
-	"\n"
-	"With no arguments, help displays a short list of commands.\n"
-	"With a command name as help argument, help displays how to use that command.\n"
-  },
-  {
-	"i[nfo]", "b[reakpoints]", "Status of breakpoints",
-	"Usage: info breakpoints [bpno1 [bpno2 [... [bpnoN]]]]\n"
-	"\n"
-	"Status of specified breakpoints (all user-settable breakpoints if no argument).\n"
-	"Arguments are breakpoint numbers with spaces in between.\n"
-  },
-  {
-	"l[ist]", NULL, "List specified line",
-	"Usage: list\n"
-	"       list first[,last]\n"
-	"       list filename:first[,last]\n"
-	"\n"
-	"Print lines from a source file.\n"
-	"\n"
-	"With first and last, list prints lines from first to last.\n"
-	"When last is empty, it stands for ten lines away from first.\n"
-	"With filename, list prints lines in the specified source file.\n"
-  },
-  {
-	"p[rint]", NULL, "Print value of expression",
-	"Usage: print expr\n"
-	"\n"
-	"It evaluates and prints the value of the mruby expression.\n"
-	"This is equivalent to the \'eval\' command.\n"
-  },
-  {
-	"q[uit]", NULL, "Exit mrdb",
-	"Usage: quit\n"
-	"\n"
-	"Exit mrdb.\n"
-  },
-  {
-	"r[un]", NULL, "Start debugged program",
-	"Usage: run\n"
-	"\n"
-	"Start debugged program.\n"
-  },
-  {
-	"s[tep]", NULL, "Step program until it reaches a different source line",
-	"Usage: step\n"
-	"\n"
-	"Step program until it reaches a different source line.\n"
+    "b[reak]", NULL, "Set breakpoint",
+    "Usage: break [file:]line\n"
+    "       break [class:]method\n"
+    "\n"
+    "Set breakpoint at specified line or method.\n"
+    "If \'[file:]line\' is specified, break at start of code for that line (in a file).\n"
+    "If \'[class:]method\' is specified, break at start of code for that method (of the class).\n"
+  },
+  {
+    "c[ontinue]", NULL, "Continue program being debugged",
+    "Usage: continue [N]\n"
+    "\n"
+    "Continue program stopped by a breakpoint.\n"
+    "If N, which is non negative value, is passed,\n"
+    "proceed program until the N-th breakpoint is coming.\n"
+    "If N is not passed, N is assumed 1.\n"
+  },
+  {
+    "d[elete]", NULL, "Delete some breakpoints",
+    "Usage: delete [bpno1 [bpno2 [... [bpnoN]]]]\n"
+    "\n"
+    "Delete some breakpoints.\n"
+    "Arguments are breakpoint numbers with spaces in between.\n"
+    "To delete all breakpoints, give no argument.\n"
+  },
+  {
+    "dis[able]", NULL, "Disable some breakpoints",
+    "Usage: disable [bpno1 [bpno2 [... [bpnoN]]]]\n"
+    "\n"
+    "Disable some breakpoints.\n"
+    "Arguments are breakpoint numbers with spaces in between.\n"
+    "To disable all breakpoints, give no argument.\n"
+  },
+  {
+    "en[able]", NULL, "Enable some breakpoints",
+    "Usage: enable [bpno1 [bpno2 [... [bpnoN]]]]\n"
+    "\n"
+    "Enable some breakpoints.\n"
+    "Arguments are breakpoint numbers with spaces in between.\n"
+    "To enable all breakpoints, give no argument.\n"
+  },
+  {
+    "ev[al]", NULL, "Evaluate expression",
+    "Usage: eval expr\n"
+    "\n"
+    "It evaluates and prints the value of the mruby expression.\n"
+    "This is equivalent to the \'print\' command.\n"
+  },
+  {
+    "h[elp]", NULL, "Print this help",
+    "Usage: help [command]\n"
+    "\n"
+    "With no arguments, help displays a short list of commands.\n"
+    "With a command name as help argument, help displays how to use that command.\n"
+  },
+  {
+    "i[nfo]", "b[reakpoints]", "Status of breakpoints",
+    "Usage: info breakpoints [bpno1 [bpno2 [... [bpnoN]]]]\n"
+    "\n"
+    "Status of specified breakpoints (all user-settable breakpoints if no argument).\n"
+    "Arguments are breakpoint numbers with spaces in between.\n"
+  },
+  {
+    "i[nfo]", "l[ocals]", "Print name of local variables",
+    "Usage: info locals\n"
+    "\n"
+    "Print name of local variables.\n"
+  },
+  {
+    "l[ist]", NULL, "List specified line",
+    "Usage: list\n"
+    "       list first[,last]\n"
+    "       list filename:first[,last]\n"
+    "\n"
+    "Print lines from a source file.\n"
+    "\n"
+    "With first and last, list prints lines from first to last.\n"
+    "When last is empty, it stands for ten lines away from first.\n"
+    "With filename, list prints lines in the specified source file.\n"
+  },
+  {
+    "p[rint]", NULL, "Print value of expression",
+    "Usage: print expr\n"
+    "\n"
+    "It evaluates and prints the value of the mruby expression.\n"
+    "This is equivalent to the \'eval\' command.\n"
+  },
+  {
+    "q[uit]", NULL, "Exit mrdb",
+    "Usage: quit\n"
+    "\n"
+    "Exit mrdb.\n"
+  },
+  {
+    "r[un]", NULL, "Start debugged program",
+    "Usage: run\n"
+    "\n"
+    "Start debugged program.\n"
+  },
+  {
+    "s[tep]", NULL, "Step program until it reaches a different source line",
+    "Usage: step\n"
+    "\n"
+    "Step program until it reaches a different source line.\n"
   },
   { NULL, NULL, NULL, NULL }
 };
 
-typedef struct listcmd_parser_state
-{
-	mrb_bool parse_error;
-	mrb_bool has_line_min;
-	mrb_bool has_line_max;
-	char *filename;
-	uint16_t line_min;
-	uint16_t line_max;
+typedef struct listcmd_parser_state {
+  mrb_bool parse_error;
+  mrb_bool has_line_min;
+  mrb_bool has_line_max;
+  char *filename;
+  uint16_t line_min;
+  uint16_t line_max;
 } listcmd_parser_state;
 
@@ -135,7 +140,7 @@
 listcmd_parser_state_new(mrb_state *mrb)
 {
-	listcmd_parser_state *st = (listcmd_parser_state *)mrb_malloc(mrb, sizeof(listcmd_parser_state));
-	memset(st, 0, sizeof(listcmd_parser_state));
-	return st;
+  listcmd_parser_state *st = (listcmd_parser_state*)mrb_malloc(mrb, sizeof(listcmd_parser_state));
+  memset(st, 0, sizeof(listcmd_parser_state));
+  return st;
 }
 
@@ -143,10 +148,10 @@
 listcmd_parser_state_free(mrb_state *mrb, listcmd_parser_state *st)
 {
-	if (st != NULL) {
-		if (st->filename != NULL) {
-			mrb_free(mrb, st->filename);
-		}
-		mrb_free(mrb, st);
-	}
+  if (st != NULL) {
+    if (st->filename != NULL) {
+      mrb_free(mrb, st->filename);
+    }
+    mrb_free(mrb, st);
+  }
 }
 
@@ -154,19 +159,19 @@
 parse_uint(char **sp, uint16_t *n)
 {
-	char *p;
-	int i;
-
-	if (*sp == NULL || **sp == '\0') {
-		return FALSE;
-	}
-
-	for (p = *sp; *p != '\0' && ISDIGIT(*p); p++);
-
-	if (p != *sp && (i = atoi(*sp)) >= 0) {
-		*n = (uint16_t)i;
-		*sp = p;
-		return TRUE;
-	}
-	return FALSE;
+  char *p;
+  int i;
+
+  if (*sp == NULL || **sp == '\0') {
+    return FALSE;
+  }
+
+  for (p = *sp; *p != '\0' && ISDIGIT(*p); p++) ;
+
+  if (p != *sp && (i = atoi(*sp)) >= 0) {
+    *n = (uint16_t)i;
+    *sp = p;
+    return TRUE;
+  }
+  return FALSE;
 }
 
@@ -174,9 +179,9 @@
 skip_char(char **sp, char c)
 {
-	if (*sp != NULL && **sp == c) {
-		++*sp;
-		return TRUE;
-	}
-	return FALSE;
+  if (*sp != NULL && **sp == c) {
+    ++*sp;
+    return TRUE;
+  }
+  return FALSE;
 }
 
@@ -184,28 +189,28 @@
 parse_lineno(mrb_state *mrb, char **sp, listcmd_parser_state *st)
 {
-	if (*sp == NULL || **sp == '\0') {
-		return FALSE;
-	}
-
-	st->has_line_min = FALSE;
-	st->has_line_max = FALSE;
-
-	if (parse_uint(sp, &st->line_min)) {
-		st->has_line_min = TRUE;
-	}
-	else {
-		return FALSE;
-	}
-
-	if (skip_char(sp, ',')) {
-		if (parse_uint(sp, &st->line_max)) {
-			st->has_line_max = TRUE;
-		}
-		else {
-			st->parse_error = TRUE;
-			return FALSE;
-		}
-	}
-	return TRUE;
+  if (*sp == NULL || **sp == '\0') {
+    return FALSE;
+  }
+
+  st->has_line_min = FALSE;
+  st->has_line_max = FALSE;
+
+  if (parse_uint(sp, &st->line_min)) {
+    st->has_line_min = TRUE;
+  }
+  else {
+    return FALSE;
+  }
+
+  if (skip_char(sp, ',')) {
+    if (parse_uint(sp, &st->line_max)) {
+      st->has_line_max = TRUE;
+    }
+    else {
+      st->parse_error = TRUE;
+      return FALSE;
+    }
+  }
+  return TRUE;
 }
 
@@ -213,29 +218,29 @@
 parse_filename(mrb_state *mrb, char **sp, listcmd_parser_state *st)
 {
-	char *p;
-	int len;
-
-	if (st->filename != NULL) {
-		mrb_free(mrb, st->filename);
-		st->filename = NULL;
-	}
-
-	if ((p = strchr(*sp, ':')) != NULL) {
-		len = p - *sp;
-	}
-	else {
-		len = strlen(*sp);
-	}
-
-	if (len > 0) {
-		st->filename = mrb_malloc(mrb, len + 1);
-		strlcpy(st->filename, *sp, len + 1);
-		st->filename[len] = '\0';
-		*sp += len;
-		return TRUE;
-	}
-	else {
-		return FALSE;
-	}
+  char *p;
+  int len;
+
+  if (st->filename != NULL) {
+    mrb_free(mrb, st->filename);
+    st->filename = NULL;
+  }
+
+  if ((p = strchr(*sp, ':')) != NULL) {
+    len = p - *sp;
+  }
+  else {
+    len = strlen(*sp);
+  }
+
+  if (len > 0) {
+    st->filename = (char*)mrb_malloc(mrb, len + 1);
+    strncpy(st->filename, *sp, len);
+    st->filename[len] = '\0';
+    *sp += len;
+    return TRUE;
+  }
+  else {
+    return FALSE;
+  }
 }
 
@@ -243,26 +248,25 @@
 replace_ext(mrb_state *mrb, const char *filename, const char *ext)
 {
-	size_t len, len2;
-	const char *p;
-	char *s;
-
-	if (filename == NULL) {
-		return NULL;
-	}
-
-	if ((p = strrchr(filename, '.')) != NULL && strchr(p, '/') == NULL) {
-		len = p - filename;
-	}
-	else {
-		len = strlen(filename);
-	}
-
-	len2 = len + strlen(ext) + 1;
-	s = mrb_malloc(mrb, len2);
-	memset(s, '\0', len2);
-	strlcpy(s, filename, len);
-	strlcat(s, ext, len2);
-
-	return s;
+  size_t len;
+  const char *p;
+  char *s;
+
+  if (filename == NULL) {
+    return NULL;
+  }
+
+  if ((p = strrchr(filename, '.')) != NULL && strchr(p, '/') == NULL) {
+    len = p - filename;
+  }
+  else {
+    len = strlen(filename);
+  }
+
+  s = (char*)mrb_malloc(mrb, len + strlen(ext) + 1);
+  memset(s, '\0', len + strlen(ext) + 1);
+  strncpy(s, filename, len);
+  strcat(s, ext);
+
+  return s;
 }
 
@@ -270,58 +274,58 @@
 parse_listcmd_args(mrb_state *mrb, mrdb_state *mrdb, listcmd_parser_state *st)
 {
-	char *p;
-
-	switch (mrdb->wcnt) {
-	case 2:
-		p = mrdb->words[1];
-
-		/* mrdb->words[1] ::= <lineno> | <filename> ':' <lineno> | <filename> */
-		if (!parse_lineno(mrb, &p, st)) {
-			if (parse_filename(mrb, &p, st)) {
-				if (skip_char(&p, ':')) {
-					if (!parse_lineno(mrb, &p, st)) {
-						st->parse_error = TRUE;
-					}
-				}
-			}
-			else {
-				st->parse_error = TRUE;
-			}
-		}
-		if (*p != '\0') {
-			st->parse_error = TRUE;
-		}
-		break;
-	case 1:
-	case 0:
-		/* do nothing */
-		break;
-	default:
-		st->parse_error = TRUE;
-		printf("too many arguments\n");
-		break;
-	}
-
-	if (!st->parse_error) {
-		if (!st->has_line_min) {
-			st->line_min = (!st->filename && mrdb->dbg->prvline > 0) ? mrdb->dbg->prvline : 1;
-		}
-
-		if (!st->has_line_max) {
-			st->line_max = st->line_min + 9;
-		}
-
-		if (st->filename == NULL) {
-			if (mrdb->dbg->prvfile && strcmp(mrdb->dbg->prvfile, "-")) {
-				st->filename = replace_ext(mrb, mrdb->dbg->prvfile, ".rb");
-			}
-		}
-	}
-
-	if (st->parse_error || st->filename == NULL) {
-		return FALSE;
-	}
-
-	return TRUE;
+  char *p;
+
+  switch (mrdb->wcnt) {
+  case 2:
+    p = mrdb->words[1];
+
+    /* mrdb->words[1] ::= <lineno> | <filename> ':' <lineno> | <filename> */
+    if (!parse_lineno(mrb, &p, st)) {
+      if (parse_filename(mrb, &p, st)) {
+        if (skip_char(&p, ':')) {
+          if (!parse_lineno(mrb, &p, st)) {
+            st->parse_error = TRUE;
+          }
+        }
+      }
+      else {
+        st->parse_error = TRUE;
+      }
+    }
+    if (*p != '\0') {
+      st->parse_error = TRUE;
+    }
+    break;
+  case 1:
+  case 0:
+    /* do nothing */
+    break;
+  default:
+    st->parse_error = TRUE;
+    printf("too many arguments\n");
+    break;
+  }
+
+  if (!st->parse_error) {
+    if (!st->has_line_min) {
+      st->line_min = (!st->filename && mrdb->dbg->prvline > 0) ? mrdb->dbg->prvline : 1;
+    }
+
+    if (!st->has_line_max) {
+      st->line_max = st->line_min + 9;
+    }
+
+    if (st->filename == NULL) {
+      if (mrdb->dbg->prvfile && strcmp(mrdb->dbg->prvfile, "-")) {
+        st->filename = replace_ext(mrb, mrdb->dbg->prvfile, ".rb");
+      }
+    }
+  }
+
+  if (st->parse_error || st->filename == NULL) {
+    return FALSE;
+  }
+
+  return TRUE;
 }
 
@@ -329,31 +333,31 @@
 check_cmd_pattern(const char *pattern, const char *cmd)
 {
-	const char *lbracket, *rbracket, *p, *q;
-
-	if (pattern == NULL && cmd == NULL) {
-		return TRUE;
-	}
-	if (pattern == NULL || cmd == NULL) {
-		return FALSE;
-	}
-	if ((lbracket = strchr(pattern, '[')) == NULL) {
-		return !strcmp(pattern, cmd);
-	}
-	if ((rbracket = strchr(pattern, ']')) == NULL) {
-		return FALSE;
-	}
-	if (strncmp(pattern, cmd, lbracket - pattern)) {
-		return FALSE;
-	}
-
-	p = lbracket + 1;
-	q = (char *)cmd + (lbracket - pattern);
-
-	for (; p < rbracket && *q != '\0'; p++, q++) {
-		if (*p != *q) {
-			break;
-		}
-	}
-	return *q == '\0';
+  const char *lbracket, *rbracket, *p, *q;
+
+  if (pattern == NULL && cmd == NULL) {
+    return TRUE;
+  }
+  if (pattern == NULL || cmd == NULL) {
+    return FALSE;
+  }
+  if ((lbracket = strchr(pattern, '[')) == NULL) {
+    return !strcmp(pattern, cmd);
+  }
+  if ((rbracket = strchr(pattern, ']')) == NULL) {
+    return FALSE;
+  }
+  if (strncmp(pattern, cmd, lbracket - pattern)) {
+    return FALSE;
+  }
+
+  p = lbracket + 1;
+  q = (char *)cmd + (lbracket - pattern);
+
+  for ( ; p < rbracket && *q != '\0'; p++, q++) {
+    if (*p != *q) {
+      break;
+    }
+  }
+  return *q == '\0';
 }
 
@@ -361,15 +365,15 @@
 get_help_msg(char *cmd1, char *cmd2)
 {
-	help_msg *p;
-
-	if (cmd1 == NULL) {
-		return NULL;
-	}
-	for (p = help_msg_list; p->cmd1 != NULL; p++) {
-		if (check_cmd_pattern(p->cmd1, cmd1) && check_cmd_pattern(p->cmd2, cmd2)) {
-			return p;
-		}
-	}
-	return NULL;
+  help_msg *p;
+
+  if (cmd1 == NULL) {
+    return NULL;
+  }
+  for (p = help_msg_list; p->cmd1 != NULL; p++) {
+    if (check_cmd_pattern(p->cmd1, cmd1) && check_cmd_pattern(p->cmd2, cmd2)) {
+      return p;
+    }
+  }
+  return NULL;
 }
 
@@ -377,17 +381,17 @@
 show_short_help(void)
 {
-	help_msg *p;
-
-	printf("Commands\n");
-
-	for (p = help_msg_list; p->cmd1 != NULL; p++) {
-		if (p->cmd2 == NULL) {
-			printf("  %s -- %s\n", p->cmd1, p->short_msg);
-		}
-		else {
-			printf("  %s %s -- %s\n", p->cmd1, p->cmd2, p->short_msg);
-		}
-	}
-	return TRUE;
+  help_msg *p;
+
+  printf("Commands\n");
+
+  for (p = help_msg_list; p->cmd1 != NULL; p++) {
+    if (p->cmd2 == NULL) {
+      printf("  %s -- %s\n", p->cmd1, p->short_msg);
+    }
+    else {
+      printf("  %s %s -- %s\n", p->cmd1, p->cmd2, p->short_msg);
+    }
+  }
+  return TRUE;
 }
 
@@ -395,11 +399,32 @@
 show_long_help(char *cmd1, char *cmd2)
 {
-	help_msg *help;
-
-	if ((help = get_help_msg(cmd1, cmd2)) == NULL) {
-		return FALSE;
-	}
-	printf("%s", help->long_msg);
-	return TRUE;
+  help_msg *help;
+
+  if ((help = get_help_msg(cmd1, cmd2)) == NULL) {
+    return FALSE;
+  }
+  printf("%s", help->long_msg);
+  return TRUE;
+}
+
+dbgcmd_state
+dbgcmd_list(mrb_state *mrb, mrdb_state *mrdb)
+{
+  char *filename;
+  listcmd_parser_state *st = listcmd_parser_state_new(mrb);
+
+  if (parse_listcmd_args(mrb, mrdb, st)) {
+    if ((filename = mrb_debug_get_source(mrb, mrdb, mrdb->srcpath, st->filename)) == NULL) {
+      filename = st->filename;
+    }
+    mrb_debug_list(mrb, mrdb->dbg, filename, st->line_min, st->line_max);
+
+    if (filename != NULL && filename != st->filename) {
+      mrb_free(mrb, filename);
+    }
+    listcmd_parser_state_free(mrb, st);
+  }
+
+  return DBGST_PROMPT;
 }
 
@@ -407,32 +432,32 @@
 dbgcmd_help(mrb_state *mrb, mrdb_state *mrdb)
 {
-	mrb_bool is_valid;
-	int i;
-
-	switch (mrdb->wcnt) {
-	case 0:
-	case 1:
-		is_valid = show_short_help();
-		break;
-	case 2:
-		is_valid = show_long_help(mrdb->words[1], NULL);
-		break;
-	case 3:
-		is_valid = show_long_help(mrdb->words[1], mrdb->words[2]);
-		break;
-	default:
-		is_valid = FALSE;
-		break;
-	}
-
-	if (!is_valid) {
-		printf("Invalid command \"");
-		for (i = 1; i < mrdb->wcnt; i++) {
-			printf("%s%s", i == 1 ? "" : " ", mrdb->words[i]);
-		}
-		printf("\". Try \"help\".\n");
-	}
-
-	return DBGST_PROMPT;
+  mrb_bool is_valid;
+  int i;
+
+  switch (mrdb->wcnt) {
+  case 0:
+  case 1:
+    is_valid = show_short_help();
+    break;
+  case 2:
+    is_valid = show_long_help(mrdb->words[1], NULL);
+    break;
+  case 3:
+    is_valid = show_long_help(mrdb->words[1], mrdb->words[2]);
+    break;
+  default:
+    is_valid = FALSE;
+    break;
+  }
+
+  if (!is_valid) {
+    printf("Invalid command \"");
+    for (i = 1; i < mrdb->wcnt; i++) {
+      printf("%s%s", i == 1 ? "" : " ", mrdb->words[i]);
+    }
+    printf("\". Try \"help\".\n");
+  }
+
+  return DBGST_PROMPT;
 }
 
@@ -440,44 +465,44 @@
 dbgcmd_quit(mrb_state *mrb, mrdb_state *mrdb)
 {
-	switch (mrdb->dbg->xm) {
-	case DBG_RUN:
-	case DBG_STEP:
-	case DBG_NEXT:
-		while (1) {
-			char c;
-			int buf;
-
-			printf("The program is running.  Exit anyway? (y or n) ");
-			fflush(stdout);
-
-			if ((buf = getchar()) == EOF) {
-				InterlockedExchange(&mrdb->dbg->xm, DBG_QUIT);
-				break;
-			}
-			c = buf;
-			while (buf != '\n' && (buf = getchar()) != EOF);
-
-			if (c == 'y' || c == 'Y') {
-				InterlockedExchange(&mrdb->dbg->xm, DBG_QUIT);
-				break;
-			}
-			else if (c == 'n' || c == 'N') {
-				break;
-			}
-			else {
-				printf("Please answer y or n.\n");
-			}
-		}
-		break;
-	default:
-		InterlockedExchange(&mrdb->dbg->xm, DBG_QUIT);
-		break;
-	}
-
-	if (mrdb->dbg->xm == DBG_QUIT) {
-		struct RClass *exc;
-		exc = mrb_define_class(mrb, "DebuggerExit", mrb_class_get(mrb, "Exception"));
-		mrb_raise(mrb, exc, "Exit mrdb.");
-	}
-	return DBGST_PROMPT;
-}
+  switch (mrdb->dbg->xm) {
+  case DBG_RUN:
+  case DBG_STEP:
+  case DBG_NEXT:
+    while (1) {
+      char c;
+      int buf;
+
+      printf("The program is running.  Exit anyway? (y or n) ");
+      fflush(stdout);
+
+      if ((buf = getchar()) == EOF) {
+        mrdb->dbg->xm = DBG_QUIT;
+        break;
+      }
+      c = buf;
+      while (buf != '\n' && (buf = getchar()) != EOF) ;
+
+      if (c == 'y' || c == 'Y') {
+        mrdb->dbg->xm = DBG_QUIT;
+        break;
+      }
+      else if (c == 'n' || c == 'N') {
+        break;
+      }
+      else {
+        printf("Please answer y or n.\n");
+      }
+    }
+    break;
+  default:
+    mrdb->dbg->xm = DBG_QUIT;
+    break;
+  }
+
+  if (mrdb->dbg->xm == DBG_QUIT) {
+    struct RClass *exc;
+    exc = mrb_define_class(mrb, "DebuggerExit", mrb->eException_class);
+    mrb_raise(mrb, exc, "Exit mrdb.");
+  }
+  return DBGST_PROMPT;
+}
Index: EcnlProtoTool/trunk/prototool/src/cmdprint.c
===================================================================
--- EcnlProtoTool/trunk/prototool/src/cmdprint.c	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/cmdprint.c	(revision 439)
@@ -4,13 +4,12 @@
 */
 
-#include <stdlib.h>
-
+#include <string.h>
 #include "mrdb.h"
-#include "mruby/value.h"
-#include "mruby/class.h"
-#include "mruby/compile.h"
-#include "mruby/error.h"
-#include "mruby/numeric.h"
-#include "mruby/string.h"
+#include <mruby/value.h>
+#include <mruby/class.h>
+#include <mruby/compile.h>
+#include <mruby/error.h>
+#include <mruby/numeric.h>
+#include <mruby/string.h>
 #include "apiprint.h"
 
@@ -18,37 +17,37 @@
 dbgcmd_print(mrb_state *mrb, mrdb_state *mrdb)
 {
-	mrb_value expr;
-	mrb_value result;
-	mrb_value s;
-	uint8_t wcnt;
-	int ai;
+  mrb_value expr;
+  mrb_value result;
+  uint8_t wcnt;
+  int ai;
 
-	if (mrdb->wcnt <= 1) {
-		puts("Parameter not specified.");
-		return DBGST_PROMPT;
-	}
+  if (mrdb->wcnt <= 1) {
+    puts("Parameter not specified.");
+    return DBGST_PROMPT;
+  }
 
-	ai = mrb_gc_arena_save(mrb);
+  ai = mrb_gc_arena_save(mrb);
 
-	/* eval expr */
-	expr = mrb_str_new_cstr(mrb, NULL);
-	for (wcnt = 1; wcnt < mrdb->wcnt; wcnt++) {
-		expr = mrb_str_cat_lit(mrb, expr, " ");
-		expr = mrb_str_cat_cstr(mrb, expr, mrdb->words[wcnt]);
-	}
+  /* eval expr */
+  expr = mrb_str_new_cstr(mrb, NULL);
+  for (wcnt=1; wcnt<mrdb->wcnt; wcnt++) {
+    expr = mrb_str_cat_lit(mrb, expr, " ");
+    expr = mrb_str_cat_cstr(mrb, expr, mrdb->words[wcnt]);
+  }
 
-	result = mrb_debug_eval(mrb, mrdb->dbg, RSTRING_PTR(expr), RSTRING_LEN(expr), NULL);
+  result = mrb_debug_eval(mrb, mrdb->dbg, RSTRING_PTR(expr), RSTRING_LEN(expr), NULL, 0);
 
-	/* $print_no = result */
-	s = mrb_str_cat_lit(mrb, result, "\0");
-	printf("$%lu = %s\n", (unsigned long)mrdb->print_no++, RSTRING_PTR(s));
+  /* $print_no = result */
+  printf("$%lu = ", (unsigned long)mrdb->print_no++);
+  fwrite(RSTRING_PTR(result), RSTRING_LEN(result), 1, stdout);
+  putc('\n', stdout);
 
-	if (mrdb->print_no == 0) {
-		mrdb->print_no = 1;
-	}
+  if (mrdb->print_no == 0) {
+    mrdb->print_no = 1;
+  }
 
-	mrb_gc_arena_restore(mrb, ai);
+  mrb_gc_arena_restore(mrb, ai);
 
-	return DBGST_PROMPT;
+  return DBGST_PROMPT;
 }
 
@@ -56,4 +55,27 @@
 dbgcmd_eval(mrb_state *mrb, mrdb_state *mrdb)
 {
-	return dbgcmd_print(mrb, mrdb);
+  return dbgcmd_print(mrb, mrdb);
 }
+
+dbgcmd_state
+dbgcmd_info_local(mrb_state *mrb, mrdb_state *mrdb)
+{
+  mrb_value result;
+  mrb_value s;
+  int ai;
+
+  ai = mrb_gc_arena_save(mrb);
+
+  result = mrb_debug_eval(mrb, mrdb->dbg, "local_variables", 0, NULL, 1);
+
+  s = mrb_str_cat_lit(mrb, result, "\0");
+  printf("$%lu = %s\n", (unsigned long)mrdb->print_no++, RSTRING_PTR(s));
+
+  if (mrdb->print_no == 0) {
+    mrdb->print_no = 1;
+  }
+
+  mrb_gc_arena_restore(mrb, ai);
+
+  return DBGST_PROMPT;
+}
Index: EcnlProtoTool/trunk/prototool/src/cmdrun.c
===================================================================
--- EcnlProtoTool/trunk/prototool/src/cmdrun.c	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/cmdrun.c	(revision 439)
@@ -4,7 +4,5 @@
 */
 
-#include <stdlib.h>
-
-#include "mruby/opcode.h"
+#include <mruby/opcode.h>
 #include "mrdb.h"
 
@@ -12,20 +10,20 @@
 dbgcmd_run(mrb_state *mrb, mrdb_state *mrdb)
 {
-	mrb_debug_context *dbg = mrdb->dbg;
+  mrb_debug_context *dbg = mrdb->dbg;
 
-	if (dbg->xm == DBG_INIT) {
-		InterlockedExchange(&dbg->xm, DBG_RUN);
-	}
-	else {
-		InterlockedExchange(&dbg->xm, DBG_QUIT);
-		if (dbg->xphase == DBG_PHASE_RUNNING) {
-			struct RClass *exc;
-			puts("Start it from the beginning.");
-			exc = mrb_define_class(mrb, "DebuggerRestart", mrb_class_get(mrb, "Exception"));
-			mrb_raise(mrb, exc, "Restart mrdb.");
-		}
-	}
+  if (dbg->xm == DBG_INIT){
+    dbg->xm = DBG_RUN;
+  }
+  else {
+    dbg->xm = DBG_QUIT;
+    if (dbg->xphase == DBG_PHASE_RUNNING){
+      struct RClass *exc;
+      puts("Start it from the beginning.");
+      exc = mrb_define_class(mrb, "DebuggerRestart", mrb->eException_class);
+      mrb_raise(mrb, exc, "Restart mrdb.");
+    }
+  }
 
-	return DBGST_RESTART;
+  return DBGST_RESTART;
 }
 
@@ -33,20 +31,20 @@
 dbgcmd_continue(mrb_state *mrb, mrdb_state *mrdb)
 {
-	mrb_debug_context *dbg = mrdb->dbg;
-	int ccnt = 1;
+  mrb_debug_context *dbg = mrdb->dbg;
+  int ccnt = 1;
 
-	if (mrdb->wcnt > 1) {
-		sscanf(mrdb->words[1], "%d", &ccnt);
-	}
-	dbg->ccnt = (uint16_t)(ccnt > 0 ? ccnt : 1);  /* count of continue */
+  if (mrdb->wcnt > 1){
+    sscanf(mrdb->words[1], "%d", &ccnt);
+  }
+  dbg->ccnt = (uint16_t)(ccnt > 0 ? ccnt : 1);  /* count of continue */
 
-	if (dbg->xphase == DBG_PHASE_AFTER_RUN) {
-		puts("The program is not running.");
-		InterlockedExchange(&dbg->xm, DBG_QUIT);
-	}
-	else {
-		InterlockedExchange(&dbg->xm, DBG_RUN);
-	}
-	return DBGST_CONTINUE;
+  if (dbg->xphase == DBG_PHASE_AFTER_RUN){
+    puts("The program is not running.");
+    dbg->xm = DBG_QUIT;
+  }
+  else {
+    dbg->xm = DBG_RUN;
+  }
+  return DBGST_CONTINUE;
 }
 
@@ -54,6 +52,6 @@
 dbgcmd_step(mrb_state *mrb, mrdb_state *mrdb)
 {
-	InterlockedExchange(&mrdb->dbg->xm, DBG_STEP);
-	return DBGST_CONTINUE;
+  mrdb->dbg->xm = DBG_STEP;
+  return DBGST_CONTINUE;
 }
 
@@ -61,6 +59,6 @@
 dbgcmd_next(mrb_state *mrb, mrdb_state *mrdb)
 {
-	InterlockedExchange(&mrdb->dbg->xm, DBG_NEXT);
-	mrdb->dbg->prvci = mrb->c->ci;
-	return DBGST_CONTINUE;
+  mrdb->dbg->xm = DBG_NEXT;
+  mrdb->dbg->prvci = mrb->c->ci;
+  return DBGST_CONTINUE;
 }
Index: EcnlProtoTool/trunk/prototool/src/linker_scriptDebug.ld
===================================================================
--- EcnlProtoTool/trunk/prototool/src/linker_scriptDebug.ld	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/linker_scriptDebug.ld	(revision 439)
Index: EcnlProtoTool/trunk/prototool/src/main.c
===================================================================
--- EcnlProtoTool/trunk/prototool/src/main.c	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/main.c	(revision 439)
@@ -1,6 +1,6 @@
 /*
- *  TOPPERS ECHONET Lite Communication Middleware
+ *  TOPPERS PROJECT Home Network Working Group Software
  *
- *  Copyright (C) 2014-2017 Cores Co., Ltd. Japan
+ *  Copyright (C) 2017-2019 Cores Co., Ltd. Japan
  *
  *  上記著作権者は，以下の(1)～(4)の条件を満たす場合に限り，本ソフトウェ
@@ -515,4 +515,11 @@
 }
 
+extern char __start_rodata, __end_rodata;
+
+mrb_bool mrb_ro_data_p(const char *p)
+{
+	return (&__start_rodata <= p && p < &__end_rodata);
+}
+
 void _start_c(long *p);
 extern int __start_idata, __start_data, __end_data;
Index: EcnlProtoTool/trunk/prototool/src/mirb.c
===================================================================
--- EcnlProtoTool/trunk/prototool/src/mirb.c	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/mirb.c	(revision 439)
@@ -7,8 +7,24 @@
 */
 
+#include <mruby.h>
+
+#ifdef MRB_DISABLE_STDIO
+# error mruby-bin-mirb conflicts 'MRB_DISABLE_STDIO' configuration in your 'build_config.rb'
+#endif
+
+#include <mruby/array.h>
+#include <mruby/proc.h>
+#include <mruby/compile.h>
+#include <mruby/dump.h>
+#include <mruby/string.h>
+#include <mruby/variable.h>
+#include <mruby/throw.h>
+
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
 #include <ctype.h>
+
+#include <signal.h>
+#include <setjmp.h>
 
 #ifdef ENABLE_READLINE
@@ -17,4 +33,10 @@
 #define MIRB_ADD_HISTORY(line) add_history(line)
 #define MIRB_READLINE(ch) readline(ch)
+#if !defined(RL_READLINE_VERSION) || RL_READLINE_VERSION < 0x600
+/* libedit & older readline do not have rl_free() */
+#define MIRB_LINE_FREE(line) free(line)
+#else
+#define MIRB_LINE_FREE(line) rl_free(line)
+#endif
 #define MIRB_WRITE_HISTORY(path) write_history(path)
 #define MIRB_READ_HISTORY(path) read_history(path)
@@ -25,4 +47,5 @@
 #define MIRB_ADD_HISTORY(line) linenoiseHistoryAdd(line)
 #define MIRB_READLINE(ch) linenoise(ch)
+#define MIRB_LINE_FREE(line) linenoiseFree(line)
 #define MIRB_WRITE_HISTORY(path) linenoiseHistorySave(path)
 #define MIRB_READ_HISTORY(path) linenoiseHistoryLoad(history_path)
@@ -30,9 +53,13 @@
 #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
 
 #ifdef ENABLE_READLINE
@@ -43,29 +70,29 @@
 get_history_path(mrb_state *mrb)
 {
-	char *path = NULL;
-	const char *home = getenv("HOME");
+  char *path = NULL;
+  const char *home = getenv("HOME");
 
 #ifdef _WIN32
-	if (home != NULL) {
-		home = getenv("USERPROFILE");
-	}
-#endif
-
-	if (home != NULL) {
-		int len = snprintf(NULL, 0, "%s/%s", home, history_file_name);
-		if (len >= 0) {
-			size_t size = len + 1;
-			path = (char *)mrb_malloc_simple(mrb, size);
-			if (path != NULL) {
-				int n = snprintf(path, size, "%s/%s", home, history_file_name);
-				if (n != len) {
-					mrb_free(mrb, path);
-					path = NULL;
-				}
-			}
-		}
-	}
-
-	return path;
+  if (home != NULL) {
+    home = getenv("USERPROFILE");
+  }
+#endif
+
+  if (home != NULL) {
+    int len = snprintf(NULL, 0, "%s/%s", home, history_file_name);
+    if (len >= 0) {
+      size_t size = len + 1;
+      path = (char *)mrb_malloc_simple(mrb, size);
+      if (path != NULL) {
+        int n = snprintf(path, size, "%s/%s", home, history_file_name);
+        if (n != len) {
+          mrb_free(mrb, path);
+          path = NULL;
+        }
+      }
+    }
+  }
+
+  return path;
 }
 
@@ -75,20 +102,23 @@
 p(mrb_state *mrb, mrb_value obj, int prompt)
 {
-	mrb_value val;
-
-	val = mrb_funcall(mrb, obj, "inspect", 0);
-	if (prompt) {
-		if (!mrb->exc) {
-			fputs(" => ", stdout);
-		}
-		else {
-			val = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0);
-		}
-	}
-	if (!mrb_string_p(val)) {
-		val = mrb_obj_as_string(mrb, obj);
-	}
-	fwrite(RSTRING_PTR(val), RSTRING_LEN(val), 1, stdout);
-	putc('\n', stdout);
+  mrb_value val;
+  char* msg;
+
+  val = mrb_funcall(mrb, obj, "inspect", 0);
+  if (prompt) {
+    if (!mrb->exc) {
+      fputs(" => ", stdout);
+    }
+    else {
+      val = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0);
+    }
+  }
+  if (!mrb_string_p(val)) {
+    val = mrb_obj_as_string(mrb, obj);
+  }
+  msg = mrb_locale_from_utf8(RSTRING_PTR(val), (int)RSTRING_LEN(val));
+  fwrite(msg, strlen(msg), 1, stdout);
+  mrb_locale_free(msg);
+  putc('\n', stdout);
 }
 
@@ -98,104 +128,104 @@
 is_code_block_open(struct mrb_parser_state *parser)
 {
-	mrb_bool code_block_open = FALSE;
-
-	/* check for heredoc */
-	if (parser->parsing_heredoc != NULL) return TRUE;
-	if (parser->heredoc_end_now) {
-		parser->heredoc_end_now = FALSE;
-		return FALSE;
-	}
-
-	/* check for unterminated string */
-	if (parser->lex_strterm) return TRUE;
-
-	/* check if parser error are available */
-	if (0 < parser->nerr) {
-		const char unexpected_end[] = "syntax error, unexpected $end";
-		const char *message = parser->error_buffer[0].message;
-
-		/* a parser error occur, we have to check if */
-		/* we need to read one more line or if there is */
-		/* a different issue which we have to show to */
-		/* the user */
-
-		if (strncmp(message, unexpected_end, sizeof(unexpected_end) - 1) == 0) {
-			code_block_open = TRUE;
-		}
-		else if (strcmp(message, "syntax error, unexpected keyword_end") == 0) {
-			code_block_open = FALSE;
-		}
-		else if (strcmp(message, "syntax error, unexpected tREGEXP_BEG") == 0) {
-			code_block_open = FALSE;
-		}
-		return code_block_open;
-	}
-
-	switch (parser->lstate) {
-
-	/* all states which need more code */
-
-	case EXPR_BEG:
-	  /* beginning of a statement, */
-	  /* that means previous line ended */
-		code_block_open = FALSE;
-		break;
-	case EXPR_DOT:
-	  /* a message dot was the last token, */
-	  /* there has to come more */
-		code_block_open = TRUE;
-		break;
-	case EXPR_CLASS:
-	  /* a class keyword is not enough! */
-	  /* we need also a name of the class */
-		code_block_open = TRUE;
-		break;
-	case EXPR_FNAME:
-	  /* a method name is necessary */
-		code_block_open = TRUE;
-		break;
-	case EXPR_VALUE:
-	  /* if, elsif, etc. without condition */
-		code_block_open = TRUE;
-		break;
-
-	  /* now all the states which are closed */
-
-	case EXPR_ARG:
-	  /* an argument is the last token */
-		code_block_open = FALSE;
-		break;
-
-	  /* all states which are unsure */
-
-	case EXPR_CMDARG:
-		break;
-	case EXPR_END:
-	  /* an expression was ended */
-		break;
-	case EXPR_ENDARG:
-	  /* closing parenthese */
-		break;
-	case EXPR_ENDFN:
-	  /* definition end */
-		break;
-	case EXPR_MID:
-	  /* jump keyword like break, return, ... */
-		break;
-	case EXPR_MAX_STATE:
-	  /* don't know what to do with this token */
-		break;
-	default:
-	  /* this state is unexpected! */
-		break;
-	}
-
-	return code_block_open;
+  mrb_bool code_block_open = FALSE;
+
+  /* check for heredoc */
+  if (parser->parsing_heredoc != NULL) return TRUE;
+
+  /* check for unterminated string */
+  if (parser->lex_strterm) return TRUE;
+
+  /* check if parser error are available */
+  if (0 < parser->nerr) {
+    const char unexpected_end[] = "syntax error, unexpected $end";
+    const char *message = parser->error_buffer[0].message;
+
+    /* a parser error occur, we have to check if */
+    /* we need to read one more line or if there is */
+    /* a different issue which we have to show to */
+    /* the user */
+
+    if (strncmp(message, unexpected_end, sizeof(unexpected_end) - 1) == 0) {
+      code_block_open = TRUE;
+    }
+    else if (strcmp(message, "syntax error, unexpected keyword_end") == 0) {
+      code_block_open = FALSE;
+    }
+    else if (strcmp(message, "syntax error, unexpected tREGEXP_BEG") == 0) {
+      code_block_open = FALSE;
+    }
+    return code_block_open;
+  }
+
+  switch (parser->lstate) {
+
+  /* all states which need more code */
+
+  case EXPR_BEG:
+    /* beginning of a statement, */
+    /* that means previous line ended */
+    code_block_open = FALSE;
+    break;
+  case EXPR_DOT:
+    /* a message dot was the last token, */
+    /* there has to come more */
+    code_block_open = TRUE;
+    break;
+  case EXPR_CLASS:
+    /* a class keyword is not enough! */
+    /* we need also a name of the class */
+    code_block_open = TRUE;
+    break;
+  case EXPR_FNAME:
+    /* a method name is necessary */
+    code_block_open = TRUE;
+    break;
+  case EXPR_VALUE:
+    /* if, elsif, etc. without condition */
+    code_block_open = TRUE;
+    break;
+
+  /* now all the states which are closed */
+
+  case EXPR_ARG:
+    /* an argument is the last token */
+    code_block_open = FALSE;
+    break;
+
+  /* all states which are unsure */
+
+  case EXPR_CMDARG:
+    break;
+  case EXPR_END:
+    /* an expression was ended */
+    break;
+  case EXPR_ENDARG:
+    /* closing parenthese */
+    break;
+  case EXPR_ENDFN:
+    /* definition end */
+    break;
+  case EXPR_MID:
+    /* jump keyword like break, return, ... */
+    break;
+  case EXPR_MAX_STATE:
+    /* don't know what to do with this token */
+    break;
+  default:
+    /* this state is unexpected! */
+    break;
+  }
+
+  return code_block_open;
 }
 
 struct _args {
-	mrb_bool verbose : 1;
-	int argc;
-	char** argv;
+  FILE *rfp;
+  mrb_bool verbose      : 1;
+  mrb_bool debug        : 1;
+  int argc;
+  char** argv;
+  int libc;
+  char **libv;
 };
 
@@ -203,17 +233,28 @@
 usage(const char *name)
 {
-	static const char *const usage_msg[] = {
-		"switches:",
-		"-v           print version number, then run in verbose mode",
-		"--verbose    run in verbose mode",
-		"--version    print the version",
-		"--copyright  print the copyright",
-		NULL
-	};
-	const char *const *p = usage_msg;
-
-	printf("Usage: %s [switches]\n", name);
-	while (*p)
-		printf("  %s\n", *p++);
+  static const char *const usage_msg[] = {
+  "switches:",
+  "-d           set $DEBUG to true (same as `mruby -d`)",
+  "-r library   same as `mruby -r`",
+  "-v           print version number, then run in verbose mode",
+  "--verbose    run in verbose mode",
+  "--version    print the version",
+  "--copyright  print the copyright",
+  NULL
+  };
+  const char *const *p = usage_msg;
+
+  printf("Usage: %s [switches] [programfile] [arguments]\n", name);
+  while (*p)
+    printf("  %s\n", *p++);
+}
+
+static char *
+dup_arg_item(mrb_state *mrb, const char *item)
+{
+  size_t buflen = strlen(item) + 1;
+  char *buf = (char*)mrb_malloc(mrb, buflen);
+  memcpy(buf, item, buflen);
+  return buf;
 }
 
@@ -221,36 +262,72 @@
 parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
 {
-	static const struct _args args_zero = {0};
-
-	*args = args_zero;
-
-	for (argc--, argv++; argc > 0; argc--, argv++) {
-		char *item;
-		if (argv[0][0] != '-') break;
-
-		item = argv[0] + 1;
-		switch (*item++) {
-		case 'v':
-			if (!args->verbose) mrb_show_version(mrb);
-			args->verbose = TRUE;
-			break;
-		case '-':
-			if (strcmp((*argv) + 2, "version") == 0) {
-				mrb_show_version(mrb);
-				exit(EXIT_SUCCESS);
-			}
-			else if (strcmp((*argv) + 2, "verbose") == 0) {
-				args->verbose = TRUE;
-				break;
-			}
-			else if (strcmp((*argv) + 2, "copyright") == 0) {
-				mrb_show_copyright(mrb);
-				exit(EXIT_SUCCESS);
-			}
-		default:
-			return EXIT_FAILURE;
-		}
-	}
-	return EXIT_SUCCESS;
+  char **origargv = argv;
+  static const struct _args args_zero = { 0 };
+
+  *args = args_zero;
+
+  for (argc--,argv++; argc > 0; argc--,argv++) {
+    char *item;
+    if (argv[0][0] != '-') break;
+
+    item = argv[0] + 1;
+    switch (*item++) {
+    case 'd':
+      args->debug = TRUE;
+      break;
+    case 'r':
+      if (!item[0]) {
+        if (argc <= 1) {
+          printf("%s: No library specified for -r\n", *origargv);
+          return EXIT_FAILURE;
+        }
+        argc--; argv++;
+        item = argv[0];
+      }
+      if (args->libc == 0) {
+        args->libv = (char**)mrb_malloc(mrb, sizeof(char*));
+      }
+      else {
+        args->libv = (char**)mrb_realloc(mrb, args->libv, sizeof(char*) * (args->libc + 1));
+      }
+      args->libv[args->libc++] = dup_arg_item(mrb, item);
+      break;
+    case 'v':
+      if (!args->verbose) mrb_show_version(mrb);
+      args->verbose = TRUE;
+      break;
+    case '-':
+      if (strcmp((*argv) + 2, "version") == 0) {
+        mrb_show_version(mrb);
+        exit(EXIT_SUCCESS);
+      }
+      else if (strcmp((*argv) + 2, "verbose") == 0) {
+        args->verbose = TRUE;
+        break;
+      }
+      else if (strcmp((*argv) + 2, "copyright") == 0) {
+        mrb_show_copyright(mrb);
+        exit(EXIT_SUCCESS);
+      }
+    default:
+      return EXIT_FAILURE;
+    }
+  }
+
+  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,5 +335,14 @@
 cleanup(mrb_state *mrb, struct _args *args)
 {
-	mrb_close(mrb);
+  if (args->rfp)
+    fclose(args->rfp);
+  mrb_free(mrb, args->argv);
+  if (args->libc) {
+    while (args->libc--) {
+      mrb_free(mrb, args->libv[args->libc]);
+    }
+    mrb_free(mrb, args->libv);
+  }
+  mrb_close(mrb);
 }
 
@@ -265,5 +351,5 @@
 print_hint(void)
 {
-	printf("mirb - Embeddable Interactive Ruby Shell\n\n");
+  printf("mirb - Embeddable Interactive Ruby Shell\n\n");
 }
 
@@ -273,11 +359,11 @@
 print_cmdline(int code_block_open)
 {
-	if (code_block_open) {
-		printf("* ");
-	}
-	else {
-		printf("> ");
-	}
-	fflush(stdout);
+  if (code_block_open) {
+    printf("* ");
+  }
+  else {
+    printf("> ");
+  }
+  fflush(stdout);
 }
 #endif
@@ -288,206 +374,336 @@
 check_keyword(const char *buf, const char *word)
 {
-	const char *p = buf;
-	size_t len = strlen(word);
-
-	/* skip preceding spaces */
-	while (*p && isspace((unsigned char)*p)) {
-		p++;
-	}
-	/* check keyword */
-	if (strncmp(p, word, len) != 0) {
-		return 0;
-	}
-	p += len;
-	/* skip trailing spaces */
-	while (*p) {
-		if (!isspace((unsigned char)*p)) return 0;
-		p++;
-	}
-	return 1;
-}
+  const char *p = buf;
+  size_t len = strlen(word);
+
+  /* skip preceding spaces */
+  while (*p && ISSPACE(*p)) {
+    p++;
+  }
+  /* check keyword */
+  if (strncmp(p, word, len) != 0) {
+    return 0;
+  }
+  p += len;
+  /* skip trailing spaces */
+  while (*p) {
+    if (!ISSPACE(*p)) return 0;
+    p++;
+  }
+  return 1;
+}
+
+
+#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
+
+#ifndef DISABLE_MIRB_UNDERSCORE
+void decl_lv_underscore(mrb_state *mrb, mrbc_context *cxt)
+{
+  struct RProc *proc;
+  struct mrb_parser_state *parser;
+
+  parser = mrb_parse_string(mrb, "_=nil", cxt);
+  if (parser == NULL) {
+    fputs("create parser state error\n", stderr);
+    mrb_close(mrb);
+    exit(EXIT_FAILURE);
+  }
+
+  proc = mrb_generate_code(mrb, parser);
+  mrb_vm_run(mrb, proc, mrb_top_self(mrb), 0);
+
+  mrb_parser_free(parser);
+}
+#endif
 
 int
 mirb_main(int argc, char **argv)
 {
-	char ruby_code[1024] = {0};
-	char last_code_line[1024] = {0};
+  char ruby_code[4096] = { 0 };
+  char last_code_line[1024] = { 0 };
 #ifndef ENABLE_READLINE
-	int last_char;
-	int char_index;
+  int last_char;
+  size_t char_index;
 #else
-	char *history_path;
-#endif
-	mrbc_context *cxt;
-	struct mrb_parser_state *parser;
-	mrb_state *mrb;
-	mrb_value result;
-	struct _args args;
-	int n;
-	mrb_bool code_block_open = FALSE;
-	int ai;
-	unsigned int stack_keep = 0;
-
-	/* new interpreter instance */
-	mrb = mrb_open();
-	if (mrb == NULL) {
-		fputs("Invalid mrb interpreter, exiting mirb\n", stderr);
-		return EXIT_FAILURE;
-	}
-	mrb_define_global_const(mrb, "ARGV", mrb_ary_new_capa(mrb, 0));
-
-	n = parse_args(mrb, argc, argv, &args);
-	if (n == EXIT_FAILURE) {
-		cleanup(mrb, &args);
-		usage(argv[0]);
-		return n;
-	}
+  char *history_path;
+  char* line;
+#endif
+  mrbc_context *cxt;
+  struct mrb_parser_state *parser;
+  mrb_state *mrb;
+  mrb_value result;
+  struct _args args;
+  mrb_value ARGV;
+  int n;
+  int i;
+  mrb_bool code_block_open = FALSE;
+  int ai;
+  unsigned int stack_keep = 0;
+
+  /* new interpreter instance */
+  mrb = mrb_open();
+  if (mrb == NULL) {
+    fputs("Invalid mrb interpreter, exiting mirb\n", stderr);
+    return EXIT_FAILURE;
+  }
+
+  n = parse_args(mrb, argc, argv, &args);
+  if (n == EXIT_FAILURE) {
+    cleanup(mrb, &args);
+    usage(argv[0]);
+    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);
+  mrb_gv_set(mrb, mrb_intern_lit(mrb, "$DEBUG"), mrb_bool_value(args.debug));
 
 #ifdef ENABLE_READLINE
-	history_path = get_history_path(mrb);
-	if (history_path == NULL) {
-		fputs("failed to get history path\n", stderr);
-		mrb_close(mrb);
-		return EXIT_FAILURE;
-	}
-
-	MIRB_USING_HISTORY();
-	MIRB_READ_HISTORY(history_path);
-#endif
-
-	print_hint();
-
-	cxt = mrbc_context_new(mrb);
-	cxt->capture_errors = TRUE;
-	cxt->lineno = 1;
-	mrbc_filename(mrb, cxt, "(mirb)");
-	if (args.verbose) cxt->dump_result = TRUE;
-
-	ai = mrb_gc_arena_save(mrb);
-
-	while (TRUE) {
-		char *utf8;
+  history_path = get_history_path(mrb);
+  if (history_path == NULL) {
+    fputs("failed to get history path\n", stderr);
+    mrb_close(mrb);
+    return EXIT_FAILURE;
+  }
+
+  MIRB_USING_HISTORY();
+  MIRB_READ_HISTORY(history_path);
+#endif
+
+  print_hint();
+
+  cxt = mrbc_context_new(mrb);
+
+#ifndef DISABLE_MIRB_UNDERSCORE
+  decl_lv_underscore(mrb, cxt);
+#endif
+
+  /* Load libraries */
+  for (i = 0; i < args.libc; i++) {
+    FILE *lfp = fopen(args.libv[i], "r");
+    if (lfp == NULL) {
+      printf("Cannot open library file. (%s)\n", args.libv[i]);
+      cleanup(mrb, &args);
+      return EXIT_FAILURE;
+    }
+    mrb_load_file_cxt(mrb, lfp, cxt);
+    fclose(lfp);
+  }
+
+  cxt->capture_errors = TRUE;
+  cxt->lineno = 1;
+  mrbc_filename(mrb, cxt, "(mirb)");
+  if (args.verbose) cxt->dump_result = TRUE;
+
+  ai = mrb_gc_arena_save(mrb);
+
+  while (TRUE) {
+    char *utf8;
+    struct mrb_jmpbuf c_jmp;
+
+    MRB_TRY(&c_jmp);
+    mrb->jmp = &c_jmp;
+    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);
-
-		char_index = 0;
-		while ((last_char = getchar()) != '\n') {
-			if (last_char == EOF) break;
-			if (char_index > sizeof(last_code_line) - 2) {
-				fputs("input string too long\n", stderr);
-				continue;
-			}
-			last_code_line[char_index++] = last_char;
-		}
-		if (last_char == EOF) {
-			fputs("\n", stdout);
-			break;
-		}
-
-		last_code_line[char_index++] = '\n';
-		last_code_line[char_index] = '\0';
+    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) {
+        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) {
+      fputs("\n", stdout);
+      break;
+    }
+
+    last_code_line[char_index++] = '\n';
+    last_code_line[char_index] = '\0';
 #else
-		char* line = MIRB_READLINE(code_block_open ? "* " : "> ");
-		if (line == NULL) {
-			printf("\n");
-			break;
-		}
-		if (strlen(line) > sizeof(last_code_line) - 2) {
-			fputs("input string too long\n", stderr);
-			continue;
-		}
-		strlcpy(last_code_line, line, sizeof(last_code_line));
-		strlcat(last_code_line, "\n", sizeof(last_code_line));
-		MIRB_ADD_HISTORY(line);
-		free(line);
-#endif
-
-		if (code_block_open) {
-			if (strlen(ruby_code) + strlen(last_code_line) > sizeof(ruby_code) - 1) {
-				fputs("concatenated input string too long\n", stderr);
-				continue;
-			}
-			strlcat(ruby_code, last_code_line, sizeof(ruby_code));
-		}
-		else {
-			if (check_keyword(last_code_line, "quit") || check_keyword(last_code_line, "exit")) {
-				break;
-			}
-			strlcpy(ruby_code, last_code_line, sizeof(ruby_code));
-		}
-
-		utf8 = mrb_utf8_from_locale(ruby_code, -1);
-		if (!utf8) abort();
-
-		/* parse code */
-		parser = mrb_parser_new(mrb);
-		if (parser == NULL) {
-			fputs("create parser state error\n", stderr);
-			break;
-		}
-		parser->s = utf8;
-		parser->send = utf8 + strlen(utf8);
-		parser->lineno = cxt->lineno;
-		mrb_parser_parse(parser, cxt);
-		code_block_open = is_code_block_open(parser);
-		mrb_utf8_free(utf8);
-
-		if (code_block_open) {
-		  /* no evaluation of code */
-		}
-		else {
-			if (0 < parser->nerr) {
-			  /* syntax error */
-				printf("line %d: %s\n", parser->error_buffer[0].lineno, parser->error_buffer[0].message);
-			}
-			else {
-			  /* generate bytecode */
-				struct RProc *proc = mrb_generate_code(mrb, parser);
-				if (proc == NULL) {
-					fputs("codegen error\n", stderr);
-					mrb_parser_free(parser);
-					break;
-				}
-
-				if (args.verbose) {
-					mrb_codedump_all(mrb, proc);
-				}
-				/* pass a proc for evaulation */
-				/* evaluate the bytecode */
-				result = mrb_context_run(mrb,
-					proc,
-					mrb_top_self(mrb),
-					stack_keep);
-				stack_keep = proc->body.irep->nlocals;
-				/* did an exception occur? */
-				if (mrb->exc) {
-					p(mrb, mrb_obj_value(mrb->exc), 0);
-					mrb->exc = 0;
-				}
-				else {
-				  /* no */
-					if (!mrb_respond_to(mrb, result, mrb_intern_lit(mrb, "inspect"))) {
-						result = mrb_any_to_s(mrb, result);
-					}
-					p(mrb, result, 1);
-				}
-			}
-			ruby_code[0] = '\0';
-			last_code_line[0] = '\0';
-			mrb_gc_arena_restore(mrb, ai);
-		}
-		mrb_parser_free(parser);
-		cxt->lineno++;
-	}
+    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");
+      break;
+    }
+    if (strlen(line) > sizeof(last_code_line)-2) {
+      fputs("input string too long\n", stderr);
+      continue;
+    }
+    strcpy(last_code_line, line);
+    strcat(last_code_line, "\n");
+    MIRB_ADD_HISTORY(line);
+    MIRB_LINE_FREE(line);
+#endif
+
+  done:
+    if (code_block_open) {
+      if (strlen(ruby_code)+strlen(last_code_line) > sizeof(ruby_code)-1) {
+        fputs("concatenated input string too long\n", stderr);
+        continue;
+      }
+      strcat(ruby_code, last_code_line);
+    }
+    else {
+      if (check_keyword(last_code_line, "quit") || check_keyword(last_code_line, "exit")) {
+        break;
+      }
+      strcpy(ruby_code, last_code_line);
+    }
+
+    utf8 = mrb_utf8_from_locale(ruby_code, -1);
+    if (!utf8) abort();
+
+    /* parse code */
+    parser = mrb_parser_new(mrb);
+    if (parser == NULL) {
+      fputs("create parser state error\n", stderr);
+      break;
+    }
+    parser->s = utf8;
+    parser->send = utf8 + strlen(utf8);
+    parser->lineno = cxt->lineno;
+    mrb_parser_parse(parser, cxt);
+    code_block_open = is_code_block_open(parser);
+    mrb_utf8_free(utf8);
+
+    if (code_block_open) {
+      /* no evaluation of code */
+    }
+    else {
+      if (0 < parser->nwarn) {
+        /* warning */
+        char* msg = mrb_locale_from_utf8(parser->warn_buffer[0].message, -1);
+        printf("line %d: %s\n", parser->warn_buffer[0].lineno, msg);
+        mrb_locale_free(msg);
+      }
+      if (0 < parser->nerr) {
+        /* syntax error */
+        char* msg = mrb_locale_from_utf8(parser->error_buffer[0].message, -1);
+        printf("line %d: %s\n", parser->error_buffer[0].lineno, msg);
+        mrb_locale_free(msg);
+      }
+      else {
+        /* generate bytecode */
+        struct RProc *proc = mrb_generate_code(mrb, parser);
+        if (proc == NULL) {
+          fputs("codegen error\n", stderr);
+          mrb_parser_free(parser);
+          break;
+        }
+
+        if (args.verbose) {
+          mrb_codedump_all(mrb, proc);
+        }
+        /* adjust stack length of toplevel environment */
+        if (mrb->c->cibase->env) {
+          struct REnv *e = mrb->c->cibase->env;
+          if (e && MRB_ENV_STACK_LEN(e) < proc->body.irep->nlocals) {
+            MRB_ENV_SET_STACK_LEN(e, proc->body.irep->nlocals);
+          }
+        }
+        /* pass a proc for evaluation */
+        /* evaluate the bytecode */
+        result = mrb_vm_run(mrb,
+            proc,
+            mrb_top_self(mrb),
+            stack_keep);
+        stack_keep = proc->body.irep->nlocals;
+        /* did an exception occur? */
+        if (mrb->exc) {
+          p(mrb, mrb_obj_value(mrb->exc), 0);
+          mrb->exc = 0;
+        }
+        else {
+          /* no */
+          if (!mrb_respond_to(mrb, result, mrb_intern_lit(mrb, "inspect"))){
+            result = mrb_any_to_s(mrb, result);
+          }
+          p(mrb, result, 1);
+#ifndef DISABLE_MIRB_UNDERSCORE
+          *(mrb->c->stack + 1) = result;
+#endif
+        }
+      }
+      ruby_code[0] = '\0';
+      last_code_line[0] = '\0';
+      mrb_gc_arena_restore(mrb, ai);
+    }
+    mrb_parser_free(parser);
+    cxt->lineno++;
+    MRB_CATCH(&c_jmp) {
+      p(mrb, mrb_obj_value(mrb->exc), 0);
+      mrb->exc = 0;
+    }
+    MRB_END_EXC(&c_jmp);
+  }
 
 #ifdef ENABLE_READLINE
-	MIRB_WRITE_HISTORY(history_path);
-	mrb_free(mrb, history_path);
-#endif
-
-	mrbc_context_free(mrb, cxt);
-	mrb_close(mrb);
-
-	return 0;
-}
+  MIRB_WRITE_HISTORY(history_path);
+  mrb_free(mrb, history_path);
+#endif
+
+  if (args.rfp) fclose(args.rfp);
+  mrb_free(mrb, args.argv);
+  if (args.libv) {
+    for (i = 0; i < args.libc; ++i) {
+      mrb_free(mrb, args.libv[i]);
+    }
+    mrb_free(mrb, args.libv);
+  }
+  mrbc_context_free(mrb, cxt);
+  mrb_close(mrb);
+
+  return 0;
+}
Index: EcnlProtoTool/trunk/prototool/src/mrbc.c
===================================================================
--- EcnlProtoTool/trunk/prototool/src/mrbc.c	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/mrbc.c	(revision 439)
@@ -1,23 +1,28 @@
+#include <mruby.h>
+
+#ifdef MRB_DISABLE_STDIO
+# error mruby-bin-mrbc conflicts 'MRB_DISABLE_STDIO' configuration in your 'build_config.rb'
+#endif
+
 #include <stdlib.h>
-
-#include "mruby.h"
-#include "mruby/compile.h"
-#include "mruby/dump.h"
-#include "mruby/proc.h"
+#include <string.h>
+#include <mruby/compile.h>
+#include <mruby/dump.h>
+#include <mruby/proc.h>
 
 #define RITEBIN_EXT ".mrb"
 #define C_EXT       ".c"
 
-struct mrbc_args
-{
-	int argc;
-	char **argv;
-	int idx;
-	const char *prog;
-	const char *outfile;
-	const char *initname;
-	mrb_bool check_syntax : 1;
-	mrb_bool verbose : 1;
-	unsigned int flags : 4;
+struct mrbc_args {
+  int argc;
+  char **argv;
+  int idx;
+  const char *prog;
+  const char *outfile;
+  const char *initname;
+  mrb_bool check_syntax : 1;
+  mrb_bool verbose      : 1;
+  mrb_bool remove_lv    : 1;
+  unsigned int flags    : 4;
 };
 
@@ -25,23 +30,24 @@
 usage(const char *name)
 {
-	static const char *const usage_msg[] = {
-		"switches:",
-		"-c           check syntax only",
-		"-o<outfile>  place the output into <outfile>",
-		"-v           print version number, then turn on verbose mode",
-		"-g           produce debugging information",
-		"-B<symbol>   binary <symbol> output in C language format",
-		"-e           generate little endian iseq data",
-		"-E           generate big endian iseq data",
-		"--verbose    run at verbose mode",
-		"--version    print the version",
-		"--copyright  print the copyright",
-		NULL
-	};
-	const char *const *p = usage_msg;
-
-	printf("Usage: %s [switches] programfile\n", name);
-	while (*p)
-		printf("  %s\n", *p++);
+  static const char *const usage_msg[] = {
+  "switches:",
+  "-c           check syntax only",
+  "-o<outfile>  place the output into <outfile>",
+  "-v           print version number, then turn on verbose mode",
+  "-g           produce debugging information",
+  "-B<symbol>   binary <symbol> output in C language format",
+  "-e           generate little endian iseq data",
+  "-E           generate big endian iseq data",
+  "--remove-lv  remove local variables",
+  "--verbose    run at verbose mode",
+  "--version    print the version",
+  "--copyright  print the copyright",
+  NULL
+  };
+  const char *const *p = usage_msg;
+
+  printf("Usage: %s [switches] programfile\n", name);
+  while (*p)
+    printf("  %s\n", *p++);
 }
 
@@ -49,20 +55,20 @@
 get_outfilename(mrb_state *mrb, char *infile, const char *ext)
 {
-	size_t infilelen;
-	size_t extlen;
-	char *outfile;
-	char *p;
-
-	infilelen = strlen(infile);
-	extlen = strlen(ext);
-	outfile = (char*)mrb_malloc(mrb, infilelen + extlen + 1);
-	memcpy(outfile, infile, infilelen + 1);
-	if (*ext) {
-		if ((p = strrchr(outfile, '.')) == NULL)
-			p = outfile + infilelen;
-		memcpy(p, ext, extlen + 1);
-	}
-
-	return outfile;
+  size_t infilelen;
+  size_t extlen;
+  char *outfile;
+  char *p;
+
+  infilelen = strlen(infile);
+  extlen = strlen(ext);
+  outfile = (char*)mrb_malloc(mrb, infilelen + extlen + 1);
+  memcpy(outfile, infile, infilelen + 1);
+  if (*ext) {
+    if ((p = strrchr(outfile, '.')) == NULL)
+      p = outfile + infilelen;
+    memcpy(p, ext, extlen + 1);
+  }
+
+  return outfile;
 }
 
@@ -70,90 +76,94 @@
 parse_args(mrb_state *mrb, int argc, char **argv, struct mrbc_args *args)
 {
-	static const struct mrbc_args args_zero = { 0 };
-	int i;
-
-	*args = args_zero;
-	args->argc = argc;
-	args->argv = argv;
-	args->prog = argv[0];
-
-	for (i = 1; i < argc; i++) {
-		if (argv[i][0] == '-') {
-			switch ((argv[i])[1]) {
-			case 'o':
-				if (args->outfile) {
-					fprintf(stderr, "%s: an output file is already specified. (%s)\n",
-						args->prog, args->outfile);
-					return -1;
-				}
-				if (argv[i][2] == '\0' && argv[i + 1]) {
-					i++;
-					args->outfile = get_outfilename(mrb, argv[i], "");
-				}
-				else {
-					args->outfile = get_outfilename(mrb, argv[i] + 2, "");
-				}
-				break;
-			case 'B':
-				if (argv[i][2] == '\0' && argv[i + 1]) {
-					i++;
-					args->initname = argv[i];
-				}
-				else {
-					args->initname = argv[i] + 2;
-				}
-				if (*args->initname == '\0') {
-					fprintf(stderr, "%s: function name is not specified.\n", args->prog);
-					return -1;
-				}
-				break;
-			case 'c':
-				args->check_syntax = TRUE;
-				break;
-			case 'v':
-				if (!args->verbose) mrb_show_version(mrb);
-				args->verbose = TRUE;
-				break;
-			case 'g':
-				args->flags |= DUMP_DEBUG_INFO;
-				break;
-			case 'E':
-				args->flags = DUMP_ENDIAN_BIG | (args->flags & ~DUMP_ENDIAN_MASK);
-				break;
-			case 'e':
-				args->flags = DUMP_ENDIAN_LIL | (args->flags & ~DUMP_ENDIAN_MASK);
-				break;
-			case 'h':
-				return -1;
-			case '-':
-				if (argv[i][1] == '\n') {
-					return i;
-				}
-				if (strcmp(argv[i] + 2, "version") == 0) {
-					mrb_show_version(mrb);
-					exit(EXIT_SUCCESS);
-				}
-				else if (strcmp(argv[i] + 2, "verbose") == 0) {
-					args->verbose = TRUE;
-					break;
-				}
-				else if (strcmp(argv[i] + 2, "copyright") == 0) {
-					mrb_show_copyright(mrb);
-					exit(EXIT_SUCCESS);
-				}
-				return -1;
-			default:
-				return i;
-			}
-		}
-		else {
-			break;
-		}
-	}
-	if (args->verbose && args->initname && (args->flags & DUMP_ENDIAN_MASK) == 0) {
-		fprintf(stderr, "%s: generating %s endian C file. specify -e/-E for cross compiling.\n",
-			args->prog, bigendian_p() ? "big" : "little");
-	}
-	return i;
+  static const struct mrbc_args args_zero = { 0 };
+  int i;
+
+  *args = args_zero;
+  args->argc = argc;
+  args->argv = argv;
+  args->prog = argv[0];
+
+  for (i=1; i<argc; i++) {
+    if (argv[i][0] == '-') {
+      switch ((argv[i])[1]) {
+      case 'o':
+        if (args->outfile) {
+          fprintf(stderr, "%s: an output file is already specified. (%s)\n",
+                  args->prog, args->outfile);
+          return -1;
+        }
+        if (argv[i][2] == '\0' && argv[i+1]) {
+          i++;
+          args->outfile = get_outfilename(mrb, argv[i], "");
+        }
+        else {
+          args->outfile = get_outfilename(mrb, argv[i] + 2, "");
+        }
+        break;
+      case 'B':
+        if (argv[i][2] == '\0' && argv[i+1]) {
+          i++;
+          args->initname = argv[i];
+        }
+        else {
+          args->initname = argv[i]+2;
+        }
+        if (*args->initname == '\0') {
+          fprintf(stderr, "%s: function name is not specified.\n", args->prog);
+          return -1;
+        }
+        break;
+      case 'c':
+        args->check_syntax = TRUE;
+        break;
+      case 'v':
+        if (!args->verbose) mrb_show_version(mrb);
+        args->verbose = TRUE;
+        break;
+      case 'g':
+        args->flags |= DUMP_DEBUG_INFO;
+        break;
+      case 'E':
+        args->flags = DUMP_ENDIAN_BIG | (args->flags & ~DUMP_ENDIAN_MASK);
+        break;
+      case 'e':
+        args->flags = DUMP_ENDIAN_LIL | (args->flags & ~DUMP_ENDIAN_MASK);
+        break;
+      case 'h':
+        return -1;
+      case '-':
+        if (argv[i][1] == '\n') {
+          return i;
+        }
+        if (strcmp(argv[i] + 2, "version") == 0) {
+          mrb_show_version(mrb);
+          exit(EXIT_SUCCESS);
+        }
+        else if (strcmp(argv[i] + 2, "verbose") == 0) {
+          args->verbose = TRUE;
+          break;
+        }
+        else if (strcmp(argv[i] + 2, "copyright") == 0) {
+          mrb_show_copyright(mrb);
+          exit(EXIT_SUCCESS);
+        }
+        else if (strcmp(argv[i] + 2, "remove-lv") == 0) {
+          args->remove_lv = TRUE;
+          break;
+        }
+        return -1;
+      default:
+        return i;
+      }
+    }
+    else {
+      break;
+    }
+  }
+  if (args->verbose && args->initname && (args->flags & DUMP_ENDIAN_MASK) == 0) {
+    fprintf(stderr, "%s: generating %s endian C file. specify -e/-E for cross compiling.\n",
+            args->prog, bigendian_p() ? "big" : "little");
+  }
+  return i;
 }
 
@@ -161,6 +171,6 @@
 cleanup(mrb_state *mrb, struct mrbc_args *args)
 {
-	mrb_free(mrb, (void*)args->outfile);
-	mrb_close(mrb);
+  mrb_free(mrb, (void*)args->outfile);
+  mrb_close(mrb);
 }
 
@@ -168,21 +178,21 @@
 partial_hook(struct mrb_parser_state *p)
 {
-	mrbc_context *c = p->cxt;
-	struct mrbc_args *args = (struct mrbc_args *)c->partial_data;
-	const char *fn;
-
-	if (p->f) fclose(p->f);
-	if (args->idx >= args->argc) {
-		p->f = NULL;
-		return -1;
-	}
-	fn = args->argv[args->idx++];
-	p->f = fopen(fn, "r");
-	if (p->f == NULL) {
-		fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, fn);
-		return -1;
-	}
-	mrb_parser_set_filename(p, fn);
-	return 0;
+  mrbc_context *c = p->cxt;
+  struct mrbc_args *args = (struct mrbc_args *)c->partial_data;
+  const char *fn;
+
+  if (p->f) fclose(p->f);
+  if (args->idx >= args->argc) {
+    p->f = NULL;
+    return -1;
+  }
+  fn = args->argv[args->idx++];
+  p->f = fopen(fn, "rb");
+  if (p->f == NULL) {
+    fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, fn);
+    return -1;
+  }
+  mrb_parser_set_filename(p, fn);
+  return 0;
 }
 
@@ -190,38 +200,38 @@
 load_file(mrb_state *mrb, struct mrbc_args *args)
 {
-	mrbc_context *c;
-	mrb_value result;
-	char *input = args->argv[args->idx];
-	FILE *infile;
-	mrb_bool need_close = FALSE;
-
-	c = mrbc_context_new(mrb);
-	if (args->verbose)
-		c->dump_result = TRUE;
-	c->no_exec = TRUE;
-	if (input[0] == '-' && input[1] == '\0') {
-		infile = stdin;
-	}
-	else {
-		need_close = TRUE;
-		if ((infile = fopen(input, "r")) == NULL) {
-			fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, input);
-			return mrb_nil_value();
-		}
-	}
-	mrbc_filename(mrb, c, input);
-	args->idx++;
-	if (args->idx < args->argc) {
-		need_close = FALSE;
-		mrbc_partial_hook(mrb, c, partial_hook, (void*)args);
-	}
-
-	result = mrb_load_file_cxt(mrb, infile, c);
-	if (need_close) fclose(infile);
-	mrbc_context_free(mrb, c);
-	if (mrb_undef_p(result)) {
-		return mrb_nil_value();
-	}
-	return result;
+  mrbc_context *c;
+  mrb_value result;
+  char *input = args->argv[args->idx];
+  FILE *infile;
+  mrb_bool need_close = FALSE;
+
+  c = mrbc_context_new(mrb);
+  if (args->verbose)
+    c->dump_result = TRUE;
+  c->no_exec = TRUE;
+  if (input[0] == '-' && input[1] == '\0') {
+    infile = stdin;
+  }
+  else {
+    need_close = TRUE;
+    if ((infile = fopen(input, "rb")) == NULL) {
+      fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, input);
+      return mrb_nil_value();
+    }
+  }
+  mrbc_filename(mrb, c, input);
+  args->idx++;
+  if (args->idx < args->argc) {
+    need_close = FALSE;
+    mrbc_partial_hook(mrb, c, partial_hook, (void*)args);
+  }
+
+  result = mrb_load_file_cxt(mrb, infile, c);
+  if (need_close) fclose(infile);
+  mrbc_context_free(mrb, c);
+  if (mrb_undef_p(result)) {
+    return mrb_nil_value();
+  }
+  return result;
 }
 
@@ -229,20 +239,23 @@
 dump_file(mrb_state *mrb, FILE *wfp, const char *outfile, struct RProc *proc, struct mrbc_args *args)
 {
-	int n = MRB_DUMP_OK;
-	mrb_irep *irep = proc->body.irep;
-
-	if (args->initname) {
-		n = mrb_dump_irep_cfunc(mrb, irep, args->flags, wfp, args->initname);
-		if (n == MRB_DUMP_INVALID_ARGUMENT) {
-			fprintf(stderr, "%s: invalid C language symbol name\n", args->initname);
-		}
-	}
-	else {
-		n = mrb_dump_irep_binary(mrb, irep, args->flags, wfp);
-	}
-	if (n != MRB_DUMP_OK) {
-		fprintf(stderr, "%s: error in mrb dump (%s) %d\n", args->prog, outfile, n);
-	}
-	return n;
+  int n = MRB_DUMP_OK;
+  mrb_irep *irep = proc->body.irep;
+
+  if (args->remove_lv) {
+    mrb_irep_remove_lv(mrb, irep);
+  }
+  if (args->initname) {
+    n = mrb_dump_irep_cfunc(mrb, irep, args->flags, wfp, args->initname);
+    if (n == MRB_DUMP_INVALID_ARGUMENT) {
+      fprintf(stderr, "%s: invalid C language symbol name\n", args->initname);
+    }
+  }
+  else {
+    n = mrb_dump_irep_binary(mrb, irep, args->flags, wfp);
+  }
+  if (n != MRB_DUMP_OK) {
+    fprintf(stderr, "%s: error in mrb dump (%s) %d\n", args->prog, outfile, n);
+  }
+  return n;
 }
 
@@ -250,69 +263,69 @@
 mrbc_main(int argc, char **argv)
 {
-	mrb_state *mrb = mrb_open_allocf(mrb_default_allocf, NULL, TRUE);
-	int n, result;
-	struct mrbc_args args;
-	FILE *wfp;
-	mrb_value load;
-
-	if (mrb == NULL) {
-		fputs("Invalid mrb_state, exiting mrbc\n", stderr);
-		return EXIT_FAILURE;
-	}
-
-	n = parse_args(mrb, argc, argv, &args);
-	if (n < 0) {
-		cleanup(mrb, &args);
-		usage(argv[0]);
-		return EXIT_FAILURE;
-	}
-	if (n == argc) {
-		fprintf(stderr, "%s: no program file given\n", args.prog);
-		return EXIT_FAILURE;
-	}
-	if (args.outfile == NULL && !args.check_syntax) {
-		if (n + 1 == argc) {
-			args.outfile = get_outfilename(mrb, argv[n], args.initname ? C_EXT : RITEBIN_EXT);
-		}
-		else {
-			fprintf(stderr, "%s: output file should be specified to compile multiple files\n", args.prog);
-			return EXIT_FAILURE;
-		}
-	}
-
-	args.idx = n;
-	load = load_file(mrb, &args);
-	if (mrb_nil_p(load)) {
-		cleanup(mrb, &args);
-		return EXIT_FAILURE;
-	}
-	if (args.check_syntax) {
-		printf("%s:%s:Syntax OK\n", args.prog, argv[n]);
-	}
-
-	if (args.check_syntax) {
-		cleanup(mrb, &args);
-		return EXIT_SUCCESS;
-	}
-
-	if (args.outfile) {
-		if (strcmp("-", args.outfile) == 0) {
-			wfp = stdout;
-		}
-		else if ((wfp = fopen(args.outfile, "wb")) == NULL) {
-			fprintf(stderr, "%s: cannot open output file:(%s)\n", args.prog, args.outfile);
-			return EXIT_FAILURE;
-		}
-	}
-	else {
-		fprintf(stderr, "Output file is required\n");
-		return EXIT_FAILURE;
-	}
-	result = dump_file(mrb, wfp, args.outfile, mrb_proc_ptr(load), &args);
-	fclose(wfp);
-	cleanup(mrb, &args);
-	if (result != MRB_DUMP_OK) {
-		return EXIT_FAILURE;
-	}
-	return EXIT_SUCCESS;
-}
+  mrb_state *mrb = mrb_open_allocf(mrb_default_allocf, NULL, TRUE);
+  int n, result;
+  struct mrbc_args args;
+  FILE *wfp;
+  mrb_value load;
+
+  if (mrb == NULL) {
+    fputs("Invalid mrb_state, exiting mrbc\n", stderr);
+    return EXIT_FAILURE;
+  }
+
+  n = parse_args(mrb, argc, argv, &args);
+  if (n < 0) {
+    cleanup(mrb, &args);
+    usage(argv[0]);
+    return EXIT_FAILURE;
+  }
+  if (n == argc) {
+    fprintf(stderr, "%s: no program file given\n", args.prog);
+    return EXIT_FAILURE;
+  }
+  if (args.outfile == NULL && !args.check_syntax) {
+    if (n + 1 == argc) {
+      args.outfile = get_outfilename(mrb, argv[n], args.initname ? C_EXT : RITEBIN_EXT);
+    }
+    else {
+      fprintf(stderr, "%s: output file should be specified to compile multiple files\n", args.prog);
+      return EXIT_FAILURE;
+    }
+  }
+
+  args.idx = n;
+  load = load_file(mrb, &args);
+  if (mrb_nil_p(load)) {
+    cleanup(mrb, &args);
+    return EXIT_FAILURE;
+  }
+  if (args.check_syntax) {
+    printf("%s:%s:Syntax OK\n", args.prog, argv[n]);
+  }
+
+  if (args.check_syntax) {
+    cleanup(mrb, &args);
+    return EXIT_SUCCESS;
+  }
+
+  if (args.outfile) {
+    if (strcmp("-", args.outfile) == 0) {
+      wfp = stdout;
+    }
+    else if ((wfp = fopen(args.outfile, "wb")) == NULL) {
+      fprintf(stderr, "%s: cannot open output file:(%s)\n", args.prog, args.outfile);
+      return EXIT_FAILURE;
+    }
+  }
+  else {
+    fprintf(stderr, "Output file is required\n");
+    return EXIT_FAILURE;
+  }
+  result = dump_file(mrb, wfp, args.outfile, mrb_proc_ptr(load), &args);
+  fclose(wfp);
+  cleanup(mrb, &args);
+  if (result != MRB_DUMP_OK) {
+    return EXIT_FAILURE;
+  }
+  return EXIT_SUCCESS;
+}
Index: EcnlProtoTool/trunk/prototool/src/mrdb.c
===================================================================
--- EcnlProtoTool/trunk/prototool/src/mrdb.c	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/mrdb.c	(revision 439)
@@ -6,14 +6,16 @@
 #include <stdlib.h>
 #include <string.h>
-
-#include "mruby.h"
-#include "mruby/dump.h"
-#include "mruby/debug.h"
-#include "mruby/class.h"
-#include "mruby/opcode.h"
-#include "mruby/variable.h"
+#include <ctype.h>
+
+#include <mruby.h>
+#include <mruby/dump.h>
+#include <mruby/debug.h>
+#include <mruby/class.h>
+#include <mruby/opcode.h>
+#include <mruby/variable.h>
 
 #include "mrdb.h"
 #include "apibreak.h"
+#include "apilist.h"
 
 void mrdb_state_free(mrb_state *);
@@ -22,23 +24,21 @@
 static mrdb_state *_mrdb_state = NULL;
 
-struct _args
-{
-	FILE *rfp;
-	char* fname;
-	char* srcpath;
-	int argc;
-	char** argv;
-	mrb_bool mrbfile : 1;
+struct _args {
+  FILE *rfp;
+  char* fname;
+  char* srcpath;
+  int argc;
+  char** argv;
+  mrb_bool mrbfile : 1;
 };
 
-typedef struct debug_command
-{
-	const char *cmd1;
-	const char *cmd2;
-	uint8_t len1;
-	uint8_t len2;
-	uint8_t div;
-	debug_command_id id;
-	debug_command_func func;
+typedef struct debug_command {
+  const char *cmd1;
+  const char *cmd2;
+  uint8_t len1;
+  uint8_t len2;
+  uint8_t div;
+  debug_command_id id;
+  debug_command_func func;
 } debug_command;
 
@@ -52,4 +52,6 @@
   {"help",      NULL,           1, 0, 1, DBGCMD_HELP,           dbgcmd_help},            /* h[elp] */
   {"info",      "breakpoints",  1, 1, 1, DBGCMD_INFO_BREAK,     dbgcmd_info_break},      /* i[nfo] b[reakpoints] */
+  {"info",      "locals",       1, 1, 0, DBGCMD_INFO_LOCAL,     dbgcmd_info_local},      /* i[nfo] l[ocals] */
+  {"list",      NULL,           1, 0, 1, DBGCMD_LIST,           dbgcmd_list},            /* l[ist] */
   {"print",     NULL,           1, 0, 0, DBGCMD_PRINT,          dbgcmd_print},           /* p[rint] */
   {"quit",      NULL,           1, 0, 0, DBGCMD_QUIT,           dbgcmd_quit},            /* q[uit] */
@@ -64,18 +66,18 @@
 usage(const char *name)
 {
-	static const char *const usage_msg[] = {
-		"switches:",
-		"-b           load and execute RiteBinary (mrb) file",
-		"-d           specify source directory",
-		"--version    print the version",
-		"--copyright  print the copyright",
-		NULL
-	};
-	const char *const *p = usage_msg;
-
-	printf("Usage: %s [switches] programfile\n", name);
-	while (*p) {
-		printf("  %s\n", *p++);
-	}
+  static const char *const usage_msg[] = {
+  "switches:",
+  "-b           load and execute RiteBinary (mrb) file",
+  "-d           specify source directory",
+  "--version    print the version",
+  "--copyright  print the copyright",
+  NULL
+  };
+  const char *const *p = usage_msg;
+
+  printf("Usage: %s [switches] programfile\n", name);
+  while (*p) {
+    printf("  %s\n", *p++);
+  }
 }
 
@@ -83,86 +85,86 @@
 parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
 {
-	char **origargv = argv;
-	static const struct _args args_zero = { 0 };
-
-	*args = args_zero;
-
-	for (argc--, argv++; argc > 0; argc--, argv++) {
-		char *item;
-		if (argv[0][0] != '-') break;
-
-		item = argv[0] + 1;
-		switch (*item++) {
-		case 'b':
-			args->mrbfile = TRUE;
-			break;
-		case 'd':
-			if (item[0]) {
-				goto append_srcpath;
-			}
-			else if (argc > 1) {
-				argc--; argv++;
-				item = argv[0];
-			append_srcpath:
-				if (!args->srcpath) {
-					size_t buflen;
-					char *buf;
-
-					buflen = strlen(item) + 1;
-					buf = (char *)mrb_malloc(mrb, buflen);
-					memcpy(buf, item, buflen);
-					args->srcpath = buf;
-				}
-				else {
-					size_t srcpathlen;
-					size_t itemlen;
-
-					srcpathlen = strlen(args->srcpath);
-					itemlen = strlen(item);
-					args->srcpath =
-						(char *)mrb_realloc(mrb, args->srcpath, srcpathlen + itemlen + 2);
-					args->srcpath[srcpathlen] = '\n';
-					memcpy(args->srcpath + srcpathlen + 1, item, itemlen + 1);
-				}
-			}
-			else {
-				printf("%s: No path specified for -d\n", *origargv);
-				return EXIT_SUCCESS;
-			}
-			break;
-		case '-':
-			if (strcmp((*argv) + 2, "version") == 0) {
-				mrb_show_version(mrb);
-				exit(EXIT_SUCCESS);
-			}
-			else if (strcmp((*argv) + 2, "copyright") == 0) {
-				mrb_show_copyright(mrb);
-				exit(EXIT_SUCCESS);
-			}
-		default:
-			return EXIT_FAILURE;
-		}
-	}
-
-	if (args->rfp == NULL) {
-		if (*argv == NULL) {
-			printf("%s: Program file not specified.\n", *origargv);
-			return EXIT_FAILURE;
-		}
-		else {
-			args->rfp = fopen(argv[0], args->mrbfile ? "rb" : "r");
-			if (args->rfp == NULL) {
-				printf("%s: Cannot open program file. (%s)\n", *origargv, *argv);
-				return EXIT_FAILURE;
-			}
-			args->fname = argv[0];
-			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;
+  char **origargv = argv;
+  static const struct _args args_zero = { 0 };
+
+  *args = args_zero;
+
+  for (argc--,argv++; argc > 0; argc--,argv++) {
+    char *item;
+    if (argv[0][0] != '-') break;
+
+    item = argv[0] + 1;
+    switch (*item++) {
+    case 'b':
+      args->mrbfile = TRUE;
+      break;
+    case 'd':
+      if (item[0]) {
+        goto append_srcpath;
+      }
+      else if (argc > 1) {
+        argc--; argv++;
+        item = argv[0];
+append_srcpath:
+        if (!args->srcpath) {
+          size_t buflen;
+          char *buf;
+
+          buflen = strlen(item) + 1;
+          buf = (char *)mrb_malloc(mrb, buflen);
+          memcpy(buf, item, buflen);
+          args->srcpath = buf;
+        }
+        else {
+          size_t srcpathlen;
+          size_t itemlen;
+
+          srcpathlen = strlen(args->srcpath);
+          itemlen = strlen(item);
+          args->srcpath =
+            (char *)mrb_realloc(mrb, args->srcpath, srcpathlen + itemlen + 2);
+          args->srcpath[srcpathlen] = '\n';
+          memcpy(args->srcpath + srcpathlen + 1, item, itemlen + 1);
+        }
+      }
+      else {
+        printf("%s: No path specified for -d\n", *origargv);
+        return EXIT_SUCCESS;
+      }
+      break;
+    case '-':
+      if (strcmp((*argv) + 2, "version") == 0) {
+        mrb_show_version(mrb);
+        exit(EXIT_SUCCESS);
+      }
+      else if (strcmp((*argv) + 2, "copyright") == 0) {
+        mrb_show_copyright(mrb);
+        exit(EXIT_SUCCESS);
+      }
+    default:
+      return EXIT_FAILURE;
+    }
+  }
+
+  if (args->rfp == NULL) {
+    if (*argv == NULL) {
+      printf("%s: Program file not specified.\n", *origargv);
+      return EXIT_FAILURE;
+    }
+    else {
+      args->rfp = fopen(argv[0], args->mrbfile ? "rb" : "r");
+      if (args->rfp == NULL) {
+        printf("%s: Cannot open program file. (%s)\n", *origargv, *argv);
+        return EXIT_FAILURE;
+      }
+      args->fname = argv[0];
+      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;
 }
 
@@ -170,12 +172,12 @@
 cleanup(mrb_state *mrb, struct _args *args)
 {
-	if (args->rfp)
-		fclose(args->rfp);
-	if (args->srcpath)
-		mrb_free(mrb, args->srcpath);
-	if (args->argv)
-		mrb_free(mrb, args->argv);
-	mrdb_state_free(mrb);
-	mrb_close(mrb);
+  if (args->rfp)
+    fclose(args->rfp);
+  if (args->srcpath)
+    mrb_free(mrb, args->srcpath);
+  if (args->argv)
+    mrb_free(mrb, args->argv);
+  mrdb_state_free(mrb);
+  mrb_close(mrb);
 }
 
@@ -183,13 +185,13 @@
 mrb_debug_context_new(mrb_state *mrb)
 {
-	mrb_debug_context *dbg = (mrb_debug_context *)mrb_malloc(mrb, sizeof(mrb_debug_context));
-
-	memset(dbg, 0, sizeof(mrb_debug_context));
-
-	InterlockedExchange(&dbg->xm, DBG_INIT);
-	dbg->xphase = DBG_PHASE_BEFORE_RUN;
-	dbg->next_bpno = 1;
-
-	return dbg;
+  mrb_debug_context *dbg = (mrb_debug_context*)mrb_malloc(mrb, sizeof(mrb_debug_context));
+
+  memset(dbg, 0, sizeof(mrb_debug_context));
+
+  dbg->xm = DBG_INIT;
+  dbg->xphase = DBG_PHASE_BEFORE_RUN;
+  dbg->next_bpno = 1;
+
+  return dbg;
 }
 
@@ -197,8 +199,8 @@
 mrb_debug_context_get(mrb_state *mrb)
 {
-	if (!_debug_context) {
-		_debug_context = mrb_debug_context_new(mrb);
-	}
-	return _debug_context;
+  if (!_debug_context) {
+    _debug_context = mrb_debug_context_new(mrb);
+  }
+  return _debug_context;
 }
 
@@ -206,5 +208,5 @@
 mrb_debug_context_set(mrb_debug_context *dbg)
 {
-	_debug_context = dbg;
+  _debug_context = dbg;
 }
 
@@ -212,9 +214,9 @@
 mrb_debug_context_free(mrb_state *mrb)
 {
-	if (_debug_context) {
-		mrb_debug_delete_break_all(mrb, _debug_context);
-		mrb_free(mrb, _debug_context);
-		_debug_context = NULL;
-	}
+  if (_debug_context) {
+    mrb_debug_delete_break_all(mrb, _debug_context);
+    mrb_free(mrb, _debug_context);
+    _debug_context = NULL;
+  }
 }
 
@@ -222,13 +224,13 @@
 mrdb_state_new(mrb_state *mrb)
 {
-	mrdb_state *mrdb = (mrdb_state *)mrb_malloc(mrb, sizeof(mrdb_state));
-
-	memset(mrdb, 0, sizeof(mrdb_state));
-
-	mrdb->dbg = mrb_debug_context_get(mrb);
-	mrdb->command = (char *)mrb_malloc(mrb, MAX_COMMAND_LINE + 1);
-	mrdb->print_no = 1;
-
-	return mrdb;
+  mrdb_state *mrdb = (mrdb_state*)mrb_malloc(mrb, sizeof(mrdb_state));
+
+  memset(mrdb, 0, sizeof(mrdb_state));
+
+  mrdb->dbg = mrb_debug_context_get(mrb);
+  mrdb->command = (char*)mrb_malloc(mrb, MAX_COMMAND_LINE+1);
+  mrdb->print_no = 1;
+
+  return mrdb;
 }
 
@@ -236,8 +238,8 @@
 mrdb_state_get(mrb_state *mrb)
 {
-	if (!_mrdb_state) {
-		_mrdb_state = mrdb_state_new(mrb);
-	}
-	return _mrdb_state;
+  if (!_mrdb_state) {
+    _mrdb_state = mrdb_state_new(mrb);
+  }
+  return _mrdb_state;
 }
 
@@ -245,5 +247,5 @@
 mrdb_state_set(mrdb_state *mrdb)
 {
-	_mrdb_state = mrdb;
+  _mrdb_state = mrdb;
 }
 
@@ -251,10 +253,10 @@
 mrdb_state_free(mrb_state *mrb)
 {
-	mrb_debug_context_free(mrb);
-	if (_mrdb_state) {
-		mrb_free(mrb, _mrdb_state->command);
-		mrb_free(mrb, _mrdb_state);
-		_mrdb_state = NULL;
-	}
+  mrb_debug_context_free(mrb);
+  if (_mrdb_state) {
+    mrb_free(mrb, _mrdb_state->command);
+    mrb_free(mrb, _mrdb_state);
+    _mrdb_state = NULL;
+  }
 }
 
@@ -262,29 +264,29 @@
 get_command(mrb_state *mrb, mrdb_state *mrdb)
 {
-	int i;
-	int c;
-
-	for (i = 0; i < MAX_COMMAND_LINE; i++) {
-		if ((c = getchar()) == EOF || c == '\n') break;
-		mrdb->command[i] = c;
-	}
-
-	if (i == 0 && feof(stdin)) {
-		clearerr(stdin);
-		strlcpy(mrdb->command, "quit", MAX_COMMAND_LINE + 1);
-		i += sizeof("quit") - 1;
-	}
-
-	if (i == MAX_COMMAND_LINE) {
-		for (; (c = getchar()) != EOF && c != '\n'; i++);
-	}
-
-	if (i > MAX_COMMAND_LINE) {
-		printf("command line too long.\n");
-		i = 0; /* discard command data */
-	}
-	mrdb->command[i] = '\0';
-
-	return mrdb->command;
+  int i;
+  int c;
+
+  for (i=0; i<MAX_COMMAND_LINE; i++) {
+    if ((c=getchar()) == EOF || c == '\n') break;
+    mrdb->command[i] = c;
+  }
+
+  if (i == 0 && feof(stdin)) {
+    clearerr(stdin);
+    strcpy(mrdb->command, "quit");
+    i += sizeof("quit") - 1;
+  }
+
+  if (i == MAX_COMMAND_LINE) {
+    for ( ; (c=getchar()) != EOF && c !='\n'; i++) ;
+  }
+
+  if (i > MAX_COMMAND_LINE) {
+    printf("command line too long.\n");
+    i = 0; /* discard command data */
+  }
+  mrdb->command[i] = '\0';
+
+  return mrdb->command;
 }
 
@@ -292,29 +294,29 @@
 pick_out_word(mrb_state *mrb, char **pp)
 {
-	char *ps;
-
-	for (ps = *pp; ISBLANK(*ps); ps++);
-	if (*ps == '\0') {
-		return NULL;
-	}
-
-	if (*ps == '\"' || *ps == '\'') {
-		*pp = strchr(ps + 1, *ps);
-		if (*pp) (*pp)++;
-	}
-	else {
-		*pp = strpbrk(ps, " \t");
-	}
-
-	if (!*pp) {
-		*pp = ps + strlen(ps);
-	}
-
-	if (**pp != '\0') {
-		**pp = '\0';
-		(*pp)++;
-	}
-
-	return ps;
+  char *ps;
+
+  for (ps=*pp; ISBLANK(*ps); ps++) ;
+  if (*ps == '\0') {
+    return NULL;
+  }
+
+  if (*ps == '\"' || *ps == '\'') {
+    *pp = strchr(ps+1, *ps);
+    if (*pp) (*pp)++;
+  }
+  else {
+    *pp = strpbrk(ps, " \t");
+  }
+
+  if (!*pp) {
+    *pp = ps + strlen(ps);
+  }
+
+  if (**pp != '\0') {
+    **pp = '\0';
+    (*pp)++;
+  }
+
+  return ps;
 }
 
@@ -322,74 +324,74 @@
 parse_command(mrb_state *mrb, mrdb_state *mrdb, char *buf)
 {
-	debug_command *cmd = NULL;
-	char *p = buf;
-	size_t wlen;
-
-	/* get word #1 */
-	mrdb->words[0] = pick_out_word(mrb, &p);
-	if (!mrdb->words[0]) {
-		return NULL;
-	}
-	mrdb->wcnt = 1;
-	/* set remain parameter */
-	for (; *p && ISBLANK(*p); p++);
-	if (*p) {
-		mrdb->words[mrdb->wcnt++] = p;
-	}
-
-	/* check word #1 */
-	for (cmd = (debug_command*)debug_command_list; cmd->cmd1; cmd++) {
-		wlen = strlen(mrdb->words[0]);
-		if (wlen >= cmd->len1 &&
-			strncmp(mrdb->words[0], cmd->cmd1, wlen) == 0) {
-			break;
-		}
-	}
-
-	if (cmd->cmd2) {
-		if (mrdb->wcnt > 1) {
-			/* get word #2 */
-			mrdb->words[1] = pick_out_word(mrb, &p);
-			if (mrdb->words[1]) {
-				/* update remain parameter */
-				for (; *p && ISBLANK(*p); p++);
-				if (*p) {
-					mrdb->words[mrdb->wcnt++] = p;
-				}
-			}
-		}
-
-		/* check word #1,#2 */
-		for (; cmd->cmd1; cmd++) {
-			wlen = strlen(mrdb->words[0]);
-			if (wlen < cmd->len1 ||
-				strncmp(mrdb->words[0], cmd->cmd1, wlen)) {
-				continue;
-			}
-
-			if (!cmd->cmd2) break;          /* word #1 only */
-
-			if (mrdb->wcnt == 1) continue;  /* word #2 not specified */
-
-			wlen = strlen(mrdb->words[1]);
-			if (wlen >= cmd->len2 &&
-				strncmp(mrdb->words[1], cmd->cmd2, wlen) == 0) {
-				break;  /* word #1 and #2 */
-			}
-		}
-	}
-
-	/* divide remain parameters */
-	if (cmd->cmd1 && cmd->div) {
-		p = mrdb->words[--mrdb->wcnt];
-		for (; mrdb->wcnt < MAX_COMMAND_WORD; mrdb->wcnt++) {
-			mrdb->words[mrdb->wcnt] = pick_out_word(mrb, &p);
-			if (!mrdb->words[mrdb->wcnt]) {
-				break;
-			}
-		}
-	}
-
-	return cmd->cmd1 ? cmd : NULL;
+  debug_command *cmd = NULL;
+  char *p = buf;
+  size_t wlen;
+
+  /* get word #1 */
+  mrdb->words[0] = pick_out_word(mrb, &p);
+  if (!mrdb->words[0]) {
+    return NULL;
+  }
+  mrdb->wcnt = 1;
+  /* set remain parameter */
+  for ( ; *p && ISBLANK(*p); p++) ;
+  if (*p) {
+    mrdb->words[mrdb->wcnt++] = p;
+  }
+
+  /* check word #1 */
+  for (cmd=(debug_command*)debug_command_list; cmd->cmd1; cmd++) {
+    wlen = strlen(mrdb->words[0]);
+    if (wlen >= cmd->len1 &&
+        strncmp(mrdb->words[0], cmd->cmd1, wlen) == 0) {
+      break;
+    }
+  }
+
+  if (cmd->cmd2) {
+    if (mrdb->wcnt > 1) {
+      /* get word #2 */
+      mrdb->words[1] = pick_out_word(mrb, &p);
+      if (mrdb->words[1]) {
+        /* update remain parameter */
+        for ( ; *p && ISBLANK(*p); p++) ;
+        if (*p) {
+          mrdb->words[mrdb->wcnt++] = p;
+        }
+      }
+    }
+
+    /* check word #1,#2 */
+    for ( ; cmd->cmd1; cmd++) {
+      wlen = strlen(mrdb->words[0]);
+      if (wlen < cmd->len1 ||
+          strncmp(mrdb->words[0], cmd->cmd1, wlen)) {
+        continue;
+      }
+
+      if (!cmd->cmd2) break;          /* word #1 only */
+
+      if (mrdb->wcnt == 1) continue;  /* word #2 not specified */
+
+      wlen = strlen(mrdb->words[1]);
+      if (wlen >= cmd->len2 &&
+          strncmp(mrdb->words[1], cmd->cmd2, wlen) == 0) {
+        break;  /* word #1 and #2 */
+      }
+    }
+  }
+
+  /* divide remain parameters */
+  if (cmd->cmd1 && cmd->div) {
+    p = mrdb->words[--mrdb->wcnt];
+    for ( ; mrdb->wcnt<MAX_COMMAND_WORD; mrdb->wcnt++) {
+      mrdb->words[mrdb->wcnt] = pick_out_word(mrb, &p);
+      if (!mrdb->words[mrdb->wcnt]) {
+        break;
+      }
+    }
+  }
+
+  return cmd->cmd1 ? cmd : NULL;
 }
 
@@ -397,36 +399,36 @@
 print_info_stopped_break(mrb_state *mrb, mrdb_state *mrdb)
 {
-	mrb_debug_breakpoint bp;
-	int32_t ret;
-	uint16_t lineno;
-	const char *file;
-	const char *method_name;
-	const char *class_name;
-
-	ret = mrb_debug_get_break(mrb, mrdb->dbg, mrdb->dbg->stopped_bpno, &bp);
-	if (ret == 0) {
-		switch (bp.type) {
-		case MRB_DEBUG_BPTYPE_LINE:
-			file = bp.point.linepoint.file;
-			lineno = bp.point.linepoint.lineno;
-			printf("Breakpoint %d, at %s:%d\n", bp.bpno, file, lineno);
-			break;
-		case MRB_DEBUG_BPTYPE_METHOD:
-			method_name = bp.point.methodpoint.method_name;
-			class_name = bp.point.methodpoint.class_name;
-			if (class_name == NULL) {
-				printf("Breakpoint %d, %s\n", bp.bpno, method_name);
-			}
-			else {
-				printf("Breakpoint %d, %s:%s\n", bp.bpno, class_name, method_name);
-			}
-			if (mrdb->dbg->isCfunc) {
-				printf("Stopped before calling the C function.\n");
-			}
-			break;
-		default:
-			break;
-		}
-	}
+  mrb_debug_breakpoint bp;
+  int32_t ret;
+  uint16_t lineno;
+  const char *file;
+  const char *method_name;
+  const char *class_name;
+
+  ret = mrb_debug_get_break(mrb, mrdb->dbg, mrdb->dbg->stopped_bpno, &bp);
+  if (ret == 0) {
+    switch(bp.type) {
+      case MRB_DEBUG_BPTYPE_LINE:
+        file = bp.point.linepoint.file;
+        lineno = bp.point.linepoint.lineno;
+        printf("Breakpoint %d, at %s:%d\n", bp.bpno, file, lineno);
+        break;
+      case MRB_DEBUG_BPTYPE_METHOD:
+        method_name = bp.point.methodpoint.method_name;
+        class_name = bp.point.methodpoint.class_name;
+        if (class_name == NULL) {
+          printf("Breakpoint %d, %s\n", bp.bpno, method_name);
+        }
+        else {
+          printf("Breakpoint %d, %s:%s\n", bp.bpno, class_name, method_name);
+        }
+        if (mrdb->dbg->isCfunc) {
+          printf("Stopped before calling the C function.\n");
+        }
+        break;
+      default:
+        break;
+    }
+  }
 }
 
@@ -434,7 +436,18 @@
 print_info_stopped_step_next(mrb_state *mrb, mrdb_state *mrdb)
 {
-	const char* file = mrdb->dbg->prvfile;
-	uint16_t lineno = mrdb->dbg->prvline;
-	printf("%s:%d\n", file, lineno);
+  const char* file = mrdb->dbg->prvfile;
+  uint16_t lineno = mrdb->dbg->prvline;
+  printf("%s:%d\n", file, lineno);
+}
+
+static void
+print_info_stopped_code(mrb_state *mrb, mrdb_state *mrdb)
+{
+  char* file = mrb_debug_get_source(mrb, mrdb, mrdb->srcpath, mrdb->dbg->prvfile);
+  uint16_t lineno = mrdb->dbg->prvline;
+  if (file != NULL) {
+    mrb_debug_list(mrb, mrdb->dbg, file, lineno, lineno);
+    mrb_free(mrb, file);
+  }
 }
 
@@ -442,15 +455,17 @@
 print_info_stopped(mrb_state *mrb, mrdb_state *mrdb)
 {
-	switch (mrdb->dbg->bm) {
-	case BRK_BREAK:
-		print_info_stopped_break(mrb, mrdb);
-		break;
-	case BRK_STEP:
-	case BRK_NEXT:
-		print_info_stopped_step_next(mrb, mrdb);
-		break;
-	default:
-		break;
-	}
+  switch(mrdb->dbg->bm) {
+    case BRK_BREAK:
+      print_info_stopped_break(mrb, mrdb);
+      print_info_stopped_code(mrb, mrdb);
+      break;
+    case BRK_STEP:
+    case BRK_NEXT:
+      print_info_stopped_step_next(mrb, mrdb);
+      print_info_stopped_code(mrb, mrdb);
+      break;
+    default:
+      break;
+  }
 }
 
@@ -458,161 +473,163 @@
 get_and_parse_command(mrb_state *mrb, mrdb_state *mrdb)
 {
-	debug_command *cmd = NULL;
-	char *p;
-	int i;
-
-	while (!cmd) {
-		for (p = NULL; !p || *p == '\0'; ) {
-			printf("(%s:%d) ", mrdb->dbg->prvfile, mrdb->dbg->prvline);
-			fflush(stdout);
-			p = get_command(mrb, mrdb);
-		}
-
-		cmd = parse_command(mrb, mrdb, p);
+  debug_command *cmd = NULL;
+  char *p;
+  int i;
+
+  while (!cmd) {
+    for (p=NULL; !p || *p=='\0'; ) {
+      printf("(%s:%d) ", mrdb->dbg->prvfile, mrdb->dbg->prvline);
+      fflush(stdout);
+      p = get_command(mrb, mrdb);
+    }
+
+    cmd = parse_command(mrb, mrdb, p);
 #ifdef _DBG_MRDB_PARSER_
-		for (i = 0; i < mrdb->wcnt; i++) {
-			printf("%d: %s\n", i, mrdb->words[i]);
-		}
+    for (i=0; i<mrdb->wcnt; i++) {
+      printf("%d: %s\n", i, mrdb->words[i]);
+    }
 #endif
-		if (!cmd) {
-			printf("invalid command (");
-			for (i = 0; i < mrdb->wcnt; i++) {
-				if (i > 0) {
-					printf(" ");
-				}
-				printf("%s", mrdb->words[i]);
-			}
-			puts(")");
-		}
-	}
-	return cmd;
+    if (!cmd) {
+      printf("invalid command (");
+      for (i=0; i<mrdb->wcnt; i++) {
+        if (i>0) {
+          printf(" ");
+        }
+        printf("%s", mrdb->words[i]);
+      }
+      puts(")");
+    }
+  }
+  return cmd;
 }
 
 static int32_t
-check_method_breakpoint(mrb_state *mrb, mrb_irep *irep, mrb_code *pc, mrb_value *regs)
-{
-	struct RClass* c = NULL;
-	mrb_sym sym;
-	int32_t bpno;
-	mrb_bool isCfunc;
-
-	mrb_debug_context *dbg = mrb_debug_context_get(mrb);
-
-	isCfunc = FALSE;
-	bpno = dbg->method_bpno;
-	dbg->method_bpno = 0;
-
-	switch (GET_OPCODE(*pc)) {
-	case OP_SEND:
-	case OP_SENDB:
-		c = mrb_class(mrb, regs[GETARG_A(*pc)]);
-		sym = irep->syms[GETARG_B(*pc)];
-		break;
-	case OP_SUPER:
-		c = mrb->c->ci->target_class->super;
-		sym = mrb->c->ci->mid;
-		break;
-	default:
-		sym = 0;
-		break;
-	}
-	if (sym != 0) {
-		dbg->method_bpno = mrb_debug_check_breakpoint_method(mrb, dbg, c, sym, &isCfunc);
-		if (isCfunc) {
-			bpno = dbg->method_bpno;
-			dbg->method_bpno = 0;
-		}
-	}
-	dbg->isCfunc = isCfunc;
-	return bpno;
+check_method_breakpoint(mrb_state *mrb, mrb_irep *irep, const mrb_code *pc, mrb_value *regs)
+{
+  struct RClass* c;
+  mrb_sym sym;
+  int32_t bpno;
+  mrb_bool isCfunc;
+  struct mrb_insn_data insn;
+
+  mrb_debug_context *dbg = mrb_debug_context_get(mrb);
+
+  isCfunc = FALSE;
+  bpno = dbg->method_bpno;
+  dbg->method_bpno = 0;
+
+  insn = mrb_decode_insn(pc);
+  switch(insn.insn) {
+    case OP_SEND:
+    case OP_SENDB:
+      c = mrb_class(mrb, regs[insn.a]);
+      sym = irep->syms[insn.b];
+      break;
+    case OP_SUPER:
+      c = mrb->c->ci->target_class->super;
+      sym = mrb->c->ci->mid;
+      break;
+    default:
+      sym = 0;
+      break;
+  }
+  if (sym != 0) {
+    dbg->method_bpno = mrb_debug_check_breakpoint_method(mrb, dbg, c, sym, &isCfunc);
+    if (isCfunc) {
+      bpno = dbg->method_bpno;
+      dbg->method_bpno = 0;
+    }
+  }
+  dbg->isCfunc = isCfunc;
+  return bpno;
 }
 
 static void
-mrb_code_fetch_hook(mrb_state *mrb, mrb_irep *irep, mrb_code *pc, mrb_value *regs)
-{
-	const char *file;
-	int32_t line;
-	int32_t bpno;
-
-	mrb_debug_context *dbg = mrb_debug_context_get(mrb);
-
-	mrb_assert(dbg);
-
-	dbg->irep = irep;
-	dbg->pc = pc;
-	dbg->regs = regs;
-
-	if (dbg->xphase == DBG_PHASE_RESTART) {
-		dbg->root_irep = irep;
-		dbg->prvfile = NULL;
-		dbg->prvline = 0;
-		dbg->prvci = NULL;
-		InterlockedExchange(&dbg->xm, DBG_RUN);
-		dbg->xphase = DBG_PHASE_RUNNING;
-	}
-
-	file = mrb_debug_get_filename(irep, (uint32_t)(pc - irep->iseq));
-	line = mrb_debug_get_line(irep, (uint32_t)(pc - irep->iseq));
-
-	switch (dbg->xm) {
-	case DBG_STEP:
-		if (!file || (dbg->prvfile == file && dbg->prvline == line)) {
-			return;
-		}
-		dbg->method_bpno = 0;
-		dbg->bm = BRK_STEP;
-		break;
-
-	case DBG_NEXT:
-		if (!file || (dbg->prvfile == file && dbg->prvline == line)) {
-			return;
-		}
-		if ((intptr_t)(dbg->prvci) < (intptr_t)(mrb->c->ci)) {
-			return;
-		}
-		dbg->prvci = NULL;
-		dbg->method_bpno = 0;
-		dbg->bm = BRK_NEXT;
-		break;
-
-	case DBG_RUN:
-		bpno = check_method_breakpoint(mrb, irep, pc, regs);
-		if (bpno > 0) {
-			dbg->stopped_bpno = bpno;
-			dbg->bm = BRK_BREAK;
-			break;
-		}
-		if (dbg->prvfile != file || dbg->prvline != line) {
-			bpno = mrb_debug_check_breakpoint_line(mrb, dbg, file, line);
-			if (bpno > 0) {
-				dbg->stopped_bpno = bpno;
-				dbg->bm = BRK_BREAK;
-				break;
-			}
-		}
-		dbg->prvfile = file;
-		dbg->prvline = line;
-		return;
-	case DBG_INIT:
-		dbg->root_irep = irep;
-		dbg->bm = BRK_INIT;
-		if (!file || line < 0) {
-			puts("Cannot get debugging information.");
-		}
-		break;
-
-	default:
-		return;
-	}
-
-	dbg->prvfile = file;
-	dbg->prvline = line;
-
-	if (dbg->bm == BRK_BREAK && --dbg->ccnt > 0) {
-		return;
-	}
-	dbg->break_hook(mrb, dbg);
-
-	dbg->xphase = DBG_PHASE_RUNNING;
+mrb_code_fetch_hook(mrb_state *mrb, mrb_irep *irep, const mrb_code *pc, mrb_value *regs)
+{
+  const char *file;
+  int32_t line;
+  int32_t bpno;
+
+  mrb_debug_context *dbg = mrb_debug_context_get(mrb);
+
+  mrb_assert(dbg);
+
+  dbg->irep = irep;
+  dbg->pc   = pc;
+  dbg->regs = regs;
+
+  if (dbg->xphase == DBG_PHASE_RESTART) {
+    dbg->root_irep = irep;
+    dbg->prvfile = NULL;
+    dbg->prvline = 0;
+    dbg->prvci = NULL;
+    dbg->xm = DBG_RUN;
+    dbg->xphase = DBG_PHASE_RUNNING;
+  }
+
+  file = mrb_debug_get_filename(mrb, irep, pc - irep->iseq);
+  line = mrb_debug_get_line(mrb, irep, pc - irep->iseq);
+
+  switch (dbg->xm) {
+  case DBG_STEP:
+    if (!file || (dbg->prvfile == file && dbg->prvline == line)) {
+      return;
+    }
+    dbg->method_bpno = 0;
+    dbg->bm = BRK_STEP;
+    break;
+
+  case DBG_NEXT:
+    if (!file || (dbg->prvfile == file && dbg->prvline == line)) {
+      return;
+    }
+    if ((intptr_t)(dbg->prvci) < (intptr_t)(mrb->c->ci)) {
+      return;
+    }
+    dbg->prvci = NULL;
+    dbg->method_bpno = 0;
+    dbg->bm = BRK_NEXT;
+    break;
+
+  case DBG_RUN:
+    bpno = check_method_breakpoint(mrb, irep, pc, regs);
+    if (bpno > 0) {
+      dbg->stopped_bpno = bpno;
+      dbg->bm = BRK_BREAK;
+      break;
+    }
+    if (dbg->prvfile != file || dbg->prvline != line) {
+      bpno = mrb_debug_check_breakpoint_line(mrb, dbg, file, line);
+      if (bpno > 0) {
+        dbg->stopped_bpno = bpno;
+        dbg->bm = BRK_BREAK;
+        break;
+      }
+    }
+    dbg->prvfile = file;
+    dbg->prvline = line;
+    return;
+  case DBG_INIT:
+    dbg->root_irep = irep;
+    dbg->bm = BRK_INIT;
+    if (!file || line < 0) {
+      puts("Cannot get debugging information.");
+    }
+    break;
+
+  default:
+    return;
+  }
+
+  dbg->prvfile = file;
+  dbg->prvline = line;
+
+  if (dbg->bm == BRK_BREAK && --dbg->ccnt > 0) {
+    return;
+  }
+  dbg->break_hook(mrb, dbg);
+
+  dbg->xphase = DBG_PHASE_RUNNING;
 }
 
@@ -620,19 +637,19 @@
 mrb_debug_break_hook(mrb_state *mrb, mrb_debug_context *dbg)
 {
-	debug_command *cmd;
-	dbgcmd_state st = DBGST_CONTINUE;
-	mrdb_state *mrdb = mrdb_state_get(mrb);
-
-	print_info_stopped(mrb, mrdb);
-
-	while (1) {
-		cmd = get_and_parse_command(mrb, mrdb);
-		mrb_assert(cmd);
-
-		st = cmd->func(mrb, mrdb);
-
-		if ((st == DBGST_CONTINUE) || (st == DBGST_RESTART)) break;
-	}
-	return dbg->xm;
+  debug_command *cmd;
+  dbgcmd_state st = DBGST_CONTINUE;
+  mrdb_state *mrdb = mrdb_state_get(mrb);
+
+  print_info_stopped(mrb, mrdb);
+
+  while (1) {
+    cmd = get_and_parse_command(mrb, mrdb);
+    mrb_assert(cmd);
+
+    st = cmd->func(mrb, mrdb);
+
+    if ((st == DBGST_CONTINUE) || (st == DBGST_RESTART)) break;
+  }
+  return dbg->xm;
 }
 
@@ -643,5 +660,8 @@
 	if (mrdb == NULL)
 		return 0;
-	return InterlockedCompareExchange(&mrdb->dbg->xm, DBG_STEP, DBG_RUN) == DBG_RUN;
+	long prev = mrdb->dbg->xm;
+	if (mrdb->dbg->xm == DBG_RUN)
+		mrdb->dbg->xm = DBG_STEP;
+	return prev == DBG_RUN;
 }
 
@@ -649,105 +669,105 @@
 mrdb_main(int argc, char **argv)
 {
-	mrb_state *mrb = mrb_open();
-	int n = -1;
-	struct _args args;
-	mrb_value v;
-	mrdb_state *mrdb;
-	mrdb_state *mrdb_backup;
-	mrb_debug_context* dbg_backup;
-	debug_command *cmd;
-
-l_restart:
-
-	if (mrb == NULL) {
-		fputs("Invalid mrb_state, exiting mruby\n", stderr);
-		return EXIT_FAILURE;
-	}
-
-	/* parse command parameters */
-	n = parse_args(mrb, argc, argv, &args);
-	if (n == EXIT_FAILURE || args.rfp == NULL) {
-		cleanup(mrb, &args);
-		usage(argv[0]);
-		return n;
-	}
-
-	/* initialize debugger information */
-	mrdb = mrdb_state_get(mrb);
-	mrb_assert(mrdb && mrdb->dbg);
-	mrdb->srcpath = args.srcpath;
-
-	if (mrdb->dbg->xm == DBG_QUIT) {
-		mrdb->dbg->xphase = DBG_PHASE_RESTART;
-	}
-	else {
-		mrdb->dbg->xphase = DBG_PHASE_BEFORE_RUN;
-	}
-	InterlockedExchange(&mrdb->dbg->xm, DBG_INIT);
-	mrdb->dbg->ccnt = 1;
-
-	/* setup hook functions */
-	mrb->code_fetch_hook = mrb_code_fetch_hook;
-	mrdb->dbg->break_hook = mrb_debug_break_hook;
-
-	if (args.mrbfile) { /* .mrb */
-		v = mrb_load_irep_file(mrb, args.rfp);
-	}
-	else {              /* .rb */
-		mrbc_context *cc = mrbc_context_new(mrb);
-		mrbc_filename(mrb, cc, args.fname);
-		v = mrb_load_file_cxt(mrb, args.rfp, cc);
-		mrbc_context_free(mrb, cc);
-	}
-	if (mrdb->dbg->xm == DBG_QUIT && !mrb_undef_p(v) && mrb->exc) {
-		const char *classname = mrb_obj_classname(mrb, mrb_obj_value(mrb->exc));
-		if (!strcmp(classname, "DebuggerExit")) {
-			cleanup(mrb, &args);
-			return 0;
-		}
-		if (!strcmp(classname, "DebuggerRestart")) {
-			mrdb_backup = mrdb_state_get(mrb);
-			dbg_backup = mrb_debug_context_get(mrb);
-
-			mrdb_state_set(NULL);
-			mrb_debug_context_set(NULL);
-
-			cleanup(mrb, &args);
-			mrb = mrb_open();
-
-			mrdb_state_set(mrdb_backup);
-			mrb_debug_context_set(dbg_backup);
-
-			goto l_restart;
-		}
-	}
-	puts("mruby application exited.");
-	mrdb->dbg->xphase = DBG_PHASE_AFTER_RUN;
-	if (!mrb_undef_p(v)) {
-		if (mrb->exc) {
-			mrb_print_error(mrb);
-		}
-		else {
-			printf(" => ");
-			mrb_p(mrb, v);
-		}
-	}
-
-	mrdb->dbg->prvfile = "-";
-	mrdb->dbg->prvline = 0;
-
-	while (1) {
-		cmd = get_and_parse_command(mrb, mrdb);
-		mrb_assert(cmd);
-
-		if (cmd->id == DBGCMD_QUIT) {
-			break;
-		}
-
-		if (cmd->func(mrb, mrdb) == DBGST_RESTART) goto l_restart;
-	}
-
-	cleanup(mrb, &args);
-
-	return 0;
-}
+  mrb_state *mrb = mrb_open();
+  int n = -1;
+  struct _args args;
+  mrb_value v;
+  mrdb_state *mrdb;
+  mrdb_state *mrdb_backup;
+  mrb_debug_context* dbg_backup;
+  debug_command *cmd;
+
+ l_restart:
+
+  if (mrb == NULL) {
+    fputs("Invalid mrb_state, exiting mruby\n", stderr);
+    return EXIT_FAILURE;
+  }
+
+  /* parse command parameters */
+  n = parse_args(mrb, argc, argv, &args);
+  if (n == EXIT_FAILURE || args.rfp == NULL) {
+    cleanup(mrb, &args);
+    usage(argv[0]);
+    return n;
+  }
+
+  /* initialize debugger information */
+  mrdb = mrdb_state_get(mrb);
+  mrb_assert(mrdb && mrdb->dbg);
+  mrdb->srcpath = args.srcpath;
+
+  if (mrdb->dbg->xm == DBG_QUIT) {
+    mrdb->dbg->xphase = DBG_PHASE_RESTART;
+  }
+  else {
+    mrdb->dbg->xphase = DBG_PHASE_BEFORE_RUN;
+  }
+  mrdb->dbg->xm = DBG_INIT;
+  mrdb->dbg->ccnt = 1;
+
+  /* setup hook functions */
+  mrb->code_fetch_hook = mrb_code_fetch_hook;
+  mrdb->dbg->break_hook = mrb_debug_break_hook;
+
+  if (args.mrbfile) { /* .mrb */
+    v = mrb_load_irep_file(mrb, args.rfp);
+  }
+  else {              /* .rb */
+    mrbc_context *cc = mrbc_context_new(mrb);
+    mrbc_filename(mrb, cc, args.fname);
+    v = mrb_load_file_cxt(mrb, args.rfp, cc);
+    mrbc_context_free(mrb, cc);
+  }
+  if (mrdb->dbg->xm == DBG_QUIT && !mrb_undef_p(v) && mrb->exc) {
+    const char *classname = mrb_obj_classname(mrb, mrb_obj_value(mrb->exc));
+    if (!strcmp(classname, "DebuggerExit")) {
+      cleanup(mrb, &args);
+      return 0;
+    }
+    if (!strcmp(classname, "DebuggerRestart")) {
+      mrdb_backup = mrdb_state_get(mrb);
+      dbg_backup = mrb_debug_context_get(mrb);
+
+      mrdb_state_set(NULL);
+      mrb_debug_context_set(NULL);
+
+      cleanup(mrb, &args);
+      mrb = mrb_open();
+
+      mrdb_state_set(mrdb_backup);
+      mrb_debug_context_set(dbg_backup);
+
+      goto l_restart;
+    }
+  }
+  puts("mruby application exited.");
+  mrdb->dbg->xphase = DBG_PHASE_AFTER_RUN;
+  if (!mrb_undef_p(v)) {
+    if (mrb->exc) {
+      mrb_print_error(mrb);
+    }
+    else {
+      printf(" => ");
+      mrb_p(mrb, v);
+    }
+  }
+
+  mrdb->dbg->prvfile = "-";
+  mrdb->dbg->prvline = 0;
+
+  while (1) {
+    cmd = get_and_parse_command(mrb, mrdb);
+    mrb_assert(cmd);
+
+    if (cmd->id == DBGCMD_QUIT) {
+      break;
+    }
+
+    if ( cmd->func(mrb, mrdb) == DBGST_RESTART ) goto l_restart;
+  }
+
+  cleanup(mrb, &args);
+
+  return 0;
+}
Index: EcnlProtoTool/trunk/prototool/src/mrdb.h
===================================================================
--- EcnlProtoTool/trunk/prototool/src/mrdb.h	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/mrdb.h	(revision 439)
@@ -7,7 +7,11 @@
 #define MRDB_H
 
-#include "mruby.h"
+#include <mruby.h>
 
 #include "mrdbconf.h"
+
+#ifdef _MSC_VER
+# define __func__ __FUNCTION__
+#endif
 
 #define MAX_COMMAND_WORD (16)
@@ -20,4 +24,5 @@
   DBGCMD_BREAK,
   DBGCMD_INFO_BREAK,
+  DBGCMD_INFO_LOCAL,
   DBGCMD_WATCH,
   DBGCMD_INFO_WATCH,
@@ -52,13 +57,4 @@
   DBG_QUIT,
 } mrdb_exemode;
-
-#ifdef _MSC_VER
-# define __func__ __FUNCTION__
-extern long __stdcall InterlockedExchange(long *dst, long src);
-extern long __stdcall InterlockedCompareExchange(long *dst, long src, long cmp);
-#else
-#define InterlockedExchange(dst, src) do { *(dst) = src; } while(0)
-__inline__ static enum mrdb_exemode InterlockedCompareExchange(enum mrdb_exemode *dst, enum mrdb_exemode src, enum mrdb_exemode cmp) { long prev = *(dst); if (*(dst) == cmp) *(dst) = src; return prev; }
-#endif
 
 typedef enum mrdb_exephase {
@@ -110,5 +106,5 @@
   struct mrb_irep *root_irep;
   struct mrb_irep *irep;
-  mrb_code *pc;
+  const mrb_code *pc;
   mrb_value *regs;
 
@@ -117,9 +113,5 @@
   mrb_callinfo *prvci;
 
-#ifdef _MSC_VER
-  long xm;
-#else
   mrdb_exemode xm;
-#endif
   mrdb_exephase xphase;
   mrdb_brkmode bm;
@@ -161,4 +153,5 @@
 dbgcmd_state dbgcmd_break(mrb_state*, mrdb_state*);
 dbgcmd_state dbgcmd_info_break(mrb_state*, mrdb_state*);
+dbgcmd_state dbgcmd_info_local(mrb_state*, mrdb_state*);
 dbgcmd_state dbgcmd_delete(mrb_state*, mrdb_state*);
 dbgcmd_state dbgcmd_enable(mrb_state*, mrdb_state*);
@@ -168,4 +161,5 @@
 dbgcmd_state dbgcmd_eval(mrb_state*, mrdb_state*);
 /* cmdmisc.c */
+dbgcmd_state dbgcmd_list(mrb_state*, mrdb_state*);
 dbgcmd_state dbgcmd_help(mrb_state*, mrdb_state*);
 dbgcmd_state dbgcmd_quit(mrb_state*, mrdb_state*);
Index: EcnlProtoTool/trunk/prototool/src/mrdbconf.h
===================================================================
--- EcnlProtoTool/trunk/prototool/src/mrdbconf.h	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/mrdbconf.h	(revision 439)
@@ -6,4 +6,12 @@
 #ifndef MRDBCONF_H
 #define MRDBCONF_H
+
+#ifndef MRB_ENABLE_DEBUG_HOOK
+# error mruby-bin-debugger need 'MRB_ENABLE_DEBUG_HOOK' configuration in your 'build_config.rb'
+#endif
+
+#ifdef MRB_DISABLE_STDIO
+# error mruby-bin-debugger conflicts 'MRB_DISABLE_STDIO' configuration in your 'build_config.rb'
+#endif
 
 /* configuration options: */
Index: EcnlProtoTool/trunk/prototool/src/mruby.c
===================================================================
--- EcnlProtoTool/trunk/prototool/src/mruby.c	(revision 438)
+++ EcnlProtoTool/trunk/prototool/src/mruby.c	(revision 439)
@@ -1,34 +1,36 @@
+#include <mruby.h>
+
+#ifdef MRB_DISABLE_STDIO
+# error mruby-bin-mruby conflicts 'MRB_DISABLE_STDIO' configuration in your 'build_config.rb'
+#endif
+
 #include <stdlib.h>
 #include <string.h>
-
-#include "mruby.h"
-#include "mruby/array.h"
-#include "mruby/compile.h"
-#include "mruby/dump.h"
-#include "mruby/variable.h"
-
-#ifdef MRB_DISABLE_STDIO
-static void
-p(mrb_state *mrb, mrb_value obj)
-{
-	mrb_value val = mrb_inspect(mrb, obj);
-
-	fwrite(RSTRING_PTR(val), RSTRING_LEN(val), 1, stdout);
-	putc('\n', stdout);
-}
-#else
-#define p(mrb,obj) mrb_p(mrb,obj)
-#endif
-
-struct _args
-{
-	FILE *rfp;
-	char* cmdline;
-	mrb_bool fname : 1;
-	mrb_bool mrbfile : 1;
-	mrb_bool check_syntax : 1;
-	mrb_bool verbose : 1;
-	int argc;
-	char** argv;
+#include <mruby/array.h>
+#include <mruby/compile.h>
+#include <mruby/dump.h>
+#include <mruby/variable.h>
+
+struct _args {
+  FILE *rfp;
+  char *cmdline;
+  mrb_bool fname        : 1;
+  mrb_bool mrbfile      : 1;
+  mrb_bool check_syntax : 1;
+  mrb_bool verbose      : 1;
+  mrb_bool version      : 1;
+  mrb_bool debug        : 1;
+  int argc;
+  char **argv;
+  int libc;
+  char **libv;
+};
+
+struct options {
+  int argc;
+  char **argv;
+  char *program;
+  char *opt;
+  char short_opt[2];
 };
 
@@ -36,20 +38,89 @@
 usage(const char *name)
 {
-	static const char *const usage_msg[] = {
-		"switches:",
-		"-b           load and execute RiteBinary (mrb) file",
-		"-c           check syntax only",
-		"-e 'command' one line of script",
-		"-v           print version number, then run in verbose mode",
-		"--verbose    run in verbose mode",
-		"--version    print the version",
-		"--copyright  print the copyright",
-		NULL
-	};
-	const char *const *p = usage_msg;
-
-	printf("Usage: %s [switches] programfile\n", name);
-	while (*p)
-		printf("  %s\n", *p++);
+  static const char *const usage_msg[] = {
+  "switches:",
+  "-b           load and execute RiteBinary (mrb) file",
+  "-c           check syntax only",
+  "-d           set debugging flags (set $DEBUG to true)",
+  "-e 'command' one line of script",
+  "-r library   load the library before executing your script",
+  "-v           print version number, then run in verbose mode",
+  "--verbose    run in verbose mode",
+  "--version    print the version",
+  "--copyright  print the copyright",
+  NULL
+  };
+  const char *const *p = usage_msg;
+
+  printf("Usage: %s [switches] [programfile] [arguments]\n", name);
+  while (*p)
+    printf("  %s\n", *p++);
+}
+
+static void
+options_init(struct options *opts, int argc, char **argv)
+{
+  opts->argc = argc;
+  opts->argv = argv;
+  opts->program = *argv;
+  *opts->short_opt = 0;
+}
+
+static const char *
+options_opt(struct options *opts)
+{
+  /* concatenated short options (e.g. `-cv`) */
+  if (*opts->short_opt && *++opts->opt) {
+   short_opt:
+    opts->short_opt[0] = *opts->opt;
+    opts->short_opt[1] = 0;
+    return opts->short_opt;
+  }
+
+  while (++opts->argv, --opts->argc) {
+    opts->opt = *opts->argv;
+
+    /*  empty         || not start with `-`  || `-` */
+    if (!opts->opt[0] || opts->opt[0] != '-' || !opts->opt[1]) return NULL;
+
+    if (opts->opt[1] == '-') {
+      /* `--` */
+      if (!opts->opt[2]) {
+        ++opts->argv, --opts->argc;
+        return NULL;
+      }
+      /* long option */
+      opts->opt += 2;
+      *opts->short_opt = 0;
+      return opts->opt;
+    }
+    else {
+      /* short option */
+      ++opts->opt;
+      goto short_opt;
+    }
+  }
+  return NULL;
+}
+
+static const char *
+options_arg(struct options *opts)
+{
+  if (*opts->short_opt && opts->opt[1]) {
+    /* concatenated short option and option argument (e.g. `-rLIBRARY`) */
+    *opts->short_opt = 0;
+    return opts->opt + 1;
+  }
+  --opts->argc, ++opts->argv;
+  return opts->argc ? *opts->argv : NULL;
+}
+
+static char *
+dup_arg_item(mrb_state *mrb, const char *item)
+{
+  size_t buflen = strlen(item) + 1;
+  char *buf = (char*)mrb_malloc(mrb, buflen);
+  memcpy(buf, item, buflen);
+  return buf;
 }
 
@@ -57,101 +128,109 @@
 parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
 {
-	char **origargv = argv;
-	static const struct _args args_zero = { 0 };
-
-	*args = args_zero;
-
-	for (argc--, argv++; argc > 0; argc--, argv++) {
-		char *item;
-		if (argv[0][0] != '-') break;
-
-		if (strlen(*argv) <= 1) {
-			argc--; argv++;
-			args->rfp = stdin;
-			break;
-		}
-
-		item = argv[0] + 1;
-		switch (*item++) {
-		case 'b':
-			args->mrbfile = TRUE;
-			break;
-		case 'c':
-			args->check_syntax = TRUE;
-			break;
-		case 'e':
-			if (item[0]) {
-				goto append_cmdline;
-			}
-			else if (argc > 1) {
-				argc--; argv++;
-				item = argv[0];
-			append_cmdline:
-				if (!args->cmdline) {
-					size_t buflen;
-					char *buf;
-
-					buflen = strlen(item) + 1;
-					buf = (char *)mrb_malloc(mrb, buflen);
-					memcpy(buf, item, buflen);
-					args->cmdline = buf;
-				}
-				else {
-					size_t cmdlinelen;
-					size_t itemlen;
-
-					cmdlinelen = strlen(args->cmdline);
-					itemlen = strlen(item);
-					args->cmdline =
-						(char *)mrb_realloc(mrb, args->cmdline, cmdlinelen + itemlen + 2);
-					args->cmdline[cmdlinelen] = '\n';
-					memcpy(args->cmdline + cmdlinelen + 1, item, itemlen + 1);
-				}
-			}
-			else {
-				printf("%s: No code specified for -e\n", *origargv);
-				return EXIT_SUCCESS;
-			}
-			break;
-		case 'v':
-			if (!args->verbose) mrb_show_version(mrb);
-			args->verbose = TRUE;
-			break;
-		case '-':
-			if (strcmp((*argv) + 2, "version") == 0) {
-				mrb_show_version(mrb);
-				exit(EXIT_SUCCESS);
-			}
-			else if (strcmp((*argv) + 2, "verbose") == 0) {
-				args->verbose = TRUE;
-				break;
-			}
-			else if (strcmp((*argv) + 2, "copyright") == 0) {
-				mrb_show_copyright(mrb);
-				exit(EXIT_SUCCESS);
-			}
-		default:
-			return EXIT_FAILURE;
-		}
-	}
-
-	if (args->rfp == NULL && args->cmdline == NULL) {
-		if (*argv == NULL) args->rfp = stdin;
-		else {
-			args->rfp = fopen(argv[0], args->mrbfile ? "rb" : "r");
-			if (args->rfp == NULL) {
-				printf("%s: Cannot open program file. (%s)\n", *origargv, *argv);
-				return EXIT_FAILURE;
-			}
-			args->fname = TRUE;
-			args->cmdline = argv[0];
-			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;
+  static const struct _args args_zero = { 0 };
+  struct options opts[1];
+  const char *opt, *item;
+
+  *args = args_zero;
+  options_init(opts, argc, argv);
+  while ((opt = options_opt(opts))) {
+    if (strcmp(opt, "b") == 0) {
+      args->mrbfile = TRUE;
+    }
+    else if (strcmp(opt, "c") == 0) {
+      args->check_syntax = TRUE;
+    }
+    else if (strcmp(opt, "d") == 0) {
+      args->debug = TRUE;
+    }
+    else if (strcmp(opt, "e") == 0) {
+      if ((item = options_arg(opts))) {
+        if (!args->cmdline) {
+          args->cmdline = dup_arg_item(mrb, item);
+        }
+        else {
+          size_t cmdlinelen;
+          size_t itemlen;
+
+          cmdlinelen = strlen(args->cmdline);
+          itemlen = strlen(item);
+          args->cmdline =
+            (char *)mrb_realloc(mrb, args->cmdline, cmdlinelen + itemlen + 2);
+          args->cmdline[cmdlinelen] = '\n';
+          memcpy(args->cmdline + cmdlinelen + 1, item, itemlen + 1);
+        }
+      }
+      else {
+        fprintf(stderr, "%s: No code specified for -e\n", opts->program);
+        return EXIT_FAILURE;
+      }
+    }
+    else if (strcmp(opt, "h") == 0) {
+      usage(opts->program);
+      exit(EXIT_SUCCESS);
+    }
+    else if (strcmp(opt, "r") == 0) {
+      if ((item = options_arg(opts))) {
+        if (args->libc == 0) {
+          args->libv = (char**)mrb_malloc(mrb, sizeof(char*));
+        }
+        else {
+          args->libv = (char**)mrb_realloc(mrb, args->libv, sizeof(char*) * (args->libc + 1));
+        }
+        args->libv[args->libc++] = dup_arg_item(mrb, item);
+      }
+      else {
+        fprintf(stderr, "%s: No library specified for -r\n", opts->program);
+        return EXIT_FAILURE;
+      }
+    }
+    else if (strcmp(opt, "v") == 0) {
+      if (!args->verbose) {
+        mrb_show_version(mrb);
+        args->version = TRUE;
+      }
+      args->verbose = TRUE;
+    }
+    else if (strcmp(opt, "version") == 0) {
+      mrb_show_version(mrb);
+      exit(EXIT_SUCCESS);
+    }
+    else if (strcmp(opt, "verbose") == 0) {
+      args->verbose = TRUE;
+    }
+    else if (strcmp(opt, "copyright") == 0) {
+      mrb_show_copyright(mrb);
+      exit(EXIT_SUCCESS);
+    }
+    else {
+      fprintf(stderr, "%s: invalid option %s%s (-h will show valid options)\n",
+              opts->program, opt[1] ? "--" : "-", opt);
+      return EXIT_FAILURE;
+    }
+  }
+
+  argc = opts->argc; argv = opts->argv;
+  if (args->cmdline == NULL) {
+    if (*argv == NULL) {
+      if (args->version) exit(EXIT_SUCCESS);
+      args->rfp = stdin;
+    }
+    else {
+      args->rfp = strcmp(argv[0], "-") == 0 ?
+        stdin : fopen(argv[0], args->mrbfile ? "rb" : "r");
+      if (args->rfp == NULL) {
+        fprintf(stderr, "%s: Cannot open program file: %s\n", opts->program, argv[0]);
+        return EXIT_FAILURE;
+      }
+      args->fname = TRUE;
+      args->cmdline = argv[0];
+      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;
 }
 
@@ -159,10 +238,16 @@
 cleanup(mrb_state *mrb, struct _args *args)
 {
-	if (args->rfp && args->rfp != stdin)
-		fclose(args->rfp);
-	if (!args->fname)
-		mrb_free(mrb, args->cmdline);
-	mrb_free(mrb, args->argv);
-	mrb_close(mrb);
+  if (args->rfp && args->rfp != stdin)
+    fclose(args->rfp);
+  if (!args->fname)
+    mrb_free(mrb, args->cmdline);
+  mrb_free(mrb, args->argv);
+  if (args->libc) {
+    while (args->libc--) {
+      mrb_free(mrb, args->libv[args->libc]);
+    }
+    mrb_free(mrb, args->libv);
+  }
+  mrb_close(mrb);
 }
 
@@ -170,76 +255,101 @@
 mruby_main(int argc, char **argv)
 {
-	mrb_state *mrb = mrb_open();
-	int n = -1;
-	int i;
-	struct _args args;
-	mrb_value ARGV;
-	mrbc_context *c;
-	mrb_value v;
-	mrb_sym zero_sym;
-
-	if (mrb == NULL) {
-		fputs("Invalid mrb_state, exiting mruby\n", stderr);
-		return EXIT_FAILURE;
-	}
-
-	n = parse_args(mrb, argc, argv, &args);
-	if (n == EXIT_FAILURE || (args.cmdline == NULL && args.rfp == NULL)) {
-		cleanup(mrb, &args);
-		usage(argv[0]);
-		return n;
-	}
-	else {
-		int ai = mrb_gc_arena_save(mrb);
-		ARGV = mrb_ary_new_capa(mrb, args.argc);
-		for (i = 0; i < args.argc; i++) {
-			mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, args.argv[i]));
-		}
-		mrb_define_global_const(mrb, "ARGV", ARGV);
-
-		c = mrbc_context_new(mrb);
-		if (args.verbose)
-			c->dump_result = TRUE;
-		if (args.check_syntax)
-			c->no_exec = TRUE;
-
-		/* Set $0 */
-		zero_sym = mrb_intern_lit(mrb, "$0");
-		if (args.rfp) {
-			const char *cmdline;
-			cmdline = args.cmdline ? args.cmdline : "-";
-			mrbc_filename(mrb, c, cmdline);
-			mrb_gv_set(mrb, zero_sym, mrb_str_new_cstr(mrb, cmdline));
-		}
-		else {
-			mrbc_filename(mrb, c, "-e");
-			mrb_gv_set(mrb, zero_sym, mrb_str_new_lit(mrb, "-e"));
-		}
-
-		/* Load program */
-		if (args.mrbfile) {
-			v = mrb_load_irep_file_cxt(mrb, args.rfp, c);
-		}
-		else if (args.rfp) {
-			v = mrb_load_file_cxt(mrb, args.rfp, c);
-		}
-		else {
-			v = mrb_load_string_cxt(mrb, args.cmdline, c);
-		}
-
-		mrb_gc_arena_restore(mrb, ai);
-		mrbc_context_free(mrb, c);
-		if (mrb->exc) {
-			if (!mrb_undef_p(v)) {
-				mrb_print_error(mrb);
-			}
-			n = -1;
-		}
-		else if (args.check_syntax) {
-			printf("Syntax OK\n");
-		}
-	}
-	cleanup(mrb, &args);
-
-	return n == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
-}
+  mrb_state *mrb = mrb_open();
+  int n = -1;
+  int i;
+  struct _args args;
+  mrb_value ARGV;
+  mrbc_context *c;
+  mrb_value v;
+  mrb_sym zero_sym;
+
+  if (mrb == NULL) {
+    fprintf(stderr, "%s: Invalid mrb_state, exiting mruby\n", *argv);
+    return EXIT_FAILURE;
+  }
+
+  n = parse_args(mrb, argc, argv, &args);
+  if (n == EXIT_FAILURE || (args.cmdline == NULL && args.rfp == NULL)) {
+    cleanup(mrb, &args);
+    return n;
+  }
+  else {
+    int ai = mrb_gc_arena_save(mrb);
+    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);
+    mrb_gv_set(mrb, mrb_intern_lit(mrb, "$DEBUG"), mrb_bool_value(args.debug));
+
+    c = mrbc_context_new(mrb);
+    if (args.verbose)
+      c->dump_result = TRUE;
+    if (args.check_syntax)
+      c->no_exec = TRUE;
+
+    /* Set $0 */
+    zero_sym = mrb_intern_lit(mrb, "$0");
+    if (args.rfp) {
+      const char *cmdline;
+      cmdline = args.cmdline ? args.cmdline : "-";
+      mrbc_filename(mrb, c, cmdline);
+      mrb_gv_set(mrb, zero_sym, mrb_str_new_cstr(mrb, cmdline));
+    }
+    else {
+      mrbc_filename(mrb, c, "-e");
+      mrb_gv_set(mrb, zero_sym, mrb_str_new_lit(mrb, "-e"));
+    }
+
+    /* Load libraries */
+    for (i = 0; i < args.libc; i++) {
+      FILE *lfp = fopen(args.libv[i], args.mrbfile ? "rb" : "r");
+      if (lfp == NULL) {
+        fprintf(stderr, "%s: Cannot open library file: %s\n", *argv, args.libv[i]);
+        mrbc_context_free(mrb, c);
+        cleanup(mrb, &args);
+        return EXIT_FAILURE;
+      }
+      if (args.mrbfile) {
+        v = mrb_load_irep_file_cxt(mrb, lfp, c);
+      }
+      else {
+        v = mrb_load_file_cxt(mrb, lfp, c);
+      }
+      fclose(lfp);
+    }
+
+    /* Load program */
+    if (args.mrbfile) {
+      v = mrb_load_irep_file_cxt(mrb, args.rfp, c);
+    }
+    else if (args.rfp) {
+      v = mrb_load_file_cxt(mrb, args.rfp, c);
+    }
+    else {
+      char* utf8 = mrb_utf8_from_locale(args.cmdline, -1);
+      if (!utf8) abort();
+      v = mrb_load_string_cxt(mrb, utf8, c);
+      mrb_utf8_free(utf8);
+    }
+
+    mrb_gc_arena_restore(mrb, ai);
+    mrbc_context_free(mrb, c);
+    if (mrb->exc) {
+      if (!mrb_undef_p(v)) {
+        mrb_print_error(mrb);
+      }
+      n = EXIT_FAILURE;
+    }
+    else if (args.check_syntax) {
+      puts("Syntax OK");
+    }
+  }
+  cleanup(mrb, &args);
+
+  return n;
+}
