BaseType_t DEBUG_ProcessCommand( const char * const pcCommandInput, char * pcWriteBuffer, size_t xWriteBufferLen ) { static const CLI_Definition_List_Item_t *pxCommand = NULL; BaseType_t xReturn = pdTRUE; const char *pcRegisteredCommandString; size_t xCommandStringLength; //Примечание: Эта функция не может использоваться повторно. Он не должен быть вызван из более чем одной задачи if( pxCommand == NULL ) { // Ищем команду в списке команд for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext ) { pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand; xCommandStringLength = strlen( pcRegisteredCommandString ); // чтобы не воспринять часть строки как команду Проверим пробел в конце или перед параметром if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) ) { if( strncmp( pcCommandInput, pcRegisteredCommandString, xCommandStringLength ) == 0 ) { // Команда была найдена. Проверьте это имеет ожидаемое количество параметров. Если Ожидаемое число параметров -1, т // о может быть переменное число параметров и проверка не производится. if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 ) { if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters ) { xReturn = pdFALSE; } } break; } } } } if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) ) { // Команда была найдена, но число параметров команды было неправильным. strncpy( pcWriteBuffer, "Incorrect command parameter(s). Enter \"help\"\r\n\r\n", xWriteBufferLen ); pxCommand = NULL; } else if( pxCommand != NULL ) { // вызываем колбэк найденой команды xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput ); // Если возвращение FALSE, то никакие дальнейшие строки не будут возвращены. if( xReturn == pdFALSE ) { pxCommand = NULL; } } else { // Команда небыла найдена strncpy( pcWriteBuffer, "\033[1;31mне поддерживается.\033[1;m\r\n\r\n", xWriteBufferLen ); xReturn = pdFALSE; } return xReturn; }
portBASE_TYPE FreeRTOS_CLIProcessCommand( const char * const pcCommandInput, char * pcWriteBuffer, size_t xWriteBufferLen ) { static const CLI_Definition_List_Item_t *pxCommand = NULL; portBASE_TYPE xReturn = pdTRUE; const char *pcRegisteredCommandString; size_t xCommandStringLength; /* Note: This function is not re-entrant. It must not be called from more thank one task. */ if( pxCommand == NULL ) { /* Search for the command string in the list of registered commands. */ for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext ) { pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand; xCommandStringLength = strlen( pcRegisteredCommandString ); /* To ensure the string lengths match exactly, so as not to pick up a sub-string of a longer command, check the byte after the expected end of the string is either the end of the string or a space before a parameter. */ if( ( pcCommandInput[ xCommandStringLength ] == ' ' ) || ( pcCommandInput[ xCommandStringLength ] == 0x00 ) ) { if( strncmp( pcCommandInput, pcRegisteredCommandString, xCommandStringLength ) == 0 ) { /* The command has been found. Check it has the expected number of parameters. If cExpectedNumberOfParameters is -1, then there could be a variable number of parameters and no check is made. */ if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 ) { if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters ) { xReturn = pdFALSE; } } break; } } } } if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) ) { /* The command was found, but the number of parameters with the command was incorrect. */ strncpy( pcWriteBuffer, "Incorrect command parameter(s). Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen ); pxCommand = NULL; } else if( pxCommand != NULL ) { /* Call the callback function that is registered to this command. */ xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput ); /* If xReturn is pdFALSE, then no further strings will be returned after this one, and pxCommand can be reset to NULL ready to search for the next entered command. */ if( xReturn == pdFALSE ) { pxCommand = NULL; } } else { /* pxCommand was NULL, the command was not found. */ strncpy( pcWriteBuffer, "Command not recognised. Enter 'help' to view a list of available commands.\r\n\r\n", xWriteBufferLen ); xReturn = pdFALSE; } return xReturn; }
portBASE_TYPE xCmdIntProcessCommand( const signed char * const pcCommandInput, signed char * pcWriteBuffer, size_t xWriteBufferLen ) { static const xCommandLineInputListItem *pxCommand = NULL; portBASE_TYPE xReturn = pdTRUE; const signed char *pcRegisteredCommandString; /* Note: This function is not re-entrant. It must not be called from more thank one task. */ if( pxCommand == NULL ) { /* Search for the command string in the list of registered commands. */ for( pxCommand = &xRegisteredCommands; pxCommand != NULL; pxCommand = pxCommand->pxNext ) { pcRegisteredCommandString = pxCommand->pxCommandLineDefinition->pcCommand; if( strncmp( ( const char * ) pcCommandInput, ( const char * ) pcRegisteredCommandString, strlen( ( const char * ) pcRegisteredCommandString ) ) == 0 ) { /* The command has been found. Check it has the expected number of parameters. If cExpectedNumberOfParameters is -1, then there could be a variable number of parameters and no check is made. */ if( pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters >= 0 ) { if( prvGetNumberOfParameters( pcCommandInput ) != pxCommand->pxCommandLineDefinition->cExpectedNumberOfParameters ) { xReturn = pdFALSE; } } break; } } } if( ( pxCommand != NULL ) && ( xReturn == pdFALSE ) ) { /* The command was found, but the number of parameters with the command was incorrect. */ (void)strncpy( ( char * ) pcWriteBuffer, "Incorrect command parameter(s). Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen ); pxCommand = NULL; } else if( pxCommand != NULL ) { /* Call the callback function that is registered to this command. */ xReturn = pxCommand->pxCommandLineDefinition->pxCommandInterpreter( pcWriteBuffer, xWriteBufferLen, pcCommandInput ); /* If xReturn is pdFALSE, then no further strings will be returned after this one, and pxCommand can be reset to NULL ready to search for the next entered command. */ if( xReturn == pdFALSE ) { pxCommand = NULL; } } else { /* pxCommand was NULL, the command was not found. */ (void)strncpy( ( char * ) pcWriteBuffer, ( const char * const ) "Command not recognised. Enter \"help\" to view a list of available commands.\r\n\r\n", xWriteBufferLen ); xReturn = pdFALSE; } return xReturn; }