-
Notifications
You must be signed in to change notification settings - Fork 100
/
mfcmd.tcl
2121 lines (1893 loc) · 67.9 KB
/
mfcmd.tcl
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
##########################################################################
# MFCMD.TCL, modulefile command 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/>.
##########################################################################
# Dictionary-style string comparison
# Use dictionary sort of lsort proc to compare two strings in the "string
# compare" fashion (returning -1, 0 or 1). Tcl dictionary-style comparison
# enables to compare software versions (ex: "1.10" is greater than "1.8")
proc versioncmp {str1 str2} {
if {$str1 eq $str2} {
return 0
# put both strings in a list, then lsort it and get first element
} elseif {[lindex [lsort -dictionary [list $str1 $str2]] 0] eq $str1} {
return -1
} else {
return 1
}
}
proc module-info {what {more {}}} {
set mode [currentState mode]
switch -- $what {
mode {
if {$more ne {}} {
set command [currentState commandname]
return [expr {$mode eq $more || ($more eq {remove} && $mode eq \
{unload}) || ($more eq {switch} && $command eq {switch}) ||\
($more eq {nonpersist} && $mode eq {refresh})}]
} else {
return $mode
}
}
command {
set command [currentState commandname]
if {$more eq {}} {
return $command
} else {
return [expr {$command eq $more}]
}
}
name {
return [currentState modulename]
}
specified {
return [currentState specifiedname]
}
shell {
if {$more ne {}} {
return [expr {[getState shell] eq $more}]
} else {
return [getState shell]
}
}
flags {
# C-version specific option, not relevant for Tcl-version but return
# a zero integer value to avoid breaking modulefiles using it
return 0
}
shelltype {
if {$more ne {}} {
return [expr {[getState shelltype] eq $more}]
} else {
return [getState shelltype]
}
}
user {
# C-version specific option, not relevant for Tcl-version but return
# an empty value or false to avoid breaking modulefiles using it
if {$more ne {}} {
return 0
} else {
return {}
}
}
alias {
set ret [resolveModuleVersionOrAlias $more [isIcase]]
if {$ret ne $more} {
return $ret
} else {
return {}
}
}
trace {
return {}
}
tracepat {
return {}
}
type {
return Tcl
}
symbols {
lassign [getModuleNameVersion $more 1] mod modname modversion
set sym_list [getVersAliasList $mod]
# if querying special symbol "default" but nothing found registered
# on it, look at symbol registered on bare module name in case there
# are symbols registered on it but no default symbol set yet to link
# to them
if {![llength $sym_list] && $modversion eq {default}} {
set sym_list [getVersAliasList $modname]
}
return [join $sym_list :]
}
tags {
# refresh mod name version and variant to correctly get all matching
# tags (in case tags apply to specific module variant)
set modname [currentState modulename]
set vrlist [getVariantList $modname 1]
if {[llength $vrlist]} {
lassign [parseModuleSpecification 0 0 0 0 $modname {*}$vrlist]\
modnamevr
} else {
set modnamevr $modname
}
collectModuleTags $modnamevr
if {$more ne {}} {
return [expr {$more in [getTagList $modnamevr [currentState\
modulefile]]}]
} else {
return [getTagList $modnamevr [currentState modulefile]]
}
}
version {
lassign [getModuleNameVersion $more 1] mod
return [resolveModuleVersionOrAlias $mod [isIcase]]
}
loaded {
lassign [getModuleNameVersion $more 1] mod
return [getLoadedMatchingName $mod returnall]
}
usergroups {
if {[getState is_win]} {
knerror "module-info usergroups not supported on Windows platform"
} else {
if {$more ne {}} {
return [expr {$more in [getState usergroups]}]
} else {
return [getState usergroups]
}
}
}
username {
if {[getState is_win]} {
knerror "module-info username not supported on Windows platform"
} else {
if {$more ne {}} {
return [expr {[getState username] eq $more}]
} else {
return [getState username]
}
}
}
default {
knerror "module-info $what not supported"
return {}
}
}
}
proc module-whatis {args} {
lappend ::g_whatis [join $args]
return {}
}
# Specifies a default or alias version for a module that points to an
# existing module version Note that aliases defaults are stored by the
# short module name (not the full path) so aliases and defaults from one
# directory will apply to modules of the same name found in other
# directories.
proc module-version {args} {
lassign [getModuleNameVersion [lindex $args 0] 1] mod modname modversion
# go for registration only if valid modulename
if {$mod ne {}} {
foreach version [lrange $args 1 end] {
set aliasversion $modname/$version
# do not alter a previously defined alias version
if {![info exists ::g_moduleVersion($aliasversion)]} {
setModuleResolution $aliasversion $mod $version
} else {
reportWarning "Symbolic version '$aliasversion' already defined"
}
}
}
return {}
}
proc module-alias {args} {
lassign [getModuleNameVersion [lindex $args 0]] alias
lassign [getModuleNameVersion [lindex $args 1] 1] mod
reportDebug "$alias = $mod"
if {[setModuleResolution $alias $mod]} {
set ::g_moduleAlias($alias) $mod
set ::g_sourceAlias($alias) [currentState modulefile]
}
return {}
}
proc module-virtual {args} {
lassign [getModuleNameVersion [lindex $args 0]] mod
set modfile [getAbsolutePath [lindex $args 1]]
reportDebug "$mod = $modfile"
set ::g_moduleVirtual($mod) $modfile
set ::g_sourceVirtual($mod) [currentState modulefile]
return {}
}
# Parse date time argument value and translate it into epoch time
proc __parseDateTimeArg {opt datetime} {
if {[regexp {^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2})?$} $datetime match\
timespec]} {
# time specification is optional
if {$timespec eq {}} {
append datetime T00:00
}
# return corresponding epoch time
return [clock scan $datetime -format %Y-%m-%dT%H:%M]
} else {
knerror "Incorrect $opt value '$datetime' (valid date time format is\
'YYYY-MM-DD\[THH:MM\]')"
}
}
# parse application criteria arguments and determine if command applies
proc parseApplicationCriteriaArgs {aftbef nearsec args} {
set otherargs {}
# parse argument list
foreach arg $args {
if {[info exists nextargisval]} {
##nagelfar vartype nextargisval varName
set $nextargisval $arg
unset nextargisval
} elseif {[info exists nextargisdatetime]} {
##nagelfar ignore Suspicious variable name
set ${nextargisdatetime}raw $arg
# get epoch time from date time argument value
##nagelfar vartype nextargisdatetime varName
##nagelfar ignore Unknown variable
set $nextargisdatetime [parseDateTimeArg $prevarg $arg]
unset nextargisdatetime
} else {
switch -- $arg {
--after - --before {
# treat --after/--before as regular content if disabled
if {!$aftbef} {
lappend otherargs $arg
} else {
set nextargisdatetime [string trimleft $arg -]
}
}
--not-group - --not-user {
if {[getState is_win]} {
knerror "Option '$arg' not supported on Windows platform"
} else {
set nextargisval not[string range $arg 6 end]list
}
}
default {
lappend otherargs $arg
}
}
set prevarg $arg
}
}
if {[info exists nextargisval] || [info exists nextargisdatetime]} {
knerror "Missing value for '$prevarg' option"
}
# does it apply to current user?
set notuser [expr {[info exists notuserlist] && [getState username] in\
$notuserlist}]
set notgroup [expr {[info exists notgrouplist] && [isIntBetweenList\
$notgrouplist [getState usergroups]]}]
# does it apply currently?
set isbefore [expr {[info exists before] && [getState clock_seconds] <\
$before}]
set isafter [expr {[info exists after] && [getState clock_seconds] >=\
$after}]
# are criteria met
set apply [expr {!$notuser && !$notgroup && ($isbefore || $isafter ||\
(![info exists before] && ![info exists after]))}]
# is end limit near ?
set isnearly [expr {!$apply && !$notuser && !$notgroup && [info exists\
after] && !$isafter && [getState clock_seconds] >= ($after - $nearsec)}]
if {![info exists afterraw]} {
set afterraw {}
}
return [list $apply $isnearly $afterraw $otherargs]
}
proc setModspecTag {modspec tag {props {}}} {
reportDebug "Set tag '$tag' with properties '$props' on module\
specification '$modspec'"
if {[isModuleFullPath $modspec]} {
# use dedicated structure for full path module specifications
if {![info exists ::g_moduleTagFullPath($modspec)] || $tag ni\
$::g_moduleTagFullPath($modspec)} {
lappend ::g_moduleTagFullPath($modspec) $tag
} else {
set idx [lsearch -exact $::g_moduleTagFullPath($modspec) $tag]
}
# record props associated to tag
if {[info exists idx]} {
lset ::g_moduleTagFullPathSpec($modspec) $idx $props
} else {
lappend ::g_moduleTagFullPathSpec($modspec) $props
}
} else {
# record tag list for mod root to optimize search
set modroot [getModuleRootFromVersSpec $modspec]
if {![info exists ::g_moduleTagRoot($modroot)]} {
lappend ::g_moduleTagRoot($modroot) $tag
set idx 0
set new 1
} else {
set idx [lsearch -exact $::g_moduleTagRoot($modroot) $tag]
if {$idx == -1} {
set idx [llength $::g_moduleTagRoot($modroot)]
lappend ::g_moduleTagRoot($modroot) $tag
set new 1
}
}
# then record mod spec and props at idx defined for tag. new spec are
# appended and firstly matching spec is returned with its props on
# search
if {[info exists new]} {
lappend ::g_moduleTagRootSpec($modroot) [list $modspec $props]
} else {
set tagrootlist [lindex $::g_moduleTagRootSpec($modroot) $idx]
lappend tagrootlist $modspec $props
lset ::g_moduleTagRootSpec($modroot) $idx $tagrootlist
}
}
}
proc module-forbid {args} {
# parse application criteria arguments to determine if command apply
lassign [parseApplicationCriteriaArgs 1 [expr {[getConf\
nearly_forbidden_days] * 86400}] {*}$args] apply isnearly after\
otherargs
# parse remaining argument list, do it even if command does not apply to
# raise any command specification error
foreach arg $otherargs {
if {[info exists nextargisval]} {
##nagelfar vartype nextargisval varName
set $nextargisval $arg
unset nextargisval
} else {
switch -glob -- $arg {
--nearly-message {
set nextargisval nearlymessage
}
--message {
set nextargisval message
}
-* {
knerror "Invalid option '$arg'"
}
default {
lappend modarglist $arg
}
}
set prevarg $arg
}
}
if {[info exists nextargisval]} {
knerror "Missing value for '$prevarg' option"
}
if {![info exists modarglist]} {
knerror {No module specified in argument}
}
# skip record if application criteria are not met
if {$apply} {
set proplist {}
if {[info exists message]} {
##nagelfar ignore Found constant
lappend proplist message $message
}
# record each forbid spec after parsing them
foreach modarg [parseModuleSpecification 0 0 0 0 {*}$modarglist] {
setModspecTag $modarg forbidden $proplist
}
} elseif {$isnearly} {
##nagelfar ignore Found constant
lappend proplist after $after
if {[info exists nearlymessage]} {
##nagelfar ignore Found constant
lappend proplist message $nearlymessage
}
# record each nearly forbid spec after parsing them
foreach modarg [parseModuleSpecification 0 0 0 0 {*}$modarglist] {
setModspecTag $modarg nearly-forbidden $proplist
}
}
}
proc module-hide {args} {
set hidinglvl 1
set hiddenloaded 0
# parse application criteria arguments to determine if command apply
lassign [parseApplicationCriteriaArgs 1 0 {*}$args] apply isnearly after\
otherargs
# parse remaining argument list, do it even if command does not apply to
# raise any command specification error
foreach arg $otherargs {
switch -glob -- $arg {
--hard {
# hardened stealth
set hidinglvl 2
}
--soft {
# soften level of camouflage
set hidinglvl 0
}
--hidden-loaded {
# module should stay hidden once being loaded
set hiddenloaded 1
}
-* {
knerror "Invalid option '$arg'"
}
default {
lappend modarglist $arg
}
}
}
if {![info exists modarglist]} {
knerror {No module specified in argument}
}
# skip hide spec record if application criteria are not met
if {$apply} {
# record each hide spec after parsing them
foreach modarg [parseModuleSpecification 0 0 0 0 {*}$modarglist] {
setModspecHidingLevel $modarg $hidinglvl
if {$hiddenloaded} {
setModspecTag $modarg hidden-loaded
}
}
}
}
proc module-tag {args} {
# parse application criteria arguments to determine if command apply
lassign [parseApplicationCriteriaArgs 0 0 {*}$args] apply isnearly after\
otherargs
# parse remaining argument list, do it even if command does not apply to
# raise any command specification error
foreach arg $otherargs {
switch -glob -- $arg {
-* {
knerror "Invalid option '$arg'"
}
default {
if {![info exists tag]} {
set tag $arg
} else {
lappend modarglist $arg
}
}
}
}
if {![info exists tag]} {
knerror {No tag specified in argument}
}
if {![info exists modarglist]} {
knerror {No module specified in argument}
}
if {$tag in [list loaded auto-loaded forbidden nearly-forbidden hidden\
hidden-loaded]} {
knerror "'$tag' is a reserved tag name and cannot be set"
}
# skip tag record if application criteria are not met
if {$apply} {
# record each hide spec after parsing them
foreach modarg [parseModuleSpecification 0 0 0 0 {*}$modarglist] {
setModspecTag $modarg $tag
}
}
}
# parse arguments sent to the unsetenv modulefile command
proc parseSetenvCommandArgs {mode dflbhv args} {
set bhv $dflbhv
foreach arg $args {
switch -- $arg {
--set-if-undef {
if {$mode eq {load}} {
set setifundef 1
}
}
default {
if {![info exists var]} {
set var $arg
} elseif {![info exists val]} {
set val $arg
} else {
# too much argument
set wrongargnum 1
}
}
}
}
if {[info exists wrongargnum] || ![info exists var] || ![info exists\
val]} {
knerror {wrong # args: should be "setenv ?--set-if-undef? var val"}
}
if {[info exists setifundef] && [isEnvVarDefined $var]} {
set bhv noop
}
reportDebug "bhv=$bhv, var=$var, val=$val"
return [list $bhv $var $val]
}
proc setenv {args} {
lassign [parseSetenvCommandArgs load set {*}$args] bhv var val
if {$bhv eq {set}} {
# clean any previously defined reference counter array
unset-env [getModshareVarName $var] 1
# clean any previously defined pushenv stack
unset-env [getPushenvVarName $var] 1
# Set the variable for later use during the modulefile evaluation
set-env $var $val
}
return {}
}
# undo setenv in unload mode
proc setenv-un {args} {
lassign [parseSetenvCommandArgs unload unset {*}$args] bhv var val
# clean any existing reference counter array
unset-env [getModshareVarName $var] 1
# clean any previously defined pushenv stack
unset-env [getPushenvVarName $var] 1
# Add variable to the list of variable to unset in shell output code but
# set it in interp context as done on load mode for later use during the
# modulefile evaluation
unset-env $var 0 $val
return {}
}
# optimized setenv for whatis mode: init env variable with an empty
# value if undefined. do not care about value, just avoid variable to be
# undefined for later use during the modulefile evaluation
proc setenv-wh {args} {
lassign [parseSetenvCommandArgs load set {*}$args] bhv var val
setEnvVarIfUndefined $var {}
return {}
}
# parse arguments sent to the getenv modulefile command
proc parseGetenvCommandArgs {cmd args} {
set returnval 0
set valifundef {}
switch -- [llength $args] {
1 {
set var [lindex $args 0]
}
2 {
switch -- [lindex $args 0] {
--return-value {
set returnval 1
set var [lindex $args 1]
}
default {
set var [lindex $args 0]
set valifundef [lindex $args 1]
}
}
}
3 {
if {[lindex $args 0] ne {--return-value}} {
set wrongargs 1
} else {
set returnval 1
set var [lindex $args 1]
set valifundef [lindex $args 2]
}
}
default {
set wrongargs 1
}
}
set argname [expr {$cmd eq {getenv} ? {variable} : {name}}]
if {[info exists wrongargs]} {
knerror "wrong # args: should be \"$cmd ?--return-value? $argname\
?valifundef?\""
}
reportDebug "$argname='$var', valifundef='$valifundef',\
returnval='$returnval'"
return [list $var $valifundef $returnval]
}
proc getenv {args} {
# parse args
lassign [parseGetenvCommandArgs getenv {*}$args] var valifundef returnval
if {[currentState mode] ne {display} || $returnval} {
return [get-env $var $valifundef]
} else {
return "\$$var"
}
}
# parse arguments sent to the unsetenv modulefile command
proc parseUnsetenvCommandArgs {mode dflbhv args} {
foreach arg $args {
switch -- $arg {
--unset-on-unload {
if {$mode eq {unload}} {
set bhv unset
}
}
--noop-on-unload {
if {$mode eq {unload}} {
set bhv noop
}
}
default {
if {![info exists var]} {
set var $arg
} elseif {![info exists val]} {
set val $arg
if {$mode eq {unload} && ![info exists bhv]} {
set bhv set
}
} else {
# too much argument
set wrongargnum 1
}
}
}
}
if {[info exists wrongargnum] || ![info exists var]} {
knerror {wrong # args: should be "unsetenv ?--noop-on-unload?\
?--unset-on-unload? var ?val?"}
}
if {![info exists bhv]} {
set bhv $dflbhv
}
# initialize val to always return same structure, val is only used if bhv
# equals 'set'
if {![info exists val]} {
set val {}
}
reportDebug "bhv=$bhv, var=$var, val=$val"
return [list $bhv $var $val]
}
proc unsetenv {args} {
lassign [parseUnsetenvCommandArgs load unset {*}$args] bhv var val
# clean any existing reference counter array
unset-env [getModshareVarName $var] 1
# clean any previously defined pushenv stack
unset-env [getPushenvVarName $var] 1
# Set the variable for later use during the modulefile evaluation
unset-env $var
return {}
}
# undo unsetenv in unload mode
proc unsetenv-un {args} {
lassign [parseUnsetenvCommandArgs unload noop {*}$args] bhv var val
switch -- $bhv {
set {
# apply value specified for set on unload
return [setenv $var $val]
}
unset {
return [unsetenv $var]
}
noop {
# otherwise just clear variable if it does not exist on unload mode
# for later use during the modulefile evaluation
if {![isEnvVarDefined $var]} {
reset-to-unset-env $var
}
}
}
return {}
}
# optimized unsetenv for whatis mode: init env variable with an empty
# value if undefined. do not care about value, just avoid variable to be
# undefined for later use during the modulefile evaluation
proc unsetenv-wh {args} {
lassign [parseUnsetenvCommandArgs load noop {*}$args] bhv var val
setEnvVarIfUndefined $var {}
return {}
}
proc chdir {dir} {
if {[file exists $dir] && [file isdirectory $dir]} {
set ::g_changeDir $dir
} else {
# report issue but does not treat it as an error to have the
# same behavior as C-version
reportWarning "Cannot chdir to '$dir' for '[currentState modulename]'"
}
return {}
}
# supersede exit command to handle it if called within a modulefile
# rather than exiting the whole process
proc exitModfileCmd {{code 0}} {
if {[currentState mode] in {load refresh}} {
setState inhibit_interp 1
}
# break to gently end interpretation of current modulefile
return -code break
}
# enables sub interp to return ModulesVersion value to the main interp
proc setModulesVersion {val} {
set ::ModulesVersion $val
}
# supersede puts command to catch content sent to stdout/stderr within
# modulefile in order to correctly send stderr content (if a pager has been
# enabled) or postpone content channel send after rendering on stdout the
# relative environment changes required by the modulefile
proc putsModfileCmd {itrp args} {
# determine if puts call targets the stdout or stderr channel
switch -- [llength $args] {
1 {
# create struct with newline status and message to output
set deferPuts [list 1 [lindex $args 0]]
}
2 {
switch -- [lindex $args 0] {
-nonewline {
set deferPuts [list 0 [lindex $args 1]]
}
stdout {
set deferPuts [list 1 [lindex $args 1]]
}
prestdout {
set deferPrePuts [list 1 [lindex $args 1]]
}
stderr {
set reportArgs [list [lindex $args 1]]
}
}
}
3 {
if {[lindex $args 0] eq {-nonewline}} {
switch -- [lindex $args 1] {
stdout {
set deferPuts [list 0 [lindex $args 2]]
}
prestdout {
set deferPrePuts [list 0 [lindex $args 2]]
}
stderr {
set reportArgs [list [lindex $args 2] 1]
}
}
} else {
set wrongNumArgs 1
}
}
default {
set wrongNumArgs 1
}
}
# raise error if bad argument number detected, do this here rather in _puts
# not to confuse people with an error reported by an internal name (_puts)
if {[info exists wrongNumArgs]} {
knerror {wrong # args: should be "puts ?-nonewline? ?channelId? string"}
# defer puts if it targets stdout (see renderSettings)
} elseif {[info exists deferPuts]} {
# current module is qualified for refresh evaluation
lappendState -nodup refresh_qualified [currentState modulename]
lappend ::g_stdoutPuts {*}$deferPuts
} elseif {[info exists deferPrePuts]} {
lappendState -nodup refresh_qualified [currentState modulename]
lappend ::g_prestdoutPuts {*}$deferPrePuts
# if it targets stderr call report, which knows what channel to use
} elseif {[info exists reportArgs]} {
# report message only if not silent
if {[isVerbosityLevel concise]} {
report {*}$reportArgs
}
# pass to real puts command if not related to stdout and do that in modfile
# interpreter context to get access to eventual specific channel
} else {
# re-throw error as a known error for accurate stack trace print
if {[catch {$itrp eval _puts $args} errMsg]} {
knerror $errMsg MODULES_ERR_CUSTOM
}
}
}
proc prepend-path {args} {
# Set the variable for later use during the modulefile evaluation
add-path prepend-path load prepend {*}$args
return {}
}
proc prepend-path-un {args} {
# Set the variable for later use during the modulefile evaluation
unload-path prepend-path unload remove {*}$args
return {}
}
proc append-path {args} {
# Set the variable for later use during the modulefile evaluation
add-path append-path load append {*}$args
return {}
}
proc append-path-un {args} {
# Set the variable for later use during the modulefile evaluation
unload-path append-path unload remove {*}$args
return {}
}
proc remove-path {args} {
# Set the variable for later use during the modulefile evaluation
unload-path remove-path load remove {*}$args
return {}
}
# undo remove-path in unload mode
proc remove-path-un {args} {
lassign [unload-path remove-path unload noop {*}$args] bhv var
# clean any previously defined pushenv stack
unset-env [getPushenvVarName $var] 1
# clear variable if it does not exist on unload mode for later use
# during the modulefile evaluation
if {![isEnvVarDefined $var]} {
reset-to-unset-env $var
}
}
# optimized *-path for whatis mode: init env variable with an empty value if
# undefined. do not care about value, just avoid variable to be undefined for
# later use during the modulefile evaluation
proc edit-path-wh {cmd args} {
# get variable name
lassign [parsePathCommandArgs $cmd load noop {*}$args] separator allow_dup\
idx_val ign_refcount glob_match bhv var path_list
setEnvVarIfUndefined $var {}
return {}
}
proc set-alias {alias what} {
set ::g_Aliases($alias) $what
set ::g_stateAliases($alias) new
# current module is qualified for refresh evaluation
lappendState -nodup refresh_qualified [currentState modulename]
return {}
}
# undo set-alias in unload mode
proc set-alias-un {alias what} {
return [unset-alias $alias]
}
proc unset-alias {alias} {
set ::g_Aliases($alias) {}
set ::g_stateAliases($alias) del
return {}
}
proc set-function {function what} {
set ::g_Functions($function) $what
set ::g_stateFunctions($function) new
# current module is qualified for refresh evaluation
lappendState -nodup refresh_qualified [currentState modulename]
return {}
}
# undo set-function in unload mode
proc set-function-un {function what} {
return [unset-function $function]
}
proc unset-function {function} {
set ::g_Functions($function) {}
set ::g_stateFunctions($function) del
return {}
}
proc is-loaded {args} {
# parse module version specification
set args [parseModuleSpecification 0 0 0 0 {*}$args]
foreach mod $args {
if {[getLoadedMatchingName $mod returnfirst] ne {}} {
return 1
}
}
# is something loaded whatever it is?
return [expr {![llength $args] && [llength\
[getEnvLoadedModulePropertyParsedList name]]}]
}
proc is-loading {args} {
foreach mod $args {
if {[getLoadedMatchingName $mod returnfirst 1] ne {}} {
return 1
}
}
# is something else loading whatever it is?
return [expr {![llength $args] && [llength [getLoadingModuleList]] > 1}]
}
proc conflict {args} {
set currentModule [currentState modulename]