# Functionality covered: this file contains a collection of tests for the # procedures in tclNamesp.c and tclEnsemble.c that implement Tcl's basic # support for namespaces. Other namespace-related tests appear in # variable.test. # # Sourcing this file into Tcl runs the tests and generates output for errors. # No output means no errors were found. # # Copyright (c) 1997 Sun Microsystems, Inc. # Copyright (c) 1998-2000 by Scriptics Corporation. # # See the file "license.terms" for information on usage and redistribution of # this file, and for a DISCLAIMER OF ALL WARRANTIES. package require tcltest 2 namespace import -force ::tcltest::* testConstraint memory [llength [info commands memory]] # # REMARK: the tests for 'namespace upvar' are not done here. They are to be # found in the file 'upvar.test'. # # Clear out any namespaces called test_ns_* catch {namespace delete {*}[namespace children :: test_ns_*]} proc fq {ns} { if {[string match ::* $ns]} {return $ns} set current [uplevel 1 {namespace current}] return [string trimright $current :]::[string trimleft $ns :] } test namespace-1.1 {TclInitNamespaces, GetNamespaceFromObj, NamespaceChildrenCmd} { namespace children :: test_ns_* } {} catch {unset l} test namespace-2.1 {Tcl_GetCurrentNamespace} { list [namespace current] [namespace eval {} {namespace current}] \ [namespace eval {} {namespace current}] } {:: :: ::} test namespace-2.2 {Tcl_GetCurrentNamespace} { set l {} lappend l [namespace current] namespace eval test_ns_1 { lappend l [namespace current] namespace eval foo { lappend l [namespace current] } } lappend l [namespace current] } {:: ::test_ns_1 ::test_ns_1::foo ::} test namespace-3.1 {Tcl_GetGlobalNamespace} { namespace eval test_ns_1 {namespace eval foo {namespace eval bar {} } } # namespace children uses Tcl_GetGlobalNamespace namespace eval test_ns_1 {namespace children foo b*} } {::test_ns_1::foo::bar} test namespace-4.1 {Tcl_PushCallFrame with isProcCallFrame=1} { namespace eval test_ns_1 { variable v 123 proc p {} { variable v return $v } } test_ns_1::p ;# does Tcl_PushCallFrame to push p's namespace } {123} test namespace-4.2 {Tcl_PushCallFrame with isProcCallFrame=0} { namespace eval test_ns_1::baz {} ;# does Tcl_PushCallFrame to create baz proc test_ns_1::baz::p {} { variable v set v 789 set v} test_ns_1::baz::p } {789} test namespace-5.1 {Tcl_PopCallFrame, no vars} { namespace eval test_ns_1::blodge {} ;# pushes then pops frame } {} test namespace-5.2 {Tcl_PopCallFrame, local vars must be deleted} { proc test_ns_1::r {} { set a 123 } test_ns_1::r ;# pushes then pop's r's frame } {123} test namespace-6.1 {Tcl_CreateNamespace} { catch {namespace delete {*}[namespace children :: test_ns_*]} list [lsort [namespace children :: test_ns_*]] \ [namespace eval test_ns_1 {namespace current}] \ [namespace eval test_ns_2 {namespace current}] \ [namespace eval ::test_ns_3 {namespace current}] \ [namespace eval ::test_ns_4 \ {namespace eval foo {namespace current}}] \ [namespace eval ::test_ns_5 \ {namespace eval ::test_ns_6 {namespace current}}] \ [lsort [namespace children :: test_ns_*]] } {{} ::test_ns_1 ::test_ns_2 ::test_ns_3 ::test_ns_4::foo ::test_ns_6 {::test_ns_1 ::test_ns_2 ::test_ns_3 ::test_ns_4 ::test_ns_5 ::test_ns_6}} test namespace-6.2 {Tcl_CreateNamespace, odd number of :'s in name is okay} { list [namespace eval :::test_ns_1::::foo {namespace current}] \ [namespace eval test_ns_2:::::foo {namespace current}] } {::test_ns_1::foo ::test_ns_2::foo} test namespace-6.3 {Tcl_CreateNamespace, trailing ::s in ns name are ignored} { list [catch {namespace eval test_ns_7::: {namespace current}} msg] $msg } {0 ::test_ns_7} test namespace-6.4 {Tcl_CreateNamespace, trailing ::s in ns name are ignored} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_1:: { namespace eval test_ns_2:: {} namespace eval test_ns_3:: {} } lsort [namespace children ::test_ns_1] } [lsort {::test_ns_1::test_ns_2 ::test_ns_1::test_ns_3}] test namespace-6.5 {Tcl_CreateNamespace, relative ns names now only looked up in current ns} { set trigger { namespace eval test_ns_2 {namespace current} } set l {} lappend l [namespace eval test_ns_1 $trigger] namespace eval test_ns_1::test_ns_2 {} lappend l [namespace eval test_ns_1 $trigger] } {::test_ns_1::test_ns_2 ::test_ns_1::test_ns_2} test namespace-7.1 {Tcl_DeleteNamespace, active call frames in ns} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_1 { proc p {} { namespace delete [namespace current] return [namespace current] } } list [test_ns_1::p] [catch {test_ns_1::p} msg] $msg } {::test_ns_1 1 {invalid command name "test_ns_1::p"}} test namespace-7.2 {Tcl_DeleteNamespace, no active call frames in ns} { namespace eval test_ns_2 { proc p {} { return [namespace current] } } list [test_ns_2::p] [namespace delete test_ns_2] } {::test_ns_2 {}} test namespace-7.3 {recursive Tcl_DeleteNamespace, active call frames in ns} { # [Bug 1355942] namespace eval test_ns_2 { set x 1 trace add variable x unset "namespace delete [namespace current];#" namespace delete [namespace current] } } {} test namespace-7.4 {recursive Tcl_DeleteNamespace, active call frames in ns} { # [Bug 1355942] namespace eval test_ns_2 { proc x {} {} trace add command x delete "namespace delete [namespace current];#" namespace delete [namespace current] } } {} test namespace-7.5 {recursive Tcl_DeleteNamespace, no active call frames in ns} { # [Bug 1355942] namespace eval test_ns_2 { set x 1 trace add variable x unset "namespace delete [namespace current];#" } namespace delete test_ns_2 } {} test namespace-7.6 {recursive Tcl_DeleteNamespace, no active call frames in ns} { # [Bug 1355942] namespace eval test_ns_2 { proc x {} {} trace add command x delete "namespace delete [namespace current];#" } namespace delete test_ns_2 } {} test namespace-7.7 {Bug 1655305} -setup { interp create slave # Can't invoke through the ensemble, since deleting the global namespace # (indirectly, via deleting ::tcl) deletes the ensemble. slave eval {rename ::tcl::info::commands ::infocommands} slave hide infocommands slave eval { proc foo {} { namespace delete :: } } } -body { slave eval foo slave invokehidden infocommands } -cleanup { interp delete slave } -result {} test namespace-8.1 {TclTeardownNamespace, delete global namespace} { catch {interp delete test_interp} interp create test_interp interp eval test_interp { namespace eval test_ns_1 { namespace export p proc p {} { return [namespace current] } } namespace eval test_ns_2 { namespace import ::test_ns_1::p variable v 27 proc q {} { variable v return "[p] $v" } } set x [test_ns_2::q] catch {set xxxx} } list [interp eval test_interp {test_ns_2::q}] \ [interp eval test_interp {namespace delete ::}] \ [catch {interp eval test_interp {set a 123}} msg] $msg \ [interp delete test_interp] } {{::test_ns_1 27} {} 1 {invalid command name "set"} {}} test namespace-8.2 {TclTeardownNamespace, remove deleted ns from parent} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_1::test_ns_2::test_ns_3a {proc p {} {}} namespace eval test_ns_1::test_ns_2::test_ns_3b {proc q {} {}} list [namespace children test_ns_1] \ [namespace delete test_ns_1::test_ns_2] \ [namespace children test_ns_1] } {::test_ns_1::test_ns_2 {} {}} test namespace-8.3 {TclTeardownNamespace, delete child namespaces} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_1::test_ns_2::test_ns_3a {proc p {} {}} namespace eval test_ns_1::test_ns_2::test_ns_3b {proc q {} {}} list [namespace children test_ns_1] \ [namespace delete test_ns_1::test_ns_2] \ [namespace children test_ns_1] \ [catch {namespace children test_ns_1::test_ns_2} msg] $msg \ [info commands test_ns_1::test_ns_2::test_ns_3a::*] } {::test_ns_1::test_ns_2 {} {} 1 {namespace "test_ns_1::test_ns_2" not found in "::"} {}} test namespace-8.4 {TclTeardownNamespace, cmds imported from deleted ns go away} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_export { namespace export cmd1 cmd2 proc cmd1 {args} {return "cmd1: $args"} proc cmd2 {args} {return "cmd2: $args"} } namespace eval test_ns_import { namespace import ::test_ns_export::* proc p {} {return foo} } list [lsort [info commands test_ns_import::*]] \ [namespace delete test_ns_export] \ [info commands test_ns_import::*] } [list [lsort {::test_ns_import::p ::test_ns_import::cmd1 ::test_ns_import::cmd2}] {} ::test_ns_import::p] test namespace-8.5 {TclTeardownNamespace: preserve errorInfo; errorCode values} { interp create slave slave eval {trace add execution error leave {namespace delete :: ;#}} catch {slave eval error foo bar baz} interp delete slave set ::errorInfo } {bar invoked from within "slave eval error foo bar baz"} test namespace-8.6 {TclTeardownNamespace: preserve errorInfo; errorCode values} { interp create slave slave eval {trace add variable errorCode write {namespace delete :: ;#}} catch {slave eval error foo bar baz} interp delete slave set ::errorInfo } {bar invoked from within "slave eval error foo bar baz"} test namespace-8.7 {TclTeardownNamespace: preserve errorInfo; errorCode values} { interp create slave slave eval {trace add execution error leave {namespace delete :: ;#}} catch {slave eval error foo bar baz} interp delete slave set ::errorCode } baz test namespace-9.1 {Tcl_Import, empty import pattern} { catch {namespace delete {*}[namespace children :: test_ns_*]} list [catch {namespace eval test_ns_import {namespace import {}}} msg] $msg } {1 {empty import pattern}} test namespace-9.2 {Tcl_Import, unknown namespace in import pattern} { list [catch {namespace eval test_ns_import {namespace import fred::x}} msg] $msg } {1 {unknown namespace in import pattern "fred::x"}} test namespace-9.3 {Tcl_Import, import ns == export ns} { list [catch {namespace eval test_ns_import {namespace import ::test_ns_import::puts}} msg] $msg } {1 {import pattern "::test_ns_import::puts" tries to import from namespace "test_ns_import" into itself}} test namespace-9.4 {Tcl_Import, simple import} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_export { namespace export cmd1 proc cmd1 {args} {return "cmd1: $args"} proc cmd2 {args} {return "cmd2: $args"} } namespace eval test_ns_import { namespace import ::test_ns_export::* proc p {} {return [cmd1 123]} } test_ns_import::p } {cmd1: 123} test namespace-9.5 {Tcl_Import, can't redefine cmd unless allowOverwrite!=0} { list [catch {namespace eval test_ns_import {namespace import ::test_ns_export::*}} msg] $msg } {0 {}} test namespace-9.6 {Tcl_Import, cmd redefinition ok if allowOverwrite!=0} { namespace eval test_ns_import { namespace import -force ::test_ns_export::* cmd1 555 } } {cmd1: 555} test namespace-9.7 {Tcl_Import, links are preserved if cmd is redefined} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_export { namespace export cmd1 proc cmd1 {args} {return "cmd1: $args"} } namespace eval test_ns_import { namespace import -force ::test_ns_export::* } list [test_ns_import::cmd1 a b c] \ [test_ns_export::cmd1 d e f] \ [proc test_ns_export::cmd1 {args} {return "new1: $args"}] \ [namespace origin test_ns_import::cmd1] \ [namespace origin test_ns_export::cmd1] \ [test_ns_import::cmd1 g h i] \ [test_ns_export::cmd1 j k l] } {{cmd1: a b c} {cmd1: d e f} {} ::test_ns_export::cmd1 ::test_ns_export::cmd1 {new1: g h i} {new1: j k l}} test namespace-9.8 {Tcl_Import: Bug 1017299} -setup { namespace eval one { namespace export cmd proc cmd {} {} } namespace eval two { namespace export cmd proc other args {} } namespace eval two \ [list namespace import [namespace current]::one::cmd] namespace eval three \ [list namespace import [namespace current]::two::cmd] namespace eval three { rename cmd other namespace export other } } -body { namespace eval two [list namespace import -force \ [namespace current]::three::other] namespace origin two::other } -cleanup { namespace delete one two three } -match glob -result *::one::cmd test namespace-9.9 {Tcl_Import: Bug 1017299} -setup { namespace eval one { namespace export cmd proc cmd {} {} } namespace eval two namespace export cmd namespace eval two \ [list namespace import [namespace current]::one::cmd] namespace eval three namespace export cmd namespace eval three \ [list namespace import [namespace current]::two::cmd] } -body { namespace eval two [list namespace import -force \ [namespace current]::three::cmd] namespace origin two::cmd } -cleanup { namespace delete one two three } -returnCodes error -match glob -result {import pattern * would create a loop*} test namespace-10.1 {Tcl_ForgetImport, check for valid namespaces} { catch {namespace delete {*}[namespace children :: test_ns_*]} list [catch {namespace forget xyzzy::*} msg] $msg } {1 {unknown namespace in namespace forget pattern "xyzzy::*"}} test namespace-10.2 {Tcl_ForgetImport, ignores patterns that don't match} { namespace eval test_ns_export { namespace export cmd1 proc cmd1 {args} {return "cmd1: $args"} proc cmd2 {args} {return "cmd2: $args"} } namespace eval test_ns_import { namespace forget ::test_ns_export::wombat } } {} test namespace-10.3 {Tcl_ForgetImport, deletes matching imported cmds} { namespace eval test_ns_import { namespace import ::test_ns_export::* proc p {} {return [cmd1 123]} set l {} lappend l [lsort [info commands ::test_ns_import::*]] namespace forget ::test_ns_export::cmd1 lappend l [info commands ::test_ns_import::*] lappend l [catch {cmd1 777} msg] $msg } } [list [lsort {::test_ns_import::p ::test_ns_import::cmd1}] ::test_ns_import::p 1 {invalid command name "cmd1"}] test namespace-10.4 {Tcl_ForgetImport: Bug 560297} -setup { namespace eval origin { namespace export cmd proc cmd {} {} } namespace eval unrelated { proc cmd {} {} } namespace eval my \ [list namespace import [namespace current]::origin::cmd] } -body { namespace eval my \ [list namespace forget [namespace current]::unrelated::cmd] my::cmd } -cleanup { namespace delete origin unrelated my } test namespace-10.5 {Tcl_ForgetImport: Bug 560297} -setup { namespace eval origin { namespace export cmd proc cmd {} {} } namespace eval my \ [list namespace import [namespace current]::origin::cmd] namespace eval my rename cmd newname } -body { namespace eval my \ [list namespace forget [namespace current]::origin::cmd] my::newname } -cleanup { namespace delete origin my } -returnCodes error -match glob -result * test namespace-10.6 {Tcl_ForgetImport: Bug 560297} -setup { namespace eval origin { namespace export cmd proc cmd {} {} } namespace eval my \ [list namespace import [namespace current]::origin::cmd] namespace eval your {} namespace eval my \ [list rename cmd [namespace current]::your::newname] } -body { namespace eval your namespace forget newname your::newname } -cleanup { namespace delete origin my your } -returnCodes error -match glob -result * test namespace-10.7 {Tcl_ForgetImport: Bug 560297} -setup { namespace eval origin { namespace export cmd proc cmd {} {} } namespace eval link namespace export cmd namespace eval link \ [list namespace import [namespace current]::origin::cmd] namespace eval link2 namespace export cmd namespace eval link2 \ [list namespace import [namespace current]::link::cmd] namespace eval my \ [list namespace import [namespace current]::link2::cmd] } -body { namespace eval my \ [list namespace forget [namespace current]::origin::cmd] my::cmd } -cleanup { namespace delete origin link link2 my } -returnCodes error -match glob -result * test namespace-10.8 {Tcl_ForgetImport: Bug 560297} -setup { namespace eval origin { namespace export cmd proc cmd {} {} } namespace eval link namespace export cmd namespace eval link \ [list namespace import [namespace current]::origin::cmd] namespace eval link2 namespace export cmd namespace eval link2 \ [list namespace import [namespace current]::link::cmd] namespace eval my \ [list namespace import [namespace current]::link2::cmd] } -body { namespace eval my \ [list namespace forget [namespace current]::link::cmd] my::cmd } -cleanup { namespace delete origin link link2 my } test namespace-10.9 {Tcl_ForgetImport: Bug 560297} -setup { namespace eval origin { namespace export cmd proc cmd {} {} } namespace eval link namespace export cmd namespace eval link \ [list namespace import [namespace current]::origin::cmd] namespace eval link2 namespace export cmd namespace eval link2 \ [list namespace import [namespace current]::link::cmd] namespace eval my \ [list namespace import [namespace current]::link2::cmd] } -body { namespace eval my \ [list namespace forget [namespace current]::link2::cmd] my::cmd } -cleanup { namespace delete origin link link2 my } -returnCodes error -match glob -result * test namespace-11.1 {TclGetOriginalCommand, check if not imported cmd} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_export { namespace export cmd1 proc cmd1 {args} {return "cmd1: $args"} } list [namespace origin set] [namespace origin test_ns_export::cmd1] } {::set ::test_ns_export::cmd1} test namespace-11.2 {TclGetOriginalCommand, directly imported cmd} { namespace eval test_ns_import1 { namespace import ::test_ns_export::* namespace export * proc p {} {namespace origin cmd1} } list [test_ns_import1::p] [namespace origin test_ns_import1::cmd1] } {::test_ns_export::cmd1 ::test_ns_export::cmd1} test namespace-11.3 {TclGetOriginalCommand, indirectly imported cmd} { namespace eval test_ns_import2 { namespace import ::test_ns_import1::* proc q {} {return [cmd1 123]} } list [test_ns_import2::q] [namespace origin test_ns_import2::cmd1] } {{cmd1: 123} ::test_ns_export::cmd1} test namespace-12.1 {InvokeImportedCmd} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_export { namespace export cmd1 proc cmd1 {args} {namespace current} } namespace eval test_ns_import { namespace import ::test_ns_export::* } list [test_ns_import::cmd1] } {::test_ns_export} test namespace-13.1 {DeleteImportedCmd, deletes imported cmds} { namespace eval test_ns_import { set l {} lappend l [info commands ::test_ns_import::*] namespace forget ::test_ns_export::cmd1 lappend l [info commands ::test_ns_import::*] } } {::test_ns_import::cmd1 {}} test namespace-14.1 {TclGetNamespaceForQualName, absolute names} { catch {namespace delete {*}[namespace children :: test_ns_*]} variable v 10 namespace eval test_ns_1::test_ns_2 { variable v 20 } namespace eval test_ns_2 { variable v 30 } namespace eval test_ns_1 { list $::v $::test_ns_2::v $::test_ns_1::test_ns_2::v \ [lsort [namespace children :: test_ns_*]] } } [list 10 30 20 [lsort {::test_ns_1 ::test_ns_2}]] test namespace-14.2 {TclGetNamespaceForQualName, invalid absolute names} { namespace eval test_ns_1 { list [catch {set ::test_ns_777::v} msg] $msg \ [catch {namespace children test_ns_777} msg] $msg } } {1 {can't read "::test_ns_777::v": no such variable} 1 {namespace "test_ns_777" not found in "::test_ns_1"}} test namespace-14.3 {TclGetNamespaceForQualName, relative names} { namespace eval test_ns_1 { list $v $test_ns_2::v } } {10 20} test namespace-14.4 {TclGetNamespaceForQualName, relative ns names looked up only in current ns} { namespace eval test_ns_1::test_ns_2 { namespace eval foo {} } namespace eval test_ns_1 { list [namespace children test_ns_2] \ [catch {namespace children test_ns_1} msg] $msg } } {::test_ns_1::test_ns_2::foo 1 {namespace "test_ns_1" not found in "::test_ns_1"}} test namespace-14.5 {TclGetNamespaceForQualName, relative ns names looked up only in current ns} { namespace eval ::test_ns_2 { namespace eval bar {} } namespace eval test_ns_1 { list [catch {namespace delete test_ns_2::bar} msg] $msg } } {1 {unknown namespace "test_ns_2::bar" in namespace delete command}} test namespace-14.6 {TclGetNamespaceForQualName, relative ns names looked up only in current ns} { namespace eval test_ns_1::test_ns_2 { namespace eval foo {} } namespace eval test_ns_1 { list [namespace children test_ns_2] \ [catch {namespace children test_ns_1} msg] $msg } } {::test_ns_1::test_ns_2::foo 1 {namespace "test_ns_1" not found in "::test_ns_1"}} test namespace-14.7 {TclGetNamespaceForQualName, ignore extra :s if ns} { namespace children test_ns_1::: } {::test_ns_1::test_ns_2} test namespace-14.8 {TclGetNamespaceForQualName, ignore extra :s if ns} { namespace children :::test_ns_1:::::test_ns_2::: } {::test_ns_1::test_ns_2::foo} test namespace-14.9 {TclGetNamespaceForQualName, extra ::s are significant for vars} { set l {} lappend l [catch {set test_ns_1::test_ns_2::} msg] $msg namespace eval test_ns_1::test_ns_2 {variable {} 2525} lappend l [set test_ns_1::test_ns_2::] } {1 {can't read "test_ns_1::test_ns_2::": no such variable} 2525} test namespace-14.10 {TclGetNamespaceForQualName, extra ::s are significant for vars} { catch {unset test_ns_1::test_ns_2::} set l {} lappend l [catch {set test_ns_1::test_ns_2::} msg] $msg set test_ns_1::test_ns_2:: 314159 lappend l [set test_ns_1::test_ns_2::] } {1 {can't read "test_ns_1::test_ns_2::": no such variable} 314159} test namespace-14.11 {TclGetNamespaceForQualName, extra ::s are significant for commands} { catch {rename test_ns_1::test_ns_2:: {}} set l {} lappend l [catch {test_ns_1::test_ns_2:: hello} msg] $msg proc test_ns_1::test_ns_2:: {args} {return "\{\}: $args"} lappend l [test_ns_1::test_ns_2:: hello] } {1 {invalid command name "test_ns_1::test_ns_2::"} {{}: hello}} test namespace-14.12 {TclGetNamespaceForQualName, extra ::s are significant for vars} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_1 { variable {} set test_ns_1::(x) y } set test_ns_1::(x) } y test namespace-14.13 {TclGetNamespaceForQualName, namespace other than global ns can't have empty name} { catch {namespace delete {*}[namespace children :: test_ns_*]} list [catch {namespace eval test_ns_1 {proc {} {} {}; namespace eval {} {}; {}}} msg] $msg } {1 {can't create namespace "": only global namespace can have empty name}} test namespace-15.1 {Tcl_FindNamespace, absolute name found} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_delete { namespace eval test_ns_delete2 {} proc cmd {args} {namespace current} } list [namespace delete ::test_ns_delete::test_ns_delete2] \ [namespace children ::test_ns_delete] } {{} {}} test namespace-15.2 {Tcl_FindNamespace, absolute name not found} { list [catch {namespace delete ::test_ns_delete::test_ns_delete2} msg] $msg } {1 {unknown namespace "::test_ns_delete::test_ns_delete2" in namespace delete command}} test namespace-15.3 {Tcl_FindNamespace, relative name found} { namespace eval test_ns_delete { namespace eval test_ns_delete2 {} namespace eval test_ns_delete3 {} list [namespace delete test_ns_delete2] \ [namespace children [namespace current]] } } {{} ::test_ns_delete::test_ns_delete3} test namespace-15.4 {Tcl_FindNamespace, relative name not found} { namespace eval test_ns_delete2 {} namespace eval test_ns_delete { list [catch {namespace delete test_ns_delete2} msg] $msg } } {1 {unknown namespace "test_ns_delete2" in namespace delete command}} test namespace-16.1 {Tcl_FindCommand, absolute name found} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_1 { proc cmd {args} {return "[namespace current]::cmd: $args"} variable v "::test_ns_1::cmd" eval $v one } } {::test_ns_1::cmd: one} test namespace-16.2 {Tcl_FindCommand, absolute name found} { eval $test_ns_1::v two } {::test_ns_1::cmd: two} test namespace-16.3 {Tcl_FindCommand, absolute name not found} { namespace eval test_ns_1 { variable v2 "::test_ns_1::ladidah" list [catch {eval $v2} msg] $msg } } {1 {invalid command name "::test_ns_1::ladidah"}} # save the "unknown" proc, which is redefined by the following two tests catch {rename unknown unknown.old} proc unknown {args} { return "unknown: $args" } test namespace-16.4 {Tcl_FindCommand, absolute name and TCL_GLOBAL_ONLY} { ::test_ns_1::foobar x y z } {unknown: ::test_ns_1::foobar x y z} test namespace-16.5 {Tcl_FindCommand, absolute name and TCL_GLOBAL_ONLY} { ::foobar 1 2 3 4 5 } {unknown: ::foobar 1 2 3 4 5} test namespace-16.6 {Tcl_FindCommand, relative name and TCL_GLOBAL_ONLY} { test_ns_1::foobar x y z } {unknown: test_ns_1::foobar x y z} test namespace-16.7 {Tcl_FindCommand, relative name and TCL_GLOBAL_ONLY} { foobar 1 2 3 4 5 } {unknown: foobar 1 2 3 4 5} # restore the "unknown" proc saved previously catch {rename unknown {}} catch {rename unknown.old unknown} test namespace-16.8 {Tcl_FindCommand, relative name found} { namespace eval test_ns_1 { cmd a b c } } {::test_ns_1::cmd: a b c} test namespace-16.9 {Tcl_FindCommand, relative name found} -body { proc cmd2 {args} {return "[namespace current]::cmd2: $args"} namespace eval test_ns_1 { cmd2 a b c } } -cleanup { catch {rename cmd2 {}} } -result {::::cmd2: a b c} test namespace-16.10 {Tcl_FindCommand, relative name found, only look in current then global ns} -body { proc cmd2 {args} {return "[namespace current]::cmd2: $args"} namespace eval test_ns_1 { proc cmd2 {args} { return "[namespace current]::cmd2 in test_ns_1: $args" } namespace eval test_ns_12 { cmd2 a b c } } } -cleanup { catch {rename cmd2 {}} } -result {::::cmd2: a b c} test namespace-16.11 {Tcl_FindCommand, relative name not found} { namespace eval test_ns_1 { list [catch {cmd3 a b c} msg] $msg } } {1 {invalid command name "cmd3"}} catch {unset x} test namespace-17.1 {Tcl_FindNamespaceVar, absolute name found} { catch {namespace delete {*}[namespace children :: test_ns_*]} set x 314159 namespace eval test_ns_1 { set ::x } } {314159} test namespace-17.2 {Tcl_FindNamespaceVar, absolute name found} { namespace eval test_ns_1 { variable x 777 set ::test_ns_1::x } } {777} test namespace-17.3 {Tcl_FindNamespaceVar, absolute name found} { namespace eval test_ns_1 { namespace eval test_ns_2 { variable x 1111 } set ::test_ns_1::test_ns_2::x } } {1111} test namespace-17.4 {Tcl_FindNamespaceVar, absolute name not found} { namespace eval test_ns_1 { namespace eval test_ns_2 { variable x 1111 } list [catch {set ::test_ns_1::test_ns_2::y} msg] $msg } } {1 {can't read "::test_ns_1::test_ns_2::y": no such variable}} test namespace-17.5 {Tcl_FindNamespaceVar, absolute name and TCL_GLOBAL_ONLY} { namespace eval test_ns_1 { namespace eval test_ns_3 { variable ::test_ns_1::test_ns_2::x 2222 } } set ::test_ns_1::test_ns_2::x } {2222} test namespace-17.6 {Tcl_FindNamespaceVar, relative name found} { namespace eval test_ns_1 { set x } } {777} test namespace-17.7 {Tcl_FindNamespaceVar, relative name found} { namespace eval test_ns_1 { unset x set x ;# must be global x now } } {314159} test namespace-17.8 {Tcl_FindNamespaceVar, relative name not found} { namespace eval test_ns_1 { list [catch {set wuzzat} msg] $msg } } {1 {can't read "wuzzat": no such variable}} test namespace-17.9 {Tcl_FindNamespaceVar, relative name and TCL_GLOBAL_ONLY} { namespace eval test_ns_1 { variable a hello } set test_ns_1::a } {hello} test namespace-17.10 {Tcl_FindNamespaceVar, interference with cached varNames} { namespace eval test_ns_1 {} proc test_ns {} { set ::test_ns_1::a 0 } test_ns rename test_ns {} namespace eval test_ns_1 unset a set a 0 namespace eval test_ns_1 set a 1 namespace delete test_ns_1 return $a } 1 catch {unset a} catch {unset x} catch {unset l} catch {rename foo {}} test namespace-18.1 {TclResetShadowedCmdRefs, one-level check for command shadowing} { catch {namespace delete {*}[namespace children :: test_ns_*]} proc foo {} {return "global foo"} namespace eval test_ns_1 { proc trigger {} { return [foo] } } set l "" lappend l [test_ns_1::trigger] namespace eval test_ns_1 { # force invalidation of cached ref to "foo" in proc trigger proc foo {} {return "foo in test_ns_1"} } lappend l [test_ns_1::trigger] } {{global foo} {foo in test_ns_1}} test namespace-18.2 {TclResetShadowedCmdRefs, multilevel check for command shadowing} { namespace eval test_ns_2 { proc foo {} {return "foo in ::test_ns_2"} } namespace eval test_ns_1 { namespace eval test_ns_2 {} proc trigger {} { return [test_ns_2::foo] } } set l "" lappend l [test_ns_1::trigger] namespace eval test_ns_1 { namespace eval test_ns_2 { # force invalidation of cached ref to "foo" in proc trigger proc foo {} {return "foo in ::test_ns_1::test_ns_2"} } } lappend l [test_ns_1::trigger] } {{foo in ::test_ns_2} {foo in ::test_ns_1::test_ns_2}} catch {unset l} catch {rename foo {}} test namespace-19.1 {GetNamespaceFromObj, global name found} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_1::test_ns_2 {} namespace children ::test_ns_1 } {::test_ns_1::test_ns_2} test namespace-19.2 {GetNamespaceFromObj, relative name found} { namespace eval test_ns_1 { namespace children test_ns_2 } } {} test namespace-19.3 {GetNamespaceFromObj, name not found} -body { namespace eval test_ns_1 { namespace children test_ns_99 } } -returnCodes error -result {namespace "test_ns_99" not found in "::test_ns_1"} test namespace-19.4 {GetNamespaceFromObj, invalidation of cached ns refs} { namespace eval test_ns_1 { proc foo {} { return [namespace children test_ns_2] } list [catch {namespace children test_ns_99} msg] $msg } set l {} lappend l [test_ns_1::foo] namespace delete test_ns_1::test_ns_2 namespace eval test_ns_1::test_ns_2::test_ns_3 {} lappend l [test_ns_1::foo] } {{} ::test_ns_1::test_ns_2::test_ns_3} test namespace-20.1 {Tcl_NamespaceObjCmd, bad subcommand} { catch {namespace delete {*}[namespace children :: test_ns_*]} list [catch {namespace} msg] $msg } {1 {wrong # args: should be "namespace subcommand ?arg ...?"}} test namespace-20.2 {Tcl_NamespaceObjCmd, bad subcommand} -body { namespace wombat {} } -returnCodes error -match glob -result {unknown or ambiguous subcommand "wombat": must be *} test namespace-20.3 {Tcl_NamespaceObjCmd, abbreviations are okay} { namespace ch :: test_ns_* } {} test namespace-21.1 {NamespaceChildrenCmd, no args} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_1::test_ns_2 {} expr {[string first ::test_ns_1 [namespace children]] != -1} } {1} test namespace-21.2 {NamespaceChildrenCmd, no args} { namespace eval test_ns_1 { namespace children } } {::test_ns_1::test_ns_2} test namespace-21.3 {NamespaceChildrenCmd, ns name given} { namespace children ::test_ns_1 } {::test_ns_1::test_ns_2} test namespace-21.4 {NamespaceChildrenCmd, ns name given} { namespace eval test_ns_1 { namespace children test_ns_2 } } {} test namespace-21.5 {NamespaceChildrenCmd, too many args} { namespace eval test_ns_1 { list [catch {namespace children test_ns_2 xxx yyy} msg] $msg } } {1 {wrong # args: should be "namespace children ?name? ?pattern?"}} test namespace-21.6 {NamespaceChildrenCmd, glob-style pattern given} { namespace eval test_ns_1::test_ns_foo {} namespace children test_ns_1 *f* } {::test_ns_1::test_ns_foo} test namespace-21.7 {NamespaceChildrenCmd, glob-style pattern given} { namespace eval test_ns_1::test_ns_foo {} lsort [namespace children test_ns_1 test*] } [lsort {::test_ns_1::test_ns_2 ::test_ns_1::test_ns_foo}] test namespace-21.8 {NamespaceChildrenCmd, trivial pattern starting with ::} { namespace eval test_ns_1 {} namespace children [namespace current] [fq test_ns_1] } [fq test_ns_1] test namespace-22.1 {NamespaceCodeCmd, bad args} { catch {namespace delete {*}[namespace children :: test_ns_*]} list [catch {namespace code} msg] $msg \ [catch {namespace code xxx yyy} msg] $msg } {1 {wrong # args: should be "namespace code arg"} 1 {wrong # args: should be "namespace code arg"}} test namespace-22.2 {NamespaceCodeCmd, arg is already scoped value} { namespace eval test_ns_1 { proc cmd {} {return "test_ns_1::cmd"} } namespace code {::namespace inscope ::test_ns_1 cmd} } {::namespace inscope ::test_ns_1 cmd} test namespace-22.3 {NamespaceCodeCmd, arg is already scoped value} { namespace code {namespace inscope ::test_ns_1 cmd} } {::namespace inscope :: {namespace inscope ::test_ns_1 cmd}} test namespace-22.4 {NamespaceCodeCmd, in :: namespace} { namespace code unknown } {::namespace inscope :: unknown} test namespace-22.5 {NamespaceCodeCmd, in other namespace} { namespace eval test_ns_1 { namespace code cmd } } {::namespace inscope ::test_ns_1 cmd} test namespace-22.6 {NamespaceCodeCmd, in other namespace} { namespace eval test_ns_1 { variable v 42 } namespace eval test_ns_2 { proc namespace args {} } namespace eval test_ns_2 [namespace eval test_ns_1 { namespace code {set v} }] } {42} test namespace-22.7 {NamespaceCodeCmd, Bug 3202171} { namespace eval demo { proc namespace args {puts $args} ::namespace code {namespace inscope foo} } } [list ::namespace inscope [fq demo] {namespace inscope foo}] test namespace-23.1 {NamespaceCurrentCmd, bad args} { catch {namespace delete {*}[namespace children :: test_ns_*]} list [catch {namespace current xxx} msg] $msg \ [catch {namespace current xxx yyy} msg] $msg } {1 {wrong # args: should be "namespace current"} 1 {wrong # args: should be "namespace current"}} test namespace-23.2 {NamespaceCurrentCmd, at global level} { namespace current } {::} test namespace-23.3 {NamespaceCurrentCmd, in nested ns} { namespace eval test_ns_1::test_ns_2 { namespace current } } {::test_ns_1::test_ns_2} test namespace-24.1 {NamespaceDeleteCmd, no args} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace delete } {} test namespace-24.2 {NamespaceDeleteCmd, one arg} { namespace eval test_ns_1::test_ns_2 {} namespace delete ::test_ns_1 } {} test namespace-24.3 {NamespaceDeleteCmd, two args} { namespace eval test_ns_1::test_ns_2 {} list [namespace delete ::test_ns_1::test_ns_2] [namespace delete ::test_ns_1] } {{} {}} test namespace-24.4 {NamespaceDeleteCmd, unknown ns} { list [catch {namespace delete ::test_ns_foo} msg] $msg } {1 {unknown namespace "::test_ns_foo" in namespace delete command}} test namespace-25.1 {NamespaceEvalCmd, bad args} { catch {namespace delete {*}[namespace children :: test_ns_*]} list [catch {namespace eval} msg] $msg } {1 {wrong # args: should be "namespace eval name arg ?arg...?"}} test namespace-25.2 {NamespaceEvalCmd, bad args} -body { namespace test_ns_1 } -returnCodes error -match glob -result {unknown or ambiguous subcommand "test_ns_1": must be *} catch {unset v} test namespace-25.3 {NamespaceEvalCmd, new namespace} { set v 123 namespace eval test_ns_1 { variable v 314159 proc p {} { variable v return $v } } test_ns_1::p } {314159} test namespace-25.4 {NamespaceEvalCmd, existing namespace} { namespace eval test_ns_1 { proc q {} {return [expr {[p]+1}]} } test_ns_1::q } {314160} test namespace-25.5 {NamespaceEvalCmd, multiple args} { namespace eval test_ns_1 "set" "v" } {314159} test namespace-25.6 {NamespaceEvalCmd, error in eval'd script} { list [catch {namespace eval test_ns_1 {xxxx}} msg] $msg $::errorInfo } {1 {invalid command name "xxxx"} {invalid command name "xxxx" while executing "xxxx" (in namespace eval "::test_ns_1" script line 1) invoked from within "namespace eval test_ns_1 {xxxx}"}} test namespace-25.7 {NamespaceEvalCmd, error in eval'd script} { list [catch {namespace eval test_ns_1 {error foo bar baz}} msg] $msg $::errorInfo } {1 foo {bar (in namespace eval "::test_ns_1" script line 1) invoked from within "namespace eval test_ns_1 {error foo bar baz}"}} test namespace-25.8 {NamespaceEvalCmd, error in eval'd script} { list [catch {namespace eval test_ns_1 error foo bar baz} msg] $msg $::errorInfo } {1 foo {bar (in namespace eval "::test_ns_1" script line 1) invoked from within "namespace eval test_ns_1 error foo bar baz"}} catch {unset v} test namespace-25.9 {NamespaceEvalCmd, 545325} { namespace eval test_ns_1 info level 0 } {namespace eval test_ns_1 info level 0} test namespace-26.1 {NamespaceExportCmd, no args and new ns} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace export } {} test namespace-26.2 {NamespaceExportCmd, just -clear arg} { namespace export -clear } {} test namespace-26.3 {NamespaceExportCmd, pattern can't specify a namespace} { namespace eval test_ns_1 { list [catch {namespace export ::zzz} msg] $msg } } {1 {invalid export pattern "::zzz": pattern can't specify a namespace}} test namespace-26.4 {NamespaceExportCmd, one pattern} { namespace eval test_ns_1 { namespace export cmd1 proc cmd1 {args} {return "cmd1: $args"} proc cmd2 {args} {return "cmd2: $args"} proc cmd3 {args} {return "cmd3: $args"} proc cmd4 {args} {return "cmd4: $args"} } namespace eval test_ns_2 { namespace import ::test_ns_1::* } list [info commands test_ns_2::*] [test_ns_2::cmd1 hello] } {::test_ns_2::cmd1 {cmd1: hello}} test namespace-26.5 {NamespaceExportCmd, sequence of patterns, patterns accumulate} { namespace eval test_ns_1 { namespace export cmd1 cmd3 } namespace eval test_ns_2 { namespace import -force ::test_ns_1::* } list [lsort [info commands test_ns_2::*]] [test_ns_2::cmd3 hello] } [list [lsort {::test_ns_2::cmd1 ::test_ns_2::cmd3}] {cmd3: hello}] test namespace-26.6 {NamespaceExportCmd, no patterns means return uniq'ed export list} { namespace eval test_ns_1 { namespace export } } {cmd1 cmd3} test namespace-26.7 {NamespaceExportCmd, -clear resets export list} { namespace eval test_ns_1 { namespace export -clear cmd4 } namespace eval test_ns_2 { namespace import ::test_ns_1::* } list [lsort [info commands test_ns_2::*]] [test_ns_2::cmd4 hello] } [list [lsort {::test_ns_2::cmd4 ::test_ns_2::cmd1 ::test_ns_2::cmd3}] {cmd4: hello}] test namespace-27.1 {NamespaceForgetCmd, no args} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace forget } {} test namespace-27.2 {NamespaceForgetCmd, args must be valid namespaces} { list [catch {namespace forget ::test_ns_1::xxx} msg] $msg } {1 {unknown namespace in namespace forget pattern "::test_ns_1::xxx"}} test namespace-27.3 {NamespaceForgetCmd, arg is forgotten} { namespace eval test_ns_1 { namespace export cmd* proc cmd1 {args} {return "cmd1: $args"} proc cmd2 {args} {return "cmd2: $args"} } namespace eval test_ns_2 { namespace import ::test_ns_1::* namespace forget ::test_ns_1::cmd1 } info commands ::test_ns_2::* } {::test_ns_2::cmd2} test namespace-28.1 {NamespaceImportCmd, no args} -setup { catch {namespace delete {*}[namespace children :: test_ns_*]} } -body { namespace eval ::test_ns_1 { proc foo {} {} proc bar {} {} proc boo {} {} proc glorp {} {} namespace export foo b* } namespace eval ::test_ns_2 { namespace import ::test_ns_1::* lsort [namespace import] } } -cleanup { catch {namespace delete {*}[namespace children :: test_ns_*]} } -result {bar boo foo} test namespace-28.2 {NamespaceImportCmd, no args and just "-force"} { namespace import -force } {} test namespace-28.3 {NamespaceImportCmd, arg is imported} { namespace eval test_ns_1 { namespace export cmd2 proc cmd1 {args} {return "cmd1: $args"} proc cmd2 {args} {return "cmd2: $args"} } namespace eval test_ns_2 { namespace import ::test_ns_1::* namespace forget ::test_ns_1::cmd1 } info commands test_ns_2::* } {::test_ns_2::cmd2} test namespace-29.1 {NamespaceInscopeCmd, bad args} { catch {namespace delete {*}[namespace children :: test_ns_*]} list [catch {namespace inscope} msg] $msg } {1 {wrong # args: should be "namespace inscope name arg ?arg...?"}} test namespace-29.2 {NamespaceInscopeCmd, bad args} { list [catch {namespace inscope ::} msg] $msg } {1 {wrong # args: should be "namespace inscope name arg ?arg...?"}} test namespace-29.3 {NamespaceInscopeCmd, specified ns must exist} -body { namespace inscope test_ns_1 {set v} } -returnCodes error -result {namespace "test_ns_1" not found in "::"} test namespace-29.4 {NamespaceInscopeCmd, simple case} { namespace eval test_ns_1 { variable v 747 proc cmd {args} { variable v return "[namespace current]::cmd: v=$v, args=$args" } } namespace inscope test_ns_1 cmd } {::test_ns_1::cmd: v=747, args=} test namespace-29.5 {NamespaceInscopeCmd, has lappend semantics} { list [namespace inscope test_ns_1 cmd x y z] \ [namespace eval test_ns_1 [concat cmd [list x y z]]] } {{::test_ns_1::cmd: v=747, args=x y z} {::test_ns_1::cmd: v=747, args=x y z}} test namespace-29.6 {NamespaceInscopeCmd, 1400572} { namespace inscope test_ns_1 {info level 0} } {namespace inscope test_ns_1 {info level 0}} test namespace-30.1 {NamespaceOriginCmd, bad args} { catch {namespace delete {*}[namespace children :: test_ns_*]} list [catch {namespace origin} msg] $msg } {1 {wrong # args: should be "namespace origin name"}} test namespace-30.2 {NamespaceOriginCmd, bad args} { list [catch {namespace origin x y} msg] $msg } {1 {wrong # args: should be "namespace origin name"}} test namespace-30.3 {NamespaceOriginCmd, command not found} { list [catch {namespace origin fred} msg] $msg } {1 {invalid command name "fred"}} test namespace-30.4 {NamespaceOriginCmd, command isn't imported} { namespace origin set } {::set} test namespace-30.5 {NamespaceOriginCmd, imported command} { namespace eval test_ns_1 { namespace export cmd* proc cmd1 {args} {return "cmd1: $args"} proc cmd2 {args} {return "cmd2: $args"} } namespace eval test_ns_2 { namespace export * namespace import ::test_ns_1::* proc p {} {} } namespace eval test_ns_3 { namespace import ::test_ns_2::* list [namespace origin foreach] \ [namespace origin p] \ [namespace origin cmd1] \ [namespace origin ::test_ns_2::cmd2] } } {::foreach ::test_ns_2::p ::test_ns_1::cmd1 ::test_ns_1::cmd2} test namespace-31.1 {NamespaceParentCmd, bad args} { catch {namespace delete {*}[namespace children :: test_ns_*]} list [catch {namespace parent a b} msg] $msg } {1 {wrong # args: should be "namespace parent ?name?"}} test namespace-31.2 {NamespaceParentCmd, no args} { namespace parent } {} test namespace-31.3 {NamespaceParentCmd, namespace specified} { namespace eval test_ns_1 { namespace eval test_ns_2 { namespace eval test_ns_3 {} } } list [namespace parent ::] \ [namespace parent test_ns_1::test_ns_2] \ [namespace eval test_ns_1::test_ns_2::test_ns_3 {namespace parent ::test_ns_1::test_ns_2}] } {{} ::test_ns_1 ::test_ns_1} test namespace-31.4 {NamespaceParentCmd, bad namespace specified} -body { namespace parent test_ns_1::test_ns_foo } -returnCodes error -result {namespace "test_ns_1::test_ns_foo" not found in "::"} test namespace-32.1 {NamespaceQualifiersCmd, bad args} { catch {namespace delete {*}[namespace children :: test_ns_*]} list [catch {namespace qualifiers} msg] $msg } {1 {wrong # args: should be "namespace qualifiers string"}} test namespace-32.2 {NamespaceQualifiersCmd, bad args} { list [catch {namespace qualifiers x y} msg] $msg } {1 {wrong # args: should be "namespace qualifiers string"}} test namespace-32.3 {NamespaceQualifiersCmd, simple name} { namespace qualifiers foo } {} test namespace-32.4 {NamespaceQualifiersCmd, leading ::} { namespace qualifiers ::x::y::z } {::x::y} test namespace-32.5 {NamespaceQualifiersCmd, no leading ::} { namespace qualifiers a::b } {a} test namespace-32.6 {NamespaceQualifiersCmd, :: argument} { namespace qualifiers :: } {} test namespace-32.7 {NamespaceQualifiersCmd, odd number of :s} { namespace qualifiers ::::: } {} test namespace-32.8 {NamespaceQualifiersCmd, odd number of :s} { namespace qualifiers foo::: } {foo} test namespace-33.1 {NamespaceTailCmd, bad args} { catch {namespace delete {*}[namespace children :: test_ns_*]} list [catch {namespace tail} msg] $msg } {1 {wrong # args: should be "namespace tail string"}} test namespace-33.2 {NamespaceTailCmd, bad args} { list [catch {namespace tail x y} msg] $msg } {1 {wrong # args: should be "namespace tail string"}} test namespace-33.3 {NamespaceTailCmd, simple name} { namespace tail foo } {foo} test namespace-33.4 {NamespaceTailCmd, leading ::} { namespace tail ::x::y::z } {z} test namespace-33.5 {NamespaceTailCmd, no leading ::} { namespace tail a::b } {b} test namespace-33.6 {NamespaceTailCmd, :: argument} { namespace tail :: } {} test namespace-33.7 {NamespaceTailCmd, odd number of :s} { namespace tail ::::: } {} test namespace-33.8 {NamespaceTailCmd, odd number of :s} { namespace tail foo::: } {} test namespace-34.1 {NamespaceWhichCmd, bad args} { catch {namespace delete {*}[namespace children :: test_ns_*]} list [catch {namespace which} msg] $msg } {1 {wrong # args: should be "namespace which ?-command? ?-variable? name"}} test namespace-34.2 {NamespaceWhichCmd, bad args} { list [catch {namespace which -fred x} msg] $msg } {1 {wrong # args: should be "namespace which ?-command? ?-variable? name"}} test namespace-34.3 {NamespaceWhichCmd, single arg is always command name} { namespace which -command } {} test namespace-34.4 {NamespaceWhichCmd, bad args} { list [catch {namespace which a b} msg] $msg } {1 {wrong # args: should be "namespace which ?-command? ?-variable? name"}} test namespace-34.5 {NamespaceWhichCmd, command lookup} { namespace eval test_ns_1 { namespace export cmd* variable v1 111 proc cmd1 {args} {return "cmd1: $args"} proc cmd2 {args} {return "cmd2: $args"} } namespace eval test_ns_2 { namespace export * namespace import ::test_ns_1::* variable v2 222 proc p {} {} } namespace eval test_ns_3 { namespace import ::test_ns_2::* variable v3 333 list [namespace which -command foreach] \ [namespace which -command p] \ [namespace which -command cmd1] \ [namespace which -command ::test_ns_2::cmd2] \ [catch {namespace which -command ::test_ns_2::noSuchCmd} msg] $msg } } {::foreach ::test_ns_3::p ::test_ns_3::cmd1 ::test_ns_2::cmd2 0 {}} test namespace-34.6 {NamespaceWhichCmd, -command is default} { namespace eval test_ns_3 { list [namespace which foreach] \ [namespace which p] \ [namespace which cmd1] \ [namespace which ::test_ns_2::cmd2] } } {::foreach ::test_ns_3::p ::test_ns_3::cmd1 ::test_ns_2::cmd2} test namespace-34.7 {NamespaceWhichCmd, variable lookup} { namespace eval test_ns_3 { list [namespace which -variable env] \ [namespace which -variable v3] \ [namespace which -variable ::test_ns_2::v2] \ [catch {namespace which -variable ::test_ns_2::noSuchVar} msg] $msg } } {::env ::test_ns_3::v3 ::test_ns_2::v2 0 {}} test namespace-35.1 {FreeNsNameInternalRep, resulting ref count > 0} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_1 { proc p {} { namespace delete [namespace current] return [namespace current] } } test_ns_1::p } {::test_ns_1} test namespace-35.2 {FreeNsNameInternalRep, resulting ref count == 0} { namespace eval test_ns_1 { proc q {} { return [namespace current] } } list [test_ns_1::q] \ [namespace delete test_ns_1] \ [catch {test_ns_1::q} msg] $msg } {::test_ns_1 {} 1 {invalid command name "test_ns_1::q"}} catch {unset x} catch {unset y} test namespace-36.1 {DupNsNameInternalRep} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_1 {} set x "::test_ns_1" list [namespace parent $x] [set y $x] [namespace parent $y] } {:: ::test_ns_1 ::} catch {unset x} catch {unset y} test namespace-37.1 {SetNsNameFromAny, ns name found} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval test_ns_1::test_ns_2 {} namespace eval test_ns_1 { namespace children ::test_ns_1 } } {::test_ns_1::test_ns_2} test namespace-37.2 {SetNsNameFromAny, ns name not found} -body { namespace eval test_ns_1 { namespace children ::test_ns_1::test_ns_foo } } -returnCodes error -result {namespace "::test_ns_1::test_ns_foo" not found} test namespace-38.1 {UpdateStringOfNsName} { catch {namespace delete {*}[namespace children :: test_ns_*]} ;# Tcl_NamespaceObjCmd calls UpdateStringOfNsName to get subcmd name list [namespace eval {} {namespace current}] \ [namespace eval {} {namespace current}] } {:: ::} test namespace-39.1 {NamespaceExistsCmd} { catch {namespace delete {*}[namespace children :: test_ns_*]} namespace eval ::test_ns_z::test_me { variable foo } list [namespace exists ::] \ [namespace exists ::bogus_namespace] \ [namespace exists ::test_ns_z] \ [namespace exists test_ns_z] \ [namespace exists ::test_ns_z::foo] \ [namespace exists ::test_ns_z::test_me] \ [namespace eval ::test_ns_z { namespace exists ::test_me }] \ [namespace eval ::test_ns_z { namespace exists test_me }] \ [namespace exists :::::test_ns_z] } {1 0 1 1 0 1 0 1 1} test namespace-39.2 {NamespaceExistsCmd error} { list [catch {namespace exists} msg] $msg } {1 {wrong # args: should be "namespace exists name"}} test namespace-39.3 {NamespaceExistsCmd error} { list [catch {namespace exists a b} msg] $msg } {1 {wrong # args: should be "namespace exists name"}} test namespace-40.1 {Ignoring namespace proc "unknown"} -setup { rename unknown _unknown } -body { proc unknown args {return global} namespace eval ns {proc unknown args {return local}} list [namespace eval ns aaa bbb] [namespace eval ns aaa] } -cleanup { rename unknown {} rename _unknown unknown namespace delete ns } -result {global global} test namespace-41.1 {Shadowing byte-compiled commands, Bug: 231259} { set res {} namespace eval ns { set res {} proc test {} { set ::g 0 } lappend ::res [test] proc set {a b} { ::set a [incr b] } lappend ::res [test] } namespace delete ns set res } {0 1} test namespace-41.2 {Shadowing byte-compiled commands, Bug: 231259} { set res {} namespace eval ns {} proc ns::a {i} { variable b proc set args {return "New proc is called"} return [set b $i] } ns::a 1 set res [ns::a 2] namespace delete ns set res } {New proc is called} test namespace-41.3 {Shadowing byte-compiled commands, Bugs: 231259, 729692} { set res {} namespace eval ns { variable b 0 } proc ns::a {i} { variable b proc set args {return "New proc is called"} return [set b $i] } set res [list [ns::a 1] $ns::b] namespace delete ns set res } {{New proc is called} 0} # Ensembles (TIP#112) test namespace-42.1 {ensembles: basic} { namespace eval ns { namespace export x proc x {} {format 1} namespace ensemble create } list [info command ns] [ns x] [namespace delete ns] [info command ns] } {ns 1 {} {}} test namespace-42.2 {ensembles: basic} { namespace eval ns { namespace export x proc x {} {format 1} namespace ensemble create } rename ns foo list [info command foo] [foo x] [namespace delete ns] [info command foo] } {foo 1 {} {}} test namespace-42.3 {ensembles: basic} { namespace eval ns { namespace export x* proc x1 {} {format 1} proc x2 {} {format 2} namespace ensemble create } set result [list [ns x1] [ns x2]] lappend result [catch {ns x} msg] $msg rename ns {} lappend result [info command ns::x1] namespace delete ns lappend result [info command ns::x1] } {1 2 1 {unknown or ambiguous subcommand "x": must be x1, or x2} ::ns::x1 {}} test namespace-42.4 {ensembles: basic} -body { namespace eval ns { namespace export y* proc x1 {} {format 1} proc x2 {} {format 2} namespace ensemble create } list [catch {ns x} msg] $msg } -cleanup { namespace delete ns } -result {1 {unknown subcommand "x": namespace ::ns does not export any commands}} test namespace-42.5 {ensembles: basic} -body { namespace eval ns { namespace export x* proc x1 {} {format 1} proc x2 {} {format 2} proc x3 {} {format 3} namespace ensemble create } list [catch {ns x} msg] $msg } -cleanup { namespace delete ns } -result {1 {unknown or ambiguous subcommand "x": must be x1, x2, or x3}} test namespace-42.6 {ensembles: nested} -body { namespace eval ns { namespace export x* namespace eval x0 { proc z {} {format 0} namespace export z namespace ensemble create } proc x1 {} {format 1} proc x2 {} {format 2} proc x3 {} {format 3} namespace ensemble create } list [ns x0 z] [ns x1] [ns x2] [ns x3] } -cleanup { namespace delete ns } -result {0 1 2 3} test namespace-42.7 {ensembles: nested} -body { namespace eval ns { namespace export x* namespace eval x0 { proc z {} {list [info level] [info level 1]} namespace export z namespace ensemble create } proc x1 {} {format 1} proc x2 {} {format 2} proc x3 {} {format 3} namespace ensemble create } list [ns x0 z] [ns x1] [ns x2] [ns x3] } -cleanup { namespace delete ns } -result {{1 ::ns::x0::z} 1 2 3} test namespace-42.8 {ensembles: [Bug 1670091]} -setup { proc demo args {} variable target [list [namespace which demo] x] proc trial args {variable target; string length $target} trace add execution demo enter [namespace code trial] namespace ensemble create -command foo -map [list bar $target] } -body { foo bar } -cleanup { unset target rename demo {} rename trial {} rename foo {} } -result {} test namespace-43.1 {ensembles: dict-driven} { namespace eval ns { namespace export x* proc x1 {} {format 1} proc x2 {} {format 2} namespace ensemble create -map {a x1 b x2} } set result [list [catch {ns c} msg] $msg [namespace ensemble exists ns]] rename ns {} lappend result [namespace ensemble exists ns] } {1 {unknown or ambiguous subcommand "c": must be a, or b} 1 0} test namespace-43.2 {ensembles: dict-driven} -body { namespace eval ns { namespace export x* proc x1 {args} {list 1 $args} proc x2 {args} {list 2 [llength $args]} namespace ensemble create -map { a ::ns::x1 b ::ns::x2 c {::ns::x1 .} d {::ns::x2 .} } } list [ns a] [ns b] [ns c] [ns c foo] [ns d] [ns d foo] } -cleanup { namespace delete ns } -result {{1 {}} {2 0} {1 .} {1 {. foo}} {2 1} {2 2}} set SETUP { namespace eval ns { namespace export a b proc a args {format 1,[llength $args]} proc b args {format 2,[llength $args]} proc c args {format 3,[llength $args]} proc d args {format 4,[llength $args]} namespace ensemble create -subcommands {b c} } } test namespace-43.3 {ensembles: list-driven} -setup $SETUP -body { namespace delete ns } -result {} test namespace-43.4 {ensembles: list-driven} -setup $SETUP -body { ns a foo bar boo spong wibble } -cleanup {namespace delete ns} -returnCodes error -result {unknown or ambiguous subcommand "a": must be b, or c} test namespace-43.5 {ensembles: list-driven} -setup $SETUP -body { ns b foo bar boo spong wibble } -cleanup {namespace delete ns} -result 2,5 test namespace-43.6 {ensembles: list-driven} -setup $SETUP -body { ns c foo bar boo spong wibble } -cleanup {namespace delete ns} -result 3,5 test namespace-43.7 {ensembles: list-driven} -setup $SETUP -body { ns d foo bar boo spong wibble } -cleanup {namespace delete ns} -returnCodes error -result {unknown or ambiguous subcommand "d": must be b, or c} set SETUP { namespace eval ns { namespace export a b proc a args {format 1,[llength $args]} proc b args {format 2,[llength $args]} proc c args {format 3,[llength $args]} proc d args {format 4,[llength $args]} namespace ensemble create -subcommands {b c} -map {c ::ns::d} } } test namespace-43.8 {ensembles: list-and-map-driven} -setup $SETUP -body { namespace delete ns } -result {} test namespace-43.9 {ensembles: list-and-map-driven} -setup $SETUP -body { ns a foo bar boo spong wibble } -cleanup {namespace delete ns} -returnCodes error -result {unknown or ambiguous subcommand "a": must be b, or c} test namespace-43.10 {ensembles: list-and-map-driven} -setup $SETUP -body { ns b foo bar boo spong wibble } -cleanup {namespace delete ns} -result 2,5 test namespace-43.11 {ensembles: list-and-map-driven} -setup $SETUP -body { ns c foo bar boo spong wibble } -cleanup {namespace delete ns} -result 4,5 test namespace-43.12 {ensembles: list-and-map-driven} -setup $SETUP -body { ns d foo bar boo spong wibble } -cleanup {namespace delete ns} -returnCodes error -result {unknown or ambiguous subcommand "d": must be b, or c} set SETUP { namespace eval ns { namespace export * proc foo args {format bar} proc spong args {format wibble} namespace ensemble create -prefixes off } } test namespace-43.13 {ensembles: turn off prefixes} -setup $SETUP -body { namespace delete ns } -result {} test namespace-43.14 {ensembles: turn off prefixes} -setup $SETUP -body { ns fo } -cleanup {namespace delete ns} -returnCodes error -result {unknown subcommand "fo": must be foo, or spong} test namespace-43.15 {ensembles: turn off prefixes} -setup $SETUP -body { ns foo } -cleanup {namespace delete ns} -result bar test namespace-43.16 {ensembles: turn off prefixes} -setup $SETUP -body { ns s } -cleanup {namespace delete ns} -returnCodes error -result {unknown subcommand "s": must be foo, or spong} test namespace-43.17 {ensembles: turn off prefixes} -setup $SETUP -body { ns spong } -cleanup {namespace delete ns} -result wibble test namespace-44.1 {ensemble: errors} { list [catch {namespace ensemble} msg] $msg } {1 {wrong # args: should be "namespace ensemble subcommand ?arg ...?"}} test namespace-44.2 {ensemble: errors} { list [catch {namespace ensemble ?} msg] $msg } {1 {bad subcommand "?": must be configure, create, or exists}} test namespace-44.3 {ensemble: errors} { namespace eval ns { list [catch {namespace ensemble create -map x} msg] $msg } } {1 {missing value to go with key}} test namespace-44.4 {ensemble: errors} { namespace eval ns { list [catch {namespace ensemble create -map {x {}}} msg] $msg } } {1 {ensemble subcommand implementations must be non-empty lists}} test namespace-44.5 {ensemble: errors} -setup { namespace ensemble create -command foobar -subcommands {foobarcget foobarconfigure} } -body { foobar foobarcon } -cleanup { rename foobar {} } -returnCodes error -result {invalid command name "::foobarconfigure"} test namespace-44.6 {ensemble: errors} -returnCodes error -body { namespace ensemble create gorp } -result {wrong # args: should be "namespace ensemble create ?option value ...?"} test namespace-45.1 {ensemble: introspection} { namespace eval ns { namespace export x proc x {} {} namespace ensemble create set ::result [namespace ensemble configure ::ns] } namespace delete ns set result } {-map {} -namespace ::ns -parameters {} -prefixes 1 -subcommands {} -unknown {}} test namespace-45.2 {ensemble: introspection} { namespace eval ns { namespace export x proc x {} {} namespace ensemble create -map {A x} set ::result [namespace ensemble configure ::ns -map] } namespace delete ns set result } {A ::ns::x} test namespace-46.1 {ensemble: modification} { namespace eval ns { namespace export x proc x {} {format 123} # Ensemble maps A->x namespace ensemble create -command ns -map {A ::ns::x} set ::result [list [namespace ensemble configure ns -map] [ns A]] # Ensemble maps B->x namespace ensemble configure ns -map {B ::ns::x} lappend ::result [namespace ensemble configure ns -map] [ns B] # Ensemble maps x->x namespace ensemble configure ns -map {} lappend ::result [namespace ensemble configure ns -map] [ns x] } namespace delete ns set result } {{A ::ns::x} 123 {B ::ns::x} 123 {} 123} test namespace-46.2 {ensemble: ensembles really use current export list} { namespace eval ns { namespace export x1 proc x1 {} {format 1} proc x2 {} {format 1} namespace ensemble create } catch {ns ?} msg; set result [list $msg] namespace eval ns {namespace export x*} catch {ns ?} msg; lappend result $msg rename ns::x1 {} catch {ns ?} msg; lappend result $msg namespace delete ns set result } {{unknown or ambiguous subcommand "?": must be x1} {unknown or ambiguous subcommand "?": must be x1, or x2} {unknown or ambiguous subcommand "?": must be x2}} test namespace-46.3 {ensemble: implementation errors} { namespace eval ns { variable count 0 namespace ensemble create -map { a {::lappend ::result} b {::incr ::ns::count} } } set result {} lappend result [catch { ns } msg] $msg ns a [ns b 10] catch {rename p {}} rename ns p p a [p b 3000] lappend result $ns::count namespace delete ns lappend result [info command p] } {1 {wrong # args: should be "ns subcommand ?arg ...?"} 10 3010 3010 {}} test namespace-46.4 {ensemble: implementation errors} { namespace eval ns { namespace ensemble create } set result [info command ns] lappend result [catch {ns ?} msg] $msg namespace delete ns set result } {ns 1 {unknown subcommand "?": namespace ::ns does not export any commands}} test namespace-46.5 {ensemble: implementation errors} { namespace eval ns { namespace ensemble create -map {makeError ::error} } list [catch {ns makeError "an error happened"} msg] $msg $::errorInfo [namespace delete ns] } {1 {an error happened} {an error happened while executing "ns makeError "an error happened""} {}} test namespace-46.6 {ensemble: implementation renames/deletes itself} { namespace eval ns { namespace ensemble create -map {to ::rename} } ns to ns foo foo to foo bar bar to bar spong spong to spong {} namespace delete ns } {} test namespace-46.7 {ensemble: implementation deletes its namespace} { namespace eval ns { namespace ensemble create -map {kill {::namespace delete}} } ns kill ns } {} test namespace-46.8 {ensemble: implementation deletes its namespace} { namespace eval ns { namespace export * proc foo {} { variable x 1 bar # Tricky; what is the correct return value anyway? info exist x } proc bar {} { namespace delete [namespace current] } namespace ensemble create } list [ns foo] [info exist ns::x] } {1 0} test namespace-46.9 {ensemble: configuring really configures things} { namespace eval ns { namespace ensemble create -map {a a} -prefixes 0 } set result [list [catch {ns x} msg] $msg] namespace ensemble configure ns -map {b b} lappend result [catch {ns x} msg] $msg namespace delete ns set result } {1 {unknown subcommand "x": must be a} 1 {unknown subcommand "x": must be b}} test namespace-47.1 {ensemble: unknown handler} { set log {} namespace eval ns { namespace export {[a-z]*} proc Magic {ensemble subcmd args} { global log if {[string match {[a-z]*} $subcmd]} { lappend log "making $subcmd" proc $subcmd args { global log lappend log "running [info level 0]" llength $args } } else { lappend log "unknown $subcmd - args = $args" return -code error \ "unknown or protected subcommand \"$subcmd\"" } } namespace ensemble create -unknown ::ns::Magic } set result {} lappend result [catch {ns a b c} msg] $msg lappend result [catch {ns a b c} msg] $msg lappend result [catch {ns b c d} msg] $msg lappend result [catch {ns c d e} msg] $msg lappend result [catch {ns Magic foo bar spong wibble} msg] $msg list $result [lsort [info commands ::ns::*]] $log [namespace delete ns] } {{0 2 0 2 0 2 0 2 1 {unknown or protected subcommand "Magic"}} {::ns::Magic ::ns::a ::ns::b ::ns::c} {{making a} {running ::ns::a b c} {running ::ns::a b c} {making b} {running ::ns::b c d} {making c} {running ::ns::c d e} {unknown Magic - args = foo bar spong wibble}} {}} test namespace-47.2 {ensemble: unknown handler} { namespace eval ns { namespace export {[a-z]*} proc Magic {ensemble subcmd args} { error foobar } namespace ensemble create -unknown ::ns::Magic } list [catch {ns spong} msg] $msg $::errorInfo [namespace delete ns] } {1 foobar {foobar while executing "error foobar" (procedure "::ns::Magic" line 2) invoked from within "::ns::Magic ::ns spong" (ensemble unknown subcommand handler) invoked from within "ns spong"} {}} test namespace-47.3 {ensemble: unknown handler} { namespace eval ns { variable count 0 namespace export {[a-z]*} proc a {} {} proc c {} {} proc Magic {ensemble subcmd args} { variable count incr count proc b {} {} } namespace ensemble create -unknown ::ns::Magic } list [catch {ns spong} msg] $msg $ns::count [namespace delete ns] } {1 {unknown or ambiguous subcommand "spong": must be a, b, or c} 1 {}} test namespace-47.4 {ensemble: unknown handler} { namespace eval ns { namespace export {[a-z]*} proc Magic {ensemble subcmd args} { return -code break } namespace ensemble create -unknown ::ns::Magic } list [catch {ns spong} msg] $msg $::errorInfo [namespace delete ns] } {1 {unknown subcommand handler returned bad code: break} {unknown subcommand handler returned bad code: break result of ensemble unknown subcommand handler: ::ns::Magic ::ns spong invoked from within "ns spong"} {}} test namespace-47.5 {ensemble: unknown handler} { namespace ensemble create -command foo -unknown bar proc bar {args} { global result target lappend result "LOG $args" return $target } set result {} set target {} lappend result [catch {foo bar} msg] $msg set target {lappend result boo hoo} lappend result [catch {foo bar} msg] $msg [namespace ensemble config foo] rename foo {} set result } {{LOG ::foo bar} 1 {unknown subcommand "bar": namespace :: does not export any commands} {LOG ::foo bar} boo hoo 0 {{LOG ::foo bar} 1 {unknown subcommand "bar": namespace :: does not export any commands} {LOG ::foo bar} boo hoo} {-map {} -namespace :: -parameters {} -prefixes 1 -subcommands {} -unknown bar}} test namespace-47.6 {ensemble: unknown handler} { namespace ensemble create -command foo -unknown bar proc bar {args} { return "\{" } set result [list [catch {foo bar} msg] $msg $::errorInfo] rename foo {} set result } {1 {unmatched open brace in list} {unmatched open brace in list while parsing result of ensemble unknown subcommand handler invoked from within "foo bar"}} test namespace-47.7 {ensemble: unknown handler, commands with spaces} { namespace ensemble create -command foo -unknown bar proc bar {args} { list ::set ::x [join $args |] } set result [foo {one two three}] rename foo {} set result } {::foo|one two three} test namespace-47.8 {ensemble: unknown handler, commands with spaces} { namespace ensemble create -command foo -unknown {bar boo} proc bar {args} { list ::set ::x [join $args |] } set result [foo {one two three}] rename foo {} set result } {boo|::foo|one two three} test namespace-48.1 {ensembles and namespace import: unknown handler} { namespace eval foo { namespace export bar namespace ensemble create -command bar -unknown ::foo::u -subcomm x proc u {ens args} { global result lappend result $ens $args namespace ensemble config $ens -subcommand {x y} } proc u2 {ens args} { global result lappend result $ens $args namespace ensemble config ::bar -subcommand {x y z} } proc x args { global result lappend result XXX $args } proc y args { global result lappend result YYY $args } proc z args { global result lappend result ZZZ $args } } namespace import -force foo::bar set result [list [namespace ensemble config bar]] bar x 123 bar y 456 namespace ensemble config bar -unknown ::foo::u2 bar z 789 namespace delete foo set result } {{-map {} -namespace ::foo -parameters {} -prefixes 1 -subcommands x -unknown ::foo::u} XXX 123 ::foo::bar {y 456} YYY 456 ::foo::bar {z 789} ZZZ 789} test namespace-48.2 {ensembles and namespace import: exists} { namespace eval foo { namespace ensemble create -command ::foo::bar namespace export bar } set result [namespace ensemble exist foo::bar] lappend result [namespace ensemble exist bar] namespace import foo::bar lappend result [namespace ensemble exist bar] rename foo::bar foo::bar2 lappend result [namespace ensemble exist bar] \ [namespace ensemble exist spong] rename bar spong lappend result [namespace ensemble exist bar] \ [namespace ensemble exist spong] rename foo::bar2 {} lappend result [namespace ensemble exist spong] namespace delete foo set result } {1 0 1 1 0 0 1 0} test namespace-48.3 {ensembles and namespace import: config} { catch {rename spong {}} namespace eval foo { namespace ensemble create -command ::foo::bar namespace export bar boo proc boo {} {} } namespace import foo::bar foo::boo set result [namespace ensemble config bar -namespace] lappend result [catch {namespace ensemble config boo} msg] $msg lappend result [catch {namespace ensemble config spong} msg] $msg namespace delete foo set result } {::foo 1 {"boo" is not an ensemble command} 1 {unknown command "spong"}} test namespace-49.1 {ensemble subcommand caching} -body { namespace ens cre -command a -map {b {lappend result 1}} namespace ens cre -command c -map {b {lappend result 2}} proc x {} {a b; c b; a b; c b} x } -result {1 2 1 2} -cleanup { rename a {} rename c {} rename x {} } test namespace-49.2 {strange delete crash} -body { namespace eval foo {namespace ensemble create -command ::bar} trace add command ::bar delete DeleteTrace proc DeleteTrace {old new op} { trace remove command ::bar delete DeleteTrace rename $old "" # This next line caused a bus error in [Bug 1220058] namespace delete foo } rename ::bar "" } -result "" -cleanup { rename DeleteTrace "" } test namespace-50.1 {ensembles affect proc arguments error messages} -body { namespace ens cre -command a -map {b {bb foo}} proc bb {c d {e f} args} {list $c $args} a b } -returnCodes error -result "wrong # args: should be \"a b d ?e? ?arg ...?\"" -cleanup { rename a {} rename bb {} } test namespace-50.2 {ensembles affect WrongNumArgs error messages} -body { namespace ens cre -command a -map {b {string is}} a b boolean } -returnCodes error -result "wrong # args: should be \"a b class ?-strict? ?-failindex var? str\"" -cleanup { rename a {} } test namespace-50.3 {chained ensembles affect error messages} -body { namespace ens cre -command a -map {b c} namespace ens cre -command c -map {d e} proc e f {} a b d } -returnCodes error -result "wrong # args: should be \"a b d f\"" -cleanup { rename a {} rename c {} } test namespace-50.4 {chained ensembles affect error messages} -body { namespace ens cre -command a -map {b {c d}} namespace ens cre -command c -map {d {e f}} proc e f {} a b d } -returnCodes error -result "wrong # args: should be \"a b\"" -cleanup { rename a {} rename c {} } test namespace-51.1 {name resolution path control} -body { namespace eval ::test_ns_1 { namespace eval test_ns_2 { proc pathtestA {} { ::return [pathtestB],[pathtestC],[pathtestD],[namespace path] } proc pathtestC {} { ::return 2 } } proc pathtestB {} { return 1 } proc pathtestC {} { return 1 } namespace path ::test_ns_1 } proc ::pathtestB {} { return global } proc ::pathtestD {} { return global } test_ns_1::test_ns_2::pathtestA } -result "global,2,global," -cleanup { namespace delete ::test_ns_1 catch {rename ::pathtestB {}} catch {rename ::pathtestD {}} } test namespace-51.2 {name resolution path control} -body { namespace eval ::test_ns_1 { namespace eval test_ns_2 { namespace path ::test_ns_1 proc pathtestA {} { ::return [pathtestB],[pathtestC],[pathtestD],[namespace path] } proc pathtestC {} { ::return 2 } } proc pathtestB {} { return 1 } proc pathtestC {} { return 1 } } proc ::pathtestB {} { return global } proc ::pathtestD {} { return global } ::test_ns_1::test_ns_2::pathtestA } -result "1,2,global,::test_ns_1" -cleanup { namespace delete ::test_ns_1 catch {rename ::pathtestB {}} catch {rename ::pathtestD {}} } test namespace-51.3 {name resolution path control} -body { namespace eval ::test_ns_1 { namespace eval test_ns_2 { proc pathtestA {} { ::return [pathtestB],[pathtestC],[pathtestD],[namespace path] } proc pathtestC {} { ::return 2 } } proc pathtestB {} { return 1 } proc pathtestC {} { return 1 } } proc ::pathtestB {} { return global } proc ::pathtestD {} { return global } set result [::test_ns_1::test_ns_2::pathtestA] namespace eval ::test_ns_1::test_ns_2 { namespace path ::test_ns_1 } lappend result [::test_ns_1::test_ns_2::pathtestA] rename ::test_ns_1::pathtestB {} lappend result [::test_ns_1::test_ns_2::pathtestA] } -result "global,2,global, 1,2,global,::test_ns_1 global,2,global,::test_ns_1" -cleanup { namespace delete ::test_ns_1 catch {rename ::pathtestB {}} catch {rename ::pathtestD {}} } test namespace-51.4 {name resolution path control} -body { namespace eval ::test_ns_1 { namespace eval test_ns_2 { proc pathtestA {} { ::return [pathtestB],[pathtestC],[pathtestD],[namespace path] } proc pathtestC {} { ::return 2 } } proc pathtestB {} { return 1 } proc pathtestC {} { return 1 } } proc ::pathtestB {} { return global } proc ::pathtestD {} { return global } set result [::test_ns_1::test_ns_2::pathtestA] namespace eval ::test_ns_1::test_ns_2 { namespace path ::test_ns_1 } lappend result [::test_ns_1::test_ns_2::pathtestA] namespace eval ::test_ns_1::test_ns_2 { namespace path {} } lappend result [::test_ns_1::test_ns_2::pathtestA] } -result "global,2,global, 1,2,global,::test_ns_1 global,2,global," -cleanup { namespace delete ::test_ns_1 catch {rename ::pathtestB {}} catch {rename ::pathtestD {}} } test namespace-51.5 {name resolution path control} -body { namespace eval ::test_ns_1 { namespace eval test_ns_2 { proc pathtestA {} { ::return [pathtestB],[pathtestC],[pathtestD],[namespace path] } proc pathtestC {} { ::return 2 } namespace path ::test_ns_1 } proc pathtestB {} { return 1 } proc pathtestC {} { return 1 } proc pathtestD {} { return 1 } } proc ::pathtestB {} { return global } proc ::pathtestD {} { return global } set result [::test_ns_1::test_ns_2::pathtestA] namespace eval ::test_ns_1::test_ns_2 { namespace path {:: ::test_ns_1} } lappend result [::test_ns_1::test_ns_2::pathtestA] rename ::test_ns_1::test_ns_2::pathtestC {} lappend result [::test_ns_1::test_ns_2::pathtestA] } -result "1,2,1,::test_ns_1 {global,2,global,:: ::test_ns_1} {global,1,global,:: ::test_ns_1}" -cleanup { namespace delete ::test_ns_1 catch {rename ::pathtestB {}} catch {rename ::pathtestD {}} } test namespace-51.6 {name resolution path control} -body { namespace eval ::test_ns_1 { namespace eval test_ns_2 { proc pathtestA {} { ::return [pathtestB],[pathtestC],[pathtestD],[namespace path] } proc pathtestC {} { ::return 2 } namespace path ::test_ns_1 } proc pathtestB {} { return 1 } proc pathtestC {} { return 1 } proc pathtestD {} { return 1 } } proc ::pathtestB {} { return global } proc ::pathtestD {} { return global } set result [::test_ns_1::test_ns_2::pathtestA] namespace eval ::test_ns_1::test_ns_2 { namespace path {:: ::test_ns_1} } lappend result [::test_ns_1::test_ns_2::pathtestA] rename ::test_ns_1::test_ns_2::pathtestC {} lappend result [::test_ns_1::test_ns_2::pathtestA] proc ::pathtestC {} { return global } lappend result [::test_ns_1::test_ns_2::pathtestA] } -result "1,2,1,::test_ns_1 {global,2,global,:: ::test_ns_1} {global,1,global,:: ::test_ns_1} {global,global,global,:: ::test_ns_1}" -cleanup { namespace delete ::test_ns_1 catch {rename ::pathtestB {}} catch {rename ::pathtestD {}} } test namespace-51.7 {name resolution path control} -body { namespace eval ::test_ns_1 { } namespace eval ::test_ns_2 { namespace path ::test_ns_1 proc getpath {} {namespace path} } list [::test_ns_2::getpath] [namespace delete ::test_ns_1] [::test_ns_2::getpath] } -result {::test_ns_1 {} {}} -cleanup { catch {namespace delete ::test_ns_1} namespace delete ::test_ns_2 } test namespace-51.8 {name resolution path control} -body { namespace eval ::test_ns_1 { } namespace eval ::test_ns_2 { } namespace eval ::test_ns_3 { } namespace eval ::test_ns_4 { namespace path {::test_ns_1 ::test_ns_2 ::test_ns_3} proc getpath {} {namespace path} } list [::test_ns_4::getpath] [namespace delete ::test_ns_2] [::test_ns_4::getpath] } -result {{::test_ns_1 ::test_ns_2 ::test_ns_3} {} {::test_ns_1 ::test_ns_3}} -cleanup { catch {namespace delete ::test_ns_1} catch {namespace delete ::test_ns_2} catch {namespace delete ::test_ns_3} catch {namespace delete ::test_ns_4} } test namespace-51.9 {name resolution path control} -body { namespace eval ::test_ns_1 { } namespace eval ::test_ns_2 { } namespace eval ::test_ns_3 { } namespace eval ::test_ns_4 { namespace path {::test_ns_1 ::test_ns_2 ::test_ns_3} proc getpath {} {namespace path} } list [::test_ns_4::getpath] [namespace delete ::test_ns_2] [namespace eval ::test_ns_2 {}] [::test_ns_4::getpath] } -result {{::test_ns_1 ::test_ns_2 ::test_ns_3} {} {} {::test_ns_1 ::test_ns_3}} -cleanup { catch {namespace delete ::test_ns_1} catch {namespace delete ::test_ns_2} catch {namespace delete ::test_ns_3} catch {namespace delete ::test_ns_4} } test namespace-51.10 {name resolution path control} -body { namespace eval ::test_ns_1 { namespace path does::not::exist } } -returnCodes error -result {namespace "does::not::exist" not found in "::test_ns_1"} -cleanup { catch {namespace delete ::test_ns_1} } test namespace-51.11 {name resolution path control} -body { namespace eval ::test_ns_1 { proc foo {} {return 1} } namespace eval ::test_ns_2 { proc foo {} {return 2} } namespace eval ::test_ns_3 { namespace path ::test_ns_1 } namespace eval ::test_ns_4 { namespace path {::test_ns_3 ::test_ns_2} foo } } -result 2 -cleanup { catch {namespace delete ::test_ns_1} catch {namespace delete ::test_ns_2} catch {namespace delete ::test_ns_3} catch {namespace delete ::test_ns_4} } test namespace-51.12 {name resolution path control} -body { namespace eval ::test_ns_1 { proc foo {} {return 1} } namespace eval ::test_ns_2 { proc foo {} {return 2} } namespace eval ::test_ns_3 { namespace path ::test_ns_1 } namespace eval ::test_ns_4 { namespace path {::test_ns_3 ::test_ns_2} list [foo] [namespace delete ::test_ns_3] [foo] } } -result {2 {} 2} -cleanup { catch {namespace delete ::test_ns_1} catch {namespace delete ::test_ns_2} catch {namespace delete ::test_ns_3} catch {namespace delete ::test_ns_4} } test namespace-51.13 {name resolution path control} -body { set ::result {} namespace eval ::test_ns_1 { proc foo {} {lappend ::result 1} } namespace eval ::test_ns_2 { proc foo {} {lappend ::result 2} trace add command foo delete "namespace eval ::test_ns_3 foo;#" } namespace eval ::test_ns_3 { proc foo {} { lappend ::result 3 namespace delete [namespace current] ::test_ns_4::bar } } namespace eval ::test_ns_4 { namespace path {::test_ns_2 ::test_ns_3 ::test_ns_1} proc bar {} { list [foo] [namespace delete ::test_ns_2] [foo] } bar } # Should the result be "2 {} {2 3 2 1}" instead? } -result {2 {} {2 3 1 1}} -cleanup { catch {namespace delete ::test_ns_1} catch {namespace delete ::test_ns_2} catch {namespace delete ::test_ns_3} catch {namespace delete ::test_ns_4} } test namespace-51.14 {name resolution path control} -setup { foreach cmd [info commands foo*] { rename $cmd {} } namespace eval ::test_ns_1 {} namespace eval ::test_ns_2 {} namespace eval ::test_ns_3 {} } -body { proc foo0 {} {} proc ::test_ns_1::foo1 {} {} proc ::test_ns_2::foo2 {} {} namespace eval ::test_ns_3 { variable result {} lappend result [info commands foo*] namespace path {::test_ns_1 ::test_ns_2} lappend result [info commands foo*] proc foo2 {} {} lappend result [info commands foo*] rename foo2 {} lappend result [info commands foo*] namespace delete ::test_ns_1 lappend result [info commands foo*] } } -cleanup { catch {namespace delete ::test_ns_1} catch {namespace delete ::test_ns_2} catch {namespace delete ::test_ns_3} } -result {foo0 {foo1 foo2 foo0} {foo2 foo1 foo0} {foo1 foo2 foo0} {foo2 foo0}} test namespace-51.15 {namespace resolution path control} -body { namespace eval ::test_ns_2 { proc foo {} {return 2} } namespace eval ::test_ns_1 { namespace eval test_ns_2 { proc foo {} {return 1_2} } namespace eval test_ns_3 { namespace path ::test_ns_1 test_ns_2::foo } } } -result 1_2 -cleanup { namespace delete ::test_ns_1 namespace delete ::test_ns_2 } test namespace-51.16 {Bug 1566526} { interp create slave slave eval namespace eval demo namespace path :: interp delete slave } {} test namespace-51.17 {resolution epoch handling: Bug 2898722} -setup { set result {} catch {namespace delete ::a} } -body { namespace eval ::a { proc c {} {lappend ::result A} c namespace eval b { variable d c lappend ::result [catch { $d }] } lappend ::result . namespace eval b { namespace path [namespace parent] $d;[format %c 99] } lappend ::result . namespace eval b { proc c {} {lappend ::result B} $d;[format %c 99] } lappend ::result . } namespace eval ::a::b { $d;[format %c 99] lappend ::result . proc ::c {} {lappend ::result G} $d;[format %c 99] lappend ::result . rename ::a::c {} $d;[format %c 99] lappend ::result . rename ::a::b::c {} $d;[format %c 99] } } -cleanup { namespace delete ::a catch {rename ::c {}} unset result } -result {A 1 . A A . B B . B B . B B . B B . G G} test namespace-51.18 {Bug 3185407} -setup { namespace eval ::test_ns_1 {} } -body { namespace eval ::test_ns_1 { variable result {} namespace eval ns {proc foo {} {}} namespace eval ns2 {proc foo {} {}} namespace path {ns ns2} variable x foo lappend result [namespace which $x] proc foo {} {} lappend result [namespace which $x] } } -cleanup { namespace delete ::test_ns_1 } -result {::test_ns_1::ns::foo ::test_ns_1::foo} # TIP 181 - namespace unknown tests test namespace-52.1 {unknown: default handler ::unknown} { set result [list [namespace eval foobar { namespace unknown }]] lappend result [namespace eval :: { namespace unknown }] namespace delete foobar set result } {{} ::unknown} test namespace-52.2 {unknown: default resolution global} { proc ::foo {} { return "GLOBAL" } namespace eval ::bar { proc foo {} { return "NAMESPACE" } } namespace eval ::bar::jim { proc test {} { foo } } set result [::bar::jim::test] namespace delete ::bar rename ::foo {} set result } {GLOBAL} test namespace-52.3 {unknown: default resolution local} { proc ::foo {} { return "GLOBAL" } namespace eval ::bar { proc foo {} { return "NAMESPACE" } proc test {} { foo } } set result [::bar::test] namespace delete ::bar rename ::foo {} set result } {NAMESPACE} test namespace-52.4 {unknown: set handler} { namespace eval foo { namespace unknown [list dispatch] proc dispatch {args} { return $args } proc test {} { UnknownCmd a b c } } set result [foo::test] namespace delete foo set result } {UnknownCmd a b c} test namespace-52.5 {unknown: search path before unknown is unaltered} { proc ::test2 {args} { return "TEST2: $args" } namespace eval foo { namespace unknown [list dispatch] proc dispatch {args} { return "UNKNOWN: $args" } proc test1 {args} { return "TEST1: $args" } proc test {} { set result [list [test1 a b c]] lappend result [test2 a b c] lappend result [test3 a b c] return $result } } set result [foo::test] namespace delete foo rename ::test2 {} set result } {{TEST1: a b c} {TEST2: a b c} {UNKNOWN: test3 a b c}} test namespace-52.6 {unknown: deleting handler restores default} { rename ::unknown ::_unknown_orig proc ::unknown {args} { return "DEFAULT: $args" } namespace eval foo { namespace unknown dummy namespace unknown {} } set result [namespace eval foo { dummy a b c }] rename ::unknown {} rename ::_unknown_orig ::unknown namespace delete foo set result } {DEFAULT: dummy a b c} test namespace-52.7 {unknown: setting global unknown handler} { proc ::myunknown {args} { return "MYUNKNOWN: $args" } namespace eval :: { namespace unknown ::myunknown } set result [namespace eval foo { dummy a b c }] namespace eval :: { namespace unknown {} } rename ::myunknown {} namespace delete foo set result } {MYUNKNOWN: dummy a b c} test namespace-52.8 {unknown: destroying and redefining global namespace} { set i [interp create] $i hide proc $i hide namespace $i hide return $i invokehidden namespace delete :: $i expose return $i invokehidden proc unknown args { return "FINE" } $i eval { foo bar bob } } {FINE} test namespace-52.9 {unknown: refcounting} -setup { proc this args { unset args ;# stop sharing set copy [namespace unknown] string length $copy ;# shimmer away list rep info level 0 } set handler [namespace unknown] namespace unknown {this is a test} catch {rename noSuchCommand {}} } -body { noSuchCommand } -cleanup { namespace unknown $handler rename this {} } -result {this is a test noSuchCommand} testConstraint testevalobjv [llength [info commands testevalobjv]] test namespace-52.10 {unknown: with TCL_EVAL_GLOBAL} -constraints { testevalobjv } -setup { rename ::unknown unknown.save proc ::unknown args { set caller [uplevel 1 {namespace current}] namespace eval $caller { variable foo return $foo } } catch {rename ::noSuchCommand {}} } -body { namespace eval :: { variable foo SUCCESS } namespace eval test_ns_1 { variable foo FAIL testevalobjv 1 noSuchCommand } } -cleanup { unset -nocomplain ::foo namespace delete test_ns_1 rename ::unknown {} rename unknown.save ::unknown } -result SUCCESS test namespace-52.11 {unknown: with TCL_EVAL_INVOKE} -setup { set handler [namespace eval :: {namespace unknown}] namespace eval :: {namespace unknown unknown} rename ::unknown unknown.save namespace eval :: { proc unknown args { return SUCCESS } } catch {rename ::noSuchCommand {}} set ::slave [interp create] } -body { $::slave alias bar noSuchCommand namespace eval test_ns_1 { namespace unknown unknown proc unknown args { return FAIL } $::slave eval bar } } -cleanup { interp delete $::slave unset ::slave namespace delete test_ns_1 rename ::unknown {} rename unknown.save ::unknown namespace eval :: [list namespace unknown $handler] } -result SUCCESS test namespace-52.12 {unknown: error case must not reset handler} -body { namespace eval foo { namespace unknown ok catch {namespace unknown {{}{}{}}} namespace unknown } } -cleanup { namespace delete foo } -result ok # TIP 314 - ensembles with parameters test namespace-53.1 {ensembles: parameters} { namespace eval ns { namespace export x proc x {para} {list 1 $para} namespace ensemble create -parameters {para1} } list [info command ns] [ns bar x] [namespace delete ns] [info command ns] } {ns {1 bar} {} {}} test namespace-53.2 {ensembles: parameters} -setup { namespace eval ns { namespace export x proc x {para} {list 1 $para} namespace ensemble create } } -body { namespace ensemble configure ns -parameters {para1} rename ns foo list [info command foo] [foo bar x] [namespace delete ns] [info command foo] } -result {foo {1 bar} {} {}} test namespace-53.3 {ensembles: parameters} -setup { namespace eval ns { namespace export x* proc x1 {para} {list 1 $para} proc x2 {para} {list 2 $para} namespace ensemble create -parameters param1 } } -body { set result [list [ns x2 x1] [ns x1 x2]] lappend result [catch {ns x} msg] $msg lappend result [catch {ns x x} msg] $msg rename ns {} lappend result [info command ns::x1] namespace delete ns lappend result [info command ns::x1] } -result\ {{1 x2} {2 x1}\ 1 {wrong # args: should be "ns param1 subcommand ?arg ...?"}\ 1 {unknown or ambiguous subcommand "x": must be x1, or x2}\ ::ns::x1 {}} test namespace-53.4 {ensembles: parameters} -setup { namespace eval ns { namespace export x* proc x1 {a1 a2} {list 1 $a1 $a2} proc x2 {a1 a2} {list 2 $a1 $a2} proc x3 {a1 a2} {list 3 $a1 $a2} namespace ensemble create } } -body { set result {} lappend result [ns x1 x2 x3] namespace ensemble configure ns -parameters p1 lappend result [ns x1 x2 x3] namespace ensemble configure ns -parameters {p1 p2} lappend result [ns x1 x2 x3] } -cleanup { namespace delete ns } -result {{1 x2 x3} {2 x1 x3} {3 x1 x2}} test namespace-53.5 {ensembles: parameters} -setup { namespace eval ns { namespace export x* proc x1 {para} {list 1 $para} proc x2 {para} {list 2 $para} proc x3 {para} {list 3 $para} namespace ensemble create } } -body { set result [list [catch {ns x x1} msg] $msg] lappend result [catch {ns x1 x} msg] $msg namespace ensemble configure ns -parameters p1 lappend result [catch {ns x1 x} msg] $msg lappend result [catch {ns x x1} msg] $msg } -cleanup { namespace delete ns } -result\ {1 {unknown or ambiguous subcommand "x": must be x1, x2, or x3}\ 0 {1 x}\ 1 {unknown or ambiguous subcommand "x": must be x1, x2, or x3}\ 0 {1 x}} test namespace-53.6 {ensembles: nested} -setup { namespace eval ns { namespace export x* namespace eval x0 { proc z {args} {list 0 $args} namespace export z namespace ensemble create } proc x1 {args} {list 1 $args} proc x2 {args} {list 2 $args} proc x3 {args} {list 3 $args} namespace ensemble create -parameters p } } -body { list [ns z x0] [ns z x1] [ns z x2] [ns z x3] } -cleanup { namespace delete ns } -result {{0 {}} {1 z} {2 z} {3 z}} test namespace-53.7 {ensembles: parameters & wrong # args} -setup { namespace eval ns { namespace export x* proc x1 {a1 a2 a3 a4} {list x1 $a1 $a2 $a3 $a4} namespace ensemble create -parameters p1 } } -body { set result {} lappend result [catch {ns} msg] $msg lappend result [catch {ns x1} msg] $msg lappend result [catch {ns x1 x1} msg] $msg lappend result [catch {ns x1 x1 x1} msg] $msg lappend result [catch {ns x1 x1 x1 x1} msg] $msg lappend result [catch {ns x1 x1 x1 x1 x1} msg] $msg } -cleanup { namespace delete ns } -result\ {1 {wrong # args: should be "ns p1 subcommand ?arg ...?"}\ 1 {wrong # args: should be "ns p1 subcommand ?arg ...?"}\ 1 {wrong # args: should be "ns x1 x1 a2 a3 a4"}\ 1 {wrong # args: should be "ns x1 x1 a2 a3 a4"}\ 1 {wrong # args: should be "ns x1 x1 a2 a3 a4"}\ 0 {x1 x1 x1 x1 x1}} test namespace-53.8 {ensemble: unknown handler changing -parameters} -setup { namespace eval ns { namespace export x* proc x1 {a1} {list 1 $a1} proc Magic {ensemble subcmd args} { namespace ensemble configure $ensemble\ -parameters [lrange p1 [llength [ namespace ensemble configure $ensemble -parameters ]] 0] list } namespace ensemble create -unknown ::ns::Magic } } -body { set result {} lappend result [catch {ns x1 x2} msg] $msg [namespace ensemble configure ns -parameters] lappend result [catch {ns x2 x1} msg] $msg [namespace ensemble configure ns -parameters] lappend result [catch {ns x2 x3} msg] $msg [namespace ensemble configure ns -parameters] } -cleanup { namespace delete ns } -result\ {0 {1 x2} {}\ 0 {1 x2} p1\ 1 {unknown or ambiguous subcommand "x2": must be x1} {}} test namespace-53.9 {ensemble: unknown handler changing -parameters,\ thereby eating all args} -setup { namespace eval ns { namespace export x* proc x1 {args} {list 1 $args} proc Magic {ensemble subcmd args} { namespace ensemble configure $ensemble\ -parameters {p1 p2 p3 p4 p5} list } namespace ensemble create -unknown ::ns::Magic } } -body { set result {} lappend result [catch {ns x1 x2} msg] $msg [namespace ensemble configure ns -parameters] lappend result [catch {ns x2 x1} msg] $msg [namespace ensemble configure ns -parameters] lappend result [catch {ns a1 a2 a3 a4 a5 x1} msg] $msg [namespace ensemble configure ns -parameters] } -cleanup { namespace delete ns } -result\ {0 {1 x2} {}\ 1 {wrong # args: should be "ns p1 p2 p3 p4 p5 subcommand ?arg ...?"} {p1 p2 p3 p4 p5}\ 0 {1 {a1 a2 a3 a4 a5}} {p1 p2 p3 p4 p5}} test namespace-53.10 {ensembles: nested rewrite} -setup { namespace eval ns { namespace export x namespace eval x { proc z0 {} {list 0} proc z1 {a1} {list 1 $a1} proc z2 {a1 a2} {list 2 $a1 $a2} proc z3 {a1 a2 a3} {list 3 $a1 $a2 $a3} namespace export z* namespace ensemble create } namespace ensemble create -parameters p } } -body { set result {} # In these cases, parsing the subensemble does not grab a new word. lappend result [catch {ns z0 x} msg] $msg lappend result [catch {ns z1 x} msg] $msg lappend result [catch {ns z2 x} msg] $msg lappend result [catch {ns z2 x v} msg] $msg namespace ensemble configure ns::x -parameters q1 # In these cases, parsing the subensemble grabs a new word. lappend result [catch {ns v x z0} msg] $msg lappend result [catch {ns v x z1} msg] $msg lappend result [catch {ns v x z2} msg] $msg lappend result [catch {ns v x z2 v2} msg] $msg } -cleanup { namespace delete ns } -result\ {0 0\ 1 {wrong # args: should be "ns z1 x a1"}\ 1 {wrong # args: should be "ns z2 x a1 a2"}\ 1 {wrong # args: should be "ns z2 x a1 a2"}\ 1 {wrong # args: should be "::ns::x::z0"}\ 0 {1 v}\ 1 {wrong # args: should be "ns v x z2 a2"}\ 0 {2 v v2}} test namespace-54.1 {leak on namespace deletion} -constraints {memory} \ -setup { proc getbytes {} { set lines [split [memory info] "\n"] lindex $lines 3 3 } } -body { set end [getbytes] for {set i 0} {$i < 5} {incr i} { set ns ::y$i namespace eval $ns {} namespace delete $ns set start $end set end [getbytes] } set leakedBytes [expr {$end - $start}] } -cleanup { rename getbytes {} unset i ns start end } -result 0 # cleanup catch {rename cmd1 {}} catch {unset l} catch {unset msg} catch {unset trigger} namespace delete {*}[namespace children :: test_ns_*] ::tcltest::cleanupTests return # Local Variables: # mode: tcl # End: a> 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
/*
* re_*comp and friends - compile REs
* This file #includes several others (see the bottom).
*
* Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
*
* Development of this software was funded, in part, by Cray Research Inc.,
* UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
* Corporation, none of whom are responsible for the results. The author
* thanks all of them.
*
* Redistribution and use in source and binary forms -- with or without
* modification -- are permitted for any purpose, provided that
* redistributions in source form retain this entire copyright notice and
* indicate the origin and nature of any modifications.
*
* I'd appreciate being given credit for this package in the documentation of
* software which uses it, but that is not a requirement.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "regguts.h"
/*
* forward declarations, up here so forward datatypes etc. are defined early
*/
/* =====^!^===== begin forwards =====^!^===== */
/* automatically gathered by fwd; do not hand-edit */
/* === regcomp.c === */
int compile(regex_t *, const chr *, size_t, int);
static void moresubs(struct vars *, int);
static int freev(struct vars *, int);
static void makesearch(struct vars *, struct nfa *);
static struct subre *parse(struct vars *, int, int, struct state *, struct state *);
static struct subre *parsebranch(struct vars *, int, int, struct state *, struct state *, int);
static void parseqatom(struct vars *, int, int, struct state *, struct state *, struct subre *);
static void nonword(struct vars *, int, struct state *, struct state *);
static void word(struct vars *, int, struct state *, struct state *);
static int scannum(struct vars *);
static void repeat(struct vars *, struct state *, struct state *, int, int);
static void bracket(struct vars *, struct state *, struct state *);
static void cbracket(struct vars *, struct state *, struct state *);
static void brackpart(struct vars *, struct state *, struct state *);
static const chr *scanplain(struct vars *);
static void onechr(struct vars *, pchr, struct state *, struct state *);
static void dovec(struct vars *, struct cvec *, struct state *, struct state *);
static void wordchrs(struct vars *);
static struct subre *subre(struct vars *, int, int, struct state *, struct state *);
static void freesubre(struct vars *, struct subre *);
static void freesrnode(struct vars *, struct subre *);
static void optst(struct vars *, struct subre *);
static int numst(struct subre *, int);
static void markst(struct subre *);
static void cleanst(struct vars *);
static long nfatree(struct vars *, struct subre *, FILE *);
static long nfanode(struct vars *, struct subre *, FILE *);
static int newlacon(struct vars *, struct state *, struct state *, int);
static void freelacons(struct subre *, int);
static void rfree(regex_t *);
static void dump(regex_t *, FILE *);
static void dumpst(struct subre *, FILE *, int);
static void stdump(struct subre *, FILE *, int);
static const char *stid(struct subre *, char *, size_t);
/* === regc_lex.c === */
static void lexstart(struct vars *);
static void prefixes(struct vars *);
static void lexnest(struct vars *, const chr *, const chr *);
static void lexword(struct vars *);
static int next(struct vars *);
static int lexescape(struct vars *);
static chr lexdigits(struct vars *, int, int, int);
static int brenext(struct vars *, pchr);
static void skip(struct vars *);
static chr newline(NOPARMS);
#ifdef REG_DEBUG
static const chr *ch(NOPARMS);
#endif
static chr chrnamed(struct vars *, const chr *, const chr *, pchr);
/* === regc_color.c === */
static void initcm(struct vars *, struct colormap *);
static void freecm(struct colormap *);
static void cmtreefree(struct colormap *, union tree *, int);
static color setcolor(struct colormap *, pchr, pcolor);
static color maxcolor(struct colormap *);
static color newcolor(struct colormap *);
static void freecolor(struct colormap *, pcolor);
static color pseudocolor(struct colormap *);
static color subcolor(struct colormap *, pchr c);
static color newsub(struct colormap *, pcolor);
static void subrange(struct vars *, pchr, pchr, struct state *, struct state *);
static void subblock(struct vars *, pchr, struct state *, struct state *);
static void okcolors(struct nfa *, struct colormap *);
static void colorchain(struct colormap *, struct arc *);
static void uncolorchain(struct colormap *, struct arc *);
static void rainbow(struct nfa *, struct colormap *, int, pcolor, struct state *, struct state *);
static void colorcomplement(struct nfa *, struct colormap *, int, struct state *, struct state *, struct state *);
#ifdef REG_DEBUG
static void dumpcolors(struct colormap *, FILE *);
static void fillcheck(struct colormap *, union tree *, int, FILE *);
static void dumpchr(pchr, FILE *);
#endif
/* === regc_nfa.c === */
static struct nfa *newnfa(struct vars *, struct colormap *, struct nfa *);
static void freenfa(struct nfa *);
static struct state *newstate(struct nfa *);
static struct state *newfstate(struct nfa *, int flag);
static void dropstate(struct nfa *, struct state *);
static void freestate(struct nfa *, struct state *);
static void destroystate(struct nfa *, struct state *);
static void newarc(struct nfa *, int, pcolor, struct state *, struct state *);
static struct arc *allocarc(struct nfa *, struct state *);
static void freearc(struct nfa *, struct arc *);
static struct arc *findarc(struct state *, int, pcolor);
static void cparc(struct nfa *, struct arc *, struct state *, struct state *);
static void moveins(struct nfa *, struct state *, struct state *);
static void copyins(struct nfa *, struct state *, struct state *);
static void moveouts(struct nfa *, struct state *, struct state *);
static void copyouts(struct nfa *, struct state *, struct state *);
static void cloneouts(struct nfa *, struct state *, struct state *, struct state *, int);
static void delsub(struct nfa *, struct state *, struct state *);
static void deltraverse(struct nfa *, struct state *, struct state *);
static void dupnfa(struct nfa *, struct state *, struct state *, struct state *, struct state *);
static void duptraverse(struct nfa *, struct state *, struct state *, int);
static void cleartraverse(struct nfa *, struct state *);
static void specialcolors(struct nfa *);
static long optimize(struct nfa *, FILE *);
static void pullback(struct nfa *, FILE *);
static int pull(struct nfa *, struct arc *);
static void pushfwd(struct nfa *, FILE *);
static int push(struct nfa *, struct arc *);
#define INCOMPATIBLE 1 /* destroys arc */
#define SATISFIED 2 /* constraint satisfied */
#define COMPATIBLE 3 /* compatible but not satisfied yet */
static int combine(struct arc *, struct arc *);
static void fixempties(struct nfa *, FILE *);
static int unempty(struct nfa *, struct arc *);
static void cleanup(struct nfa *);
static void markreachable(struct nfa *, struct state *, struct state *, struct state *);
static void markcanreach(struct nfa *, struct state *, struct state *, struct state *);
static long analyze(struct nfa *);
static void compact(struct nfa *, struct cnfa *);
static void carcsort(struct carc *, struct carc *);
static void freecnfa(struct cnfa *);
static void dumpnfa(struct nfa *, FILE *);
#ifdef REG_DEBUG
static void dumpstate(struct state *, FILE *);
static void dumparcs(struct state *, FILE *);
static int dumprarcs(struct arc *, struct state *, FILE *, int);
static void dumparc(struct arc *, struct state *, FILE *);
#endif
static void dumpcnfa(struct cnfa *, FILE *);
#ifdef REG_DEBUG
static void dumpcstate(int, struct carc *, struct cnfa *, FILE *);
#endif
/* === regc_cvec.c === */
static struct cvec *clearcvec(struct cvec *);
static void addchr(struct cvec *, pchr);
static void addrange(struct cvec *, pchr, pchr);
static struct cvec *newcvec(int, int);
static struct cvec *getcvec(struct vars *, int, int);
static void freecvec(struct cvec *);
/* === regc_locale.c === */
static celt element(struct vars *, const chr *, const chr *);
static struct cvec *range(struct vars *, celt, celt, int);
static int before(celt, celt);
static struct cvec *eclass(struct vars *, celt, int);
static struct cvec *cclass(struct vars *, const chr *, const chr *, int);
static struct cvec *allcases(struct vars *, pchr);
static int cmp(const chr *, const chr *, size_t);
static int casecmp(const chr *, const chr *, size_t);
/* automatically gathered by fwd; do not hand-edit */
/* =====^!^===== end forwards =====^!^===== */
/* internal variables, bundled for easy passing around */
struct vars {
regex_t *re;
const chr *now; /* scan pointer into string */
const chr *stop; /* end of string */
const chr *savenow; /* saved now and stop for "subroutine call" */
const chr *savestop;
int err; /* error code (0 if none) */
int cflags; /* copy of compile flags */
int lasttype; /* type of previous token */
int nexttype; /* type of next token */
chr nextvalue; /* value (if any) of next token */
int lexcon; /* lexical context type (see lex.c) */
int nsubexp; /* subexpression count */
struct subre **subs; /* subRE pointer vector */
size_t nsubs; /* length of vector */
struct subre *sub10[10]; /* initial vector, enough for most */
struct nfa *nfa; /* the NFA */
struct colormap *cm; /* character color map */
color nlcolor; /* color of newline */
struct state *wordchrs; /* state in nfa holding word-char outarcs */
struct subre *tree; /* subexpression tree */
struct subre *treechain; /* all tree nodes allocated */
struct subre *treefree; /* any free tree nodes */
int ntree; /* number of tree nodes */
struct cvec *cv; /* interface cvec */
struct cvec *cv2; /* utility cvec */
struct subre *lacons; /* lookahead-constraint vector */
int nlacons; /* size of lacons */
};
/* parsing macros; most know that `v' is the struct vars pointer */
#define NEXT() (next(v)) /* advance by one token */
#define SEE(t) (v->nexttype == (t)) /* is next token this? */
#define EAT(t) (SEE(t) && next(v)) /* if next is this, swallow it */
#define VISERR(vv) ((vv)->err != 0)/* have we seen an error yet? */
#define ISERR() VISERR(v)
#define VERR(vv,e) \
((vv)->nexttype = EOS, ((vv)->err) ? (vv)->err : ((vv)->err = (e)))
#define ERR(e) VERR(v, e) /* record an error */
#define NOERR() {if (ISERR()) return;} /* if error seen, return */
#define NOERRN() {if (ISERR()) return NULL;} /* NOERR with retval */
#define NOERRZ() {if (ISERR()) return 0;} /* NOERR with retval */
#define INSIST(c, e) ((c) ? 0 : ERR(e)) /* if condition false, error */
#define NOTE(b) (v->re->re_info |= (b)) /* note visible condition */
#define EMPTYARC(x, y) newarc(v->nfa, EMPTY, 0, x, y)
/* token type codes, some also used as NFA arc types */
#define EMPTY 'n' /* no token present */
#define EOS 'e' /* end of string */
#define PLAIN 'p' /* ordinary character */
#define DIGIT 'd' /* digit (in bound) */
#define BACKREF 'b' /* back reference */
#define COLLEL 'I' /* start of [. */
#define ECLASS 'E' /* start of [= */
#define CCLASS 'C' /* start of [: */
#define END 'X' /* end of [. [= [: */
#define RANGE 'R' /* - within [] which might be range delim. */
#define LACON 'L' /* lookahead constraint subRE */
#define AHEAD 'a' /* color-lookahead arc */
#define BEHIND 'r' /* color-lookbehind arc */
#define WBDRY 'w' /* word boundary constraint */
#define NWBDRY 'W' /* non-word-boundary constraint */
#define SBEGIN 'A' /* beginning of string (even if not BOL) */
#define SEND 'Z' /* end of string (even if not EOL) */
#define PREFER 'P' /* length preference */
/* is an arc colored, and hence on a color chain? */
#define COLORED(a) \
((a)->type == PLAIN || (a)->type == AHEAD || (a)->type == BEHIND)
/* static function list */
static struct fns functions = {
rfree, /* regfree insides */
};
/*
- compile - compile regular expression
^ int compile(regex_t *, const chr *, size_t, int);
*/
int
compile(
regex_t *re,
const chr *string,
size_t len,
int flags)
{
AllocVars(v);
struct guts *g;
int i;
size_t j;
FILE *debug = (flags®_PROGRESS) ? stdout : NULL;
#define CNOERR() { if (ISERR()) return freev(v, v->err); }
/*
* Sanity checks.
*/
if (re == NULL || string == NULL) {
FreeVars(v);
return REG_INVARG;
}
if ((flags®_QUOTE) && (flags&(REG_ADVANCED|REG_EXPANDED|REG_NEWLINE))) {
FreeVars(v);
return REG_INVARG;
}
if (!(flags®_EXTENDED) && (flags®_ADVF)) {
FreeVars(v);
return REG_INVARG;
}
/*
* Initial setup (after which freev() is callable).
*/
v->re = re;
v->now = string;
v->stop = v->now + len;
v->savenow = v->savestop = NULL;
v->err = 0;
v->cflags = flags;
v->nsubexp = 0;
v->subs = v->sub10;
v->nsubs = 10;
for (j = 0; j < v->nsubs; j++) {
v->subs[j] = NULL;
}
v->nfa = NULL;
v->cm = NULL;
v->nlcolor = COLORLESS;
v->wordchrs = NULL;
v->tree = NULL;
v->treechain = NULL;
v->treefree = NULL;
v->cv = NULL;
v->cv2 = NULL;
v->lacons = NULL;
v->nlacons = 0;
re->re_magic = REMAGIC;
re->re_info = 0; /* bits get set during parse */
re->re_csize = sizeof(chr);
re->re_guts = NULL;
re->re_fns = VS(&functions);
/*
* More complex setup, malloced things.
*/
re->re_guts = VS(MALLOC(sizeof(struct guts)));
if (re->re_guts == NULL) {
return freev(v, REG_ESPACE);
}
g = (struct guts *) re->re_guts;
g->tree = NULL;
initcm(v, &g->cmap);
v->cm = &g->cmap;
g->lacons = NULL;
g->nlacons = 0;
ZAPCNFA(g->search);
v->nfa = newnfa(v, v->cm, NULL);
CNOERR();
v->cv = newcvec(100, 20);
if (v->cv == NULL) {
return freev(v, REG_ESPACE);
}
/*
* Parsing.
*/
lexstart(v); /* also handles prefixes */
if ((v->cflags®_NLSTOP) || (v->cflags®_NLANCH)) {
/*
* Assign newline a unique color.
*/
v->nlcolor = subcolor(v->cm, newline());
okcolors(v->nfa, v->cm);
}
CNOERR();
v->tree = parse(v, EOS, PLAIN, v->nfa->init, v->nfa->final);
assert(SEE(EOS)); /* even if error; ISERR() => SEE(EOS) */
CNOERR();
assert(v->tree != NULL);
/*
* Finish setup of nfa and its subre tree.
*/
specialcolors(v->nfa);
CNOERR();
if (debug != NULL) {
fprintf(debug, "\n\n\n========= RAW ==========\n");
dumpnfa(v->nfa, debug);
dumpst(v->tree, debug, 1);
}
optst(v, v->tree);
v->ntree = numst(v->tree, 1);
markst(v->tree);
cleanst(v);
if (debug != NULL) {
fprintf(debug, "\n\n\n========= TREE FIXED ==========\n");
dumpst(v->tree, debug, 1);
}
/*
* Build compacted NFAs for tree and lacons.
*/
re->re_info |= nfatree(v, v->tree, debug);
CNOERR();
assert(v->nlacons == 0 || v->lacons != NULL);
for (i = 1; i < v->nlacons; i++) {
if (debug != NULL) {
fprintf(debug, "\n\n\n========= LA%d ==========\n", i);
}
nfanode(v, &v->lacons[i], debug);
}
CNOERR();
if (v->tree->flags&SHORTER) {
NOTE(REG_USHORTEST);
}
/*
* Build compacted NFAs for tree, lacons, fast search.
*/
if (debug != NULL) {
fprintf(debug, "\n\n\n========= SEARCH ==========\n");
}
/*
* Can sacrifice main NFA now, so use it as work area.
*/
(DISCARD) optimize(v->nfa, debug);
CNOERR();
makesearch(v, v->nfa);
CNOERR();
compact(v->nfa, &g->search);
CNOERR();
/*
* Looks okay, package it up.
*/
re->re_nsub = v->nsubexp;
v->re = NULL; /* freev no longer frees re */
g->magic = GUTSMAGIC;
g->cflags = v->cflags;
g->info = re->re_info;
g->nsub = re->re_nsub;
g->tree = v->tree;
v->tree = NULL;
g->ntree = v->ntree;
g->compare = (v->cflags®_ICASE) ? casecmp : cmp;
g->lacons = v->lacons;
v->lacons = NULL;
g->nlacons = v->nlacons;
if (flags®_DUMP) {
dump(re, stdout);
}
assert(v->err == 0);
return freev(v, 0);
}
/*
- moresubs - enlarge subRE vector
^ static void moresubs(struct vars *, int);
*/
static void
moresubs(
struct vars *v,
int wanted) /* want enough room for this one */
{
struct subre **p;
size_t n;
assert(wanted > 0 && (size_t)wanted >= v->nsubs);
n = (size_t)wanted * 3 / 2 + 1;
if (v->subs == v->sub10) {
p = (struct subre **) MALLOC(n * sizeof(struct subre *));
if (p != NULL) {
memcpy(p, v->subs, v->nsubs * sizeof(struct subre *));
}
} else {
p = (struct subre **) REALLOC(v->subs, n*sizeof(struct subre *));
}
if (p == NULL) {
ERR(REG_ESPACE);
return;
}
v->subs = p;
for (p = &v->subs[v->nsubs]; v->nsubs < n; p++, v->nsubs++) {
*p = NULL;
}
assert(v->nsubs == n);
assert((size_t)wanted < v->nsubs);
}
/*
- freev - free vars struct's substructures where necessary
* Optionally does error-number setting, and always returns error code (if
* any), to make error-handling code terser.
^ static int freev(struct vars *, int);
*/
static int
freev(
struct vars *v,
int err)
{
register int ret;
if (v->re != NULL) {
rfree(v->re);
}
if (v->subs != v->sub10) {
FREE(v->subs);
}
if (v->nfa != NULL) {
freenfa(v->nfa);
}
if (v->tree != NULL) {
freesubre(v, v->tree);
}
if (v->treechain != NULL) {
cleanst(v);
}
if (v->cv != NULL) {
freecvec(v->cv);
}
if (v->cv2 != NULL) {
freecvec(v->cv2);
}
if (v->lacons != NULL) {
freelacons(v->lacons, v->nlacons);
}
ERR(err); /* nop if err==0 */
ret = v->err;
FreeVars(v);
return ret;
}
/*
- makesearch - turn an NFA into a search NFA (implicit prepend of .*?)
* NFA must have been optimize()d already.
^ static void makesearch(struct vars *, struct nfa *);
*/
static void
makesearch(
struct vars *v,
struct nfa *nfa)
{
struct arc *a, *b;
struct state *pre = nfa->pre;
struct state *s, *s2, *slist;
/*
* No loops are needed if it's anchored.
*/
for (a = pre->outs; a != NULL; a = a->outchain) {
assert(a->type == PLAIN);
if (a->co != nfa->bos[0] && a->co != nfa->bos[1]) {
break;
}
}
if (a != NULL) {
/*
* Add implicit .* in front.
*/
rainbow(nfa, v->cm, PLAIN, COLORLESS, pre, pre);
/*
* And ^* and \A* too -- not always necessary, but harmless.
*/
newarc(nfa, PLAIN, nfa->bos[0], pre, pre);
newarc(nfa, PLAIN, nfa->bos[1], pre, pre);
}
/*
* Now here's the subtle part. Because many REs have no lookback
* constraints, often knowing when you were in the pre state tells you
* little; it's the next state(s) that are informative. But some of them
* may have other inarcs, i.e. it may be possible to make actual progress
* and then return to one of them. We must de-optimize such cases,
* splitting each such state into progress and no-progress states.
*/
/*
* First, make a list of the states.
*/
slist = NULL;
for (a=pre->outs ; a!=NULL ; a=a->outchain) {
s = a->to;
for (b=s->ins ; b!=NULL ; b=b->inchain) {
if (b->from != pre) {
break;
}
}
if (b != NULL && s->tmp == NULL) {
/*
* Must be split if not already in the list (fixes bugs 505048,
* 230589, 840258, 504785).
*/
s->tmp = slist;
slist = s;
}
}
/*
* Do the splits.
*/
for (s=slist ; s!=NULL ; s=s2) {
s2 = newstate(nfa);
copyouts(nfa, s, s2);
for (a=s->ins ; a!=NULL ; a=b) {
b = a->inchain;
if (a->from != pre) {
cparc(nfa, a, a->from, s2);
freearc(nfa, a);
}
}
s2 = s->tmp;
s->tmp = NULL; /* clean up while we're at it */
}
}
/*
- parse - parse an RE
* This is actually just the top level, which parses a bunch of branches tied
* together with '|'. They appear in the tree as the left children of a chain
* of '|' subres.
^ static struct subre *parse(struct vars *, int, int, struct state *,
^ struct state *);
*/
static struct subre *
parse(
struct vars *v,
int stopper, /* EOS or ')' */
int type, /* LACON (lookahead subRE) or PLAIN */
struct state *init, /* initial state */
struct state *final) /* final state */
{
struct state *left, *right; /* scaffolding for branch */
struct subre *branches; /* top level */
struct subre *branch; /* current branch */
struct subre *t; /* temporary */
int firstbranch; /* is this the first branch? */
assert(stopper == ')' || stopper == EOS);
branches = subre(v, '|', LONGER, init, final);
NOERRN();
branch = branches;
firstbranch = 1;
do { /* a branch */
if (!firstbranch) {
/*
* Need a place to hang the branch.
*/
branch->right = subre(v, '|', LONGER, init, final);
NOERRN();
branch = branch->right;
}
firstbranch = 0;
left = newstate(v->nfa);
right = newstate(v->nfa);
NOERRN();
EMPTYARC(init, left);
EMPTYARC(right, final);
NOERRN();
branch->left = parsebranch(v, stopper, type, left, right, 0);
NOERRN();
branch->flags |= UP(branch->flags | branch->left->flags);
if ((branch->flags &~ branches->flags) != 0) { /* new flags */
for (t = branches; t != branch; t = t->right) {
t->flags |= branch->flags;
}
}
} while (EAT('|'));
assert(SEE(stopper) || SEE(EOS));
if (!SEE(stopper)) {
assert(stopper == ')' && SEE(EOS));
ERR(REG_EPAREN);
}
/*
* Optimize out simple cases.
*/
if (branch == branches) { /* only one branch */
assert(branch->right == NULL);
t = branch->left;
branch->left = NULL;
freesubre(v, branches);
branches = t;
} else if (!MESSY(branches->flags)) { /* no interesting innards */
freesubre(v, branches->left);
branches->left = NULL;
freesubre(v, branches->right);
branches->right = NULL;
branches->op = '=';
}
return branches;
}
/*
- parsebranch - parse one branch of an RE
* This mostly manages concatenation, working closely with parseqatom().
* Concatenated things are bundled up as much as possible, with separate
* ',' nodes introduced only when necessary due to substructure.
^ static struct subre *parsebranch(struct vars *, int, int, struct state *,
^ struct state *, int);
*/
static struct subre *
parsebranch(
struct vars *v,
int stopper, /* EOS or ')' */
int type, /* LACON (lookahead subRE) or PLAIN */
struct state *left, /* leftmost state */
struct state *right, /* rightmost state */
int partial) /* is this only part of a branch? */
{
struct state *lp; /* left end of current construct */
int seencontent; /* is there anything in this branch yet? */
struct subre *t;
lp = left;
seencontent = 0;
t = subre(v, '=', 0, left, right); /* op '=' is tentative */
NOERRN();
while (!SEE('|') && !SEE(stopper) && !SEE(EOS)) {
if (seencontent) { /* implicit concat operator */
lp = newstate(v->nfa);
NOERRN();
moveins(v->nfa, right, lp);
}
seencontent = 1;
/* NB, recursion in parseqatom() may swallow rest of branch */
parseqatom(v, stopper, type, lp, right, t);
}
if (!seencontent) { /* empty branch */
if (!partial) {
NOTE(REG_UUNSPEC);
}
assert(lp == left);
EMPTYARC(left, right);
}
return t;
}
/*
- parseqatom - parse one quantified atom or constraint of an RE
* The bookkeeping near the end cooperates very closely with parsebranch(); in
* particular, it contains a recursion that can involve parsing the rest of
* the branch, making this function's name somewhat inaccurate.
^ static void parseqatom(struct vars *, int, int, struct state *,
^ struct state *, struct subre *);
*/
static void
parseqatom(
struct vars *v,
int stopper, /* EOS or ')' */
int type, /* LACON (lookahead subRE) or PLAIN */
struct state *lp, /* left state to hang it on */
struct state *rp, /* right state to hang it on */
struct subre *top) /* subtree top */
{
struct state *s; /* temporaries for new states */
struct state *s2;
#define ARCV(t, val) newarc(v->nfa, t, val, lp, rp)
int m, n;
struct subre *atom; /* atom's subtree */
struct subre *t;
int cap; /* capturing parens? */
int pos; /* positive lookahead? */
int subno; /* capturing-parens or backref number */
int atomtype;
int qprefer; /* quantifier short/long preference */
int f;
struct subre **atomp; /* where the pointer to atom is */
/*
* Initial bookkeeping.
*/
atom = NULL;
assert(lp->nouts == 0); /* must string new code */
assert(rp->nins == 0); /* between lp and rp */
subno = 0; /* just to shut lint up */
/*
* An atom or constraint...
*/
atomtype = v->nexttype;
switch (atomtype) {
/* first, constraints, which end by returning */
case '^':
ARCV('^', 1);
if (v->cflags®_NLANCH) {
ARCV(BEHIND, v->nlcolor);
}
NEXT();
return;
case '$':
ARCV('$', 1);
if (v->cflags®_NLANCH) {
ARCV(AHEAD, v->nlcolor);
}
NEXT();
return;
case SBEGIN:
ARCV('^', 1); /* BOL */
ARCV('^', 0); /* or BOS */
NEXT();
return;
case SEND:
ARCV('$', 1); /* EOL */
ARCV('$', 0); /* or EOS */
NEXT();
return;
case '<':
wordchrs(v); /* does NEXT() */
s = newstate(v->nfa);
NOERR();
nonword(v, BEHIND, lp, s);
word(v, AHEAD, s, rp);
return;
case '>':
wordchrs(v); /* does NEXT() */
s = newstate(v->nfa);
NOERR();
word(v, BEHIND, lp, s);
nonword(v, AHEAD, s, rp);
return;
case WBDRY:
wordchrs(v); /* does NEXT() */
s = newstate(v->nfa);
NOERR();
nonword(v, BEHIND, lp, s);
word(v, AHEAD, s, rp);
s = newstate(v->nfa);
NOERR();
word(v, BEHIND, lp, s);
nonword(v, AHEAD, s, rp);
return;
case NWBDRY:
wordchrs(v); /* does NEXT() */
s = newstate(v->nfa);
NOERR();
word(v, BEHIND, lp, s);
word(v, AHEAD, s, rp);
s = newstate(v->nfa);
NOERR();
nonword(v, BEHIND, lp, s);
nonword(v, AHEAD, s, rp);
return;
case LACON: /* lookahead constraint */
pos = v->nextvalue;
NEXT();
s = newstate(v->nfa);
s2 = newstate(v->nfa);
NOERR();
t = parse(v, ')', LACON, s, s2);
freesubre(v, t); /* internal structure irrelevant */
assert(SEE(')') || ISERR());
NEXT();
n = newlacon(v, s, s2, pos);
NOERR();
ARCV(LACON, n);
return;
/*
* Then errors, to get them out of the way.
*/
case '*':
case '+':
case '?':
case '{':
ERR(REG_BADRPT);
return;
default:
ERR(REG_ASSERT);
return;
/*
* Then plain characters, and minor variants on that theme.
*/
case ')': /* unbalanced paren */
if ((v->cflags®_ADVANCED) != REG_EXTENDED) {
ERR(REG_EPAREN);
return;
}
/*
* Legal in EREs due to specification botch.
*/
NOTE(REG_UPBOTCH);
/* fallthrough into case PLAIN */
case PLAIN:
onechr(v, v->nextvalue, lp, rp);
okcolors(v->nfa, v->cm);
NOERR();
NEXT();
break;
case '[':
if (v->nextvalue == 1) {
bracket(v, lp, rp);
} else {
cbracket(v, lp, rp);
}
assert(SEE(']') || ISERR());
NEXT();
break;
case '.':
rainbow(v->nfa, v->cm, PLAIN,
(v->cflags®_NLSTOP) ? v->nlcolor : COLORLESS, lp, rp);
NEXT();
break;
/*
* And finally the ugly stuff.
*/
case '(': /* value flags as capturing or non */
cap = (type == LACON) ? 0 : v->nextvalue;
if (cap) {
v->nsubexp++;
subno = v->nsubexp;
if ((size_t)subno >= v->nsubs) {
moresubs(v, subno);
}
assert((size_t)subno < v->nsubs);
} else {
atomtype = PLAIN; /* something that's not '(' */
}
NEXT();
/*
* Need new endpoints because tree will contain pointers.
*/
s = newstate(v->nfa);
s2 = newstate(v->nfa);
NOERR();
EMPTYARC(lp, s);
EMPTYARC(s2, rp);
NOERR();
atom = parse(v, ')', PLAIN, s, s2);
assert(SEE(')') || ISERR());
NEXT();
NOERR();
if (cap) {
v->subs[subno] = atom;
t = subre(v, '(', atom->flags|CAP, lp, rp);
NOERR();
t->subno = subno;
t->left = atom;
atom = t;
}
/*
* Postpone everything else pending possible {0}.
*/
break;
case BACKREF: /* the Feature From The Black Lagoon */
INSIST(type != LACON, REG_ESUBREG);
INSIST(v->nextvalue < v->nsubs, REG_ESUBREG);
INSIST(v->subs[v->nextvalue] != NULL, REG_ESUBREG);
NOERR();
assert(v->nextvalue > 0);
atom = subre(v, 'b', BACKR, lp, rp);
subno = v->nextvalue;
atom->subno = subno;
EMPTYARC(lp, rp); /* temporarily, so there's something */
NEXT();
break;
}
/*
* ...and an atom may be followed by a quantifier.
*/
switch (v->nexttype) {
case '*':
m = 0;
n = INFINITY;
qprefer = (v->nextvalue) ? LONGER : SHORTER;
NEXT();
break;
case '+':
m = 1;
n = INFINITY;
qprefer = (v->nextvalue) ? LONGER : SHORTER;
NEXT();
break;
case '?':
m = 0;
n = 1;
qprefer = (v->nextvalue) ? LONGER : SHORTER;
NEXT();
break;
case '{':
NEXT();
m = scannum(v);
if (EAT(',')) {
if (SEE(DIGIT)) {
n = scannum(v);
} else {
n = INFINITY;
}
if (m > n) {
ERR(REG_BADBR);
return;
}
/*
* {m,n} exercises preference, even if it's {m,m}
*/
qprefer = (v->nextvalue) ? LONGER : SHORTER;
} else {
n = m;
/*
* {m} passes operand's preference through.
*/
qprefer = 0;
}
if (!SEE('}')) { /* catches errors too */
ERR(REG_BADBR);
return;
}
NEXT();
break;
default: /* no quantifier */
m = n = 1;
qprefer = 0;
break;
}
/*
* Annoying special case: {0} or {0,0} cancels everything.
*/
if (m == 0 && n == 0) {
if (atom != NULL) {
freesubre(v, atom);
}
if (atomtype == '(') {
v->subs[subno] = NULL;
}
delsub(v->nfa, lp, rp);
EMPTYARC(lp, rp);
return;
}
/*
* If not a messy case, avoid hard part.
*/
assert(!MESSY(top->flags));
f = top->flags | qprefer | ((atom != NULL) ? atom->flags : 0);
if (atomtype != '(' && atomtype != BACKREF && !MESSY(UP(f))) {
if (!(m == 1 && n == 1)) {
repeat(v, lp, rp, m, n);
}
if (atom != NULL) {
freesubre(v, atom);
}
top->flags = f;
return;
}
/*
* hard part: something messy
* That is, capturing parens, back reference, short/long clash, or an atom
* with substructure containing one of those.
*/
/*
* Now we'll need a subre for the contents even if they're boring.
*/
if (atom == NULL) {
atom = subre(v, '=', 0, lp, rp);
NOERR();
}
/*
* Prepare a general-purpose state skeleton.
*
* ---> [s] ---prefix---> [begin] ---atom---> [end] ----rest---> [rp]
* / /
* [lp] ----> [s2] ----bypass---------------------
*
* where bypass is an empty, and prefix is some repetitions of atom
*/
s = newstate(v->nfa); /* first, new endpoints for the atom */
s2 = newstate(v->nfa);
NOERR();
moveouts(v->nfa, lp, s);
moveins(v->nfa, rp, s2);
NOERR();
atom->begin = s;
atom->end = s2;
s = newstate(v->nfa); /* and spots for prefix and bypass */
s2 = newstate(v->nfa);
NOERR();
EMPTYARC(lp, s);
EMPTYARC(lp, s2);
NOERR();
/*
* Break remaining subRE into x{...} and what follows.
*/
t = subre(v, '.', COMBINE(qprefer, atom->flags), lp, rp);
t->left = atom;
atomp = &t->left;
/*
* Here we should recurse... but we must postpone that to the end.
*/
/*
* Split top into prefix and remaining.
*/
assert(top->op == '=' && top->left == NULL && top->right == NULL);
top->left = subre(v, '=', top->flags, top->begin, lp);
top->op = '.';
top->right = t;
/*
* If it's a backref, now is the time to replicate the subNFA.
*/
if (atomtype == BACKREF) {
assert(atom->begin->nouts == 1); /* just the EMPTY */
delsub(v->nfa, atom->begin, atom->end);
assert(v->subs[subno] != NULL);
/*
* And here's why the recursion got postponed: it must wait until the
* skeleton is filled in, because it may hit a backref that wants to
* copy the filled-in skeleton.
*/
dupnfa(v->nfa, v->subs[subno]->begin, v->subs[subno]->end,
atom->begin, atom->end);
NOERR();
}
/*
* It's quantifier time; first, turn x{0,...} into x{1,...}|empty
*/
if (m == 0) {
EMPTYARC(s2, atom->end);/* the bypass */
assert(PREF(qprefer) != 0);
f = COMBINE(qprefer, atom->flags);
t = subre(v, '|', f, lp, atom->end);
NOERR();
t->left = atom;
t->right = subre(v, '|', PREF(f), s2, atom->end);
NOERR();
t->right->left = subre(v, '=', 0, s2, atom->end);
NOERR();
*atomp = t;
atomp = &t->left;
m = 1;
}
/*
* Deal with the rest of the quantifier.
*/
if (atomtype == BACKREF) {
/*
* Special case: backrefs have internal quantifiers.
*/
EMPTYARC(s, atom->begin); /* empty prefix */
/*
* Just stuff everything into atom.
*/
repeat(v, atom->begin, atom->end, m, n);
atom->min = (short) m;
atom->max = (short) n;
atom->flags |= COMBINE(qprefer, atom->flags);
} else if (m == 1 && n == 1) {
/*
* No/vacuous quantifier: done.
*/
EMPTYARC(s, atom->begin); /* empty prefix */
} else {
/*
* Turn x{m,n} into x{m-1,n-1}x, with capturing parens in only second
* x
*/
dupnfa(v->nfa, atom->begin, atom->end, s, atom->begin);
assert(m >= 1 && m != INFINITY && n >= 1);
repeat(v, s, atom->begin, m-1, (n == INFINITY) ? n : n-1);
f = COMBINE(qprefer, atom->flags);
t = subre(v, '.', f, s, atom->end); /* prefix and atom */
NOERR();
t->left = subre(v, '=', PREF(f), s, atom->begin);
NOERR();
t->right = atom;
*atomp = t;
}
/*
* And finally, look after that postponed recursion.
*/
t = top->right;
if (!(SEE('|') || SEE(stopper) || SEE(EOS))) {
t->right = parsebranch(v, stopper, type, atom->end, rp, 1);
} else {
EMPTYARC(atom->end, rp);
t->right = subre(v, '=', 0, atom->end, rp);
}
assert(SEE('|') || SEE(stopper) || SEE(EOS));
t->flags |= COMBINE(t->flags, t->right->flags);
top->flags |= COMBINE(top->flags, t->flags);
}
/*
- nonword - generate arcs for non-word-character ahead or behind
^ static void nonword(struct vars *, int, struct state *, struct state *);
*/
static void
nonword(
struct vars *v,
int dir, /* AHEAD or BEHIND */
struct state *lp,
struct state *rp)
{
int anchor = (dir == AHEAD) ? '$' : '^';
assert(dir == AHEAD || dir == BEHIND);
newarc(v->nfa, anchor, 1, lp, rp);
newarc(v->nfa, anchor, 0, lp, rp);
colorcomplement(v->nfa, v->cm, dir, v->wordchrs, lp, rp);
/* (no need for special attention to \n) */
}
/*
- word - generate arcs for word character ahead or behind
^ static void word(struct vars *, int, struct state *, struct state *);
*/
static void
word(
struct vars *v,
int dir, /* AHEAD or BEHIND */
struct state *lp,
struct state *rp)
{
assert(dir == AHEAD || dir == BEHIND);
cloneouts(v->nfa, v->wordchrs, lp, rp, dir);
/* (no need for special attention to \n) */
}
/*
- scannum - scan a number
^ static int scannum(struct vars *);
*/
static int /* value, <= DUPMAX */
scannum(
struct vars *v)
{
int n = 0;
while (SEE(DIGIT) && n < DUPMAX) {
n = n*10 + v->nextvalue;
NEXT();
}
if (SEE(DIGIT) || n > DUPMAX) {
ERR(REG_BADBR);
return 0;
}
return n;
}
/*
- repeat - replicate subNFA for quantifiers
* The duplication sequences used here are chosen carefully so that any
* pointers starting out pointing into the subexpression end up pointing into
* the last occurrence. (Note that it may not be strung between the same left
* and right end states, however!) This used to be important for the subRE
* tree, although the important bits are now handled by the in-line code in
* parse(), and when this is called, it doesn't matter any more.
^ static void repeat(struct vars *, struct state *, struct state *, int, int);
*/
static void
repeat(
struct vars *v,
struct state *lp,
struct state *rp,
int m,
int n)
{
#define SOME 2
#define INF 3
#define PAIR(x, y) ((x)*4 + (y))
#define REDUCE(x) ( ((x) == INFINITY) ? INF : (((x) > 1) ? SOME : (x)) )
const int rm = REDUCE(m);
const int rn = REDUCE(n);
struct state *s, *s2;
switch (PAIR(rm, rn)) {
case PAIR(0, 0): /* empty string */
delsub(v->nfa, lp, rp);
EMPTYARC(lp, rp);
break;
case PAIR(0, 1): /* do as x| */
EMPTYARC(lp, rp);
break;
case PAIR(0, SOME): /* do as x{1,n}| */
repeat(v, lp, rp, 1, n);
NOERR();
EMPTYARC(lp, rp);
break;
case PAIR(0, INF): /* loop x around */
s = newstate(v->nfa);
NOERR();
moveouts(v->nfa, lp, s);
moveins(v->nfa, rp, s);
EMPTYARC(lp, s);
EMPTYARC(s, rp);
break;
case PAIR(1, 1): /* no action required */
break;
case PAIR(1, SOME): /* do as x{0,n-1}x = (x{1,n-1}|)x */
s = newstate(v->nfa);
NOERR();
moveouts(v->nfa, lp, s);
dupnfa(v->nfa, s, rp, lp, s);
NOERR();
repeat(v, lp, s, 1, n-1);
NOERR();
EMPTYARC(lp, s);
break;
case PAIR(1, INF): /* add loopback arc */
s = newstate(v->nfa);
s2 = newstate(v->nfa);
NOERR();
moveouts(v->nfa, lp, s);
moveins(v->nfa, rp, s2);
EMPTYARC(lp, s);
EMPTYARC(s2, rp);
EMPTYARC(s2, s);
break;
case PAIR(SOME, SOME): /* do as x{m-1,n-1}x */
s = newstate(v->nfa);
NOERR();
moveouts(v->nfa, lp, s);
dupnfa(v->nfa, s, rp, lp, s);
NOERR();
repeat(v, lp, s, m-1, n-1);
break;
case PAIR(SOME, INF): /* do as x{m-1,}x */
s = newstate(v->nfa);
NOERR();
moveouts(v->nfa, lp, s);
dupnfa(v->nfa, s, rp, lp, s);
NOERR();
repeat(v, lp, s, m-1, n);
break;
default:
ERR(REG_ASSERT);
break;
}
}
/*
- bracket - handle non-complemented bracket expression
* Also called from cbracket for complemented bracket expressions.
^ static void bracket(struct vars *, struct state *, struct state *);
*/
static void
bracket(
struct vars *v,
struct state *lp,
struct state *rp)
{
assert(SEE('['));
NEXT();
while (!SEE(']') && !SEE(EOS)) {
brackpart(v, lp, rp);
}
assert(SEE(']') || ISERR());
okcolors(v->nfa, v->cm);
}
/*
- cbracket - handle complemented bracket expression
* We do it by calling bracket() with dummy endpoints, and then complementing
* the result. The alternative would be to invoke rainbow(), and then delete
* arcs as the b.e. is seen... but that gets messy.
^ static void cbracket(struct vars *, struct state *, struct state *);
*/
static void
cbracket(
struct vars *v,
struct state *lp,
struct state *rp)
{
struct state *left = newstate(v->nfa);
struct state *right = newstate(v->nfa);
NOERR();
bracket(v, left, right);
if (v->cflags®_NLSTOP) {
newarc(v->nfa, PLAIN, v->nlcolor, left, right);
}
NOERR();
assert(lp->nouts == 0); /* all outarcs will be ours */
/*
* Easy part of complementing, and all there is to do since the MCCE code
* was removed.
*/
colorcomplement(v->nfa, v->cm, PLAIN, left, lp, rp);
NOERR();
dropstate(v->nfa, left);
assert(right->nins == 0);
freestate(v->nfa, right);
return;
}
/*
- brackpart - handle one item (or range) within a bracket expression
^ static void brackpart(struct vars *, struct state *, struct state *);
*/
static void
brackpart(
struct vars *v,
struct state *lp,
struct state *rp)
{
celt startc, endc;
struct cvec *cv;
const chr *startp, *endp;
chr c[1];
/*
* Parse something, get rid of special cases, take shortcuts.
*/
switch (v->nexttype) {
case RANGE: /* a-b-c or other botch */
ERR(REG_ERANGE);
return;
break;
case PLAIN:
c[0] = v->nextvalue;
NEXT();
/*
* Shortcut for ordinary chr (not range).
*/
if (!SEE(RANGE)) {
onechr(v, c[0], lp, rp);
return;
}
startc = element(v, c, c+1);
NOERR();
break;
case COLLEL:
startp = v->now;
endp = scanplain(v);
INSIST(startp < endp, REG_ECOLLATE);
NOERR();
startc = element(v, startp, endp);
NOERR();
break;
case ECLASS:
startp = v->now;
endp = scanplain(v);
INSIST(startp < endp, REG_ECOLLATE);
NOERR();
startc = element(v, startp, endp);
NOERR();
cv = eclass(v, startc, (v->cflags®_ICASE));
NOERR();
dovec(v, cv, lp, rp);
return;
break;
case CCLASS:
startp = v->now;
endp = scanplain(v);
INSIST(startp < endp, REG_ECTYPE);
NOERR();
cv = cclass(v, startp, endp, (v->cflags®_ICASE));
NOERR();
dovec(v, cv, lp, rp);
return;
break;
default:
ERR(REG_ASSERT);
return;
break;
}
if (SEE(RANGE)) {
NEXT();
switch (v->nexttype) {
case PLAIN:
case RANGE:
c[0] = v->nextvalue;
NEXT();
endc = element(v, c, c+1);
NOERR();
break;
case COLLEL:
startp = v->now;
endp = scanplain(v);
INSIST(startp < endp, REG_ECOLLATE);
NOERR();
endc = element(v, startp, endp);
NOERR();
break;
default:
ERR(REG_ERANGE);
return;
break;
}
} else {
endc = startc;
}
/*
* Ranges are unportable. Actually, standard C does guarantee that digits
* are contiguous, but making that an exception is just too complicated.
*/
if (startc != endc) {
NOTE(REG_UUNPORT);
}
cv = range(v, startc, endc, (v->cflags®_ICASE));
NOERR();
dovec(v, cv, lp, rp);
}
/*
- scanplain - scan PLAIN contents of [. etc.
* Certain bits of trickery in lex.c know that this code does not try to look
* past the final bracket of the [. etc.
^ static const chr *scanplain(struct vars *);
*/
static const chr * /* just after end of sequence */
scanplain(
struct vars *v)
{
const chr *endp;
assert(SEE(COLLEL) || SEE(ECLASS) || SEE(CCLASS));
NEXT();
endp = v->now;
while (SEE(PLAIN)) {
endp = v->now;
NEXT();
}
assert(SEE(END) || ISERR());
NEXT();
return endp;
}
/*
- onechr - fill in arcs for a plain character, and possible case complements
* This is mostly a shortcut for efficient handling of the common case.
^ static void onechr(struct vars *, pchr, struct state *, struct state *);
*/
static void
onechr(
struct vars *v,
pchr c,
struct state *lp,
struct state *rp)
{
if (!(v->cflags®_ICASE)) {
newarc(v->nfa, PLAIN, subcolor(v->cm, c), lp, rp);
return;
}
/*
* Rats, need general case anyway...
*/
dovec(v, allcases(v, c), lp, rp);
}
/*
- dovec - fill in arcs for each element of a cvec
^ static void dovec(struct vars *, struct cvec *, struct state *,
^ struct state *);
*/
static void
dovec(
struct vars *v,
struct cvec *cv,
struct state *lp,
struct state *rp)
{
chr ch, from, to;
const chr *p;
int i;
for (p = cv->chrs, i = cv->nchrs; i > 0; p++, i--) {
ch = *p;
newarc(v->nfa, PLAIN, subcolor(v->cm, ch), lp, rp);
}
for (p = cv->ranges, i = cv->nranges; i > 0; p += 2, i--) {
from = *p;
to = *(p+1);
if (from <= to) {
subrange(v, from, to, lp, rp);
}
}
}
/*
- wordchrs - set up word-chr list for word-boundary stuff, if needed
* The list is kept as a bunch of arcs between two dummy states; it's disposed
* of by the unreachable-states sweep in NFA optimization. Does NEXT(). Must
* not be called from any unusual lexical context. This should be reconciled
* with the \w etc. handling in lex.c, and should be cleaned up to reduce
* dependencies on input scanning.
^ static void wordchrs(struct vars *);
*/
static void
wordchrs(
struct vars *v)
{
struct state *left, *right;
if (v->wordchrs != NULL) {
NEXT(); /* for consistency */
return;
}
left = newstate(v->nfa);
right = newstate(v->nfa);
NOERR();
/*
* Fine point: implemented with [::], and lexer will set REG_ULOCALE.
*/
lexword(v);
NEXT();
assert(v->savenow != NULL && SEE('['));
bracket(v, left, right);
assert((v->savenow != NULL && SEE(']')) || ISERR());
NEXT();
NOERR();
v->wordchrs = left;
}
/*
- subre - allocate a subre
^ static struct subre *subre(struct vars *, int, int, struct state *,
^ struct state *);
*/
static struct subre *
subre(
struct vars *v,
int op,
int flags,
struct state *begin,
struct state *end)
{
struct subre *ret = v->treefree;
if (ret != NULL) {
v->treefree = ret->left;
} else {
ret = (struct subre *) MALLOC(sizeof(struct subre));
if (ret == NULL) {
ERR(REG_ESPACE);
return NULL;
}
ret->chain = v->treechain;
v->treechain = ret;
}
assert(strchr("|.b(=", op) != NULL);
ret->op = op;
ret->flags = flags;
ret->retry = 0;
ret->subno = 0;
ret->min = ret->max = 1;
ret->left = NULL;
ret->right = NULL;
ret->begin = begin;
ret->end = end;
ZAPCNFA(ret->cnfa);
return ret;
}
/*
- freesubre - free a subRE subtree
^ static void freesubre(struct vars *, struct subre *);
*/
static void
freesubre(
struct vars *v, /* might be NULL */
struct subre *sr)
{
if (sr == NULL) {
return;
}
if (sr->left != NULL) {
freesubre(v, sr->left);
}
if (sr->right != NULL) {
freesubre(v, sr->right);
}
freesrnode(v, sr);
}
/*
- freesrnode - free one node in a subRE subtree
^ static void freesrnode(struct vars *, struct subre *);
*/
static void
freesrnode(
struct vars *v, /* might be NULL */
struct subre *sr)
{
if (sr == NULL) {
return;
}
if (!NULLCNFA(sr->cnfa)) {
freecnfa(&sr->cnfa);
}
sr->flags = 0;
if (v != NULL) {
sr->left = v->treefree;
v->treefree = sr;
} else {
FREE(sr);
}
}
/*
- optst - optimize a subRE subtree
^ static void optst(struct vars *, struct subre *);
*/
static void
optst(
struct vars *v,
struct subre *t)
{
/*
* DGP (2007-11-13): I assume it was the programmer's intent to eventually
* come back and add code to optimize subRE trees, but the routine coded
* just spends effort traversing the tree and doing nothing. We can do
* nothing with less effort.
*/
return;
}
/*
- numst - number tree nodes (assigning retry indexes)
^ static int numst(struct subre *, int);
*/
static int /* next number */
numst(
struct subre *t,
int start) /* starting point for subtree numbers */
{
int i;
assert(t != NULL);
i = start;
t->retry = (short) i++;
if (t->left != NULL) {
i = numst(t->left, i);
}
if (t->right != NULL) {
i = numst(t->right, i);
}
return i;
}
/*
- markst - mark tree nodes as INUSE
^ static void markst(struct subre *);
*/
static void
markst(
struct subre *t)
{
assert(t != NULL);
t->flags |= INUSE;
if (t->left != NULL) {
markst(t->left);
}
if (t->right != NULL) {
markst(t->right);
}
}
/*
- cleanst - free any tree nodes not marked INUSE
^ static void cleanst(struct vars *);
*/
static void
cleanst(
struct vars *v)
{
struct subre *t;
struct subre *next;
for (t = v->treechain; t != NULL; t = next) {
next = t->chain;
if (!(t->flags&INUSE)) {
FREE(t);
}
}
v->treechain = NULL;
v->treefree = NULL; /* just on general principles */
}
/*
- nfatree - turn a subRE subtree into a tree of compacted NFAs
^ static long nfatree(struct vars *, struct subre *, FILE *);
*/
static long /* optimize results from top node */
nfatree(
struct vars *v,
struct subre *t,
FILE *f) /* for debug output */
{
assert(t != NULL && t->begin != NULL);
if (t->left != NULL) {
(DISCARD) nfatree(v, t->left, f);
}
if (t->right != NULL) {
(DISCARD) nfatree(v, t->right, f);
}
return nfanode(v, t, f);
}
/*
- nfanode - do one NFA for nfatree
^ static long nfanode(struct vars *, struct subre *, FILE *);
*/
static long /* optimize results */
nfanode(
struct vars *v,
struct subre *t,
FILE *f) /* for debug output */
{
struct nfa *nfa;
long ret = 0;
char idbuf[50];
assert(t->begin != NULL);
if (f != NULL) {
fprintf(f, "\n\n\n========= TREE NODE %s ==========\n",
stid(t, idbuf, sizeof(idbuf)));
}
nfa = newnfa(v, v->cm, v->nfa);
NOERRZ();
dupnfa(nfa, t->begin, t->end, nfa->init, nfa->final);
if (!ISERR()) {
specialcolors(nfa);
ret = optimize(nfa, f);
}
if (!ISERR()) {
compact(nfa, &t->cnfa);
}
freenfa(nfa);
return ret;
}
/*
- newlacon - allocate a lookahead-constraint subRE
^ static int newlacon(struct vars *, struct state *, struct state *, int);
*/
static int /* lacon number */
newlacon(
struct vars *v,
struct state *begin,
struct state *end,
int pos)
{
struct subre *sub;
int n;
if (v->nlacons == 0) {
v->lacons = (struct subre *) MALLOC(2 * sizeof(struct subre));
n = 1; /* skip 0th */
v->nlacons = 2;
} else {
v->lacons = (struct subre *) REALLOC(v->lacons,
(v->nlacons+1)*sizeof(struct subre));
n = v->nlacons++;
}
if (v->lacons == NULL) {
ERR(REG_ESPACE);
return 0;
}
sub = &v->lacons[n];
sub->begin = begin;
sub->end = end;
sub->subno = pos;
ZAPCNFA(sub->cnfa);
return n;
}
/*
- freelacons - free lookahead-constraint subRE vector
^ static void freelacons(struct subre *, int);
*/
static void
freelacons(
struct subre *subs,
int n)
{
struct subre *sub;
int i;
assert(n > 0);
for (sub=subs+1, i=n-1; i>0; sub++, i--) { /* no 0th */
if (!NULLCNFA(sub->cnfa)) {
freecnfa(&sub->cnfa);
}
}
FREE(subs);
}
/*
- rfree - free a whole RE (insides of regfree)
^ static void rfree(regex_t *);
*/
static void
rfree(
regex_t *re)
{
struct guts *g;
if (re == NULL || re->re_magic != REMAGIC) {
return;
}
re->re_magic = 0; /* invalidate RE */
g = (struct guts *) re->re_guts;
re->re_guts = NULL;
re->re_fns = NULL;
g->magic = 0;
freecm(&g->cmap);
if (g->tree != NULL) {
freesubre(NULL, g->tree);
}
if (g->lacons != NULL) {
freelacons(g->lacons, g->nlacons);
}
if (!NULLCNFA(g->search)) {
freecnfa(&g->search);
}
FREE(g);
}
/*
- dump - dump an RE in human-readable form
^ static void dump(regex_t *, FILE *);
*/
static void
dump(
regex_t *re,
FILE *f)
{
#ifdef REG_DEBUG
struct guts *g;
int i;
if (re->re_magic != REMAGIC) {
fprintf(f, "bad magic number (0x%x not 0x%x)\n",
re->re_magic, REMAGIC);
}
if (re->re_guts == NULL) {
fprintf(f, "NULL guts!!!\n");
return;
}
g = (struct guts *) re->re_guts;
if (g->magic != GUTSMAGIC) {
fprintf(f, "bad guts magic number (0x%x not 0x%x)\n",
g->magic, GUTSMAGIC);
}
fprintf(f, "\n\n\n========= DUMP ==========\n");
fprintf(f, "nsub %d, info 0%lo, csize %d, ntree %d\n",
re->re_nsub, re->re_info, re->re_csize, g->ntree);
dumpcolors(&g->cmap, f);
if (!NULLCNFA(g->search)) {
printf("\nsearch:\n");
dumpcnfa(&g->search, f);
}
for (i = 1; i < g->nlacons; i++) {
fprintf(f, "\nla%d (%s):\n", i,
(g->lacons[i].subno) ? "positive" : "negative");
dumpcnfa(&g->lacons[i].cnfa, f);
}
fprintf(f, "\n");
dumpst(g->tree, f, 0);
#endif
}
/*
- dumpst - dump a subRE tree
^ static void dumpst(struct subre *, FILE *, int);
*/
static void
dumpst(
struct subre *t,
FILE *f,
int nfapresent) /* is the original NFA still around? */
{
if (t == NULL) {
fprintf(f, "null tree\n");
} else {
stdump(t, f, nfapresent);
}
fflush(f);
}
/*
- stdump - recursive guts of dumpst
^ static void stdump(struct subre *, FILE *, int);
*/
static void
stdump(
struct subre *t,
FILE *f,
int nfapresent) /* is the original NFA still around? */
{
char idbuf[50];
fprintf(f, "%s. `%c'", stid(t, idbuf, sizeof(idbuf)), t->op);
if (t->flags&LONGER) {
fprintf(f, " longest");
}
if (t->flags&SHORTER) {
fprintf(f, " shortest");
}
if (t->flags&MIXED) {
fprintf(f, " hasmixed");
}
if (t->flags&CAP) {
fprintf(f, " hascapture");
}
if (t->flags&BACKR) {
fprintf(f, " hasbackref");
}
if (!(t->flags&INUSE)) {
fprintf(f, " UNUSED");
}
if (t->subno != 0) {
fprintf(f, " (#%d)", t->subno);
}
if (t->min != 1 || t->max != 1) {
fprintf(f, " {%d,", t->min);
if (t->max != INFINITY) {
fprintf(f, "%d", t->max);
}
fprintf(f, "}");
}
if (nfapresent) {
fprintf(f, " %ld-%ld", (long)t->begin->no, (long)t->end->no);
}
if (t->left != NULL) {