pathmani.nsi 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. ; This file comes from the NSIS archive.
  2. !verbose 3
  3. !include "WinMessages.NSH"
  4. !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. Function AddToPath
  9. Exch $0
  10. Push $1
  11. Push $2
  12. Push $3
  13. # don't add if the path doesn't exist
  14. IfFileExists "$0\*.*" "" AddToPath_done
  15. ReadEnvStr $1 PATH
  16. Push "$1;"
  17. Push "$0;"
  18. Call StrStr
  19. Pop $2
  20. StrCmp $2 "" "" AddToPath_done
  21. Push "$1;"
  22. Push "$0\;"
  23. Call StrStr
  24. Pop $2
  25. StrCmp $2 "" "" AddToPath_done
  26. GetFullPathName /SHORT $3 $0
  27. Push "$1;"
  28. Push "$3;"
  29. Call StrStr
  30. Pop $2
  31. StrCmp $2 "" "" AddToPath_done
  32. Push "$1;"
  33. Push "$3\;"
  34. Call StrStr
  35. Pop $2
  36. StrCmp $2 "" "" AddToPath_done
  37. Call IsNT
  38. Pop $1
  39. StrCmp $1 1 AddToPath_NT
  40. ; Not on NT
  41. StrCpy $1 $WINDIR 2
  42. FileOpen $1 "$1\autoexec.bat" a
  43. FileSeek $1 -1 END
  44. FileReadByte $1 $2
  45. IntCmp $2 26 0 +2 +2 # DOS EOF
  46. FileSeek $1 -1 END # write over EOF
  47. FileWrite $1 "$\r$\nSET PATH=%PATH%;$3$\r$\n"
  48. FileClose $1
  49. SetRebootFlag true
  50. Goto AddToPath_done
  51. AddToPath_NT:
  52. ReadRegStr $1 HKCU "Environment" "PATH"
  53. StrCpy $2 $1 1 -1 # copy last char
  54. StrCmp $2 ";" 0 +2 # if last char == ;
  55. StrCpy $1 $1 -1 # remove last char
  56. StrCmp $1 "" AddToPath_NTdoIt
  57. StrCpy $0 "$1;$0"
  58. AddToPath_NTdoIt:
  59. WriteRegExpandStr HKCU "Environment" "PATH" $0
  60. SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
  61. AddToPath_done:
  62. Pop $3
  63. Pop $2
  64. Pop $1
  65. Pop $0
  66. FunctionEnd
  67. ; RemoveFromPath - Remove a given dir from the path
  68. ; Input: head of the stack
  69. Function un.RemoveFromPath
  70. Exch $0
  71. Push $1
  72. Push $2
  73. Push $3
  74. Push $4
  75. Push $5
  76. Push $6
  77. IntFmt $6 "%c" 26 # DOS EOF
  78. Call un.IsNT
  79. Pop $1
  80. StrCmp $1 1 unRemoveFromPath_NT
  81. ; Not on NT
  82. StrCpy $1 $WINDIR 2
  83. FileOpen $1 "$1\autoexec.bat" r
  84. GetTempFileName $4
  85. FileOpen $2 $4 w
  86. GetFullPathName /SHORT $0 $0
  87. StrCpy $0 "SET PATH=%PATH%;$0"
  88. Goto unRemoveFromPath_dosLoop
  89. unRemoveFromPath_dosLoop:
  90. FileRead $1 $3
  91. StrCpy $5 $3 1 -1 # read last char
  92. StrCmp $5 $6 0 +2 # if DOS EOF
  93. StrCpy $3 $3 -1 # remove DOS EOF so we can compare
  94. StrCmp $3 "$0$\r$\n" unRemoveFromPath_dosLoopRemoveLine
  95. StrCmp $3 "$0$\n" unRemoveFromPath_dosLoopRemoveLine
  96. StrCmp $3 "$0" unRemoveFromPath_dosLoopRemoveLine
  97. StrCmp $3 "" unRemoveFromPath_dosLoopEnd
  98. FileWrite $2 $3
  99. Goto unRemoveFromPath_dosLoop
  100. unRemoveFromPath_dosLoopRemoveLine:
  101. SetRebootFlag true
  102. Goto unRemoveFromPath_dosLoop
  103. unRemoveFromPath_dosLoopEnd:
  104. FileClose $2
  105. FileClose $1
  106. StrCpy $1 $WINDIR 2
  107. Delete "$1\autoexec.bat"
  108. CopyFiles /SILENT $4 "$1\autoexec.bat"
  109. Delete $4
  110. Goto unRemoveFromPath_done
  111. unRemoveFromPath_NT:
  112. ReadRegStr $1 HKCU "Environment" "PATH"
  113. StrCpy $5 $1 1 -1 # copy last char
  114. StrCmp $5 ";" +2 # if last char != ;
  115. StrCpy $1 "$1;" # append ;
  116. Push $1
  117. Push "$0;"
  118. Call un.StrStr ; Find `$0;` in $1
  119. Pop $2 ; pos of our dir
  120. StrCmp $2 "" unRemoveFromPath_done
  121. ; else, it is in path
  122. # $0 - path to add
  123. # $1 - path var
  124. StrLen $3 "$0;"
  125. StrLen $4 $2
  126. StrCpy $5 $1 -$4 # $5 is now the part before the path to remove
  127. StrCpy $6 $2 "" $3 # $6 is now the part after the path to remove
  128. StrCpy $3 $5$6
  129. StrCpy $5 $3 1 -1 # copy last char
  130. StrCmp $5 ";" 0 +2 # if last char == ;
  131. StrCpy $3 $3 -1 # remove last char
  132. WriteRegExpandStr HKCU "Environment" "PATH" $3
  133. SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
  134. unRemoveFromPath_done:
  135. Pop $6
  136. Pop $5
  137. Pop $4
  138. Pop $3
  139. Pop $2
  140. Pop $1
  141. Pop $0
  142. FunctionEnd
  143. ###########################################
  144. # Utility Functions #
  145. ###########################################
  146. ; IsNT
  147. ; no input
  148. ; output, top of the stack = 1 if NT or 0 if not
  149. ;
  150. ; Usage:
  151. ; Call IsNT
  152. ; Pop $R0
  153. ; ($R0 at this point is 1 or 0)
  154. !macro IsNT un
  155. Function ${un}IsNT
  156. Push $0
  157. ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion
  158. StrCmp $0 "" 0 IsNT_yes
  159. ; we are not NT.
  160. Pop $0
  161. Push 0
  162. Return
  163. IsNT_yes:
  164. ; NT!!!
  165. Pop $0
  166. Push 1
  167. FunctionEnd
  168. !macroend
  169. !insertmacro IsNT ""
  170. !insertmacro IsNT "un."
  171. ; StrStr
  172. ; input, top of stack = string to search for
  173. ; top of stack-1 = string to search in
  174. ; output, top of stack (replaces with the portion of the string remaining)
  175. ; modifies no other variables.
  176. ;
  177. ; Usage:
  178. ; Push "this is a long ass string"
  179. ; Push "ass"
  180. ; Call StrStr
  181. ; Pop $R0
  182. ; ($R0 at this point is "ass string")
  183. !macro StrStr un
  184. Function ${un}StrStr
  185. Exch $R1 ; st=haystack,old$R1, $R1=needle
  186. Exch ; st=old$R1,haystack
  187. Exch $R2 ; st=old$R1,old$R2, $R2=haystack
  188. Push $R3
  189. Push $R4
  190. Push $R5
  191. StrLen $R3 $R1
  192. StrCpy $R4 0
  193. ; $R1=needle
  194. ; $R2=haystack
  195. ; $R3=len(needle)
  196. ; $R4=cnt
  197. ; $R5=tmp
  198. loop:
  199. StrCpy $R5 $R2 $R3 $R4
  200. StrCmp $R5 $R1 done
  201. StrCmp $R5 "" done
  202. IntOp $R4 $R4 + 1
  203. Goto loop
  204. done:
  205. StrCpy $R1 $R2 "" $R4
  206. Pop $R5
  207. Pop $R4
  208. Pop $R3
  209. Pop $R2
  210. Exch $R1
  211. FunctionEnd
  212. !macroend
  213. !insertmacro StrStr ""
  214. !insertmacro StrStr "un."