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} };



tcgetattr(STDIN_FILENO,&tiop);
   newtiop=tiop;
   newtiop.c_cc[VMIN]=1;
   newtiop.c_cc[VTIME]=0;

   newtiop.c_lflag &= ~ICANON;
   newtiop.c_lflag |= ECHO;
   tcsetattr(STDIN_FILENO,TCSAFLUSH,&newtiop);

   if( !rl_flag ){
      init_readline();
      rl_flag = 1;
   }

   while(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);*/
         /*}*/
         /*free(line_read);*/
      }
   }
   return rv;
}


Source: c#

Leave a Reply