Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

# 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: G4UIterminal.cc,v 1.21 2006/06/29 19:09:56 gunter Exp $
    00028 // GEANT4 tag $Name: geant4-08-01-patch-01 $
    00029 //
    00030
    00031 #include "G4Types.hh"
    00032 #include "G4StateManager.hh"
    00033 #include "G4UIcommandTree.hh"
    00034 #include "G4UIcommand.hh"
    00035 #include "G4UIcommandStatus.hh"
    00036 #include "G4UIterminal.hh"
    00037 #include "G4UIcsh.hh"
    00038 #include <sstream>
    00039
    00040
    00042 G4UIterminal::G4UIterminal(G4VUIshell* aShell)
    00044 {
    00045   UI= G4UImanager::GetUIpointer();
    00046   UI-> SetSession(this);
    00047   UI-> SetCoutDestination(this);
    00048
    00049   iExit= FALSE;
    00050   iCont= FALSE;
    00051
    00052   if(aShell) shell= aShell;
    00053   else shell= new G4UIcsh;
    00054 }
    00055
    00057 G4UIterminal::~G4UIterminal()
    00058
    00059 {
    00060   if(shell) delete shell;
    00061
    00062   if(G4UImanager::GetUIpointer()) {
    00063     UI-> SetSession(NULL);
    00064     UI-> SetCoutDestination(NULL);
    00065   }
    00066 }
    00067
    00068
    00070 void G4UIterminal::SetPrompt(const G4String& prompt)
    00071
    00072 {
    00073   shell-> SetPrompt(prompt);
    00074 }
    00075
    00077 G4UIsession* G4UIterminal::SessionStart()
    00079 {
    00080   iExit= TRUE;
    00081
    00082   G4String newCommand= GetCommand();
    00083   while(iExit){
    00084     ExecuteCommand(newCommand);
    00085     newCommand= GetCommand();
    00086   }
    00087   return NULL;
    00088 }
    00089
    00091 void G4UIterminal::PauseSessionStart(G4String msg)
    00093 {
    00094   iCont= TRUE;
    00095
    00096   G4String newCommand= GetCommand(msg);
    00097   while(iCont){
    00098     ExecuteCommand(newCommand);
    00099     newCommand= GetCommand(msg);
    00100   }
    00101 }
    00102
    00104 void G4UIterminal::ExecuteCommand(G4String aCommand)
    00106 {
    00107   if(aCommand.length()<2) return;
    00108
    00109   G4int returnVal = UI-> ApplyCommand(aCommand);
    00110
    00111   G4int paramIndex = returnVal % 100;
    00112   // 0 - 98 : paramIndex-th parameter is invalid
    00113   // 99     : convination of parameters is invalid
    00114   G4int commandStatus = returnVal - paramIndex;
    00115
    00116   G4UIcommand* cmd = 0;
    00117   if(commandStatus!=fCommandSucceeded)
    00118   { cmd = FindCommand(aCommand); }
    00119
    00120   switch(commandStatus) {
    00121   case fCommandSucceeded:
    00122     break;
    00123   case fCommandNotFound:
    00124     G4cerr << "command <" << UI->SolveAlias(aCommand) << "> not found" << G4endl;
    00125     if( aCommand.index("@@") != G4String::npos) {
    00126       G4cout << "@@G4UIterminal" << G4endl;
    00127     }
    00128     break;
    00129   case fIllegalApplicationState:
    00130     G4cerr << "illegal application state -- command refused" << G4endl;
    00131     break;
    00132   case fParameterOutOfRange:
    00133     // if(paramIndex<99) {
    00134     //   G4cerr << "Parameter is out of range (index " << paramIndex << ")" << G4endl;
    00135     //   G4cerr << "Allowed range : " << cmd->GetParameter(paramIndex)->GetParameterRange() << G4endl;
    00136     // } else {
    00137     //   G4cerr << "Parameter is out of range" << G4endl;
    00138     //   G4cerr << "Allowed range : " << cmd->GetRange() << G4endl;
    00139     // }
    00140     break;
    00141   case fParameterOutOfCandidates:
    00142     G4cerr << "Parameter is out of candidate list (index " << paramIndex << ")" << G4endl;
    00143     G4cerr << "Candidates : " << cmd->GetParameter(paramIndex)->GetParameterCandidates() << G4endl;
    00144     break;
    00145   case fParameterUnreadable:
    00146     G4cerr << "Parameter is wrong type and/or is not omittable (index " << paramIndex << ")" << G4endl;
    00147     break;
    00148   case fAliasNotFound:
    00149   default:
    00150     G4cerr << "command refused (" << commandStatus << ")" << G4endl;
    00151   }
    00152 }
    00153
    00155 G4String G4UIterminal::GetCommand(const char* msg)
    00157 {
    00158   G4String newCommand;
    00159   G4String nullString;
    00160
    00161   newCommand= shell-> GetCommandLine(msg);
    00162
    00163   G4String nC= newCommand.strip(G4String::leading);
    00164   if( nC.length() == 0 ) {
    00165     newCommand= nullString;
    00166
    00167   } else if( nC(0) == '#' ) {
    00168     G4cout << nC << G4endl;
    00169     newCommand= nullString;
    00170
    00171   } else if(nC=="ls" || nC(0,3)=="ls " ) {  // list commands
    00172     ListDirectory(nC);
    00173     newCommand= nullString;
    00174
    00175   } else if(nC=="lc" || nC(0,3)=="lc " ) {  // ... by shell
    00176     shell-> ListCommand(nC.remove(0,2));
    00177     newCommand= nullString;
    00178
    00179   } else if(nC == "pwd") { // show current directory
    00180     G4cout << "Current Command Directory : "
    00181            << GetCurrentWorkingDirectory() << G4endl;
    00182     newCommand= nullString;
    00183
    00184   } else if(nC == "cwd") { // ... by shell
    00185     shell-> ShowCurrentDirectory();
    00186     newCommand= nullString;
    00187
    00188   } else if(nC == "cd" || nC(0,3) == "cd ") {  // "cd"
    00189     ChangeDirectoryCommand(nC);
    00190     shell-> SetCurrentDirectory(GetCurrentWorkingDirectory());
    00191     newCommand= nullString;
    00192
    00193   } else if(nC == "help" || nC(0,5) == "help ") {  // "help"
    00194     TerminalHelp(nC);
    00195     newCommand= nullString;
    00196
    00197   } else if(nC(0) == '?') {   // "show current value of a parameter"
    00198     ShowCurrent(nC);
    00199     newCommand= nullString;
    00200
    00201   } else if(nC == "hist" || nC == "history") {     // "hist/history"
    00202     G4int nh= UI-> GetNumberOfHistory();
    00203     for (G4int i=0; i<nh; i++) {
    00204       G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl;
    00205     }
    00206     newCommand= nullString;
    00207
    00208   } else if(nC(0) == '!') {   // "!"
    00209     G4String ss= nC(1, nC.length()-1);
    00210     G4int vl;
    00211     const char* tt= ss;
    00212     std::istringstream is(tt);
    00213     is >> vl;
    00214     G4int nh= UI-> GetNumberOfHistory();
    00215     if(vl>=0 && vl<nh) {
    00216       newCommand= UI-> GetPreviousCommand(vl);
    00217       G4cout << newCommand << G4endl;
    00218     } else {
    00219       G4cerr << "history " << vl << " is not found." << G4endl;
    00220       newCommand= nullString;
    00221     }
    00222
    00223   } else if(nC == "exit") {   // "exit"
    00224     if(iCont) {
    00225       G4cout << "You are now processing RUN." << G4endl;
    00226       G4cout << "Please abort it using \"/run/abort\" command first" << G4endl;
    00227       G4cout << " and use \"continue\" command until the application"
    00228              << G4endl;
    00229       G4cout << " becomes to Idle." << G4endl;
    00230     } else {
    00231       iExit= FALSE;
    00232       newCommand= nullString;
    00233     }
    00234
    00235   } else if( nC == "cont" || nC == "continue"){     // "cont/continu"
    00236     iCont= FALSE;
    00237     newCommand= nullString;
    00238
    00239   } else if( nC.empty() ){ // NULL command
    00240     newCommand= nullString;
    00241
    00242   } else {
    00243   }
    00244
    00245   return ModifyToFullPathCommand(newCommand);
    00246 }
    00247
    00248
    00250 G4int G4UIterminal::ReceiveG4cout(G4String coutString)
    00252 {
    00253   std::cout << coutString << std::flush;
    00254   return 0;
    00255 }
    00256
    00258 G4int G4UIterminal::ReceiveG4cerr(G4String cerrString)
    00260 {
    00261   std::cerr << cerrString << std::flush;
    00262   return 0;
    00263 }
    00264
    00266 G4bool G4UIterminal::GetHelpChoice(G4int& aInt)
    00268 {
    00269   G4cin >> aInt;
    00270   if(!G4cin.good()){
    00271     G4cin.clear();
    00272     G4cin.ignore(30,'\n');
    00273     return FALSE;
    00274   }
    00275   return TRUE;
    00276 }
    00277
    00279 void G4UIterminal::ExitHelp()
    00281 {
    00282   char temp[100];
    00283   G4cin.getline(temp, 100);
    00284 }
    00285