source: trunk/source/interfaces/common/src/G4VBasicShell.cc-1.12@ 1337

Last change on this file since 1337 was 1316, checked in by garnier, 15 years ago

update geant4-09-04-beta-cand-01 interfaces-V09-03-09 vis-V09-03-08

File size: 15.0 KB
Line 
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.12 2006/06/29 19:10:20 gunter 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
40G4VBasicShell::G4VBasicShell()
41:currentDirectory("/")
42{
43}
44
45G4VBasicShell::~G4VBasicShell()
46{;}
47
48G4String 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
70G4String G4VBasicShell::GetCurrentWorkingDirectory()
71{
72 return currentDirectory;
73}
74
75G4bool 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
88G4UIcommandTree* 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
110G4UIcommand* 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
125G4String 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////////////////////////////////////////////
168G4String 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}
182G4String G4VBasicShell::FindMatchingPath(
183 G4UIcommandTree* aTree
184,G4String aCommandPath
185)
186// From intercoms/src/G4UIcommandTree::FindPath.
187{
188 G4String empty = "";
189 if(aTree==NULL) return empty;
190 G4String pathName = aTree->GetPathName();
191 if( aCommandPath.index( pathName ) == std::string::npos ) return empty;
192 G4String remainingPath = aCommandPath;
193 remainingPath.remove(0,pathName.length());
194 size_t i = remainingPath.first('/');
195 if( i == std::string::npos ) {
196 // Look for number of matching commands :
197 std::vector<G4UIcommand*> commands;
198 G4int n_commandEntry = aTree->GetCommandEntry();
199 for( G4int i_thCommand = 1; i_thCommand <= n_commandEntry; i_thCommand++ ) {
200 G4UIcommand* cmd = aTree->GetCommand(i_thCommand);
201 G4String ss = cmd->GetCommandName();
202 ss.resize(remainingPath.length());
203 if( remainingPath == ss ) commands.push_back(cmd);
204 }
205 n_commandEntry = commands.size();
206 if(n_commandEntry==1) {
207 return (pathName + commands[0]->GetCommandName());
208 } else if (n_commandEntry>=2) {
209 G4cout << "Matching commands :" << G4endl;
210 for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ ) {
211 G4UIcommand* cmd = commands[i_thCommand];
212 G4cout << cmd->GetCommandName() << G4endl;
213 }
214 return empty;
215 }
216 // Look for sub tree :
217 std::vector<G4UIcommandTree*> trees;
218 G4String nextPath = pathName;
219 nextPath.append(remainingPath);
220 G4int n_treeEntry = aTree->GetTreeEntry();
221 for( G4int i_thTree = 1; i_thTree <= n_treeEntry; i_thTree++ ) {
222 G4UIcommandTree* tree = aTree->GetTree(i_thTree);
223 G4String ss = tree->GetPathName();
224 ss.resize(nextPath.length());
225 if( nextPath == ss ) trees.push_back(tree);
226 }
227 n_treeEntry = trees.size();
228 if(n_treeEntry==1) {
229 return trees[0]->GetPathName();
230 } else if (n_treeEntry>=2) {
231 G4cout << "Matching directories :" << G4endl;
232 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ ) {
233 G4UIcommandTree* tree = trees[i_thTree];
234 G4cout << tree->GetPathName() << G4endl;
235 }
236 return empty;
237 } else {
238 return empty; // No match.
239 }
240 } else {
241 // Find path
242 G4String nextPath = pathName;
243 nextPath.append(remainingPath(0,i+1));
244 G4int n_treeEntry = aTree->GetTreeEntry();
245 for( G4int i_thTree = 1; i_thTree <= n_treeEntry; i_thTree++ ) {
246 G4UIcommandTree* tree = aTree->GetTree(i_thTree);
247 if( nextPath == tree->GetPathName() ) {
248 return FindMatchingPath(tree,aCommandPath );
249 }
250 }
251 }
252 return empty;
253}
254////////////////////////////////////////////
255// Method involving an interactive G4cout //
256////////////////////////////////////////////
257/***************************************************************************/
258void G4VBasicShell::ExecuteCommand (
259 G4String aCommand
260)
261/***************************************************************************/
262// Should be put in G4VBasicShell.
263/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
264{
265 if(aCommand.length()<2) return;
266 G4UImanager* UI = G4UImanager::GetUIpointer();
267 if(UI==NULL) return;
268 G4int commandStatus = UI->ApplyCommand(aCommand);
269 switch(commandStatus) {
270 case fCommandSucceeded:
271 break;
272 case fCommandNotFound:
273 G4cerr << "command not found" << G4endl;
274 break;
275 case fIllegalApplicationState:
276 G4cerr << "illegal application state -- command refused" << G4endl;
277 break;
278 case fParameterOutOfRange:
279 case fParameterUnreadable:
280 case fParameterOutOfCandidates:
281 default:
282 G4cerr << "command refused (" << commandStatus << ")" << G4endl;
283 }
284}
285/***************************************************************************/
286void G4VBasicShell::ApplyShellCommand (
287 G4String a_string
288,G4bool& exitSession
289,G4bool& exitPause
290)
291/***************************************************************************/
292/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
293{
294 G4UImanager* UI = G4UImanager::GetUIpointer();
295 if(UI==NULL) return;
296
297 G4String command = a_string.strip(G4String::leading);
298 if( command(0) == '#' ) {
299
300 G4cout << command << G4endl;
301
302 } else if( command == "ls" || command(0,3) == "ls " ) {
303
304 ListDirectory( command );
305
306 } else if( command == "pwd" ) {
307
308 G4cout << "Current Working Directory : "
309 << GetCurrentWorkingDirectory() << G4endl;
310
311 } else if( command(0,2) == "cd" ) {
312
313 ChangeDirectoryCommand ( command );
314
315 } else if( command(0,4) == "help" ) {
316
317 TerminalHelp( command );
318
319 } else if( command(0) == '?' ) {
320
321 ShowCurrent( command );
322
323 } else if( command(0,4) == "hist" ) {
324
325 G4int nh = UI->GetNumberOfHistory();
326 for(G4int i=0;i<nh;i++) {
327 G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl;
328 }
329
330 } else if( command(0) == '!' ) {
331
332 G4String ss = command(1,command.length()-1);
333 G4int vl;
334 const char* tt = ss;
335 std::istringstream is(tt);
336 is >> vl;
337 G4int nh = UI->GetNumberOfHistory();
338 if(vl>=0 && vl<nh) {
339 G4String prev = UI->GetPreviousCommand(vl);
340 G4cout << prev << G4endl;
341 ExecuteCommand (ModifyToFullPathCommand(prev));
342 } else {
343 G4cerr << "history " << vl << " is not found." << G4endl;
344 }
345
346 } else if( command(0,4) == "exit" ) {
347
348 if( exitPause == false) { //In a secondary loop.
349 G4cout << "You are now processing RUN." << G4endl;
350 G4cout << "Please abort it using \"/run/abort\" command first" << G4endl;
351 G4cout << " and use \"continue\" command until the application" << G4endl;
352 G4cout << " becomes to Idle." << G4endl;
353 } else {
354 exitSession = true;
355 }
356
357 } else if( command(0,4) == "cont" ) {
358
359 exitPause = true;
360
361 } else {
362
363 ExecuteCommand (ModifyToFullPathCommand(a_string));
364
365 }
366}
367void G4VBasicShell::ShowCurrent(G4String newCommand)
368{
369 G4UImanager* UI = G4UImanager::GetUIpointer();
370 if(UI==NULL) return;
371 G4String comString = newCommand(1,newCommand.length()-1);
372 G4String theCommand = ModifyToFullPathCommand(comString);
373 G4String curV = UI->GetCurrentValues(theCommand);
374 if( ! curV.isNull() ) {
375 G4cout << "Current value(s) of the parameter(s) : " << curV << G4endl;
376 }
377}
378void G4VBasicShell::ChangeDirectoryCommand(G4String newCommand)
379{
380 G4String prefix;
381 if( newCommand.length() <= 3 ) {
382 prefix = "/";
383 } else {
384 G4String aNewPrefix = newCommand(3,newCommand.length()-3);
385 prefix = aNewPrefix.strip(G4String::both);
386 }
387 if(!ChangeDirectory(prefix)) {
388 G4cout << "directory <" << prefix << "> not found." << G4endl;
389 }
390}
391void G4VBasicShell::ListDirectory(G4String newCommand)
392{
393 G4String targetDir;
394 if( newCommand.length() <= 3 ) {
395 targetDir = GetCurrentWorkingDirectory();
396 } else {
397 G4String newPrefix = newCommand(3,newCommand.length()-3);
398 targetDir = newPrefix.strip(G4String::both);
399 }
400 G4UIcommandTree* commandTree = FindDirectory( targetDir );
401 if( commandTree == NULL ) {
402 G4cout << "Directory <" << targetDir << "> is not found." << G4endl;
403 } else {
404 commandTree->ListCurrent();
405 }
406}
407void G4VBasicShell::TerminalHelp(G4String newCommand)
408{
409 G4UImanager* UI = G4UImanager::GetUIpointer();
410 if(UI==NULL) return;
411 G4UIcommandTree * treeTop = UI->GetTree();
412 size_t i = newCommand.index(" ");
413 if( i != std::string::npos )
414 {
415 G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
416 newValue.strip(G4String::both);
417 G4String targetCom = ModifyToFullPathCommand( newValue );
418 G4UIcommand* theCommand = treeTop->FindPath( targetCom );
419 if( theCommand != NULL )
420 {
421 theCommand->List();
422 return;
423 }
424 else
425 {
426 G4cout << "Command <" << newValue << " is not found." << G4endl;
427 return;
428 }
429 }
430
431 G4UIcommandTree * floor[10];
432 floor[0] = treeTop;
433 G4int iFloor = 0;
434 size_t prefixIndex = 1;
435 G4String prefix = GetCurrentWorkingDirectory();
436 while( prefixIndex < prefix.length()-1 )
437 {
438 size_t ii = prefix.index("/",prefixIndex);
439 floor[iFloor+1] =
440 floor[iFloor]->GetTree(G4String(prefix(0,ii+1)));
441 prefixIndex = ii+1;
442 iFloor++;
443 }
444 floor[iFloor]->ListCurrentWithNum();
445 // 1998 Oct 2 non-number input
446 while(1){
447 //G4cout << G4endl << "Type the number ( 0:end, -n:n level back ) : "<<std::flush;
448 G4cout << G4endl << "Type the number ( 0:end, -n:n level back ) : "<<G4endl;
449 G4int i;
450 if(!GetHelpChoice(i)){
451 G4cout << G4endl << "Not a number, once more" << G4endl;
452 continue;
453 } else if( i < 0 ){
454 iFloor += i;
455 if( iFloor < 0 ) iFloor = 0;
456 floor[iFloor]->ListCurrentWithNum();
457 continue;
458 } else if(i == 0) {
459 break;
460 } else if( i > 0 ) {
461 G4int n_tree = floor[iFloor]->GetTreeEntry();
462 if( i > n_tree )
463 {
464 if( i <= n_tree + floor[iFloor]->GetCommandEntry() )
465 {
466 floor[iFloor]->GetCommand(i-n_tree)->List();
467 }
468 }
469 else
470 {
471 floor[iFloor+1] = floor[iFloor]->GetTree(i);
472 iFloor++;
473 floor[iFloor]->ListCurrentWithNum();
474 }
475 }
476 }
477 G4cout << "Exit from HELP." << G4endl << G4endl;
478 //G4cout << G4endl;
479 ExitHelp();
480}
481
482
483
484
485
486
487
488
489
490
491
492
Note: See TracBrowser for help on using the repository browser.