Debian Codegraph @

windows/nsis/include/win_environment_functions.nsh

raw
1 !ifndef _AddToPath_nsh
2 !define _AddToPath_nsh
3
4 !verbose 3
5 !include "WinMessages.NSH"
6 !verbose 4
7
8 !ifndef WriteEnvStr_RegKey
9 !ifdef ALL_USERS
10 !define WriteEnvStr_RegKey \
11 'HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"'
12 !else
13 !define WriteEnvStr_RegKey 'HKCU "Environment"'
14 !endif
15 !endif
16
17 ; AddToPath - Adds the given dir to the search path.
18 ; Input - head of the stack
19 ; Note - Win9x systems requires reboot
20
21 Function AddToPath
22 Exch $0
23 Push $1
24 Push $2
25 Push $3
26
27 # don't add if the path doesn't exist
28 IfFileExists "$0\*.*" "" AddToPath_done
29
30 ReadEnvStr $1 PATH
31 Push "$1;"
32 Push "$0;"
33 Call StrStr
34 Pop $2
35 StrCmp $2 "" "" AddToPath_done
36 Push "$1;"
37 Push "$0\;"
38 Call StrStr
39 Pop $2
40 StrCmp $2 "" "" AddToPath_done
41 GetFullPathName /SHORT $3 $0
42 Push "$1;"
43 Push "$3;"
44 Call StrStr
45 Pop $2
46 StrCmp $2 "" "" AddToPath_done
47 Push "$1;"
48 Push "$3\;"
49 Call StrStr
50 Pop $2
51 StrCmp $2 "" "" AddToPath_done
52
53 Call IsNT
54 Pop $1
55 StrCmp $1 1 AddToPath_NT
56 ; Not on NT
57 StrCpy $1 $WINDIR 2
58 FileOpen $1 "$1\autoexec.bat" a
59 FileSeek $1 -1 END
60 FileReadByte $1 $2
61 IntCmp $2 26 0 +2 +2 # DOS EOF
62 FileSeek $1 -1 END # write over EOF
63 FileWrite $1 "$\r$\nSET PATH=%PATH%;$3$\r$\n"
64 FileClose $1
65 SetRebootFlag true
66 Goto AddToPath_done
67
68 AddToPath_NT:
69 ReadRegStr $1 ${WriteEnvStr_RegKey} "PATH"
70 StrCmp $1 "" AddToPath_NTdoIt
71 Push $1
72 Call Trim
73 Pop $1
74 StrCpy $0 "$1;$0"
75 AddToPath_NTdoIt:
76 WriteRegExpandStr ${WriteEnvStr_RegKey} "PATH" $0
77 SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
78
79 AddToPath_done:
80 Pop $3
81 Pop $2
82 Pop $1
83 Pop $0
84 FunctionEnd
85
86 ; RemoveFromPath - Remove a given dir from the path
87 ; Input: head of the stack
88
89 Function un.RemoveFromPath
90 Exch $0
91 Push $1
92 Push $2
93 Push $3
94 Push $4
95 Push $5
96 Push $6
97
98 IntFmt $6 "%c" 26 # DOS EOF
99
100 Call un.IsNT
101 Pop $1
102 StrCmp $1 1 unRemoveFromPath_NT
103 ; Not on NT
104 StrCpy $1 $WINDIR 2
105 FileOpen $1 "$1\autoexec.bat" r
106 GetTempFileName $4
107 FileOpen $2 $4 w
108 GetFullPathName /SHORT $0 $0
109 StrCpy $0 "SET PATH=%PATH%;$0"
110 Goto unRemoveFromPath_dosLoop
111
112 unRemoveFromPath_dosLoop:
113 FileRead $1 $3
114 StrCpy $5 $3 1 -1 # read last char
115 StrCmp $5 $6 0 +2 # if DOS EOF
116 StrCpy $3 $3 -1 # remove DOS EOF so we can compare
117 StrCmp $3 "$0$\r$\n" unRemoveFromPath_dosLoopRemoveLine
118 StrCmp $3 "$0$\n" unRemoveFromPath_dosLoopRemoveLine
119 StrCmp $3 "$0" unRemoveFromPath_dosLoopRemoveLine
120 StrCmp $3 "" unRemoveFromPath_dosLoopEnd
121 FileWrite $2 $3
122 Goto unRemoveFromPath_dosLoop
123 unRemoveFromPath_dosLoopRemoveLine:
124 SetRebootFlag true
125 Goto unRemoveFromPath_dosLoop
126
127 unRemoveFromPath_dosLoopEnd:
128 FileClose $2
129 FileClose $1
130 StrCpy $1 $WINDIR 2
131 Delete "$1\autoexec.bat"
132 CopyFiles /SILENT $4 "$1\autoexec.bat"
133 Delete $4
134 Goto unRemoveFromPath_done
135
136 unRemoveFromPath_NT:
137 ReadRegStr $1 ${WriteEnvStr_RegKey} "PATH"
138 StrCpy $5 $1 1 -1 # copy last char
139 StrCmp $5 ";" +2 # if last char != ;
140 StrCpy $1 "$1;" # append ;
141 Push $1
142 Push "$0;"
143 Call un.StrStr ; Find `$0;` in $1
144 Pop $2 ; pos of our dir
145 StrCmp $2 "" unRemoveFromPath_done
146 ; else, it is in path
147 # $0 - path to add
148 # $1 - path var
149 StrLen $3 "$0;"
150 StrLen $4 $2
151 StrCpy $5 $1 -$4 # $5 is now the part before the path to remove
152 StrCpy $6 $2 "" $3 # $6 is now the part after the path to remove
153 StrCpy $3 $5$6
154
155 StrCpy $5 $3 1 -1 # copy last char
156 StrCmp $5 ";" 0 +2 # if last char == ;
157 StrCpy $3 $3 -1 # remove last char
158
159 WriteRegExpandStr ${WriteEnvStr_RegKey} "PATH" $3
160 SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
161
162 unRemoveFromPath_done:
163 Pop $6
164 Pop $5
165 Pop $4
166 Pop $3
167 Pop $2
168 Pop $1
169 Pop $0
170 FunctionEnd
171
172 ; AddToEnvVar - Adds the given value to the given environment var
173 ; Input - head of the stack $0 environement variable $1=value to add
174 ; Note - Win9x systems requires reboot
175
176 Function AddToEnvVar
177
178 Exch $1 ; $1 has environment variable value
179 Exch
180 Exch $0 ; $0 has environment variable name
181
182 DetailPrint "Adding $1 to $0"
183 Push $2
184 Push $3
185 Push $4
186
187
188 ReadEnvStr $2 $0
189 Push "$2;"
190 Push "$1;"
191 Call StrStr
192 Pop $3
193 StrCmp $3 "" "" AddToEnvVar_done
194
195 Push "$2;"
196 Push "$1\;"
197 Call StrStr
198 Pop $3
199 StrCmp $3 "" "" AddToEnvVar_done
200
201
202 Call IsNT
203 Pop $2
204 StrCmp $2 1 AddToEnvVar_NT
205 ; Not on NT
206 StrCpy $2 $WINDIR 2
207 FileOpen $2 "$2\autoexec.bat" a
208 FileSeek $2 -1 END
209 FileReadByte $2 $3
210 IntCmp $3 26 0 +2 +2 # DOS EOF
211 FileSeek $2 -1 END # write over EOF
212 FileWrite $2 "$\r$\nSET $0=%$0%;$4$\r$\n"
213 FileClose $2
214 SetRebootFlag true
215 Goto AddToEnvVar_done
216
217 AddToEnvVar_NT:
218 ReadRegStr $2 ${WriteEnvStr_RegKey} $0
219 StrCpy $3 $2 1 -1 # copy last char
220 StrCmp $3 ";" 0 +2 # if last char == ;
221 StrCpy $2 $2 -1 # remove last char
222 StrCmp $2 "" AddToEnvVar_NTdoIt
223 StrCpy $1 "$2;$1"
224 AddToEnvVar_NTdoIt:
225 WriteRegExpandStr ${WriteEnvStr_RegKey} $0 $1
226 SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
227
228 AddToEnvVar_done:
229 Pop $4
230 Pop $3
231 Pop $2
232 Pop $0
233 Pop $1
234
235 FunctionEnd
236
237 ; RemoveFromEnvVar - Remove a given value from a environment var
238 ; Input: head of the stack
239
240 Function un.RemoveFromEnvVar
241
242 Exch $1 ; $1 has environment variable value
243 Exch
244 Exch $0 ; $0 has environment variable name
245
246 DetailPrint "Removing $1 from $0"
247 Push $2
248 Push $3
249 Push $4
250 Push $5
251 Push $6
252 Push $7
253
254 IntFmt $7 "%c" 26 # DOS EOF
255
256 Call un.IsNT
257 Pop $2
258 StrCmp $2 1 unRemoveFromEnvVar_NT
259 ; Not on NT
260 StrCpy $2 $WINDIR 2
261 FileOpen $2 "$2\autoexec.bat" r
262 GetTempFileName $5
263 FileOpen $3 $5 w
264 GetFullPathName /SHORT $1 $1
265 StrCpy $1 "SET $0=%$0%;$1"
266 Goto unRemoveFromEnvVar_dosLoop
267
268 unRemoveFromEnvVar_dosLoop:
269 FileRead $2 $4
270 StrCpy $6 $4 1 -1 # read last char
271 StrCmp $6 $7 0 +2 # if DOS EOF
272 StrCpy $4 $4 -1 # remove DOS EOF so we can compare
273 StrCmp $4 "$1$\r$\n" unRemoveFromEnvVar_dosLoopRemoveLine
274 StrCmp $4 "$1$\n" unRemoveFromEnvVar_dosLoopRemoveLine
275 StrCmp $4 "$1" unRemoveFromEnvVar_dosLoopRemoveLine
276 StrCmp $4 "" unRemoveFromEnvVar_dosLoopEnd
277 FileWrite $3 $4
278 Goto unRemoveFromEnvVar_dosLoop
279 unRemoveFromEnvVar_dosLoopRemoveLine:
280 SetRebootFlag true
281 Goto unRemoveFromEnvVar_dosLoop
282
283 unRemoveFromEnvVar_dosLoopEnd:
284 FileClose $3
285 FileClose $2
286 StrCpy $2 $WINDIR 2
287 Delete "$2\autoexec.bat"
288 CopyFiles /SILENT $5 "$2\autoexec.bat"
289 Delete $5
290 Goto unRemoveFromEnvVar_done
291
292 unRemoveFromEnvVar_NT:
293 ReadRegStr $2 ${WriteEnvStr_RegKey} $0
294 StrCpy $6 $2 1 -1 # copy last char
295 StrCmp $6 ";" +2 # if last char != ;
296 StrCpy $2 "$2;" # append ;
297 Push $2
298 Push "$1;"
299 Call un.StrStr ; Find `$1;` in $2
300 Pop $3 ; pos of our dir
301 StrCmp $3 "" unRemoveFromEnvVar_done
302 ; else, it is in path
303 # $1 - path to add
304 # $2 - path var
305 StrLen $4 "$1;"
306 StrLen $5 $3
307 StrCpy $6 $2 -$5 # $6 is now the part before the path to remove
308 StrCpy $7 $3 "" $4 # $7 is now the part after the path to remove
309 StrCpy $4 $6$7
310
311 StrCpy $6 $4 1 -1 # copy last char
312 StrCmp $6 ";" 0 +2 # if last char == ;
313 StrCpy $4 $4 -1 # remove last char
314
315 WriteRegExpandStr ${WriteEnvStr_RegKey} $0 $4
316
317 ; delete reg value if null
318 StrCmp $4 "" 0 +2 # if null delete reg
319 DeleteRegValue ${WriteEnvStr_RegKey} $0
320
321 SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
322
323 unRemoveFromEnvVar_done:
324 Pop $7
325 Pop $6
326 Pop $5
327 Pop $4
328 Pop $3
329 Pop $2
330 Pop $1
331 Pop $0
332 FunctionEnd
333
334
335
336
337 !ifndef IsNT_KiCHiK
338 !define IsNT_KiCHiK
339
340 ###########################################
341 # Utility Functions #
342 ###########################################
343
344 ; IsNT
345 ; no input
346 ; output, top of the stack = 1 if NT or 0 if not
347 ;
348 ; Usage:
349 ; Call IsNT
350 ; Pop $R0
351 ; ($R0 at this point is 1 or 0)
352
353 !macro IsNT un
354 Function ${un}IsNT
355 Push $0
356 ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion
357 StrCmp $0 "" 0 IsNT_yes
358 ; we are not NT.
359 Pop $0
360 Push 0
361 Return
362
363 IsNT_yes:
364 ; NT!!!
365 Pop $0
366 Push 1
367 FunctionEnd
368 !macroend
369 !insertmacro IsNT ""
370 !insertmacro IsNT "un."
371
372 !endif ; IsNT_KiCHiK
373
374 ; StrStr
375 ; input, top of stack = string to search for
376 ; top of stack-1 = string to search in
377 ; output, top of stack (replaces with the portion of the string remaining)
378 ; modifies no other variables.
379 ;
380 ; Usage:
381 ; Push "this is a long ass string"
382 ; Push "ass"
383 ; Call StrStr
384 ; Pop $R0
385 ; ($R0 at this point is "ass string")
386
387 !macro StrStr un
388 Function ${un}StrStr
389 Exch $R1 ; st=haystack,old$R1, $R1=needle
390 Exch ; st=old$R1,haystack
391 Exch $R2 ; st=old$R1,old$R2, $R2=haystack
392 Push $R3
393 Push $R4
394 Push $R5
395 StrLen $R3 $R1
396 StrCpy $R4 0
397 ; $R1=needle
398 ; $R2=haystack
399 ; $R3=len(needle)
400 ; $R4=cnt
401 ; $R5=tmp
402 loop:
403 StrCpy $R5 $R2 $R3 $R4
404 StrCmp $R5 $R1 done
405 StrCmp $R5 "" done
406 IntOp $R4 $R4 + 1
407 Goto loop
408 done:
409 StrCpy $R1 $R2 "" $R4
410 Pop $R5
411 Pop $R4
412 Pop $R3
413 Pop $R2
414 Exch $R1
415 FunctionEnd
416 !macroend
417 # working with environment variables in NSIS
418 # script taken from the NSIS-Wiki @ http://nsis.sourceforge.net/Path_Manipulation
419
420 !insertmacro StrStr ""
421 !insertmacro StrStr "un."
422
423 !endif ; _AddToPath_nsh
424
425 Function Trim ; Added by Pelaca
426 Exch $R1
427 Push $R2
428 Loop:
429 StrCpy $R2 "$R1" 1 -1
430 StrCmp "$R2" " " RTrim
431 StrCmp "$R2" "$\n" RTrim
432 StrCmp "$R2" "$\r" RTrim
433 StrCmp "$R2" ";" RTrim
434 GoTo Done
435 RTrim:
436 StrCpy $R1 "$R1" -1
437 Goto Loop
438 Done:
439 Pop $R2
440 Exch $R1
441 FunctionEnd