VirtualBox

Changeset 85840 in vbox for trunk/tools/win/vbscript


Ignore:
Timestamp:
Aug 19, 2020 5:32:39 PM (4 years ago)
Author:
vboxsync
Message:

helper.vbs,configure.vbs: Added a bunch of new helpers and adjusted/renamed several old ones in preparation for a new envSub.vbs script.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/win/vbscript/helpers.vbs

    r85824 r85840  
    2727dim g_objFileSys
    2828Set g_objFileSys = WScript.CreateObject("Scripting.FileSystemObject")
     29
     30'' Whether to ignore (continue) on errors.
     31dim g_blnContinueOnError
     32g_blnContinueOnError = False
     33
     34'' The script's exit code (for ignored errors).
     35dim g_rcScript
     36g_rcScript = 0
    2937
    3038
     
    140148
    141149
     150''
     151' Compare two paths w/o abspathing them.
     152'
     153' Ignores case, slash direction, multiple slashes and single dot components.
     154'
     155function PathMatch(strPath1, strPath2)
     156   PathMatch = true
     157   if StrComp(strPath1, strPath2, vbTextCompare) <> 0 then
     158      strPath1 = DosSlashes(strPath1)
     159      strPath2 = DosSlashes(strPath2)
     160      if StrComp(strPath1, strPath2, vbTextCompare) <> 0 then
     161         ' Compare character by character
     162         dim off1 : off1 = 1
     163         dim off2 : off2 = 1
     164
     165         ' Compare UNC prefix if any, because the code below cannot handle it.  UNC has exactly two slashes.
     166         if Mid(strPath1, 1, 2) = "\\" and Mid(strPath2, 1, 2) = "\\" then
     167            if (Mid(strPath1, 3, 1) = "\") <> (Mid(strPath2, 3, 1) = "\") then
     168               PathMatch = false
     169               exit function
     170            end if
     171            off1 = off1 + 2
     172            off2 = off2 + 2
     173            if Mid(strPath1, 3, 1) = "\" then
     174               off1 = PathMatchSkipSlashesAndSlashDotHelper(strPath1, off1)
     175               off2 = PathMatchSkipSlashesAndSlashDotHelper(strPath2, off2)
     176            end if
     177         end if
     178
     179         ' Compare the rest.
     180         dim ch1, ch2
     181         do while off1 <= Len(strPath1) and off2 <= Len(strPath2)
     182            ch1 = Mid(strPath1, off1, 1)
     183            ch2 = Mid(strPath2, off2, 1)
     184            if StrComp(ch1, ch2, vbTextCompare) = 0 then
     185               off1 = off1 + 1
     186               off2 = off2 + 1
     187               if ch1 = "\" then
     188                  off1 = PathMatchSkipSlashesAndSlashDotHelper(strPath1, off1)
     189                  off2 = PathMatchSkipSlashesAndSlashDotHelper(strPath2, off2)
     190               end if
     191            else
     192               PathMatch = False
     193               exit function
     194            end if
     195         loop
     196
     197         ' One or both of the strings ran out.  That's fine if we've only got slashes
     198         ' and "." components left in the other.
     199         if off1 <= Len(strPath1) and Mid(strPath1, off1, 1) = "\" then
     200            off1 = PathMatchSkipSlashesAndSlashDotHelper(strPath1, off1 + 1)
     201         end if
     202         if off2 <= Len(strPath2) and Mid(strPath2, off2, 1) = "\" then
     203            off2 = PathMatchSkipSlashesAndSlashDotHelper(strPath2, off2 + 1)
     204         end if
     205         PathMatch = off1 > Len(strPath1) and off2 > Len(strPath2)
     206      end if
     207   end if
     208end function
     209
     210'' PathMatch helper
     211function PathMatchSkipSlashesAndSlashDotHelper(strPath, off)
     212   dim ch
     213   do while off <= Len(strPath)
     214      ch = Mid(strPath, off, 1)
     215      if ch = "\" then
     216         off = off + 1
     217      elseif ch = "." and off = Len(strPath) then
     218         off = off + 1
     219      elseif ch = "." and Mid(strPath, off, 2) = ".\" then
     220         off = off + 2
     221      else
     222         exit do
     223      end if
     224   loop
     225   PathMatchSkipSlashesAndSlashDotHelper = off
     226end function
     227
     228
    142229''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    143230'  Helpers: Files and Dirs                                                                                                       '
     
    393480
    394481''
     482' Gets an environment variable with default value if not found.
     483function EnvGetDef(strName, strDefault)
     484   dim strValue
     485   strValue = g_objShell.Environment("PROCESS")(strName)
     486   if strValue = "" or IsNull(strValue) or IsEmpty(strValue) then
     487      EnvGetDef = strDefault
     488   else
     489      EnvGetDef = strValue
     490   end if
     491end function
     492
     493
     494''
     495' Gets an environment variable with default value if not found or not
     496' in the array of valid values.  Issue warning about invalid values.
     497function EnvGetDefValid(strName, strDefault, arrValidValues)
     498   dim strValue
     499   strValue = g_objShell.Environment("PROCESS")(strName)
     500   if strValue = "" or IsNull(strValue) or IsEmpty(strValue) then
     501      EnvGetDefValid = strDefault
     502   elseif not ArrayContainsString(arrValidValues, strValue) then
     503      MsgWarning "Invalid value " & strName & " value '" & EnvGetDefValid & "', using '" & strDefault & "' instead."
     504      EnvGetDefValid = strDefault
     505   else
     506      EnvGetDefValid = strValue
     507   end if
     508end function
     509
     510
     511''
    395512' Sets an environment variable.
    396513sub EnvSet(strName, strValue)
     
    401518
    402519''
    403 ' Appends a string to an environment variable
    404 sub EnvAppend(strName, strValue)
    405    dim str
    406    str = g_objShell.Environment("PROCESS")(strName)
    407    g_objShell.Environment("PROCESS")(strName) =  str & strValue
    408    LogPrint "EnvAppend: " & strName & "=" & str & strValue
     520' Prepends a string to an Path-like environment variable.
     521function EnvPrependItemEx(strName, strItem, strSep, blnKeepEmpty, ByRef fnItemMatcher)
     522   dim strValue
     523   strValue = EnvRemoveItemEx(strName, strItem, strSep, blnKeepEmpty, fnItemMatcher, "EnvPrependItemEx")
     524   if strValue <> "" then
     525      strValue = strItem & strSep & strValue
     526   else
     527      strValue = strItem
     528   end if
     529   g_objShell.Environment("PROCESS")(strName) = strValue
     530   EnvPrependItemEx = strValue
     531end function
     532
     533
     534''
     535' Appends a string to an Path-like environment variable,
     536function EnvAppendItemEx(strName, strItem, strSep, blnKeepEmpty, ByRef fnItemMatcher)
     537   dim strValue
     538   strValue = EnvRemoveItemEx(strName, strItem, strSep, blnKeepEmpty, fnItemMatcher, "EnvAppendItemEx")
     539   if strValue <> "" then
     540      strValue = strValue & strSep & strItem
     541   else
     542      strValue = strItem
     543   end if
     544   g_objShell.Environment("PROCESS")(strName) = strValue
     545   EnvAppendItemEx = strValue
     546end function
     547
     548
     549''
     550' Generic item remover.
     551'
     552' fnItemMatcher(strItem1, strItem2)
     553'
     554function EnvRemoveItemEx(strName, strItem, strSep, blnKeepEmpty, ByRef fnItemMatcher, strCaller)
     555   dim strValue, off
     556   strValue = g_objShell.Environment("PROCESS")(strName)
     557   EnvRemoveItemEx = strValue
     558   if strValue <> "" then
     559      ' Split it up into an array of items
     560      dim   arrItems    : arrItems    = Split(strValue, strSep, -1, vbTextCompare)
     561
     562      ' Create an array of matching indexes that we should remove.
     563      dim   cntToRemove : cntToRemove = 0
     564      redim arrIdxToRemove(ArraySize(arrItems) - 1)
     565      dim   i, strCur
     566      for i = LBound(arrItems) to UBound(arrItems)
     567         strCur = arrItems(i)
     568         if fnItemMatcher(strCur, strItem) or (not blnKeepEmpty and strCur = "") then
     569            arrIdxToRemove(cntToRemove) = i
     570            cntToRemove = cntToRemove + 1
     571         end if
     572      next
     573
     574      ' Did we find anthing to remove?
     575      if cntToRemove > 0 then
     576         ' Update the array and join it up again.
     577         for i = cntToRemove - 1 to 0 step -1
     578            arrItems = ArrayRemove(arrItems, arrIdxToRemove(i))
     579         next
     580         dim strNewValue : strNewValue = ArrayJoinString(arrItems, strSep)
     581         EnvRemoveItemEx = strNewValue
     582
     583         ' Update the environment variable.
     584         LogPrint strCaller &": " & strName & ": '" & strValue & "' --> '" & strNewValue & "'"
     585         g_objShell.Environment("PROCESS")(strName) = strNewValue
     586      end if
     587   end if
     588end function
     589
     590
     591''
     592' Generic case-insensitive item matcher.
     593' See also PathMatch().
     594function EnvItemMatch(strItem1, strItem2)
     595   EnvItemMatch = (StrComp(strItem1, strItem2) = 0)
     596end function
     597
     598
     599''
     600' Prepends an item to an environment variable, after first removing any
     601' existing ones (case-insensitive, preserves empty elements).
     602function EnvPrependItem(strName, strItem, strSep)
     603   EnvPrependItem = EnvPrependItemEx(strName, strItem, strSep, true, GetRef("EnvItemMatch"))
     604   LogPrint "EnvPrependItem: " & strName & "=" & EnvPrependPathItem
     605end function
     606
     607
     608''
     609' Appends an item to an environment variable, after first removing any
     610' existing ones (case-insensitive, preserves empty elements).
     611function EnvAppendItem(strName, strItem, strSep)
     612   EnvAppendItem = EnvAppendItemEx(strName, strItem, strSep, true, GetRef("EnvItemMatch"))
     613   LogPrint "EnvAppendItem: " & strName & "=" & EnvPrependPathItem
     614end function
     615
     616
     617''
     618' Removes a string element from an environment variable, case
     619' insensitive but preserving empty elements.
     620function EnvRemoveItem(strName, strItem, strSep)
     621   EnvRemoveItem = EnvRemoveItemEx(strName, strIten, strSep, true, GetRef("EnvItemMatch"), "EnvRemoveItem")
     622end function
     623
     624
     625''
     626' Appends a string to an Path-like environment variable,
     627function EnvPrependPathItem(strName, strItem, strSep)
     628   EnvPrependPathItem = EnvPrependItemEx(strName, strItem, strSep, false, GetRef("PathMatch"))
     629   LogPrint "EnvPrependPathItem: " & strName & "=" & EnvPrependPathItem
     630end function
     631
     632
     633''
     634' Appends a string to an Path-like environment variable,
     635function EnvAppendPathItem(strName, strItem, strSep)
     636   EnvAppendPathItem = EnvAppendItemEx(strName, strItem, strSep, false, GetRef("PathMatch"))
     637   LogPrint "EnvAppendPathItem: " & strName & "=" & EnvAppendPathItem
     638end function
     639
     640
     641''
     642' Removes a string element from an Path-like environment variable, case
     643' insensitive and treating forward and backward slashes the same way.
     644function EnvRemovePathItem(strName, strItem, strSep)
     645   EnvRemovePathItem = EnvRemoveItemEx(strName, strIten, strSep, false, GetRef("PathMatch"), "EnvRemovePathItem")
     646end function
     647
     648
     649''
     650' Prepends a string to an environment variable
     651sub EnvUnset(strName)
     652   g_objShell.Environment("PROCESS").Remove(strName)
     653   LogPrint "EnvUnset: " & strName
    409654end sub
    410655
    411 
    412 ''
    413 ' Prepends a string to an environment variable
    414 sub EnvPrepend(strName, strValue)
    415    dim str
    416    str = g_objShell.Environment("PROCESS")(strName)
    417    g_objShell.Environment("PROCESS")(strName) =  strValue & str
    418    LogPrint "EnvPrepend: " & strName & "=" & strValue & str
    419 end sub
    420656
    421657''
     
    426662      EnvGetFirst = g_objShell.Environment("PROCESS")(strName2)
    427663   end if
     664end function
     665
     666''
     667' Checks if the given enviornment variable exists.
     668function EnvExists(strName)
     669   EnvExists = g_objShell.Environment("PROCESS")(strName) <> ""
    428670end function
    429671
     
    718960' Returns an Array() statement string
    719961function ArrayToString(arrStrings)
    720    dim strRet
     962   dim strRet, i
    721963   strRet = "Array("
    722964   for i = LBound(arrStrings) to UBound(arrStrings)
     
    725967   next
    726968   ArrayToString = strRet & ")"
     969end function
     970
     971
     972''
     973' Joins the elements of an array into a string using the given item separator.
     974' @remark this is the same as Join() really.
     975function ArrayJoinString(arrStrings, strSep)
     976   if ArraySize(arrStrings) = 0 then
     977      ArrayJoinString = ""
     978   else
     979      dim i
     980      ArrayJoinString = "" & arrStrings(LBound(arrStrings))
     981      for i = LBound(arrStrings) + 1 to UBound(arrStrings)
     982         ArrayJoinString = ArrayJoinString & strSep & arrStrings(i)
     983      next
     984   end if
    727985end function
    728986
     
    11441402
    11451403''
     1404' Info message.
     1405sub MsgInfo(strMsg)
     1406   Print "info: " & strMsg
     1407end sub
     1408
     1409
     1410''
    11461411' Warning message.
    11471412sub MsgWarning(strMsg)
     
    11671432   g_rcScript = 1
    11681433end sub
     1434
     1435''
     1436' Error message, fatal unless flag to ignore errors is given.
     1437' @note does not return
     1438sub MsgSyntaxError(strMsg)
     1439   Print "syntax error: " & strMsg
     1440   Wscript.Quit(2)
     1441end sub
     1442
     1443
     1444''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
     1445'  Helpers: Misc                                                                                                                 '
     1446''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
     1447
     1448''
     1449' Translate a kBuild / VBox architecture name to a windows one.
     1450function XlateArchitectureToWin(strArch)
     1451   strArch = LCase(strArch)
     1452   XlateArchitectureToWin = strArch
     1453   if strArch = "amd64" then XlateArchitectureToWin = "x64"
     1454end function
    11691455
    11701456
     
    12491535   if ArrayToString(arr) <> "Array(""v10"", ""v1"", ""v0"")" then MsgFatal "SelfTest: Array #10: " & ArrayToString(arr)
    12501536
     1537   if ArrayJoinString(arr, ":") <> "v10:v1:v0" then MsgFatal "SelfTest: Array #11: " & ArrayJoinString(arr, ":")
     1538
     1539   if PathMatch("c:\", "C:\") <> true then MsgFatal "SelfTest: PathMatch #1"
     1540   if PathMatch("c:\\\winDows/sysTem32", "C:\WindowS\.\\.\System32\.") <> true then MsgFatal "SelfTest: PathMatch #2"
     1541   if PathMatch("c:\\\winDows/sysTem32", "C:\WindowS\.\\..\System32\.") <> false then MsgFatal "SelfTest: PathMatch #3"
     1542   if PathMatch("\\x\", "\\\x\") <> false then MsgFatal "SelfTest: PathMatch #4"
     1543   if PathMatch("\\x\", "\\x\") <> true then MsgFatal "SelfTest: PathMatch #5"
     1544   if PathMatch("\\", "\\") <> true then MsgFatal "SelfTest: PathMatch #6"
     1545   if PathMatch("\\x", "\\x") <> true then MsgFatal "SelfTest: PathMatch #7"
     1546
    12511547end sub
    12521548
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette