Recent

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

tetrastes

  • Sr. Member
  • ****
  • Posts: 483
Re: How to detect the configured codepage of my console (Linux)?
« Reply #15 on: January 18, 2023, 01:13:56 pm »
@Hartmut
All this means that your console in fact UTF-8, not CP850, and "locale charmap" confirms it, as you wrote.
How do you setup CP for console? If you use Gnome Console Profile settings, it fools you. It simply shows output in other encoding, not really changes CP of console, which leads to garbage.
You have to set LANG environment variable.
But first check what locales are available at your system by "locale -a". Is there any locale with CP850 at its output?
For example, I have C, POSIX and several UTF-8 locales. If I set LANG to anything other, pascal program outputs 20127 (i.e ASCII), which corresponds to
Code: Bash  [Select][+][-]
  1. $ locale -c charmap
  2. locale: Cannot set LC_CTYPE to default locale: No such file or directory
  3. locale: Cannot set LC_MESSAGES to default locale: No such file or directory
  4. locale: Cannot set LC_ALL to default locale: No such file or directory
  5. LC_CTYPE
  6. ANSI_X3.4-1968
which is the same as 20127.

Hartmut

  • Hero Member
  • *****
  • Posts: 751
Re: How to detect the configured codepage of my console (Linux)?
« Reply #16 on: January 18, 2023, 01:47:22 pm »
Thank you tetrastes for your reply.

How do you setup CP for console? If you use Gnome Console Profile settings, it fools you. It simply shows output in other encoding, not really changes CP of console, which leads to garbage.

I use Gnome Console Profile settings. That explains why it is so difficult to detect.

Quote
But first check what locales are available at your system by "locale -a". Is there any locale with CP850 at its output?

Command "locale -a" returns the same result in both consoles, which is:
Code: Text  [Select][+][-]
  1. C
  2. C.UTF-8
  3. de_AT.utf8
  4. de_BE.utf8
  5. de_CH.utf8
  6. de_DE.utf8
  7. de_IT.utf8
  8. de_LI.utf8
  9. de_LU.utf8
  10. en_AG
  11. en_AG.utf8
  12. en_AU.utf8
  13. en_BW.utf8
  14. en_CA.utf8
  15. en_DK.utf8
  16. en_GB.utf8
  17. en_HK.utf8
  18. en_IE.utf8
  19. en_IL
  20. en_IL.utf8
  21. en_IN
  22. en_IN.utf8
  23. en_NG
  24. en_NG.utf8
  25. en_NZ.utf8
  26. en_PH.utf8
  27. en_SG.utf8
  28. en_US.utf8
  29. en_ZA.utf8
  30. en_ZM
  31. en_ZM.utf8
  32. en_ZW.utf8
  33. POSIX

tetrastes

  • Sr. Member
  • ****
  • Posts: 483
Re: How to detect the configured codepage of my console (Linux)?
« Reply #17 on: January 18, 2023, 02:04:33 pm »
Thank you tetrastes for your reply.

How do you setup CP for console? If you use Gnome Console Profile settings, it fools you. It simply shows output in other encoding, not really changes CP of console, which leads to garbage.

I use Gnome Console Profile settings. That explains why it is so difficult to detect.
It is NOT difficult to detect. It does NOT change CP of console, as I have written.
And there is NO locale with CP850 at your system, as you can see for yourself:


Quote
But first check what locales are available at your system by "locale -a". Is there any locale with CP850 at its output?
Command "locale -a" returns the same result in both consoles, which is:

"locale -a" does not depend on console locale/CP, it simply writes names of available locales.

Hartmut

  • Hero Member
  • *****
  • Posts: 751
Re: How to detect the configured codepage of my console (Linux)?
« Reply #18 on: January 18, 2023, 04:04:40 pm »
I use Gnome Console Profile settings. That explains why it is so difficult to detect.
It is NOT difficult to detect.

Hmm, how would you detect the 2 different codepages?

tetrastes

  • Sr. Member
  • ****
  • Posts: 483
Re: How to detect the configured codepage of my console (Linux)?
« Reply #19 on: January 18, 2023, 07:58:23 pm »
Hmm, how would you detect the 2 different codepages?
Code: Bash  [Select][+][-]
  1. $ LANG=C; export LANG
  2. $ locale -c charmap
  3. LC_CTYPE
  4. ANSI_X3.4-1968
Your code (with uses cwstring) outputs 20127. This is correct.

Code: Bash  [Select][+][-]
  1. $ LANG=C.utf8; export LANG
  2. $ locale -c charmap
  3. LC_CTYPE
  4. UTF-8
Your code outputs 65001. This is correct also.

It is difficult to detect CP850 when the real CP is UTF-8, especially if there is no locale with CP850 in the system.
You confuse real console CP with the Gnome Console (Gnome Terminal, to be precise) Character Encoding setting. It is somewhat like Character Encoding setting in text editor, nothing to do with real locale/CP.
If you want to detect Gnome Terminal settings in your program, you have to dig in dconf or/and maybe Gnome API. And this is difficult, I suppose. ;)
« Last Edit: January 18, 2023, 08:29:35 pm by tetrastes »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: How to detect the configured codepage of my console (Linux)?
« Reply #20 on: January 18, 2023, 09:00:02 pm »
If you want to detect Gnome Terminal settings in your program, you have to dig in dconf or/and maybe Gnome API. And this is difficult, I suppose. ;)

Which is what OP's made fairly clear that he's asking.

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: 483
Re: How to detect the configured codepage of my console (Linux)?
« Reply #21 on: January 18, 2023, 09:44:53 pm »
Maybe for you it's clear, but I don't see that clearness. I see the subject and his code and questions (and answers), they all are about detecting console codepages, not Gnome Terminal settings.
 

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: How to detect the configured codepage of my console (Linux)?
« Reply #22 on: January 18, 2023, 09:53:05 pm »
OP:

Quote
...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

That's pretty unambiguous. Your suggestion of dconf might be worth following up though, but as I've said I think it's going to be very much specific to whatever is wrapping the shell: xterm or a later equivalent.

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: 483
Re: How to detect the configured codepage of my console (Linux)?
« Reply #23 on: January 18, 2023, 11:09:00 pm »
OP:

Quote
...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

That's pretty unambiguous.

MarkMLl
That's pretty ambiguous. What means "which is configured to use good old DOS-codepage (CP850)" and "codepage, which is configured in the console"? Actually - setting LANG.
And what means "console"? Gnome Terminal? OP wrote "GNOME-console" because he uses it, but I understand "console" in the second line in a general sense. And you also understood it the same way, as I see from your answers. You wrote about console locale/CP, not [Gnome] Terminal settings. Not every X Terminal emulator has something like Gnome Terminal Character Encoding setting, and of course not character-mode console.

And then:
Quote
I tried already:
   writeln('DefaultSystemCodePage=', DefaultSystemCodePage);
   writeln('Console codepage: ', GetTextCodePage(OUTPUT));
but in both consoles they show '0'.
What does this code has to do with [Gnome] Terminal settings?


Hartmut

  • Hero Member
  • *****
  • Posts: 751
Re: How to detect the configured codepage of my console (Linux)?
« Reply #24 on: January 19, 2023, 12:37:31 pm »
Hmm, how would you detect the 2 different codepages?
Code: Bash  [Select][+][-]
  1. $ LANG=C; export LANG
  2. $ locale -c charmap
  3. LC_CTYPE
  4. ANSI_X3.4-1968

If I understand you correctly, this commands would have to be executed at every start of Gnome Terminal. Of course I don't want to type them in manually after each start. Unfortunately I found no way to execute this commands automatically *and* to have after that a normal operating terminal, where I can enter commands in the usual way. Nor via command line options of Gnome terminal neither via its profile options. Do I miss something?

If you want to detect Gnome Terminal settings in your program, you have to dig in dconf or/and maybe Gnome API. And this is difficult, I suppose. ;)
Which is what OP's made fairly clear that he's asking.

For me it does not so much matter *how* I can distinguish between the 2 different codepages or the 2 different terminals. Of course, if I could detect the codepages directly, this would be "better". But more important is for me, that it's not too difficult. Sorry for confusion.

If there would be a simple way, that Gnome terminal first executes a batchfile and after that behaves as a normal terminal, that would be fine. Otherwise I would prefer to check in my program for some environment variables, which are only set in Gnome terminal, as indicated in reply #11.

tetrastes

  • Sr. Member
  • ****
  • Posts: 483
Re: How to detect the configured codepage of my console (Linux)?
« Reply #25 on: January 19, 2023, 02:52:16 pm »
Hmm, how would you detect the 2 different codepages?
Code: Bash  [Select][+][-]
  1. $ LANG=C; export LANG
  2. $ locale -c charmap
  3. LC_CTYPE
  4. ANSI_X3.4-1968

If I understand you correctly, this commands would have to be executed at every start of Gnome Terminal.
No, these were only examples.
I'm completely confused. What do you want to achieve? To set console to locale with CP850, or to get the value of Character Encoding setting of Gnome Terminal? These are completely different tasks.
« Last Edit: January 19, 2023, 02:59:14 pm by tetrastes »

Hartmut

  • Hero Member
  • *****
  • Posts: 751
Re: How to detect the configured codepage of my console (Linux)?
« Reply #26 on: January 19, 2023, 03:04:51 pm »
I'm completely confused. What do you want to achieve? To set console to locale with CP850, or to get the value of Character Encoding setting of Gnome Terminal? These are completely different tasks.

Sorry for confusion. What I want to achive is, to be able to detect in a FPC console program, whether it was started from a Gnome Terminal (which needs CP850) or from another Terminal (which needs UTF8).

tetrastes

  • Sr. Member
  • ****
  • Posts: 483
Re: How to detect the configured codepage of my console (Linux)?
« Reply #27 on: January 19, 2023, 06:58:08 pm »
Sorry for confusion. What I want to achive is, to be able to detect in a FPC console program, whether it was started from a Gnome Terminal (which needs CP850) or from another Terminal (which needs UTF8).
Something like this?

Code: Pascal  [Select][+][-]
  1. uses Dos;
  2.  
  3. begin
  4.     if GetEnv('GNOME_TERMINAL_SCREEN') = '' then
  5.         writeln('I am running at some other Terminal')
  6.     else
  7.         writeln('I am running at GNOME Terminal');
  8. end.

dsiders

  • Hero Member
  • *****
  • Posts: 1084
Re: How to detect the configured codepage of my console (Linux)?
« Reply #28 on: January 19, 2023, 07:31:53 pm »
Sorry for confusion. What I want to achive is, to be able to detect in a FPC console program, whether it was started from a Gnome Terminal (which needs CP850) or from another Terminal (which needs UTF8).
Something like this?

Code: Pascal  [Select][+][-]
  1. uses Dos;
  2.  
  3. begin
  4.     if GetEnv('GNOME_TERMINAL_SCREEN') = '' then
  5.         writeln('I am running at some other Terminal')
  6.     else
  7.         writeln('I am running at GNOME Terminal');
  8. end.

Isn't this what the TERM_PROGRAM environment variable is for?
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

tetrastes

  • Sr. Member
  • ****
  • Posts: 483
Re: How to detect the configured codepage of my console (Linux)?
« Reply #29 on: January 19, 2023, 07:41:18 pm »
Isn't this what the TERM_PROGRAM environment variable is for?

I don't have this environment variable at my system.

 

TinyPortal © 2005-2018