Skip to content

Commit c898667

Browse files
committed
Merge branch 'develop'
2 parents 75dca55 + e39db64 commit c898667

File tree

6 files changed

+44
-14
lines changed

6 files changed

+44
-14
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Develop
44

5+
## v1.1.0
6+
7+
- Add support for `listcmd` to print all registered commands
8+
- Optimize code and remove unnecessary brackets
9+
510
## v1.0.0
611

712
- First stable release

dev/VisualStudio/lwshell_opts.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* This file is part of Lightweight shell library.
3030
*
3131
* Author: Tilen MAJERLE <[email protected]>
32-
* Version: v1.0.0
32+
* Version: v1.1.0
3333
*/
3434
#ifndef LWSHELL_HDR_OPTS_H
3535
#define LWSHELL_HDR_OPTS_H
@@ -38,6 +38,7 @@
3838

3939
#include "windows.h"
4040

41-
#define LWSHELL_CFG_USE_OUTPUT 1
41+
#define LWSHELL_CFG_USE_OUTPUT 1
42+
#define LWSHELL_CFG_USE_ENABLE_LIST_CMD 1
4243

4344
#endif /* LWSHELL_HDR_OPTS_H */

lwshell/src/include/lwshell/lwshell.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* This file is part of LwSHELL - Lightweight shell library.
3030
*
3131
* Author: Tilen MAJERLE <[email protected]>
32-
* Version: v1.0.0
32+
* Version: v1.1.0
3333
*/
3434
#ifndef LWSHELL_HDR_H
3535
#define LWSHELL_HDR_H

lwshell/src/include/lwshell/lwshell_opt.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/**
22
* \file lwshell_opt.h
33
* \brief LwSHELL options
44
*/
@@ -29,7 +29,7 @@
2929
* This file is part of LwSHELL - Lightweight shell library.
3030
*
3131
* Author: Tilen MAJERLE <[email protected]>
32-
* Version: v1.0.0
32+
* Version: v1.1.0
3333
*/
3434
#ifndef LWSHELL_HDR_OPT_H
3535
#define LWSHELL_HDR_OPT_H
@@ -97,6 +97,15 @@ extern "C" {
9797
#define LWSHELL_CFG_USE_OUTPUT 1
9898
#endif
9999

100+
/**
101+
* \brief Enables `1` or disables `0` generic ˙listcmd` command to list of registered commands
102+
*
103+
* \ref LWSHELL_CFG_USE_OUTPUT must be enabled to use this feature
104+
*/
105+
#ifndef LWSHELL_CFG_USE_ENABLE_LIST_CMD
106+
#define LWSHELL_CFG_USE_ENABLE_LIST_CMD 0
107+
#endif
108+
100109
/**
101110
* \}
102111
*/

lwshell/src/include/lwshell/lwshell_opts_template.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* This file is part of LwSHELL - Lightweight shell library.
3030
*
3131
* Author: Tilen MAJERLE <[email protected]>
32-
* Version: v1.0.0
32+
* Version: v1.1.0
3333
*/
3434
#ifndef LWSHELL_HDR_OPTS_H
3535
#define LWSHELL_HDR_OPTS_H

lwshell/src/lwshell/lwshell.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@
2929
* This file is part of LwSHELL - Lightweight shell library.
3030
*
3131
* Author: Tilen MAJERLE <[email protected]>
32-
* Version: v1.0.0
32+
* Version: v1.1.0
3333
*/
3434
#include <string.h>
3535
#include "lwshell/lwshell.h"
3636

37+
/* Check enabled features */
38+
#if LWSHELL_CFG_USE_ENABLE_LIST_CMD && !LWSHELL_CFG_USE_OUTPUT
39+
#error "To use list command feature, LWSHELL_CFG_USE_OUTPUT must be enabled"
40+
#endif
41+
3742
/* Default characters */
3843
#define LWSHELL_ASCII_NULL 0x00/*!< Null character */
3944
#define LWSHELL_ASCII_BACKSPACE 0x08/*!< Backspace */
@@ -87,7 +92,6 @@ static lwshell_t shell;
8792
static void
8893
prv_parse_input(lwshell_t* lw) {
8994
size_t s_len;
90-
char ch, prev_ch;
9195
char* str;
9296

9397
lw = LWSHELL_GET_LW(lw);
@@ -100,14 +104,11 @@ prv_parse_input(lwshell_t* lw) {
100104

101105
/* Must be more than `1` character since we have to include end of line */
102106
if (lw->buff_ptr > 0) {
103-
uint8_t in_quote = 0;
104-
105107
/* Set default values */
106108
lw->argc = 0;
107109
lw->argv[0] = lw->buff;
108110

109111
/* Process complete input */
110-
prev_ch = '\0';
111112
str = lw->buff;
112113

113114
/* Process complete string */
@@ -140,7 +141,7 @@ prv_parse_input(lwshell_t* lw) {
140141
}
141142
} else {
142143
lw->argv[lw->argc++] = str; /* Set start of argument directly on character */
143-
while ((*str != ' ' && *str != '\0')) {
144+
while (*str != ' ' && *str != '\0') {
144145
if (*str == '"') { /* Quote should not be here... */
145146
*str = '\0'; /* ...add NULL termination to end token */
146147
}
@@ -159,10 +160,12 @@ prv_parse_input(lwshell_t* lw) {
159160
/* Check for command */
160161
if (lw->argc > 0 && cmds_cnt > 0) {
161162
lwshell_cmd_t* c = NULL;
163+
size_t arg_len = strlen(lw->argv[0]);
164+
162165
/* Process all commands */
163166
for (size_t i = 0; i < cmds_cnt; ++i) {
164-
if (strlen(lw->argv[0]) == strlen(cmds[i].name)
165-
&& strncmp(cmds[i].name, lw->argv[0], strlen(lw->argv[0])) == 0) {
167+
if (arg_len == strlen(cmds[i].name)
168+
&& strncmp(cmds[i].name, lw->argv[0], arg_len) == 0) {
166169
c = &cmds[i];
167170
break;
168171
}
@@ -178,6 +181,18 @@ prv_parse_input(lwshell_t* lw) {
178181
} else {
179182
c->fn(lw->argc, lw->argv);
180183
}
184+
#if LWSHELL_CFG_USE_ENABLE_LIST_CMD
185+
} else if (strncmp(lw->argv[0], "listcmd", 7) == 0) {
186+
LW_OUTPUT(lw, "List of registered commands\r\n");
187+
for (size_t i = 0; i < cmds_cnt; ++i) {
188+
LW_OUTPUT(lw, cmds[i].name);
189+
LW_OUTPUT(lw, "\t\t\t");
190+
LW_OUTPUT(lw, cmds[i].desc);
191+
LW_OUTPUT(lw, "\r\n");
192+
}
193+
#endif /* LWSHELL_CFG_USE_ENABLE_LIST_CMD */
194+
} else {
195+
LW_OUTPUT(lw, "Unknown command\r\n");
181196
}
182197
}
183198
}

0 commit comments

Comments
 (0)