| 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: G4VBasicShell.cc,v 1.14 2009/11/17 17:41:33 lgarnier Exp $
|
|---|
| 28 | // GEANT4 tag $Name: $
|
|---|
| 29 | //
|
|---|
| 30 |
|
|---|
| 31 | #include "G4VBasicShell.hh"
|
|---|
| 32 | #include "G4StateManager.hh"
|
|---|
| 33 | #include "G4UIcommandTree.hh"
|
|---|
| 34 | #include "G4UIcommand.hh"
|
|---|
| 35 | #include "G4UIcommandStatus.hh"
|
|---|
| 36 | #include "G4UImanager.hh"
|
|---|
| 37 | #include <vector>
|
|---|
| 38 | #include <sstream>
|
|---|
| 39 |
|
|---|
| 40 | G4VBasicShell::G4VBasicShell()
|
|---|
| 41 | :currentDirectory("/")
|
|---|
| 42 | {
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | G4VBasicShell::~G4VBasicShell()
|
|---|
| 46 | {;}
|
|---|
| 47 |
|
|---|
| 48 | G4String G4VBasicShell::ModifyToFullPathCommand(const char* aCommandLine)
|
|---|
| 49 | {
|
|---|
| 50 | G4String rawCommandLine = (G4String)aCommandLine;
|
|---|
| 51 | if(rawCommandLine.isNull()||rawCommandLine(0)=='\0') return rawCommandLine;
|
|---|
| 52 | G4String commandLine = (G4String)rawCommandLine.strip(G4String::both);
|
|---|
| 53 | G4String commandString;
|
|---|
| 54 | G4String parameterString;
|
|---|
| 55 | size_t i = commandLine.index(" ");
|
|---|
| 56 | if( i != std::string::npos )
|
|---|
| 57 | {
|
|---|
| 58 | commandString = (G4String)commandLine(0,i);
|
|---|
| 59 | parameterString = " ";
|
|---|
| 60 | parameterString += (G4String)commandLine(i+1,commandLine.length()-(i+1));
|
|---|
| 61 | }
|
|---|
| 62 | else
|
|---|
| 63 | { commandString = commandLine; }
|
|---|
| 64 |
|
|---|
| 65 | G4String fullPathCommandLine
|
|---|
| 66 | = ModifyPath( commandString )+parameterString;
|
|---|
| 67 | return fullPathCommandLine;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | G4String G4VBasicShell::GetCurrentWorkingDirectory()
|
|---|
| 71 | {
|
|---|
| 72 | return currentDirectory;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | G4bool G4VBasicShell::ChangeDirectory(const char* newDir)
|
|---|
| 76 | {
|
|---|
| 77 | G4String aNewPrefix = (G4String)newDir;
|
|---|
| 78 | G4String newPrefix = (G4String)aNewPrefix.strip(G4String::both);
|
|---|
| 79 | G4String newDirectory = ModifyPath( newPrefix );
|
|---|
| 80 | if( newDirectory( newDirectory.length() - 1 ) != '/' )
|
|---|
| 81 | { newDirectory += "/"; }
|
|---|
| 82 | if( FindDirectory( (const char*)newDirectory ) == NULL )
|
|---|
| 83 | { return false; }
|
|---|
| 84 | currentDirectory = newDirectory;
|
|---|
| 85 | return true;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | G4UIcommandTree* G4VBasicShell::FindDirectory(const char* dirName)
|
|---|
| 89 | {
|
|---|
| 90 | G4String aDirName = (G4String)dirName;
|
|---|
| 91 | G4String theDir = (G4String)aDirName.strip(G4String::both);
|
|---|
| 92 | G4String targetDir = ModifyPath( theDir );
|
|---|
| 93 | if( targetDir( targetDir.length()-1 ) != '/' )
|
|---|
| 94 | { targetDir += "/"; }
|
|---|
| 95 | G4UIcommandTree* comTree = G4UImanager::GetUIpointer()->GetTree();
|
|---|
| 96 | if( targetDir == "/" )
|
|---|
| 97 | { return comTree; }
|
|---|
| 98 | size_t idx = 1;
|
|---|
| 99 | while( idx < targetDir.length()-1 )
|
|---|
| 100 | {
|
|---|
| 101 | size_t i = targetDir.index("/",idx);
|
|---|
| 102 | comTree = comTree->GetTree((G4String)targetDir(0,i+1));
|
|---|
| 103 | if( comTree == NULL )
|
|---|
| 104 | { return NULL; }
|
|---|
| 105 | idx = i+1;
|
|---|
| 106 | }
|
|---|
| 107 | return comTree;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | G4UIcommand* G4VBasicShell::FindCommand(const char* commandName)
|
|---|
| 111 | {
|
|---|
| 112 | G4String rawCommandLine = (G4String)commandName;
|
|---|
| 113 | G4String commandLine = (G4String)rawCommandLine.strip(G4String::both);
|
|---|
| 114 | G4String commandString;
|
|---|
| 115 | size_t i = commandLine.index(" ");
|
|---|
| 116 | if( i != std::string::npos )
|
|---|
| 117 | { commandString = (G4String)commandLine(0,i); }
|
|---|
| 118 | else
|
|---|
| 119 | { commandString = commandLine; }
|
|---|
| 120 |
|
|---|
| 121 | G4String targetCom = ModifyPath(commandString);
|
|---|
| 122 | return G4UImanager::GetUIpointer()->GetTree()->FindPath(targetCom);
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | G4String G4VBasicShell::ModifyPath(G4String tempPath)
|
|---|
| 126 | {
|
|---|
| 127 | G4String newPath = currentDirectory;
|
|---|
| 128 |
|
|---|
| 129 | if( tempPath.length()>0 )
|
|---|
| 130 | {
|
|---|
| 131 |
|
|---|
| 132 | if( tempPath(0) == '/' ) // full path is given
|
|---|
| 133 | { newPath = tempPath; }
|
|---|
| 134 | else if( tempPath(0) != '.' ) // add current prefix
|
|---|
| 135 | { newPath += tempPath; }
|
|---|
| 136 | else if( tempPath(0,2) == "./" ) // add current prefix
|
|---|
| 137 | { newPath += (G4String)tempPath(2,tempPath.length()-2); }
|
|---|
| 138 | else // swim up with ".."
|
|---|
| 139 | {
|
|---|
| 140 | while( 1 )
|
|---|
| 141 | {
|
|---|
| 142 | if( tempPath(0,2) == ".." )
|
|---|
| 143 | {
|
|---|
| 144 | if( newPath != "/" )
|
|---|
| 145 | {
|
|---|
| 146 | G4String tmpString = (G4String)newPath(0,newPath.length()-1);
|
|---|
| 147 | newPath = (G4String)newPath(0,tmpString.last('/')+1);
|
|---|
| 148 | }
|
|---|
| 149 | if( tempPath == ".." || tempPath == "../" )
|
|---|
| 150 | { break; }
|
|---|
| 151 | tempPath = (G4String)tempPath(3,tempPath.length()-3);
|
|---|
| 152 | }
|
|---|
| 153 | else
|
|---|
| 154 | {
|
|---|
| 155 | newPath += tempPath;
|
|---|
| 156 | break;
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | return newPath;
|
|---|
| 164 | }
|
|---|
| 165 | ////////////////////////////////////////////
|
|---|
| 166 | // Method used for command completion //////
|
|---|
| 167 | ////////////////////////////////////////////
|
|---|
| 168 | G4String G4VBasicShell::Complete(G4String commandName)
|
|---|
| 169 | {
|
|---|
| 170 | G4String rawCommandLine = commandName;
|
|---|
| 171 | G4String commandLine = rawCommandLine.strip(G4String::both);
|
|---|
| 172 | size_t i = commandLine.index(" ");
|
|---|
| 173 | if( i != std::string::npos ) return rawCommandLine; // Already entering parameters,
|
|---|
| 174 | // assume command path is correct.
|
|---|
| 175 | G4String commandString = commandLine;
|
|---|
| 176 | G4String targetCom = ModifyPath(commandString);
|
|---|
| 177 | G4UIcommandTree* tree = G4UImanager::GetUIpointer()->GetTree();
|
|---|
| 178 | G4String value = FindMatchingPath(tree,targetCom);
|
|---|
| 179 | if(value=="") return rawCommandLine;
|
|---|
| 180 | return value;
|
|---|
| 181 | }
|
|---|
| 182 | G4String G4VBasicShell::FindMatchingPath(
|
|---|
| 183 | G4UIcommandTree* aTree
|
|---|
| 184 | ,G4String aCommandPath
|
|---|
| 185 | )
|
|---|
| 186 | {
|
|---|
| 187 | return aTree->CompleteCommandPath(aCommandPath);
|
|---|
| 188 | }
|
|---|
| 189 | ////////////////////////////////////////////
|
|---|
| 190 | // Method involving an interactive G4cout //
|
|---|
| 191 | ////////////////////////////////////////////
|
|---|
| 192 | /***************************************************************************/
|
|---|
| 193 | void G4VBasicShell::ExecuteCommand (
|
|---|
| 194 | G4String aCommand
|
|---|
| 195 | )
|
|---|
| 196 | /***************************************************************************/
|
|---|
| 197 | // Should be put in G4VBasicShell.
|
|---|
| 198 | /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
|
|---|
| 199 | {
|
|---|
| 200 | if(aCommand.length()<2) return;
|
|---|
| 201 | G4UImanager* UI = G4UImanager::GetUIpointer();
|
|---|
| 202 | if(UI==NULL) return;
|
|---|
| 203 | G4int commandStatus = UI->ApplyCommand(aCommand);
|
|---|
| 204 | switch(commandStatus) {
|
|---|
| 205 | case fCommandSucceeded:
|
|---|
| 206 | break;
|
|---|
| 207 | case fCommandNotFound:
|
|---|
| 208 | G4cerr << "command not found" << G4endl;
|
|---|
| 209 | break;
|
|---|
| 210 | case fIllegalApplicationState:
|
|---|
| 211 | G4cerr << "illegal application state -- command refused" << G4endl;
|
|---|
| 212 | break;
|
|---|
| 213 | case fParameterOutOfRange:
|
|---|
| 214 | case fParameterUnreadable:
|
|---|
| 215 | case fParameterOutOfCandidates:
|
|---|
| 216 | default:
|
|---|
| 217 | G4cerr << "command refused (" << commandStatus << ")" << G4endl;
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 | /***************************************************************************/
|
|---|
| 221 | void G4VBasicShell::ApplyShellCommand (
|
|---|
| 222 | G4String a_string
|
|---|
| 223 | ,G4bool& exitSession
|
|---|
| 224 | ,G4bool& exitPause
|
|---|
| 225 | )
|
|---|
| 226 | /***************************************************************************/
|
|---|
| 227 | /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
|
|---|
| 228 | {
|
|---|
| 229 | G4UImanager* UI = G4UImanager::GetUIpointer();
|
|---|
| 230 | if(UI==NULL) return;
|
|---|
| 231 |
|
|---|
| 232 | G4String command = a_string.strip(G4String::leading);
|
|---|
| 233 | if( command(0) == '#' ) {
|
|---|
| 234 |
|
|---|
| 235 | G4cout << command << G4endl;
|
|---|
| 236 |
|
|---|
| 237 | } else if( command == "ls" || command(0,3) == "ls " ) {
|
|---|
| 238 |
|
|---|
| 239 | ListDirectory( command );
|
|---|
| 240 |
|
|---|
| 241 | } else if( command == "pwd" ) {
|
|---|
| 242 |
|
|---|
| 243 | G4cout << "Current Working Directory : "
|
|---|
| 244 | << GetCurrentWorkingDirectory() << G4endl;
|
|---|
| 245 |
|
|---|
| 246 | } else if( command == "cd" || command(0,3) == "cd ") {
|
|---|
| 247 |
|
|---|
| 248 | ChangeDirectoryCommand ( command );
|
|---|
| 249 |
|
|---|
| 250 | } else if( command == "help" || command(0,5) == "help ") {
|
|---|
| 251 |
|
|---|
| 252 | TerminalHelp( command );
|
|---|
| 253 |
|
|---|
| 254 | } else if( command(0) == '?' ) {
|
|---|
| 255 |
|
|---|
| 256 | ShowCurrent( command );
|
|---|
| 257 |
|
|---|
| 258 | } else if( command == "hist" || command == "history") {
|
|---|
| 259 |
|
|---|
| 260 | G4int nh = UI->GetNumberOfHistory();
|
|---|
| 261 | for(G4int i=0;i<nh;i++) {
|
|---|
| 262 | G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | } else if( command(0) == '!' ) {
|
|---|
| 266 |
|
|---|
| 267 | G4String ss = command(1,command.length()-1);
|
|---|
| 268 | G4int vl;
|
|---|
| 269 | const char* tt = ss;
|
|---|
| 270 | std::istringstream is(tt);
|
|---|
| 271 | is >> vl;
|
|---|
| 272 | G4int nh = UI->GetNumberOfHistory();
|
|---|
| 273 | if(vl>=0 && vl<nh) {
|
|---|
| 274 | G4String prev = UI->GetPreviousCommand(vl);
|
|---|
| 275 | G4cout << prev << G4endl;
|
|---|
| 276 | ExecuteCommand (ModifyToFullPathCommand(prev));
|
|---|
| 277 | } else {
|
|---|
| 278 | G4cerr << "history " << vl << " is not found." << G4endl;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | } else if( command == "exit" ) {
|
|---|
| 282 |
|
|---|
| 283 | if( exitPause == false) { //In a secondary loop.
|
|---|
| 284 | G4cout << "You are now processing RUN." << G4endl;
|
|---|
| 285 | G4cout << "Please abort it using \"/run/abort\" command first" << G4endl;
|
|---|
| 286 | G4cout << " and use \"continue\" command until the application" << G4endl;
|
|---|
| 287 | G4cout << " becomes to Idle." << G4endl;
|
|---|
| 288 | } else {
|
|---|
| 289 | exitSession = true;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | } else if( command == "cont" || command == "continue"){
|
|---|
| 293 |
|
|---|
| 294 | exitPause = true;
|
|---|
| 295 |
|
|---|
| 296 | } else {
|
|---|
| 297 |
|
|---|
| 298 | ExecuteCommand (ModifyToFullPathCommand(a_string));
|
|---|
| 299 |
|
|---|
| 300 | }
|
|---|
| 301 | }
|
|---|
| 302 | void G4VBasicShell::ShowCurrent(G4String newCommand)
|
|---|
| 303 | {
|
|---|
| 304 | G4UImanager* UI = G4UImanager::GetUIpointer();
|
|---|
| 305 | if(UI==NULL) return;
|
|---|
| 306 | G4String comString = newCommand(1,newCommand.length()-1);
|
|---|
| 307 | G4String theCommand = ModifyToFullPathCommand(comString);
|
|---|
| 308 | G4String curV = UI->GetCurrentValues(theCommand);
|
|---|
| 309 | if( ! curV.isNull() ) {
|
|---|
| 310 | G4cout << "Current value(s) of the parameter(s) : " << curV << G4endl;
|
|---|
| 311 | }
|
|---|
| 312 | }
|
|---|
| 313 | void G4VBasicShell::ChangeDirectoryCommand(G4String newCommand)
|
|---|
| 314 | {
|
|---|
| 315 | G4String prefix;
|
|---|
| 316 | if( newCommand.length() <= 3 ) {
|
|---|
| 317 | prefix = "/";
|
|---|
| 318 | } else {
|
|---|
| 319 | G4String aNewPrefix = newCommand(3,newCommand.length()-3);
|
|---|
| 320 | prefix = aNewPrefix.strip(G4String::both);
|
|---|
| 321 | }
|
|---|
| 322 | if(!ChangeDirectory(prefix)) {
|
|---|
| 323 | G4cout << "directory <" << prefix << "> not found." << G4endl;
|
|---|
| 324 | }
|
|---|
| 325 | }
|
|---|
| 326 | void G4VBasicShell::ListDirectory(G4String newCommand)
|
|---|
| 327 | {
|
|---|
| 328 | G4String targetDir;
|
|---|
| 329 | if( newCommand.length() <= 3 ) {
|
|---|
| 330 | targetDir = GetCurrentWorkingDirectory();
|
|---|
| 331 | } else {
|
|---|
| 332 | G4String newPrefix = newCommand(3,newCommand.length()-3);
|
|---|
| 333 | targetDir = newPrefix.strip(G4String::both);
|
|---|
| 334 | }
|
|---|
| 335 | G4UIcommandTree* commandTree = FindDirectory( targetDir );
|
|---|
| 336 | if( commandTree == NULL ) {
|
|---|
| 337 | G4cout << "Directory <" << targetDir << "> is not found." << G4endl;
|
|---|
| 338 | } else {
|
|---|
| 339 | commandTree->ListCurrent();
|
|---|
| 340 | }
|
|---|
| 341 | }
|
|---|
| 342 | void G4VBasicShell::TerminalHelp(G4String newCommand)
|
|---|
| 343 | {
|
|---|
| 344 | G4UImanager* UI = G4UImanager::GetUIpointer();
|
|---|
| 345 | if(UI==NULL) return;
|
|---|
| 346 | G4UIcommandTree * treeTop = UI->GetTree();
|
|---|
| 347 | size_t i = newCommand.index(" ");
|
|---|
| 348 | if( i != std::string::npos )
|
|---|
| 349 | {
|
|---|
| 350 | G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
|
|---|
| 351 | newValue.strip(G4String::both);
|
|---|
| 352 | G4String targetCom = ModifyToFullPathCommand( newValue );
|
|---|
| 353 | G4UIcommand* theCommand = treeTop->FindPath( targetCom );
|
|---|
| 354 | if( theCommand != NULL )
|
|---|
| 355 | {
|
|---|
| 356 | theCommand->List();
|
|---|
| 357 | return;
|
|---|
| 358 | }
|
|---|
| 359 | else
|
|---|
| 360 | {
|
|---|
| 361 | G4cout << "Command <" << newValue << " is not found." << G4endl;
|
|---|
| 362 | return;
|
|---|
| 363 | }
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | G4UIcommandTree * floor[10];
|
|---|
| 367 | floor[0] = treeTop;
|
|---|
| 368 | G4int iFloor = 0;
|
|---|
| 369 | size_t prefixIndex = 1;
|
|---|
| 370 | G4String prefix = GetCurrentWorkingDirectory();
|
|---|
| 371 | while( prefixIndex < prefix.length()-1 )
|
|---|
| 372 | {
|
|---|
| 373 | size_t ii = prefix.index("/",prefixIndex);
|
|---|
| 374 | floor[iFloor+1] =
|
|---|
| 375 | floor[iFloor]->GetTree(G4String(prefix(0,ii+1)));
|
|---|
| 376 | prefixIndex = ii+1;
|
|---|
| 377 | iFloor++;
|
|---|
| 378 | }
|
|---|
| 379 | floor[iFloor]->ListCurrentWithNum();
|
|---|
| 380 | // 1998 Oct 2 non-number input
|
|---|
| 381 | while(1){
|
|---|
| 382 | //G4cout << G4endl << "Type the number ( 0:end, -n:n level back ) : "<<std::flush;
|
|---|
| 383 | G4cout << G4endl << "Type the number ( 0:end, -n:n level back ) : "<<G4endl;
|
|---|
| 384 | G4int i;
|
|---|
| 385 | if(!GetHelpChoice(i)){
|
|---|
| 386 | G4cout << G4endl << "Not a number, once more" << G4endl;
|
|---|
| 387 | continue;
|
|---|
| 388 | } else if( i < 0 ){
|
|---|
| 389 | iFloor += i;
|
|---|
| 390 | if( iFloor < 0 ) iFloor = 0;
|
|---|
| 391 | floor[iFloor]->ListCurrentWithNum();
|
|---|
| 392 | continue;
|
|---|
| 393 | } else if(i == 0) {
|
|---|
| 394 | break;
|
|---|
| 395 | } else if( i > 0 ) {
|
|---|
| 396 | G4int n_tree = floor[iFloor]->GetTreeEntry();
|
|---|
| 397 | if( i > n_tree )
|
|---|
| 398 | {
|
|---|
| 399 | if( i <= n_tree + floor[iFloor]->GetCommandEntry() )
|
|---|
| 400 | {
|
|---|
| 401 | floor[iFloor]->GetCommand(i-n_tree)->List();
|
|---|
| 402 | }
|
|---|
| 403 | }
|
|---|
| 404 | else
|
|---|
| 405 | {
|
|---|
| 406 | floor[iFloor+1] = floor[iFloor]->GetTree(i);
|
|---|
| 407 | iFloor++;
|
|---|
| 408 | floor[iFloor]->ListCurrentWithNum();
|
|---|
| 409 | }
|
|---|
| 410 | }
|
|---|
| 411 | }
|
|---|
| 412 | G4cout << "Exit from HELP." << G4endl << G4endl;
|
|---|
| 413 | //G4cout << G4endl;
|
|---|
| 414 | ExitHelp();
|
|---|
| 415 | }
|
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|