Index: trunk/SophyaPI/PIext/piacmd.cc
===================================================================
--- trunk/SophyaPI/PIext/piacmd.cc	(revision 2211)
+++ trunk/SophyaPI/PIext/piacmd.cc	(revision 2214)
@@ -9,4 +9,5 @@
 #include "pdlmgr.h"
 #include "ctimer.h"
+#include "strutil.h"
 // #include "dlftypes.h"
 
@@ -272,4 +273,13 @@
 return(parent); 
 }
+
+// ------------------
+//  Classe PIACmdScript 
+// ------------------
+class PIACmdScript {
+public:
+  PIACmdScript();
+  virtual ~PIACmdScript();
+};
 
 // ------------------------------------------------------------
@@ -542,5 +552,5 @@
 
 // >>>> Substitution d'alias (1er mot)
-CmdVarList::iterator it;
+CmdStrList::iterator it;
 p = 0;
 q = s.find_first_of(" \t");
@@ -756,5 +766,5 @@
 //  Variable substitution  
 {
-NamedObjMgr omg;
+NamedObjMgr& omg = *mObjMgr;
 
 size_t p,q,q2,l;
@@ -763,5 +773,5 @@
 p = 0;
 l = s.length();
-string vn;
+string vn, vv;
 while (p < l) {
   q = s.find('$',p);
@@ -803,9 +813,6 @@
       }
     vn = s.substr(q+2,q2-q-2);
-    if ( (vn.length() < 1) || (!omg.HasVar(vn)) )  {
-      cerr << " Error: Undefined variable " << vn << " ! " << endl;
-      return(5);
-      }
-    vn = omg.GetVar(vn);
+    if (!GetVar(vn, vv)) return(5);
+    vn = vv;
     q2++;
     }
@@ -815,9 +822,6 @@
     vn = s.substr(q+1, q2-q-1); 
     }
-  if ( (vn.length() < 1) || (!omg.HasVar(vn)) )  {
-    cerr << " Error: Undefined variable " << vn << " ! " << endl;
-    return(5);
-    }
-  s2 += (s.substr(p, q-p) + omg.GetVar(vn));
+  if (!GetVar(vn, vv)) return(5);
+  s2 += (s.substr(p, q-p) + vv);
   p = q2;
   } 
@@ -828,4 +832,50 @@
 
 return(0);
+}
+
+/* --Methode-- */
+bool PIACmd::GetVar(string & vn, string & vv)
+{
+NamedObjMgr& omg = *mObjMgr;
+if (vn.length() < 1) {
+  cerr << " PIACmd::SubstituteVar/Error: length(varname=" << vn << ")<1 !" << endl;
+  vv = "";    return(false);
+}
+// Variable de type $# $0 $1 ... (argument de .pic ou de script)
+int ka = 0;
+if (vn == "#") {
+  if (ArgsStack.empty()) {
+    cerr << " PIACmd::SubstituteVar/Error: ArgsStack empty ! " 
+	 << " ($" << vn << ")" << endl;
+    vv = ""; return(false);
+  }
+  char buff[32];
+  long an = ArgsStack.top().size();
+  sprintf(buff,"%ld", an);
+  vv = buff;
+}
+else if (ctoi(vn.c_str(), &ka) > 0) {  // $0 $1 $2 ...
+  if (ArgsStack.empty()) {
+    cerr << " PIACmd::SubstituteVar/Error: ArgsStack empty ! " 
+	 << " ($" << vn << ")" << endl;
+    vv = ""; return(false);
+  }
+  if ( (ka < 0) || (ka >= ArgsStack.top().size()) ) {
+    cerr << " PIACmd::SubstituteVar/Error: Undefined variable ! " 
+	 << " ($" << vn << ")" << endl;
+    vv = ""; return(false);
+  }
+  vv = ArgsStack.top()[ka];
+}
+else {  // variable ordinaire geree par NamedObjMgr
+  if ( (!omg.HasVar(vn)) )  {
+    cerr << " PIACmd::SubstituteVarError: Undefined variable " 
+	 << vn << " ! " << endl;
+    vv = ""; return(false);
+  }
+  vv = omg.GetVar(vn);
+}
+
+return(true);
 }
 
@@ -955,5 +1005,5 @@
 else if (kw == "listalias") {
   cout << "PIACmd::Interpret()  Alias List , AliasName = Value \n";
-  CmdVarList::iterator it;
+  CmdStrList::iterator it;
   for(it = mAliases.begin(); it != mAliases.end(); it++)  
     cout << (*it).first << " = " <<  (*it).second << "\n";
@@ -1072,26 +1122,6 @@
 // hist << "### Executing commands from " << file << endl;
 
-// Setting $0 ... $99 variables 
-int k;
-char buff[32];
-// First, we clear all previous values
-NamedObjMgr omg;
-string vn="#";
-omg.DeleteVar(vn); 
-for(k=0; k<99; k++) {   
-  sprintf(buff,"%d",k);
-  vn = buff;
-  omg.DeleteVar(vn); 
-  }
-// We then set them
-string vval;
-vn="#";
-sprintf(buff,"%d",(int)args.size());
-omg.SetVar(vn, buff);
-for(k=0; k<args.size(); k++) {   
-  sprintf(buff,"%d",k);
-  vn = buff;
-  omg.SetVar(vn, args[k]);
-  }
+// We push the argument list (args) on the stack
+ArgsStack.push(args);
 
 if (trace) { 
@@ -1118,4 +1148,7 @@
   mImgApp->GetConsole()->AddStr(" ) \n", PIVA_Magenta);
   }
+
+// We remove the argument list (args) from the stack
+ArgsStack.pop();
 
 return(0);
Index: trunk/SophyaPI/PIext/piacmd.h
===================================================================
--- trunk/SophyaPI/PIext/piacmd.h	(revision 2211)
+++ trunk/SophyaPI/PIext/piacmd.h	(revision 2214)
@@ -13,4 +13,5 @@
 #include <vector>
 #include <list>
+#include <stack>
 #include <map>
 #include <functional>
@@ -53,5 +54,6 @@
 class CxxOptionWind; // Option de CxxExecutor
 
-class PIACmdBloc;
+class PIACmdBloc;    // Bloc de type foreach / for de l'interpreteur PIACmd
+class PIACmdScript;  // Script de commandes defini ds l'interpreteur PIACmd
 
 // ---------------------------------------------------------------------
@@ -107,4 +109,5 @@
   int	        EvaluateTest(vector<string> & args, 
 			     string & line, bool & res);
+  bool          GetVar(string & vn, string & vv);
 
   NamedObjMgr* mObjMgr;
@@ -137,7 +140,14 @@
   InterpMap interpmap;
  
-//  Pour stocker les variables definies par l'interpreteur
-  typedef map<string, string, less<string> > CmdVarList;
-  CmdVarList mAliases;  // Liste des alias 
+//  Pour stocker les scripts definis ds l'interpreteur
+  typedef map<string, PIACmdScript*, less<string> > ScriptList;
+  ScriptList mScripts;  // Liste des scripts
+
+//  Pour stocker les alias definies par l'interpreteur
+  typedef map<string, string, less<string> > CmdStrList;
+  CmdStrList mAliases;  // Liste des alias 
+
+// Le stack pour les arguments des .pic et des scripts
+  stack< vector<string> > ArgsStack;
 
   PIACmdBloc * curblk;  // Bloc de commande courant (foreach, ...)
