Skip to content

Commit 9162230

Browse files
committed
feat: Add option for static commands, add option to disable default dynamic commands
1 parent b52c415 commit 9162230

File tree

8 files changed

+229
-53
lines changed

8 files changed

+229
-53
lines changed

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
"lwevt_type.h": "c",
55
"lwevt.h": "c",
66
"string.h": "c",
7-
"lwevt_opt.h": "c"
7+
"lwevt_opt.h": "c",
8+
"lwshell.h": "c",
9+
"lwshell_opt.h": "c",
10+
"lwshell_opts.h": "c",
11+
"typeinfo": "c"
812
},
913
"esbonio.sphinx.confDir": ""
1014
}

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- Change license year to 2022
66
- Update code style with astyle
77
- Add `.clang-format` draft
8+
- Add option for statically allocated commands array (improvement for small devices w/ little memory)
9+
- Add option to disable dynamic commands allocation (default value)
810

911
## v1.1.1
1012

TODO.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# TODO
22

33
- Add option to decide for delimiter string (`\r\n`)
4-
- Improve helper functions for number parsing (do not use math.h to reduce memory footprint)
5-
- Add option to statically allocate array of commands and assign them to parser -> use of flash memory on embedded systems
4+
- Improve helper functions for number parsing (do not use math.h to reduce memory footprint)

dev/lwshell_opts.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838

3939
#include "windows.h"
4040

41-
#define LWSHELL_CFG_USE_OUTPUT 1
42-
#define LWSHELL_CFG_USE_ENABLE_LIST_CMD 1
41+
#define LWSHELL_CFG_USE_OUTPUT 0
42+
#define LWSHELL_CFG_USE_LIST_CMD 1
43+
#define LWSHELL_CFG_USE_DYNAMIC_COMMANDS 1
44+
#define LWSHELL_CFG_USE_STATIC_COMMANDS 1
4345

4446
#endif /* LWSHELL_HDR_OPTS_H */

dev/main.c

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,34 @@
1010
* Please note that conio.h is a windows only header
1111
*/
1212
#ifndef LWSHELL_TEST_READ_SINGLE_CHAR
13-
#define LWSHELL_TEST_READ_SINGLE_CHAR 0
13+
#define LWSHELL_TEST_READ_SINGLE_CHAR 0
1414
#endif
1515

1616
#if LWSHELL_TEST_READ_SINGLE_CHAR
1717
#include <conio.h>
1818
#endif
1919

20-
void example_minimal(void);
20+
void example_minimal(void);
21+
22+
#if LWSHELL_CFG_USE_OUTPUT
23+
24+
/**
25+
* \brief Application output function
26+
* \param[in] str: String to print, null-terminated
27+
* \param[in] lw: LwSHELL instance
28+
*/
29+
static void
30+
shell_output(const char* str, lwshell_t* lw) {
31+
(void)lw;
32+
printf("%s", str);
33+
if (*str == '\r') {
34+
printf("\n");
35+
}
36+
}
37+
38+
#endif /* LWSHELL_CFG_USE_OUTPUT */
39+
40+
/* Commands ... */
2141

2242
int32_t
2343
addint_cmd(int32_t argc, char** argv) {
@@ -28,7 +48,7 @@ addint_cmd(int32_t argc, char** argv) {
2848
i1 = lwshell_parse_long_long(argv[1]);
2949
i2 = lwshell_parse_long_long(argv[2]);
3050

31-
printf("%lld\r\n", i1 + i2);
51+
printf("%lld\r\n", (i1 + i2));
3252
return 0;
3353
}
3454

@@ -41,7 +61,7 @@ subint_cmd(int32_t argc, char** argv) {
4161
i1 = lwshell_parse_long_long(argv[1]);
4262
i2 = lwshell_parse_long_long(argv[2]);
4363

44-
printf("%lld\r\n", i1 - i2);
64+
printf("%lld\r\n", (i1 - i2));
4565
return 0;
4666
}
4767

@@ -71,34 +91,67 @@ subdbl_cmd(int32_t argc, char** argv) {
7191
return 0;
7292
}
7393

74-
/**
75-
* \brief Application output function
76-
* \param[in] str: String to print, null-terminated
77-
* \param[in] lw: LwSHELL instance
78-
*/
79-
void
80-
shell_output(const char* str, lwshell_t* lw) {
81-
(void)lw;
82-
printf("%s", str);
83-
if (*str == '\r') {
84-
printf("\n");
85-
}
94+
#if LWSHELL_CFG_USE_STATIC_COMMANDS
95+
96+
int32_t
97+
addintstatic_cmd(int32_t argc, char** argv) {
98+
printf("Static command...\r\n");
99+
return addint_cmd(argc, argv);
100+
}
101+
102+
int32_t
103+
subintstatic_cmd(int32_t argc, char** argv) {
104+
printf("Static command...\r\n");
105+
return subint_cmd(argc, argv);
86106
}
87107

108+
int32_t
109+
adddblstatic_cmd(int32_t argc, char** argv) {
110+
printf("Static command...\r\n");
111+
return adddbl_cmd(argc, argv);
112+
}
113+
114+
int32_t
115+
subdblstatic_cmd(int32_t argc, char** argv) {
116+
printf("Static command...\r\n");
117+
return subdbl_cmd(argc, argv);
118+
}
119+
120+
/*
121+
* Define some static commands, set as const.
122+
*/
123+
static const lwshell_cmd_t static_cmds[] = {
124+
{.name = "addintstatic", .desc = "Add 2 integers, a static implementation", .fn = addintstatic_cmd},
125+
{.name = "subintstatic", .desc = "Add 2 integers, a static implementation", .fn = subintstatic_cmd},
126+
{.name = "adddblstatic", .desc = "Add 2 integers, a static implementation", .fn = adddblstatic_cmd},
127+
{.name = "subdblstatic", .desc = "Add 2 integers, a static implementation", .fn = subdblstatic_cmd},
128+
};
129+
130+
#endif /* LWSHELL_CFG_USE_STATIC_COMMANDS */
131+
88132
/* Program entry point */
89133
int
90134
main(void) {
91135
/* Init library */
92136
lwshell_init();
93137

138+
#if LWSHELL_CFG_USE_OUTPUT
94139
/* Add optional output function for the purpose of the feedback */
95140
lwshell_set_output_fn(shell_output);
141+
#endif /* LWSHELL_CFG_USE_OUTPUT */
96142

143+
#if LWSHELL_CFG_USE_DYNAMIC_COMMANDS
97144
/* Define shell commands */
98145
lwshell_register_cmd("addint", addint_cmd, "Adds 2 integer numbers and prints them");
99146
lwshell_register_cmd("subint", subint_cmd, "Substitute 2 integer numbers and prints them");
100147
lwshell_register_cmd("adddbl", adddbl_cmd, "Adds 2 double numbers and prints them");
101148
lwshell_register_cmd("subdbl", subdbl_cmd, "Substitute 2 double numbers and prints them");
149+
#endif /* LWSHELL_CFG_USE_DYNAMIC_COMMANDS */
150+
151+
#if LWSHELL_CFG_USE_STATIC_COMMANDS
152+
/* Register static commands -> one-time call for all commands */
153+
lwshell_register_static_cmds(static_cmds, LWSHELL_ARRAYSIZE(static_cmds));
154+
#endif /* LWSHELL_CFG_USE_STATIC_COMMANDS */
102155

103156
/* User input to process every character */
104157
printf("Start entering your command and press enter...\r\n");

lwshell/src/include/lwshell/lwshell.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,32 @@ typedef struct {
9595
* \brief LwSHELL main structure
9696
*/
9797
typedef struct lwshell {
98+
#if LWSHELL_CFG_USE_OUTPUT || __DOXYGEN__
9899
lwshell_output_fn out_fn; /*!< Optional output function */
100+
#endif /* LWSHELL_CFG_USE_OUTPUT || __DOXYGEN__ */
99101
char buff[LWSHELL_CFG_MAX_INPUT_LEN + 1]; /*!< Shell command input buffer */
100102
size_t buff_ptr; /*!< Buffer pointer for input */
101103
int32_t argc; /*!< Number of arguments parsed in command */
102-
char* argv[LWSHELL_CFG_MAX_CMD_ARGS]; /*!< Array of all arguments */
103-
lwshell_cmd_t cmds[LWSHELL_CFG_MAX_CMDS]; /*!< Shell registered commands */
104-
size_t cmds_cnt; /*!< Number of registered commands */
104+
char* argv[LWSHELL_CFG_MAX_CMD_ARGS]; /*!< Array of pointers to all arguments */
105+
106+
#if LWSHELL_CFG_USE_DYNAMIC_COMMANDS || __DOXYGEN__
107+
lwshell_cmd_t dynamic_cmds[LWSHELL_CFG_MAX_DYNAMIC_CMDS]; /*!< Shell registered dynamic commands */
108+
size_t dynamic_cmds_cnt; /*!< Number of registered dynamic commands */
109+
#endif /* LWSHELL_CFG_USE_DYNAMIC_COMMANDS || __DOXYGEN__ */
110+
111+
#if LWSHELL_CFG_USE_STATIC_COMMANDS || __DOXYGEN__
112+
const lwshell_cmd_t* static_cmds; /*!< Pointer to an array of static commands */
113+
size_t static_cmds_cnt; /*!< Length of status commands array */
114+
#endif /* LWSHELL_CFG_USE_STATIC_COMMANDS || __DOXYGEN__ */
105115
} lwshell_t;
106116

107117
lwshellr_t lwshell_init_ex(lwshell_t* lwobj);
108118
lwshellr_t lwshell_set_output_fn_ex(lwshell_t* lwobj, lwshell_output_fn out_fn);
109119
lwshellr_t lwshell_register_cmd_ex(lwshell_t* lwobj, const char* cmd_name, lwshell_cmd_fn cmd_fn, const char* desc);
110120
lwshellr_t lwshell_input_ex(lwshell_t* lwobj, const void* in_data, size_t len);
111121

122+
lwshellr_t lwshell_register_static_cmds_ex(lwshell_t* lwobj, const lwshell_cmd_t* cmds, size_t cmds_len);
123+
112124
/**
113125
* \brief Initialize shell interface
114126
* \note It applies to default shell instance
@@ -132,6 +144,7 @@ lwshellr_t lwshell_input_ex(lwshell_t* lwobj, const void* in_data, size_t len);
132144
* \param[in] cmd_fn: Function to call on command match
133145
* \param[in] desc: Custom command description
134146
* \return \ref lwshellOK on success, member of \ref lwshellr_t otherwise
147+
* \note Available only when \ref LWSHELL_CFG_USE_DYNAMIC_COMMANDS is enabled
135148
*/
136149
#define lwshell_register_cmd(cmd_name, cmd_fn, desc) lwshell_register_cmd_ex(NULL, (cmd_name), (cmd_fn), (desc))
137150

@@ -144,6 +157,16 @@ lwshellr_t lwshell_input_ex(lwshell_t* lwobj, const void* in_data, size_t len);
144157
*/
145158
#define lwshell_input(in_data, len) lwshell_input_ex(NULL, (in_data), (len))
146159

160+
/**
161+
* \brief Register new command to shell
162+
* \note It applies to default shell instance
163+
* \param[in] cmds: Array of const static commands. It can be from non-volatile memory
164+
* \param[in] cmds_len: Length of array elements
165+
* \return \ref lwshellOK on success, member of \ref lwshellr_t otherwise
166+
* \note Available only when \ref LWSHELL_CFG_USE_STATIC_COMMANDS is enabled
167+
*/
168+
#define lwshell_register_static_cmds(cmds, cmds_len) lwshell_register_static_cmds_ex(NULL, (cmds), (cmds_len))
169+
147170
/**
148171
* \brief Parse input string as `integer`
149172
* \param[in] str: String to parse

lwshell/src/include/lwshell/lwshell_opt.h

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,49 @@ extern "C" {
5353
*/
5454

5555
/**
56-
* \brief Maximum number of different commands to be registered
57-
*
56+
* \brief Enables `1` or disables `0` dynamic command register with \ref lwshell_register_cmd or \ref lwshell_register_cmd_ex functions
57+
*
58+
* \note Set to `1` by default for backward compatibility
59+
* \sa LWSHELL_CFG_USE_STATIC_COMMANDS
60+
*/
61+
#ifndef LWSHELL_CFG_USE_DYNAMIC_COMMANDS
62+
#define LWSHELL_CFG_USE_DYNAMIC_COMMANDS 1
63+
#endif
64+
65+
/**
66+
* \brief Enables `1` or disables `0` static command registration.
67+
*
68+
* When enabled, a single register call is used where application
69+
* can pass constant array of the commands and respective callback functions.
70+
*
71+
* This allows RAM reduction as lookup tables can be stored in the non-volatile memory
72+
*
73+
* \note Set to `0` by default for backward compatibility
74+
* \sa LWSHELL_CFG_USE_DYNAMIC_COMMANDS
75+
*/
76+
#ifndef LWSHELL_CFG_USE_STATIC_COMMANDS
77+
#define LWSHELL_CFG_USE_STATIC_COMMANDS 0
78+
#endif
79+
80+
/**
81+
* \brief Maximum number of different dynamic registered commands
82+
*
83+
* \warning Deprecated and replaced with \ref LWSHELL_CFG_MAX_DYNAMIC_CMDS
84+
* \deprecated
5885
*/
5986
#ifndef LWSHELL_CFG_MAX_CMDS
6087
#define LWSHELL_CFG_MAX_CMDS 8
6188
#endif
6289

90+
/**
91+
* \brief Maximum number of different dynamic registered commands
92+
*
93+
* \note Used only when \ref LWSHELL_CFG_USE_DYNAMIC_COMMANDS is enabled
94+
*/
95+
#ifndef LWSHELL_CFG_MAX_DYNAMIC_CMDS
96+
#define LWSHELL_CFG_MAX_DYNAMIC_CMDS LWSHELL_CFG_MAX_CMDS
97+
#endif
98+
6399
/**
64100
* \brief Maximum characters for command line input
65101
*
@@ -71,8 +107,9 @@ extern "C" {
71107
#endif
72108

73109
/**
74-
* \brief Maximum characters for command name
75-
*
110+
* \brief Maximum characters for command name in bytes.
111+
*
112+
* \note Used only when \ref LWSHELL_CFG_USE_DYNAMIC_COMMANDS is enabled
76113
*/
77114
#ifndef LWSHELL_CFG_MAX_CMD_NAME_LEN
78115
#define LWSHELL_CFG_MAX_CMD_NAME_LEN 16
@@ -102,8 +139,8 @@ extern "C" {
102139
*
103140
* \ref LWSHELL_CFG_USE_OUTPUT must be enabled to use this feature
104141
*/
105-
#ifndef LWSHELL_CFG_USE_ENABLE_LIST_CMD
106-
#define LWSHELL_CFG_USE_ENABLE_LIST_CMD 0
142+
#ifndef LWSHELL_CFG_USE_LIST_CMD
143+
#define LWSHELL_CFG_USE_LIST_CMD 0
107144
#endif
108145

109146
/**

0 commit comments

Comments
 (0)