G4VUIshell.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 
00030 #include "G4UImanager.hh"
00031 #include "G4UIcommand.hh"
00032 #include "G4UIcommandTree.hh"
00033 #include "G4StateManager.hh"
00034 #include "G4UIcommandStatus.hh"
00035 #include "G4VUIshell.hh"
00036 #include "G4UIArrayString.hh"
00037 
00038 // terminal color string
00039 static const G4String strESC= '\033';
00040 static const G4String TermColorString[8] ={ 
00041   strESC+"[30m", strESC+"[31m", strESC+"[32m", strESC+"[33m", 
00042   strESC+"[34m", strESC+"[35m", strESC+"[36m", strESC+"[37m"
00043 };
00044 
00046 G4VUIshell::G4VUIshell(const G4String& prompt)
00047   : promptSetting(prompt), promptString(""), nColumn(80),  
00048     lsColorFlag(FALSE), directoryColor(BLACK), commandColor(BLACK),
00049     currentCommandDir("/")
00051 {
00052 }
00053 
00055 G4VUIshell::~G4VUIshell()
00057 {
00058 }
00059 
00061 void G4VUIshell::MakePrompt(const char* msg) 
00062 
00063 {
00064   if(promptSetting.length()<=1) {
00065     promptString= promptSetting;
00066     return;
00067   }
00068 
00069   promptString="";
00070   G4int i;
00071   for(i=0; i<G4int(promptSetting.length())-1; i++){
00072     if(promptSetting[(size_t)i]=='%'){
00073       switch (promptSetting[(size_t)(i+1)]) {
00074       case 's':  // current application status
00075         {
00076            G4String stateStr;
00077            if(msg)
00078            { stateStr = msg; }
00079            else
00080            {
00081              G4StateManager* statM= G4StateManager::GetStateManager();
00082              stateStr= statM-> GetStateString(statM->GetCurrentState());
00083            }
00084            promptString.append(stateStr);
00085            i++;
00086         }
00087         break;
00088       case '/':  // current working directory
00089         promptString.append(currentCommandDir);
00090         i++;
00091         break;
00092       default:
00093         promptString.append(G4String(promptSetting[(size_t)i]));
00094         break;
00095       }           
00096     } else {
00097       promptString.append(G4String(promptSetting[(size_t)i]));
00098     }
00099   }
00100 
00101   // append last chaacter
00102   if(i == G4int(promptSetting.length())-1) 
00103     promptString.append(G4String(promptSetting[(size_t)i]));
00104 }
00105 
00106 
00108 void G4VUIshell::ResetTerminal()
00110 {
00111 
00112 }
00113 
00114 // --------------------------------------------------------------------
00115 //      G4command operations
00116 // --------------------------------------------------------------------
00118 G4UIcommandTree* G4VUIshell::GetCommandTree(const G4String& input) const
00120 {
00121   G4UImanager* UI= G4UImanager::GetUIpointer();
00122 
00123   G4UIcommandTree* cmdTree= UI-> GetTree();  // root tree
00124 
00125   G4String absPath= input; // G4String::strip() CONST !!
00126   absPath= GetAbsCommandDirPath(absPath.strip(G4String::both));
00127 
00128   // parsing absolute path ...
00129   if(absPath.length()==0) return NULL;
00130   if(absPath[absPath.length()-1] != '/') return NULL; // error??
00131   if(absPath=="/") return cmdTree;
00132 
00133   for(G4int indx=1; indx<G4int(absPath.length())-1; ) {
00134     G4int jslash= absPath.index("/", indx);  // search index begin with "/" 
00135     if(jslash != G4int(G4String::npos)) {
00136       if(cmdTree != NULL)
00137         cmdTree= cmdTree-> GetTree(G4String(absPath(0,jslash+1)));
00138     }
00139     indx= jslash+1;
00140   }
00141 
00142   if(cmdTree == NULL) return NULL;
00143   else return cmdTree;
00144 }
00145 
00147 G4String G4VUIshell::GetAbsCommandDirPath(const G4String& apath) const
00149 {
00150   if(apath.empty()) return apath;  // null string
00151 
00152   // if "apath" does not start with "/", 
00153   //   then it is treared as relative path
00154   G4String bpath= apath;
00155   if(apath[(size_t)0] != '/') bpath= currentCommandDir + apath;
00156 
00157   // parsing...
00158   G4String absPath= "/";
00159   for(G4int indx=1; indx<=G4int(bpath.length())-1; ) {
00160     G4int jslash= bpath.index("/", indx);  // search index begin with "/"
00161     if(indx == jslash) { // skip first '///'
00162       indx++;
00163       continue;
00164     }
00165     if(jslash != G4int(G4String::npos)) {
00166       if(bpath.substr(indx,jslash-indx) == ".."){  // directory up
00167         if(absPath == "/") {
00168           indx = jslash+1;
00169           continue;
00170         }
00171         if(absPath.length() >= 2) {
00172           absPath.remove(absPath.length()-1);  // remove last  "/"
00173           G4int jpre= absPath.last('/');
00174           if(jpre != G4int(G4String::npos)) absPath.remove(jpre+1);
00175         }
00176       } else if(bpath.substr(indx,jslash-indx) == "."){  // nothing to do
00177       } else { // add
00178         if( !(jslash==indx && bpath(indx)=='/') ) // truncate "////"
00179           absPath+= bpath(indx, jslash-indx+1);
00180           // better to be check directory existence. (it costs!)
00181       }
00182       indx= jslash+1;
00183     } else { // directory ONLY (ignore non-"/" terminated string)
00184       break;
00185     }
00186   }
00187 
00188   return  absPath;
00189 }
00190 
00191 
00193 G4String G4VUIshell::GetCommandPathTail(const G4String& apath) const
00195 {   // xxx/xxx/zzz -> zzz, trancate /// -> /
00196   if(apath.empty()) return apath;
00197 
00198   G4int lstr= apath.length();
00199 
00200   // for trancating "/"
00201   G4bool Qsla= FALSE;
00202   if(apath[(size_t)(lstr-1)]=='/') Qsla= TRUE;
00203 
00204   // searching last '/' from tail
00205   G4int indx= -1;
00206   for(G4int i=lstr-1; i>=0; i--) {
00207     if(Qsla && apath[(size_t)i]!='/') Qsla= FALSE; // break "/" flag!!
00208     if(apath[(size_t)i]=='/' && !Qsla) {
00209       indx= i;
00210       break;
00211     } 
00212   }
00213 
00214   if(indx==-1) return apath;  // not found
00215 
00216   if(indx==0  && lstr==1) { // "/"
00217     G4String nullStr;
00218     return nullStr;
00219   } else {  
00220     //G4String newPath= apath(indx+1,lstr-indx-1); 
00221     G4String newPath= apath;
00222     newPath= newPath(indx+1,lstr-indx-1);
00223     return newPath;
00224   }
00225 }
00226 
00227 // --------------------------------------------------------------------
00228 //      shell commands
00229 // --------------------------------------------------------------------
00231 void G4VUIshell::ListCommand(const G4String& dir, 
00232                              const G4String& candidate) const
00234 {
00235   // specified directpry
00236   G4String input= dir; // ...
00237   input= input.strip(G4String::both);
00238 
00239   // command tree of "user specified directory"
00240   G4String vpath= currentCommandDir;
00241   G4String vcmd;
00242 
00243   G4int len= input.length();
00244   if(! input.empty()) {
00245     G4int indx= -1;
00246     for(G4int i=len-1; i>=0; i--) { // search last '/'
00247       if(input[(size_t)i]=='/') {
00248         indx= i;
00249         break;
00250       }   
00251     }
00252     // get abs. path
00253     if(indx != -1) vpath= GetAbsCommandDirPath(input(0,indx+1));
00254     if(!(indx==0  && len==1)) vcmd= input(indx+1,len-indx-1); // care for "/"
00255   }
00256 
00257   // check "vcmd" is directory?
00258   G4String inputpath= vpath+vcmd;
00259   if(! vcmd.empty()){
00260     G4String tmpstr= inputpath + "/";
00261     if(GetCommandTree(tmpstr) != NULL) {
00262       vpath= tmpstr;
00263       vcmd= "";
00264     }
00265   }
00266       
00267   // check "vpath" directory exists?
00268   G4UIcommandTree* atree= GetCommandTree(vpath);  
00269   if(atree == NULL) {
00270     G4cout << "<" << input << ">: No such directory" << G4endl;
00271     return;
00272   }
00273 
00274   // list matched directories/commands
00275   G4String stream;
00276   G4bool isMatch= FALSE;
00277 
00278   G4int Ndir= atree-> GetTreeEntry();
00279   G4int Ncmd= atree-> GetCommandEntry();
00280   if(Ndir==0 && Ncmd==0) return;  // no contents
00281   
00282   // directory ...
00283   for(G4int idir=1; idir<=Ndir; idir++) {
00284     if(idir==1 && lsColorFlag) stream+= TermColorString[directoryColor];
00285     G4String fpdir= atree-> GetTree(idir)-> GetPathName();
00286     // matching test
00287     if(candidate.empty()) { // list all
00288       if(vcmd=="" || fpdir==inputpath) {
00289         stream+= GetCommandPathTail(fpdir); stream+= "  ";
00290         isMatch= TRUE;
00291       }
00292     } else { // list only matched with candidate
00293       if( fpdir.index(candidate, 0) == 0) {
00294         stream+= GetCommandPathTail(fpdir); stream+= "  ";
00295       }
00296     }
00297   }
00298   
00299   // command ...
00300   for(G4int icmd=1; icmd<=Ncmd; icmd++){
00301     if(icmd==1 && lsColorFlag) stream+= TermColorString[commandColor];
00302     G4String fpcmd= atree-> GetPathName() +
00303              atree-> GetCommand(icmd) -> GetCommandName();
00304     // matching test
00305     if(candidate.empty()) { // list all
00306       if(vcmd=="" || fpcmd==inputpath) {
00307         stream+= GetCommandPathTail(fpcmd); stream+= "*  ";
00308         isMatch= TRUE;
00309       }
00310     } else {  // list only matched with candidate
00311       if( fpcmd.index(candidate, 0) == 0) {
00312         stream+= GetCommandPathTail(fpcmd); stream+= "*  ";
00313       }
00314     }
00315   }
00316   
00317   // waring : not matched
00318   if(!isMatch && candidate.empty()) 
00319     G4cout << "<" << input 
00320            << ">: No such directory or command" << std::flush;
00321 
00322   // display
00323   G4UIArrayString arrayString(stream);
00324   arrayString.Show(nColumn);
00325 }
00326 

Generated on Mon May 27 17:50:23 2013 for Geant4 by  doxygen 1.4.7