示例#1
0
文件: Base64.cs 项目: jdruin/F5Eagle
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        string subCommand = arguments[1];
                        bool   tried      = false;

                        code = ScriptOps.TryExecuteSubCommandFromEnsemble(
                            interpreter, this, clientData, arguments, true,
                            false, ref subCommand, ref tried, ref result);

                        if ((code == ReturnCode.Ok) && !tried)
                        {
                            switch (subCommand)
                            {
                            case "decode":
                            {
                                if (arguments.Count >= 3)
                                {
                                    OptionDictionary options = new OptionDictionary(
                                        new IOption[] {
                                            new Option(null, OptionFlags.MustHaveEncodingValue, Index.Invalid, Index.Invalid, "-encoding", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                        });

                                    int argumentIndex = Index.Invalid;

                                    code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        if ((argumentIndex != Index.Invalid) &&
                                            ((argumentIndex + 1) == arguments.Count))
                                        {
                                            Variant  value    = null;
                                            Encoding encoding = null;

                                            if (options.IsPresent("-encoding", ref value))
                                            {
                                                encoding = (Encoding)value.Value;
                                            }

                                            if (code == ReturnCode.Ok)
                                            {
                                                try
                                                {
                                                    string stringValue = null;

                                                    code = StringOps.GetString(encoding,
                                                                               Convert.FromBase64String(arguments[argumentIndex]),
                                                                               EncodingType.Binary, ref stringValue, ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        result = stringValue;
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    Engine.SetExceptionErrorCode(interpreter, e);

                                                    result = e;
                                                    code   = ReturnCode.Error;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                Option.LooksLikeOption(arguments[argumentIndex]))
                                            {
                                                result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                            }
                                            else
                                            {
                                                result = "wrong # args: should be \"base64 decode ?options? string\"";
                                            }

                                            code = ReturnCode.Error;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"base64 decode ?options? string\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "encode":
                            {
                                if (arguments.Count >= 3)
                                {
                                    OptionDictionary options = new OptionDictionary(
                                        new IOption[] {
                                            new Option(null, OptionFlags.MustHaveEncodingValue, Index.Invalid, Index.Invalid, "-encoding", null),
                                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                                        });

                                    int argumentIndex = Index.Invalid;

                                    code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        if ((argumentIndex != Index.Invalid) && ((argumentIndex + 1) == arguments.Count))
                                        {
                                            Variant  value    = null;
                                            Encoding encoding = null;

                                            if (options.IsPresent("-encoding", ref value))
                                            {
                                                encoding = (Encoding)value.Value;
                                            }

                                            if (code == ReturnCode.Ok)
                                            {
                                                try
                                                {
                                                    byte[] bytes = null;

                                                    code = StringOps.GetBytes(
                                                        encoding, arguments[argumentIndex],
                                                        EncodingType.Binary, ref bytes, ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        result = Convert.ToBase64String(bytes,
                                                                                        Base64FormattingOptions.InsertLineBreaks);
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    Engine.SetExceptionErrorCode(interpreter, e);

                                                    result = e;
                                                    code   = ReturnCode.Error;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                Option.LooksLikeOption(arguments[argumentIndex]))
                                            {
                                                result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                            }
                                            else
                                            {
                                                result = "wrong # args: should be \"base64 encode ?options? string\"";
                                            }

                                            code = ReturnCode.Error;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"base64 encode ?options? string\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            default:
                            {
                                result = ScriptOps.BadSubCommand(
                                    interpreter, null, null, subCommand, this, null, null);

                                code = ReturnCode.Error;
                                break;
                            }
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"base64 option ?arg ...?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
示例#2
0
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if ((arguments.Count == 2) || (arguments.Count == 3))
                    {
                        string  channelId = arguments[1];
                        Channel channel   = interpreter.GetChannel(channelId, ref result);

                        if (channel != null)
                        {
                            Encoding encoding = null;

                            if (interpreter.GetChannelEncoding(channel, ref encoding) == ReturnCode.Ok)
                            {
                                try
                                {
                                    ByteList buffer = null;

                                    code = channel.Read(ref buffer, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        string stringValue = null;

                                        code = StringOps.GetString(
                                            encoding, buffer.ToArray(), EncodingType.Binary,
                                            ref stringValue, ref result);

                                        if (code == ReturnCode.Ok)
                                        {
                                            if (arguments.Count == 3)
                                            {
                                                code = interpreter.SetVariableValue(
                                                    VariableFlags.None, arguments[2], stringValue,
                                                    null, ref result);

                                                if (code == ReturnCode.Ok)
                                                {
                                                    int length = stringValue.Length;

                                                    if (length > 0)
                                                    {
                                                        result = length;
                                                    }
                                                    else
                                                    {
                                                        bool canSeek = channel.CanSeek;

                                                        if ((canSeek && channel.EndOfStream) ||
                                                            (!canSeek && channel.HitEndOfStream))
                                                        {
                                                            result = Channel.EndOfFile;
                                                        }
                                                        else
                                                        {
                                                            result = length; /* ZERO */
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                result = stringValue;
                                            }
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Engine.SetExceptionErrorCode(interpreter, e);

                                    result = e;
                                    code   = ReturnCode.Error;
                                }
                            }
                            else
                            {
                                result = String.Format(
                                    "failed to get encoding for channel \"{0}\"",
                                    channelId);

                                code = ReturnCode.Error;
                            }
                        }
                        else
                        {
                            code = ReturnCode.Error;
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"gets channelId ?varName?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
示例#3
0
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        string subCommand = arguments[1];
                        bool   tried      = false;

                        code = ScriptOps.TryExecuteSubCommandFromEnsemble(
                            interpreter, this, clientData, arguments, true,
                            false, ref subCommand, ref tried, ref result);

                        if ((code == ReturnCode.Ok) && !tried)
                        {
                            switch (subCommand)
                            {
                            case "deserialize":
                            {
                                if (arguments.Count >= 4)
                                {
#if SERIALIZATION
                                    OptionDictionary options = ObjectOps.GetDeserializeOptions();

                                    int argumentIndex = Index.Invalid;

                                    code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        if ((argumentIndex != Index.Invalid) && ((argumentIndex + 2) == arguments.Count))
                                        {
                                            bool verbose;
                                            bool strictType;
                                            bool noCase;

                                            ObjectOps.ProcessGetTypeOptions(
                                                options, out verbose, out strictType, out noCase);

                                            Type        returnType;
                                            ObjectFlags objectFlags;
                                            string      objectName;
                                            string      interpName;
                                            bool        create;
                                            bool        dispose;
                                            bool        alias;
                                            bool        aliasRaw;
                                            bool        aliasAll;
                                            bool        aliasReference;
                                            bool        toString;

                                            ObjectOps.ProcessFixupReturnValueOptions(
                                                options, null, out returnType, out objectFlags,
                                                out objectName, out interpName, out create,
                                                out dispose, out alias, out aliasRaw, out aliasAll,
                                                out aliasReference, out toString);

                                            if (noCase)
                                            {
                                                objectFlags |= ObjectFlags.NoCase;
                                            }

                                            Variant  value    = null;
                                            Encoding encoding = null;

                                            if (options.IsPresent("-encoding", ref value))
                                            {
                                                encoding = (Encoding)value.Value;
                                            }

                                            if (code == ReturnCode.Ok)
                                            {
                                                Type       objectType = null;
                                                ResultList errors     = null;

                                                code = Value.GetType(interpreter,
                                                                     arguments[argumentIndex], null, interpreter.GetAppDomain(),
                                                                     Value.GetTypeValueFlags(strictType, verbose, noCase),
                                                                     interpreter.CultureInfo, ref objectType, ref errors);

                                                if (code == ReturnCode.Ok)
                                                {
                                                    byte[] bytes = null;

                                                    code = StringOps.GetBytes(
                                                        encoding, arguments[argumentIndex + 1],
                                                        EncodingType.Default, ref bytes, ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        object @object = null;

                                                        code = XmlOps.Deserialize(
                                                            objectType, bytes, ref @object, ref result);

                                                        if (code == ReturnCode.Ok)
                                                        {
                                                            ObjectOptionType objectOptionType =
                                                                ObjectOptionType.Deserialize |
                                                                ObjectOps.GetOptionType(aliasRaw, aliasAll);

                                                            code = MarshalOps.FixupReturnValue(
                                                                interpreter, interpreter.Binder, interpreter.CultureInfo,
                                                                returnType, objectFlags, ObjectOps.GetInvokeOptions(
                                                                    objectOptionType), objectOptionType, objectName, interpName,
                                                                @object, create, dispose, alias, aliasReference, toString,
                                                                ref result);
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    errors.Insert(0, String.Format(
                                                                      "type \"{0}\" not found",
                                                                      arguments[argumentIndex]));

                                                    result = errors;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                Option.LooksLikeOption(arguments[argumentIndex]))
                                            {
                                                result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                            }
                                            else
                                            {
                                                result = "wrong # args: should be \"xml deserialize ?options? type xml\"";
                                            }

                                            code = ReturnCode.Error;
                                        }
                                    }
#else
                                    result = "not implemented";
                                    code   = ReturnCode.Error;
#endif
                                }
                                else
                                {
                                    result = "wrong # args: should be \"xml deserialize ?options? type xml\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "serialize":
                            {
                                if (arguments.Count >= 4)
                                {
#if SERIALIZATION
                                    OptionDictionary options = ObjectOps.GetSerializeOptions();

                                    int argumentIndex = Index.Invalid;

                                    code = interpreter.GetOptions(options, arguments, 0, 2, Index.Invalid, true, ref argumentIndex, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        if ((argumentIndex != Index.Invalid) && ((argumentIndex + 2) == arguments.Count))
                                        {
                                            bool noCase = false;

                                            if (options.IsPresent("-nocase"))
                                            {
                                                noCase = true;
                                            }

                                            bool strictType = false;

                                            if (options.IsPresent("-stricttype"))
                                            {
                                                strictType = true;
                                            }

                                            bool verbose = false;

                                            if (options.IsPresent("-verbose"))
                                            {
                                                verbose = true;
                                            }

                                            Variant  value    = null;
                                            Encoding encoding = null;

                                            if (options.IsPresent("-encoding", ref value))
                                            {
                                                encoding = (Encoding)value.Value;
                                            }

                                            if (code == ReturnCode.Ok)
                                            {
                                                Type       objectType = null;
                                                ResultList errors     = null;

                                                code = Value.GetType(interpreter,
                                                                     arguments[argumentIndex], null, interpreter.GetAppDomain(),
                                                                     Value.GetTypeValueFlags(strictType, verbose, noCase),
                                                                     interpreter.CultureInfo, ref objectType, ref errors);

                                                if (code == ReturnCode.Ok)
                                                {
                                                    IObject @object = null;

                                                    code = interpreter.GetObject(
                                                        arguments[argumentIndex + 1], LookupFlags.Default,
                                                        ref @object, ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        byte[] bytes = null;

                                                        code = XmlOps.Serialize(
                                                            (@object != null) ? @object.Value : null,
                                                            objectType, null, ref bytes, ref result);

                                                        if (code == ReturnCode.Ok)
                                                        {
                                                            string stringValue = null;

                                                            code = StringOps.GetString(
                                                                encoding, bytes, EncodingType.Default,
                                                                ref stringValue, ref result);

                                                            if (code == ReturnCode.Ok)
                                                            {
                                                                result = stringValue;
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    errors.Insert(0, String.Format(
                                                                      "type \"{0}\" not found",
                                                                      arguments[argumentIndex]));

                                                    result = errors;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if ((argumentIndex != Index.Invalid) &&
                                                Option.LooksLikeOption(arguments[argumentIndex]))
                                            {
                                                result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                            }
                                            else
                                            {
                                                result = "wrong # args: should be \"xml serialize ?options? type object\"";
                                            }

                                            code = ReturnCode.Error;
                                        }
                                    }
#else
                                    result = "not implemented";
                                    code   = ReturnCode.Error;
#endif
                                }
                                else
                                {
                                    result = "wrong # args: should be \"xml serialize ?options? type object\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "validate":
                            {
                                if (arguments.Count == 4)
                                {
                                    XmlDocument document = null;

                                    code = XmlOps.LoadString(
                                        arguments[3], ref document, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        code = XmlOps.Validate(
                                            arguments[2], document, ref result);

                                        if (code == ReturnCode.Ok)
                                        {
                                            result = String.Empty;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"xml validate schemaXml documentXml\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            default:
                            {
                                result = ScriptOps.BadSubCommand(
                                    interpreter, null, null, subCommand, this, null, null);

                                code = ReturnCode.Error;
                                break;
                            }
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"xml option ?arg ...?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
示例#4
0
文件: Fcopy.cs 项目: jdruin/F5Eagle
        ///////////////////////////////////////////////////////////////////////////////////////////////

        #region IExecute Members
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 3)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.MustHaveIntegerValue, Index.Invalid, Index.Invalid, "-size", null),
                            new Option(null, OptionFlags.MustHaveValue | OptionFlags.Unsupported, Index.Invalid, Index.Invalid, "-command", null),
                            new Option(typeof(EventFlags), OptionFlags.MustHaveEnumValue, Index.Invalid, Index.Invalid, "-eventflags",
                                       new Variant(interpreter.EngineEventFlags)),
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, Option.EndOfOptions, null)
                        });

                        int argumentIndex = Index.Invalid;

                        if (arguments.Count > 3)
                        {
                            code = interpreter.GetOptions(options, arguments, 0, 3, Index.Invalid, true, ref argumentIndex, ref result);
                        }
                        else
                        {
                            code = ReturnCode.Ok;
                        }

                        if (code == ReturnCode.Ok)
                        {
                            if (argumentIndex == Index.Invalid)
                            {
                                Variant value = null;
                                int     size  = _Size.Invalid;

                                if (options.IsPresent("-size", ref value))
                                {
                                    size = (int)value.Value;

                                    //
                                    // NOTE: All negative values become "invalid",
                                    //       which means "read until end-of-file".
                                    //
                                    if (size < 0)
                                    {
                                        size = _Size.Invalid;
                                    }
                                }

#if MONO_BUILD
#pragma warning disable 219
#endif
                                string command = null; // NOTE: Flagged by the Mono C# compiler.
#if MONO_BUILD
#pragma warning restore 219
#endif

                                if (options.IsPresent("-command", ref value))
                                {
                                    command = value.ToString(); /* NOT YET IMPLEMENTED */
                                }
                                EventFlags eventFlags = interpreter.EngineEventFlags;

                                if (options.IsPresent("-eventflags", ref value))
                                {
                                    eventFlags = (EventFlags)value.Value;
                                }

                                string  inputChannelId = arguments[1];
                                Channel inputChannel   = interpreter.GetChannel(inputChannelId, ref result);

                                if (inputChannel != null)
                                {
                                    if (inputChannel.CanRead)
                                    {
                                        Encoding inputEncoding = null;

                                        if (interpreter.GetChannelEncoding(inputChannel, ref inputEncoding) == ReturnCode.Ok)
                                        {
                                            string  outputChannelId = arguments[2];
                                            Channel outputChannel   = interpreter.GetChannel(outputChannelId, ref result);

                                            if (outputChannel != null)
                                            {
                                                if (outputChannel.CanWrite)
                                                {
                                                    Encoding outputEncoding = null;

                                                    if (interpreter.GetChannelEncoding(outputChannel, ref outputEncoding) == ReturnCode.Ok)
                                                    {
                                                        try
                                                        {
                                                            BinaryWriter binaryWriter = null; /* NOTE: Output channel. */
                                                            int          outputBytes  = 0;

                                                            //
                                                            // NOTE: Reset the end-of-file indicator here because we may
                                                            //       need to use it to terminate the loop.
                                                            //
                                                            inputChannel.HitEndOfStream = false;

                                                            do
                                                            {
                                                                if (inputChannel.CanSeek && inputChannel.EndOfStream)
                                                                {
                                                                    break;
                                                                }
                                                                else if (inputChannel.HitEndOfStream)
                                                                {
                                                                    break;
                                                                }

                                                                ByteList inputBuffer = null;

                                                                int readSize = size;

                                                                if ((readSize != _Size.Invalid) && (readSize > MaximumReadSize))
                                                                {
                                                                    readSize = MaximumReadSize;
                                                                }

                                                                if (readSize == _Size.Invalid)
                                                                {
                                                                    code = inputChannel.Read(null, false, ref inputBuffer, ref result);
                                                                }
                                                                else
                                                                {
                                                                    code = inputChannel.Read(readSize, null, false, ref inputBuffer, ref result);
                                                                }

                                                                if (code == ReturnCode.Ok)
                                                                {
                                                                    //
                                                                    // NOTE: Grab the input byte array from the input
                                                                    //       buffer byte list.
                                                                    //
                                                                    byte[] inputArray = inputBuffer.ToArray();

                                                                    //
                                                                    // NOTE: Update the total input byte count with the
                                                                    //       number of bytes we just read.
                                                                    //
                                                                    if (size != _Size.Invalid)
                                                                    {
                                                                        size -= inputArray.Length;
                                                                    }

                                                                    if (outputChannel.IsVirtualOutput)
                                                                    {
                                                                        //
                                                                        // NOTE: Virtual output means that we must get
                                                                        //       the text for the input bytes.
                                                                        //
                                                                        string stringValue = null;

                                                                        code = StringOps.GetString(
                                                                            inputEncoding, inputArray, EncodingType.Binary,
                                                                            ref stringValue, ref result);

                                                                        if (code == ReturnCode.Ok)
                                                                        {
                                                                            //
                                                                            // NOTE: The encoding is ignored, because this is
                                                                            //       directly from the input string, which is
                                                                            //       already Unicode.
                                                                            //
                                                                            outputChannel.AppendVirtualOutput(stringValue);

                                                                            //
                                                                            // NOTE: Update the total output byte count with
                                                                            //       the number of bytes we just wrote.
                                                                            //
                                                                            code = StringOps.GetByteCount(
                                                                                inputEncoding, stringValue, EncodingType.Binary,
                                                                                ref outputBytes, ref result);
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        if (binaryWriter == null)
                                                                        {
                                                                            binaryWriter = interpreter.GetChannelBinaryWriter(
                                                                                outputChannel);
                                                                        }

                                                                        if (binaryWriter != null)
                                                                        {
                                                                            //
                                                                            // NOTE: Convert the input bytes into output
                                                                            //       bytes based on both the input and
                                                                            //       output encodings, if any.  If both
                                                                            //       encodings are null, the input bytes
                                                                            //       are used verbatim.
                                                                            //
                                                                            byte[] outputArray = null;

                                                                            code = StringOps.ConvertBytes(
                                                                                inputEncoding, outputEncoding, EncodingType.Binary,
                                                                                EncodingType.Binary, inputArray, ref outputArray,
                                                                                ref result);

                                                                            if (code == ReturnCode.Ok)
                                                                            {
                                                                                //
                                                                                // NOTE: Ready the output channel for "append"
                                                                                //       mode, if necessary.
                                                                                //
                                                                                outputChannel.CheckAppend(); /* throw */

                                                                                //
                                                                                // NOTE: Attempt to write the output bytes to
                                                                                //       the output channel.
                                                                                //
                                                                                binaryWriter.Write(outputArray); /* throw */

#if MONO || MONO_HACKS
                                                                                //
                                                                                // HACK: *MONO* As of Mono 2.8.0, it seems that
                                                                                //       Mono "loses" output unless a flush is
                                                                                //       performed right after a write.  So far,
                                                                                //       this has only been observed for the
                                                                                //       console channels; however, always using
                                                                                //       flush here on Mono shouldn't cause too
                                                                                //       many problems, except a slight loss in
                                                                                //       performance.
                                                                                //       https://bugzilla.novell.com/show_bug.cgi?id=645193
                                                                                //
                                                                                if (CommonOps.Runtime.IsMono())
                                                                                {
                                                                                    binaryWriter.Flush(); /* throw */
                                                                                }
                                                                                else
#endif
                                                                                {
                                                                                    //
                                                                                    // NOTE: Check if we should automatically
                                                                                    //       flush the channel after each write
                                                                                    //       done by this command.
                                                                                    //
                                                                                    /* IGNORED */
                                                                                    outputChannel.CheckAutoFlush();
                                                                                }

                                                                                //
                                                                                // NOTE: Update the total output byte count with
                                                                                //       the number of bytes we just wrote.
                                                                                //
                                                                                outputBytes += outputArray.Length;
                                                                            }
                                                                        }
                                                                        else
                                                                        {
                                                                            result = String.Format(
                                                                                "failed to get binary writer for channel \"{0}\"",
                                                                                outputChannelId);

                                                                            code = ReturnCode.Error;
                                                                        }
                                                                    }
                                                                }

                                                                //
                                                                // NOTE: If any of the above actions failed, bail out of the
                                                                //       copy loop now.
                                                                //
                                                                if (code != ReturnCode.Ok)
                                                                {
                                                                    break;
                                                                }

                                                                //
                                                                // NOTE: Are we done reading input bytes?  If this value is
                                                                //       less than zero, it means we read until end-of-file.
                                                                //       If we have read the specified number of bytes, bail
                                                                //       out.
                                                                //
                                                                if (size == 0)
                                                                {
                                                                    break;
                                                                }

                                                                //
                                                                // NOTE: Check for any pending events in the interpreter and
                                                                //       service them now.
                                                                //
                                                                code = Engine.CheckEvents(interpreter, eventFlags, ref result);

                                                                if (code != ReturnCode.Ok)
                                                                {
                                                                    break;
                                                                }
                                                            }while (true);

                                                            //
                                                            // NOTE: Return the number of bytes written to the output
                                                            //       channel.
                                                            //
                                                            if (code == ReturnCode.Ok)
                                                            {
                                                                result = outputBytes;
                                                            }
                                                        }
                                                        catch (Exception e)
                                                        {
                                                            Engine.SetExceptionErrorCode(interpreter, e);

                                                            result = e;
                                                            code   = ReturnCode.Error;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        result = String.Format(
                                                            "failed to get encoding for output channel \"{0}\"",
                                                            outputChannelId);

                                                        code = ReturnCode.Error;
                                                    }
                                                }
                                                else
                                                {
                                                    result = String.Format(
                                                        "channel \"{0}\" wasn't opened for writing",
                                                        outputChannelId);

                                                    code = ReturnCode.Error;
                                                }
                                            }
                                            else
                                            {
                                                code = ReturnCode.Error;
                                            }
                                        }
                                        else
                                        {
                                            result = String.Format(
                                                "failed to get encoding for input channel \"{0}\"",
                                                inputChannelId);

                                            code = ReturnCode.Error;
                                        }
                                    }
                                    else
                                    {
                                        result = String.Format(
                                            "channel \"{0}\" wasn't opened for reading",
                                            inputChannelId);

                                        code = ReturnCode.Error;
                                    }
                                }
                                else
                                {
                                    code = ReturnCode.Error;
                                }
                            }
                            else
                            {
                                result = "wrong # args: should be \"fcopy input output ?-size size? ?-command callback?\"";
                                code   = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"fcopy input output ?-size size? ?-command callback?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
示例#5
0
文件: Randstr.cs 项目: jdruin/F5Eagle
        ///////////////////////////////////////////////////////////////////////////////////////////////

        #region IExecuteArgument Members
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Argument value,
            ref Result error
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count == (this.Arguments + 1))
                    {
                        int intValue = 0;

                        code = Value.GetInteger2(
                            (IGetValue)arguments[1], ValueFlags.AnyInteger,
                            interpreter.CultureInfo, ref intValue, ref error);

                        if (code == ReturnCode.Ok)
                        {
                            if (intValue > 0)
                            {
                                try
                                {
                                    RandomNumberGenerator rng;

                                    lock (interpreter.SyncRoot)
                                    {
                                        rng = interpreter.RandomNumberGenerator;
                                    }

                                    if (rng != null)
                                    {
                                        byte[] bytes = new byte[intValue];

                                        rng.GetBytes(bytes);

                                        string stringValue = null;

                                        code = StringOps.GetString(
                                            null, bytes, EncodingType.Binary,
                                            ref stringValue, ref error);

                                        if (code == ReturnCode.Ok)
                                        {
                                            value = stringValue;
                                        }
                                    }
                                    else
                                    {
                                        error = "random number generator not available";
                                        code  = ReturnCode.Error;
                                    }
                                }
                                catch (Exception e)
                                {
                                    Engine.SetExceptionErrorCode(interpreter, e);

                                    error = String.Format(
                                        "caught math exception: {0}",
                                        e);

                                    code = ReturnCode.Error;
                                }
                            }
                            else
                            {
                                error = "number of bytes must be greater than zero";
                                code  = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        if (arguments.Count > (this.Arguments + 1))
                        {
                            error = String.Format(
                                "too many arguments for math function \"{0}\"",
                                base.Name);
                        }
                        else
                        {
                            error = String.Format(
                                "too few arguments for math function \"{0}\"",
                                base.Name);
                        }

                        code = ReturnCode.Error;
                    }
                }
                else
                {
                    error = "invalid argument list";
                    code  = ReturnCode.Error;
                }
            }
            else
            {
                error = "invalid interpreter";
                code  = ReturnCode.Error;
            }

            return(code);
        }
示例#6
0
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        OptionDictionary options = new OptionDictionary(
                            new IOption[] {
                            new Option(null, OptionFlags.None, Index.Invalid, Index.Invalid, "-nonewline", null)
                        });

                        int argumentIndex = Index.Invalid;

                        code = interpreter.GetOptions(options, arguments, 0, 1, Index.Invalid, false, ref argumentIndex, ref result);

                        if (code == ReturnCode.Ok)
                        {
                            if ((argumentIndex != Index.Invalid) && ((argumentIndex + 2) >= arguments.Count))
                            {
                                bool newLine = true;

                                if (options.IsPresent("-nonewline"))
                                {
                                    newLine = false;
                                }

                                int count = Count.Invalid;

                                if ((argumentIndex + 1) < arguments.Count)
                                {
                                    code = Value.GetInteger2(
                                        (IGetValue)arguments[argumentIndex + 1], ValueFlags.AnyInteger,
                                        interpreter.CultureInfo, ref count, ref result);
                                }

                                if (code == ReturnCode.Ok)
                                {
                                    string  channelId = arguments[argumentIndex];
                                    Channel channel   = interpreter.GetChannel(channelId, ref result);

                                    if (channel != null)
                                    {
                                        CharList endOfLine = channel.GetInputEndOfLine();
                                        Encoding encoding  = null;

                                        if (interpreter.GetChannelEncoding(channel, ref encoding) == ReturnCode.Ok)
                                        {
                                            //
                                            // NOTE: If they do not specify a count we simply read
                                            //       until end-of-file.
                                            //
                                            try
                                            {
                                                ByteList buffer = null;

                                                code = channel.Read(count, null, false, ref buffer, ref result);

                                                if (code == ReturnCode.Ok)
                                                {
                                                    //
                                                    // BUGFIX: Remove trailing end-of-line character even
                                                    //         when reading the entire stream.
                                                    //
                                                    if (!newLine)
                                                    {
                                                        channel.RemoveTrailingEndOfLine(buffer, endOfLine);
                                                    }

                                                    string stringValue = null;

                                                    code = StringOps.GetString(
                                                        encoding, buffer.ToArray(), EncodingType.Binary,
                                                        ref stringValue, ref result);

                                                    if (code == ReturnCode.Ok)
                                                    {
                                                        result = stringValue;
                                                    }
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                Engine.SetExceptionErrorCode(interpreter, e);

                                                result = e;
                                                code   = ReturnCode.Error;
                                            }
                                        }
                                        else
                                        {
                                            result = String.Format(
                                                "failed to get encoding for channel \"{0}\"",
                                                channelId);

                                            code = ReturnCode.Error;
                                        }
                                    }
                                    else
                                    {
                                        code = ReturnCode.Error;
                                    }
                                }
                            }
                            else
                            {
                                if ((argumentIndex != Index.Invalid) &&
                                    Option.LooksLikeOption(arguments[argumentIndex]))
                                {
                                    result = OptionDictionary.BadOption(options, arguments[argumentIndex]);
                                }
                                else
                                {
                                    result = "wrong # args: should be \"read channelId ?numChars?\" or \"read ?-nonewline? channelId\"";
                                }

                                code = ReturnCode.Error;
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"read channelId ?numChars?\" or \"read ?-nonewline? channelId\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
示例#7
0
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 2)
                    {
                        string subCommand = arguments[1];
                        bool   tried      = false;

                        code = ScriptOps.TryExecuteSubCommandFromEnsemble(
                            interpreter, this, clientData, arguments, true,
                            false, ref subCommand, ref tried, ref result);

                        if ((code == ReturnCode.Ok) && !tried)
                        {
                            switch (subCommand)
                            {
                            case "convertfrom":
                            {
                                if ((arguments.Count == 3) || (arguments.Count == 4))
                                {
                                    //
                                    // NOTE: (from Tcl encoding.n): Convert data to Unicode from the
                                    //       specified encoding. The characters in data are treated
                                    //       as binary data where the lower 8-bits of each character
                                    //       is taken as a single byte. The resulting sequence of
                                    //       bytes is treated as a string in the specified encoding.
                                    //       If encoding is not specified, the current system encoding
                                    //       is used.
                                    //
                                    int      argumentIndex = 2;
                                    Encoding encoding      = null;

                                    if (arguments.Count == 4)
                                    {
                                        code = interpreter.GetEncoding(
                                            arguments[argumentIndex++], LookupFlags.Default,
                                            ref encoding, ref result);
                                    }
                                    else
                                    {
                                        code = ReturnCode.Ok;
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        string stringValue = null;

                                        code = StringOps.ConvertString(
                                            null, encoding, EncodingType.Binary, EncodingType.System,
                                            arguments[argumentIndex], ref stringValue, ref result);

                                        if (code == ReturnCode.Ok)
                                        {
                                            result = stringValue;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"encoding convertfrom ?encoding? data\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "convertto":
                            {
                                if ((arguments.Count == 3) || (arguments.Count == 4))
                                {
                                    //
                                    // NOTE: (from Tcl encoding.n): Convert string from Unicode to the
                                    //       specified encoding. The result is a sequence of bytes that
                                    //       represents the converted string. Each byte is stored in the
                                    //       lower 8-bits of a Unicode character. If encoding is not
                                    //       specified, the current system encoding is used.
                                    //
                                    int      argumentIndex = 2;
                                    Encoding encoding      = null;

                                    if (arguments.Count == 4)
                                    {
                                        code = interpreter.GetEncoding(
                                            arguments[argumentIndex++], LookupFlags.Default,
                                            ref encoding, ref result);
                                    }
                                    else
                                    {
                                        code = ReturnCode.Ok;
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        string stringValue = null;

                                        code = StringOps.ConvertString(
                                            encoding, null, EncodingType.System, EncodingType.Binary,
                                            arguments[argumentIndex], ref stringValue, ref result);

                                        if (code == ReturnCode.Ok)
                                        {
                                            result = stringValue;
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"encoding convertto ?encoding? data\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "getstring":
                            {
                                if ((arguments.Count == 3) || (arguments.Count == 4))
                                {
                                    IObject @object = null;

                                    code = interpreter.GetObject(
                                        arguments[2], LookupFlags.Default,
                                        ref @object, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        Encoding encoding = null;

                                        if (arguments.Count == 4)
                                        {
                                            code = interpreter.GetEncoding(
                                                arguments[3], LookupFlags.Default,
                                                ref encoding, ref result);
                                        }
                                        else
                                        {
                                            code = ReturnCode.Ok;
                                        }

                                        if (code == ReturnCode.Ok)
                                        {
                                            if (@object.Value is byte[])
                                            {
                                                string stringValue = null;

                                                code = StringOps.GetString(
                                                    encoding, (byte[])@object.Value, EncodingType.System,
                                                    ref stringValue, ref result);

                                                if (code == ReturnCode.Ok)
                                                {
                                                    result = stringValue;
                                                }
                                            }
                                            else
                                            {
                                                result = String.Format(
                                                    "object \"{0}\" is not a byte array",
                                                    arguments[2]);

                                                code = ReturnCode.Error;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"encoding getstring object ?encoding?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "names":
                            {
                                if ((arguments.Count >= 2) && (arguments.Count <= 4))
                                {
                                    bool system = true;         /* COMPAT: Tcl. */

                                    if (arguments.Count >= 3)
                                    {
                                        code = Value.GetBoolean2(
                                            arguments[2], ValueFlags.AnyBoolean,
                                            interpreter.CultureInfo, ref system,
                                            ref result);
                                    }

                                    if (code == ReturnCode.Ok)
                                    {
                                        EncodingDictionary encodings = null;

                                        if (system)
                                        {
                                            StringOps.GetSystemEncodings(ref encodings);
                                        }

                                        interpreter.GetEncodings(ref encodings);

                                        string pattern = null;

                                        if (arguments.Count == 4)
                                        {
                                            pattern = arguments[3];
                                        }

                                        result = encodings.ToString(pattern, false);
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"encoding names ?system? ?pattern?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            case "system":
                            {
                                //
                                // NOTE: The system encoding in Eagle is always Unicode and cannot
                                //       be changed.
                                //
                                if ((arguments.Count == 2) || (arguments.Count == 3))
                                {
                                    if (arguments.Count == 3)
                                    {
                                        result = "not implemented";
                                        code   = ReturnCode.Error;
                                    }
                                    else
                                    {
                                        result = StringOps.SystemEncodingWebName;
                                    }
                                }
                                else
                                {
                                    result = "wrong # args: should be \"encoding system ?encoding?\"";
                                    code   = ReturnCode.Error;
                                }
                                break;
                            }

                            default:
                            {
                                result = ScriptOps.BadSubCommand(
                                    interpreter, null, null, subCommand, this, null, null);

                                code = ReturnCode.Error;
                                break;
                            }
                            }
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"encoding option ?arg ...?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }