source: Sophya/trunk/SophyaPI/PIext/piacmd.cc@ 2672

Last change on this file since 2672 was 2672, checked in by ansari, 20 years ago

Ajout de la commande breakexe - pour arreter l'execution de commande par l'interpreteur ds PIACmd et ajout impression pour SaveFits ds nobjmgr.cc - Reza 18/4/2005

File size: 9.6 KB
RevLine 
[2615]1#include "sopnamsp.h"
[293]2#include "piacmd.h"
[165]3#include <stdio.h>
4#include <stdlib.h>
[349]5#include <ctype.h>
[165]6#include <math.h>
7
[293]8#include "basexecut.h"
[165]9
[293]10#include "pdlmgr.h"
[165]11#include "ctimer.h"
[2214]12#include "strutil.h"
[2215]13#include "strutilxx.h"
[293]14// #include "dlftypes.h"
[2307]15#ifdef SANS_EVOLPLANCK
[2308]16#include "nbrandom.h"
[2307]17#else
18#include "srandgen.h"
19#endif
[165]20
[293]21#include "pistdimgapp.h"
[165]22#include "nobjmgr.h"
[361]23#include "piafitting.h"
[463]24#include "pawexecut.h"
[1224]25#include "cxxexecutor.h"
[1251]26#include "cxxexecwin.h"
[1828]27#include "contmodex.h"
[1920]28#include "flowmodex.h"
[165]29
[293]30#include PISTDWDG_H
31#include PILIST_H
[165]32
[293]33// ------------------------------------------------------------
[349]34// Gestion d'une fenetre d'aide interactive
35// Classe PIAHelpWind
[293]36// ------------------------------------------------------------
[165]37
[2474]38#define HGRPMSGOFFSET 100000
39
[293]40class PIAHelpWind : public PIWindow {
41public :
42 PIAHelpWind(PIStdImgApp* par, PIACmd* piacmd);
43 virtual ~PIAHelpWind();
[330]44 virtual void Show();
[293]45 virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
[330]46 inline void AddHelpGroup(const char * hgrp, int gid)
[2474]47 { hgrpom->AppendItem(hgrp, HGRPMSGOFFSET+gid); }
[2465]48 inline void AddHelpGroup(string const & hgrp, int gid)
[2474]49 { hgrpom->AppendItem(hgrp.c_str(), HGRPMSGOFFSET+gid); }
[330]50 inline void ClearHelpList()
51 { mNitem=0; hitemlist->DeleteAllItems(); }
[293]52 inline void AddHelpItem(const char * hitem)
[330]53 { mNitem++; hitemlist->AppendItem(hitem, 100+mNitem); }
[2465]54 inline void SetHelpTextLabel(string const & htxt, string const & hlab)
55 { mTxt->SetText(htxt); mLab->SetLabel(hlab); }
56
[293]57protected :
58 PIStdImgApp* dap;
59 PIACmd* piac;
60 int mNitem;
61 PIList* hitemlist;
[330]62 PIOptMenu* hgrpom;
[293]63 PIButton * mBut;
64 PILabel * mLab;
65 PIText* mTxt;
66};
[165]67
[293]68/* --Methode-- */
69PIAHelpWind::PIAHelpWind(PIStdImgApp *par, PIACmd* piacmd)
70 : PIWindow((PIMsgHandler *)par, "Help-PIApp", PIWK_normal, 400, 300, 100, 350)
71{
72dap = par;
73piac = piacmd;
74mNitem = 0;
[330]75SetMsg(77);
[165]76
[293]77int bsx, bsy;
78int tsx, tsy;
79int spx, spy;
80PIApplicationPrefCompSize(bsx, bsy);
81spx = bsx/6; spy = bsy/6;
82tsx = 10*bsx+2*spx; tsy = 7*bsy+3*spy;
83SetSize(tsx,tsy);
[330]84hgrpom = new PIOptMenu(this, "hgrpoptmen", bsx*2.0, bsy, spx/2, spy);
85hgrpom->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
86hitemlist = new PIList(this, "hitemlist", bsx*2.0, tsy-3*spy-bsy, spx/2, 2*spy+bsy);
[293]87hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
88// hitemlist->SetBorderWidth(2);
[324]89mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
90// mTxt->SetMutiLineMode(true);
[293]91mTxt->SetTextEditable(false);
92mTxt->SetText("");
93mTxt->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
94mLab = new PILabel(this, "helpitem", bsx*4, bsy, bsx*2.5+2*spx, tsy-spy-bsy);
95mLab->SetBorderWidth(1);
96mLab->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
97mLab->SetLabel("");
[330]98mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
[293]99mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
100}
[165]101
102/* --Methode-- */
[293]103PIAHelpWind::~PIAHelpWind()
104{
[330]105delete hgrpom;
[293]106delete hitemlist;
107delete mTxt;
108delete mLab;
109delete mBut;
110}
111
112/* --Methode-- */
113void PIAHelpWind::Process(PIMessage msg, PIMsgHandler* sender, void* /*data*/)
114{
115PIMessage um = UserMsg(msg);
[330]116if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) {
[293]117 Hide();
118 return;
119 }
[2474]120else if ( (um >= HGRPMSGOFFSET) && (sender == hgrpom)) { // Selection de groupe de Help
[330]121 mTxt->SetText("");
122 mLab->SetLabel("");
[2474]123 piac->UpdateHelpList(this, um-HGRPMSGOFFSET);
[330]124}
125else if ( (um > 100) && (sender == hitemlist) && (ModMsg(msg) == PIMsg_Select) ) {
[293]126 string s = hitemlist->GetSelectionStr();
127 mTxt->SetText(piac->GetUsage(s));
128 mLab->SetLabel(s);
129 }
130}
131
[330]132/* --Methode-- */
133void PIAHelpWind::Show()
134{
[2474]135hgrpom->SetValue(HGRPMSGOFFSET); // Groupe All
[330]136mTxt->SetText("");
137mLab->SetLabel("");
138piac->UpdateHelpList(this, 0);
139PIWindow::Show();
140}
[293]141
[349]142
143// ------------------------------------------------------------
144// Classe PIACmd
145// ------------------------------------------------------------
146
[2463]147// static PIACmd* curpiacmd = NULL;
[293]148/* --Methode-- */
[2490]149PIACmd::PIACmd(PIStdImgApp* app)
[2463]150 : Commander()
[165]151{
[2490]152mObjMgr = new NamedObjMgr;
[165]153mImgApp = app;
[293]154helpwin = new PIAHelpWind(app, this);
[2465]155helpwin_initdone = false;
[293]156
[2305]157string grp = "PIACmd";
[2465]158string gdesc = "piapp interpreter (class PIACmd) additional builtin commands";
159AddHelpGroup(grp, gdesc);
160
[2672]161string kw = "exitpiapp";
[2463]162string usage = "To end the piapp session ";
[2419]163RegisterCommand(kw, usage, NULL, grp);
[2463]164kw = "helpwindow";
165usage = "To display the Help window ";
[2419]166RegisterCommand(kw, usage, NULL, grp);
[2672]167kw = "breakexe";
168usage = "To break (stop) interpreter (PIACmd/Commander) execution";
169RegisterCommand(kw, usage, NULL, grp);
[2203]170
[2490]171basexec = new PIABaseExecutor(this, mObjMgr, app);
[361]172fitexec = new PIAFitter(this, app);
[463]173pawexec = new PAWExecutor(this, app);
[1251]174CxxExecutor * cxxe = new CxxExecutor(this, app);
175cxxexec = cxxe;
[1828]176
177ContModExecutor *cntxx = new ContModExecutor(this, app);//_OP_
178cntexec = cntxx; //_OP_
[1920]179FlowModExecutor *flwxx = new FlowModExecutor(this, app);//_OP_
180flwexec = flwxx; //_OP_
[1828]181
[1251]182cxxoptwin = new CxxOptionWind(app, cxxe);
183cxxexwin = new CxxExecWind(app, cxxe);
184
[2465]185InitializeHelpWindowMenu();
[2486]186// <ZThread> Thread control attributes
187fg_thrstop = false;
188fg_threxe = false;
[165]189}
190
191/* --Methode-- */
192PIACmd::~PIACmd()
193{
[2490]194delete mObjMgr;
[293]195delete helpwin;
[1251]196delete cxxexwin;
197delete cxxoptwin;
[361]198delete basexec;
199delete fitexec;
[463]200delete pawexec;
[1224]201delete cxxexec;
[165]202}
203
204
[293]205/* --Methode-- */
[2486]206void PIACmd::AddInputLine(string const & line)
207{
[2672]208 if (line == "breakexe") {
209 StopExecution();
210 return;
211 }
[2486]212 mutx_inps.lock();
[2536]213 inputlines.push(line); // Ajout d'un element en fin de queue
[2486]214 mutx_inps.unlock();
215 mutx_inps.broadcast();
216}
217
218/* --Methode-- */
219void PIACmd::StopThread()
220{
221 fg_thrstop = true;
222 mutx_inps.broadcast();
223}
224
225/* --Methode-- */
226void PIACmd::run()
227{
228 while (!fg_thrstop) {
229 mutx_inps.lock();
230 while (inputlines.empty()) {
231 fg_threxe = false;
232 mutx_inps.wait();
233 }
[2536]234 string line = inputlines.front(); // On recupere l'element de tete
235 inputlines.pop(); // On supprime l'element de tete
[2486]236 fg_threxe = true;
237 mutx_inps.unlock();
[2606]238 try {
239 Interpret(line);
240 }
241 catch (PThrowable& pex) { // Catching SOPHYA exceptions
242 cerr << "PIACmd::run() SOPHYA exception in call to Interpret(" << line << ")\n"
243 << (string)typeid(pex).name() << " Msg= " << pex.Msg() << endl;
244 }
245 catch (std::exception& sex) {
246 cerr << "PIACmd::run() std::exception in call to Interpret(" << line << ")\n"
247 << (string)typeid(sex).name() << " Msg= " << sex.what() << endl;
248 }
249 catch (...) {
250 cerr << "PIACmd::run() unknown exception (...) in call to Interpret("
251 << line << ")" << endl;
252 }
[2486]253 }
254}
255
256/* --Methode-- */
[2465]257bool PIACmd::CheckHelpGrp(string& grp, int& gid, string& desc)
[330]258{
[2465]259bool fgnewgrp = Commander::CheckHelpGrp(grp, gid, desc);
260if (helpwin_initdone && fgnewgrp) helpwin->AddHelpGroup(grp.c_str(), gid);
261return fgnewgrp;
[330]262}
263
264/* --Methode-- */
265void PIACmd::UpdateHelpList(PIAHelpWind* hw, int gid)
266{
267helpwin->ClearHelpList();
[2465]268if (gid == 0) {
269 string htxt = "All registered commands";
270 string hlab = "All (HelpGroup)";
271 helpwin->SetHelpTextLabel(htxt, hlab);
272}
273else {
274 CmdHGroup::iterator ith;
275 for(ith = cmdhgrp.begin(); ith != cmdhgrp.end(); ith++) {
276 if ((*ith).second.gid == gid) {
277 string hlab = (*ith).first;
278 hlab += " (HelpGroup)";
279 helpwin->SetHelpTextLabel((*ith).second.desc, hlab);
280 break;
281 }
282 }
283}
[330]284CmdExmap::iterator it;
285for(it = helpexmap.begin(); it != helpexmap.end(); it++) {
286 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
287 helpwin->AddHelpItem((*it).first.c_str());
288 }
289for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
290 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
291 helpwin->AddHelpItem((*it).first.c_str());
292 }
293}
294
[2465]295/* --Methode-- */
296void PIACmd::InitializeHelpWindowMenu()
297{
298 helpwin->AddHelpGroup("All", 0);
299 CmdHGroup::iterator it;
300 for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++)
301 helpwin->AddHelpGroup((*it).first, (*it).second.gid);
302 helpwin_initdone = true;
303}
[165]304
[357]305/* Fonction */
[368]306static string GetStringFrStdin(PIACmd* piac)
[357]307{
[368]308PIStdImgApp* piapp = piac->GetImgApp();
309if (piapp) {
310 PIBaseWdg* wdg = piapp->CurrentBaseWdg();
311 if (wdg) wdg->Refresh();
[357]312#ifndef __mac__
[368]313 /* On vide le buffer X-Window */
[374]314 XSync(PIXDisplay(),False);
[357]315#endif
[368]316}
[357]317char buff[128];
318fgets(buff, 128, stdin);
319buff[127] = '\0';
320return((string)buff);
321}
322
[165]323
[1262]324/* --Methode-- */
325int PIACmd::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
326{
327int rc = 0;
328NamedObjMgr omg;
[349]329
[165]330// >>>>>>>>>>> Commande d'interpreteur
[2465]331if (kw == "helpwindow") {
332 ShowHelpWindow();
333 return 0;
334}
[2307]335// ----> Sortie d'application
336else if (kw == "exitpiapp") {
337 mImgApp->Stop();
[2465]338 return 0;
[2307]339}
[2463]340else return Commander::ExecuteCommandLine(kw, tokens, toks);
[2465]341return 0;
[165]342}
343
[333]344
[165]345/* --Methode-- */
[293]346void PIACmd::ShowHelpWindow()
[165]347{
[293]348helpwin->Show();
[165]349}
[357]350
[463]351/* --Methode-- */
[1251]352void PIACmd::ShowCxxOptionWindow()
353{
354cxxoptwin->Show();
355}
356
357/* --Methode-- */
358void PIACmd::ShowCxxExecWindow()
359{
360cxxexwin->Show();
361}
362
[2463]363/* --Methode-- */
364void PIACmd::SetCurrentPrompt(const char* pr)
[2377]365{
[2463]366 Commander::SetCurrentPrompt(pr);
[2489]367 if (mImgApp) {
368 ZSync(mImgApp->getMutex());
369 mImgApp->GetConsole()->SetPrompt(pr);
370 }
[2377]371}
372
[1251]373/* --Methode-- */
[2463]374void PIACmd::ShowMessage(const char * msg, int att)
[463]375{
[2489]376 /*
377 Il faut faire quelque chose pour gerer correctement ca en multithread
[2463]378 char va = (att == 0) ? 0 : PIVA_Magenta;
[2489]379 if (mImgApp) {
380 ZSync(mImgApp->getMutex());
381 mImgApp->GetConsole()->AddStr(msg, va);
382 }
383 */
384Commander::ShowMessage(msg, att);
[2263]385}
Note: See TracBrowser for help on using the repository browser.