| 1 |
########################################################################## |
| 2 |
|
| 3 |
# MAIN.TCL, main procedures and code |
| 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 |
# exit in a clean manner by flushing and closing interaction with external |
| 24 |
# components |
| 25 |
proc flushAndExit {} { |
| 26 |
# output all shell code generated on stdout |
| 27 |
renderFlush |
| 28 |
# send messages to log if any and enabled |
| 29 |
logFlush |
| 30 |
# output last messages on the report file descriptor and close it |
| 31 |
reportFlush |
| 32 |
|
| 33 |
exit [expr {[getState error_count] > 0}] |
| 34 |
} |
| 35 |
|
| 36 |
# runs the global RC files if they exist |
| 37 |
proc runModulerc {} { |
| 38 |
setState rc_running 1 |
| 39 |
foreach rc [getGlobalRcFileList] { |
| 40 |
reportDebug "Executing $rc" |
| 41 |
cmdModuleSource load $rc |
| 42 |
lappendState rc_loaded $rc |
| 43 |
} |
| 44 |
unsetState rc_running |
| 45 |
|
| 46 |
# identify alias or symbolic version set in these global RC files to be |
| 47 |
# able to include them or not in output or resolution processes |
| 48 |
array set ::g_rcAlias [array get ::g_moduleAlias] |
| 49 |
array set ::g_rcVersion [array get ::g_moduleVersion] |
| 50 |
array set ::g_rcVirtual [array get ::g_moduleVirtual] |
| 51 |
} |
| 52 |
|
| 53 |
proc aboveCommandName {} { |
| 54 |
return [lindex [getState commandname] end-1] |
| 55 |
} |
| 56 |
|
| 57 |
proc ongoingCommandName {commandName} { |
| 58 |
return [expr {[lsearch -exact [getState commandname] $commandName] != -1}] |
| 59 |
} |
| 60 |
|
| 61 |
# Know if we are currently at the top evaluation level: (1) at the modulecmd |
| 62 |
# level (module command written by user in terminal or script) or (2) during |
| 63 |
# the evaluation of rc or modulefile by a source or autoinit sub-command |
| 64 |
# triggered from modulecmd level or (3) during the evaluation of global rc |
| 65 |
# file. (2) and (3) are considered "extended" top evaluation contexts. |
| 66 |
proc isTopEvaluation {{extended 1}} { |
| 67 |
return [expr {([depthState modulename] == 0 && [currentState eval_context]\ |
| 68 |
in {{} load unload swload swunload}) || ($extended && [depthState\ |
| 69 |
modulename] == 1 && ([aboveCommandName] in {source autoinit} ||\ |
| 70 |
[isStateDefined rc_running]))}] |
| 71 |
} |
| 72 |
|
| 73 |
# analyze/translate command name passed to module |
| 74 |
proc parseModuleCommandName {command defaultcmd} { |
| 75 |
set cmdempty 0 |
| 76 |
|
| 77 |
# resolve command if alias or shortcut name used |
| 78 |
switch -- $command { |
| 79 |
add {set command load} |
| 80 |
try-add {set command try-load} |
| 81 |
add-any {set command load-any} |
| 82 |
rm - remove {set command unload} |
| 83 |
show {set command display} |
| 84 |
apropos - keyword {set command search} |
| 85 |
{} { |
| 86 |
# if empty string supplied translate to default command |
| 87 |
set command $defaultcmd |
| 88 |
set cmdempty 1 |
| 89 |
} |
| 90 |
default { |
| 91 |
# specific match for shortcut names |
| 92 |
set cmdlen [string length $command] |
| 93 |
foreach {match minlen sccmd} {load 2 load unload 4 unload delete 3\ |
| 94 |
unload refresh 3 refresh reload 3 reload switch 2 switch swap 2\ |
| 95 |
switch display 2 display available 2 avail aliases 2 aliases list\ |
| 96 |
2 list whatis 2 whatis purge 2 purge initadd 5 initadd initload 6\ |
| 97 |
initadd initprepend 5 initprepend initswitch 6 initswitch\ |
| 98 |
initswap 6 initswitch initunload 8 initrm initlist 5 initlist\ |
| 99 |
spider 3 spider update 2 reload disable 4 saverm describe 3\ |
| 100 |
saveshow} { |
| 101 |
if {$cmdlen >= $minlen && [string equal -length $cmdlen $command\ |
| 102 |
$match]} { |
| 103 |
set command $sccmd |
| 104 |
break |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
set cmdvalid [expr {$command in [list load unload reload use unuse source\ |
| 111 |
switch display avail aliases path paths list whatis search purge save\ |
| 112 |
restore saverm saveshow savelist initadd initprepend initswitch initrm\ |
| 113 |
initlist initclear autoinit clear config help test prepend-path\ |
| 114 |
append-path remove-path is-loaded is-saved is-used is-avail info-loaded\ |
| 115 |
sh-to-mod edit try-load refresh state load-any lint mod-to-sh reset\ |
| 116 |
stash stashpop stashrm stashshow stashclear stashlist cachebuild\ |
| 117 |
cacheclear spider]}] |
| 118 |
|
| 119 |
reportDebug "(command=$command, cmdvalid=$cmdvalid, cmdempty=$cmdempty)" |
| 120 |
return [list $command $cmdvalid $cmdempty] |
| 121 |
} |
| 122 |
|
| 123 |
# analyze arg list passed to a module cmd to set options |
| 124 |
proc parseModuleCommandArgs {topcall cmd ignerr args} { |
| 125 |
set show_oneperline 0 |
| 126 |
set show_mtime 0 |
| 127 |
set show_filter {} |
| 128 |
set indepth_opt [expr {$cmd eq {spider} ? {spider_indepth} :\ |
| 129 |
{avail_indepth}}] |
| 130 |
set search_filter [expr {[getConf $indepth_opt] ? {} : {noindepth}}] |
| 131 |
set search_match [getConf search_match] |
| 132 |
set dump_state 0 |
| 133 |
set addpath_pos prepend |
| 134 |
set not_req 0 |
| 135 |
set tag_list {} |
| 136 |
set otherargs {} |
| 137 |
|
| 138 |
# parse argument list |
| 139 |
foreach arg $args { |
| 140 |
if {[info exists nextargisval]} { |
| 141 |
##nagelfar vartype nextargisval varName |
| 142 |
set $nextargisval $arg |
| 143 |
unset nextargisval |
| 144 |
} elseif {[info exists nextargisvaltosplit]} { |
| 145 |
##nagelfar vartype nextargisvaltosplit varName |
| 146 |
set $nextargisvaltosplit [split $arg :] |
| 147 |
unset nextargisvaltosplit |
| 148 |
} elseif {[info exists ignore_next_arg]} { |
| 149 |
unset ignore_next_arg |
| 150 |
} else { |
| 151 |
switch -glob -- $arg { |
| 152 |
-j - --json { |
| 153 |
# enable json output only on supported command |
| 154 |
if {$cmd in [list avail savelist stashlist list search\ |
| 155 |
whatis spider]} { |
| 156 |
setState report_format json |
| 157 |
set show_oneperline 0 |
| 158 |
set show_mtime 0 |
| 159 |
} |
| 160 |
} |
| 161 |
-t - --terse { |
| 162 |
set show_oneperline 1 |
| 163 |
set show_mtime 0 |
| 164 |
setState report_format terse |
| 165 |
} |
| 166 |
-l - --long { |
| 167 |
set show_mtime 1 |
| 168 |
set show_oneperline 0 |
| 169 |
setState report_format long |
| 170 |
} |
| 171 |
-o { |
| 172 |
# option is only valid for specific sub-commands |
| 173 |
if {$cmd in [list avail list spider]} { |
| 174 |
set nextargisval asked_output |
| 175 |
set output_arg -o |
| 176 |
} else { |
| 177 |
if {!$ignerr} { |
| 178 |
knerror "Unsupported option '$arg' on $cmd sub-command" |
| 179 |
} |
| 180 |
set ignore_next_arg 1 |
| 181 |
} |
| 182 |
} |
| 183 |
--output=* { |
| 184 |
# option is only valid for specific sub-commands |
| 185 |
if {$cmd in [list avail list spider]} { |
| 186 |
set asked_output [string range $arg 9 end] |
| 187 |
set output_arg --output |
| 188 |
} elseif {!$ignerr} { |
| 189 |
knerror "Unsupported option '--output' on $cmd sub-command" |
| 190 |
} |
| 191 |
} |
| 192 |
--tag=* - --tag { |
| 193 |
# option is only valid for specific sub-commands |
| 194 |
# unload allowed not to raise error on unload/load mixed ml cmd |
| 195 |
if {$cmd in [list load try-load load-any switch unload]} { |
| 196 |
if {$arg eq {--tag}} { |
| 197 |
##nagelfar ignore Found constant |
| 198 |
set nextargisvaltosplit tag_list |
| 199 |
} else { |
| 200 |
set tag_list [split [string range $arg 6 end] :] |
| 201 |
if {![llength $tag_list]} { |
| 202 |
knerror "Missing value for '--tag' option" |
| 203 |
} |
| 204 |
} |
| 205 |
} elseif {!$ignerr} { |
| 206 |
knerror "Unsupported option '--tag' on $cmd sub-command" |
| 207 |
} |
| 208 |
} |
| 209 |
--append - -append { |
| 210 |
if {$cmd eq {use}} { |
| 211 |
set addpath_pos append |
| 212 |
} else { |
| 213 |
lappend otherargs $arg |
| 214 |
} |
| 215 |
} |
| 216 |
-p - --prepend - -prepend { |
| 217 |
if {$cmd eq {use}} { |
| 218 |
set addpath_pos prepend |
| 219 |
} else { |
| 220 |
lappend otherargs $arg |
| 221 |
} |
| 222 |
} |
| 223 |
--all - --show_hidden { |
| 224 |
# include hidden modules only on a limited set of command |
| 225 |
if {$cmd in [list avail aliases search whatis ml list lint\ |
| 226 |
savelist spider]} { |
| 227 |
set ::asked_hiding_threshold 2 |
| 228 |
} else { |
| 229 |
lappend otherargs $arg |
| 230 |
} |
| 231 |
} |
| 232 |
-a { |
| 233 |
# -a option has a different meaning whether sub-command is use |
| 234 |
# or one of the search/listing sub-commands |
| 235 |
if {$cmd eq {use}} { |
| 236 |
set addpath_pos append |
| 237 |
} elseif {$cmd in [list avail aliases search whatis ml list\ |
| 238 |
lint savelist spider]} { |
| 239 |
set ::asked_hiding_threshold 2 |
| 240 |
} else { |
| 241 |
lappend otherargs $arg |
| 242 |
} |
| 243 |
} |
| 244 |
-d - --default { |
| 245 |
# in case of *-path command, -d means --delim |
| 246 |
if {$arg eq {-d} && [string match *-path $cmd]} { |
| 247 |
lappend otherargs $arg |
| 248 |
} else { |
| 249 |
set show_filter onlydefaults |
| 250 |
} |
| 251 |
} |
| 252 |
-L - --latest { |
| 253 |
set show_filter onlylatest |
| 254 |
} |
| 255 |
-C - --contains { |
| 256 |
set search_match contains |
| 257 |
} |
| 258 |
-S - --starts-with { |
| 259 |
set search_match starts_with |
| 260 |
} |
| 261 |
--indepth { |
| 262 |
# empty value means 'in depth' as it is default behavior |
| 263 |
set search_filter {} |
| 264 |
} |
| 265 |
--no-indepth { |
| 266 |
set search_filter noindepth |
| 267 |
} |
| 268 |
--dump-state { |
| 269 |
set dump_state 1 |
| 270 |
} |
| 271 |
--auto - --no-auto - -f - --force { |
| 272 |
reportWarning "Unsupported option '$arg'" |
| 273 |
} |
| 274 |
--not-req { |
| 275 |
if {!$topcall && $cmd in [list load try-load load-any unload\ |
| 276 |
switch]} { |
| 277 |
set not_req 1 |
| 278 |
} else { |
| 279 |
knerror "Unsupported option '$arg' on $cmd sub-command" |
| 280 |
} |
| 281 |
} |
| 282 |
--output { |
| 283 |
knerror "Missing value for '$arg' option" |
| 284 |
} |
| 285 |
default { |
| 286 |
lappend otherargs $arg |
| 287 |
} |
| 288 |
} |
| 289 |
set prevarg $arg |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
if {[info exists nextargisval] || [info exists nextargisvaltosplit]} { |
| 294 |
knerror "Missing value for '$prevarg' option" |
| 295 |
} |
| 296 |
|
| 297 |
foreach tag $tag_list { |
| 298 |
if {$tag in [list loaded auto-loaded forbidden nearly-forbidden\ |
| 299 |
hidden warning]} { |
| 300 |
knerror "Tag '$tag' cannot be manually set" |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
if {[info exists asked_output]} { |
| 305 |
if {[getState report_format] in [list long json]} { |
| 306 |
knerror "Unsupported option '$output_arg' on [getState\ |
| 307 |
report_format] output mode" |
| 308 |
} else { |
| 309 |
# get config name relative to current sub-command and output format |
| 310 |
set outputconf $cmd |
| 311 |
if {[getState report_format] ne {regular}} { |
| 312 |
append outputconf _[getState report_format] |
| 313 |
} |
| 314 |
append outputconf _output |
| 315 |
|
| 316 |
# check option value is coherent with current sub-command (remove |
| 317 |
# append/subtract operator at the start if any) |
| 318 |
set asked_output_list [split [expr {[string index $asked_output 0]\ |
| 319 |
in {+ -} ? [string range $asked_output 1 end] : $asked_output}] :] |
| 320 |
if {[isDiffBetweenList $asked_output_list [lindex\ |
| 321 |
$::g_config_defs($outputconf) 4]]} { |
| 322 |
knerror "Invalid element in value list for '$output_arg' option\ |
| 323 |
on $cmd sub-command\nAllowed elements are: [lindex\ |
| 324 |
$::g_config_defs($outputconf) 4] (separated by ':')" |
| 325 |
} else { |
| 326 |
##nagelfar ignore Suspicious variable name |
| 327 |
set ::asked_$outputconf $asked_output |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
reportDebug "(show_oneperline=$show_oneperline, show_mtime=$show_mtime,\ |
| 333 |
show_filter=$show_filter, search_filter=$search_filter,\ |
| 334 |
search_match=$search_match, dump_state=$dump_state,\ |
| 335 |
addpath_pos=$addpath_pos, not_req=$not_req, tag_list=$tag_list,\ |
| 336 |
otherargs=$otherargs)" |
| 337 |
return [list $show_oneperline $show_mtime $show_filter $search_filter\ |
| 338 |
$search_match $dump_state $addpath_pos $not_req $tag_list $otherargs] |
| 339 |
} |
| 340 |
|
| 341 |
proc module {command args} { |
| 342 |
# guess if called from top level |
| 343 |
set topcall [isTopEvaluation 0] |
| 344 |
set tryhelpmsg [expr {$topcall ? "\nTry 'module --help' for more\ |
| 345 |
information." : {}}] |
| 346 |
if {$topcall} { |
| 347 |
set msgprefix {} |
| 348 |
} else { |
| 349 |
set msgprefix {module: } |
| 350 |
} |
| 351 |
|
| 352 |
# get mode, set to load if called from top level |
| 353 |
set mode [expr {$topcall ? {load} : [currentState mode]}] |
| 354 |
|
| 355 |
# resolve and check command name |
| 356 |
lassign [parseModuleCommandName $command help] command cmdvalid cmdempty |
| 357 |
# clear other args if no command name supplied |
| 358 |
if {$cmdempty} { |
| 359 |
set args {} |
| 360 |
} |
| 361 |
# raise error if supplied command is not known |
| 362 |
if {!$cmdvalid} { |
| 363 |
knerror "${msgprefix}Invalid command '$command'$tryhelpmsg" |
| 364 |
} |
| 365 |
|
| 366 |
# define states needed by module spec parsing and sub-command processing |
| 367 |
# define if modfile should always be fully read even for validity check |
| 368 |
lappendState always_read_full_file [expr {$command ni [list path paths\ |
| 369 |
list avail aliases edit spider]}] |
| 370 |
lappendState commandname $command |
| 371 |
|
| 372 |
# parse options, do that globally to ignore options not related to a given |
| 373 |
# module sub-command (exclude them from arg list) |
| 374 |
lassign [parseModuleCommandArgs $topcall $command 0 {*}$args]\ |
| 375 |
show_oneperline show_mtime show_filter search_filter search_match\ |
| 376 |
dump_state addpath_pos not_req tag_list parsed_args |
| 377 |
|
| 378 |
# parse module version specification |
| 379 |
defineParseModuleSpecificationProc [getConf advanced_version_spec] |
| 380 |
if {$command in [list avail paths whatis load unload switch help test\ |
| 381 |
display path is-avail edit try-load load-any list lint mod-to-sh\ |
| 382 |
source spider]} { |
| 383 |
# some cmds allow to express variant without module name |
| 384 |
set allow_noname_spec [expr {$command in {avail list paths whatis\ |
| 385 |
spider}}] |
| 386 |
# some cmds allow extra specifier in query |
| 387 |
set allow_xt_spec [expr {$command in {avail paths whatis spider}}] |
| 388 |
set parsed_args [parseModuleSpecification 0 $allow_noname_spec\ |
| 389 |
$allow_xt_spec 0 {*}$parsed_args] |
| 390 |
} |
| 391 |
|
| 392 |
if {!$topcall} { |
| 393 |
# some commands can only be called from top level, not within modulefile |
| 394 |
switch -- $command { |
| 395 |
path - paths - autoinit - help - prepend-path - append-path -\ |
| 396 |
remove-path - is-loaded - is-saved - is-used - is-avail -\ |
| 397 |
info-loaded - clear - sh-to-mod - edit - refresh - source - state -\ |
| 398 |
lint - mod-to-sh - reset - stash - stashpop - stashrm - stashshow -\ |
| 399 |
stashclear - stashlist - cachebuild - cacheclear - spider { |
| 400 |
knerror "${msgprefix}Command '$command' not supported$tryhelpmsg" |
| 401 |
} |
| 402 |
} |
| 403 |
# other commands can only be called from modulefile evaluated from |
| 404 |
# command acting as top-level context (source and autoinit) |
| 405 |
if {([depthState modulename] > 1 || [aboveCommandName] ni [list source\ |
| 406 |
autoinit]) && $command eq {config}} { |
| 407 |
knerror "${msgprefix}Command '$command' not supported$tryhelpmsg" |
| 408 |
} |
| 409 |
# no requirement should be recorded this module load/unload/switch cmd |
| 410 |
if {$not_req || ![getConf implicit_requirement]} { |
| 411 |
lappendState inhibit_req_record [currentState evalid] |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
# argument number check |
| 416 |
switch -- $command { |
| 417 |
unload - source - display - initadd - initprepend - initrm - test -\ |
| 418 |
is-avail - try-load - load-any { |
| 419 |
if {![llength $parsed_args]} { |
| 420 |
set argnberr 1 |
| 421 |
} |
| 422 |
} |
| 423 |
refresh - reload - aliases - purge - initlist - initclear - autoinit -\ |
| 424 |
reset - stash - stashclear - stashlist - cacheclear { |
| 425 |
if {[llength $parsed_args]} { |
| 426 |
set argnberr 1 |
| 427 |
} |
| 428 |
} |
| 429 |
switch { |
| 430 |
if {![llength $parsed_args] || [llength $parsed_args] > 2} { |
| 431 |
set argnberr 1 |
| 432 |
} |
| 433 |
} |
| 434 |
path - paths - info-loaded - edit { |
| 435 |
if {[llength $parsed_args] != 1} { |
| 436 |
set argnberr 1 |
| 437 |
} |
| 438 |
} |
| 439 |
search - save - restore - saverm - saveshow - clear - state - stashpop\ |
| 440 |
- stashrm - stashshow { |
| 441 |
if {[llength $parsed_args] > 1} { |
| 442 |
set argnberr 1 |
| 443 |
} |
| 444 |
} |
| 445 |
initswitch { |
| 446 |
if {[llength $parsed_args] != 2} { |
| 447 |
set argnberr 1 |
| 448 |
} |
| 449 |
} |
| 450 |
prepend-path - append-path - remove-path - sh-to-mod - mod-to-sh { |
| 451 |
if {[llength $parsed_args] < 2} { |
| 452 |
set argnberr 1 |
| 453 |
} |
| 454 |
} |
| 455 |
config { |
| 456 |
if {[llength $parsed_args] > 2} { |
| 457 |
set argnberr 1 |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
if {[info exists argnberr]} { |
| 462 |
knerror "Unexpected number of args for '$command' command$tryhelpmsg" |
| 463 |
} |
| 464 |
|
| 465 |
if {$topcall} { |
| 466 |
logEvent requested_cmd {command} $command arguments $args |
| 467 |
# Find and execute any global rc file found |
| 468 |
runModulerc |
| 469 |
} |
| 470 |
|
| 471 |
# define procedure to launch and associated options |
| 472 |
set cmdopts [list] |
| 473 |
switch -- $command { |
| 474 |
load - try-load - load-any { |
| 475 |
set cmdprocsuffix IntLoad |
| 476 |
} |
| 477 |
unload { |
| 478 |
set cmdprocsuffix IntUnload |
| 479 |
} |
| 480 |
switch { |
| 481 |
set cmdprocsuffix IntSwitch |
| 482 |
} |
| 483 |
initadd - initprepend - initswitch - initrm - initlist - initclear { |
| 484 |
set cmdprocsuffix Init |
| 485 |
lappend cmdopts [string range $command 4 end] |
| 486 |
} |
| 487 |
sh-to-mod { |
| 488 |
set cmdprocsuffix ShToMod |
| 489 |
} |
| 490 |
mod-to-sh { |
| 491 |
set cmdprocsuffix ModToSh |
| 492 |
} |
| 493 |
prepend-path - append-path - remove-path - is-loaded - is-saved -\ |
| 494 |
is-used - is-avail { |
| 495 |
set cmdprocsuffix Resurface |
| 496 |
lappend cmdopts $command |
| 497 |
} |
| 498 |
info-loaded { |
| 499 |
set cmdprocsuffix Resurface |
| 500 |
lappend cmdopts module-info loaded |
| 501 |
} |
| 502 |
whatis { |
| 503 |
set cmdprocsuffix Search |
| 504 |
lappend cmdopts {} |
| 505 |
} |
| 506 |
default { |
| 507 |
set cmdprocsuffix [string toupper $command 0 0] |
| 508 |
} |
| 509 |
} |
| 510 |
set cmdprocname cmdModule$cmdprocsuffix |
| 511 |
switch -- $command { |
| 512 |
load - try-load - load-any { |
| 513 |
lappend cmdopts $topcall $command $mode $tag_list |
| 514 |
} |
| 515 |
unload { |
| 516 |
lappend cmdopts $mode |
| 517 |
} |
| 518 |
switch { |
| 519 |
lappend cmdopts $mode $tag_list |
| 520 |
} |
| 521 |
use { |
| 522 |
lappend cmdopts $mode $addpath_pos |
| 523 |
} |
| 524 |
unuse { |
| 525 |
lappend cmdopts $mode |
| 526 |
} |
| 527 |
source { |
| 528 |
lappend cmdopts load |
| 529 |
} |
| 530 |
avail { |
| 531 |
lappend cmdopts $show_oneperline $show_mtime $show_filter\ |
| 532 |
$search_filter $search_match [getModulePathList exiterronundef] |
| 533 |
} |
| 534 |
spider { |
| 535 |
lappend cmdopts $show_oneperline $show_mtime $show_filter\ |
| 536 |
$search_filter $search_match |
| 537 |
} |
| 538 |
list - savelist { |
| 539 |
lappend cmdopts $show_oneperline $show_mtime $search_match |
| 540 |
} |
| 541 |
config { |
| 542 |
lappend cmdopts $dump_state |
| 543 |
} |
| 544 |
stashlist { |
| 545 |
lappend cmdopts $show_oneperline $show_mtime |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
$cmdprocname {*}$cmdopts {*}$parsed_args |
| 550 |
lpopState commandname |
| 551 |
lpopState always_read_full_file |
| 552 |
|
| 553 |
if {!$topcall && ($not_req || ![getConf implicit_requirement])} { |
| 554 |
lpopState inhibit_req_record |
| 555 |
} |
| 556 |
|
| 557 |
# if called from top level render settings if any |
| 558 |
if {$topcall} { |
| 559 |
renderSettings |
| 560 |
} |
| 561 |
|
| 562 |
return {} |
| 563 |
} |
| 564 |
|
| 565 |
proc ml {args} { |
| 566 |
# filter out all known options from argument list to guess command name |
| 567 |
# without them in the way |
| 568 |
lassign [parseModuleCommandArgs 1 ml 1 {*}$args] show_oneperline\ |
| 569 |
show_mtime show_filter search_filter search_match dump_state\ |
| 570 |
addpath_pos not_req tag_list fargs |
| 571 |
|
| 572 |
# determine if first argument is a known module sub-command |
| 573 |
lassign [parseModuleCommandName [lindex $fargs 0] list] command cmdvalid\ |
| 574 |
cmdempty |
| 575 |
|
| 576 |
if {$cmdempty} { |
| 577 |
# consider empty string supplied as first argument as module name |
| 578 |
if {[llength $fargs]} { |
| 579 |
set cmdvalid 0 |
| 580 |
} |
| 581 |
set margs $args |
| 582 |
} else { |
| 583 |
# first argument was command name |
| 584 |
set margs [lrange $args 1 end] |
| 585 |
} |
| 586 |
|
| 587 |
# directly call module procedure if sub-command spotted as first argument |
| 588 |
# or no argument supplied |
| 589 |
if {$cmdvalid} { |
| 590 |
module $command {*}$margs |
| 591 |
} else { |
| 592 |
# define if modfile should always be fully read even for validity check |
| 593 |
lappendState always_read_full_file 1 |
| 594 |
lappendState commandname ml |
| 595 |
|
| 596 |
# parse specified module and get list of mods to unload and mods to load |
| 597 |
defineParseModuleSpecificationProc [getConf advanced_version_spec] |
| 598 |
lassign [parseModuleSpecification 1 0 0 0 {*}$fargs] modunlist modlolist |
| 599 |
|
| 600 |
# main procedure has already raised error for badly written argument |
| 601 |
# like '-' or '--', but we need here to replay module-specific argument |
| 602 |
# parsing to raise error if some arg are not allowed on unload/load cmd |
| 603 |
set mlcmd [expr {[llength $modunlist] ? {unload} : {load}}] |
| 604 |
lassign [parseModuleCommandArgs 1 $mlcmd 0 {*}$args] show_oneperline\ |
| 605 |
show_mtime show_filter search_filter search_match dump_state\ |
| 606 |
addpath_pos not_req tag_list fargs |
| 607 |
|
| 608 |
logEvent requested_cmd {command} ml arguments $args |
| 609 |
|
| 610 |
# Find and execute any global rc file found |
| 611 |
runModulerc |
| 612 |
|
| 613 |
set ret 0 |
| 614 |
pushSettings |
| 615 |
|
| 616 |
# first unload specified modules |
| 617 |
if {[llength $modunlist]} { |
| 618 |
set ret [cmdModuleUnload unload match 1 s 0 {*}$modunlist] |
| 619 |
} |
| 620 |
# then load other modules unless unload phase failed and abort mode |
| 621 |
if {(!$ret || ![commandAbortOnError]) && [llength $modlolist]} { |
| 622 |
set ret [cmdModuleLoad load 1 0 0 $tag_list {} {*}$modlolist] |
| 623 |
} |
| 624 |
|
| 625 |
# rollback changes if abort mode and any load or unload failed |
| 626 |
if {$ret && [commandAbortOnError]} { |
| 627 |
restoreSettings |
| 628 |
} |
| 629 |
popSettings |
| 630 |
|
| 631 |
lpopState commandname |
| 632 |
lpopState always_read_full_file |
| 633 |
|
| 634 |
renderSettings |
| 635 |
} |
| 636 |
|
| 637 |
return {} |
| 638 |
} |
| 639 |
|
| 640 |
# |
| 641 |
# Main program |
| 642 |
# |
| 643 |
|
| 644 |
# needed on a gentoo system. Shouldn't hurt since it is |
| 645 |
# supposed to be the default behavior |
| 646 |
fconfigure stderr -translation auto |
| 647 |
|
| 648 |
if {[catch { |
| 649 |
# parse all command-line arguments before doing any action, no output is |
| 650 |
# made during argument parse to wait for potential paging to be setup |
| 651 |
set show_help 0 |
| 652 |
set show_version 0 |
| 653 |
set show_name 0 |
| 654 |
setState cmdline "$argv0 $argv" |
| 655 |
|
| 656 |
# Load extension library if enabled |
| 657 |
##nagelfar ignore #6 Strange command |
| 658 |
##nagelfar ignore +2 Too long line |
| 659 |
@libtclenvmodules@if {[file readable [getConf tcl_ext_lib]]} { |
| 660 |
@libtclenvmodules@ reportDebug "Load Tcl extension library ([getConf tcl_ext_lib])" |
| 661 |
@libtclenvmodules@ load [file normalize [getConf tcl_ext_lib]] Envmodules |
| 662 |
@libtclenvmodules@ setState tcl_ext_lib_loaded 1 |
| 663 |
@libtclenvmodules@} |
| 664 |
# use fallback procs if extension library is not loaded |
| 665 |
if {[info commands readFile] eq {}} { |
| 666 |
rename ::__readFile ::readFile |
| 667 |
rename ::__getFilesInDirectory ::getFilesInDirectory |
| 668 |
rename ::__initStateUsergroups ::initStateUsergroups |
| 669 |
rename ::__initStateUsername ::initStateUsername |
| 670 |
rename ::__initStateClockSeconds ::initStateClockSeconds |
| 671 |
rename ::__parseDateTimeArg ::parseDateTimeArg |
| 672 |
} |
| 673 |
##nagelfar syntax readFile x x? x? |
| 674 |
##nagelfar syntax getFilesInDirectory x x |
| 675 |
##nagelfar syntax initStateUsergroups |
| 676 |
##nagelfar syntax initStateUsername |
| 677 |
##nagelfar syntax initStateClockSeconds |
| 678 |
##nagelfar syntax parseDateTimeArg x x |
| 679 |
|
| 680 |
# source site configuration script if any |
| 681 |
sourceSiteConfig |
| 682 |
|
| 683 |
setState supported_shells {sh bash ksh zsh csh tcsh fish cmd tcl perl\ |
| 684 |
python ruby lisp cmake r pwsh} |
| 685 |
|
| 686 |
# Parse shell |
| 687 |
setState shell [lindex $argv 0] |
| 688 |
if {[getState shell] ni [getState supported_shells]} { |
| 689 |
reportErrorAndExit "Unknown shell type '([getState shell])'" |
| 690 |
} |
| 691 |
|
| 692 |
# extract options and command switches from other args |
| 693 |
set otherargv {} |
| 694 |
set extraargv {} |
| 695 |
set ddelimarg 0 |
| 696 |
# split first arg if multi-word string detected for compat with previous |
| 697 |
# doc on module usage with scripting language: module('load mod1 mod2') |
| 698 |
##nagelfar ignore #2 Badly formed if statement |
| 699 |
set argtoparse [if {[llength [lindex $argv 1]] > 1} {list {*}[split\ |
| 700 |
[lindex $argv 1]] {*}[lrange $argv 2 end]} {lrange $argv 1 end}] |
| 701 |
foreach arg $argtoparse { |
| 702 |
if {[info exists ignore_next_arg]} { |
| 703 |
unset ignore_next_arg |
| 704 |
} elseif {[info exists nextargisextraargv]} { |
| 705 |
lappend extraargv $arg |
| 706 |
unset nextargisextraargv |
| 707 |
} elseif {[info exists nextargisval]} { |
| 708 |
##nagelfar vartype nextargisval varName |
| 709 |
set $nextargisval $arg |
| 710 |
unset nextargisval |
| 711 |
} else { |
| 712 |
switch -glob -- $arg { |
| 713 |
-T - --trace { |
| 714 |
set asked_verbosity trace |
| 715 |
} |
| 716 |
-D - -DD - --debug { |
| 717 |
set asked_verbosity [expr {$arg eq {-DD} || ([info exists\ |
| 718 |
asked_verbosity] && $asked_verbosity in {debug debug2}) ?\ |
| 719 |
{debug2} : {debug}}] |
| 720 |
} |
| 721 |
-s - --silent { |
| 722 |
set asked_verbosity silent |
| 723 |
} |
| 724 |
-v - -vv - --verbose { |
| 725 |
set asked_verbosity [expr {$arg eq {-vv} || ([info exists\ |
| 726 |
asked_verbosity] && $asked_verbosity in {verbose verbose2})\ |
| 727 |
? {verbose2} : {verbose}}] |
| 728 |
} |
| 729 |
--help - -h { |
| 730 |
set show_help 1 |
| 731 |
} |
| 732 |
-V - --version { |
| 733 |
set show_version 1 |
| 734 |
} |
| 735 |
--dumpname { |
| 736 |
set show_name 1 |
| 737 |
} |
| 738 |
--paginate { |
| 739 |
set asked_paginate 1 |
| 740 |
} |
| 741 |
--no-pager { |
| 742 |
set asked_paginate 0 |
| 743 |
} |
| 744 |
--redirect { |
| 745 |
if {[getState shelltype] ni {sh fish pwsh}} { |
| 746 |
reportWarning "Unsupported option '--redirect' on [getState\ |
| 747 |
shell] shell" |
| 748 |
} else { |
| 749 |
set asked_redirect_output 1 |
| 750 |
} |
| 751 |
} |
| 752 |
--no-redirect - --no_redirect { |
| 753 |
set asked_redirect_output 0 |
| 754 |
} |
| 755 |
--auto { |
| 756 |
set asked_auto_handling 1 |
| 757 |
} |
| 758 |
--no-auto { |
| 759 |
set asked_auto_handling 0 |
| 760 |
} |
| 761 |
-f - --force { |
| 762 |
set asked_force 1 |
| 763 |
} |
| 764 |
--color* { |
| 765 |
set asked_color [string range $arg 8 end] |
| 766 |
if {$asked_color eq {}} { |
| 767 |
set asked_color always |
| 768 |
} elseif {$asked_color ni [lindex $::g_config_defs(color) 4]} { |
| 769 |
unset asked_color |
| 770 |
} |
| 771 |
} |
| 772 |
-o { |
| 773 |
# add with next arg to the command-specific switches |
| 774 |
lappend extraargv $arg |
| 775 |
set nextargisextraargv 1 |
| 776 |
} |
| 777 |
--width* { |
| 778 |
set asked_term_width [string range $arg 8 end] |
| 779 |
set term_width_arg --width |
| 780 |
if {$asked_term_width eq {}} { |
| 781 |
set asked_term_width 0 |
| 782 |
} |
| 783 |
} |
| 784 |
-w { |
| 785 |
##nagelfar ignore Found constant |
| 786 |
set nextargisval asked_term_width |
| 787 |
set term_width_arg -w |
| 788 |
} |
| 789 |
-t - --terse - -l - --long - --default - -L - --latest - -S -\ |
| 790 |
--starts-with - -C - --contains - -j - --json - --output=* { |
| 791 |
# command-specific switches that can for compatibility be |
| 792 |
# passed before the command name, so add them to a specific |
| 793 |
# arg list to ensure command name as first position argument |
| 794 |
lappend extraargv $arg |
| 795 |
} |
| 796 |
-d { |
| 797 |
# in case of *-path command, -d means --delim |
| 798 |
if {$ddelimarg} { |
| 799 |
lappend otherargv $arg |
| 800 |
} else { |
| 801 |
lappend extraargv $arg |
| 802 |
} |
| 803 |
} |
| 804 |
-a - --append - -append - --all - -p - --prepend - -prepend -\ |
| 805 |
--delim - -delim - --delim=* - -delim=* - --duplicates - --index\ |
| 806 |
- --notuasked - --indepth - --no-indepth - --dump-state -\ |
| 807 |
--reset - --tag - --tag=* - --glob - --show_hidden { |
| 808 |
# command-specific switches interpreted later on |
| 809 |
lappend otherargv $arg |
| 810 |
} |
| 811 |
append-path - prepend-path - remove-path { |
| 812 |
# detect *-path commands to say -d means --delim, not --default |
| 813 |
set ddelimarg 1 |
| 814 |
lappend otherargv $arg |
| 815 |
} |
| 816 |
-i - --icase { |
| 817 |
set asked_icase always |
| 818 |
} |
| 819 |
--ignore-cache - --ignore_cache { |
| 820 |
set asked_ignore_cache 1 |
| 821 |
} |
| 822 |
--ignore-user-rc { |
| 823 |
set asked_ignore_user_rc 1 |
| 824 |
} |
| 825 |
--timer { |
| 826 |
setState timer 1 |
| 827 |
set timer_start [clock microseconds] |
| 828 |
} |
| 829 |
--human - -c - --create - --userlvl=* { |
| 830 |
# ignore C-version specific option, no error only warning |
| 831 |
reportWarning "Unsupported option '$arg'" |
| 832 |
} |
| 833 |
-u - --userlvl { |
| 834 |
reportWarning "Unsupported option '$arg'" |
| 835 |
# also ignore argument value |
| 836 |
set ignore_next_arg 1 |
| 837 |
} |
| 838 |
--output { |
| 839 |
reportErrorAndExit "Missing value for '$arg' option\nTry\ |
| 840 |
'module --help' for more information." |
| 841 |
} |
| 842 |
--initial_load { |
| 843 |
# ignore option for compatibility |
| 844 |
} |
| 845 |
{-} - {--} - {--*} { |
| 846 |
reportErrorAndExit "Invalid option '$arg'\nTry 'module --help'\ |
| 847 |
for more information." |
| 848 |
} |
| 849 |
-* { |
| 850 |
# verify current command accepts minus arg (-*) |
| 851 |
if {![info exists accept_minus_arg] && [llength $otherargv]} { |
| 852 |
set subcmdtest [lindex $otherargv 0] |
| 853 |
if {$subcmdtest ne {ml}} { |
| 854 |
lassign [parseModuleCommandName $subcmdtest {}]\ |
| 855 |
subcmdtest |
| 856 |
} |
| 857 |
# accepted if command is ml or config or if adv vers spec is |
| 858 |
# enabled and command can receive boolean variant |
| 859 |
# specification |
| 860 |
set accept_minus_arg [expr {$subcmdtest in {ml config} ||\ |
| 861 |
([getConf advanced_version_spec] && $subcmdtest in\ |
| 862 |
{avail list display help is-avail is-loaded load path\ |
| 863 |
paths switch test unload whatis mod-to-sh source\ |
| 864 |
spider})}] |
| 865 |
} |
| 866 |
# spare argument if minus arg is accepted |
| 867 |
if {[info exists accept_minus_arg] && $accept_minus_arg} { |
| 868 |
lappend otherargv $arg |
| 869 |
} else { |
| 870 |
reportErrorAndExit "Invalid option '$arg'\nTry 'module\ |
| 871 |
--help' for more information." |
| 872 |
} |
| 873 |
} |
| 874 |
default { |
| 875 |
lappend otherargv $arg |
| 876 |
} |
| 877 |
} |
| 878 |
set prevarg $arg |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
if {[info exists nextargisextraargv]} { |
| 883 |
reportErrorAndExit "Missing value for '$prevarg' option\nTry 'module\ |
| 884 |
--help' for more information." |
| 885 |
} |
| 886 |
if {[info exists asked_term_width]} { |
| 887 |
set rangewidth [lindex $::g_config_defs(term_width) 4] |
| 888 |
if {[string is integer -strict $::asked_term_width] && \ |
| 889 |
$::asked_term_width >= [lindex $rangewidth 0] &&\ |
| 890 |
$::asked_term_width <= [lindex $rangewidth 1]} { |
| 891 |
set validval 1 |
| 892 |
} else { |
| 893 |
reportErrorAndExit "Invalid value for option\ |
| 894 |
'$term_width_arg'\nValue should be an integer comprised between\ |
| 895 |
[lindex $rangewidth 0] and [lindex $rangewidth 1]" |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
setState subcmd [lindex $otherargv 0] |
| 900 |
set otherargv [list {*}[lreplace $otherargv 0 0] {*}$extraargv] |
| 901 |
setState subcmd_args $otherargv |
| 902 |
# call ml frontend if it is asked command |
| 903 |
if {[getState subcmd] eq {ml}} { |
| 904 |
set execcmdlist [list ml {*}$otherargv] |
| 905 |
} else { |
| 906 |
set execcmdlist [list module [getState subcmd] {*}$otherargv] |
| 907 |
} |
| 908 |
|
| 909 |
# now options are known initialize error report (start pager if enabled) |
| 910 |
initErrorReport |
| 911 |
|
| 912 |
# put back quarantine variables in env, if quarantine mechanism supported |
| 913 |
if {[info exists env(__MODULES_QUARANTINE_SET)] &&\ |
| 914 |
$env(__MODULES_QUARANTINE_SET) eq {1}} { |
| 915 |
foreach var [split [getConf run_quarantine]] { |
| 916 |
# check variable name is valid |
| 917 |
if {[regexp {^[A-Za-z_][A-Za-z0-9_]*$} $var]} { |
| 918 |
set quarvar __MODULES_QUAR_${var} |
| 919 |
# put back value |
| 920 |
if {[info exists env($quarvar)]} { |
| 921 |
reportDebug "Release '$var' environment variable from\ |
| 922 |
quarantine ($env($quarvar))" |
| 923 |
set env($var) $env($quarvar) |
| 924 |
unset env($quarvar) |
| 925 |
# or unset env var if no value found in quarantine |
| 926 |
} elseif {[info exists env($var)]} { |
| 927 |
reportDebug "Unset '$var' environment variable after\ |
| 928 |
quarantine" |
| 929 |
unset env($var) |
| 930 |
} |
| 931 |
} elseif {[string length $var]} { |
| 932 |
reportWarning "Bad variable name set in MODULES_RUN_QUARANTINE\ |
| 933 |
($var)" |
| 934 |
} |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
if {$show_help} { |
| 939 |
if {[getState subcmd] eq {ml}} { |
| 940 |
reportMlUsage |
| 941 |
} else { |
| 942 |
reportUsage |
| 943 |
} |
| 944 |
flushAndExit |
| 945 |
} |
| 946 |
if {$show_version} { |
| 947 |
reportVersion |
| 948 |
flushAndExit |
| 949 |
} |
| 950 |
if {$show_name} { |
| 951 |
reportName |
| 952 |
flushAndExit |
| 953 |
} |
| 954 |
|
| 955 |
# eval needed to pass otherargv as list to module proc |
| 956 |
{*}$execcmdlist |
| 957 |
} errMsg ]} { |
| 958 |
# re-enable error report in case it was previously inhibited |
| 959 |
setState inhibit_errreport 0 |
| 960 |
# remove any message record id to render next error |
| 961 |
clearAllMsgRecordId |
| 962 |
reportFinalError $errMsg |
| 963 |
# init error report here in case the error raised before the regular init |
| 964 |
initErrorReport |
| 965 |
flushAndExit |
| 966 |
} |
| 967 |
|
| 968 |
flushAndExit |
| 969 |
|
| 970 |
# ;;; Local Variables: *** |
| 971 |
# ;;; mode:tcl *** |
| 972 |
# ;;; End: *** |
| 973 |
# vim:set tabstop=3 shiftwidth=3 expandtab autoindent: |