source: trunk/source/interfaces/common/src/G4VBasicShell.cc @ 985

Last change on this file since 985 was 921, checked in by garnier, 15 years ago

en test de gl2ps. Problemes de libraries

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