| 1 |
########################################################################## |
| 2 |
|
| 3 |
# INIT.TCL, configuration and state handing procedures |
| 4 |
# Copyright (C) 2002-2004 Mark Lakata |
| 5 |
# Copyright (C) 2004-2017 Kent Mein |
| 6 |
# Copyright (C) 2016-2025 Xavier Delaruelle |
| 7 |
# |
| 8 |
# This program is free software: you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU General Public License as published by |
| 10 |
# the Free Software Foundation, either version 2 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# This program is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU General Public License |
| 19 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
########################################################################## |
| 22 |
|
| 23 |
# Runtime state properties (default value, proc to call to initialize state |
| 24 |
# value?) |
| 25 |
##nagelfar ignore +38 Found constant |
| 26 |
array set g_state_defs [list\ |
| 27 |
autoinit {0}\ |
| 28 |
cache_mcookie_version {5.3}\ |
| 29 |
clock_seconds {<undef> initStateClockSeconds}\ |
| 30 |
domainname {<undef> {runCommand domainname}}\ |
| 31 |
error_count {0}\ |
| 32 |
extra_siteconfig_loaded {0}\ |
| 33 |
false_rendered {0}\ |
| 34 |
force {0}\ |
| 35 |
hiding_threshold {0 initStateHidingThreshold}\ |
| 36 |
inhibit_errreport {0}\ |
| 37 |
inhibit_interp {0}\ |
| 38 |
init_error_report {0}\ |
| 39 |
is_stderr_tty {<undef> initStateIsStderrTty}\ |
| 40 |
is_win {<undef> initStateIsWin}\ |
| 41 |
kernelversion {<undef> {runCommand uname -v}}\ |
| 42 |
lm_info_cached {0}\ |
| 43 |
logfd {{} initStateLogfd}\ |
| 44 |
logging {<undef> initStateLogging}\ |
| 45 |
lsb_codename {<undef> {runCommand lsb_release -s -c}}\ |
| 46 |
lsb_id {<undef> {runCommand lsb_release -s -i}}\ |
| 47 |
lsb_release {<undef> {runCommand lsb_release -s -r}}\ |
| 48 |
machine [list $::tcl_platform(machine)]\ |
| 49 |
modules_release {@MODULES_RELEASE@}\ |
| 50 |
nodename {<undef> {runCommand uname -n}}\ |
| 51 |
os [list $::tcl_platform(os)]\ |
| 52 |
osversion [list $::tcl_platform(osVersion)]\ |
| 53 |
paginate {<undef> initStatePaginate}\ |
| 54 |
path_separator {<undef> initStatePathSeparator}\ |
| 55 |
report_format {regular}\ |
| 56 |
reportfd {stderr initStateReportfd}\ |
| 57 |
return_false {0}\ |
| 58 |
siteconfig_loaded {0}\ |
| 59 |
shelltype {<undef> initStateShellType}\ |
| 60 |
sub1_separator {&}\ |
| 61 |
sub2_separator {|}\ |
| 62 |
tcl_ext_lib_loaded {0}\ |
| 63 |
tcl_version [list [info patchlevel]]\ |
| 64 |
term_columns {<undef> initStateTermColumns}\ |
| 65 |
timer {0}\ |
| 66 |
usergroups {<undef> initStateUsergroups}\ |
| 67 |
username {<undef> initStateUsername}\ |
| 68 |
] |
| 69 |
|
| 70 |
# Configuration option properties (superseding environment variable, default |
| 71 |
# value, is configuration lockable to default value, value kind, valid value |
| 72 |
# list?, internal value representation?, proc to call to initialize option |
| 73 |
# value, valid value list kind |
| 74 |
##nagelfar ignore #81 Too long line |
| 75 |
array set g_config_defs [list\ |
| 76 |
contact {MODULECONTACT root@localhost 0 s}\ |
| 77 |
abort_on_error {MODULES_ABORT_ON_ERROR {@abortonerror@} 0 l {load ml\ |
| 78 |
mod-to-sh purge reload switch switch_unload try-load unload} {} {}\ |
| 79 |
eltlist}\ |
| 80 |
auto_handling {MODULES_AUTO_HANDLING @autohandling@ 0 b {0 1}}\ |
| 81 |
avail_indepth {MODULES_AVAIL_INDEPTH @availindepth@ 0 b {0 1}}\ |
| 82 |
avail_output {MODULES_AVAIL_OUTPUT {@availoutput@} 0 l {modulepath alias\ |
| 83 |
provided-alias dirwsym indesym sym tag key hidden variant variantifspec\ |
| 84 |
via} {} {} eltlist}\ |
| 85 |
avail_terse_output {MODULES_AVAIL_TERSE_OUTPUT {@availterseoutput@} 0 l\ |
| 86 |
{modulepath alias provided-alias dirwsym indesym sym tag key hidden\ |
| 87 |
variant variantifspec} {} {} eltlist}\ |
| 88 |
cache_buffer_bytes {MODULES_CACHE_BUFFER_BYTES 32768 0 i {4096 1000000} {}\ |
| 89 |
{} intbe}\ |
| 90 |
cache_expiry_secs {MODULES_CACHE_EXPIRY_SECS 0 0 i {0 31536000} {} {}\ |
| 91 |
intbe}\ |
| 92 |
collection_pin_version {MODULES_COLLECTION_PIN_VERSION 0 0 b {0 1}}\ |
| 93 |
collection_pin_tag {MODULES_COLLECTION_PIN_TAG 0 0 b {0 1}}\ |
| 94 |
collection_target {MODULES_COLLECTION_TARGET <undef> 0 s}\ |
| 95 |
color {MODULES_COLOR @color@ 0 s {never auto always} {0 1 2}\ |
| 96 |
initConfColor}\ |
| 97 |
colors {MODULES_COLORS {} 0 l {} {} initConfColors}\ |
| 98 |
conflict_unload {MODULES_CONFLICT_UNLOAD @conflictunload@ 0 b {0 1}}\ |
| 99 |
csh_limit {{} 4000 0 i}\ |
| 100 |
extra_siteconfig {MODULES_SITECONFIG <undef> 1 s {}}\ |
| 101 |
editor {MODULES_EDITOR {@editor@} 0 s {} {} initConfEditor}\ |
| 102 |
hide_auto_loaded {MODULES_HIDE_AUTO_LOADED 0 0 b {0 1}}\ |
| 103 |
home {MODULESHOME {@moduleshome@} 0 s}\ |
| 104 |
icase {MODULES_ICASE @icase@ 0 s {never search always}}\ |
| 105 |
ignore_cache {MODULES_IGNORE_CACHE 0 0 b {0 1}}\ |
| 106 |
ignore_user_rc {MODULES_IGNORE_USER_RC 0 0 b {0 1}}\ |
| 107 |
ignored_dirs {{} {CVS RCS SCCS .svn .git .SYNC .sos} 0 o}\ |
| 108 |
implicit_requirement {MODULES_IMPLICIT_REQUIREMENT @implicitrequirement@ 0\ |
| 109 |
b {0 1}}\ |
| 110 |
list_output {MODULES_LIST_OUTPUT {@listoutput@} 0 l {header idx variant\ |
| 111 |
alias indesym sym tag hidden key} {} {} eltlist}\ |
| 112 |
list_terse_output {MODULES_LIST_TERSE_OUTPUT {@listterseoutput@} 0 l\ |
| 113 |
{header idx variant alias indesym sym tag hidden key} {} {} eltlist}\ |
| 114 |
locked_configs {{} {@lockedconfigs@} 0 o}\ |
| 115 |
logged_events {MODULES_LOGGED_EVENTS {@loggedevents@} 1 l {auto_eval\ |
| 116 |
requested_eval requested_cmd} {} {} eltlist}\ |
| 117 |
logger {MODULES_LOGGER {@loggercmd@} 1 s}\ |
| 118 |
mcookie_check {MODULES_MCOOKIE_CHECK always 0 s {eval always}}\ |
| 119 |
mcookie_version_check {MODULES_MCOOKIE_VERSION_CHECK\ |
| 120 |
@mcookieversioncheck@ 0 b {0 1}}\ |
| 121 |
ml {MODULES_ML @ml@ 0 b {0 1}}\ |
| 122 |
nearly_forbidden_days {MODULES_NEARLY_FORBIDDEN_DAYS @nearlyforbiddendays@\ |
| 123 |
0 i {0 365} {} {} intbe}\ |
| 124 |
pager {MODULES_PAGER {@pagercmd@} 0 s}\ |
| 125 |
protected_envvars {MODULES_PROTECTED_ENVVARS <undef> 0 l}\ |
| 126 |
rcfile {MODULERCFILE <undef> 0 l}\ |
| 127 |
redirect_output {MODULES_REDIRECT_OUTPUT 1 0 b {0 1}}\ |
| 128 |
require_via {MODULES_REQUIRE_VIA @requirevia@ 0 b {0 1}}\ |
| 129 |
reset_target_state {MODULES_RESET_TARGET_STATE __init__ 0 s}\ |
| 130 |
quarantine_support {MODULES_QUARANTINE_SUPPORT @quarantinesupport@ 0 b {0\ |
| 131 |
1}}\ |
| 132 |
run_quarantine {MODULES_RUN_QUARANTINE <undef> 0 o}\ |
| 133 |
shells_with_ksh_fpath {MODULES_SHELLS_WITH_KSH_FPATH {} 0 l {sh bash csh\ |
| 134 |
tcsh fish} {} {} eltlist}\ |
| 135 |
silent_shell_debug {MODULES_SILENT_SHELL_DEBUG @silentshdbgsupport@ 0 b {0\ |
| 136 |
1}}\ |
| 137 |
siteconfig {{} {@etcdir@/siteconfig.tcl} 0 s}\ |
| 138 |
spider_indepth {MODULES_SPIDER_INDEPTH @spiderindepth@ 0 b {0 1}}\ |
| 139 |
spider_output {MODULES_SPIDER_OUTPUT {@spideroutput@} 0 l {modulepath\ |
| 140 |
alias provided-alias dirwsym indesym sym tag key hidden variant\ |
| 141 |
variantifspec via} {} {} eltlist}\ |
| 142 |
spider_terse_output {MODULES_SPIDER_TERSE_OUTPUT {@spiderterseoutput@} 0 l\ |
| 143 |
{modulepath alias provided-alias dirwsym indesym sym tag key hidden\ |
| 144 |
variant variantifspec} {} {} eltlist}\ |
| 145 |
source_cache {MODULES_SOURCE_CACHE @sourcecache@ 0 b {0 1}}\ |
| 146 |
sticky_purge {MODULES_STICKY_PURGE {@stickypurge@} 0 s {error warning\ |
| 147 |
silent}}\ |
| 148 |
tag_abbrev {MODULES_TAG_ABBREV {@tagabbrev@} 0 l {} {} initConfTagAbbrev}\ |
| 149 |
tag_color_name {MODULES_TAG_COLOR_NAME {@tagcolorname@} 0 l {} {}\ |
| 150 |
initConfTagColorName}\ |
| 151 |
tcl_ext_lib {{} {} 0 s {} {} initConfTclExtLib}\ |
| 152 |
tcl_linter {MODULES_TCL_LINTER {@tcllintercmd@} 0 s}\ |
| 153 |
term_background {MODULES_TERM_BACKGROUND @termbg@ 0 s {dark light}}\ |
| 154 |
term_width {MODULES_TERM_WIDTH 0 0 i {0 1000} {} {} intbe}\ |
| 155 |
unique_name_loaded {MODULES_UNIQUE_NAME_LOADED @uniquenameloaded@ 0 b {0\ |
| 156 |
1}}\ |
| 157 |
unload_match_order {MODULES_UNLOAD_MATCH_ORDER @unloadmatchorder@ 0 s\ |
| 158 |
{returnlast returnfirst}}\ |
| 159 |
implicit_default {MODULES_IMPLICIT_DEFAULT @implicitdefault@ 1 b {0 1}}\ |
| 160 |
extended_default {MODULES_EXTENDED_DEFAULT @extendeddefault@ 0 b {0 1}}\ |
| 161 |
advanced_version_spec {MODULES_ADVANCED_VERSION_SPEC @advversspec@ 0 b {0\ |
| 162 |
1}}\ |
| 163 |
search_match {MODULES_SEARCH_MATCH @searchmatch@ 0 s {starts_with\ |
| 164 |
contains}}\ |
| 165 |
set_shell_startup {MODULES_SET_SHELL_STARTUP @setshellstartup@ 0 b {0 1}}\ |
| 166 |
variant_shortcut {MODULES_VARIANT_SHORTCUT {@variantshortcut@} 0 l {} {}\ |
| 167 |
initConfVariantShortcut}\ |
| 168 |
verbosity {MODULES_VERBOSITY @verbosity@ 0 s {silent concise normal\ |
| 169 |
verbose verbose2 trace debug debug2}}\ |
| 170 |
wa_277 {MODULES_WA_277 @wa277@ 0 b {0 1}}\ |
| 171 |
] |
| 172 |
|
| 173 |
# Get state value |
| 174 |
proc getState {state {valifundef {}} {catchinitproc 0}} { |
| 175 |
if {![info exists ::g_states($state)]} { |
| 176 |
# fetch state properties (including its default value) if defined |
| 177 |
if {[info exists ::g_state_defs($state)]} { |
| 178 |
lassign $::g_state_defs($state) value initproclist |
| 179 |
} else { |
| 180 |
set value <undef> |
| 181 |
set initproclist {} |
| 182 |
} |
| 183 |
|
| 184 |
# call specific proc to initialize state if any |
| 185 |
if {$initproclist ne {}} { |
| 186 |
# catch init procedure error and report it as warning, so default |
| 187 |
# value will get set for state |
| 188 |
if {$catchinitproc} { |
| 189 |
if {[catch {set value [{*}$initproclist]} errMsg]} { |
| 190 |
reportWarning $errMsg |
| 191 |
} |
| 192 |
} else { |
| 193 |
set value [{*}$initproclist] |
| 194 |
} |
| 195 |
# overridden value coming the command-line |
| 196 |
##nagelfar ignore Suspicious variable |
| 197 |
} elseif {[info exists ::asked_$state]} { |
| 198 |
set value [set ::asked_$state] |
| 199 |
} |
| 200 |
|
| 201 |
# return passed value if undefined and no value record |
| 202 |
if {$value eq {<undef>}} { |
| 203 |
set value $valifundef |
| 204 |
} else { |
| 205 |
setState $state $value |
| 206 |
} |
| 207 |
return $value |
| 208 |
} else { |
| 209 |
return $::g_states($state) |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
# Clear state |
| 214 |
proc unsetState {state} { |
| 215 |
if {[isStateDefined $state]} { |
| 216 |
unset ::g_states($state) |
| 217 |
reportDebug "$state unset" |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
# Set state value |
| 222 |
proc setState {state value} { |
| 223 |
set ::g_states($state) $value |
| 224 |
reportDebug "$state set to '$value'" |
| 225 |
} |
| 226 |
|
| 227 |
# Append each passed value to the existing state value list |
| 228 |
proc lappendState {state args} { |
| 229 |
if {$state eq {-nodup}} { |
| 230 |
set state [lindex $args 0] |
| 231 |
# retrieve current val through getState to initialize it if still undef |
| 232 |
set value [getState $state] |
| 233 |
##nagelfar ignore Found constant |
| 234 |
appendNoDupToList value {*}[lrange $args 1 end] |
| 235 |
setState $state $value |
| 236 |
} else { |
| 237 |
lappend ::g_states($state) {*}$args |
| 238 |
reportDebug "$state appended with '$args'" |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
# Remove last element from state value list |
| 243 |
proc lpopState {state} { |
| 244 |
setState $state [lrange [getState $state] 0 end-1] |
| 245 |
} |
| 246 |
|
| 247 |
# Return first element from state value list |
| 248 |
proc topState {state} { |
| 249 |
return [lindex [getState $state] 0] |
| 250 |
} |
| 251 |
|
| 252 |
# Return last element from state value list |
| 253 |
proc currentState {state} { |
| 254 |
return [lindex [getState $state] end] |
| 255 |
} |
| 256 |
|
| 257 |
# Get number of element from state value list |
| 258 |
proc depthState {state} { |
| 259 |
return [llength [getState $state]] |
| 260 |
} |
| 261 |
|
| 262 |
# Check if state has been defined |
| 263 |
proc isStateDefined {state} { |
| 264 |
return [info exists ::g_states($state)] |
| 265 |
} |
| 266 |
|
| 267 |
# Check if state equals passed value |
| 268 |
proc isStateEqual {state value} { |
| 269 |
return [expr {[getState $state] eq $value}] |
| 270 |
} |
| 271 |
|
| 272 |
proc isConfigLocked {option} { |
| 273 |
return [expr {[lsearch -exact [getConf locked_configs] $option] != -1}] |
| 274 |
} |
| 275 |
|
| 276 |
# Get configuration option value |
| 277 |
proc getConf {option {valifundef {}}} { |
| 278 |
if {![info exists ::g_configs($option)]} { |
| 279 |
# fetch option properties (including its default value) |
| 280 |
lassign $::g_config_defs($option) envvar value islockable valuekind\ |
| 281 |
validvallist intvallist initproc validvallistkind |
| 282 |
|
| 283 |
# ensure option is not locked before superseding its default value |
| 284 |
if {!$islockable || ![isConfigLocked $option]} { |
| 285 |
# call specific proc to initialize config option if any |
| 286 |
if {$initproc ne {}} { |
| 287 |
set value [$initproc $envvar $value $validvallist $intvallist] |
| 288 |
} else { |
| 289 |
# overridden value coming from environment |
| 290 |
if {$envvar ne {} && [isEnvVarDefined $envvar]} { |
| 291 |
switch -- $validvallistkind { |
| 292 |
eltlist { |
| 293 |
# ignore non-valid values |
| 294 |
if {![isDiffBetweenList [split $::env($envvar) :]\ |
| 295 |
$validvallist]} { |
| 296 |
set value $::env($envvar) |
| 297 |
} |
| 298 |
} |
| 299 |
intbe { |
| 300 |
# ignore non-valid values |
| 301 |
if {[string is integer -strict $::env($envvar)] &&\ |
| 302 |
$::env($envvar) >= [lindex $validvallist 0] &&\ |
| 303 |
$::env($envvar) <= [lindex $validvallist 1]} { |
| 304 |
set value $::env($envvar) |
| 305 |
} |
| 306 |
} |
| 307 |
{} { |
| 308 |
# ignore non-valid values |
| 309 |
##nagelfar ignore +3 Non static subcommand |
| 310 |
if {[switch -- [llength $validvallist] { |
| 311 |
0 {expr {1 == 1}} |
| 312 |
1 {string is $validvallist -strict $::env($envvar)} |
| 313 |
default {expr {$::env($envvar) in $validvallist}} |
| 314 |
}]} { |
| 315 |
set value $::env($envvar) |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
# overridden value coming the command-line (already validated) |
| 322 |
##nagelfar ignore Suspicious variable |
| 323 |
if {[info exists ::asked_$option]} { |
| 324 |
set askedval [set ::asked_$option] |
| 325 |
# append or subtract value to existing configuration value if |
| 326 |
# new value starts with '+' or '-' (for colon-separated list |
| 327 |
# option only) |
| 328 |
if {$valuekind eq {l} && [string index $askedval 0] in {+ -}} { |
| 329 |
set curvaluelist [split $value :] |
| 330 |
switch -- [string index $askedval 0] { |
| 331 |
+ { |
| 332 |
##nagelfar ignore Found constant |
| 333 |
appendNoDupToList curvaluelist {*}[split [string\ |
| 334 |
range $askedval 1 end] :] |
| 335 |
} |
| 336 |
- { |
| 337 |
lassign [getDiffBetweenList $curvaluelist [split\ |
| 338 |
[string range $askedval 1 end] :]] curvaluelist |
| 339 |
} |
| 340 |
} |
| 341 |
set value [join $curvaluelist :] |
| 342 |
} else { |
| 343 |
set value $askedval |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
# convert value to its internal representation |
| 348 |
if {[llength $intvallist]} { |
| 349 |
set value [lindex $intvallist [lsearch -exact $validvallist\ |
| 350 |
$value]] |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
# return passed value if undefined and no value record |
| 356 |
if {$value eq {<undef>}} { |
| 357 |
set value $valifundef |
| 358 |
} else { |
| 359 |
setConf $option $value |
| 360 |
} |
| 361 |
return $value |
| 362 |
} else { |
| 363 |
return $::g_configs($option) |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
# Set configuration option value |
| 368 |
proc setConf {option value} { |
| 369 |
set ::g_configs($option) $value |
| 370 |
reportDebug "$option set to '$value'" |
| 371 |
} |
| 372 |
|
| 373 |
# Unset configuration option value if it is set |
| 374 |
proc unsetConf {option} { |
| 375 |
if {[info exists ::g_configs($option)]} { |
| 376 |
unset ::g_configs($option) |
| 377 |
reportDebug "$option unset" |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
# Append each passed value to the existing config option value list |
| 382 |
proc lappendConf {option args} { |
| 383 |
# retrieve current value through getConf to initialize it if still undef |
| 384 |
set value [getConf $option] |
| 385 |
##nagelfar ignore Found constant |
| 386 |
appendNoDupToList value {*}$args |
| 387 |
setConf $option $value |
| 388 |
} |
| 389 |
|
| 390 |
# Get configuration option value split as a list |
| 391 |
proc getConfList {option {valifundef {}}} { |
| 392 |
return [split [getConf $option $valifundef] :] |
| 393 |
} |
| 394 |
|
| 395 |
# Source site config which can be used to define global procedures or |
| 396 |
# settings. We first look for the global siteconfig, then if an extra |
| 397 |
# siteconfig is defined and allowed, source that file if it exists |
| 398 |
proc sourceSiteConfig {} { |
| 399 |
lappend siteconfiglist [getConf siteconfig] |
| 400 |
for {set i 0} {$i < [llength $siteconfiglist]} {incr i} { |
| 401 |
set siteconfig [lindex $siteconfiglist $i] |
| 402 |
if {[file readable $siteconfig]} { |
| 403 |
reportDebug "Source site configuration ($siteconfig)" |
| 404 |
if {[catch {uplevel 1 source "{$siteconfig}"} errMsg]} { |
| 405 |
set errMsg "Site configuration source failed\n" |
| 406 |
# issue line number is lost due to uplevel use |
| 407 |
append errMsg [formatErrStackTrace $::errorInfo $siteconfig {}] |
| 408 |
reportErrorAndExit $errMsg |
| 409 |
} |
| 410 |
##nagelfar ignore Found constant |
| 411 |
if {$siteconfig eq [getConf siteconfig]} { |
| 412 |
setState siteconfig_loaded 1 |
| 413 |
} else { |
| 414 |
setState extra_siteconfig_loaded 1 |
| 415 |
} |
| 416 |
} |
| 417 |
# check on extra_siteconfig after initial siteconfig loaded in case |
| 418 |
# it inhibits this extra load |
| 419 |
##nagelfar ignore Found constant |
| 420 |
if {$siteconfig eq [getConf siteconfig] && [getConf\ |
| 421 |
extra_siteconfig] ne {}} { |
| 422 |
lappend siteconfiglist [getConf extra_siteconfig] |
| 423 |
} |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
# Used to tell if a machine is running Windows or not |
| 428 |
proc initStateIsWin {} { |
| 429 |
return [expr {$::tcl_platform(platform) eq {windows}}] |
| 430 |
} |
| 431 |
|
| 432 |
# Get default path separator |
| 433 |
proc initStatePathSeparator {} { |
| 434 |
return [expr {[getState is_win] ? {;} : {:}}] |
| 435 |
} |
| 436 |
|
| 437 |
# Detect if terminal is attached to stderr message channel |
| 438 |
proc initStateIsStderrTty {} { |
| 439 |
return [expr {![catch {fconfigure stderr -mode}]}] |
| 440 |
} |
| 441 |
|
| 442 |
# Determine if pagination need to be started |
| 443 |
proc initStatePaginate {} { |
| 444 |
set pager [getConf pager] |
| 445 |
# empty or 'cat' pager command means no-pager |
| 446 |
set no_cmds [list {} cat] |
| 447 |
set only_shell_types [list sh csh fish cmd pwsh] |
| 448 |
# default pager enablement depends on pager command value and current shell |
| 449 |
set paginate [expr {[file tail [lindex $pager 0]] ni $no_cmds && [getState\ |
| 450 |
shelltype] in $only_shell_types}] |
| 451 |
|
| 452 |
# asked enablement could only nullify a previous asked disablement as it |
| 453 |
# requires a valid pager command configuration, which by default enables |
| 454 |
# pagination; some module command may also turn off pager; also if error |
| 455 |
# stream is not attached to a terminal |
| 456 |
set no_subcmds [list clear edit] |
| 457 |
if {$paginate && (([info exists ::asked_paginate] && !$::asked_paginate)\ |
| 458 |
|| [getState subcmd] in $no_subcmds || ([getState subcmd] eq {ml} &&\ |
| 459 |
[lindex [getState subcmd_args] 0] in $no_subcmds) || ![getState\ |
| 460 |
is_stderr_tty])} { |
| 461 |
set paginate 0 |
| 462 |
} |
| 463 |
|
| 464 |
return $paginate |
| 465 |
} |
| 466 |
|
| 467 |
# start pager pipe process with defined configuration |
| 468 |
proc initStateReportfd {} { |
| 469 |
# get default value |
| 470 |
lassign $::g_state_defs(reportfd) reportfd |
| 471 |
|
| 472 |
# start pager at first call and only if enabled |
| 473 |
if {[getState paginate]} { |
| 474 |
if {[catch { |
| 475 |
set reportfd [open "|[getConf pager] >@stderr 2>@stderr" w] |
| 476 |
fconfigure $reportfd -buffering line -blocking 1 -buffersize 65536 |
| 477 |
} errMsg]} { |
| 478 |
# silently set reportfd to its fallback value to process warn msg |
| 479 |
set ::g_states(reportfd) $reportfd |
| 480 |
reportWarning $errMsg |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
# startup content in case of structured output format (puts here rather |
| 485 |
# calling report proc to avoid infinite reportfd init loop |
| 486 |
if {[isStateEqual report_format json]} { |
| 487 |
puts -nonewline $reportfd \{ |
| 488 |
} |
| 489 |
|
| 490 |
return $reportfd |
| 491 |
} |
| 492 |
|
| 493 |
# Determine if logging need to be started |
| 494 |
proc initStateLogging {} { |
| 495 |
set logger_not_empty [string length [lindex [getConf logger] 0]] |
| 496 |
set something_to_log [info exists ::g_log_msg_list] |
| 497 |
return [expr {$logger_not_empty && $something_to_log}] |
| 498 |
} |
| 499 |
|
| 500 |
# start logger pipe process with defined configuration |
| 501 |
proc initStateLogfd {} { |
| 502 |
# sets default fallback value |
| 503 |
lassign $::g_state_defs(logfd) logfd |
| 504 |
|
| 505 |
# start logger at first call and only if enabled |
| 506 |
if {[getState logging]} { |
| 507 |
if {[catch { |
| 508 |
# drop output of logger command to avoid it pollutes main channels |
| 509 |
set logfd [open "|[getConf logger] >/dev/null 2>/dev/null" w] |
| 510 |
fconfigure $logfd -buffering none -blocking 1 |
| 511 |
} errMsg]} { |
| 512 |
reportWarning $errMsg |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
return $logfd |
| 517 |
} |
| 518 |
|
| 519 |
# Provide columns number for output formatting |
| 520 |
proc initStateTermColumns {} { |
| 521 |
set cols [getConf term_width] |
| 522 |
if {$cols == 0} { |
| 523 |
# determine col number from tty capabilities |
| 524 |
# tty info query depends on running OS |
| 525 |
switch -- $::tcl_platform(os) { |
| 526 |
SunOS { |
| 527 |
catch {regexp {columns = (\d+);} [exec stty] match cols} errMsg |
| 528 |
} |
| 529 |
{Windows NT} { |
| 530 |
catch {regexp {Columns:\s+(\d+)} [exec mode] match cols} errMsg |
| 531 |
} |
| 532 |
default { |
| 533 |
catch {set cols [lindex [exec stty size] 1]} errMsg |
| 534 |
} |
| 535 |
} |
| 536 |
# default size if tty cols cannot be found |
| 537 |
set cols [expr {![info exists cols] || $cols eq {0} ? 80 : $cols}] |
| 538 |
} |
| 539 |
return $cols |
| 540 |
} |
| 541 |
|
| 542 |
# Deduce shelltype value from shell state value |
| 543 |
proc initStateShellType {} { |
| 544 |
switch -- [getState shell] { |
| 545 |
sh - bash - ksh - zsh { |
| 546 |
return sh |
| 547 |
} |
| 548 |
csh - tcsh { |
| 549 |
return csh |
| 550 |
} |
| 551 |
default { |
| 552 |
return [getState shell] |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
proc initStateHidingThreshold {} { |
| 558 |
# sets default fallback value |
| 559 |
lassign $::g_state_defs(hiding_threshold) hiding_threshold |
| 560 |
|
| 561 |
if {[isEltInReport hidden 0]} { |
| 562 |
set hiding_threshold 2 |
| 563 |
} elseif {[info exists ::asked_hiding_threshold]} { |
| 564 |
set hiding_threshold $::asked_hiding_threshold |
| 565 |
} |
| 566 |
|
| 567 |
return $hiding_threshold |
| 568 |
} |
| 569 |
|
| 570 |
# Get all groups of user running modulecmd.tcl process |
| 571 |
proc __initStateUsergroups {} { |
| 572 |
# ensure groups including space in their name (found on Cygwin/MSYS |
| 573 |
# platforms) are correctly set as list element |
| 574 |
if {[catch { |
| 575 |
return [split [string range [runCommand id -G -n -z] 0 end-1] \0] |
| 576 |
} errMsg]} { |
| 577 |
# fallback if '-z' option is not supported |
| 578 |
return [runCommand id -G -n] |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
# Get name of user running modulecmd.tcl process |
| 583 |
proc __initStateUsername {} { |
| 584 |
return [runCommand id -u -n] |
| 585 |
} |
| 586 |
|
| 587 |
# Get Epoch time (number of seconds elapsed since Unix epoch) |
| 588 |
proc __initStateClockSeconds {} { |
| 589 |
return [clock seconds] |
| 590 |
} |
| 591 |
|
| 592 |
# Initialize Select Graphic Rendition table |
| 593 |
proc initConfColors {envvar value validvallist intvallist} { |
| 594 |
# overridden value coming from environment |
| 595 |
if {[isEnvVarDefined $envvar]} { |
| 596 |
set colors_list $::env($envvar) |
| 597 |
if {[catch { |
| 598 |
# test overridden value could be set to a dummy array variable |
| 599 |
array set test_colors [split $colors_list {:=}] |
| 600 |
} errMsg ]} { |
| 601 |
# report issue as a debug message rather warning to avoid |
| 602 |
# disturbing user with a warning message in the middle of a |
| 603 |
# useful output as this table will be initialized at first use |
| 604 |
reportDebug "Ignore invalid value set in $envvar ($colors_list)" |
| 605 |
unset colors_list |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
# if no valid override set use default color theme for terminal |
| 610 |
# background color kind (light or dark) |
| 611 |
if {![info exists colors_list]} { |
| 612 |
if {[getConf term_background] eq {light}} { |
| 613 |
##nagelfar ignore Too long line |
| 614 |
set colors_list {@lightbgcolors@} |
| 615 |
} else { |
| 616 |
##nagelfar ignore Too long line |
| 617 |
set colors_list {@darkbgcolors@} |
| 618 |
} |
| 619 |
if {[catch { |
| 620 |
array set test_colors [split $colors_list {:=}] |
| 621 |
} errMsg ]} { |
| 622 |
reportDebug "Ignore invalid default [getConf term_background]\ |
| 623 |
background colors ($colors_list)" |
| 624 |
# define an empty list if no valid value set |
| 625 |
set colors_list {} |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
# check each color defined and unset invalid codes |
| 630 |
set value {} |
| 631 |
foreach {elt col} [split $colors_list {:=}] { |
| 632 |
if {![regexp {^[\d;]+$} $col]} { |
| 633 |
reportDebug "Ignore invalid color code for '$elt' ($col)" |
| 634 |
} else { |
| 635 |
lappend value $elt=$col |
| 636 |
} |
| 637 |
} |
| 638 |
set value [join $value :] |
| 639 |
|
| 640 |
# set SGR table as an array to easily access rendition for each key |
| 641 |
array unset ::g_colors |
| 642 |
array set ::g_colors [split $value {:=}] |
| 643 |
|
| 644 |
return $value |
| 645 |
} |
| 646 |
|
| 647 |
# Initialize color configuration value |
| 648 |
proc initConfColor {envvar value validvallist intvallist} { |
| 649 |
# overridden value coming from environment via standard variable |
| 650 |
# https://no-color.org/ and https://bixense.com/clicolors/ |
| 651 |
if {[isEnvVarDefined NO_COLOR]} { |
| 652 |
set value never |
| 653 |
} elseif {[isEnvVarDefined CLICOLOR]} { |
| 654 |
if {[envVarEquals CLICOLOR 0]} { |
| 655 |
set value never |
| 656 |
} else { |
| 657 |
set value auto |
| 658 |
} |
| 659 |
} elseif {[isEnvVarDefined CLICOLOR_FORCE] && $::env(CLICOLOR_FORCE) ne\ |
| 660 |
{0}} { |
| 661 |
set value always |
| 662 |
} |
| 663 |
|
| 664 |
# overridden value coming from environment via Modules-specific variable |
| 665 |
if {$envvar ne {} && [isEnvVarDefined $envvar]} { |
| 666 |
# ignore non-valid values |
| 667 |
if {![llength $validvallist] || $::env($envvar) in $validvallist} { |
| 668 |
set value $::env($envvar) |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
# overridden value coming the command-line |
| 673 |
if {[info exists ::asked_color]} { |
| 674 |
set value [set ::asked_color] |
| 675 |
} |
| 676 |
|
| 677 |
# convert value to its internal representation |
| 678 |
if {[llength $intvallist]} { |
| 679 |
set value [lindex $intvallist [lsearch -exact $validvallist $value]] |
| 680 |
} |
| 681 |
|
| 682 |
# disable color mode if no terminal attached except if 'always' asked |
| 683 |
if {$value != 0 && (![getState is_stderr_tty] || $value == 2)} { |
| 684 |
incr value -1 |
| 685 |
} |
| 686 |
|
| 687 |
# initialize color theme if color mode enabled |
| 688 |
getConf colors |
| 689 |
|
| 690 |
return $value |
| 691 |
} |
| 692 |
|
| 693 |
# Initialize tcl_ext_lib configuration value |
| 694 |
proc initConfTclExtLib {envvar value validvallist intvallist} { |
| 695 |
set libfile libtclenvmodules@SHLIB_SUFFIX@ |
| 696 |
|
| 697 |
# determine lib directory |
| 698 |
##nagelfar ignore #19 Strange command |
| 699 |
##nagelfar ignore +13 Too long line |
| 700 |
@notmultilibsupport@set libdir {@libdir@} |
| 701 |
@multilibsupport@switch -- [getState machine] { |
| 702 |
@multilibsupport@ x86_64 - aarch64 - ppc64le - s390x { |
| 703 |
@multilibsupport@ set libdirmain {@libdir64@} |
| 704 |
@multilibsupport@ set libdiralt {@libdir32@} |
| 705 |
@multilibsupport@ } |
| 706 |
@multilibsupport@ default { |
| 707 |
@multilibsupport@ set libdirmain {@libdir32@} |
| 708 |
@multilibsupport@ set libdiralt {@libdir64@} |
| 709 |
@multilibsupport@ } |
| 710 |
@multilibsupport@} |
| 711 |
@multilibsupport@# use alternative arch lib if available and not main one |
| 712 |
@multilibsupport@if {![file exists [file join $libdirmain $libfile]] && [file exists [file\ |
| 713 |
@multilibsupport@ join $libdiralt $libfile]]} { |
| 714 |
@multilibsupport@ set libdir $libdiralt |
| 715 |
@multilibsupport@} else { |
| 716 |
@multilibsupport@ set libdir $libdirmain |
| 717 |
@multilibsupport@} |
| 718 |
##nagelfar variable libdir |
| 719 |
|
| 720 |
return [file join $libdir $libfile] |
| 721 |
} |
| 722 |
|
| 723 |
# Initialize module tag abbreviation table |
| 724 |
proc initConfTagAbbrev {envvar value validvallist intvallist} { |
| 725 |
# overridden value coming from environment |
| 726 |
if {[isEnvVarDefined $envvar]} { |
| 727 |
if {[catch { |
| 728 |
# try to set the tag-abbreviation mapping table |
| 729 |
array set ::g_tagAbbrev [split $::env($envvar) {:=}] |
| 730 |
set value $::env($envvar) |
| 731 |
} errMsg ]} { |
| 732 |
reportWarning "Ignore invalid value set in $envvar ($::env($envvar))" |
| 733 |
array unset ::g_tagAbbrev |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
# test default value |
| 738 |
if {![array exists ::g_tagAbbrev]} { |
| 739 |
if {[catch { |
| 740 |
array set ::g_tagAbbrev [split $value {:=}] |
| 741 |
} errMsg ]} { |
| 742 |
reportWarning "Ignore invalid default value for 'tag_abbrev' config\ |
| 743 |
($value)" |
| 744 |
array unset ::g_tagAbbrev |
| 745 |
# define an empty list if no valid value set |
| 746 |
set value {} |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
# build abbrev:tagname array |
| 751 |
foreach {tag abbrev} [array get ::g_tagAbbrev] { |
| 752 |
# skip tags not relevant for current command, that share their |
| 753 |
# abbreviation with another tag |
| 754 |
switch -- $tag { |
| 755 |
hidden-loaded { |
| 756 |
set setabbrevtag [expr {[currentState commandname] eq {list}}] |
| 757 |
} |
| 758 |
hidden { |
| 759 |
set setabbrevtag [expr {[currentState commandname] in {avail\ |
| 760 |
spider}}] |
| 761 |
} |
| 762 |
default { |
| 763 |
set setabbrevtag 1 |
| 764 |
} |
| 765 |
} |
| 766 |
if {$setabbrevtag} { |
| 767 |
set ::g_abbrevTag($abbrev) $tag |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
return $value |
| 772 |
} |
| 773 |
|
| 774 |
# Initialize module tag color name table |
| 775 |
proc initConfTagColorName {envvar value validvallist intvallist} { |
| 776 |
# overridden value coming from environment |
| 777 |
if {[isEnvVarDefined $envvar]} { |
| 778 |
set value $::env($envvar) |
| 779 |
} |
| 780 |
|
| 781 |
# set table for efficient search |
| 782 |
foreach tag [split $value :] { |
| 783 |
set ::g_tagColorName($tag) 1 |
| 784 |
} |
| 785 |
|
| 786 |
return $value |
| 787 |
} |
| 788 |
|
| 789 |
# Initialize interactive editor command |
| 790 |
proc initConfEditor {envvar value validvallist intvallist} { |
| 791 |
# overridden value coming from environment via Modules-specific variable |
| 792 |
if {$envvar ne {} && [isEnvVarDefined $envvar]} { |
| 793 |
set value $::env($envvar) |
| 794 |
# overridden value coming from environment via standard variable |
| 795 |
} elseif {[isEnvVarDefined VISUAL]} { |
| 796 |
set value $::env(VISUAL) |
| 797 |
} elseif {[isEnvVarDefined EDITOR]} { |
| 798 |
set value $::env(EDITOR) |
| 799 |
} |
| 800 |
return $value |
| 801 |
} |
| 802 |
|
| 803 |
# Initialize variant shortcut table |
| 804 |
proc initConfVariantShortcut {envvar value validvallist intvallist} { |
| 805 |
# overridden value coming from environment |
| 806 |
if {[isEnvVarDefined $envvar]} { |
| 807 |
if {[catch { |
| 808 |
# try to set the variant-shortcut mapping table |
| 809 |
array set testarr [split $::env($envvar) {:=}] |
| 810 |
set value $::env($envvar) |
| 811 |
set setfromenv 1 |
| 812 |
} errMsg ]} { |
| 813 |
reportWarning "Ignore invalid value set in $envvar ($::env($envvar))" |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
# test default value |
| 818 |
if {![info exists setfromenv]} { |
| 819 |
if {[catch { |
| 820 |
array set testarr [split $value {:=}] |
| 821 |
} errMsg ]} { |
| 822 |
reportWarning "Ignore invalid default value for 'variant_shortcut'\ |
| 823 |
config ($value)" |
| 824 |
# define an empty list if no valid value set |
| 825 |
set value {} |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
# ignore shortcut if not equal to one character or if set on alphanum char |
| 830 |
# or on char with special meaning |
| 831 |
foreach {vr sc} [split $value {:=}] { |
| 832 |
if {[string length $sc] == 1 && ![string match {[a-zA-Z0-9+~/@=-,:]}\ |
| 833 |
$sc]} { |
| 834 |
# remove duplicate shortcut or variant definition |
| 835 |
if {[info exists ::g_variantShortcut($vr)]} { |
| 836 |
unset ::g_shortcutVariant($::g_variantShortcut($vr)) |
| 837 |
} |
| 838 |
if {[info exists ::g_shortcutVariant($sc)]} { |
| 839 |
unset ::g_variantShortcut($::g_shortcutVariant($sc)) |
| 840 |
} |
| 841 |
set ::g_variantShortcut($vr) $sc |
| 842 |
set ::g_shortcutVariant($sc) $vr |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
# update value after above filtering step |
| 847 |
set value {} |
| 848 |
foreach vr [array names ::g_variantShortcut] { |
| 849 |
if {[string length $value]} { |
| 850 |
append value : |
| 851 |
} |
| 852 |
append value $vr=$::g_variantShortcut($vr) |
| 853 |
} |
| 854 |
|
| 855 |
return $value |
| 856 |
} |
| 857 |
|
| 858 |
# Is currently set verbosity level is equal or higher than level passed as arg |
| 859 |
proc isVerbosityLevel {name} { |
| 860 |
return [expr {[lsearch -exact [lindex $::g_config_defs(verbosity) 4]\ |
| 861 |
[getConf verbosity]] >= [lsearch -exact [lindex\ |
| 862 |
$::g_config_defs(verbosity) 4] $name]}] |
| 863 |
} |
| 864 |
|
| 865 |
# Is match performed in a case sensitive or insensitive manner |
| 866 |
proc isIcase {} { |
| 867 |
# depending on current sub-command, list values that equal to a case |
| 868 |
# insensitive match enablement |
| 869 |
lappend enabledValList always |
| 870 |
if {[currentState commandname] in [list avail list whatis search paths\ |
| 871 |
savelist spider]} { |
| 872 |
lappend enabledValList search |
| 873 |
} |
| 874 |
return [expr {[getConf icase] in $enabledValList}] |
| 875 |
} |
| 876 |
|
| 877 |
proc commandAbortOnError {{command {}}} { |
| 878 |
if {![string length $command]} { |
| 879 |
set command [currentState commandname] |
| 880 |
} |
| 881 |
set abort_command_list [getConfList abort_on_error] |
| 882 |
return [expr {[isTopEvaluation] && ![getState force] && $command in\ |
| 883 |
$abort_command_list}] |
| 884 |
} |
| 885 |
|
| 886 |
# ;;; Local Variables: *** |
| 887 |
# ;;; mode:tcl *** |
| 888 |
# ;;; End: *** |
| 889 |
# vim:set tabstop=3 shiftwidth=3 expandtab autoindent: |