public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var filename = args.GetString("filename").GetValue();
            var encoding = args.GetString("encoding", "UTF-8").GetValue();

            return(new ValueInput(new StreamReader(filename, encoding == "UTF-8" ? Encoding.UTF8 : Encoding.GetEncoding(encoding))));
        }
示例#2
0
 public override Value Execute(Args args, Environment environment, SourcePos pos)
 {
     if (args.IsNull("str"))
     {
         return(ValueBoolean.FALSE);
     }
     return(ValueBoolean.From(args.GetString("str").GetValue().Contains(args.GetString("part").GetValue())));
 }
示例#3
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var filename = args.GetString("filename").GetValue();
            var encoding = args.GetString("encoding", "UTF-8").GetValue();
            var append   = ValueBoolean.FALSE;

            if (args.HasArg("append"))
            {
                append = args.Get("append").AsBoolean();
            }
            return(new ValueOutput(new StreamWriter(filename, append.GetValue(), encoding == "UTF-8" ? Encoding.UTF8 : Encoding.GetEncoding(encoding))));
        }
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var    nativeName = args.GetString("native").GetValue();
            string alias      = null;

            if (args.HasArg("alias"))
            {
                alias = args.GetString("alias").GetValue();
            }
            BindNative.Bind(environment, nativeName, alias, pos);
            return(ValueNull.NULL);
        }
示例#5
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var src  = args.GetString("src").GetValue();
            var dest = args.GetString("dest").GetValue();

            try
            {
                File.Copy(src, dest, true);
            }
            catch
            {
                throw new ControlErrorException(new ValueString("ERROR"), "Cannot copy " + src + " to " + dest, pos);
            }
            return(ValueNull.NULL);
        }
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var    program   = args.GetString("program").GetValue();
            var    arguments = args.GetList("args").GetValue();
            string work_dir  = null;

            if (args.HasArg("work_dir"))
            {
                work_dir = args.GetString("work_dir").GetValue();
            }
            var echo = false;

            if (args.HasArg("echo"))
            {
                echo = args.GetBoolean("echo").GetValue();
            }
            try
            {
                var list = new StringBuilder();
                list.Append(program).Append(" ");
                foreach (var argument in arguments)
                {
                    list.Append(argument.AsString().GetValue()).Append(" ");
                }
                list.Remove(list.Length - 1, 1);
                var process = new Process();
                var info    = process.StartInfo;
                info.FileName  = program;
                info.Arguments = list.ToString();
                if (work_dir != null)
                {
                    info.WorkingDirectory = work_dir;
                }
                info.CreateNoWindow         = true;
                info.RedirectStandardOutput = true;
                info.RedirectStandardError  = true;
                info.RedirectStandardInput  = true;
                if (echo)
                {
                    System.Console.WriteLine(program + " " + list);
                }
                process.Start();
                process.WaitForExit();
                return(new ValueInt(process.ExitCode));
            } catch (Exception e) {
                throw new ControlErrorException(new ValueString("ERROR"), "Cannot execute " + program + ": " + e.Message, pos);
            }
        }
示例#7
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var filename = args.GetString("filename").GetValue();

            if (File.Exists(filename))
            {
                var result = new ValueObject();
                var info   = new FileInfo(filename);
                result.AddItem("size", new ValueInt(info.Length));
                result.AddItem("is_dir", ValueBoolean.FALSE);
                result.AddItem("modified", new ValueDate(info.LastWriteTime));
                result.AddItem("created", new ValueDate(info.CreationTime));
                return(result);
            }

            if (Directory.Exists(filename))
            {
                var result = new ValueObject();
                var info   = new DirectoryInfo(filename);
                result.AddItem("is_dir", ValueBoolean.TRUE);
                result.AddItem("modified", new ValueDate(info.LastWriteTime));
                result.AddItem("created", new ValueDate(info.CreationTime));
                return(result);
            }

            return(ValueNull.NULL);
        }
示例#8
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("str"))
            {
                return(ValueNull.NULL);
            }
            var value = args.GetString("str").GetValue();
            var start = (int)args.GetInt("startidx").GetValue();

            if (start < 0)
            {
                start = value.Length + start;
            }
            if (start > value.Length)
            {
                return(new ValueString(""));
            }
            var end = (int)args.GetInt("endidx", value.Length).GetValue();

            if (end < 0)
            {
                end = value.Length + end;
            }
            if (end > value.Length)
            {
                end = value.Length;
            }
            return(new ValueString(value.Substring(start, end - start)));
        }
示例#9
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var dir       = args.GetString("dir").GetValue();
            var recursive = false;

            if (args.HasArg("recursive"))
            {
                recursive = args.GetBoolean("recursive").GetValue();
            }
            var include_path = recursive;

            if (args.HasArg("include_path"))
            {
                include_path = args.GetBoolean("include_path").GetValue();
            }
            var include_dirs = false;

            if (args.HasArg("include_dirs"))
            {
                include_dirs = args.GetBoolean("include_dirs").GetValue();
            }
            var result = new ValueList();

            CollectFiles(dir, recursive, include_path, include_dirs, result);
            return(result);
        }
示例#10
0
 public override Value Execute(Args args, Environment environment, SourcePos pos)
 {
     if (args.IsNull("str"))
     {
         return(ValueBoolean.FALSE);
     }
     return(ValueBoolean.From(args.GetString("str").Matches(args.GetAsPattern("pattern"))));
 }
示例#11
0
 public override Value Execute(Args args, Environment environment, SourcePos pos)
 {
     try {
         return(JSON.Parse(args.GetString("s").GetValue()));
     } catch (Exception) {
         throw new ControlErrorException(new ValueString("ERROR"), "Cannot parse string as JSON", pos);
     }
 }
示例#12
0
 public override Value Execute(Args args, Environment environment, SourcePos pos)
 {
     if (args.IsNull("ch"))
     {
         return(ValueNull.NULL);
     }
     return(new ValueInt(char.ConvertToUtf32(args.GetString("ch").GetValue(), 0)));
 }
示例#13
0
 public override Value Execute(Args args, Environment environment, SourcePos pos)
 {
     if (args.IsNull("str"))
     {
         return(ValueNull.NULL);
     }
     return(new ValueString(args.GetString("str").GetValue().ToUpper()));
 }
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("s"))
            {
                return(ValueNull.NULL);
            }
            var value = args.GetString("s").GetValue();

            return(new ValueString(value.Replace("|", "\\|").Replace(".", "\\.")));
        }
示例#15
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var src  = args.GetString("src").GetValue();
            var dest = args.GetString("dest").GetValue();

            try
            {
                if (File.Exists(dest))
                {
                    File.Delete(dest);
                }
                File.Move(src, dest);
            }
            catch
            {
                throw new ControlErrorException(new ValueString("ERROR"), "Cannot move " + src + " to " + dest, pos);
            }
            return(ValueNull.NULL);
        }
示例#16
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.Get(("s")).IsNode())
            {
                return(args.GetAsNode("s").GetValue().Evaluate(environment));
            }
            var node = Parser.Parse(args.GetString("s").GetValue(), pos.filename);

            return(node.Evaluate(environment));
        }
示例#17
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("str"))
            {
                return(ValueNull.NULL);
            }
            var str  = args.GetString("str").GetValue();
            var fmts = new List <string>();

            if (args.HasArg("fmt"))
            {
                if (args.Get("fmt").IsList())
                {
                    foreach (var fmt_ in args.GetList("fmt").GetValue())
                    {
                        fmts.Add(fmt_.AsString().GetValue());
                    }
                }
                else
                {
                    fmts.Add(args.GetString("fmt").GetValue());
                }
            }
            else
            {
                fmts.Add("yyyyMMdd");
            }

            foreach (var fmt in fmts)
            {
                try
                {
                    return(new ValueDate(DateTime.ParseExact(str, fmt, DateTimeFormatInfo.InvariantInfo)));
                }
                catch
                {
                    // continue;
                }
            }

            return(ValueNull.NULL);
        }
示例#18
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("date"))
            {
                return(ValueNull.NULL);
            }
            var value = args.GetDate("date");
            var date  = value.AsDate().GetValue();
            var fmt   = args.HasArg("fmt") ? args.GetString("fmt").GetValue() : "yyyy-MM-dd HH:mm:ss";

            return(new ValueString(date.ToString(fmt, DateTimeFormatInfo.InvariantInfo)));
        }
示例#19
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var file = args.GetString("file").GetValue();

            try
            {
                return(interpreter.Interpret(File.ReadAllText(file), file));
            }
            catch (FileNotFoundException)
            {
                throw new ControlErrorException(new ValueString("ERROR"), "File " + file + " not found", pos);
            }
        }
示例#20
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var filename = args.GetString("filename").GetValue();

            try
            {
                File.Delete(filename);
            }
            catch
            {
                throw new ControlErrorException(new ValueString("ERROR"), "Cannot delete file " + filename, pos);
            }
            return(ValueNull.NULL);
        }
示例#21
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("s"))
            {
                return(ValueNull.NULL);
            }
            var s     = args.GetString("s").GetValue();
            var a     = args.GetString("a").GetValue();
            var b     = args.GetString("b").GetValue();
            var start = args.GetInt("start", 0L).GetValue();

            if (start >= s.Length)
            {
                return(args.GetString("s"));
            }
            if (start == 0)
            {
                return(new ValueString(s.Replace(a, b)));
            }
            var prefix = s.Substring(0, (int)start);

            return(new ValueString(prefix + s.Substring((int)start).Replace(a, b)));
        }
示例#22
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("str"))
            {
                return(ValueNull.NULL);
            }
            var value = args.GetString("str").GetValue();
            var delim = "[ \\t]+";

            if (args.HasArg("delim"))
            {
                delim = args.GetAsString("delim").GetValue();
            }
            return(SplitValue(value, delim));
        }
示例#23
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("str"))
            {
                return(ValueNull.NULL);
            }
            var value  = args.GetString("str").GetValue();
            var sep1   = args.GetAsString("sep1").GetValue();
            var sep2   = args.GetAsString("sep2").GetValue();
            var result = FuncSplit.SplitValue(value, sep1);
            var list   = result.GetValue();

            for (var i = 0; i < list.Count; i++)
            {
                list[i] = FuncSplit.SplitValue(list[i].AsString().GetValue(), sep2);
            }
            return(result);
        }
示例#24
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var dir = args.GetString("dir").GetValue();

            if (Directory.Exists(dir))
            {
                return(ValueNull.NULL);
            }
            var with_parents = false;

            if (args.HasArg("with_parents"))
            {
                with_parents = args.GetBoolean("with_parents").GetValue();
            }
            if (with_parents)
            {
                try
                {
                    Directory.CreateDirectory(dir);
                }
                catch
                {
                    throw new ControlErrorException(new ValueString("ERROR"), "Cannot create directory " + dir, pos);
                }
            }
            else
            {
                if (!Directory.GetParent(dir).Exists)
                {
                    throw new ControlErrorException(new ValueString("ERROR"), "Cannot create directory " + dir, pos);
                }

                try
                {
                    Directory.CreateDirectory(dir);
                }
                catch
                {
                    throw new ControlErrorException(new ValueString("ERROR"), "Cannot create directory " + dir, pos);
                }
            }
            return(ValueNull.NULL);
        }
示例#25
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("obj"))
            {
                return(ValueNull.NULL);
            }
            var obj = args.Get("obj");
            var key = args.HasArg("key") ? args.GetFunc("key") : null;

            if (obj.IsString())
            {
                var str   = obj.AsString().GetValue();
                var start = (int)args.GetInt("start", str.Length - 1).GetValue();
                var part  = args.GetString("part").GetValue();
                return(new ValueInt(str.LastIndexOf(part, start, StringComparison.Ordinal)));
            }
            if (obj.IsList())
            {
                var env = environment;
                if (key != null)
                {
                    env = environment.NewEnv();
                }
                var item  = args.Get("part");
                var list  = obj.AsList().GetValue();
                var start = (int)args.GetInt("start", list.Count - 1).GetValue();
                for (var idx = start; idx >= 0; idx--)
                {
                    var elem = list[idx];
                    if (key != null)
                    {
                        elem = key.Execute(new Args(key.GetArgNames()[0], elem, pos), env, pos);
                    }
                    if (elem.IsEquals(item))
                    {
                        return(new ValueInt(idx));
                    }
                }
                return(new ValueInt(-1));
            }
            throw new ControlErrorException(new ValueString("ERROR"), "Find_last only works with strings and lists", pos);
        }
示例#26
0
 public override Value Execute(Args args, Environment environment, SourcePos pos)
 {
     return(new ValueNode(Parser.Parse(args.GetString("s").GetValue(), pos.filename)));
 }
示例#27
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            var var = args.GetString("var").GetValue();

            return(new ValueString(System.Environment.GetEnvironmentVariable(var)));
        }
示例#28
0
 public override Value Execute(Args args, Environment environment, SourcePos pos)
 {
     return(new ValueInput(new StringReader(args.GetString("str").GetValue())));
 }
示例#29
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("fmt"))
            {
                return(ValueNull.NULL);
            }
            var fmt       = args.GetString("fmt").GetValue();
            var arguments = args.Get("args...").AsList().GetValue();
            var result    = "";
            var lastidx   = 0;
            var pattern   = "\\{([0-9]+)(#[^}]+)?\\}";

            foreach (Match match in Regex.Matches(fmt, pattern))
            {
                var start = match.Groups[0].Index;
                var end   = start + match.Groups[0].Length;
                var var   = int.Parse(match.Groups[1].Value);
                var spec  = match.Groups[2].Value;

                var width   = 0;
                var zeroes  = false;
                var leading = true;
                int numbase = 10;
                var digits  = -1;

                if (spec != null && spec != "")
                {
                    spec = spec.Substring(1); // skip #
                    if (spec.StartsWith("-"))
                    {
                        leading = false;
                        spec    = spec.Substring(1);
                    }
                    if (spec.StartsWith("0"))
                    {
                        zeroes  = true;
                        leading = false;
                        spec    = spec.Substring(1);
                    }
                    if (spec.EndsWith("x"))
                    {
                        numbase = 16;
                        spec    = spec.Substring(0, spec.Length - 1);
                    }
                    int idx = spec.IndexOf('.');
                    if (idx == -1)
                    {
                        digits = -1;
                        width  = int.Parse(spec == "" ? "0" : spec);
                    }
                    else
                    {
                        digits = int.Parse(spec.Substring(idx + 1));
                        width  = idx == 0 ? 0 : int.Parse(spec.Substring(0, idx));
                    }
                }

                string value;
                if (numbase != 10)
                {
                    value = string.Format("{0:x}", arguments[var].AsInt().GetValue());
                }
                else if (digits != -1)
                {
                    value = string.Format("{0:f" + digits + "}", arguments[var].AsDecimal().GetValue());
                }
                else
                {
                    value = arguments[var].AsString().GetValue();
                }
                while (value.Length < width)
                {
                    if (leading)
                    {
                        value = ' ' + value;
                    }
                    else if (zeroes)
                    {
                        value = '0' + value;
                    }
                    else
                    {
                        value = value + ' ';
                    }
                }

                if (lastidx < start)
                {
                    result += fmt.Substring(lastidx, start - lastidx);
                }
                result += value;
                lastidx = end;
            }
            if (lastidx < fmt.Length)
            {
                result += fmt.Substring(lastidx);
            }
            return(new ValueString(result));
        }
示例#30
0
        public override Value Execute(Args args, Environment environment, SourcePos pos)
        {
            if (args.IsNull("str"))
            {
                return(ValueNull.NULL);
            }
            var str    = args.GetString("str").GetValue();
            var start_ = (int)args.GetInt("start", 0).GetValue();

            if (start_ < 0)
            {
                start_ = str.Length + start_;
            }

            var result = "";

            if (start_ > 0)
            {
                result = str.Substring(0, start_);
                str    = str.Substring(start_);
            }

            var lastidx = 0;
            var pattern = "\\{([^#{}]+)(#-?[0-9.]*x?)?\\}";

            foreach (Match match in Regex.Matches(str, pattern))
            {
                var start = match.Groups[0].Index;
                var end   = start + match.Groups[0].Length;
                var expr  = match.Groups[1].Value;
                var spec  = match.Groups[2].Value;

                var width   = 0;
                var zeroes  = false;
                var leading = true;
                int numbase = 10;
                var digits  = -1;

                if (spec != null && spec != "")
                {
                    spec = spec.Substring(1); // skip #
                    if (spec.StartsWith("-"))
                    {
                        leading = false;
                        spec    = spec.Substring(1);
                    }
                    if (spec.StartsWith("0"))
                    {
                        zeroes  = true;
                        leading = false;
                        spec    = spec.Substring(1);
                    }
                    if (spec.EndsWith("x"))
                    {
                        numbase = 16;
                        spec    = spec.Substring(0, spec.Length - 1);
                    }
                    int idx = spec.IndexOf('.');
                    if (idx == -1)
                    {
                        digits = -1;
                        width  = int.Parse(spec == "" ? "0" : spec);
                    }
                    else
                    {
                        digits = int.Parse(spec.Substring(idx + 1));
                        width  = idx == 0 ? 0 : int.Parse(spec.Substring(0, idx));
                    }
                }

                string value = "";
                try {
                    var node = Parser.Parse(expr, pos.filename);
                    var val  = node.Evaluate(environment);
                    if (numbase != 10)
                    {
                        value = string.Format("{0:x}", val.AsInt().GetValue());
                    }
                    else if (digits != -1)
                    {
                        value = string.Format("{0:f" + digits + "}", val.AsDecimal().GetValue());
                    }
                    else
                    {
                        value = val.AsString().GetValue();
                    }
                    while (value.Length < width)
                    {
                        if (leading)
                        {
                            value = ' ' + value;
                        }
                        else if (zeroes)
                        {
                            value = '0' + value;
                        }
                        else
                        {
                            value = value + ' ';
                        }
                    }
                } catch (Exception) {
                    // ignore
                }

                if (lastidx < start)
                {
                    result += str.Substring(lastidx, start - lastidx);
                }
                result += value;
                lastidx = end;
            }
            if (lastidx < str.Length)
            {
                result += str.Substring(lastidx);
            }
            return(new ValueString(result));
        }