예제 #1
0
        ///////////////////////////////////////////////////////////////////////

#if ISOLATED_PLUGINS
        public static bool IsIsolated(
            IPluginData pluginData
            )
        {
            return((pluginData != null) &&
                   FlagOps.HasFlags(pluginData.Flags, PluginFlags.Isolated, true));
        }
예제 #2
0
        ///////////////////////////////////////////////////////////////////////

        #region Command History Support Methods
        public static bool MatchData(
            IClientData clientData,
            IHistoryFilter historyFilter
            )
        {
            HistoryClientData historyClientData = clientData as HistoryClientData;

            if (historyClientData != null)
            {
                if (historyFilter != null)
                {
                    if ((historyFilter.StartLevel != Level.Invalid) &&
                        (historyClientData.Levels < historyFilter.StartLevel))
                    {
                        return(false);
                    }

                    if ((historyFilter.StopLevel != Level.Invalid) &&
                        (historyClientData.Levels > historyFilter.StopLevel))
                    {
                        return(false);
                    }

                    if ((historyFilter.HasFlags != HistoryFlags.None) &&
                        !FlagOps.HasFlags(historyClientData.Flags,
                                          historyFilter.HasFlags, historyFilter.HasAll))
                    {
                        return(false);
                    }

                    if ((historyFilter.NotHasFlags != HistoryFlags.None) &&
                        FlagOps.HasFlags(historyClientData.Flags,
                                         historyFilter.NotHasFlags, historyFilter.NotHasAll))
                    {
                        return(false);
                    }
                }

                return(true);
            }

            return(false);
        }
예제 #3
0
        ///////////////////////////////////////////////////////////////////////

        public static bool MatchData(
            int levels,
            HistoryFlags flags,
            IHistoryFilter historyFilter
            )
        {
            if (historyFilter != null)
            {
                if ((historyFilter.StartLevel != Level.Invalid) &&
                    (levels < historyFilter.StartLevel))
                {
                    return(false);
                }

                if ((historyFilter.StopLevel != Level.Invalid) &&
                    (levels > historyFilter.StopLevel))
                {
                    return(false);
                }

                if ((historyFilter.HasFlags != HistoryFlags.None) &&
                    !FlagOps.HasFlags(flags, historyFilter.HasFlags,
                                      historyFilter.HasAll))
                {
                    return(false);
                }

                if ((historyFilter.NotHasFlags != HistoryFlags.None) &&
                    FlagOps.HasFlags(flags, historyFilter.NotHasFlags,
                                     historyFilter.NotHasAll))
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #4
0
파일: TraceOps.cs 프로젝트: jdruin/F5Eagle
        ///////////////////////////////////////////////////////////////////////

        //
        // HACK: This is part of a hack that solves a chicken-and-egg problem
        //       with the diagnostic tracing method used by this library.  We
        //       allow tracing to be disabled via an environment variable
        //       and/or the shell command line.  Unfortunately, by the time we
        //       disable tracing, many messages will have typically already
        //       been written to the trace listeners.  To prevent this noise
        //       (that the user wishes to suppress), we internalize the check
        //       (i.e. we do it from inside the core trace method itself) and
        //       initialize this variable [once] with the result of checking
        //       the environment variable.
        //
        private static bool IsTraceEnabled(
            TracePriority priority,
            params string[] categories
            )
        {
            bool result;

            lock (syncRoot) /* TRANSACTIONAL */
            {
                if (isTraceEnabled == null)
                {
                    //
                    // NOTE: Cannot use the GlobalConfiguration.GetValue
                    //       method at this point because that method may
                    //       call into the DebugTrace method (below), which
                    //       calls this method.
                    //
                    if (CommonOps.Environment.DoesVariableExist(
                            EnvVars.NoTrace))
                    {
                        isTraceEnabled = false;
                    }
                    else if (CommonOps.Environment.DoesVariableExist(
                                 EnvVars.Trace))
                    {
                        isTraceEnabled = true;
                    }
                    else
                    {
                        isTraceEnabled = isTraceEnabledByDefault;
                    }
                }

                //
                // NOTE: Determine if tracing is globally enabled or disabled.
                //
                result = (bool)isTraceEnabled;

                //
                // NOTE: If tracing has been globally disabled, do not bother
                //       checking any categories.
                //
                if (result)
                {
                    //
                    // NOTE: The priority flags specified by the caller must
                    //       all be present in the configured trace priority
                    //       flags.
                    //
                    if (!FlagOps.HasFlags(tracePriorities, priority, true))
                    {
                        result = false;
                    }
                    else
                    {
                        //
                        // NOTE: If the caller specified a null category -OR-
                        //       there are no trace categories specifically
                        //       enabled (i.e. all trace categories are
                        //       allowed), always allow the message through.
                        //
                        if ((categories != null) &&
                            (categories.Length > 0) &&
                            (traceCategories != null) &&
                            (traceCategories.Count > 0))
                        {
                            //
                            // NOTE: At this point, at least one of the
                            //       specified trace categories, if any,
                            //       must exist in the dictionary and its
                            //       associated value must be non-zero;
                            //       otherwise, the trace message is not
                            //       allowed through.
                            //
                            bool found = false;

                            foreach (string category in categories)
                            {
                                if (category == null)
                                {
                                    continue;
                                }

                                int value;

                                if (traceCategories.TryGetValue(
                                        category, out value) &&
                                    (value != 0))
                                {
                                    found = true;
                                    break;
                                }
                            }

                            if (!found)
                            {
                                result = false;
                            }
                        }
                    }
                }
            }

            return(result);
        }
예제 #5
0
        ///////////////////////////////////////////////////////////////////////

        #region Public Methods (Internal Use Only)
        public ReturnCode UnloadNoThrow( /* EXEMPT: object-15.11 */
            ref int loaded,
            ref Result error
            )
        {
            // CheckDisposed(); /* EXEMPT */

            lock (syncRoot) /* TRANSACTIONAL */
            {
                //
                // NOTE: If the module was already loaded previously,
                //       do nothing.
                //
                if (module == IntPtr.Zero)
                {
                    return(ReturnCode.Ok);
                }

                //
                // NOTE: If there are still outstanding references to
                //       the native module, do nothing.
                //
                if (Interlocked.Decrement(ref referenceCount) > 0)
                {
                    return(ReturnCode.Ok);
                }

                //
                // NOTE: If the native module has been locked in place
                //       (because it cannot be cleanly unloaded?), then
                //       leave it alone.
                //
                if (FlagOps.HasFlags(
                        flags, ModuleFlags.NoUnload, true))
                {
                    return(ReturnCode.Ok);
                }

                try
                {
                    int lastError;

                    if (NativeOps.FreeLibrary(
                            module, out lastError)) /* throw */
                    {
                        Interlocked.Decrement(ref loaded);

                        module = IntPtr.Zero;
                        return(ReturnCode.Ok);
                    }
                    else
                    {
                        error = String.Format(
                            "FreeLibrary(0x{1:X}) failed with error {0}: {2}",
                            lastError, module, NativeOps.GetDynamicLoadingError(
                                lastError));
                    }
                }
                catch (Exception e)
                {
                    error = e;
                }
            }

            return(ReturnCode.Error);
        }
예제 #6
0
        ///////////////////////////////////////////////////////////////////////

        public static bool IsAvailable(
            Interpreter interpreter
            )
        {
            lock (syncRoot) /* TRANSACTIONAL */
            {
                try
                {
                    if (isAvailable == null)
                    {
                        //
                        // NOTE: If loading the native utility library has
                        //       been temporarily locked out, return false to
                        //       indicate that it is temporarily unavailable.
                        //       Do nothing else.  That way, it may become
                        //       available later after being unlocked.
                        //
                        if (locked)
                        {
                            return(false);
                        }

                        //
                        // NOTE: If loading the native utility library has
                        //       been prohibited, mark it as "permanently"
                        //       unavailable and return now.
                        //
                        bool verbose = Interpreter.IsVerbose(interpreter);

                        if (((interpreter != null) &&
                             FlagOps.HasFlags(
                                 interpreter.CreateFlagsNoLock,
                                 CreateFlags.NoUtility, true)) ||
                            GlobalConfiguration.DoesValueExist(
                                EnvVars.NoUtility, GlobalConfiguration.GetFlags(
                                    ConfigurationFlags.NativeUtility, verbose)))
                        {
                            disabled = true; /* INFORMATIONAL */
                            return((bool)(isAvailable = false));
                        }

                        //
                        // NOTE: If loading the native utility library fails,
                        //       mark it as "permanently" unavailable.  This
                        //       must be done; otherwise, we will try to load
                        //       it everytime a list needs to be joined or
                        //       split, potentially slowing things down rather
                        //       significantly.
                        //
                        if (!LoadNativeLibrary(interpreter))
                        {
                            return((bool)(isAvailable = false));
                        }

                        IntPtr pVersion = IntPtr.Zero;

                        if ((nativeFreeMemory != null) &&
                            (nativeGetVersion != null))
                        {
                            try
                            {
                                pVersion = nativeGetVersion();

                                if (pVersion != IntPtr.Zero)
                                {
                                    version = Marshal.PtrToStringUni(
                                        pVersion);

                                    if (IsUsable(version))
                                    {
                                        MaybeEnableReflection(false);
                                        isAvailable = true;
                                    }
                                    else
                                    {
                                        version     = null;
                                        isAvailable = false;
                                    }
                                }
                                else
                                {
                                    version     = null;
                                    isAvailable = false;
                                }
                            }
                            catch
                            {
                                //
                                // NOTE: Prevent an exception during the native
                                //       function call from causing this check
                                //       to be repeated [forever] in the future.
                                //
                                version     = null;
                                isAvailable = false;

                                //
                                // NOTE: Next, re-throw the exception (i.e. to
                                //       be caught by the outer catch block).
                                //
                                throw;
                            }
                            finally
                            {
                                if (pVersion != IntPtr.Zero)
                                {
                                    nativeFreeMemory(pVersion);
                                    pVersion = IntPtr.Zero;
                                }
                            }
                        }
                        else
                        {
                            TraceOps.DebugTrace(String.Format(
                                                    "IsAvailable: one or more required " +
                                                    "functions are unavailable: {0} or {1}",
                                                    typeof(Eagle_FreeMemory).Name,
                                                    typeof(Eagle_GetVersion).Name),
                                                typeof(NativeUtility).Name,
                                                TracePriority.NativeError);

                            version     = null;
                            isAvailable = false;
                        }
                    }

                    return((bool)isAvailable);
                }
                catch (Exception e)
                {
                    TraceOps.DebugTrace(
                        e, typeof(NativeUtility).Name,
                        TracePriority.NativeError);
                }
            }

            return(false);
        }
예제 #7
0
        ///////////////////////////////////////////////////////////////////////////////////////////////

        #region Channel Type Members
#if CONSOLE
        public virtual bool IsConsole()
        {
            CheckDisposed();

            return(FlagOps.HasFlags(channelType, ChannelType.Console, true));
        }
예제 #8
0
        ///////////////////////////////////////////////////////////////////////

        public static void UnsetValue(
            string variable,
            ConfigurationFlags flags
            ) /* THREAD-SAFE */
        {
            string prefixedVariable = null;

            //
            // NOTE: If the variable name is null or empty, do nothing.
            //
            if (String.IsNullOrEmpty(variable))
            {
                goto done;
            }

            //
            // NOTE: Try to set the variable name without the package name
            //       prefix?
            //
            bool unprefixed = FlagOps.HasFlags(
                flags, ConfigurationFlags.Unprefixed, true);

            //
            // NOTE: Set the variable name prefixed by package name instead?
            //
            if (FlagOps.HasFlags(flags, ConfigurationFlags.Prefixed, true))
            {
                prefixedVariable = String.Format(
                    EnvVarFormat, EnvVarPrefix, variable);
            }

#if CONFIGURATION
            //
            // NOTE: Does the caller want to remove the loaded AppSettings?
            //
            if (FlagOps.HasFlags(flags, ConfigurationFlags.AppSettings, true))
            {
                //
                // NOTE: Try to unset the requested AppSettings value(s).
                //
                if (unprefixed)
                {
                    ConfigurationOps.UnsetAppSetting(variable);
                }

                if (prefixedVariable != null)
                {
                    ConfigurationOps.UnsetAppSetting(prefixedVariable);
                }
            }
#endif

            //
            // NOTE: Does the caller want to remove the environment variables?
            //
            if (FlagOps.HasFlags(flags, ConfigurationFlags.Environment, true))
            {
                //
                // NOTE: Try to unset the requested environment variable(s).
                //
                if (unprefixed)
                {
                    CommonOps.Environment.UnsetVariable(variable);
                }

                if (prefixedVariable != null)
                {
                    CommonOps.Environment.UnsetVariable(prefixedVariable);
                }
            }

done:

            //
            // NOTE: Output diagnostic message about the configuration value
            //       request.
            //
            if (DefaultVerbose ||
                FlagOps.HasFlags(flags, ConfigurationFlags.Verbose, true))
            {
                TraceOps.DebugTrace(String.Format(
                                        "UnsetValue: variable = {0}, prefixedVariable = {1}, " +
                                        "defaultVerbose = {2}, flags = {3}",
                                        FormatOps.WrapOrNull(variable),
                                        FormatOps.WrapOrNull(prefixedVariable),
                                        DefaultVerbose, FormatOps.WrapOrNull(flags)),
                                    typeof(GlobalConfiguration).Name,
                                    TracePriority.StartupDebug);
            }
        }
예제 #9
0
        ///////////////////////////////////////////////////////////////////////

        public static string GetValue(
            string variable,
            ConfigurationFlags flags
            ) /* THREAD-SAFE */
        {
            string prefixedVariable = null;

            //
            // NOTE: The default return value is null, which means that the
            //       value is not available and/or not set.
            //
            string value = null;

            //
            // NOTE: If the variable name is null or empty, return the default
            //       value (null) instead of potentially throwing an exception
            //       later.
            //
            if (String.IsNullOrEmpty(variable))
            {
                goto done;
            }

            //
            // NOTE: Try to get the variable name without the package name
            //       prefix?
            //
            bool unprefixed = FlagOps.HasFlags(
                flags, ConfigurationFlags.Unprefixed, true);

            //
            // NOTE: Try to get the variable name prefixed by package name
            //       first?
            //
            if (FlagOps.HasFlags(flags, ConfigurationFlags.Prefixed, true))
            {
                prefixedVariable = String.Format(
                    EnvVarFormat, EnvVarPrefix, variable);
            }

            //
            // NOTE: Does the caller want to check the environment variables?
            //
            if (FlagOps.HasFlags(flags, ConfigurationFlags.Environment, true))
            {
                //
                // NOTE: Try the variable name prefixed by our package name
                //       first?
                //
                if ((prefixedVariable != null) && (value == null))
                {
                    value = CommonOps.Environment.GetVariable(prefixedVariable);
                }

                //
                // NOTE: Failing that, just try for the variable name?
                //
                if (unprefixed && (value == null))
                {
                    value = CommonOps.Environment.GetVariable(variable);
                }
            }

#if CONFIGURATION
            //
            // NOTE: Does the caller want to check the loaded AppSettings?
            //
            if (FlagOps.HasFlags(flags, ConfigurationFlags.AppSettings, true))
            {
                //
                // NOTE: Try the variable name prefixed by our package name
                //       first?
                //
                if ((prefixedVariable != null) && (value == null))
                {
                    value = ConfigurationOps.GetAppSetting(prefixedVariable);
                }

                //
                // NOTE: Failing that, just try for the variable name?
                //
                if (unprefixed && (value == null))
                {
                    value = ConfigurationOps.GetAppSetting(variable);
                }
            }
#endif

            //
            // NOTE: If necessary, expand any contained environment variables.
            //
            if (!String.IsNullOrEmpty(value) &&
                FlagOps.HasFlags(flags, ConfigurationFlags.Expand, true))
            {
                value = CommonOps.Environment.ExpandVariables(value);
            }

done:

            //
            // NOTE: Output diagnostic message about the configuration value
            //       request.
            //
            if (DefaultVerbose ||
                FlagOps.HasFlags(flags, ConfigurationFlags.Verbose, true))
            {
                TraceOps.DebugTrace(String.Format(
                                        "GetValue: variable = {0}, prefixedVariable = {1}, " +
                                        "value = {2}, defaultVerbose = {3}, flags = {4}",
                                        FormatOps.WrapOrNull(variable),
                                        FormatOps.WrapOrNull(prefixedVariable),
                                        FormatOps.WrapOrNull(value), DefaultVerbose,
                                        FormatOps.WrapOrNull(flags)),
                                    typeof(GlobalConfiguration).Name,
                                    TracePriority.StartupDebug);
            }

            return(value);
        }
예제 #10
0
        ///////////////////////////////////////////////////////////////////////

        public static ReturnCode ResetStreams(
            ChannelType channelType,
            ref Result error
            )
        {
#if !MONO
            if (!CommonOps.Runtime.IsMono())
            {
                if (ConsoleType == null)
                {
                    error = "invalid system console type";
                    return(ReturnCode.Error);
                }

                //
                // HACK: Because the System.Console object in the .NET Framework
                //       provides no means to reset the underlying input/output
                //       streams, we must do it here by force.
                //
                try
                {
                    //
                    // NOTE: Which standard channels do we want to reset?
                    //
                    bool resetInput = FlagOps.HasFlags(
                        channelType, ChannelType.Input, true);

                    bool resetOutput = FlagOps.HasFlags(
                        channelType, ChannelType.Output, true);

                    bool resetError = FlagOps.HasFlags(
                        channelType, ChannelType.Error, true);

                    if (resetInput)
                    {
                        ConsoleType.InvokeMember("_consoleInputHandle",
                                                 MarshalOps.PrivateStaticSetFieldBindingFlags,
                                                 null, null, new object[] { IntPtr.Zero });
                    }

                    if (resetOutput)
                    {
                        ConsoleType.InvokeMember("_consoleOutputHandle",
                                                 MarshalOps.PrivateStaticSetFieldBindingFlags,
                                                 null, null, new object[] { IntPtr.Zero });
                    }

                    if (resetInput)
                    {
                        ConsoleType.InvokeMember("_in",
                                                 MarshalOps.PrivateStaticSetFieldBindingFlags,
                                                 null, null, new object[] { null });
                    }

                    if (resetOutput)
                    {
                        ConsoleType.InvokeMember("_out",
                                                 MarshalOps.PrivateStaticSetFieldBindingFlags,
                                                 null, null, new object[] { null });
                    }

                    if (resetError)
                    {
                        ConsoleType.InvokeMember("_error",
                                                 MarshalOps.PrivateStaticSetFieldBindingFlags,
                                                 null, null, new object[] { null });
                    }

#if NET_40
                    if (CommonOps.Runtime.IsFramework45OrHigher())
                    {
                        if (resetInput)
                        {
                            ConsoleType.InvokeMember("_stdInRedirectQueried",
                                                     MarshalOps.PrivateStaticSetFieldBindingFlags,
                                                     null, null, new object[] { false });
                        }

                        if (resetOutput)
                        {
                            ConsoleType.InvokeMember("_stdOutRedirectQueried",
                                                     MarshalOps.PrivateStaticSetFieldBindingFlags,
                                                     null, null, new object[] { false });
                        }

                        if (resetError)
                        {
                            ConsoleType.InvokeMember("_stdErrRedirectQueried",
                                                     MarshalOps.PrivateStaticSetFieldBindingFlags,
                                                     null, null, new object[] { false });
                        }
                    }
#endif

                    return(ReturnCode.Ok);
                }
                catch (Exception e)
                {
                    error = e;
                }

                return(ReturnCode.Error);
            }
            else
#endif
            {
                //
                // NOTE: This is not supported (or necessary) on Mono;
                //       therefore, just fake success.
                //
                return(ReturnCode.Ok);
            }
        }
예제 #11
0
        ///////////////////////////////////////////////////////////////////////

        public static HeaderFlags GetHeaderFlags(
            IInteractiveHost interactiveHost,
            HeaderFlags headerFlags,
            bool debug,
            bool show,
            bool empty,
            bool @default
            )
        {
            //
            // NOTE: If we are in debug mode and no header display flags have
            //       been explicitly set for the interpreter, initialize them
            //       to the default value.
            //
            if (@default && FlagOps.HasFlags(
                    headerFlags, HeaderFlags.Invalid, true))
            {
                //
                // NOTE: Remove the "these flags have not been setup before"
                //       indicator flag.
                //
                headerFlags &= ~HeaderFlags.Invalid;

                //
                // NOTE: Add the default header flags for the interactive
                //       host.  If the interactive host is not available,
                //       fallback on the system default header flags.
                //
                HeaderFlags defaultHeaderFlags = HeaderFlags.Default;

                if (interactiveHost != null)
                {
                    headerFlags |= HostOps.GetHeaderFlags(
                        interactiveHost, defaultHeaderFlags);
                }
                else
                {
                    headerFlags |= defaultHeaderFlags;
                }
            }

            //
            // NOTE: Only modify (set or unset) the active debugger flag if we
            //       have been told to do so; otherwise, the active debugger
            //       flag may have been manually changed and should be left
            //       alone.
            //
            if (show)
            {
                //
                // NOTE: Is there an active debugger?
                //
                if (debug)
                {
                    //
                    // NOTE: Set the active debugger flag.
                    //
                    headerFlags |= HeaderFlags.Debug;
                }
                else
                {
                    //
                    // NOTE: Unset the active debugger flag.
                    //
                    headerFlags &= ~HeaderFlags.Debug;
                }
            }

            //
            // NOTE: Show empty content?
            //
            if (empty)
            {
                headerFlags |= HeaderFlags.EmptyContent;
            }

            return(headerFlags);
        }