From 70e4fc578a9d62231d565da44cbf1854b3c9c6e6 Mon Sep 17 00:00:00 2001 From: paladin-t Date: Mon, 18 Jan 2016 11:39:24 +0800 Subject: [PATCH] +added a NOW statement to the shell. --- HISTORY | 1 + shell/main.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/HISTORY b/HISTORY index fd3b30d..c9ac4eb 100755 --- a/HISTORY +++ b/HISTORY @@ -1,4 +1,5 @@ Jan. 18 2016 +Added a NOW statement to the shell Polished shell implementation code Jan. 17 2016 diff --git a/shell/main.c b/shell/main.c index c106758..baf336b 100755 --- a/shell/main.c +++ b/shell/main.c @@ -1061,6 +1061,7 @@ static int ticks(struct mb_interpreter_t* s, void** l) { mb_assert(s && l); mb_check(mb_attempt_open_bracket(s, l)); + mb_check(mb_attempt_close_bracket(s, l)); mb_check(mb_push_int(s, l, _ticks())); @@ -1069,12 +1070,43 @@ static int ticks(struct mb_interpreter_t* s, void** l) { } #endif /* _HAS_TICKS */ +static int now(struct mb_interpreter_t* s, void** l) { + int result = MB_FUNC_OK; + time_t ct; + struct tm* timeinfo; + char buf[80]; + char* arg = 0; + + mb_assert(s && l); + + mb_check(mb_attempt_open_bracket(s, l)); + + if(mb_has_arg(s, l)) { + mb_check(mb_pop_string(s, l, &arg)); + } + + mb_check(mb_attempt_close_bracket(s, l)); + + time(&ct); + timeinfo = localtime(&ct); + if(arg) { + strftime(buf, _countof(buf), arg, timeinfo); + mb_check(mb_push_string(s, l, mb_memdup(buf, (unsigned)(strlen(buf) + 1)))); + } else { + arg = asctime(timeinfo); + mb_check(mb_push_string(s, l, mb_memdup(arg, (unsigned)(strlen(arg) + 1)))); + } + + return result; +} + static int beep(struct mb_interpreter_t* s, void** l) { int result = MB_FUNC_OK; mb_assert(s && l); mb_check(mb_attempt_func_begin(s, l)); + mb_check(mb_attempt_func_end(s, l)); putchar('\a'); @@ -1149,6 +1181,7 @@ static void _on_startup(void) { #ifdef _HAS_TICKS mb_reg_fun(bas, ticks); #endif /* _HAS_TICKS */ + mb_reg_fun(bas, now); mb_reg_fun(bas, beep); }