oops.. only build it when it _is_ valid.
[AROS-Contrib.git] / regina / common / path.nsh
blobae08e359f75d9523a7644a6bf7d933fdb8d00d42
1 !verbose 3
2 !include "WinMessages.NSH"
3 !verbose 4
5 ; AddToPath - Adds the given dir to the search path.
6 ;        Input - head of the stack
7 ;        Note - Win9x systems requires reboot
8 ; Usage:
9 ; Push "directory" or ".ext"
10 ; Push "true" or "false"
11 ; Push "PATH" or "PATHEXT"
12 ; Call AddToPath
14 Function AddToPath
15   Pop $R8 ; env variable; PATH or PATHEXT
16   Pop $R7 ; "true" or "false" if admin user
17   Exch $0
18   Push $1
19   Push $2
20   Push $3
22   StrCpy $4 "$0" ;save install directory in $4
23   ; don't add if the new item already exists in the registry value
24   StrCmp $R7 "true" ReadPath_AllUsers
25   ReadRegStr $1 HKCU "Environment" "$R8"
26   Goto ReadPath_UserCont
27   ReadPath_AllUsers:
28   ReadRegStr $1 HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "$R8"
29   ReadPath_UserCont:
30   Push "$1;"
31   Push "$0;"
32   Call StrStr
33   Pop $2
34   StrCmp $2 "" "" AddToPath_done
35   Push "$1;"
36   Push "$0\;"
37   Call StrStr
38   Pop $2
39   StrCmp $2 "" "" AddToPath_done
40   ;
41   ; Only compare shortpath if PATH env variable
42   ;
43   StrCmp $R8 "PATH" "" AddToPath_Not_PATH
44   GetFullPathName /SHORT $3 $0
45   Push "$1;"
46   Push "$3;"
47   Call StrStr
48   Pop $2
49   StrCmp $2 "" "" AddToPath_done
50   Push "$1;"
51   Push "$3\;"
52   Call StrStr
53   Pop $2
54   StrCmp $2 "" "" AddToPath_done
55   Goto AddToPath_cont
56   AddToPath_Not_PATH:
57   StrCpy $3 $0
58   AddToPath_cont:
59   Call IsNT
60   Pop $1
61   StrCmp $1 1 AddToPath_NT
62     ; Not on NT
63     StrCpy $1 $WINDIR 2
64     FileOpen $1 "$1\autoexec.bat" a
65     FileSeek $1 -1 END
66     FileReadByte $1 $2
67     IntCmp $2 26 0 +2 +2 # DOS EOF
68       FileSeek $1 -1 END # write over EOF
69     FileWrite $1 "$\r$\nSET $R8=%$R8%;$3$\r$\n"
70     FileClose $1
71     SetRebootFlag true
72     Goto AddToPath_done
74   AddToPath_NT:
75     StrCmp $R7 "true" AddToPath_AllUsers
76     ReadRegStr $1 HKCU "Environment" "$R8"
77     Goto AddToPath_UserCont
78   AddToPath_AllUsers:
79     ReadRegStr $1 HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "$R8"
80   AddToPath_UserCont:
81     StrCpy $2 $1 1 -1 # copy last char
82     StrCmp $2 ";" 0 +2 # if last char == ;
83     StrCpy $1 $1 -1 # remove last char
84     StrCmp $1 "" AddToPath_NTdoIt ; if current value is blank, do not prepend existing value
85     StrCpy $0 "$1;$0"
86     AddToPath_NTdoIt:
87       StrCmp $R7 "true" AddToPath_AllUsers_doit
88       ; writing registry for current user
89       WriteRegExpandStr HKCU "Environment" "$R8" $0
90       DetailPrint "$4 added to $R8 for Current User"
91       Goto AddToPath_UserCont_doit
92     AddToPath_AllUsers_doit:
93       ; writing registry for all users
94       WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "$R8" $0
95       DetailPrint "$4 added to $R8 for All Users"
96     AddToPath_UserCont_doit:
97       SendMessage ${HWND_BROADCAST} ${WM_SETTINGCHANGE} 0 "STR:Environment" /TIMEOUT=10000
99   AddToPath_done:
100     Pop $3
101     Pop $2
102     Pop $1
103     Pop $0
104 FunctionEnd
106 ; RemoveFromPath - Remove a given dir from the path
107 ;     Input: head of the stack
108 ; Usage:
109 ; Push "directory" or ".ext"
110 ; Push "true" or "false"
111 ; Push "PATH" or "PATHEXT"
112 ; Call un.RemoveFromPath
114 Function un.RemoveFromPath
115   Pop $R8 ; env variable; PATH or PATHEXT
116   Pop $R7 ; "true" or "false" if admin user
117   Exch $0
118   Push $1
119   Push $2
120   Push $3
121   Push $4
122   Push $5
123   Push $6
125   IntFmt $6 "%c" 26 # DOS EOF
127   Call un.IsNT
128   Pop $1
129   StrCmp $1 1 unRemoveFromPath_NT
130     ; Not on NT
131     StrCpy $1 $WINDIR 2
132     FileOpen $1 "$1\autoexec.bat" r
133     GetTempFileName $4
134     FileOpen $2 $4 w
135     ;
136     ; Only compare shortpath if PATH env variable
137     ;
138     StrCmp $R8 "PATH" "" unRemoveFromPath_Not_PATH
139     GetFullPathName /SHORT $0 $0
140     unRemoveFromPath_Not_PATH:
141     StrCpy $0 "SET $R8=%$R8%;$0"
142     Goto unRemoveFromPath_dosLoop
144     unRemoveFromPath_dosLoop:
145       FileRead $1 $3
146       StrCpy $5 $3 1 -1 # read last char
147       StrCmp $5 $6 0 +2 # if DOS EOF
148         StrCpy $3 $3 -1 # remove DOS EOF so we can compare
149       StrCmp $3 "$0$\r$\n" unRemoveFromPath_dosLoopRemoveLine
150       StrCmp $3 "$0$\n" unRemoveFromPath_dosLoopRemoveLine
151       StrCmp $3 "$0" unRemoveFromPath_dosLoopRemoveLine
152       StrCmp $3 "" unRemoveFromPath_dosLoopEnd
153       FileWrite $2 $3
154       Goto unRemoveFromPath_dosLoop
155       unRemoveFromPath_dosLoopRemoveLine:
156         SetRebootFlag true
157         Goto unRemoveFromPath_dosLoop
159     unRemoveFromPath_dosLoopEnd:
160       FileClose $2
161       FileClose $1
162       StrCpy $1 $WINDIR 2
163       Delete "$1\autoexec.bat"
164       CopyFiles /SILENT $4 "$1\autoexec.bat"
165       Delete $4
166       Goto unRemoveFromPath_done
168   unRemoveFromPath_NT:
169     StrCmp $R7 "true" unRemoveFromPath_AllUsers
170     ReadRegStr $1 HKCU "Environment" "$R8"
171     Goto unRemoveFromPath_UserCont
172   unRemoveFromPath_AllUsers:
173     ReadRegStr $1 HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "$R8"
174   unRemoveFromPath_UserCont:
175     StrCpy $5 $1 1 -1 # copy last char
176     StrCmp $5 ";" +2 # if last char != ;
177       StrCpy $1 "$1;" # append ;
178     Push $1
179     Push "$0;"
180     Call un.StrStr ; Find `$0;` in $1
181     Pop $2 ; pos of our dir
182     StrCmp $2 "" unRemoveFromPath_done
183       ; else, it is in path
184       # $0 - path to add
185       # $1 - path var
186       StrLen $3 "$0;"
187       StrLen $4 $2
188       StrCpy $5 $1 -$4 # $5 is now the part before the path to remove
189       StrCpy $6 $2 "" $3 # $6 is now the part after the path to remove
190       StrCpy $3 $5$6
192       StrCpy $5 $3 1 -1 # copy last char
193       StrCmp $5 ";" 0 +2 # if last char == ;
194         StrCpy $3 $3 -1 # remove last char
195       ;
196       ; If the value is now empty, delete it
197       ;
198       StrCmp $3 "" "" unRemoveFromPath_rewrite
199       ; we need to delete the value
200       StrCmp $R7 "true" unRemoveFromPath_AllUsers_delete
201       DeleteRegValue HKCU "Environment" "$R8"
202       DetailPrint "$0 removed from $R8 for Current User"
203       Goto unRemoveFromPath_UserCont_doit
204     unRemoveFromPath_AllUsers_delete:
205       DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "$R8"
206       DetailPrint "$0 removed from $R8 for All Users"
207       Goto unRemoveFromPath_UserCont_doit
208     unRemoveFromPath_rewrite:
209       StrCmp $R7 "true" unRemoveFromPath_AllUsers_rewrite
210       WriteRegExpandStr HKCU "Environment" "$R8" $3
211       DetailPrint "$0 removed from $R8 for Current User"
212       Goto unRemoveFromPath_UserCont_doit
213     unRemoveFromPath_AllUsers_rewrite:
214       WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "$R8" $3
215       DetailPrint "$0 removed from $R8 for All Users"
216     unRemoveFromPath_UserCont_doit:
217       SendMessage ${HWND_BROADCAST} ${WM_SETTINGCHANGE} 0 "STR:Environment" /TIMEOUT=10000
219   unRemoveFromPath_done:
220     Pop $6
221     Pop $5
222     Pop $4
223     Pop $3
224     Pop $2
225     Pop $1
226     Pop $0
227 FunctionEnd
229 ;====================================================
230 ; EmptyDirectory - Determines if directory passed is empty
231 ;                  and deletes it and from PATH if not empty
232 ;          Returns: nothing
233 ;          Input - PATH
234 ;                - "true" or "false"
235 ;====================================================
236 Function un.EmptyDirectory
237   Pop $R7 ; "true" or "false" if admin user
238   Pop $R5
239   FindFirst $2 $1 "$5\*.*"
240   StrCmp $1 "" empty
241   StrCmp $1 "." cont
242   StrCmp $1 ".." cont
243   Goto done
244 cont:
245   FindNext $2 $1
246   ;Dumpstate::debug
247   StrCmp $1 "" empty
248   StrCmp $1 "." cont
249   StrCmp $1 ".." cont
250   ; to get here we have a real file, so get out now
251   Goto done
252 empty:
253   ; Remove the directory from PATH
254   Push $R5
255   Push $R7
256   Push "PATH"
257   Call un.RemoveFromPath
258 done:
259   FindClose $2
260 FunctionEnd
262 ###########################################
263 #            Utility Functions            #
264 ###########################################
267 ; StrStr
268 ; input, top of stack = string to search for
269 ;        top of stack-1 = string to search in
270 ; output, top of stack (replaces with the portion of the string remaining)
271 ; modifies no other variables.
273 ; Usage:
274 ;   Push "this is a long ass string"
275 ;   Push "ass"
276 ;   Call StrStr
277 ;   Pop $R0
278 ;  ($R0 at this point is "ass string")
280 !macro StrStr un
281 Function ${un}StrStr
282 Exch $R1 ; st=haystack,old$R1, $R1=needle
283   Exch    ; st=old$R1,haystack
284   Exch $R2 ; st=old$R1,old$R2, $R2=haystack
285   Push $R3
286   Push $R4
287   Push $R5
288   StrLen $R3 $R1
289   StrCpy $R4 0
290   ; $R1=needle
291   ; $R2=haystack
292   ; $R3=len(needle)
293   ; $R4=cnt
294   ; $R5=tmp
295   loop:
296     StrCpy $R5 $R2 $R3 $R4
297     StrCmp $R5 $R1 done
298     StrCmp $R5 "" done
299     IntOp $R4 $R4 + 1
300     Goto loop
301 done:
302   StrCpy $R1 $R2 "" $R4
303   Pop $R5
304   Pop $R4
305   Pop $R3
306   Pop $R2
307   Exch $R1
308 FunctionEnd
309 !macroend
310 !insertmacro StrStr ""
311 !insertmacro StrStr "un."