Using readline with termios bounces the input char back

I’m trying to use the gnu readline library together with the termios library, what I’d like to do is to poll on the stdin and when the user is typing at least a char the readline() function should kick in and manage any autocompletion.

The problem is that when I starts typing the first char I type gets doubled on the stdout, can’t figure out why.
It happens only when I use the readline() function, if I use a simple read() there’s no similar behaviour.

Here’s the code:

int main(){
   struct termios newtiop, tiop;
   /*unsigned char buf[BUFSIZE];*/
   int rv=0;
   /*readline init flag*/
   static int rl_flag = 0;
   //set up poll struct
   struct pollfd pfd[] = { {STDIN_FILENO, POLLIN | POLLHUP, 0} };


   newtiop.c_lflag &= ~ICANON;
   newtiop.c_lflag |= ECHO;

   if( !rl_flag ){
      rl_flag = 1;

      //do the polling
      poll(pfd, 2, -1);

      if( pfd[0].revents & POLLHUP )
         return -1;
      if( pfd[0].revents & POLLIN ){
         /*char buf;*/
         /*read(STDIN_FILENO, &buf, 1);*/
         /*printf("tread :%cn", buf);*/
         char *line_read = readline(NULL);
         /*if (!line_read)*/
            /*return 1;*/
         /*else {*/
            /*line_read = stripwhite(line_read);*/
            /*printf("%sn", line_read);*/
   return rv;

Source: c#

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.