protected override Variable Evaluate(ParsingScript script) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 3, m_name); string varName = Utils.GetSafeString(args, 0); Variable lines = Utils.GetSafeVariable(args, 1); int fromLine = Utils.GetSafeInt(args, 2); string hash2 = Utils.GetSafeString(args, 3); string sepStr = Utils.GetSafeString(args, 4, "\t"); if (sepStr == "\\t") { sepStr = "\t"; } char[] sep = sepStr.ToCharArray(); var function = ParserFunction.GetFunction(varName, script); Variable mapVar = function != null?function.GetValue(script) : new Variable(Variable.VarType.ARRAY); for (int counter = fromLine; counter < lines.Tuple.Count; counter++) { Variable lineVar = lines.Tuple[counter]; Variable toAdd = new Variable(counter - fromLine); string line = lineVar.AsString(); var tokens = line.Split(sep); string hash = tokens[0]; mapVar.AddVariableToHash(hash, toAdd); if (!string.IsNullOrWhiteSpace(hash2) && !hash2.Equals(hash, StringComparison.OrdinalIgnoreCase)) { mapVar.AddVariableToHash(hash2, toAdd); } } ParserFunction.AddGlobalOrLocalVariable(varName, new GetVarFunction(mapVar)); return(Variable.EmptyInstance); }
protected override Variable Evaluate(ParsingScript script) { bool isList = false; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); Utils.CheckArgs(args.Count, 3, m_name); string varName = Utils.GetSafeString(args, 0); Variable lines = Utils.GetSafeVariable(args, 1); int fromLine = Utils.GetSafeInt(args, 2); string sepStr = Utils.GetSafeString(args, 3, "\t"); if (sepStr == "\\t") { sepStr = "\t"; } char[] sep = sepStr.ToCharArray(); var function = ParserFunction.GetFunction(varName); Variable allTokensVar = new Variable(Variable.VarType.ARRAY); for (int counter = fromLine; counter < lines.Tuple.Count; counter++) { Variable lineVar = lines.Tuple[counter]; Variable toAdd = new Variable(counter - fromLine); string line = lineVar.AsString(); var tokens = line.Split(sep); Variable tokensVar = new Variable(Variable.VarType.ARRAY); foreach (string token in tokens) { tokensVar.Tuple.Add(new Variable(token)); } allTokensVar.Tuple.Add(tokensVar); } ParserFunction.AddGlobalOrLocalVariable(varName, new GetVarFunction(allTokensVar)); return(Variable.EmptyInstance); }
protected override Variable Evaluate(ParsingScript script) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 2, m_name, true); string language = args[0].AsString(); string funcName = args[1].AsString(); ParserFunction function = ParserFunction.GetFunction(funcName); CustomFunction custFunc = function as CustomFunction; Utils.CheckNotNull(funcName, custFunc); string body = Utils.BeautifyScript(custFunc.Body, custFunc.Header); string translated = Translation.TranslateScript(body, language); Translation.PrintScript(translated); return(new Variable(translated)); }
protected override Variable Evaluate(ParsingScript script) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, m_name); string varName = Utils.GetSafeString(args, 0); Variable currentValue = Utils.GetSafeVariable(args, 1); if (currentValue == null) { currentValue = new Variable(""); } string scopeName = Path.GetFileName(script.Filename); ParserFunction.AddLocalScopeVariable(varName, scopeName, new GetVarFunction(currentValue)); return(currentValue); }
protected override Variable Evaluate(ParsingScript script) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, m_name, true); Variable id = args[0]; Utils.CheckPosInt(id); int processId = (int)id.Value; try { Process process = Process.GetProcessById(processId); process.Kill(); Interpreter.Instance.AppendOutput("Process " + processId + " killed", true); } catch (Exception exc) { throw new ArgumentException("Couldn't kill process " + processId + " (" + exc.Message + ")"); } return(Variable.EmptyInstance); }
protected override Variable Evaluate(ParsingScript script) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, m_name); var tableName = Utils.GetSafeString(args, 0); var colData = SQLQueryFunction.GetColumnData(tableName); if (colData == null || colData.Count == 0) { return(new Variable("")); } Variable results = new Variable(Variable.VarType.ARRAY); foreach (KeyValuePair <string, SqlDbType> entry in colData) { results.AddVariable(new Variable(entry.Key)); results.AddVariable(new Variable(entry.Value.ToString())); } return(results); }
protected override Variable Evaluate(ParsingScript script) { List <Variable> args = script.GetFunctionArgs(); CSCS_SQL.CheckConnectionString(script, m_name); switch (m_mode) { case Mode.SP_DESC: Utils.CheckArgs(args.Count, 1, m_name); var spName = "sp_helptext"; var argName = Utils.GetSafeString(args, 0); List <KeyValuePair <string, object> > spParams = new List <KeyValuePair <string, object> >() { new KeyValuePair <string, object>("@objname", argName) }; var results = SQLSPFunction.ExecuteSP(spName, spParams); if (results.Type == Variable.VarType.ARRAY && results.Tuple.Count >= 1 && results.Tuple[0].Type == Variable.VarType.ARRAY && results.Tuple[0].Count >= 1) { var r = results.Tuple[0].Tuple[0].AsString(); var res = System.Text.RegularExpressions.Regex.Replace(r, @"\s{2,}", " "); return(new Variable(res)); } return(results); case Mode.TABLES: return(RemoveListEntries(SQLQueryFunction.GetData("SELECT name FROM sysobjects WHERE xtype = 'U'", null, null, false))); case Mode.PROCEDURES: return(RemoveListEntries(SQLQueryFunction.GetData("SELECT NAME from SYS.PROCEDURES", null, null, false))); } return(Variable.EmptyInstance); }
protected override Variable Evaluate(ParsingScript script) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, m_name); CSCS_SQL.CheckConnectionString(script, m_name); var arg = Utils.GetSafeString(args, 0).Trim(); string statement = ""; switch (m_mode) { case Mode.DROP_DB: statement = "DROP DATABASE " + arg; break; case Mode.CREATE_DB: statement = "CREATE DATABASE " + arg; break; case Mode.DROP_TABLE: statement = "DROP TABLE " + arg; break; } int result = 0; using (SqlConnection con = new SqlConnection(CSCS_SQL.ConnectionString)) { using (SqlCommand cmd = new SqlCommand(statement, con)) { con.Open(); result = cmd.ExecuteNonQuery(); } } return(new Variable(result)); }
protected override Variable Evaluate(ParsingScript script) { bool isList = false; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); Utils.CheckArgs(args.Count, 1, m_name); string data = Utils.GetSafeString(args, 0); string sep = Utils.GetSafeString(args, 1, "\t"); if (sep == "\\t") { sep = "\t"; } var sepArray = sep.ToCharArray(); string[] tokens = data.Split(sepArray); var option = Utils.GetSafeString(args, 2); List <Variable> results = new List <Variable>(); for (int i = 0; i < tokens.Length; i++) { string token = tokens[i]; if (i > 0 && string.IsNullOrWhiteSpace(token) && option.StartsWith("prev", StringComparison.OrdinalIgnoreCase)) { token = tokens[i - 1]; } results.Add(new Variable(token)); } return(new Variable(results)); }
protected override async Task<Variable> EvaluateAsync(ParsingScript script) { List<Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 2, m_name); string method = args[0].AsString().ToUpper(); string uri = args[1].AsString(); string load = Utils.GetSafeString(args, 2); string tracking = Utils.GetSafeString(args, 3); string onSuccess = Utils.GetSafeString(args, 4); string onFailure = Utils.GetSafeString(args, 5, onSuccess); string contentType = Utils.GetSafeString(args, 6, "application/x-www-form-urlencoded"); Variable headers = Utils.GetSafeVariable(args, 7); int timeoutMs = Utils.GetSafeInt(args, 8, 10 * 1000); bool justFire = Utils.GetSafeInt(args, 9) > 0; if (!s_allowedMethods.Contains(method)) { throw new ArgumentException("Unknown web request method: " + method); } await ProcessWebRequestAsync(uri, method, load, onSuccess, onFailure, tracking, contentType, headers, timeoutMs, justFire); return Variable.EmptyInstance; }
protected override Variable Evaluate(ParsingScript script) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, m_name); string source = Utils.GetSafeString(args, 0); string argument = Utils.GetSafeString(args, 1); string parameter = Utils.GetSafeString(args, 2, "case"); int startFrom = Utils.GetSafeInt(args, 3, 0); int length = Utils.GetSafeInt(args, 4, source.Length); StringComparison comp = StringComparison.Ordinal; if (parameter.Equals("nocase") || parameter.Equals("no_case")) { comp = StringComparison.OrdinalIgnoreCase; } source = source.Replace("\\\"", "\""); argument = argument.Replace("\\\"", "\""); switch (m_mode) { case Mode.CONTAINS: return(new Variable(source.IndexOf(argument, comp) >= 0)); case Mode.STARTS_WITH: return(new Variable(source.StartsWith(argument, comp))); case Mode.ENDS_WITH: return(new Variable(source.EndsWith(argument, comp))); case Mode.INDEX_OF: return(new Variable(source.IndexOf(argument, startFrom, comp))); case Mode.EQUALS: return(new Variable(source.Equals(argument, comp))); case Mode.REPLACE: return(new Variable(source.Replace(argument, parameter))); case Mode.UPPER: return(new Variable(source.ToUpper())); case Mode.LOWER: return(new Variable(source.ToLower())); case Mode.TRIM: return(new Variable(source.Trim())); case Mode.SUBSTRING: startFrom = Utils.GetSafeInt(args, 1, 0); length = Utils.GetSafeInt(args, 2, source.Length); length = Math.Min(length, source.Length - startFrom); return(new Variable(source.Substring(startFrom, length))); case Mode.BEETWEEN: case Mode.BEETWEEN_ANY: int index1 = source.IndexOf(argument, comp); int index2 = m_mode == Mode.BEETWEEN ? source.IndexOf(parameter, index1 + 1, comp) : source.IndexOfAny(parameter.ToCharArray(), index1 + 1); startFrom = index1 + argument.Length; if (index1 < 0 || index2 < index1) { throw new ArgumentException("Couldn't extract string between [" + argument + "] and [" + parameter + "] + from " + source); } string result = source.Substring(startFrom, index2 - startFrom); return(new Variable(result)); } return(new Variable(-1)); }
Variable GetCoreProperty(string propName, ParsingScript script = null) { Variable result = Variable.EmptyInstance; if (m_propertyMap.TryGetValue(propName, out result) || m_propertyMap.TryGetValue(GetRealName(propName), out result)) { return(result); } else if (propName.Equals(Constants.OBJECT_PROPERTIES, StringComparison.OrdinalIgnoreCase)) { return(new Variable(GetProperties())); } else if (propName.Equals(Constants.OBJECT_TYPE, StringComparison.OrdinalIgnoreCase)) { return(new Variable((int)Type)); } else if (propName.Equals(Constants.SIZE, StringComparison.OrdinalIgnoreCase)) { return(new Variable(GetSize())); } else if (propName.Equals(Constants.UPPER, StringComparison.OrdinalIgnoreCase)) { return(new Variable(AsString().ToUpper())); } else if (propName.Equals(Constants.LOWER, StringComparison.OrdinalIgnoreCase)) { return(new Variable(AsString().ToLower())); } else if (propName.Equals(Constants.STRING, StringComparison.OrdinalIgnoreCase)) { return(new Variable(AsString())); } else if (propName.Equals(Constants.FIRST, StringComparison.OrdinalIgnoreCase)) { if (Tuple != null && Tuple.Count > 0) { return(Tuple[0]); } return(AsString().Length > 0 ? new Variable("" + AsString()[0]) : Variable.EmptyInstance); } else if (propName.Equals(Constants.LAST, StringComparison.OrdinalIgnoreCase)) { if (Tuple != null && Tuple.Count > 0) { return(Tuple.Last <Variable>()); } return(AsString().Length > 0 ? new Variable("" + AsString().Last <char>()) : Variable.EmptyInstance); } else if (script != null && propName.Equals(Constants.INDEX_OF, StringComparison.OrdinalIgnoreCase)) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, propName); string search = Utils.GetSafeString(args, 0); int startFrom = Utils.GetSafeInt(args, 1, 0); string param = Utils.GetSafeString(args, 2, "no_case"); StringComparison comp = param.Equals("case", StringComparison.OrdinalIgnoreCase) ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase; return(new Variable(AsString().IndexOf(search, startFrom, comp))); } else if (script != null && propName.Equals(Constants.SUBSTRING, StringComparison.OrdinalIgnoreCase)) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, propName); int startFrom = Utils.GetSafeInt(args, 0, 0); int length = Utils.GetSafeInt(args, 1, AsString().Length); length = Math.Min(length, AsString().Length - startFrom); return(new Variable(AsString().Substring(startFrom, length))); } else if (script != null && propName.Equals(Constants.REVERSE, StringComparison.OrdinalIgnoreCase)) { script.GetFunctionArgs(); if (Tuple != null) { Tuple.Reverse(); } else if (Type == VarType.STRING) { char[] charArray = AsString().ToCharArray(); Array.Reverse(charArray); String = new string(charArray); } return(this); } else if (script != null && propName.Equals(Constants.SORT, StringComparison.OrdinalIgnoreCase)) { script.GetFunctionArgs(); Sort(); return(this); } else if (script != null && propName.Equals(Constants.SPLIT, StringComparison.OrdinalIgnoreCase)) { List <Variable> args = script.GetFunctionArgs(); string sep = Utils.GetSafeString(args, 0, " "); var option = Utils.GetSafeString(args, 1); return(TokenizeFunction.Tokenize(AsString(), sep, option)); } else if (script != null && propName.Equals(Constants.JOIN, StringComparison.OrdinalIgnoreCase)) { List <Variable> args = script.GetFunctionArgs(); string sep = Utils.GetSafeString(args, 0, " "); if (Tuple == null) { return(new Variable(AsString())); } var join = string.Join(sep, Tuple); return(new Variable(join)); } else if (script != null && propName.Equals(Constants.ADD, StringComparison.OrdinalIgnoreCase)) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, propName); Variable var = Utils.GetSafeVariable(args, 0); if (Tuple != null) { Tuple.Add(var); } else if (Type == VarType.NUMBER) { Value += var.AsDouble(); } else { String += var.AsString(); } return(var); } else if (script != null && propName.Equals(Constants.AT, StringComparison.OrdinalIgnoreCase)) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, propName); int at = Utils.GetSafeInt(args, 0); if (Tuple != null && Tuple.Count > 0) { return(Tuple.Count > at ? Tuple[at] : Variable.EmptyInstance); } string str = AsString(); return(str.Length > at ? new Variable("" + str[at]) : Variable.EmptyInstance); } else if (script != null && propName.Equals(Constants.REPLACE, StringComparison.OrdinalIgnoreCase)) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 2, propName); string oldVal = Utils.GetSafeString(args, 0); string newVal = Utils.GetSafeString(args, 1); return(new Variable(AsString().Replace(oldVal, newVal))); } else if (propName.Equals(Constants.EMPTY_WHITE, StringComparison.OrdinalIgnoreCase)) { bool isEmpty = string.IsNullOrWhiteSpace(AsString()); return(new Variable(isEmpty)); } else if (script != null && propName.Equals(Constants.REPLACE_TRIM, StringComparison.OrdinalIgnoreCase)) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 2, propName); string currentValue = AsString(); for (int i = 0; i < args.Count; i += 2) { string oldVal = Utils.GetSafeString(args, i); string newVal = Utils.GetSafeString(args, i + 1); currentValue = currentValue.Replace(oldVal, newVal); } return(new Variable(currentValue.Trim())); } else if (script != null && propName.Equals(Constants.CONTAINS, StringComparison.OrdinalIgnoreCase)) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, propName); string val = Utils.GetSafeString(args, 0); string param = Utils.GetSafeString(args, 1, "no_case"); StringComparison comp = param.Equals("case", StringComparison.OrdinalIgnoreCase) ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase; bool contains = false; if (Type == Variable.VarType.ARRAY) { string lower = val.ToLower(); contains = m_dictionary != null && m_dictionary.ContainsKey(lower); if (!contains && Tuple != null) { foreach (var item in Tuple) { if (item.AsString().Equals(val, comp)) { contains = true; break; } } } } else { contains = val != "" && AsString().IndexOf(val, comp) >= 0; } return(new Variable(contains)); } else if (script != null && propName.Equals(Constants.EQUALS, StringComparison.OrdinalIgnoreCase)) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, propName); string val = Utils.GetSafeString(args, 0); string param = Utils.GetSafeString(args, 1, "no_case"); StringComparison comp = param.Equals("case", StringComparison.OrdinalIgnoreCase) ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase; return(new Variable(AsString().Equals(val, comp))); } else if (script != null && propName.Equals(Constants.STARTS_WITH, StringComparison.OrdinalIgnoreCase)) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, propName); string val = Utils.GetSafeString(args, 0); string param = Utils.GetSafeString(args, 1, "no_case"); StringComparison comp = param.Equals("case", StringComparison.OrdinalIgnoreCase) ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase; return(new Variable(AsString().StartsWith(val, comp))); } else if (script != null && propName.Equals(Constants.ENDS_WITH, StringComparison.OrdinalIgnoreCase)) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, propName); string val = Utils.GetSafeString(args, 0); string param = Utils.GetSafeString(args, 1, "no_case"); StringComparison comp = param.Equals("case", StringComparison.OrdinalIgnoreCase) ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase; return(new Variable(AsString().EndsWith(val, comp))); } else if (script != null && propName.Equals(Constants.TRIM, StringComparison.OrdinalIgnoreCase)) { script.GetFunctionArgs(); return(new Variable(AsString().Trim())); } else if (propName.Equals(Constants.KEYS, StringComparison.OrdinalIgnoreCase)) { List <Variable> results = GetAllKeys(); return(new Variable(results)); } return(result); }
protected override Variable Evaluate(ParsingScript script) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 3, m_name); var tableName = Utils.GetSafeString(args, 0).Trim(); var colsStr = Utils.GetSafeString(args, 1).Trim().ToLower(); if (string.IsNullOrWhiteSpace(SqlLiteFunction.DBPath)) { throw new ArgumentException("DB has not been initialized."); } var colData = SqlLiteQueryFunction.GetColumnData(tableName); if (colData == null || colData.Count == 0) { return(new Variable("Error: table [" + tableName + "] doesn't exist.")); } var queryStatement = "INSERT INTO " + tableName + " (" + colsStr + ") VALUES ("; //@a,@b,@c);" var cols = colsStr.Split(','); for (int i = 0; i < cols.Length; i++) { if (string.IsNullOrWhiteSpace(cols[i]) || !colData.Keys.Contains(cols[i])) { return(new Variable("Error: column [" + cols[i] + "] doesn't exist.")); } queryStatement += "@" + cols[i] + ","; } queryStatement = queryStatement.Remove(queryStatement.Length - 1) + ")";; var valsVariable = args[2]; if (valsVariable.Type != Variable.VarType.ARRAY || valsVariable.Tuple.Count < cols.Length) { return(new Variable("Error: not enough values (" + valsVariable.Tuple.Count + ") given for " + cols.Length + " columns.")); } try { using (var connection = new SqliteConnection("Data Source=" + SqlLiteFunction.DBPath)) { connection.Open(); using (var cmd = new SqliteCommand(queryStatement, connection)) { for (int i = 0; i < cols.Length; i++) { var varName = "@" + cols[i]; var varType = colData[cols[i]]; cmd.Parameters.Add(varName, varType); cmd.Parameters[varName].Value = SqlLiteQueryFunction.SqlDbTypeToType(varType, valsVariable.Tuple[i]); } cmd.ExecuteNonQuery(); } } } catch (Exception exc) { throw new ArgumentException("Couldn't run [" + queryStatement + "] on DB " + SqlLiteFunction.DBPath + " :" + exc.Message); } return(new Variable("Inserted new row.")); }