Shell in ncurses window?

∥☆過路亽.° 提交于 2019-12-04 13:14:05
abligh

You will need to do roughly the following:

  • Create a pty (pseudoterminal). You don't mention which OS you are using, but if it's POSIX based, man pty will help. Also look at openpty
  • The slave end of the pty needs to be connected to whatever you are running (e.g. the shell), whereas the master end needs to be connected to your terminal emulator.
  • You'll thus have to fork(), and in the child, dup2 the slave fd onto the stdin, stdout and stderr
  • Then you need to connect the master fd in the parent to your ncurses environment (for output) and keyboard handling (for input).

This answer, This answer, and this answer might be useful, but I recommend especially the first given it refers Stephens' Advanced Programming in the UNIX Environment (I've linked to the more modern edition), and the source code is downloadable. Buy the book, because it's an invaluable resource, but if you look in the pty directory there (I won't copy and paste for copyright reasons) you'll find a good example of forkpty.

That leaves the question of how you get from output data from the process you are running to ncurses. Well, you need to write a terminal emulator. Pick the terminal you want to emulate, read up on all the control sequences it uses, and implement all of them.

If this sounds quite like hard work (which it is), I suggest you look at using tmux or screen instead; the latter which does exactly what you want, including a full VT100/ANSI terminal emulator; the former is almost as complete. tmux is a BSD licensed program, which means you can simply adapt the source to do your bidding in the unlikely event it doesn't already. screen is GPL, so unless your program is also GPL, it's for inspiration only.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!