Changeset 2483 in Sophya for trunk/SophyaLib/SysTools


Ignore:
Timestamp:
Dec 22, 2003, 12:41:46 AM (22 years ago)
Author:
ansari
Message:

Amelioration gestion variables ds interpreteur (classe Commander) - Introduction de trois niveaux : Var. interpreteur, env. appli, environnement global + amelioration gestion vecteur au niveau de l'interpreteur - Reza 22/12/2003

Location:
trunk/SophyaLib/SysTools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/SysTools/commander.cc

    r2473 r2483  
    1010
    1111
     12namespace SOPHYA {
    1213// ------------------------------------------------------------
    1314//         Bloc de commandes (Foreach, ...)   
     
    2122class CommanderBloc {
    2223public:
    23   enum BType { BT_None, BT_ForeachList, BT_ForeachInt, BT_ForeachFloat };
     24  enum BType { BT_None, BT_ForeachList, BT_ForeachInt, BT_ForeachFloat,
     25               BT_ForeachLineInFile };
    2426
    2527  CommanderBloc(Commander* piac, CommanderBloc* par, string& kw, vector<string>& args);
     
    4446  BType typ;         // foreach , integer loop, float loop, test
    4547  string varname;
     48  string filename;   // forinfile bloc
    4649  vector<string> strlist;
    4750  vector<string> lines;
     
    6871
    6972if ((args.size() < 2) ||  !isalpha((int)args[0][0]) )  return;
    70 if ((kw != "foreach") && (kw != "for"))  return;
     73if ((kw != "foreach") && (kw != "for") && (kw != "forinfile"))  return;
    7174varname = args[0];  // $CHECK$ Variable name should be checked
    7275//if (isalpha((int)args[1][0]) ) {  This is a foreach bloc with string list
    73 if (kw == "foreach" ) { // This is a foreach bloc with string list
     76if (kw == "forinfile") {
     77  filename = args[1];
     78  blkok = true;
     79}
     80else if (kw == "foreach" ) { // This is a foreach bloc with string list
    7481  if ((args[1] != "(") || (args[args.size()-1] != ")") ) return;
    7582  for(int kk=2; kk<args.size()-1; kk++) strlist.push_back(args[kk]);
    7683  typ = BT_ForeachList;
    7784  blkok = true;
    78   }
     85}
    7986else { // This is an integer or float loop
    8087  size_t l = args[1].length();
     
    144151int mxloop = _commander->GetMaxLoopLimit();
    145152
    146 if (typ == BT_ForeachList)   // foreach string loop
     153if (typ == BT_ForeachLineInFile) {  // foreach line in file loop
     154}
     155else if (typ == BT_ForeachList)   // foreach string loop
    147156  for(k=0; k<strlist.size(); k++) {
    148157    cmd = "set " + varname + " '" + strlist[k] + "'";
     
    313322spromptmul = "Cmd> ";
    314323SetCurrentPrompt(spromptmul);
     324SetDefaultPrompt(spromptmul);
    315325curscript = NULL;
     326
     327_xstatus = 0;
     328 _retstr = "";
    316329
    317330CmdBlks.push(NULL);
     
    612625    cerr << "Commander::Interpret()/syntax error near end else endif endscript \n"
    613626         << "line: " << s << endl;
    614     return(1);
     627    _xstatus = 91;
     628    return(91);
    615629  }
    616630}
     
    633647      SetCurrentPrompt("Cmd> ");
    634648      curscript = NULL;
     649      _xstatus = 0;
    635650      return(0);
    636651    }
     
    640655      SetCurrentPrompt("Cmd> ");
    641656      curscript = NULL;
    642       return(2);
     657      _xstatus = 92;
     658      return(92);
    643659    }
    644660  }
    645661  else curscript->AddLine(s, kw);
     662  _xstatus = 0;
    646663  return(0);
    647664}
     
    658675           << " within for/foreach bloc ! " << endl;
    659676      delete curb;
    660       return(2); 
     677      _xstatus = 93;
     678      return(93); 
    661679    }
    662680       //       cout << " *DBG* Executing bloc " << endl;
     
    672690      PopStack(false);
    673691    }
     692    SetCurrentPrompt(defprompt);
    674693    delete curb;
    675694    histon = ohv;
    676695  }
    677696  else CmdBlks.top()->AddLine(s, kw);
     697  _xstatus = 0;
    678698  return(0);
    679699}
     
    681701  cerr << "Commander::Interpret()/syntax error - end outside for/foreach bloc \n"
    682702       << "line: " << s << endl;
    683   return(1); 
     703  _xstatus = 94;
     704  return(94); 
    684705}
    685706
     
    690711      cerr << "Commander::Interpret()/syntax error - multiple else in if bloc \n"
    691712           << "line: " << s << endl;
    692       return(1);
     713      _xstatus = 95;
     714      return(95);
    693715    }
    694716    else {
     
    697719      SetCurrentPrompt(npr);
    698720      (*tresit) |= 2;
     721      _xstatus = 0;
    699722      return(0);
    700723    }
     
    723746    else curtestresult = true;
    724747    SetCurrentPrompt(npr);
     748    _xstatus = 0;
    725749    return(0);
    726750  }
     
    729753  cerr << "Commander::Interpret()/syntax error - else,endif outside if bloc \n"
    730754       << "line: " << s << endl;
    731   return(1); 
     755  _xstatus = 91;
     756  return(91); 
    732757}
    733758
     
    744769}
    745770
    746 if ((!fgcont) && (kw != "if"))  return(0);
     771if ((!fgcont) && (kw != "if"))  {
     772  _xstatus = 0;
     773  return(0);
     774}
    747775
    748776
    749777// Les mots cles break et return peuvent de sortir de boucles/scripts/execfile
    750778if (kw == "break") return(77766);
    751 else if (kw == "return")  return(77777);
     779else if (kw == "return")  {
     780  _retstr = toks;
     781  return(77777);
     782}
    752783
    753784// Nous ne sommes donc pas dans un bloc ....  Substitution de variables
     
    759790  cerr << "Commander::Interpret()/syntax error in SubstituteVars() \n"
    760791       << "line: " << s << endl;
    761   return(rcs);
     792  _xstatus = 99;
     793  return(99);
    762794}
    763795// >>>> Separating keyword and tokens
     
    767799
    768800// Si c'est un for/foreach, on cree un nouveau bloc
    769 if ((kw == "foreach") || (kw == "for")) {
     801if ((kw == "foreach") || (kw == "for") || (kw == "forinfile") ) {
    770802  //     cout << " *DBG* We got a foreach... " << endl;
    771803  CommanderBloc* bloc = new CommanderBloc(this, CmdBlks.top(), kw, tokens);
     
    773805    cerr << "Commander::Interpret() for/foreach syntax Error ! " << endl;
    774806    delete bloc;
    775     return(1);
     807    _xstatus = 91;
     808    return(91);
    776809    }
    777810  felevel++;
     
    787820  if (rct) {
    788821    cerr << "Commander::Interpret() if syntax Error ! " << "line: " << s << endl;
    789     return(1);
     822    _xstatus = 91;
     823    return(91);
    790824  }
    791825  char res_tst = (restst) ? 1 : 0;
     
    802836  if (rcev) {
    803837    cerr << "Commander::Interpret() evaluation (RPN) syntax Error ! " << "line: " << s << endl;
    804     return(1);
     838    _xstatus = 98;
     839    return(98);
    805840  }
    806841  return(0);
     
    815850  else {
    816851    cerr << "Commander::Interpret() No script name in defscript" << "line: " << s << endl;
    817     return(1);
     852    _xstatus = 91;
     853    return(91);
    818854  }
    819855}
     
    832868  //  Execution de commandes
    833869  else rc = ExecuteCommandLine(kw, tokens, toks);
     870  _xstatus = rc;
    834871  return(rc);
    835872
     
    941978      }
    942979    vn = s.substr(q+2,q2-q-2);
    943     if (!GetVar(vn, vv)) return(5);
     980    if (!Var2Str(vn, vv)) return(5);
    944981    vn = vv;
    945982    q2++;
     
    961998      if (isalpha(sia[0])) {
    962999        string sia2;
    963         if (!GetVar(sia, sia2) || (sia2.length() < 1)) {
     1000        if (!Var2Str(sia, sia2) || (sia2.length() < 1)) {
    9641001          cerr << " Syntax error - in $varname[index] : $" 
    9651002               << vn << "[" << sia <<"]" << endl;
     
    9761013    }
    9771014  }
    978   if (!GetVar(vn, vv)) return(5);
    9791015  if (iarr < 0) {
     1016    if (!Var2Str(vn, vv)) return(5);
    9801017    s2 += (s.substr(p, q-p) + vv);
    9811018    p = q2;
    9821019  }
    9831020  else {
    984     vector<string> vs;
    985     FillVStringFrString(vv, vs);
    986     if (iarr >= vs.size()) {
     1021    if (! Var2Str(vn, iarr, vv) ) {
    9871022      cerr << " Substitution error - word index out of range in "
    9881023           << "$varname[iarr] : $" << vn << "[" << iarr <<"]" << endl;
    9891024      return(4);
    9901025    }
    991     else s2 += (s.substr(p, q-p) + vs[iarr]);
     1026    else s2 += (s.substr(p, q-p) + vv);
    9921027    p = q3+1;
    9931028  }
     
    10021037
    10031038/* --Methode-- */
    1004 bool Commander::GetVar(string & vn, string & vv)
     1039bool Commander::Var2Str(string const & vn, string & vv)
    10051040{
    10061041if (vn.length() < 1) {
     
    10101045// Variable de type $# $0 $1 ... (argument de .pic ou de script)
    10111046int ka = 0;
     1047char buff[32];
     1048 
    10121049if (vn == "#") {
    10131050  if (ArgsStack.empty()) {
     
    10351072}
    10361073else if (vn[0] == '#') {  // Variable de type $#vname --> size(vname)
    1037   vn = vn.substr(1);
    1038   if (!HasVariable(vn) ) {
     1074  CmdVarList::iterator it = variables.find(vn.substr(1));
     1075  if (it == variables.end()) {
    10391076    cerr << " Commander::SubstituteVarError: Undefined variable "
    10401077         << vn << " ! " << endl;
    10411078    vv = "";  return(false);
    10421079  }
    1043   vn = GetVariable(vn);
    1044   vector<string> vs;
    1045   FillVStringFrString(vn, vs);
    1046   char buff[32];
    1047   sprintf(buff,"%d", (int)vs.size());
     1080  sprintf(buff,"%d", (int)(*it).second.size());
    10481081  vv = buff; return(true);
    1049   }
    1050 else {  // variable ordinaire geree par NamedObjMgr
    1051   if ( (!HasVariable(vn)) )  {
     1082}
     1083else if (vn == "status") {
     1084  sprintf(buff,"%d", _xstatus);
     1085  vv = buff;
     1086  return true;
     1087}
     1088else if (vn == "retstr") {
     1089  vv = _retstr;
     1090  return true;
     1091}
     1092else {  // Variable de l'interpreteur, ou de l'environnement application , env. global
     1093  if (GetVar(vn, vv))  return true;
     1094  else if (GetVarApp(vn, vv))  return true;
     1095  else if (GetVarEnv(vn, vv))  return true;
     1096  else {
    10521097    cerr << " Commander::SubstituteVarError: Undefined variable "
    10531098         << vn << " ! " << endl;
    1054     vv = "";  return(false);
    1055   }
    1056   vv = GetVariable(vn);  return(true);
    1057 }
    1058 
    1059 return(false);
    1060 }
    1061 /* --Methode-- */
    1062 string Commander::GetVariable(string const & key)
    1063 {
    1064   if ((key.length() < 1) || (! isalpha(key[0])) )   {
    1065     cout << "Commander::GetVariable( " << key << ") Bad VarName " << endl;
    1066     return("");
    1067   }
    1068   // cout << " DEBUG::GetVar " << variables << endl;
    1069   return(variables.GetS(key));
    1070 }
    1071 
    1072 /* --Methode-- */
    1073 bool Commander::HasVariable(string const & key)
    1074 {
    1075   if ((key.length() < 1) || (! isalpha(key[0])) )   {
    1076     cout << "Commander::HasVar( " << key << ") Bad VarName " << endl;
     1099    vv = "";  return false;
     1100  }
     1101}
     1102
     1103return false;
     1104}
     1105
     1106/* --Methode-- */
     1107bool Commander::SetVariable(string const & vn, string const & vv)
     1108{
     1109  // On verifie si le nom est de type vname[idx]
     1110  size_t p,q,l;
     1111  l = vn.length();
     1112  p = vn.find('[');
     1113  if (p < l) {
     1114    q = vn.find(']');
     1115    if (q != (l-1)) {
     1116      cout << "Commander::Str2Var/SetVar() - Bad varname with []: "
     1117           << vn << endl;
     1118      return false;
     1119    }
     1120    string vna = vn.substr(0, p);
     1121    string sia = vn.substr(p+1, q-(p+1));
     1122    if (isalpha(sia[0])) {
     1123      string sia2;
     1124      if (!Var2Str(sia, sia2) || (sia2.length() < 1)) {
     1125        cerr << "Commander::Str2Var/SetVar() Syntax error- varname[index]:" 
     1126             << vn << endl;
     1127        return false;     
     1128      }
     1129      sia = sia2;
     1130    }
     1131    int iarr;
     1132    int rcdia = ctoi(sia.c_str(), &iarr);
     1133    if (rcdia < 0) {
     1134      cerr << "Commander::Str2Var/SetVar() Syntax error- varname[iarr]: "
     1135           << vn << endl;
     1136      return false;
     1137    }
     1138    return SetVar(vna, iarr, vv);
     1139  }
     1140  else {
     1141    if (vn == "status") {
     1142      _xstatus = atoi(vv.c_str());
     1143      return true;
     1144    }
     1145    else if (vn == "retstr") {
     1146      _retstr = vv;
     1147      return true;
     1148    }
     1149    else return SetVar(vn, vv);
     1150  }
     1151}
     1152
     1153/* --Methode-- */
     1154bool Commander::GetVar(string const & vn, string & vv)
     1155{
     1156  CmdVarList::iterator it = variables.find(vn);
     1157  if (it == variables.end()) {
     1158    vv = "";
     1159    return false;
     1160  }
     1161  vv = (*it).second[0];
     1162  if ((*it).second.size() > 1) {   
     1163    for(int k=1; k<(*it).second.size(); k++) {
     1164      vv += ' ';  vv += (*it).second[k];
     1165    }
     1166  }
     1167  return true;
     1168}
     1169
     1170/* --Methode-- */
     1171bool Commander::GetVar(string const & vn, int idx, string & vv)
     1172{
     1173  vv = "";
     1174  CmdVarList::iterator it = variables.find(vn);
     1175  if (it == variables.end())  return false;
     1176  if ((idx < 0) || (idx > (*it).second.size()-1))
     1177    return false;
     1178  vv = (*it).second[idx];
     1179  return true;
     1180}
     1181
     1182/* --Methode-- */
     1183bool Commander::GetVar(string const & vn, vector<string> & vv)
     1184{
     1185  vv.clear();
     1186  //  vv.erase(vv.begin(),vv.end());
     1187  CmdVarList::iterator it = variables.find(vn);
     1188  if (it == variables.end())  return false;
     1189  vv = (*it).second;
     1190  return true;
     1191}
     1192
     1193/* --Methode-- */
     1194bool Commander::SetVar(string const & vn, string const & val)
     1195{
     1196  if ( !CheckVarName(vn) )  {
     1197    cerr << "Commander::SetVar( " << vn << " ...) Bad VarName " << endl;
    10771198    return(false);
    10781199  }
    1079   return(variables.HasKey(key));
    1080 }
    1081 
    1082 /* --Methode-- */
    1083 bool Commander::SetVar(string const & key, string const & val)
    1084 {
    1085    if ((key.length() < 1) || (! isalpha(key[0])) )   {
    1086     cout << "Commander::SetVar( " << key << " ...) Bad VarName " << endl;
     1200  bool fg = false;
     1201  vector<string> nouv;
     1202  nouv.push_back(val);
     1203  CmdVarList::iterator it = variables.find(vn);
     1204  if (it == variables.end())  variables[vn] = nouv;
     1205  else {
     1206    (*it).second = nouv;
     1207    fg = true;
     1208  }
     1209  return fg;
     1210}
     1211
     1212/* --Methode-- */
     1213bool Commander::SetVar(string const & vn, int idx, string const & val)
     1214{
     1215  if ( !CheckVarName(vn) )  {
     1216    cerr << "Commander::SetVar( " << vn << " ,idx, ...) Bad VarName " << endl;
    10871217    return(false);
    10881218  }
    1089   bool fg = variables.HasKey(key);
    1090   variables.SetS(key, val);
    1091   // cout << " DEBUG::SetVar " << variables << endl;
     1219  if ((vn == "status") || (vn == "retstr")) {
     1220    cerr << "Commander::SetVar(vn,idx,val) ERROR - special var status/retstr "
     1221         << endl;
     1222    return(false);
     1223  }
     1224  if (idx < 0) {
     1225    cout << "Commander::SetVar(vn," << idx << ",...) Error idx < 0" << endl;
     1226    return(false);
     1227  }
     1228  bool fg = false;
     1229  CmdVarList::iterator it = variables.find(vn);
     1230  if (it == variables.end()) {
     1231    vector<string> nouv;
     1232    for(int j=0; j<idx; j++)  nouv.push_back("");
     1233    nouv.push_back(val);
     1234    variables[vn] = nouv;
     1235  }
     1236  else {
     1237    if (idx >= (*it).second.size())
     1238      for(int j=(*it).second.size(); j<idx; j++)  (*it).second.push_back("");
     1239    (*it).second.push_back(val);
     1240    fg = true;
     1241  }
    10921242  return fg;
    10931243}
    10941244
    10951245/* --Methode-- */
    1096 bool Commander::DeleteVar(string const & key)
    1097 {
    1098   if ((key.length() < 1) || (! isalpha(key[0])) )   {
    1099     cout << "Commander::DeleteVar( " << key << ") Bad VarName " << endl;
     1246bool Commander::SetVar(string const & vn, vector<string> const & val)
     1247{
     1248  if ( !CheckVarName(vn) )  {
     1249    cerr << "Commander::SetVar( " << vn << " ...) Bad VarName " << endl;
    11001250    return(false);
    11011251  }
    1102   return(variables.DeleteKey(key));
    1103 }
    1104 
    1105 /* --Methode-- */
    1106 void Commander::ListVars()
    1107 {
    1108   cout << " ---- Commander::ListVars() List of defined variables ---- "
     1252  if ((vn == "status") || (vn == "retstr")) {
     1253    cerr << "Commander::SetVar(vn, vector<string>) ERROR - special var status/retstr "
     1254         << endl;
     1255    return(false);
     1256  }
     1257  bool fg = false;
     1258  CmdVarList::iterator it = variables.find(vn);
     1259  if (it == variables.end())  variables[vn] = val;
     1260  else {
     1261    (*it).second = val;
     1262    fg = true;
     1263  }
     1264  return fg;
     1265}
     1266
     1267/* --Methode-- */
     1268bool Commander::CheckVarName(string const & vn)
     1269{
     1270  size_t l,k;
     1271  l = vn.length();
     1272  if (l < 1)  return false;
     1273  if (!isalpha(vn[0]))  return false;
     1274  for(k=1; k<l; k++)
     1275    if ((!isalnum(vn[k])) && (vn[k] != '_'))  return false;
     1276  return true; 
     1277}
     1278
     1279/* --Methode-- */
     1280bool Commander::DeleteVar(string const & vn)
     1281{
     1282  CmdVarList::iterator it = variables.find(vn);
     1283  if (it == variables.end())  {
     1284    cerr << "Commander::DeleteVar() Var " << vn << " undefined!" << endl;
     1285    return false;
     1286  }
     1287  variables.erase(it);
     1288  return true;
     1289}
     1290
     1291/* --Methode-- */
     1292void Commander::ListVar()
     1293{
     1294  cout << " ---- Commander::ListVar() List of defined variables ---- "
    11091295       << endl;
    1110   cout << variables;
     1296  CmdVarList::iterator it;
     1297  for(it = variables.begin(); it != variables.end(); it++) {
     1298    string vn = (*it).first; 
     1299    int vs = (*it).second.size();
     1300    cout << vn << " -> Size= " << vs  << endl;
     1301  }
    11111302  cout << "---------------------------------------------------------- "
    11121303       << endl;
    11131304}
     1305
     1306/* --Methode-- */
     1307bool Commander::GetVarApp(string const & vn, string & vv)
     1308{
     1309  vv = "";
     1310  cout << " Commander::GetVarApp() Not available ! " << endl;
     1311  return false;
     1312}
     1313
     1314/* --Methode-- */
     1315bool Commander::SetVarApp(string const & vn, string const & vv)
     1316{
     1317  cout << " Commander::SetVarApp() Not available ! " << endl;
     1318  return false; 
     1319}
     1320
     1321/* --Methode-- */
     1322bool Commander::DeleteVarApp(string const & vn)
     1323{
     1324  cout << " Commander::DeleteVarApp() Not available ! " << endl;
     1325  return false; 
     1326}
     1327
     1328/* --Methode-- */
     1329void Commander::ListVarApp()
     1330{
     1331  cout << " Commander::ListVarApp() Not available ! " << endl;
     1332  return; 
     1333}
     1334
     1335
     1336/* --Methode-- */
     1337bool Commander::GetVarEnv(string const & vn, string & vv)
     1338{
     1339  vv = "";
     1340  cout << " Commander::GetVarEnv() Not available ! " << endl;
     1341  return false;
     1342}
     1343
     1344/* --Methode-- */
     1345bool Commander::SetVarEnv(string const & vn, string const & vv)
     1346{
     1347  cout << " Commander::SetVarEnv() Not available ! " << endl;
     1348  return false; 
     1349}
     1350
     1351/* --Methode-- */
     1352bool Commander::DeleteVarEnv(string const & vn)
     1353{
     1354  cout << " Commander::DeleteVarEnv() Not available ! " << endl;
     1355  return false; 
     1356}
     1357
     1358/* --Methode-- */
     1359void Commander::ListVarEnv()
     1360{
     1361  cout << " Commander::ListVarEnv() Not available ! " << endl;
     1362  return; 
     1363}
     1364
    11141365
    11151366/* --Methode-- */
     
    12281479  }
    12291480  else if (args.size() == 2) {
    1230     SetVar(args[0], args[1]);
     1481    SetVariable(args[0], args[1]);
    12311482    return(0);
    12321483  }
     
    14151666  sprintf(buff, "%g", x);
    14161667  string res = buff;
    1417   SetVar(args[0], res);
     1668  SetVariable(args[0], res);
    14181669  return(0);
    14191670}
     
    14611712
    14621713else if (kw == "set") {
    1463   if (tokens.size() < 2) { cout << "Commander::Interpret() Usage: set varname string" << endl;  return(0); }
    1464   if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
    1465     cerr << "Commander::Interpret()/Error Variable name should start with alphabetic" << endl;
    1466     return(0);
    1467     }
    1468   //  string xx = tokens[1];
    1469   //  for (int kk=2; kk<tokens.size(); kk++)  xx += (' ' + tokens[kk] );
    1470   SetVar(tokens[0], tokens[1]);
    1471   }
    1472 
     1714  if (tokens.size() < 2) { cout << "Commander::Interpret() Usage: set varname value" << endl;  return(0); }
     1715  if (tokens.size() == 2)
     1716    SetVariable(tokens[0], tokens[1]);
     1717  else {
     1718    vector<string> vv;
     1719    for(int k=1; k<tokens.size(); k++)  vv.push_back(tokens[k]);
     1720    SetVar(tokens[0], vv);
     1721  }
     1722  return 0;
     1723}
     1724/*  RZDELETE
    14731725else if (kw == "getvar") {
    14741726  if (tokens.size() < 2) { cout << "Commander::Interpret() Usage: getvar newvarname varname" << endl;  return(0); }
     
    14831735  SetVar(tokens[0], GetVariable(tokens[1]) );
    14841736  }
    1485 
     1737  END-RZDELETE */
    14861738else if (kw == "alias") {
    14871739  if (tokens.size() < 2) { cout << "Commander::Interpret() Usage: alias aliasname string" << endl;  return(0); }
     
    14891741    cerr << "Commander::Interpret()/Error alias name should start with alphabetic" << endl;
    14901742    return(0);
    1491     }
     1743  }
    14921744  string xx = tokens[1];
    14931745  for (int kk=2; kk<tokens.size(); kk++)  xx += (' ' + tokens[kk]);
    14941746  mAliases[tokens[0]] = xx;
    1495   }
    1496 
    1497 else if (kw == "unset") {
    1498   if (tokens.size() < 1) { cout << "Commander::Interpret() Usage: unset varname" << endl;  return(0); }
    1499   if (HasVariable(tokens[0])) DeleteVar(tokens[0]) ;
    1500   else cerr << "Commander::Interpret() No variable with name " << tokens[0] << endl;
    1501   }
    1502  else if (kw == "rpneval") {  // Evaluation d'expression en notation polonaise inverse
     1747}
     1748
     1749else if ( (kw == "unset") || (kw == "clearvar") ) {
     1750  if (tokens.size() < 1) {
     1751    cout << "Commander::Interpret() Usage: unset/clearvar varname" << endl; 
     1752    return(0);
     1753  }
     1754  else DeleteVar(tokens[0]);
     1755}
     1756// Evaluation d'expression en notation polonaise inverse
     1757else if (kw == "rpneval") { 
    15031758  return(EvalRPNExpr(tokens, toks));
    15041759}
     
    15291784  }
    15301785
    1531 else if (kw == "listvars") ListVars();
     1786else if (kw == "listvar") ListVar();
    15321787else if (kw == "listalias") {
    15331788  cout << "Commander::Interpret()  Alias List , AliasName = Value \n";
     
    18972152
    18982153
    1899 
     2154}  // End of namespace SOPHYA
     2155
  • trunk/SophyaLib/SysTools/commander.h

    r2473 r2483  
    2222#include "ctimer.h"
    2323
     24namespace SOPHYA {
    2425// Classe definissant l'interface pour un executeur de commande
    2526
     
    4950class CommanderScript;  // Script de commandes defini ds l'interpreteur Commander
    5051
    51 /*
    52 //! Interface definition for SOPHYA::Commander help manager
    53 class CommanderHelpMgr {
    54 public :
    55                         CommanderHelpMgr();
    56   virtual               ~CommanderHelpMgr();
    57   virtual void          AddHelpGroup(const char * hgrp, int gid);
    58   virtual void          AddHelpItem(const char * hitem);
    59   virtual void          ClearHelpList();
    60   virtual void          Activate();
    61 }
    62 */
    63 
    64 
    6552//! A simple command interpreter with c-shell like syntax with dynamic load capability.
    6653
     
    10491  inline  CmdInterpreter* CurrentInterpreter() { return(curcmdi); }
    10592
    106 
    107 //   Utilitaire pour decoupage en mot
     93  //   Utilitaire pour decoupage en mot
    10894  static  int   LineToWords(string& line, string& kw, vector<string>& tokens,
    10995                            string& toks, bool uq=true);
     
    123109  int           EvalRPNExpr(vector<string> & args, string & line);
    124110
    125   virtual bool    GetVar(string & vn, string & vv);
    126   virtual string  GetVariable(string const & vn);
    127   virtual bool    HasVariable(string const & vn);
     111  // Acces aux variables
     112  virtual bool    Var2Str(string const & vn, string & vv);
     113  inline  bool    Var2Str(string const & vn, int idx, string & vv)
     114  { return GetVar(vn, idx, vv); }
     115  inline  bool    Var2Str(string const & vn, vector<string> & vv)
     116  { return GetVar(vn, vv); }
     117
     118  // ----- Action / gestion des variables propres de l'interpreteur
     119  // Verifie l'existence de la variable nomme vn et retourne sa valeur ds vv
     120  // Retourne false si la variable n'existe pas
     121  virtual bool    SetVariable(string const & vn, string const & vv);
     122  virtual bool    GetVar(string const & vn, string & vv);
     123  virtual bool    GetVar(string const & vn, int idx, string & vv);
     124  virtual bool    GetVar(string const & vn, vector<string> & vv);
    128125  virtual bool    SetVar(string const & vn, string const & vv);
     126  virtual bool    SetVar(string const & vn, int idx, string const & vv);
     127  virtual bool    SetVar(string const & vn, vector<string> const & vv);
     128  virtual bool    CheckVarName(string const & vn);
    129129  virtual bool    DeleteVar(string const & vn);
    130   virtual void    ListVars();
     130  virtual void    ListVar();
     131  // Variables de l'environnement application
     132  virtual bool    GetVarApp(string const & vn, string & vv);
     133  virtual bool    SetVarApp(string const & vn, string const & vv);
     134  virtual bool    DeleteVarApp(string const & vn);
     135  virtual void    ListVarApp();
     136  // Variables d'environnement globales
     137  virtual bool    GetVarEnv(string const & vn, string & vv);
     138  virtual bool    SetVarEnv(string const & vn, string const & vv);
     139  virtual bool    DeleteVarEnv(string const & vn);
     140  virtual void    ListVarEnv();
    131141
    132142  virtual string  GetTmpDir();
     
    134144  virtual void  SetCurrentPrompt(const char* pr);
    135145  inline void   SetCurrentPrompt(string const & pr) { SetCurrentPrompt(pr.c_str()); }
     146  inline void   SetDefaultPrompt(string const & pr) { defprompt = pr; }
    136147
    137148  virtual void  ShowMessage(const char * msg, int att);
     
    143154
    144155// Gestion des variables
    145   DVList variables;
     156  typedef map< string,  vector<string>, less<string> > CmdVarList;
     157  CmdVarList variables;
    146158
    147159// Pour enregistrer la liste de commandes et leurs executeurs et le help
     
    168180  CommanderScript* curscript; // Script en cours de definition
    169181
    170 //  Pour stocker les alias definies par l'interpreteur
     182  // Code de retour execution commande
     183  int _xstatus;
     184  // Valeur de retour (par l'instruction return) -
     185  string _retstr;
     186
     187  //  Pour stocker les alias definies par l'interpreteur
    171188  typedef map<string, string, less<string> > CmdStrList;
    172189  CmdStrList mAliases;  // Liste des alias
    173190
    174 // Le stack pour les arguments des .pic et des scripts
     191  // Le stack pour les arguments des .pic et des scripts
    175192  stack< vector<string> > ArgsStack;
    176 // Stack pour les Prompts
     193  // Stack pour les Prompts
    177194  stack<string> PromptStack;
    178195
     196  // Gestion des blocs de commandes et tests (if)
    179197  stack< CommanderBloc * > CmdBlks;  // Bloc de commande courant (foreach, ...)
    180198  int felevel;                    // foreach-for level
     
    184202  bool curtestresult;             // Resultat courant des tests
    185203
     204  // Commande splitees sur plusieurs lignes
    186205  bool mulinefg;            // Bloc multi-lignes (ligne suite)
    187206  string mulinecmd;         // Commande multi-lignes
     207
     208  // Texte de prompt (attente de commande)
    188209  string spromptmul;        // Prompt console avant multi-ligne
    189   string curprompt;
    190 
     210  string curprompt;         // Prompt courant
     211  string defprompt;         // Prompt par defaut
     212
     213  // Gestion d'historique, trace, timing des commandes
    191214  ofstream hist;       //  History file
    192215  bool histon;        //  True ->  history file
     
    195218  Timer* gltimer;      // pour Display CPU Time
    196219
     220friend class CommanderBloc; 
     221friend class CommanderScript; 
     222
    197223};
    198224
     225} // namespace SOPHYA
    199226
    200227/* end of ifdef COMMANDER_H_SEEN */
Note: See TracChangeset for help on using the changeset viewer.