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