- Add putty compatibility
- Fix debug message with garbage cmd string if you press enter twice by zero initialising variables and checking when printing
This commit is contained in:
2023-12-05 20:11:50 +01:00
parent 5c5fda62e6
commit bd89deceb6

View File

@@ -8,6 +8,8 @@
#define MAX_TOKENS 10
#define MAX_CMD_LEN 50
#define LINUX
static const char* TAG = "tcp_cmd";
static uint32_t color_txt = 0xffffffff; // Store text color
static uint32_t color_bg = 0xff000000; // Store background color
@@ -75,7 +77,7 @@ void tcp_cmd_remove_newline(char* str, size_t len) {
char* tcp_cmd_remove_leading_space(char* str, size_t len) {
size_t i = 0;
if (str == NULL) {
if (str == NULL || str[0] == '\0' || len == 0) {
return NULL;
}
while (str[i] != '\0' && i < len) {
@@ -108,6 +110,9 @@ void tcp_cmd_str_tolower(char* str) {
* @return char* The next token
*/
char* tcp_cmd_get_next_token(char* input, const char* delimiters, char** next) {
if (input == NULL && *next == NULL) {
return NULL;
}
if (input == NULL) {
input = *next;
}
@@ -350,11 +355,11 @@ static bool tcp_cmd_parser(struct tcp_pcb* pcb, int argc, char** argv) {
*/
err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err) {
int argc = 0;
char cmd[MAX_CMD_LEN];
char* cmd_ptr;
char* argv[MAX_TOKENS];
char cmd[MAX_CMD_LEN] = {0};
char* cmd_ptr = NULL;
char* argv[MAX_TOKENS] = {0};
bool close_conn = false;
char* next;
char* next = NULL;
LWIP_UNUSED_ARG(arg);
@@ -382,7 +387,9 @@ err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err) {
tcp_recved(pcb, p->tot_len);
tcp_cmd_remove_newline(cmd, strlen(cmd));
LOG_INFO(TAG, "cmd: %s", cmd);
if (cmd[0] != '\0') {
LOG_INFO(TAG, "cmd: %s", cmd);
}
// Split string into tokens by delimiter (space)
cmd_ptr = tcp_cmd_remove_leading_space(cmd, strlen(cmd));
argv[0] = tcp_cmd_get_next_token(cmd_ptr, " ", &next);
@@ -408,9 +415,17 @@ err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err) {
if (close_conn) {
LOG_INFO(TAG, "Closing connection");
tcp_cmd_close(pcb);
#ifdef LINUX
} else {
tcp_cmd_write(pcb, "$>");
}
#else
} else if(argc > 0) {
tcp_cmd_write(pcb, "$>");
} else {
tcp_cmd_write(pcb, "");
}
#endif
defer:
pbuf_free(p);