Debian Codegraph @

doc/example/top-priority-values/siteconfig.tcl

raw
1 #
2 # siteconfig.tcl - Site specific configuration introducing a '--top' argument
3 # for the setenv, append-path and prepend-path modulefile commands. '--top'
4 # argument enables a value set with this flag on to hold top priority and
5 # thus cannot be altered unless by another top priority value. For
6 # 'append-path' command, '--top' ensure value will stay at last position in
7 # path-like variable. Same goes for 'prepend-path' where '--top' ensure
8 # first position in path-like variable.
9 #
10 # Author: Xavier Delaruelle <[email protected]>
11 # Compatibility: Modules v4.2
12 #
13 # Installation: put this file in the 'etc' directory of your Modules
14 # installation. Refer to the "Modulecmd startup" section in the
15 # module(1) man page to get this location.
16
17 # override 'setenv' procedure to add a '--top' optional argument
18 rename ::setenv ::__setenv
19 proc setenv {args} {
20 set arglist [lsearch -all -inline -not -exact $args "--top"]
21 lassign [parseSetenvCommandArgs load set {*}$arglist] bhv var val
22
23 # top priority asked
24 if {[llength $arglist] != [llength $args]} {
25 # define an helper variable to know a top-priority value has been set
26 if {[currentState mode] ne "display"} {
27 __setenv MODULES_PRIORITY_$var $val
28 }
29 __setenv {*}$arglist
30 # set non-priority value only if no top priority value already set
31 } elseif {![info exists ::env(MODULES_PRIORITY_$var)]} {
32 __setenv {*}$arglist
33 }
34 }
35
36 # override 'setenv-un' procedure to interpret the '--top' optional argument
37 # when setenv is evaluated on an unload mode
38 rename ::setenv-un ::__setenv-un
39 proc setenv-un {args} {
40 set arglist [lsearch -all -inline -not -exact $args "--top"]
41 lassign [parseSetenvCommandArgs unload unset {*}$arglist] bhv var val
42
43 # top priority asked
44 if {[llength $arglist] != [llength $args]} {
45 # define an helper variable to know a top-priority value has been set
46 if {[currentState mode] ne "display"} {
47 __setenv-un MODULES_PRIORITY_$var $val
48 }
49 __setenv-un {*}$arglist
50 # set non-priority value only if no top priority value already set
51 } elseif {![info exists ::env(MODULES_PRIORITY_$var)]} {
52 __setenv-un {*}$arglist
53 }
54 }
55
56 # override 'setenv-wh' procedure to interpret the '--top' optional argument
57 # when setenv is evaluated on an whatis mode
58 rename ::setenv-wh ::__setenv-wh
59 proc setenv-wh {args} {
60 set arglist [lsearch -all -inline -not -exact $args "--top"]
61 __setenv-wh {*}$arglist
62 }
63
64 # override 'add-path' procedure to add a '--top' optional argument, which
65 # will benefit to the 'append-path' and 'prepend-path' modulefile commands
66 rename ::add-path ::__add-path
67 proc add-path {cmd mode dflbhv args} {
68 set keep_top_priority 0
69 set pos [expr {$cmd eq {append-path} ? {append} : {prepend}}]
70 set arglist [lsearch -all -inline -not -exact $args "--top"]
71 lassign [parsePathCommandArgs $cmd $mode $dflbhv {*}$arglist] separator\
72 allow_dup idx_val ign_refcount bhv var path_list
73
74 # top priority asked
75 if {[llength $arglist] != [llength $args]} {
76 # record this top priority value in an helper variable
77 __setenv MODULES_PRIORITY_${pos}_$var $path_list
78 } elseif {[info exists ::env(MODULES_PRIORITY_${pos}_$var)]} {
79 set keep_top_priority 1
80 }
81
82 # ensure top-priority value keeps first or last position by unloading it
83 # priority new value addition, then restoring it
84 if {$keep_top_priority} {
85 eval __unload-path $cmd load remove $var\
86 $::env(MODULES_PRIORITY_${pos}_$var)
87 }
88
89 __add-path $cmd $mode $dflbhv {*}$arglist
90
91 if {$keep_top_priority} {
92 eval __add-path $cmd $mode $dflbhv $var\
93 $::env(MODULES_PRIORITY_${pos}_$var)
94 }
95 }
96
97 rename ::unload-path ::__unload-path
98 proc unload-path {cmd mode dflbhv args} {
99 set arglist [lsearch -all -inline -not -exact $args "--top"]
100 lassign [parsePathCommandArgs $cmd $mode $dflbhv {*}$arglist] separator\
101 allow_dup idx_val ign_refcount bhv var path_list
102
103 if {[llength $arglist] != [llength $args]} {
104 # wipe priority helper variable when unloading top priority value
105 switch -- $cmd {
106 {append-path} { set pos append }
107 {prepend-path} { set pos prepend }
108 }
109 if {[info exists pos]} {
110 __setenv MODULES_PRIORITY_${pos}_$var $path_list
111 }
112 }
113
114 __unload-path $cmd $mode $dflbhv {*}$arglist
115 }
116
117 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent: