G4UIterminal.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // ====================================================================
00030 //   G4UIterminal.cc
00031 //
00032 // ====================================================================
00033 #include "G4Types.hh"
00034 #include "G4StateManager.hh"
00035 #include "G4UIcommandTree.hh"
00036 #include "G4UIcommand.hh"
00037 #include "G4UIcommandStatus.hh"
00038 #include "G4UIterminal.hh"
00039 #include "G4UIcsh.hh"
00040 #include <sstream>
00041 
00042 #ifndef WIN32
00043 #include <signal.h>
00044 #endif
00045 
00046 // ====================================================================
00047 // signal handler for soft-abort
00048 // ====================================================================
00049 
00050 static G4VUIshell* theshell= 0;
00051 
00052 #ifndef WIN32
00053 
00054 extern "C" {
00055 
00057 static void SignalHandler(G4int)
00059 {
00060   G4StateManager* stateManager= G4StateManager::GetStateManager();
00061   G4ApplicationState state= stateManager-> GetCurrentState();
00062 
00063   if(state==G4State_GeomClosed || state==G4State_EventProc) {
00064     G4cout << "aborting Run ...";
00065     G4UImanager::GetUIpointer()->ApplyCommand("/run/abort");
00066     G4cout << G4endl;
00067   } else {
00068     G4cout << G4endl
00069            << "Session terminated." << G4endl;
00070     theshell-> ResetTerminal();
00071     G4Exception("G4UIterminal::SignalHandler()",
00072                 "UI0001",
00073                 FatalException, 
00074                 "KeyboardInterrput with Ctrl-C");
00075   }
00076 
00077   // for original Unix / System V
00078   signal(SIGINT, SignalHandler);
00079 }
00080 
00081 }
00082 #endif
00083 
00084 // ====================================================================
00085 //
00086 // class description
00087 //
00088 // ====================================================================
00089 
00091 G4UIterminal::G4UIterminal(G4VUIshell* aShell, G4bool qsig)
00093 {
00094   UI= G4UImanager::GetUIpointer();
00095   UI-> SetSession(this);
00096   UI-> SetCoutDestination(this);
00097 
00098   iExit= FALSE;
00099   iCont= FALSE;
00100 
00101   if(aShell) shell= aShell;
00102   else shell= new G4UIcsh;
00103   theshell= shell; // locally stored for the signal handler
00104 
00105   // add signal handler
00106   if(qsig) {
00107 #ifndef WIN32
00108   signal(SIGINT, SignalHandler);
00109 #endif
00110   }
00111 }
00112 
00114 G4UIterminal::~G4UIterminal() 
00115 
00116 { 
00117   if(shell) delete shell;
00118 
00119   if(G4UImanager::GetUIpointer()) {
00120     UI-> SetSession(NULL);
00121     UI-> SetCoutDestination(NULL);
00122   }
00123 }
00124 
00125 
00127 void G4UIterminal::SetPrompt(const G4String& prompt) 
00128 
00129 {
00130   shell-> SetPrompt(prompt);
00131 }
00132 
00134 G4UIsession* G4UIterminal::SessionStart()
00136 {
00137   iExit= TRUE;
00138 
00139   G4String newCommand= GetCommand();
00140   while(iExit){
00141     ExecuteCommand(newCommand);
00142     newCommand= GetCommand();
00143   }
00144   return NULL;
00145 }
00146 
00148 void G4UIterminal::PauseSessionStart(const G4String& msg)
00150 {
00151   iCont= TRUE;
00152 
00153   G4String newCommand= GetCommand(msg);
00154   while(iCont){
00155     ExecuteCommand(newCommand);
00156     newCommand= GetCommand(msg);
00157   }
00158 }
00159 
00161 void G4UIterminal::ExecuteCommand(const G4String& aCommand)
00163 {
00164   if(aCommand.length()<2) return;
00165 
00166   G4int returnVal = UI-> ApplyCommand(aCommand);
00167 
00168   G4int paramIndex = returnVal % 100;
00169   // 0 - 98 : paramIndex-th parameter is invalid
00170   // 99     : convination of parameters is invalid
00171   G4int commandStatus = returnVal - paramIndex;
00172 
00173   G4UIcommand* cmd = 0;
00174   if(commandStatus!=fCommandSucceeded)
00175   { cmd = FindCommand(aCommand); }
00176 
00177   switch(commandStatus) {
00178   case fCommandSucceeded:
00179     break;
00180   case fCommandNotFound:
00181     G4cerr << "command <" << UI->SolveAlias(aCommand) << "> not found" << G4endl;
00182     if( aCommand.index("@@") != G4String::npos) {
00183       G4cout << "@@G4UIterminal" << G4endl;
00184     }
00185     break;
00186   case fIllegalApplicationState:
00187     G4cerr << "illegal application state -- command refused" << G4endl;
00188     break;
00189   case fParameterOutOfRange:
00190     // if(paramIndex<99) {
00191     //   G4cerr << "Parameter is out of range (index " << paramIndex << ")" << G4endl;
00192     //   G4cerr << "Allowed range : " << cmd->GetParameter(paramIndex)->GetParameterRange() << G4endl;
00193     // } else {
00194     //   G4cerr << "Parameter is out of range" << G4endl;
00195     //   G4cerr << "Allowed range : " << cmd->GetRange() << G4endl;
00196     // }
00197     break;
00198   case fParameterOutOfCandidates:
00199     G4cerr << "Parameter is out of candidate list (index " << paramIndex << ")" << G4endl;
00200     G4cerr << "Candidates : " << cmd->GetParameter(paramIndex)->GetParameterCandidates() << G4endl;
00201     break;
00202   case fParameterUnreadable:
00203     G4cerr << "Parameter is wrong type and/or is not omittable (index " << paramIndex << ")" << G4endl;
00204     break;
00205   case fAliasNotFound:
00206   default:
00207     G4cerr << "command refused (" << commandStatus << ")" << G4endl;
00208   }
00209 }
00210 
00212 G4String G4UIterminal::GetCommand(const char* msg)
00214 {
00215   G4String newCommand;
00216   G4String nullString;
00217 
00218   newCommand= shell-> GetCommandLineString(msg);
00219 
00220   G4String nC = newCommand.strip(G4String::leading);
00221   if( nC.length() == 0 ) {
00222     newCommand= nullString;
00223 
00224   } else if( nC(0) == '#' ) {  
00225     G4cout << nC << G4endl;
00226     newCommand= nullString;
00227 
00228   } else if(nC=="ls" || nC(0,3)=="ls " ) {  // list commands
00229     ListDirectory(nC); 
00230     newCommand= nullString;
00231 
00232   } else if(nC=="lc" || nC(0,3)=="lc " ) {  // ... by shell
00233     shell-> ListCommand(nC.remove(0,2)); 
00234     newCommand= nullString;
00235 
00236   } else if(nC == "pwd") { // show current directory
00237     G4cout << "Current Command Directory : " 
00238            << GetCurrentWorkingDirectory() << G4endl; 
00239     newCommand= nullString;
00240 
00241   } else if(nC == "cwd") { // ... by shell
00242     shell-> ShowCurrentDirectory();
00243     newCommand= nullString;
00244 
00245   } else if(nC == "cd" || nC(0,3) == "cd ") {  // "cd"
00246     ChangeDirectoryCommand(nC); 
00247     shell-> SetCurrentDirectory(GetCurrentWorkingDirectory());
00248     newCommand= nullString;
00249 
00250   } else if(nC == "help" || nC(0,5) == "help ") {  // "help"
00251     TerminalHelp(nC);
00252     newCommand= nullString;
00253 
00254   } else if(nC(0) == '?') {   // "show current value of a parameter"
00255     ShowCurrent(nC);
00256     newCommand= nullString;
00257 
00258   } else if(nC == "hist" || nC == "history") {     // "hist/history"
00259     G4int nh= UI-> GetNumberOfHistory();
00260     for (G4int i=0; i<nh; i++) { 
00261       G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl; 
00262     }
00263     newCommand= nullString;
00264 
00265   } else if(nC(0) == '!') {   // "!"
00266     G4String ss= nC(1, nC.length()-1);
00267     G4int vl;
00268     const char* tt= ss;
00269     std::istringstream is(tt);
00270     is >> vl;
00271     G4int nh= UI-> GetNumberOfHistory();
00272     if(vl>=0 && vl<nh) { 
00273       newCommand= UI-> GetPreviousCommand(vl); 
00274       G4cout << newCommand << G4endl;
00275     } else { 
00276       G4cerr << "history " << vl << " is not found." << G4endl; 
00277       newCommand= nullString;
00278     }
00279 
00280   } else if(nC == "exit") {   // "exit"
00281     if(iCont) { 
00282       G4cout << "You are now processing RUN." << G4endl;
00283       G4cout << "Please abort it using \"/run/abort\" command first" << G4endl;
00284       G4cout << " and use \"continue\" command until the application" 
00285              << G4endl;
00286       G4cout << " becomes to Idle." << G4endl;
00287     } else {
00288       iExit= FALSE;
00289       newCommand= nullString;
00290     }
00291 
00292   } else if( nC == "cont" || nC == "continue"){     // "cont/continu"
00293     iCont= FALSE;
00294     newCommand= nullString;
00295 
00296   } else if( nC.empty() ){ // NULL command
00297     newCommand= nullString;
00298     
00299   } else {
00300   }
00301 
00302   return ModifyToFullPathCommand(newCommand);
00303 }
00304 
00305 
00307 G4int G4UIterminal::ReceiveG4cout(const G4String& coutString)
00309 {
00310   std::cout << coutString << std::flush;
00311   return 0;
00312 }
00313 
00315 G4int G4UIterminal::ReceiveG4cerr(const G4String& cerrString)
00317 {
00318   std::cerr << cerrString << std::flush;
00319   return 0;
00320 }
00321 
00323 G4bool G4UIterminal::GetHelpChoice(G4int& aInt)
00325 {
00326   G4cin >> aInt;
00327   if(!G4cin.good()){
00328     G4cin.clear();
00329     G4cin.ignore(30,'\n');
00330     return FALSE;
00331   }
00332   return TRUE;
00333 }
00334 
00336 void G4UIterminal::ExitHelp() const
00338 {
00339   char temp[100];
00340   G4cin.getline(temp, 100);
00341 }
00342 

Generated on Mon May 27 17:50:06 2013 for Geant4 by  doxygen 1.4.7