00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "G4UImanager.hh"
00032 #include "G4UIcommand.hh"
00033 #include "G4UIcommandTree.hh"
00034 #include "G4StateManager.hh"
00035 #include "G4UIcommandStatus.hh"
00036 #include "G4VUIshell.hh"
00037 #include "G4UIArrayString.hh"
00038
00039
00040 static const G4String strESC= '\033';
00041 static const G4String TermColorString[8] ={
00042 strESC+"[30m", strESC+"[31m", strESC+"[32m", strESC+"[33m",
00043 strESC+"[34m", strESC+"[35m", strESC+"[36m", strESC+"[37m"
00044 };
00045
00047 G4VUIshell::G4VUIshell(const G4String& prompt)
00048 : promptSetting(prompt), promptString(""), nColumn(80),
00049 lsColorFlag(FALSE), directoryColor(BLACK), commandColor(BLACK),
00050 currentCommandDir("/")
00052 {
00053 }
00054
00056 G4VUIshell::~G4VUIshell()
00058 {
00059 }
00060
00062 void G4VUIshell::MakePrompt(const char* msg)
00063
00064 {
00065 if(promptSetting.length()<=1) {
00066 promptString= promptSetting;
00067 return;
00068 }
00069
00070 promptString="";
00071 G4int i;
00072 for(i=0; i<G4int(promptSetting.length())-1; i++){
00073 if(promptSetting[(size_t)i]=='%'){
00074 switch (promptSetting[(size_t)(i+1)]) {
00075 case 's':
00076 {
00077 G4String stateStr;
00078 if(msg)
00079 { stateStr = msg; }
00080 else
00081 {
00082 G4StateManager* statM= G4StateManager::GetStateManager();
00083 stateStr= statM-> GetStateString(statM->GetCurrentState());
00084 }
00085 promptString.append(stateStr);
00086 i++;
00087 }
00088 break;
00089 case '/':
00090 promptString.append(currentCommandDir);
00091 i++;
00092 break;
00093 default:
00094 promptString.append(G4String(promptSetting[(size_t)i]));
00095 break;
00096 }
00097 } else {
00098 promptString.append(G4String(promptSetting[(size_t)i]));
00099 }
00100 }
00101
00102
00103 if(i == G4int(promptSetting.length())-1)
00104 promptString.append(G4String(promptSetting[(size_t)i]));
00105 }
00106
00107
00108
00109
00110
00112 G4UIcommandTree* G4VUIshell::GetCommandTree(const G4String& input) const
00114 {
00115 G4UImanager* UI= G4UImanager::GetUIpointer();
00116
00117 G4UIcommandTree* cmdTree= UI-> GetTree();
00118
00119 G4String absPath= input;
00120 absPath= GetAbsCommandDirPath(absPath.strip(G4String::both));
00121
00122
00123 if(absPath.length()==0) return NULL;
00124 if(absPath[absPath.length()-1] != '/') return NULL;
00125 if(absPath=="/") return cmdTree;
00126
00127 for(G4int indx=1; indx<G4int(absPath.length())-1; ) {
00128 G4int jslash= absPath.index("/", indx);
00129 if(jslash != G4int(G4String::npos)) {
00130 if(cmdTree != NULL)
00131 cmdTree= cmdTree-> GetTree(G4String(absPath(0,jslash+1)));
00132 }
00133 indx= jslash+1;
00134 }
00135
00136 if(cmdTree == NULL) return NULL;
00137 else return cmdTree;
00138 }
00139
00141 G4String G4VUIshell::GetAbsCommandDirPath(const G4String& apath) const
00143 {
00144 if(apath.empty()) return apath;
00145
00146
00147
00148 G4String bpath= apath;
00149 if(apath[(size_t)0] != '/') bpath= currentCommandDir + apath;
00150
00151
00152 G4String absPath= "/";
00153 for(G4int indx=1; indx<=G4int(bpath.length())-1; ) {
00154 G4int jslash= bpath.index("/", indx);
00155 if(jslash != G4int(G4String::npos)) {
00156 if(bpath(indx,jslash-indx) == ".."){
00157 if(absPath.length() >=1) {
00158 absPath.remove(absPath.length()-1);
00159 G4int jpre= absPath.last('/');
00160 if(jpre != G4int(G4String::npos)) absPath.remove(jpre+1);
00161 }
00162 } else if(bpath(indx,jslash-indx) == "."){
00163 } else {
00164 if( !(jslash==indx && bpath(indx)=='/') )
00165 absPath+= bpath(indx, jslash-indx+1);
00166
00167 }
00168 } else {
00169 }
00170 indx= jslash+1;
00171 }
00172 return absPath;
00173 }
00174
00175
00177 G4String G4VUIshell::GetCommandPathTail(const G4String& apath) const
00179 {
00180 if(apath.empty()) return apath;
00181
00182 G4int lstr= apath.length();
00183
00184
00185 G4bool Qsla= FALSE;
00186 if(apath[(size_t)(lstr-1)]=='/') Qsla= TRUE;
00187
00188
00189 G4int indx= -1;
00190 for(G4int i=lstr-1; i>=0; i--) {
00191 if(Qsla && apath[(size_t)i]!='/') Qsla= FALSE;
00192 if(apath[(size_t)i]=='/' && !Qsla) {
00193 indx= i;
00194 break;
00195 }
00196 }
00197
00198 if(indx==-1) return apath;
00199
00200 if(indx==0 && lstr==1) {
00201 G4String nullStr;
00202 return nullStr;
00203 } else {
00204
00205 G4String newPath= apath;
00206 newPath= newPath(indx+1,lstr-indx-1);
00207 return newPath;
00208 }
00209 }
00210
00211
00212
00213
00215 void G4VUIshell::ListCommand(const G4String& dir,
00216 const G4String& candidate) const
00218 {
00219
00220 G4String input= dir;
00221 input= input.strip(G4String::both);
00222
00223
00224 G4String vpath= currentCommandDir;
00225 G4String vcmd;
00226
00227 G4int len= input.length();
00228 if(! input.empty()) {
00229 G4int indx= -1;
00230 for(G4int i=len-1; i>=0; i--) {
00231 if(input[(size_t)i]=='/') {
00232 indx= i;
00233 break;
00234 }
00235 }
00236
00237 if(indx != -1) vpath= GetAbsCommandDirPath(input(0,indx+1));
00238 if(!(indx==0 && len==1)) vcmd= input(indx+1,len-indx-1);
00239 }
00240
00241
00242 G4String inputpath= vpath+vcmd;
00243 if(! vcmd.empty()){
00244 G4String tmpstr= inputpath + "/";
00245 if(GetCommandTree(tmpstr) != NULL) {
00246 vpath= tmpstr;
00247 vcmd= "";
00248 }
00249 }
00250
00251
00252 G4UIcommandTree* atree= GetCommandTree(vpath);
00253 if(atree == NULL) {
00254 G4cout << "<" << input << ">: No such directory" << G4endl;
00255 return;
00256 }
00257
00258
00259 G4String stream;
00260 G4bool isMatch= FALSE;
00261
00262 G4int Ndir= atree-> GetTreeEntry();
00263 G4int Ncmd= atree-> GetCommandEntry();
00264 if(Ndir==0 && Ncmd==0) return;
00265
00266
00267 for(G4int idir=1; idir<=Ndir; idir++) {
00268 if(idir==1 && lsColorFlag) stream+= TermColorString[directoryColor];
00269 G4String fpdir= atree-> GetTree(idir)-> GetPathName();
00270
00271 if(candidate.empty()) {
00272 if(vcmd=="" || fpdir==inputpath) {
00273 stream+= GetCommandPathTail(fpdir); stream+= " ";
00274 isMatch= TRUE;
00275 }
00276 } else {
00277 if( fpdir.index(candidate, 0) == 0) {
00278 stream+= GetCommandPathTail(fpdir); stream+= " ";
00279 }
00280 }
00281 }
00282
00283
00284 for(G4int icmd=1; icmd<=Ncmd; icmd++){
00285 if(icmd==1 && lsColorFlag) stream+= TermColorString[commandColor];
00286 G4String fpcmd= atree-> GetPathName() +
00287 atree-> GetCommand(icmd) -> GetCommandName();
00288
00289 if(candidate.empty()) {
00290 if(vcmd=="" || fpcmd==inputpath) {
00291 stream+= GetCommandPathTail(fpcmd); stream+= "* ";
00292 isMatch= TRUE;
00293 }
00294 } else {
00295 if( fpcmd.index(candidate, 0) == 0) {
00296 stream+= GetCommandPathTail(fpcmd); stream+= "* ";
00297 }
00298 }
00299 }
00300
00301
00302 if(!isMatch && candidate.empty())
00303 G4cout << "<" << input
00304 << ">: No such directory or command" << std::flush;
00305
00306
00307 G4UIArrayString arrayString(stream);
00308 arrayString.Show(nColumn);
00309 }