ENH: Better support for adding and removing path

This commit is contained in:
Andy Cedilnik 2006-04-27 16:20:07 -04:00
parent 2b5d8e84c9
commit 8d2c78f43c
1 changed files with 181 additions and 141 deletions

View File

@ -81,168 +81,208 @@ FunctionEnd
!verbose 3 !verbose 3
!include "WinMessages.NSH" !include "WinMessages.NSH"
!verbose 4 !verbose 4
;----------------------------------------
; based upon a script of "Written by KiCHiK 2003-01-18 05:57:02"
;----------------------------------------
!verbose 3
!include "WinMessages.NSH"
!verbose 4
;====================================================
; get_NT_environment
; Returns: the selected environment
; Output : head of the stack
;====================================================
!macro select_NT_profile UN
Function ${UN}select_NT_profile
MessageBox MB_YESNO|MB_ICONQUESTION "Change the environment for all users? $\n\
$\n\
Saying no here will change the envrironment for the current user only. $\n\
$\n\
(Administrator permissions required for all users)" \
IDNO environment_single
DetailPrint "Selected environment for all users"
Push "all"
Return
environment_single:
DetailPrint "Selected environment for current user only."
Push "current"
Return
FunctionEnd
!macroend
!insertmacro select_NT_profile ""
!insertmacro select_NT_profile "un."
;----------------------------------------------------
!define NT_current_env 'HKCU "Environment"'
!define NT_all_env 'HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"'
;====================================================
; AddToPath - Adds the given dir to the search path. ; AddToPath - Adds the given dir to the search path.
; Input - head of the stack ; Input - head of the stack
; Note - Win9x systems requires reboot ; Note - Win9x systems requires reboot
;====================================================
Function AddToPath Function AddToPath
Exch $0 Exch $0
Push $1 Push $1
Push $2 Push $2
Push $3
Call IsNT
Pop $1
StrCmp $1 1 AddToPath_NT
; Not on NT
StrCpy $1 $WINDIR 2
FileOpen $1 "$1\autoexec.bat" a
FileSeek $1 0 END
GetFullPathName /SHORT $0 $0
FileWrite $1 "$\r$\n\
$\r$\n\
SET PATH=%PATH%;$0$\r$\n\
$\r$\n\
"
FileClose $1
Goto AddToPath_done
# don't add if the path doesn't exist AddToPath_NT:
IfFileExists "$0\*.*" "" AddToPath_done Push $4
Call select_NT_profile
Pop $4
ReadEnvStr $1 PATH AddToPath_NT_selection_done:
Push "$1;" StrCmp $4 "current" read_path_NT_current
Push "$0;" ReadRegStr $1 ${NT_all_env} "PATH"
Call StrStr Goto read_path_NT_resume
Pop $2 read_path_NT_current:
StrCmp $2 "" "" AddToPath_done ReadRegStr $1 ${NT_current_env} "PATH"
Push "$1;" read_path_NT_resume:
Push "$0\;" StrCmp $1 "" AddToPath_NTdoIt
Call StrStr StrCpy $2 "$0;$1"
Pop $2 Goto AddToPath_NTdoIt
StrCmp $2 "" "" AddToPath_done AddToPath_NTdoIt:
GetFullPathName /SHORT $3 $0 StrCmp $4 "current" write_path_NT_current
Push "$1;" ClearErrors
Push "$3;" WriteRegExpandStr ${NT_all_env} "PATH" $2
Call StrStr IfErrors 0 write_path_NT_resume
Pop $2 MessageBox MB_YESNO|MB_ICONQUESTION "The path could not be set for all users$\n\
StrCmp $2 "" "" AddToPath_done $\n\
Push "$1;" Should I try for the current user?" \
Push "$3\;" IDNO write_path_NT_failed
Call StrStr ; change selection
Pop $2 StrCpy $4 "current"
StrCmp $2 "" "" AddToPath_done Goto AddToPath_NT_selection_done
write_path_NT_current:
Call IsNT ClearErrors
Pop $1 WriteRegExpandStr ${NT_current_env} "PATH" $2
StrCmp $1 1 AddToPath_NT IfErrors 0 write_path_NT_resume
; Not on NT MessageBox MB_OK|MB_ICONINFORMATION "The path could not be set for the current user."
StrCpy $1 $WINDIR 2 Goto write_path_NT_failed
FileOpen $1 "$1\autoexec.bat" a write_path_NT_resume:
FileSeek $1 -1 END SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
FileReadByte $1 $2 DetailPrint "added path for user ($4), $0"
IntCmp $2 26 0 +2 +2 # DOS EOF write_path_NT_failed:
FileSeek $1 -1 END # write over EOF
FileWrite $1 "$\r$\nSET PATH=%PATH%;$3$\r$\n" Pop $4
FileClose $1 AddToPath_done:
SetRebootFlag true Pop $2
Goto AddToPath_done Pop $1
Pop $0
AddToPath_NT:
MessageBox MB_OK 'Add to path "$0"'
ReadRegStr $1 SHCTX "Environment" "PATH"
StrCpy $2 $1 1 -1 # copy last char
StrCmp $2 ";" 0 +2 # if last char == ;
StrCpy $1 $1 -1 # remove last char
StrCmp $1 "" AddToPath_NTdoIt
StrCpy $0 "$1;$0"
AddToPath_NTdoIt:
WriteRegExpandStr SHCTX "Environment" "PATH" $0
SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
AddToPath_done:
Pop $3
Pop $2
Pop $1
Pop $0
FunctionEnd FunctionEnd
;====================================================
; RemoveFromPath - Remove a given dir from the path ; RemoveFromPath - Remove a given dir from the path
; Input: head of the stack ; Input: head of the stack
;====================================================
Function un.RemoveFromPath Function un.RemoveFromPath
Exch $0 Exch $0
Push $1 Push $1
Push $2 Push $2
Push $3 Push $3
Push $4 Push $4
Push $5
Push $6 Call un.IsNT
Pop $1
IntFmt $6 "%c" 26 # DOS EOF StrCmp $1 1 unRemoveFromPath_NT
; Not on NT
Call un.IsNT
Pop $1
StrCmp $1 1 unRemoveFromPath_NT
; Not on NT
StrCpy $1 $WINDIR 2
FileOpen $1 "$1\autoexec.bat" r
GetTempFileName $4
FileOpen $2 $4 w
GetFullPathName /SHORT $0 $0
StrCpy $0 "SET PATH=%PATH%;$0"
Goto unRemoveFromPath_dosLoop
unRemoveFromPath_dosLoop:
FileRead $1 $3
StrCpy $5 $3 1 -1 # read last char
StrCmp $5 $6 0 +2 # if DOS EOF
StrCpy $3 $3 -1 # remove DOS EOF so we can compare
StrCmp $3 "$0$\r$\n" unRemoveFromPath_dosLoopRemoveLine
StrCmp $3 "$0$\n" unRemoveFromPath_dosLoopRemoveLine
StrCmp $3 "$0" unRemoveFromPath_dosLoopRemoveLine
StrCmp $3 "" unRemoveFromPath_dosLoopEnd
FileWrite $2 $3
Goto unRemoveFromPath_dosLoop
unRemoveFromPath_dosLoopRemoveLine:
SetRebootFlag true
Goto unRemoveFromPath_dosLoop
unRemoveFromPath_dosLoopEnd:
FileClose $2
FileClose $1
StrCpy $1 $WINDIR 2 StrCpy $1 $WINDIR 2
Delete "$1\autoexec.bat" FileOpen $1 "$1\autoexec.bat" r
CopyFiles /SILENT $4 "$1\autoexec.bat" GetTempFileName $4
Delete $4 FileOpen $2 $4 w
Goto unRemoveFromPath_done GetFullPathName /SHORT $0 $0
StrCpy $0 "SET PATH=%PATH%;$0"
SetRebootFlag true
Goto unRemoveFromPath_dosLoop
unRemoveFromPath_dosLoop:
FileRead $1 $3
StrCmp $3 "$0$$\r$\n\
$\r$\n\
" unRemoveFromPath_dosLoop
StrCmp $3 "$0$\r$\n\
" unRemoveFromPath_dosLoop
StrCmp $3 "$0" unRemoveFromPath_dosLoop
StrCmp $3 "" unRemoveFromPath_dosLoopEnd
FileWrite $2 $3
Goto unRemoveFromPath_dosLoop
unRemoveFromPath_NT: unRemoveFromPath_dosLoopEnd:
MessageBox MB_OK 'Remove from path "$0"' FileClose $2
ReadRegStr $1 SHCTX "Environment" "PATH" FileClose $1
StrCpy $5 $1 1 -1 # copy last char StrCpy $1 $WINDIR 2
StrCmp $5 ";" +2 # if last char != ; Delete "$1\autoexec.bat"
StrCpy $1 "$1;" # append ; CopyFiles /SILENT $4 "$1\autoexec.bat"
Push $1 Delete $4
Push "$0;" Goto unRemoveFromPath_done
Call un.StrStr ; Find `$0;` in $1
Pop $2 ; pos of our dir
StrCmp $2 "" unRemoveFromPath_done
; else, it is in path
# $0 - path to add
# $1 - path var
StrLen $3 "$0;"
StrLen $4 $2
StrCpy $5 $1 -$4 # $5 is now the part before the path to remove
StrCpy $6 $2 "" $3 # $6 is now the part after the path to remove
StrCpy $3 $5$6
StrCpy $5 $3 1 -1 # copy last char unRemoveFromPath_NT:
StrCmp $5 ";" 0 +2 # if last char == ; StrLen $2 $0
StrCpy $3 $3 -1 # remove last char Call un.select_NT_profile
Pop $4
WriteRegExpandStr SHCTX "Environment" "PATH" $3 StrCmp $4 "current" un_read_path_NT_current
SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000 ReadRegStr $1 ${NT_all_env} "PATH"
Goto un_read_path_NT_resume
un_read_path_NT_current:
ReadRegStr $1 ${NT_current_env} "PATH"
un_read_path_NT_resume:
unRemoveFromPath_done: Push $1
Pop $6 Push $0
Pop $5 Call un.StrStr ; Find $0 in $1
Pop $4 Pop $0 ; pos of our dir
Pop $3 IntCmp $0 -1 unRemoveFromPath_done
Pop $2 ; else, it is in path
Pop $1 StrCpy $3 $1 $0 ; $3 now has the part of the path before our dir
Pop $0 IntOp $2 $2 + $0 ; $2 now contains the pos after our dir in the path (';')
IntOp $2 $2 + 1 ; $2 now containts the pos after our dir and the semicolon.
StrLen $0 $1
StrCpy $1 $1 $0 $2
StrCpy $3 "$3$1"
StrCmp $4 "current" un_write_path_NT_current
WriteRegExpandStr ${NT_all_env} "PATH" $3
Goto un_write_path_NT_resume
un_write_path_NT_current:
WriteRegExpandStr ${NT_current_env} "PATH" $3
un_write_path_NT_resume:
SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
unRemoveFromPath_done:
Pop $4
Pop $3
Pop $2
Pop $1
Pop $0
FunctionEnd FunctionEnd
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Uninstall sutff
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
########################################### ###########################################
# Utility Functions # # Utility Functions #
########################################### ###########################################
;====================================================
; IsNT - Returns 1 if the current system is NT, 0
; otherwise.
; Output: head of the stack
;====================================================
; IsNT ; IsNT
; no input ; no input
; output, top of the stack = 1 if NT or 0 if not ; output, top of the stack = 1 if NT or 0 if not