Recent

Author Topic: [SOLVED] How to detect the configured codepage of my console (Linux)?  (Read 3036 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 749
Normally consoles on Linux use UTF8-codepage, but sometimes I use a GNOME-console which is configured to use good old DOS-codepage (CP850).
My question is: is it possible for a running FPC console program to detect the codepage, which is configured in the console, from where the program was started?

I tried already:
   writeln('DefaultSystemCodePage=', DefaultSystemCodePage);
   writeln('Console codepage: ', GetTextCodePage(OUTPUT));
but in both consoles they show '0'.

I use FPC 3.2.0. Thanks in advance.
« Last Edit: January 20, 2023, 08:27:57 am by Hartmut »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: How to detect the configured codepage of my console (Linux)?
« Reply #1 on: January 17, 2023, 07:30:59 pm »
The best I can suggest for the moment is looking at the LANG shell variable, which at least in the case of KDE's Konsole has a codepage appended.

I don't know the extent to which a desktop environment terminal session has locale etc. information distinct from that of X11, or to what extent that is distinct from an internal kernel setting.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

tetrastes

  • Sr. Member
  • ****
  • Posts: 481
Re: How to detect the configured codepage of my console (Linux)?
« Reply #2 on: January 17, 2023, 09:17:22 pm »
I tried already:
   writeln('DefaultSystemCodePage=', DefaultSystemCodePage);
   writeln('Console codepage: ', GetTextCodePage(OUTPUT));
but in both consoles they show '0'.

Add uses cwstring (see https://wiki.freepascal.org/FPC_Unicode_support#DefaultSystemCodePage).


MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: How to detect the configured codepage of my console (Linux)?
« Reply #3 on: January 17, 2023, 09:58:47 pm »
Add uses cwstring (see https://wiki.freepascal.org/FPC_Unicode_support#DefaultSystemCodePage).

Good point. I thought Lazarus did that automatically under some circumstances?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: How to detect the configured codepage of my console (Linux)?
« Reply #4 on: January 17, 2023, 11:36:40 pm »
Add uses cwstring (see https://wiki.freepascal.org/FPC_Unicode_support#DefaultSystemCodePage).

Good point. I thought Lazarus did that automatically under some circumstances?

MarkMLl
If you use LazUtf8 unit (as the LCL does), it will.

Bart

Hartmut

  • Hero Member
  • *****
  • Posts: 749
Re: How to detect the configured codepage of my console (Linux)?
« Reply #5 on: January 18, 2023, 11:19:19 am »
The best I can suggest for the moment is looking at the LANG shell variable, which at least in the case of KDE's Konsole has a codepage appended.

Thanks MarkMLl for that suggestion. Unfortunately the LANG shell variable in both consoles contains
   LANG=de_DE.UTF-8
so there is no difference.

Add uses cwstring (see https://wiki.freepascal.org/FPC_Unicode_support#DefaultSystemCodePage).

Thanks tetrastes for that idea. Unfortunately this changes
   writeln('DefaultSystemCodePage=', DefaultSystemCodePage);
   writeln('Console codepage: ', GetTextCodePage(OUTPUT));
in both consoles to 65001. Maybe because with unit cwstring the currently set LANG environment variable is asked, as your documentation link says. The LC_CTYPE environment variable is not defined in both consoles.

Any other ideas?

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: How to detect the configured codepage of my console (Linux)?
« Reply #6 on: January 18, 2023, 11:45:24 am »
I'm not particularly comfortable with this since not being able to get at something as important as the codepage has always struck me as incredibly sloppy design.

The fact that you're talking about the Gnome console suggests that you're talking about a shell session wrapped in a graphical window (i.e. the canonical xterm etc.)... however even starting there I can't find anything other than some woolly references to $LANG, $LC_TYPE and the locale. Does  locale charmap  show anything useful?

Gut feeling is that you need an escape sequence etc. which is fed through the pty to whatever GUI component (xterm etc.) is handling the rendering i.e. using Cairo or whatever. So far I've not turned up anything useful, but there /might/ turn out to be some sort of X11 backdoor similar to what a window manager etc. can use to query font metrics.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2049
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to detect the configured codepage of my console (Linux)?
« Reply #7 on: January 18, 2023, 11:52:33 am »
Sorry to jump unprepared in, just my 2ct on this topic, how to detect = i do not know, but i think linux goes same like windows, when no codepage can be resolved an oem codepage like cp850 for western europe will be used. This is just a guessing in the dark!
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Thaddy

  • Hero Member
  • *****
  • Posts: 14359
  • Sensorship about opinions does not belong here.
Re: How to detect the configured codepage of my console (Linux)?
« Reply #8 on: January 18, 2023, 11:59:09 am »
type "export" in a console. The configuration can be used in Tprocess and parsed. or:
Code: Pascal  [Select][+][-]
  1. function GetActiveCodepage: string;
  2. begin
  3.   result := GetEnvironmentVariable('LC_ALL');
  4.   if result= '' then
  5.     result:= GetEnvironmentVariable('LC_CTYPE') else
  6.   if result= '' then
  7.     result:= GetEnvironmentVariable('LANG') else
  8.   if result= '' then
  9.     result := GetEnvironmentVariable('LANGUAGE') else
  10.   if result = '' then
  11.     result:= GetEnvironmentVariable('LC_MESSAGES') else
  12.   if result= '' then
  13.     result:= 'UTF-8'; // by no means fool proof
  14. end;
« Last Edit: January 18, 2023, 12:09:43 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: How to detect the configured codepage of my console (Linux)?
« Reply #9 on: January 18, 2023, 12:08:15 pm »
type "export" in a console. The configuration can be used in Tprocess and parsed.

Shell variables can be used directly, and I think we've already exhausted that approach.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 14359
  • Sensorship about opinions does not belong here.
Re: How to detect the configured codepage of my console (Linux)?
« Reply #10 on: January 18, 2023, 12:10:35 pm »
Ok, Mark... I added a function to my reply... :-X Find the fool.... ;)

Or simply use Tprocess and call locale -a
« Last Edit: January 18, 2023, 12:33:35 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Hartmut

  • Hero Member
  • *****
  • Posts: 749
Re: How to detect the configured codepage of my console (Linux)?
« Reply #11 on: January 18, 2023, 12:46:21 pm »
Does  locale charmap  show anything useful?

Command "locale charmap" shows in both consoles "UTF-8", so there is no difference.

type "export" in a console. The configuration can be used in Tprocess and parsed.

Thank you Thaddy. There are some differences. As a workaround I could use them, if we don't find a better (more directly) way.

Quote
Code: Pascal  [Select][+][-]
  1. function GetActiveCodepage: string;
  2. begin
  3.   result := GetEnvironmentVariable('LC_ALL');
  4.   if result= '' then
  5.     result:= GetEnvironmentVariable('LC_CTYPE') else
  6.   if result= '' then
  7.     result:= GetEnvironmentVariable('LANG') else
  8.   if result= '' then
  9.     result := GetEnvironmentVariable('LANGUAGE') else
  10.   if result = '' then
  11.     result:= GetEnvironmentVariable('LC_MESSAGES') else
  12.   if result= '' then
  13.     result:= 'UTF-8'; // by no means fool proof
  14. end;

Unfortunately all this environment variables either contain the same or do not exist in both consoles.

Thaddy

  • Hero Member
  • *****
  • Posts: 14359
  • Sensorship about opinions does not belong here.
Re: How to detect the configured codepage of my console (Linux)?
« Reply #12 on: January 18, 2023, 12:48:04 pm »
II am aware of that. Alas I had to write "by no means fool proof"
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Hartmut

  • Hero Member
  • *****
  • Posts: 749
Re: How to detect the configured codepage of my console (Linux)?
« Reply #13 on: January 18, 2023, 12:50:39 pm »
Or simply use Tprocess and call locale -a

Command "locale -a" returns the same result in both consoles.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: How to detect the configured codepage of my console (Linux)?
« Reply #14 on: January 18, 2023, 01:05:46 pm »
...do not exist in both consoles.

If it's the same distro and version that might be your best bet.

Output some characters, take a screenshot, OCR it :-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018