summaryrefslogtreecommitdiff
path: root/src/IFSelect/IFSelect_SessionPilot.cxx
blob: 361d721409eda93cd1c3da8f1d5053e052f9bfeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include <Standard_Stream.hxx>

#include <IFSelect_SessionPilot.ixx>
#include <IFSelect_Activator.hxx>
#include <IFSelect_Selection.hxx>
#include <Interface_EntityIterator.hxx>
#include <Interface_InterfaceModel.hxx>
#include <TColStd_HSequenceOfAsciiString.hxx>

#include <Interface_Macros.hxx>
#include <Message_Messenger.hxx>
#include <Message.hxx>

#include <stdio.h>

#define MAXWORDS 200
#define MAXCARS 1000

static int initactor = 0;
static char* trace;

static TCollection_AsciiString nulword;

// Nb Maxi de words : cf thewords et method SetCommandLine

    IFSelect_SessionPilot::IFSelect_SessionPilot (const Standard_CString prompt)
    : theprompt (prompt) , thewords (0,MAXWORDS-1) , thewordeb (0,MAXWORDS-1)
{
  if (theprompt.Length() == 0) theprompt.AssignCat ("Test-XSTEP>");
  therecord = Standard_False;  thenbwords = 0;
  if (initactor) return;  initactor = 1;
  Add (1,"x");
  Add (1,"exit");
  Add (2,"?");
  Add (2,"xhelp");
  Add (3,"xcommand");
  Add (4,"xsource");
  Add (5,"xstep");
  Add (6,"xnew");
  trace = getenv("DEBUGMODE");
}


    Handle(IFSelect_WorkSession)  IFSelect_SessionPilot::Session () const 
      {  return thesession;  }

    Handle(IFSelect_WorkLibrary)  IFSelect_SessionPilot::Library () const 
      {  return thesession->WorkLibrary();  }

    Standard_Boolean  IFSelect_SessionPilot::RecordMode () const 
      {  return therecord;  }

    void  IFSelect_SessionPilot::SetSession
  (const Handle(IFSelect_WorkSession)& WS)
      {  thesession = WS;  }

    void  IFSelect_SessionPilot::SetLibrary
  (const Handle(IFSelect_WorkLibrary)& WL)
      {  if (!thesession.IsNull()) thesession->SetLibrary(WL);  }

    void  IFSelect_SessionPilot::SetRecordMode (const Standard_Boolean mode)
      {  therecord = mode;  }


    void  IFSelect_SessionPilot::SetCommandLine
  (const TCollection_AsciiString& command)
{
  Standard_Integer lc = command.Length();
  if (lc > 200) cout<<" Commande TRES LONGUE : "<<lc<<" caracteres :"<<endl
    <<command.ToCString()<<endl;
  thecommand = command;
  if (thecommand.Value(lc) <= ' ')  {  thecommand.Remove(lc);  lc --;  }
  thenbwords = 0;
  Standard_Integer i, nc = 0;
  char unarg[MAXCARS];
  for (i = 1; i <= lc; i ++) {
    char val = command.Value(i);
    if (val <= ' ') {
      if (nc == 0) continue;
      if (thenbwords >= MAXWORDS) {  unarg[nc] = val;  nc ++;  continue;  }
      unarg[nc] = '\0';
      thewords(thenbwords).Clear();  thewords(thenbwords).AssignCat(unarg);
      if (trace) cout<<"thewords("<<thenbwords<<") ="<<unarg<<endl;
      thenbwords ++; nc = 0;
      continue;
    }
    if (nc == 0) thewordeb.SetValue (thenbwords,i);
    if (nc > MAXCARS) {  cout<<"Arg."<<thenbwords<<" > "<<MAXCARS<<" car.s, tronque"<<endl; continue;  }
    unarg[nc] = val;  nc ++;
  }
  if (nc > 0) {
    unarg[nc] = '\0'; thewords(thenbwords).Clear();
    thewords(thenbwords).AssignCat(unarg);
    if (trace) cout<<"thewords("<<thenbwords<<")="<<unarg<<endl<<" .. Fin avec thenbwords="<<thenbwords+1<<endl;
    thenbwords ++;
  }
/*
    aligner sur MAXWORDS
  char l0[80],l1[80],l2[80],l3[80],l4[80],l5[80],l6[80],l7[80],l8[80],l9[80];
  char m0[80],m1[80],m2[80],m3[80],m4[80],m5[80],m6[80],m7[80],m8[80],m9[80];
  thenbwords = sscanf
    (thecommand.ToCString(),"%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
     l0,l1,l2,l3,l4,l5,l6,l7,l8,l9,m0,m1,m2,m3,m4,m5,m6,m7,m8,m9);
  if (thenbwords <  0) thenbwords = 0;
  if (thenbwords > MAXWORDS) thenbwords = MAXWORDS;
  Standard_Integer nb = thewords.Upper();
  for (i = 0; i <= nb; i ++) thewords(i).Clear();
  switch (thenbwords) {
    case 20 : thewords(19).AssignCat(m9);
    case 19 : thewords(18).AssignCat(m8);
    case 18 : thewords(17).AssignCat(m7);
    case 17 : thewords(16).AssignCat(m6);
    case 16 : thewords(15).AssignCat(m5);
    case 15 : thewords(14).AssignCat(m4);
    case 14 : thewords(13).AssignCat(m3);
    case 13 : thewords(12).AssignCat(m2);
    case 12 : thewords(11).AssignCat(m1);
    case 11 : thewords(10).AssignCat(m0);
    case 10 : thewords(9).AssignCat(l9);
    case  9 : thewords(8).AssignCat(l8);
    case  8 : thewords(7).AssignCat(l7);
    case  7 : thewords(6).AssignCat(l6);
    case  6 : thewords(5).AssignCat(l5);
    case  5 : thewords(4).AssignCat(l4);
    case  4 : thewords(3).AssignCat(l3);
    case  3 : thewords(2).AssignCat(l2);
    case  2 : thewords(1).AssignCat(l1);
    case  1 : thewords(0).AssignCat(l0);
    default : break;
  }
*/
  thenumrec = 0;
  theobjrec.Nullify();
}

    const TCollection_AsciiString&  IFSelect_SessionPilot::CommandLine () const
      {  return thecommand;  }

    Standard_CString  IFSelect_SessionPilot::CommandPart
  (const Standard_Integer numarg) const
{
  if (numarg <= 0) return thecommand.ToCString();
  if (numarg >= thenbwords) return "";
  return &(thecommand.ToCString())[thewordeb(numarg)-1];
}

    Standard_Integer  IFSelect_SessionPilot::NbWords () const 
      {  return thenbwords;  }

    const TCollection_AsciiString&  IFSelect_SessionPilot::Word
  (const Standard_Integer num) const 
      {  if (num < thenbwords) return thewords(num);  return nulword;  }

    Standard_CString  IFSelect_SessionPilot::Arg
  (const Standard_Integer num) const 
      {  return Word(num).ToCString();  }

    Standard_Boolean  IFSelect_SessionPilot::RemoveWord
  (const Standard_Integer num)
{
  if (num < 0 || num > thenbwords) return Standard_False;
  Standard_Integer i; // svv Jan11 2000 : porting on DEC
  for (i = num; i < thenbwords; i ++) {
    thewords(i).Clear();
    thewords(i).AssignCat(thewords(i+1).ToCString());
  }
  thewords(thenbwords).Clear();
  thenbwords --;
//  Et refaire thecommand. Si num = 0, on supprime le debut (facile)
  if (num == 0) {
    thecommand.Remove(1,thewordeb(1));
  } else {
//   Sinon, reconstituer, a partir des words
    thecommand.Clear();
    for (i = 0; i < thenbwords; i ++) {
      if (i > 0) thecommand.AssignCat(" ");
      thecommand.AssignCat(thewords(i));
    }
  }

  return Standard_True;
}

    Standard_Integer  IFSelect_SessionPilot::NbCommands () const 
      {  return thecomlist.Length();  }

    const TCollection_AsciiString&  IFSelect_SessionPilot::Command
  (const Standard_Integer num) const 
      {  return thecomlist(num);  }


    IFSelect_ReturnStatus  IFSelect_SessionPilot::RecordItem
  (const Handle(Standard_Transient)& item)
{
  theobjrec = item;
  return (item.IsNull() ? IFSelect_RetFail : IFSelect_RetDone);
}

    Handle(Standard_Transient)  IFSelect_SessionPilot::RecordedItem () const
      {  return theobjrec;  }

    void  IFSelect_SessionPilot::Clear ()
      { thecomlist.Clear(); }


//  #######################################################################
//  ########        CONTROLE D EXECUTION


    IFSelect_ReturnStatus  IFSelect_SessionPilot::ReadScript
  (const Standard_CString file)
{
  FILE* fic; int lefic = 0;
  if (file != NULL && file[0] != '\0') {
    fic = fopen (file,"r");
    if (fic) lefic = 1;
    else { cout<<" ...   Script File "<<file<<" not found"<<endl; return IFSelect_RetFail; }
    cout << " ...   Reading Script File " << file << endl;
  }
  else fic = stdin;
  IFSelect_ReturnStatus stat = IFSelect_RetVoid;

  for (;;) {
    char ligne[100];
    if (!lefic) printf ("%s",theprompt.ToCString());
    ligne[0] = '\0';
    fgets(ligne,100,fic);
    if (feof(fic)) break;
    if (ligne[0] == '\0') continue;
//    On interprete cette commande
    TCollection_AsciiString command(ligne);
    if (lefic) cout<<file<<":"<<command;  // le return est dans la ligne ... !
    stat = Execute(command);
    if (stat == IFSelect_RetStop) break;
    if ((stat == IFSelect_RetError || stat == IFSelect_RetFail) && lefic)
      { cout << " ...   Error in Script File, abandon"<<endl;  break; }
  }
  if (!lefic) return IFSelect_RetStop;
  fclose(fic);
  cout<<"End of Reading Script File " << file << endl;
  if (stat == IFSelect_RetError || stat == IFSelect_RetFail) return stat;
  return IFSelect_RetVoid;        // fin fichier : depiler
}


//  On boucle sur la lecture jusqu a une commande de fin ou un EOF

    IFSelect_ReturnStatus  IFSelect_SessionPilot::Perform ()
{
  IFSelect_ReturnStatus stat = IFSelect_RetVoid;
  if (thenbwords == 0) return stat;
  if (thewords(0).Value(1) == '#') return stat;  // commentaire

  theobjrec.Nullify();
//  Est-ce un nom ?

//  Commande pour un Acteur
  Handle(IFSelect_Activator) actor;  Standard_Integer num;
  if (IFSelect_Activator::Select(thewords(0).ToCString(),num,actor)) {
    stat = actor->Do(num,this);
//  Prise en compte des commandes a resultat
//  Ici, resultat non nomme;  Resultat nomme par commande x (plus loin)
    if (!theobjrec.IsNull()) {
      thesession->RemoveItem(theobjrec);  //// depannage ?
      Standard_Boolean addws = thesession->AddItem(theobjrec);
      if (!addws) { cout<<"Could not add item to session, sorry"<<endl; return IFSelect_RetFail; }
    }

    if (stat == IFSelect_RetVoid || stat == IFSelect_RetDone) {
      if (therecord) thecomlist.Append(thecommand);
    }
    else if (stat == IFSelect_RetError) cout<<"Error in Command : "<<thecommand<<endl;
    else if (stat == IFSelect_RetFail) cout << "Execution Failure for : " <<thecommand<<endl;
    return stat;
  }
  cout << " Command : " << thewords(0) << " unknown" << endl;
  return IFSelect_RetError;    // pas reconnu donc incorrect
}

    IFSelect_ReturnStatus  IFSelect_SessionPilot::ExecuteAlias
  (const TCollection_AsciiString& alias)
{
  if (alias.Length() > 0) thewords(0) = alias;
  return Perform();
}

    IFSelect_ReturnStatus  IFSelect_SessionPilot::Execute
  (const TCollection_AsciiString& command)
{
  SetCommandLine(command);
  return Perform();
}

    IFSelect_ReturnStatus  IFSelect_SessionPilot::ExecuteCounter
  (const Handle(IFSelect_SignCounter)& counter, const Standard_Integer numword,
   const IFSelect_PrintCount mode)
{
  if (counter.IsNull()) return IFSelect_RetError;
  counter->Clear();
  if (NbWords() <= numword) counter->AddModel (thesession->Model());
  else {
//   on demande un givelist
    Handle(TColStd_HSequenceOfTransient) list = thesession->GiveList (CommandPart(numword));
    if (list.IsNull()) {
      cout<<"Nothing selected from : "<<CommandPart(numword)<<endl;
      return IFSelect_RetError;
    }
    counter->AddWithGraph (list,thesession->Graph());
  }
  counter->PrintList(Message::DefaultMessenger(),thesession->Model(),mode);
  return IFSelect_RetVoid;
}

    Standard_Integer  IFSelect_SessionPilot::Number
  (const Standard_CString val) const
{
  Standard_Integer num = thesession->NumberFromLabel (val);
  if (num < 0)  cout<<" Label:"<<val<<" ->"<<-num<<" ent.s, refus"<<endl;
  return num;
}


//  #########################################################################
//  ########        ACTIONS SPECIFIQUES DU PILOTE

#define MAXCOMPERLINE  5
#define LENGTHFORCOM  15

    IFSelect_ReturnStatus  IFSelect_SessionPilot::Do
  (const Standard_Integer number,
   const Handle(IFSelect_SessionPilot)& session)
{
//                  Commandes Propres : x, exit, undo, redo, ?, help
  IFSelect_ReturnStatus  stat = IFSelect_RetVoid;
  Standard_Integer       argc = NbWords();
  const Standard_CString arg1 = Word(1).ToCString();
  Standard_Integer       modhelp = -1;
  switch (number) {
    case -1 :                                 //        ****     HELP-XSNEW
      modhelp = 1;
      cout<<"  --  Commands candidate for  xsnew  --"<<endl;
//  HELP : soit complet (par defaut)  soit limite a xsnew
    case  0 : {                               //        ****     HELP
      Handle(TColStd_HSequenceOfAsciiString) list;
//    Help complet : on donne la liste des commandes, sans plus (deja pas mal)
      if (thenbwords <= 1) {
	list = IFSelect_Activator::Commands(modhelp);
	Standard_Integer nbcom = 0;
	Standard_Integer nb = list->Length();
	cout << " -- Liste des Commands Disponibles --"<<endl;
	for (Standard_Integer i = 1; i <= nb; i ++) {
	  const TCollection_AsciiString& uncom = list->Value(i);
	  Standard_Integer loncom = uncom.Length();
	  nbcom ++;
	  if (nbcom > MAXCOMPERLINE) { cout<<endl; nbcom = 1; }
	  cout<<" "<<uncom;
	  if (nbcom == MAXCOMPERLINE) continue;
	  for (Standard_Integer j = loncom; j < LENGTHFORCOM; j ++) cout<<" ";
	}
	if (nbcom > 0) cout<<endl;
	cout<<"\nhelp *  liste toutes les commandes avec un help sur chacune\n"
	  <<"help <com> liste la ou les commande debutant par <com>"
	  <<" avec un help sur chacune"<<endl;

//    Un Help particulier
      } else {
	if (thewords(1).IsEqual("*")) list = IFSelect_Activator::Commands(modhelp);

	else list = IFSelect_Activator::Commands(modhelp,thewords(1).ToCString());

	Standard_Integer nb = list->Length();
	for (Standard_Integer i = 1; i <= nb; i ++) {
	  Handle(IFSelect_Activator) actor;  Standard_Integer num;
	  if (IFSelect_Activator::Select
	      (list->Value(i).ToCString(),num,actor)) {
	    if (IFSelect_Activator::Mode (list->Value(i).ToCString()) == 1)
	      cout<<"[xsnew name] ";
	    cout << list->Value(i) << "	: " << actor->Help(num) << endl;
	  }
	}
	if (nb == 0 && thenbwords > 1) cout<<" Command "<<Word(1)<<" unknown. "
	  << " help (without command) lists all the commands" << endl;
      }
      return IFSelect_RetVoid;
    }
    case  1 : return IFSelect_RetStop;        //        ****     Fin de session
    case  2 : {                               //        ****     HELP
      return Do(0,this);
    }
    case  3 : {                               //        ****     COMMAND
      if (argc < 2) { cout << "Donner une option :\n"
	 <<"a : analyse une ligne  r : toggle record mode\n"
	 <<"l : list recorded  c : clear  f nom : sauver dans fichier de nom"
	 << endl; return IFSelect_RetVoid; }
      switch (arg1[0]) {
        case 'a' : {                          //        ****    command analyse
	  cout<<"Command n0 " << number <<" : "<< session->CommandLine()<<endl;
	  cout<<"Nb Words : " << argc-2 << " :\n";
	  for (Standard_Integer i = 2; i < argc; i ++) {
	    cout << " Word." << i-1 << " : " << session->Word(i) <<endl;
	  }
	  break;
	}
	case 'c' : session->Clear();  break;  //        ****    command clear
	case 'f' : {
	  if (argc < 3) { cout<<"Donner nom de fichier"<<endl; return IFSelect_RetError; }
	  Standard_Integer nb = session->NbCommands();
	  if (nb == 0) { cout<<"Aucune commande enregistree"<<endl; break; }
	  cout << "Nb Commandes enregistrees : " << nb <<endl;
	  ofstream fout(Word(2).ToCString(),ios::out);
	  for (Standard_Integer i = 1; i <= nb; i ++)
	    fout<<session->Command(i)<<endl;
	  break;
	}
	case 'l' : {                          //        ****    command list
	  if (session->RecordMode()) cout<<"  -- Record Mode Actif"<<endl;
	  else                       cout<<"  -- Record Mode Inactif"<<endl;
	  Standard_Integer nb = session->NbCommands();
	  cout << "Nb Commandes enregistrees : " << nb << " :"<<endl;
	  for (Standard_Integer i = 1; i <= nb; i ++) {
	    cout<<"  "<<i<<"	"<<session->Command(i)<<endl;
	  }
	  break;
	}
	case 'r' : {                          //        ****    command record
	  Standard_Boolean mode = session->RecordMode();
	  if (mode) cout << " -- Record Mode a present Inactif" <<endl;
	  else      cout << " -- Record Mode a present Actif"   <<endl;
	  session->SetRecordMode(!mode);
	  break;
	}
	default  : cout << "Option de controle de commande non comprise"<<endl;
      }
      return IFSelect_RetVoid;
    }

    case  4 : {                               //        ****     FILE
      if (argc < 2) { cout<<"Donner nom de fichier"<<endl; return IFSelect_RetError; }
      return session->ReadScript
	(TCollection_AsciiString(session->Word(1)).ToCString());
//          On recopie la string parce que Word(1) change tout le temps !
    }

    case  5 : {                               //        ****     XSTEP
      if (argc < 2) {
	cout<<"xstep : prefixe neutre pour toute commande xstep-draw"<<endl
	  <<"xstep command args  equivaut a  command args"<<endl;
	return Do(2,this);
      } else {
	RemoveWord(0);
	return Perform();
      }
    }
    case  6 : {                               //        ****    XSNEW(variable)
      if (argc < 3) {
	cout<<"xsnew nomvar command [args]   creates an item"<<endl
	  <<"  nomvar : name of item (must be a new name) in the session"<<endl;
	return Do (-1,this);
      } else {

	theobjrec.Nullify();
	TCollection_AsciiString name = Word(1);
//  Le nom ne doit pas etre deja pris !
	  if (thesession.IsNull()) { cout<<"Command with a Name and no Session defined !"<<endl; return IFSelect_RetFail; }
//////    if (thesession->NameIdent(thewords(0).ToCString()) > 0)
//////      { cout<<"Command with name:"<<thewords(0)<<", already taken"<<endl; return IFSelect_RetFail; }
	RemoveWord(0);  RemoveWord(0);

//  Commande pour un Acteur
	Handle(IFSelect_Activator) actor;  Standard_Integer num;
	if (IFSelect_Activator::Select(thewords(0).ToCString(),num,actor)) {
	  theobjrec.Nullify();
	  stat = actor->Do(num,this);
//  Prise en compte des commandes a resultat
	  if (!theobjrec.IsNull()) {
	    thesession->RemoveItem(theobjrec);  //// depannage ?
	    Standard_Boolean addws =
	      thesession->AddNamedItem(name.ToCString(),theobjrec);
	    theobjrec.Nullify();
	    if (!addws) { cout<<"Could not add named item:"<<name<<", sorry"<<endl; return IFSelect_RetFail; }
	  }
	  else cout<<"Remark : xsnew with name:"<<name<<" and no result"<<endl;

	  return stat;
	}
	cout << " Command : " << thewords(0) << " unknown" << endl;
	return IFSelect_RetError;    // pas reconnu donc incorrect
      }
    }
    default : return IFSelect_RetError;
  }
}



    Standard_CString  IFSelect_SessionPilot::Help
  (const Standard_Integer number) const
{
  switch (number) {
    case  1 : return "exit ou x : Fin de session";
    case  2 : return "Liste les commandes. ? <titre> : commandes debutant par <titre>";
    case  3 : return "controle de commande. command tout court pour help complet";
    case  4 : return "lit les commandes depuis un fichier";
    case  5 : return "prefixe neutre pour xstep-draw";
    case  6 : return "creation item : donner nom_item puis commande args";
    default : return "";
  }
}