Search code examples
cterminaltermcap

Why does an empty printf allow me to continue reading data from the stdin?


CODE

while (1)
    {
        keycode = key_hook();
        if (keycode == SPACE || keycode == BKSPACE)
        {
            render_again = 1;
        }
        if (keycode == ESC)
            break;
        if (render_again)
        {
            render_again = 0;
            render(all);
        }
        dprintf(1, "");      //I have no idea why this prevents the program from freezing
    }
    int key_hook()
    {
     char buffer[4];

     read(0, buffer, 4);
     return (*(unsigned int *)buffer);
    }

Alright, so this piece of code handles redrawing of text on screen. Some rows of text are underlined or highlighted using termcaps (tputs(tgetstr("us, NULL")......). Everything prints fine but after the first redraw of the text the while apparently freezes unless a dprintf/printf is present. The key_hook function just reads 4 bytes from the stdin and converts them to an int.


Solution

  • When I last did work here, my version of key_hook had a loop of single byte reads. This was broken by an alarm of 1 second and logic to whether the data so far was a key prefix.

    The alarm interrupted the read, and stopped freeze