Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

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   BackEnd::init(argc,argv);
00044   after_backend_init(argc, argv);
00045   return 0;
00046 }
00047 
00048 void after_backend_init(int argc, char **argv){
00049 #define CWDSIZE 1024
00050   char origcwd_buf[CWDSIZE];
00051   char currentdir_buf[CWDSIZE];
00052 
00053   ScriptTcl *script = new ScriptTcl;
00054   Node::Object()->setScript(script);
00055 
00056   for(argc = 0; argv[argc]; ++argc);
00057   if ( argc < 2 ) {
00058     NAMD_die("No simulation config file specified on command line.");
00059   }
00060   char *origcwd = GETCWD(origcwd_buf,CWDSIZE);
00061   if ( ! origcwd ) NAMD_err("getcwd");
00062 #ifdef NAMD_TCL
00063   for(int i = 1; i < argc; ++i) {
00064   if ( strstr(argv[i],"--") == argv[i] ) {
00065     char buf[1024];
00066     if ( i + 1 == argc ) {
00067       sprintf(buf, "missing argument for command line option %s", argv[i]);
00068       NAMD_die(buf);
00069     }
00070     sprintf(buf, "%s %s", argv[i]+2, argv[i+1]);
00071     iout << iINFO << "Command-line argument is --" << buf << "\n" << endi;
00072     script->eval(buf);
00073     ++i;
00074     continue;
00075   }
00076   char *confFile = argv[i];
00077 #else
00078   char *confFile = argv[argc-1];
00079 #endif
00080   iout << iINFO << "Configuration file is " << confFile << "\n" << endi;
00081 
00082   char *currentdir=confFile;
00083   char *tmp;
00084   for(tmp=confFile;*tmp;++tmp); // find final null
00085   for( ; tmp != confFile && *tmp != PATHSEP; --tmp); // find last '/'
00086 #if defined(WIN32) && !defined(__CYGWIN__)
00087   if (tmp == confFile) {
00088     // in case this is under cygwin, search for '/' as well
00089     for(tmp=confFile;*tmp;++tmp); // find final null
00090     for( ; tmp != confFile && *tmp != '/'; --tmp); // find last '/'
00091   }
00092 #endif
00093   if ( CHDIR(origcwd) ) NAMD_err(origcwd);
00094   if ( tmp != confFile )
00095   {
00096     *tmp = 0; confFile = tmp + 1;
00097     if ( CHDIR(currentdir) ) NAMD_err(currentdir);
00098     struct stat statBuf;
00099     if (stat(confFile, &statBuf)) {
00100       char buf[1024];
00101       sprintf(buf,"Unable to access config file %s%c%s",currentdir,PATHSEP,confFile);
00102       NAMD_die(buf);
00103     }
00104     iout << iINFO << "Changed directory to " << currentdir << "\n" << endi;
00105     currentdir = GETCWD(currentdir_buf,CWDSIZE);
00106     if ( ! currentdir ) NAMD_err("getcwd after chdir");
00107   }
00108   else{
00109       if ( *tmp == PATHSEP ){ // config file in / is odd, but it might happen
00110           if ( CHDIR(PATHSEPSTR) ) NAMD_err(PATHSEPSTR);
00111           struct stat statBuf;
00112           if (stat(confFile, &statBuf)) {
00113             char buf[1024];
00114             sprintf(buf,"Unable to access config file %s",confFile);
00115             NAMD_die(buf);
00116           }
00117       }else{ // just a config file name, so the path is the current working path
00118           struct stat statBuf;
00119           if (stat(confFile, &statBuf)) {
00120             char buf[1024];
00121             if ( confFile[0] == '-' || confFile[0] == '+' ) {
00122               sprintf(buf,"Unknown command-line option %s",confFile);
00123             } else {
00124               sprintf(buf,"Unable to access config file %s",confFile);
00125             }
00126             NAMD_die(buf);
00127           }
00128           char tmpcurdir[3];
00129           tmpcurdir[0] = '.';
00130           tmpcurdir[1] = PATHSEP;
00131           tmpcurdir[2] = 0;
00132           currentdir = tmpcurdir;
00133           iout << iINFO << "Working in the current directory " << origcwd << "\n" << endi;
00134       }
00135   }
00136 
00137 #ifdef MEM_OPT_VERSION
00138     int dirlen = strlen(currentdir);
00139     gWorkDir = new char[dirlen+1];
00140     gWorkDir[dirlen]=0;
00141     memcpy(gWorkDir, currentdir, dirlen);
00142 #endif
00143 
00144   currentdir = NULL;
00145 
00146 #ifdef NAMD_TCL
00147   script->load(confFile);
00148 #else
00149   script->run(confFile);
00150 #endif
00151 
00152 #ifdef NAMD_TCL
00153 }
00154   script->run();
00155 #endif
00156 
00157   BackEnd::exit();
00158 }
00159 

Generated on Sun Feb 12 04:07:55 2012 for NAMD by  doxygen 1.3.9.1