mainfunc.C

Go to the documentation of this file.
00001 
00007 #include "memusage.h"
00008 #include "converse.h"
00009 #include "common.h"
00010 #include "BackEnd.h"
00011 #include "InfoStream.h"
00012 #include "Broadcasts.h"
00013 
00014 #include "NamdState.h"
00015 #include "Node.h"
00016 #if defined(WIN32) && !defined(__CYGWIN__)
00017 #include <direct.h>
00018 #define CHDIR _chdir
00019 #define GETCWD _getcwd
00020 #define PATHSEP '\\'
00021 #define PATHSEPSTR "\\"
00022 #else
00023 #include <unistd.h>
00024 #define CHDIR chdir
00025 #define GETCWD getcwd
00026 #define PATHSEP '/'
00027 #define PATHSEPSTR "/"
00028 #endif
00029 #include <sys/stat.h>
00030 #include "ConfigList.h"
00031 #include "ScriptTcl.h"
00032 
00033 
00034 void after_backend_init(int argc, char **argv);
00035 
00036 #ifdef MEM_OPT_VERSION
00037 //record the working directory when reading the configuration file
00038 //for Parallel IO Input --Chao Mei
00039 char *gWorkDir = NULL;
00040 #endif
00041 
00042 int main(int argc, char **argv) {
00043 #ifdef NAMD_TCL
00044   if ( argc > 2 && ! strcmp(argv[1],"+tclsh") ) {
00045     // pass all remaining arguments to script
00046     return ScriptTcl::tclsh(argc-2,argv+2);
00047   }
00048 #endif
00049   BackEnd::init(argc,argv);
00050   after_backend_init(argc, argv);
00051   return 0;
00052 }
00053 
00054 void after_backend_init(int argc, char **argv){
00055 #define CWDSIZE 1024
00056   char origcwd_buf[CWDSIZE];
00057   char currentdir_buf[CWDSIZE];
00058 
00059   ScriptTcl *script = new ScriptTcl;
00060   Node::Object()->setScript(script);
00061 
00062   for(argc = 0; argv[argc]; ++argc);
00063   if ( argc < 2 ) {
00064 #if defined(WIN32) && !defined(__CYGWIN__)
00065     CkPrintf("\nFATAL ERROR: No simulation config file specified on command line.\n");
00066     CkPrintf("\nNOTE: NAMD has no graphical interface and must be run from a command line.\n");
00067     int nsleep = 10;
00068     CkPrintf("\nSleeping %d seconds before exiting...\n", nsleep);
00069     fflush(stdout);
00070     sleep(nsleep);
00071     CkPrintf("\n");
00072 #endif
00073     NAMD_die("No simulation config file specified on command line.");
00074   }
00075   char *origcwd = GETCWD(origcwd_buf,CWDSIZE);
00076   if ( ! origcwd ) NAMD_err("getcwd");
00077 #ifdef NAMD_TCL
00078   for(int i = 1; i < argc; ++i) {
00079   if ( strstr(argv[i],"--") == argv[i] ) {
00080     char buf[1024];
00081     if ( i + 1 == argc ) {
00082       sprintf(buf, "missing argument for command line option %s", argv[i]);
00083       NAMD_die(buf);
00084     }
00085     if ( ! strcmp(argv[i],"--tclmain") ) {
00086       // pass all remaining arguments to script
00087       iout << iINFO << "Command-line argument is";
00088       for ( int j=i; j<argc; ++j ) { iout << " " << argv[j]; }
00089       iout << "\n" << endi;
00090       script->tclmain(argc-i-1,argv+i+1);
00091       BackEnd::exit();
00092       return;
00093     }
00094     sprintf(buf, "%s %s", argv[i]+2, argv[i+1]);
00095     iout << iINFO << "Command-line argument is --" << buf << "\n" << endi;
00096     script->eval(buf);
00097     ++i;
00098     continue;
00099   }
00100   char *confFile = argv[i];
00101 #else
00102   char *confFile = argv[argc-1];
00103 #endif
00104   iout << iINFO << "Configuration file is " << confFile << "\n" << endi;
00105 
00106   char *currentdir=confFile;
00107   char *tmp;
00108   for(tmp=confFile;*tmp;++tmp); // find final null
00109   for( ; tmp != confFile && *tmp != PATHSEP; --tmp); // find last '/'
00110 #if defined(WIN32) && !defined(__CYGWIN__)
00111   if (tmp == confFile) {
00112     // in case this is under cygwin, search for '/' as well
00113     for(tmp=confFile;*tmp;++tmp); // find final null
00114     for( ; tmp != confFile && *tmp != '/'; --tmp); // find last '/'
00115   }
00116 #endif
00117   if ( CHDIR(origcwd) ) NAMD_err(origcwd);
00118   if ( tmp != confFile )
00119   {
00120     *tmp = 0; confFile = tmp + 1;
00121     if ( CHDIR(currentdir) ) NAMD_err(currentdir);
00122     struct stat statBuf;
00123     if (stat(confFile, &statBuf)) {
00124       char buf[1024];
00125       sprintf(buf,"Unable to access config file %s%c%s",currentdir,PATHSEP,confFile);
00126       NAMD_die(buf);
00127     }
00128     iout << iINFO << "Changed directory to " << currentdir << "\n" << endi;
00129     currentdir = GETCWD(currentdir_buf,CWDSIZE);
00130     if ( ! currentdir ) NAMD_err("getcwd after chdir");
00131   }
00132   else{
00133       if ( *tmp == PATHSEP ){ // config file in / is odd, but it might happen
00134           if ( CHDIR(PATHSEPSTR) ) NAMD_err(PATHSEPSTR);
00135           struct stat statBuf;
00136           if (stat(confFile, &statBuf)) {
00137             char buf[1024];
00138             sprintf(buf,"Unable to access config file %s",confFile);
00139             NAMD_die(buf);
00140           }
00141       }else{ // just a config file name, so the path is the current working path
00142           struct stat statBuf;
00143           if (stat(confFile, &statBuf)) {
00144             char buf[1024];
00145             if ( confFile[0] == '-' || confFile[0] == '+' ) {
00146               sprintf(buf,"Unknown command-line option %s",confFile);
00147             } else {
00148               sprintf(buf,"Unable to access config file %s",confFile);
00149             }
00150             NAMD_die(buf);
00151           }
00152           char tmpcurdir[3];
00153           tmpcurdir[0] = '.';
00154           tmpcurdir[1] = PATHSEP;
00155           tmpcurdir[2] = 0;
00156           currentdir = tmpcurdir;
00157           iout << iINFO << "Working in the current directory " << origcwd << "\n" << endi;
00158       }
00159   }
00160 
00161 #ifdef MEM_OPT_VERSION
00162     int dirlen = strlen(currentdir);
00163     gWorkDir = new char[dirlen+1];
00164     gWorkDir[dirlen]=0;
00165     memcpy(gWorkDir, currentdir, dirlen);
00166 #endif
00167 
00168   currentdir = NULL;
00169 
00170 #ifdef NAMD_TCL
00171   script->load(confFile);
00172 #else
00173   script->run(confFile);
00174 #endif
00175 
00176 #ifdef NAMD_TCL
00177 }
00178   script->run();
00179 #endif
00180 
00181   BackEnd::exit();
00182 }
00183 

Generated on Tue Sep 19 01:17:12 2017 for NAMD by  doxygen 1.4.7