Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4UIterminal.cc
Go to the documentation of this file.
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// $Id$
28//
29// ====================================================================
30// G4UIterminal.cc
31//
32// ====================================================================
33#include "G4Types.hh"
34#include "G4StateManager.hh"
35#include "G4UIcommandTree.hh"
36#include "G4UIcommand.hh"
37#include "G4UIcommandStatus.hh"
38#include "G4UIterminal.hh"
39#include "G4UIcsh.hh"
40#include <sstream>
41
42#ifndef WIN32
43#include <signal.h>
44#endif
45
46// ====================================================================
47// signal handler for soft-abort
48// ====================================================================
49
50static G4VUIshell* theshell= 0;
51
52#ifndef WIN32
53
54extern "C" {
55
56////////////////////////////////
57static void SignalHandler(G4int)
58////////////////////////////////
59{
61 G4ApplicationState state= stateManager-> GetCurrentState();
62
63 if(state==G4State_GeomClosed || state==G4State_EventProc) {
64 G4cout << "aborting Run ...";
66 G4cout << G4endl;
67 } else {
68 G4cout << G4endl
69 << "Session terminated." << G4endl;
70 theshell-> ResetTerminal();
71 G4Exception("G4UIterminal::SignalHandler()",
72 "UI0001",
74 "KeyboardInterrput with Ctrl-C");
75 }
76
77 // for original Unix / System V
78 signal(SIGINT, SignalHandler);
79}
80
81}
82#endif
83
84// ====================================================================
85//
86// class description
87//
88// ====================================================================
89
90///////////////////////////////////////////////////////////
92///////////////////////////////////////////////////////////
93{
95 UI-> SetSession(this);
96 UI-> SetCoutDestination(this);
97
98 iExit= FALSE;
99 iCont= FALSE;
100
101 if(aShell) shell= aShell;
102 else shell= new G4UIcsh;
103 theshell= shell; // locally stored for the signal handler
104
105 // add signal handler
106 if(qsig) {
107#ifndef WIN32
108 signal(SIGINT, SignalHandler);
109#endif
110 }
111}
112
113/////////////////////////////
115/////////////////////////////
116{
117 if(shell) delete shell;
118
120 UI-> SetSession(NULL);
121 UI-> SetCoutDestination(NULL);
122 }
123}
124
125
126////////////////////////////////////////////////////
128////////////////////////////////////////////////////
129{
130 shell-> SetPrompt(prompt);
131}
132
133/////////////////////////////////////////
135/////////////////////////////////////////
136{
137 iExit= TRUE;
138
139 G4String newCommand= GetCommand();
140 while(iExit){
141 ExecuteCommand(newCommand);
142 newCommand= GetCommand();
143 }
144 return NULL;
145}
146
147/////////////////////////////////////////////////////////
149/////////////////////////////////////////////////////////
150{
151 iCont= TRUE;
152
153 G4String newCommand= GetCommand(msg);
154 while(iCont){
155 ExecuteCommand(newCommand);
156 newCommand= GetCommand(msg);
157 }
158}
159
160///////////////////////////////////////////////////////////
161void G4UIterminal::ExecuteCommand(const G4String& aCommand)
162///////////////////////////////////////////////////////////
163{
164 if(aCommand.length()<2) return;
165
166 G4int returnVal = UI-> ApplyCommand(aCommand);
167
168 G4int paramIndex = returnVal % 100;
169 // 0 - 98 : paramIndex-th parameter is invalid
170 // 99 : convination of parameters is invalid
171 G4int commandStatus = returnVal - paramIndex;
172
173 G4UIcommand* cmd = 0;
174 if(commandStatus!=fCommandSucceeded)
175 { cmd = FindCommand(aCommand); }
176
177 switch(commandStatus) {
179 break;
180 case fCommandNotFound:
181 G4cerr << "command <" << UI->SolveAlias(aCommand) << "> not found" << G4endl;
182 if( aCommand.index("@@") != G4String::npos) {
183 G4cout << "@@G4UIterminal" << G4endl;
184 }
185 break;
187 G4cerr << "illegal application state -- command refused" << G4endl;
188 break;
190 // if(paramIndex<99) {
191 // G4cerr << "Parameter is out of range (index " << paramIndex << ")" << G4endl;
192 // G4cerr << "Allowed range : " << cmd->GetParameter(paramIndex)->GetParameterRange() << G4endl;
193 // } else {
194 // G4cerr << "Parameter is out of range" << G4endl;
195 // G4cerr << "Allowed range : " << cmd->GetRange() << G4endl;
196 // }
197 break;
199 G4cerr << "Parameter is out of candidate list (index " << paramIndex << ")" << G4endl;
200 G4cerr << "Candidates : " << cmd->GetParameter(paramIndex)->GetParameterCandidates() << G4endl;
201 break;
203 G4cerr << "Parameter is wrong type and/or is not omittable (index " << paramIndex << ")" << G4endl;
204 break;
205 case fAliasNotFound:
206 default:
207 G4cerr << "command refused (" << commandStatus << ")" << G4endl;
208 }
209}
210
211//////////////////////////////////////////////////
212G4String G4UIterminal::GetCommand(const char* msg)
213//////////////////////////////////////////////////
214{
215 G4String newCommand;
216 G4String nullString;
217
218 newCommand= shell-> GetCommandLineString(msg);
219
220 G4String nC = newCommand.strip(G4String::leading);
221 if( nC.length() == 0 ) {
222 newCommand= nullString;
223
224 } else if( nC(0) == '#' ) {
225 G4cout << nC << G4endl;
226 newCommand= nullString;
227
228 } else if(nC=="ls" || nC(0,3)=="ls " ) { // list commands
229 ListDirectory(nC);
230 newCommand= nullString;
231
232 } else if(nC=="lc" || nC(0,3)=="lc " ) { // ... by shell
233 shell-> ListCommand(nC.remove(0,2));
234 newCommand= nullString;
235
236 } else if(nC == "pwd") { // show current directory
237 G4cout << "Current Command Directory : "
239 newCommand= nullString;
240
241 } else if(nC == "cwd") { // ... by shell
242 shell-> ShowCurrentDirectory();
243 newCommand= nullString;
244
245 } else if(nC == "cd" || nC(0,3) == "cd ") { // "cd"
247 shell-> SetCurrentDirectory(GetCurrentWorkingDirectory());
248 newCommand= nullString;
249
250 } else if(nC == "help" || nC(0,5) == "help ") { // "help"
251 TerminalHelp(nC);
252 newCommand= nullString;
253
254 } else if(nC(0) == '?') { // "show current value of a parameter"
255 ShowCurrent(nC);
256 newCommand= nullString;
257
258 } else if(nC == "hist" || nC == "history") { // "hist/history"
259 G4int nh= UI-> GetNumberOfHistory();
260 for (G4int i=0; i<nh; i++) {
261 G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl;
262 }
263 newCommand= nullString;
264
265 } else if(nC(0) == '!') { // "!"
266 G4String ss= nC(1, nC.length()-1);
267 G4int vl;
268 const char* tt= ss;
269 std::istringstream is(tt);
270 is >> vl;
271 G4int nh= UI-> GetNumberOfHistory();
272 if(vl>=0 && vl<nh) {
273 newCommand= UI-> GetPreviousCommand(vl);
274 G4cout << newCommand << G4endl;
275 } else {
276 G4cerr << "history " << vl << " is not found." << G4endl;
277 newCommand= nullString;
278 }
279
280 } else if(nC == "exit") { // "exit"
281 if(iCont) {
282 G4cout << "You are now processing RUN." << G4endl;
283 G4cout << "Please abort it using \"/run/abort\" command first" << G4endl;
284 G4cout << " and use \"continue\" command until the application"
285 << G4endl;
286 G4cout << " becomes to Idle." << G4endl;
287 } else {
288 iExit= FALSE;
289 newCommand= nullString;
290 }
291
292 } else if( nC == "cont" || nC == "continue"){ // "cont/continu"
293 iCont= FALSE;
294 newCommand= nullString;
295
296 } else if( nC.empty() ){ // NULL command
297 newCommand= nullString;
298
299 } else {
300 }
301
302 return ModifyToFullPathCommand(newCommand);
303}
304
305
306/////////////////////////////////////////////////////////////
308/////////////////////////////////////////////////////////////
309{
310 std::cout << coutString << std::flush;
311 return 0;
312}
313
314/////////////////////////////////////////////////////////////
316/////////////////////////////////////////////////////////////
317{
318 std::cerr << cerrString << std::flush;
319 return 0;
320}
321
322///////////////////////////////////////////////
323G4bool G4UIterminal::GetHelpChoice(G4int& aInt)
324///////////////////////////////////////////////
325{
326 G4cin >> aInt;
327 if(!G4cin.good()){
328 G4cin.clear();
329 G4cin.ignore(30,'\n');
330 return FALSE;
331 }
332 return TRUE;
333}
334
335///////////////////////////////////
336void G4UIterminal::ExitHelp() const
337///////////////////////////////////
338{
339 char temp[100];
340 G4cin.getline(temp, 100);
341}
342
G4ApplicationState
@ G4State_EventProc
@ G4State_GeomClosed
@ FatalException
int G4int
Definition: G4Types.hh:66
bool G4bool
Definition: G4Types.hh:67
@ fParameterOutOfCandidates
@ fCommandNotFound
@ fAliasNotFound
@ fIllegalApplicationState
@ fParameterUnreadable
@ fCommandSucceeded
@ fParameterOutOfRange
#define G4endl
Definition: G4ios.hh:52
#define G4cin
Definition: G4ios.hh:51
G4DLLIMPORT std::ostream G4cerr
G4DLLIMPORT std::ostream G4cout
static G4StateManager * GetStateManager()
G4String & remove(str_size)
str_size index(const char *, G4int pos=0) const
G4String strip(G4int strip_Type=trailing, char c=' ')
G4UIparameter * GetParameter(G4int i) const
Definition: G4UIcommand.hh:140
G4int ApplyCommand(const char *aCommand)
Definition: G4UImanager.cc:369
G4String GetPreviousCommand(G4int i) const
Definition: G4UImanager.hh:222
G4String SolveAlias(const char *aCmd)
Definition: G4UImanager.cc:316
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:51
G4String GetParameterCandidates() const
void SetPrompt(const G4String &prompt)
virtual G4int ReceiveG4cout(const G4String &coutString)
G4UIterminal(G4VUIshell *aShell=0, G4bool qsig=true)
Definition: G4UIterminal.cc:91
virtual void PauseSessionStart(const G4String &msg)
virtual G4UIsession * SessionStart()
virtual G4int ReceiveG4cerr(const G4String &cerrString)
G4UIcommand * FindCommand(const char *commandName) const
G4String ModifyToFullPathCommand(const char *aCommandLine) const
void ShowCurrent(const G4String &) const
void TerminalHelp(const G4String &)
void ListDirectory(const G4String &) const
G4String GetCurrentWorkingDirectory() const
void ChangeDirectoryCommand(const G4String &)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
#define TRUE
Definition: globals.hh:55
#define FALSE
Definition: globals.hh:52
#define const
Definition: zconf.h:118