Source: site.view [edit]
Function name: consoleCmd
Arguments: input
Description:
Page type: webl
Render function:  
Module: siteutil

Page source:

var response;


var varSet = fun(str, newVar)
   var vars = {};
   if Str_Trim(str) != "" then
      vars = (WubEval(str) ? {})
   end;
   if (newVar != nil) then
      if (newVar.name != "bkvars") then 
         vars = vars + {newVar.name}
      end
   end;
   return vars
end;

var initVars = fun(str, newVar)
   var res = "";
   every v in varSet(str, newVar) do
      if v != "bkvars" then
         res = res + "var " + v + ";\n"
      end
   end;  
   return res
end;
              
var isVar = fun(str)
   var ok = true;
   every ch in str do
      ok = ok and (((ch >= '0') and (ch <= '9')) or 
                   ((ch >= 'A') and (ch <= 'z')))
   end;
   return ok
end;
               
               
var checkStatement = fun(symbol, str)              
   var res = nil;
   var i = Str_IndexOf(symbol, str);
   if i > 0 then
      var lhs = Str_Trim(Select(str, 0, i));
      var rhs = Str_Trim(Select(str, i+Size(symbol), Size(str)));
      if Str_EndsWith(rhs, ";") then
         rhs = Select(rhs, 0, Size(rhs)-1)
      end;
      if isVar(lhs) then
         res = [. name=lhs, op=symbol, rhs=rhs .]
      end
   end;   
   return res
end;

               
var checkVarAssigned = fun(str)
   var res = checkStatement(":=", str);
   if (res == nil) then
      res = checkStatement("=", str)
   end;
   return res
end;

var analyzeResponseType = fun(res, rhs)
  var dic = ToSource(res);
  var ac = dic;
  
  if Pagep(res) or Piecep(res) then
     var n = ToString(Wub_RandomInt(99999));
     var ctype = "html";
     var desc = "Page";
     if Piecep(res) then
        ctype = "snippet";
        desc = "Piece"
     end;
     var pagename = "tmp" + desc + n;
     var finfo = [. module=wubinfo.user, name=pagename, ctype=ctype, description="Page saved by console command.",
                      render="", test="", version="0", tags="", flags="tmpconsole", exec=Markup(res), cacheexpire="", 
                      cacherefreshrate="", usecache="", createdby=wubinfo.user, modified="", 
                      created="", modifiedby=wubinfo.user,arglist="" .];
     Wub_SaveFunctionInfo(finfo);
     dic = "<" + desc + " id='" + n + "'>";
     ac = `WubCall("` + pagename + `", [])`;
  elsif PieceSetp(res) or Tagp(res) then
     if PieceSetp(res) then
        dic = "<Piece Set>"
     else
        dic = "<Tag>"
     end;
     ac = rhs
  end;
              
  return [. displayInConsole=dic, assignmentCode=ac .]
end;
              
var clearTmpFiles = fun()
   var P;
   var res = [];
   every func in Wub_GetFunctionsByTag("flag.tmpconsole") do
      Wub_DeleteFunction(func);
      res = res + [func]
   end;
   return res
end;
              
              
var main = fun()       
              
    input = Wub_ReplaceAll(Str_Trim(input), "|", `"`);
    if Str_StartsWith(input, "var ") then
       input = Select(input, 4, Size(input))
    end;
    if !Str_EndsWith(input, ";") then
       input = input + ";"
    end;
              
    if input == "clear;" then
       response = input
    elsif input == "bknext;" then
       wubvars["breakdone"] := true;
       response = "Releasing breakpoint"
    elsif input == "help;" then
       response = "HELP\n" + 
                  "  =====================\n" +
                  "  clear   Clear console\n" +
                  "  new     Clear vars\n" +
                  "  vars    See list of defined variables\n" +
                  "  bkvars  A variable representing breakpoint vars\n" +
                  "  bknext  Advance to next breakpoint\n" +
                  "  help    See list of commands\n";
    else
    
        var finfo = Wub_GetFunctionInfo(wubinfo.user + ".tmpConsole");
        if (finfo == nil) or (input == "new;") then
           finfo = [. module=wubinfo.user, name="tmpConsole", ctype="text", description="",
                      render="", test="", version="0", tags="", flags="", exec="", cacheexpire="", 
                      cacherefreshrate="", usecache="", createdby=wubinfo.user, modified="", 
                      created="", modifiedby=wubinfo.user,arglist="" .];
        end;
              
        
        try
           var varAssigned = checkVarAssigned(input);

           if input == "vars;" then
              response = ToString(varSet(finfo.description, varAssigned));
              return
           elsif input == "new;" then
              Wub_SaveFunctionInfo(finfo);
              var fils = clearTmpFiles();
              response = "Vars cleared.\n{}";
              return
           end;
              
           var bkvars = "var bkvars = " + ToString(wubvars.bkvars ? nil) + ";\n";
           var varInit = initVars(finfo.description, varAssigned);
           var res = WubEval(bkvars + varInit + finfo.exec + input);
           var respInfo = analyzeResponseType(res, varAssigned.rhs ? input);
           response = respInfo.displayInConsole;
        
           if (varAssigned != nil) then
              finfo.exec := finfo.exec + "// " + input + "\n" + varAssigned.name + varAssigned.op + respInfo.assignmentCode + ";\n";
              finfo.description = ToString(varSet(finfo.description, varAssigned));
              Wub_SaveFunctionInfo(finfo)
           end;
             
        catch E 
          on true do
    //          finfo.exec := finfo.exec + "\n" + ToString([. input=input, error=E .]);
    //          Wub_SaveFunctionInfo(finfo);
             response = E.type + ": ";
             var msg = WubEval(E.msg);
             msg = msg[0];
             msg = Select(msg, 0, Str_IndexOf("(", msg));
             response = response + msg
        end
    end;
end;
     
main();
               
NewPage(response, "text/html");