Skip to content

Commit

Permalink
Define stricter naming rules for variants
Browse files Browse the repository at this point in the history
Instead of just requiring that first character of variant name should
correspond to the [A-Za-z0-9_] range, now variant names should only be
made of [A-Za-z0-9_-] characters. First character of these names cannot
be "-" and full name cannot be a number.

Adapt module specification parsing not to consider string parts
beginning with boolean variant prefix "+" and "~" as a variant
specification if the string after the prefix does not corresponds to a
valid variant name.

With this change, "/path/to/modul+files/foo/1.0" and "java/17.0.2+8" are
now parsed as only a module name and version rather also including a
boolean variant specification.

Fixes #527
  • Loading branch information
xdelaruelle committed Jun 13, 2024
1 parent e7a6e5c commit ce82cee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
7 changes: 6 additions & 1 deletion tcl/mfcmd.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,11 @@ proc source-sh-di {args} {
}
}

proc isVariantNameValid {name} {
return [expr {![string is digit -strict $name] && [regexp\
{^[A-Za-z0-9_][A-Za-z0-9_-]*$} $name]}]
}

# parse arguments set on a variant modulefile command
proc parseVariantCommandArgs {args} {
set dflvalue {}
Expand Down Expand Up @@ -1944,7 +1949,7 @@ proc parseVariantCommandArgs {args} {
if {![info exists name]} {
knerror {No variant name specified}
}
if {![string match {[A-Za-z0-9_]} [string index $name 0]]} {
if {![isVariantNameValid $name]} {
knerror "Invalid variant name '$name'"
}
set values [lrange $args $i end]
Expand Down
50 changes: 22 additions & 28 deletions tcl/modspec.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -1324,38 +1324,32 @@ proc parseModuleSpecificationProcAdvVersSpec {mlspec nonamespec xtspec\
if {[regexp {^[a-z-]+:} $arg]} {
lappend curarglist $arg
} else {
set split_chars {@ ~ +}
lappend split_chars {*}[array names ::g_shortcutVariant]
set split_arg_list {}
set previ 0
for {set i 1} {$i < [string length $arg]} {incr i} {
set c [string index $arg $i]
switch -- $c {
@ - ~ {
lappend curarglist [string range $arg $previ $i-1]
set previ $i
}
+ {
# allow one or more '+' char at end of module name if not
# followed by non-special character (@, ~ or /)
set nexti [expr {$i + 1}]
if {$nexti < [string length $arg]} {
switch -- [string index $arg $nexti] {
@ - + - ~ - / {}
default {
lappend curarglist [string range $arg $previ $i-1]
set previ $i
}
}
}
}
default {
# check if a variant shortcut matches
if {[info exists ::g_shortcutVariant($c)]} {
lappend curarglist [string range $arg $previ $i-1]
set previ $i
}
}
if {[string index $arg $i] in $split_chars} {
lappend split_arg_list [string range $arg $previ $i-1]
set previ $i
}
}
lappend split_arg_list [string range $arg $previ $i-1]

# unsplit some arg parts: those starting with boolean variant prefix
# (+ or ~) but not followed by a valid variant name
set prev_arg [lindex $split_arg_list 0]
for {set i 1} {$i < [llength $split_arg_list]} {incr i} {
set split_arg [lindex $split_arg_list $i]
if {[string index $split_arg 0] ni {+ ~} || [isVariantNameValid\
[string range $split_arg 1 end]]} {
lappend curarglist $prev_arg
set prev_arg $split_arg
} else {
append prev_arg $split_arg
}
}
lappend curarglist [string range $arg $previ $i-1]
lappend curarglist $prev_arg
}

# parse each specification element
Expand Down

0 comments on commit ce82cee

Please sign in to comment.