Index: trunk/SophyaPI/PIext/piacmd.cc
===================================================================
--- trunk/SophyaPI/PIext/piacmd.cc	(revision 1553)
+++ trunk/SophyaPI/PIext/piacmd.cc	(revision 1562)
@@ -132,5 +132,5 @@
   enum BType { BT_None, BT_ForeachList, BT_ForeachInt, BT_ForeachFloat };
 
-  PIACmdBloc(PIACmd* piac, PIACmdBloc* par, vector<string>& args); 
+  PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args); 
   ~PIACmdBloc();
   inline PIACmdBloc*   Parent() { return(parent); }
@@ -156,5 +156,5 @@
 
 /* --Methode-- */
-PIACmdBloc::PIACmdBloc(PIACmd* piac, PIACmdBloc* par, vector<string>& args)
+PIACmdBloc::PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args)
 {
 piacmd = piac;
@@ -165,6 +165,8 @@
 f1 = 0.; f2 = -1.; df = 1.;
 if ((args.size() < 2) ||  !isalpha((int)args[0][0]) )  return; 
-varname = args[0];
-if (isalpha((int)args[1][0]) ) { // This is a foreach bloc with string list
+if ((kw != "foreach") && (kw != "for"))  return;
+varname = args[0];  // $CHECK$ Variable name should be checked 
+//if (isalpha((int)args[1][0]) ) {  This is a foreach bloc with string list
+if (kw == "foreach" ) { // This is a foreach bloc with string list
   for(int kk=1; kk<args.size(); kk++) strlist.push_back(args[kk]);
   typ = BT_ForeachList;
@@ -309,6 +311,6 @@
 usage += "  > readstdin varname      # reads a line from stdin into $varname \n";
 usage += "  > foreach varname string-list # Loop \n"; 
-usage += "  > foreach varname i1:i2[:di]  # Integer loop  \n"; 
-usage += "  > foreach varname f1:f2[:df]  # Float loop  \n";
+usage += "  > for varname i1:i2[:di]      # Integer loop  \n"; 
+usage += "  > for varname f1:f2[:df]      # Float loop  \n";
 usage += "  > end                         # end loops \n"; 
 usage += "  > listvars      # List of variable names and values \n";
@@ -509,5 +511,5 @@
   mulinecmd = "";
   mulinefg = false;
-  const char * rprompt = (curblk == NULL) ? "Cmd> " : "foreach? ";
+  const char * rprompt = (curblk == NULL) ? "Cmd> " : "for...? ";
   mImgApp->GetConsole()->SetPrompt(rprompt);
 }
@@ -541,25 +543,42 @@
 else { kw = s.substr(p,l-p);  toks = ""; }
 
-// On verifie si nous sommes dans un bloc 
-if ( (curblk != NULL) && (kw != "foreach") ) { // On est dans un bloc 
-  if (kw != "end")   { curblk->AddLine(s);  return(0); }
-  else { 
+// les mot-cle end else endif doivent etre le seul mot de la ligne
+if ( (kw == "end") || (kw == "else") || (kw == "endif") ) {
+  size_t ltk = toks.length();
+  if (toks.find_first_not_of(" \t") < ltk) {
+    cerr << "PIACmd::Interpret()/syntax error near end else endif \n"
+	 << "line: " << s << endl;
+    return(1);
+  }
+}
+
+// On verifie si nous sommes dans un bloc (for , foreach)
+if (curblk != NULL)  { // On est dans un bloc
+  if ( (kw == "for") || (kw == "foreach")) felevel++;
+  else if (kw == "end") felevel--;
+  if (felevel == 0) { // Il faut executer le bloc
     PIACmdBloc* curb = curblk;
     curblk = curb->Parent();
-    felevel--;
-    if (curblk  == NULL) { 
-       mImgApp->GetConsole()->SetPrompt("Cmd> ");
+    mImgApp->GetConsole()->SetPrompt("Cmd> ");
        //       cout << " *DBG* Executing bloc " << endl;
-       curb->Execute();
-       }
-    else  {
-      //      char prompt[64];
-      //      sprintf(prompt, "foreach-%d? ", felevel);
-      mImgApp->GetConsole()->SetPrompt("foreach? ");
-      }
+    bool ohv = histon;
+    histon = false;
+    curb->Execute();
+    histon = ohv;
+  }
+  else curblk->AddLine(s); 
+  return(0);
+}
+
+// Sommes-nous dans un bloc de test if then else 
+if (testresult.size() > 0) {  // Nous sommes ds un bloc if 
+  if (kw == "endif") {
+    list<bool>::iterator dbit = tresit;
+    tresit--;
+    testresult.erase(dbit);
     return(0);
-    }
-  }
-
+  }
+}
+if ((testresult.size() > 0) && !(*tresit)) return(0);
 
 // Nous ne sommes donc pas dans un bloc ....  Substitution de variables
@@ -616,10 +635,10 @@
 
 
-// Si c'est un foreach, on cree un nouveau bloc
-if (kw == "foreach") {
+// Si c'est un for/foreach, on cree un nouveau bloc
+if ((kw == "foreach") || (kw == "for")) {
   //     cout << " *DBG* We got a foreach... " << endl;
-  PIACmdBloc* bloc = new PIACmdBloc(this, curblk, tokens);
+  PIACmdBloc* bloc = new PIACmdBloc(this, curblk, kw, tokens);
   if (!bloc->CheckOK()) {
-    cerr << "foreach syntax Error ! " << endl;
+    cerr << "for/foreach syntax Error ! " << endl;
     delete bloc;
     return(0);
@@ -627,14 +646,15 @@
   felevel++;
   if (curblk)  curblk->AddBloc(bloc);
-  else {
-    char prompt[64];
-    sprintf(prompt, "foreach-%d> ", felevel);
-    mImgApp->GetConsole()->SetPrompt(prompt);
-    }
+  else  mImgApp->GetConsole()->SetPrompt("for...> ");
   curblk = bloc;
   //  cout << " *DBG* New Bloc created ... " << endl;
   return(0);
   }
-
+else if (kw == "if") {  // Un test 
+  bool res_tst = EvaluateTest(tokens);
+  testresult.push_back(res_tst);
+  if (testresult.size() == 1) tresit = testresult.begin();
+  else tresit++;
+}
 else rc = ExecuteCommandLine(kw, tokens, toks);
 return(rc);
@@ -705,5 +725,5 @@
     }
   else { 
-    q2 = s.find_first_of(" .:/,]()$\"'",q+1); 
+    q2 = s.find_first_of(" .:+-*/,[](){}&|!$\"'",q+1); 
     if (q2 > l) q2 = l;
     vn = s.substr(q+1, q2-q-1); 
@@ -722,4 +742,14 @@
 
 return(0);
+}
+
+/* --Methode-- */
+bool PIACmd::EvaluateTest(vector<string> & args)
+{
+  if (args.size() < 2) {
+    cerr << " PIACmd::EvaluateTest()/Warning - args.size() < 2 " << endl;
+    return true;
+  }
+  else return(args[0] == args[1]);
 }
 
@@ -970,4 +1000,5 @@
   }
 
+bool ohv = histon;
 histon = false; 
 while (fgets(line_buff,511,fip) != NULL)
@@ -978,5 +1009,5 @@
   Interpret(line);
   }
-histon = true; 
+histon = ohv; 
 
 // hist << "### End of Exec( " << file << " ) " << endl;
