Is silent by command line

From NSIS Wiki
Jump to navigationJump to search
Author: KiCHiK (talk, contrib)


Description

Superseded by: NSIS 2.0b4 - IfSilent command.

This function returns 1 (through the stack, see example) if the user passed /S in the command line making the installer silent, and 0 if he didn't.

The Function

Function IsSilent
  Push $0
  Push $CMDLINE
  Push "/S"
  Call StrStr
  Pop $0
  StrCpy $0 $0 3
  StrCmp $0 "/S" silent
  StrCmp $0 "/S " silent
    StrCpy $0 0
    Goto notsilent
  silent: StrCpy $0 1
  notsilent: Exch $0
FunctionEnd

Full Example

Name "IsSilent Tester"
OutFile "IsSilent Tester.exe"
 
Function .onInit
  Call IsSilent
  Pop $0
  StrCmp $0 1 0 +3
    MessageBox MB_OK|MB_ICONSTOP "This installer can not run in silent mode!"
    Abort
FunctionEnd
 
Section
  # do stuff
SectionEnd
 
Function IsSilent
  Push $0
  Push $CMDLINE
  Push "/S"
  Call StrStr
  Pop $0
  StrCpy $0 $0 3
  StrCmp $0 "/S" silent
  StrCmp $0 "/S " silent
    StrCpy $0 0
    Goto notsilent
  silent: StrCpy $0 1
  notsilent: Exch $0
FunctionEnd
 
Function StrStr
  Exch $R1 ; st=haystack,old$R1, $R1=needle
  Exch    ; st=old$R1,haystack
  Exch $R2 ; st=old$R1,old$R2, $R2=haystack
  Push $R3
  Push $R4
  Push $R5
  StrLen $R3 $R1
  StrCpy $R4 0
  ; $R1=needle
  ; $R2=haystack
  ; $R3=len(needle)
  ; $R4=cnt
  ; $R5=tmp
  loop:
    StrCpy $R5 $R2 $R3 $R4
    StrCmp $R5 $R1 done
    StrCmp $R5 "" done
    IntOp $R4 $R4 + 1
    Goto loop
  done:
  StrCpy $R1 $R2 "" $R4
  Pop $R5
  Pop $R4
  Pop $R3
  Pop $R2
  Exch $R1
FunctionEnd