22 #include "config/Config.h"
23 #include "tools/Exception.h"
24 #include "tools/Communicator.h"
28 #include "tools/Tools.h"
29 #include "tools/DLLoader.h"
39 CLToolMain::CLToolMain():
51 #define CHECK_NULL(val,word) plumed_massert(val,"NULL pointer received in cmd(\"CLTool " + word + "\")");
55 }
else if(word==
"setArgc"){
57 argc=*
static_cast<int*
>(val);
58 }
else if(word==
"setArgv"){
60 char**v=
static_cast<char**
>(val);
61 for(
int i=0;i<
argc;++i)
argv.push_back(
string(v[i]));
62 }
else if(word==
"setArgvLine"){
64 const char*v=
static_cast<char*
>(val);
66 }
else if(word==
"setIn"){
68 in=
static_cast<FILE*
>(val);
69 }
else if(word==
"setOut"){
71 out=
static_cast<FILE*
>(val);
72 }
else if(word==
"setMPIComm"){
74 }
else if(word==
"setMPIFComm"){
76 }
else if(word==
"run"){
79 char**v=
new char* [
argc];
80 for(
int i=0;i<
argc;++i){
81 v[i]=
new char [
argv[i].length()+1];
82 for(
unsigned c=0;c<argv[i].length();++c) v[i][c]=argv[i][c];
83 v[i][argv[i].length()]=0;
86 for(
int i=0;i<
argc;++i)
delete [] v[i];
88 *
static_cast<int*
>(val)=ret;
90 plumed_merror(
"cannot interpret cmd(\"CLTool " + word +
"\"). check plumed developers manual to see the available commands.");
101 bool printhelp=
false;
107 bool standalone_executable=
false;
114 if(a.length()==0)
continue;
115 if(a==
"help" || a==
"-h" || a==
"--help"){
118 }
else if(a==
"--has-mpi"){
121 }
else if(a==
"--has-matheval"){
123 }
else if(a==
"--has-almost"){
125 }
else if(a==
"--has-cregex"){
127 }
else if(a==
"--has-dlopen"){
129 }
else if(a==
"--no-mpi"){
132 fprintf(stderr,
"--no-mpi option should can only be used as the first option");
135 }
else if(a==
"--standalone-executable"){
136 standalone_executable=
true;
137 }
else if(a.find(
"--load=")==0){
138 a.erase(0,a.find(
"=")+1);
140 void *p=dlloader.
load(a);
142 fprintf(stderr,
"ERROR: cannot load library %s\n",a.c_str());
143 fprintf(stderr,
"ERROR: %s\n",dlloader.
error().c_str());
146 }
else if(a==
"--load"){
148 }
else if(a[0]==
'-') {
149 string msg=
"ERROR: Unknown option " +
a;
150 fprintf(stderr,
"%s\n",msg.c_str());
156 if(!standalone_executable){
158 if(find(files.begin(),files.end(),
"patches")==files.end()) {
160 "ERROR: I cannot find "+root+
"/patches/ directory\n";
161 fprintf(stderr,
"%s",msg.c_str());
169 vector<string> availableShell;
170 if(!standalone_executable) {
173 for(
unsigned j=0;j<tmp.size();++j){
174 size_t ff=tmp[j].find(
".sh");
175 if(ff==string::npos) tmp[j].erase();
176 else tmp[j].erase(ff);
178 for(
unsigned j=0;j<tmp.size();++j)
if(tmp[j].length()>0) availableShell.push_back(tmp[j]);
183 "Usage: plumed [options] [command] [command options]\n"
184 " plumed [command] -h|--help: to print help for a specific command\n"
186 " [help|-h|--help] : to print this help\n"
187 " [--has-mpi] : fails if plumed is running without MPI\n"
188 " [--has-matheval] : fails if plumed is compiled without matheval\n"
189 " [--has-almost] : fails if plumed is compiled without almost\n"
190 " [--has-dlopen] : fails if plumed is compiled without dlopen\n"
191 " [--load LIB] : loads a shared object (typically a plugin library)\n"
192 " [--standalone-executable] : tells plumed not to look for commands implemented as scripts\n"
194 fprintf(out,
"%s",msg.c_str());
195 for(
unsigned j=0;j<availableCxx.size();++j){
198 string manual=availableCxx[j]+
" : "+cl->
description();
200 fprintf(out,
" plumed %s\n", manual.c_str());
202 for(
unsigned j=0;j<availableShell.size();++j){
203 string cmd=
"env PLUMED_ROOT="+root+
" "+root+
"/scripts/"+availableShell[j]+
".sh --description";
204 FILE *fp=popen(cmd.c_str(),
"r");
208 manual= availableShell[j]+
" : "+manual;
209 fprintf(out,
" plumed %s\n", manual.c_str());
214 fprintf(out,
"%s",
"Nothing to do. Use 'plumed help' for help\n");
219 string command(argv[i]);
221 if(find(availableCxx.begin(),availableCxx.end(),command)!=availableCxx.end()){
225 if( !cl->
readInput( argc-i,&argv[i],in,out ) ){
delete cl;
return 0; }
226 int ret=cl->
main(in,out,pc);
231 if(find(availableShell.begin(),availableShell.end(),command)!=availableShell.end()){
232 plumed_massert(in==stdin,
"shell tools can only work on stdin");
233 plumed_massert(out==stdout,
"shell tools can only work on stdin");
234 string cmd=
"env PLUMED_ROOT="+root+
" "+root+
"/scripts/"+command+
".sh";
235 for(
int j=i+1;j<
argc;j++) cmd+=
string(
" ")+argv[j];
240 string msg=
"ERROR: unknown command " + command +
". Use 'plumed help' for help";
241 fprintf(stderr,
"%s\n",msg.c_str());
static bool initialized()
Tests if MPI library is initialized.
std::string getPlumedRoot()
Class taking care of dynamic loading.
Class containing wrappers to MPI.
#define CHECK_NULL(val, word)
static int run(int argc, char **argv, FILE *in, FILE *out, Communicator &pc)
This is the entry point to the command line tools included in the plumed library. ...
void cmd(const std::string &key, void *val=NULL)
Send messages to the CLToolMain.
const std::string & error()
Returns the last error in dynamic loader.
std::vector< std::string > argv
arguments for command-line mode:
CLToolRegister & cltoolRegister()
void * load(const std::string &)
Load a library, returning its handle.
int argc
arguments for command-line mode:
void Set_comm(MPI_Comm)
Set from a real MPI communicator.
void Set_fcomm(void *comm)
Set from a pointer to a real MPI communicator (FORTRAN)