Examination of Linefeeds with CALL

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
dbenham
Expert
Posts: 2288
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Examination of Linefeeds with CALL

#16 Post by dbenham » 13 Jun 2019 18:43

No, because the newlines don't exist until after phase 1 is complete. Within Phase 1, only the outer loop looks for <LF> to truncate. After the %\n% gets replaced, the outer loop scanner continues with the next character after the replacement, so the <LF> is never seen by the scanner, and it is safe.


Dave Benham

jeb
Expert
Posts: 917
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Examination of Linefeeds with CALL

#17 Post by jeb » 14 Jun 2019 00:53

Hi Dave,

it's hard to remember all the problems parsing CALL/LF variables.

I retested some of the behaviour and I suppose your rules are correct now.

jeb

dbenham
Expert
Posts: 2288
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Examination of Linefeeds with CALL

#18 Post by dbenham » 14 Jun 2019 07:11

Below is evidence that Phase 0 preserves the end of line newline, and that phase 1 truly truncates the line at the first appearing newline not within expanding percents. Also, percent expansion error is not fatal after CALL.

It is important that the last line of the script does not end with a newline.

Code: Select all

@echo off
if "%~1" neq "" goto %1
setlocal
set var=CONTENT

(set \n=^
%= Creates a newline character =%
)

for /l %%N in (1 1 4) do (
  echo ====== TEST %%N ======
  cmd /v:on /c ^""%~f0" test%%N^"
  echo --------------------
)
exit /b

:test1
call echo %%var:=WRONG SYNTAX!\n!%% NEWLINE PRESERVED WITHIN PERCENTS
echo The syntax error is not fatal after CALL
exit /b

:test2
call echo Line truncated at newline so no error!\n! %%var:=WRONG SYNTAX%%
exit /b

:test3
%var:=WRONG SYNTAX% THE END-OF-LINE NEWLINE IS PRESERVED AFTER PHASE 0

:test4
%var:=WRONG SYNTAX% THIS LINE HAS NO NEWLINE, SO ERROR MESSAGE NOT SPLIT
OUTPUT wrote: ====== TEST 1 ======
=WRONG SYNTAX
% NEWLINE PRESERVED WITHIN PERCENTS was unexpected at this time.
The syntax error is not fatal after CALL
--------------------
====== TEST 2 ======
Line truncated at newline so no error
--------------------
====== TEST 3 ======
=WRONG SYNTAX% THE END-OF-LINE NEWLINE IS PRESERVED AFTER PHASE 0
was unexpected at this time.
The syntax of the command is incorrect.
--------------------
====== TEST 4 ======
=WRONG SYNTAX% THIS LINE HAS NO NEWLINE, SO ERROR MESSAGE NOT SPLIT was unexpected at this time.
The syntax of the command is incorrect.
--------------------

Dave Benham

dbenham
Expert
Posts: 2288
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Examination of Linefeeds with CALL

#19 Post by dbenham » 24 Jun 2019 11:05

I just realized I had to make another change to step 2.3 in the phase rules - Both the search and replace terms allow <LF> characters.

I also corrected step 5.2 in delayed expansion to also allow <LF> in search and replace terms. Not sure why this one wasn't caught ages ago.


Dave Benham

Post Reply