From 080d62c7c80c6c4f7eaab5431b0a39c990c325fc Mon Sep 17 00:00:00 2001 From: Tilen Majerle Date: Thu, 8 Apr 2021 19:34:20 +0200 Subject: [PATCH] Add more code --- dev/VisualStudio/main.c | 54 +++++++++++++++++++++++++-- lwshell/src/include/lwshell/lwshell.h | 29 ++++++++++++++ lwshell/src/lwshell/lwshell.c | 17 ++++++++- 3 files changed, 94 insertions(+), 6 deletions(-) diff --git a/dev/VisualStudio/main.c b/dev/VisualStudio/main.c index c9c8a63..532f24c 100644 --- a/dev/VisualStudio/main.c +++ b/dev/VisualStudio/main.c @@ -5,10 +5,53 @@ int32_t addint_cmd(int32_t argc, char** argv) { - printf("ADDINT CMD, args: %d\r\n", argc); - for (size_t i = 0; i < argc; ++i) { - printf("Arg %d: %s\r\n", (int)i, argv[i]); + long long i1, i2; + if (argc < 3) { + return -1; } + i1 = lwshell_parse_long_long(argv[1]); + i2 = lwshell_parse_long_long(argv[2]); + + printf("%lld\r\n", i1 + i2); + return 0; +} + +int32_t +subint_cmd(int32_t argc, char** argv) { + long long i1, i2; + if (argc < 3) { + return -1; + } + i1 = lwshell_parse_long_long(argv[1]); + i2 = lwshell_parse_long_long(argv[2]); + + printf("%lld\r\n", i1 - i2); + return 0; +} + +int32_t +adddbl_cmd(int32_t argc, char** argv) { + double i1, i2; + if (argc < 3) { + return -1; + } + i1 = lwshell_parse_double(argv[1]); + i2 = lwshell_parse_double(argv[2]); + + printf("%f\r\n", (i1 + i2)); + return 0; +} + +int32_t +subdbl_cmd(int32_t argc, char** argv) { + double i1, i2; + if (argc < 3) { + return -1; + } + i1 = lwshell_parse_double(argv[1]); + i2 = lwshell_parse_double(argv[2]); + + printf("%f\r\n", (i1 - i2)); return 0; } @@ -22,7 +65,10 @@ main(int argc, char** argv) { printf("\r\n"); lwshell_init(); - lwshell_register_cmd("addint", addint_cmd, "Adds int between 2 arguments"); + lwshell_register_cmd("addint", addint_cmd, "Adds 2 integer numbers and prints them"); + lwshell_register_cmd("subint", subint_cmd, "Substitute 2 integer numbers and prints them"); + lwshell_register_cmd("adddbl", adddbl_cmd, "Adds 2 double numbers and prints them"); + lwshell_register_cmd("subdbl", subdbl_cmd, "Substitute 2 double numbers and prints them"); /* User input to process every character */ printf("Start entering your text and press enter...\r\n"); diff --git a/lwshell/src/include/lwshell/lwshell.h b/lwshell/src/include/lwshell/lwshell.h index 67d1088..8a06418 100644 --- a/lwshell/src/include/lwshell/lwshell.h +++ b/lwshell/src/include/lwshell/lwshell.h @@ -35,6 +35,7 @@ #define LWSHELL_HDR_H #include +#include #include "lwshell/lwshell_opt.h" #ifdef __cplusplus @@ -82,6 +83,34 @@ lwshellr_t lwshell_init(void); lwshellr_t lwshell_register_cmd(const char* cmd_name, lwshell_cmd_fn cmd_fn, const char* desc); lwshellr_t lwshell_input(const void* in_data, size_t len); +/** + * \brief Parse input string as `integer` + * \param[in] str: String to parse + * \return String parsed as integer + */ +#define lwshell_parse_int(str) atoi(str) + +/** + * \brief Parse input string as `double` + * \param[in] str: String to parse + * \return String parsed as `double` + */ +#define lwshell_parse_double(str) atof(str) + +/** + * \brief Parse input string as `long` + * \param[in] str: String to parse + * \return String parsed as `long` + */ +#define lwshell_parse_long(str) atol(str) + +/** + * \brief Parse input string as `long long` + * \param[in] str: String to parse + * \return String parsed as `long long` + */ +#define lwshell_parse_long_long(str) atoll(str) + /** * \} */ diff --git a/lwshell/src/lwshell/lwshell.c b/lwshell/src/lwshell/lwshell.c index 1179984..db23083 100644 --- a/lwshell/src/lwshell/lwshell.c +++ b/lwshell/src/lwshell/lwshell.c @@ -155,10 +155,23 @@ prv_parse_input(lwshell_t* lw) { /* Check for command */ if (lw->argc > 0 && cmds_cnt > 0) { + lwshell_cmd_t* c = NULL; /* Process all commands */ for (size_t i = 0; i < cmds_cnt; ++i) { - if (strcmp(cmds[i].name, lw->argv[0]) == 0) { - cmds[i].fn(lw->argc, lw->argv); + if (strncmp(cmds[i].name, lw->argv[0], strlen(lw->argv[0])) == 0) { + c = &cmds[i]; + break; + } + } + + /* Valid command ready? */ + if (c != NULL) { + if (lw->argc == 2 + && lw->argv[1][0] == '-' && lw->argv[1][1] == 'h') { + /* Here we can print version */ + printf("%s\r\n", c->desc); + } else { + c->fn(lw->argc, lw->argv); } } }