| 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 
 | 
|---|
| 20 | extern "C" {
 | 
|---|
| 21 | void sopiamodule_init();
 | 
|---|
| 22 | void sopiamodule_end();
 | 
|---|
| 23 | void skymapmodule_init();
 | 
|---|
| 24 | void skymapmodule_end();
 | 
|---|
| 25 | void fitsbtadapter_init();
 | 
|---|
| 26 | void fitsbtadapter_end();
 | 
|---|
| 27 | void W2PSModule_init();
 | 
|---|
| 28 | void 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 
 | 
|---|
| 52 | SOPHYA 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 | 
 | 
|---|
| 73 | int 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 | 
 | 
|---|
| 101 | int main(int narg, char *arg[])
 | 
|---|
| 102 | {
 | 
|---|
| 103 | int ofa;
 | 
|---|
| 104 | bool fgfpe, fgsegv, fgred, fghidezsw, fgexec, fgsmall, fgtermrd;
 | 
|---|
| 105 | 
 | 
|---|
| 106 | SkyTInitiator skyinit;
 | 
|---|
| 107 |    
 | 
|---|
| 108 | InitTim();
 | 
|---|
| 109 | 
 | 
|---|
| 110 | if(narg>1) 
 | 
|---|
| 111 |   for(int jh=1;jh<narg;jh++) if(strcmp(arg[jh],"-h") == 0) return Usage(false);
 | 
|---|
| 112 | 
 | 
|---|
| 113 | ofa = 1;
 | 
|---|
| 114 | fgred = fgfpe = fgsegv = true;
 | 
|---|
| 115 | fghidezsw = false;
 | 
|---|
| 116 | fgexec = false;
 | 
|---|
| 117 | bool fgtmp = false;
 | 
|---|
| 118 | fgsmall = false;
 | 
|---|
| 119 |  fgtermrd = false;
 | 
|---|
| 120 | string tmpdir;
 | 
|---|
| 121 | string exfc;
 | 
|---|
| 122 | // Pour fabriquer le help
 | 
|---|
| 123 | bool fgtexh = false;
 | 
|---|
| 124 | int ka;
 | 
|---|
| 125 | for(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 |  
 | 
|---|
| 152 | if (fgexec) printf(">>>>> Starting piapp , Executing %s \n", exfc.c_str()); 
 | 
|---|
| 153 | else printf(">>>>> Starting piapp <<<<< \n");
 | 
|---|
| 154 | printf("Version: piapp=%g  PI=%g  SOPHYA=%g \n", (double)PIAPP_VERSIONNUMBER, 
 | 
|---|
| 155 |               (double)PI_VERSIONNUMBER, (double)skyinit.Version());
 | 
|---|
| 156 | 
 | 
|---|
| 157 | char* vcmds=NULL;
 | 
|---|
| 158 | string vcmd;
 | 
|---|
| 159 | if (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 | 
 | 
|---|
| 167 | char* tmpde = getenv("TMPDIR");
 | 
|---|
| 168 | char  tmpdname[32];
 | 
|---|
| 169 | if (tmpde == NULL) tmpde = "./";
 | 
|---|
| 170 | if (tmpde[strlen(tmpde)-1] != '/')
 | 
|---|
| 171 |   strcpy(tmpdname, "/PIATmp_XXXXXX"); 
 | 
|---|
| 172 | else 
 | 
|---|
| 173 |   strcpy(tmpdname, "PIATmp_XXXXXX"); 
 | 
|---|
| 174 | mktemp(tmpdname);
 | 
|---|
| 175 | tmpdir = tmpde;
 | 
|---|
| 176 | tmpdir += tmpdname;
 | 
|---|
| 177 | tmpdir += '/';
 | 
|---|
| 178 | vcmd = "mkdir ";
 | 
|---|
| 179 | vcmd += tmpde;
 | 
|---|
| 180 | vcmd += tmpdname;
 | 
|---|
| 181 | cout << " --piapp: Creating Tmp Directory: " << tmpdir << endl;
 | 
|---|
| 182 | int rcc = system(vcmd.c_str());
 | 
|---|
| 183 | if (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 | 
 | 
|---|
| 191 | PIStdImgApp * app = new PIStdImgApp(fgsmall, narg, arg);
 | 
|---|
| 192 | // cout << " DBG-2 " <<  app->ObjMgr()->GetTmpDir() << endl;
 | 
|---|
| 193 | cout << "  NamedObjMgr::SetTmpDir()+XNTuple::SetSwapPath() " << tmpdir << endl;
 | 
|---|
| 194 | app->ObjMgr()->SetTmpDir(tmpdir);
 | 
|---|
| 195 | XNTuple::SetSwapPath(const_cast<char *>(tmpdir.c_str()));
 | 
|---|
| 196 | 
 | 
|---|
| 197 | 
 | 
|---|
| 198 | 
 | 
|---|
| 199 | // Gestion de redirection stdout/err et Signaux
 | 
|---|
| 200 | if (fgred) app->RedirectStdOutErr(true);
 | 
|---|
| 201 | else app->RedirectStdOutErr(false);
 | 
|---|
| 202 | app->CatchSignals(fgfpe, fgsegv);
 | 
|---|
| 203 | 
 | 
|---|
| 204 | // if Hide Zoom/Stat Win
 | 
|---|
| 205 | if (fghidezsw)  app->StatZoomWindowSetVisible(false);
 | 
|---|
| 206 | 
 | 
|---|
| 207 | // S'il y a un fichier de commande a executer
 | 
|---|
| 208 | // if (fgexec) app->CmdInterpreter()->Interpret(exfc);
 | 
|---|
| 209 | if (fgexec) app->SubmitCommand(exfc);
 | 
|---|
| 210 | 
 | 
|---|
| 211 | // On charge le module sopiamodule
 | 
|---|
| 212 | sopiamodule_init();
 | 
|---|
| 213 | // On charge le module skymapmodule
 | 
|---|
| 214 | skymapmodule_init();
 | 
|---|
| 215 | // On charge le module de lecture ligne a ligne des Fits BINARY/ASCII tables
 | 
|---|
| 216 | fitsbtadapter_init();
 | 
|---|
| 217 | // On charge le module des commandes de creation de fichier postscript
 | 
|---|
| 218 | W2PSModule_init();
 | 
|---|
| 219 | 
 | 
|---|
| 220 | // S'il y a besoin de faire le fichier Help
 | 
|---|
| 221 | if (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)
 | 
|---|
| 230 | PIACmdReader cmdrdr(app);
 | 
|---|
| 231 | if (fgtermrd) cmdrdr.start();
 | 
|---|
| 232 | 
 | 
|---|
| 233 | int rc = 0;
 | 
|---|
| 234 | bool cont = true;
 | 
|---|
| 235 | char rep; // ans[32]
 | 
|---|
| 236 | int excnt = 0;
 | 
|---|
| 237 | while(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 | }
 | 
|---|
| 292 | cout << "\n ------------------------------------------------ \n"
 | 
|---|
| 293 |      << " piapp: Cleaning up ... " << endl;
 | 
|---|
| 294 | 
 | 
|---|
| 295 | app->RedirectStdOutErr(false);  
 | 
|---|
| 296 | // -- ca fait planter sur linux -- Reza 04/2006 if (fgtermrd) cmdrdr.cancel();
 | 
|---|
| 297 | 
 | 
|---|
| 298 | // On de-charge le module sopiamodule et fitsbtadapter
 | 
|---|
| 299 | sopiamodule_end();
 | 
|---|
| 300 | skymapmodule_end();
 | 
|---|
| 301 | fitsbtadapter_end();
 | 
|---|
| 302 | W2PSModule_end();
 | 
|---|
| 303 | 
 | 
|---|
| 304 | delete app;
 | 
|---|
| 305 | if (fgtmp) delete[] vcmds;
 | 
|---|
| 306 | 
 | 
|---|
| 307 | vcmd = "rm -rf ";
 | 
|---|
| 308 | vcmd += tmpdir;
 | 
|---|
| 309 | cout << " --piapp: Removing Tmp Directory: " << tmpdir << endl;
 | 
|---|
| 310 | rcc = system(vcmd.c_str());
 | 
|---|
| 311 | if (rcc != 0) {
 | 
|---|
| 312 |   cout << " --piapp: Error deleting TmpDir " << vcmd << endl;
 | 
|---|
| 313 |   return(9);
 | 
|---|
| 314 | }
 | 
|---|
| 315 | 
 | 
|---|
| 316 | cout << "------------------------------------------------ \n"
 | 
|---|
| 317 |      << "-------- piapp : Exiting .... Rc= " << rc << " -----------\n"
 | 
|---|
| 318 |      << "------------------------------------------------ " << endl; 
 | 
|---|
| 319 | return(rc);
 | 
|---|
| 320 | }
 | 
|---|
| 321 | 
 | 
|---|