Arrow keys and color output in shell in forked child process

  bash, docker, linux, readline, terminal

It’s somewhat unclear to me how GNU readline() implements up/down arrow keys and where the actual interaction with the terminal to support this happens. Basically, I’m trying to get up/down arrow keys and color output to work in a debugger in a program running in Docker. The whole system is a cluster submission environment that also supports local execution. The start procedure is therefore somewhat contrived, since it forwards stdout to various places and needs to set up fairly complicated signal handlers.

To be more precise, the start procedure is the following:

  1. Docker runs a bash script that does some setup.
  2. Bash script execs a C-program.
  3. C program sets up pipes for stdin/stdout, signal handlers, forks() and exec()s in the child a Python interpreter (the user’s program).

Note that the steps (1) and (2) always moves to the next step through exec().

The Python interpreter’s stdin and stdout are both pipes to the C program. The C program forwards anything from stdin to the pipe while anything coming from the pipe it forwards to stdout as well as various log files/services.

Now if I start a debugger in the Python script. It’ll work just fine, except that arrow keys don’t. My understanding is that this is handled through GNU readline(), but it’s not exactly clear to me what needs to happen for up/down keys to work.

Is it bash that handles the output formatting to the terminal? Since I exec the C program in the Docker entrypoint shell script, there’s no bash interpreter running anymore.

Anyone have an idea how to solve this?

EDIT: I currently transfer the values of TERM, COLORTERM and SHELL environment variables to the python program from the host as well as LS_COLORS among a few other things.

Source: StackOverflow