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