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 #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
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':
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 '/':
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
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
00116
00118 G4UIcommandTree* G4VUIshell::GetCommandTree(const G4String& input) const
00120 {
00121 G4UImanager* UI= G4UImanager::GetUIpointer();
00122
00123 G4UIcommandTree* cmdTree= UI-> GetTree();
00124
00125 G4String absPath= input;
00126 absPath= GetAbsCommandDirPath(absPath.strip(G4String::both));
00127
00128
00129 if(absPath.length()==0) return NULL;
00130 if(absPath[absPath.length()-1] != '/') return NULL;
00131 if(absPath=="/") return cmdTree;
00132
00133 for(G4int indx=1; indx<G4int(absPath.length())-1; ) {
00134 G4int jslash= absPath.index("/", indx);
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;
00151
00152
00153
00154 G4String bpath= apath;
00155 if(apath[(size_t)0] != '/') bpath= currentCommandDir + apath;
00156
00157
00158 G4String absPath= "/";
00159 for(G4int indx=1; indx<=G4int(bpath.length())-1; ) {
00160 G4int jslash= bpath.index("/", indx);
00161 if(indx == jslash) {
00162 indx++;
00163 continue;
00164 }
00165 if(jslash != G4int(G4String::npos)) {
00166 if(bpath.substr(indx,jslash-indx) == ".."){
00167 if(absPath == "/") {
00168 indx = jslash+1;
00169 continue;
00170 }
00171 if(absPath.length() >= 2) {
00172 absPath.remove(absPath.length()-1);
00173 G4int jpre= absPath.last('/');
00174 if(jpre != G4int(G4String::npos)) absPath.remove(jpre+1);
00175 }
00176 } else if(bpath.substr(indx,jslash-indx) == "."){
00177 } else {
00178 if( !(jslash==indx && bpath(indx)=='/') )
00179 absPath+= bpath(indx, jslash-indx+1);
00180
00181 }
00182 indx= jslash+1;
00183 } else {
00184 break;
00185 }
00186 }
00187
00188 return absPath;
00189 }
00190
00191
00193 G4String G4VUIshell::GetCommandPathTail(const G4String& apath) const
00195 {
00196 if(apath.empty()) return apath;
00197
00198 G4int lstr= apath.length();
00199
00200
00201 G4bool Qsla= FALSE;
00202 if(apath[(size_t)(lstr-1)]=='/') Qsla= TRUE;
00203
00204
00205 G4int indx= -1;
00206 for(G4int i=lstr-1; i>=0; i--) {
00207 if(Qsla && apath[(size_t)i]!='/') Qsla= FALSE;
00208 if(apath[(size_t)i]=='/' && !Qsla) {
00209 indx= i;
00210 break;
00211 }
00212 }
00213
00214 if(indx==-1) return apath;
00215
00216 if(indx==0 && lstr==1) {
00217 G4String nullStr;
00218 return nullStr;
00219 } else {
00220
00221 G4String newPath= apath;
00222 newPath= newPath(indx+1,lstr-indx-1);
00223 return newPath;
00224 }
00225 }
00226
00227
00228
00229
00231 void G4VUIshell::ListCommand(const G4String& dir,
00232 const G4String& candidate) const
00234 {
00235
00236 G4String input= dir;
00237 input= input.strip(G4String::both);
00238
00239
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--) {
00247 if(input[(size_t)i]=='/') {
00248 indx= i;
00249 break;
00250 }
00251 }
00252
00253 if(indx != -1) vpath= GetAbsCommandDirPath(input(0,indx+1));
00254 if(!(indx==0 && len==1)) vcmd= input(indx+1,len-indx-1);
00255 }
00256
00257
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
00268 G4UIcommandTree* atree= GetCommandTree(vpath);
00269 if(atree == NULL) {
00270 G4cout << "<" << input << ">: No such directory" << G4endl;
00271 return;
00272 }
00273
00274
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;
00281
00282
00283 for(G4int idir=1; idir<=Ndir; idir++) {
00284 if(idir==1 && lsColorFlag) stream+= TermColorString[directoryColor];
00285 G4String fpdir= atree-> GetTree(idir)-> GetPathName();
00286
00287 if(candidate.empty()) {
00288 if(vcmd=="" || fpdir==inputpath) {
00289 stream+= GetCommandPathTail(fpdir); stream+= " ";
00290 isMatch= TRUE;
00291 }
00292 } else {
00293 if( fpdir.index(candidate, 0) == 0) {
00294 stream+= GetCommandPathTail(fpdir); stream+= " ";
00295 }
00296 }
00297 }
00298
00299
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
00305 if(candidate.empty()) {
00306 if(vcmd=="" || fpcmd==inputpath) {
00307 stream+= GetCommandPathTail(fpcmd); stream+= "* ";
00308 isMatch= TRUE;
00309 }
00310 } else {
00311 if( fpcmd.index(candidate, 0) == 0) {
00312 stream+= GetCommandPathTail(fpcmd); stream+= "* ";
00313 }
00314 }
00315 }
00316
00317
00318 if(!isMatch && candidate.empty())
00319 G4cout << "<" << input
00320 << ">: No such directory or command" << std::flush;
00321
00322
00323 G4UIArrayString arrayString(stream);
00324 arrayString.Show(nColumn);
00325 }
00326