-
Notifications
You must be signed in to change notification settings - Fork 110
/
main.tcl.in
1166 lines (1097 loc) · 41 KB
/
main.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
##########################################################################
# MAIN.TCL, main procedures and code
# Copyright (C) 2002-2004 Mark Lakata
# Copyright (C) 2004-2017 Kent Mein
# Copyright (C) 2016-2022 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/>.
##########################################################################
# exit in a clean manner by closing interaction with external components
proc cleanupAndExit {code} {
# report execution time if asked
if {[getState timer]} {
reportSeparateNextContent
reportTimer "Total execution took %.3f ms" $::timer_start [clock\
microseconds]
}
# finish output document if json format enabled
if {[isStateEqual report_format json]} {
# render error messages all together
if {[info exists ::g_report_erralist]} {
# ignite report first to get eventual error message from report
# initialization in order 'foreach' got all messages prior firing
report "\"errors\": \[" 1
foreach {sev msg} $::g_report_erralist {
# split message in lines
lappend dispmsglist "\n{ \"severity\": \"$sev\", \"message\": \[\
\"[join [split [charEscaped $msg \"] \n] {", "}]\" \] }"
}
report "[join $dispmsglist ,] \]"
}
# inhibit next content separator if output is ending
if {[isStateDefined report_sep_next]} {
unsetState report_sep_next
}
report \}
}
# close pager if enabled
if {[isStateDefined reportfd] && ![isStateEqual reportfd stderr]} {
catch {flush [getState reportfd]}
catch {close [getState reportfd]}
}
exit $code
}
# runs the global RC files if they exist
proc runModulerc {} {
setState rc_running 1
foreach rc [getGlobalRcFileList] {
reportDebug "Executing $rc"
cmdModuleSource load $rc
lappendState rc_loaded $rc
}
unsetState rc_running
# identify alias or symbolic version set in these global RC files to be
# able to include them or not in output or resolution processes
array set ::g_rcAlias [array get ::g_moduleAlias]
array set ::g_rcVersion [array get ::g_moduleVersion]
array set ::g_rcVirtual [array get ::g_moduleVirtual]
}
proc aboveCommandName {} {
return [lindex [getState commandname] end-1]
}
proc ongoingCommandName {commandName} {
return [expr {[lsearch -exact [getState commandname] $commandName] != -1}]
}
# Know if we are currently at the top evaluation level: (1) at the modulecmd
# level (module command written by user in terminal or script) or (2) during
# the evaluation of rc or modulefile by a source or autoinit sub-command
# triggered from modulecmd level or (3) during the evaluation of global rc
# file. (2) and (3) are considered "extended" top evaluation contexts.
proc isTopEvaluation {{extended 1}} {
return [expr {[depthState modulename] == 0 || ($extended && [depthState\
modulename] == 1 && ([aboveCommandName] in {source autoinit} ||\
[isStateDefined rc_running]))}]
}
# analyze/translate command name passed to module
proc parseModuleCommandName {command defaultcmd} {
set cmdempty 0
# resolve command if alias or shortcut name used
switch -- $command {
add {set command load}
try-add {set command try-load}
add-any {set command load-any}
rm - remove {set command unload}
show {set command display}
apropos - keyword {set command search}
{} {
# if empty string supplied translate to default command
set command $defaultcmd
set cmdempty 1
}
default {
# specific match for shortcut names
set cmdlen [string length $command]
foreach {match minlen sccmd} {load 2 load unload 4 unload delete 3\
unload refresh 3 refresh reload 3 reload switch 2 switch swap 2\
switch display 2 display available 2 avail aliases 2 aliases list\
2 list whatis 2 whatis purge 2 purge initadd 5 initadd initload 6\
initadd initprepend 5 initprepend initswitch 6 initswitch\
initswap 6 initswitch initunload 8 initrm initlist 5 initlist} {
if {$cmdlen >= $minlen && [string equal -length $cmdlen $command\
$match]} {
set command $sccmd
break
}
}
}
}
set cmdvalid [expr {$command in [list load unload reload use unuse source\
switch display avail aliases path paths list whatis search purge save\
restore saverm saveshow savelist initadd initprepend initswitch initrm\
initlist initclear autoinit clear config help test prepend-path\
append-path remove-path is-loaded is-saved is-used is-avail info-loaded\
sh-to-mod edit try-load refresh state load-any lint]}]
reportDebug "(command=$command, cmdvalid=$cmdvalid, cmdempty=$cmdempty)"
return [list $command $cmdvalid $cmdempty]
}
# analyze arg list passed to a module cmd to set options
proc parseModuleCommandArgs {topcall cmd ignerr args} {
set show_oneperline 0
set show_mtime 0
set show_filter {}
set search_filter [expr {[getConf avail_indepth] ? {} : {noindepth}}]
set search_match [getConf search_match]
set dump_state 0
set addpath_pos prepend
set not_req 0
set tag_list {}
set otherargs {}
# parse argument list
foreach arg $args {
if {[info exists nextargisval]} {
set $nextargisval $arg
unset nextargisval
} elseif {[info exists nextargisvaltosplit]} {
set $nextargisvaltosplit [split $arg :]
unset nextargisvaltosplit
} elseif {[info exists ignore_next_arg]} {
unset ignore_next_arg
} else {
switch -glob -- $arg {
-j - --json {
# enable json output only on supported command
if {$cmd in [list avail savelist list search whatis]} {
setState report_format json
set show_oneperline 0
set show_mtime 0
}
}
-t - --terse {
set show_oneperline 1
set show_mtime 0
setState report_format terse
}
-l - --long {
set show_mtime 1
set show_oneperline 0
setState report_format long
}
-o {
# option is only valid for specific sub-commands
if {$cmd in [list avail list]} {
set nextargisval asked_output
set output_arg -o
} else {
if {!$ignerr} {
knerror "Unsupported option '$arg' on $cmd sub-command"
}
set ignore_next_arg 1
}
}
--output=* {
# option is only valid for specific sub-commands
if {$cmd in [list avail list]} {
set asked_output [string range $arg 9 end]
set output_arg --output
} elseif {!$ignerr} {
knerror "Unsupported option '--output' on $cmd sub-command"
}
}
--tag=* - --tag {
# option is only valid for specific sub-commands
# unload allowed not to raise error on unload/load mixed ml cmd
if {$cmd in [list load try-load load-any switch unload]} {
if {$arg eq {--tag}} {
set nextargisvaltosplit tag_list
} else {
set tag_list [split [string range $arg 6 end] :]
if {[llength $tag_list] == 0} {
knerror "Missing value for '--tag' option"
}
}
} elseif {!$ignerr} {
knerror "Unsupported option '--tag' on $cmd sub-command"
}
}
--append - -append {
if {$cmd eq {use}} {
set addpath_pos append
} else {
lappend otherargs $arg
}
}
-p - --prepend - -prepend {
if {$cmd eq {use}} {
set addpath_pos prepend
} else {
lappend otherargs $arg
}
}
--all {
# include hidden modules only on a limited set of command
if {$cmd in [list avail aliases search whatis ml list lint]} {
setState hiding_threshold 2
} else {
lappend otherargs $arg
}
}
-a {
# -a option has a different meaning whether sub-command is use
# or one of the search/listing sub-commands
if {$cmd eq {use}} {
set addpath_pos append
} elseif {$cmd in [list avail aliases search whatis ml list\
lint]} {
setState hiding_threshold 2
} else {
lappend otherargs $arg
}
}
-d - --default {
# in case of *-path command, -d means --delim
if {$arg eq {-d} && [string match *-path $cmd]} {
lappend otherargs $arg
} else {
set show_filter onlydefaults
}
}
-L - --latest {
set show_filter onlylatest
}
-C - --contains {
set search_match contains
}
-S - --starts-with {
set search_match starts_with
}
--indepth {
# empty value means 'in depth' as it is default behavior
set search_filter {}
}
--no-indepth {
set search_filter noindepth
}
--dump-state {
set dump_state 1
}
--auto - --no-auto - -f - --force {
reportWarning "Unsupported option '$arg'"
}
--not-req {
if {!$topcall && $cmd in [list load try-load load-any unload\
switch]} {
set not_req 1
} else {
knerror "Unsupported option '$arg' on $cmd sub-command"
}
}
--output {
knerror "Missing value for '$arg' option"
}
default {
lappend otherargs $arg
}
}
set prevarg $arg
}
}
if {[info exists nextargisval] || [info exists nextargisvaltosplit]} {
knerror "Missing value for '$prevarg' option"
}
foreach tag $tag_list {
if {$tag in [list loaded auto-loaded forbidden nearly-forbidden\
hidden]} {
knerror "Tag '$tag' cannot be manually set"
}
}
if {[info exists asked_output]} {
if {[getState report_format] in [list long json]} {
knerror "Unsupported option '$output_arg' on [getState\
report_format] output mode"
} else {
# get config name relative to current sub-command and output format
set outputconf $cmd
if {[getState report_format] ne {regular}} {
append outputconf _[getState report_format]
}
append outputconf _output
# check option value is coherent with current sub-command
if {[isDiffBetweenList [split $asked_output :] [lindex\
$::g_config_defs($outputconf) 3]]} {
if {!$ignerr} {
knerror "Invalid element in value list for '$output_arg'\
option on $cmd sub-command\nAllowed elements are: [lindex\
$::g_config_defs($outputconf) 3] (separated by ':')"
}
} else {
set ::asked_$outputconf $asked_output
}
}
}
reportDebug "(show_oneperline=$show_oneperline, show_mtime=$show_mtime,\
show_filter=$show_filter, search_filter=$search_filter,\
search_match=$search_match, dump_state=$dump_state,\
addpath_pos=$addpath_pos, not_req=$not_req, tag_list=$tag_list,\
otherargs=$otherargs)"
return [list $show_oneperline $show_mtime $show_filter $search_filter\
$search_match $dump_state $addpath_pos $not_req $tag_list $otherargs]
}
proc module {command args} {
# guess if called from top level
set topcall [isTopEvaluation 0]
set tryhelpmsg [expr {$topcall ? "\nTry 'module --help' for more\
information." : {}}]
if {$topcall} {
set msgprefix {}
} else {
set msgprefix {module: }
}
# get mode, set to load if called from top level
set mode [expr {$topcall ? {load} : [currentState mode]}]
# resolve and check command name
lassign [parseModuleCommandName $command help] command cmdvalid cmdempty
# clear other args if no command name supplied
if {$cmdempty} {
set args {}
}
# raise error if supplied command is not known
if {!$cmdvalid} {
knerror "${msgprefix}Invalid command '$command'$tryhelpmsg"
}
# parse options, do that globally to ignore options not related to a given
# module sub-command (exclude them from arg list)
lassign [parseModuleCommandArgs $topcall $command 0 {*}$args]\
show_oneperline show_mtime show_filter search_filter search_match\
dump_state addpath_pos not_req tag_list args
# parse module version specification
defineParseModuleSpecificationProc [getConf advanced_version_spec]
if {$command in [list avail paths whatis load unload switch help test\
display path is-avail edit try-load load-any list lint]} {
set args [parseModuleSpecification 0 {*}$args]
}
if {!$topcall} {
# some commands can only be called from top level, not within modulefile
switch -- $command {
path - paths - autoinit - help - prepend-path - append-path -\
remove-path - is-loaded - is-saved - is-used - is-avail -\
info-loaded - clear - sh-to-mod - edit - refresh - source - state -\
lint {
knerror "${msgprefix}Command '$command' not supported$tryhelpmsg"
}
}
# other commands can only be called from modulefile evaluated from
# command acting as top-level context (source and autoinit)
if {([depthState modulename] > 1 || [currentState commandname] ni [list\
source autoinit]) && $command eq {config}} {
knerror "${msgprefix}Command '$command' not supported$tryhelpmsg"
}
# no requirement should be recorded this module load/unload/switch cmd
if {$not_req || ![getConf implicit_requirement]} {
lappendState inhibit_req_record [currentState evalid]
}
}
# argument number check
switch -- $command {
unload - source - display - initadd - initprepend - initrm - test -\
is-avail - try-load - load-any {
if {[llength $args] == 0} {
set argnberr 1
}
}
refresh - reload - aliases - purge - savelist - initlist - initclear -\
autoinit {
if {[llength $args] != 0} {
set argnberr 1
}
}
switch {
if {[llength $args] == 0 || [llength $args] > 2} {
set argnberr 1
}
}
path - paths - info-loaded - edit {
if {[llength $args] != 1} {
set argnberr 1
}
}
search - save - restore - saverm - saveshow - clear - state {
if {[llength $args] > 1} {
set argnberr 1
}
}
initswitch {
if {[llength $args] != 2} {
set argnberr 1
}
}
prepend-path - append-path - remove-path - sh-to-mod {
if {[llength $args] < 2} {
set argnberr 1
}
}
config {
if {[llength $args] > 2} {
set argnberr 1
}
}
}
if {[info exists argnberr]} {
knerror "Unexpected number of args for '$command' command$tryhelpmsg"
}
# define if modfile should always be fully read even for validity check
lappendState always_read_full_file [expr {$command ni [list path paths\
list avail aliases edit]}]
lappendState commandname $command
if {$topcall} {
# Find and execute any global rc file found
runModulerc
}
switch -- $command {
load - try-load - load-any {
# ignore flag used in collection to track non-user asked state
set args [replaceFromList $args --notuasked]
# no error raised on empty argument list to cope with
# initadd command that may expect this behavior
if {[llength $args] > 0} {
set ret 0
# if top command is source, consider module load commands made
# within sourced file evaluation as top load command
if {[isTopEvaluation]} {
# is eval a regular attempt or a try (silence not found error)
set tryload [expr {$command in {try-load load-any}}]
set loadany [expr {$command eq {load-any}}]
set ret [cmdModuleLoad load 1 $tryload $loadany $tag_list\
{*}$args]
} elseif {$mode eq {load}} {
# auto load is inhibited if currently in DepRe context only
# register requirement
set subauto [expr {[currentModuleEvalContext] eq {depre} ? {0}\
: {1}}]
if {$command eq {try-load}} {
# attempt load of not already loaded modules
if {$subauto} {
foreach arg $args {
lassign [loadRequirementModuleList 1 0 $tag_list\
$arg] retlo
# update return value if an issue occurred unless
# force mode is enabled
if {$retlo != 0 && ![getState force]} {
set ret $retlo
}
}
}
# record requirement as optional: no error if not loaded
# but reload will be triggered if loaded later on
prereqAllModfileCmd 1 0 --optional --tag [join $tag_list :]\
{*}$args
} elseif {$command eq {load-any}} {
# load and register requirement in a OR-operation
prereqAnyModfileCmd 1 $subauto --tag [join $tag_list :]\
{*}$args
} else {
# load and register requirement in a AND-operation
prereqAllModfileCmd 0 $subauto --tag [join $tag_list :]\
{*}$args
}
# mods unload is handled via UReqUn mechanism when auto enabled
# (unless if implicit_requirement has been inhibited) also unloads
# are triggered by ongoing reload, purge or restore commands
} elseif {(![getConf auto_handling] || [getState\
inhibit_req_record] eq [currentState evalid]) &&\
[aboveCommandName] ni [list purge reload restore]} {
# on unload mode, unload mods in reverse order, if loaded
# prior this mod, if not user asked and not required by
# other loaded mods
set modlist [getLoadedModuleList]
set modidx [lsearch -exact $modlist [currentState modulename]]
if {$modidx != 0} {
set priormodlist [lrange $modlist 0 $modidx]
foreach arg [lreverse $args] {
if {[set unmod [getLoadedMatchingName $arg {} 0\
$priormodlist]] ne {}} {
if {[cmdModuleUnload urequn match 1 0 1 1 $unmod]} {
reportWarning "Unload of useless requirement\
[getModuleDesignation loaded $unmod] failed" 1
}
}
}
}
}
# sub-module interpretation failed, raise error
if {$ret && !$topcall} {
knerror {} MODULES_ERR_SUBFAILED
}
}
}
unload {
# if top command is source, consider module load commands made
# within sourced file evaluation as top load command
if {[isTopEvaluation]} {
set ret [cmdModuleUnload unload match 1 0 0 0 {*}$args]
} elseif {$mode eq {load}} {
# unload mods only on load mode, nothing done on unload mode as
# the registered conflict guarantees the target module cannot
# be loaded unless forced
# do not unload module required by others even in force mode
set ret [cmdModuleUnload conun match 0 0 0 1 {*}$args]
# register modulefiles to unload as individual conflicts
foreach arg $args {
# do not break on error yet, go through the whole modfile
# evaluation in case conflict is solved later on
catch {conflict $arg}
}
# sub-module interpretation failed, raise error
if {$ret} {
knerror {} MODULES_ERR_SUBFAILED
}
}
}
refresh {
cmdModuleRefresh
}
reload {
cmdModuleReload
}
use {
cmdModuleUse $mode $addpath_pos {*}$args
}
unuse {
cmdModuleUnuse $mode {*}$args
}
source {
cmdModuleSource load {*}$args
}
switch {
# pass 'user asked state' to switch procedure
set uasked [isTopEvaluation]
if {$uasked} {
cmdModuleSwitch $uasked $tag_list {*}$args
} else {
# CAUTION: it is not recommended to use the `switch`
# sub-command in modulefiles as this command is intended for
# the command-line for a 2in1 operation. Could be removed from
# the modulefile scope in a future release. Use `module unload`
# and `module load` commands in modulefiles instead.
switch -- $mode {
load {
cmdModuleSwitch $uasked $tag_list {*}$args
}
unload {
# find what has been asked for unload and load
lassign $args swunmod swlomod
if {$swlomod eq {} && $swunmod ne {}} {
set swlomod $swunmod
}
# apply same mechanisms than for 'module load' and
# 'module unload' for an unload evaluation: nothing done
# for switched-off module and unload of switched-on
# module. If auto handling is enabled switched-on module
# is handled via UReqUn mechanism (unless if
# implicit_requirement has been inhibited). Also unloads are
# triggered by ongoing reload, purge or restore commands
if {(![getConf auto_handling] || [getState\
inhibit_req_record] eq [currentState evalid]) &&\
$swlomod ne {} && [aboveCommandName] ni [list purge\
reload restore]} {
# unload mod if it was loaded prior this mod, not user
# asked and not required by another loaded module
set modlist [getLoadedModuleList]
set modidx [lsearch -exact $modlist [currentState\
modulename]]
if {$modidx != 0} {
set priormodlist [lrange $modlist 0 $modidx]
if {[set unmod [getLoadedMatchingName $swlomod {} 0\
$priormodlist]] ne {}} {
if {[cmdModuleUnload urequn match 1 0 1 1 $unmod]} {
reportWarning "Unload of useless requirement\
[getModuleDesignation loaded $unmod] failed"\
1
}
}
}
}
}
}
}
}
display {
cmdModuleDisplay {*}$args
}
avail {
{*}cmdModuleAvail $show_oneperline $show_mtime $show_filter\
$search_filter $search_match {*}$args
}
aliases {
cmdModuleAliases
}
path {
cmdModulePath {*}$args
}
paths {
cmdModulePaths {*}$args
}
list {
cmdModuleList $show_oneperline $show_mtime $search_match {*}$args
}
whatis {
if {$args ne {}} {
foreach arg $args {
cmdModuleWhatIs $arg
}
} else {
cmdModuleWhatIs
}
}
search {
cmdModuleApropos {*}$args
}
purge {
cmdModulePurge
}
save {
cmdModuleSave {*}$args
}
restore {
cmdModuleRestore {*}$args
}
saverm {
cmdModuleSaverm {*}$args
}
saveshow {
cmdModuleSaveshow {*}$args
}
savelist {
cmdModuleSavelist $show_oneperline $show_mtime
}
initadd {
cmdModuleInit add {*}$args
}
initprepend {
cmdModuleInit prepend {*}$args
}
initswitch {
cmdModuleInit switch {*}$args
}
initrm {
cmdModuleInit rm {*}$args
}
initlist {
cmdModuleInit list {*}$args
}
initclear {
cmdModuleInit clear {*}$args
}
autoinit {
cmdModuleAutoinit
}
clear {
# ensure empty string is correctly passed
cmdModuleClear [lindex $args 0] [llength $args]
}
config {
cmdModuleConfig $dump_state {*}$args
}
state {
cmdModuleState {*}$args
}
sh-to-mod {
cmdModuleShToMod {*}$args
}
edit {
cmdModuleEdit {*}$args
}
lint {
cmdModuleLint {*}$args
}
help {
cmdModuleHelp {*}$args
}
test {
cmdModuleTest {*}$args
}
prepend-path - append-path - remove-path - is-loaded - is-saved -\
is-used - is-avail {
cmdModuleResurface $command {*}$args
}
info-loaded {
cmdModuleResurface module-info loaded {*}$args
}
}
lpopState commandname
lpopState always_read_full_file
if {!$topcall && ($not_req || ![getConf implicit_requirement])} {
lpopState inhibit_req_record
}
# if called from top level render settings if any
if {$topcall} {
renderSettings
}
return {}
}
proc ml {args} {
# filter out all known options from argument list to guess command name
# without them in the way
lassign [parseModuleCommandArgs 1 ml 1 {*}$args] show_oneperline\
show_mtime show_filter search_filter search_match dump_state\
addpath_pos not_req tag_list fargs
# determine if first argument is a known module sub-command
lassign [parseModuleCommandName [lindex $fargs 0] list] command cmdvalid\
cmdempty
if {$cmdempty} {
# consider empty string supplied as first argument as module name
if {[llength $fargs] > 0} {
set cmdvalid 0
}
set margs $args
} else {
# first argument was command name
set margs [lrange $args 1 end]
}
# directly call module procedure if sub-command spotted as first argument
# or no argument supplied
if {$cmdvalid} {
module $command {*}$margs
} else {
# parse specified module and get list of mods to unload and mods to load
defineParseModuleSpecificationProc [getConf advanced_version_spec]
lassign [parseModuleSpecification 1 {*}$fargs] modunlist modlolist
# main procedure has already raised error for badly written argument
# like '-' or '--', but we need here to replay module-specific argument
# parsing to raise error if some arg are not allowed on unload/load cmd
set mlcmd [expr {[llength $modunlist] > 0 ? {unload} : {load}}]
lassign [parseModuleCommandArgs 1 $mlcmd 0 {*}$args] show_oneperline\
show_mtime show_filter search_filter search_match dump_state\
addpath_pos not_req tag_list fargs
# define if modfile should always be fully read even for validity check
lappendState always_read_full_file 1
lappendState commandname ml
# Find and execute any global rc file found
runModulerc
set ret 0
pushSettings
# first unload specified modules
if {[llength $modunlist] > 0} {
set ret [cmdModuleUnload unload match 1 0 0 0 {*}$modunlist]
}
# then load other modules unless unload phase failed
if {!$ret && [llength $modlolist] > 0} {
set ret [cmdModuleLoad load 1 0 0 $tag_list {*}$modlolist]
}
# rollback changes if any load or unload failed
if {$ret} {
restoreSettings
}
popSettings
lpopState commandname
lpopState always_read_full_file
renderSettings
}
return {}
}
#
# Main program
#
# needed on a gentoo system. Shouldn't hurt since it is
# supposed to be the default behavior
fconfigure stderr -translation auto
if {[catch {
# parse all command-line arguments before doing any action, no output is
# made during argument parse to wait for potential paging to be setup
set show_help 0
set show_version 0
setState cmdline "$argv0 $argv"
# Load extension library if enabled
@libtclenvmodules@if {[file readable [getConf tcl_ext_lib]]} {
@libtclenvmodules@ reportDebug "Load Tcl extension library ([getConf tcl_ext_lib])"
@libtclenvmodules@ load [file normalize [getConf tcl_ext_lib]] Envmodules
@libtclenvmodules@ setState tcl_ext_lib_loaded 1
@libtclenvmodules@}
# use fallback procs if extension library is not loaded
if {[info commands readFile] eq {}} {
rename ::__readFile ::readFile
rename ::__getFilesInDirectory ::getFilesInDirectory
rename ::__initStateUsergroups ::initStateUsergroups
rename ::__initStateUsername ::initStateUsername
rename ::__initStateClockSeconds ::initStateClockSeconds
rename ::__parseDateTimeArg ::parseDateTimeArg
}
# source site configuration script if any
sourceSiteConfig
setState supported_shells {sh bash ksh zsh csh tcsh fish cmd tcl perl\
python ruby lisp cmake r}
# Parse shell
setState shell [lindex $argv 0]
if {[getState shell] ni [getState supported_shells]} {
reportErrorAndExit "Unknown shell type \'([getState shell])\'"
}
switch -- [getState shell] {
sh - bash - ksh - zsh {
setState shelltype sh
}
csh - tcsh {
setState shelltype csh
}
default {
setState shelltype [getState shell]
}
}
# extract options and command switches from other args
set otherargv {}
set extraargv {}
set ddelimarg 0
# split first arg if multi-word string detected for compat with previous
# doc on module usage with scripting language: module('load mod1 mod2')
set argtoparse [if {[llength [lindex $argv 1]] > 1} {list {*}[split\
[lindex $argv 1]] {*}[lrange $argv 2 end]} {lrange $argv 1 end}]
foreach arg $argtoparse {
if {[info exists ignore_next_arg]} {
unset ignore_next_arg
} elseif {[info exists nextargisextraargv]} {
lappend extraargv $arg
unset nextargisextraargv
} elseif {[info exists nextargisval]} {
set $nextargisval $arg
unset nextargisval
} else {
switch -glob -- $arg {
-T - --trace {
set asked_verbosity trace
}
-D - -DD - --debug {
set asked_verbosity [expr {$arg eq {-DD} || ([info exists\
asked_verbosity] && $asked_verbosity in {debug debug2}) ?\
{debug2} : {debug}}]
}
-s - --silent {
set asked_verbosity silent
}
-v - -vv - --verbose {
set asked_verbosity [expr {$arg eq {-vv} || ([info exists\
asked_verbosity] && $asked_verbosity in {verbose verbose2})\
? {verbose2} : {verbose}}]
}
--help - -h {
set show_help 1
}
-V - --version {
set show_version 1
}
--paginate {
set asked_paginate 1
}
--no-pager {
set asked_paginate 0
}
--redirect {
if {[getState shelltype] ni {sh fish}} {
reportWarning "Unsupported option '--redirect' on [getState\
shell] shell"
} else {
set asked_redirect_output 1
}
}
--no-redirect - --no_redirect {
set asked_redirect_output 0
}
--auto {
set asked_auto_handling 1
}
--no-auto {
set asked_auto_handling 0
}
-f - --force {
set asked_force 1
}
--color* {
set asked_color [string range $arg 8 end]
if {$asked_color eq {}} {
set asked_color always
} elseif {$asked_color ni [lindex $::g_config_defs(color) 3]} {
unset asked_color
}
}
-o {
# add with next arg to the command-specific switches
lappend extraargv $arg
set nextargisextraargv 1
}
--width* {
set asked_term_width [string range $arg 8 end]
set term_width_arg --width
if {$asked_term_width eq {}} {
set asked_term_width 0
}
}
-w {
set nextargisval asked_term_width
set term_width_arg -w
}
-t - --terse - -l - --long - --default - -L - --latest - -S -\
--starts-with - -C - --contains - -j - --json - --output=* {
# command-specific switches that can for compatibility be
# passed before the command name, so add them to a specific
# arg list to ensure command name as first position argument
lappend extraargv $arg
}
-d {
# in case of *-path command, -d means --delim
if {$ddelimarg} {
lappend otherargv $arg
} else {
lappend extraargv $arg
}
}
-a - --append - -append - --all - -p - --prepend - -prepend -\
--delim - -delim - --delim=* - -delim=* - --duplicates - --index\
- --notuasked - --indepth - --no-indepth - --dump-state -\
--reset - --tag - --tag=* {
# command-specific switches interpreted later on
lappend otherargv $arg