mruby mbed

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed-src mruby-mbed

Files at this revision

API Documentation at this revision

Comitter:
mzta
Date:
Wed Mar 25 18:25:34 2015 +0000
Commit message:
mruby_mbed_web initial commit

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
cmd.h Show annotated file Show diff for this revision Revisions of this file
cmd/mirb.cpp Show annotated file Show diff for this revision Revisions of this file
cmd/mruby.cpp Show annotated file Show diff for this revision Revisions of this file
htmlcontent.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed-src.lib Show annotated file Show diff for this revision Revisions of this file
mruby-mbed.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 4bb480aaa402 EthernetInterface.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Wed Mar 25 18:25:34 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#2fc406e2553f
diff -r 000000000000 -r 4bb480aaa402 SDFileSystem.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Wed Mar 25 18:25:34 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/mbed/code/SDFileSystem/#7b35d1709458
diff -r 000000000000 -r 4bb480aaa402 cmd.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd.h	Wed Mar 25 18:25:34 2015 +0000
@@ -0,0 +1,7 @@
+#ifndef CMD_H
+#define CMD_H
+
+int cmd_mruby(int argc, char **argv);
+int cmd_mirb(int argc, char **argv);
+
+#endif
diff -r 000000000000 -r 4bb480aaa402 cmd/mirb.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd/mirb.cpp	Wed Mar 25 18:25:34 2015 +0000
@@ -0,0 +1,509 @@
+/*
+** mirb - Embeddable Interactive Ruby Shell
+**
+** This program takes code from the user in
+** an interactive way and executes it
+** immediately. It's a REPL...
+*/
+
+#include "mbed.h"
+
+extern Serial pc;
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <ctype.h>
+
+#ifdef ENABLE_READLINE
+#include <readline/readline.h>
+#include <readline/history.h>
+#define MIRB_ADD_HISTORY(line) add_history(line)
+#define MIRB_READLINE(ch) readline(ch)
+#define MIRB_WRITE_HISTORY(path) write_history(path)
+#define MIRB_READ_HISTORY(path) read_history(path)
+#define MIRB_USING_HISTORY() using_history()
+#elif defined(ENABLE_LINENOISE)
+#define ENABLE_READLINE
+#include <linenoise.h>
+#define MIRB_ADD_HISTORY(line) linenoiseHistoryAdd(line)
+#define MIRB_READLINE(ch) linenoise(ch)
+#define MIRB_WRITE_HISTORY(path) linenoiseHistorySave(path)
+#define MIRB_READ_HISTORY(path) linenoiseHistoryLoad(history_path)
+#define MIRB_USING_HISTORY()
+#endif
+
+#include "mruby.h"
+#include "mruby/array.h"
+#include "mruby/proc.h"
+#include "mruby/compile.h"
+#include "mruby/string.h"
+
+#ifdef ENABLE_READLINE
+
+static const char history_file_name[] = ".mirb_history";
+
+static char *
+get_history_path(mrb_state *mrb)
+{
+  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;
+}
+
+#endif
+
+static void
+p(mrb_state *mrb, mrb_value obj, int prompt)
+{
+  obj = mrb_funcall(mrb, obj, "inspect", 0);
+  if (prompt) {
+    if (!mrb->exc) {
+      pc.printf(" => ");
+    }
+    else {
+      obj = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0);
+    }
+  }
+  fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stdout);
+  putc('\n', stdout);
+}
+
+/* Guess if the user might want to enter more
+ * or if he wants an evaluation of his code now */
+static mrb_bool
+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;
+}
+
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+void mrb_show_version(mrb_state *);
+void mrb_show_copyright(mrb_state *);
+#if defined(__cplusplus)
+}
+#endif
+
+struct _args {
+  mrb_bool verbose      : 1;
+  int argc;
+  char** argv;
+};
+
+static void
+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;
+
+  pc.printf("Usage: %s [switches]\n", name);
+  while (*p)
+    pc.printf("  %s\n", *p++);
+}
+
+static int
+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;
+}
+
+static void
+cleanup(mrb_state *mrb, struct _args *args)
+{
+  mrb_close(mrb);
+}
+
+/* Print a short remark for the user */
+static void
+print_hint(void)
+{
+  pc.printf("mirb - Embeddable Interactive Ruby Shell\n\n");
+}
+
+#ifndef ENABLE_READLINE
+/* Print the command line prompt of the REPL */
+static void
+print_cmdline(int code_block_open)
+{
+  if (code_block_open) {
+    pc.printf("* ");
+  }
+  else {
+    pc.printf("> ");
+  }
+}
+#endif
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+void mrb_codedump_all(mrb_state*, struct RProc*);
+#if defined(__cplusplus)
+}
+#endif
+
+static int
+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;
+}
+
+int
+cmd_mirb(int argc, char **argv)
+{
+  char ruby_code[1024] = { 0 };
+  char last_code_line[1024] = { 0 };
+#ifndef ENABLE_READLINE
+  int last_char;
+  int 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) {
+    pc.printf("Invalid mrb interpreter, exiting mirb\n");
+    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;
+  }
+
+#ifdef ENABLE_READLINE
+  history_path = get_history_path(mrb);
+  if (history_path == NULL) {
+    pc.printf("failed to get history path\n");
+    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) {
+#ifndef ENABLE_READLINE
+    print_cmdline(code_block_open);
+
+    char_index = 0;
+    while ((last_char = pc.getc()) != '\n') {
+      pc.printf("%c", last_char);
+      if (last_char == EOF) break;
+      if (char_index > sizeof(last_code_line)-2) {
+        pc.printf("input string too long\n");
+        continue;
+      }
+      if (last_char == '\x08') {
+        if (char_index > 0) char_index--;
+        continue;
+      } else {
+        last_code_line[char_index++] = last_char;
+      }
+    }
+    pc.printf("\n");
+    
+    if (last_char == EOF) {
+      pc.printf("\n");
+      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) {
+      pc.printf("input string too long\n");
+      continue;
+    }
+    strcpy(last_code_line, line);
+    strcat(last_code_line, "\n");
+    MIRB_ADD_HISTORY(line);
+    free(line);
+#endif
+
+    if (code_block_open) {
+      if (strlen(ruby_code)+strlen(last_code_line) > sizeof(ruby_code)-1) {
+        pc.printf("concatenated input string too long\n");
+        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);
+    }
+
+    /* parse code */
+    parser = mrb_parser_new(mrb);
+    if (parser == NULL) {
+      pc.printf("create parser state error\n");
+      break;
+    }
+    parser->s = ruby_code;
+    parser->send = ruby_code + strlen(ruby_code);
+    parser->lineno = cxt->lineno;
+    mrb_parser_parse(parser, cxt);
+    code_block_open = is_code_block_open(parser);
+
+    if (code_block_open) {
+      /* no evaluation of code */
+    }
+    else {
+      if (0 < parser->nerr) {
+        /* syntax error */
+        pc.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) {
+          pc.printf("codegen error\n");
+          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++;
+  }
+  
+#ifdef ENABLE_READLINE
+  MIRB_WRITE_HISTORY(history_path);
+  mrb_free(mrb, history_path);
+#endif
+
+  mrbc_context_free(mrb, cxt);
+  mrb_close(mrb);
+  
+  return 0;
+}
diff -r 000000000000 -r 4bb480aaa402 cmd/mruby.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd/mruby.cpp	Wed Mar 25 18:25:34 2015 +0000
@@ -0,0 +1,252 @@
+#include "mbed.h"
+#include "SDFileSystem.h"
+ 
+extern SDFileSystem sd;
+extern Serial pc;
+
+#include <stdio.h>
+#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"
+#include "cmd.h"
+
+#ifndef ENABLE_STDIO
+static void
+p(mrb_state *mrb, mrb_value obj)
+{
+  obj = mrb_funcall(mrb, obj, "inspect", 0);
+  fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stdout);
+  putc('\n', stdout);
+}
+#else
+#define p(mrb,obj) mrb_p(mrb,obj)
+#endif
+
+void mrb_show_version(mrb_state *);
+void mrb_show_copyright(mrb_state *);
+
+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;
+};
+
+static void
+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 int
+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 void
+cleanup(mrb_state *mrb, struct _args *args)
+{
+  if (args->rfp && args->rfp != stdin)
+    fclose(args->rfp);
+  if (args->cmdline && !args->fname)
+    mrb_free(mrb, args->cmdline);
+  if (args->argv)
+    mrb_free(mrb, args->argv);
+  mrb_close(mrb);
+}
+
+int
+cmd_mruby(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;
+  }
+
+  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);
+  }
+
+  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;
+}
+
diff -r 000000000000 -r 4bb480aaa402 htmlcontent.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/htmlcontent.h	Wed Mar 25 18:25:34 2015 +0000
@@ -0,0 +1,59 @@
+#define EDITPAGE "<!DOCTYPE html>" \
+"<html lang=\"en\">" \
+"<head>" \
+"<meta charset=\"utf-8\">" \
+"<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">" \
+"<title>mbed mruby editor</title>" \
+"<link href=\"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.0.0/codemirror.min.css\" rel=\"stylesheet\" type=\"text/css\">" \
+"<link href=\"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.0.0/theme/solarized.min.css\" rel=\"stylesheet\" type=\"text/css\">" \
+"<link href=\"http://fonts.googleapis.com/css?family=Source+Code+Pro\" rel=\"stylesheet\" type=\"text/css\">" \
+"<style>" \
+"textarea{font-family: 'Source Code Pro', sans-serif;}" \
+"h2{color: #333;}" \
+"#nav-bar{height: 30px;}" \
+"#header{height: 2.5em;}" \
+"#editor{height: 500px; width: 100%;}" \
+"</style>" \
+"</head>" \
+"<body>" \
+"<div id=\"header\">" \
+"<h2>mbed mruby editor</h2>" \
+"</div>" \
+"<div id=\"nav-bar\">" \
+"<button id=\"run-btn\">Run</button>" \
+"</div>" \
+"<div id=\"editor-container\">" \
+"<textarea id=\"editor\">include Mbed\n" \
+"led = DigitalOut.new LED1\n" \
+"sleep 1\n" \
+"led.write 1\n" \
+"sleep 1\n" \
+"led.write 0\n" \
+"sleep 1\n" \
+"led.write 1</textarea>\n" \
+"</div>\n" \
+"<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js\"></script>" \
+"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.0.0/codemirror.min.js\" type=\"text/javascript\" charset=\"utf-8\"></script>" \
+"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.0.0/mode/ruby/ruby.min.js\" type=\"text/javascript\" charset=\"utf-8\"></script>" \
+"<script>" \
+"var editor = CodeMirror.fromTextArea(document.getElementById(\"editor\"), {" \
+"    lineNumbers: true," \
+"    mode: \"ruby\"," \
+"    theme: \"solarized light\"" \
+"});" \
+"$('#run-btn').click(function() {" \
+"    var code = editor.getValue();" \
+"    $.ajax({" \
+"        url: 'run'," \
+"        type: 'POST'," \
+"        data: code," \
+"        contentType: 'text/plain'," \
+"        success: function() {" \
+"        }," \
+"        error: function() {" \
+"        }" \
+"    });" \
+"});" \
+"</script>" \
+"</body>" \
+"</html>"
diff -r 000000000000 -r 4bb480aaa402 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Mar 25 18:25:34 2015 +0000
@@ -0,0 +1,87 @@
+#include <string>
+
+#include "mbed.h"
+#include "SDFileSystem.h"
+#include "EthernetInterface.h"
+#include "htmlcontent.h"
+#include "cmd.h"
+
+#define RBFILE "/sd/test.rb"
+#define ENABLE_DHCP
+
+SDFileSystem sd(P8_5, P8_6, P8_3, P8_4, "sd");
+
+Serial pc(USBTX, USBRX);
+char i_buff[1024*3];
+char o_buff[1024*3];
+
+int mruby_argc = 2;
+char *mruby_argv[] = {
+    "mruby",
+    RBFILE
+};
+
+int getRequestBody(char *request, char *buff, int *size)
+{
+    string req(request);
+    int sp = req.find("Content-Length", 0);
+    int ep = req.find("\r\n", sp);
+    int len = 0;
+    sscanf(req.substr(sp, ep-sp).c_str(), "Content-Length: %d", &len);
+
+    sp = req.find("\r\n\r\n", 0) + 4;
+    string body = req.substr(sp, len);
+    *size = len;
+    sprintf(buff, body.c_str());
+}
+
+int writeScriptFile(char *buff, int size)
+{
+    int i;
+    FILE *f = fopen(RBFILE, "w");
+ 
+    for (i=0; i < size; i++) {
+        fprintf(f, "%c", buff[i]);
+    }
+
+    fclose(f);
+}
+
+int main() {
+    EthernetInterface eth0;
+
+    printf("Init network interface...\n");
+#ifdef ENABLE_DHCP
+    eth0.init();
+#else
+    eth0.init("192.168.1.100", "255.255.255.0", "192.168.1.1");
+#endif
+    eth0.connect();
+    printf("IP Address: %s\n", eth0.getIPAddress());
+    printf("done!\n\n");
+ 
+    TCPSocketServer server;
+    TCPSocketConnection conn;
+    server.bind(80);
+    server.listen();
+     
+    while (true) {
+        server.accept(conn);
+        conn.receive(i_buff, sizeof(i_buff));
+        
+        if (i_buff[0] == 'P') {
+            int size;
+            getRequestBody(i_buff, o_buff, &size);
+            writeScriptFile(o_buff, size);
+            cmd_mruby(mruby_argc, mruby_argv);
+        } else {
+            sprintf(o_buff, EDITPAGE);        
+        }
+        
+        conn.send(o_buff, sizeof(EDITPAGE));
+        conn.close();
+    }
+ 
+    server.close();
+    eth0.disconnect();
+}
diff -r 000000000000 -r 4bb480aaa402 mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Wed Mar 25 18:25:34 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#d3d0e710b443
diff -r 000000000000 -r 4bb480aaa402 mbed-src.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-src.lib	Wed Mar 25 18:25:34 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-src/#543871686697
diff -r 000000000000 -r 4bb480aaa402 mruby-mbed.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mruby-mbed.lib	Wed Mar 25 18:25:34 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mzta/code/mruby-mbed/#158c61bb030f