I don't know the answer.
It sounds like you're going to reinvent the wheel though. May I suggest using ncurses? Handling terminals manually is hell; don't go there.