source: Sophya/trunk/SophyaPI/ProgPI/piapp.cc@ 4029

Last change on this file since 4029 was 3572, checked in by cmv, 17 years ago

char* -> const char* pour regler les problemes de deprecated string const... + comparaison unsigned signed + suppression EVOL_PLANCK rz+cmv 07/02/2009

File size: 9.8 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <unistd.h>
4#include <iostream>
5
6#include "sopnamsp.h"
7#include "pistdimgapp.h"
8#include "piacmd.h"
9#include "piacmdrdr.h"
10#include "piversion.h"
11#include "piaversion.h"
12
13#include "timing.h"
14#include "skyinit.h"
15
16
17// ---- Pour charger automatiquement le module sopiamodule
18extern "C" {
19void sopiamodule_init();
20void sopiamodule_end();
21void skymapmodule_init();
22void skymapmodule_end();
23void fitsbtadapter_init();
24void fitsbtadapter_end();
25void W2PSModule_init();
26void W2PSModule_end();
27}
28
29/*!
30 \defgroup ProgPI ProgPI module
31 This module contains programs for interactive data analysis and
32 visualisation, based on SOPHYA class libray and PI (GUI framework)
33 and PIext (Interactive data analysis framework).
34*/
35
36/*!
37 \ingroup ProgPI
38 \file piapp.cc
39 \brief \b spiapp: Starts the piapp interactive data analysis program.
40
41 This interactive data analysis program uses the SOPHYA libray,
42 the PI GUI library. It has multiple execution threads and a
43 c-shell inspired command execution interpreter.
44 See the piapp user manual for more information.
45 The SOPHYA piapp executable is called \b spiapp.
46
47 \verbatim
48 csh> spiapp -h
49 PIOPersist::Initialize() Starting Sophya Persistence management service
50SOPHYA Version 2.1 Revision 0 (V_Nov2007) -- Nov 24 2007 13:08:58 cxx
51
52 piapp: Interactive data analysis and visualisation program
53 Usage: piapp [-nored] [-doublered] [-termread] [-term]
54 [-hidezswin] [-small] [-nosig] [-nosigfpe] [-nosigsegv]
55 [-tmpdir TmpDirectory] [-help2tex] [-exec file [args]]
56 -nored : Don't redirect stdout/stderr to piapp console
57 -doublered : Redirect stdout/stderr to piapp console AND the terminal
58 -termread : Read commands on terminal (stdin)
59 -term : equivalent to -nored -termread -small
60 -hidezswin : Hide Zoom/Stat/ColMap window
61 -small : Create small size main piapp window
62 -nosig : Don't catch SigFPE, SigSEGV
63 -nosigfpe -nosigsegv: Don t catch SigFPE / SigSEGV
64 -tmpdir TmpDirectory: defines TMDIR for temporary files
65 -help2tex: Create a LaTeX help file (piahelp.tex)
66 -exec file [args] : Execute command file (last option)
67
68 \endverbatim
69
70*/
71
72int Usage(bool fgerr)
73{
74 if (fgerr) {
75 cout << " piapp : Argument Error ! piapp -h for Usage" << endl;
76 return 1;
77 }
78 else {
79 cout << "\n piapp: Interactive data analysis and visualisation program \n"
80 << " Usage: piapp [-nored] [-doublered] [-termread] [-term] \n"
81 << " [-hidezswin] [-small] [-nosig] [-nosigfpe] [-nosigsegv] \n"
82 << " [-tmpdir TmpDirectory] [-help2tex] [-exec file [args]] \n"
83 << " -nored : Don't redirect stdout/stderr to piapp console\n"
84 << " -doublered : Redirect stdout/stderr to piapp console AND the terminal \n"
85 << " -termread : Read commands on terminal (stdin)\n"
86 << " -term : equivalent to -nored -termread -small \n"
87 << " -hidezswin : Hide Zoom/Stat/ColMap window \n"
88 << " -small : Create small size main piapp window \n"
89 << " -nosig : Don't catch SigFPE, SigSEGV \n"
90 << " -nosigfpe -nosigsegv: Don t catch SigFPE / SigSEGV \n"
91 << " -tmpdir TmpDirectory: defines TMDIR for temporary files \n"
92 << " -help2tex: Create a LaTeX help file (piahelp.tex)\n"
93 << " -exec file [args] : Execute command file (last option)\n"
94 << endl;
95 return 0;
96 }
97}
98
99/* ================================ MAIN() ================================= */
100
101int main(int narg, char *arg[])
102{
103int ofa;
104bool fgfpe, fgsegv, fghidezsw, fgexec, fgsmall, fgtermrd;
105RED_OUTERR_FLG fgred = CONSRED_OE;
106
107SkyTInitiator skyinit;
108
109InitTim();
110
111if(narg>1)
112 for(int jh=1;jh<narg;jh++) if(strcmp(arg[jh],"-h") == 0) return Usage(false);
113
114ofa = 1;
115fgred = CONSRED_OE;
116fgfpe = fgsegv = true;
117fghidezsw = false;
118fgexec = false;
119bool fgtmp = false;
120fgsmall = false;
121 fgtermrd = false;
122string tmpdir;
123string exfc;
124// Pour fabriquer le help
125bool fgtexh = false;
126int ka;
127for(ka=1; ka<narg; ka++) {
128 if (strcmp(arg[ka],"-nored") == 0) fgred=NORED_OE;
129 else if (strcmp(arg[ka],"-doublered") == 0) fgred=DOUBLERED_OE;
130 else if (strcmp(arg[ka],"-nosig") == 0) fgfpe=fgsegv=false;
131 else if (strcmp(arg[ka],"-termread") == 0) fgtermrd=true;
132 else if (strcmp(arg[ka],"-term") == 0)
133 { fgred=NORED_OE; fgtermrd=true; fgsmall=true; }
134 else if (strcmp(arg[ka],"-nosigfpe") == 0) fgfpe=false;
135 else if (strcmp(arg[ka],"-nosigsegv") == 0) fgsegv=false;
136 else if (strcmp(arg[ka],"-small") == 0) fgsmall=true;
137 else if (strcmp(arg[ka],"-hidezswin") == 0) fghidezsw=true;
138 else if (strcmp(arg[ka],"-help2tex") == 0) { fgtexh=true; fgred=NORED_OE; }
139 else if (strcmp(arg[ka],"-tmpdir") == 0) {
140 if (ka == narg-1) return Usage(true);
141 fgtmp = true; ka++;
142 tmpdir = arg[ka];
143 }
144 else if (strcmp(arg[ka],"-exec") == 0) {
145 if (ka < narg-1) {
146 fgexec = true;
147 exfc = "exec";
148 for(int kaa=ka+1; kaa<narg; kaa++) { exfc += ' '; exfc += arg[kaa]; }
149 }
150 break;
151 }
152 }
153
154
155if (fgexec) printf(">>>>> Starting piapp , Executing %s \n", exfc.c_str());
156else printf(">>>>> Starting piapp <<<<< \n");
157
158SophyaInitiator::PrintVersion(true); // SOPHYA version and module list
159
160// Reza , Jan07
161// On cree l'objet PIApplication avant de faire mkdir TmpDir
162// En cas de pb X11 , il n'y a pas de repertoire PIATmpxxx qui traine apres
163
164PIStdImgApp * app = new PIStdImgApp(fgsmall, narg, arg);
165// cout << " DBG-2 " << app->ObjMgr()->GetTmpDir() << endl;
166
167char* vcmds=NULL;
168string vcmd;
169if (fgtmp) {
170 if (tmpdir[tmpdir.length()-1] != '/') tmpdir += '/'; // Necessaire pour SetSwapPath
171 vcmd = "TMPDIR=" + tmpdir;
172 vcmds = new char[vcmd.length()+1];
173 strcpy(vcmds, vcmd.c_str());
174 putenv(vcmds);
175}
176
177const char* tmpde = getenv("TMPDIR");
178char tmpdname[32];
179if (tmpde == NULL) tmpde = "./";
180if (tmpde[strlen(tmpde)-1] != '/')
181 strcpy(tmpdname, "/PIATmp_XXXXXX");
182else
183 strcpy(tmpdname, "PIATmp_XXXXXX");
184mktemp(tmpdname);
185tmpdir = tmpde;
186tmpdir += tmpdname;
187tmpdir += '/';
188vcmd = "mkdir ";
189vcmd += tmpde;
190vcmd += tmpdname;
191cout << " --piapp: Creating Tmp Directory: " << tmpdir << endl;
192int rcc = system(vcmd.c_str());
193if (rcc != 0) {
194 cout << " --piapp: Error creating TmpDir " << vcmd << " ---> exit !" << endl;
195 return(9);
196}
197
198cout << " NamedObjMgr::SetTmpDir() " << tmpdir << endl;
199app->ObjMgr()->SetTmpDir(tmpdir);
200
201// Gestion de redirection stdout/err et Signaux
202app->RedirectStdOutErr(fgred);
203app->CatchSignals(fgfpe, fgsegv);
204
205// if Hide Zoom/Stat Win
206if (fghidezsw) app->StatZoomWindowSetVisible(false);
207
208// S'il y a un fichier de commande a executer
209// if (fgexec) app->CmdInterpreter()->Interpret(exfc);
210if (fgexec) app->SubmitCommand(exfc);
211
212// On charge le module sopiamodule
213sopiamodule_init();
214// On charge le module skymapmodule
215skymapmodule_init();
216// On charge le module de lecture ligne a ligne des Fits BINARY/ASCII tables
217fitsbtadapter_init();
218// On charge le module des commandes de creation de fichier postscript
219W2PSModule_init();
220
221// S'il y a besoin de faire le fichier Help
222if (fgtexh) {
223 string thf = "piahelp.tex";
224 app->CmdInterpreter()->HelptoLaTeX(thf);
225 printf("piapp : Help file piahelp.tex created --> exit(0) \n");
226 delete app;
227 exit(0);
228}
229
230// Creation du lecteur de commande sur terminal (avec GNU readline)
231PIACmdReader cmdrdr(app);
232if (fgtermrd) cmdrdr.start();
233
234int rc = 0;
235bool cont = true;
236char rep; // ans[32]
237int excnt = 0;
238while(cont) {
239 rc = 0; cont = false;
240 app->SetReady();
241 app->RedirectStdOutErr(fgred);
242 try {
243 app->Run();
244 }
245 catch(PThrowable exc) { // Catching SOPHYA exceptions
246 app->Stop();
247 app->RedirectStdOutErr(NORED_OE);
248 //BUG ? fait planter OSF-cxx fflush(stdout);
249 cout << endl; cerr << endl;
250 cout << "\n piapp/main() PThrowable catched ! " << exc.Msg() << endl;
251 cout << ++excnt << "- continue <CR>, Close Windows, "
252 << " continue C<CR> Stop program S<CR> ? " << endl;
253 rep = getchar(); // was: gets(ans);
254 rep = toupper(rep);
255 if (rep == 'S') {
256 cout << " !!!! Stopping piapp !!!! " << endl;
257 rc = 78;
258 cont = false;
259 }
260 else {
261 if (rep == 'C') {
262 cout << "piapp/main() Closing all windows ... " << endl;
263 app->CloseAllWindows();
264 }
265 cout << " *** piapp - Continuing event loop *** " << endl;
266 cont = true;
267 }
268 }
269 catch(...) { // Catching all other exceptions
270 app->Stop();
271 app->RedirectStdOutErr(NORED_OE);
272 cout << endl; cerr << endl;
273 cout << "\n piapp/main() exception catched ! " << endl;
274 cout << ++excnt << "- continue <CR>, Close Windows, "
275 << " continue C<CR> Stop program S<CR> ? " << endl;
276 rep = getchar(); // gets(ans);
277 rep = toupper(rep);
278 if (rep == 'S') {
279 cout << " !!!! Stopping piapp !!!! " << endl;
280 rc = 78;
281 cont = false;
282 }
283 else {
284 if (rep == 'C') {
285 cout << "piapp/main() Closing all windows ... " << endl;
286 app->CloseAllWindows();
287 }
288 cout << " *** piapp - Continuing event loop *** " << endl;
289 cont = true;
290 }
291 }
292}
293cout << "\n ------------------------------------------------ \n"
294 << " piapp: Cleaning up ... " << endl;
295app->RedirectStdOutErr(NORED_OE);
296cout << "\n ------------------------------------------------ \n"
297 << " piapp: Exiting - Cleaning up ... " << endl;
298
299// -- ca fait planter sur linux -- Reza 04/2006 if (fgtermrd) cmdrdr.cancel();
300
301// On de-charge le module sopiamodule et fitsbtadapter
302sopiamodule_end();
303skymapmodule_end();
304fitsbtadapter_end();
305W2PSModule_end();
306
307delete app;
308if (fgtmp) delete[] vcmds;
309
310vcmd = "rm -rf ";
311vcmd += tmpdir;
312cout << " --piapp: Removing Tmp Directory: " << tmpdir << endl;
313rcc = system(vcmd.c_str());
314if (rcc != 0) {
315 cout << " --piapp: Error deleting TmpDir " << vcmd << endl;
316 return(9);
317}
318
319cout << "------------------------------------------------ \n"
320 << "-------- piapp : Exiting .... Rc= " << rc << " -----------\n"
321 << "------------------------------------------------ " << endl;
322return(rc);
323}
324
Note: See TracBrowser for help on using the repository browser.