Editline/readline function in C adds extra line-feed to input when using cyrillic

157 views Asked by At

I'm writing a program in which I use the editline C library to receive user input.

#include <locale.h>
#include <stdio.h>
#include <stdlib.h>

#include <editline/readline.h>
#include <editline/history.h>

int main(int argc, char **argv)
{
    setlocale(LC_ALL, "sr_RS.utf8");

    while (1)
    {
        char *input = readline("prompt> ");

        add_history(input);

        printf("%s\n", input);

        free(input);
    }

    return 0;
}

I'm trying to make the program capable of processing text in cyrillic. I have already set the programs locale to Serbian cyrillic, and it seems to process it fine.

However when using the readline function in the "editline/readline.h" header to process the text, a strange runtime bug happens.

prompt> k
k
prompt> к
к

prompt> k
k

Namely, whenever the line entered contains even a single cyrillic character, the readline function adds an extra line-feed character at the end of the string which isn't usually there.

I inserted a crude check into the while loop to test that this is in fact a problem with the readline function.

while (1)
{
    char *input = readline("prompt> ");

    add_history(input);

    for (int i = 0; input[i] != '\0'; ++i)
    {
        printf("%d\n", (int) input[i]);
    }

    free(input);
}

as expected it gives the following result:

prompt> k
107
prompt> к
-48
-70
10

My question is, where does this extra line-feed character come from, am I misusing the function, and if so, how do I get rid of this problem, or if that's not the case do I simply check for that extra character every line i get and get rid of it. It seems like there should be a cleaner way to get rid of this problem, but I don't know how.

Just for the sake of clarity I'm using Linux Mint on which I've already set up the sr_RS.utf8 locale.

0

There are 0 answers