开发者

Check for a particular string in stdin

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?


Your program should just do following:

  1. read data from stdin.
  2. search for new command string. if found, do some job.
  3. send all your processed input to 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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜