int command_main(int argc, char *argv[]) { char *p; int size; int num = 0; char *cmd[COMMAND_WORD_NUM]; int cmd_len = 0; int i, j; send_use(SERIAL_DEFAULT_DEVICE); command_init( cmd ); file_init(); while (1) { send_write("command> "); /* プロンプト表示 */ /* コマンド格納バッファを0クリアする */ for( j = 0; j < COMMAND_WORD_NUM; j++ ){ memset( cmd[j], 0, COMMAND_WORD_LENGTH ); } /* コンソールからの受信文字列を受け取る */ kz_recv(MSGBOX_ID_CONSINPUT, &size, &p); p[size] = '\0'; /* 入力文字列を分解する */ if( ( num = command_split( p, cmd ) ) == 0 ){ send_write("input nothing\n"); continue; } /* コマンドの実行 */ cmd_len = strlen( cmd[0] ); for( i = 0; command_table[i].cmd != NULL; i++ ){ if( !strncmp( cmd[0], command_table[i].cmd, (cmd_len>command_table[i].size?cmd_len:command_table[i].size) ) ){ command_table[i].func( num, cmd ); break; } } if( command_table[i].cmd == NULL ){ send_write("unknown.\n"); } kz_kmfree(p); } command_terminate( cmd ); return 0; }
int command_doline(buffer_t* out, command_t* commands, const char* line) { uint8_t argc; char** argv = NULL; int error; command_t* command; if (NULL == commands || NULL == line) return EFAULT; argc = command_split(line, &argv); if (NULL == argv) return errno; if (!argc || NULL == (command = command_find(commands, argv[0]))) { command_sfree(&argv); command_report_error(out, commands, ENOCMD); return ENOCMD; } error = command_do(out, command, argc, argv); command_sfree(&argv); return error; }