// @(#)root/base:$Id: 8944840ba34631ec28efc779647618db43c0eee5 $
// Author: Fons Rademakers   15/09/95

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TSystem                                                              //
//                                                                      //
// Abstract base class defining a generic interface to the underlying   //
// Operating System.                                                    //
// This is not an ABC in the strict sense of the (C++) word. For        //
// every member function there is an implementation (often not more     //
// than a call to AbstractMethod() which prints a warning saying        //
// that the method should be overridden in a derived class), which      //
// allows a simple partial implementation for new OS'es.                //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifdef WIN32
#include <io.h>
#endif
#include <stdlib.h>
#include <errno.h>
#include <algorithm>
#include <sys/stat.h>

#include "Riostream.h"
#include "TSystem.h"
#include "TApplication.h"
#include "TException.h"
#include "TROOT.h"
#include "TClass.h"
#include "TClassTable.h"
#include "TEnv.h"
#include "TBrowser.h"
#include "TString.h"
#include "TOrdCollection.h"
#include "TInterpreter.h"
#include "TRegexp.h"
#include "TTimer.h"
#include "TObjString.h"
#include "TError.h"
#include "TPluginManager.h"
#include "TUrl.h"
#include "TVirtualMutex.h"
#include "compiledata.h"
#include "RConfigure.h"

const char *gRootDir;
const char *gProgName;
const char *gProgPath;

TSystem      *gSystem   = 0;
TFileHandler *gXDisplay = 0;  // Display server event handler, set in TGClient

static Int_t *gLibraryVersion    = 0;   // Set in TVersionCheck, used in Load()
static Int_t  gLibraryVersionIdx = 0;   // Set in TVersionCheck, used in Load()
static Int_t  gLibraryVersionMax = 256;


ClassImp(TProcessEventTimer)

//______________________________________________________________________________
TProcessEventTimer::TProcessEventTimer(Long_t delay) : TTimer(delay, kFALSE)
{
   // Create async event processor timer. Delay is in milliseconds.

   gROOT->SetInterrupt(kFALSE);
   TurnOn();
}

//______________________________________________________________________________
Bool_t TProcessEventTimer::ProcessEvents()
{
   // Process events if timer did time out. Returns kTRUE if interrupt
   // flag is set (by hitting a key in the canvas or selecting the
   // Interrupt menu item in canvas or some other action).

   if (fTimeout) {
      if (gSystem->ProcessEvents()) {
         Remove();
         return kTRUE;
      } else {
         Reset();
         return kFALSE;
      }
   }
   return kFALSE;
}



ClassImp(TSystem)

TVirtualMutex* gSystemMutex = 0;

//______________________________________________________________________________
TSystem::TSystem(const char *name, const char *title) : TNamed(name, title), fAclicProperties(0)
{
   // Create a new OS interface.

   if (gSystem && name[0] != '-' && strcmp(name, "Generic"))
      Error("TSystem", "only one instance of TSystem allowed");

   fOnExitList          = 0;
   fSignalHandler       = 0;
   fFileHandler         = 0;
   fStdExceptionHandler = 0;
   fTimers              = 0;
   fCompiled            = 0;
   fHelpers             = 0;
   fInsideNotify        = kFALSE;
   fBeepDuration        = 0;
   fBeepFreq            = 0;
   fReadmask            = 0;
   fWritemask           = 0;
   fReadready           = 0;
   fWriteready          = 0;
   fSignals             = 0;
   fDone                = kFALSE;