00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "G4VBasicShell.hh"
00032 #include "G4StateManager.hh"
00033 #include "G4UIcommandTree.hh"
00034 #include "G4UIcommand.hh"
00035 #include "G4UIcommandStatus.hh"
00036 #include "G4UImanager.hh"
00037 #include <vector>
00038 #include <sstream>
00039
00040 G4VBasicShell::G4VBasicShell()
00041 :currentDirectory("/")
00042 {
00043 }
00044
00045 G4VBasicShell::~G4VBasicShell()
00046 {;}
00047
00048 G4String G4VBasicShell::ModifyToFullPathCommand(const char* aCommandLine)
00049 {
00050 G4String rawCommandLine = (G4String)aCommandLine;
00051 if(rawCommandLine.isNull()||rawCommandLine(0)=='\0') return rawCommandLine;
00052 G4String commandLine = (G4String)rawCommandLine.strip(G4String::both);
00053 G4String commandString;
00054 G4String parameterString;
00055 size_t i = commandLine.index(" ");
00056 if( i != std::string::npos )
00057 {
00058 commandString = (G4String)commandLine(0,i);
00059 parameterString = " ";
00060 parameterString += (G4String)commandLine(i+1,commandLine.length()-(i+1));
00061 }
00062 else
00063 { commandString = commandLine; }
00064
00065 G4String fullPathCommandLine
00066 = ModifyPath( commandString )+parameterString;
00067 return fullPathCommandLine;
00068 }
00069
00070 G4String G4VBasicShell::GetCurrentWorkingDirectory()
00071 {
00072 return currentDirectory;
00073 }
00074
00075 G4bool G4VBasicShell::ChangeDirectory(const char* newDir)
00076 {
00077 G4String aNewPrefix = (G4String)newDir;
00078 G4String newPrefix = (G4String)aNewPrefix.strip(G4String::both);
00079 G4String newDirectory = ModifyPath( newPrefix );
00080 if( newDirectory( newDirectory.length() - 1 ) != '/' )
00081 { newDirectory += "/"; }
00082 if( FindDirectory( (const char*)newDirectory ) == NULL )
00083 { return false; }
00084 currentDirectory = newDirectory;
00085 return true;
00086 }
00087
00088 G4UIcommandTree* G4VBasicShell::FindDirectory(const char* dirName)
00089 {
00090 G4String aDirName = (G4String)dirName;
00091 G4String theDir = (G4String)aDirName.strip(G4String::both);
00092 G4String targetDir = ModifyPath( theDir );
00093 if( targetDir( targetDir.length()-1 ) != '/' )
00094 { targetDir += "/"; }
00095 G4UIcommandTree* comTree = G4UImanager::GetUIpointer()->GetTree();
00096 if( targetDir == "/" )
00097 { return comTree; }
00098 size_t idx = 1;
00099 while( idx < targetDir.length()-1 )
00100 {
00101 size_t i = targetDir.index("/",idx);
00102 comTree = comTree->GetTree((G4String)targetDir(0,i+1));
00103 if( comTree == NULL )
00104 { return NULL; }
00105 idx = i+1;
00106 }
00107 return comTree;
00108 }
00109
00110 G4UIcommand* G4VBasicShell::FindCommand(const char* commandName)
00111 {
00112 G4String rawCommandLine = (G4String)commandName;
00113 G4String commandLine = (G4String)rawCommandLine.strip(G4String::both);
00114 G4String commandString;
00115 size_t i = commandLine.index(" ");
00116 if( i != std::string::npos )
00117 { commandString = (G4String)commandLine(0,i); }
00118 else
00119 { commandString = commandLine; }
00120
00121 G4String targetCom = ModifyPath(commandString);
00122 return G4UImanager::GetUIpointer()->GetTree()->FindPath(targetCom);
00123 }
00124
00125 G4String G4VBasicShell::ModifyPath(G4String tempPath)
00126 {
00127 G4String newPath = currentDirectory;
00128
00129 if( tempPath.length()>0 )
00130 {
00131
00132 if( tempPath(0) == '/' )
00133 { newPath = tempPath; }
00134 else if( tempPath(0) != '.' )
00135 { newPath += tempPath; }
00136 else if( tempPath(0,2) == "./" )
00137 { newPath += (G4String)tempPath(2,tempPath.length()-2); }
00138 else
00139 {
00140 while( 1 )
00141 {
00142 if( tempPath(0,2) == ".." )
00143 {
00144 if( newPath != "/" )
00145 {
00146 G4String tmpString = (G4String)newPath(0,newPath.length()-1);
00147 newPath = (G4String)newPath(0,tmpString.last('/')+1);
00148 }
00149 if( tempPath == ".." || tempPath == "../" )
00150 { break; }
00151 tempPath = (G4String)tempPath(3,tempPath.length()-3);
00152 }
00153 else
00154 {
00155 newPath += tempPath;
00156 break;
00157 }
00158 }
00159 }
00160
00161 }
00162
00163 return newPath;
00164 }
00166
00168 G4String G4VBasicShell::Complete(G4String commandName)
00169 {
00170 G4String rawCommandLine = commandName;
00171 G4String commandLine = rawCommandLine.strip(G4String::both);
00172 size_t i = commandLine.index(" ");
00173 if( i != std::string::npos ) return rawCommandLine;
00174
00175 G4String commandString = commandLine;
00176 G4String targetCom = ModifyPath(commandString);
00177 G4UIcommandTree* tree = G4UImanager::GetUIpointer()->GetTree();
00178 G4String value = FindMatchingPath(tree,targetCom);
00179 if(value=="") return rawCommandLine;
00180 return value;
00181 }
00182 G4String G4VBasicShell::FindMatchingPath(
00183 G4UIcommandTree* aTree
00184 ,G4String aCommandPath
00185 )
00186
00187 {
00188 G4String empty = "";
00189 if(aTree==NULL) return empty;
00190 G4String pathName = aTree->GetPathName();
00191 if( aCommandPath.index( pathName ) == std::string::npos ) return empty;
00192 G4String remainingPath = aCommandPath;
00193 remainingPath.remove(0,pathName.length());
00194 size_t i = remainingPath.first('/');
00195 if( i == std::string::npos ) {
00196
00197 std::vector<G4UIcommand*> commands;
00198 G4int n_commandEntry = aTree->GetCommandEntry();
00199 for( G4int i_thCommand = 1; i_thCommand <= n_commandEntry; i_thCommand++ ) {
00200 G4UIcommand* cmd = aTree->GetCommand(i_thCommand);
00201 G4String ss = cmd->GetCommandName();
00202 ss.resize(remainingPath.length());
00203 if( remainingPath == ss ) commands.push_back(cmd);
00204 }
00205 n_commandEntry = commands.size();
00206 if(n_commandEntry==1) {
00207 return (pathName + commands[0]->GetCommandName());
00208 } else if (n_commandEntry>=2) {
00209 G4cout << "Matching commands :" << G4endl;
00210 for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ ) {
00211 G4UIcommand* cmd = commands[i_thCommand];
00212 G4cout << cmd->GetCommandName() << G4endl;
00213 }
00214 return empty;
00215 }
00216
00217 std::vector<G4UIcommandTree*> trees;
00218 G4String nextPath = pathName;
00219 nextPath.append(remainingPath);
00220 G4int n_treeEntry = aTree->GetTreeEntry();
00221 for( G4int i_thTree = 1; i_thTree <= n_treeEntry; i_thTree++ ) {
00222 G4UIcommandTree* tree = aTree->GetTree(i_thTree);
00223 G4String ss = tree->GetPathName();
00224 ss.resize(nextPath.length());
00225 if( nextPath == ss ) trees.push_back(tree);
00226 }
00227 n_treeEntry = trees.size();
00228 if(n_treeEntry==1) {
00229 return trees[0]->GetPathName();
00230 } else if (n_treeEntry>=2) {
00231 G4cout << "Matching directories :" << G4endl;
00232 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ ) {
00233 G4UIcommandTree* tree = trees[i_thTree];
00234 G4cout << tree->GetPathName() << G4endl;
00235 }
00236 return empty;
00237 } else {
00238 return empty;
00239 }
00240 } else {
00241
00242 G4String nextPath = pathName;
00243 nextPath.append(remainingPath(0,i+1));
00244 G4int n_treeEntry = aTree->GetTreeEntry();
00245 for( G4int i_thTree = 1; i_thTree <= n_treeEntry; i_thTree++ ) {
00246 G4UIcommandTree* tree = aTree->GetTree(i_thTree);
00247 if( nextPath == tree->GetPathName() ) {
00248 return FindMatchingPath(tree,aCommandPath );
00249 }
00250 }
00251 }
00252 return empty;
00253 }
00255
00257
00258 void G4VBasicShell::ExecuteCommand (
00259 G4String aCommand
00260 )
00261
00262
00264 {
00265 if(aCommand.length()<2) return;
00266 G4UImanager* UI = G4UImanager::GetUIpointer();
00267 if(UI==NULL) return;
00268 G4int commandStatus = UI->ApplyCommand(aCommand);
00269 switch(commandStatus) {
00270 case fCommandSucceeded:
00271 break;
00272 case fCommandNotFound:
00273 G4cerr << "command not found" << G4endl;
00274 break;
00275 case fIllegalApplicationState:
00276 G4cerr << "illegal application state -- command refused" << G4endl;
00277 break;
00278 case fParameterOutOfRange:
00279 case fParameterUnreadable:
00280 case fParameterOutOfCandidates:
00281 default:
00282 G4cerr << "command refused (" << commandStatus << ")" << G4endl;
00283 }
00284 }
00285
00286 void G4VBasicShell::ApplyShellCommand (
00287 G4String a_string
00288 ,G4bool& exitSession
00289 ,G4bool& exitPause
00290 )
00291
00293 {
00294 G4UImanager* UI = G4UImanager::GetUIpointer();
00295 if(UI==NULL) return;
00296
00297 G4String command = a_string.strip(G4String::leading);
00298 if( command(0) == '#' ) {
00299
00300 G4cout << command << G4endl;
00301
00302 } else if( command == "ls" || command(0,3) == "ls " ) {
00303
00304 ListDirectory( command );
00305
00306 } else if( command == "pwd" ) {
00307
00308 G4cout << "Current Working Directory : "
00309 << GetCurrentWorkingDirectory() << G4endl;
00310
00311 } else if( command(0,2) == "cd" ) {
00312
00313 ChangeDirectoryCommand ( command );
00314
00315 } else if( command(0,4) == "help" ) {
00316
00317 TerminalHelp( command );
00318
00319 } else if( command(0) == '?' ) {
00320
00321 ShowCurrent( command );
00322
00323 } else if( command(0,4) == "hist" ) {
00324
00325 G4int nh = UI->GetNumberOfHistory();
00326 for(G4int i=0;i<nh;i++) {
00327 G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl;
00328 }
00329
00330 } else if( command(0) == '!' ) {
00331
00332 G4String ss = command(1,command.length()-1);
00333 G4int vl;
00334 const char* tt = ss;
00335 std::istringstream is(tt);
00336 is >> vl;
00337 G4int nh = UI->GetNumberOfHistory();
00338 if(vl>=0 && vl<nh) {
00339 G4String prev = UI->GetPreviousCommand(vl);
00340 G4cout << prev << G4endl;
00341 ExecuteCommand (ModifyToFullPathCommand(prev));
00342 } else {
00343 G4cerr << "history " << vl << " is not found." << G4endl;
00344 }
00345
00346 } else if( command(0,4) == "exit" ) {
00347
00348 if( exitPause == false) {
00349 G4cout << "You are now processing RUN." << G4endl;
00350 G4cout << "Please abort it using \"/run/abort\" command first" << G4endl;
00351 G4cout << " and use \"continue\" command until the application" << G4endl;
00352 G4cout << " becomes to Idle." << G4endl;
00353 } else {
00354 exitSession = true;
00355 }
00356
00357 } else if( command(0,4) == "cont" ) {
00358
00359 exitPause = true;
00360
00361 } else {
00362
00363 ExecuteCommand (ModifyToFullPathCommand(a_string));
00364
00365 }
00366 }
00367 void G4VBasicShell::ShowCurrent(G4String newCommand)
00368 {
00369 G4UImanager* UI = G4UImanager::GetUIpointer();
00370 if(UI==NULL) return;
00371 G4String comString = newCommand(1,newCommand.length()-1);
00372 G4String theCommand = ModifyToFullPathCommand(comString);
00373 G4String curV = UI->GetCurrentValues(theCommand);
00374 if( ! curV.isNull() ) {
00375 G4cout << "Current value(s) of the parameter(s) : " << curV << G4endl;
00376 }
00377 }
00378 void G4VBasicShell::ChangeDirectoryCommand(G4String newCommand)
00379 {
00380 G4String prefix;
00381 if( newCommand.length() <= 3 ) {
00382 prefix = "/";
00383 } else {
00384 G4String aNewPrefix = newCommand(3,newCommand.length()-3);
00385 prefix = aNewPrefix.strip(G4String::both);
00386 }
00387 if(!ChangeDirectory(prefix)) {
00388 G4cout << "directory <" << prefix << "> not found." << G4endl;
00389 }
00390 }
00391 void G4VBasicShell::ListDirectory(G4String newCommand)
00392 {
00393 G4String targetDir;
00394 if( newCommand.length() <= 3 ) {
00395 targetDir = GetCurrentWorkingDirectory();
00396 } else {
00397 G4String newPrefix = newCommand(3,newCommand.length()-3);
00398 targetDir = newPrefix.strip(G4String::both);
00399 }
00400 G4UIcommandTree* commandTree = FindDirectory( targetDir );
00401 if( commandTree == NULL ) {
00402 G4cout << "Directory <" << targetDir << "> is not found." << G4endl;
00403 } else {
00404 commandTree->ListCurrent();
00405 }
00406 }
00407 void G4VBasicShell::TerminalHelp(G4String newCommand)
00408 {
00409 G4UImanager* UI = G4UImanager::GetUIpointer();
00410 if(UI==NULL) return;
00411 G4UIcommandTree * treeTop = UI->GetTree();
00412 size_t i = newCommand.index(" ");
00413 if( i != std::string::npos )
00414 {
00415 G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
00416 newValue.strip(G4String::both);
00417 G4String targetCom = ModifyToFullPathCommand( newValue );
00418 G4UIcommand* theCommand = treeTop->FindPath( targetCom );
00419 if( theCommand != NULL )
00420 {
00421 theCommand->List();
00422 return;
00423 }
00424 else
00425 {
00426 G4cout << "Command <" << newValue << " is not found." << G4endl;
00427 return;
00428 }
00429 }
00430
00431 G4UIcommandTree * floor[10];
00432 floor[0] = treeTop;
00433 G4int iFloor = 0;
00434 size_t prefixIndex = 1;
00435 G4String prefix = GetCurrentWorkingDirectory();
00436 while( prefixIndex < prefix.length()-1 )
00437 {
00438 size_t ii = prefix.index("/",prefixIndex);
00439 floor[iFloor+1] =
00440 floor[iFloor]->GetTree(G4String(prefix(0,ii+1)));
00441 prefixIndex = ii+1;
00442 iFloor++;
00443 }
00444 floor[iFloor]->ListCurrentWithNum();
00445
00446 while(1){
00447
00448 G4cout << G4endl << "Type the number ( 0:end, -n:n level back ) : "<<G4endl;
00449 G4int i;
00450 if(!GetHelpChoice(i)){
00451 G4cout << G4endl << "Not a number, once more" << G4endl;
00452 continue;
00453 } else if( i < 0 ){
00454 iFloor += i;
00455 if( iFloor < 0 ) iFloor = 0;
00456 floor[iFloor]->ListCurrentWithNum();
00457 continue;
00458 } else if(i == 0) {
00459 break;
00460 } else if( i > 0 ) {
00461 G4int n_tree = floor[iFloor]->GetTreeEntry();
00462 if( i > n_tree )
00463 {
00464 if( i <= n_tree + floor[iFloor]->GetCommandEntry() )
00465 {
00466 floor[iFloor]->GetCommand(i-n_tree)->List();
00467 }
00468 }
00469 else
00470 {
00471 floor[iFloor+1] = floor[iFloor]->GetTree(i);
00472 iFloor++;
00473 floor[iFloor]->ListCurrentWithNum();
00474 }
00475 }
00476 }
00477 G4cout << "Exit from HELP." << G4endl << G4endl;
00478
00479 ExitHelp();
00480 }
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492