Debian Codegraph @

tcl/modscan.tcl

raw
1 ##########################################################################
2
3 # MODSCAN.TCL, modulefile scan and extra match search procedures
4 # Copyright (C) 2022-2025 Xavier Delaruelle
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 ##########################################################################
20
21 # optimized variant command for scan mode: init entry in ModuleVariant array
22 # to avoid variable being undefined when accessed during modulefile evaluation
23 # record variant definition in structure for extra match search or report
24 proc variant-sc {itrp args} {
25 # parse args
26 lassign [parseVariantCommandArgs {*}$args] name values defdflvalue\
27 dflvalue isboolean
28
29 # remove duplicate possible values for boolean variant
30 if {$isboolean} {
31 set values {on off}
32 if {$defdflvalue} {
33 set dflvalue [expr {$dflvalue ? {on} : {off}}]
34 }
35 }
36
37 recordScanModuleElt $name variant
38
39 lappend ::g_scanModuleVariant([currentState modulename]) [list $name\
40 $values $defdflvalue $dflvalue $isboolean]
41
42 # instantiate variant in modulefile context to an empty value
43 reportDebug "Set variant on $itrp: ModuleVariant($name) = ''"
44 $itrp eval set "{::ModuleVariant($name)}" "{}"
45 }
46
47 proc setenv-sc {args} {
48 lassign [parseSetenvCommandArgs load set {*}$args] bhv var val
49
50 recordScanModuleElt $var setenv envvar
51
52 setEnvVarIfUndefined $var {}
53 return {}
54 }
55
56 proc edit-path-sc {cmd args} {
57 lassign [parsePathCommandArgs $cmd load noop {*}$args] separator allow_dup\
58 idx_val ign_refcount val_set_is_delim glob_match bhv var path_list
59
60 recordScanModuleElt $var $cmd envvar
61
62 # record MODULEPATH edition as "module use" command
63 if {$var eq {MODULEPATH} && $cmd in {append-path prepend-path}} {
64 recordScanModuleUsePathList $path_list
65 }
66
67 setEnvVarIfUndefined $var {}
68 return {}
69 }
70
71 proc pushenv-sc {var val} {
72 recordScanModuleElt $var pushenv envvar
73
74 setEnvVarIfUndefined $var {}
75 return {}
76 }
77
78 proc unsetenv-sc {args} {
79 lassign [parseUnsetenvCommandArgs load noop {*}$args] bhv var val
80
81 recordScanModuleElt $var unsetenv envvar
82
83 setEnvVarIfUndefined $var {}
84 return {}
85 }
86
87 proc complete-sc {shell name body} {
88 if {![string length $name]} {
89 knerror "Invalid command name '$name'"
90 }
91
92 recordScanModuleElt $name complete
93 }
94
95 proc uncomplete-sc {name} {
96 if {![string length $name]} {
97 knerror "Invalid command name '$name'"
98 }
99
100 recordScanModuleElt $name uncomplete
101 }
102
103 proc set-alias-sc {alias what} {
104 recordScanModuleElt $alias set-alias
105 }
106
107 proc unset-alias-sc {alias} {
108 recordScanModuleElt $alias unset-alias
109 }
110
111 proc set-function-sc {function what} {
112 recordScanModuleElt $function set-function
113 }
114
115 proc unset-function-sc {function} {
116 recordScanModuleElt $function unset-function
117 }
118
119 proc chdir-sc {dir} {
120 recordScanModuleElt $dir chdir
121 }
122
123 proc family-sc {name} {
124 if {![string length $name] || ![regexp {^[A-Za-z0-9_]*$} $name]} {
125 knerror "Invalid family name '$name'"
126 }
127 recordScanModuleElt $name family provided-alias
128 }
129
130 proc provide-sc {args} {
131 if {![llength $args]} {
132 knerror {No module specified in argument}
133 }
134 foreach alias $args {
135 recordScanModuleElt $alias provide provided-alias
136 }
137 }
138
139 proc prereq-sc {args} {
140 lassign [parsePrereqCommandArgs prereq {*}$args] tag_list modulepath_list\
141 optional opt_list args
142
143 foreach modspec [parseModuleSpecification 0 0 0 0 {*}$args] {
144 recordScanModuleElt $modspec prereq prereq-any depends-on-any require
145 }
146 }
147
148 proc prereq-all-sc {args} {
149 lassign [parsePrereqCommandArgs prereq-all {*}$args] tag_list\
150 modulepath_list optional opt_list args
151
152 foreach modspec [parseModuleSpecification 0 0 0 0 {*}$args] {
153 recordScanModuleElt $modspec prereq-all depends-on require
154 }
155 }
156
157 proc always-load-sc {args} {
158 lassign [parsePrereqCommandArgs always-load {*}$args] tag_list\
159 modulepath_list optional opt_list args
160
161 foreach modspec [parseModuleSpecification 0 0 0 0 {*}$args] {
162 recordScanModuleElt $modspec always-load require
163 }
164 }
165
166 proc conflict-sc {args} {
167 foreach modspec [parseModuleSpecification 0 0 0 0 {*}$args] {
168 recordScanModuleElt $modspec conflict incompat
169 }
170 }
171
172 proc module-sc {command args} {
173 lassign [parseModuleCommandName $command help] command cmdvalid cmdempty
174 # ignore sub-commands that do not either load or unload
175 if {$command in {load load-any switch try-load unload use}} {
176 # parse options to distinguish them from module version spec
177 lassign [parseModuleCommandArgs 0 $command 0 {*}$args] show_oneperline\
178 show_mtime show_filter search_filter search_match dump_state\
179 addpath_pos not_req tag_list args
180 set modspeclist [parseModuleSpecification 0 0 0 0 {*}$args]
181
182 # no require/incompat extra specifier alias if --not-req option is set
183 if {$not_req} {
184 set xtaliasinc {}
185 set xtaliasreq {}
186 } else {
187 set xtaliasinc [list incompat]
188 set xtaliasreq [list require]
189 }
190
191 if {$command eq {switch}} {
192 # distinguish switched-off module spec from switched-on
193 # ignore command without or with too much argument
194 switch -- [llength $modspeclist] {
195 {1} {
196 # no switched-off module with one-arg form
197 recordScanModuleElt $modspeclist switch switch-on\
198 {*}$xtaliasreq
199 }
200 {2} {
201 lassign $modspeclist swoffarg swonarg
202 recordScanModuleElt $swoffarg switch switch-off {*}$xtaliasinc
203 recordScanModuleElt $swonarg switch switch-on {*}$xtaliasreq
204 }
205 }
206 } elseif {$command eq {use}} {
207 recordScanModuleUsePathList $modspeclist
208 } else {
209 set xtalias [expr {$command eq {unload} ? $xtaliasinc : $xtaliasreq}]
210 # record each module spec
211 foreach modspec $modspeclist {
212 recordScanModuleElt $modspec $command {*}$xtalias
213 }
214 }
215 }
216 }
217
218 proc recordScanModuleUsePathList {path_list} {
219 foreach path $path_list {
220 set resolved_abs_path [getAbsolutePath [resolvStringWithEnv $path]]
221 recordScanModuleElt $resolved_abs_path use
222 }
223 }
224
225 proc recordScanModuleElt {name args} {
226 set mod [currentState modulename]
227 set modpath [currentState modulepath]
228 if {![info exists ::g_scanModuleElt]} {
229 set ::g_scanModuleElt [dict create]
230 }
231 foreach elt $args {
232 if {![dict exists $::g_scanModuleElt $modpath $elt $name]} {
233 dict set ::g_scanModuleElt $modpath $elt $name [list $mod]
234 } else {
235 ##nagelfar ignore Suspicious variable name
236 dict with ::g_scanModuleElt $modpath $elt {lappend $name $mod}
237 }
238 reportDebug "Module $mod defines $elt:$name"
239 }
240 }
241
242 proc getScanModuleElt {modpath elt} {
243 if {[info exists ::g_scanModuleElt]} {
244 if {[dict exists $::g_scanModuleElt $modpath $elt]} {
245 return [dict get $::g_scanModuleElt $modpath $elt]
246 }
247 }
248 }
249
250 # test given variant specification matches what scanned module defines
251 proc doesModVariantMatch {mod pvrlist} {
252 set ret 1
253 if {[info exists ::g_scanModuleVariant($mod)]} {
254 foreach availvr $::g_scanModuleVariant($mod) {
255 set availvrarr([lindex $availvr 0]) [lindex $availvr 1]
256 set availvrisbool([lindex $availvr 0]) [lindex $availvr 4]
257 }
258 }
259 # no match if a specified variant is not found among module variants or
260 # if the value is not available
261 foreach pvr $pvrlist {
262 set pvrvallist [lassign $pvr vrname pvrnot pvrisbool]
263 # check at least one variant value from specification matches defined
264 # available variant values
265 set one_vrval_match 0
266 foreach pvrval $pvrvallist {
267 # if variant is a boolean, specified value should be a boolean too
268 # any value accepted for free-value variant
269 if {[info exists availvrarr($vrname)] && (($pvrisbool &&\
270 $availvrisbool($vrname)) || (!$availvrisbool($vrname) &&\
271 (![llength $availvrarr($vrname)] || $pvrval in\
272 $availvrarr($vrname))))} {
273 set one_vrval_match 1
274 break
275 }
276 }
277 # toggle result if negation set for this pattern
278 if {$pvrnot} {
279 set one_vrval_match [expr {!$one_vrval_match}]
280 }
281 if {!$one_vrval_match} {
282 set ret 0
283 break
284 }
285 }
286 return $ret
287 }
288
289 # test given tag specification matches tags defined over module
290 proc doesModTagMatch {mod modfile ptaglist} {
291 set ret 1
292 foreach ptag $ptaglist {
293 set namelist [lassign $ptag elt pnot]
294 # check if at least one tag name from specifier value is applied on mod
295 set one_name_match 0
296 foreach name $namelist {
297 if {[isModuleTagged $mod $name 1 $modfile]} {
298 set one_name_match 1
299 break
300 }
301 }
302 # toggle result if negation set for this pattern
303 if {$pnot} {
304 set one_name_match [expr {!$one_name_match}]
305 }
306 # no tag name from specifier match mod mean no match on extra query
307 if {!$one_name_match} {
308 set ret 0
309 break
310 }
311 }
312 return $ret
313 }
314
315 # collect list of modules matching all extra specifier criteria
316 proc getModMatchingExtraSpec {modpath pxtlist} {
317 set res [list]
318 if {[info exists ::g_scanModuleElt] && [dict exists $::g_scanModuleElt\
319 $modpath]} {
320 foreach pxt $pxtlist {
321 set namelist [lassign $pxt elt pnot]
322 set one_crit_res [list]
323 foreach name $namelist {
324 if {$elt in {require incompat load unload prereq conflict\
325 prereq-all prereq-any depends-on depends-on-any always-load\
326 load-any try-load switch switch-on switch-off}} {
327 if {[dict exists $::g_scanModuleElt $modpath $elt]} {
328 foreach {modspec values} [dict get $::g_scanModuleElt\
329 $modpath $elt] {
330 # modEq proc has been initialized in getModules phase #2
331 if {[modEq $modspec $name eqstart 1 5 1]} {
332 # possible duplicate module entry in result list
333 lappend one_crit_res {*}[dict get $::g_scanModuleElt\
334 $modpath $elt $modspec]
335 }
336 }
337 }
338 } else {
339 # get mods matching one value of one extra specifier criterion
340 if {[dict exists $::g_scanModuleElt $modpath $elt $name]} {
341 lappend one_crit_res {*}[dict get $::g_scanModuleElt\
342 $modpath $elt $name]
343 }
344 }
345 }
346 # result is all other modules if negation set for this pattern
347 if {$pnot} {
348 set modpath_mod_list [dict get $::g_scanModuleElt $modpath all\
349 modulename]
350 lassign [getDiffBetweenList $modpath_mod_list $one_crit_res]\
351 one_crit_res
352 }
353 lappend all_crit_res $one_crit_res
354 # no match on one criterion means no match globally, no need to test
355 # further criteria
356 if {![llength $one_crit_res]} {
357 break
358 }
359 }
360 # matching modules are those found in every criteria result
361 set res [getIntersectBetweenList {*}$all_crit_res]
362 }
363 return $res
364 }
365
366 # determine if current module search requires an extra match search
367 proc isExtraMatchSearchRequired {mod} {
368 # an extra match search is required if not currently inhibited and:
369 # * variant or provided-alias should be reported in output
370 # * mod specification contains variant during avail/paths/whatis
371 # * mod specification contains extra specifier during avail/paths/whatis
372 return [expr {![getState inhibit_ems 0] && ([isEltInReport variant 0] ||\
373 [isEltInReport provided-alias 0] || (([llength\
374 [getVariantListFromVersSpec $mod]] || [llength\
375 [getExtraListFromVersSpec $mod]]) && [currentState commandname] in\
376 {avail paths whatis spider}))}]
377 }
378
379 proc insertProvidedAliases {modpath res_arrname} {
380 upvar $res_arrname found_list
381 foreach {alias target_mod} [getScanModuleElt $modpath provided-alias] {
382 if {![info exists found_list($alias)]} {
383 ##nagelfar ignore Found constant
384 set found_list($alias) [list alias $target_mod]
385 }
386 }
387 }
388
389 # scan modulefiles from currently being built module search result if extra
390 # match search is needed
391 proc scanExtraMatchSearch {modpath mod res_arrname} {
392 upvar $res_arrname found_list
393
394 # disable error reporting to avoid modulefile errors (not coping with
395 # scan evaluation for instance) to pollute result
396 set alreadyinhibit [getState inhibit_errreport]
397 if {!$alreadyinhibit} {
398 inhibitErrorReport
399 }
400 # evaluate all modules found in scan mode to gather content information
401 lappendState mode scan
402
403 foreach elt [array names found_list] {
404 switch -- [lindex $found_list($elt) 0] {
405 modulefile - virtual {
406 # skip evaluation of fully forbidden modulefile
407 if {![isModuleTagged $elt forbidden 0 [lindex $found_list($elt)\
408 2]]} {
409 ##nagelfar ignore Suspicious variable name
410 execute-modulefile [lindex $found_list($elt) 2] $elt $elt $elt\
411 0 0 0 $modpath
412 }
413 }
414 }
415 }
416
417 lpopState mode
418 # re-enable error report only is it was disabled from this procedure
419 if {!$alreadyinhibit} {
420 setState inhibit_errreport 0
421 }
422 }
423
424 # perform extra match search on currently being built module search result
425 proc filterExtraMatchSearch {modpath mod res_arrname versmod_arrname} {
426 # link to variables/arrays from upper context
427 upvar $res_arrname found_list
428 upvar $versmod_arrname versmod_list
429
430 # get extra match query properties
431 set spec_vr_list [getVariantListFromVersSpec $mod]
432 set check_variant [llength $spec_vr_list]
433 lassign [getSplitExtraListFromVersSpec $mod] spec_tag_list spec_xt_list
434 set check_extra [llength $spec_xt_list]
435 set check_tag [llength $spec_tag_list]
436 set filter_res [expr {$check_variant || $check_extra || $check_tag}]
437
438 if {$check_tag} {
439 # load tags from loaded mods prior collecting tags found during rc eval
440 cacheCurrentModules 0
441 }
442
443 set unset_list {}
444 set keep_list {}
445 foreach elt [array names found_list] {
446 if {$check_tag} {
447 collectModuleTags $elt
448 }
449
450 # unset elements that do not match extra query
451 if {$filter_res} {
452 switch -- [lindex $found_list($elt) 0] {
453 alias {
454 # module alias does not hold properties to match extra query
455 lappend unset_list $elt
456 }
457 modulefile - virtual {
458 if {$check_variant && ![doesModVariantMatch $elt\
459 $spec_vr_list]} {
460 lappend unset_list $elt
461 } elseif {$check_tag && ![doesModTagMatch $elt [lindex\
462 $found_list($elt) 2] $spec_tag_list]} {
463 lappend unset_list $elt
464 } elseif {$check_extra} {
465 # know currently retained modules to later compute those
466 # to withdrawn
467 lappend keep_list $elt
468 }
469 }
470 }
471 }
472 }
473
474 if {$check_tag} {
475 # indicate tags have been collected for this modulepath
476 lappendState tags_collected_in $modpath
477 }
478
479 # get list of modules matching extra specifiers to determine those to not
480 # matching that need to be withdrawn from result
481 if {$check_extra} {
482 set extra_keep_list [getModMatchingExtraSpec $modpath $spec_xt_list]
483 lassign [getDiffBetweenList $keep_list $extra_keep_list]\
484 extra_unset_list
485 lappend unset_list {*}$extra_unset_list
486 }
487
488 # unset marked elements
489 foreach elt $unset_list {
490 unset found_list($elt)
491 # also unset any symbolic version pointing to unset elt
492 if {[info exists versmod_list($elt)]} {
493 set eltsym_list $versmod_list($elt)
494 for {set i 0} {$i < [llength $eltsym_list]} {incr i} {
495 set eltsym [lindex $eltsym_list $i]
496 # getModules phase #2 may have already withdrawn symbol
497 if {[info exists found_list($eltsym)]} {
498 unset found_list($eltsym)
499 }
500 # also unset symbolic version applying to dir name if removing
501 # default symbol
502 if {[file tail $eltsym] eq {default}} {
503 set eltdir [file dirname $eltsym]
504 if {[info exists versmod_list($eltdir)]} {
505 lappend eltsym_list {*}$versmod_list($eltdir)
506 }
507 }
508 }
509 }
510 }
511 }
512
513 # ;;; Local Variables: ***
514 # ;;; mode:tcl ***
515 # ;;; End: ***
516 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent: