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

Last change on this file since 2949 was 2949, checked in by ansari, 19 years ago

ajout flag -term a piapp.cc , Reza 27/4/2006

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