NAMD
Macros | Functions
mainfunc.C File Reference
#include "memusage.h"
#include "converse.h"
#include "common.h"
#include "BackEnd.h"
#include "InfoStream.h"
#include "Broadcasts.h"
#include "NamdState.h"
#include "Node.h"
#include <unistd.h>
#include <sys/stat.h>
#include "ConfigList.h"
#include "ScriptTcl.h"

Go to the source code of this file.

Macros

#define CHDIR   chdir
 
#define GETCWD   getcwd
 
#define PATHSEP   '/'
 
#define PATHSEPSTR   "/"
 
#define CWDSIZE   1024
 

Functions

void after_backend_init (int argc, char **argv)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

#define CHDIR   chdir

Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000 by The Board of Trustees of the University of Illinois. All rights reserved.

Definition at line 24 of file mainfunc.C.

Referenced by after_backend_init().

#define CWDSIZE   1024

Referenced by after_backend_init().

#define GETCWD   getcwd

Definition at line 25 of file mainfunc.C.

Referenced by after_backend_init().

#define PATHSEP   '/'

Definition at line 26 of file mainfunc.C.

Referenced by after_backend_init().

#define PATHSEPSTR   "/"

Definition at line 27 of file mainfunc.C.

Referenced by after_backend_init().

Function Documentation

void after_backend_init ( int  argc,
char **  argv 
)

Definition at line 54 of file mainfunc.C.

References CHDIR, CWDSIZE, endi(), ScriptTcl::eval(), BackEnd::exit(), GETCWD, iINFO(), iout, ScriptTcl::load(), NAMD_die(), NAMD_err(), Node::Object(), PATHSEP, PATHSEPSTR, ScriptTcl::run(), Node::setScript(), and ScriptTcl::tclmain().

Referenced by main(), and slave_init().

54  {
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 }
static Node * Object()
Definition: Node.h:86
std::ostream & iINFO(std::ostream &s)
Definition: InfoStream.C:107
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
#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
#define GETCWD
Definition: mainfunc.C:25
infostream & endi(infostream &s)
Definition: InfoStream.C:38
int main ( int  argc,
char **  argv 
)

Definition at line 42 of file mainfunc.C.

References after_backend_init(), BackEnd::init(), and ScriptTcl::tclsh().

42  {
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 }
void after_backend_init(int argc, char **argv)
Definition: mainfunc.C:54
static void init(int argc, char **argv)
Definition: BackEnd.C:239
static int tclsh(int, char **)
Definition: ScriptTcl.C:2265