Debian Codegraph @

tcl/modspec.tcl

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