| 1 |
########################################################################## |
| 2 |
|
| 3 |
# UTIL.TCL, utility procedures |
| 4 |
# Copyright (C) 2002-2004 Mark Lakata |
| 5 |
# Copyright (C) 2004-2017 Kent Mein |
| 6 |
# Copyright (C) 2016-2025 Xavier Delaruelle |
| 7 |
# |
| 8 |
# This program is free software: you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU General Public License as published by |
| 10 |
# the Free Software Foundation, either version 2 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# This program is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU General Public License |
| 19 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
########################################################################## |
| 22 |
|
| 23 |
proc charEscaped {str {charlist " \\\\\t{}\\\[\\\]|<>!;#^\$&*?\"'`()"}} { |
| 24 |
return [regsub -all "\(\[$charlist\]\)" $str {\\\1}] |
| 25 |
} |
| 26 |
|
| 27 |
proc charUnescaped {str {charlist " \\\\\t{}\\\[\\\]|<>!;#^\$&*?\"'`()"}} { |
| 28 |
return [regsub -all "\\\\\(\[$charlist\]\)" $str {\1}] |
| 29 |
} |
| 30 |
|
| 31 |
proc escapeGlobChars {str} { |
| 32 |
return [charEscaped $str {*?[]\\}] |
| 33 |
} |
| 34 |
|
| 35 |
proc strTo {lang str {esc 1}} { |
| 36 |
switch -- $lang { |
| 37 |
tcl { set enco \{; set encc \}} |
| 38 |
shell { set enco '; set encc '} |
| 39 |
} |
| 40 |
# escape all special characters |
| 41 |
if {$esc} { |
| 42 |
set str [charEscaped $str] |
| 43 |
} |
| 44 |
# enclose if empty or if contain a space character unless already escaped |
| 45 |
if {$str eq {} || (!$esc && [regexp {\s} $str])} { |
| 46 |
set str "$enco$str$encc" |
| 47 |
} |
| 48 |
return $str |
| 49 |
} |
| 50 |
|
| 51 |
proc listTo {lang lst {esc 1}} { |
| 52 |
set lout [list] |
| 53 |
# transform each list element |
| 54 |
foreach str $lst { |
| 55 |
lappend lout [strTo $lang $str $esc] |
| 56 |
} |
| 57 |
return [join $lout { }] |
| 58 |
} |
| 59 |
|
| 60 |
# find command path and remember it |
| 61 |
proc getCommandPath {cmd} { |
| 62 |
return [lindex [auto_execok $cmd] 0] |
| 63 |
} |
| 64 |
|
| 65 |
# find then run command or raise error if command not found |
| 66 |
proc runCommand {cmd args} { |
| 67 |
set cmdpath [getCommandPath $cmd] |
| 68 |
if {$cmdpath eq {}} { |
| 69 |
knerror "Command '$cmd' cannot be found" MODULES_ERR_GLOBAL |
| 70 |
} else { |
| 71 |
return [exec $cmdpath {*}$args] |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
proc getAbsolutePath {path} { |
| 76 |
# currently executing a modulefile or rc, so get the directory of this file |
| 77 |
if {[currentState modulefile] ne {}} { |
| 78 |
set curdir [file dirname [currentState modulefile]] |
| 79 |
# elsewhere get module command current working directory |
| 80 |
} else { |
| 81 |
# register pwd at first call |
| 82 |
if {![isStateDefined cwd]} { |
| 83 |
# raise a global known error if cwd cannot be retrieved (especially |
| 84 |
# when this directory has been removed) |
| 85 |
if {[catch {setState cwd [pwd]} errorMsg]} { |
| 86 |
knerror $errorMsg |
| 87 |
} |
| 88 |
} |
| 89 |
set curdir [getState cwd] |
| 90 |
} |
| 91 |
|
| 92 |
# empty result if empty path |
| 93 |
if {$path eq {}} { |
| 94 |
set abspath {} |
| 95 |
# consider path absolute if it starts with a variable ref |
| 96 |
} elseif {[string index $path 0] eq {$}} { |
| 97 |
set abspath $path |
| 98 |
} else { |
| 99 |
set abslist {} |
| 100 |
# get a first version of the absolute path by joining the current |
| 101 |
# working directory to the given path. if given path is already absolute |
| 102 |
# 'file join' will not break it as $curdir will be ignored as soon a |
| 103 |
# beginning '/' character is found on $path. this first pass also clean |
| 104 |
# extra '/' character. then each element of the path is analyzed to |
| 105 |
# clear "." and ".." components. |
| 106 |
foreach elt [file split [file join $curdir $path]] { |
| 107 |
if {$elt eq {..}} { |
| 108 |
# skip ".." element if it comes after root element, remove last |
| 109 |
# element elsewhere |
| 110 |
if {[llength $abslist] > 1} { |
| 111 |
set abslist [lreplace $abslist end end] |
| 112 |
} |
| 113 |
# skip any "." element |
| 114 |
} elseif {$elt ne {.}} { |
| 115 |
lappend abslist $elt |
| 116 |
} |
| 117 |
} |
| 118 |
set abspath [file join {*}$abslist] |
| 119 |
} |
| 120 |
|
| 121 |
# return cleaned absolute path |
| 122 |
return $abspath |
| 123 |
} |
| 124 |
|
| 125 |
# if no exact match found but icase mode is enabled then search if an icase |
| 126 |
# match exists among all array key elements, select dictionary highest version |
| 127 |
# if multiple icase matches are returned |
| 128 |
proc getArrayKey {arrname name icase} { |
| 129 |
if {$icase} { |
| 130 |
upvar $arrname arr |
| 131 |
if {![info exists arr($name)]} { |
| 132 |
foreach elt [lsort -dictionary -decreasing [array names arr]] { |
| 133 |
if {[string equal -nocase $name $elt]} { |
| 134 |
reportDebug "key '$elt' in array '$arrname' matches '$name'" |
| 135 |
set name $elt |
| 136 |
break |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
return $name |
| 142 |
} |
| 143 |
|
| 144 |
# split string while ignore any separator character that is escaped |
| 145 |
proc psplit {str sep} { |
| 146 |
# use standard split if no sep character found |
| 147 |
if {[string first \\$sep $str] == -1} { |
| 148 |
set res [split $str $sep] |
| 149 |
} else { |
| 150 |
set previdx -1 |
| 151 |
set idx [string first $sep $str] |
| 152 |
while {$idx != -1} { |
| 153 |
# look ahead if found separator is escaped |
| 154 |
if {[string index $str $idx-1] ne "\\"} { |
| 155 |
# unescape any separator character when adding to list |
| 156 |
lappend res [charUnescaped [string range $str $previdx+1 $idx-1]\ |
| 157 |
$sep] |
| 158 |
set previdx $idx |
| 159 |
} |
| 160 |
set idx [string first $sep $str $idx+1] |
| 161 |
} |
| 162 |
|
| 163 |
lappend res [charUnescaped [string range $str $previdx+1 end] $sep] |
| 164 |
} |
| 165 |
|
| 166 |
return $res |
| 167 |
} |
| 168 |
|
| 169 |
# join list while escape any character equal to separator |
| 170 |
proc pjoin {lst sep} { |
| 171 |
# use standard join if no sep character found |
| 172 |
if {[string first $sep $lst] == -1} { |
| 173 |
set res [join $lst $sep] |
| 174 |
} else { |
| 175 |
set res {} |
| 176 |
foreach elt $lst { |
| 177 |
# preserve empty entries |
| 178 |
if {[info exists not_first]} { |
| 179 |
append res $sep |
| 180 |
} else { |
| 181 |
set not_first 1 |
| 182 |
} |
| 183 |
# escape any separator character when adding to string |
| 184 |
append res [charEscaped $elt $sep] |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
return $res |
| 189 |
} |
| 190 |
|
| 191 |
# Is provided string a version number: consider first element of string if |
| 192 |
# '.' character used in it. [0-9af] on this first part is considered valid |
| 193 |
# anything else could be used in latter elements |
| 194 |
proc isVersion {str} { |
| 195 |
return [string is xdigit -strict [lindex [split $str .] 0]] |
| 196 |
} |
| 197 |
|
| 198 |
# Return number of occurrences of passed character in passed string |
| 199 |
proc countChar {str char} { |
| 200 |
return [expr {[string length $str] - [string length [string map [list\ |
| 201 |
$char {}] $str]]}] |
| 202 |
} |
| 203 |
|
| 204 |
proc appendNoDupToList {lstname args} { |
| 205 |
set ret 0 |
| 206 |
upvar $lstname lst |
| 207 |
foreach elt $args { |
| 208 |
if {![info exists lst] || $elt ni $lst} { |
| 209 |
lappend lst $elt |
| 210 |
set ret 1 |
| 211 |
} |
| 212 |
} |
| 213 |
return $ret |
| 214 |
} |
| 215 |
|
| 216 |
proc replaceFromList {list1 item {item2 {}}} { |
| 217 |
while {[set xi [lsearch -exact $list1 $item]] >= 0} { |
| 218 |
##nagelfar ignore #2 Badly formed if statement |
| 219 |
set list1 [if {![string length $item2]} {lreplace $list1 $xi $xi}\ |
| 220 |
{lreplace $list1 $xi $xi $item2}] |
| 221 |
} |
| 222 |
|
| 223 |
return $list1 |
| 224 |
} |
| 225 |
|
| 226 |
proc lprepend {lst_name args} { |
| 227 |
upvar $lst_name lst |
| 228 |
if {[info exists lst]} { |
| 229 |
set lst [list {*}$args {*}$lst] |
| 230 |
} else { |
| 231 |
set lst $args |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
# test if 2 lists have at least one element in common |
| 236 |
proc isIntBetweenList {list1 list2} { |
| 237 |
foreach elt $list1 { |
| 238 |
if {$elt in $list2} { |
| 239 |
return 1 |
| 240 |
} |
| 241 |
} |
| 242 |
return 0 |
| 243 |
} |
| 244 |
|
| 245 |
# test if 2 lists have at least one element in diff |
| 246 |
proc isDiffBetweenList {list1 list2} { |
| 247 |
foreach elt $list1 { |
| 248 |
if {$elt ni $list2} { |
| 249 |
return 1 |
| 250 |
} |
| 251 |
} |
| 252 |
return 0 |
| 253 |
} |
| 254 |
|
| 255 |
# returns elements from list1 not part of list2 and elements from list2 not |
| 256 |
# part of list1 |
| 257 |
proc getDiffBetweenList {list1 list2} { |
| 258 |
set res1 [list] |
| 259 |
set res2 [list] |
| 260 |
|
| 261 |
foreach elt $list1 { |
| 262 |
if {$elt ni $list2} { |
| 263 |
lappend res1 $elt |
| 264 |
} |
| 265 |
} |
| 266 |
foreach elt $list2 { |
| 267 |
if {$elt ni $list1} { |
| 268 |
lappend res2 $elt |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
return [list $res1 $res2] |
| 273 |
} |
| 274 |
|
| 275 |
# return intersection of all lists: elements present in every list |
| 276 |
proc getIntersectBetweenList {args} { |
| 277 |
foreach lst $args { |
| 278 |
if {![info exists res]} { |
| 279 |
set cur_res $lst |
| 280 |
} else { |
| 281 |
set cur_res [list] |
| 282 |
foreach elt $res { |
| 283 |
if {$elt in $lst} { |
| 284 |
lappend cur_res $elt |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
set res $cur_res |
| 289 |
# stop when intersection result becomes empty |
| 290 |
if {![llength $res]} { |
| 291 |
break |
| 292 |
} |
| 293 |
} |
| 294 |
return $res |
| 295 |
} |
| 296 |
|
| 297 |
# return elements from arr1 not in arr2, elements from arr1 in arr2 but with a |
| 298 |
# different value and elements from arr2 not in arr1. |
| 299 |
# if notset_equals_empty is enabled, not-set element in array is equivalent to |
| 300 |
# element set to an empty value. |
| 301 |
# if unordered_lists_compared is enabled, value of array element is considered |
| 302 |
# a list and difference between list entries is made (order insensitive) |
| 303 |
proc getDiffBetweenArray {arrname1 arrname2 {notset_equals_empty 0}\ |
| 304 |
{unordered_lists_compared 0}} { |
| 305 |
upvar $arrname1 arr1 |
| 306 |
upvar $arrname2 arr2 |
| 307 |
set notin2 [list] |
| 308 |
set diff [list] |
| 309 |
set notin1 [list] |
| 310 |
|
| 311 |
foreach name [array names arr1] { |
| 312 |
# element in arr1 not in arr2 |
| 313 |
if {![info exists arr2($name)]} { |
| 314 |
if {!$notset_equals_empty} { |
| 315 |
lappend notin2 $name |
| 316 |
# if we consider a not-set entry equal to an empty value, there is a |
| 317 |
# difference only if entry in the other array is not empty |
| 318 |
} elseif {$arr1($name) ne {}} { |
| 319 |
lappend diff $name |
| 320 |
} |
| 321 |
# element present in both arrays but with a different value |
| 322 |
} elseif {!$unordered_lists_compared} { |
| 323 |
# but with a different value |
| 324 |
if {$arr1($name) ne $arr2($name)} { |
| 325 |
lappend diff $name |
| 326 |
} |
| 327 |
} else { |
| 328 |
# with a different value, not considering order |
| 329 |
lassign [getDiffBetweenList $arr1($name) $arr2($name)] notin2 notin1 |
| 330 |
if {[llength $notin2] || [llength $notin1]} { |
| 331 |
lappend diff $name |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
foreach name [array names arr2] { |
| 337 |
# element in arr2 not in arr1 |
| 338 |
if {![info exists arr1($name)]} { |
| 339 |
if {!$notset_equals_empty} { |
| 340 |
lappend notin1 $name |
| 341 |
} elseif {$arr2($name) ne {}} { |
| 342 |
lappend diff $name |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
return [list $notin2 $diff $notin1] |
| 348 |
} |
| 349 |
|
| 350 |
proc getCallingProcName {} { |
| 351 |
if {[info level] > 2} { |
| 352 |
return [lindex [info level -2] 0] |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
# ;;; Local Variables: *** |
| 357 |
# ;;; mode:tcl *** |
| 358 |
# ;;; End: *** |
| 359 |
# vim:set tabstop=3 shiftwidth=3 expandtab autoindent: |