NAMD
mainfunc.C
Go to the documentation of this file.
1 
7 #include "memusage.h"
8 #include "converse.h"
9 #include "common.h"
10 #include "BackEnd.h"
11 #include "InfoStream.h"
12 #include "Broadcasts.h"
13 
14 #include "NamdState.h"
15 #include "Node.h"
16 #if defined(WIN32) && !defined(__CYGWIN__)
17 #include <direct.h>
18 #define CHDIR _chdir
19 #define GETCWD _getcwd
20 #define PATHSEP '\\'
21 #define PATHSEPSTR "\\"
22 #else
23 #include <unistd.h>
24 #define CHDIR chdir
25 #define GETCWD getcwd
26 #define PATHSEP '/'
27 #define PATHSEPSTR "/"
28 #endif
29 #include <sys/stat.h>
30 #include "ConfigList.h"
31 #include "ScriptTcl.h"
32 
33 
34 void after_backend_init(int argc, char **argv);
35 
36 #ifdef MEM_OPT_VERSION
37 //record the working directory when reading the configuration file
38 //for Parallel IO Input --Chao Mei
39 char *gWorkDir = NULL;
40 #endif
41 
42 int main(int argc, char **argv) {
43 #ifdef NAMD_TCL
44  if ( argc > 2 && ! strcmp(argv[1],"+tclsh") ) {
45  // pass all remaining arguments to script
46  return ScriptTcl::tclsh(argc-2,argv+2);
47  }
48 #endif
49  BackEnd::init(argc,argv);
50  after_backend_init(argc, argv);
51  return 0;
52 }
53 
54 void after_backend_init(int argc, char **argv){
55 #define CWDSIZE 1024
56  char origcwd_buf[CWDSIZE];
57  char currentdir_buf[CWDSIZE];
58 
59  ScriptTcl *script = new ScriptTcl;
60  Node::Object()->setScript(script);
61 
62  for(argc = 0; argv[argc]; ++argc);
63  if ( argc < 2 ) {
64 #if defined(WIN32) && !defined(__CYGWIN__)
65  CkPrintf("\nFATAL ERROR: No simulation config file specified on command line.\n");
66  CkPrintf("\nNOTE: NAMD has no graphical interface and must be run from a command line.\n");
67  int nsleep = 10;
68  CkPrintf("\nSleeping %d seconds before exiting...\n", nsleep);
69  fflush(stdout);
70  sleep(nsleep);
71  CkPrintf("\n");
72 #endif
73  NAMD_die("No simulation config file specified on command line.");
74  }
75  char *origcwd = GETCWD(origcwd_buf,CWDSIZE);
76  if ( ! origcwd ) NAMD_err("getcwd");
77 #ifdef NAMD_TCL
78  for(int i = 1; i < argc; ++i) {
79  if ( strstr(argv[i],"--") == argv[i] ) {
80  char buf[1024];
81  if ( i + 1 == argc ) {
82  sprintf(buf, "missing argument for command line option %s", argv[i]);
83  NAMD_die(buf);
84  }
85  if ( ! strcmp(argv[i],"--tclmain") ) {
86  // pass all remaining arguments to script
87  iout << iINFO << "Command-line argument is";
88  for ( int j=i; j<argc; ++j ) { iout << " " << argv[j]; }
89  iout << "\n" << endi;
90  script->tclmain(argc-i-1,argv+i+1);
91  BackEnd::exit();
92  return;
93  }
94  sprintf(buf, "%s %s", argv[i]+2, argv[i+1]);
95  iout << iINFO << "Command-line argument is --" << buf << "\n" << endi;
96  script->eval(buf);
97  ++i;
98  continue;
99  }
100  char *confFile = argv[i];
101 #else
102  char *confFile = argv[argc-1];
103 #endif
104  iout << iINFO << "Configuration file is " << confFile << "\n" << endi;
105 
106  char *currentdir=confFile;
107  char *tmp;
108  char tmpcurdir[3];
109  for(tmp=confFile;*tmp;++tmp); // find final null
110  for( ; tmp != confFile && *tmp != PATHSEP; --tmp); // find last '/'
111 #if defined(WIN32) && !defined(__CYGWIN__)
112  if (tmp == confFile) {
113  // in case this is under cygwin, search for '/' as well
114  for(tmp=confFile;*tmp;++tmp); // find final null
115  for( ; tmp != confFile && *tmp != '/'; --tmp); // find last '/'
116  }
117 #endif
118  if ( CHDIR(origcwd) ) NAMD_err(origcwd);
119  if ( tmp != confFile )
120  {
121  *tmp = 0; confFile = tmp + 1;
122  if ( CHDIR(currentdir) ) NAMD_err(currentdir);
123  struct stat statBuf;
124  if (stat(confFile, &statBuf)) {
125  char buf[1024];
126  sprintf(buf,"Unable to access config file %s%c%s",currentdir,PATHSEP,confFile);
127  NAMD_die(buf);
128  }
129  iout << iINFO << "Changed directory to " << currentdir << "\n" << endi;
130  currentdir = GETCWD(currentdir_buf,CWDSIZE);
131  if ( ! currentdir ) NAMD_err("getcwd after chdir");
132  }
133  else{
134  if ( *tmp == PATHSEP ){ // config file in / is odd, but it might happen
136  struct stat statBuf;
137  if (stat(confFile, &statBuf)) {
138  char buf[1024];
139  sprintf(buf,"Unable to access config file %s",confFile);
140  NAMD_die(buf);
141  }
142  }else{ // just a config file name, so the path is the current working path
143  struct stat statBuf;
144  if (stat(confFile, &statBuf)) {
145  char buf[1024];
146  if ( confFile[0] == '-' || confFile[0] == '+' ) {
147  sprintf(buf,"Unknown command-line option %s",confFile);
148  } else {
149  sprintf(buf,"Unable to access config file %s",confFile);
150  }
151  NAMD_die(buf);
152  }
153  tmpcurdir[0] = '.';
154  tmpcurdir[1] = PATHSEP;
155  tmpcurdir[2] = 0;
156  currentdir = tmpcurdir;
157  iout << iINFO << "Working in the current directory " << origcwd << "\n" << endi;
158  }
159  }
160 
161 #ifdef MEM_OPT_VERSION
162  int dirlen = strlen(currentdir);
163  gWorkDir = new char[dirlen+1];
164  gWorkDir[dirlen]=0;
165  memcpy(gWorkDir, currentdir, dirlen);
166 #endif
167 
168  currentdir = NULL;
169 
170 #ifdef NAMD_TCL
171  script->load(confFile);
172 #else
173  script->run(confFile);
174 #endif
175 
176 #ifdef NAMD_TCL
177 }
178  script->run();
179 #endif
180 
181  BackEnd::exit();
182 }
183 
static Node * Object()
Definition: Node.h:86
std::ostream & iINFO(std::ostream &s)
Definition: InfoStream.C:107
void after_backend_init(int argc, char **argv)
Definition: mainfunc.C:54
void NAMD_err(const char *err_msg)
Definition: common.C:102
void load(char *scriptFile)
Definition: ScriptTcl.C:2304
static void exit(int status=0)
Definition: BackEnd.C:276
#define iout
Definition: InfoStream.h:87
static void init(int argc, char **argv)
Definition: BackEnd.C:239
#define CHDIR
Definition: mainfunc.C:24
#define PATHSEP
Definition: mainfunc.C:26
void run()
Definition: ScriptTcl.C:2321
void setScript(ScriptTcl *s)
Definition: Node.h:191
#define CWDSIZE
void NAMD_die(const char *err_msg)
Definition: common.C:83
void tclmain(int, char **)
Definition: ScriptTcl.C:2287
void eval(char *script)
Definition: ScriptTcl.C:2247
#define PATHSEPSTR
Definition: mainfunc.C:27
int main(int argc, char *argv[])
Definition: diffbinpdb.c:15
#define GETCWD
Definition: mainfunc.C:25
infostream & endi(infostream &s)
Definition: InfoStream.C:38
static int tclsh(int, char **)
Definition: ScriptTcl.C:2265