Debian Codegraph @

tcl/envmngt.tcl.in

raw
1 ##########################################################################
2
3 # ENVMNGT.TCL, environment management procedures
4 # Copyright (C) 2002-2004 Mark Lakata
5 # Copyright (C) 2004-2017 Kent Mein
6 # Copyright (C) 2016-2025 Xavier Delaruelle
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ##########################################################################
22
23 proc incrErrorCount {} {
24 updateErrorCount [expr {[getState error_count] + 1}]
25 }
26
27 proc decrErrorCount {} {
28 updateErrorCount [expr {[getState error_count] - 1}]
29 }
30
31 proc updateErrorCount {count} {
32 # hold update of error count
33 if {[depthState reportholdid] > 0} {
34 lappend ::g_holdReport([currentState reportholdid]) [list\
35 updateErrorCount $count]
36 } else {
37 setState error_count $count
38 }
39 }
40
41 proc renderFalse {} {
42 if {[isStateDefined false_rendered]} {
43 reportDebug {false already rendered}
44 # no shell code to render false if shell not set
45 } elseif {[isStateDefined shell]} {
46 # setup flag to render only once
47 setState false_rendered 1
48
49 # render a false value most of the time through a variable assignment
50 # that will be looked at in the shell module function calling
51 # modulecmd.tcl to return in turns a boolean status. Except for python
52 # and cmake, the value assigned to variable is also returned as the
53 # entire rendering status
54 switch -- [getState shelltype] {
55 sh - csh - fish {
56 # no need to set a variable on real shells as last statement
57 # result can easily be checked
58 lappend ::g_shcode_out {test 0 = 1;}
59 }
60 tcl {
61 lappend ::g_shcode_out {set _mlstatus 0;}
62 }
63 cmd {
64 lappend ::g_shcode_out {set errorlevel=1}
65 }
66 perl {
67 lappend ::g_shcode_out {{ no strict 'vars'; $_mlstatus = 0; }}
68 }
69 python {
70 lappend ::g_shcode_out {_mlstatus = False}
71 }
72 ruby {
73 lappend ::g_shcode_out {_mlstatus = false}
74 }
75 lisp {
76 lappend ::g_shcode_out {nil}
77 }
78 cmake {
79 lappend ::g_shcode_out {set(_mlstatus FALSE)}
80 }
81 r {
82 lappend ::g_shcode_out {mlstatus <- FALSE}
83 }
84 pwsh {
85 lappend ::g_shcode_out {Set-Variable -Name "_mlstatus" -Value\
86 $false -Scope Global}
87 }
88 }
89 }
90 }
91
92 proc renderTrue {} {
93 # render a true value most of the time through a variable assignment that
94 # will be looked at in the shell module function calling modulecmd.tcl to
95 # return in turns a boolean status. Except for python, cmake, and pwsh,
96 # the value assigned to variable is also returned as the full rendering
97 # status
98 switch -- [getState shelltype] {
99 sh - csh - fish {
100 # no need to set a variable on real shells as last statement
101 # result can easily be checked
102 lappend ::g_shcode_out {test 0;}
103 }
104 tcl {
105 lappend ::g_shcode_out {set _mlstatus 1;}
106 }
107 cmd {
108 lappend ::g_shcode_out {set errorlevel=0}
109 }
110 perl {
111 lappend ::g_shcode_out {{ no strict 'vars'; $_mlstatus = 1; }}
112 }
113 python {
114 lappend ::g_shcode_out {_mlstatus = True}
115 }
116 ruby {
117 lappend ::g_shcode_out {_mlstatus = true}
118 }
119 lisp {
120 lappend ::g_shcode_out {t}
121 }
122 cmake {
123 lappend ::g_shcode_out {set(_mlstatus TRUE)}
124 }
125 r {
126 lappend ::g_shcode_out {mlstatus <- TRUE}
127 }
128 pwsh {
129 lappend ::g_shcode_out {Set-Variable -Name "_mlstatus" -Value\
130 $true -Scope Global}
131 }
132 }
133 }
134
135 proc renderText {text} {
136 # render a text value most of the time through a variable assignment that
137 # will be looked at in the shell module function calling modulecmd.tcl to
138 # return in turns a string value.
139 switch -- [getState shelltype] {
140 sh - csh - fish {
141 foreach word $text {
142 # no need to set a variable on real shells, echoing text will make
143 # it available as result
144 lappend ::g_shcode_out "echo '$word';"
145 }
146 }
147 tcl {
148 lappend ::g_shcode_out "set _mlstatus \"$text\";"
149 }
150 cmd {
151 foreach word $text {
152 lappend ::g_shcode_out "echo $word"
153 }
154 }
155 perl {
156 lappend ::g_shcode_out "{ no strict 'vars'; \$_mlstatus = '$text'; }"
157 }
158 python {
159 lappend ::g_shcode_out "_mlstatus = '$text'"
160 }
161 ruby {
162 lappend ::g_shcode_out "_mlstatus = '$text'"
163 }
164 lisp {
165 lappend ::g_shcode_out "(message \"$text\")"
166 }
167 cmake {
168 lappend ::g_shcode_out "set(_mlstatus \"$text\")"
169 }
170 r {
171 lappend ::g_shcode_out "mlstatus <- '$text'"
172 }
173 pwsh {
174 lappend ::g_shcode_out "Set-Variable -Name \"_mlstatus\" -Value\
175 '$text' -Scope Global"
176 }
177 }
178 }
179
180 proc renderSettings {} {
181 global g_stateEnvVars g_stateAliases g_stateFunctions g_stateCompletes\
182 g_newXResources g_delXResources g_shcode_out
183
184 # preliminaries if there is stuff to render
185 if {[getState autoinit] || [array size g_stateEnvVars] || [array size\
186 g_stateAliases] || [array size g_newXResources] || [array size\
187 g_stateFunctions] || [array size g_stateCompletes] || [array size\
188 g_delXResources] || [info exists ::g_changeDir] || [info exists\
189 ::g_stdoutPuts] || [info exists ::g_prestdoutPuts] || [info exists\
190 ::g_return_text]} {
191 switch -- [getState shelltype] {
192 python {
193 lappend g_shcode_out {import os}
194 }
195 }
196 set has_rendered 1
197 } else {
198 set has_rendered 0
199 }
200
201 # send pre content deferred during modulefile interpretation
202 if {[info exists ::g_prestdoutPuts]} {
203 foreach {newline msg} $::g_prestdoutPuts {
204 append outmsg $msg
205 if {$newline} {
206 lappend g_shcode_out $outmsg
207 unset outmsg
208 }
209 }
210 # add last remaining message
211 if {[info exists outmsg]} {
212 lappend g_shcode_out $outmsg
213 }
214 }
215
216 if {[getState autoinit]} {
217 renderAutoinit
218 }
219
220 # filter Modules-specific environment variables for mod-to-sh subcmd
221 if {[getState modtosh_real_shell] ne {}} {
222 foreach globvar [getModulesEnvVarGlobList 1] {
223 foreach var [array names g_stateEnvVars -glob $globvar] {
224 unset g_stateEnvVars($var)
225 }
226 }
227 }
228
229 # new environment variables
230 foreach var [array names g_stateEnvVars] {
231 switch -- $g_stateEnvVars($var) {
232 new {
233 switch -- [getState shelltype] {
234 csh {
235 # cannot handle newline character in env var value with csh
236 # shells: chop newline character in value (as done on
237 # Modules v3 for all shells)
238 set val [string map {\n {}} $::env($var)]
239 set val [charEscaped $val]
240 # csh barfs on long env vars
241 if {[getState shell] eq {csh} && [string length $val] >\
242 [getConf csh_limit]} {
243 if {$var eq {PATH}} {
244 reportWarning "PATH exceeds [getConf csh_limit]\
245 characters, truncating and appending\
246 /usr/bin:/bin ..."
247 set val [string range $val 0 [getConf\
248 csh_limit]-1]:/usr/bin:/bin
249 } else {
250 reportWarning "$var exceeds [getConf csh_limit]\
251 characters, truncating..."
252 set val [string range $val 0 [getConf csh_limit]-1]
253 }
254 }
255 lappend g_shcode_out "setenv $var $val;"
256 }
257 sh {
258 set val [string map {' '\\''} $::env($var)]
259 lappend g_shcode_out "$var='$val'; export $var;"
260 }
261 fish {
262 set val [charEscaped $::env($var) ']
263 # fish shell has special treatment for PATH variable
264 # so its value should be provided as a list separated
265 # by spaces not by semi-colons
266 if {$var eq {PATH}} {
267 set val [join [split $val :] {' '}]
268 }
269 lappend g_shcode_out "set -xg $var '$val';"
270 }
271 tcl {
272 set val $::env($var)
273 lappend g_shcode_out "set ::env($var) {$val};"
274 }
275 cmd {
276 set val $::env($var)
277 lappend g_shcode_out "set $var=$val"
278 }
279 perl {
280 set val [charEscaped $::env($var) ']
281 lappend g_shcode_out "\$ENV{'$var'} = '$val';"
282 }
283 python {
284 set val [charEscaped $::env($var) ']
285 lappend g_shcode_out "os.environ\['$var'\] = '$val'"
286 }
287 ruby {
288 set val [charEscaped $::env($var) ']
289 lappend g_shcode_out "ENV\['$var'\] = '$val'"
290 }
291 lisp {
292 set val [charEscaped $::env($var) \"]
293 lappend g_shcode_out "(setenv \"$var\" \"$val\")"
294 }
295 cmake {
296 set val [charEscaped $::env($var) \"]
297 lappend g_shcode_out "set(ENV{$var} \"$val\")"
298 }
299 r {
300 set val [charEscaped $::env($var) {\\'}]
301 lappend g_shcode_out "Sys.setenv('$var'='$val')"
302 }
303 pwsh {
304 set val [charEscaped $::env($var) ']
305 lappend g_shcode_out "\$env:$var = '$val'"
306 }
307 }
308 }
309 del {
310 switch -- [getState shelltype] {
311 csh {
312 lappend g_shcode_out "unsetenv $var;"
313 }
314 sh {
315 lappend g_shcode_out "unset $var;"
316 }
317 fish {
318 lappend g_shcode_out "set -e $var;"
319 }
320 tcl {
321 lappend g_shcode_out "catch {unset ::env($var)};"
322 }
323 cmd {
324 lappend g_shcode_out "set $var="
325 }
326 perl {
327 lappend g_shcode_out "delete \$ENV{'$var'};"
328 }
329 python {
330 lappend g_shcode_out "os.environ\['$var'\] = ''"
331 lappend g_shcode_out "del os.environ\['$var'\]"
332 }
333 ruby {
334 lappend g_shcode_out "ENV\['$var'\] = nil"
335 }
336 lisp {
337 lappend g_shcode_out "(setenv \"$var\" nil)"
338 }
339 cmake {
340 lappend g_shcode_out "unset(ENV{$var})"
341 }
342 r {
343 lappend g_shcode_out "Sys.unsetenv('$var')"
344 }
345 pwsh {
346 lappend g_shcode_out "Remove-Item -Path env:$var\
347 -ErrorAction SilentlyContinue"
348 }
349 }
350 }
351 }
352 }
353
354 foreach var [array names g_stateAliases] {
355 switch -- $g_stateAliases($var) {
356 new {
357 set val $::g_Aliases($var)
358 # convert $n in !!:n and $* in !* on csh (like on compat version)
359 if {[getState shelltype] eq {csh}} {
360 regsub -all {([^\\]|^)\$([0-9]+)} $val {\1!!:\2} val
361 regsub -all {([^\\]|^)\$\*} $val {\1!*} val
362 }
363 # unescape \$ after now csh-specific conversion is over
364 regsub -all {\\\$} $val {$} val
365 switch -- [getState shelltype] {
366 csh {
367 set val [charEscaped $val]
368 lappend g_shcode_out "alias $var $val;"
369 }
370 sh {
371 set val [charEscaped $val]
372 lappend g_shcode_out "alias $var=$val;"
373 }
374 fish {
375 set val [charEscaped $val]
376 lappend g_shcode_out "alias $var $val;"
377 }
378 cmd {
379 lappend g_shcode_out "doskey $var=$val"
380 }
381 pwsh {
382 lappend g_shcode_out "function global:$var { $val }"
383 }
384 }
385 }
386 del {
387 switch -- [getState shelltype] {
388 csh {
389 lappend g_shcode_out "unalias $var;"
390 }
391 sh {
392 lappend g_shcode_out "unalias $var 2>/dev/null || true;"
393 }
394 fish {
395 lappend g_shcode_out "functions -e $var;"
396 }
397 cmd {
398 lappend g_shcode_out "doskey $var="
399 }
400 pwsh {
401 lappend g_shcode_out "Remove-Item -Path function:$var\
402 -ErrorAction SilentlyContinue"
403 }
404 }
405 }
406 }
407 }
408 foreach funcname [array names g_stateFunctions] {
409 switch -- $g_stateFunctions($funcname) {
410 new {
411 # trim function body to smoothly add a finishing ;
412 set val [string trim $::g_Functions($funcname) "; \t\n\r"]
413 switch -- [getState shell] {
414 sh - ksh - zsh {
415 lappend g_shcode_out "$funcname () { $val; };"
416 }
417 bash {
418 lappend g_shcode_out "$funcname () { $val; }; export -f\
419 $funcname;"
420 }
421 fish {
422 lappend g_shcode_out "function $funcname; $val; end;"
423 }
424 pwsh {
425 lappend g_shcode_out "function global:$funcname { $val }"
426 }
427 }
428 }
429 del {
430 switch -- [getState shelltype] {
431 sh {
432 lappend g_shcode_out "unset -f $funcname 2>/dev/null ||\
433 true;"
434 }
435 fish {
436 lappend g_shcode_out "functions -e $funcname;"
437 }
438 pwsh {
439 lappend g_shcode_out "Remove-Item -Path function:$funcname\
440 -ErrorAction SilentlyContinue"
441 }
442 }
443 }
444 }
445 }
446 foreach compname [array names g_stateCompletes] {
447 switch -- $g_stateCompletes($compname) {
448 new {
449 foreach {compshell body} $::g_Completes($compname) {
450 # skip definition not made for current shell
451 if {$compshell eq [getState shell]} {
452 switch -- [getState shell] {
453 bash {
454 lappend g_shcode_out "complete $body $compname;"
455 }
456 tcsh {
457 lappend g_shcode_out "complete $compname $body;"
458 }
459 fish {
460 # ensure pre-existing fish completion is cleared
461 if {![info exists fishcompclear($compname)]} {
462 lappend g_shcode_out "complete -e -c $compname;"
463 set fishcompclear($compname) 1
464 }
465 lappend g_shcode_out "complete -c $compname $body;"
466 }
467 pwsh {
468 lappend g_shcode_out "Register-ArgumentCompleter\
469 -CommandName $compname -ScriptBlock { $body }"
470 }
471 }
472 }
473 }
474 }
475 del {
476 switch -- [getState shell] {
477 bash {
478 lappend g_shcode_out "complete -r $compname;"
479 }
480 tcsh {
481 lappend g_shcode_out "uncomplete $compname;"
482 }
483 fish {
484 lappend g_shcode_out "complete -e -c $compname;"
485 }
486 pwsh {
487 lappend g_shcode_out "Unregister-ArgumentCompleter\
488 -CommandName $compname"
489 }
490 }
491 }
492 }
493 }
494
495 # preliminaries for x-resources stuff
496 if {[array size g_newXResources] || [array size g_delXResources]} {
497 switch -- [getState shelltype] {
498 python {
499 lappend g_shcode_out {import subprocess}
500 }
501 ruby {
502 lappend g_shcode_out {require 'open3'}
503 }
504 }
505 }
506
507 # new x resources
508 if {[array size g_newXResources]} {
509 # xrdb executable has already be verified in x-resource
510 set xrdb [getCommandPath xrdb]
511 foreach var [array names g_newXResources] {
512 set val $g_newXResources($var)
513 # empty val means that var is a file to parse
514 if {$val eq {}} {
515 switch -- [getState shelltype] {
516 sh - csh - fish {
517 lappend g_shcode_out "$xrdb -merge $var;"
518 }
519 tcl {
520 lappend g_shcode_out "exec $xrdb -merge $var;"
521 }
522 perl {
523 lappend g_shcode_out "system(\"$xrdb -merge $var\");"
524 }
525 python {
526 set var [charEscaped $var ']
527 lappend g_shcode_out "subprocess.Popen(\['$xrdb',\
528 '-merge', '$var'\])"
529 }
530 ruby {
531 set var [charEscaped $var ']
532 lappend g_shcode_out "Open3.popen2('$xrdb -merge $var')"
533 }
534 lisp {
535 lappend g_shcode_out "(shell-command-to-string \"$xrdb\
536 -merge $var\")"
537 }
538 cmake {
539 lappend g_shcode_out "execute_process(COMMAND $xrdb -merge\
540 $var)"
541 }
542 r {
543 set var [charEscaped $var {\\'}]
544 lappend g_shcode_out "system('$xrdb -merge $var')"
545 }
546 }
547 } else {
548 switch -- [getState shelltype] {
549 sh - csh - fish {
550 set var [charEscaped $var \"]
551 set val [charEscaped $val \"]
552 lappend g_shcode_out "echo \"$var: $val\" | $xrdb -merge;"
553 }
554 tcl {
555 lappend g_shcode_out "set XRDBPIPE \[open \"|$xrdb -merge\"\
556 r+\];"
557 set var [charEscaped $var \"]
558 set val [charEscaped $val \"]
559 lappend g_shcode_out "puts \$XRDBPIPE \"$var: $val\";"
560 lappend g_shcode_out {close $XRDBPIPE;}
561 lappend g_shcode_out {unset XRDBPIPE;}
562 }
563 perl {
564 lappend g_shcode_out "open(XRDBPIPE, \"|$xrdb -merge\");"
565 set var [charEscaped $var \"]
566 set val [charEscaped $val \"]
567 lappend g_shcode_out "print XRDBPIPE \"$var: $val\\n\";"
568 lappend g_shcode_out {close XRDBPIPE;}
569 }
570 python {
571 set var [charEscaped $var ']
572 set val [charEscaped $val ']
573 lappend g_shcode_out "subprocess.Popen(\['$xrdb',\
574 '-merge'\],\
575 stdin=subprocess.PIPE).communicate(input='$var:\
576 $val\\n')"
577 }
578 ruby {
579 set var [charEscaped $var ']
580 set val [charEscaped $val ']
581 lappend g_shcode_out "Open3.popen2('$xrdb -merge') {|i,o,t|\
582 i.puts '$var: $val'}"
583 }
584 lisp {
585 lappend g_shcode_out "(shell-command-to-string \"echo $var:\
586 $val | $xrdb -merge\")"
587 }
588 cmake {
589 set var [charEscaped $var \"]
590 set val [charEscaped $val \"]
591 lappend g_shcode_out "execute_process(COMMAND echo \"$var:\
592 $val\" COMMAND $xrdb -merge)"
593 }
594 r {
595 set var [charEscaped $var {\\'}]
596 set val [charEscaped $val {\\'}]
597 lappend g_shcode_out "system('$xrdb -merge', input='$var:\
598 $val')"
599 }
600 }
601 }
602 }
603 }
604
605 if {[array size g_delXResources]} {
606 ##nagelfar ignore Found constant
607 set xrdb [getCommandPath xrdb]
608 set xres_to_del {}
609 foreach var [array names g_delXResources] {
610 # empty val means that var is a file to parse
611 if {$g_delXResources($var) eq {}} {
612 # xresource file has to be parsed to find what resources
613 # are declared there and need to be unset
614 foreach fline [split [exec $xrdb -n load $var] \n] {
615 lappend xres_to_del [lindex [split $fline :] 0]
616 }
617 } else {
618 lappend xres_to_del $var
619 }
620 }
621
622 # xresource strings are unset by emptying their value since there
623 # is no command of xrdb that can properly remove one property
624 switch -- [getState shelltype] {
625 sh - csh - fish {
626 foreach var $xres_to_del {
627 lappend g_shcode_out "echo \"$var:\" | $xrdb -merge;"
628 }
629 }
630 tcl {
631 foreach var $xres_to_del {
632 lappend g_shcode_out "set XRDBPIPE \[open \"|$xrdb -merge\"\
633 r+\];"
634 set var [charEscaped $var \"]
635 lappend g_shcode_out "puts \$XRDBPIPE \"$var:\";"
636 lappend g_shcode_out {close $XRDBPIPE;}
637 lappend g_shcode_out {unset XRDBPIPE;}
638 }
639 }
640 perl {
641 foreach var $xres_to_del {
642 lappend g_shcode_out "open(XRDBPIPE, \"|$xrdb -merge\");"
643 set var [charEscaped $var \"]
644 lappend g_shcode_out "print XRDBPIPE \"$var:\\n\";"
645 lappend g_shcode_out {close XRDBPIPE;}
646 }
647 }
648 python {
649 foreach var $xres_to_del {
650 set var [charEscaped $var ']
651 lappend g_shcode_out "subprocess.Popen(\['$xrdb', '-merge'\],\
652 stdin=subprocess.PIPE).communicate(input='$var:\\n')"
653 }
654 }
655 ruby {
656 foreach var $xres_to_del {
657 set var [charEscaped $var ']
658 lappend g_shcode_out "Open3.popen2('$xrdb -merge') {|i,o,t|\
659 i.puts '$var:'}"
660 }
661 }
662 lisp {
663 foreach var $xres_to_del {
664 lappend g_shcode_out "(shell-command-to-string \"echo $var: |\
665 $xrdb -merge\")"
666 }
667 }
668 cmake {
669 foreach var $xres_to_del {
670 set var [charEscaped $var \"]
671 lappend g_shcode_out "execute_process(COMMAND echo \"$var:\"\
672 COMMAND $xrdb -merge)"
673 }
674 }
675 r {
676 foreach var $xres_to_del {
677 set var [charEscaped $var {\\'}]
678 lappend g_shcode_out "system('$xrdb -merge', input='$var:')"
679 }
680 }
681 }
682 }
683
684 if {[info exists ::g_changeDir]} {
685 switch -- [getState shelltype] {
686 sh - csh - fish {
687 lappend g_shcode_out "cd '$::g_changeDir';"
688 }
689 tcl {
690 lappend g_shcode_out "cd \"$::g_changeDir\";"
691 }
692 cmd {
693 lappend g_shcode_out "cd $::g_changeDir"
694 }
695 perl {
696 lappend g_shcode_out "chdir '$::g_changeDir';"
697 }
698 python {
699 lappend g_shcode_out "os.chdir('$::g_changeDir')"
700 }
701 ruby {
702 lappend g_shcode_out "Dir.chdir('$::g_changeDir')"
703 }
704 lisp {
705 lappend g_shcode_out "(shell-command-to-string \"cd\
706 '$::g_changeDir'\")"
707 }
708 r {
709 lappend g_shcode_out "setwd('$::g_changeDir')"
710 }
711 pwsh {
712 lappend g_shcode_out "Set-Location '$::g_changeDir'"
713 }
714 }
715 # cannot change current directory of cmake "shell"
716 }
717
718 # send content deferred during modulefile interpretation
719 if {[info exists ::g_stdoutPuts]} {
720 foreach {newline msg} $::g_stdoutPuts {
721 append outmsg $msg
722 if {$newline} {
723 lappend g_shcode_out $outmsg
724 unset outmsg
725 }
726 }
727 # add last remaining message
728 if {[info exists outmsg]} {
729 lappend g_shcode_out $outmsg
730 }
731 }
732
733 # if currently processing mod-to-sh subcmd, send shell code to the message
734 # channel then reset code and shell state to get evaluation result shell
735 # code on stdout channel
736 if {[getState modtosh_real_shell] ne {}} {
737 if {[info exists g_shcode_out]} {
738 report [join $::g_shcode_out \n]
739 unset g_shcode_out
740 }
741
742 setState shell [getState modtosh_real_shell]
743 unsetState shelltype
744 }
745
746 # return text value if defined even if error happened
747 if {[info exists ::g_return_text]} {
748 reportDebug {text value should be returned.}
749 renderText $::g_return_text
750 } elseif {[getState error_count] > 0} {
751 reportDebug "[getState error_count] error(s) detected."
752 renderFalse
753 } elseif {[getState return_false]} {
754 reportDebug {false value should be returned.}
755 renderFalse
756 } elseif {$has_rendered} {
757 # finish with true statement if something has been put
758 renderTrue
759 }
760 }
761
762 # Output all gathered shell code to stdout. This is a separate proc rather the
763 # final code of renderSettings as such output is also done when exiting after
764 # an error. Stdout is flushed from flushAndExit proc
765 proc renderFlush {} {
766 if {[info exists ::g_shcode_out]} {
767 # required to work on cygwin, shouldn't hurt real linux
768 fconfigure stdout -translation lf
769
770 puts stdout [join $::g_shcode_out \n]
771 }
772 }
773
774 proc renderAutoinit {} {
775 # automatically detect which tclsh should be used for
776 # future module commands
777 set tclshbin [info nameofexecutable]
778
779 # ensure script path is absolute
780 set ::argv0 [getAbsolutePath $::argv0]
781
782 set is_mogui_avail [string length [getCommandPath mogui-cmd]]
783
784 ##nagelfar ignore #482 Close brace not aligned with line
785 switch -- [getState shelltype] {
786 csh {
787 set pre_hi {set _histchars = $histchars; unset histchars;}
788 set post_hi {set histchars = $_histchars; unset _histchars;}
789 set pre_pr {set _prompt=$prompt:q; set prompt="";}
790 set post_pr {set prompt=$_prompt:q; unset _prompt;}
791 # apply workaround for Tcsh history if set
792 set eval_cmd [expr {[getConf wa_277] ? "eval `$tclshbin\
793 '\"'\"'$::argv0'\"'\"' [getState shell] \\!*`;" : "eval\
794 \"`$tclshbin '\"'\"'$::argv0'\"'\"' [getState shell] \\!*:q`\";"}]
795 set pre_ex {set _exit="$status";}
796 set post_ex {test 0 = $_exit}
797
798 set fdef "if ( \$?histchars && \$?prompt )\
799 alias module '$pre_hi $pre_pr $eval_cmd $pre_ex $post_hi $post_pr $post_ex' ;
800 if ( \$?histchars && ! \$?prompt )\
801 alias module '$pre_hi $eval_cmd $pre_ex $post_hi $post_ex' ;
802 if ( ! \$?histchars && \$?prompt )\
803 alias module '$pre_pr $eval_cmd $pre_ex $post_pr $post_ex' ;
804 if ( ! \$?histchars && ! \$?prompt ) alias module '$eval_cmd' ;"
805 if {[getConf ml]} {
806 append fdef {
807 alias ml 'module ml \!*' ;}
808 }
809 if {$is_mogui_avail} {
810 append fdef "\nalias mogui 'eval \"`mogui-cmd [getState shell]\
811 \\!*:q`\"' ;"
812 }
813 }
814 sh {
815 # Considering the diversity of ways local variables are handled
816 # through the sh-variants ('local' known everywhere except on ksh,
817 # 'typeset' known everywhere except on pure-sh, and on some systems
818 # the pure-sh is in fact a 'ksh'), no local variables are defined and
819 # these variables that should have been local are unset at the end
820
821 # on zsh, word splitting should be enabled explicitly
822 set wsplit [expr {[getState shell] eq {zsh} ? {^^=} : {}}]
823 # build quarantine mechanism in module function
824 # an empty runtime variable is set even if no corresponding
825 # MODULES_RUNENV_* variable found, as var cannot be unset on
826 # modified environment command-line
827 set fdef "_module_raw() {"
828 if {[getConf silent_shell_debug]} {
829 append fdef {
830 unset _mlshdbg;
831 if [ "${MODULES_SILENT_SHELL_DEBUG:-0}" = '1' ]; then
832 case "$-" in
833 *v*x*) set +vx; _mlshdbg='vx' ;;
834 *v*) set +v; _mlshdbg='v' ;;
835 *x*) set +x; _mlshdbg='x' ;;
836 *) _mlshdbg='' ;;
837 esac;
838 fi;}
839 }
840 if {[getConf quarantine_support]} {
841 append fdef "
842 unset _mlre _mlIFS;
843 if \[ -n \"\${IFS+x}\" \]; then
844 _mlIFS=\$IFS;
845 fi;
846 IFS=' ';
847 for _mlv in \${${wsplit}MODULES_RUN_QUARANTINE:-}; do"
848 ##nagelfar ignore #4 Too long line
849 append fdef {
850 if [ "${_mlv}" = "${_mlv##*[!A-Za-z0-9_]}" ] && [ "${_mlv}" = "${_mlv#[0-9]}" ]; then
851 if [ -n "$(eval 'echo ${'"$_mlv"'+x}')" ]; then
852 _mlre="${_mlre:-}__MODULES_QUAR_${_mlv}='$(eval 'echo ${'"$_mlv"'}')' ";
853 fi;
854 _mlrv="MODULES_RUNENV_${_mlv}";
855 _mlre="${_mlre:-}${_mlv}='$(eval 'echo ${'"$_mlrv"':-}')' ";
856 fi;
857 done;
858 if [ -n "${_mlre:-}" ]; then
859 _mlre="${_mlre:-}__MODULES_QUARANTINE_SET=1 ";}
860 append fdef "\n eval \"\$(eval \${${wsplit}_mlre} $tclshbin\
861 '$::argv0' [getState shell] '\"\$@\"')\";
862 else
863 eval \"\$($tclshbin '$::argv0' [getState shell] \"\$@\")\";
864 fi;"
865 } else {
866 append fdef "
867 eval \"\$($tclshbin '$::argv0' [getState shell] \"\$@\")\";"
868 }
869 append fdef {
870 _mlstatus=$?;}
871 if {[getConf quarantine_support]} {
872 append fdef {
873 if [ -n "${_mlIFS+x}" ]; then
874 IFS=$_mlIFS;
875 else
876 unset IFS;
877 fi;
878 unset _mlre _mlv _mlrv _mlIFS;}
879 }
880 if {[getConf silent_shell_debug]} {
881 append fdef {
882 if [ -n "${_mlshdbg:-}" ]; then
883 set -"$_mlshdbg";
884 fi;
885 unset _mlshdbg;}
886 }
887 append fdef {
888 return $_mlstatus;}
889 # define both _module_raw and module functions in any cases to allow
890 # content redirection when command-line switch is used (--redirect or
891 # --no-redirect)
892 # content is redirected by default when shell session is attached to
893 # a terminal (non-terminal session are not redirected to avoid
894 # breaking things like scp or sftp transfer)
895 # use local/typeset variable in this context as we cannot unset it
896 # afterward (since _module_raw should be the cmd to return status)
897 set localcmd [expr {[getState shell] eq {ksh} ? {typeset} : {local}}]
898 ##nagelfar ignore Unescaped close brace
899 append fdef "\n};\nmodule() {
900 $localcmd _mlredir=[getState is_stderr_tty];\n"
901 append fdef { if [ -n "${MODULES_REDIRECT_OUTPUT+x}" ]; then
902 if [ "$MODULES_REDIRECT_OUTPUT" = '0' ]; then
903 _mlredir=0;
904 elif [ "$MODULES_REDIRECT_OUTPUT" = '1' ]; then
905 _mlredir=1;
906 fi;
907 fi;
908 case " $@ " in
909 *' --no-redirect '*) _mlredir=0 ;;
910 *' --redirect '*) _mlredir=1 ;;
911 esac;
912 if [ $_mlredir -eq 0 ]; then
913 _module_raw "$@";
914 else
915 _module_raw "$@" 2>&1;
916 fi;}
917 append fdef "\n};"
918 if {[getState shell] eq {bash}} {
919 append fdef {
920 export -f _module_raw;
921 export -f module;}
922 }
923 if {[getConf ml]} {
924 append fdef {
925 ml() { module ml "$@"; };}
926 if {[getState shell] eq {bash}} {
927 append fdef {
928 export -f ml;}
929 }
930 }
931 if {$is_mogui_avail} {
932 append fdef "\nmogui() { eval \"\$(mogui-cmd [getState shell]\
933 \"\$@\")\"; };"
934 if {[getState shell] eq {bash}} {
935 append fdef {
936 export -f mogui;}
937 }
938 }
939 }
940 fish {
941 set fdef "function _module_raw\n"
942 if {[getConf quarantine_support]} {
943 append fdef { set -l _mlre ''; set -l _mlv; set -l _mlrv;
944 for _mlv in (string split ' ' $MODULES_RUN_QUARANTINE)
945 if string match -r '^[A-Za-z_][A-Za-z0-9_]*$' $_mlv >/dev/null
946 if set -q $_mlv
947 set _mlre $_mlre"__MODULES_QUAR_"$_mlv"='$$_mlv' "
948 end
949 set _mlrv "MODULES_RUNENV_$_mlv"
950 set _mlre "$_mlre$_mlv='$$_mlrv' "
951 end
952 end
953 if [ -n "$_mlre" ]
954 set _mlre "env $_mlre __MODULES_QUARANTINE_SET=1"
955 end}
956 # use "| source -" rather than "eval" to be able
957 # to redirect stderr after stdout being evaluated
958 append fdef "\n eval \$_mlre $tclshbin \\'$::argv0\\' [getState\
959 shell] (string escape -- \$argv) | source -\n"
960 } else {
961 append fdef " eval $tclshbin \\'$::argv0\\' [getState shell]\
962 (string escape -- \$argv) | source -\n"
963 }
964 append fdef "end
965 function module
966 set _mlredir [getState is_stderr_tty]
967 if set -q MODULES_REDIRECT_OUTPUT
968 if \[ \"\$MODULES_REDIRECT_OUTPUT\" = '0' \]
969 set _mlredir 0
970 else if \[ \"\$MODULES_REDIRECT_OUTPUT\" = '1' \]
971 set _mlredir 1
972 end
973 end
974 if contains -- --no-redirect \$argv; or begin ; \[ \$_mlredir -eq 0\
975 \]; and not contains -- --redirect \$argv ; end
976 _module_raw \$argv
977 else
978 _module_raw \$argv 2>&1
979 end
980 end"
981 if {[getConf ml]} {
982 append fdef {
983 function ml
984 module ml $argv
985 end}
986 }
987 if {$is_mogui_avail} {
988 append fdef {
989 function mogui
990 eval mogui-cmd fish (string escape -- $argv) | source -
991 end}
992 }
993 }
994 tcl {
995 set fdef "proc module {args} {"
996 if {[getConf quarantine_support]} {
997 append fdef {
998 set _mlre {};
999 if {[info exists ::env(MODULES_RUN_QUARANTINE)]} {
1000 foreach _mlv [split $::env(MODULES_RUN_QUARANTINE) " "] {
1001 if {[regexp {^[A-Za-z_][A-Za-z0-9_]*$} $_mlv]} {
1002 if {[info exists ::env($_mlv)]} {
1003 lappend _mlre "__MODULES_QUAR_${_mlv}=$::env($_mlv)"
1004 }
1005 set _mlrv "MODULES_RUNENV_${_mlv}"
1006 lappend _mlre [expr {[info exists ::env($_mlrv)] ?\
1007 "${_mlv}=$::env($_mlrv)" : "${_mlv}="}]
1008 }
1009 }
1010 if {[llength $_mlre]} {
1011 lappend _mlre "__MODULES_QUARANTINE_SET=1"
1012 set _mlre [linsert $_mlre 0 "env"]
1013 }
1014 }}
1015 }
1016 append fdef {
1017 set _mlstatus 1;}
1018 if {[getConf quarantine_support]} {
1019 append fdef "\n catch {exec {*}\$_mlre \"$tclshbin\"\
1020 \"$::argv0\" \"[getState shell]\" {*}\$args 2>@stderr}\
1021 script\n"
1022 } else {
1023 append fdef "\n catch {exec \"$tclshbin\" \"$::argv0\"\
1024 \"[getState shell]\" {*}\$args 2>@stderr} script\n"
1025 }
1026 append fdef { eval $script;
1027 return $_mlstatus}
1028 append fdef "\n}"
1029 if {[getConf ml]} {
1030 append fdef {
1031 proc ml {args} {
1032 return [module ml {*}$args]
1033 }}
1034 }
1035 }
1036 cmd {
1037 reportErrorAndExit {No autoinit mode available for 'cmd' shell}
1038 }
1039 perl {
1040 set fdef "sub module {"
1041 if {[getConf quarantine_support]} {
1042 append fdef {
1043 my $_mlre = '';
1044 if (defined $ENV{'MODULES_RUN_QUARANTINE'}) {
1045 foreach my $_mlv (split(' ', $ENV{'MODULES_RUN_QUARANTINE'})) {
1046 if ($_mlv =~ /^[A-Za-z_][A-Za-z0-9_]*$/) {
1047 if (defined $ENV{$_mlv}) {
1048 $_mlre .= "__MODULES_QUAR_${_mlv}='$ENV{$_mlv}' ";
1049 }
1050 my $_mlrv = "MODULES_RUNENV_$_mlv";
1051 $_mlre .= "$_mlv='$ENV{$_mlrv}' ";
1052 }
1053 }
1054 if ($_mlre ne "") {
1055 $_mlre = "env ${_mlre}__MODULES_QUARANTINE_SET=1 ";
1056 }
1057 }}
1058 }
1059 append fdef {
1060 my $args = '';
1061 if (@_ > 0) {
1062 $args = '"' . join('" "', @_) . '"';
1063 }
1064 my $_mlstatus = 1;}
1065 if {[getConf quarantine_support]} {
1066 append fdef "\n eval `\${_mlre}$tclshbin '$::argv0' perl\
1067 \$args`;\n"
1068 } else {
1069 append fdef "\n eval `$tclshbin '$::argv0' perl \$args`;\n"
1070 }
1071 append fdef { return $_mlstatus;}
1072 append fdef "\n}"
1073 if {[getConf ml]} {
1074 append fdef {
1075 sub ml {
1076 return module('ml', @_);
1077 }}
1078 }
1079 }
1080 python {
1081 set fdef {import sys, re, subprocess
1082 def module(*arguments):}
1083 if {[getConf quarantine_support]} {
1084 append fdef {
1085 _mlre = os.environ.copy()
1086 if 'MODULES_RUN_QUARANTINE' in os.environ:
1087 for _mlv in os.environ['MODULES_RUN_QUARANTINE'].split():
1088 if re.match('^[A-Za-z_][A-Za-z0-9_]*$', _mlv):
1089 if _mlv in os.environ:
1090 _mlre['__MODULES_QUAR_' + _mlv] = os.environ[_mlv]
1091 _mlrv = 'MODULES_RUNENV_' + _mlv
1092 if _mlrv in os.environ:
1093 _mlre[_mlv] = os.environ[_mlrv]
1094 else:
1095 _mlre[_mlv] = ''
1096 _mlre['__MODULES_QUARANTINE_SET'] = '1'}
1097 }
1098 append fdef {
1099 ns = {}}
1100 if {[getConf quarantine_support]} {
1101 append fdef "\n out, err = subprocess.Popen(\['$tclshbin',\
1102 '$::argv0', 'python'\] + list(arguments),\
1103 stdout=subprocess.PIPE, stderr=subprocess.PIPE,\
1104 env=_mlre).communicate()\n"
1105 } else {
1106 append fdef "\n out, err = subprocess.Popen(\['$tclshbin',\
1107 '$::argv0', 'python'\] + list(arguments),\
1108 stdout=subprocess.PIPE,\
1109 stderr=subprocess.PIPE).communicate()\n"
1110 }
1111 append fdef { exec(out, ns)
1112 if '_mlstatus' in ns:
1113 _mlstatus = ns['_mlstatus']
1114 else:
1115 _mlstatus = True
1116 if err.decode():
1117 print(err.decode(), end='', file=sys.stderr)
1118 return _mlstatus}
1119 if {[getConf ml]} {
1120 append fdef {
1121 def ml(*arguments):
1122 return module('ml', *arguments)
1123 }
1124 }
1125 }
1126 ruby {
1127 set fdef {class ENVModule
1128 def ENVModule.module(*args)}
1129 if {[getConf quarantine_support]} {
1130 ##nagelfar ignore +7 Too long line
1131 append fdef {
1132 _mlre = ''
1133 if ENV.has_key?('MODULES_RUN_QUARANTINE') then
1134 ENV['MODULES_RUN_QUARANTINE'].split(' ').each do |_mlv|
1135 if _mlv =~ /^[A-Za-z_][A-Za-z0-9_]*$/ then
1136 if ENV.has_key?(_mlv) then
1137 _mlre << "__MODULES_QUAR_" + _mlv + "='" + ENV[_mlv].to_s + "' "
1138 end
1139 _mlrv = 'MODULES_RUNENV_' + _mlv
1140 _mlre << _mlv + "='" + ENV[_mlrv].to_s + "' "
1141 end
1142 end
1143 unless _mlre.empty?
1144 _mlre = 'env ' + _mlre + '__MODULES_QUARANTINE_SET=1 '
1145 end
1146 end}
1147 }
1148 append fdef {
1149 if args[0].kind_of?(Array) then
1150 args = args[0]
1151 end
1152 if args.length == 0 then
1153 args = ''
1154 else
1155 args = "\"#{args.join('" "')}\""
1156 end
1157 _mlstatus = true}
1158 if {[getConf quarantine_support]} {
1159 ##nagelfar ignore +2 Suspicious # char
1160 append fdef "\n eval `#{_mlre}$tclshbin '$::argv0' ruby\
1161 #{args}`\n"
1162 } else {
1163 append fdef "\n eval `$tclshbin '$::argv0' ruby #{args}`\n"
1164 }
1165 append fdef { return _mlstatus
1166 end}
1167 if {[getConf ml]} {
1168 append fdef {
1169 def ENVModule.ml(*args)
1170 return ENVModule.module('ml', *args)
1171 end}
1172 }
1173 append fdef {
1174 end}
1175 }
1176 lisp {
1177 reportErrorAndExit {lisp mode autoinit not yet implemented}
1178 }
1179 cmake {
1180 if {[getConf quarantine_support]} {
1181 set pre_exec "\n execute_process(COMMAND \${_mlre} $tclshbin\
1182 \"$::argv0\" cmake "
1183 } else {
1184 set pre_exec "\n execute_process(COMMAND $tclshbin\
1185 \"$::argv0\" cmake "
1186 }
1187 set post_exec "\n OUTPUT_FILE \${tempfile_name})\n"
1188 set fdef {function(module)
1189 cmake_policy(SET CMP0007 NEW)}
1190 if {[getConf quarantine_support]} {
1191 append fdef {
1192 set(_mlre "")
1193 if(DEFINED ENV{MODULES_RUN_QUARANTINE})
1194 string(REPLACE " " ";" _mlv_list "$ENV{MODULES_RUN_QUARANTINE}")
1195 foreach(_mlv ${_mlv_list})
1196 if(${_mlv} MATCHES "^[A-Za-z_][A-Za-z0-9_]*$")
1197 if(DEFINED ENV{${_mlv}})
1198 set(_mlre "${_mlre}__MODULES_QUAR_${_mlv}=$ENV{${_mlv}};")
1199 endif()
1200 set(_mlrv "MODULES_RUNENV_${_mlv}")
1201 set(_mlre "${_mlre}${_mlv}=$ENV{${_mlrv}};")
1202 endif()
1203 endforeach()
1204 if (NOT "${_mlre}" STREQUAL "")
1205 set(_mlre "env;${_mlre}__MODULES_QUARANTINE_SET=1;")
1206 endif()
1207 endif()}
1208 }
1209 append fdef {
1210 set(_mlstatus TRUE)
1211 execute_process(COMMAND mktemp -t moduleinit.cmake.XXXXXXXXXXXX
1212 OUTPUT_VARIABLE tempfile_name
1213 OUTPUT_STRIP_TRAILING_WHITESPACE)
1214 if(${ARGC} EQUAL 1)}
1215 # adapt command definition depending on the number of args to be
1216 # able to pass to some extend (<5 args) empty string element to
1217 # modulecmd (no other way as empty element in ${ARGV} are skipped
1218 append fdef "$pre_exec\"\${ARGV0}\"$post_exec"
1219 append fdef { elseif(${ARGC} EQUAL 2)}
1220 append fdef "$pre_exec\"\${ARGV0}\" \"\${ARGV1}\"$post_exec"
1221 append fdef { elseif(${ARGC} EQUAL 3)}
1222 append fdef "$pre_exec\"\${ARGV0}\" \"\${ARGV1}\"\
1223 \"\${ARGV2}\"$post_exec"
1224 append fdef { elseif(${ARGC} EQUAL 4)}
1225 append fdef "$pre_exec\"\${ARGV0}\" \"\${ARGV1}\"\
1226 \"\${ARGV2}\" \"\${ARGV3}\"$post_exec"
1227 append fdef { else()}
1228 append fdef "$pre_exec\${ARGV}$post_exec"
1229 append fdef { endif()
1230 if(EXISTS ${tempfile_name})
1231 include(${tempfile_name})
1232 file(REMOVE ${tempfile_name})
1233 endif()
1234 set(module_result ${_mlstatus} PARENT_SCOPE)
1235 endfunction(module)}
1236 if {[getConf ml]} {
1237 append fdef {
1238 function(ml)
1239 module(ml ${ARGV})
1240 set(module_result ${module_result} PARENT_SCOPE)
1241 endfunction(ml)}
1242 }
1243 }
1244 r {
1245 set fdef "module <- function(...){"
1246 if {[getConf quarantine_support]} {
1247 ##nagelfar ignore +7 Too long line
1248 append fdef {
1249 mlre <- ''
1250 if (!is.na(Sys.getenv('MODULES_RUN_QUARANTINE', unset=NA))) {
1251 for (mlv in strsplit(Sys.getenv('MODULES_RUN_QUARANTINE'), ' ')[[1]]) {
1252 if (grepl('^[A-Za-z_][A-Za-z0-9_]*$', mlv)) {
1253 if (!is.na(Sys.getenv(mlv, unset=NA))) {
1254 mlre <- paste0(mlre, "__MODULES_QUAR_", mlv, "='", Sys.getenv(mlv), "' ")
1255 }
1256 mlrv <- paste0('MODULES_RUNENV_', mlv)
1257 mlre <- paste0(mlre, mlv, "='", Sys.getenv(mlrv), "' ")
1258 }
1259 }
1260 if (mlre != '') {
1261 mlre <- paste0('env ', mlre, '__MODULES_QUARANTINE_SET=1 ')
1262 }
1263 }}
1264 }
1265 append fdef {
1266 arglist <- as.list(match.call())
1267 arglist[1] <- 'r'
1268 args <- paste0('"', paste0(arglist, collapse='" "'), '"')}
1269 if {[getConf quarantine_support]} {
1270 append fdef "\n cmd <- paste(mlre, '$tclshbin', '\"$::argv0\"',\
1271 args, sep=' ')\n"
1272 } else {
1273 append fdef "\n cmd <- paste('$tclshbin', '\"$::argv0\"', args,\
1274 sep=' ')\n"
1275 }
1276 append fdef { mlstatus <- TRUE
1277 hndl <- pipe(cmd)
1278 eval(expr = parse(file=hndl))
1279 close(hndl)
1280 invisible(mlstatus)}
1281 append fdef "\n}"
1282 if {[getConf ml]} {
1283 append fdef {
1284 ml <- function(...){
1285 module('ml', ...)
1286 }}
1287 }
1288 }
1289 pwsh {
1290 if {[getState is_win]} {
1291 reportErrorAndExit {No autoinit mode available for 'pwsh' shell\
1292 on Windows}
1293 }
1294
1295 set fdef "function global:_envmodule_common {"
1296 append fdef {
1297 param([Parameter(ValueFromRemainingArguments)] [string[]] $allargs)
1298 $global:_mlstatus = $true
1299 $cmd_is_query = "$allargs" -match '(is-loaded|is-avail|is-used|is-saved)'}
1300 if {[getConf quarantine_support]} {
1301 append fdef {
1302 $_mlre_set = ''
1303 $_mlre_reset = ''
1304 if ($env:MODULES_RUN_QUARANTINE) {
1305 foreach ($_mlv in $env:MODULES_RUN_QUARANTINE -split ' ') {
1306 if ($_mlv -match '^[A-Za-z_][A-Za-z0-9_]*$') {
1307 $_mlv_value = [System.Environment]::GetEnvironmentVariable($_mlv)
1308 if ($_mlv_value) {
1309 $_mlre_set += "`$env:__MODULES_QUAR_${_mlv}='$_mlv_value';"
1310 $_mlre_reset = "Remove-Item -Path env:__MODULES_QUAR_${_mlv};"\
1311 + $_mlre_reset
1312 }
1313 $_mlrv = "MODULES_RUNENV_$_mlv"
1314 $_mlrv_value =\
1315 [System.Environment]::GetEnvironmentVariable($_mlrv)
1316 $_mlre_set += "`$env:$_mlv='$_mlrv_value';"
1317 $_mlre_reset = "`$env:$_mlv=`"`$env:__MODULES_QUAR_${_mlv}`";"\
1318 + $_mlre_reset
1319 }
1320 }
1321 if ($_mlre_set -ne '') {
1322 $_mlre_set += "`$env:__MODULES_QUARANTINE_SET=1"
1323 $_mlre_reset += "Remove-Item -Path env:__MODULES_QUARANTINE_SET"
1324 }
1325 }
1326 if ($_mlre_set -ne '') {
1327 Invoke-Expression "$_mlre_set"
1328 Remove-Variable "_mlre_set"
1329 }}
1330 }
1331 append fdef {
1332 $output = }
1333 append fdef "& \"$tclshbin\" \"$::argv0\" pwsh \$allargs 2>&1"
1334 if {[getConf quarantine_support]} {
1335 append fdef {
1336 if ($_mlre_reset -ne '') {
1337 Invoke-Expression "$_mlre_reset"
1338 Remove-Variable "_mlre_reset"
1339 }}
1340 }
1341 append fdef {
1342 $outmsg = ($output | ? {$_.gettype().Name -ne "ErrorRecord"}) -join "`n"
1343 $errmsg = ($output | ? {$_.gettype().Name -eq "ErrorRecord"}) -join "`n"
1344 $errmsg = $errmsg.replace(
1345 "System.Management.Automation.RemoteException", ""
1346 )
1347 if ($outmsg) {
1348 Invoke-Expression $outmsg
1349 }
1350 if (($_mlstatus -eq $false) -and (!$cmd_is_query)) {
1351 $global:LastExitCode = 1
1352 } else {
1353 $global:LastExitCode = 0
1354 }}
1355 append fdef "\n \$mlredir = \[bool\][getState is_stderr_tty]"
1356 append fdef {
1357 if ($env:MODULES_REDIRECT_OUTPUT -ne $null) {
1358 if ($env:MODULES_REDIRECT_OUTPUT -eq '0') {
1359 $mlredir = $false
1360 } elseif ($env:MODULES_REDIRECT_OUTPUT -eq '1') {
1361 $mlredir = $true
1362 }
1363 }
1364 if ("$allargs" -match '--no-redirect') {
1365 $mlredir = $false
1366 } elseif ("$allargs" -match '--redirect') {
1367 $mlredir = $true
1368 }
1369 if ($errmsg) {
1370 if ($_mlstatus -eq $false) {
1371 $global:LastExitCode = 1
1372 } else {
1373 if ($mlredir) {
1374 [Console]::WriteLine("$errmsg")
1375 } else {
1376 [Console]::Error.WriteLine("$errmsg")
1377 }
1378 $errmsg = $null
1379 }
1380 }
1381 if ((($_mlstatus -ne $true) -and ($_mlstatus -ne $false)) -or\
1382 ($cmd_is_query)) {
1383 return $errmsg,$_mlstatus
1384 }
1385 return $errmsg,$null}
1386 append fdef "\n}\nfunction global:envmodule {"
1387 append fdef {
1388 param([Parameter(ValueFromRemainingArguments)] [string[]] $allargs)
1389 $errmsg,$retmsg = & _envmodule_common $allargs
1390 if ("$errmsg" -ne "") {
1391 $PSCmdlet.ThrowTerminatingError(
1392 [System.Management.Automation.ErrorRecord]::new(
1393 ([System.Exception]"$errmsg"),
1394 'error while running modulecmd.tcl via envmodule',
1395 [System.Management.Automation.ErrorCategory]::FromStdErr,
1396 $null
1397 ))
1398 }
1399 if ("$retmsg" -ne "") {
1400 return $retmsg
1401 }}
1402 append fdef "\n}"
1403 if {[getConf ml]} {
1404 append fdef "\nfunction global:ml {"
1405 append fdef {
1406 param([Parameter(ValueFromRemainingArguments)] [string[]] $allargs)
1407 $mlargs = @('ml')
1408 if ($allargs.Length) {
1409 $mlargs += $allargs
1410 }
1411 $errmsg,$retmsg = _envmodule_common $mlargs
1412 if ("$errmsg" -ne "") {
1413 $PSCmdlet.ThrowTerminatingError(
1414 [System.Management.Automation.ErrorRecord]::new(
1415 ([System.Exception]"$errmsg"),
1416 'error while running modulecmd.tcl via ml',
1417 [System.Management.Automation.ErrorCategory]::FromStdErr,
1418 $null
1419 ))
1420 }
1421 if ("$retmsg" -ne "") {
1422 return $retmsg
1423 }}
1424 append fdef "\n}"
1425 }
1426 }
1427 }
1428
1429 # output function definition
1430 lappend ::g_shcode_out $fdef
1431 }
1432
1433 proc get-env {var {valifunset {}}} {
1434 if {[isEnvVarDefined $var] && ![isEnvVarCleared $var]} {
1435 return $::env($var)
1436 } else {
1437 return $valifunset
1438 }
1439 }
1440
1441 proc envVarEquals {var_name value} {
1442 return [expr {[isEnvVarDefined $var_name] && [get-env $var_name]\
1443 eq $value}]
1444 }
1445
1446 proc isEnvVarDefined {var_name} {
1447 return [info exists ::env($var_name)]
1448 }
1449
1450 proc setEnvVarIfUndefined {var_name value} {
1451 if {![isEnvVarDefined $var_name]} {
1452 set ::env($var_name) $value
1453 }
1454 }
1455
1456 proc set-env {var val} {
1457 set mode [currentState mode]
1458 reportDebug "$var=$val"
1459
1460 if {![isEnvVarProtected $var]} {
1461 # an empty string value means unsetting variable on Windows platform, so
1462 # call unset-env to ensure variable will not be seen defined yet raising
1463 # an error when trying to access it
1464 if {[getState is_win] && $val eq {}} {
1465 unset-env $var
1466 } else {
1467 interp-sync-env set $var $val
1468
1469 unsetEnvVarAsCleared $var
1470
1471 # propagate variable setup to shell environment on load and unload
1472 # mode
1473 if {$mode eq {load} || $mode eq {unload}} {
1474 set ::g_stateEnvVars($var) new
1475 }
1476 }
1477 } else {
1478 reportWarning "Modification of protected environment variable $var\
1479 ignored"
1480 }
1481 }
1482
1483 proc isEnvVarProtected {var_name} {
1484 return [expr {$var_name in [getConfList protected_envvars]}]
1485 }
1486
1487 proc reset-to-unset-env {var {val {}}} {
1488 interp-sync-env set $var $val
1489 # set var as cleared if val is empty
1490 if {$val eq {}} {
1491 setEnvVarAsCleared $var
1492 }
1493 }
1494
1495 proc setEnvVarAsCleared {var_name} {
1496 set ::g_clearedEnvVars($var_name) 1
1497 }
1498
1499 proc unsetEnvVarAsCleared {var_name} {
1500 if {[isEnvVarCleared $var_name]} {
1501 unset ::g_clearedEnvVars($var_name)
1502 }
1503 }
1504
1505 proc isEnvVarCleared {var_name} {
1506 return [info exists ::g_clearedEnvVars($var_name)]
1507 }
1508
1509 proc unset-env {var {internal 0} {val {}}} {
1510 set mode [currentState mode]
1511 reportDebug "$var (internal=$internal, val=$val)"
1512
1513 if {![isEnvVarProtected $var]} {
1514 # clear value instead of unset it not to break variable later reference
1515 # in modulefile. clear whether variable set or not to get a later usage
1516 # consistent behavior whatever env is setup
1517 if {!$internal} {
1518 reset-to-unset-env $var $val
1519 # internal variables (like ref counter var) are purely unset if they
1520 # exists
1521 } elseif {[isEnvVarDefined $var]} {
1522 interp-sync-env unset $var
1523 set intwasset 1
1524 }
1525
1526 # propagate deletion in any case if variable is public and for internal
1527 # one only if variable was set
1528 if {($mode eq {load} || $mode eq {unload}) && (!$internal ||\
1529 [info exists intwasset])} {
1530 set ::g_stateEnvVars($var) del
1531 }
1532 } else {
1533 reportWarning "Modification of protected environment variable $var\
1534 ignored"
1535 }
1536 }
1537
1538 proc getPushenvVarName {var} {
1539 return __MODULES_PUSHENV_${var}
1540 }
1541
1542 proc getModshareVarName {var} {
1543 # no modshare variable for Modules-specific path variables as each entry
1544 # should be unique (no reference counting mechanism for these variables
1545 # expect for MODULEPATH)
1546 if {$var in {_LMFILES_ LOADEDMODULES} || [string equal -length 10\
1547 __MODULES_ $var] || [string equal -length 8 MODULES_ $var]} {
1548 return {}
1549 # specific modshare variable use a Modules-specific prefix (rather suffix
1550 # that may lead to warning message for instance for DYLD-related variables)
1551 } else {
1552 return __MODULES_SHARE_${var}
1553 }
1554 }
1555
1556 # path fiddling
1557 proc getReferenceCountArray {var separator} {
1558 # get reference counter set in environment
1559 set sharevar [getModshareVarName $var]
1560 array set refcount {}
1561 if {[isEnvVarDefined $sharevar]} {
1562 set modsharelist [psplit $::env($sharevar) [getState path_separator]]
1563 # ignore environment ref count variable if malformed
1564 if {([llength $modsharelist] % 2) == 0} {
1565 array set refcount $modsharelist
1566 } else {
1567 reportDebug "Reference counter value in '$sharevar' is malformed\
1568 ($modsharelist)"
1569 }
1570 }
1571
1572 array set countarr {}
1573 if {[isEnvVarDefined $var]} {
1574 # do not skip a bare empty path entry that can also be found in
1575 # reference counter array (sometimes var is cleared by setting it
1576 # empty not unsetting it, ignore var in this case)
1577 if {$::env($var) eq {} && [info exists refcount()]} {
1578 lappend eltlist {}
1579 } else {
1580 set eltlist [split $::env($var) $separator]
1581 }
1582
1583 # just go thought the elements of the variable, which means additional
1584 # elements part of the reference counter variable will be ignored
1585 foreach elt $eltlist {
1586 # no reference counter, means value has been set once
1587 if {![info exists refcount($elt)]} {
1588 set count 1
1589 # bad reference counter value is ignored
1590 } elseif {![string is digit -strict $refcount($elt)] ||\
1591 $refcount($elt) < 1} {
1592 reportDebug "Reference counter value for '$elt' in '$sharevar' is\
1593 erroneous ($refcount($elt))"
1594 set count 1
1595 } else {
1596 set count $refcount($elt)
1597 }
1598 set countarr($elt) $count
1599 }
1600 }
1601
1602 set count_list [array get countarr]
1603 reportDebug "(var=$var, delim=$separator) got '$count_list'"
1604
1605 return $count_list
1606 }
1607
1608 proc getPathReferenceCount {var path separator {resolv 0}} {
1609 array set ref_count_arr [getReferenceCountArray $var $separator]
1610 if {[info exists ref_count_arr($path)]} {
1611 set ref_count $ref_count_arr($path)
1612 } else {
1613 set ref_count 0
1614 if {$resolv} {
1615 set matching_path [getStringFromListMatchingResString [array names\
1616 ref_count_arr] $path]
1617 if {[string length $matching_path]} {
1618 set ref_count $ref_count_arr($matching_path)
1619 }
1620 }
1621 }
1622 return $ref_count
1623 }
1624
1625 # generate reference count value to export in user environment
1626 proc setReferenceCountRecordValue {sharevar countlist} {
1627 foreach {elt refcount} $countlist {
1628 # do not export elements with a reference count of 1 as no reference
1629 # count entry means a reference count of 1
1630 # exception made for empty string which is recorded even for a single
1631 # reference count to be able to distinguish between an empty path
1632 # variable and a path variable with an empty string as unique element
1633 if {$refcount > 1 || $elt eq {}} {
1634 lappend reclist $elt $refcount
1635 }
1636 }
1637
1638 if {[info exists reclist]} {
1639 set-env $sharevar [pjoin $reclist [getState path_separator]]
1640 # unset ref count var if found set whereas no counter should be recorded
1641 } elseif {[isEnvVarDefined $sharevar]} {
1642 unset-env $sharevar 1
1643 }
1644 }
1645
1646 proc unload-path {cmd mode dflbhv args} {
1647 reportDebug "($args) cmd=$cmd, mode=$mode, dflbhv=$dflbhv"
1648
1649 lassign [parsePathCommandArgs $cmd $mode $dflbhv {*}$args] separator\
1650 allow_dup idx_val ign_refcount val_set_is_delim glob_match bhv var\
1651 path_list
1652
1653 switch -- $bhv {
1654 noop {
1655 return [list $bhv $var]
1656 }
1657 append - prepend {
1658 # set paths instead of removing them
1659 add-path unload-path load $bhv $cmd $separator $var {*}$path_list
1660 return [list $bhv $var]
1661 }
1662 }
1663
1664 # clean any previously defined pushenv stack
1665 unset-env [getPushenvVarName $var] 1
1666
1667 # no reference count management when no share variable is returned
1668 set isrefcount [expr {[set sharevar [getModshareVarName $var]] ne {}}]
1669 if {$isrefcount} {
1670 array set countarr [getReferenceCountArray $var $separator]
1671 }
1672
1673 # Don't worry about dealing with this variable if it is already scheduled
1674 # for deletion
1675 if {[info exists ::g_stateEnvVars($var)] && $::g_stateEnvVars($var) eq\
1676 {del}} {
1677 return [list $bhv $var]
1678 }
1679
1680 # save initial variable content to match index arguments
1681 set dir_list [split [get-env $var] $separator]
1682 # detect if empty env value means empty path entry
1683 if {![llength $dir_list] && [info exists countarr()]} {
1684 lappend dir_list {}
1685 }
1686
1687 # match glob pattern against existing values
1688 if {$glob_match} {
1689 set new_path_list [list]
1690 foreach glob_pattern $path_list {
1691 set pattern_match_list [lsearch -glob -all -inline $dir_list\
1692 $glob_pattern]
1693 if {[llength $pattern_match_list]} {
1694 lappend new_path_list {*}$pattern_match_list
1695 }
1696 }
1697 set path_list $new_path_list
1698 }
1699
1700 # build list of index to remove from variable
1701 set del_idx_list [list]
1702 foreach dir $path_list {
1703 # retrieve dir value if working on an index list
1704 if {$idx_val} {
1705 set idx $dir
1706 # go to next index if this one is not part of the existing range
1707 # needed to distinguish an empty value to an out-of-bound value
1708 if {$idx < 0 || $idx >= [llength $dir_list]} {
1709 continue
1710 } else {
1711 set dir [lindex $dir_list $idx]
1712 }
1713 }
1714
1715 # update reference counter array
1716 if {[info exists countarr($dir)]} {
1717 # unload value in any case if counter ignored (--ignore-refcount set)
1718 if {$ign_refcount} {
1719 set countarr($dir) 0
1720 } else {
1721 incr countarr($dir) -1
1722 }
1723 if {$countarr($dir) <= 0} {
1724 unset countarr($dir)
1725 set newcount 0
1726 } else {
1727 set newcount $countarr($dir)
1728 }
1729 } else {
1730 set newcount 0
1731 }
1732
1733 # get all entry indexes corresponding to dir
1734 set found_idx_list [lsearch -all -exact $dir_list $dir]
1735
1736 # remove all found entries
1737 if {$newcount <= 0} {
1738 # only remove passed position in --index mode
1739 if {$idx_val} {
1740 lappend del_idx_list $idx
1741 } else {
1742 lappend del_idx_list {*}$found_idx_list
1743 }
1744 # if multiple entries found remove the extra entries compared to new
1745 # reference counter
1746 } elseif {[llength $found_idx_list] > $newcount} {
1747 # only remove passed position in --index mode
1748 if {$idx_val} {
1749 lappend del_idx_list $idx
1750 } else {
1751 # delete extra entries, starting from end of the list (on a path
1752 # variable, entries at the end have less priority than those at
1753 # the start)
1754 lappend del_idx_list {*}[lrange $found_idx_list $newcount end]
1755 }
1756 }
1757 }
1758
1759 # update variable if some element need to be removed
1760 if {[llength $del_idx_list]} {
1761 set del_idx_list [lsort -integer -unique $del_idx_list]
1762 set newpath [list]
1763 set nbelem [llength $dir_list]
1764 # rebuild list of element without indexes set for deletion
1765 for {set i 0} {$i < $nbelem} {incr i} {
1766 if {$i ni $del_idx_list} {
1767 lappend newpath [lindex $dir_list $i]
1768 }
1769 }
1770 } else {
1771 set newpath $dir_list
1772 }
1773
1774 # set env variable and corresponding reference counter in any case
1775 if {![llength $newpath]} {
1776 unset-env $var
1777 } else {
1778 set-env $var [join $newpath $separator]
1779 }
1780
1781 if {$isrefcount} {
1782 setReferenceCountRecordValue $sharevar [array get countarr]
1783 }
1784 return [list $bhv $var]
1785 }
1786
1787 proc add-path {cmd mode dflbhv args} {
1788 reportDebug "($args) cmd=$cmd, mode=$mode, dflbhv=$dflbhv"
1789
1790 # may be called from unload-path where argument parsing was already done
1791 if {$cmd eq {unload-path}} {
1792 set path_list [lassign $args cmd separator var]
1793 set allow_dup 0
1794 set ign_refcount 0
1795 set val_set_is_delim 0
1796 set bhv $dflbhv
1797 } else {
1798 lassign [parsePathCommandArgs $cmd $mode $dflbhv {*}$args] separator\
1799 allow_dup idx_val ign_refcount val_set_is_delim glob_match bhv var\
1800 path_list
1801 }
1802
1803 # clean any previously defined pushenv stack
1804 unset-env [getPushenvVarName $var] 1
1805
1806 # no reference count management when no share variable is returned
1807 set isrefcount [expr {[set sharevar [getModshareVarName $var]] ne {}}]
1808 if {$isrefcount} {
1809 array set countarr [getReferenceCountArray $var $separator]
1810 }
1811
1812 if {$bhv eq {prepend}} {
1813 set path_list [lreverse $path_list]
1814 }
1815
1816 set val [get-env $var]
1817
1818 foreach dir $path_list {
1819 if {![info exists countarr($dir)] || $allow_dup} {
1820 # ignore env var set empty if no empty entry found in reference
1821 # counter array (sometimes var is cleared by setting it empty not
1822 # unsetting it)
1823 if {$val ne {} || [info exists countarr()]} {
1824 set sep [expr {$val eq $separator ? {} : $separator}]
1825 set val [expr {$bhv eq {prepend} ? "$dir$sep$val" :\
1826 "$val$sep$dir"}]
1827 } else {
1828 set val $dir
1829 }
1830 }
1831 if {[info exists countarr($dir)]} {
1832 # do not increase counter if bare separator string is added or if
1833 # reference count is ignored (--ignore-refcount set) unless if
1834 # duplicate mode is enabled (--duplicates set)
1835 if {!$val_set_is_delim && (!$ign_refcount || $allow_dup)} {
1836 incr countarr($dir)
1837 }
1838 } else {
1839 set countarr($dir) 1
1840 }
1841 }
1842
1843 set-env $var $val
1844 if {$isrefcount} {
1845 setReferenceCountRecordValue $sharevar [array get countarr]
1846 }
1847
1848 if {$var eq {MODULEPATH}} {
1849 registerCurrentModuleUse {*}$path_list
1850 }
1851
1852 return {}
1853 }
1854
1855 # analyze argument list passed to a path command to set default value or raise
1856 # error in case some attributes are missing
1857 proc parsePathCommandArgs {cmd mode dflbhv args} {
1858 # parse argument list
1859 set next_is_delim 0
1860 set next_is_ignored 0
1861 set next_is_reset 0
1862 set allow_dup 0
1863 set idx_val 0
1864 set ign_refcount 0
1865 set val_set_is_delim 0
1866 set glob_match 0
1867 set bhv $dflbhv
1868 foreach arg $args {
1869 # everything passed after variable name is considered a value
1870 if {[info exists var]} {
1871 switch -- $arg {
1872 --append-on-unload - --prepend-on-unload {
1873 if {$cmd ne {remove-path}} {
1874 knerror "invalid option '$arg' for $cmd"
1875 } elseif {$mode ne {unload}} {
1876 # ignore value set after this argument if not unloading
1877 # unless if no list is already set for load mode
1878 if {[info exists val_raw_list]} {
1879 set next_is_ignored 1
1880 }
1881 } else {
1882 set bhv [expr {$arg eq {--append-on-unload} ? {append} :\
1883 {prepend}}]
1884 # if another argument is set, current value list will be
1885 # withdrawn to start from new list set after this argument
1886 set next_is_reset 1
1887 }
1888 set bhvopt $arg
1889 }
1890 --remove-on-unload - --noop-on-unload {
1891 if {$cmd ne {remove-path}} {
1892 knerror "invalid option '$arg' for $cmd"
1893 } elseif {$mode eq {unload}} {
1894 set bhv [expr {$arg eq {--remove-on-unload} ? {remove} :\
1895 {noop}}]
1896 }
1897 set bhvopt $arg
1898 }
1899 default {
1900 if {$next_is_reset} {
1901 set next_is_reset 0
1902 set val_raw_list [list $arg]
1903 } elseif {!$next_is_ignored} {
1904 # set multiple passed values in a list
1905 lappend val_raw_list $arg
1906 }
1907 }
1908 }
1909 } else {
1910 switch -glob -- $arg {
1911 --index {
1912 if {$cmd ne {remove-path}} {
1913 reportWarning "--index option has no effect on $cmd"
1914 } else {
1915 set idx_val 1
1916 }
1917 }
1918 --duplicates {
1919 # raise warning when option is used by remove-path
1920 if {$cmd eq {remove-path}} {
1921 reportWarning "--duplicates option has no effect on $cmd"
1922 } else {
1923 set allow_dup 1
1924 }
1925 }
1926 --glob {
1927 # raise warning when option is not used by remove-path
1928 if {$cmd eq {remove-path}} {
1929 set glob_match 1
1930 } else {
1931 reportWarning "--glob option has no effect on $cmd"
1932 }
1933 }
1934 -d - -delim - --delim {
1935 set next_is_delim 1
1936 }
1937 --delim=* {
1938 set delim [string range $arg 8 end]
1939 }
1940 --ignore-refcount {
1941 set ign_refcount 1
1942 }
1943 --append-on-unload - --prepend-on-unload - --remove-on-unload -\
1944 --noop-on-unload {
1945 if {$cmd ne {remove-path}} {
1946 knerror "invalid option '$arg' for $cmd"
1947 } elseif {$mode eq {unload}} {
1948 switch -- [string range $arg 2 5] {
1949 noop { set bhv noop }
1950 remo { set bhv remove }
1951 appe { set bhv append }
1952 prep { set bhv prepend }
1953 }
1954 }
1955 set bhvopt $arg
1956 }
1957 -* {
1958 knerror "invalid option '$arg' for $cmd"
1959 }
1960 default {
1961 if {$next_is_delim} {
1962 set delim $arg
1963 set next_is_delim 0
1964 } else {
1965 set var $arg
1966 }
1967 }
1968 }
1969 }
1970 }
1971
1972 # adapt with default value or raise error if some arguments are missing
1973 if {![info exists delim]} {
1974 set delim [getState path_separator]
1975 } elseif {$delim eq {}} {
1976 knerror "$cmd should get a non-empty path delimiter"
1977 }
1978 if {![info exists var]} {
1979 knerror "$cmd should get an environment variable name"
1980 } elseif {$var eq {}} {
1981 knerror "$cmd should get a valid environment variable name"
1982 }
1983 if {![info exists val_raw_list]} {
1984 knerror "$cmd should get a value for environment variable $var"
1985 }
1986
1987 # some options cannot be mixed
1988 if {$idx_val != 0 && ([info exists bhvopt] && $bhvopt ne\
1989 {--noop-on-unload})} {
1990 knerror "--index and $bhvopt options cannot be simultaneously set"
1991 }
1992 if {$idx_val && $glob_match} {
1993 knerror "--index and --glob options cannot be simultaneously set"
1994 }
1995 if {$glob_match && ([info exists bhvopt] && $bhvopt in\
1996 {--append-on-unload --prepend-on-unload})} {
1997 knerror "--glob and $bhvopt options cannot be simultaneously set"
1998 }
1999
2000 set nb_path_eq_delim [llength [lsearch -all -exact $val_raw_list $delim]]
2001 if {$nb_path_eq_delim && $cmd eq {remove-path}} {
2002 knerror "$cmd cannot handle path equals to separator string"
2003 } elseif {$nb_path_eq_delim > 1} {
2004 knerror "$cmd cannot handle multiple paths equal to separator string"
2005 }
2006
2007 # set list of value to add
2008 set val_list [list]
2009 foreach val $val_raw_list {
2010 # check passed indexes are numbers
2011 if {$idx_val && ![string is integer -strict $val]} {
2012 knerror "$cmd should get valid number as index value"
2013 }
2014
2015 if {![string length $val]} {
2016 lappend val_list {}
2017 } elseif {$val eq $delim} {
2018 if {[llength $val_raw_list] > 1 || [get-env $var] ne {}} {
2019 lappend val_list {}
2020 # make separator string appear once in resulting value
2021 } elseif {[getPathReferenceCount $var {} $delim]} {
2022 lappend val_list {}
2023 set allow_dup 1
2024 } else {
2025 lappend val_list {} {}
2026 set val_set_is_delim 1
2027 set allow_dup 1
2028 set ign_refcount 1
2029 }
2030 } else {
2031 # split passed value with delimiter
2032 lappend val_list {*}[split $val $delim]
2033 }
2034 }
2035
2036 reportDebug "(delim=$delim, allow_dup=$allow_dup, idx_val=$idx_val,\
2037 ign_refcount=$ign_refcount, val_set_is_delim=$val_set_is_delim,\
2038 glob_match=$glob_match, bhv=$bhv, var=$var, val=$val_list,\
2039 nbval=[llength $val_list])"
2040
2041 return [list $delim $allow_dup $idx_val $ign_refcount $val_set_is_delim\
2042 $glob_match $bhv $var $val_list]
2043 }
2044
2045 # get list of Modules-specific environment variable in glob form
2046 proc getModulesEnvVarGlobList {{loaded_ctx 0}} {
2047 set envvar_glob_list [list LOADEDMODULES _LMFILES_]
2048 # only return list of variables related to module loaded context
2049 if {$loaded_ctx} {
2050 lappend envvar_glob_list __MODULES_*
2051 } else {
2052 lappend envvar_glob_list MODULE* __MODULES_* *_module*
2053 }
2054 return $envvar_glob_list
2055 }
2056
2057 # ;;; Local Variables: ***
2058 # ;;; mode:tcl ***
2059 # ;;; End: ***
2060 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent: