How to make readLine() timeout

前端 未结 2 953
走了就别回头了
走了就别回头了 2021-01-15 20:38

My application basically is a CLI with all the expected features like prompt, history etc., it needs to wait on STDIN for user input. For this I am using readLine system cal

相关标签:
2条回答
  • 2021-01-15 21:20

    how about using this? I also tried to find the same solution as this thread. and those code just works but I'm not sure it has no prob. if anybody found fault in it or better solution, please let me know it.

    int event_hook(){
        rl_line_buffer[0] = '\0';
        rl_done = 1;
        return 0;
    }
    
    rl_event_hook = event_hook;
    rl_set_keyboard_input_timeout(500000);  // in usec
    char* line = readline(">");      // returns after 1s. I don't know why it takes double time that I set up.
    
    0 讨论(0)
  • 2021-01-15 21:22

    I am using readLine system call.

    libreadline

    You provide inconsistent information. What system would that be that has a system call readLine?
    If you use libreadline, you are rather calling library functions. But then, according to the GNU Readline Library Function and Variable Index, there is no function readLine, there's only readline. With it you could use either the

    • Variable: rl_hook_func_t * rl_event_hook
      If non-zero, this is the address of a function to call periodically when Readline is waiting for terminal input. By default, this will be called at most ten times a second if there is no keyboard input.

      (you'd set it to a function where you poll your network socket and respond to messages)

    or the

    • Alternate Interface
      An alternate interface is available to plain readline(). Some applications need to interleave keyboard I/O with file, device, or window system I/O, typically by using a main loop to select() on various file descriptors. To accommodate this need, readline can also be invoked as a `callback' function from an event loop. There are functions available to make this easy.

      There's an example program using the alternate interface: Alternate Interface Example.

    0 讨论(0)
提交回复
热议问题