[476] | 1 | // |
---|
| 2 | // ******************************************************************** |
---|
| 3 | // * License and Disclaimer * |
---|
| 4 | // * * |
---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
| 7 | // * conditions of the Geant4 Software License, included in the file * |
---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
| 9 | // * include a list of copyright holders. * |
---|
| 10 | // * * |
---|
| 11 | // * Neither the authors of this software system, nor their employing * |
---|
| 12 | // * institutes,nor the agencies providing financial support for this * |
---|
| 13 | // * work make any representation or warranty, express or implied, * |
---|
| 14 | // * regarding this software system or assume any liability for its * |
---|
| 15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
| 16 | // * for the full disclaimer and the limitation of liability. * |
---|
| 17 | // * * |
---|
| 18 | // * This code implementation is the result of the scientific and * |
---|
| 19 | // * technical work of the GEANT4 collaboration. * |
---|
| 20 | // * By using, copying, modifying or distributing the software (or * |
---|
| 21 | // * any work based on the software) you agree to acknowledge its * |
---|
| 22 | // * use in resulting scientific publications, and indicate your * |
---|
| 23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
| 24 | // ******************************************************************** |
---|
| 25 | // |
---|
| 26 | // |
---|
| 27 | // $Id: G4UIterminal.cc,v 1.21 2006/06/29 19:09:56 gunter Exp $ |
---|
[483] | 28 | // GEANT4 tag $Name: geant4-08-01-patch-01 $ |
---|
[476] | 29 | // |
---|
| 30 | |
---|
| 31 | #include "G4Types.hh" |
---|
| 32 | #include "G4StateManager.hh" |
---|
| 33 | #include "G4UIcommandTree.hh" |
---|
| 34 | #include "G4UIcommand.hh" |
---|
| 35 | #include "G4UIcommandStatus.hh" |
---|
| 36 | #include "G4UIterminal.hh" |
---|
| 37 | #include "G4UIcsh.hh" |
---|
| 38 | #include <sstream> |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | ////////////////////////////////////////////// |
---|
| 42 | G4UIterminal::G4UIterminal(G4VUIshell* aShell) |
---|
| 43 | ////////////////////////////////////////////// |
---|
| 44 | { |
---|
| 45 | UI= G4UImanager::GetUIpointer(); |
---|
| 46 | UI-> SetSession(this); |
---|
| 47 | UI-> SetCoutDestination(this); |
---|
| 48 | |
---|
| 49 | iExit= FALSE; |
---|
| 50 | iCont= FALSE; |
---|
| 51 | |
---|
| 52 | if(aShell) shell= aShell; |
---|
| 53 | else shell= new G4UIcsh; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | ///////////////////////////// |
---|
| 57 | G4UIterminal::~G4UIterminal() |
---|
| 58 | ///////////////////////////// |
---|
| 59 | { |
---|
| 60 | if(shell) delete shell; |
---|
| 61 | |
---|
| 62 | if(G4UImanager::GetUIpointer()) { |
---|
| 63 | UI-> SetSession(NULL); |
---|
| 64 | UI-> SetCoutDestination(NULL); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | //////////////////////////////////////////////////// |
---|
| 70 | void G4UIterminal::SetPrompt(const G4String& prompt) |
---|
| 71 | //////////////////////////////////////////////////// |
---|
| 72 | { |
---|
| 73 | shell-> SetPrompt(prompt); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | ///////////////////////////////////////// |
---|
| 77 | G4UIsession* G4UIterminal::SessionStart() |
---|
| 78 | ///////////////////////////////////////// |
---|
| 79 | { |
---|
| 80 | iExit= TRUE; |
---|
| 81 | |
---|
| 82 | G4String newCommand= GetCommand(); |
---|
| 83 | while(iExit){ |
---|
| 84 | ExecuteCommand(newCommand); |
---|
| 85 | newCommand= GetCommand(); |
---|
| 86 | } |
---|
| 87 | return NULL; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | ////////////////////////////////////////////////// |
---|
| 91 | void G4UIterminal::PauseSessionStart(G4String msg) |
---|
| 92 | ////////////////////////////////////////////////// |
---|
| 93 | { |
---|
| 94 | iCont= TRUE; |
---|
| 95 | |
---|
| 96 | G4String newCommand= GetCommand(msg); |
---|
| 97 | while(iCont){ |
---|
| 98 | ExecuteCommand(newCommand); |
---|
| 99 | newCommand= GetCommand(msg); |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | //////////////////////////////////////////////////// |
---|
| 104 | void G4UIterminal::ExecuteCommand(G4String aCommand) |
---|
| 105 | //////////////////////////////////////////////////// |
---|
| 106 | { |
---|
| 107 | if(aCommand.length()<2) return; |
---|
| 108 | |
---|
| 109 | G4int returnVal = UI-> ApplyCommand(aCommand); |
---|
| 110 | |
---|
| 111 | G4int paramIndex = returnVal % 100; |
---|
| 112 | // 0 - 98 : paramIndex-th parameter is invalid |
---|
| 113 | // 99 : convination of parameters is invalid |
---|
| 114 | G4int commandStatus = returnVal - paramIndex; |
---|
| 115 | |
---|
| 116 | G4UIcommand* cmd = 0; |
---|
| 117 | if(commandStatus!=fCommandSucceeded) |
---|
| 118 | { cmd = FindCommand(aCommand); } |
---|
| 119 | |
---|
| 120 | switch(commandStatus) { |
---|
| 121 | case fCommandSucceeded: |
---|
| 122 | break; |
---|
| 123 | case fCommandNotFound: |
---|
| 124 | G4cerr << "command <" << UI->SolveAlias(aCommand) << "> not found" << G4endl; |
---|
| 125 | if( aCommand.index("@@") != G4String::npos) { |
---|
| 126 | G4cout << "@@G4UIterminal" << G4endl; |
---|
| 127 | } |
---|
| 128 | break; |
---|
| 129 | case fIllegalApplicationState: |
---|
| 130 | G4cerr << "illegal application state -- command refused" << G4endl; |
---|
| 131 | break; |
---|
| 132 | case fParameterOutOfRange: |
---|
| 133 | // if(paramIndex<99) { |
---|
| 134 | // G4cerr << "Parameter is out of range (index " << paramIndex << ")" << G4endl; |
---|
| 135 | // G4cerr << "Allowed range : " << cmd->GetParameter(paramIndex)->GetParameterRange() << G4endl; |
---|
| 136 | // } else { |
---|
| 137 | // G4cerr << "Parameter is out of range" << G4endl; |
---|
| 138 | // G4cerr << "Allowed range : " << cmd->GetRange() << G4endl; |
---|
| 139 | // } |
---|
| 140 | break; |
---|
| 141 | case fParameterOutOfCandidates: |
---|
| 142 | G4cerr << "Parameter is out of candidate list (index " << paramIndex << ")" << G4endl; |
---|
| 143 | G4cerr << "Candidates : " << cmd->GetParameter(paramIndex)->GetParameterCandidates() << G4endl; |
---|
| 144 | break; |
---|
| 145 | case fParameterUnreadable: |
---|
| 146 | G4cerr << "Parameter is wrong type and/or is not omittable (index " << paramIndex << ")" << G4endl; |
---|
| 147 | break; |
---|
| 148 | case fAliasNotFound: |
---|
| 149 | default: |
---|
| 150 | G4cerr << "command refused (" << commandStatus << ")" << G4endl; |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /////////////////////////////////// |
---|
| 155 | G4String G4UIterminal::GetCommand(const char* msg) |
---|
| 156 | /////////////////////////////////// |
---|
| 157 | { |
---|
| 158 | G4String newCommand; |
---|
| 159 | G4String nullString; |
---|
| 160 | |
---|
| 161 | newCommand= shell-> GetCommandLine(msg); |
---|
| 162 | |
---|
| 163 | G4String nC= newCommand.strip(G4String::leading); |
---|
| 164 | if( nC.length() == 0 ) { |
---|
| 165 | newCommand= nullString; |
---|
| 166 | |
---|
| 167 | } else if( nC(0) == '#' ) { |
---|
| 168 | G4cout << nC << G4endl; |
---|
| 169 | newCommand= nullString; |
---|
| 170 | |
---|
| 171 | } else if(nC=="ls" || nC(0,3)=="ls " ) { // list commands |
---|
| 172 | ListDirectory(nC); |
---|
| 173 | newCommand= nullString; |
---|
| 174 | |
---|
| 175 | } else if(nC=="lc" || nC(0,3)=="lc " ) { // ... by shell |
---|
| 176 | shell-> ListCommand(nC.remove(0,2)); |
---|
| 177 | newCommand= nullString; |
---|
| 178 | |
---|
| 179 | } else if(nC == "pwd") { // show current directory |
---|
| 180 | G4cout << "Current Command Directory : " |
---|
| 181 | << GetCurrentWorkingDirectory() << G4endl; |
---|
| 182 | newCommand= nullString; |
---|
| 183 | |
---|
| 184 | } else if(nC == "cwd") { // ... by shell |
---|
| 185 | shell-> ShowCurrentDirectory(); |
---|
| 186 | newCommand= nullString; |
---|
| 187 | |
---|
| 188 | } else if(nC == "cd" || nC(0,3) == "cd ") { // "cd" |
---|
| 189 | ChangeDirectoryCommand(nC); |
---|
| 190 | shell-> SetCurrentDirectory(GetCurrentWorkingDirectory()); |
---|
| 191 | newCommand= nullString; |
---|
| 192 | |
---|
| 193 | } else if(nC == "help" || nC(0,5) == "help ") { // "help" |
---|
| 194 | TerminalHelp(nC); |
---|
| 195 | newCommand= nullString; |
---|
| 196 | |
---|
| 197 | } else if(nC(0) == '?') { // "show current value of a parameter" |
---|
| 198 | ShowCurrent(nC); |
---|
| 199 | newCommand= nullString; |
---|
| 200 | |
---|
| 201 | } else if(nC == "hist" || nC == "history") { // "hist/history" |
---|
| 202 | G4int nh= UI-> GetNumberOfHistory(); |
---|
| 203 | for (G4int i=0; i<nh; i++) { |
---|
| 204 | G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl; |
---|
| 205 | } |
---|
| 206 | newCommand= nullString; |
---|
| 207 | |
---|
| 208 | } else if(nC(0) == '!') { // "!" |
---|
| 209 | G4String ss= nC(1, nC.length()-1); |
---|
| 210 | G4int vl; |
---|
| 211 | const char* tt= ss; |
---|
| 212 | std::istringstream is(tt); |
---|
| 213 | is >> vl; |
---|
| 214 | G4int nh= UI-> GetNumberOfHistory(); |
---|
| 215 | if(vl>=0 && vl<nh) { |
---|
| 216 | newCommand= UI-> GetPreviousCommand(vl); |
---|
| 217 | G4cout << newCommand << G4endl; |
---|
| 218 | } else { |
---|
| 219 | G4cerr << "history " << vl << " is not found." << G4endl; |
---|
| 220 | newCommand= nullString; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | } else if(nC == "exit") { // "exit" |
---|
| 224 | if(iCont) { |
---|
| 225 | G4cout << "You are now processing RUN." << G4endl; |
---|
| 226 | G4cout << "Please abort it using \"/run/abort\" command first" << G4endl; |
---|
| 227 | G4cout << " and use \"continue\" command until the application" |
---|
| 228 | << G4endl; |
---|
| 229 | G4cout << " becomes to Idle." << G4endl; |
---|
| 230 | } else { |
---|
| 231 | iExit= FALSE; |
---|
| 232 | newCommand= nullString; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | } else if( nC == "cont" || nC == "continue"){ // "cont/continu" |
---|
| 236 | iCont= FALSE; |
---|
| 237 | newCommand= nullString; |
---|
| 238 | |
---|
| 239 | } else if( nC.empty() ){ // NULL command |
---|
| 240 | newCommand= nullString; |
---|
| 241 | |
---|
| 242 | } else { |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | return ModifyToFullPathCommand(newCommand); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | ////////////////////////////////////////////////////// |
---|
| 250 | G4int G4UIterminal::ReceiveG4cout(G4String coutString) |
---|
| 251 | ////////////////////////////////////////////////////// |
---|
| 252 | { |
---|
| 253 | std::cout << coutString << std::flush; |
---|
| 254 | return 0; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | ////////////////////////////////////////////////////// |
---|
| 258 | G4int G4UIterminal::ReceiveG4cerr(G4String cerrString) |
---|
| 259 | ////////////////////////////////////////////////////// |
---|
| 260 | { |
---|
| 261 | std::cerr << cerrString << std::flush; |
---|
| 262 | return 0; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | /////////////////////////////////////////////// |
---|
| 266 | G4bool G4UIterminal::GetHelpChoice(G4int& aInt) |
---|
| 267 | /////////////////////////////////////////////// |
---|
| 268 | { |
---|
| 269 | G4cin >> aInt; |
---|
| 270 | if(!G4cin.good()){ |
---|
| 271 | G4cin.clear(); |
---|
| 272 | G4cin.ignore(30,'\n'); |
---|
| 273 | return FALSE; |
---|
| 274 | } |
---|
| 275 | return TRUE; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | ///////////////////////////// |
---|
| 279 | void G4UIterminal::ExitHelp() |
---|
| 280 | ///////////////////////////// |
---|
| 281 | { |
---|
| 282 | char temp[100]; |
---|
| 283 | G4cin.getline(temp, 100); |
---|
| 284 | } |
---|
| 285 | |
---|