How to implement a stdin, stdout wrapper?

前端 未结 4 2171
挽巷
挽巷 2021-02-10 10:57

I have an interactive program that runs stdin and stdout. I need to create wrapper that will send X to it\'s stdin, check that it prints Y and then redirects wrapper\'s stdin an

4条回答
  •  再見小時候
    2021-02-10 11:27

    I'm a little confused as to what exactly you're trying to achieve; as I understand you wish to:

    1. Start the wrapper program specifying the input X, and expected output Y.
    2. Have the wrapper initiate the target program attaching input X to it's stdin, and verifying that it's output matches Y.
    3. Have the target program return, and after output is verified, rerun it from the same instance of the wrapper program, this time using the wrapper programs stdin and stdout.

    If this is the case you want to do this:

    1. Have the wrapper program open pipes for the stdin and stdout of the target program.
    2. Fork, and close the appropriate ends of said pipes.
    3. Have the parent process and verify the output, while the child exec()s and executes thh target program.
    4. Wait for the child process to terminate when it's stdout closes.
    5. Have the wrapper program exec() into the target program.
    6. Now the target program will be executing as normal.

    If this is correct, I can provide a ~30 line C program, or ~10 line Python program that achives this.

提交回复
热议问题