###############################################################################
#
# safe.eagle --
#
# Extensible Adaptable Generalized Logic Engine (Eagle)
# Safe Interpreter Initialization File
#
# Copyright (c) 2007-2012 by Joe Mistachkin.  All rights reserved.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: $
#
###############################################################################

#
# NOTE: Use our own namespace here because even though we do not directly
#       support namespaces ourselves, we do not want to pollute the global
#       namespace if this script actually ends up being evaluated in Tcl.
#
namespace eval ::Eagle {
  #
  # NOTE: This is the procedure that detects whether or not we are
  #       running in Eagle (otherwise, we are running in vanilla Tcl).
  #       This procedure must function correctly in both Tcl and Eagle
  #       and must return non-zero only when running in Eagle.
  #
  proc isEagle {} {
    #
    # NOTE: Nothing too fancy or expensive should be done in here.  In
    #       theory, use of this routine should be rare; however, in
    #       practice, this routine is actually used quite a bit (e.g.
    #       by the test suite).
    #
    return [expr {[info exists ::tcl_platform(engine)] && \
        [string compare -nocase eagle $::tcl_platform(engine)] == 0}]
  }

  if {[isEagle]} then {
    ###########################################################################
    ############################ BEGIN Eagle ONLY #############################
    ###########################################################################

    proc unknown { name args } {
      #
      # NOTE: This is a stub unknown procedure that simply produces an
      #       appropriate error message.
      #
      # TODO: Add support for auto-loading packages here in the future?
      #
      return -code error "invalid command name \"$name\""
    }

    #
    # TODO: Revise or remove this procedure when full [namespace] support has
    #       been added.
    #
    proc ::tcl::tm::UnknownHandler { original name args } {
      #
      # NOTE: Do nothing except call the original handler.
      #
      uplevel 1 $original [::linsert $args 0 $name]
    }

    proc tclPkgUnknown { name args } {
      #
      # NOTE: Do nothing since this is [most likely] a safe
      #       interpreter.
      #
      return
    }

    ###########################################################################
    ############################# END Eagle ONLY ##############################
    ###########################################################################
  }

  #
  # NOTE: Provide the Eagle "safe" package to the interpreter.
  #
  package provide Eagle.Safe \
    [expr {[isEagle] ? [info engine PatchLevel] : "1.0"}]
}
