From 922991f9088a1c565b5c0312226c984f7059434b Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Wed, 25 Oct 2023 21:02:13 +0100 Subject: [PATCH] Add TCL echo command --- src/rt/shell.c | 27 +++++++++++++++++++++++++-- test/test_shell.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/rt/shell.c b/src/rt/shell.c index e7117474..13dba5b0 100644 --- a/src/rt/shell.c +++ b/src/rt/shell.c @@ -886,9 +886,31 @@ static const char copyright_help[] = "Display copyright information"; static int shell_cmd_copyright(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) { - tcl_shell_t *sh = cd; + Tcl_Channel channel = Tcl_GetStdChannel(TCL_STDOUT); + extern char copy_string[]; - shell_printf(sh, "%s\n", copy_string); + Tcl_WriteChars(channel, copy_string, -1); + Tcl_WriteChars(channel, "\n", 1); + Tcl_Flush(channel); + + return TCL_OK; +} + +static const char echo_help[] = "Display value of arguments"; + +static int shell_cmd_echo(ClientData cd, Tcl_Interp *interp, + int objc, Tcl_Obj *const objv[]) +{ + Tcl_Channel channel = Tcl_GetStdChannel(TCL_STDOUT); + + for (int i = 1; i < objc; i++) { + if (i > 1) Tcl_WriteChars(channel, " ", 1); + Tcl_WriteObj(channel, objv[i]); + } + + Tcl_WriteChars(channel, "\n", 1); + Tcl_Flush(channel); + return TCL_OK; } @@ -1068,6 +1090,7 @@ tcl_shell_t *shell_new(jit_factory_t make_jit) shell_add_cmd(sh, "quit", shell_cmd_quit, quit_help); shell_add_cmd(sh, "force", shell_cmd_force, force_help); shell_add_cmd(sh, "noforce", shell_cmd_noforce, noforce_help); + shell_add_cmd(sh, "echo", shell_cmd_echo, echo_help); qsort(sh->cmds, sh->ncmds, sizeof(shell_cmd_t), compare_shell_cmd); diff --git a/test/test_shell.c b/test/test_shell.c index c0a5927a..fe1e6e86 100644 --- a/test/test_shell.c +++ b/test/test_shell.c @@ -419,6 +419,35 @@ START_TEST(test_force1) } END_TEST +static void echo_stdout_handler(const char *buf, size_t nchars, void *ctx) +{ + int *state = ctx; + ck_assert_str_eq(buf, "hello 3\n"); + ck_assert_int_eq(nchars, 8); + (*state)++; +} + +START_TEST(test_echo) +{ + tcl_shell_t *sh = shell_new(NULL); + + int state = 0; + shell_handler_t handler = { + .stdout_write = echo_stdout_handler, + .context = &state, + }; + shell_set_handler(sh, &handler); + + const char *result = NULL; + fail_unless(shell_eval(sh, "echo hello [expr 1 + 2]", &result)); + ck_assert_str_eq(result, ""); + + ck_assert_int_eq(state, 1); + + shell_free(sh); +} +END_TEST + Suite *get_shell_tests(void) { Suite *s = suite_create("shell"); @@ -431,6 +460,7 @@ Suite *get_shell_tests(void) tcase_add_test(tc, test_redirect); tcase_add_test(tc, test_force1); tcase_add_exit_test(tc, test_exit, 5); + tcase_add_test(tc, test_echo); suite_add_tcase(s, tc); return s; -- 2.39.2