My C program run multiple thread in the terminal that print messages asynchronously. I'd like a thread to show curses output, but as it is asynchronous it must be in another terminal.
My idea is to write the curses output to a fifo and open a terminal with
cat fifo
.
But how could I recover the ouput the curses out and output it to a file?
Thank you.
curses requires direct access to the terminal in order lookup escape codes, detect resolution etc. You should probably try to have one thread dedicated for curses output in the current (main) terminal, and redirect all debug messages from other threads to either a logfile/pipe/logging facility (and present them inside the curses if you want to)
ncurses needs a terminal because it initializes the I/O connection. A FIFO would not be suitable, since it is one-way and (see for example Perform action if input is redirected) probably cannot be initialized as a terminal. ncurses uses the
TERM
environment variable to lookup the terminal description (the actual terminal is not consulted on the matter).There is a simple example,
ditto
in ncurses-examples which usesxterm
for multiple input/output screens. That uses the pty interface, e.g.,and
newterm
to pass that file descriptor to ncurses:If you read the first quoted section of
ditto
, you will notice that it uses an option ofxterm
which allows an application to pass a file descriptor to it:Here is a screenshot showing
ditto
:curses uses a terminal for its input and output, so if you want to intercept that and make it go somewhere other than a terminal, the easiest method (though non-trivial) is to use a psuedoterminal. You do that by calling
posix_openpt
which gives you a pseudoterminal master device. You then callgrantpt
,unlockpt
, andptsname
to get the name of a terminal device that you can thenfopen
and pass to cursesnewterm
to initialize the terminal.Once that is done, everything that curses writes to the terminal will be readble from the master, and everything written to the master will be input to curses. It is like a fifo, just with all the extra terminal functionality curses expects.