Archive: Bad characters(<>?:) in $INSTDIR variable


Bad characters(<>?:) in $INSTDIR variable
Hi,

I noticed that if I entered bad characters(<>?:) in the installation directory MUI page, the $INSTDIR variable skips those bad characters. I want to warn user that they have entered an invalid path name, so is there a way to get the raw string from installation directory page? Thanks in advance.


Figured it out, just do not use $INSTDIR to catch directory from MUI

I assigned $R1 instead

!define MUI_DIRECTORYPAGE_VARIABLE $R1

Use StringFilter to filter these symbols: '/:*?"<>|'

; Stores the first two symbols to $R1
StrCpy $R1 $INSTDIR 2
; Others to $INSTDIR
StrCpy $INSTDIR $INSTDIR "" 2

; {StrFilter} "[string]" "[options]" "[symbols1]" "[symbols2]" $var
; You can create your own rule by modifying the list of symbols include or exclude

; Delete illegal symbols from $INSTDIR
; Note there is a space after ')', for a blank space is valid in path
${StrFilter} $INSTDIR 12 '.\{}[]() ' '/:*?"<>|' $INSTDIR

; Replace continuous spaces with one space
${WordReplace} $INSTDIR ' ' ' ' '+*' $INSTDIR
${WordReplace} $INSTDIR ' ' ' ' '+' $INSTDIR

; Replace continuous dots with one dot
${WordReplace} $INSTDIR '..' '.' '+*' $INSTDIR
${WordReplace} $INSTDIR '..' '.' '+' $INSTDIR

; A dot at the end of a folder name is invalid
${WordReplace} $INSTDIR '.\' '\' '+*' $INSTDIR
${WordReplace} $INSTDIR '.\' '\' '+' $INSTDIR

; Replace continuous backsplashes with one backsplash
${WordReplace} $INSTDIR '\\' '\' '+*' $INSTDIR
${WordReplace} $INSTDIR '\\' '\' '+' $INSTDIR

; A space at the begening of a folder name is invalid
${WordReplace} $INSTDIR ' \' '\' '+*' $INSTDIR
${WordReplace} $INSTDIR ' \' '\' '+' $INSTDIR

; A space at the end of a folder name is invalid
${WordReplace} $INSTDIR '\ ' '\' '+*' $INSTDIR
${WordReplace} $INSTDIR '\ ' '\' '+' $INSTDIR

; Join to full path
StrCpy $INSTDIR $R1$INSTDIR
Before: D:\AB CD [{] / |\ ...\.E !@ / } <] \ / F \ \\\ \df .}.1. ///fff\ff ?f>\ 1 2d ff] \
After: D:\AB CD [{]\.E } ]\F\df .}.1. fff\ff f\1 2d ff]

This is the snapshot of an example using the code:
http://forums.winamp.com/attachment....chmentid=47287

I think bensmatt was referring to $INSTDIR being one of the special internal variables that NSIS treats as a file path, which automatically strips illegal characters. The same way one might use...


Push $PathToClean
Exch $EXEDIR
Exch $EXEDIR
Pop $PathToClean

This presents the developer with a problem if they want to warn the user about illegal characters, as they are already filtered before you can detect them.

I.e. execute the following, and try to enter the illegal path: c:\*.*

OutFile "test.exe"

Page Directory

Function .onVerifyInstDir
MessageBox MB_OK "$$INSTDIR = $INSTDIR"
FunctionEnd

Section
SectionEnd

The MessageBox will state that "$INSTDIR = c:\.". Thus you can't detect the asterisks having been entered. Note that these asterisks -are- still present in the actual directory text field, of course. So to the end-user, there doesn't seem to be anything amiss.

If instead you use:

OutFile "test.exe"

Var varInstDir
PageEx Directory
DirVar $varInstDir
PageExEnd

Function .onVerifyInstDir
MessageBox MB_OK "$$varInstDir = $varInstDir"
FunctionEnd

Section
SectionEnd

Then the behavior is more like what you might expect. You could then use that to adjust the path and warn the user; though more easily done from a custom page, the basic principles are the same.