Patch 007976 of Crt is incorrect
Original Reporter info from Mantis: kudrna
-
Reporter name: Pavel Kudrna
Original Reporter info from Mantis: kudrna
- Reporter name: Pavel Kudrna
Description:
The problem is switching to ANSI codepage using SetConsoleOutputCP(GetACP) in rtl/win/crt.pp. It garbles program output for character codes over 127 when unit crt is used.
There are several other serious disadvantages:
- different behaviour with and without the crt unit
- it is not backward compatible, eg. it precludes box-drawing characters
- there is no way to disable this feature
- produced code doesn't follow chcp console setting
- incompatibility with FreePascal IDE which uses OEM encoding
Two related bugs reported in 2010 are:
0016064: [windows] adding "uses crt" causes implicit code page conversion
0016846: Console output with unit Crt always using ANSI codepage
Problem was created by the patch
http://lists.freepascal.org/lists/fpc-devel/2006-May/007976.html
which attempted to solve
http://bugs.freepascal.org/view.php?id=6785
where reporter compiled source code in ANSI CP-1251 and run it without properly configuring the console window from the default OEM encoding corresponding to his system setting to required ANSI CP.
If he would perform "chcp 1251" and then run his program in the same cmd console window it would correctly print both 33 lowercase and 33 uppercase russian characters as he expected.
To test it on windows where system setting for non-unicode programs is not russian it is also necessary to switch to TrueType font (e.g. Lucida Console) in the cmd console via it's system menu.
The original code.pp attached to bug report is damaged by the &# html conversion so here are two source codes with russian characters portably encoded, first in CP 1251 and the second in 866:
uses crt;
begin
writeln('This non-unicode program produces russian alphabet in win-1251 encoding:');
writeln(
#$E0, #$E1, #$E2, #$E3, #$E4, #$E5, #$B8, #$E6, #$E7, #$E8, #$E9,
#$EA, #$EB, #$EC, #$ED, #$EE, #$EF, #$F0, #$F1, #$F2, #$F3, #$F4,
#$F5, #$F6, #$F7, #$F8, #$F9, #$FA, #$FB, #$FC, #$FD, #$FE, #$FF
);
writeln(
#$C0, #$C1, #$C2, #$C3, #$C4, #$C5, #$A8, #$C6, #$C7, #$C8, #$C9,
#$CA, #$CB, #$CC, #$CD, #$CE, #$CF, #$D0, #$D1, #$D2, #$D3, #$D4,
#$D5, #$D6, #$D7, #$D8, #$D9, #$DA, #$DB, #$DC, #$DD, #$DE, #$DF
);
end.
uses crt;
begin
writeln('This non-unicode program prints russian alphabet in CP-866 encoding:');
writeln(
#$A0, #$A1, #$A2, #$A3, #$A4, #$A5, #$F1, #$A6, #$A7, #$A8, #$A9,
#$AA, #$AB, #$AC, #$AD, #$AE, #$AF, #$E0, #$E1, #$E2, #$E3, #$E4,
#$E5, #$E6, #$E7, #$E8, #$E9, #$EA, #$EB, #$EC, #$ED, #$EE, #$EF
);
writeln(
#$80, #$81, #$82, #$83, #$84, #$85, #$F0, #$86, #$87, #$88, #$89,
#$8A, #$8B, #$8C, #$8D, #$8E, #$8F, #$90, #$91, #$92, #$93, #$94,
#$95, #$96, #$97, #$98, #$99, #$9A, #$9B, #$9C, #$9D, #$9E, #$9F
);
end.
Both run well if preceeded by the correct chcp setting and if
a) line "uses crt;" is commented out or
b) "SetConsoleOutputCP(GetACP);" is commented out in the patch and crt.o and crt.ppu are updated.
Both produce wrong characters with "uses crt;" and the patch.
Tested on XPsp3 and VistaSP2.
Summary: The patch brings no improvement.
Suggested resolution: Remove the patch.
Complement to resolution: Add note to users that with TrueType font (e.g. Lucida Console) win32 console works well.
Mantis conversion info:
- Mantis ID: 24257
- OS: MS Windows
- OS Build: XP, Vista
- Platform: Win32
- Version: 2.6.2
- Monitored by: » Legolas (Francesco Lombardi), » kudrna (Pavel Kudrna), » @xhajt03 (Tomas Hajny)