00001
00002
00003
00004
00005
00006
00008 #include "JobControl/JobCRootEnv.h"
00009
00010 #include <TSystem.h>
00011
00012 #include <iostream>
00013 #include <cassert>
00014 #include <cstring>
00015 #include <cstdio>
00016 using namespace std;
00017 extern "C" {
00018 #include <sys/types.h>
00019 #include <dirent.h>
00020 #ifdef MACOSX
00021 #include <unistd.h>
00022 #else
00023 #include <getopt.h>
00024 #endif
00025 #ifndef GETOPTDONE // Some getopt.h's have this, some don't...
00026 #define GETOPTDONE (-1)
00027 #endif
00028 }
00029 #include "MessageService/MsgService.h"
00030
00031 #include "TROOT.h"
00032 #include "TRint.h"
00033 #include "TSystem.h"
00034 #include "TString.h"
00035 #include "TSysEvtHandler.h"
00036 #include "TInterpreter.h"
00037
00038 #include "JobControl/IsArgMacroFile.h"
00039
00040 CVSID("$Id: JobCRootEnv.cxx,v 1.35 2007/04/26 14:22:13 west Exp $");
00041
00042
00043
00044 JobCRootEnv::JobCRootEnv(int argc, char** argv) :
00045 fHaveBatchOpt(false),
00046 fHaveQuitOpt(false),
00047 fHaveMacroFiles(false),
00048 fHaveInaccessibleFile(false)
00049 {
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 static int localArgc = 0;
00061 static char* localArgv[1024];
00062 if (argc>0) {
00063 localArgv[localArgc++] = argv[0];
00064 }
00065 else {
00066 localArgv[localArgc++] = const_cast<char*>("moot");
00067 }
00068
00069
00070 if (gROOT==0) {
00071 static TROOT root("TROOT",localArgv[0]);
00072 }
00073 assert(gROOT);
00074
00075
00076 TApplication* app = gROOT->GetApplication();
00077 if (app==0) {
00078 int c;
00079
00080 #ifndef MACOSX
00081 optind = 0;
00082 #else
00083 optind = 1;
00084 #endif
00085
00086 #ifdef IRIX6
00087 getoptreset();
00088 #endif
00089 const char* opts = "bs:nqlt:r:x:hH:d:u:p:o:v:";
00090 while ((c=getopt(argc, argv, opts)) != GETOPTDONE) {
00091 MSG("JobC",Msg::kDebug)
00092 << "Processing job command-line ROOT option argument: "
00093 << c << " '" << (char)c << "'" << endl;
00094 switch (c) {
00095
00096
00097 case 'l': localArgv[localArgc++] = const_cast<char*>("-l"); break;
00098 case 'b': localArgv[localArgc++] = const_cast<char*>("-b"); fHaveBatchOpt = true; break;
00099 case 'n': localArgv[localArgc++] = const_cast<char*>("-n"); break;
00100 case 'q': localArgv[localArgc++] = const_cast<char*>("-q"); fHaveQuitOpt = true; break;
00101 case 'h': localArgv[localArgc++] = const_cast<char*>("-h"); break;
00102 default: break;
00103 }
00104 }
00105
00106
00107 int ndir = 0;
00108 for (int i=optind; i<argc; ++i) {
00109
00110
00111 DIR* d = opendir(argv[i]);
00112 if (d) {
00113 localArgv[localArgc++] = argv[i];
00114 closedir(d);
00115 ++ndir;
00116 }
00117
00118
00119
00120 TString fname = IsArgMacroFile(argv[i]);
00121 if (fname != "") {
00122 if (gSystem->AccessPathName(fname.Data())) {
00123 cerr << "Skipping inaccessible file: " << argv[i] << endl;
00124 fHaveInaccessibleFile = true;
00125 continue;
00126 }
00127 fHaveMacroFiles = true;
00128 localArgv[localArgc++] = argv[i];
00129 }
00130 }
00131
00132
00133 if (ndir>1) {
00134 MSG("JobC",Msg::kError) <<
00135 "Ambiguous: More than one directory specified on command line.\n";
00136 exit(1);
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146 TRint* rint = new TRint("TAPP",&localArgc,localArgv,0,0,kTRUE);
00147 assert(rint == gROOT->GetApplication());
00148 std::string p = gSystem->BaseName(localArgv[0]); p += " [%d] ";
00149 rint->SetPrompt(p.c_str());
00150
00151
00152 this->SignalConfig();
00153 this->InterpreterConfig();
00154 this->LoadIncludes();
00155 this->LoadClasses();
00156 }
00157 }
00158
00159
00160
00161 void JobCRootEnv::InterpreterConfig()
00162 {
00163
00164
00165
00166 if (gInterpreter) {
00167 gInterpreter->SaveContext();
00168 gInterpreter->SaveGlobalsContext();
00169 }
00170 }
00171
00172
00173
00174 void JobCRootEnv::SignalConfig()
00175 {
00176
00177
00178
00179 if (gSystem) {
00180
00181 gSystem->ResetSignal(kSigBus, kTRUE);
00182 gSystem->ResetSignal(kSigSegmentationViolation,kTRUE);
00183 gSystem->ResetSignal(kSigSystem, kTRUE);
00184 gSystem->ResetSignal(kSigPipe, kTRUE);
00185 gSystem->ResetSignal(kSigIllegalInstruction, kTRUE);
00186 gSystem->ResetSignal(kSigQuit, kTRUE);
00187 if (fHaveBatchOpt)
00188 gSystem->ResetSignal(kSigInterrupt, kTRUE);
00189 gSystem->ResetSignal(kSigWindowChanged, kTRUE);
00190
00191
00192 gSystem->ResetSignal(kSigChild, kTRUE);
00193 gSystem->ResetSignal(kSigUrgent, kTRUE);
00194 gSystem->ResetSignal(kSigFloatingException, kTRUE);
00195 gSystem->ResetSignal(kSigTermination, kTRUE);
00196 gSystem->ResetSignal(kSigUser1, kTRUE);
00197 gSystem->ResetSignal(kSigUser2, kTRUE);
00198 }
00199 }
00200
00201
00202
00203 void JobCRootEnv::LoadIncludes()
00204 {
00205
00206
00207
00208 TApplication* app = gROOT->GetApplication();
00209 if (app) {
00210
00211
00212 app->ProcessLine("#include <iomanip>");
00213 app->ProcessLine("#include <string>");
00214
00215
00216 TString mp = gROOT->GetMacroPath();
00217 TString ip;
00218 const char* p;
00219 p = gSystem->Getenv("SRT_PRIVATE_CONTEXT");
00220 if (p) {
00221 mp += ":";
00222 mp += p;
00223 mp += ":";
00224 mp += p;
00225 mp += "/macros";
00226 ip += " -I";
00227 ip += p;
00228 }
00229 p = gSystem->Getenv("SRT_PUBLIC_CONTEXT");
00230 if (p) {
00231 mp += ":";
00232 mp += p;
00233 mp += "/macros";
00234 ip += " -I";
00235 ip += p;
00236 }
00237
00238 gROOT->SetMacroPath(mp.Data());
00239 gSystem->SetIncludePath(ip);
00240
00241 TString dip = ".include ";
00242 dip += gSystem->Getenv("SRT_PRIVATE_CONTEXT");
00243 gROOT->ProcessLine(dip.Data());
00244
00245 dip = ".include ";
00246 dip += gSystem->Getenv("SRT_PUBLIC_CONTEXT");
00247 gROOT->ProcessLine(dip.Data());
00248 dip += "/RDBC/include";
00249 gROOT->ProcessLine(dip.Data());
00250 }
00251 }
00252
00253
00254
00255 void JobCRootEnv::LoadClasses()
00256 {
00257
00258
00259
00260 if (gROOT) {
00261 gROOT->LoadClass("TGeometry", "Graf3d");
00262 gROOT->LoadClass("TTree", "Tree");
00263 gROOT->LoadClass("TMatrix", "Matrix");
00264 gROOT->LoadClass("TMinuit", "Minuit");
00265 gROOT->LoadClass("TPostScript", "Postscript");
00266 gROOT->LoadClass("TCanvas", "Gpad");
00267 gROOT->LoadClass("THtml", "Html");
00268 }
00269 }
00270
00271
00272
00273 JobCRootEnv::~JobCRootEnv()
00274 {
00275
00276
00277
00278 }
00279
00280
00281
00282 int JobCRootEnv::RunTheApp()
00283 {
00284
00285
00286
00287 TApplication* app = gROOT->GetApplication();
00288 if (app) {
00289 app->Run(kTRUE);
00290 return 1;
00291 }
00292 return 0;
00293 }
00294