I want to debug my cgi script (C++) from IDE, so I would like to create a "debug mode": read file from disk, push it to own stdin, set some environment variables, that correspond this file and run the rest of the script as it was called by the web server. Is it possible and if it is, then how can I do that?
Asked
Active
Viewed 9,583 times
2 Answers
12
You can't "push to own stdin", but you can redirect a file to your own stdin.
freopen("myfile.txt","r",stdin);

J-16 SDiZ
- 26,473
- 4
- 65
- 84
-
Well, assuming stdio, there's [fungetc](http://pubs.opengroup.org/onlinepubs/7908799/xsh/ungetc.html). But that doesn't guarantee more than one byte of push-back. – ephemient Jun 20 '12 at 20:55
-
well. `fungetc` just work for 1 byte. It can not used for cgi input as the op intended. – J-16 SDiZ Jun 20 '12 at 20:59
2
Everybody knows that standard input is a file descriptor defined as STDIN_FILENO
. Though its value is not guaranteed to be 0
, I never saw anything else. Anyway, there is nothing that prevents you from writing to that file descriptor. For the sake of example, here is a small program that write 10 messages to its own standard input:
#include <unistd.h>
#include <string>
#include <sstream>
#include <iostream>
#include <thread>
int main()
{
std::thread mess_with_stdin([] () {
for (int i = 0; i < 10; ++i) {
std::stringstream msg;
msg << "Self-message #" << i
<< ": Hello! How do you like that!?\n";
auto s = msg.str();
write(STDIN_FILENO, s.c_str(), s.size());
usleep(1000);
}
});
std::string str;
while (getline(std::cin, str))
std::cout << "String: " << str << std::endl;
mess_with_stdin.join();
}
Save that into test.cpp
, compile and run:
$ g++ -std=c++0x -Wall -o test ./test.cpp -lpthread
$ ./test
Self-message #0: Hello! How do you like that!?
Self-message #1: Hello! How do you like that!?
Self-message #2: Hello! How do you like that!?
Self-message #3: Hello! How do you like that!?
Self-message #4: Hello! How do you like that!?
Self-message #5: Hello! How do you like that!?
Self-message #6: Hello! How do you like that!?
Self-message #7: Hello! How do you like that!?
Self-message #8: Hello! How do you like that!?
Self-message #9: Hello! How do you like that!?
hello?
String: hello?
$
The "hello?" part is something that I typed after all 10 messages were sent. Then you press Ctrl+D to indicate end of input and program exits.
-
4That happens to look somewhat-working because you're at a terminal where file descriptors 0, 1, and 2 are all tied to the pty. You are not writing anything that the program itself could read back. See http://stackoverflow.com/q/1441251 for some exposition on this topic. – ephemient Jun 20 '12 at 21:22
-
True. If you are detached from the tty, your input fd can as well be closed. I guess an exact solution depends on context. It is quite possible to do pipe/dup2 etc. And if you reopen `stdin`, how about `std::cin` etc? Reminds me of a movie where a robot was constantly saying "Need more input" >;-)) – Jun 20 '12 at 21:27
-
1This does not work as intended. See how all the Self-message lines are not preceded by "String: ". That should be the case if it actually went through the while loop in the main. – DarioP Mar 21 '20 at 18:19