-
Notifications
You must be signed in to change notification settings - Fork 100
/
envmngt.tcl.in
1801 lines (1712 loc) · 59.8 KB
/
envmngt.tcl.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##########################################################################
# ENVMNGT.TCL, environment management procedures
# Copyright (C) 2002-2004 Mark Lakata
# Copyright (C) 2004-2017 Kent Mein
# Copyright (C) 2016-2024 Xavier Delaruelle
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##########################################################################
proc raiseErrorCount {} {
# hold error count raise
if {[depthState reportholdid] > 0} {
lappend ::g_holdReport([currentState reportholdid]) [list\
raiseErrorCount]
} else {
setState error_count [expr {[getState error_count] + 1}]
}
}
proc renderFalse {} {
if {[isStateDefined false_rendered]} {
reportDebug {false already rendered}
# no shell code to render false if shell not set
} elseif {[isStateDefined shell]} {
# setup flag to render only once
setState false_rendered 1
# render a false value most of the time through a variable assignment
# that will be looked at in the shell module function calling
# modulecmd.tcl to return in turns a boolean status. Except for python
# and cmake, the value assigned to variable is also returned as the
# entire rendering status
switch -- [getState shelltype] {
sh - csh - fish {
# no need to set a variable on real shells as last statement
# result can easily be checked
lappend ::g_shcode_out {test 0 = 1;}
}
tcl {
lappend ::g_shcode_out {set _mlstatus 0;}
}
cmd {
lappend ::g_shcode_out {set errorlevel=1}
}
perl {
lappend ::g_shcode_out {{ no strict 'vars'; $_mlstatus = 0; }}
}
python {
lappend ::g_shcode_out {_mlstatus = False}
}
ruby {
lappend ::g_shcode_out {_mlstatus = false}
}
lisp {
lappend ::g_shcode_out {nil}
}
cmake {
lappend ::g_shcode_out {set(_mlstatus FALSE)}
}
r {
lappend ::g_shcode_out {mlstatus <- FALSE}
}
}
}
}
proc renderTrue {} {
# render a true value most of the time through a variable assignment that
# will be looked at in the shell module function calling modulecmd.tcl to
# return in turns a boolean status. Except for python and cmake, the
# value assigned to variable is also returned as the full rendering status
switch -- [getState shelltype] {
sh - csh - fish {
# no need to set a variable on real shells as last statement
# result can easily be checked
lappend ::g_shcode_out {test 0;}
}
tcl {
lappend ::g_shcode_out {set _mlstatus 1;}
}
cmd {
lappend ::g_shcode_out {set errorlevel=0}
}
perl {
lappend ::g_shcode_out {{ no strict 'vars'; $_mlstatus = 1; }}
}
python {
lappend ::g_shcode_out {_mlstatus = True}
}
ruby {
lappend ::g_shcode_out {_mlstatus = true}
}
lisp {
lappend ::g_shcode_out {t}
}
cmake {
lappend ::g_shcode_out {set(_mlstatus TRUE)}
}
r {
lappend ::g_shcode_out {mlstatus <- TRUE}
}
}
}
proc renderText {text} {
# render a text value most of the time through a variable assignment that
# will be looked at in the shell module function calling modulecmd.tcl to
# return in turns a string value.
switch -- [getState shelltype] {
sh - csh - fish {
foreach word $text {
# no need to set a variable on real shells, echoing text will make
# it available as result
lappend ::g_shcode_out "echo '$word';"
}
}
tcl {
lappend ::g_shcode_out "set _mlstatus \"$text\";"
}
cmd {
foreach word $text {
lappend ::g_shcode_out "echo $word"
}
}
perl {
lappend ::g_shcode_out "{ no strict 'vars'; \$_mlstatus = '$text'; }"
}
python {
lappend ::g_shcode_out "_mlstatus = '$text'"
}
ruby {
lappend ::g_shcode_out "_mlstatus = '$text'"
}
lisp {
lappend ::g_shcode_out "(message \"$text\")"
}
cmake {
lappend ::g_shcode_out "set(_mlstatus \"$text\")"
}
r {
lappend ::g_shcode_out "mlstatus <- '$text'"
}
}
}
proc renderSettings {} {
global g_stateEnvVars g_stateAliases g_stateFunctions g_stateCompletes\
g_newXResources g_delXResources g_shcode_out
# preliminaries if there is stuff to render
if {[getState autoinit] || [array size g_stateEnvVars] || [array size\
g_stateAliases] || [array size g_newXResources] || [array size\
g_stateFunctions] || [array size g_stateCompletes] || [array size\
g_delXResources] || [info exists ::g_changeDir] || [info exists\
::g_stdoutPuts] || [info exists ::g_prestdoutPuts] || [info exists\
::g_return_text]} {
switch -- [getState shelltype] {
python {
lappend g_shcode_out {import os}
}
}
set has_rendered 1
} else {
set has_rendered 0
}
# send pre content deferred during modulefile interpretation
if {[info exists ::g_prestdoutPuts]} {
foreach {newline msg} $::g_prestdoutPuts {
append outmsg $msg
if {$newline} {
lappend g_shcode_out $outmsg
unset outmsg
}
}
# add last remaining message
if {[info exists outmsg]} {
lappend g_shcode_out $outmsg
}
}
if {[getState autoinit]} {
renderAutoinit
}
# filter Modules-specific environment variables for mod-to-sh subcmd
if {[getState modtosh_real_shell] ne {}} {
foreach globvar [getModulesEnvVarGlobList 1] {
foreach var [array names g_stateEnvVars -glob $globvar] {
unset g_stateEnvVars($var)
}
}
}
# new environment variables
foreach var [array names g_stateEnvVars] {
switch -- $g_stateEnvVars($var) {
new {
switch -- [getState shelltype] {
csh {
set val [charEscaped $::env($var)]
# csh barfs on long env vars
if {[getState shell] eq {csh} && [string length $val] >\
[getConf csh_limit]} {
if {$var eq {PATH}} {
reportWarning "PATH exceeds [getConf csh_limit]\
characters, truncating and appending\
/usr/bin:/bin ..."
set val [string range $val 0 [getConf\
csh_limit]-1]:/usr/bin:/bin
} else {
reportWarning "$var exceeds [getConf csh_limit]\
characters, truncating..."
set val [string range $val 0 [getConf csh_limit]-1]
}
}
lappend g_shcode_out "setenv $var $val;"
}
sh {
lappend g_shcode_out "$var=[charEscaped $::env($var)];\
export $var;"
}
fish {
set val [charEscaped $::env($var)]
# fish shell has special treatment for PATH variable
# so its value should be provided as a list separated
# by spaces not by semi-colons
if {$var eq {PATH}} {
regsub -all : $val { } val
}
lappend g_shcode_out "set -xg $var $val;"
}
tcl {
set val $::env($var)
lappend g_shcode_out "set ::env($var) {$val};"
}
cmd {
set val $::env($var)
lappend g_shcode_out "set $var=$val"
}
perl {
set val [charEscaped $::env($var) \']
lappend g_shcode_out "\$ENV{'$var'} = '$val';"
}
python {
set val [charEscaped $::env($var) \']
lappend g_shcode_out "os.environ\['$var'\] = '$val'"
}
ruby {
set val [charEscaped $::env($var) \']
lappend g_shcode_out "ENV\['$var'\] = '$val'"
}
lisp {
set val [charEscaped $::env($var) \"]
lappend g_shcode_out "(setenv \"$var\" \"$val\")"
}
cmake {
set val [charEscaped $::env($var) \"]
lappend g_shcode_out "set(ENV{$var} \"$val\")"
}
r {
set val [charEscaped $::env($var) {\\'}]
lappend g_shcode_out "Sys.setenv('$var'='$val')"
}
}
}
del {
switch -- [getState shelltype] {
csh {
lappend g_shcode_out "unsetenv $var;"
}
sh {
lappend g_shcode_out "unset $var;"
}
fish {
lappend g_shcode_out "set -e $var;"
}
tcl {
lappend g_shcode_out "catch {unset ::env($var)};"
}
cmd {
lappend g_shcode_out "set $var="
}
perl {
lappend g_shcode_out "delete \$ENV{'$var'};"
}
python {
lappend g_shcode_out "os.environ\['$var'\] = ''"
lappend g_shcode_out "del os.environ\['$var'\]"
}
ruby {
lappend g_shcode_out "ENV\['$var'\] = nil"
}
lisp {
lappend g_shcode_out "(setenv \"$var\" nil)"
}
cmake {
lappend g_shcode_out "unset(ENV{$var})"
}
r {
lappend g_shcode_out "Sys.unsetenv('$var')"
}
}
}
}
}
foreach var [array names g_stateAliases] {
switch -- $g_stateAliases($var) {
new {
set val $::g_Aliases($var)
# convert $n in !!:n and $* in !* on csh (like on compat version)
if {[getState shelltype] eq {csh}} {
regsub -all {([^\\]|^)\$([0-9]+)} $val {\1!!:\2} val
regsub -all {([^\\]|^)\$\*} $val {\1!*} val
}
# unescape \$ after now csh-specific conversion is over
regsub -all {\\\$} $val {$} val
switch -- [getState shelltype] {
csh {
set val [charEscaped $val]
lappend g_shcode_out "alias $var $val;"
}
sh {
set val [charEscaped $val]
lappend g_shcode_out "alias $var=$val;"
}
fish {
set val [charEscaped $val]
lappend g_shcode_out "alias $var $val;"
}
cmd {
lappend g_shcode_out "doskey $var=$val"
}
}
}
del {
switch -- [getState shelltype] {
csh {
lappend g_shcode_out "unalias $var;"
}
sh {
lappend g_shcode_out "unalias $var;"
}
fish {
lappend g_shcode_out "functions -e $var;"
}
cmd {
lappend g_shcode_out "doskey $var="
}
}
}
}
}
foreach funcname [array names g_stateFunctions] {
switch -- $g_stateFunctions($funcname) {
new {
# trim function body to smoothly add a finishing ;
set val [string trim $::g_Functions($funcname) "; \t\n\r"]
switch -- [getState shell] {
sh - ksh - zsh {
lappend g_shcode_out "$funcname () { $val; };"
}
bash {
lappend g_shcode_out "$funcname () { $val; }; export -f\
$funcname;"
}
fish {
lappend g_shcode_out "function $funcname; $val; end;"
}
}
}
del {
switch -- [getState shelltype] {
sh {
lappend g_shcode_out "unset -f $funcname;"
}
fish {
lappend g_shcode_out "functions -e $funcname;"
}
}
}
}
}
foreach compname [array names g_stateCompletes] {
switch -- $g_stateCompletes($compname) {
new {
foreach {compshell body} $::g_Completes($compname) {
# skip definition not made for current shell
if {$compshell eq [getState shell]} {
switch -- [getState shell] {
bash {
lappend g_shcode_out "complete $body $compname;"
}
tcsh {
lappend g_shcode_out "complete $compname $body;"
}
fish {
# ensure pre-existing fish completion is cleared
if {![info exists fishcompclear($compname)]} {
lappend g_shcode_out "complete -e -c $compname;"
set fishcompclear($compname) 1
}
lappend g_shcode_out "complete -c $compname $body;"
}
}
}
}
}
del {
switch -- [getState shell] {
bash {
lappend g_shcode_out "complete -r $compname;"
}
tcsh {
lappend g_shcode_out "uncomplete $compname;"
}
fish {
lappend g_shcode_out "complete -e -c $compname;"
}
}
}
}
}
# preliminaries for x-resources stuff
if {[array size g_newXResources] || [array size g_delXResources]} {
switch -- [getState shelltype] {
python {
lappend g_shcode_out {import subprocess}
}
ruby {
lappend g_shcode_out {require 'open3'}
}
}
}
# new x resources
if {[array size g_newXResources]} {
# xrdb executable has already be verified in x-resource
set xrdb [getCommandPath xrdb]
foreach var [array names g_newXResources] {
set val $g_newXResources($var)
# empty val means that var is a file to parse
if {$val eq {}} {
switch -- [getState shelltype] {
sh - csh - fish {
lappend g_shcode_out "$xrdb -merge $var;"
}
tcl {
lappend g_shcode_out "exec $xrdb -merge $var;"
}
perl {
lappend g_shcode_out "system(\"$xrdb -merge $var\");"
}
python {
set var [charEscaped $var \']
lappend g_shcode_out "subprocess.Popen(\['$xrdb',\
'-merge', '$var'\])"
}
ruby {
set var [charEscaped $var \']
lappend g_shcode_out "Open3.popen2('$xrdb -merge $var')"
}
lisp {
lappend g_shcode_out "(shell-command-to-string \"$xrdb\
-merge $var\")"
}
cmake {
lappend g_shcode_out "execute_process(COMMAND $xrdb -merge\
$var)"
}
r {
set var [charEscaped $var {\\'}]
lappend g_shcode_out "system('$xrdb -merge $var')"
}
}
} else {
switch -- [getState shelltype] {
sh - csh - fish {
set var [charEscaped $var \"]
set val [charEscaped $val \"]
lappend g_shcode_out "echo \"$var: $val\" | $xrdb -merge;"
}
tcl {
lappend g_shcode_out "set XRDBPIPE \[open \"|$xrdb -merge\"\
r+\];"
set var [charEscaped $var \"]
set val [charEscaped $val \"]
lappend g_shcode_out "puts \$XRDBPIPE \"$var: $val\";"
lappend g_shcode_out {close $XRDBPIPE;}
lappend g_shcode_out {unset XRDBPIPE;}
}
perl {
lappend g_shcode_out "open(XRDBPIPE, \"|$xrdb -merge\");"
set var [charEscaped $var \"]
set val [charEscaped $val \"]
lappend g_shcode_out "print XRDBPIPE \"$var: $val\\n\";"
lappend g_shcode_out {close XRDBPIPE;}
}
python {
set var [charEscaped $var \']
set val [charEscaped $val \']
lappend g_shcode_out "subprocess.Popen(\['$xrdb',\
'-merge'\],\
stdin=subprocess.PIPE).communicate(input='$var:\
$val\\n')"
}
ruby {
set var [charEscaped $var \']
set val [charEscaped $val \']
lappend g_shcode_out "Open3.popen2('$xrdb -merge') {|i,o,t|\
i.puts '$var: $val'}"
}
lisp {
lappend g_shcode_out "(shell-command-to-string \"echo $var:\
$val | $xrdb -merge\")"
}
cmake {
set var [charEscaped $var \"]
set val [charEscaped $val \"]
lappend g_shcode_out "execute_process(COMMAND echo \"$var:\
$val\" COMMAND $xrdb -merge)"
}
r {
set var [charEscaped $var {\\'}]
set val [charEscaped $val {\\'}]
lappend g_shcode_out "system('$xrdb -merge', input='$var:\
$val')"
}
}
}
}
}
if {[array size g_delXResources]} {
##nagelfar ignore Found constant
set xrdb [getCommandPath xrdb]
set xres_to_del {}
foreach var [array names g_delXResources] {
# empty val means that var is a file to parse
if {$g_delXResources($var) eq {}} {
# xresource file has to be parsed to find what resources
# are declared there and need to be unset
foreach fline [split [exec $xrdb -n load $var] \n] {
lappend xres_to_del [lindex [split $fline :] 0]
}
} else {
lappend xres_to_del $var
}
}
# xresource strings are unset by emptying their value since there
# is no command of xrdb that can properly remove one property
switch -- [getState shelltype] {
sh - csh - fish {
foreach var $xres_to_del {
lappend g_shcode_out "echo \"$var:\" | $xrdb -merge;"
}
}
tcl {
foreach var $xres_to_del {
lappend g_shcode_out "set XRDBPIPE \[open \"|$xrdb -merge\"\
r+\];"
set var [charEscaped $var \"]
lappend g_shcode_out "puts \$XRDBPIPE \"$var:\";"
lappend g_shcode_out {close $XRDBPIPE;}
lappend g_shcode_out {unset XRDBPIPE;}
}
}
perl {
foreach var $xres_to_del {
lappend g_shcode_out "open(XRDBPIPE, \"|$xrdb -merge\");"
set var [charEscaped $var \"]
lappend g_shcode_out "print XRDBPIPE \"$var:\\n\";"
lappend g_shcode_out {close XRDBPIPE;}
}
}
python {
foreach var $xres_to_del {
set var [charEscaped $var \']
lappend g_shcode_out "subprocess.Popen(\['$xrdb', '-merge'\],\
stdin=subprocess.PIPE).communicate(input='$var:\\n')"
}
}
ruby {
foreach var $xres_to_del {
set var [charEscaped $var \']
lappend g_shcode_out "Open3.popen2('$xrdb -merge') {|i,o,t|\
i.puts '$var:'}"
}
}
lisp {
foreach var $xres_to_del {
lappend g_shcode_out "(shell-command-to-string \"echo $var: |\
$xrdb -merge\")"
}
}
cmake {
foreach var $xres_to_del {
set var [charEscaped $var \"]
lappend g_shcode_out "execute_process(COMMAND echo \"$var:\"\
COMMAND $xrdb -merge)"
}
}
r {
foreach var $xres_to_del {
set var [charEscaped $var {\\'}]
lappend g_shcode_out "system('$xrdb -merge', input='$var:')"
}
}
}
}
if {[info exists ::g_changeDir]} {
switch -- [getState shelltype] {
sh - csh - fish {
lappend g_shcode_out "cd '$::g_changeDir';"
}
tcl {
lappend g_shcode_out "cd \"$::g_changeDir\";"
}
cmd {
lappend g_shcode_out "cd $::g_changeDir"
}
perl {
lappend g_shcode_out "chdir '$::g_changeDir';"
}
python {
lappend g_shcode_out "os.chdir('$::g_changeDir')"
}
ruby {
lappend g_shcode_out "Dir.chdir('$::g_changeDir')"
}
lisp {
lappend g_shcode_out "(shell-command-to-string \"cd\
'$::g_changeDir'\")"
}
r {
lappend g_shcode_out "setwd('$::g_changeDir')"
}
}
# cannot change current directory of cmake "shell"
}
# send content deferred during modulefile interpretation
if {[info exists ::g_stdoutPuts]} {
foreach {newline msg} $::g_stdoutPuts {
append outmsg $msg
if {$newline} {
lappend g_shcode_out $outmsg
unset outmsg
}
}
# add last remaining message
if {[info exists outmsg]} {
lappend g_shcode_out $outmsg
}
}
# if currently processing mod-to-sh subcmd, send shell code to the message
# channel then reset code and shell state to get evaluation result shell
# code on stdout channel
if {[getState modtosh_real_shell] ne {}} {
if {[info exists g_shcode_out]} {
report [join $::g_shcode_out \n]
unset g_shcode_out
}
setState shell [getState modtosh_real_shell]
unsetState shelltype
}
# return text value if defined even if error happened
if {[info exists ::g_return_text]} {
reportDebug {text value should be returned.}
renderText $::g_return_text
} elseif {[getState error_count] > 0} {
reportDebug "[getState error_count] error(s) detected."
renderFalse
} elseif {[getState return_false]} {
reportDebug {false value should be returned.}
renderFalse
} elseif {$has_rendered} {
# finish with true statement if something has been put
renderTrue
}
}
# Output all gathered shell code to stdout. This is a separate proc rather the
# final code of renderSettings as such output is also done when exiting after
# an error. Stdout is flushed from flushAndExit proc
proc renderFlush {} {
if {[info exists ::g_shcode_out]} {
# required to work on cygwin, shouldn't hurt real linux
fconfigure stdout -translation lf
puts stdout [join $::g_shcode_out \n]
}
}
proc renderAutoinit {} {
# automatically detect which tclsh should be used for
# future module commands
set tclshbin [info nameofexecutable]
# ensure script path is absolute
set ::argv0 [getAbsolutePath $::argv0]
##nagelfar ignore #462 Close brace not aligned with line
switch -- [getState shelltype] {
csh {
set pre_hi {set _histchars = $histchars; unset histchars;}
set post_hi {set histchars = $_histchars; unset _histchars;}
set pre_pr {set _prompt=$prompt:q; set prompt="";}
set post_pr {set prompt=$_prompt:q; unset _prompt;}
# apply workaround for Tcsh history if set
set eval_cmd [expr {[getConf wa_277] ? "eval `$tclshbin\
'\"'\"'$::argv0'\"'\"' [getState shell] \\!*`;" : "eval\
\"`$tclshbin '\"'\"'$::argv0'\"'\"' [getState shell] \\!*:q`\";"}]
set pre_ex {set _exit="$status";}
set post_ex {test 0 = $_exit}
set fdef "if ( \$?histchars && \$?prompt )\
alias module '$pre_hi $pre_pr $eval_cmd $pre_ex $post_hi $post_pr $post_ex' ;
if ( \$?histchars && ! \$?prompt )\
alias module '$pre_hi $eval_cmd $pre_ex $post_hi $post_ex' ;
if ( ! \$?histchars && \$?prompt )\
alias module '$pre_pr $eval_cmd $pre_ex $post_pr $post_ex' ;
if ( ! \$?histchars && ! \$?prompt ) alias module '$eval_cmd' ;"
if {[getConf ml]} {
append fdef {
alias ml 'module ml \!*' ;}
}
}
sh {
# Considering the diversity of ways local variables are handled
# through the sh-variants ('local' known everywhere except on ksh,
# 'typeset' known everywhere except on pure-sh, and on some systems
# the pure-sh is in fact a 'ksh'), no local variables are defined and
# these variables that should have been local are unset at the end
# on zsh, word splitting should be enabled explicitly
set wsplit [expr {[getState shell] eq {zsh} ? {^^=} : {}}]
# build quarantine mechanism in module function
# an empty runtime variable is set even if no corresponding
# MODULES_RUNENV_* variable found, as var cannot be unset on
# modified environment command-line
set fdef "_module_raw() {"
if {[getConf silent_shell_debug]} {
append fdef {
unset _mlshdbg;
if [ "${MODULES_SILENT_SHELL_DEBUG:-0}" = '1' ]; then
case "$-" in
*v*x*) set +vx; _mlshdbg='vx' ;;
*v*) set +v; _mlshdbg='v' ;;
*x*) set +x; _mlshdbg='x' ;;
*) _mlshdbg='' ;;
esac;
fi;}
}
if {[getConf quarantine_support]} {
append fdef "
unset _mlre _mlIFS;
if \[ -n \"\${IFS+x}\" \]; then
_mlIFS=\$IFS;
fi;
IFS=' ';
for _mlv in \${${wsplit}MODULES_RUN_QUARANTINE:-}; do"
##nagelfar ignore #4 Too long line
append fdef {
if [ "${_mlv}" = "${_mlv##*[!A-Za-z0-9_]}" ] && [ "${_mlv}" = "${_mlv#[0-9]}" ]; then
if [ -n "$(eval 'echo ${'"$_mlv"'+x}')" ]; then
_mlre="${_mlre:-}__MODULES_QUAR_${_mlv}='$(eval 'echo ${'"$_mlv"'}')' ";
fi;
_mlrv="MODULES_RUNENV_${_mlv}";
_mlre="${_mlre:-}${_mlv}='$(eval 'echo ${'"$_mlrv"':-}')' ";
fi;
done;
if [ -n "${_mlre:-}" ]; then
_mlre="${_mlre:-}__MODULES_QUARANTINE_SET=1 ";}
append fdef "\n eval \"\$(eval \${${wsplit}_mlre} $tclshbin\
'$::argv0' [getState shell] '\"\$@\"')\";
else
eval \"\$($tclshbin '$::argv0' [getState shell] \"\$@\")\";
fi;"
} else {
append fdef "
eval \"\$($tclshbin '$::argv0' [getState shell] \"\$@\")\";"
}
append fdef {
_mlstatus=$?;}
if {[getConf quarantine_support]} {
append fdef {
if [ -n "${_mlIFS+x}" ]; then
IFS=$_mlIFS;
else
unset IFS;
fi;
unset _mlre _mlv _mlrv _mlIFS;}
}
if {[getConf silent_shell_debug]} {
append fdef {
if [ -n "${_mlshdbg:-}" ]; then
set -"$_mlshdbg";
fi;
unset _mlshdbg;}
}
append fdef {
return $_mlstatus;}
# define both _module_raw and module functions in any cases to allow
# content redirection when command-line switch is used (--redirect or
# --no-redirect)
# content is redirected by default when shell session is attached to
# a terminal (non-terminal session are not redirected to avoid
# breaking things like scp or sftp transfer)
# use local/typeset variable in this context as we cannot unset it
# afterward (since _module_raw should be the cmd to return status)
set localcmd [expr {[getState shell] eq {ksh} ? {typeset} : {local}}]
##nagelfar ignore Unescaped close brace
append fdef "\n};\nmodule() {
$localcmd _mlredir=[getState is_stderr_tty];\n"
append fdef { if [ -n "${MODULES_REDIRECT_OUTPUT+x}" ]; then
if [ "$MODULES_REDIRECT_OUTPUT" = '0' ]; then
_mlredir=0;
elif [ "$MODULES_REDIRECT_OUTPUT" = '1' ]; then
_mlredir=1;
fi;
fi;
case " $@ " in
*' --no-redirect '*) _mlredir=0 ;;
*' --redirect '*) _mlredir=1 ;;
esac;
if [ $_mlredir -eq 0 ]; then
_module_raw "$@";
else
_module_raw "$@" 2>&1;
fi;}
append fdef "\n};"
if {[getState shell] eq {bash}} {
append fdef {
export -f _module_raw;
export -f module;}
}
if {[getConf ml]} {
append fdef {
ml() { module ml "$@"; };}
if {[getState shell] eq {bash}} {
append fdef {
export -f ml;}
}
}
}
fish {
set fdef "function _module_raw\n"
if {[getConf quarantine_support]} {
append fdef { set -l _mlre ''; set -l _mlv; set -l _mlrv;
for _mlv in (string split ' ' $MODULES_RUN_QUARANTINE)
if string match -r '^[A-Za-z_][A-Za-z0-9_]*$' $_mlv >/dev/null
if set -q $_mlv
set _mlre $_mlre"__MODULES_QUAR_"$_mlv"='$$_mlv' "
end
set _mlrv "MODULES_RUNENV_$_mlv"
set _mlre "$_mlre$_mlv='$$_mlrv' "
end
end
if [ -n "$_mlre" ]
set _mlre "env $_mlre __MODULES_QUARANTINE_SET=1"
end}
# use "| source -" rather than "eval" to be able
# to redirect stderr after stdout being evaluated
append fdef "\n eval \$_mlre $tclshbin \\'$::argv0\\' [getState\
shell] (string escape -- \$argv) | source -\n"
} else {
append fdef " eval $tclshbin \\'$::argv0\\' [getState shell]\
(string escape -- \$argv) | source -\n"
}
append fdef "end
function module
set _mlredir [getState is_stderr_tty]
if set -q MODULES_REDIRECT_OUTPUT
if \[ \"\$MODULES_REDIRECT_OUTPUT\" = '0' \]
set _mlredir 0
else if \[ \"\$MODULES_REDIRECT_OUTPUT\" = '1' \]
set _mlredir 1
end
end
if contains -- --no-redirect \$argv; or begin ; \[ \$_mlredir -eq 0\
\]; and not contains -- --redirect \$argv ; end
_module_raw \$argv
else
_module_raw \$argv 2>&1
end
end"
if {[getConf ml]} {
append fdef {
function ml
module ml $argv
end}
}
}
tcl {
set fdef "proc module {args} {"
if {[getConf quarantine_support]} {
append fdef {
set _mlre {};
if {[info exists ::env(MODULES_RUN_QUARANTINE)]} {
foreach _mlv [split $::env(MODULES_RUN_QUARANTINE) " "] {
if {[regexp {^[A-Za-z_][A-Za-z0-9_]*$} $_mlv]} {
if {[info exists ::env($_mlv)]} {
lappend _mlre "__MODULES_QUAR_${_mlv}=$::env($_mlv)"
}
set _mlrv "MODULES_RUNENV_${_mlv}"
lappend _mlre [expr {[info exists ::env($_mlrv)] ?\
"${_mlv}=$::env($_mlrv)" : "${_mlv}="}]
}
}
if {[llength $_mlre]} {
lappend _mlre "__MODULES_QUARANTINE_SET=1"
set _mlre [linsert $_mlre 0 "env"]
}
}}
}
append fdef {
set _mlstatus 1;}
if {[getConf quarantine_support]} {
append fdef "\n catch {exec {*}\$_mlre \"$tclshbin\"\
\"$::argv0\" \"[getState shell]\" {*}\$args 2>@stderr}\
script\n"
} else {
append fdef "\n catch {exec \"$tclshbin\" \"$::argv0\"\
\"[getState shell]\" {*}\$args 2>@stderr} script\n"
}
append fdef { eval $script;
return $_mlstatus}
append fdef "\n}"
if {[getConf ml]} {
append fdef {
proc ml {args} {
return [module ml {*}$args]
}}
}
}
cmd {
reportErrorAndExit {No autoinit mode available for 'cmd' shell}
}
perl {
set fdef "sub module {"
if {[getConf quarantine_support]} {
append fdef {
my $_mlre = '';
if (defined $ENV{'MODULES_RUN_QUARANTINE'}) {
foreach my $_mlv (split(' ', $ENV{'MODULES_RUN_QUARANTINE'})) {
if ($_mlv =~ /^[A-Za-z_][A-Za-z0-9_]*$/) {
if (defined $ENV{$_mlv}) {
$_mlre .= "__MODULES_QUAR_${_mlv}='$ENV{$_mlv}' ";
}
my $_mlrv = "MODULES_RUNENV_$_mlv";
$_mlre .= "$_mlv='$ENV{$_mlrv}' ";
}
}
if ($_mlre ne "") {
$_mlre = "env ${_mlre}__MODULES_QUARANTINE_SET=1 ";
}
}}
}
append fdef {
my $args = '';
if (@_ > 0) {
$args = '"' . join('" "', @_) . '"';
}
my $_mlstatus = 1;}
if {[getConf quarantine_support]} {
append fdef "\n eval `\${_mlre}$tclshbin '$::argv0' perl\
\$args`;\n"
} else {
append fdef "\n eval `$tclshbin '$::argv0' perl \$args`;\n"
}
append fdef { return $_mlstatus;}
append fdef "\n}"
if {[getConf ml]} {
append fdef {
sub ml {
return module('ml', @_);
}}
}
}