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