Frew Schmidt
Frew Schmidt

Reputation: 9544

How can I convert literal newlines into $\n in NSIS?

I'm writing an NSIS installer that logs the output of a few external commands (see How can I make this function actually work in NSIS? .) Unfortunately when nsis stores the output it eats the literal newlines, presumably because it is looking for a literal $\n (I don't actually know that.)

Can anyone tell me how to correctly convert the newlines from $0 in the following snippet?

nsExec::ExecToStack '...'
Pop $0

I'll be writing the stored string to both a file and with DetailPrint.

Upvotes: 1

Views: 1041

Answers (1)

Anders
Anders

Reputation: 101666

I don't believe ExecToStack eats newlines but it is limited to ${NSIS_MAX_STRLEN} of output.

The ExecDos plug-in has a /TOFUNC parameter that can call a NSIS function for each line.

Or you can parse it on your own:

Function nsExecLine
Pop $0
DetailPrint Line|$0|
; FileWrite ...
FunctionEnd  



!include LogicLib.nsh
nsExec::ExecToStack 'ping -n 2 localhost'
Pop $0
${If} $0 != "error"
    Pop $0
    StrCpy $1 -1
    StrCpy $3 "" ; \r \n merge
    more:
        IntOp $1 $1 + 1
        StrCpy $2 $0 1 $1
        StrCmp $2 "" done ; Can also be printlast if desired
        StrCmp $2 "$\n" +2
        StrCmp $2 "$\r" +1 more
        StrCpy $2 $0 $1
        StrCpy $4 $0 1 $1
        StrCmp $3 "" +2
        StrCmp $3 $4 0 more
        StrCpy $3 $4
        IntOp $1 $1 + 1
    ;printlast:
        StrCpy $0 $0 "" $1
        Push $0
        Push $3
        Push $2
        Call nsExecLine
        Pop $3
        Pop $0
        StrCpy $1 -1
        StrCmp $0 "" +1 more
    done:
${EndIf}

...or as a bit of a hack, use ExecToLog:

!include LogicLib.nsh
!include WinMessages.nsh
!ifndef LVM_GETITEM
!ifndef LVM_FIRST
!define LVM_FIRST 0x00001000
!endif
!define /math LVM_GETITEMCOUNT ${LVM_FIRST} +  4
!ifndef NSIS_UNICODE
!define /math LVM_GETITEM ${LVM_FIRST} +  5
!else
!define /math LVM_GETITEM ${LVM_FIRST} + 75
!endif
!endif
FindWindow $9 "#32770" "" $HWNDPARENT
GetDlgItem $9 $9 0x3F8
SendMessage $9 ${LVM_GETITEMCOUNT} 0 0 $7
nsExec::ExecToLog 'ping -n 2 localhost'
Pop $0
${If} $0 != "error"
    !define MAXEXECLINE 5000
    System::Call '*(i,i,i,i,i,i,i,i,i,&t${MAXEXECLINE})i.r6'
    IntOp $8 $6 + 36
    System::Call '*$6(i,i,i,i,i,ir8,i${MAXEXECLINE},i,i)'
    SendMessage $9 ${LVM_GETITEMCOUNT} 0 0 $8
    ${DoWhile} $7 < $8
        Push $6
        Push $7
        Push $8
        Push $9
        System::Call '*$6(i1,i$7,i0)'
        SendMessage $9 ${LVM_GETITEM} 0 $6
        System::Call '*$6(i,i,i,i,i,t.r7)' ; (i,i,i,i,i,i,i,i,i,&t${MAXEXECLINE} .r7)'
        Push $7
        Call nsExecLine ; nsExecLine should not DetailPrint in this case...
        Pop $9
        Pop $8
        Pop $7
        Pop $6
        IntOp $7 $7 + 1
    ${Loop}
    System::Free $6
${EndIf}

Upvotes: 2

Related Questions