1919
2020#include " cli/cli.hpp"
2121
22+ #include " cli/cli_commands.hpp"
23+ #include " comm/stream_group.hpp"
24+ #include " flash/lfs_custom.hpp"
25+ #include " util/log.h"
26+
2227#include < strings.h>
28+
2329#include < cctype>
2430#include < cstdarg>
2531#include < cstdio>
2632#include < cstring>
2733
28- #include " cli/cli_commands.hpp"
29- #include " comm/stream_group.hpp"
30- #include " flash/lfs_custom.hpp"
31- #include " util/log.h"
32-
33- #define CLI_IN_BUFFER_SIZE 128
34- #define CLI_OUT_BUFFER_SIZE 256
34+ constexpr uint16_t CLI_IN_BUFFER_SIZE = 128 ;
35+ constexpr uint16_t CLI_OUT_BUFFER_SIZE = 256 ;
3536
37+ // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
3638static uint32_t buffer_index = 0 ;
3739
3840static char cli_buffer[CLI_IN_BUFFER_SIZE];
3941static char old_cli_buffer[CLI_IN_BUFFER_SIZE];
42+ // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
4043
4144static void cli_print_error_va (const char *cmdName, const char *format, va_list va);
4245static void cli_print_error (const char *cmdName, const char *format, ...) __attribute__((format(printf, 2 , 3 )));
@@ -59,7 +62,8 @@ void get_min_max(const cli_value_t *var, int *min, int *max) {
5962 }
6063}
6164
62- void cli_print (const char *str) { stream_write (USB_SG.out , (uint8_t *)str, strlen (str)); }
65+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
66+ void cli_print (const char *str) { stream_write (USB_SG.out , reinterpret_cast <const uint8_t *>(str), strlen (str)); }
6367
6468static void cli_prompt () { cli_printf (" \r\n ^._.^:%s> " , cwd); }
6569
@@ -126,25 +130,26 @@ char *skip_space(char *buffer) {
126130 return buffer;
127131}
128132
133+ // NOLINTBEGIN(readability-implicit-bool-conversion,bugprone-narrowing-conversions)
129134static char *check_command (char *cmdline, const char *command) {
130- if (! strncasecmp (cmdline, command, strlen (command)) // command names match
131- && (isspace (( unsigned ) cmdline[strlen (command)]) || cmdline[strlen (command)] == 0 )) {
135+ if (( strncasecmp (cmdline, command, strlen (command)) == 0 ) // command names match
136+ && (isspace (static_cast < uint32_t >( cmdline[strlen (command)]) ) || cmdline[strlen (command)] == 0 )) {
132137 return skip_space (cmdline + strlen (command) + 1 );
133- } else {
134- return nullptr ;
135138 }
139+ return nullptr ;
136140}
141+ // NOLINTEND(readability-implicit-bool-conversion,bugprone-narrowing-conversions)
137142
138143static void process_character (const char c) {
139- if (buffer_index && (c == ' \n ' || c == ' \r ' )) {
144+ if (( buffer_index > 0 ) && (c == ' \n ' || c == ' \r ' )) {
140145 // enter pressed
141146 cli_print_linefeed ();
142147
143148 // Strip comment starting with # from line
144149 char *p = cli_buffer;
145150 p = strchr (p, ' #' );
146151 if (nullptr != p) {
147- buffer_index = ( uint32_t ) (p - cli_buffer);
152+ buffer_index = static_cast < uint32_t > (p - cli_buffer);
148153 }
149154 // Strip trailing whitespace
150155 while (buffer_index > 0 && cli_buffer[buffer_index - 1 ] == ' ' ) {
@@ -155,11 +160,13 @@ static void process_character(const char c) {
155160 if (buffer_index > 0 ) {
156161 cli_buffer[buffer_index] = 0 ; // null terminate
157162
158- const clicmd_t *cmd;
163+ const clicmd_t *cmd{ nullptr } ;
159164 char *options = nullptr ;
160165 for (cmd = cmd_table; cmd < cmd_table + NUM_CLI_COMMANDS; cmd++) {
161166 options = check_command (cli_buffer, cmd->name );
162- if (options) break ;
167+ if (options != nullptr ) {
168+ break ;
169+ }
163170 }
164171 if (cmd < cmd_table + NUM_CLI_COMMANDS) {
165172 cmd->cli_command (cmd->name , options);
@@ -175,36 +182,43 @@ static void process_character(const char c) {
175182 // 'exit' will reset this flag, so we don't need to print prompt again
176183
177184 } else if (buffer_index < sizeof (cli_buffer) && c >= 32 && c <= 126 ) {
178- if (!buffer_index && c == ' ' ) return ; // Ignore leading spaces
185+ if ((buffer_index == 0 ) && (c == ' ' )) {
186+ return ; // Ignore leading spaces
187+ }
179188 cli_buffer[buffer_index++] = c;
180189 cli_write (c);
181190 }
182191}
183192
193+ // NOLINTNEXTLINE(readability-function-cognitive-complexity)
184194static void process_character_interactive (const char c) {
185195 // We ignore a few characters, this is only used for the up arrow
186196 static uint16_t ignore = 0 ;
187- if (ignore) {
197+ if (ignore > 0 ) {
188198 ignore--;
189199 return ;
190200 }
191201 if (c == ' \t ' || c == ' ?' ) {
192202 // do tab completion
193- const clicmd_t *cmd, *pstart = nullptr , *pend = nullptr ;
203+ const clicmd_t *cmd{nullptr };
204+ const clicmd_t *pstart{nullptr };
205+ const clicmd_t *pend{nullptr };
194206 uint32_t i = buffer_index;
195207 for (cmd = cmd_table; cmd < cmd_table + NUM_CLI_COMMANDS; cmd++) {
196- if (buffer_index && (strncasecmp (cli_buffer, cmd->name , buffer_index) != 0 )) {
208+ if (( buffer_index != 0 ) && (strncasecmp (cli_buffer, cmd->name , buffer_index) != 0 )) {
197209 continue ;
198210 }
199- if (! pstart) {
211+ if (pstart == nullptr ) {
200212 pstart = cmd;
201213 }
202214 pend = cmd;
203215 }
204- if (pstart) { /* Buffer matches one or more commands */
216+ if (pstart != nullptr ) { /* Buffer matches one or more commands */
205217 for (;; buffer_index++) {
206- if (pstart->name [buffer_index] != pend->name [buffer_index]) break ;
207- if (!pstart->name [buffer_index] && buffer_index < sizeof (cli_buffer) - 2 ) {
218+ if (pstart->name [buffer_index] != pend->name [buffer_index]) {
219+ break ;
220+ }
221+ if ((pstart->name [buffer_index] == ' \0 ' ) && (buffer_index < sizeof (cli_buffer) - 2 )) {
208222 /* Unambiguous -- append a space */
209223 cli_buffer[buffer_index++] = ' ' ;
210224 cli_buffer[buffer_index] = ' \0 ' ;
@@ -213,7 +227,7 @@ static void process_character_interactive(const char c) {
213227 cli_buffer[buffer_index] = pstart->name [buffer_index];
214228 }
215229 }
216- if (! buffer_index || pstart != pend) {
230+ if (( buffer_index == 0 ) || ( pstart != pend) ) {
217231 /* Print list of ambiguous matches */
218232 cli_print (" \r\n\033 [K" );
219233 for (cmd = pstart; cmd <= pend && cmd != nullptr ; cmd++) {
@@ -223,7 +237,9 @@ static void process_character_interactive(const char c) {
223237 cli_prompt ();
224238 i = 0 ; /* Redraw prompt */
225239 }
226- for (; i < buffer_index; i++) cli_write (cli_buffer[i]);
240+ for (; i < buffer_index; i++) {
241+ cli_write (cli_buffer[i]);
242+ }
227243 } else if (c == 4 ) {
228244 // CTRL-D - clear screen
229245 cli_print (" \033 [2J\033 [1;1H" );
@@ -237,18 +253,21 @@ static void process_character_interactive(const char c) {
237253 }
238254 } else if (c == ' \b ' ) {
239255 // backspace
240- if (buffer_index) {
256+ if (buffer_index > 0 ) {
241257 cli_buffer[--buffer_index] = 0 ;
242258 cli_print (" \010 \010 " );
243259 }
244260 } else if (c == 27 ) { // ESC character is called from the up arrow, we only look at the first of 3 characters
245261 // up arrow
246- while (buffer_index) {
262+ while (buffer_index > 0 ) {
247263 cli_buffer[--buffer_index] = 0 ;
248264 cli_print (" \010 \010 " );
249265 }
266+ // NOLINTNEXTLINE(modernize-loop-convert)
250267 for (uint32_t i = 0 ; i < sizeof (old_cli_buffer); i++) {
251- if (old_cli_buffer[i] == 0 ) break ;
268+ if (old_cli_buffer[i] == 0 ) {
269+ break ;
270+ }
252271 process_character (old_cli_buffer[i]);
253272 }
254273 // Ignore the following characters
@@ -258,11 +277,11 @@ static void process_character_interactive(const char c) {
258277 }
259278}
260279
261- void cli_process (void ) {
280+ void cli_process () {
262281 while (stream_length (USB_SG.in ) > 0 ) {
263282 uint8_t ch = 0 ;
264283 if (stream_read_byte (USB_SG.in , &ch)) {
265- process_character_interactive (ch );
284+ process_character_interactive (static_cast < char >(ch) );
266285 }
267286 }
268287}
@@ -276,27 +295,27 @@ static void print_value_pointer(const char *cmdName, const cli_value_t *var, con
276295 default :
277296 case VAR_UINT8:
278297 // uint8_t array
279- cli_printf (" %d" , (( uint8_t *) valuePointer)[i]);
298+ cli_printf (" %d" , static_cast < const uint8_t *>( valuePointer)[i]);
280299 break ;
281300
282301 case VAR_INT8:
283302 // int8_t array
284- cli_printf (" %d" , (( int8_t *) valuePointer)[i]);
303+ cli_printf (" %d" , static_cast < const int8_t *>( valuePointer)[i]);
285304 break ;
286305
287306 case VAR_UINT16:
288307 // uin16_t array
289- cli_printf (" %d" , (( uint16_t *) valuePointer)[i]);
308+ cli_printf (" %d" , static_cast < const uint16_t *>( valuePointer)[i]);
290309 break ;
291310
292311 case VAR_INT16:
293312 // int16_t array
294- cli_printf (" %d" , (( int16_t *) valuePointer)[i]);
313+ cli_printf (" %d" , static_cast < const int16_t *>( valuePointer)[i]);
295314 break ;
296315
297316 case VAR_UINT32:
298317 // uin32_t array
299- cli_printf (" %lu" , (( uint32_t *) valuePointer)[i]);
318+ cli_printf (" %lu" , static_cast < const uint32_t *>( valuePointer)[i]);
300319 break ;
301320 }
302321
@@ -309,23 +328,25 @@ static void print_value_pointer(const char *cmdName, const cli_value_t *var, con
309328
310329 switch (var->type & VALUE_TYPE_MASK) {
311330 case VAR_UINT8:
312- value = *( uint8_t *) valuePointer;
331+ value = *static_cast < const uint8_t *>( valuePointer) ;
313332
314333 break ;
315334 case VAR_INT8:
316- value = *(int8_t *)valuePointer;
335+ // NOLINTNEXTLINE(bugprone-signed-char-misuse)
336+ value = *static_cast <const int8_t *>(valuePointer);
317337
318338 break ;
319339 case VAR_UINT16:
320- value = *( uint16_t *) valuePointer;
340+ value = *static_cast < const uint16_t *>( valuePointer) ;
321341
322342 break ;
323343 case VAR_INT16:
324- value = *( int16_t *) valuePointer;
344+ value = *static_cast < const int16_t *>( valuePointer) ;
325345
326346 break ;
327347 case VAR_UINT32:
328- value = *(uint32_t *)valuePointer;
348+ // NOLINTNEXTLINE(bugprone-narrowing-conversions)
349+ value = *static_cast <const uint32_t *>(valuePointer);
329350
330351 break ;
331352 }
@@ -334,15 +355,15 @@ static void print_value_pointer(const char *cmdName, const cli_value_t *var, con
334355 switch (var->type & VALUE_MODE_MASK) {
335356 case MODE_DIRECT:
336357 if ((var->type & VALUE_TYPE_MASK) == VAR_UINT32) {
337- cli_printf (" %lu" , ( uint32_t ) value);
338- if (( uint32_t ) value > var->config .u32_max ) {
358+ cli_printf (" %lu" , static_cast < uint32_t >( value) );
359+ if (static_cast < uint32_t >( value) > var->config .u32_max ) {
339360 value_is_corrupted = true ;
340361 } else if (full) {
341362 cli_printf (" 0 %lu" , var->config .u32_max );
342363 }
343364 } else {
344- int min;
345- int max;
365+ int min{ 0 } ;
366+ int max{ 0 } ;
346367 get_min_max (var, &min, &max);
347368
348369 cli_printf (" %d" , value);
@@ -361,14 +382,16 @@ static void print_value_pointer(const char *cmdName, const cli_value_t *var, con
361382 }
362383 break ;
363384 case MODE_BITSET:
364- if (value & 1 << var->config .bitpos ) {
385+ if (static_cast < bool >(( static_cast < uint32_t >( value) & 1U ) << var->config .bitpos ) ) {
365386 cli_printf (" ON" );
366387 } else {
367388 cli_printf (" OFF" );
368389 }
369390 break ;
370391 case MODE_STRING:
371- cli_printf (" %s" , (strlen ((char *)valuePointer) == 0 ) ? " -" : (char *)valuePointer);
392+ cli_printf (" %s" , (strlen (static_cast <const char *>(valuePointer)) == 0 )
393+ ? " -"
394+ : static_cast <const char *>(valuePointer));
372395 break ;
373396 }
374397
@@ -407,7 +430,7 @@ uint16_t cli_get_setting_index(char *name, uint8_t length) {
407430
408431const char *next_arg (const char *current_arg) {
409432 const char *ptr = strchr (current_arg, ' ' );
410- while (ptr && *ptr == ' ' ) {
433+ while (ptr != nullptr && *ptr == ' ' ) {
411434 ptr++;
412435 }
413436 return ptr;
0 commit comments