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 "G4Types.hh"
00032 #include "G4StateManager.hh"
00033 #include "G4UIcommandTree.hh"
00034 #include "G4UIcommand.hh"
00035 #include "G4UIcommandStatus.hh"
00036 #include "G4UIterminal.hh"
00037 #include "G4UIcsh.hh"
00038 #include <sstream>
00039
00040
00042 G4UIterminal::G4UIterminal(G4VUIshell* aShell)
00044 {
00045 UI= G4UImanager::GetUIpointer();
00046 UI-> SetSession(this);
00047 UI-> SetCoutDestination(this);
00048
00049 iExit= FALSE;
00050 iCont= FALSE;
00051
00052 if(aShell) shell= aShell;
00053 else shell= new G4UIcsh;
00054 }
00055
00057 G4UIterminal::~G4UIterminal()
00058
00059 {
00060 if(shell) delete shell;
00061
00062 if(G4UImanager::GetUIpointer()) {
00063 UI-> SetSession(NULL);
00064 UI-> SetCoutDestination(NULL);
00065 }
00066 }
00067
00068
00070 void G4UIterminal::SetPrompt(const G4String& prompt)
00071
00072 {
00073 shell-> SetPrompt(prompt);
00074 }
00075
00077 G4UIsession* G4UIterminal::SessionStart()
00079 {
00080 iExit= TRUE;
00081
00082 G4String newCommand= GetCommand();
00083 while(iExit){
00084 ExecuteCommand(newCommand);
00085 newCommand= GetCommand();
00086 }
00087 return NULL;
00088 }
00089
00091 void G4UIterminal::PauseSessionStart(G4String msg)
00093 {
00094 iCont= TRUE;
00095
00096 G4String newCommand= GetCommand(msg);
00097 while(iCont){
00098 ExecuteCommand(newCommand);
00099 newCommand= GetCommand(msg);
00100 }
00101 }
00102
00104 void G4UIterminal::ExecuteCommand(G4String aCommand)
00106 {
00107 if(aCommand.length()<2) return;
00108
00109 G4int returnVal = UI-> ApplyCommand(aCommand);
00110
00111 G4int paramIndex = returnVal % 100;
00112
00113
00114 G4int commandStatus = returnVal - paramIndex;
00115
00116 G4UIcommand* cmd = 0;
00117 if(commandStatus!=fCommandSucceeded)
00118 { cmd = FindCommand(aCommand); }
00119
00120 switch(commandStatus) {
00121 case fCommandSucceeded:
00122 break;
00123 case fCommandNotFound:
00124 G4cerr << "command <" << UI->SolveAlias(aCommand) << "> not found" << G4endl;
00125 if( aCommand.index("@@") != G4String::npos) {
00126 G4cout << "@@G4UIterminal" << G4endl;
00127 }
00128 break;
00129 case fIllegalApplicationState:
00130 G4cerr << "illegal application state -- command refused" << G4endl;
00131 break;
00132 case fParameterOutOfRange:
00133
00134
00135
00136
00137
00138
00139
00140 break;
00141 case fParameterOutOfCandidates:
00142 G4cerr << "Parameter is out of candidate list (index " << paramIndex << ")" << G4endl;
00143 G4cerr << "Candidates : " << cmd->GetParameter(paramIndex)->GetParameterCandidates() << G4endl;
00144 break;
00145 case fParameterUnreadable:
00146 G4cerr << "Parameter is wrong type and/or is not omittable (index " << paramIndex << ")" << G4endl;
00147 break;
00148 case fAliasNotFound:
00149 default:
00150 G4cerr << "command refused (" << commandStatus << ")" << G4endl;
00151 }
00152 }
00153
00155 G4String G4UIterminal::GetCommand(const char* msg)
00157 {
00158 G4String newCommand;
00159 G4String nullString;
00160
00161 newCommand= shell-> GetCommandLine(msg);
00162
00163 G4String nC= newCommand.strip(G4String::leading);
00164 if( nC.length() == 0 ) {
00165 newCommand= nullString;
00166
00167 } else if( nC(0) == '#' ) {
00168 G4cout << nC << G4endl;
00169 newCommand= nullString;
00170
00171 } else if(nC=="ls" || nC(0,3)=="ls " ) {
00172 ListDirectory(nC);
00173 newCommand= nullString;
00174
00175 } else if(nC=="lc" || nC(0,3)=="lc " ) {
00176 shell-> ListCommand(nC.remove(0,2));
00177 newCommand= nullString;
00178
00179 } else if(nC == "pwd") {
00180 G4cout << "Current Command Directory : "
00181 << GetCurrentWorkingDirectory() << G4endl;
00182 newCommand= nullString;
00183
00184 } else if(nC == "cwd") {
00185 shell-> ShowCurrentDirectory();
00186 newCommand= nullString;
00187
00188 } else if(nC == "cd" || nC(0,3) == "cd ") {
00189 ChangeDirectoryCommand(nC);
00190 shell-> SetCurrentDirectory(GetCurrentWorkingDirectory());
00191 newCommand= nullString;
00192
00193 } else if(nC == "help" || nC(0,5) == "help ") {
00194 TerminalHelp(nC);
00195 newCommand= nullString;
00196
00197 } else if(nC(0) == '?') {
00198 ShowCurrent(nC);
00199 newCommand= nullString;
00200
00201 } else if(nC == "hist" || nC == "history") {
00202 G4int nh= UI-> GetNumberOfHistory();
00203 for (G4int i=0; i<nh; i++) {
00204 G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl;
00205 }
00206 newCommand= nullString;
00207
00208 } else if(nC(0) == '!') {
00209 G4String ss= nC(1, nC.length()-1);
00210 G4int vl;
00211 const char* tt= ss;
00212 std::istringstream is(tt);
00213 is >> vl;
00214 G4int nh= UI-> GetNumberOfHistory();
00215 if(vl>=0 && vl<nh) {
00216 newCommand= UI-> GetPreviousCommand(vl);
00217 G4cout << newCommand << G4endl;
00218 } else {
00219 G4cerr << "history " << vl << " is not found." << G4endl;
00220 newCommand= nullString;
00221 }
00222
00223 } else if(nC == "exit") {
00224 if(iCont) {
00225 G4cout << "You are now processing RUN." << G4endl;
00226 G4cout << "Please abort it using \"/run/abort\" command first" << G4endl;
00227 G4cout << " and use \"continue\" command until the application"
00228 << G4endl;
00229 G4cout << " becomes to Idle." << G4endl;
00230 } else {
00231 iExit= FALSE;
00232 newCommand= nullString;
00233 }
00234
00235 } else if( nC == "cont" || nC == "continue"){
00236 iCont= FALSE;
00237 newCommand= nullString;
00238
00239 } else if( nC.empty() ){
00240 newCommand= nullString;
00241
00242 } else {
00243 }
00244
00245 return ModifyToFullPathCommand(newCommand);
00246 }
00247
00248
00250 G4int G4UIterminal::ReceiveG4cout(G4String coutString)
00252 {
00253 std::cout << coutString << std::flush;
00254 return 0;
00255 }
00256
00258 G4int G4UIterminal::ReceiveG4cerr(G4String cerrString)
00260 {
00261 std::cerr << cerrString << std::flush;
00262 return 0;
00263 }
00264
00266 G4bool G4UIterminal::GetHelpChoice(G4int& aInt)
00268 {
00269 G4cin >> aInt;
00270 if(!G4cin.good()){
00271 G4cin.clear();
00272 G4cin.ignore(30,'\n');
00273 return FALSE;
00274 }
00275 return TRUE;
00276 }
00277
00279 void G4UIterminal::ExitHelp()
00281 {
00282 char temp[100];
00283 G4cin.getline(temp, 100);
00284 }
00285