Geant4  10.01.p01
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 74547 2013-10-14 12:51:17Z 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 
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(newPath.size()-3,3) == "/..") {
155  if( newPath.size() == 3) {
156  newPath = "/";
157  } else {
158  size_t idx = newPath.find_last_of('/', newPath.size()-4);
159  if(idx != G4String::npos) newPath.erase(idx+1);
160  }
161  }
162  if(newPath(newPath.size()-2,2) == "/.") newPath.erase(newPath.size()-1,1);
163 
164  // truncate "/////" to "/"
165  while(1) {
166  size_t idx = newPath.find("//");
167  if( idx == G4String::npos) break;
168  newPath.erase(idx,1);
169  }
170 
171  return newPath;
172 }
174 // Method used for command completion //////
177 {
178  G4String rawCommandLine = commandName;
179  G4String commandLine = rawCommandLine.strip(G4String::both);
180  size_t i = commandLine.index(" ");
181  if( i != std::string::npos ) return rawCommandLine; // Already entering parameters,
182  // assume command path is correct.
183  G4String commandString = commandLine;
184  G4String targetCom = ModifyPath(commandString);
186  G4String value = FindMatchingPath(tree,targetCom);
187  if(value=="") return rawCommandLine;
188  return value;
189 }
190 
192  const G4String& aCommandPath)
193 {
194  return aTree-> CompleteCommandPath(aCommandPath);
195 }
196 
198 // Method involving an interactive G4cout //
200 /***************************************************************************/
202 /***************************************************************************/
203 // Should be put in G4VBasicShell.
205 {
206  if(aCommand.length()<2) return;
208  if(UI==NULL) return;
209  G4int commandStatus = UI->ApplyCommand(aCommand);
210  switch(commandStatus) {
211  case fCommandSucceeded:
212  break;
213  case fCommandNotFound:
214  G4cerr << "command not found: " << "\"" << aCommand << "\"" << G4endl;
215  break;
217  G4cerr << "illegal application state -- command refused:" << "\"" << aCommand << "\"" << G4endl;
218  break;
222  default:
223  G4cerr << "command refused (" << commandStatus << "):" << "\"" << aCommand << "\"" << G4endl;
224  }
225 }
226 /***************************************************************************/
228  G4bool& exitSession, G4bool& exitPause
229 )
230 /***************************************************************************/
232 {
234  if(UI==NULL) return;
235 
236  G4String command = a_string;
237  command.strip(G4String::leading);
238 
239  if( command(0) == '#' ) {
240 
241  G4cout << command << G4endl;
242 
243  } else if( command == "ls" || command(0,3) == "ls " ) {
244 
245  ListDirectory( command );
246 
247  } else if( command == "pwd" ) {
248 
249  G4cout << "Current Working Directory : "
251 
252  } else if( command == "cd" || command(0,3) == "cd ") {
253 
254  ChangeDirectoryCommand ( command );
255 
256  } else if( command == "help" || command(0,5) == "help ") {
257 
258  TerminalHelp( command );
259 
260  } else if( command(0) == '?' ) {
261 
262  ShowCurrent( command );
263 
264  } else if( command == "hist" || command == "history") {
265 
266  G4int nh = UI->GetNumberOfHistory();
267  for(G4int i=0;i<nh;i++) {
268  G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl;
269  }
270 
271  } else if( command(0) == '!' ) {
272 
273  G4String ss = command(1,command.length()-1);
274  G4int vl;
275  const char* tt = ss;
276  std::istringstream is(tt);
277  is >> vl;
278  G4int nh = UI->GetNumberOfHistory();
279  if(vl>=0 && vl<nh) {
280  G4String prev = UI->GetPreviousCommand(vl);
281  G4cout << prev << G4endl;
283  } else {
284  G4cerr << "history " << vl << " is not found." << G4endl;
285  }
286 
287  } else if( command == "exit" ) {
288 
289  if( exitPause == false) { //In a secondary loop.
290  G4cout << "You are now processing RUN." << G4endl;
291  G4cout << "Please abort it using \"/run/abort\" command first" << G4endl;
292  G4cout << " and use \"continue\" command until the application" << G4endl;
293  G4cout << " becomes to Idle." << G4endl;
294  } else {
295  exitSession = true;
296  }
297 
298  } else if( command == "cont" || command == "continue"){
299 
300  exitPause = true;
301 
302  } else {
303 
305 
306  }
307 }
308 
309 void G4VBasicShell::ShowCurrent(const G4String& newCommand) const
310 {
312  if(UI==NULL) return;
313  G4String comString = newCommand.substr(1,newCommand.length()-1);
314  G4String theCommand = ModifyToFullPathCommand(comString);
315  G4String curV = UI->GetCurrentValues(theCommand);
316  if( ! curV.isNull() ) {
317  G4cout << "Current value(s) of the parameter(s) : " << curV << G4endl;
318  }
319 }
320 
322 {
324  if( newCommand.length() <= 3 ) {
325  prefix = "/";
326  } else {
327  G4String aNewPrefix = newCommand.substr(3, newCommand.length()-3);
328  prefix = aNewPrefix.strip(G4String::both);
329  }
330  if(!ChangeDirectory(prefix)) {
331  G4cout << "directory <" << prefix << "> not found." << G4endl;
332  }
333 }
334 
335 void G4VBasicShell::ListDirectory(const G4String& newCommand) const
336 {
337  G4String targetDir;
338  if( newCommand.length() <= 3 ) {
339  targetDir = GetCurrentWorkingDirectory();
340  } else {
341  G4String newPrefix = newCommand.substr(3, newCommand.length()-3);
342  targetDir = newPrefix.strip(G4String::both);
343  }
344  G4UIcommandTree* commandTree = FindDirectory( targetDir );
345  if( commandTree == NULL ) {
346  G4cout << "Directory <" << targetDir << "> is not found." << G4endl;
347  } else {
348  commandTree->ListCurrent();
349  }
350 }
351 void G4VBasicShell::TerminalHelp(const G4String& newCommand)
352 {
354  if(UI==NULL) return;
355  G4UIcommandTree * treeTop = UI->GetTree();
356  size_t i = newCommand.index(" ");
357  if( i != std::string::npos )
358  {
359  G4String newValue = newCommand.substr(i+1, newCommand.length()-(i+1));
360  newValue.strip(G4String::both);
361  G4String targetCom = ModifyToFullPathCommand(newValue);
362  G4UIcommand* theCommand = treeTop->FindPath(targetCom);
363  if( theCommand != NULL )
364  {
365  theCommand->List();
366  return;
367  }
368  else
369  {
370  G4cout << "Command <" << newValue << " is not found." << G4endl;
371  return;
372  }
373  }
374 
375  G4UIcommandTree * floor[10];
376  floor[0] = treeTop;
377  size_t iFloor = 0;
378  size_t prefixIndex = 1;
380  while( prefixIndex < prefix.length()-1 )
381  {
382  size_t ii = prefix.index("/",prefixIndex);
383  floor[iFloor+1] =
384  floor[iFloor]->GetTree(G4String(prefix(0,ii+1)));
385  prefixIndex = ii+1;
386  iFloor++;
387  }
388  floor[iFloor]->ListCurrentWithNum();
389  // 1998 Oct 2 non-number input
390  while(1){
391  //G4cout << G4endl << "Type the number ( 0:end, -n:n level back ) : "<<std::flush;
392  G4cout << G4endl << "Type the number ( 0:end, -n:n level back ) : "<<G4endl;
393  G4int j;
394  if(!GetHelpChoice(j)){
395  G4cout << G4endl << "Not a number, once more" << G4endl;
396  continue;
397  } else if( j < 0 ){
398  if( iFloor < (size_t)-j ) iFloor = 0;
399  else iFloor += j;
400  //iFloor += j;
401  //if( iFloor < 0 ) iFloor = 0;
402  floor[iFloor]->ListCurrentWithNum();
403  continue;
404  } else if(j == 0) {
405  break;
406  } else if( j > 0 ) {
407  G4int n_tree = floor[iFloor]->GetTreeEntry();
408  if( j > n_tree )
409  {
410  if( j <= n_tree + floor[iFloor]->GetCommandEntry() )
411  {
412  floor[iFloor]->GetCommand(j-n_tree)->List();
413  }
414  }
415  else
416  {
417  floor[iFloor+1] = floor[iFloor]->GetTree(j);
418  iFloor++;
419  floor[iFloor]->ListCurrentWithNum();
420  }
421  }
422  }
423  G4cout << "Exit from HELP." << G4endl << G4endl;
424  //G4cout << G4endl;
425  ExitHelp();
426 }
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=' ')
G4String currentDirectory
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:58
G4GLOB_DLL std::ostream G4cout
str_size index(const char *, G4int pos=0) const
G4UIcommandTree * GetTree(G4int i)
bool G4bool
Definition: G4Types.hh:79
void ListCurrentWithNum() const
virtual void ExecuteCommand(const G4String &)
G4bool ChangeDirectory(const char *newDir)
G4String GetCurrentValues(const char *aCommand)
Definition: G4UImanager.cc:156
G4String ModifyToFullPathCommand(const char *aCommandLine) const
G4UIcommandTree * GetTree() const
Definition: G4UImanager.hh:206
G4String ModifyPath(const G4String &tempPath) const
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:348
G4String GetCurrentWorkingDirectory() const
G4bool isNull() const
G4UIcommand * FindCommand(const char *commandName) const
G4int ApplyCommand(const char *aCommand)
Definition: G4UImanager.cc:432
G4GLOB_DLL std::ostream G4cerr