merged the w and u versions of the execmd exerciser

This commit is contained in:
Jean-Francois Dockes 2015-09-13 11:14:39 +02:00
parent 3956d0a867
commit fcd4a6a3ac
2 changed files with 65 additions and 32 deletions

View file

@ -109,8 +109,8 @@ trfileudi.o : fileudi.cpp fileudi.h
EXECMD_OBJS= trexecmd.o EXECMD_OBJS= trexecmd.o
trexecmd : $(EXECMD_OBJS) trexecmd : $(EXECMD_OBJS)
$(CXX) $(ALL_CXXFLAGS) -o trexecmd $(EXECMD_OBJS) $(LIBRECOLL) $(CXX) $(ALL_CXXFLAGS) -o trexecmd $(EXECMD_OBJS) $(LIBRECOLL)
trexecmd.o : execmd.cpp execmd.h trexecmd.o : trexecmd.cpp execmd.h
$(CXX) -o trexecmd.o -c $(ALL_CXXFLAGS) -DTEST_EXECMD execmd.cpp $(CXX) -o trexecmd.o -c $(ALL_CXXFLAGS) -I../xaposix trexecmd.cpp
TRANSCODE_OBJS= trtranscode.o TRANSCODE_OBJS= trtranscode.o
transcode : $(TRANSCODE_OBJS) transcode : $(TRANSCODE_OBJS)

View file

@ -6,6 +6,9 @@
#include <stdlib.h> #include <stdlib.h>
#include "safeunistd.h" #include "safeunistd.h"
#include <string.h> #include <string.h>
#ifndef _WIN32
#include <signal.h>
#endif
#include <string> #include <string>
#include <iostream> #include <iostream>
@ -32,9 +35,12 @@ bool exercise_mhexecm(const string& cmdstr, const string& mimetype,
ExecCmd cmd; ExecCmd cmd;
vector<string> myparams; vector<string> myparams;
#ifdef _WIN32
// Hack for windows: the command is always "Python somescript" // Hack for windows: the command is always "Python somescript"
myparams.push_back(files[0]); myparams.push_back(files[0]);
files.erase(files.begin()); files.erase(files.begin());
#endif
if (cmd.startExec(cmdstr, myparams, 1, 1) < 0) { if (cmd.startExec(cmdstr, myparams, 1, 1) < 0) {
cerr << "startExec " << cmdstr << " failed. Missing command?\n"; cerr << "startExec " << cmdstr << " failed. Missing command?\n";
@ -121,11 +127,12 @@ bool exercise_mhexecm(const string& cmdstr, const string& mimetype,
static char *thisprog; static char *thisprog;
static char usage [] = static char usage [] =
"trexecmd [-c -r -i -o] cmd [arg1 arg2 ...]\n" "trexecmd [-c -r -i -o] [-e <fn>] cmd [arg1 arg2 ...]\n"
" -c : test cancellation (ie: trexecmd -c sleep 1000)\n" " -c : test cancellation (ie: trexecmd -c sleep 1000)\n"
" -r : run reexec. Must be separate option.\n" " -r : run reexec. Must be separate option.\n"
" -i : command takes input\n" " -i : command takes input\n"
" -o : command produces output\n" " -o : command produces output\n"
" -e <fn> : send stderr to file named fn (will truncate it)\n"
" If -i is set, we send /etc/group contents to whatever command is run\n" " If -i is set, we send /etc/group contents to whatever command is run\n"
" If -o is set, we print whatever comes out\n" " If -o is set, we print whatever comes out\n"
"trexecmd -m <filter> <mimetype> <file> [file ...]: test execm:\n" "trexecmd -m <filter> <mimetype> <file> [file ...]: test execm:\n"
@ -148,6 +155,7 @@ static int op_flags;
#define OPT_r 0x20 #define OPT_r 0x20
#define OPT_m 0x40 #define OPT_m 0x40
#define OPT_o 0x80 #define OPT_o 0x80
#define OPT_e 0x100
// Data sink for data coming out of the command. We also use it to set // Data sink for data coming out of the command. We also use it to set
// a cancellation after a moment. // a cancellation after a moment.
@ -199,7 +207,7 @@ public:
ReExec reexec; ReExec reexec;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
#if 0 #ifndef _WIN32
reexec.init(argc, argv); reexec.init(argc, argv);
if (0) { if (0) {
@ -214,6 +222,7 @@ int main(int argc, char *argv[])
} }
#endif #endif
string stderrFile;
thisprog = argv[0]; thisprog = argv[0];
argc--; argv++; argc--; argv++;
@ -225,11 +234,14 @@ int main(int argc, char *argv[])
while (**argv) while (**argv)
switch (*(*argv)++) { switch (*(*argv)++) {
case 'c': op_flags |= OPT_c; break; case 'c': op_flags |= OPT_c; break;
case 'r': op_flags |= OPT_r; break; case 'e':
case 'w': op_flags |= OPT_w; break; op_flags |= OPT_e;
case 'm': op_flags |= OPT_m; break; if (argc < 2) {
case 'i': op_flags |= OPT_i; break; Usage();
case 'o': op_flags |= OPT_o; break; }
stderrFile = *(++argv); argc--;
goto b1;
case 'h': case 'h':
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
cout << "MESSAGE " << i << " FROM TREXECMD\n"; cout << "MESSAGE " << i << " FROM TREXECMD\n";
@ -237,6 +249,11 @@ int main(int argc, char *argv[])
//sleep(1); //sleep(1);
} }
return 0; return 0;
case 'i': op_flags |= OPT_i; break;
case 'o': op_flags |= OPT_o; break;
case 'm': op_flags |= OPT_m; break;
case 'r': op_flags |= OPT_r; break;
case 'w': op_flags |= OPT_w; break;
default: Usage(); break; default: Usage(); break;
} }
b1: argc--; argv++; b1: argc--; argv++;
@ -253,10 +270,26 @@ int main(int argc, char *argv[])
DebugLog::getdbl()->setloglevel(DEBDEB1); DebugLog::getdbl()->setloglevel(DEBDEB1);
DebugLog::setfilename("stderr"); DebugLog::setfilename("stderr");
#if 0 #ifndef _WIN32
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
if (op_flags & OPT_r) {
// Test reexec. Normally only once, next time we fall through
// because we remove the -r option (only works if it was
// isolated, not like -rc
chdir("/");
argv[0] = strdup("");
sleep(1);
cerr << "Calling reexec\n";
// We remove the -r arg from list, otherwise we are going to
// loop (which you can try by commenting out the following
// line)
reexec.removeArg("-r");
reexec.reexec();
}
#endif #endif
if (op_flags & OPT_w) { if (op_flags & OPT_w) {
// Test "which" method // Test "which" method
string path; string path;
@ -281,10 +314,10 @@ int main(int argc, char *argv[])
MEAdv adv; MEAdv adv;
mexec.setAdvise(&adv); mexec.setAdvise(&adv);
//mexec.setTimeout(5); //mexec.setTimeout(5);
#ifdef LATER
// Stderr output goes there // Stderr output goes there
mexec.setStderr("/tmp/trexecStderr"); if (!stderrFile.empty())
#endif mexec.setStderr(stderrFile);
// A few environment variables. Check with trexecmd env // A few environment variables. Check with trexecmd env
mexec.putenv("TESTVARIABLE1=TESTVALUE1"); mexec.putenv("TESTVARIABLE1=TESTVALUE1");
mexec.putenv("TESTVARIABLE2=TESTVALUE2"); mexec.putenv("TESTVARIABLE2=TESTVALUE2");