检查 stdin 中的特定字符串
我有一个小问题,我不知道如何解决。
我有一个程序,基本上是由标准输入中的输入命令的。它只是继续运行,直到收到特定的字符串,然后执行其他操作。好的,我无权访问控制这些输入的代码,但我想添加一个新命令。
我的想法如下:
。在我的程序中创建一个管道
。分叉该进程
。其中一个继续执行原始程序,而另一个则从标准输入读取。
。将继续通常执行的进程的标准输入重定向到管道。
。持续监听标准输入的部分,检查接收到的内容,如果不是新命令,则重定向管道(以便旧命令仍然有效)
。如果是新命令,则发送特定信号。
我一直在考虑在c++中使用pipe/fork/dup2来实现它,但我不知道如何在继续执行的过程中发送和捕获信号。我怎样才能在 C++ 中做到这一点?你们有什么建议吗?这可能有效吗?
I have a small problem that I don't know how to solve.
I have a program which is basically commanded by inputs in stdin. It just keeps running until receives a particular string and then do something else. Ok, I don't have access to the code that controls these inputs but I would like to add a new command.
The idea I have is the following:
. Create a pipe in my program
. Fork the process
. One of them goes on with the the original program while the other reads from stdin.
. Redirect stdin of process that continued the usual execution to the pipe.
. The part that keeps listening from stdin, checks what is receives, if it is not the new command, just redirect the pipe (so that the old commands still work)
. If it is the new command, send a particular signal.
I've been thinking about implementing that with pipe/fork/dup2 in c++, but I don't know how I would send and catch a signal in the process that continued the execution. How can I do that in c++? Do you guys have any suggestion? Does this might work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的程序应该执行以下操作:
stdin
读取数据。并且应该像这样执行:
$ cat input.txt |我的程序 | other_prog
所有未由您的程序处理的输入都将转到现有程序,因为它是从 input.txt 获取的(当然,数据源可以是任意的,不一定是文件)。
Your program should just do following:
stdin
.stdout
(or all data except extracted command data, depending on your needs)And it shoulde be executed like this:
$ cat input.txt | my_prog | other_prog
All input which is not handled by your program, will go to existing program, as it is got from input.txt (of course, data source could be arbitrary, not necessarily file).