Author: tgoettlicher
Date: Fri Jul 4 16:06:19 2008
New Revision: 48893
URL: http://svn.opensuse.org/viewcvs/yast?rev=48893&view=rev
Log:
added an example to read from a binaries stdout and stderr
Added:
branches/tmp/jdsn/registration/src/callprogram/
branches/tmp/jdsn/registration/src/callprogram/README
branches/tmp/jdsn/registration/src/callprogram/callprogram.cpp
branches/tmp/jdsn/registration/src/callprogram/output.sh (with props)
Added: branches/tmp/jdsn/registration/src/callprogram/README
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/jdsn/registration/src/callprogram/README?rev=48893&view=auto
==============================================================================
--- branches/tmp/jdsn/registration/src/callprogram/README (added)
+++ branches/tmp/jdsn/registration/src/callprogram/README Fri Jul 4 16:06:19 2008
@@ -0,0 +1,9 @@
+call a external program and get its stdout and stderr
+
+g++ callprogram.cpp -o callprogram
+
+TODO:
+- if the external program doesn't open stderr the read(errfd[0]) call blocks
+- buffer size for stderr and stdin needs to be adapted
+- get the external program's return value
+- only binaries are able to be called. workaround: call /bin/sh script.sh
Added: branches/tmp/jdsn/registration/src/callprogram/callprogram.cpp
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/jdsn/registration/src/callprogram/callprogram.cpp?rev=48893&view=auto
==============================================================================
--- branches/tmp/jdsn/registration/src/callprogram/callprogram.cpp (added)
+++ branches/tmp/jdsn/registration/src/callprogram/callprogram.cpp Fri Jul 4 16:06:19 2008
@@ -0,0 +1,122 @@
+#include
+#include
+#include <string>
+#include
+#include <iostream>
+ #include
+ #include
+
+
+
+
+int callProgram(char* programCall[], std::string in, std::string& out, std::string& err)
+{
+ // programName: path and name of the binary to call
+ // in: text to write to the binary's stdin
+ // out: text the binary writes to stdout
+ // err: text the binary writes to stderr
+ // retval: the return value of the binary TODO doesn't work at the moment
+
+
+ int outfd[2];
+ int infd[2];
+ int errfd[2];
+
+ int oldstdin, oldstdout, oldstderr;
+
+ pipe(outfd); // Where the parent is going to write to
+ pipe(infd); // From where parent is going to read stdout
+ pipe(errfd); // From where parent is going to read stderr
+
+ oldstdin = dup(0); // Save stdin
+ oldstdout = dup(1); // Save stdout
+ oldstderr = dup(2); // Save stderr
+
+ close(0);
+ close(1);
+ close(2);
+
+ dup2(outfd[0], 0); // Make the read end of outfd pipe as stdin
+ dup2(infd[1],1); // Make the write end of infd as stdout
+ dup2(errfd[1],2); // Make the write end of errfd as stderr
+
+ if(!fork())
+ {
+
+
+ close(outfd[0]); // Not required for the child
+ close(outfd[1]);
+ close(infd[0]);
+ close(infd[1]);
+ close(errfd[0]);
+ close(errfd[1]);
+
+ execv(programCall[0], programCall);
+
+ }
+ else
+ {
+
+ char input[100];
+ char error[100];
+ close(0); // Restore the original std fds of parent
+ close(1);
+ close(2);
+ dup2(oldstdin, 0);
+ dup2(oldstdout, 1);
+ dup2(oldstderr, 2);
+
+ close(outfd[0]); // These are being used by the child
+ close(infd[1]);
+ close(errfd[1]);
+
+ write(outfd[1], in.c_str() , in.length()); // Write to childâs stdin
+
+ input[read(infd[0],input,100)] = 0; // Read from childâs stdout
+ out = input;
+ input[0]='\0';
+
+ int val = fcntl (errfd[0], F_GETFL, 0);
+ if (val < 0)
+ {
+ std::cout << "get flags failed";
+ return -1;
+ }
+
+ // Set nonblocking flag.
+ int ret = fcntl (errfd[0], F_SETFL, val|O_NONBLOCK);
+ if (ret < 0)
+ {
+ std::cout << "set flags failed";
+ return -1;
+ }
+ fcntl (errfd[0], F_SETFL, val|O_NONBLOCK);
+
+ input[read(errfd[0],input,100)] = 0; // Read from childâs stderr
+
+ err = input;
+ }
+ return 0;
+}
+
+int main()
+{
+ std::string out;
+ std::string err;
+ int retval;
+
+// char *programCall[] = {"/bin/sh", "./output.sh",0};
+// char *programCall[] = {"/bin/ls", "-l", 0};
+// char *programCall[]={"/usr/bin/lsof", 0};
+
+ char *programCall[]={"/bin/ls", "dummy", 0};
+ std::string in = "";
+
+ retval = callProgram(programCall, in, out, err);
+
+ std::cout << "This is return value: " << retval << std::endl;
+ std::cout << "This is stdout:" << std::endl << out << std::endl;
+ std::cout << "This is stderr:" << std::endl << err << std::endl;
+
+ return 0;
+}
Added: branches/tmp/jdsn/registration/src/callprogram/output.sh
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/jdsn/registration/src/callprogram/output.sh?rev=48893&view=auto
==============================================================================
--- branches/tmp/jdsn/registration/src/callprogram/output.sh (added)
+++ branches/tmp/jdsn/registration/src/callprogram/output.sh Fri Jul 4 16:06:19 2008
@@ -0,0 +1,2 @@
+echo "dies ist stdout"
+echo "dies ist stderr" > /dev/stderr
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org