Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scripted interpreter extension #12

Open
xk2600 opened this issue Sep 23, 2021 · 0 comments
Open

scripted interpreter extension #12

xk2600 opened this issue Sep 23, 2021 · 0 comments

Comments

@xk2600
Copy link

xk2600 commented Sep 23, 2021

I've mostly worked this out, but I keep running into areas where I want to be able to extend the functionality in the various modules without having to go to C land, to avoid the problems that come with keeping my own fork of tnm. I will try and put together an actual pull request, but just for anyone interested in how I'm doing this today, here is the bones of a solution.

mib-init.tcl
package require Tnm

rename ::Tnm::mib ::mib
interp hide {} mib

namespace eval ::Tnm::mib {
  set ns [namespace current]
  set builtins [list {*}{
    access children compare defval description displayhint
    enums exists file format index info label length load
    macro member module name oid pack parent range scan size
    split status subtree syntax type unpack variables walk
  }]
  foreach builtin $builtins {
    # export the builtin
    namespace export $builtin
    
    # create alias to invoke hidden ::Tnm::mib command
    interp alias {} ${ns}::$builtin {} interp invokehidden {} mib $builtin
  }
  namespace ensemble create -command ::Tnm::mib
    
  # alternative implementation for slective dispatch between c-side sub-commands and
  # and interp subcommands
  if 0 {
    proc ensembleUnknown {command args} {
      variable builtins
      if {$command in $builtins} {
        tailcall [namespace current]::mib {*}$args
      }
      set badoptionerr {bad option "%s": must be %s, or %s}
      set options [dict get keys [namespace ensemble configure -map]]
      set optionsString [join [lrange $options 0 end-1] ", "]
      set message [format $badoptionerr $optionsString [lindex $options end]]
      return -code error $message
    }
    namespace ensemble create -command ::Tnm::mib -unknown ensembleUnknown
  }
}

# ::Tnm::mib ensemble extensions
namespace eval ::Tnm::mib {
  namespace export tables
  
  proc tables {root} {
    set tables {}
    mib walk OID $root {
      if {[mib type $OID] == {SEQUENCE OF}} {
        lappend tables [mib type $OID] $OID
      }
    }
    return $tables
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant