Have you tried to develop a key input feature on your small embedded platform with a serial input? It is not so easy to implement correctly because the input methodology is using a complicated protocol known as VT100. In my recent project "Natural Tiny Shell (NT-Shell)" provides VT100 compatible terminal control features for small embedded systems. However, the middleware is still large for small embedded processors such as 8-bit MCUs, 16-bit MCUs and also for small 32-bit MCUs like Cortex-M0. This "MicroShell" middleware provides a minimal terminal control for the platforms.
util/mscmd.c@0:e91b984e285d, 2017-02-05 (annotated)
- Committer:
- shintamainjp
- Date:
- Sun Feb 05 03:55:01 2017 +0000
- Revision:
- 0:e91b984e285d
first commitment.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shintamainjp | 0:e91b984e285d | 1 | /** |
shintamainjp | 0:e91b984e285d | 2 | * @file mscmd.c |
shintamainjp | 0:e91b984e285d | 3 | * @author Shinichiro Nakamura (CuBeatSystems) |
shintamainjp | 0:e91b984e285d | 4 | * =============================================================== |
shintamainjp | 0:e91b984e285d | 5 | * MicroShell (Version 0.0.1) |
shintamainjp | 0:e91b984e285d | 6 | * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems) |
shintamainjp | 0:e91b984e285d | 7 | * =============================================================== |
shintamainjp | 0:e91b984e285d | 8 | * The MIT License : https://opensource.org/licenses/MIT |
shintamainjp | 0:e91b984e285d | 9 | * |
shintamainjp | 0:e91b984e285d | 10 | * Copyright (c) 2016, 2017 Shinichiro Nakamura (CuBeatSystems) |
shintamainjp | 0:e91b984e285d | 11 | * |
shintamainjp | 0:e91b984e285d | 12 | * Permission is hereby granted, free of charge, to any person |
shintamainjp | 0:e91b984e285d | 13 | * obtaining a copy of this software and associated documentation |
shintamainjp | 0:e91b984e285d | 14 | * files (the "Software"), to deal in the Software without |
shintamainjp | 0:e91b984e285d | 15 | * restriction, including without limitation the rights to use, |
shintamainjp | 0:e91b984e285d | 16 | * copy, modify, merge, publish, distribute, sublicense, and/or |
shintamainjp | 0:e91b984e285d | 17 | * sell copies of the Software, and to permit persons to whom the |
shintamainjp | 0:e91b984e285d | 18 | * Software is furnished to do so, subject to the following |
shintamainjp | 0:e91b984e285d | 19 | * conditions: |
shintamainjp | 0:e91b984e285d | 20 | * |
shintamainjp | 0:e91b984e285d | 21 | * The above copyright notice and this permission notice shall be |
shintamainjp | 0:e91b984e285d | 22 | * included in all copies or substantial portions of the Software. |
shintamainjp | 0:e91b984e285d | 23 | * |
shintamainjp | 0:e91b984e285d | 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
shintamainjp | 0:e91b984e285d | 25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
shintamainjp | 0:e91b984e285d | 26 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
shintamainjp | 0:e91b984e285d | 27 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
shintamainjp | 0:e91b984e285d | 28 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
shintamainjp | 0:e91b984e285d | 29 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
shintamainjp | 0:e91b984e285d | 30 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
shintamainjp | 0:e91b984e285d | 31 | * OTHER DEALINGS IN THE SOFTWARE. |
shintamainjp | 0:e91b984e285d | 32 | */ |
shintamainjp | 0:e91b984e285d | 33 | |
shintamainjp | 0:e91b984e285d | 34 | #include "msopt.h" |
shintamainjp | 0:e91b984e285d | 35 | #include "mscmd.h" |
shintamainjp | 0:e91b984e285d | 36 | #include "ntlibc.h" |
shintamainjp | 0:e91b984e285d | 37 | |
shintamainjp | 0:e91b984e285d | 38 | int mscmd_init(MSCMD *handle, MSCMD_COMMAND_TABLE *command_table, int command_count, MSCMD_USER_OBJECT usrobj) |
shintamainjp | 0:e91b984e285d | 39 | { |
shintamainjp | 0:e91b984e285d | 40 | handle->command_table = command_table; |
shintamainjp | 0:e91b984e285d | 41 | handle->command_count = command_count; |
shintamainjp | 0:e91b984e285d | 42 | handle->usrobj = usrobj; |
shintamainjp | 0:e91b984e285d | 43 | return 0; |
shintamainjp | 0:e91b984e285d | 44 | } |
shintamainjp | 0:e91b984e285d | 45 | |
shintamainjp | 0:e91b984e285d | 46 | int mscmd_execute(MSCMD *handle, char *text, MSCMD_USER_RESULT *result) |
shintamainjp | 0:e91b984e285d | 47 | { |
shintamainjp | 0:e91b984e285d | 48 | MSOPT msopt; |
shintamainjp | 0:e91b984e285d | 49 | int argc = 0; |
shintamainjp | 0:e91b984e285d | 50 | char argv0[MSCONF_MAX_INPUT_LENGTH]; |
shintamainjp | 0:e91b984e285d | 51 | |
shintamainjp | 0:e91b984e285d | 52 | msopt_init(&msopt, text); |
shintamainjp | 0:e91b984e285d | 53 | msopt_get_argc(&msopt, &argc); |
shintamainjp | 0:e91b984e285d | 54 | if (argc == 0) { |
shintamainjp | 0:e91b984e285d | 55 | return -1; |
shintamainjp | 0:e91b984e285d | 56 | } |
shintamainjp | 0:e91b984e285d | 57 | MSCMD_COMMAND_TABLE *p = handle->command_table; |
shintamainjp | 0:e91b984e285d | 58 | int i; |
shintamainjp | 0:e91b984e285d | 59 | for (i = 0; i < handle->command_count; i++) { |
shintamainjp | 0:e91b984e285d | 60 | msopt_get_argv(&msopt, 0, argv0, sizeof(argv0)); |
shintamainjp | 0:e91b984e285d | 61 | if (ntlibc_strcmp(argv0, p->argv0) == 0) { |
shintamainjp | 0:e91b984e285d | 62 | *result = p->func(&msopt, handle->usrobj); |
shintamainjp | 0:e91b984e285d | 63 | return 0; |
shintamainjp | 0:e91b984e285d | 64 | } |
shintamainjp | 0:e91b984e285d | 65 | p++; |
shintamainjp | 0:e91b984e285d | 66 | } |
shintamainjp | 0:e91b984e285d | 67 | return -2; |
shintamainjp | 0:e91b984e285d | 68 | } |
shintamainjp | 0:e91b984e285d | 69 | |
shintamainjp | 0:e91b984e285d | 70 |