-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiBUGS.cpp
More file actions
134 lines (125 loc) · 3.93 KB
/
Copy pathMultiBUGS.cpp
File metadata and controls
134 lines (125 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <string>
#include <cstring>
#include <iostream>
#ifdef _WIN32
#include <windows.h>
#include <shellapi.h>
#include <shlobj.h>
#endif
#ifdef __linux__
#include <cstdlib>
#include <libgen.h>
#include <unistd.h>
#include <limits.h>
#endif
// MultiBUGS.exe is a very simple wrapper to avoid users needing to call
// mpiexec themselves.
//
// The program essentially just (syncronously) calls
//
// mpiexec -n 1 <OtherArgs> OpenBUGS.exe <OpenBUGSArgs>
//
// where <OpenBUGSArgs> are the usual BUGS/BlackBox arguments
// and <OtherArgs> are any other arguments, which are passed to mpiexec
//
//
// Should be compiled with VS using
// rc -fo Win/Rsrc/Bugslogo.res Win/Rsrc/Bugslogo.rc
// cvtres -machine:ix86 -out:Bugslogo.obj Win/Rsrc/Bugslogo.res
// cl /EHsc MultiBUGS.cpp /link shell32.lib Bugslogo.obj /subsystem:windows /entry:mainCRTStartup
// Returns path to directory containing this executable
std::string ExePath(){
#ifdef _WIN32
char buffer[MAX_PATH];
GetModuleFileName(NULL, buffer, MAX_PATH);
std::string::size_type pos = std::string(buffer).find_last_of("\\/");
return std::string(buffer).substr(0, pos);
#endif
#ifdef __linux__
char buff[PATH_MAX];
ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
if (len != -1) {
buff[len] = '\0';
return std::string(dirname(buff));
}
#endif
}
int main(int argc, char *argv[]){
std::string quote = "\"";
// split args into those for mpiexec and those for BlackBox/MultiBUGS
std::string mpiexec_args, multibugs_args;
int i = 1; // ignore exe name
while (i < argc){
if (// First handle two-part BlackBox/MultiBUGS arguments
strcmp("/PAR", argv[i]) == 0 ||
strcmp("/USE", argv[i]) == 0 ||
strcmp("/INIFILE", argv[i]) == 0 ||
strcmp("/O", argv[i]) == 0 ||
strcmp("/P", argv[i]) == 0 ||
strcmp("/LOAD", argv[i]) == 0 ||
strcmp("/LRTB", argv[i]) == 0 ||
strcmp("/LANG", argv[i]) == 0){
multibugs_args = multibugs_args + " " + argv[i];
if (i + 1 <= argc){
if (std::string(argv[i + 1]).find(" ") != std::string::npos){
multibugs_args = multibugs_args + " " + quote + argv[i + 1] + quote;
} else {
multibugs_args = multibugs_args + " " + argv[i + 1];
}
}
i = i + 2;
} else if (// Now handle BlackBox/MultiBUGS flags
strcmp("/HEADLESS", argv[i]) == 0 ||
strcmp("/PORTABLE", argv[i]) == 0 ||
strcmp("/EMBEDDING", argv[i]) == 0 ||
strcmp("/NOAPPWIN", argv[i]) == 0 ||
strcmp("/NOSCROLL", argv[i]) == 0 ||
strcmp("/FULLSIZE", argv[i]) == 0){
multibugs_args = multibugs_args + " " + argv[i];
i++;
} else {// Now handle any other arguments/flags
if (std::string(argv[i]).find(" ") != std::string::npos){
mpiexec_args = mpiexec_args + " " + quote + argv[i] + quote;
} else {
mpiexec_args = mpiexec_args + " " + argv[i];
}
i++;
}
}
#ifdef _WIN32
// build full call to mpiexec
std::string param =
" -n 1" +
mpiexec_args +
" \"" +
ExePath() +
"/OpenBUGS.exe\"" +
multibugs_args;
// call mpiexec
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = "open";
ShExecInfo.lpFile = "mpiexec";
ShExecInfo.lpParameters = param.c_str();
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_HIDE;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
// wait till mpiexec is finished
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
CloseHandle(ShExecInfo.hProcess);
#endif
#ifdef __linux__
// build full call to mpiexec
std::string param =
"mpiexec -n 1" +
mpiexec_args +
" \"" +
ExePath() +
"/OpenBUGS\"" +
multibugs_args;
std::system(param.c_str());
#endif
}