17 #ifdef ENABLE_READLINE
19 #include <readline/readline.h>
20 #include <readline/history.h>
25 #ifdef ENABLE_READLINE
26 static const char *history_file_name =
".mirb_history";
37 fputs(
" => ", stdout);
52 int code_block_open =
FALSE;
62 if (0 < parser->
nerr) {
63 const char *unexpected_end =
"syntax error, unexpected $end";
71 if (strncmp(message, unexpected_end, strlen(unexpected_end)) == 0) {
72 code_block_open =
TRUE;
74 else if (strcmp(message,
"syntax error, unexpected keyword_end") == 0) {
75 code_block_open =
FALSE;
77 else if (strcmp(message,
"syntax error, unexpected tREGEXP_BEG") == 0) {
78 code_block_open =
FALSE;
80 return code_block_open;
93 code_block_open =
TRUE;
98 code_block_open =
TRUE;
103 code_block_open =
TRUE;
107 code_block_open =
TRUE;
111 code_block_open =
TRUE;
118 code_block_open =
FALSE;
145 return code_block_open;
158 usage(
const char *name)
160 static const char *
const usage_msg[] = {
162 "-v print version number, then run in verbose mode",
163 "--verbose run in verbose mode",
164 "--version print the version",
165 "--copyright print the copyright",
168 const char *
const *p = usage_msg;
170 printf(
"Usage: %s [switches]\n", name);
172 printf(
" %s\n", *p++);
176 parse_args(
mrb_state *mrb,
int argc,
char **argv,
struct _args *args)
178 static const struct _args args_zero = { 0 };
182 for (argc--,argv++; argc > 0; argc--,argv++) {
184 if (argv[0][0] !=
'-')
break;
193 if (strcmp((*argv) + 2,
"version") == 0) {
197 else if (strcmp((*argv) + 2,
"verbose") == 0) {
201 else if (strcmp((*argv) + 2,
"copyright") == 0) {
222 printf(
"mirb - Embeddable Interactive Ruby Shell\n");
223 printf(
"\nThis is a very early version, please test and report errors.\n");
224 printf(
"Thanks :)\n\n");
231 if (code_block_open) {
242 char ruby_code[1024] = { 0 };
243 char last_code_line[1024] = { 0 };
244 #ifndef ENABLE_READLINE
256 int code_block_open =
FALSE;
262 fputs(
"Invalid mrb interpreter, exiting mirb\n", stderr);
267 n = parse_args(mrb, argc, argv, &args);
268 if (n == EXIT_FAILURE) {
284 #ifdef ENABLE_READLINE
286 home = getenv(
"HOME");
289 home = getenv(
"USERPROFILE");
292 strcpy(history_path, home);
293 strcat(history_path,
"/");
294 strcat(history_path, history_file_name);
295 read_history(history_path);
301 #ifndef ENABLE_READLINE
305 while ((last_char = getchar()) !=
'\n') {
306 if (last_char == EOF)
break;
307 last_code_line[char_index++] = last_char;
309 if (last_char == EOF) {
314 last_code_line[char_index] =
'\0';
316 char* line = readline(code_block_open ?
"* " :
"> ");
321 strncpy(last_code_line, line,
sizeof(last_code_line)-1);
326 if ((strcmp(last_code_line,
"quit") == 0) || (strcmp(last_code_line,
"exit") == 0)) {
327 if (!code_block_open) {
332 strcat(ruby_code,
"\n");
333 strcat(ruby_code, last_code_line);
337 if (code_block_open) {
338 strcat(ruby_code,
"\n");
339 strcat(ruby_code, last_code_line);
342 strcpy(ruby_code, last_code_line);
348 parser->
s = ruby_code;
349 parser->
send = ruby_code + strlen(ruby_code);
354 if (code_block_open) {
358 if (0 < parser->
nerr) {
373 p(mrb, mrb_obj_value(mrb->
exc), 0);
385 last_code_line[0] =
'\0';
394 #ifdef ENABLE_READLINE
395 write_history(history_path);