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

Last change on this file since 3460 was 3410, checked in by ansari, 18 years ago

Correction Usage() -h + commentaires doxygen - Reza 26/11/2007

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