source: trunk/source/interfaces/basic/src/G4UIterminal.cc @ 989

Last change on this file since 989 was 989, checked in by garnier, 15 years ago

fichiers manquants

File size: 10.0 KB
Line 
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: G4UIterminal.cc,v 1.28 2008/07/18 06:38:59 kmura Exp $
28// GEANT4 tag $Name: geant4-09-02-ref-02 $
29//
30// ====================================================================
31//   G4UIterminal.cc
32//
33// ====================================================================
34#include "G4Types.hh"
35#include "G4StateManager.hh"
36#include "G4UIcommandTree.hh"
37#include "G4UIcommand.hh"
38#include "G4UIcommandStatus.hh"
39#include "G4UIterminal.hh"
40#include "G4UIcsh.hh"
41#include <sstream>
42
43#ifndef WIN32
44#include <signal.h>
45#endif
46
47// ====================================================================
48// signal handler for soft-abort
49// ====================================================================
50
51static G4VUIshell* theshell= 0;
52
53#ifndef WIN32
54
55extern "C" {
56
57////////////////////////////////
58static void SignalHandler(G4int)
59////////////////////////////////
60{
61  G4StateManager* stateManager= G4StateManager::GetStateManager();
62  G4ApplicationState state= stateManager-> GetCurrentState();
63
64  if(state==G4State_GeomClosed || state==G4State_EventProc) {
65    G4cout << "aborting Run ...";
66    G4UImanager::GetUIpointer()->ApplyCommand("/run/abort");
67    G4cout << G4endl;
68  } else {
69    G4cout << G4endl
70           << "Session terminated." << G4endl;
71    theshell-> ResetTerminal();
72    G4Exception("G4UIterminal/SignalHandler", "KeyboardInterrupt",
73                FatalException, "Interrupt 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///////////////////////////////////////////////////////////
90G4UIterminal::G4UIterminal(G4VUIshell* aShell, G4bool qsig)
91///////////////////////////////////////////////////////////
92{
93  UI= G4UImanager::GetUIpointer();
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/////////////////////////////
113G4UIterminal::~G4UIterminal() 
114/////////////////////////////
115{ 
116  if(shell) delete shell;
117
118  if(G4UImanager::GetUIpointer()) {
119    UI-> SetSession(NULL);
120    UI-> SetCoutDestination(NULL);
121  }
122}
123
124
125////////////////////////////////////////////////////
126void G4UIterminal::SetPrompt(const G4String& prompt) 
127////////////////////////////////////////////////////
128{
129  shell-> SetPrompt(prompt);
130}
131
132/////////////////////////////////////////
133G4UIsession* G4UIterminal::SessionStart()
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//////////////////////////////////////////////////
147void G4UIterminal::PauseSessionStart(G4String msg)
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(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) {
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;
185  case fIllegalApplicationState:
186    G4cerr << "illegal application state -- command refused" << G4endl;
187    break;
188  case fParameterOutOfRange:
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;
197  case fParameterOutOfCandidates:
198    G4cerr << "Parameter is out of candidate list (index " << paramIndex << ")" << G4endl;
199    G4cerr << "Candidates : " << cmd->GetParameter(paramIndex)->GetParameterCandidates() << G4endl;
200    break;
201  case fParameterUnreadable:
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;
215  G4String nullString;
216
217  newCommand= shell-> GetCommandLine(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(0,3)=="ls " ) {  // list commands
228    ListDirectory(nC); 
229    newCommand= nullString;
230
231  } else if(nC=="lc" || nC(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 : " 
237           << GetCurrentWorkingDirectory() << G4endl; 
238    newCommand= nullString;
239
240  } else if(nC == "cwd") { // ... by shell
241    shell-> ShowCurrentDirectory();
242    newCommand= nullString;
243
244  } else if(nC == "cd" || nC(0,3) == "cd ") {  // "cd"
245    ChangeDirectoryCommand(nC); 
246    shell-> SetCurrentDirectory(GetCurrentWorkingDirectory());
247    newCommand= nullString;
248
249  } else if(nC == "help" || nC(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
305//////////////////////////////////////////////////////
306G4int G4UIterminal::ReceiveG4cout(G4String coutString)
307//////////////////////////////////////////////////////
308{
309  std::cout << coutString << std::flush;
310  return 0;
311}
312
313//////////////////////////////////////////////////////
314G4int G4UIterminal::ReceiveG4cerr(G4String cerrString)
315//////////////////////////////////////////////////////
316{
317  std::cerr << cerrString << std::flush;
318  return 0;
319}
320
321///////////////////////////////////////////////
322G4bool G4UIterminal::GetHelpChoice(G4int& aInt)
323///////////////////////////////////////////////
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
334/////////////////////////////
335void G4UIterminal::ExitHelp()
336/////////////////////////////
337{
338  char temp[100];
339  G4cin.getline(temp, 100);
340}
341
Note: See TracBrowser for help on using the repository browser.