Debian Codegraph @

tcl/modspec.tcl

raw
1 ##########################################################################
2
3 # MODSPEC.TCL, module specification procedures
4 # Copyright (C) 2016-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 # Define procedure to get how many parts between passed name and mod are equal
22 # Adapt procedure code whether icase is enabled or disabled
23 proc defineModStartNbProc {icase} {
24 set procname modStartNbProc
25 if {$icase} {
26 append procname Icase
27 }
28 # define proc if not done yet or if it was defined for another context
29 if {[info procs modStartNb] eq {} || $::g_modStartNb_proc ne $procname} {
30 if {[info exists ::g_modStartNb_proc]} {
31 # remove existing debug trace if any
32 initProcReportTrace remove modStartNb
33 rename ::modStartNb ::$::g_modStartNb_proc
34 }
35 ##nagelfar syntax modStartNb x x
36 rename ::$procname ::modStartNb
37 # set report traces if some debug mode enabled
38 initProcReportTrace add modStartNb
39 set ::g_modStartNb_proc $procname
40 }
41 }
42
43 # alternative definitions of modStartNb proc
44 proc modStartNbProc {mod name} {
45 # first compare against name's parent chunk by chunk
46 set modname [getModuleNameFromVersSpec $name]
47 if {$modname eq {.}} {
48 set i 0
49 set imax 0
50 } else {
51 set namesplit [split $modname /]
52 set modsplit [split $mod /]
53 set imax [tcl::mathfunc::min [llength $namesplit] [llength $modsplit]]
54 for {set i 0} {$i < $imax} {incr i} {
55 if {![string equal [lindex $modsplit $i] [lindex $namesplit $i]]} {
56 break
57 }
58 }
59 }
60 # if name's parent matches check if full name also matches
61 if {$i == $imax && [modEq $name $mod eqstart]} {
62 incr i
63 }
64 return $i
65 }
66 proc modStartNbProcIcase {mod name} {
67 set modname [getModuleNameFromVersSpec $name]
68 if {$modname eq {.}} {
69 set i 0
70 set imax 0
71 } else {
72 set namesplit [split $modname /]
73 set modsplit [split $mod /]
74 ##nagelfar ignore #2 Badly formed if statement
75 set imax [if {[llength $namesplit] < [llength $modsplit]} {llength\
76 $namesplit} {llength $modsplit}]
77 for {set i 0} {$i < $imax} {incr i} {
78 if {![string equal -nocase [lindex $modsplit $i] [lindex $namesplit\
79 $i]]} {
80 break
81 }
82 }
83 }
84 if {$i == $imax && [modEq $name $mod eqstart]} {
85 incr i
86 }
87 return $i
88 }
89
90 # Define procedure to compare module names set as array keys against pattern.
91 # Adapt procedure code whether implicit_default is enabled or disabled
92 proc defineGetEqArrayKeyProc {icase extdfl impdfl} {
93 set procname getEqArrayKeyProc
94 if {$impdfl} {
95 append procname Impdfl
96 }
97
98 # define proc if not done yet or if it was defined for another context
99 if {[info procs getEqArrayKey] eq {} || $::g_getEqArrayKey_proc ne\
100 $procname} {
101 if {[info exists ::g_getEqArrayKey_proc]} {
102 # remove existing debug trace if any
103 initProcReportTrace remove getEqArrayKey
104 rename ::getEqArrayKey ::$::g_getEqArrayKey_proc
105 }
106 ##nagelfar syntax getEqArrayKey x x
107 rename ::$procname ::getEqArrayKey
108 # set report traces if some debug mode enabled
109 initProcReportTrace add getEqArrayKey
110 set ::g_getEqArrayKey_proc $procname
111 }
112
113 # also define modEq which is called by getEqArrayKey
114 defineModEqProc $icase $extdfl
115 }
116
117 # alternative definitions of getEqArrayKey proc
118 proc getEqArrayKeyProcImpdfl {arrname name} {
119 set icase [isIcase]
120 upvar $arrname arr
121
122 # extract single module specified if any
123 lassign [getModuleVersSpec $name] mod modname
124 # check name eventual icase match
125 set mod [getArrayKey arr [string trimright $mod /] $icase]
126
127 if {$mod ne {} && [info exists arr($mod)]} {
128 set match $mod
129 } else {
130 set mlist {}
131 foreach elt [array names arr] {
132 if {[modEq $name $elt]} {
133 lappend mlist $elt
134 }
135 }
136 if {[llength $mlist] == 1} {
137 set match [lindex $mlist 0]
138 # in case multiple modules match query, check directory default and
139 # return it if it is part of match list, elsewhere return highest result
140 } elseif {[llength $mlist] > 1} {
141 # get corresponding icase parent directory
142 set pname [getArrayKey arr $modname $icase]
143 if {[info exists arr($pname)]} {
144 set dfl $pname/[lindex $arr($pname) 1]
145 }
146 # resolve symbolic version entries
147 foreach elt $mlist {
148 if {[lindex $arr($elt) 0] eq {version}} {
149 lappend mrlist [lindex $arr($elt) 1]
150 } else {
151 lappend mrlist $elt
152 }
153 }
154 if {[info exists dfl] && $dfl in $mrlist} {
155 set match $dfl
156 } else {
157 set match [lindex [lsort -dictionary $mrlist] end]
158 }
159 }
160 }
161 if {[info exists match]} {
162 reportDebug "key '$match' in array '$arrname' matches '$name'"
163 set name $match
164 }
165 return $name
166 }
167 proc getEqArrayKeyProc {arrname name} {
168 set icase [isIcase]
169 upvar $arrname arr
170
171 lassign [getModuleVersSpec $name] mod modname cmpspec versspec modnamere\
172 modescglob modroot variantlist modnvspec
173 # check name eventual icase match
174 set mod [getArrayKey arr [string trimright $mod /] $icase]
175
176 if {$mod ne {} && [info exists arr($mod)]} {
177 set match $mod
178 } else {
179 set mlist {}
180 foreach elt [array names arr] {
181 if {[modEq $name $elt]} {
182 lappend mlist $elt
183 }
184 }
185 # must have a default part of result even if only one result
186 if {[llength $mlist] >= 1} {
187 # get corresponding icase parent directory
188 set pname [getArrayKey arr $modname $icase]
189 if {[info exists arr($pname)]} {
190 set dfl $pname/[lindex $arr($pname) 1]
191 }
192 # resolve symbolic version entries
193 foreach elt $mlist {
194 if {[lindex $arr($elt) 0] eq {version}} {
195 lappend mrlist [lindex $arr($elt) 1]
196 } else {
197 lappend mrlist $elt
198 }
199 }
200 if {[info exists dfl] && $dfl in $mrlist} {
201 set match $dfl
202 } else {
203 # raise error as no default part of result
204 upvar retlist retlist
205 set retlist [list {} $modnvspec $name none "No default version\
206 defined for '$name'"]
207 }
208 }
209 }
210 if {[info exists match]} {
211 reportDebug "key '$match' in array '$arrname' matches '$name'"
212 set name $match
213 }
214 return $name
215 }
216
217 # Check a module name does match query at the depth level expressed in query
218 # when search mode is not contains. Define procedure on the fly to adapt its
219 # code to search configuration option and querydepth and test mode params.
220 proc defineDoesModMatchAtDepthProc {contains querydepth test} {
221 set procprops $contains:$querydepth:$test
222
223 # define proc if not done yet or if it was defined for another context
224 if {[info procs doesModMatchAtDepth] eq {} ||\
225 $::g_doesModMatchAtDepth_procprops ne $procprops} {
226 if {[info exists ::g_doesModMatchAtDepth_procprops]} {
227 # remove existing debug trace if any
228 initProcReportTrace remove doesModMatchAtDepth
229 rename ::doesModMatchAtDepth {}
230 }
231 set ::g_doesModMatchAtDepth_procprops $procprops
232
233 # define optimized procedure
234 if {$contains} {
235 set atdepth {$mod}
236 } else {
237 set atdepth "\[join \[lrange \[split \$mod /\] 0 $querydepth\] /\]"
238 }
239 ##nagelfar syntax doesModMatchAtDepth x
240 ##nagelfar ignore Non constant argument to proc
241 proc doesModMatchAtDepth {mod} "return \[modEqStatic $atdepth $test *\]"
242 # set report traces if some debug mode enabled
243 initProcReportTrace add doesModMatchAtDepth
244 }
245 }
246
247 # Define procedure to check module version equals pattern. Adapt procedure
248 # code whether icase and extended_default are enabled or disabled
249 proc defineModVersCmpProc {icase extdfl} {
250 set procname modVersCmpProc
251 if {$icase} {
252 append procname Icase
253 }
254 if {$extdfl} {
255 append procname Extdfl
256 }
257
258 # define proc if not done yet or if it was defined for another context
259 if {[info procs modVersCmp] eq {} || $::g_modVersCmp_proc ne $procname} {
260 if {[info exists ::g_modVersCmp_proc]} {
261 # remove existing debug trace if any
262 initProcReportTrace remove modVersCmp
263 rename ::modVersCmp ::$::g_modVersCmp_proc
264 }
265 ##nagelfar syntax modVersCmp x x x x x?
266 rename ::$procname ::modVersCmp
267 # set report traces if some debug mode enabled
268 initProcReportTrace add modVersCmp
269 set ::g_modVersCmp_proc $procname
270 }
271 }
272
273 # alternative definitions of modVersCmp proc
274 proc modVersCmpProc {cmpspec versspec modvers test {psuf {}}} {
275 set ret 0
276 switch -- $cmpspec {
277 in {
278 # check each verspec in list until match
279 foreach inspec $versspec {
280 lassign $inspec incmp invers
281 if {[set ret [modVersCmp $incmp $invers $modvers $test $psuf]]} {
282 break
283 }
284 }
285 }
286 eq {
287 append versspec $psuf
288 if {$test eq {eqstart}} {
289 set ret [string equal -length [string length $versspec/]\
290 $versspec/ $modvers/]
291 } else {
292 ##nagelfar ignore Non static subcommand
293 set ret [string $test $versspec $modvers]
294 }
295 }
296 ge {
297 # as we work here on a version range: psuf suffix is ignored, checks
298 # are always extended_default-enabled (as 1.2 includes 1.2.12 for
299 # instance) and equal, eqstart and match tests are equivalent
300 set ret [expr {[isVersion $modvers] && ([versioncmp $modvers\
301 $versspec] != -1 || [string match $versspec.* $modvers])}]
302 }
303 le {
304 # 'ge' comment also applies here
305 set ret [expr {[isVersion $modvers] && ([versioncmp $versspec\
306 $modvers] != -1 || [string match $versspec.* $modvers])}]
307 }
308 be {
309 # 'ge' comment also applies here
310 lassign $versspec lovers hivers
311 set ret [expr {[isVersion $modvers] && ([versioncmp $modvers\
312 $lovers] != -1 || [string match $lovers.* $modvers]) &&\
313 ([versioncmp $hivers $modvers] != -1 || [string match\
314 $hivers.* $modvers])}]
315 }
316 }
317 return $ret
318 }
319 proc modVersCmpProcIcase {cmpspec versspec modvers test {psuf {}}} {
320 set ret 0
321 switch -- $cmpspec {
322 in {
323 foreach inspec $versspec {
324 lassign $inspec incmp invers
325 if {[set ret [modVersCmp $incmp $invers $modvers $test $psuf]]} {
326 break
327 }
328 }
329 }
330 eq {
331 append versspec $psuf
332 if {$test eq {eqstart}} {
333 set ret [string equal -nocase -length [string length $versspec/]\
334 $versspec/ $modvers/]
335 } else {
336 ##nagelfar ignore Non static subcommand
337 set ret [string $test -nocase $versspec $modvers]
338 }
339 }
340 ge {
341 set ret [expr {[isVersion $modvers] && ([versioncmp $modvers\
342 $versspec] != -1 || [string match -nocase $versspec.* $modvers])}]
343 }
344 le {
345 set ret [expr {[isVersion $modvers] && ([versioncmp $versspec\
346 $modvers] != -1 || [string match -nocase $versspec.* $modvers])}]
347 }
348 be {
349 lassign $versspec lovers hivers
350 set ret [expr {[isVersion $modvers] && ([versioncmp $modvers\
351 $lovers] != -1 || [string match $lovers.* $modvers]) &&\
352 ([versioncmp $hivers $modvers] != -1 || [string match -nocase\
353 $hivers.* $modvers])}]
354 }
355 }
356 return $ret
357 }
358 proc modVersCmpProcExtdfl {cmpspec versspec modvers test {psuf {}}} {
359 set ret 0
360 switch -- $cmpspec {
361 in {
362 foreach inspec $versspec {
363 lassign $inspec incmp invers
364 if {[set ret [modVersCmp $incmp $invers $modvers $test $psuf]]} {
365 break
366 }
367 }
368 }
369 eq {
370 append versspec $psuf
371 if {$test eq {eqstart}} {
372 set ret [string equal -length [string length $versspec/]\
373 $versspec/ $modvers/]
374 } else {
375 ##nagelfar ignore Non static subcommand
376 set ret [string $test $versspec $modvers]
377 }
378 if {!$ret && [string match $versspec.* $modvers]} {
379 set ret 1
380 }
381 }
382 ge {
383 set ret [expr {[isVersion $modvers] && ([versioncmp $modvers\
384 $versspec] != -1 || [string match $versspec.* $modvers])}]
385 }
386 le {
387 set ret [expr {[isVersion $modvers] && ([versioncmp $versspec\
388 $modvers] != -1 || [string match $versspec.* $modvers])}]
389 }
390 be {
391 lassign $versspec lovers hivers
392 set ret [expr {[isVersion $modvers] && ([versioncmp $modvers\
393 $lovers] != -1 || [string match $lovers.* $modvers]) &&\
394 ([versioncmp $hivers $modvers] != -1 || [string match\
395 $hivers.* $modvers])}]
396 }
397 }
398 return $ret
399 }
400 proc modVersCmpProcIcaseExtdfl {cmpspec versspec modvers test {psuf {}}} {
401 set ret 0
402 switch -- $cmpspec {
403 in {
404 foreach inspec $versspec {
405 lassign $inspec incmp invers
406 if {[set ret [modVersCmp $incmp $invers $modvers $test $psuf]]} {
407 break
408 }
409 }
410 }
411 eq {
412 append versspec $psuf
413 if {$test eq {eqstart}} {
414 set ret [string equal -nocase -length [string length $versspec/]\
415 $versspec/ $modvers/]
416 } else {
417 ##nagelfar ignore Non static subcommand
418 set ret [string $test -nocase $versspec $modvers]
419 }
420 if {!$ret && [string match -nocase $versspec.* $modvers]} {
421 set ret 1
422 }
423 }
424 ge {
425 set ret [expr {[isVersion $modvers] && ([versioncmp $modvers\
426 $versspec] != -1 || [string match -nocase $versspec.* $modvers])}]
427 }
428 le {
429 set ret [expr {[isVersion $modvers] && ([versioncmp $versspec\
430 $modvers] != -1 || [string match -nocase $versspec.* $modvers])}]
431 }
432 be {
433 lassign $versspec lovers hivers
434 set ret [expr {[isVersion $modvers] && ([versioncmp $modvers\
435 $lovers] != -1 || [string match $lovers.* $modvers]) &&\
436 ([versioncmp $hivers $modvers] != -1 || [string match -nocase\
437 $hivers.* $modvers])}]
438 }
439 }
440 return $ret
441 }
442
443 proc modVariantCmp {pvrlist modvrlist {missmean 0}} {
444 set ret 1
445 # missing variant in mod spec means default value
446 if {$missmean == 1} {
447 foreach {modvrname modvrval modvrisdfl} $modvrlist {
448 set modvrarr($modvrname) $modvrval
449 set modvrisdflarr($modvrname) $modvrisdfl
450 }
451 } else {
452 array set modvrarr $modvrlist
453 # if missmean == 2 pattern is mod, thus missing variant on mod is ok
454 # it is used for extra specifier where pattern is definition inside
455 # modulefile and mod is extra specifier defined on command line
456 }
457 foreach pvr $pvrlist {
458 set pvrarr([lindex $pvr 0]) [lindex $pvr 3]
459 }
460
461 # no match if a specified variant is not found among module variants (and
462 # if miss is not ok) or if the value differs
463 foreach vrname [array names pvrarr] {
464 if {(![info exists modvrarr($vrname)] && $missmean != 2) || ([info\
465 exists modvrarr($vrname)] && $pvrarr($vrname) ne\
466 $modvrarr($vrname))} {
467 set ret 0
468 break
469 }
470 }
471
472 # if an unset variant on pattern means variant default value pattern and
473 # mod are not equal if variant unset on pattern and non-default value is
474 # set for variant on mod
475 if {$missmean == 1} {
476 foreach vrname [array names modvrisdflarr] {
477 if {!$modvrisdflarr($vrname) && ![info exists pvrarr($vrname)]} {
478 set ret 0
479 break
480 }
481 }
482 }
483
484 return $ret
485 }
486
487 # Setup a hardwire version of modEq procedure called modEqStatic. This
488 # optimized procedure already knows the module pattern to compare to, whose
489 # specification has already been resolved at procedure definition time, which
490 # saves lot of processing time.
491 # modEqStatic does not compare against loaded modules so it has no need to
492 # compare variants set on module specification
493 proc defineModEqStaticProc {icase extdfl modspec} {
494 set procprops $icase:$extdfl:$modspec
495
496 # define proc if not done yet or if it was defined for another context
497 if {[info procs modEqStatic] eq {} || $::g_modEqStatic_procprops ne\
498 $procprops} {
499 if {[info exists ::g_modEqStatic_procprops]} {
500 # remove existing debug trace if any
501 initProcReportTrace remove modEqStatic
502 rename ::modEqStatic {}
503 } else {
504 # also define modVersCmp which is called by modEqStatic
505 defineModVersCmpProc $icase $extdfl
506 }
507 set ::g_modEqStatic_procprops $procprops
508
509 # define optimized procedure
510 lassign [getModuleVersSpec $modspec] pmod pmodname cmpspec versspec\
511 pmodnamere pmodescglob
512 # trim dup trailing / char and adapt pmod suffix if it starts with /
513 if {[string index $pmod end] eq {/}} {
514 set pmod [string trimright $pmod /]/
515 set endwslash 1
516 } else {
517 set endwslash 0
518 }
519 set nocasearg [expr {$icase ? {-nocase } : {}}]
520 set pmodnameslen [string length $pmodname/]
521 if {$pmod ne {} || $modspec eq {}} {
522 set procbody "
523 set pmod {$pmod}
524 if {\$psuf ne {}} {
525 if {$endwslash && \[string index \$psuf 0\] eq {/}} {
526 append pmod \[string range \$psuf 1 end\]
527 } else {
528 append pmod \$psuf
529 }
530 }
531 if {\$test eq {eqstart}} {
532 set ret \[string equal $nocasearg-length \[string length\
533 \$pmod/\] \$pmod/ \$mod/\]
534 } else {
535 if {\$test eq {matchin}} {
536 set test match
537 set pmod *\$pmod
538 }
539 set ret \[string \$test $nocasearg\$pmod \$mod\]
540 }"
541 if {$extdfl} {
542 append procbody "
543 if {!\$ret && \[string first / \$pmod\] != -1} {
544 if {\$test eq {match}} {
545 set pmodextdfl \$pmod.*
546 } else {
547 set pmodextdfl {$pmodescglob.*}
548 }
549 set ret \[string match $nocasearg\$pmodextdfl \$mod\]
550 }"
551 }
552 } else {
553 set procbody "
554 set pmodname {$pmodname}
555 set pmodnamere {$pmodnamere}
556 if {\$test eq {matchin}} {
557 set test match
558 if {\$pmodnamere ne {}} {
559 set pmodnamere .*\$pmodnamere
560 } else {
561 set pmodnamere {.*$pmodname}
562 }
563 }
564 if {(\$pmodnamere ne {} && \$test eq {match} && \[regexp\
565 $nocasearg (^\$pmodnamere)/ \$mod/ rematch pmodname\]) ||\
566 \[string equal $nocasearg -length $pmodnameslen {$pmodname/}\
567 \$mod/\]} {
568 set modvers \[string range \$mod \[string length \$pmodname/\]\
569 end\]
570 set ret \[modVersCmp {$cmpspec} {$versspec} \$modvers \$test\
571 \$psuf\]
572 } else {
573 set ret 0
574 }"
575 }
576 append procbody "
577 return \$ret"
578 ##nagelfar syntax modEqStatic x x? x?
579 ##nagelfar ignore Non constant argument to proc
580 proc modEqStatic {mod {test equal} {psuf {}}} $procbody
581 # set report traces if some debug mode enabled
582 initProcReportTrace add modEqStatic
583 }
584 }
585
586 # Define procedure to check module name equals pattern. Adapt procedure
587 # code whether icase and extended_default are enabled or disabled
588 proc defineModEqProc {icase extdfl {loadedmod 0}} {
589 set procname modEqProc
590 if {$icase} {
591 append procname Icase
592 }
593 if {$extdfl} {
594 append procname Extdfl
595 }
596
597 # define proc if not done yet or if it was defined for another context
598 if {[info procs modEq] eq {} || $::g_modEq_proc ne $procname} {
599 if {[info exists ::g_modEq_proc]} {
600 # remove existing debug trace if any
601 initProcReportTrace remove modEq
602 rename ::modEq ::$::g_modEq_proc
603 }
604 ##nagelfar syntax modEq x x x? x? x? x? x? x?
605 rename ::$procname ::modEq
606 # set report traces if some debug mode enabled
607 initProcReportTrace add modEq
608 set ::g_modEq_proc $procname
609 }
610
611 # also define modVersCmp which is called by modEq
612 defineModVersCmpProc $icase $extdfl
613
614 # comparing against loaded modules requires to know their alternative names
615 if {$loadedmod} {
616 cacheCurrentModules
617 }
618 }
619
620 # alternative definitions of modEq proc
621 proc modEqProc {pattern mod {test equal} {trspec 1} {ismodlo 0} {vrcmp 0}\
622 {modvrlist 0} {psuf {}}} {
623 # extract specified module name from name and version spec
624 if {$trspec} {
625 lassign [getModuleVersSpec $pattern] pmod pmodname cmpspec versspec\
626 pmodnamere pmodescglob pmodroot pvrlist
627 } else {
628 set pmod $pattern
629 }
630 # trim dup trailing / char and adapt pmod suffix if it starts with /
631 if {[string index $pmod end] eq {/}} {
632 set pmod [string trimright $pmod /]/
633 set endwslash 1
634 } else {
635 set endwslash 0
636 }
637 # get alternative names if mod is loading(1) or loaded(2)
638 set altlist [switch -- $ismodlo {
639 7 {getLoadedAltname $mod {alias}}
640 6 {getLoadedAltname $mod {sym autosym}}
641 5 {getAvailListFromVersSpec $mod}
642 4 {getAllModuleResolvedName $mod 0 {} 1}
643 3 {getLoadedAltAndSimplifiedName $mod}
644 2 {getLoadedAltname $mod}
645 1 {getAllModuleResolvedName $mod}
646 0 {list}}]
647 # fetch variant definition from spec if not loaded/loading
648 if {$vrcmp && $ismodlo in {0 5}} {
649 set modvrlist [getVariantList $mod 0 0 1]
650 set mod [getModuleNameAndVersFromVersSpec $mod]
651 }
652 # specified module can be translated in a simple mod name/vers or is empty
653 if {$pmod ne {} || $pattern eq {}} {
654 if {$psuf ne {}} {
655 if {$endwslash && [string index $psuf 0] eq {/}} {
656 append pmod [string range $psuf 1 end]
657 } else {
658 append pmod $psuf
659 }
660 }
661 if {$test eq {eqstart}} {
662 set ret [string equal -length [string length $pmod/] $pmod/ $mod/]
663 # apply comparison to alternative names if any and no match for mod
664 if {!$ret && [llength $altlist]} {
665 foreach alt $altlist {
666 if {[set ret [string equal -length [string length $pmod/]\
667 $pmod/ $alt/]]} {
668 break
669 }
670 }
671 }
672 } else {
673 # contains test
674 if {$test eq {matchin}} {
675 set test match
676 set pmod *$pmod
677 } elseif {$test eq {eqspec}} {
678 set test equal
679 }
680 ##nagelfar ignore Non static subcommand
681 set ret [string $test $pmod $mod]
682 # apply comparison to alternative names if any and no match for mod
683 if {!$ret && [llength $altlist]} {
684 foreach alt $altlist {
685 ##nagelfar ignore Non static subcommand
686 if {[set ret [string $test $pmod $alt]]} {
687 break
688 }
689 }
690 }
691 }
692 } elseif {$test eq {eqspec}} {
693 # test equality against all version described in spec (list or range
694 # boundaries), trspec is considered enabled and psuf empty
695 foreach pmod [getAllModulesFromVersSpec $pattern] {
696 if {[set ret [string equal $pmod $mod]]} {
697 break
698 }
699 }
700 } else {
701 # contains test
702 if {$test eq {matchin}} {
703 set test match
704 if {$pmodnamere ne {}} {
705 set pmodnamere .*$pmodnamere
706 } else {
707 set pmodnamere .*$pmodname
708 }
709 }
710 # for more complex specification, first check if module name matches
711 # use a regexp test if module name contains wildcard characters
712 if {($pmodnamere ne {} && $test eq {match} && [regexp (^$pmodnamere)/\
713 $mod/ rematch pmodname]) || [string equal -length [string length\
714 $pmodname/] $pmodname/ $mod/]} {
715 # then compare versions
716 set modvers [string range $mod [string length $pmodname/] end]
717 set ret [modVersCmp $cmpspec $versspec $modvers $test $psuf]
718 } else {
719 set ret 0
720 }
721 # apply comparison to alternative names if any and no match for mod
722 if {!$ret && [llength $altlist]} {
723 foreach alt $altlist {
724 if {($pmodnamere ne {} && $test eq {match} && [regexp\
725 (^$pmodnamere)/ $alt/ rematch pmodname]) || [string equal\
726 -length [string length $pmodname/] $pmodname/ $alt/]} {
727 # then compare versions
728 set modvers [string range $alt [string length $pmodname/] end]
729 if {[set ret [modVersCmp $cmpspec $versspec $modvers $test\
730 $psuf]]} {
731 break
732 }
733 }
734 }
735 }
736 }
737 # check if variant specified matches those of selected loaded/ing module
738 if {$ret && $vrcmp && $ismodlo ni {3 5} && [llength $pvrlist]} {
739 if {$modvrlist eq {0}} {
740 set modvrlist [getVariantList $mod]
741 }
742 set ret [modVariantCmp $pvrlist $modvrlist]
743 # variant miss means variant default val when comparing collection content
744 } elseif {$ret && $vrcmp && $ismodlo == 3} {
745 set ret [modVariantCmp $pvrlist [getVariantList $mod 3] 1]
746 # variant miss is ok when comparing an extra specifier passed as mod
747 } elseif {$ret && $vrcmp && $ismodlo == 5} {
748 set ret [modVariantCmp $pvrlist $modvrlist 2]
749 }
750 return $ret
751 }
752 proc modEqProcIcase {pattern mod {test equal} {trspec 1} {ismodlo 0} {vrcmp\
753 0} {modvrlist 0} {psuf {}}} {
754 if {$trspec} {
755 lassign [getModuleVersSpec $pattern] pmod pmodname cmpspec versspec\
756 pmodnamere pmodescglob pmodroot pvrlist
757 } else {
758 set pmod $pattern
759 }
760 if {[string index $pmod end] eq {/}} {
761 set pmod [string trimright $pmod /]/
762 set endwslash 1
763 } else {
764 set endwslash 0
765 }
766 set altlist [switch -- $ismodlo {
767 7 {getLoadedAltname $mod {alias}}
768 6 {getLoadedAltname $mod {sym autosym}}
769 5 {getAvailListFromVersSpec $mod}
770 4 {getAllModuleResolvedName $mod 0 {} 1}
771 3 {getLoadedAltAndSimplifiedName $mod}
772 2 {getLoadedAltname $mod}
773 1 {getAllModuleResolvedName $mod}
774 0 {list}}]
775 if {$vrcmp && $ismodlo in {0 5}} {
776 set modvrlist [getVariantList $mod 0 0 1]
777 set mod [getModuleNameAndVersFromVersSpec $mod]
778 }
779 if {$pmod ne {} || $pattern eq {}} {
780 if {$psuf ne {}} {
781 if {$endwslash && [string index $psuf 0] eq {/}} {
782 append pmod [string range $psuf 1 end]
783 } else {
784 append pmod $psuf
785 }
786 }
787 if {$test eq {eqstart}} {
788 set ret [string equal -nocase -length [string length $pmod/] $pmod/\
789 $mod/]
790 if {!$ret && [llength $altlist]} {
791 foreach alt $altlist {
792 if {[set ret [string equal -nocase -length [string length\
793 $pmod/] $pmod/ $alt/]]} {
794 break
795 }
796 }
797 }
798 } else {
799 # contains test
800 if {$test eq {matchin}} {
801 set test match
802 set pmod *$pmod
803 } elseif {$test eq {eqspec}} {
804 set test equal
805 }
806 ##nagelfar ignore Non static subcommand
807 set ret [string $test -nocase $pmod $mod]
808 if {!$ret && [llength $altlist]} {
809 foreach alt $altlist {
810 ##nagelfar ignore Non static subcommand
811 if {[set ret [string $test -nocase $pmod $alt]]} {
812 break
813 }
814 }
815 }
816 }
817 } elseif {$test eq {eqspec}} {
818 # test equality against all version described in spec (list or range
819 # boundaries), trspec is considered enabled and psuf empty
820 foreach pmod [getAllModulesFromVersSpec $pattern] {
821 if {[set ret [string equal -nocase $pmod $mod]]} {
822 break
823 }
824 }
825 } else {
826 # contains test
827 if {$test eq {matchin}} {
828 set test match
829 if {$pmodnamere ne {}} {
830 set pmodnamere .*$pmodnamere
831 } else {
832 set pmodnamere .*$pmodname
833 }
834 }
835 # for more complex specification, first check if module name matches
836 # use a regexp test if module name contains wildcard characters
837 if {($pmodnamere ne {} && $test eq {match} && [regexp -nocase\
838 (^$pmodnamere)/ $mod/ rematch pmodname]) || [string equal -nocase\
839 -length [string length $pmodname/] $pmodname/ $mod/]} {
840 # then compare versions
841 set modvers [string range $mod [string length $pmodname/] end]
842 set ret [modVersCmp $cmpspec $versspec $modvers $test $psuf]
843 } else {
844 set ret 0
845 }
846 if {!$ret && [llength $altlist]} {
847 foreach alt $altlist {
848 if {($pmodnamere ne {} && $test eq {match} && [regexp -nocase\
849 (^$pmodnamere)/ $alt/ rematch pmodname]) || [string equal\
850 -nocase -length [string length $pmodname/] $pmodname/ $alt/]} {
851 # then compare versions
852 set modvers [string range $alt [string length $pmodname/] end]
853 if {[set ret [modVersCmp $cmpspec $versspec $modvers $test\
854 $psuf]]} {
855 break
856 }
857 }
858 }
859 }
860 }
861 if {$ret && $vrcmp && $ismodlo ni {3 5} && [llength $pvrlist]} {
862 if {$modvrlist eq {0}} {
863 set modvrlist [getVariantList $mod]
864 }
865 set ret [modVariantCmp $pvrlist $modvrlist]
866 } elseif {$ret && $vrcmp && $ismodlo == 3} {
867 set ret [modVariantCmp $pvrlist [getVariantList $mod 3] 1]
868 } elseif {$ret && $vrcmp && $ismodlo == 5} {
869 set ret [modVariantCmp $pvrlist $modvrlist 2]
870 }
871 return $ret
872 }
873 proc modEqProcExtdfl {pattern mod {test equal} {trspec 1} {ismodlo 0} {vrcmp\
874 0} {modvrlist 0} {psuf {}}} {
875 if {$trspec} {
876 lassign [getModuleVersSpec $pattern] pmod pmodname cmpspec versspec\
877 pmodnamere pmodescglob pmodroot pvrlist
878 } else {
879 set pmod $pattern
880 }
881 if {[string index $pmod end] eq {/}} {
882 set pmod [string trimright $pmod /]/
883 set endwslash 1
884 } else {
885 set endwslash 0
886 }
887 set altlist [switch -- $ismodlo {
888 7 {getLoadedAltname $mod {alias}}
889 6 {getLoadedAltname $mod {sym autosym}}
890 5 {getAvailListFromVersSpec $mod}
891 4 {getAllModuleResolvedName $mod 0 {} 1}
892 3 {getLoadedAltAndSimplifiedName $mod}
893 2 {getLoadedAltname $mod}
894 1 {getAllModuleResolvedName $mod}
895 0 {list}}]
896 if {$vrcmp && $ismodlo in {0 5}} {
897 set modvrlist [getVariantList $mod 0 0 1]
898 set mod [getModuleNameAndVersFromVersSpec $mod]
899 }
900 if {$pmod ne {} || $pattern eq {}} {
901 if {$psuf ne {}} {
902 if {$endwslash && [string index $psuf 0] eq {/}} {
903 append pmod [string range $psuf 1 end]
904 } else {
905 append pmod $psuf
906 }
907 }
908 if {$test eq {eqstart}} {
909 set ret [string equal -length [string length $pmod/] $pmod/ $mod/]
910 if {!$ret && [llength $altlist]} {
911 foreach alt $altlist {
912 if {[set ret [string equal -length [string length $pmod/]\
913 $pmod/ $alt/]]} {
914 break
915 }
916 }
917 }
918 } else {
919 # contains test
920 if {$test eq {matchin}} {
921 set test match
922 set pmod *$pmod
923 } elseif {$test eq {eqspec}} {
924 set test equal
925 set eqspec 1
926 }
927 ##nagelfar ignore Non static subcommand
928 set ret [string $test $pmod $mod]
929 if {!$ret && [llength $altlist]} {
930 foreach alt $altlist {
931 ##nagelfar ignore Non static subcommand
932 if {[set ret [string $test $pmod $alt]]} {
933 break
934 }
935 }
936 }
937 }
938 # try the extended default match if not root module and not eqspec test
939 if {![info exists eqspec] && !$ret && [string first / $pmod] != -1} {
940 if {$test eq {match}} {
941 set pmodextdfl $pmod.*
942 } else {
943 set pmodextdfl $pmodescglob.*
944 }
945 set ret [string match $pmodextdfl $mod]
946 if {!$ret && [llength $altlist]} {
947 foreach alt $altlist {
948 if {[set ret [string match $pmodextdfl $alt]]} {
949 break
950 }
951 }
952 }
953 }
954 } elseif {$test eq {eqspec}} {
955 # test equality against all version described in spec (list or range
956 # boundaries), trspec is considered enabled and psuf empty
957 foreach pmod [getAllModulesFromVersSpec $pattern] {
958 if {[set ret [string equal $pmod $mod]]} {
959 break
960 }
961 }
962 } else {
963 # contains test
964 if {$test eq {matchin}} {
965 set test match
966 if {$pmodnamere ne {}} {
967 set pmodnamere .*$pmodnamere
968 } else {
969 set pmodnamere .*$pmodname
970 }
971 }
972 # for more complex specification, first check if module name matches
973 # use a regexp test if module name contains wildcard characters
974 if {($pmodnamere ne {} && $test eq {match} && [regexp (^$pmodnamere)/\
975 $mod/ rematch pmodname]) || [string equal -length [string length\
976 $pmodname/] $pmodname/ $mod/]} {
977 # then compare versions
978 set modvers [string range $mod [string length $pmodname/] end]
979 set ret [modVersCmp $cmpspec $versspec $modvers $test $psuf]
980 } else {
981 set ret 0
982 }
983 if {!$ret && [llength $altlist]} {
984 foreach alt $altlist {
985 if {($pmodnamere ne {} && $test eq {match} && [regexp\
986 (^$pmodnamere)/ $alt/ rematch pmodname]) || [string equal\
987 -length [string length $pmodname/] $pmodname/ $alt/]} {
988 # then compare versions
989 set modvers [string range $alt [string length $pmodname/] end]
990 if {[set ret [modVersCmp $cmpspec $versspec $modvers $test\
991 $psuf]]} {
992 break
993 }
994 }
995 }
996 }
997 }
998 if {$ret && $vrcmp && $ismodlo ni {3 5} && [llength $pvrlist]} {
999 if {$modvrlist eq {0}} {
1000 set modvrlist [getVariantList $mod]
1001 }
1002 set ret [modVariantCmp $pvrlist $modvrlist]
1003 } elseif {$ret && $vrcmp && $ismodlo == 3} {
1004 set ret [modVariantCmp $pvrlist [getVariantList $mod 3] 1]
1005 } elseif {$ret && $vrcmp && $ismodlo == 5} {
1006 set ret [modVariantCmp $pvrlist $modvrlist 2]
1007 }
1008 return $ret
1009 }
1010 proc modEqProcIcaseExtdfl {pattern mod {test equal} {trspec 1} {ismodlo 0}\
1011 {vrcmp 0} {modvrlist 0} {psuf {}}} {
1012 if {$trspec} {
1013 lassign [getModuleVersSpec $pattern] pmod pmodname cmpspec versspec\
1014 pmodnamere pmodescglob pmodroot pvrlist
1015 } else {
1016 set pmod $pattern
1017 }
1018 if {[string index $pmod end] eq {/}} {
1019 set pmod [string trimright $pmod /]/
1020 set endwslash 1
1021 } else {
1022 set endwslash 0
1023 }
1024 set altlist [switch -- $ismodlo {
1025 7 {getLoadedAltname $mod {alias}}
1026 6 {getLoadedAltname $mod {sym autosym}}
1027 5 {getAvailListFromVersSpec $mod}
1028 4 {getAllModuleResolvedName $mod 0 {} 1}
1029 3 {getLoadedAltAndSimplifiedName $mod}
1030 2 {getLoadedAltname $mod}
1031 1 {getAllModuleResolvedName $mod}
1032 0 {list}}]
1033 if {$vrcmp && $ismodlo in {0 5}} {
1034 set modvrlist [getVariantList $mod 0 0 1]
1035 set mod [getModuleNameAndVersFromVersSpec $mod]
1036 }
1037 if {$pmod ne {} || $pattern eq {}} {
1038 if {$psuf ne {}} {
1039 if {$endwslash && [string index $psuf 0] eq {/}} {
1040 append pmod [string range $psuf 1 end]
1041 } else {
1042 append pmod $psuf
1043 }
1044 }
1045 if {$test eq {eqstart}} {
1046 set ret [string equal -nocase -length [string length $pmod/] $pmod/\
1047 $mod/]
1048 if {!$ret && [llength $altlist]} {
1049 foreach alt $altlist {
1050 if {[set ret [string equal -nocase -length [string length\
1051 $pmod/] $pmod/ $alt/]]} {
1052 break
1053 }
1054 }
1055 }
1056 } else {
1057 # contains test
1058 if {$test eq {matchin}} {
1059 set test match
1060 set pmod *$pmod
1061 } elseif {$test eq {eqspec}} {
1062 set test equal
1063 set eqspec 1
1064 }
1065 ##nagelfar ignore Non static subcommand
1066 set ret [string $test -nocase $pmod $mod]
1067 if {!$ret && [llength $altlist]} {
1068 foreach alt $altlist {
1069 ##nagelfar ignore Non static subcommand
1070 if {[set ret [string $test -nocase $pmod $alt]]} {
1071 break
1072 }
1073 }
1074 }
1075 }
1076 # try the extended default match if not root module and not eqspec test
1077 if {![info exists eqspec] && !$ret && [string first / $pmod] != -1} {
1078 if {$test eq {match}} {
1079 set pmodextdfl $pmod.*
1080 } else {
1081 set pmodextdfl $pmodescglob.*
1082 }
1083 set ret [string match -nocase $pmodextdfl $mod]
1084 if {!$ret && [llength $altlist]} {
1085 foreach alt $altlist {
1086 if {[set ret [string match -nocase $pmodextdfl $alt]]} {
1087 break
1088 }
1089 }
1090 }
1091 }
1092 } elseif {$test eq {eqspec}} {
1093 # test equality against all version described in spec (list or range
1094 # boundaries), trspec is considered enabled and psuf empty
1095 foreach pmod [getAllModulesFromVersSpec $pattern] {
1096 if {[set ret [string equal -nocase $pmod $mod]]} {
1097 break
1098 }
1099 }
1100 } else {
1101 # contains test
1102 if {$test eq {matchin}} {
1103 set test match
1104 if {$pmodnamere ne {}} {
1105 set pmodnamere .*$pmodnamere
1106 } else {
1107 set pmodnamere .*$pmodname
1108 }
1109 }
1110 # for more complex specification, first check if module name matches
1111 # use a regexp test if module name contains wildcard characters
1112 if {($pmodnamere ne {} && $test eq {match} && [regexp -nocase\
1113 (^$pmodnamere)/ $mod/ rematch pmodname]) || [string equal -nocase\
1114 -length [string length $pmodname/] $pmodname/ $mod/]} {
1115 # then compare versions
1116 set modvers [string range $mod [string length $pmodname/] end]
1117 set ret [modVersCmp $cmpspec $versspec $modvers $test $psuf]
1118 } else {
1119 set ret 0
1120 }
1121 if {!$ret && [llength $altlist]} {
1122 foreach alt $altlist {
1123 if {($pmodnamere ne {} && $test eq {match} && [regexp -nocase\
1124 (^$pmodnamere)/ $alt/ rematch pmodname]) || [string equal\
1125 -nocase -length [string length $pmodname/] $pmodname/ $alt/]} {
1126 # then compare versions
1127 set modvers [string range $alt [string length $pmodname/] end]
1128 if {[set ret [modVersCmp $cmpspec $versspec $modvers $test\
1129 $psuf]]} {
1130 break
1131 }
1132 }
1133 }
1134 }
1135 }
1136 if {$ret && $vrcmp && $ismodlo ni {3 5} && [llength $pvrlist]} {
1137 if {$modvrlist eq {0}} {
1138 set modvrlist [getVariantList $mod]
1139 }
1140 set ret [modVariantCmp $pvrlist $modvrlist]
1141 } elseif {$ret && $vrcmp && $ismodlo == 3} {
1142 set ret [modVariantCmp $pvrlist [getVariantList $mod 3] 1]
1143 } elseif {$ret && $vrcmp && $ismodlo == 5} {
1144 set ret [modVariantCmp $pvrlist $modvrlist 2]
1145 }
1146 return $ret
1147 }
1148
1149 proc modEqAny {pattern_list mod {test equal} {trspec 1} {ismodlo 0} {vrcmp 0}\
1150 {modvrlist 0} {psuf {}}} {
1151 foreach pattern $pattern_list {
1152 if {[modEq $pattern $mod $test $trspec $ismodlo $vrcmp $modvrlist\
1153 $psuf]} {
1154 return 1
1155 }
1156 }
1157 return 0
1158 }
1159
1160 # analyze module version specified within module specification
1161 proc parseModuleVersionSpecifier {modspec} {
1162 set invalidversspec 0
1163 set invalidversrange 0
1164 set islist [expr {[string first , $modspec] != -1}]
1165 set isrange [expr {[string first : $modspec] != -1}]
1166 # no deep version specification allowed
1167 if {[string first / $modspec] != -1} {
1168 set invalidversspec 1
1169 # ',' separates multiple versions
1170 } elseif {$islist} {
1171 set cmpspec in
1172 set inspeclist [split $modspec ,]
1173 # empty element in list is erroneous
1174 set invalidversspec [expr {[lsearch -exact $inspeclist {}] != -1}]
1175 if {!$invalidversspec} {
1176 # recursive call to check each element in list (can be range, etc)
1177 foreach inspec $inspeclist {
1178 lappend versspec [parseModuleVersionSpecifier $inspec]
1179 }
1180 }
1181 # ':' separates range elements
1182 } elseif {$isrange} {
1183 set versspec [split $modspec :]
1184 set lovers [lindex $versspec 0]
1185 set hivers [lindex $versspec 1]
1186 if {[llength $versspec] != 2 || ($lovers eq {} && $hivers eq {})} {
1187 set invalidversspec 1
1188 } elseif {($lovers ne {} && ![isVersion $lovers]) || ($hivers ne {} &&\
1189 ![isVersion $hivers])} {
1190 set invalidversrange 1
1191 # greater or equal
1192 } elseif {$hivers eq {}} {
1193 set cmpspec ge
1194 set versspec $lovers
1195 # lower or equal
1196 } elseif {$lovers eq {}} {
1197 set cmpspec le
1198 set versspec $hivers
1199 # between or equal
1200 } elseif {[versioncmp $lovers $hivers] == 1} {
1201 set invalidversrange 1
1202 } else {
1203 set cmpspec be
1204 }
1205 } else {
1206 set cmpspec eq
1207 set versspec $modspec
1208 }
1209 if {$invalidversspec} {
1210 knerror "Invalid version specifier '$modspec'"
1211 }
1212 if {$invalidversrange} {
1213 knerror "Invalid version range '$modspec'"
1214 }
1215 return [list $cmpspec $versspec]
1216 }
1217
1218 # Define procedure to parse modulefile specification passed as argument
1219 # Adapt procedure code whether advanced_version_spec is enabled or disabled
1220 proc defineParseModuleSpecificationProc {advverspec} {
1221 set procname parseModuleSpecificationProc
1222 if {$advverspec} {
1223 append procname AdvVersSpec
1224 # resolved configured variant shortcut
1225 getConf variant_shortcut
1226 }
1227 # define proc if not done yet or if it was defined for another context
1228 if {[info procs parseModuleSpecification] eq {} ||\
1229 $::g_parseModuleSpecification_proc ne $procname} {
1230 if {[info exists ::g_parseModuleSpecification_proc]} {
1231 # remove existing debug trace if any
1232 initProcReportTrace remove parseModuleSpecification
1233 rename ::parseModuleSpecification\
1234 ::$::g_parseModuleSpecification_proc
1235 }
1236 ##nagelfar syntax parseModuleSpecification x x x x x*
1237 rename ::$procname ::parseModuleSpecification
1238 # set report traces if some debug mode enabled
1239 initProcReportTrace add parseModuleSpecification
1240 set ::g_parseModuleSpecification_proc $procname
1241 }
1242 }
1243
1244 # when advanced_version_spec option is enabled, parse argument list to set in
1245 # a global context version specification of modules passed as argument.
1246 # mlspec: specification may vary whether it comes from the ml or another
1247 # command. nonamespec: sometimes specification may omit module name and
1248 # version and just provides variant properties. xtspec: is extra specifier
1249 # specification allowed. getavails: query available modules to get those
1250 # matching module name and version
1251 proc parseModuleSpecificationProc {mlspec nonamespec xtspec getavails args} {
1252 # skip arg parse if proc was already call with same arg set by an upper
1253 # proc. check all args to ensure current arglist does not deviate from
1254 # what was previously parsed
1255 foreach arg $args {
1256 if {![info exists ::g_moduleVersSpec($arg)]} {
1257 set need_parse 1
1258 break
1259 }
1260 }
1261 if {![info exists need_parse]} {
1262 return $args
1263 }
1264
1265 set unarglist [list]
1266 set arglist [list]
1267
1268 foreach arg $args {
1269 if {$mlspec && [string index $arg 0] eq {-}} {
1270 set modname [string range $arg 1 end]
1271 set mlunload 1
1272 } else {
1273 set modname $arg
1274 set mlunload 0
1275 }
1276 # keep arg enclosed if composed of several words
1277 if {[string first { } $modname] != -1} {
1278 set modarg "{$modname}"
1279 } else {
1280 set modarg $modname
1281 }
1282 # record spec, especially needed if arg is enclosed
1283 setModuleVersSpec $modarg $modname eq {} {} {} $arg {} $getavails
1284 # append to unload list if ml spec and - prefix used
1285 if {$mlunload} {
1286 lappend unarglist $modarg
1287 } else {
1288 lappend arglist $modarg
1289 }
1290 }
1291
1292 if {$mlspec} {
1293 return [list $unarglist $arglist]
1294 } else {
1295 return $arglist
1296 }
1297
1298 }
1299 proc parseModuleSpecificationProcAdvVersSpec {mlspec nonamespec xtspec\
1300 getavails args} {
1301 foreach arg $args {
1302 if {![info exists ::g_moduleVersSpec($arg)]} {
1303 set need_parse 1
1304 break
1305 }
1306 }
1307 if {![info exists need_parse]} {
1308 return $args
1309 }
1310
1311 # define extra specifier known list, to raise error if argument does not
1312 # match
1313 set xtelt_valid_list [list always-load append-path chdir complete conflict\
1314 depends-on depends-on-any envvar family incompat load load-any\
1315 prepend-path prereq prereq-all prereq-any provide provided-alias\
1316 pushenv remove-path require set-alias set-function setenv switch\
1317 switch-on switch-off tag try-load uncomplete unload unset-alias\
1318 unset-function unsetenv use variant]
1319 set xtelt_modspec_list [list always-load conflict depends-on\
1320 depends-on-any incompat load load-any prereq prereq-all prereq-any\
1321 require switch switch-on switch-off try-load unload]
1322
1323 set mlunload 0
1324 set nextmlunload 0
1325 set arglist [list]
1326 set unarglist [list]
1327 set vrlist [list]
1328 set vridx -1
1329 set xtlist [list]
1330 set rawarg [list]
1331 foreach arg $args {
1332 # set each specification element as separate word but preserve space
1333 # character in each arg
1334 set curarglist {}
1335 # skip argument split if extra specifier detected
1336 if {[regexp {^[a-z-]+:} $arg]} {
1337 lappend curarglist $arg
1338 } else {
1339 set split_chars {@ ~ +}
1340 lappend split_chars {*}[array names ::g_shortcutVariant]
1341 set split_arg_list {}
1342 set previ 0
1343 for {set i 1} {$i < [string length $arg]} {incr i} {
1344 if {[string index $arg $i] in $split_chars} {
1345 lappend split_arg_list [string range $arg $previ $i-1]
1346 set previ $i
1347 }
1348 }
1349 lappend split_arg_list [string range $arg $previ $i-1]
1350
1351 # unsplit some arg parts: those starting with boolean variant prefix
1352 # (+ or ~) but not followed by a valid variant name
1353 set prev_arg [lindex $split_arg_list 0]
1354 for {set i 1} {$i < [llength $split_arg_list]} {incr i} {
1355 set split_arg [lindex $split_arg_list $i]
1356 if {[string index $split_arg 0] ni {+ ~} || [isVariantNameValid\
1357 [string range $split_arg 1 end]]} {
1358 lappend curarglist $prev_arg
1359 set prev_arg $split_arg
1360 } else {
1361 append prev_arg $split_arg
1362 }
1363 }
1364 lappend curarglist $prev_arg
1365 }
1366
1367 # parse each specification element
1368 foreach curarg $curarglist {
1369 set vrisbool 0
1370
1371 set xtnot 0
1372 if {[string equal -length 4 $curarg not:]} {
1373 if {!$xtspec} {
1374 knerror "No extra specification allowed on this command"
1375 }
1376 set xtnot 1
1377 set curarg [string range $curarg 4 end]
1378 }
1379
1380 set c [string index $curarg 0]
1381 switch -- $c {
1382 @ {
1383 if {$xtnot} {
1384 knerror "Invalid extra specification '$arg'"
1385 }
1386 set modspec [string range $curarg 1 end]
1387 lassign [parseModuleVersionSpecifier $modspec] cmpspec versspec
1388 continue
1389 }
1390 + {
1391 set curarg [string range $curarg 1 end]
1392 append curarg =1
1393 set vrisbool 1
1394 }
1395 - {
1396 set curarg [string range $curarg 1 end]
1397 if {$mlspec} {
1398 set nextmlunload 1
1399 } else {
1400 append curarg =0
1401 set vrisbool 1
1402 }
1403 }
1404 ~ {
1405 set curarg [string range $curarg 1 end]
1406 append curarg =0
1407 set vrisbool 1
1408 }
1409 default {
1410 # translate shortcut in variant name in arg
1411 if {[info exists ::g_shortcutVariant($c)]} {
1412 set curarg [string replace $curarg 0 0\
1413 $::g_shortcutVariant($c)=]
1414 }
1415 }
1416 }
1417
1418 switch -glob -- $curarg {
1419 *:* {
1420 # extra specification may not be accepted on current context
1421 if {!$xtspec} {
1422 knerror "No extra specification allowed on this command"
1423 }
1424 # extract extra specifier spec
1425 set xtsepidx [string first : $curarg]
1426 set xtelt [string range $curarg 0 $xtsepidx-1]
1427 set xtnamelist [split [string range $curarg $xtsepidx+1 end] ,]
1428
1429 # check no other : character is found in argument or element
1430 # and name are not an empty string
1431 if {![string length $xtelt] || ![llength $xtnamelist] || {} in\
1432 $xtnamelist || ([string last : $curarg] != $xtsepidx &&\
1433 $xtelt ni $xtelt_modspec_list)} {
1434 knerror "Invalid extra specification '$arg'"
1435 }
1436 if {$xtelt ni $xtelt_valid_list} {
1437 knerror "Invalid extra specifier '$xtelt'\nValid extra\
1438 specifiers are: $xtelt_valid_list"
1439 }
1440 set spec_xt [list $xtelt $xtnot]
1441 # parse and resolve module spec set as extra specifier value
1442 if {$xtelt in $xtelt_modspec_list} {
1443 foreach xtname $xtnamelist {
1444 lassign [parseModuleSpecification 0 0 0 1 {*}$xtname]\
1445 xtname
1446 lappend spec_xt $xtname
1447 }
1448 } else {
1449 # For tag extra specifier, resolve each name to get
1450 # corresponding tag name if tag abbreviation set as name
1451 if {$xtelt eq {tag}} {
1452 foreach xtname $xtnamelist {
1453 lappend spec_xt $xtname
1454 if {[set tag [getTagFromAbbrev $xtname]] ne {}} {
1455 lappend spec_xt $tag
1456 }
1457 }
1458 } else {
1459 lappend spec_xt {*}$xtnamelist
1460 }
1461 }
1462 # save extra specifier element and name value, multiple values
1463 # may be set (means OR operator), same element can appear
1464 # multiple time (means AND operator)
1465 lappend xtlist $spec_xt
1466 }
1467 *=* {
1468 # extract valued-variant spec
1469 set vrsepidx [string first = $curarg]
1470 set vrname [string range $curarg 0 $vrsepidx-1]
1471 set vrvaluelist [split [string range $curarg $vrsepidx+1 end]\
1472 ,]
1473 # value is one empty string
1474 if {![llength $vrvaluelist]} {
1475 lappend vrvaluelist {}
1476 }
1477
1478 if {$vrname eq {}} {
1479 knerror "No variant name defined in argument '$curarg'"
1480 }
1481 # check no other = character is found in argument and that only
1482 # one value is set unless if extra specifier search is enabled
1483 if {[string last = $curarg] != $vrsepidx || (!$xtspec &&\
1484 [llength $vrvaluelist] > 1)} {
1485 knerror "Invalid variant specification '$arg'"
1486 }
1487 # replace previous value for variant if already set unless if
1488 # extra specifier search enabled where all variant spec forms
1489 # an AND operation
1490 if {[info exists vrnamearr($vrname)] && !$xtspec} {
1491 lreplace $vrlist $vrnamearr($vrname) $vrnamearr($vrname)
1492 } else {
1493 incr vridx
1494 }
1495 # translate boolean vrvalue in canonical boolean
1496 if {!$vrisbool} {
1497 set vrisbool 1
1498 for {set i 0} {$i < [llength $vrvaluelist]} {incr i 1} {
1499 set vrvalue [lindex $vrvaluelist $i]
1500 if {[string is boolean -strict $vrvalue] && ![string is\
1501 integer -strict $vrvalue]} {
1502 lset vrvaluelist $i [string is true -strict $vrvalue]
1503 } else {
1504 # consider variant not a boolean as soon as one value
1505 # set is not a boolean
1506 set vrisbool 0
1507 }
1508 }
1509 }
1510 # save variant name and value
1511 set vrnamearr($vrname) $vridx
1512 lappend vrlist [list $vrname $xtnot $vrisbool {*}$vrvaluelist]
1513 }
1514 default {
1515 if {$xtnot} {
1516 knerror "Invalid extra specification '$arg'"
1517 }
1518 # save previous mod version spec and transformed arg if any
1519 if {[info exists modarglist]} {
1520 set modarg [join $modarglist]
1521 if {![info exists cmpspec]} {
1522 set cmpspec eq
1523 set versspec {}
1524 }
1525 # wild search name if no module name allowed
1526 if {$nonamespec && ![info exists modname]} {
1527 set modname *
1528 set modspec *
1529 }
1530 if {[info exists modname] && ($modname ne {} || $modspec\
1531 eq {})} {
1532 setModuleVersSpec $modarg $modname $cmpspec $versspec\
1533 $modspec $vrlist $rawarg $xtlist $getavails
1534 # rework args to have 1 str element for whole mod spec
1535 # append to unload list if ml spec and - prefix used
1536 if {$mlunload} {
1537 lappend unarglist $modarg
1538 } else {
1539 lappend arglist $modarg
1540 }
1541 } else {
1542 knerror "No module name defined in argument '$modarg'"
1543 }
1544 unset modarglist
1545 set vrlist [list]
1546 array unset vrnamearr
1547 set vridx -1
1548 set xtlist [list]
1549 set rawarg [list]
1550 unset cmpspec versspec
1551 }
1552 set mlunload $nextmlunload
1553 set nextmlunload 0
1554 set modname $curarg
1555 set modspec {}
1556 }
1557 }
1558 }
1559 lappend rawarg $arg
1560
1561 # keep arg enclosed if composed of several words
1562 if {[string first { } $arg] != -1} {
1563 lappend modarglist "{$arg}"
1564 } else {
1565 lappend modarglist $arg
1566 }
1567 }
1568 # transform last args
1569 set modarg [join $modarglist]
1570 if {$nonamespec && ![info exists modname]} {
1571 set modname *
1572 set modspec *
1573 }
1574 if {[info exists modname] && ($modname ne {} || $modspec eq {})} {
1575 if {![info exists cmpspec]} {
1576 set cmpspec eq
1577 set versspec {}
1578 }
1579 setModuleVersSpec $modarg $modname $cmpspec $versspec $modspec $vrlist\
1580 $rawarg $xtlist $getavails
1581 # rework args to have 1 string element for whole module spec
1582 # append to unload list if ml spec and - prefix used
1583 if {$mlunload || $nextmlunload} {
1584 lappend unarglist $modarg
1585 } else {
1586 lappend arglist $modarg
1587 }
1588 } else {
1589 knerror "No module name defined in argument '$modarg'"
1590 }
1591
1592 if {$mlspec} {
1593 return [list $unarglist $arglist]
1594 } else {
1595 return $arglist
1596 }
1597 }
1598
1599 proc setModuleVersSpec {modarg modname cmpspec versspec rawversspec\
1600 variantlist rawarg extralist getavails} {
1601 # translate @loaded version into currently loaded mod matching modname
1602 if {$cmpspec eq {eq} && $versspec eq {loaded}} {
1603 if {[set lmmod [getLoadedMatchingName $modname]] ne {}} {
1604 set modname [file dirname $lmmod]
1605 set versspec [file tail $lmmod]
1606 set variantlist [getVariantList $lmmod 2]
1607 } else {
1608 knerror "No loaded version found for '$modname' module"
1609 }
1610 }
1611 # save module root name
1612 set modroot [lindex [file split $modname] 0]
1613 # save module single designation if any and module name
1614 if {$versspec eq {}} {
1615 set mod $modname
1616 set modname [file dirname $modname]
1617 } else {
1618 set modname [string trimright $modname /]
1619 if {$cmpspec ne {eq}} {
1620 set mod {}
1621 } else {
1622 set mod $modname/$versspec
1623 }
1624 }
1625 # save a regexp-ready version of modname (apply
1626 # non-greedy quantifier to '*', to avoid matching final
1627 # '/' in string comparison
1628 set modnamere [string map {. \\. + \\+ * .*? ? .} $modname]
1629 if {$modname eq $modnamere} {
1630 set modnamere {}
1631 }
1632 # save a glob-special-chars escaped version of mod
1633 set modescglob [string map {* \\* ? \\?} $mod]
1634
1635 # save module name and version specification (without variant specs)
1636 if {$mod eq {} && $rawversspec ne {} && $modname ne {.}} {
1637 set modnvspec ${modname}@${rawversspec}
1638 } else {
1639 set modnvspec $mod
1640 }
1641
1642 # record most of the spec now to be able to rely on this record to get
1643 # matching available modules
1644 set ::g_moduleVersSpec($modarg) [list $mod $modname $cmpspec $versspec\
1645 $modnamere $modescglob $modroot $variantlist $modnvspec $rawarg\
1646 $extralist]
1647
1648 # get all available module name and version matching module name and
1649 # version spec (inhibit extra match search not to trigger an infinite loop)
1650 if {$getavails} {
1651 setState inhibit_ems 1
1652 set availlist [getAllAvailModule $modarg]
1653 setState inhibit_ems 0
1654 } else {
1655 set availlist {}
1656 }
1657 # finalize module version spec record
1658 lappend ::g_moduleVersSpec($modarg) $availlist
1659
1660 reportDebug "Set module '$mod' (escglob '$modescglob'), module name\
1661 '$modname' (re '$modnamere'), module root '$modroot', version cmp\
1662 '$cmpspec', version(s) '$versspec', variant(s) '$variantlist' and\
1663 module name version spec '$modnvspec' for argument '$modarg' (raw\
1664 '$rawarg'), extra specifier(s) '$extralist' and matching available\
1665 module(s) '$availlist'"
1666 }
1667
1668 proc getModuleVersSpec {modarg} {
1669 if {[info exists ::g_moduleVersSpec($modarg)]} {
1670 return $::g_moduleVersSpec($modarg)
1671 } else {
1672 return [list $modarg [file dirname $modarg] {} {} {} [string map {* \\*\
1673 ? \\?} $modarg] [lindex [file split $modarg] 0] {} $modarg $modarg\
1674 {} {}]
1675 }
1676 }
1677
1678 proc unsetModuleVersSpec {modarg} {
1679 if {[info exists ::g_moduleVersSpec($modarg)]} {
1680 unset ::g_moduleVersSpec($modarg)
1681 }
1682 }
1683
1684 # get module name from module name and version spec if parsed
1685 proc getModuleNameFromVersSpec {modarg} {
1686 if {[info exists ::g_moduleVersSpec($modarg)]} {
1687 return [lindex $::g_moduleVersSpec($modarg) 1]
1688 } else {
1689 return [file dirname $modarg]
1690 }
1691 }
1692
1693 proc getCmpSpecFromVersSpec {modarg} {
1694 if {[info exists ::g_moduleVersSpec($modarg)]} {
1695 return [lindex $::g_moduleVersSpec($modarg) 2]
1696 } else {
1697 return eq
1698 }
1699 }
1700 # get module root name from module name and version spec if parsed
1701 proc getModuleRootFromVersSpec {modarg} {
1702 if {[info exists ::g_moduleVersSpec($modarg)]} {
1703 return [lindex $::g_moduleVersSpec($modarg) 6]
1704 } else {
1705 return [lindex [file split $modarg] 0]
1706 }
1707 }
1708
1709 # translate module name version spec to return all modules mentioned
1710 proc getAllModulesFromVersSpec {modarg} {
1711 if {[info exists ::g_moduleVersSpec($modarg)]} {
1712 lassign $::g_moduleVersSpec($modarg) mod modname cmpspec versspec
1713 if {$mod eq {} && $cmpspec eq {in}} {
1714 # loop around each spec in list
1715 foreach inspec $versspec {
1716 lassign $inspec incmp invers
1717 foreach vers $invers {
1718 lappend modlist $modname/$vers
1719 }
1720 }
1721 } elseif {$mod eq {} && $cmpspec ne {eq}} {
1722 foreach vers $versspec {
1723 lappend modlist $modname/$vers
1724 }
1725 } else {
1726 # add empty mod specification if cmpspec is 'eq'
1727 lappend modlist $mod
1728 }
1729 } else {
1730 lappend modlist $modarg
1731 }
1732
1733 return $modlist
1734 }
1735
1736 # translate module name version spec to return one module mentioned
1737 proc getOneModuleFromVersSpec {modarg} {
1738 if {[info exists ::g_moduleVersSpec($modarg)]} {
1739 lassign $::g_moduleVersSpec($modarg) mod modname cmpspec versspec
1740 if {$mod eq {} && $cmpspec eq {in}} {
1741 set inspec [lindex $versspec 0]
1742 lassign $inspec incmp invers
1743 set mod $modname/[lindex $invers 0]
1744 } elseif {$mod eq {} && $cmpspec ne {eq}} {
1745 set mod $modname/[lindex $versspec 0]
1746 }
1747 } else {
1748 set mod $modarg
1749 }
1750
1751 return $mod
1752 }
1753
1754 # translate module name version spec to return the list of variant mentioned
1755 proc getVariantListFromVersSpec {modarg} {
1756 if {[info exists ::g_moduleVersSpec($modarg)]} {
1757 return [lindex $::g_moduleVersSpec($modarg) 7]
1758 }
1759 }
1760
1761 # get module name and version from version spec if parsed
1762 proc getModuleNameAndVersFromVersSpec {modarg} {
1763 if {[info exists ::g_moduleVersSpec($modarg)]} {
1764 return [lindex $::g_moduleVersSpec($modarg) 8]
1765 } else {
1766 return $modarg
1767 }
1768 }
1769
1770 # get raw argument specified from parsed version spec
1771 proc getRawArgumentFromVersSpec {modarg} {
1772 if {[info exists ::g_moduleVersSpec($modarg)]} {
1773 return [lindex $::g_moduleVersSpec($modarg) 9]
1774 } else {
1775 return $modarg
1776 }
1777 }
1778
1779 # translate module name version spec to return the list of extra specifier
1780 # mentioned
1781 proc getExtraListFromVersSpec {modarg} {
1782 if {[info exists ::g_moduleVersSpec($modarg)]} {
1783 return [lindex $::g_moduleVersSpec($modarg) 10]
1784 }
1785 }
1786
1787 # translate module name version spec to return list of tag specifier and list
1788 # of other extra specifier
1789 proc getSplitExtraListFromVersSpec {modarg} {
1790 set taglist {}
1791 set otherlist {}
1792 if {[info exists ::g_moduleVersSpec($modarg)]} {
1793 foreach spec_xt [lindex $::g_moduleVersSpec($modarg) 10] {
1794 if {[lindex $spec_xt 0] eq {tag}} {
1795 lappend taglist $spec_xt
1796 } else {
1797 lappend otherlist $spec_xt
1798 }
1799 }
1800 }
1801 return [list $taglist $otherlist]
1802 }
1803
1804 # get available modules matching module specification
1805 proc getAvailListFromVersSpec {modarg} {
1806 if {[info exists ::g_moduleVersSpec($modarg)]} {
1807 return [lindex $::g_moduleVersSpec($modarg) 11]
1808 }
1809 }
1810
1811 # is mod spec containing variant but no module name and version
1812 proc isSpecWildWithVariant {mod} {
1813 return [expr {[getModuleNameAndVersFromVersSpec $mod] eq {*} && [llength\
1814 [getVariantListFromVersSpec $mod]]}]
1815 }
1816
1817 # ;;; Local Variables: ***
1818 # ;;; mode:tcl ***
1819 # ;;; End: ***
1820 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent: