Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4VUIshell.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: G4VUIshell.cc 66892 2013-01-17 10:57:59Z gunter $
28 //
29 
30 #include "G4UImanager.hh"
31 #include "G4UIcommand.hh"
32 #include "G4UIcommandTree.hh"
33 #include "G4StateManager.hh"
34 #include "G4UIcommandStatus.hh"
35 #include "G4VUIshell.hh"
36 #include "G4UIArrayString.hh"
37 
38 // terminal color string
39 static const G4String strESC= '\033';
40 static const G4String TermColorString[8] ={
41  strESC+"[30m", strESC+"[31m", strESC+"[32m", strESC+"[33m",
42  strESC+"[34m", strESC+"[35m", strESC+"[36m", strESC+"[37m"
43 };
44 
47  : promptSetting(prompt), promptString(""), nColumn(80),
48  lsColorFlag(FALSE), directoryColor(BLACK), commandColor(BLACK),
49  currentCommandDir("/")
51 {
52 }
53 
57 {
58 }
59 
61 void G4VUIshell::MakePrompt(const char* msg)
62 {
64  if(promptSetting.length()<=1) {
66  return;
67  }
68 
69  promptString="";
70  G4int i;
71  for(i=0; i<G4int(promptSetting.length())-1; i++){
72  if(promptSetting[(size_t)i]=='%'){
73  switch (promptSetting[(size_t)(i+1)]) {
74  case 's': // current application status
75  {
76  G4String stateStr;
77  if(msg)
78  { stateStr = msg; }
79  else
80  {
82  stateStr= statM-> GetStateString(statM->GetCurrentState());
83  }
84  promptString.append(stateStr);
85  i++;
86  }
87  break;
88  case '/': // current working directory
90  i++;
91  break;
92  default:
94  break;
95  }
96  } else {
98  }
99  }
100 
101  // append last chaacter
102  if(i == G4int(promptSetting.length())-1)
104 }
105 
106 
110 {
111 
112 }
113 
114 // --------------------------------------------------------------------
115 // G4command operations
116 // --------------------------------------------------------------------
120 {
122 
123  G4UIcommandTree* cmdTree= UI-> GetTree(); // root tree
124 
125  G4String absPath= input; // G4String::strip() CONST !!
126  absPath= GetAbsCommandDirPath(absPath.strip(G4String::both));
127 
128  // parsing absolute path ...
129  if(absPath.length()==0) return NULL;
130  if(absPath[absPath.length()-1] != '/') return NULL; // error??
131  if(absPath=="/") return cmdTree;
132 
133  for(G4int indx=1; indx<G4int(absPath.length())-1; ) {
134  G4int jslash= absPath.index("/", indx); // search index begin with "/"
135  if(jslash != G4int(G4String::npos)) {
136  if(cmdTree != NULL)
137  cmdTree= cmdTree-> GetTree(G4String(absPath(0,jslash+1)));
138  }
139  indx= jslash+1;
140  }
141 
142  if(cmdTree == NULL) return NULL;
143  else return cmdTree;
144 }
145 
149 {
150  if(apath.empty()) return apath; // null string
151 
152  // if "apath" does not start with "/",
153  // then it is treared as relative path
154  G4String bpath= apath;
155  if(apath[(size_t)0] != '/') bpath= currentCommandDir + apath;
156 
157  // parsing...
158  G4String absPath= "/";
159  for(G4int indx=1; indx<=G4int(bpath.length())-1; ) {
160  G4int jslash= bpath.index("/", indx); // search index begin with "/"
161  if(indx == jslash) { // skip first '///'
162  indx++;
163  continue;
164  }
165  if(jslash != G4int(G4String::npos)) {
166  if(bpath.substr(indx,jslash-indx) == ".."){ // directory up
167  if(absPath == "/") {
168  indx = jslash+1;
169  continue;
170  }
171  if(absPath.length() >= 2) {
172  absPath.remove(absPath.length()-1); // remove last "/"
173  G4int jpre= absPath.last('/');
174  if(jpre != G4int(G4String::npos)) absPath.remove(jpre+1);
175  }
176  } else if(bpath.substr(indx,jslash-indx) == "."){ // nothing to do
177  } else { // add
178  if( !(jslash==indx && bpath(indx)=='/') ) // truncate "////"
179  absPath+= bpath(indx, jslash-indx+1);
180  // better to be check directory existence. (it costs!)
181  }
182  indx= jslash+1;
183  } else { // directory ONLY (ignore non-"/" terminated string)
184  break;
185  }
186  }
187 
188  return absPath;
189 }
190 
191 
195 { // xxx/xxx/zzz -> zzz, trancate /// -> /
196  if(apath.empty()) return apath;
197 
198  G4int lstr= apath.length();
199 
200  // for trancating "/"
201  G4bool Qsla= FALSE;
202  if(apath[(size_t)(lstr-1)]=='/') Qsla= TRUE;
203 
204  // searching last '/' from tail
205  G4int indx= -1;
206  for(G4int i=lstr-1; i>=0; i--) {
207  if(Qsla && apath[(size_t)i]!='/') Qsla= FALSE; // break "/" flag!!
208  if(apath[(size_t)i]=='/' && !Qsla) {
209  indx= i;
210  break;
211  }
212  }
213 
214  if(indx==-1) return apath; // not found
215 
216  if(indx==0 && lstr==1) { // "/"
217  G4String nullStr;
218  return nullStr;
219  } else {
220  //G4String newPath= apath(indx+1,lstr-indx-1);
221  G4String newPath= apath;
222  newPath= newPath(indx+1,lstr-indx-1);
223  return newPath;
224  }
225 }
226 
227 // --------------------------------------------------------------------
228 // shell commands
229 // --------------------------------------------------------------------
232  const G4String& candidate) const
234 {
235  // specified directpry
236  G4String input= dir; // ...
237  input= input.strip(G4String::both);
238 
239  // command tree of "user specified directory"
240  G4String vpath= currentCommandDir;
241  G4String vcmd;
242 
243  G4int len= input.length();
244  if(! input.empty()) {
245  G4int indx= -1;
246  for(G4int i=len-1; i>=0; i--) { // search last '/'
247  if(input[(size_t)i]=='/') {
248  indx= i;
249  break;
250  }
251  }
252  // get abs. path
253  if(indx != -1) vpath= GetAbsCommandDirPath(input(0,indx+1));
254  if(!(indx==0 && len==1)) vcmd= input(indx+1,len-indx-1); // care for "/"
255  }
256 
257  // check "vcmd" is directory?
258  G4String inputpath= vpath+vcmd;
259  if(! vcmd.empty()){
260  G4String tmpstr= inputpath + "/";
261  if(GetCommandTree(tmpstr) != NULL) {
262  vpath= tmpstr;
263  vcmd= "";
264  }
265  }
266 
267  // check "vpath" directory exists?
268  G4UIcommandTree* atree= GetCommandTree(vpath);
269  if(atree == NULL) {
270  G4cout << "<" << input << ">: No such directory" << G4endl;
271  return;
272  }
273 
274  // list matched directories/commands
275  G4String stream;
276  G4bool isMatch= FALSE;
277 
278  G4int Ndir= atree-> GetTreeEntry();
279  G4int Ncmd= atree-> GetCommandEntry();
280  if(Ndir==0 && Ncmd==0) return; // no contents
281 
282  // directory ...
283  for(G4int idir=1; idir<=Ndir; idir++) {
284  if(idir==1 && lsColorFlag) stream+= TermColorString[directoryColor];
285  G4String fpdir= atree-> GetTree(idir)-> GetPathName();
286  // matching test
287  if(candidate.empty()) { // list all
288  if(vcmd=="" || fpdir==inputpath) {
289  stream+= GetCommandPathTail(fpdir); stream+= " ";
290  isMatch= TRUE;
291  }
292  } else { // list only matched with candidate
293  if( fpdir.index(candidate, 0) == 0) {
294  stream+= GetCommandPathTail(fpdir); stream+= " ";
295  }
296  }
297  }
298 
299  // command ...
300  for(G4int icmd=1; icmd<=Ncmd; icmd++){
301  if(icmd==1 && lsColorFlag) stream+= TermColorString[commandColor];
302  G4String fpcmd= atree-> GetPathName() +
303  atree-> GetCommand(icmd) -> GetCommandName();
304  // matching test
305  if(candidate.empty()) { // list all
306  if(vcmd=="" || fpcmd==inputpath) {
307  stream+= GetCommandPathTail(fpcmd); stream+= "* ";
308  isMatch= TRUE;
309  }
310  } else { // list only matched with candidate
311  if( fpcmd.index(candidate, 0) == 0) {
312  stream+= GetCommandPathTail(fpcmd); stream+= "* ";
313  }
314  }
315  }
316 
317  // waring : not matched
318  if(!isMatch && candidate.empty())
319  G4cout << "<" << input
320  << ">: No such directory or command" << std::flush;
321 
322  // display
323  G4UIArrayString arrayString(stream);
324  arrayString.Show(nColumn);
325 }
326 
const XML_Char int len
Definition: expat.h:262
virtual void MakePrompt(const char *msg=0)
Definition: G4VUIshell.cc:61
G4String GetCommandPathTail(const G4String &apath) const
Definition: G4VUIshell.cc:193
G4String promptString
Definition: G4VUIshell.hh:62
G4String & remove(str_size)
G4String strip(G4int strip_Type=trailing, char c=' ')
static const G4String strESC
Definition: G4VUIshell.cc:39
virtual void ListCommand(const G4String &input, const G4String &candidate="") const
Definition: G4VUIshell.cc:231
int G4int
Definition: G4Types.hh:78
void Show(G4int ncol)
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:59
G4String GetAbsCommandDirPath(const G4String &apath) const
Definition: G4VUIshell.cc:147
static G4StateManager * GetStateManager()
static const G4String TermColorString[8]
Definition: G4VUIshell.cc:40
G4GLOB_DLL std::ostream G4cout
str_size index(const char *, G4int pos=0) const
virtual ~G4VUIshell()
Definition: G4VUIshell.cc:55
bool G4bool
Definition: G4Types.hh:79
#define FALSE
Definition: globals.hh:52
#define TRUE
Definition: globals.hh:55
G4ApplicationState GetCurrentState() const
G4UIcommandTree * GetCommandTree(const G4String &dir) const
Definition: G4VUIshell.cc:118
virtual void ResetTerminal()
Definition: G4VUIshell.cc:108
G4int last(char) const
G4String & append(const G4String &)
G4String currentCommandDir
Definition: G4VUIshell.hh:73
#define G4endl
Definition: G4ios.hh:61
G4String promptSetting
Definition: G4VUIshell.hh:61
G4VUIshell(const G4String &prompt="> ")
Definition: G4VUIshell.cc:46