[388] | 1 | /*
|
---|
| 2 | * Copyright (C) 2008-2017 by TOPPERS Project
|
---|
| 3 | *
|
---|
| 4 | * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
| 5 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
| 6 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
| 7 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
| 8 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
| 9 | * スコード中に含まれていること.
|
---|
| 10 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
| 11 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
| 12 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
| 13 | * の無保証規定を掲載すること.
|
---|
| 14 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
| 15 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
| 16 | * と.
|
---|
| 17 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
| 18 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
| 19 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
| 20 | * 報告すること.
|
---|
| 21 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
| 22 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
| 23 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
| 24 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
| 25 | * 免責すること.
|
---|
| 26 | *
|
---|
| 27 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
| 28 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
| 29 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
| 30 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
| 31 | * の責任を負わない.
|
---|
| 32 | *
|
---|
| 33 | * @(#) $Id$
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | /*
|
---|
| 37 | * 以前の nMruby_tMrubyProc.c は、mruby の本体に含まれていた
|
---|
| 38 | * mrbgems/mruby-bin-mruby/tools/mruby/mruby.c
|
---|
| 39 | * をベースに TECS のコードを足していたが、TOPPERS ライセンスと異なるため
|
---|
| 40 | * 新たに書き下ろした。(小さなコードのため、概ね似たようなものになるが。
|
---|
| 41 | */
|
---|
| 42 |
|
---|
| 43 | #include <stdio.h>
|
---|
| 44 | #include <stdlib.h>
|
---|
| 45 | #include <string.h>
|
---|
| 46 | #include "mruby.h"
|
---|
| 47 | #include "mruby/string.h"
|
---|
| 48 | #include "mruby/array.h"
|
---|
| 49 | #include "mruby/compile.h"
|
---|
| 50 | #include "mruby/dump.h"
|
---|
| 51 | #include "mruby/variable.h"
|
---|
| 52 |
|
---|
| 53 | /* #[<PREAMBLE>]#
|
---|
| 54 | * Don't edit the comments between #[<...>]# and #[</...>]#
|
---|
| 55 | * These comment are used by tecsmerege when merging.
|
---|
| 56 | *
|
---|
| 57 | * attr access macro #_CAAM_#
|
---|
| 58 | * mrb mrb_state* VAR_mrb
|
---|
| 59 | *
|
---|
| 60 | * call port function #_TCPF_#
|
---|
| 61 | * call port: cInit signature: nMruby_sInitializeBridge context:task optional:true
|
---|
| 62 | * bool_t is_cInit_joined() check if joined
|
---|
| 63 | * void cInit_initializeBridge( const mrb_state* mrb );
|
---|
| 64 | *
|
---|
| 65 | * #[</PREAMBLE>]# */
|
---|
| 66 |
|
---|
| 67 | /* プロトタイプ宣言や変数の定義をここに書きます #_PAC_# */
|
---|
| 68 | #include "nMruby_tMrubyProc_tecsgen.h"
|
---|
| 69 |
|
---|
| 70 | #ifndef E_OK
|
---|
| 71 | #define E_OK 0 /* success */
|
---|
| 72 | #define E_ID (-18) /* illegal ID */
|
---|
| 73 | #endif
|
---|
| 74 |
|
---|
| 75 | /* 受け口関数 #_TEPF_# */
|
---|
| 76 | /* #[<ENTRY_PORT>]# eMain
|
---|
| 77 | * entry port: eMain
|
---|
| 78 | * signature: nPosix_sMain
|
---|
| 79 | * context: task
|
---|
| 80 | * #[</ENTRY_PORT>]# */
|
---|
| 81 |
|
---|
| 82 | /* #[<ENTRY_FUNC>]# eMain_main
|
---|
| 83 | * name: eMain_main
|
---|
| 84 | * global_name: nMruby_tMrubyProc_eMain_main
|
---|
| 85 | * oneway: false
|
---|
| 86 | * #[</ENTRY_FUNC>]# */
|
---|
| 87 | int
|
---|
| 88 | eMain_main(CELLIDX idx, int argc, const char** argv)
|
---|
| 89 | {
|
---|
| 90 | CELLCB *p_cellcb;
|
---|
| 91 | mrb_state *mrb;
|
---|
| 92 | mrbc_context* context;
|
---|
| 93 | if (VALID_IDX(idx)) {
|
---|
| 94 | p_cellcb = GET_CELLCB(idx);
|
---|
| 95 | }
|
---|
| 96 | else {
|
---|
| 97 | /* エラー処理コードをここに記述します */
|
---|
| 98 | return -1;
|
---|
| 99 | } /* end if VALID_IDX(idx) */
|
---|
| 100 |
|
---|
| 101 | /* ここに処理本体を記述します #_TEFB_# */
|
---|
| 102 | if (argc != 2) {
|
---|
| 103 | printf("usage: %s script.rb\n", argv[0]);
|
---|
| 104 | goto error_return;
|
---|
| 105 | }
|
---|
| 106 | mrb = mrb_open();
|
---|
| 107 | if( mrb == 0 ){
|
---|
| 108 | printf("%s: mrb_open() failed\n", argv[0]);
|
---|
| 109 | goto error_return;
|
---|
| 110 | }
|
---|
| 111 | context = mrbc_context_new(mrb);
|
---|
| 112 | if (context == 0) {
|
---|
| 113 | printf("%s: mrbc_context_new() failed\n", argv[0]);
|
---|
| 114 | goto error_close;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /*-------- ARGV ----------*/
|
---|
| 118 | {
|
---|
| 119 | mrb_value ARGV = mrb_ary_new_capa(mrb, argc);
|
---|
| 120 | int i;
|
---|
| 121 | for (i = 0; i < argc; i++) {
|
---|
| 122 | mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, argv[i]));
|
---|
| 123 | }
|
---|
| 124 | mrb_define_global_const(mrb, "ARGV", ARGV);
|
---|
| 125 | }
|
---|
| 126 | /*-------- $0 ----------*/
|
---|
| 127 | {
|
---|
| 128 | mrb_sym zero_sym = mrb_intern_lit(mrb, "$0");
|
---|
| 129 | mrbc_filename(mrb, context, argv[0]);
|
---|
| 130 | mrb_gv_set(mrb, zero_sym, mrb_str_new_cstr(mrb, argv[0]));
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /* ------- register TECS bridge classes -------*/
|
---|
| 134 | if (is_cInit_joined()) {
|
---|
| 135 | cInit_initializeBridge(mrb);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /*-------- load .rb file -------*/
|
---|
| 139 | {
|
---|
| 140 | FILE *file = fopen(argv[1], "r");
|
---|
| 141 | if (file == NULL) {
|
---|
| 142 | printf("file not found: %s\n", argv[1]);
|
---|
| 143 | goto error_free;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | mrb_value val = mrb_load_file_cxt(mrb, file, context);
|
---|
| 147 | if (mrb->exc) {
|
---|
| 148 | if(! mrb_undef_p(val)){
|
---|
| 149 | mrb_print_error(mrb);
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /*---------- success case --------*/
|
---|
| 155 | mrbc_context_free(mrb, context);
|
---|
| 156 | mrb_close( mrb );
|
---|
| 157 | return 0;
|
---|
| 158 |
|
---|
| 159 | /*---------- error case ----------*/
|
---|
| 160 | error_free:
|
---|
| 161 | mrbc_context_free(mrb, context);
|
---|
| 162 | error_close:
|
---|
| 163 | mrb_close( mrb );
|
---|
| 164 | error_return:
|
---|
| 165 |
|
---|
| 166 | return -1;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /* #[<POSTAMBLE>]#
|
---|
| 170 | * Put non-entry functions below.
|
---|
| 171 | * #[</POSTAMBLE>]#*/
|
---|