From 1df9097e326a221851ba36202cd96f32fc1f4edd Mon Sep 17 00:00:00 2001 From: ProDrone Date: Wed, 13 May 2015 21:16:42 +0200 Subject: [PATCH 1/3] Added # to versionstring and strip comments Prepended the version string with a # to avoid the CLI from interpreting the version string as a command when pasting a dump file back (restore in CLI). Strip comments starting with `# comment` from lines. This is to allow adding comments to CLI dumped backup files. For this i have an automated addition of comments and/or manual comments by the user in mind. --- src/main/io/serial_cli.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/io/serial_cli.c b/src/main/io/serial_cli.c index f4497417f1..256bb6ac7d 100644 --- a/src/main/io/serial_cli.c +++ b/src/main/io/serial_cli.c @@ -1644,7 +1644,7 @@ static void cliVersion(char *cmdline) { UNUSED(cmdline); - printf("Cleanflight/%s %s %s / %s (%s)", + printf("# Cleanflight/%s %s %s / %s (%s)", targetName, FC_VERSION_STRING, buildDate, @@ -1710,6 +1710,13 @@ void cliProcess(void) clicmd_t target; cliPrint("\r\n"); + // Strip comment starting with # from line + char *p = cliBuffer; + p = strchr(++p, '#'); + if (NULL != p) { + bufferIndex = (uint32_t)(p - cliBuffer); + } + // Strip trailing whitespace while (bufferIndex > 0 && cliBuffer[bufferIndex - 1] == ' ') { bufferIndex--; From 0ee67a521be5d4f369a4310f06a912ba5bf14a64 Mon Sep 17 00:00:00 2001 From: ProDrone Date: Thu, 14 May 2015 00:11:31 +0200 Subject: [PATCH 2/3] Optimized comment stripper code --- src/main/io/serial_cli.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/io/serial_cli.c b/src/main/io/serial_cli.c index 256bb6ac7d..668edc9af7 100644 --- a/src/main/io/serial_cli.c +++ b/src/main/io/serial_cli.c @@ -1712,19 +1712,20 @@ void cliProcess(void) // Strip comment starting with # from line char *p = cliBuffer; - p = strchr(++p, '#'); + p = strchr(p, '#'); if (NULL != p) { bufferIndex = (uint32_t)(p - cliBuffer); } - // Strip trailing whitespace - while (bufferIndex > 0 && cliBuffer[bufferIndex - 1] == ' ') { - bufferIndex--; - } + // Process non-empty lines + if (bufferIndex > 0) { + // Strip trailing whitespace + while (bufferIndex > 0 && cliBuffer[bufferIndex - 1] == ' ') { + bufferIndex--; + } - cliBuffer[bufferIndex] = 0; // null terminate + cliBuffer[bufferIndex] = 0; // null terminate - if (cliBuffer[0] != '#') { target.name = cliBuffer; target.param = NULL; From 6d9394d783365d93df66b7d34fca5fded91a7458 Mon Sep 17 00:00:00 2001 From: ProDrone Date: Thu, 14 May 2015 00:58:51 +0200 Subject: [PATCH 3/3] Optimized comment stripper code (again) --- src/main/io/serial_cli.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/io/serial_cli.c b/src/main/io/serial_cli.c index 668edc9af7..4dbcf8d172 100644 --- a/src/main/io/serial_cli.c +++ b/src/main/io/serial_cli.c @@ -1717,15 +1717,14 @@ void cliProcess(void) bufferIndex = (uint32_t)(p - cliBuffer); } + // Strip trailing whitespace + while (bufferIndex > 0 && cliBuffer[bufferIndex - 1] == ' ') { + bufferIndex--; + } + // Process non-empty lines if (bufferIndex > 0) { - // Strip trailing whitespace - while (bufferIndex > 0 && cliBuffer[bufferIndex - 1] == ' ') { - bufferIndex--; - } - cliBuffer[bufferIndex] = 0; // null terminate - target.name = cliBuffer; target.param = NULL; @@ -1734,10 +1733,10 @@ void cliProcess(void) cmd->func(cliBuffer + strlen(cmd->name) + 1); else cliPrint("Unknown command, try 'help'"); + bufferIndex = 0; } memset(cliBuffer, 0, sizeof(cliBuffer)); - bufferIndex = 0; // 'exit' will reset this flag, so we don't need to print prompt again if (!cliMode)