Pergunta

I'm writing a simplified version of Linux standard less command for OS academic classes, and I'm allowed to use ncurses to make it easier. "Simplified" means that the user should be able to scroll the view with arrows, PgUp, PgDown, and also use g and G jump commands. Also, the program should read the input either from a file (its name given as a parameter) or standard input stream, like

echo "test" | ./less

Reading from file was easy (no problems in this case), the problem occurs when I'm using the pipeline operator - getch() keeps returning -1 (EOF) and doesn't respond to any key presses. My current code looks like this:

char c;

while((c = fget(stdin)) != EOF)
{
    // reading from stdin char by char, let's say
}

// enabling ncurses mode
initscr();
keypad(stdscr, TRUE);
noecho();
cbreak();

int ch;

while(true)
{
    ch = getch();

    switch(ch)
    {
        case KEY_LEFT:
        ...
    }
}

...

I thought it's a matter of flushing the stream, but none of similar solutions worked for me. I read all data from stdin, till the very end (EOF), so I don't understand why EOF stays in the buffer.

Is there any workaround for this? I've browsed through similar questions, but I haven't found any clue for my problem. Maybe ncurses isn't a good solution for my task?

Foi útil?

Solução

Problem solved, I added one line before entering ncurses mode:

...
freopen("/dev/tty", "rw", stdin);
// enabling ncurses mode
initscr();
...
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top