Geant4 11.1.1
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//
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
49static G4ThreadLocal G4VUIshell* theshell= 0;
50
51#ifndef WIN32
52
53extern "C" {
54
55////////////////////////////////
56static void SignalHandler(G4int)
57////////////////////////////////
58{
60 G4ApplicationState state= stateManager-> GetCurrentState();
61
62 if(state==G4State_GeomClosed || state==G4State_EventProc) {
63 G4cout << "aborting Run ...";
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
89///////////////////////////////////////////////////////////
91///////////////////////////////////////////////////////////
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
112/////////////////////////////
114/////////////////////////////
115{
116 if(shell) delete shell;
117
119 UI-> SetSession(NULL);
120 UI-> SetCoutDestination(NULL);
121 }
122}
123
124
125////////////////////////////////////////////////////
127////////////////////////////////////////////////////
128{
129 shell-> SetPrompt(prompt);
130}
131
132/////////////////////////////////////////
134/////////////////////////////////////////
135{
136 iExit= TRUE;
137
138 G4String newCommand= GetCommand();
139 while(iExit){
140 ExecuteCommand(newCommand);
141 newCommand= GetCommand();
142 }
143 return NULL;
144}
145
146/////////////////////////////////////////////////////////
148/////////////////////////////////////////////////////////
149{
150 iCont= TRUE;
151
152 G4String newCommand= GetCommand(msg);
153 while(iCont){
154 ExecuteCommand(newCommand);
155 newCommand= GetCommand(msg);
156 }
157}
158
159///////////////////////////////////////////////////////////
160void G4UIterminal::ExecuteCommand(const G4String& aCommand)
161///////////////////////////////////////////////////////////
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) {
178 break;
179 case fCommandNotFound:
180 G4cerr << "command <" << UI->SolveAlias(aCommand) << "> not found" << G4endl;
181 if( aCommand.find("@@") != 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
210//////////////////////////////////////////////////
211G4String G4UIterminal::GetCommand(const char* msg)
212//////////////////////////////////////////////////
213{
214 G4String newCommand = shell->GetCommandLineString(msg);
215 G4String nullString = "";
216
217 G4String nC = G4StrUtil::lstrip_copy(newCommand);
218
219 if( nC.length() == 0 ) {
220 newCommand= nullString;
221
222 } else if( nC[0] == '#' ) {
223 G4cout << nC << G4endl;
224 newCommand= nullString;
225
226 } else if(nC=="ls" || nC.substr(0,3)=="ls " ) { // list commands
227 ListDirectory(nC);
228 newCommand= nullString;
229
230 } else if(nC=="lc" || nC.substr(0,3)=="lc " ) { // ... by shell
231 shell-> ListCommand(nC.erase(0,2));
232 newCommand= nullString;
233
234 } else if(nC == "pwd") { // show current directory
235 G4cout << "Current Command Directory : "
237 newCommand= nullString;
238
239 } else if(nC == "cwd") { // ... by shell
240 shell-> ShowCurrentDirectory();
241 newCommand= nullString;
242
243 } else if(nC == "cd" || nC.substr(0,3) == "cd ") { // "cd"
245 shell-> SetCurrentDirectory(GetCurrentWorkingDirectory());
246 newCommand= nullString;
247
248 } else if(nC == "help" || nC.substr(0,5) == "help ") { // "help"
249 TerminalHelp(nC);
250 newCommand= nullString;
251
252 } else if(nC[0] == '?') { // "show current value of a parameter"
253 ShowCurrent(nC);
254 newCommand= nullString;
255
256 } else if(nC == "hist" || nC == "history") { // "hist/history"
257 G4int nh= UI-> GetNumberOfHistory();
258 for (G4int i=0; i<nh; i++) {
259 G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl;
260 }
261 newCommand= nullString;
262
263 } else if(nC[0] == '!') { // "!"
264 G4String ss= nC.substr(1, nC.length()-1);
265 G4int vl;
266 const char* tt= ss;
267 std::istringstream is(tt);
268 is >> vl;
269 G4int nh= UI-> GetNumberOfHistory();
270 if(vl>=0 && vl<nh) {
271 newCommand= UI-> GetPreviousCommand(vl);
272 G4cout << newCommand << G4endl;
273 } else {
274 G4cerr << "history " << vl << " is not found." << G4endl;
275 newCommand= nullString;
276 }
277
278 } else if(nC == "exit") { // "exit"
279 if(iCont) {
280 G4cout << "You are now processing RUN." << G4endl;
281 G4cout << "Please abort it using \"/run/abort\" command first" << G4endl;
282 G4cout << " and use \"continue\" command until the application"
283 << G4endl;
284 G4cout << " becomes to Idle." << G4endl;
285 } else {
286 iExit= FALSE;
287 newCommand= nullString;
288 }
289
290 } else if( nC == "cont" || nC == "continue"){ // "cont/continu"
291 iCont= FALSE;
292 newCommand= nullString;
293
294 } else if( nC.empty() ){ // NULL command
295 newCommand= nullString;
296
297 } else {
298 }
299
300 return ModifyToFullPathCommand(newCommand);
301}
302
303
304/////////////////////////////////////////////////////////////
306/////////////////////////////////////////////////////////////
307{
308 std::cout << coutString << std::flush;
309 return 0;
310}
311
312/////////////////////////////////////////////////////////////
314/////////////////////////////////////////////////////////////
315{
316 std::cerr << cerrString << std::flush;
317 return 0;
318}
319
320///////////////////////////////////////////////
321G4bool G4UIterminal::GetHelpChoice(G4int& aInt)
322///////////////////////////////////////////////
323{
324 G4cin >> aInt;
325 if(!G4cin.good()){
326 G4cin.clear();
327 G4cin.ignore(30,'\n');
328 return FALSE;
329 }
330 return TRUE;
331}
332
333///////////////////////////////////
334void G4UIterminal::ExitHelp() const
335///////////////////////////////////
336{
337 char temp[100];
338 G4cin.getline(temp, 100);
339}
340
G4ApplicationState
@ G4State_EventProc
@ G4State_GeomClosed
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:59
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
@ fParameterOutOfCandidates
@ fCommandNotFound
@ fAliasNotFound
@ fIllegalApplicationState
@ fParameterUnreadable
@ fCommandSucceeded
@ fParameterOutOfRange
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
#define G4cin
Definition: G4ios.hh:56
static G4StateManager * GetStateManager()
G4UIparameter * GetParameter(G4int i) const
Definition: G4UIcommand.hh:140
G4int ApplyCommand(const char *aCommand)
Definition: G4UImanager.cc:495
G4String GetPreviousCommand(G4int i) const
Definition: G4UImanager.hh:202
G4String SolveAlias(const char *aCmd)
Definition: G4UImanager.cc:430
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:77
const G4String & GetParameterCandidates() const
void SetPrompt(const G4String &prompt)
virtual G4int ReceiveG4cout(const G4String &coutString)
G4UIterminal(G4VUIshell *aShell=0, G4bool qsig=true)
Definition: G4UIterminal.cc:90
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 &)
virtual G4String GetCommandLineString(const char *msg=0)=0
#define TRUE
Definition: globals.hh:41
#define FALSE
Definition: globals.hh:38
G4String lstrip_copy(G4String str, char ch=' ')
Return copy of string with leading characters removed.
#define G4ThreadLocal
Definition: tls.hh:77