|
| 1 | +-- This script adds/removes the SDM installation directory to/from the PATH |
| 2 | +-- environment variable. |
| 3 | +-- This is an experimental script that is not currently used by the installer. |
| 4 | +-- Usage: addtopath.lua <add|remove> |
| 5 | + |
| 6 | +if #arg~=1 or (arg[1]~="add" and arg[1]~="remove") then |
| 7 | + print("Adds/removes SDM to/from the Windows PATH environment variable") |
| 8 | + print("Usage: "..arg[0].." <add|remove>") |
| 9 | + return |
| 10 | +end |
| 11 | + |
| 12 | +-- Import the necessary Win32 functions |
| 13 | + |
| 14 | +dfic=require("luadfic") |
| 15 | + |
| 16 | +local RegCreateKeyExA=dfic.import("advapi32","RegCreateKeyExA", |
| 17 | + "long@winapi@uintptr_t,".. -- HKEY hKey |
| 18 | + "const char*,".. -- LPCSTR lpSubKey |
| 19 | + "unsigned long,".. -- DWORD Reserved |
| 20 | + "const void*,".. -- LPSTR lpClass |
| 21 | + "unsigned long,".. -- DWORD dwOptions |
| 22 | + "unsigned long,".. -- REGSAM samDesired |
| 23 | + "const void*,".. -- const LPSECURITY_ATTRIBUTES lpSecurityAttributes |
| 24 | + "uintptr_t*,".. -- PHKEY phkResult, |
| 25 | + "const void*") -- LPDWORD lpdwDisposition |
| 26 | + |
| 27 | +local RegQueryValueExW=dfic.import("advapi32","RegQueryValueExW", |
| 28 | + "long@winapi@uintptr_t,".. -- HKEY hKey, |
| 29 | + "const void*,".. -- LPCWSTR lpValueName, |
| 30 | + "const unsigned long*,".. -- LPDWORD lpReserved, |
| 31 | + "unsigned long*,".. -- LPDWORD lpType, |
| 32 | + "void*,".. -- LPBYTE lpData, |
| 33 | + "unsigned long*") -- LPDWORD lpcbData |
| 34 | + |
| 35 | +local RegSetValueExW=dfic.import("advapi32","RegSetValueExW", |
| 36 | + "long@winapi@uintptr_t,".. -- HKEY hKey, |
| 37 | + "const void*,".. -- LPCWSTR lpValueName, |
| 38 | + "unsigned long,".. -- DWORD Reserved, |
| 39 | + "unsigned long,".. -- DWORD dwType, |
| 40 | + "const void*,".. -- const BYTE *lpData, |
| 41 | + "unsigned long") -- DWORD cbData |
| 42 | + |
| 43 | +local RegCloseKey=dfic.import("advapi32","RegCloseKey","long@winapi@uintptr_t") |
| 44 | + |
| 45 | +local SendMessageTimeoutA=dfic.import("user32","SendMessageTimeoutA", |
| 46 | + "long@winapi@uintptr_t,".. -- HWND hWnd, |
| 47 | + "unsigned int,".. -- UINT Msg, |
| 48 | + "const void*,".. -- WPARAM wParam, |
| 49 | + "const char*,".. -- LPARAM lParam, |
| 50 | + "unsigned int,".. -- UINT fuFlags, |
| 51 | + "unsigned int,".. -- UINT uTimeout, |
| 52 | + "unsigned int*" -- PDWORD_PTR lpdwResult |
| 53 | +); |
| 54 | + |
| 55 | +-- A helper function to parse the PATH environment variable |
| 56 | + |
| 57 | +function parse_path(path) |
| 58 | + local t={} |
| 59 | + local i=1 |
| 60 | + |
| 61 | + function additem(str) |
| 62 | + if str~="" then table.insert(t,str) end |
| 63 | + end |
| 64 | + |
| 65 | + while true do |
| 66 | + -- "i" points to the beginning of an item |
| 67 | + if path:sub(i,i)~="\"" then |
| 68 | + -- normal item |
| 69 | + local r=path:find(";",i,true) |
| 70 | + if not r then |
| 71 | + additem(path:sub(i)) |
| 72 | + break |
| 73 | + end |
| 74 | + additem(path:sub(i,r-1)) |
| 75 | + i=r+1 |
| 76 | + else |
| 77 | + -- quoted item |
| 78 | + local r=path:find("\"",i+1,true) |
| 79 | + if not r then break end -- malformed string |
| 80 | + additem(path:sub(i+1,r-1)) |
| 81 | + i=path:find(";",r+1,true) |
| 82 | + if not i then break end |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + return t |
| 87 | +end |
| 88 | + |
| 89 | +-- Create a UTF-16 codec because Windows API doesn't support UTF-8 |
| 90 | + |
| 91 | +local c=codec.createcodec("utf-16") |
| 92 | + |
| 93 | +-- Open the environment registry key |
| 94 | + |
| 95 | +local HKEY_LOCAL_MACHINE=0x80000002 |
| 96 | +local KEY_ALL_ACCESS=0xF003F |
| 97 | +local subkey="SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment" |
| 98 | + |
| 99 | +local r,key=RegCreateKeyExA(HKEY_LOCAL_MACHINE,subkey,0,nil,0,KEY_ALL_ACCESS,nil,1,nil) |
| 100 | + |
| 101 | +if r~=0 then |
| 102 | + print("Error opening registry key") |
| 103 | + return |
| 104 | +end |
| 105 | + |
| 106 | +-- Query the Path value |
| 107 | + |
| 108 | +local valuename_buf=dfic.buffer(256) |
| 109 | +valuename_buf.write(c.fromutf8("Path")) |
| 110 | + |
| 111 | +local r,valuetype,size=RegQueryValueExW(key[1],valuename_buf.ptr(),nil,1,nil,1) |
| 112 | + |
| 113 | +if r~=0 then |
| 114 | + print("Error querying registry key value") |
| 115 | + RegCloseKey(key[1]) |
| 116 | + return |
| 117 | +end |
| 118 | + |
| 119 | +local path_buf=dfic.buffer(size[1]) |
| 120 | + |
| 121 | +r,valuetype,size=RegQueryValueExW(key[1],valuename_buf.ptr(),nil,1,path_buf.ptr(),{size[1]}) |
| 122 | + |
| 123 | +if r~=0 then |
| 124 | + print("Error querying registry key value") |
| 125 | + RegCloseKey(key[1]) |
| 126 | + return |
| 127 | +end |
| 128 | + |
| 129 | +if valuetype[1]~=1 and valuetype[1]~=2 then |
| 130 | + print("Unexpected [Path] value type") |
| 131 | + RegCloseKey(key[1]) |
| 132 | + return |
| 133 | +end |
| 134 | + |
| 135 | +local path=c.toutf8(path_buf.read(1,size[1])) |
| 136 | + |
| 137 | +-- Remove the terminating null character(s), if any |
| 138 | + |
| 139 | +for i=#path,1,-1 do |
| 140 | + if path:byte(i)~=0 then |
| 141 | + path=path:sub(1,i) |
| 142 | + break |
| 143 | + end |
| 144 | +end |
| 145 | + |
| 146 | +-- Dissect the path string |
| 147 | + |
| 148 | +local path_table=parse_path(path) |
| 149 | + |
| 150 | +-- Obtain the path to the SDM installation directory |
| 151 | + |
| 152 | +local installprefix=sdm.path("installprefix") |
| 153 | + |
| 154 | +-- Modify the path table |
| 155 | + |
| 156 | +if arg[1]=="add" then |
| 157 | +-- Add the SDM installation prefix to the PATH |
| 158 | +-- Check whether it is not already present |
| 159 | + for k,v in ipairs(path_table) do |
| 160 | + if v==installprefix or v==installprefix.."\\" then |
| 161 | + print("["..installprefix.."] is already present in the PATH") |
| 162 | + RegCloseKey(key[1]) |
| 163 | + return |
| 164 | + end |
| 165 | + end |
| 166 | + table.insert(path_table,installprefix) |
| 167 | +else |
| 168 | +-- Remove the SDM installation prefix from the PATH |
| 169 | + local removed=false |
| 170 | + for k,v in ipairs(path_table) do |
| 171 | + if v==installprefix or v==installprefix.."\\" then |
| 172 | + table.remove(path_table,k) |
| 173 | + removed=true |
| 174 | + end |
| 175 | + end |
| 176 | + if not removed then |
| 177 | + print("["..installprefix.."] is not present in the PATH") |
| 178 | + RegCloseKey(key[1]) |
| 179 | + return |
| 180 | + end |
| 181 | +end |
| 182 | + |
| 183 | +-- Reassemble the path string |
| 184 | + |
| 185 | +path="" |
| 186 | +for i=1,#path_table do |
| 187 | + if path_table[i]:find(";",1,true) then |
| 188 | + path=path.."\""..path_table[i].."\"" |
| 189 | + else |
| 190 | + path=path..path_table[i] |
| 191 | + end |
| 192 | + if i~=#path_table then path=path..";" end |
| 193 | +end |
| 194 | + |
| 195 | +-- Set the new PATH environment variable value |
| 196 | + |
| 197 | +local path_utf16=c.fromutf8(path) |
| 198 | +path_buf.resize(#path_utf16+2) |
| 199 | +path_buf.write(path_utf16) |
| 200 | +path_buf.write("\0\0",#path_utf16) -- add terminating null character |
| 201 | + |
| 202 | +r=RegSetValueExW(key[1],valuename_buf.ptr(),0,valuetype[1],path_buf.ptr(),#path_buf) |
| 203 | + |
| 204 | +if r~=0 then |
| 205 | + print("Error setting registry key value") |
| 206 | + RegCloseKey(key[1]) |
| 207 | + return |
| 208 | +end |
| 209 | + |
| 210 | +RegCloseKey(key[1]) |
| 211 | + |
| 212 | +-- Inform applications that the environment has been updated |
| 213 | + |
| 214 | +local HWND_BROADCAST=0xFFFF |
| 215 | +local WM_SETTINGCHANGE=0x001A |
| 216 | + |
| 217 | +SendMessageTimeoutA(HWND_BROADCAST,WM_SETTINGCHANGE,nil,"Environment",0,1000,1) |
| 218 | + |
| 219 | +print("PATH environment variable has been successfully modified") |
0 commit comments