Debian Codegraph @

tcl/modscan.tcl

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