majerle / lwshell Goto Github PK
View Code? Open in Web Editor NEWLightweight shell implementation for embedded systems
License: MIT License
Lightweight shell implementation for embedded systems
License: MIT License
/* Must be more than `1` character since we have to include end of line */
if (lw->buff_ptr > 0) {
/* Set default values */
lw->argc = 0;
lw->argv[0] = lw->buff;
/* Process complete input */
str = lw->buff;
/* Process complete string */
lw->argc = 0;
while (*str != '\0') {
Line 112 in lwshell.c
You clear argc, then clear it again before anything could have changed.
As topic.
I have a primary shell I use for on-module CLI access, as well as another shell that allows access to a bootloader CLI of off-board module. This latter terminal has different inputs and a tokenizer.
It would be nice to set both shells up differently and use this framework to address either.
Hi,
Really great simple shell but for me there is one handling really bad. So if as an example the user inputs some command (calc
) using a terminal which gives /r/n at the end of the command then the input string looks like this
calc 1 2/r/n
So then in the handle function of calc i do some printing to the terminal. But after that /r and /n will be printed also i think that is not a behavior for everyone.
So a quick fix for me was change parse input. And if the parser found some command i will ignore the output of CR or LF but maybe there is a better solution.
best regards,
mathias
@MaJerle - I can do a pull request for the below issue, but want to know your preference first.
when the input buffer is full, we get an overflow while parsing the args:
} else {
lw->argv[lw->argc++] = str; /* Set start of argument directly on character */
while (*str != ' ' && *str != '\0') {
if (*str == '"') { /* Quote should not be here... */
*str = '\0'; /* ...add NULL termination to end token */
}
++str;
}
*str = '\0';
++str; /* <<<==== here str increment over the input buff when the above while stop on the last byte of the buff */
}
Since this is a light shell, I assume you want to keep performance as good as possible:
1.
add 2 extra bytes (instead of +1) to the input buffer and change the ADD macro. this way we guarantee an extra NULL at the end:
char buff[LWSHELL_CFG_MAX_INPUT_LEN + 2]; /*!< Shell command input buffer */
/* Add character to instance */
#define LWSHELL_ADD_CH(lw, ch) do { \
if ((lw)->buff_ptr < (LWSHELL_ARRAYSIZE(lw->buff) - 2)) { \
(lw)->buff[(lw)->buff_ptr] = ch; \
(lw)->buff[++(lw)->buff_ptr] = '\0'; \
} \
} while (0)
2.
Stop the loop with some pointer calculation:
/* Check for number of arguments */
if (lw->argc == LWSHELL_ARRAYSIZE(lw->argv)) {
break;
}
/* Consume all input buffer... */
if (str - lw->buff >= sizeof((lw)->buff)) {
break;
}
}
In minimal example code, input string const char* input_str = "mycmd param1 \"param 2 with space\"";
is missing a newline or carriage return character. Until one of these characters is detected by lwshell_input
function, parsing will not begin and the mycmd_fn
function in example will not run.
Changing the input string to const char* input_str = "mycmd param1 \"param 2 with space\"\n";
(or \r
) solves the issue and produces the expected behaviour.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.