外壳在ncurses窗口中? [英] Shell in ncurses window?

查看:97
本文介绍了外壳在ncurses窗口中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用ncurses编写一个最小的终端多路复用器.但是,当我尝试execv一个shell时,它不会打印到窗口,而是启动一个新的子进程来控制该窗口(即忽略ncurses).

I'm currently attempting to write a minimal terminal multiplexer using ncurses. However, when I try to execv a shell, it doesn't print to the window, and instead starts a new subprocess that takes control of the window (i.e. ignores ncurses).

如何防止这种情况并使ncurses控制它?有什么方法可以获取tty并进行控制吗?

How can I prevent that and have ncurses control it? Is there some way of getting the tty and controlling that?

编辑

感谢Ross Ridge的评论,我现在知道我需要创建一个伪终端,然后将其读入ncurses窗口.但是,我不知道如何使标准输出在ncurses窗口中打印.是否有一个ncurses函数可以读取文件描述符?还是我必须将read()printw()一起使用?

Thanks to Ross Ridge in the comments, I now know that I need to create a pseudo-terminal, which I then read into an ncurses window. However, I can't figure out how to get the stdout to print in an ncurses window. Is there an ncurses function that reads file descriptors? Or do I have to use read() with printw() somehow?

推荐答案

您将需要大致执行以下操作:

You will need to do roughly the following:

  • 创建一个pty(伪终端).您没有提到正在使用哪个操作系统,但是如果它是基于POSIX的,则man pty会有所帮助.还要看看openpty
  • pty的从属端需要连接到您正在运行的任何设备(例如外壳),而主端需要连接到您的终端仿真器.
  • 因此,您必须fork(),在子级中,将dup2从属fd放入stdinstdoutstderr
  • 然后,您需要将父级服务器中的master fd连接到您的ncurses环境(用于输出)和键盘处理(用于输入).
  • 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).

此答案此答案可能有用,但我特别推荐第一个,因为它引用了斯蒂芬斯(Stephens)的

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.

剩下的问题是如何从正在运行的进程到ncurses的输出数据中获取数据.好吧,您需要编写一个终端仿真器.选择您要仿真的终端,阅读其使用的所有控制序列,并实现所有这些控制序列.

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.

如果这听起来像是艰苦的工作(确实如此),建议您使用 tmux screen 代替;后者可以完全满足您的要求,包括完整的VT100/ANSI终端仿真器;前者几乎是完整的. tmux是BSD许可的程序,这意味着您可以简单地调整来源,以在尚未发生的极少数情况下进行出价. screen是GPL,所以除非您的程序也是GPL,否则它仅是出于启发.

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.

这篇关于外壳在ncurses窗口中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆