From 0651051ded8bd731556ac6adb3d000e031034e8c Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Thu, 15 May 2014 12:07:02 +0900 Subject: allow quit/exit to work within mirb; fix #2253 close #2254 also allow spaces around quit/exit. --- mrbgems/mruby-bin-mirb/tools/mirb/mirb.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c b/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c index b1b8767bc..5c9524161 100644 --- a/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c +++ b/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c @@ -9,6 +9,7 @@ #include #include #include +#include #ifdef ENABLE_READLINE #include @@ -250,6 +251,29 @@ print_cmdline(int code_block_open) void mrb_codedump_all(mrb_state*, struct RProc*); +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(*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; +} + int main(int argc, char **argv) { @@ -356,7 +380,7 @@ main(int argc, char **argv) strcat(ruby_code, last_code_line); } else { - if ((strcmp(last_code_line, "quit") == 0) || (strcmp(last_code_line, "exit") == 0)) { + if (check_keyword(last_code_line, "quit") || check_keyword(last_code_line, "exit")) { break; } strcpy(ruby_code, last_code_line); -- cgit v1.2.3