Linux测试程序交互实现

void sig_handler(int signo) 
{
    std::cout << "oops! stop!!!" << std::endl;
    _exit(0);
}

int main(int argc, char **argv)
{

    PadSim pad_sim ;
    pad_sim.init(node);

    signal( SIGINT, sig_handler); /*注册ctrl+c 退出函数*/
    int value = system("stty erase ^H");/* 退格键不回显 */

    char cmd[256];
    std::cout <<"***************************************************************" << std::endl;
    std::cout <<"The command list:" << std::endl;
    std::cout << std::left << std::setw(20) << "h"
                        << " This help text\n"

                        << std::setw(20) << "auto"
                        << " Start: switch control mode to auto \n"
                        << std::setw(20) << "manual"
                        << " Stop:  switch control mode to manual\n"

                        << std::setw(20) << "start"
                        << " Start: switch task mode start \n"
                        << std::setw(20) << "stop"
                        << " Stop:  switch task mode to stop\n"
                        << std::setw(20) << "pause"
                        << " Pause: switch task mode to pause\n"
                        << std::setw(20) << "continue"
                        << " Pause: switch task mode to continue\n"
                        << std::setw(20) << "shutdown"
                        << " Shut down the program\n"
                        << std::flush;
    std::cout <<"***************************************************************" << std::endl;
    std::cout << "> ";
    while (std::cin.getline(cmd, sizeof(cmd))) 
    {
        std::string strCmd(cmd);
        if (strCmd == "shutdown") 
        {
            break;
        } 
        else if (strCmd == "h")
        {
            std::cout << std::left << std::setw(20) << "h"
                        << " This help text\n"

                        << std::setw(20) << "auto"
                        << " Start: switch control mode to auto \n"
                        << std::setw(20) << "manual"
                        << " Stop:  switch control mode to manual\n"

                        << std::setw(20) << "start"
                        << " Start: switch task mode to start \n"
                        << std::setw(20) << "stop"
                        << " Stop:  switch task mode to stop\n"
                        << std::setw(20) << "pause"
                        << " Pause: switch task mode to pause\n"
                        << std::setw(20) << "continue"
                        << " Pause: switch task mode to continue\n"
                        << std::setw(20) << "shutdown"
                        << " Shut down the program\n"
                        << std::flush;
        } 
        else if (strCmd == "auto") 
        {
            pad_sim.pad_send_message(1008);
        } 
        else if (strCmd == "manual") 
        {
            pad_sim.pad_send_message(1009);
        } 
        else if (strCmd == "start") 
        {
            pad_sim.pad_send_message(1007);
        } 
        else if (strCmd == "stop") 
        {
            pad_sim.pad_send_message(1006);
        } 
        else if (strCmd == "pause") 
        {
            pad_sim.pad_send_message(1004);
        } 
        else if (strCmd == "continue") 
        {
            pad_sim.pad_send_message(1005);
        } 
        else 
        {
            std::cerr << "Error: unknown commamd" << std::endl;
        }           
        std::cout << "> ";
    }
    std::cout << "exit " << std::endl;
    return 0;
}

  

posted @ 2020-07-02 16:46  youlin  阅读(216)  评论(0编辑  收藏  举报