Debian Codegraph @

tcl/modfind.tcl.in

raw
1 ##########################################################################
2
3 # MODFIND.TCL, available or loaded module find 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 # convert environment variable references in string to their values
24 # every local variable is prefixed by '0' to ensure they will not be
25 # overwritten through variable reference resolution process
26 proc resolvStringWithEnv {0str} {
27 # fetch variable references in string
28 set 0match_list [regexp -all -inline {\$[{]?([A-Za-z_][A-Za-z0-9_]*)[}]?}\
29 ${0str}]
30 if {[llength ${0match_list}]} {
31 # put in local scope every environment variable referred in string
32 for {set 0i 1} {${0i} < [llength ${0match_list}]} {incr 0i 2} {
33 set 0varname [lindex ${0match_list} ${0i}]
34 ##nagelfar vartype 0varname varName
35 if {![info exists ${0varname}]} {
36 set ${0varname} [get-env ${0varname}]
37 }
38 }
39 # resolve variable reference with values (now in local scope)
40 set 0res [subst -nobackslashes -nocommands ${0str}]
41 } else {
42 set 0res ${0str}
43 }
44
45 reportDebug "'${0str}' resolved to '${0res}'"
46
47 return ${0res}
48 }
49
50 # in each string of a list, convert their variable reference into actual value
51 proc resolvStringListWithEnv {str_list} {
52 set res_list {}
53 foreach str $str_list {
54 lappend res_list [resolvStringWithEnv $str]
55 }
56 return $res_list
57 }
58
59 # Return first string in list matching given resolved string. Strings in list
60 # are resolved to help comparison in case they contain an env variable ref.
61 proc getStringFromListMatchingResString {str_list resolved_str} {
62 foreach str $str_list {
63 if {$resolved_str eq [resolvStringWithEnv $str]} {
64 return $str
65 }
66 }
67 }
68
69 # deduce modulepath from modulefile and module name
70 proc getModulepathFromModuleName {modfile modname} {
71 return [string range $modfile 0 end-[string length /$modname]]
72 }
73
74 # deduce module name from modulefile and modulepath
75 proc getModuleNameFromModulepath {modfile modpath} {
76 return [string range $modfile [string length $modpath/] end]
77 }
78
79 # check if given modulefile is hosted in one the given modulepaths
80 proc isModulefileInModulepathList {mod_file modulepath_list} {
81 set ret 0
82 foreach modulepath $modulepath_list {
83 if {[string first $modulepath/ $mod_file] == 0} {
84 set ret 1
85 break
86 }
87 }
88 return $ret
89 }
90
91 # return list of all enabled modulepaths matching given path
92 proc getMatchingModulepathList {path} {
93 set ret {}
94 foreach modulepath [getModulePathList] {
95 if {[string first $path/ $modulepath] == 0} {
96 lappend ret $modulepath
97 }
98 }
99 return $ret
100 }
101
102 # extract module name from modulefile and currently enabled modulepaths
103 proc findModuleNameFromModulefile {modfile} {
104 set ret {}
105
106 foreach modpath [getModulePathList] {
107 if {[string first $modpath/ $modfile/] == 0} {
108 set ret [getModuleNameFromModulepath $modfile $modpath]
109 break
110 }
111 }
112 return $ret
113 }
114
115 # extract modulepath from modulefile and currently enabled modulepaths
116 proc findModulepathFromModulefile {modfile} {
117 set ret {}
118
119 foreach modpath [getModulePathList] {
120 if {[string first $modpath/ $modfile/] == 0} {
121 set ret $modpath
122 break
123 }
124 }
125 return $ret
126 }
127
128 # Determine with a name provided as argument the corresponding module name,
129 # version and name/version. Module name is guessed from current module name
130 # when shorthand version notation is used. Both name and version are guessed
131 # from current module if name provided is empty. If 'name_relative_tocur' is
132 # enabled then name argument may be interpreted as a name relative to the
133 # current modulefile directory (useful for module-version and module-alias
134 # for instance).
135 proc getModuleNameVersion {{name {}} {name_relative_tocur 0}} {
136 set curmod [currentState modulename]
137 set curmodname [file dirname $curmod]
138 set curmodversion [file tail $curmod]
139
140 if {$name eq {}} {
141 set name $curmodname
142 set version $curmodversion
143 # check for shorthand version notation like "/version" or "./version"
144 # only if we are currently interpreting a modulefile or modulerc
145 } elseif {$curmod ne {} && [regexp {^\.?\/(.*)$} $name match version]} {
146 # if we cannot distinguish a module name, raise error when shorthand
147 # version notation is used
148 if {[currentState modulefile] ne $curmod && $curmod ne {.modulerc}} {
149 # name is the name of current module directory
150 set name $curmodname
151 } else {
152 reportError "Invalid modulename '$name' found"
153 return {}
154 }
155 } else {
156 set name [string trimright $name /]
157 set version [file tail $name]
158 if {$name eq $version} {
159 set version {}
160 } else {
161 set name [file dirname $name]
162 }
163 # name may correspond to last part of current module
164 # if so name is replaced by current module name
165 if {$name_relative_tocur && [file tail $curmodname] eq $name} {
166 set name $curmodname
167 }
168 }
169
170 if {$version eq {}} {
171 set mod $name
172 } else {
173 set mod $name/$version
174 }
175
176 return [list $mod $name $version]
177 }
178
179 # Register alias or symbolic version deep resolution in a global array that
180 # can be used thereafter to get in one query the actual modulefile behind
181 # a virtual name. Also consolidate a global array that in the same manner
182 # list all the symbols held by modulefiles.
183 proc setModuleResolution {mod target {symver {}} {override_res_path 1}\
184 {auto_symver 0}} {
185 global g_moduleResolved g_resolvedHash g_resolvedPath g_symbolHash
186
187 # find end-point module and register step-by-step path to get to it
188 set res $target
189 lappend res_path $res
190 while {$mod ne $res && [info exists g_resolvedPath($res)]} {
191 set res $g_resolvedPath($res)
192 lappend res_path $res
193 }
194
195 # error if resolution end on initial module
196 if {$mod eq $res} {
197 reportError "Resolution loop on '$res' detected"
198 return 0
199 }
200
201 # module name will be useful when registering symbol
202 if {$symver ne {}} {
203 lassign [getModuleNameVersion $mod] modfull modname
204 }
205
206 # change default symbol owner if previously given; auto symbol are defined
207 # only if no default is pre-existing
208 if {$symver eq {default} && !$auto_symver} {
209 # alternative name "modname" is set when mod = "modname/default" both
210 # names will be registered to be known for queries and resolution defs
211 set modalt $modname
212
213 if {[info exists g_moduleResolved($mod)]} {
214 set prev $g_moduleResolved($mod)
215 # there may not be a default in case of auto symbol
216 if {[info exists g_symbolHash($prev)] && [set idx [lsearch -exact\
217 $g_symbolHash($prev) default]] != -1} {
218 reportDebug "remove symbol 'default' from '$prev'"
219 set g_symbolHash($prev) [lreplace $g_symbolHash($prev) $idx $idx]
220 }
221 }
222 }
223
224 # register end-point resolution
225 reportDebug "$mod resolved to $res"
226 set g_moduleResolved($mod) $res
227 # set first element of resolution path only if not already set or
228 # scratching enabled, no change when propagating symbol along res path
229 if {$override_res_path || ![info exists g_resolvedPath($mod)]} {
230 set g_resolvedPath($mod) $target
231 }
232 lappend g_resolvedHash($res) $mod
233
234 # also register resolution on alternative name if any
235 if {[info exists modalt]} {
236 reportDebug "$modalt resolved to $res"
237 set g_moduleResolved($modalt) $res
238 if {$override_res_path || ![info exists g_resolvedPath($modalt)]} {
239 set g_resolvedPath($modalt) $target
240 }
241 lappend g_resolvedHash($res) $modalt
242 # register name alternative to know their existence
243 set ::g_moduleAltName($modalt) $mod
244 set ::g_moduleAltName($mod) $modalt
245 }
246
247 # if other modules were pointing to this one, adapt resolution end-point
248 set relmod_list {}
249 if {[info exists g_resolvedHash($mod)]} {
250 set relmod_list $g_resolvedHash($mod)
251 unset g_resolvedHash($mod)
252 }
253 # also adapt resolution for modules pointing to the alternative name
254 if {[info exists modalt] && [info exists g_resolvedHash($modalt)]} {
255 lappend relmod_list {*}$g_resolvedHash($modalt)
256 unset g_resolvedHash($modalt)
257 }
258 foreach relmod $relmod_list {
259 set g_moduleResolved($relmod) $res
260 reportDebug "$relmod now resolved to $res"
261 lappend g_resolvedHash($res) $relmod
262 }
263
264 # register and propagate symbols to the resolution path, exception made for
265 # auto symbol which are stored separately and not propagated
266 if {[info exists g_symbolHash($mod)]} {
267 set sym_list $g_symbolHash($mod)
268 } else {
269 set sym_list {}
270 }
271 if {$symver ne {} && $auto_symver} {
272 reportDebug "define auto symbolic version '$mod' targeting $target"
273 set ::g_autoSymbol($mod) $target
274 } elseif {$symver ne {} && !$auto_symver} {
275 # merge symbol definitions in case of alternative name
276 if {[info exists modalt] && [info exists g_symbolHash($modalt)]} {
277 set sym_list [lsort -dictionary -unique [list {*}$sym_list\
278 {*}$g_symbolHash($modalt)]]
279 reportDebug "set symbols '$sym_list' to $mod and $modalt"
280 set g_symbolHash($mod) $sym_list
281 set g_symbolHash($modalt) $sym_list
282 }
283
284 # dictionary-sort symbols and remove eventual duplicates
285 set sym_list [lsort -dictionary -unique [list {*}$sym_list $symver]]
286
287 # propagate symbols in g_symbolHash and g_moduleVersion toward the
288 # resolution path, handle that locally if we still work on same
289 # modulename, call for a proper resolution as soon as we change of
290 # module to get this new resolution registered
291 foreach modres $res_path {
292 lassign [getModuleNameVersion $modres] modfull modresname
293 if {$modname eq $modresname} {
294 if {[info exists g_symbolHash($modres)]} {
295 set modres_sym_list [lsort -dictionary -unique [list\
296 {*}$g_symbolHash($modres) {*}$sym_list]]
297 } else {
298 set modres_sym_list $sym_list
299 }
300 # sync symbols of alternative name if any
301 if {[info exists ::g_moduleAltName($modres)]} {
302 set altmodres $::g_moduleAltName($modres)
303 reportDebug "set symbols '$modres_sym_list' to $modres and\
304 $altmodres"
305 set g_symbolHash($altmodres) $modres_sym_list
306 } else {
307 reportDebug "set symbols '$modres_sym_list' to $modres"
308 }
309 set g_symbolHash($modres) $modres_sym_list
310
311 # register symbolic version for querying in g_moduleVersion
312 foreach symelt $sym_list {
313 set modvers $modresname/$symelt
314 reportDebug "module-version $modvers = $modres"
315 set ::g_moduleVersion($modvers) $modres
316 set ::g_sourceVersion($modvers) [currentState modulefile]
317 # record eventual missing resolution
318 if {![info exists g_moduleResolved($modvers)]} {
319 set g_moduleResolved($modvers) $modres
320 reportDebug "$modvers resolved to $modres"
321 lappend g_resolvedHash($modres) $modvers
322 }
323 }
324 # as we change of module name a proper resolution call should be
325 # made (see below) and will handle the rest of the resolution path
326 } else {
327 set need_set_res 1
328 break
329 }
330 }
331 # when registering an alias, existing symbols on alias source name should
332 # be broadcast along the resolution path with a proper resolution call
333 # (see below)
334 } else {
335 lassign [getModuleNameVersion $target] modres modresname
336 set need_set_res 1
337 }
338
339 # resolution needed to broadcast symbols along resolution path without
340 # altering initial path already set for these symbols
341 if {[info exists need_set_res]} {
342 foreach symelt $sym_list {
343 set modvers $modresname/$symelt
344 reportDebug "set resolution for $modvers"
345 setModuleResolution $modvers $modres $symelt 0
346 }
347 }
348
349 return 1
350 }
351
352 # retrieve all names that resolve to passed mod
353 proc getAllModuleResolvedName {mod {flag_type 0} {modspec {}} {filter_default\
354 0}} {
355 set namelist {}
356 set resmodlist {}
357 set icase [isIcase]
358 defineModEqProc $icase [getConf extended_default]
359
360 # get parent directories of mod
361 foreach modelt [split $mod /] {
362 if {[info exists modroot]} {
363 append modroot /
364 }
365 append modroot $modelt
366 lappend resmodlist $modroot
367 }
368
369 set modpar [file dirname $mod]
370 set moddfl $modpar/default
371
372 # add additionally all the altnames set on directories, parents of mod
373 # or on distant directories whose default version resolves to mod
374 for {set i 0} {$i < [llength $resmodlist]} {incr i 1} {
375 set modelt [getArrayKey ::g_resolvedHash [lindex $resmodlist $i] $icase]
376 if {[info exists ::g_resolvedHash($modelt)]} {
377 foreach resmod $::g_resolvedHash($modelt) {
378 # if alternative name corresponds to one root name test if default
379 # symbol is hidden
380 set resmoddfl [expr {[lsearch -exact $resmodlist $resmod] != -1 ?\
381 "$resmod/default" : {}}]
382 # if alternative name corresponds to default symbol and is hidden
383 # test if query matches bare module name
384 set resmodpar [expr {[file tail $resmod] eq {default} ? [file\
385 dirname $resmod] : {}}]
386
387 # if modelt is not a parent directory of mod, check its resolution
388 # points to mod (directly for alias/sym or indirectly for dir
389 # whose default version bridge resolution toward mod)
390 # if modspec arg is set, exclude hidden entries not explicitly
391 # matching modspec. auto symbols cannot be hidden
392 # if filter_default is asked, skip parent module name and /default
393 # symbol name from result list
394 if {($modspec eq {} || ([info exists ::g_autoSymbol($resmod)] &&\
395 $::g_autoSymbol($resmod) eq $mod) || (![isModuleHidden $resmod\
396 $modspec] && ($resmoddfl eq {} || ![isModuleHidden $resmoddfl\
397 $modspec])) || [modEq $modspec $resmod eqspec] || ($resmodpar\
398 ne {} && [modEq $modspec $resmodpar eqspec])) && ([modEq\
399 $modelt $mod eqstart] || $::g_moduleResolved($resmod) eq $mod\
400 || $mod eq [lindex [getPathToModule\
401 $::g_moduleResolved($resmod) {} 0] 1]) && (!$filter_default ||\
402 (![modEq $resmod $modpar] && ![modEq $resmod $moddfl]))} {
403 # prefix automatically generated syms with type flag if asked
404 if {$flag_type} {
405 set name_entry [list]
406 if {[info exists ::g_moduleAlias($resmod)]} {
407 lappend name_entry al
408 } elseif {[info exists ::g_autoSymbol($resmod)]} {
409 lappend name_entry as
410 }
411 lappend name_entry $resmod
412 } else {
413 set name_entry $resmod
414 }
415 appendNoDupToList namelist $name_entry
416
417 unset modroot
418 foreach reselt [split [file dirname $resmod] /] {
419 if {[info exists modroot]} {
420 append modroot /
421 }
422 append modroot $reselt
423 appendNoDupToList resmodlist $modroot
424 }
425 }
426 }
427 }
428 }
429 return $namelist
430 }
431
432 # build list for a given loaded module of its alternative names plus its
433 # simplified names (name minus version or any subdir if this version or subdir
434 # are the implicit default for module)
435 proc getLoadedAltAndSimplifiedName {mod} {
436 if {[isModuleFullPath $mod]} {
437 set namelist [list $mod]
438 # use already computed name list if any
439 } elseif {[info exists ::g_loadedAltAndSimplifiedName($mod)]} {
440 set namelist $::g_loadedAltAndSimplifiedName($mod)
441 } else {
442 # get recorded alternative name
443 set namelist [getLoadedAltname $mod]
444 # also look through modpaths for simplified mod name
445 set modpathlist [getModulePathList]
446 if {[llength $modpathlist]} {
447 set modfile [getModulefileFromLoadedModule $mod]
448 set parentmod [file dirname $mod]
449 # simplify to parent name as long as it resolves to current mod
450 while {$parentmod ne {.}} {
451 lassign [getPathToModule $parentmod $modpathlist 0] parentfile
452 if {$parentfile eq $modfile} {
453 lappend namelist $parentmod
454 set parentmod [file dirname $parentmod]
455 } else {
456 set parentmod .
457 }
458 }
459 }
460 # recorded computed name list for later use
461 set ::g_loadedAltAndSimplifiedName($mod) $namelist
462 }
463 return $namelist
464 }
465
466 # return value list of a loaded module property by parsing corresponding
467 # environment variable
468 proc getEnvLoadedModulePropertyParsedList {prop} {
469 set unserialize_depth [getEnvLoadedModulePropertyStructDepth $prop]
470 set unserialize_proc unserialize${unserialize_depth}ModulePropertyValueList
471 set char_unmap_list [getEnvLoadedModulePropertyCharUnmapList $prop]
472
473 set serialized_value_list [getEnvLoadedModulePropertyList $prop]
474 set structured_value_list [$unserialize_proc $serialized_value_list\
475 $char_unmap_list]
476 return $structured_value_list
477 }
478
479 proc getEnvLoadedModulePropertyList {prop} {
480 set env_var_name [getLoadedModulePropertyEnvVarName $prop]
481 return [split [get-env $env_var_name] [getState path_separator]]
482 }
483
484 proc getLoadedModulePropertyEnvVarName {prop} {
485 switch -- $prop {
486 name {set env_var_name LOADEDMODULES}
487 file {set env_var_name _LMFILES_}
488 modulepath {set env_var_name MODULEPATH}
489 default {set env_var_name __MODULES_LM[string toupper $prop]}
490 }
491 return $env_var_name
492 }
493
494 # return list of module paths by parsing MODULEPATH env variable
495 # behavior param enables to exit in error when no MODULEPATH env variable
496 # is set. by default an empty list is returned if no MODULEPATH set
497 # resolv_var param tells if environment variable references in path elements
498 # should be resolved or passed as-is in result list
499 # set_abs param applies an absolute path name conversion to path elements
500 # if enabled
501 proc getModulePathList {{behavior returnempty} {resolv_var 1} {set_abs 1}} {
502 if {![isEnvVarDefined MODULEPATH] && $behavior eq {exiterronundef}} {
503 reportErrorAndExit {No module path defined}
504 }
505
506 set modpath_list [list]
507 foreach modpath [getEnvLoadedModulePropertyParsedList modulepath] {
508 if {$resolv_var} {
509 set modpath [resolvStringWithEnv $modpath]
510 }
511 if {$set_abs} {
512 set modpath [getAbsolutePath $modpath]
513 }
514 appendNoDupToList modpath_list $modpath
515 }
516 return $modpath_list
517 }
518
519 proc getModulepathLabel {modpath} {
520 if {[info exists ::g_modulepathLabel($modpath)]} {
521 return $::g_modulepathLabel($modpath)
522 } else {
523 return $modpath
524 }
525 }
526
527 # return list of the configured and existing global RC files
528 proc getGlobalRcFileList {} {
529 set rclist {}
530
531 set rcfilelist [getConfList rcfile]
532 if {[llength $rcfilelist]} {
533 foreach rcfile $rcfilelist {
534 # if rcfile is a dir, look at a modulerc file in it
535 if {[file isdirectory $rcfile] && [file isfile $rcfile/modulerc]} {
536 lappend rclist $rcfile/modulerc
537 } elseif {[file isfile $rcfile]} {
538 lappend rclist $rcfile
539 }
540 }
541 }
542 if {[file isfile {@etcdir@/rc}]} {
543 lappend rclist {@etcdir@/rc}
544 }
545 # ignore user rc if relative configuration is enabled
546 if {![getConf ignore_user_rc] && [isEnvVarDefined HOME] && [file isfile\
547 $::env(HOME)/.modulerc]} {
548 lappend rclist $::env(HOME)/.modulerc
549 }
550
551 set readable_rclist {}
552 foreach rc $rclist {
553 if {[file readable $rc]} {
554 lappend readable_rclist $rc
555 }
556 }
557
558 return $readable_rclist
559 }
560
561 proc getModuleTag {mod full_path_mod tag} {
562 # check if tag is set on full path module designation (no prop there)
563 lassign [getFullPathModuleTagSpec $full_path_mod $tag] tag_mod_spec\
564 tag_props
565 if {[string length $tag_mod_spec]} {
566 return [list $tag $tag_props]
567 }
568 # look if mod matches one of the module specs applying to mod root
569 foreach {tmodspec tag_props} [getModuleRootTagSpecList $mod $tag] {
570 # first matching module spec wins with its properties
571 if {[modEq $tmodspec $mod eqstart 1 0 1]} {
572 return [list $tag $tag_props]
573 }
574 }
575 return {}
576 }
577
578 proc getModuleTagRuleList {mod full_path_mod tag} {
579 set rule_list {}
580 lassign [getFullPathModuleTagSpec $full_path_mod $tag] tag_mod_spec\
581 tag_props
582 if {[string length $tag_mod_spec]} {
583 lappend rule_list $tag_mod_spec
584 }
585 foreach {tag_mod_spec tag_props} [getModuleRootTagSpecList $mod $tag] {
586 if {$tag_mod_spec ni $rule_list && [modEq $tag_mod_spec $mod eqstart 1\
587 0 1]} {
588 lappend rule_list $tag_mod_spec
589 }
590 }
591 return $rule_list
592 }
593
594 proc getModuleRootTagSpecList {mod tag} {
595 set tag_spec_list {}
596 set mod_root [getModuleRootFromVersSpec $mod]
597 if {[info exists ::g_moduleTagRoot($mod_root)]} {
598 set idx [lsearch -exact $::g_moduleTagRoot($mod_root) $tag]
599 set tag_spec_list [lindex $::g_moduleTagRootSpec($mod_root) $idx]
600 }
601 return $tag_spec_list
602 }
603
604 proc getFullPathModuleTagSpec {full_path_mod tag} {
605 set tag_spec {}
606 if {$full_path_mod ne {} && [info exists\
607 ::g_moduleTagFullPath($full_path_mod)]} {
608 set idx [lsearch -exact $::g_moduleTagFullPath($full_path_mod) $tag]
609 if {$idx != -1} {
610 set tag_spec [list $full_path_mod [lindex\
611 $::g_moduleTagFullPathSpec($full_path_mod) $idx]]
612 }
613 }
614 return $tag_spec
615 }
616
617 proc isModuleTagged {mod tag {collected 0} {fpmod {}}} {
618 # retrieve tag information from collected tags or raw info
619 if {$collected} {
620 return [expr {$tag in [getTagList $mod $fpmod]}]
621 } else {
622 return [llength [getModuleTag $mod $fpmod $tag]]
623 }
624 }
625
626 proc getTaggedLoadedModuleList {tag} {
627 set modlist [list]
628 foreach mod [getEnvLoadedModulePropertyParsedList name] {
629 if {[isModuleTagged $mod $tag 1]} {
630 lappend modlist $mod
631 }
632 }
633 return $modlist
634 }
635
636 proc getModuleTagProp {mod fpmod tag prop} {
637 array set tags [getModuleTag $mod $fpmod $tag]
638
639 if {[info exists tags($tag)]} {
640 array set props $tags($tag)
641 if {[info exists props($prop)]} {
642 return $props($prop)
643 }
644 }
645 }
646
647 # Gather all tags applying to a given module
648 proc collectModuleTags {mod} {
649 if {[info exists ::g_tagHash($mod)]} {
650 set known_tag_list $::g_tagHash($mod)
651 } else {
652 set known_tag_list {}
653 }
654 set tag_list [getMatchingTagList $mod $known_tag_list]
655 if {[llength $tag_list]} {
656 setModuleTag $mod {*}$tag_list
657 }
658 }
659
660 proc getMatchingTagList {mod {known_tag_list {}}} {
661 set tag_list {}
662 set modroot [getModuleRootFromVersSpec $mod]
663 # look if mod matches one of the module specs applying to mod root
664 if {[info exists ::g_moduleTagRoot($modroot)]} {
665 for {set i 0} {$i < [llength $::g_moduleTagRoot($modroot)]} {incr i} {
666 set tag [lindex $::g_moduleTagRoot($modroot) $i]
667 if {$tag ni $tag_list} {
668 foreach {tmodspec tprops} [lindex\
669 $::g_moduleTagRootSpec($modroot) $i] {
670 # add tag if one modspec matches mod
671 if {[modEq $tmodspec $mod eqstart 1 0 1]} {
672 lappend known_tag_list $tag
673 lappend tag_list $tag
674 break
675 }
676 }
677 }
678 }
679 }
680 return $tag_list
681 }
682
683 proc setModuleTag {mod args} {
684 appendNoDupToList ::g_tagHash($mod) {*}$args
685 }
686
687 proc setModuleAndVariantsTag {mod mod_with_vr args} {
688 setModuleTag $mod {*}$args
689 if {$mod_with_vr ne {} && $mod ne $mod_with_vr} {
690 setModuleTag $mod_with_vr {*}$args
691 }
692 }
693
694 proc setModuleExtraTag {mod args} {
695 appendNoDupToList ::g_extraTagHash($mod) {*}$args
696 }
697
698 proc setModuleAndVariantsExtraTag {mod mod_with_vr args} {
699 setModuleExtraTag $mod {*}$args
700 if {$mod_with_vr ne {} && $mod ne $mod_with_vr} {
701 setModuleExtraTag $mod_with_vr {*}$args
702 }
703 }
704
705 proc unsetModuleTag {mod args} {
706 if {[info exists ::g_tagHash($mod)]} {
707 lassign [getDiffBetweenList $::g_tagHash($mod) $args] diff_list
708 if {[llength $args] && [llength $diff_list]} {
709 set ::g_tagHash($mod) $diff_list
710 } else {
711 unset ::g_tagHash($mod)
712 }
713 }
714 }
715
716 proc unsetModuleAndVariantsTag {mod mod_with_vr args} {
717 unsetModuleTag $mod {*}$args
718 if {$mod_with_vr ne {} && $mod ne $mod_with_vr} {
719 unsetModuleTag $mod_with_vr {*}$args
720 }
721 }
722
723 proc unsetModuleExtraTag {mod args} {
724 if {[info exists ::g_extraTagHash($mod)]} {
725 lassign [getDiffBetweenList $::g_extraTagHash($mod) $args] diff_list
726 if {[llength $args] && [llength $diff_list]} {
727 set ::g_extraTagHash($mod) $diff_list
728 } else {
729 unset ::g_extraTagHash($mod)
730 }
731 }
732 }
733
734 proc unsetModuleAndVariantsExtraTag {mod mod_with_vr args} {
735 unsetModuleExtraTag $mod {*}$args
736 if {$mod_with_vr ne {} && $mod ne $mod_with_vr} {
737 unsetModuleExtraTag $mod_with_vr {*}$args
738 }
739 }
740
741 proc getTagList {mod {fpmod {}} {sort 1}} {
742 set tag_list {}
743 # get tags applied over module full path designation
744 if {$fpmod ne {} && [info exists ::g_moduleTagFullPath($fpmod)]} {
745 appendNoDupToList tag_list {*}$::g_moduleTagFullPath($fpmod)
746 }
747 # recompute tags applying from module specification if mod is loaded but
748 # not the same modulefile. tags applying to loaded mod are recorded in
749 # g_tagHash but should not apply to same mod from a different modulepath
750 # do not make this distinction if modulefile is not provided
751 # directly check on g_loadedModules array to avoid triggering env check
752 if {[string length $fpmod] && [info exists ::g_loadedModules($mod)] &&\
753 $fpmod ne [getModulefileFromLoadedModule $mod]} {
754 appendNoDupToList tag_list {*}[getMatchingTagList $mod]
755 # get collected tags applied on short designation
756 } elseif {[info exists ::g_tagHash($mod)]} {
757 appendNoDupToList tag_list {*}$::g_tagHash($mod)
758 }
759 if {[llength $tag_list]} {
760 if {$sort} {
761 set tag_list [lsort -dictionary $tag_list]
762 }
763 reportDebug "'$mod' has tag list '$tag_list'"
764 }
765 return $tag_list
766 }
767
768 # return tags applying to mod that can be exported
769 proc getExportTagList {mod {fpmod {}}} {
770 # remove loaded/hidden tags from the list to register
771 lassign [getDiffBetweenList [getTagList $mod $fpmod 0] [list loaded\
772 hidden]] ret
773 return $ret
774 }
775
776 # return extra tags set on mod
777 proc getExtraTagList {mod} {
778 if {[info exists ::g_extraTagHash($mod)]} {
779 return $::g_extraTagHash($mod)
780 }
781 }
782
783 # return rule definition of sticky/super-sticky tag on given module
784 proc getStickyRuleList {mod {fpmod {}}} {
785 set ret {}
786 foreach tag {sticky super-sticky} {
787 if {[isModuleTagged $mod $tag 1]} {
788 set export_sticky_rule 1
789 set tag_rule_list [getModuleTagRuleList $mod $fpmod $tag]
790
791 # skip rule export if one matches module name and version or full
792 # path designation
793 foreach tag_rule $tag_rule_list {
794 if {[modEq $tag_rule $mod equal 1 0 1] || [modEq $tag_rule $fpmod\
795 equal 1 0 1]} {
796 set export_sticky_rule 0
797 break
798 }
799 }
800
801 if {[llength $tag_rule_list] && $export_sticky_rule} {
802 lappend ret [list $tag {*}$tag_rule_list]
803 }
804 }
805 }
806 return $ret
807 }
808
809 # return tags set on mod to record in collection
810 proc getSaveTagList {mod} {
811 # return all tags if pin tag is enabled, except nearly-forbidden that is
812 # not intended for save
813 if {[getConf collection_pin_tag]} {
814 lassign [getDiffBetweenList [getExportTagList $mod] [list\
815 nearly-forbidden]] ret
816 } else {
817 set ret [getExtraTagList $mod]
818 # in addition to those set with --tag option, add tags obtained by the
819 # way mod has been loaded
820 if {[isModuleTagged $mod auto-loaded 1]} {
821 lappend ret auto-loaded
822 }
823 if {[isModuleTagged $mod keep-loaded 1]} {
824 lappend ret keep-loaded
825 }
826 }
827 return $ret
828 }
829
830 proc abbrevTagList {taglist} {
831 set ret [list]
832 foreach tag $taglist {
833 if {[info exists ::g_tagAbbrev($tag)]} {
834 # empty string abbrev means no tag report
835 if {$::g_tagAbbrev($tag) ne {}} {
836 lappend ret $::g_tagAbbrev($tag)
837 }
838 } else {
839 lappend ret $tag
840 }
841 }
842 return [lsort -dictionary $ret]
843 }
844
845 proc getTagFromAbbrev {abbrev} {
846 if {![array exists ::g_abbrevTag]} {
847 getConf tag_abbrev
848 }
849 if {[info exists ::g_abbrevTag($abbrev)]} {
850 return $::g_abbrevTag($abbrev)
851 }
852 }
853
854 proc getVariantList {mod {report 0} {excl_dflval 0} {from 0}} {
855 set vrspeclist {}
856 set loadedsgrkey {}
857 # fetch variant information from
858 switch -- $from {
859 0 {
860 # variant set after module being loaded
861 if {[info exists ::g_loadedModuleVariant($mod)]} {
862 set vrspeclist $::g_loadedModuleVariant($mod)
863 }
864 }
865 1 {
866 # module specification
867 set vrspeclist {}
868 foreach vrspec [getVariantListFromVersSpec $mod] {
869 lassign $vrspec vrname vrnot vrvalisbool vrvalue
870 lappend vrspeclist [list $vrname $vrvalue $vrvalisbool]
871 }
872 }
873 2 {
874 # variant definition collected during scan evaluation
875 if {[info exists ::g_scanModuleVariant($mod)]} {
876 set vrspeclist $::g_scanModuleVariant($mod)
877 }
878 # if module is currently loaded, gather its variant values
879 if {[info exists ::g_loadedModuleVariant($mod)]} {
880 foreach lovrspec $::g_loadedModuleVariant($mod) {
881 lassign $lovrspec lovrname lovrvalue lovrvalisbool lovrvalisdfl
882 if {$lovrvalisbool} {
883 set lovrvalue [expr {$lovrvalue ? {on} : {off}}]
884 }
885 set lovrarr($lovrname) $lovrvalue
886 }
887 # get sgr key corresponding to loaded tag (if tag reported)
888 if {$report == 7 && [isEltInReport tag]} {
889 set loadedkind [expr {[isModuleTagged $mod auto-loaded 1] ?\
890 {auto-loaded} : {loaded}}]
891 lassign [abbrevTagList [list $loadedkind]] loadedsgrkey
892 }
893 }
894 }
895 }
896
897 # use array to make variant unique
898 foreach vrspec $vrspeclist {
899 if {$from == 2} {
900 # report all available variant values on avail
901 lassign $vrspec vrname vrvalues vrdefdflval vrdflval
902 set vrvalisbool 0
903 set vrvalisdfl 0
904 # build value list to output for free-variant value
905 if {![llength $vrvalues]} {
906 # if module is loaded, add loaded variant value
907 if {[info exists lovrarr($vrname)]} {
908 lappend vrvalues $lovrarr($vrname)
909 }
910 # if a default value is defined, add it to possible value list
911 if {$vrdefdflval && $vrdflval ni $vrvalues} {
912 lappend vrvalues $vrdflval
913 }
914 # indicate on free-value variant that all values are possible
915 lappend vrvalues *
916 }
917 } else {
918 lassign $vrspec vrname vrvalue vrvalisbool vrvalisdfl
919 set vrvalues [list $vrvalue]
920 }
921 # correct is-default value if invalid
922 if {![string is integer -strict $vrvalisdfl]} {
923 set vrvalisdfl 0
924 }
925 # correct is-boolean value if invalid
926 if {![string is boolean -strict $vrvalisbool]} {
927 set vrvalisbool 0
928 }
929 set vrisbool($vrname) $vrvalisbool
930 # correct boolean value if invalid
931 if {$vrvalisbool && ![string is boolean -strict $vrvalue]} {
932 set vrvalue 0
933 }
934 # do not return variant if value is default, unless if variant was
935 # specifically set to default value (isdfl=1) and we only want to
936 # exclude the automatically set default value (excl_dflval=2)
937 if {$excl_dflval > 0} {
938 if {$vrvalisdfl == 0 || ($excl_dflval == 2 && $vrvalisdfl == 1)} {
939 set vrarr($vrname) $vrvalue
940 # nullify previous duplicate definition if this one is default
941 } elseif {[info exists vrarr($vrname)]} {
942 unset vrarr($vrname)
943 }
944 } else {
945 if {$report == 7} {
946 # indicate what value in the list is the default one
947 if {[info exists vrdefdflval]} {
948 set vrdflidxarr($vrname) [expr {$vrdefdflval ? [lsearch -exact\
949 $vrvalues $vrdflval] : {-1}}]
950 } else {
951 set vrdflidxarr($vrname) [expr {$vrvalisdfl ? {0} : {-1}}]
952 }
953 # indicate what value in the list is the loaded one
954 set vrloidxarr($vrname) [expr {[info exists lovrarr($vrname)] ?\
955 [lsearch -exact $vrvalues $lovrarr($vrname)] : {-1}}]
956 if {$vrvalisbool} {
957 set vrarr($vrname) $vrvalue
958 } else {
959 set vrarr($vrname) $vrvalues
960 }
961 # is-default hint has to be transmitted on report mode '3'
962 } elseif {$report == 3} {
963 set vrarr($vrname) [list $vrvalue [expr {$vrvalisdfl > 0 &&\
964 $vrvalisdfl < 3}]]
965 } else {
966 set vrarr($vrname) $vrvalue
967 }
968 }
969 }
970 # sort variant to report
971 set ret {}
972 foreach vrname [lsort -dictionary [array names vrarr]] {
973 switch -- $report {
974 4 {lappend ret $vrname $vrarr($vrname) $vrisbool($vrname)}
975 3 {lappend ret $vrname [lindex $vrarr($vrname) 0] [lindex\
976 $vrarr($vrname) 1]}
977 2 {lappend ret [list $vrname 0 $vrisbool($vrname) $vrarr($vrname)]}
978 1 - 5 - 6 {
979 if {$vrisbool($vrname)} {
980 if {$vrarr($vrname)} {
981 lappend ret +$vrname
982 # track if +var variant has been reported to build key
983 # unless if key should not be updated (report mode '6')
984 if {$report != 6 && ![info exists ::g_used_va(on)]} {
985 set ::g_used_va(on) 1
986 }
987 } else {
988 lappend ret -$vrname
989 # track if -var variant has been reported to build key
990 # unless if key should not be updated (report mode '6')
991 if {$report != 6 && ![info exists ::g_used_va(off)]} {
992 set ::g_used_va(off) 1
993 }
994 }
995 # use defined shortcut to report if any set for this variant
996 # ignore shortcut if report=5 (when saving collection)
997 } elseif {$report != 5 && [info exists\
998 ::g_variantShortcut($vrname)]} {
999 set sc $::g_variantShortcut($vrname)
1000 lappend ret $sc$vrarr($vrname)
1001 # track if variant shortcut has been reported to build key
1002 # unless if key should not be updated (report mode '6')
1003 if {$report != 6 && ![info exists ::g_used_va($sc)]} {
1004 set ::g_used_va($sc) $vrname
1005 }
1006 } else {
1007 lappend ret $vrname=$vrarr($vrname)
1008 # track if var=val variant has been reported to build key
1009 # unless if key should not be updated (report mode '6')
1010 if {$report != 6 && ![info exists ::g_used_va(val)]} {
1011 set ::g_used_va(val) 1
1012 }
1013 }
1014 }
1015 7 {
1016 if {$vrisbool($vrname)} {
1017 if {$vrarr($vrname)} {
1018 lappend ret [list $vrname {} [list +$vrname]\
1019 $vrdflidxarr($vrname) $vrloidxarr($vrname) $loadedsgrkey]
1020 # track if +var variant has been reported to build key
1021 if {![info exists ::g_used_va(on)]} {
1022 set ::g_used_va(on) 1
1023 }
1024 } else {
1025 lappend ret [list $vrname {} [list -$vrname]\
1026 $vrdflidxarr($vrname) $vrloidxarr($vrname) $loadedsgrkey]
1027 # track if -var variant has been reported to build key
1028 if {![info exists ::g_used_va(off)]} {
1029 set ::g_used_va(off) 1
1030 }
1031 }
1032 # use defined shortcut to report if any set for this variant
1033 # ignore shortcut if report=5 (when saving collection)
1034 } elseif {$report != 5 && [info exists\
1035 ::g_variantShortcut($vrname)]} {
1036 set sc $::g_variantShortcut($vrname)
1037 lappend ret [list $vrname $sc $vrarr($vrname)\
1038 $vrdflidxarr($vrname) $vrloidxarr($vrname) $loadedsgrkey]
1039 # track if variant shortcut has been reported to build key
1040 # unless if key should not be updated (report mode '6')
1041 if {$report != 6 && ![info exists ::g_used_va($sc)]} {
1042 set ::g_used_va($sc) $vrname
1043 }
1044 } else {
1045 lappend ret [list $vrname $vrname= $vrarr($vrname)\
1046 $vrdflidxarr($vrname) $vrloidxarr($vrname) $loadedsgrkey]
1047 # track if var=val variant has been reported to build key
1048 # unless if key should not be updated (report mode '6')
1049 if {$report != 6 && ![info exists ::g_used_va(val)]} {
1050 set ::g_used_va(val) 1
1051 }
1052 }
1053 }
1054 0 {lappend ret $vrname $vrarr($vrname)}
1055 }
1056 }
1057
1058 return $ret
1059 }
1060
1061 proc isOtherVariantOfModuleLoading {mod} {
1062 set mod_name [getModuleNameAndVersFromVersSpec $mod]
1063 set mod_has_variant [expr {$mod ne $mod_name}]
1064
1065 return [expr {$mod_has_variant && [isModuleLoading $mod_name] && ![string\
1066 length [getLoadedMatchingName $mod {} 1]]}]
1067 }
1068
1069 proc isOtherVariantOfModuleLoaded {mod} {
1070 set mod_name [getModuleNameAndVersFromVersSpec $mod]
1071 set mod_has_variant [expr {$mod ne $mod_name}]
1072
1073 return [expr {$mod_has_variant && [isModuleLoaded $mod_name] && ![string\
1074 length [getLoadedMatchingName $mod]]}]
1075 }
1076
1077 proc isModuleDotHidden {mod} {
1078 foreach elt [split $mod /] {
1079 if {[string index $elt 0] eq {.}} {
1080 return 1
1081 }
1082 }
1083 return 0
1084 }
1085
1086 proc getModuleHidingLevel {mod fpmod} {
1087 # if full path module has been hidden, this hidden level is the base level
1088 # value we will return
1089 set baselvl [expr {$fpmod ne {} && [info exists\
1090 ::g_moduleHideFullPath($fpmod)] ? $::g_moduleHideFullPath($fpmod) :\
1091 {-1}}]
1092
1093 set modroot [lindex [file split $mod] 0]
1094 # look if mod matches one of the hidden module specs applying to mod root
1095 if {[info exists ::g_moduleHideRoot($modroot)]} {
1096 for {set lvl 2} {$lvl > -1} {incr lvl -1} {
1097 # no need to search anymore if we have reached same hiding level as
1098 # one defined on full path module designation
1099 if {$lvl == $baselvl} {
1100 break
1101 }
1102 foreach hmodspec [lindex $::g_moduleHideRoot($modroot) $lvl] {
1103 if {[modEq $hmodspec $mod eqstart 1 0 1 {}]} {
1104 return $lvl
1105 }
1106 }
1107 }
1108 }
1109 return $baselvl
1110 }
1111
1112 proc setModspecHidingLevel {modspec lvl} {
1113 # skip record if an higher hiding level is already set
1114 if {![info exists ::g_moduleHide($modspec)] || $lvl >\
1115 $::g_moduleHide($modspec)} {
1116 reportDebug "Record hidden module specification '$modspec' (lvl=$lvl)"
1117 set ::g_moduleHide($modspec) $lvl
1118
1119 if {[isModuleFullPath $modspec]} {
1120 set ::g_moduleHideFullPath($modspec) $lvl
1121 } else {
1122 # record hidden mod spec for mod root to optimize search
1123 set modroot [getModuleRootFromVersSpec $modspec]
1124 if {![info exists ::g_moduleHideRoot($modroot)]} {
1125 set ::g_moduleHideRoot($modroot) [list [list] [list] [list]]
1126 }
1127 # record in list corresponding to hidden level to search from
1128 # strongest to weakest hidden level
1129 set hiderootlvllist [lindex $::g_moduleHideRoot($modroot) $lvl]
1130 lappend hiderootlvllist $modspec
1131 lset ::g_moduleHideRoot($modroot) $lvl $hiderootlvllist
1132 }
1133 }
1134 }
1135
1136 # test if mod is declared hidden or has one element in its name starting with
1137 # dot character. mod is considered hidden depending on their hiding level,
1138 # current search query and hiding threshold. when retdetails option is
1139 # enabled, mod hiding level and query match hind are also returned
1140 # also checks full path module designation if fpmod is set
1141 proc isModuleHidden {mod {modspec {}} {retdetails 0} {fpmod {}}} {
1142 set defhidlvl [set hidlvl [getModuleHidingLevel $mod $fpmod]]
1143 if {$hidlvl >= [getState hiding_threshold]} {
1144 # soft hidden mods are considered matched if their root name matches
1145 # search query, other kind of hidden mods must fully matches query
1146 set hidmatch [expr {$hidlvl == 0 ? [modStartNb $mod $modspec] > 0 :\
1147 [modEq $modspec $mod eqspec]}]
1148 } else {
1149 set hidlvl -1
1150 set hidmatch 0
1151 }
1152 if {$hidlvl < 1 && [set isdot [isModuleDotHidden $mod]] && [getState\
1153 hiding_threshold] < 1} {
1154 set hidlvl 1
1155 # dot hidden are considered matched if remaining string part after
1156 # search query is not dot hidden
1157 set hidmatch [expr {[set i [modStartNb $mod $modspec]] > 0 &&\
1158 ![isModuleDotHidden [join [lrange [file split $mod] $i end] /]]}]
1159 }
1160 # hidden if hiding level greater or equal hiding threshold and not
1161 # matched or if matched hard hiding level are kept hidden
1162 set ishid [expr {$hidlvl != -1 && (!$hidmatch || $hidlvl > 1)}]
1163
1164 # is module hidden by definition (whatever hiding threshold or match)
1165 set hidbydef [expr {$defhidlvl > 0 || ([info exists isdot] && $isdot)}]
1166
1167 return [expr {$retdetails ? [list $hidlvl $hidmatch $hidbydef $ishid]\
1168 : $ishid}]
1169 }
1170
1171 # check if module name is specified as a full pathname (not a name relative
1172 # to a modulepath)
1173 proc isModuleFullPath {mod} {
1174 return [regexp {^(|\.|\.\.)/} $mod]
1175 }
1176
1177 # check if a module corresponds to a virtual module (module name
1178 # does not corresponds to end of the modulefile name)
1179 proc isModuleVirtual {mod modfile} {
1180 return [expr {[string first $mod $modfile end-[string length $mod]] == -1}]
1181 }
1182
1183 # Return the full pathname and modulename to the module.
1184 # Resolve aliases and default versions if the module name is something like
1185 # "name/version" or just "name" (find default version).
1186 proc getPathToModule {mod {indir {}} {report_issue 1} {look_loaded no}\
1187 {excdir {}}} {
1188 reportDebug "finding '$mod' in '$indir' (report_issue=$report_issue,\
1189 look_loaded=$look_loaded, excdir='$excdir')"
1190
1191 set vrlist [getVariantList $mod 1 0 1]
1192 if {$mod eq {}} {
1193 set retlist [list {} {} {} none [getEmptyNameMsg module]]
1194 # try first to look at loaded modules if enabled to find matching module
1195 # or to find a closest match (used when switching with single name arg)
1196 } elseif {$look_loaded ne {no}} {
1197 switch -- $look_loaded {
1198 exact {set getLoadedNameProc getLoadedExactName}
1199 match {set getLoadedNameProc getLoadedEqstartName}
1200 close {set getLoadedNameProc getLoadedWithClosestName}
1201 }
1202 if {[set lm [$getLoadedNameProc $mod $indir]] ne {}} {
1203 set vrlist [getVariantList $lm 1]
1204 set retlist [list [getModulefileFromLoadedModule $lm] $lm]
1205 } else {
1206 set retlist [list {} [getModuleNameAndVersFromVersSpec $mod]\
1207 $mod notloaded]
1208 }
1209 # Check for $mod specified as a full pathname
1210 } elseif {[isModuleFullPath $mod]} {
1211 set mod [getAbsolutePath $mod]
1212 # note that a raw filename as an argument returns the full
1213 # path as the module name
1214 lassign [checkValidModule $mod] check_valid check_msg
1215 switch -- $check_valid {
1216 true {
1217 set retlist [list $mod $mod]
1218 }
1219 invalid - accesserr {
1220 set retlist [list {} $mod $mod $check_valid $check_msg $mod]
1221 }
1222 }
1223 } else {
1224 set dir_list [expr {$indir ne {} ? $indir : [getModulePathList\
1225 exiterronundef]}]
1226 # remove excluded directories (already searched)
1227 foreach dir $excdir {
1228 set dir_list [replaceFromList $dir_list $dir]
1229 }
1230
1231 set icase [isIcase]
1232 defineGetEqArrayKeyProc $icase [getConf extended_default] [getConf\
1233 implicit_default]
1234
1235 # Now search for $mod in module paths
1236 set modspec $mod
1237 foreach dir $dir_list {
1238 # get list of modules corresponding to searched query
1239 array unset mod_list
1240 array set mod_list [getModules $dir $mod 0 [list rc_defs_included\
1241 resolve]]
1242
1243 set prevmod {}
1244 set mod_res {}
1245 # loop to resolve correct modulefile in case specified mod is a
1246 # directory that should be analyzed to get default mod in it
1247 while {$prevmod ne $mod} {
1248 ##nagelfar ignore Found constant
1249 set mod [getEqArrayKey mod_list $mod]
1250 set prevmod $mod
1251 if {[info exists mod_list($mod)]} {
1252 switch -- [lindex $mod_list($mod) 0] {
1253 alias - version {
1254 set newmod [resolveModuleVersionOrAlias $mod $icase]
1255 if {[info exists mod_list($newmod)]} {
1256 set mod $newmod
1257 } else {
1258 # add specified variants to current resolution
1259 set newmod [getAndParseModuleWithVariant $newmod\
1260 $vrlist]
1261 # restart search on new modulename, constrained to
1262 # specified dir if set, if not found in current res
1263 return [getPathToModule $newmod $indir $report_issue]
1264 }
1265 }
1266 directory {
1267 # is implicit default disabled and none explicitly set?
1268 if {[lindex $mod_list($mod) 1] eq {}} {
1269 set retlist [list {} $mod $mod none "No default\
1270 version defined for '$mod'"]
1271 } else {
1272 # Move to default element in directory
1273 set mod $mod/[lindex $mod_list($mod) 1]
1274 # restart search if default element is an hidden dir
1275 if {![info exists mod_list($mod)] && [isModuleHidden\
1276 $mod $modspec 0 $dir/$mod]} {
1277 # add specified variants to current resolution
1278 set mod [getAndParseModuleWithVariant $mod $vrlist]
1279 return [getPathToModule $mod $indir $report_issue]
1280 }
1281 }
1282 }
1283 modulefile {
1284 # If mod was a file in this path, return that file
1285 set retlist [list $dir/$mod $mod]
1286 }
1287 virtual {
1288 # return virtual name with file it targets
1289 set retlist [list [lindex $mod_list($mod) 2] $mod]
1290 }
1291 invalid - accesserr {
1292 # may found mod but issue, so end search with error
1293 set retlist [list {} $mod $mod {*}$mod_list($mod)]
1294 }
1295 }
1296 }
1297 }
1298 # break loop if found something (valid or invalid module)
1299 # elsewhere go to next path
1300 if {[info exists retlist]} {
1301 break
1302 }
1303 }
1304 }
1305
1306 # set result if nothing found
1307 if {![info exists retlist]} {
1308 set retlist [list {} $mod $mod none "Unable to locate a modulefile for\
1309 '$mod'"]
1310 } else {
1311 # build module name and variant with variant specified
1312 set modnamevr [getAndParseModuleWithVariant [lindex $retlist 1] $vrlist]
1313 # update result if forbidden
1314 if {[isModuleTagged $modnamevr forbidden 0 [lindex $retlist 0]]} {
1315 set retlist [list {} [lindex $retlist 1] [lindex $retlist 2]\
1316 accesserr [getForbiddenMsg $modnamevr [lindex $retlist 0]]]
1317 }
1318 }
1319 if {[lindex $retlist 0] ne {}} {
1320 lappend retlist $modnamevr
1321 reportTrace "'[lindex $retlist 1]' ([lindex $retlist 0]) matching\
1322 '$mod'" {Select module}
1323 # no error if we look at loaded modules and passed mod not found loaded
1324 } elseif {[lindex $retlist 3] ne {notloaded} && $report_issue} {
1325 reportIssue {*}[lrange $retlist 3 5]
1326 }
1327 return $retlist
1328 }
1329
1330 proc isModuleLoaded {mod} {
1331 cacheCurrentModules
1332
1333 return [info exists ::g_loadedModules($mod)]
1334 }
1335
1336 proc getModulefileFromLoadedModule {mod} {
1337 if {[isModuleLoaded $mod]} {
1338 return $::g_loadedModules($mod)
1339 }
1340 }
1341
1342 proc getModulepathFromLoadedOrLoadingModule {mod} {
1343 set modfile [getModulefileFromLoadingModule $mod]
1344 if {![string length $modfile]} {
1345 set modfile [getModulefileFromLoadedModule $mod]
1346 }
1347 if {[isModuleVirtual $mod $modfile]} {
1348 # an empty string is returned if no enabled modulepath matches modfile
1349 return [findModulepathFromModulefile $modfile]
1350 } else {
1351 return [getModulepathFromModuleName $modfile $mod]
1352 }
1353 }
1354
1355 proc isModulefileLoaded {modfile} {
1356 cacheCurrentModules
1357
1358 return [info exists ::g_loadedModuleFiles($modfile)]
1359 }
1360
1361 proc getModuleFromLoadedModulefile {modfile {idx all}} {
1362 if {[isModulefileLoaded $modfile]} {
1363 if {$idx eq {all}} {
1364 return $::g_loadedModuleFiles($modfile)
1365 } else {
1366 return [lindex $::g_loadedModuleFiles($modfile) $idx]
1367 }
1368 }
1369 }
1370
1371 proc isModuleLoading {mod} {
1372 return [expr {$mod in [getLoadingModuleList]}]
1373 }
1374
1375 proc isModulefileLoading {modfile} {
1376 return [expr {$modfile in [getLoadingModuleFileList]}]
1377 }
1378
1379 proc getModulefileFromLoadingModule {mod} {
1380 if {[isModuleLoading $mod]} {
1381 set idx [lsearch -exact [getLoadingModuleList] $mod]
1382 return [lindex [getLoadingModuleFileList] $idx]
1383 }
1384 }
1385
1386 proc getModuleFromLoadingModulefile {modfile {idx all}} {
1387 if {[isModulefileLoading $modfile]} {
1388 set loadingmodlist [getLoadingModuleList]
1389 foreach i [lsearch -all -exact [getLoadingModuleFileList] $modfile] {
1390 lappend modlist [lindex $loadingmodlist $i]
1391 }
1392
1393 if {$idx eq {all}} {
1394 return $modlist
1395 } else {
1396 return [lindex $modlist $idx]
1397 }
1398 }
1399 }
1400
1401 proc isModuleRefreshQualified {mod} {
1402 return $::g_loadedModulesRefresh($mod)
1403 }
1404
1405 proc setLoadedModule {mod modfile uasked modvr refresh} {
1406 set ::g_loadedModules($mod) $modfile
1407 # a loaded modfile may correspond to multiple loaded virtual modules
1408 lappend ::g_loadedModuleFiles($modfile) $mod
1409
1410 # record if mod has been asked by user and relative loaded/auto-loaded tag
1411 set loadedtag [expr {$uasked ? {loaded} : {auto-loaded}}]
1412 setModuleAndVariantsTag $mod $modvr $loadedtag
1413
1414 # is module qualified for refresh evaluation
1415 set ::g_loadedModulesRefresh($mod) $refresh
1416
1417 # build dependency chain
1418 setModuleDependency $mod
1419 }
1420
1421 proc unsetLoadedModule {mod modfile} {
1422 unset ::g_loadedModules($mod)
1423 # a loaded modfile may correspond to multiple loaded virtual modules
1424 if {[llength $::g_loadedModuleFiles($modfile)] == 1} {
1425 unset ::g_loadedModuleFiles($modfile)
1426 } else {
1427 set ::g_loadedModuleFiles($modfile) [replaceFromList\
1428 $::g_loadedModuleFiles($modfile) $mod]
1429 }
1430
1431 unset ::g_loadedModulesRefresh($mod)
1432
1433 # update dependencies
1434 unsetModuleDependency $mod
1435 }
1436
1437 # return the currently loaded module whose name is the closest to the
1438 # name passed as argument. if no loaded module match at least one part
1439 # of the passed name, an empty string is returned.
1440 proc getLoadedWithClosestName {name modulepath_list} {
1441 set ret {}
1442 set retmax 1
1443
1444 if {[isModuleFullPath $name]} {
1445 set fullname [getAbsolutePath $name]
1446 # if module is passed as full modulefile path name, get corresponding
1447 # short name from used modulepaths
1448 if {[set shortname [findModuleNameFromModulefile $fullname]] ne {}} {
1449 set nametosplit $shortname
1450 # or look at lmfile names to return the eventual exact match
1451 } else {
1452 # module may be loaded with its full path name
1453 if {[isModuleLoaded $fullname]} {
1454 set ret $fullname
1455 # or name corresponds to the _lmfiles_ entry of a virtual modules in
1456 # which case lastly loaded virtual module is returned
1457 } elseif {[isModulefileLoaded $fullname]} {
1458 set ret [getModuleFromLoadedModulefile $fullname end]
1459 }
1460 }
1461 } else {
1462 set nametosplit $name
1463 }
1464
1465 if {[info exists nametosplit]} {
1466 cacheCurrentModules
1467 set icase [isIcase]
1468 defineModStartNbProc $icase
1469 defineModEqProc $icase [getConf extended_default]
1470 # compare name to each currently loaded module name
1471 ##nagelfar ignore Found constant
1472 foreach mod [getEnvLoadedModulePropertyParsedList name] {
1473 # if module loaded as fullpath but test name not, try to get loaded
1474 # mod short name (with currently used modulepaths) to compare it
1475 if {[isModuleFullPath $mod] && [set modname\
1476 [findModuleNameFromModulefile $mod]] ne {}} {
1477 # no alt name to retrieve if module has been loaded full path
1478 set matchmodlist [list $modname]
1479 } else {
1480 # add alternative names of mod to the matching list
1481 set matchmodlist [list $mod {*}[getLoadedAltname $mod]]
1482 }
1483
1484 # compare each element of the name to find closest answer. in case of
1485 # equality, last loaded module will be returned as it overwrites
1486 # previously found value
1487 foreach matchmod $matchmodlist {
1488 if {[set i [modStartNb $matchmod $nametosplit]] >= $retmax} {
1489 set retmax $i
1490 set ret $mod
1491 break
1492 }
1493 }
1494 }
1495 }
1496
1497 reportDebug "'$ret' closest to '$name'"
1498 return $ret
1499 }
1500
1501 proc getLoadedExactName {name modulepath_list} {
1502 return [getLoadedMatchingName $name {} 0 {} $modulepath_list eqspec]
1503 }
1504
1505 proc getLoadedEqstartName {name modulepath_list} {
1506 return [getLoadedMatchingName $name {} 0 {} $modulepath_list]
1507 }
1508
1509 # return the currently loaded module whose name is equal or include the name
1510 # passed as argument. if no loaded module match, an empty string is returned.
1511 # loading: look at currently loading modules instead of loaded if loading == 1
1512 # lmlist: only take into account passed loaded module list not all loaded mods
1513 proc getLoadedMatchingName {name {behavior {}} {loading 0} {lmlist {}}\
1514 {modulepath_list {}} {eqtest eqstart}} {
1515 set ret {}
1516 set retmax 0
1517 # get default behavior from unload_match_order config
1518 if {$behavior eq {}} {
1519 set behavior [getConf unload_match_order]
1520 }
1521
1522 # use loading-specific procedures instead of loaded-specific ones
1523 if {$loading} {
1524 set isModulefileLoaded isModulefileLoading
1525 set getModuleFromLoadedModulefile getModuleFromLoadingModulefile
1526 set isLoadedMatchSpecificPath isLoadingMatchSpecificPath
1527 set getLoadedModuleList [list getLoadingModuleList]
1528 } else {
1529 ##nagelfar ignore #4 Found constant
1530 set isModulefileLoaded isModulefileLoaded
1531 set getModuleFromLoadedModulefile getModuleFromLoadedModulefile
1532 set isLoadedMatchSpecificPath isLoadedMatchSpecificPath
1533 set getLoadedModuleList [list getEnvLoadedModulePropertyParsedList name]
1534 }
1535
1536 # fetch currently loaded/loading module name is no list provided
1537 if {![llength $lmlist]} {
1538 set lmlist [{*}$getLoadedModuleList]
1539 }
1540
1541 # if module is passed as full modulefile path name, look at lmfile names
1542 # to return the eventual exact match
1543 if {[isModuleFullPath $name]} {
1544 set mod [getAbsolutePath $name]
1545 # if module is loaded with its full path name loadedmodules entry is
1546 # equivalent to _lmfiles_ corresponding entry so only check _lmfiles_
1547 if {[$isModulefileLoaded $mod]} {
1548 # a loaded modfile may correspond to multiple loaded virtual modules
1549 switch -- $behavior {
1550 returnlast {
1551 # the last loaded/loading module will be returned
1552 set ret [$getModuleFromLoadedModulefile $mod end]
1553 }
1554 returnfirst {
1555 # the first loaded/loading module will be returned
1556 set ret [$getModuleFromLoadedModulefile $mod 0]
1557 }
1558 returnall {
1559 # all loaded/loading modules will be returned
1560 set ret [$getModuleFromLoadedModulefile $mod]
1561 }
1562 }
1563 }
1564 } elseif {$name ne {}} {
1565 defineModEqProc [isIcase] [getConf extended_default] [expr {!$loading}]
1566 # compare name to each currently loaded/loading module name, if multiple
1567 # mod match name:
1568 foreach mod $lmlist {
1569 # if module loaded as fullpath but test name not, try to get loaded
1570 # mod short name (with currently used modulepaths) to compare it
1571 if {[isModuleFullPath $mod] && [set modname\
1572 [findModuleNameFromModulefile $mod]] ne {}} {
1573 set matchmod $modname
1574 } else {
1575 set matchmod $mod
1576 }
1577 # test module matches specified modulepaths
1578 if {![$isLoadedMatchSpecificPath $mod $modulepath_list]} {
1579 continue
1580 }
1581 if {[modEq $name $matchmod $eqtest 1 [expr {$loading ? 1 : 2}] 1]} {
1582 switch -- $behavior {
1583 returnlast {
1584 # the last loaded module will be returned
1585 set ret $mod
1586 }
1587 returnfirst {
1588 # the first loaded module will be returned
1589 set ret $mod
1590 break
1591 }
1592 returnall {
1593 # all loaded modules will be returned
1594 lappend ret $mod
1595 }
1596 }
1597 }
1598 }
1599 }
1600
1601 reportDebug "'$ret' matches '$name'"
1602 return $ret
1603 }
1604
1605 # return if loaded mod is part of modulepath from specified constrained list
1606 proc isLoadedMatchSpecificPath {mod modulepath_list} {
1607 set mod_file [getModulefileFromLoadedModule $mod]
1608 return [isModulefileMatchSpecificPath $mod_file $modulepath_list]
1609 }
1610
1611 proc isLoadingMatchSpecificPath {mod modulepath_list} {
1612 set mod_file [getModulefileFromLoadingModule $mod]
1613 return [isModulefileMatchSpecificPath $mod_file $modulepath_list]
1614 }
1615
1616 proc isModulefileMatchSpecificPath {mod_file modulepath_list} {
1617 if {[llength $modulepath_list]} {
1618 return [isModulefileInModulepathList $mod_file $modulepath_list]
1619 } else {
1620 return 1
1621 }
1622 }
1623
1624 proc setLoadedSourceSh {mod args} {
1625 foreach arg $args {
1626 # each arg is a list with source-sh call string at index 0 and resulting
1627 # modulefile commands at all later index positions
1628 set shtomodargs [lindex $arg 0]
1629 set modcontent [lrange $arg 1 end]
1630 if {![llength [getLoadedSourceShScriptContent $mod $shtomodargs]]} {
1631 # filter alias/function/complete definition not to record them
1632 set shtomod_entry [list $shtomodargs]
1633 foreach modcmdlist $modcontent {
1634 set modcmd [lindex $modcmdlist 0]
1635 switch -- $modcmd {
1636 set-alias - set-function {
1637 # set an empty body to make valid unset-* call
1638 lappend shtomod_entry [list {*}[lrange $modcmdlist 0 1] {}]
1639 }
1640 complete {
1641 set complist [lrange $modcmdlist 0 2]
1642 # ensure only one order is recorded for each shell-name
1643 if {![info exists filtcomp($complist)]} {
1644 lappend shtomod_entry [list {*}$complist {}]
1645 set filtcomp($complist) 1
1646 }
1647 }
1648 default {
1649 lappend shtomod_entry $modcmdlist
1650 }
1651 }
1652 }
1653 lappend ::g_loadedModuleSourceSh($mod) $shtomod_entry
1654 }
1655 }
1656 }
1657
1658 proc unsetLoadedSourceSh {mod} {
1659 if {[info exists ::g_loadedModuleSourceSh($mod)]} {
1660 unset ::g_loadedModuleSourceSh($mod)
1661 }
1662 }
1663
1664 proc getLoadedSourceSh {mod} {
1665 if {[info exists ::g_loadedModuleSourceSh($mod)]} {
1666 return $::g_loadedModuleSourceSh($mod)
1667 }
1668 }
1669
1670 proc getLoadedSourceShScriptContent {mod script} {
1671 set content {}
1672 foreach shtomod_entry [getLoadedSourceSh $mod] {
1673 if {$script eq [lindex $shtomod_entry 0]} {
1674 set content [lrange $shtomod_entry 1 end]
1675 break
1676 }
1677 }
1678 return $content
1679 }
1680
1681 proc setLoadedConflict {mod args} {
1682 appendNoDupToList ::g_loadedModuleConflict($mod) {*}$args
1683 }
1684
1685 proc unsetLoadedConflict {mod} {
1686 if {[info exists ::g_loadedModuleConflict($mod)]} {
1687 unset ::g_loadedModuleConflict($mod)
1688 }
1689 }
1690
1691 proc getLoadedConflict {mod} {
1692 if {[info exists ::g_loadedModuleConflict($mod)]} {
1693 return $::g_loadedModuleConflict($mod)
1694 }
1695 }
1696
1697 proc getModuleLoadedConflict {mod {modulepath_list {}}} {
1698 set mod_con_list {}
1699 defineModEqProc [isIcase] [getConf extended_default] 1
1700 # get module short name if loaded by its full pathname
1701 if {[set isfullpath [isModuleFullPath $mod]]} {
1702 set smod [findModuleNameFromModulefile $mod]
1703 }
1704
1705 # check if any loaded module has declared a conflict
1706 foreach mod_con [array names ::g_loadedModuleConflict] {
1707 # look if some loaded or loading modules correspond to conflict defined
1708 # by mod
1709 if {$mod_con eq $mod || ($isfullpath && $mod_con eq $smod)} {
1710 foreach withmod $::g_loadedModuleConflict($mod_con) {
1711 # skip own reflexive conflict (look at mod main and alternative
1712 # names) and those already known
1713 if {![modEq $withmod $mod eqstart 1 2 1] && (!$isfullpath ||\
1714 ![modEq $withmod $smod eqstart 1 2 1]) && ([set lm_mod_list\
1715 [getLoadedMatchingName $withmod returnall]] ne {} || [set\
1716 lm_mod_list [getLoadedMatchingName $withmod returnall 1]] ne\
1717 {})} {
1718 # multiple loaded module may match conflict declared name
1719 appendNoDupToList mod_con_list {*}$lm_mod_list
1720 }
1721 }
1722 # other loaded module declared conflicts (skipping those already known)
1723 } elseif {$mod_con ni $mod_con_list} {
1724 foreach withmod $::g_loadedModuleConflict($mod_con) {
1725 # check if mod or one of its alt name match conflict
1726 if {[modEq $withmod $mod eqstart 1 2 1] || ($isfullpath &&\
1727 [modEq $withmod $smod eqstart 1 2 1])} {
1728 lappend mod_con_list $mod_con
1729 break
1730 }
1731 }
1732 }
1733 }
1734
1735 if {[isOtherVariantOfModuleLoaded $mod] || ([isModuleLoaded $mod] &&\
1736 ![isLoadedMatchSpecificPath $mod $modulepath_list])} {
1737 lappend mod_con_list [getModuleNameAndVersFromVersSpec $mod]
1738 }
1739
1740 reportDebug "'$mod' conflicts with '$mod_con_list'"
1741 return $mod_con_list
1742 }
1743
1744 proc setLoadedPrereq {mod args} {
1745 appendNoDupToList ::g_loadedModulePrereq($mod) {*}$args
1746 }
1747
1748 proc unsetLoadedPrereq {mod} {
1749 if {[info exists ::g_loadedModulePrereq($mod)]} {
1750 unset ::g_loadedModulePrereq($mod)
1751 }
1752 }
1753
1754 proc getLoadedPrereq {mod} {
1755 if {[info exists ::g_loadedModulePrereq($mod)]} {
1756 return $::g_loadedModulePrereq($mod)
1757 }
1758 }
1759
1760 proc setLoadedPrereqPath {mod args} {
1761 appendNoDupToList ::g_loadedModulePrereqPath($mod) {*}$args
1762 }
1763
1764 proc unsetLoadedPrereqPath {mod} {
1765 if {[info exists ::g_loadedModulePrereqPath($mod)]} {
1766 unset ::g_loadedModulePrereqPath($mod)
1767 }
1768 }
1769
1770 proc getLoadedPrereqPath {mod {prereq {}}} {
1771 if {[info exists ::g_loadedModulePrereqPath($mod)]} {
1772 if {![string length $prereq]} {
1773 return $::g_loadedModulePrereqPath($mod)
1774 }
1775 # return specific entry for given prereq
1776 array set prereq_path_arr $::g_loadedModulePrereqPath($mod)
1777 if {[info exists prereq_path_arr($prereq)]} {
1778 return $prereq_path_arr($prereq)
1779 }
1780 }
1781 }
1782
1783 proc setLoadedAltname {mod args} {
1784 foreach arg $args {
1785 switch -- [lindex $arg 0] {
1786 al {
1787 appendNoDupToList ::g_loadedModuleAliasAltname($mod) [lindex $arg\
1788 1]
1789 }
1790 as {
1791 appendNoDupToList ::g_loadedModuleAutoAltname($mod) [lindex $arg\
1792 1]
1793 }
1794 default {
1795 appendNoDupToList ::g_loadedModuleAltname($mod) [lindex $arg 0]
1796 }
1797 }
1798 }
1799 }
1800
1801 proc unsetLoadedAltname {mod} {
1802 if {[info exists ::g_loadedModuleAltname($mod)]} {
1803 unset ::g_loadedModuleAltname($mod)
1804 }
1805 if {[info exists ::g_loadedModuleAliasAltname($mod)]} {
1806 unset ::g_loadedModuleAliasAltname($mod)
1807 }
1808 if {[info exists ::g_loadedModuleAutoAltname($mod)]} {
1809 unset ::g_loadedModuleAutoAltname($mod)
1810 }
1811 }
1812
1813 proc getLoadedAltname {mod {typelist {sym alias autosym}} {with_type 0}} {
1814 set ret {}
1815 if {[info exists ::g_loadedModuleAltname($mod)] && {sym} in $typelist} {
1816 if {$with_type} {
1817 foreach altname $::g_loadedModuleAltname($mod) {
1818 lappend ret [list $altname]
1819 }
1820 } else {
1821 set ret $::g_loadedModuleAltname($mod)
1822 }
1823 }
1824 if {[info exists ::g_loadedModuleAliasAltname($mod)] && {alias} in\
1825 $typelist} {
1826 if {$with_type} {
1827 foreach altname $::g_loadedModuleAliasAltname($mod) {
1828 lappend ret [list al $altname]
1829 }
1830 } else {
1831 lappend ret {*}$::g_loadedModuleAliasAltname($mod)
1832 }
1833 }
1834 if {[info exists ::g_loadedModuleAutoAltname($mod)] && {autosym} in\
1835 $typelist} {
1836 if {$with_type} {
1837 foreach altname $::g_loadedModuleAutoAltname($mod) {
1838 lappend ret [list as $altname]
1839 }
1840 } else {
1841 lappend ret {*}$::g_loadedModuleAutoAltname($mod)
1842 }
1843 }
1844 return $ret
1845 }
1846
1847 proc setLoadedVariant {mod args} {
1848 lappend ::g_loadedModuleVariant($mod) {*}$args
1849 }
1850
1851 proc unsetLoadedVariant {mod} {
1852 if {[info exists ::g_loadedModuleVariant($mod)]} {
1853 unset ::g_loadedModuleVariant($mod)
1854 }
1855 }
1856
1857 proc getLoadedVariant {mod} {
1858 if {[info exists ::g_loadedModuleVariant($mod)]} {
1859 return $::g_loadedModuleVariant($mod)
1860 }
1861 }
1862
1863 proc getLoadedModuleWithVariantList {} {
1864 set modvrlist [list]
1865 foreach mod [getEnvLoadedModulePropertyParsedList name] {
1866 set modvr [list $mod]
1867 set vrlist [getVariantList $mod 5]
1868 if {[llength $vrlist]} {
1869 lappend modvr {*}$vrlist
1870 }
1871 lappend modvrlist $modvr
1872 }
1873 return $modvrlist
1874 }
1875
1876 proc getLoadedModuleWithVariantSaveTagArrayList {} {
1877 array set tag_arr {}
1878 foreach mod [getEnvLoadedModulePropertyParsedList name] {
1879 set modvr [list $mod]
1880 set vrlist [getVariantList $mod 1]
1881 if {[llength $vrlist]} {
1882 lappend modvr {*}$vrlist
1883 }
1884 # create entry in array only if tags set
1885 set tag_list [getSaveTagList $mod]
1886 if {[llength $tag_list]} {
1887 set tag_arr($modvr) $tag_list
1888 }
1889 }
1890 return [array get tag_arr]
1891 }
1892
1893 proc getAndParseLoadedModuleWithVariant {mod} {
1894 set vr_list [getVariantList $mod 1]
1895 return [getAndParseModuleWithVariant $mod $vr_list]
1896 }
1897
1898 proc getAndParseModuleWithVariant {mod vr_list} {
1899 if {[llength $vr_list]} {
1900 lassign [parseModuleSpecification 0 0 0 0 $mod {*}$vr_list] mod_vr
1901 } else {
1902 set mod_vr $mod
1903 }
1904 return $mod_vr
1905 }
1906
1907 proc setLoadedUse {mod args} {
1908 appendNoDupToList ::g_loadedModuleUse($mod) {*}$args
1909 }
1910
1911 proc unsetLoadedUse {mod} {
1912 if {[info exists ::g_loadedModuleUse($mod)]} {
1913 unset ::g_loadedModuleUse($mod)
1914 }
1915 }
1916
1917 proc getLoadedUse {mod} {
1918 if {[info exists ::g_loadedModuleUse($mod)]} {
1919 return $::g_loadedModuleUse($mod)
1920 }
1921 }
1922
1923 proc getViaModuleForModulepath {modpath} {
1924 lassign [getAllLoadedModuleUsingModulepath $modpath] via
1925 if {![string length $via] && $modpath ni [getModulePathList]} {
1926 # get via information collected from spider processing
1927 set via [getModuleUsingModulepath $modpath]
1928 }
1929 return $via
1930 }
1931
1932 proc getModuleUsingModulepath {modpath} {
1933 foreach mod [array names ::g_loadedModuleUse] {
1934 # no need to resolve variable reference in used path as this resolution
1935 # as already been performed when recording in global structure during
1936 # spider processing
1937 if {$modpath in $::g_loadedModuleUse($mod)} {
1938 return $mod
1939 }
1940 }
1941 }
1942
1943 # Return all loaded module that enables given modulepath. Reference counter
1944 # is checked to detect if modulepath was enabled prior loading the modules in
1945 # which case modulepath is considered not enabled by loaded modules. modpath
1946 # may not be currently enabled.
1947 proc getAllLoadedModuleUsingModulepath {modpath} {
1948 set mod_list {}
1949 foreach mod [getEnvLoadedModulePropertyParsedList name] {
1950 if {$modpath in [resolvStringListWithEnv [getLoadedUse $mod]]} {
1951 lappend mod_list $mod
1952 }
1953 }
1954 set ref_count [getPathReferenceCount MODULEPATH $modpath [getState\
1955 path_separator] 1]
1956 if {$ref_count <= [llength $mod_list]} {
1957 return $mod_list
1958 }
1959 }
1960
1961 proc unsetEnvLoadedModuleProperty {mod_name prop} {
1962 set prop_env_var [getLoadedModulePropertyEnvVarName $prop]
1963 set env_loaded_prop_list [getEnvLoadedModulePropertyList $prop]
1964 set mod_pattern_in_env_loaded_prop [escapeGlobChars $mod_name][getState\
1965 sub1_separator]*
1966
1967 set mod_env_loaded_prop [lsearch -glob -inline $env_loaded_prop_list\
1968 $mod_pattern_in_env_loaded_prop]
1969
1970 if {[string length $mod_env_loaded_prop]} {
1971 remove-path $prop_env_var $mod_env_loaded_prop
1972 }
1973 }
1974
1975 proc setEnvLoadedModuleProperty {mod_name prop value_list} {
1976 set serialize_depth [getEnvLoadedModulePropertyStructDepth $prop]
1977 set serialize_proc serialize${serialize_depth}ModulePropertyValue
1978 set char_map_list [getEnvLoadedModulePropertyCharMapList $prop]
1979 set prop_env_var [getLoadedModulePropertyEnvVarName $prop]
1980
1981 if {[llength $value_list]} {
1982 set serialized_value_list [$serialize_proc $mod_name $value_list\
1983 $char_map_list]
1984 append-path $prop_env_var $serialized_value_list
1985 }
1986 }
1987
1988 proc getEnvLoadedModulePropertyStructDepth {prop} {
1989 switch -- $prop {
1990 file - init - modulepath - name - refresh {
1991 return 0Lvl
1992 }
1993 conflict - extratag - tag - use {
1994 return 1Lvl
1995 }
1996 altname - prereq - prereqpath - sourcesh - stickyrule - variant {
1997 return 2Lvl
1998 }
1999 }
2000 }
2001
2002 proc getEnvLoadedModulePropertyCharUnmapList {prop} {
2003 set char_unmap_list {}
2004 foreach {char mapped} [getEnvLoadedModulePropertyCharMapList $prop] {
2005 lappend char_unmap_list $mapped $char
2006 }
2007 return $char_unmap_list
2008 }
2009
2010 proc getEnvLoadedModulePropertyCharMapList {prop} {
2011 return [switch -- $prop {
2012 sourcesh {
2013 list [getState path_separator] <EnvModEscPS>\
2014 [getState sub1_separator] <EnvModEscS1>\
2015 [getState sub2_separator] <EnvModEscS2>
2016 }
2017 default {
2018 list : <
2019 }
2020 }]
2021 }
2022
2023 proc serialize2LvlModulePropertyValue {mod structured_value_list\
2024 char_map_list} {
2025 set sub2_sepa [getState sub2_separator]
2026 set serialized_value_list [list]
2027 foreach structured_value $structured_value_list {
2028 set mapped_structured_value [string map $char_map_list\
2029 $structured_value]
2030 lappend serialized_value_list [join $mapped_structured_value $sub2_sepa]
2031 }
2032 # no character translation as it already occurred here
2033 return [serialize1LvlModulePropertyValue $mod $serialized_value_list {}]
2034 }
2035
2036 proc serialize1LvlModulePropertyValue {mod structured_value_list\
2037 char_map_list} {
2038 set serialized_value [join [list $mod {*}$structured_value_list] [getState\
2039 sub1_separator]]
2040 return [string map $char_map_list $serialized_value]
2041 }
2042
2043 proc unserialize2LvlModulePropertyValueList {serialized_value_list\
2044 char_unmap_list} {
2045 set lvl1_serialized_value_list [unserialize1LvlModulePropertyValueList\
2046 $serialized_value_list {}]
2047 set structured_value_list {}
2048 set sub2_sepa [getState sub2_separator]
2049 foreach lvl1_serialized_value $lvl1_serialized_value_list {
2050 set lvl1_strutured_value {}
2051 set first 1
2052 foreach lvl2_serialized_value $lvl1_serialized_value {
2053 # keep first element as string (module name)
2054 if {$first} {
2055 set lvl2_structured_value [string map $char_unmap_list\
2056 $lvl2_serialized_value]
2057 set first 0
2058 } else {
2059 set lvl2_structured_value {}
2060 foreach value [split $lvl2_serialized_value $sub2_sepa] {
2061 lappend lvl2_structured_value [string map $char_unmap_list\
2062 $value]
2063 }
2064 }
2065 lappend lvl1_strutured_value $lvl2_structured_value
2066 }
2067 lappend structured_value_list $lvl1_strutured_value
2068 }
2069 return $structured_value_list
2070 }
2071
2072 proc unserialize1LvlModulePropertyValueList {serialized_value_list\
2073 char_unmap_list} {
2074 set structured_value_list {}
2075 set sub1_sepa [getState sub1_separator]
2076 foreach serialized_value $serialized_value_list {
2077 set structured_value {}
2078 foreach value [split $serialized_value $sub1_sepa] {
2079 if {[string length $value]} {
2080 lappend structured_value [string map $char_unmap_list $value]
2081 }
2082 }
2083 # ignore empty element (1 is meaningless as first elt is loaded mod)
2084 if {[llength $structured_value] > 1} {
2085 lappend structured_value_list $structured_value
2086 }
2087 }
2088 return $structured_value_list
2089 }
2090
2091 proc unserialize0LvlModulePropertyValueList {serialized_value_list\
2092 char_unmap_list} {
2093 set value_list {}
2094 foreach serialized_value $serialized_value_list {
2095 if {[string length $serialized_value]} {
2096 lappend value_list [string map $char_unmap_list $serialized_value]
2097 }
2098 }
2099 return $value_list
2100
2101 }
2102
2103 # get current loaded state to record it as the virtual init collection
2104 proc getLoadedInit {} {
2105 set ret [formatCollectionContent [getModulePathList returnempty 0]\
2106 [getLoadedModuleWithVariantList]\
2107 [getLoadedModuleWithVariantSaveTagArrayList] {}]
2108
2109 # remove ending newline in collection output
2110 set ret [string trimright $ret \n]
2111
2112 # get init state as a string that can be registered in an env var translate
2113 # tag list separator ':' into '<' and line split '\n' into ':'
2114 return [string map {: < \n :} $ret]
2115 }
2116
2117 # get all modules from all enabled modulepaths that matches a specification
2118 proc getAllAvailModule {mod} {
2119 array set mod_list {}
2120 # search is done from lowest to greatest priority path (if result array
2121 # content is kept later on, it will hold accurate priority information)
2122 foreach dir [lreverse [getModulePathList]] {
2123 array set mod_list [getModules $dir $mod 0 [list rc_defs_included]]
2124 }
2125 return [array names mod_list]
2126 }
2127
2128 # register conflict violation state between loaded modules
2129 proc setModuleConflictViolation {mod modconlist} {
2130 reportDebug "set conflict violation state for '$mod'"
2131 set ::g_conflictViolation($mod) $modconlist
2132 # also update violation state for loaded mod conflicting with mod
2133 foreach lmmod $modconlist {
2134 if {[appendNoDupToList ::g_conflictViolation($lmmod) $mod]} {
2135 reportDebug "set/update conflict violation state for '$lmmod'"
2136 }
2137 }
2138 }
2139
2140 # unregister conflict violation state between modules
2141 proc unsetModuleConflictViolation {mod} {
2142 if {[info exists ::g_conflictViolation($mod)]} {
2143 # also update violation state for loaded mod conflicting with mod
2144 foreach lmmod $::g_conflictViolation($mod) {
2145 set convio [replaceFromList\
2146 $::g_conflictViolation($lmmod) $mod]
2147 reportDebug "unset/update conflict violation state for '$lmmod'"
2148 if {![llength $convio]} {
2149 unset ::g_conflictViolation($lmmod)
2150 } else {
2151 set ::g_conflictViolation($lmmod) $convio
2152 }
2153 }
2154 reportDebug "unset conflict violation state for '$mod'"
2155 unset ::g_conflictViolation($mod)
2156 }
2157 }
2158
2159 # build dependency chain between loaded modules based on registered prereqs
2160 proc setModuleDependency {mod} {
2161 set deplist {}
2162 set depnpolist {}
2163
2164 foreach prereq_match_aftmatch [getLoadedModulePrereqListAndLoadedMatch\
2165 $mod] {
2166 set prereq_item_match_aftmatch_list [lassign $prereq_match_aftmatch\
2167 prereq prereq_path_list]
2168
2169 set lmprelist {}
2170 set lmnpolist {}
2171 foreach {prereq_mod_item bef_loaded_mod_match aft_loaded_mod_match}\
2172 $prereq_item_match_aftmatch_list {
2173 if {[string length $bef_loaded_mod_match]} {
2174 appendNoDupToList lmprelist $bef_loaded_mod_match
2175 appendNoDupToList lmnpolist $bef_loaded_mod_match
2176 } else {
2177 reportDebug "set an unmet requirement on '$prereq_mod_item' for\
2178 '$mod'"
2179 lappend ::g_moduleUnmetDep($mod) $prereq_mod_item
2180 lappend ::g_unmetDepHash($prereq_mod_item) $mod $prereq_path_list
2181 }
2182
2183 # is requirement loaded after mod (in *No Particular Order*)
2184 if {[string length $aft_loaded_mod_match]} {
2185 appendNoDupToList lmnpolist $aft_loaded_mod_match
2186 }
2187 }
2188
2189 switch -- [llength $lmprelist] {
2190 0 {
2191 # prereq not satisfied
2192 reportDebug "set prereq violation state for '$mod'"
2193 lappend ::g_prereqViolation($mod) $prereq
2194 }
2195 1 {
2196 set lmmod [lindex $lmprelist 0]
2197 lappend deplist [list $lmmod]
2198 # set 'is depended by' relations
2199 lappend ::g_dependHash($lmmod) [list $mod]
2200 }
2201 default {
2202 lappend deplist $lmprelist
2203 # many modules in prereq list, means they all set an optional dep
2204 foreach lmmod $lmprelist {
2205 lappend ::g_dependHash($lmmod) [list $mod 1]
2206 }
2207 }
2208 }
2209
2210 # build 'is depended by' relations not taking loading order into account
2211 switch -- [llength $lmnpolist] {
2212 0 {
2213 # even on No Particular Order mode, prereq is not satisfied
2214 reportDebug "set NPO prereq violation state for '$mod'"
2215 lappend ::g_prereqNPOViolation($mod) $prereq
2216 }
2217 1 {
2218 set lmmod [lindex $lmnpolist 0]
2219 lappend depnpolist [list $lmmod]
2220 # set 'is depended by' relations
2221 lappend ::g_dependNPOHash($lmmod) [list $mod]
2222 }
2223 default {
2224 lappend depnpolist $lmnpolist
2225 # many modules in prereq list, means they all set an optional dep
2226 foreach lmmod $lmnpolist {
2227 lappend ::g_dependNPOHash($lmmod) [list $mod 1]
2228 }
2229 }
2230 }
2231 }
2232
2233 # conflict not satisfied
2234 set modconlist [getModuleLoadedConflict $mod]
2235 if {[llength $modconlist]} {
2236 setModuleConflictViolation $mod $modconlist
2237 }
2238
2239 # update eventual registered unmet dependencies
2240 foreach modpre [array names ::g_unmetDepHash] {
2241 if {[modEq $modpre $mod eqstart 1 2 1]} {
2242 reportDebug "refresh requirements targeting '$modpre'"
2243 foreach {lmmod prereq_path_list} $::g_unmetDepHash($modpre) {
2244 if {![isLoadedMatchSpecificPath $mod $prereq_path_list]} {
2245 continue
2246 }
2247 if {$mod in [getDependentLoadedModuleList [list $lmmod] 0 0]} {
2248 reportDebug "skip deps refresh for '$lmmod' as dep cycle\
2249 detected with '$mod'"
2250
2251 # remove dependency link in no particular order structs to
2252 # avoid cycle first in 'is depended by' struct
2253 if {[info exists ::g_dependNPOHash($mod)]} {
2254 set depmodlist $::g_dependNPOHash($mod)
2255 for {set i 0} {$i < [llength $depmodlist]} {incr i 1} {
2256 if {[lindex [lindex $depmodlist $i] 0] eq $lmmod} {
2257 set depmodlist [lreplace $depmodlist $i $i]
2258 break
2259 }
2260 }
2261 set ::g_dependNPOHash($mod) $depmodlist
2262 reportDebug "update NPO dependent of '$mod' to\
2263 '$depmodlist'"
2264 }
2265 # then update 'depend on' struct
2266 set lmmoddepnpolist {}
2267 foreach depmodlist $::g_moduleNPODepend($lmmod) {
2268 if {[set depidx [lsearch -exact $depmodlist $mod]] != -1} {
2269 set depmodlist [lreplace $depmodlist $depidx $depidx]
2270 # implies to update consistently alternate requirement or
2271 # violation state if no alternative loaded
2272 switch -- [llength $depmodlist] {
2273 0 {
2274 # do not know exact prereq name, so use correspond.
2275 # loaded module matching it
2276 lappend ::g_prereqNPOViolation($lmmod) $mod
2277 reportDebug "set NPO prereq violation state for\
2278 '$lmmod'"
2279 }
2280 1 {
2281 # update alternate loaded mod which became a strong
2282 # requirement
2283 set altmod [lindex $depmodlist 0]
2284 set ::g_dependNPOHash($altmod) [replaceFromList\
2285 $::g_dependNPOHash($altmod) [list $lmmod 1]\
2286 $lmmod]
2287 reportDebug "update NPO dependent of '$altmod' to\
2288 '$::g_dependNPOHash($altmod)'"
2289 }
2290 }
2291 }
2292 lappend lmmoddepnpolist $depmodlist
2293 }
2294 reportDebug "update NPO requirement of '$lmmod' to\
2295 '$lmmoddepnpolist'"
2296 set ::g_moduleNPODepend($lmmod) $lmmoddepnpolist
2297 } else {
2298 # refresh actual dependencies of targeting mod
2299 unsetModuleDependency $lmmod
2300 setModuleDependency $lmmod
2301 }
2302 }
2303 }
2304 }
2305
2306 # set 'depends on' relation
2307 reportDebug "set requirements of '$mod' to '$deplist'"
2308 set ::g_moduleDepend($mod) $deplist
2309 reportDebug "set NPO requirements of '$mod' to '$depnpolist'"
2310 set ::g_moduleNPODepend($mod) $depnpolist
2311 }
2312
2313 # update dependency chain when unloading module
2314 proc unsetModuleDependency {mod} {
2315 foreach lmmodlist $::g_moduleDepend($mod) {
2316 set manymod [expr {[llength $lmmodlist] > 1}]
2317
2318 # unset 'is depended by' mod relations
2319 foreach lmmod $lmmodlist {
2320 if {[info exists ::g_dependHash($lmmod)]} {
2321 if {$manymod} {
2322 set hashdep [list $mod 1]
2323 } else {
2324 set hashdep [list $mod]
2325 }
2326 set ::g_dependHash($lmmod) [replaceFromList\
2327 $::g_dependHash($lmmod) $hashdep]
2328 if {![llength $::g_dependHash($lmmod)]} {
2329 unset ::g_dependHash($lmmod)
2330 }
2331 }
2332 }
2333 }
2334 # unset mod's 'depends on' relation
2335 reportDebug "unset requirements of '$mod'"
2336 unset ::g_moduleDepend($mod)
2337
2338 foreach lmmodlist $::g_moduleNPODepend($mod) {
2339 set manymod [expr {[llength $lmmodlist] > 1}]
2340
2341 # unset 'is depended by' mod relations
2342 foreach lmmod $lmmodlist {
2343 if {[info exists ::g_dependNPOHash($lmmod)]} {
2344 if {$manymod} {
2345 set hashdep [list $mod 1]
2346 } else {
2347 set hashdep [list $mod]
2348 }
2349 set ::g_dependNPOHash($lmmod) [replaceFromList\
2350 $::g_dependNPOHash($lmmod) $hashdep]
2351 if {![llength $::g_dependNPOHash($lmmod)]} {
2352 unset ::g_dependNPOHash($lmmod)
2353 }
2354 }
2355 }
2356 }
2357 # unset mod's No Particular Order 'depends on' relation
2358 reportDebug "unset NPO requirements of '$mod'"
2359 unset ::g_moduleNPODepend($mod)
2360
2361 # unset eventual violation states
2362 if {[info exists ::g_prereqViolation($mod)]} {
2363 reportDebug "unset prereq violation state for '$mod'"
2364 unset ::g_prereqViolation($mod)
2365 }
2366 if {[info exists ::g_prereqNPOViolation($mod)]} {
2367 reportDebug "unset NPO prereq violation state for '$mod'"
2368 unset ::g_prereqNPOViolation($mod)
2369 }
2370 unsetModuleConflictViolation $mod
2371
2372 # unset eventual registered unmet dependencies
2373 if {[info exists ::g_moduleUnmetDep($mod)]} {
2374 foreach ummod $::g_moduleUnmetDep($mod) {
2375 if {[info exists ::g_unmetDepHash($ummod)]} {
2376 set idx [lsearch -exact $::g_unmetDepHash($ummod) $mod]
2377 set ::g_unmetDepHash($ummod) [lreplace $::g_unmetDepHash($ummod)\
2378 $idx $idx+1]
2379 if {![llength $::g_unmetDepHash($ummod)]} {
2380 unset ::g_unmetDepHash($ummod)
2381 }
2382 }
2383 }
2384 reportDebug "unset unmet requirements for '$mod'"
2385 unset ::g_moduleUnmetDep($mod)
2386 }
2387
2388 # unset mod's 'is depended by' relations
2389 set hashdeplist [getDirectDependentList $mod]
2390 if {[llength $hashdeplist]} {
2391 reportDebug "refresh dependent of '$mod'"
2392 foreach lmmod $hashdeplist {
2393 # refresh actual dependencies of targeting mod
2394 unsetModuleDependency $lmmod
2395 setModuleDependency $lmmod
2396 }
2397 }
2398 }
2399
2400 # return the list of prereqs of a loaded or loading module and the associated
2401 # loaded module corresponding to these prereq definitions. The list of each
2402 # prereq is returned, each entry is a list of each prereq item definition
2403 # associated to the matching module loaded prior passed mod and loaded after
2404 proc getLoadedModulePrereqListAndLoadedMatch {mod} {
2405 set prereq_match_aftmatch_list {}
2406 set prereq_list [getLoadedPrereq $mod]
2407
2408 set via_mod_list {}
2409 if {[getConf require_via]} {
2410 set modpath [getModulepathFromLoadedOrLoadingModule $mod]
2411 set via_mod_list [getAllLoadedModuleUsingModulepath $modpath]
2412 }
2413
2414 # skip processing if no prereq defined
2415 if {![llength $prereq_list] && ![llength $via_mod_list]} {
2416 return
2417 }
2418
2419 set loaded_mod_list [getEnvLoadedModulePropertyParsedList name]
2420 set mod_load_idx [lsearch -exact $loaded_mod_list $mod]
2421 # consider mod as the lastly loaded if not found in module list
2422 if {$mod_load_idx == -1} {
2423 set mod_load_idx [llength $loaded_mod_list]
2424 }
2425
2426 # distinguish modules loaded before or after passed loaded mod
2427 set bef_loaded_mod_list [lrange $loaded_mod_list 0 $mod_load_idx]
2428 set aft_loaded_mod_list [lrange $loaded_mod_list $mod_load_idx+1 end]
2429 # reverse list to get closest match if returning lastly loaded module
2430 if {[getConf unload_match_order] eq {returnlast}} {
2431 set bef_loaded_mod_list [lreverse $bef_loaded_mod_list]
2432 }
2433
2434 defineModEqProc [isIcase] [getConf extended_default] 1
2435
2436 foreach prereq $prereq_list {
2437 set prereq_path_list [getLoadedPrereqPath $mod $prereq]
2438 set prereq_match_aftmatch [list $prereq $prereq_path_list]
2439
2440 foreach prereq_mod_item $prereq {
2441 # look at modules loaded before to find requirements
2442 set bef_loaded_mod_match {}
2443 foreach loaded_mod $bef_loaded_mod_list {
2444 if {[isLoadedMatchSpecificPath $loaded_mod $prereq_path_list] &&\
2445 [modEq $prereq_mod_item $loaded_mod eqstart 1 2 1]} {
2446 set bef_loaded_mod_match $loaded_mod
2447 break
2448 }
2449 }
2450
2451 # modules loaded afterward are unmet dependencies as dependent have
2452 # not been reloaded after them
2453 set aft_loaded_mod_match {}
2454 foreach loaded_mod $aft_loaded_mod_list {
2455 if {[isLoadedMatchSpecificPath $loaded_mod $prereq_path_list] &&\
2456 [modEq $prereq_mod_item $loaded_mod eqstart 1 2 1]} {
2457 set aft_loaded_mod_match $loaded_mod
2458 break
2459 }
2460 }
2461
2462 lappend prereq_match_aftmatch $prereq_mod_item $bef_loaded_mod_match\
2463 $aft_loaded_mod_match
2464 }
2465
2466 lappend prereq_match_aftmatch_list $prereq_match_aftmatch
2467 }
2468
2469 # add via module requirement if enabled and if any
2470 if {[llength $via_mod_list]} {
2471 set bef_loaded_mod_match {}
2472 foreach loaded_mod $bef_loaded_mod_list {
2473 if {$loaded_mod in $via_mod_list} {
2474 set bef_loaded_mod_match $loaded_mod
2475 break
2476 }
2477 }
2478 set aft_loaded_mod_match {}
2479 foreach loaded_mod $aft_loaded_mod_list {
2480 if {$loaded_mod in $via_mod_list} {
2481 set aft_loaded_mod_match $loaded_mod
2482 break
2483 }
2484 }
2485 # use "<VIA>" string as prereq specification, this value is used for
2486 # unsatisfied requirement, which does not exist for via requirement
2487 lappend prereq_match_aftmatch_list [list <VIA> {} <VIA>\
2488 $bef_loaded_mod_match $aft_loaded_mod_match]
2489 }
2490
2491 return $prereq_match_aftmatch_list
2492 }
2493
2494 # returns if any loaded module (if passed mod is empty) or passed mod and all
2495 # its requirement chain satisfy their loading constraints (prereq & conflict)
2496 proc areModuleConstraintsSatisfied {{mod {}} {nporeq 0}} {
2497 set ret 1
2498 cacheCurrentModules
2499
2500 # are requirements loaded after their dependent included or not
2501 if {$nporeq} {
2502 set reqVioVar ::g_prereqNPOViolation
2503 set reqListVar ::g_moduleNPODepend
2504 } else {
2505 set reqVioVar ::g_prereqViolation
2506 set reqListVar ::g_moduleDepend
2507 }
2508
2509 # check if any loaded module violates its prereq or conflict constraints
2510 ##nagelfar vartype reqVioVar varName
2511 if {$mod eq {}} {
2512 if {[array size ::g_conflictViolation] || [array size $reqVioVar]} {
2513 set ret 0
2514 }
2515 } else {
2516 set fulllist [list $mod]
2517 for {set i 0} {$i < [llength $fulllist]} {incr i 1} {
2518 set depmod [lindex $fulllist $i]
2519
2520 # check if depmod violates its prereq or conflict constraints
2521 ##nagelfar ignore +2 Suspicious variable name
2522 if {[info exists ::g_conflictViolation($depmod)] || [info exists\
2523 ${reqVioVar}($depmod)]} {
2524 # found violation among the requirement chain of mod so the
2525 # constraint of mod are not satisfied
2526 set ret 0
2527 break
2528 }
2529 # add requirements of depmod to the module to check list
2530 ##nagelfar ignore #2 Suspicious variable name
2531 if {[info exists ${reqListVar}($depmod)]} {
2532 foreach lmmodlist [set ${reqListVar}($depmod)] {
2533 appendNoDupToList fulllist {*}$lmmodlist
2534 }
2535 }
2536 }
2537 }
2538
2539 return $ret
2540 }
2541
2542 proc cacheCurrentModules {{exitonerr 1}} {
2543 # parse loaded modules information only once, global arrays are updated
2544 # afterwards when module commands update loaded modules state
2545 if {![isStateDefined lm_info_cached]} {
2546 setState lm_info_cached 1
2547 # mark specific as well as generic modules as loaded
2548 set i 0
2549 set modfilelist [getEnvLoadedModulePropertyParsedList file]
2550 set modlist [getEnvLoadedModulePropertyParsedList name]
2551 set refreshlist [getEnvLoadedModulePropertyParsedList refresh]
2552
2553 if {[llength $modlist] == [llength $modfilelist]} {
2554 # cache declared variant of loaded modules
2555 foreach modvrspec [getEnvLoadedModulePropertyParsedList variant] {
2556 setLoadedVariant {*}$modvrspec
2557 # prepare modvr designation of loaded mod needed in next steps
2558 set mod [lindex $modvrspec 0]
2559 set vrlist [getVariantList $mod 6]
2560 set modvr [list $mod {*}$vrlist]
2561 set modvrarr($mod) $modvr
2562 }
2563
2564 # cache declared tags of loaded modules
2565 foreach modtag [getEnvLoadedModulePropertyParsedList tag] {
2566 set tag_list [lassign $modtag mod]
2567 if {[info exists modvrarr($mod)]} {
2568 set mod_with_vr $modvrarr($mod)
2569 } else {
2570 set mod_with_vr {}
2571 }
2572 setModuleAndVariantsTag $mod $mod_with_vr {*}$tag_list
2573 }
2574 foreach modtag [getEnvLoadedModulePropertyParsedList extratag] {
2575 set tag_list [lassign $modtag mod]
2576 if {[info exists modvrarr($mod)]} {
2577 set mod_with_vr $modvrarr($mod)
2578 } else {
2579 set mod_with_vr {}
2580 }
2581 setModuleAndVariantsExtraTag $mod $mod_with_vr {*}$tag_list
2582 }
2583 foreach modstickyrule [getEnvLoadedModulePropertyParsedList\
2584 stickyrule] {
2585 set mod [lindex $modstickyrule 0]
2586 foreach modtagspeclist [lrange $modstickyrule 1 end] {
2587 set modspec_list [lassign $modtagspeclist modtag]
2588 foreach modspec $modspec_list {
2589 lassign [parseModuleSpecification 0 0 0 0 {*}$modspec]\
2590 parsed_modspec
2591 # record tag definition on given module specification
2592 setModspecTag $parsed_modspec $modtag
2593 }
2594 }
2595 }
2596
2597 # cache declared alternative names of loaded modules
2598 foreach modalt [getEnvLoadedModulePropertyParsedList altname] {
2599 setLoadedAltname {*}$modalt
2600 }
2601
2602 # cache declared source-sh of loaded modules
2603 foreach modsrcsh [getEnvLoadedModulePropertyParsedList sourcesh] {
2604 setLoadedSourceSh {*}$modsrcsh
2605 }
2606
2607 # cache declared conflict of loaded modules
2608 foreach modcon [getEnvLoadedModulePropertyParsedList conflict] {
2609 # parse module version specification to record translation
2610 foreach modconelt [lrange $modcon 1 end] {
2611 parseModuleSpecification 0 0 0 0 {*}$modconelt
2612 }
2613 setLoadedConflict {*}$modcon
2614 }
2615
2616 # cache declared prereq of loaded modules, prior to setLoadedModule
2617 # which triggers dependency chain build
2618 foreach modpre [getEnvLoadedModulePropertyParsedList prereq] {
2619 # parse module version specification to record translation
2620 foreach modpreeltlist [lrange $modpre 1 end] {
2621 foreach modpreelt $modpreeltlist {
2622 parseModuleSpecification 0 0 0 0 {*}$modpreelt
2623 }
2624 }
2625 setLoadedPrereq {*}$modpre
2626 }
2627 foreach modpre_path [getEnvLoadedModulePropertyParsedList\
2628 prereqpath] {
2629 setLoadedPrereqPath {*}$modpre_path
2630 }
2631
2632 foreach moduse [getEnvLoadedModulePropertyParsedList use] {
2633 setLoadedUse {*}$moduse
2634 }
2635
2636 foreach mod $modlist {
2637 # get all tags also recorded on mod and vr designation
2638 if {[info exists modvrarr($mod)]} {
2639 set modvr $modvrarr($mod)
2640 } else {
2641 set modvr {}
2642 }
2643 setLoadedModule $mod [lindex $modfilelist $i] [expr\
2644 {![isModuleTagged $mod auto-loaded 1]}] $modvr [expr {$mod in\
2645 $refreshlist}]
2646 incr i
2647 }
2648
2649 reportDebug "$i loaded"
2650 } else {
2651 set errproc [expr {$exitonerr ? {reportErrorAndExit} :\
2652 {reportError}}]
2653 $errproc "Loaded environment state is\
2654 inconsistent\nLOADEDMODULES=$modlist\n_LMFILES_=$modfilelist"
2655 }
2656 }
2657 }
2658
2659 # This proc resolves module aliases or version aliases to the real module name
2660 # and version.
2661 proc resolveModuleVersionOrAlias {name icase} {
2662 set name [getArrayKey ::g_moduleResolved $name $icase]
2663 if {[info exists ::g_moduleResolved($name)]} {
2664 set ret $::g_moduleResolved($name)
2665 } else {
2666 set ret $name
2667 }
2668
2669 reportTrace "'$name' into '$ret'" Resolve
2670 return $ret
2671 }
2672
2673 proc parseAccessIssue {modfile} {
2674 # retrieve and return access issue message
2675 if {[regexp {POSIX .* \{(.*)\}$} $::errorCode match errMsg]} {
2676 return "[string totitle $errMsg] on '$modfile'"
2677 } else {
2678 return "Cannot access '$modfile'"
2679 }
2680 }
2681
2682 proc checkValidModule {modfile} {
2683 # test file only once, cache result obtained to minimize file query
2684 # consider modfile valid without reading it if mcookie_check < always
2685 return [expr {[info exists ::g_modfileValid($modfile)]\
2686 ? $::g_modfileValid($modfile)\
2687 : [set ::g_modfileValid($modfile) [expr {[getConf mcookie_check] eq\
2688 {always} ? [getModuleValidy $modfile] : [list true {}]}]]}]
2689 }
2690
2691 # get file modification time, cache it at first query, use cache afterward
2692 proc getFileMtime {fpath} {
2693 if {[info exists ::g_fileMtime($fpath)]} {
2694 return $::g_fileMtime($fpath)
2695 } else {
2696 # protect 'file mtime' call in case we do not know what we are checking
2697 # when mcookie is not checked
2698 if {[catch {
2699 set mtime [file mtime $fpath]
2700 } errMsg ]} {
2701 reportError [parseAccessIssue $fpath]
2702 set mtime {}
2703 }
2704 return [set ::g_fileMtime($fpath) $mtime]
2705 }
2706 }
2707
2708 # define proc that will be used as fallback to command provided by extension
2709 # library in case this library is not loaded
2710 proc __readFile {filename {firstline 0} {must_have_cookie 0}} {
2711 set fid [open $filename r]
2712 if {$firstline} {
2713 set fdata [gets $fid]
2714 } else {
2715 # read a first data chunk and check if magic cookie is there if
2716 # mandatory. skip read of any additional content if mandatory cookie is
2717 # not found
2718 set fdata [read $fid 4096]
2719 if {![eof $fid] && (!$must_have_cookie || [string equal -length 8\
2720 $fdata {#%Module}])} {
2721 append fdata [read $fid]
2722 }
2723 }
2724 close $fid
2725 return $fdata
2726 }
2727
2728 proc getModuleValidy {modfile} {
2729 if {[catch {lassign [getModuleHeaderAndContent $modfile 1] header}]} {
2730 set msg [parseAccessIssue $modfile]
2731 set valid accesserr
2732 } else {
2733 lassign [getModuleHeaderValidity $header] valid msg
2734 }
2735 return [list $valid $msg]
2736 }
2737
2738 proc getModuleHeaderAndContent {modfile {only_get_header 0}} {
2739 if {[info exists ::g_modfileContent($modfile)]} {
2740 set res $::g_modfileContent($modfile)
2741 } else {
2742 # only read beginning of file if just checking validity and not asked to
2743 # always fully read files. when full file should be read cookie is
2744 # checked if asked to avoid to fully read non modfile
2745 set content [readFile $modfile [expr {$only_get_header &&\
2746 ![currentState always_read_full_file]}] 1]
2747 # extract magic cookie (first word of modulefile)
2748 set header [string trimright [lindex [split [string range $content 0\
2749 32]] 0] #]
2750 set res [list $header $content]
2751 # cache full file read to minimize file operations
2752 if {!$only_get_header || [currentState always_read_full_file]} {
2753 set ::g_modfileContent($modfile) $res
2754 }
2755 }
2756 return $res
2757 }
2758
2759 proc getModuleHeaderValidity {header} {
2760 if {![string equal -length 8 $header {#%Module}]} {
2761 set msg {Magic cookie '#%Module' missing}
2762 set valid invalid
2763 # check if specified min version requirement is met
2764 } elseif {[string length $header] > 8 && [getConf mcookie_version_check]\
2765 && [versioncmp [getState modules_release] [string range $header 8 end]]\
2766 < 0} {
2767 set msg "Modulefile requires at least Modules version [string range\
2768 $header 8 end]"
2769 set valid invalid
2770 } else {
2771 set msg {}
2772 set valid true
2773 }
2774 return [list $valid $msg]
2775 }
2776
2777 proc getModuleContent {mod_file} {
2778 if {[catch {lassign [getModuleHeaderAndContent $mod_file] mod_header\
2779 mod_content}]} {
2780 error [parseAccessIssue $mod_file] {} MODULES_ERR_READ
2781 }
2782 # raise error if module is not valid
2783 lassign [getModuleHeaderValidity $mod_header] mod_valid valid_msg
2784 if {$mod_valid ne {true}} {
2785 error $valid_msg {} MODULES_ERR_VALIDITY
2786 }
2787 return $mod_content
2788 }
2789
2790 # If given module maps to default or other symbolic versions, a list of
2791 # those versions is returned. This takes module/version as an argument.
2792 proc getVersAliasList {mod} {
2793 set sym_list {}
2794 if {[info exists ::g_symbolHash($mod)]} {
2795 set sym_list $::g_symbolHash($mod)
2796 # withdraw hidden symbol from list
2797 if {[info exists ::g_hiddenSymHash($mod)]} {
2798 lassign [getDiffBetweenList $sym_list $::g_hiddenSymHash($mod)]\
2799 sym_list
2800 }
2801 }
2802
2803 reportDebug "'$mod' has symbolic version list '$sym_list'"
2804 return $sym_list
2805 }
2806
2807 proc doesModuleHaveSym {mod} {
2808 # is there any non-hidden symbol for mod
2809 return [expr {[info exists ::g_symbolHash($mod)] && (![info exists\
2810 ::g_hiddenSymHash($mod)] || [llength [lindex [getDiffBetweenList\
2811 $::g_symbolHash($mod) $::g_hiddenSymHash($mod)] 0]])}]
2812 }
2813
2814 # get list of elements located in a directory passed as argument. a flag is
2815 # set after each element to know if it is considered hidden or not. a
2816 # fetch_dotversion argument controls whether .version file should be looked at
2817 # in directory .proc will be used as a fallback to command provided by
2818 # extension library
2819 proc __getFilesInDirectory {dir fetch_dotversion} {
2820 set dir_list [list]
2821
2822 set elt_list [glob -nocomplain -directory $dir *]
2823
2824 # Add each element in the current directory to the list
2825 foreach elt $elt_list {
2826 lappend dir_list $elt 0
2827 }
2828
2829 # search for hidden files
2830 foreach elt [glob -nocomplain -types hidden -directory $dir -tails *] {
2831 switch -- $elt {
2832 . - .. { }
2833 .modulerc - .version {
2834 if {($fetch_dotversion || $elt ne {.version}) && [file readable\
2835 $dir/$elt]} {
2836 lappend dir_list $dir/$elt 0
2837 }
2838 }
2839 default {
2840 lappend dir_list $dir/$elt 1
2841 }
2842 }
2843 }
2844
2845 return $dir_list
2846 }
2847
2848 # check if an existing findModules cache entry matches current search by
2849 # evaluating search ids. if an exact match cannot be found, look at saved
2850 # searches that contains current search (superset of looked elements), extra
2851 # elements will be filtered-out by GetModules
2852 proc findModulesInMemCache {searchid} {
2853 # exact same search is cached
2854 if {[info exists ::g_foundModulesMemCache($searchid)]} {
2855 set match_searchid $searchid
2856 set mod_list $::g_foundModulesMemCache($searchid)
2857 # look for a superset search
2858 } else {
2859 set match_searchid {}
2860 set mod_list {}
2861 foreach cacheid [array names ::g_foundModulesMemCache] {
2862 # cache id acts as pattern to check if it contains current search
2863 if {[string match $cacheid $searchid]} {
2864 set match_searchid $cacheid
2865 set mod_list $::g_foundModulesMemCache($cacheid)
2866 break
2867 }
2868 }
2869 }
2870
2871 return [list $match_searchid $mod_list]
2872 }
2873
2874 # Walk through provided list of directories and files to find modules
2875 proc findModulesFromDirsAndFiles {dir full_list depthlvl fetch_mtime\
2876 res_arrname {indir_arrname {}} {hidden_listname {}} {fknown_arrname {}}\
2877 {dknown_arrname {}}} {
2878 # link to variables/arrays from upper context
2879 upvar $res_arrname mod_list
2880 if {$indir_arrname ne {}} {
2881 upvar $indir_arrname modfile_indir
2882 }
2883 if {$hidden_listname ne {}} {
2884 upvar $hidden_listname hidden_list
2885 }
2886 if {$fknown_arrname ne {}} {
2887 upvar $fknown_arrname fknown_arr
2888 }
2889 if {$dknown_arrname ne {}} {
2890 upvar $dknown_arrname dknown_arr
2891 }
2892
2893 foreach igndir [getConf ignored_dirs] {
2894 set ignored_dirs($igndir) 1
2895 }
2896 array set mod_list {}
2897 for {set i 0} {$i < [llength $full_list]} {incr i 1} {
2898 set element [lindex $full_list $i]
2899 set tail [file tail $element]
2900 set modulename [getModuleNameFromModulepath $element $dir]
2901 set parentname [file dirname $modulename]
2902 set moddepthlvl [llength [file split $modulename]]
2903 # check if element is a directory if we do not already know if it is a
2904 # dir or a file from transmitted structures
2905 if {[info exists dknown_arr($modulename)] || (![info exists\
2906 fknown_arr($modulename)] && [file isdirectory $element])} {
2907 if {![info exists ignored_dirs($tail)]} {
2908 if {[catch {
2909 set elt_list [getFilesInDirectory $element 1]
2910 } errMsg]} {
2911 set mod_list($modulename) [list accesserr [parseAccessIssue\
2912 $element] $element]
2913 } else {
2914 # Add each element in the current directory to the list
2915 foreach {fpelt hid} $elt_list {
2916 lappend full_list $fpelt
2917 # Flag hidden files
2918 if {$hid} {
2919 set hidden_list($fpelt) 1
2920 }
2921 }
2922 }
2923 }
2924 } else {
2925 switch -glob -- $tail {
2926 .modulerc {
2927 set mod_list($modulename) [list modulerc]
2928 }
2929 .version {
2930 # skip .version file from different depth level than search
2931 # targets if no in depth mode is enabled
2932 if {$depthlvl == 0 || $moddepthlvl == $depthlvl} {
2933 set mod_list($modulename) [list modulerc]
2934 }
2935 }
2936 .modulecache - *~ - *,v - \#*\# { }
2937 default {
2938 # skip modfile in no in depth mode search if it does not relate
2939 # to targeted depth level and one valid modfile has already be
2940 # found for the dirs lying at other depth level
2941 if {$depthlvl == 0 || $moddepthlvl == $depthlvl || ![info\
2942 exists modfile_indir($parentname)]} {
2943 lassign [checkValidModule $element] check_valid check_msg
2944 switch -- $check_valid {
2945 true {
2946 set mtime [expr {$fetch_mtime ? [getFileMtime\
2947 $element] : {}}]
2948 set mod_list($modulename) [list modulefile $mtime\
2949 $element]
2950
2951 # a valid modfile has been found in directory
2952 if {![info exists hidden_list($element)]} {
2953 set modfile_indir($parentname) 1
2954 }
2955 }
2956 default {
2957 # register check error and relative message to get it
2958 # in case of direct access of this module element, but
2959 # no registering in parent directory structure as
2960 # element is not valid
2961 set mod_list($modulename) [list $check_valid\
2962 $check_msg $element]
2963 }
2964 }
2965 }
2966 }
2967 }
2968 }
2969 }
2970 }
2971
2972 # finds all module-related files matching mod in the module path dir
2973 proc findModules {dir mod depthlvl fetch_mtime} {
2974 reportDebug "finding '$mod' in $dir (depthlvl=$depthlvl,\
2975 fetch_mtime=$fetch_mtime)"
2976
2977 # generated search id (for cache search/save) by compacting given args
2978 set searchid $dir:$mod:$depthlvl:$fetch_mtime
2979
2980 # look at memory cache for a compatible result
2981 lassign [findModulesInMemCache $searchid] cache_searchid cache_list
2982 if {$cache_searchid ne {}} {
2983 reportDebug "use cache entry '$cache_searchid'"
2984 return $cache_list
2985 }
2986
2987 # look at modulepath cache file
2988 lassign [findModulesInCacheFile $dir $mod $depthlvl $fetch_mtime] cache_ok\
2989 cache_list
2990 if {$cache_ok} {
2991 # record cache file findinds in memory cache
2992 reportDebug "create cache entry '$searchid'"
2993 set ::g_foundModulesMemCache($searchid) $cache_list
2994 return $cache_list
2995 }
2996
2997 defineModEqStaticProc [isIcase] [getConf extended_default] $mod
2998
2999 # every entries are requested
3000 set findall [expr {$mod eq {} || $mod eq {*}}]
3001
3002 # use catch protection to handle non-readable and non-existent dir
3003 if {[catch {
3004 set full_list {}
3005 foreach {fpelt hid} [getFilesInDirectory $dir 0] {
3006 set elt [file tail $fpelt]
3007 # include any .modulerc file found at the modulepath root
3008 if {$elt eq {.modulerc} || $findall || [modEqStatic $elt match]} {
3009 lappend full_list $fpelt
3010 }
3011 }
3012 }]} {
3013 return {}
3014 }
3015
3016 # walk through list of dirs and files to find modules
3017 findModulesFromDirsAndFiles $dir $full_list $depthlvl $fetch_mtime mod_list
3018
3019 reportDebug "found [array names mod_list]"
3020
3021 # cache search results
3022 reportDebug "create cache entry '$searchid'"
3023 set found_list [array get mod_list]
3024 set ::g_foundModulesMemCache($searchid) $found_list
3025
3026 return $found_list
3027 }
3028
3029 proc getModules {dir {mod {}} {fetch_mtime 0} {search {}} {filter {}}} {
3030 global g_sourceAlias g_sourceVersion g_sourceVirtual g_rcAlias\
3031 g_moduleAlias g_rcVersion g_moduleVersion g_rcVirtual g_moduleVirtual\
3032 g_rcfilesSourced
3033
3034 reportDebug "get '$mod' in $dir (fetch_mtime=$fetch_mtime, search=$search,\
3035 filter=$filter)"
3036
3037 # generated search id (for cache search/save) by compacting given args
3038 set searchid $dir:$mod:$fetch_mtime:$search:$filter
3039
3040 # look at memory cache for a compatible result
3041 if {[info exists ::g_gotModulesMemCache($searchid)]} {
3042 reportDebug "use cache entry '$searchid'"
3043 return $::g_gotModulesMemCache($searchid)
3044 }
3045
3046 # extract one module name from query
3047 set modqe [getOneModuleFromVersSpec $mod]
3048
3049 # perform an in depth search or not
3050 set indepth [expr {{noindepth} ni $search}]
3051
3052 # set a default if none defined on directory entries
3053 set implicitdfl [getConf implicit_default]
3054
3055 # automatically define latest and default sym for all modules
3056 # disable when implicit default or advanced version spec are disabled or
3057 # if search query does not contain a module name and version but variant
3058 set autosymbol [expr {$implicitdfl && [getConf advanced_version_spec] &&\
3059 ![isSpecWildWithVariant $mod]}]
3060
3061 # match passed name against any part of avail module names
3062 set contains [expr {{contains} in $search}]
3063 set mtest [expr {$contains ? {matchin} : {match}}]
3064
3065 set icase [isIcase]
3066
3067 set wild [expr {{wild} in $search}]
3068
3069 # will only keep default or latest elts in the end or remove plain dirs
3070 set filtering [expr {$filter eq {noplaindir}}]
3071 set keeping [expr {!$filtering && $filter ne {}}]
3072
3073 # check search query string corresponds to directory
3074 set querydir [string trimright $modqe *]
3075 set isquerydir [expr {[string index $querydir end] eq {/}}]
3076 set querydir [string trimright $querydir /]
3077 set querydepth [countChar $modqe /]
3078
3079 # get directory relative to module name
3080 set moddir [getModuleNameFromVersSpec $mod]
3081 set hasmoddir [expr {$moddir ne {.}}]
3082 set modroot [getModuleRootFromVersSpec $mod]
3083
3084 # get all in case of contains search or if provided-aliases are included
3085 set find_all [expr {$contains || [isEltInReport provided-alias 0]}]
3086
3087 set earlyfilter [expr {!$find_all && !$wild && $modroot eq [string\
3088 map {* {} ? {}} $modroot]}]
3089
3090 # are result entries gathered in a resolution context ?
3091 set resctx [expr {{resolve} in $search}]
3092
3093 # need to perform an extra match search?
3094 set ems_required [isExtraMatchSearchRequired $mod]
3095
3096 # if search for global or user rc alias only, no dir lookup is performed
3097 # and aliases from g_rcAlias are returned
3098 if {{rc_alias_only} in $search} {
3099 set add_rc_defs 1
3100 array set found_list {}
3101 } else {
3102 # find modules by searching mod root name in order to catch all module
3103 # related entries to correctly computed auto symbols afterward
3104
3105 if {$find_all} {
3106 set findmod *
3107 } else {
3108 set findmod $modroot
3109 # if searched mod is an empty or flat element append wildcard
3110 # character to match anything starting with mod
3111 if {$wild && !$hasmoddir && [string index $findmod end] ne {*}} {
3112 append findmod *
3113 }
3114 }
3115
3116 # add alias/version definitions from global or user rc to result
3117 set add_rc_defs [expr {{rc_defs_included} in $search}]
3118
3119 # if no indepth mode search, pass the depth level of the search query
3120 # unless EMS need to be performed (findModules should fetch everything)
3121 set depthlvl [expr {$indepth || $ems_required ? 0 : $querydepth + 1}]
3122
3123 array set found_list [findModules $dir $findmod $depthlvl $fetch_mtime]
3124 }
3125
3126 # Phase #1: consolidate every kind of entries (directory, modulefile,
3127 # symbolic version, alias and virtual module) in found_list
3128
3129 array set err_list {}
3130 array set versmod_list {}
3131
3132 foreach elt [lsort [array names found_list]] {
3133 switch -- [lindex $found_list($elt) 0] {
3134 modulerc {
3135 # process rc files them remove them from found_list
3136 if {![info exists g_rcfilesSourced($dir/$elt)]} {
3137 execute-modulerc $dir/$elt $elt $elt
3138 # Keep track of already sourced rc files not to run them again
3139 set g_rcfilesSourced($dir/$elt) 1
3140 }
3141 unset found_list($elt)
3142 }
3143 modulefile {
3144 }
3145 default {
3146 # flag entries with error
3147 set err_list($elt) 1
3148 }
3149 }
3150 }
3151
3152 # add all versions found when parsing .version or .modulerc files in this
3153 # directory or in global or user rc definitions
3154 foreach vers [array names g_moduleVersion] {
3155 set versmod $g_moduleVersion($vers)
3156 if {($dir ne {} && [string first $dir/ $g_sourceVersion($vers)] == 0)\
3157 || [info exists g_rcVersion($vers)]} {
3158 set found_list($vers) [list version $versmod]
3159
3160 # build module symbol list
3161 lappend versmod_list($versmod) $vers
3162 # add global/user rc def to module symbol list in any cases
3163 } elseif {!$add_rc_defs && [info exists g_rcVersion($vers)]} {
3164 lappend versmod_list($versmod) $vers
3165 }
3166 }
3167
3168 # add aliases found when parsing .version or .modulerc files in this
3169 # directory (skip aliases not registered from this directory except if
3170 # global or user rc definitions should be included)
3171 foreach alias [array names g_moduleAlias] {
3172 if {($dir ne {} && [string first $dir/ $g_sourceAlias($alias)] == 0)\
3173 || ($add_rc_defs && [info exists g_rcAlias($alias)])} {
3174 ##nagelfar ignore Found constant
3175 set found_list($alias) [list alias $g_moduleAlias($alias)]
3176 }
3177 }
3178
3179 # add virtual mods found when parsing .version or .modulerc files in this
3180 # directory (skip virtual mods not registered from this directory except if
3181 # global or user rc definitions should be included)
3182 foreach virt [array names g_moduleVirtual] {
3183 if {($dir ne {} && [string first $dir/ $g_sourceVirtual($virt)] == 0)\
3184 || ($add_rc_defs && [info exists g_rcVirtual($virt)])} {
3185 lassign [checkValidModule $g_moduleVirtual($virt)] check_valid\
3186 check_msg
3187 switch -- $check_valid {
3188 true {
3189 set mtime [expr {$fetch_mtime ? [getFileMtime\
3190 $g_moduleVirtual($virt)] : {}}]
3191 # set mtime at index 1 like a modulefile entry
3192 set found_list($virt) [list virtual $mtime\
3193 $g_moduleVirtual($virt)]
3194 }
3195 default {
3196 # register check error and relative message to get it in
3197 # case of direct access of this module element
3198 set found_list($virt) [list $check_valid $check_msg\
3199 $g_moduleVirtual($virt)]
3200 set err_list($virt) 1
3201 }
3202 }
3203 }
3204 }
3205
3206 # Phase #2: early filtering of non-matching elements
3207
3208 defineModEqProc $icase [getConf extended_default]
3209
3210 if {$earlyfilter} {
3211 foreach elt [array names found_list] {
3212 if {![modEq $modroot $elt eqstart]} {
3213 unset found_list($elt)
3214 }
3215 }
3216 }
3217
3218 # Phase #3: scan modulefiles if extra match search is needed
3219
3220 if {$ems_required} {
3221 scanExtraMatchSearch $dir $mod found_list
3222 if {[isEltInReport provided-alias 0]} {
3223 insertProvidedAliases $dir found_list
3224 }
3225 }
3226
3227 # Phase #4: filter-out dynamically hidden or expired elements
3228
3229 # define module name and version comparison procs
3230 defineModStartNbProc $icase
3231 defineModEqStaticProc $icase [getConf extended_default] $mod
3232
3233 # remove hidden elements unless they are (or their symbols) targeted by
3234 # search query.
3235 foreach elt [array names found_list] {
3236 if {[lassign [isModuleHidden $elt $mod 1 [lindex $found_list($elt) 2]]\
3237 hidlvl hidmatch hidbydef]} {
3238 # is there a symbol that matches query (bare module name query
3239 # matches default symbol on resolve context or if onlydefaults filter
3240 # is applied)
3241 if {!$hidmatch && [info exists versmod_list($elt)]} {
3242 foreach eltsym $versmod_list($elt) {
3243 if {[modEqStatic $eltsym] || (($resctx || $filter eq\
3244 {onlydefaults}) && "$mod/default" eq $eltsym)} {
3245 set hidmatch 1
3246 break
3247 }
3248 }
3249 }
3250 # consider 'default' symbols are explicitly specified if
3251 # onlydefaults filter applied or resolving bare module name
3252 if {!$hidmatch && [lindex $found_list($elt) 0] eq {version} && [file\
3253 tail $elt] eq {default} && ($filter eq {onlydefaults} || ($resctx\
3254 && "$mod/default" eq $elt))} {
3255 set hidmatch 1
3256 }
3257
3258 # not hidden if matched unless if hard hiding apply
3259 if {!$hidmatch || $hidlvl > 1} {
3260 # record hidden symbol, not to display it in listModules
3261 if {[lindex $found_list($elt) 0] eq {version}} {
3262 lappend ::g_hiddenSymHash([lindex $found_list($elt) 1]) [file\
3263 tail $elt]
3264 }
3265 # transform forbidden module in error entry if it specifically
3266 # matches search query
3267 if {$hidlvl == 2 && $hidmatch && [isModuleTagged $elt forbidden 0\
3268 $dir/$elt]} {
3269 set found_list($elt) [list accesserr [getForbiddenMsg $elt\
3270 $dir/$elt]]
3271 set err_list($elt) 1
3272 } else {
3273 unset found_list($elt)
3274 }
3275 }
3276 }
3277 # apply hidden tag if an hidden definition apply to module
3278 if {$hidbydef} {
3279 setModuleTag $elt hidden
3280 }
3281 }
3282
3283 # Phase #5: elaborate directory content with default element selection
3284
3285 array set dir_list {}
3286 array set autosym_list {}
3287
3288 # build list of elements contained in each directory
3289 foreach elt [array names found_list] {
3290 # add a ref to element in its parent directory unless element has error
3291 # or is a symbolic version then recursively add parent element until
3292 # reaching top directory
3293 if {![info exists err_list($elt)] && [lindex $found_list($elt) 0] ne\
3294 {version}} {
3295 set direlt $elt
3296 while {[set pardir [file dirname $direlt]] ne {.}} {
3297 appendNoDupToList dir_list($pardir) [file tail $direlt]
3298 set direlt $pardir
3299 }
3300 }
3301 }
3302
3303 # determine default element for each directory and record sorted elt list
3304 # unless if an alias or a virtual module has overwritten directory entry
3305 # but override alias entry if extra match search is required (as it
3306 # withdrawn module aliases)
3307 foreach elt [array names dir_list] {
3308 if {![info exists found_list($elt)] || ($ems_required && [lindex\
3309 $found_list($elt) 0] eq {alias})} {
3310 set dir_list($elt) [lsort -dictionary $dir_list($elt)]
3311 # get default element: explicitly defined default (whether it exists
3312 # or is in error) or implicit default if enabled
3313 if {[info exists found_list($elt/default)] && [lindex\
3314 $found_list($elt/default) 0] eq {version}} {
3315 set dfl [file tail [lindex $found_list($elt/default) 1]]
3316 } elseif {$implicitdfl} {
3317 set dfl [lindex $dir_list($elt) end]
3318 } else {
3319 set dfl {}
3320 }
3321 # record directory properties
3322 set found_list($elt) [list directory $dfl {*}$dir_list($elt)]
3323
3324 # automatically define symbols for all modules matching query if
3325 # these names do not exist yet or if in error, in which case only
3326 # auto symbol resolution is set
3327 if {$autosymbol && (!$hasmoddir || [modEq $modroot $elt eqstart])} {
3328 if {![info exists found_list($elt/default)] || [info exists\
3329 err_list($elt/default)]} {
3330 if {![info exists found_list($elt/default)]} {
3331 set found_list($elt/default) [list version $elt/$dfl]
3332 lappend versmod_list($elt/$dfl) $elt/default
3333 set autosym_list($elt/default) 1
3334 }
3335 setModuleResolution $elt/default $elt/$dfl default 1 1
3336 }
3337 if {![info exists found_list($elt/latest)] || [info exists\
3338 err_list($elt/latest)]} {
3339 set lat [lindex $dir_list($elt) end]
3340 if {![info exists found_list($elt/latest)]} {
3341 set found_list($elt/latest) [list version $elt/$lat]
3342 lappend versmod_list($elt/$lat) $elt/latest
3343 set autosym_list($elt/latest) 1
3344 }
3345 setModuleResolution $elt/latest $elt/$lat latest 1 1
3346 }
3347 }
3348 }
3349 }
3350
3351 # Phase #6: perform extra match search filtering
3352
3353 if {$ems_required} {
3354 filterExtraMatchSearch $dir $mod found_list versmod_list
3355 }
3356
3357 # Phase #7: filter results to keep those matching search query
3358
3359 # define module name and version comparison procs
3360 defineDoesModMatchAtDepthProc $contains $querydepth $mtest
3361
3362 # element to include in output
3363 set report_indesym [isEltInReport indesym 0]
3364 set report_dirwsym [expr {!$report_indesym && [isEltInReport dirwsym]}]
3365
3366 array set mod_list {}
3367 array set fdir_list {}
3368 array set keep_list {}
3369
3370 # keep element matching query, add directory of element matching query,
3371 # also add directory to result if query name finishes with trailing slash;
3372 # only keep auto syms if fully matched or version not specified in query;
3373 # (hidden elements have been filtered on phase 2)
3374 foreach elt [array names found_list] {
3375 set elt_type [lindex $found_list($elt) 0]
3376 if {(($wild && [doesModMatchAtDepth $elt]) || (!$wild && ([modEqStatic\
3377 $elt match /*] || [modEqStatic $elt match] || ($hasmoddir &&\
3378 $elt_type eq {directory} && [modEq $moddir $elt]))) || ($isquerydir\
3379 && $elt_type eq {directory} && [modEq $querydir $elt match 0])) &&\
3380 (![info exists autosym_list($elt)] || ([countChar $elt /]\
3381 != $querydepth && !$contains) || [modEqStatic $elt]) && ![info\
3382 exists mod_list($elt)]} {
3383 if {$elt_type eq {directory}} {
3384 # add matching directory to the result list, its entries will be
3385 # computed in a second time and directory will be dropped if it
3386 # has no entry in the end
3387 set mod_list($elt) [list directory]
3388 # add dir to the filter dir list to enable its removal in next
3389 # step if dir is empty
3390 if {![info exists fdir_list($elt)]} {
3391 set fdir_list($elt) {}
3392 }
3393 } else {
3394 set mod_list($elt) $found_list($elt)
3395 }
3396
3397 # version may matches query but not its target, so it should be in
3398 # this case manually added to result (if it exists and unless if
3399 # versions should be reported independently)
3400 if {$elt_type eq {version} && !$report_indesym} {
3401 # resolve eventual icase target
3402 set versmod [getArrayKey found_list [lindex $mod_list($elt) 1]\
3403 $icase]
3404 # add target to dir struct (not version) if not already recorded
3405 set direlt .
3406
3407 # recursively add targets to result (process directory content if
3408 # target is a directory
3409 set tgt_list [list $versmod]
3410 for {set i 0} {$i < [llength $tgt_list]} {incr i} {
3411 set tgt [lindex $tgt_list $i]
3412
3413 if {![info exists mod_list($tgt)]} {
3414 if {[info exists found_list($tgt)]} {
3415 set mod_list($tgt) $found_list($tgt)
3416 # version target is directory: recursively add content
3417 if {[lindex $mod_list($tgt) 0] eq {directory}} {
3418 foreach tgtelt $dir_list($tgt) {
3419 lappend tgt_list $tgt/$tgtelt
3420 }
3421 # add dir to the filter dir list to enable its removal
3422 # in next step if dir is empty
3423 if {![info exists fdir_list($tgt)]} {
3424 set fdir_list($tgt) {}
3425 }
3426 }
3427 }
3428
3429 # record target in dir struct if part of found elts or if
3430 # hidden but should not be in error
3431 set pardir [file dirname $tgt]
3432 # do not test if full path module is hidden as full path
3433 # designation cannot be computed (target not in found_list)
3434 if {([info exists found_list($tgt)] || ($pardir ne {.} &&\
3435 ![info exists found_list($pardir)] && [isModuleHidden\
3436 $tgt $mod])) && ![info exists err_list($tgt)]} {
3437 # create parent directory if it does not exist
3438 if {$pardir ne {.} && ![info exists\
3439 found_list($pardir)]} {
3440 set found_list($pardir) [list directory]
3441 set mod_list($pardir) [list directory]
3442 }
3443
3444 if {$i == 0} {
3445 set direlt $tgt
3446 } else {
3447 lappend fdir_list([file dirname $tgt]) [file tail\
3448 $tgt]
3449 }
3450 }
3451 }
3452 }
3453 # skip adding element to directory content if in error
3454 } elseif {[info exists err_list($elt)]} {
3455 set direlt .
3456 } else {
3457 set direlt $elt
3458 }
3459
3460 # track directory content, as directory are also reported to their
3461 # parent directory the directory structure is also tracked
3462 if {[set pardir [file dirname $direlt]] ne {.}} {
3463 lappend fdir_list($pardir) [file tail $direlt]
3464 # track top level entries that will be kept if result is filtered
3465 } elseif {$keeping && $direlt ne {.} && $elt_type ne {directory}} {
3466 set keep_list($elt) 1
3467 }
3468 }
3469 }
3470
3471 # determine default element for each directory and record sorted element
3472 # list unless directory entry has been overwritten by a different module
3473 # kind or unless only matching directory should be part of result
3474 foreach elt [lsort -decreasing [array names fdir_list]] {
3475 if {[lindex $found_list($elt) 0] eq {directory} && ([info exists\
3476 mod_list($elt)] || $keeping)} {
3477 set fdir_list($elt) [lsort -dictionary $fdir_list($elt)]
3478 # get default element: explicitly defined default if included in
3479 # result or not found or implicit default if enabled
3480 if {[info exists found_list($elt/default)] && [lindex\
3481 $found_list($elt/default) 0] eq {version} && ([info exists\
3482 mod_list([set versmod [lindex $found_list($elt/default) 1]])] ||\
3483 ![info exists found_list($versmod)])} {
3484 set dfl [file tail $versmod]
3485 } elseif {$implicitdfl} {
3486 set dfl [lindex $fdir_list($elt) end]
3487 } else {
3488 set dfl {}
3489 }
3490 # remove empty dirs
3491 if {![llength $fdir_list($elt)]} {
3492 unset mod_list($elt)
3493 unset fdir_list($elt)
3494 # remove unset dir reference in parent directory. parent dir
3495 # will be treated after unset dir (due to decreasing sort) if it
3496 # needs to get in turn unset
3497 if {[set pardir [file dirname $elt]] ne {.}} {
3498 set fdir_list($pardir) [replaceFromList $fdir_list($pardir)\
3499 [file tail $elt]]
3500 }
3501 } else {
3502 # record directory properties
3503 set mod_list($elt) [list directory $dfl {*}$fdir_list($elt)]
3504
3505 # list elements to keep for filtering step
3506 if {$keeping} {
3507 if {$filter eq {onlylatest}} {
3508 set keepelt $elt/[lindex $fdir_list($elt) end]
3509 } elseif {$dfl ne {}} {
3510 set keepelt $elt/$dfl
3511 } else {
3512 set keepelt {}
3513 }
3514
3515 # keep directory if its element depth is deeper than query
3516 if {!$indepth && [countChar $keepelt /] > $querydepth} {
3517 set keep_list($elt) 1
3518 # otherwise only keep existing modules (not directories)
3519 } elseif {[info exists mod_list($keepelt)] && [lindex\
3520 $mod_list($keepelt) 0] ne {directory}} {
3521 set keep_list($keepelt) 1
3522 }
3523 # when noplaindir filtering, only keep dirs with syms when indepth
3524 # enabled or if corresponds to query depth when indepth disabled
3525 } elseif {$filtering && $filter eq {noplaindir} &&\
3526 (($indepth && (!$report_dirwsym || ![doesModuleHaveSym $elt]))\
3527 || (!$indepth && [countChar $elt /] != $querydepth))} {
3528 unset mod_list($elt)
3529 }
3530 }
3531 }
3532 }
3533
3534 # now all matching modulefiles are settled, only keep those found at search
3535 # query depth level if 'noindepth' mode asked
3536 if {!$indepth} {
3537 # remove entries with more filename path separator than query pattern
3538 foreach elt [array names mod_list] {
3539 if {[countChar $elt /] > $querydepth} {
3540 unset mod_list($elt)
3541 }
3542 }
3543 }
3544
3545 # if result should be filtered, only keep marked elements
3546 if {$keeping} {
3547 foreach elt [array names mod_list] {
3548 if {![info exists keep_list($elt)]} {
3549 unset mod_list($elt)
3550 }
3551 }
3552 }
3553
3554 # Phase #6: consolidate tags set for retained modules
3555
3556 # skip collecting tags if already performed for this modulepath or command
3557 # is different than avail/spider
3558 if {[currentState commandname] in {avail spider} && $dir ni [getState\
3559 tags_collected_in]} {
3560 # load tags from loaded modules in the global structure prior collecting
3561 # tags found during this getModules evaluation
3562 cacheCurrentModules 0
3563
3564 foreach elt [array names mod_list] {
3565 switch -- [lindex $mod_list($elt) 0] {
3566 alias - modulefile - virtual {
3567 # gather all tags applying to elt
3568 collectModuleTags $elt
3569 }
3570 }
3571 }
3572 }
3573
3574 reportTrace "{[array names mod_list]} matching '$mod' in '$dir'" {Get\
3575 modules}
3576
3577 # cache search results
3578 reportDebug "create cache entry '$searchid'"
3579 set got_list [array get mod_list]
3580 set ::g_gotModulesMemCache($searchid) $got_list
3581
3582 return $got_list
3583 }
3584
3585 proc getMatchingAnyModules {dir pattern_list fetch_mtime search filter} {
3586 set mod_list {}
3587 foreach pattern $pattern_list {
3588 lappend mod_list {*}[getModules $dir $pattern $fetch_mtime $search\
3589 $filter]
3590 }
3591 return $mod_list
3592 }
3593
3594 # ;;; Local Variables: ***
3595 # ;;; mode:tcl ***
3596 # ;;; End: ***
3597 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent: