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

Last change on this file since 836 was 593, checked in by garnier, 17 years ago

r627@mac-90108: laurentgarnier | 2007-11-09 07:57:42 +0100
modif dans les includes directives

File size: 9.9 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.27 2007/10/29 18:49:45 asaim Exp $
28// GEANT4 tag $Name:  $
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    exit(0);
73  }
74
75  // for original Unix / System V
76  signal(SIGINT, SignalHandler);
77}
78
79}
80#endif
81
82// ====================================================================
83//
84// class description
85//
86// ====================================================================
87
88///////////////////////////////////////////////////////////
89G4UIterminal::G4UIterminal(G4VUIshell* aShell, G4bool qsig)
90///////////////////////////////////////////////////////////
91{
92  UI= G4UImanager::GetUIpointer();
93  UI-> SetSession(this);
94  UI-> SetCoutDestination(this);
95
96  iExit= FALSE;
97  iCont= FALSE;
98
99  if(aShell) shell= aShell;
100  else shell= new G4UIcsh;
101  theshell= shell; // locally stored for the signal handler
102
103  // add signal handler
104  if(qsig) {
105#ifndef WIN32
106  signal(SIGINT, SignalHandler);
107#endif
108  }
109}
110
111/////////////////////////////
112G4UIterminal::~G4UIterminal() 
113/////////////////////////////
114{ 
115  if(shell) delete shell;
116
117  if(G4UImanager::GetUIpointer()) {
118    UI-> SetSession(NULL);
119    UI-> SetCoutDestination(NULL);
120  }
121}
122
123
124////////////////////////////////////////////////////
125void G4UIterminal::SetPrompt(const G4String& prompt) 
126////////////////////////////////////////////////////
127{
128  shell-> SetPrompt(prompt);
129}
130
131/////////////////////////////////////////
132G4UIsession* G4UIterminal::SessionStart()
133/////////////////////////////////////////
134{
135  iExit= TRUE;
136
137  G4String newCommand= GetCommand();
138  while(iExit){
139    ExecuteCommand(newCommand);
140    newCommand= GetCommand();
141  }
142  return NULL;
143}
144
145//////////////////////////////////////////////////
146void G4UIterminal::PauseSessionStart(G4String msg)
147//////////////////////////////////////////////////
148{
149  iCont= TRUE;
150
151  G4String newCommand= GetCommand(msg);
152  while(iCont){
153    ExecuteCommand(newCommand);
154    newCommand= GetCommand(msg);
155  }
156}
157
158////////////////////////////////////////////////////
159void G4UIterminal::ExecuteCommand(G4String aCommand)
160////////////////////////////////////////////////////
161{
162  if(aCommand.length()<2) return;
163
164  G4int returnVal = UI-> ApplyCommand(aCommand);
165
166  G4int paramIndex = returnVal % 100;
167  // 0 - 98 : paramIndex-th parameter is invalid
168  // 99     : convination of parameters is invalid
169  G4int commandStatus = returnVal - paramIndex;
170
171  G4UIcommand* cmd = 0;
172  if(commandStatus!=fCommandSucceeded)
173  { cmd = FindCommand(aCommand); }
174
175  switch(commandStatus) {
176  case fCommandSucceeded:
177    break;
178  case fCommandNotFound:
179    G4cerr << "command <" << UI->SolveAlias(aCommand) << "> not found" << G4endl;
180    if( aCommand.index("@@") != G4String::npos) {
181      G4cout << "@@G4UIterminal" << G4endl;
182    }
183    break;
184  case fIllegalApplicationState:
185    G4cerr << "illegal application state -- command refused" << G4endl;
186    break;
187  case fParameterOutOfRange:
188    // if(paramIndex<99) {
189    //   G4cerr << "Parameter is out of range (index " << paramIndex << ")" << G4endl;
190    //   G4cerr << "Allowed range : " << cmd->GetParameter(paramIndex)->GetParameterRange() << G4endl;
191    // } else {
192    //   G4cerr << "Parameter is out of range" << G4endl;
193    //   G4cerr << "Allowed range : " << cmd->GetRange() << G4endl;
194    // }
195    break;
196  case fParameterOutOfCandidates:
197    G4cerr << "Parameter is out of candidate list (index " << paramIndex << ")" << G4endl;
198    G4cerr << "Candidates : " << cmd->GetParameter(paramIndex)->GetParameterCandidates() << G4endl;
199    break;
200  case fParameterUnreadable:
201    G4cerr << "Parameter is wrong type and/or is not omittable (index " << paramIndex << ")" << G4endl;
202    break;
203  case fAliasNotFound:
204  default:
205    G4cerr << "command refused (" << commandStatus << ")" << G4endl;
206  }
207}
208
209///////////////////////////////////
210G4String G4UIterminal::GetCommand(const char* msg)
211///////////////////////////////////
212{
213  G4String newCommand;
214  G4String nullString;
215
216  newCommand= shell-> GetCommandLine(msg);
217
218  G4String nC= newCommand.strip(G4String::leading);
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(0,3)=="ls " ) {  // list commands
227    ListDirectory(nC); 
228    newCommand= nullString;
229
230  } else if(nC=="lc" || nC(0,3)=="lc " ) {  // ... by shell
231    shell-> ListCommand(nC.remove(0,2)); 
232    newCommand= nullString;
233
234  } else if(nC == "pwd") { // show current directory
235    G4cout << "Current Command Directory : " 
236           << GetCurrentWorkingDirectory() << G4endl; 
237    newCommand= nullString;
238
239  } else if(nC == "cwd") { // ... by shell
240    shell-> ShowCurrentDirectory();
241    newCommand= nullString;
242
243  } else if(nC == "cd" || nC(0,3) == "cd ") {  // "cd"
244    ChangeDirectoryCommand(nC); 
245    shell-> SetCurrentDirectory(GetCurrentWorkingDirectory());
246    newCommand= nullString;
247
248  } else if(nC == "help" || nC(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(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//////////////////////////////////////////////////////
305G4int G4UIterminal::ReceiveG4cout(G4String coutString)
306//////////////////////////////////////////////////////
307{
308  std::cout << coutString << std::flush;
309  return 0;
310}
311
312//////////////////////////////////////////////////////
313G4int G4UIterminal::ReceiveG4cerr(G4String cerrString)
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()
335/////////////////////////////
336{
337  char temp[100];
338  G4cin.getline(temp, 100);
339}
340
Note: See TracBrowser for help on using the repository browser.