#include "TROOT.h"
#include "TClass.h"
#include "TVirtualX.h"
#include "Getline.h"
#include "TStyle.h"
#include "TObjectTable.h"
#include "TClassTable.h"
#include "TStopwatch.h"
#include "TBenchmark.h"
#include "TRint.h"
#include "TSystem.h"
#include "TEnv.h"
#include "TSysEvtHandler.h"
#include "TError.h"
#include "TException.h"
#include "TInterpreter.h"
#include "TObjArray.h"
#include "TObjString.h"
#include "TTabCom.h"
#include "TError.h"
#include "G__ci.h"
#include "snprintf.h"
#ifdef R__UNIX
#include <signal.h>
#endif
R__EXTERN void *gMmallocDesc; 
static Int_t Key_Pressed(Int_t key)
{
   gApplication->KeyPressed(key);
   return 0;
}
static Int_t BeepHook()
{
   if (!gSystem) return 0;
   gSystem->Beep();
   return 1;
}
static void ResetTermAtExit()
{
   
   Getlinem(kCleanUp, 0);
}
class TInterruptHandler : public TSignalHandler {
public:
   TInterruptHandler() : TSignalHandler(kSigInterrupt, kFALSE) { }
   Bool_t  Notify();
};
Bool_t TInterruptHandler::Notify()
{
   
   if (fDelay) {
      fDelay++;
      return kTRUE;
   }
   
   gMmallocDesc = 0;
   if (!G__get_security_error())
      G__genericerror("\n *** Break *** keyboard interrupt");
   else {
      Break("TInterruptHandler::Notify", "keyboard interrupt");
      if (TROOT::Initialized()) {
         Getlinem(kInit, "Root > ");
         gInterpreter->RewindDictionary();
         Throw(GetSignal());
      }
   }
   return kTRUE;
}
class TTermInputHandler : public TFileHandler {
public:
   TTermInputHandler(Int_t fd) : TFileHandler(fd, 1) { }
   Bool_t Notify();
   Bool_t ReadNotify() { return Notify(); }
};
Bool_t TTermInputHandler::Notify()
{
   
   return gApplication->HandleTermInput();
}
ClassImp(TRint)
TRint::TRint(const char *appClassName, Int_t *argc, char **argv, void *options,
             Int_t numOptions, Bool_t noLogo)
       : TApplication(appClassName, argc, argv, options, numOptions)
{
   
   
   
   
   fNcmd          = 0;
   fDefaultPrompt = "root [%d] ";
   fInterrupt     = kFALSE;
   gBenchmark = new TBenchmark();
   if (!noLogo && !NoLogoOpt())
      PrintLogo();
   
   Int_t includes = gEnv->GetValue("Rint.Includes",1);
   
   
   
   
   
   
   
   
   
   
   
   if (includes > 0) {
      ProcessLine("#include <iostream>", kTRUE);
      ProcessLine("#include <_string>", kTRUE); 
      ProcessLine("#include <RtypesCint.h>", kTRUE);
      if (includes > 1) {
         ProcessLine("#include <vector>", kTRUE);  
         ProcessLine("#include <pair>", kTRUE);    
                                                   
      }
   }
   
   ProcessLine("#define ROOT_Rtypes 0", kTRUE);
   ProcessLine("#define ROOT_TError 0", kTRUE);
   ProcessLine("#define ROOT_TGenericClassInfo 0", kTRUE);
   
   const char *logon;
   logon = gEnv->GetValue("Rint.Load", (char*)0);
   if (logon) {
      char *mac = gSystem->Which(TROOT::GetMacroPath(), logon, kReadPermission);
      if (mac)
         ProcessLine(Form(".L %s",logon),kTRUE);
      delete [] mac;
   }
   
   ExecLogon();
   
   gInterpreter->SaveContext();
   gInterpreter->SaveGlobalsContext();
   
   TInterruptHandler *ih = new TInterruptHandler();
   ih->Add();
   SetSignalHandler(ih);
   
   fInputHandler = new TTermInputHandler(0);
   fInputHandler->Add();
   
   char defhist[kMAXPATHLEN];
#ifndef R__VMS
   sprintf(defhist, "%s/.root_hist", gSystem->HomeDirectory());
#else
   sprintf(defhist, "%s.root_hist", gSystem->HomeDirectory());
#endif
   logon = gEnv->GetValue("Rint.History", defhist);
   int hist_size = gEnv->GetValue("Rint.HistorySize", 500);
   int hist_save = gEnv->GetValue("Rint.HistorySave", 400);
   const char *envHist = gSystem->Getenv("ROOT_HIST");
   if (envHist) {
      hist_size = atoi(envHist);
      envHist = strchr(envHist, ':');
      if (envHist)
         hist_save = atoi(envHist+1);
   }
   Gl_histsize(hist_size, hist_save);
   Gl_histinit((char *)logon);
   Gl_windowchanged();
   atexit(ResetTermAtExit);
   
   gTabCom      = new TTabCom;
   Gl_in_key    = &Key_Pressed;
   Gl_beep_hook = &BeepHook;
}
TRint::~TRint()
{
   
}
void TRint::ExecLogon()
{
   
   
   
   
   
   
   
   if (NoLogOpt()) return;
   TString name = ".rootlogon.C";
   TString sname = "system";
   sname += name;
#ifdef ROOTETCDIR
   char *s = gSystem->ConcatFileName(ROOTETCDIR, sname);
#else
   TString etc = gRootDir;
#ifdef WIN32
   etc += "\\etc";
#else
   etc += "/etc";
#endif
   char *s = gSystem->ConcatFileName(etc, sname);
#endif
   if (!gSystem->AccessPathName(s, kReadPermission)) {
      ProcessFile(s);
   }
   delete [] s;
   s = gSystem->ConcatFileName(gSystem->HomeDirectory(), name);
   if (!gSystem->AccessPathName(s, kReadPermission)) {
      ProcessFile(s);
   }
   delete [] s;
   
   if (strcmp(gSystem->HomeDirectory(), gSystem->WorkingDirectory())) {
      if (!gSystem->AccessPathName(name, kReadPermission))
         ProcessFile(name);
   }
   
   const char *logon = gEnv->GetValue("Rint.Logon", (char*)0);
   if (logon) {
      char *mac = gSystem->Which(TROOT::GetMacroPath(), logon, kReadPermission);
      if (mac)
         ProcessFile(logon);
      delete [] mac;
   }
}
void TRint::Run(Bool_t retrn)
{
   
   
   
   
   
   
   
   
   Getlinem(kInit, GetPrompt());
   Long_t retval = 0;
   Int_t  error = 0;
   volatile Bool_t needGetlinemInit = kFALSE;
   
   if (InputFiles()) {
      
      
      fInputHandler->DeActivate();
      TIter next(InputFiles());
      RETRY {
         retval = 0; error = 0;
         Int_t nfile = 0;
         TObjString *file;
         while ((file = (TObjString *)next())) {
            char cmd[kMAXPATHLEN+50];
            if (!fNcmd)
               printf("\n");
            if (file->String().EndsWith(".root") || file->String().BeginsWith("file:")) {
               file->String().ReplaceAll("\\","/");
               const char *rfile = (const char*)file->String();
               Printf("Attaching file %s as _file%d...", rfile, nfile);
               snprintf(cmd, kMAXPATHLEN+50, "TFile *_file%d = TFile::Open(\"%s\")", nfile++, rfile);
            } else {
               Printf("Processing %s...", (const char*)file->String());
               snprintf(cmd, kMAXPATHLEN+50, ".x %s", (const char*)file->String());
            }
            Getlinem(kCleanUp, 0);
            Gl_histadd(cmd);
            fNcmd++;
            
            
            
            needGetlinemInit = kFALSE;
            retval = ProcessLine(cmd, kFALSE, &error);
            gInterpreter->EndOfLineAction();
            
            
            needGetlinemInit = kTRUE;
            if (error != 0) break;
         }
      } ENDTRY;
      
      
      fInputHandler->Activate();
      if (QuitOpt()) {
         if (retrn) return;
         Terminate(error == 0 ? retval : error);
      }
      ClearInputFiles();
      if (needGetlinemInit) Getlinem(kInit, GetPrompt());
   }
   if (QuitOpt()) {
      printf("\n");
      if (retrn) return;
      Terminate(0);
   }
   TApplication::Run(retrn);
   Getlinem(kCleanUp, 0);
}
void TRint::PrintLogo()
{
   
   Int_t iday,imonth,iyear;
   static const char *months[] = {"January","February","March","April","May",
                                  "June","July","August","September","October",
                                  "November","December"};
   const char *root_version = gROOT->GetVersion();
   Int_t idatqq = gROOT->GetVersionDate();
   iday   = idatqq%100;
   imonth = (idatqq/100)%100;
   iyear  = (idatqq/10000);
   char *version_date = Form("%d %s %4d",iday,months[imonth-1],iyear);
   idatqq = gROOT->GetBuiltDate();
   iday   = idatqq%100;
   imonth = (idatqq/100)%100;
   iyear  = (idatqq/10000);
   char *built_date = Form("%d %s %4d",iday,months[imonth-1],iyear);
   Printf("  *******************************************");
   Printf("  *                                         *");
   Printf("  *        W E L C O M E  to  R O O T       *");
   Printf("  *                                         *");
   Printf("  *   Version%10s %17s   *", root_version, version_date);
   Printf("  *                                         *");
   Printf("  *  You are welcome to visit our Web site  *");
   Printf("  *          http://root.cern.ch            *");
   Printf("  *                                         *");
   Printf("  *******************************************");
   if (strstr(gVirtualX->GetName(), "TTF")) {
      Int_t major, minor, patch;
      
      
      major = 2; minor = 1; patch = 9;
      Printf("\nFreeType Engine v%d.%d.%d used to render TrueType fonts.",
             major, minor, patch);
   }
#if defined (_REENTRANT) || defined (WIN32)
   else
      printf("\n");
   Printf("Compiled on %s for %s with thread support.", built_date,
          gSystem->GetBuildArch());
#else
   else
      printf("\n");
   Printf("Compiled on %s for %s.", built_date, gSystem->GetBuildArch());
#endif
   gInterpreter->PrintIntro();
#ifdef R__UNIX
   
   for (int i = 0; i < Argc(); i++)
      if (!strcmp(Argv(i), "-splash"))
         kill(getppid(), SIGUSR1);
#endif
}
char *TRint::GetPrompt()
{
   
   char *s = gInterpreter->GetPrompt();
   if (s[0])
      strcpy(fPrompt, s);
   else
      sprintf(fPrompt, fDefaultPrompt.Data(), fNcmd);
   return fPrompt;
}
const char *TRint::SetPrompt(const char *newPrompt)
{
   
   
   
   
   
   
   
   static TString op = fDefaultPrompt;
   if (newPrompt && strlen(newPrompt) <= 55)
      fDefaultPrompt = newPrompt;
   else
      Error("SetPrompt", "newPrompt too long (> 55 characters)");
   return op.Data();
}
Bool_t TRint::HandleTermInput()
{
   
   static TStopwatch timer;
   char *line;
   if ((line = Getlinem(kOneChar, 0))) {
      if (line[0] == 0 && Gl_eof())
         Terminate(0);
      gVirtualX->SetKeyAutoRepeat(kTRUE);
      Gl_histadd(line);
      TString sline = line;
      line[0] = 0;
      
      sline = sline.Chop();
      sline = sline.Strip(TString::kBoth);
      ReturnPressed((char*)sline.Data());
      fInterrupt = kFALSE;
      if (!gInterpreter->GetMore() && !sline.IsNull()) fNcmd++;
      
      fInputHandler->DeActivate();
      if (gROOT->Timer()) timer.Start();
      Bool_t added = kFALSE;
      
      SetBit(kProcessRemotely);
#ifdef R__EH
      try {
#endif
         TRY {
            ProcessLine(sline);
         } CATCH(excode) {
            
            fInputHandler->Activate();
            added = kTRUE;
            Throw(excode);
         } ENDTRY;
#ifdef R__EH
      }
      
      catch (...) {
         
         if (!added) fInputHandler->Activate();
         throw;
      }
#endif
      if (gROOT->Timer()) timer.Print("u");
      
      fInputHandler->Activate();
      if (!sline.BeginsWith(".reset"))
         gInterpreter->EndOfLineAction();
      gTabCom->ClearAll();
      Getlinem(kInit, GetPrompt());
   }
   return kTRUE;
}
void TRint::Terminate(Int_t status)
{
   
   
   Getlinem(kCleanUp, 0);
   if (ReturnFromRun()) {
      gSystem->ExitLoop();
   } else {
      
      const char *logoff;
      logoff = gEnv->GetValue("Rint.Logoff", (char*)0);
      if (logoff && !NoLogOpt()) {
         char *mac = gSystem->Which(TROOT::GetMacroPath(), logoff, kReadPermission);
         if (mac)
            ProcessFile(logoff);
         delete [] mac;
      }
      gSystem->Exit(status);
   }
}
void TRint::SetEchoMode(Bool_t mode)
{
   
   
   
   
   Gl_config("noecho", mode ? 0 : 1);
}
Long_t TRint::ProcessRemote(const char *line, Int_t *)
{
   
   
   
   
   
   
   
   
   
   Long_t ret = TApplication::ProcessRemote(line);
   if (ret == 1) {
      if (fAppRemote) {
         TString prompt = Form("%s:root [%%d] ", fAppRemote->ApplicationName());
         SetPrompt(prompt);
      } else {
         SetPrompt("root [%d] ");
      }
   }
   return ret;
}
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.