How to render a remote ncurses console?

I want to write a remote console that acts as a telnet server. A user can use telnet to enter the server, and then write some commands to do some work.

A good example for this is the os router console. What I'm confusing right now, I can accept user input, do something, and then print some texts, but I want to use ncurses so that the console has more features (such as "cmd auto-complete", syntax color ... ), so how can I do this? Since the console is on the user side, if the server calls the ncurses APIs, it will simply change the contents on the server ...

This may be a stupid question, but I'm really new to this. Any suggestions are welcome.

+8
networking console ncurses telnet
source share
2 answers

This is harder than you think.

You need to understand how terminals work - they use special escape sequences, for example. moving the cursor or color output. This is described using a terminfo file, which depends on the terminal. Ncurses translates API calls (for example, moving the cursor to a specific position) in such control sequences using terminfo.

Since the terminal (currently xterm , gnome-terminal , screen , tmux , etc.) is on the client side, you need to transfer the type of terminal from the client to the server. This is why, for example, ssh transfers this information from the ssh client to the server (try echo $TERM in your ssh session - it could be "linux" if you logged in via the console or "xterm" if you use X and Xterm). Also, you better have the appropriate terminfo available on the server.

Another puzzle piece is pseudo-terminals . Since relatively few people currently use serial terminals, their semantics are emulated, so applications and libraries (such as curses and her friends), originally developed for serial consoles, continue to work. This is achieved using pseudo-terminals - they look like pipes, the master and slave communicate, everything that is written on one side comes out on the other side. For the getty login getty , for example, you can simply use one side of the pty device and consider it a sequential line - your server program should process the other side of the pty, sending everything it receives from pty to your client via the network.

Terminal emulators also use ptys, type tty in your terminal and you will get something like /dev/pts/9 if you use a terminal emulator. On the other hand, pty is usually your shell, communicating with your terminal emulator through pty.

Your client program may more or less use standard input and standard output. If the information about your terminal is correct, the rest will be processed by your terminal emulator, just transfer everything you get from your server program to stdout and send everything you read from stdin to your server program.

I hope I have not left any important details. Good luck

+9
source share

It is possible that ncurses work with threads other than stdin and stdout. Call newterm() before initscr() to set the input and output descriptors for ncurses.

But you will need to know which terminal is at the remote end of the connection (ssh and telnet have mechanisms for sending this message to the server), and you will also want to return to the non-ncurses interface if the remote end does not support the terminal type (or if you cannot determine the type of terminal).

+2
source share

All Articles