ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4UIterminal.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4UIterminal.cc
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 //
28 // ====================================================================
29 // G4UIterminal.cc
30 //
31 // ====================================================================
32 #include "G4Types.hh"
33 #include "G4StateManager.hh"
34 #include "G4UIcommandTree.hh"
35 #include "G4UIcommand.hh"
36 #include "G4UIcommandStatus.hh"
37 #include "G4UIterminal.hh"
38 #include "G4UIcsh.hh"
39 #include <sstream>
40 
41 #ifndef WIN32
42 #include <signal.h>
43 #endif
44 
45 // ====================================================================
46 // signal handler for soft-abort
47 // ====================================================================
48 
50 
51 #ifndef WIN32
52 
53 extern "C" {
54 
56 static void SignalHandler(G4int)
58 {
60  G4ApplicationState state= stateManager-> GetCurrentState();
61 
62  if(state==G4State_GeomClosed || state==G4State_EventProc) {
63  G4cout << "aborting Run ...";
64  G4UImanager::GetUIpointer()->ApplyCommand("/run/abort");
65  G4cout << G4endl;
66  } else {
67  G4cout << G4endl
68  << "Session terminated." << G4endl;
69  theshell-> ResetTerminal();
70  G4Exception("G4UIterminal::SignalHandler()",
71  "UI0001",
73  "KeyboardInterrput with Ctrl-C");
74  }
75 
76  // for original Unix / System V
77  signal(SIGINT, SignalHandler);
78 }
79 
80 }
81 #endif
82 
83 // ====================================================================
84 //
85 // class description
86 //
87 // ====================================================================
88 
92 {
94  UI-> SetSession(this);
95  UI-> SetCoutDestination(this);
96 
97  iExit= FALSE;
98  iCont= FALSE;
99 
100  if(aShell) shell= aShell;
101  else shell= new G4UIcsh;
102  theshell= shell; // locally stored for the signal handler
103 
104  // add signal handler
105  if(qsig) {
106 #ifndef WIN32
107  signal(SIGINT, SignalHandler);
108 #endif
109  }
110 }
111 
114 
115 {
116  if(shell) delete shell;
117 
119  UI-> SetSession(NULL);
120  UI-> SetCoutDestination(NULL);
121  }
122 }
123 
124 
126 void G4UIterminal::SetPrompt(const G4String& prompt)
127 
128 {
129  shell-> SetPrompt(prompt);
130 }
131 
135 {
136  iExit= TRUE;
137 
138  G4String newCommand= GetCommand();
139  while(iExit){
140  ExecuteCommand(newCommand);
141  newCommand= GetCommand();
142  }
143  return NULL;
144 }
145 
149 {
150  iCont= TRUE;
151 
152  G4String newCommand= GetCommand(msg);
153  while(iCont){
154  ExecuteCommand(newCommand);
155  newCommand= GetCommand(msg);
156  }
157 }
158 
162 {
163  if(aCommand.length()<2) return;
164 
165  G4int returnVal = UI-> ApplyCommand(aCommand);
166 
167  G4int paramIndex = returnVal % 100;
168  // 0 - 98 : paramIndex-th parameter is invalid
169  // 99 : convination of parameters is invalid
170  G4int commandStatus = returnVal - paramIndex;
171 
172  G4UIcommand* cmd = 0;
173  if(commandStatus!=fCommandSucceeded)
174  { cmd = FindCommand(aCommand); }
175 
176  switch(commandStatus) {
177  case fCommandSucceeded:
178  break;
179  case fCommandNotFound:
180  G4cerr << "command <" << UI->SolveAlias(aCommand) << "> not found" << G4endl;
181  if( aCommand.index("@@") != G4String::npos) {
182  G4cout << "@@G4UIterminal" << G4endl;
183  }
184  break;
186  G4cerr << "illegal application state -- command refused" << G4endl;
187  break;
189  // if(paramIndex<99) {
190  // G4cerr << "Parameter is out of range (index " << paramIndex << ")" << G4endl;
191  // G4cerr << "Allowed range : " << cmd->GetParameter(paramIndex)->GetParameterRange() << G4endl;
192  // } else {
193  // G4cerr << "Parameter is out of range" << G4endl;
194  // G4cerr << "Allowed range : " << cmd->GetRange() << G4endl;
195  // }
196  break;
198  G4cerr << "Parameter is out of candidate list (index " << paramIndex << ")" << G4endl;
199  G4cerr << "Candidates : " << cmd->GetParameter(paramIndex)->GetParameterCandidates() << G4endl;
200  break;
202  G4cerr << "Parameter is wrong type and/or is not omittable (index " << paramIndex << ")" << G4endl;
203  break;
204  case fAliasNotFound:
205  default:
206  G4cerr << "command refused (" << commandStatus << ")" << G4endl;
207  }
208 }
209 
213 {
214  G4String newCommand;
215  G4String nullString;
216 
217  newCommand= shell-> GetCommandLineString(msg);
218 
219  G4String nC = newCommand.strip(G4String::leading);
220  if( nC.length() == 0 ) {
221  newCommand= nullString;
222 
223  } else if( nC(0) == '#' ) {
224  G4cout << nC << G4endl;
225  newCommand= nullString;
226 
227  } else if(nC=="ls" || nC.substr(0,3)=="ls " ) { // list commands
228  ListDirectory(nC);
229  newCommand= nullString;
230 
231  } else if(nC=="lc" || nC.substr(0,3)=="lc " ) { // ... by shell
232  shell-> ListCommand(nC.remove(0,2));
233  newCommand= nullString;
234 
235  } else if(nC == "pwd") { // show current directory
236  G4cout << "Current Command Directory : "
238  newCommand= nullString;
239 
240  } else if(nC == "cwd") { // ... by shell
241  shell-> ShowCurrentDirectory();
242  newCommand= nullString;
243 
244  } else if(nC == "cd" || nC.substr(0,3) == "cd ") { // "cd"
246  shell-> SetCurrentDirectory(GetCurrentWorkingDirectory());
247  newCommand= nullString;
248 
249  } else if(nC == "help" || nC.substr(0,5) == "help ") { // "help"
250  TerminalHelp(nC);
251  newCommand= nullString;
252 
253  } else if(nC(0) == '?') { // "show current value of a parameter"
254  ShowCurrent(nC);
255  newCommand= nullString;
256 
257  } else if(nC == "hist" || nC == "history") { // "hist/history"
258  G4int nh= UI-> GetNumberOfHistory();
259  for (G4int i=0; i<nh; i++) {
260  G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl;
261  }
262  newCommand= nullString;
263 
264  } else if(nC(0) == '!') { // "!"
265  G4String ss= nC(1, nC.length()-1);
266  G4int vl;
267  const char* tt= ss;
268  std::istringstream is(tt);
269  is >> vl;
270  G4int nh= UI-> GetNumberOfHistory();
271  if(vl>=0 && vl<nh) {
272  newCommand= UI-> GetPreviousCommand(vl);
273  G4cout << newCommand << G4endl;
274  } else {
275  G4cerr << "history " << vl << " is not found." << G4endl;
276  newCommand= nullString;
277  }
278 
279  } else if(nC == "exit") { // "exit"
280  if(iCont) {
281  G4cout << "You are now processing RUN." << G4endl;
282  G4cout << "Please abort it using \"/run/abort\" command first" << G4endl;
283  G4cout << " and use \"continue\" command until the application"
284  << G4endl;
285  G4cout << " becomes to Idle." << G4endl;
286  } else {
287  iExit= FALSE;
288  newCommand= nullString;
289  }
290 
291  } else if( nC == "cont" || nC == "continue"){ // "cont/continu"
292  iCont= FALSE;
293  newCommand= nullString;
294 
295  } else if( nC.empty() ){ // NULL command
296  newCommand= nullString;
297 
298  } else {
299  }
300 
301  return ModifyToFullPathCommand(newCommand);
302 }
303 
304 
308 {
309  std::cout << coutString << std::flush;
310  return 0;
311 }
312 
316 {
317  std::cerr << cerrString << std::flush;
318  return 0;
319 }
320 
324 {
325  G4cin >> aInt;
326  if(!G4cin.good()){
327  G4cin.clear();
328  G4cin.ignore(30,'\n');
329  return FALSE;
330  }
331  return TRUE;
332 }
333 
337 {
338  char temp[100];
339  G4cin.getline(temp, 100);
340 }
341