Recent

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

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: How to detect the configured codepage of my console (Linux)?
« Reply #30 on: January 20, 2023, 08:26:23 am »
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.

Yes, exactly that was my idea. Because we did not find something better (more directly checking the codepage), I will implement this now. Thanks to all who helped me.

Isn't this what the TERM_PROGRAM environment variable is for?

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

I don't have this environment variable too in both terminals.

TRon

  • Hero Member
  • *****
  • Posts: 2432
Re: [SOLVED] How to detect the configured codepage of my console (Linux)?
« Reply #31 on: January 20, 2023, 10:11:50 am »
... from where the program was started?
If you already know that the code-page is different depending on which terminal was used to launch your program then you could perhaps take a look at the parent to determine the difference.

Code: Pascal  [Select][+][-]
  1. program parent;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   sysutils, baseunix;
  7.  
  8. function GetParentProcessName: string;
  9. begin
  10.   GetParentProcessName := fpReadLink('/proc/' + fpGetppid.ToString + '/exe');
  11. end;
  12.  
  13. begin
  14.   writeln('name of parent process = ', GetParentPRocessName);
  15. end.
  16.  
  17.  

tetrastes

  • Sr. Member
  • ****
  • Posts: 473
Re: [SOLVED] How to detect the configured codepage of my console (Linux)?
« Reply #32 on: January 20, 2023, 10:28:48 am »
... from where the program was started?
If you already know that the code-page is different depending on which terminal was used to launch your program then you could perhaps take a look at the parent to determine the difference.

Code: Pascal  [Select][+][-]
  1. program parent;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   sysutils, baseunix;
  7.  
  8. function GetParentProcessName: string;
  9. begin
  10.   GetParentProcessName := fpReadLink('/proc/' + fpGetppid.ToString + '/exe');
  11. end;
  12.  
  13. begin
  14.   writeln('name of parent process = ', GetParentPRocessName);
  15. end.
  16.  
  17.  

Have you tried your code? It outputs
Code: Bash  [Select][+][-]
  1. name of parent process = /usr/bin/bash
at any terminal.

TRon

  • Hero Member
  • *****
  • Posts: 2432
Re: [SOLVED] How to detect the configured codepage of my console (Linux)?
« Reply #33 on: January 20, 2023, 10:35:24 am »
Have you tried your code? It outputs
Code: Bash  [Select][+][-]
  1. name of parent process = /usr/bin/bash
at any terminal.
Yes, i have. And when i make a desktop shortcut to my console program it displays the desktops used default terminal (not bin/bash).

tetrastes

  • Sr. Member
  • ****
  • Posts: 473
Re: [SOLVED] How to detect the configured codepage of my console (Linux)?
« Reply #34 on: January 20, 2023, 10:50:31 am »
Have you tried your code? It outputs
Code: Bash  [Select][+][-]
  1. name of parent process = /usr/bin/bash
at any terminal.
Yes, i have. And when i make a desktop shortcut to my console program it displays the desktops used default terminal (not bin/bash).
Maybe, but it is obviously not universal method. Try to launch terminal from where your DE places it by default (menu, what's else). What do you see?

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: [SOLVED] How to detect the configured codepage of my console (Linux)?
« Reply #35 on: January 20, 2023, 11:01:17 am »
Yes, i have. And when i make a desktop shortcut to my console program it displays the desktops used default terminal (not bin/bash).
Maybe, but it is obviously not universal method. Try to launch terminal from where your DE places it by default (menu, what's else). What do you see?

It's an interesting approach though, provided that OP is happy with something that "broad brush" rather than (as he phrased it) actually wanting to know what codepage was being used.

An alternative might be checking for the existence of the DISPLAY variable, which I'd expect to be set for /any/ program running under X11 (possibly also Wayland, but that would need to be tested).

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

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: [SOLVED] How to detect the configured codepage of my console (Linux)?
« Reply #36 on: January 20, 2023, 11:33:01 am »
If you already know that the code-page is different depending on which terminal was used to launch your program then you could perhaps take a look at the parent to determine the difference.

Code: Pascal  [Select][+][-]
  1. program parent;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   sysutils, baseunix;
  7.  
  8. function GetParentProcessName: string;
  9. begin
  10.   GetParentProcessName := fpReadLink('/proc/' + fpGetppid.ToString + '/exe');
  11. end;
  12.  
  13. begin
  14.   writeln('name of parent process = ', GetParentPRocessName);
  15. end.

Unfortunately in my both terminals the result is
   name of parent process = /bin/bash
But it was an interesting approach, again I learned something. Thank you.

An alternative might be checking for the existence of the DISPLAY variable, which I'd expect to be set for /any/ program running under X11 (possibly also Wayland, but that would need to be tested).

The DISPLAY variable contains in both terminals ":0", so there is no difference.

Meanwhile I implemented the solution from reply #30. The problem is now solved. Thanks again.

TRon

  • Hero Member
  • *****
  • Posts: 2432
Re: [SOLVED] How to detect the configured codepage of my console (Linux)?
« Reply #37 on: January 20, 2023, 11:38:48 am »
Maybe, but it is obviously not universal method. Try to launch terminal from where your DE places it by default (menu, what's else). What do you see?
You get what you ask for  :)

So if you start your terminal and wait for the prompt what else do you think is going to be invoked by the DE terminal program ? Exactly: your default shell.

But that was not OP question. OP's literal words are: "....from where the program was started?"

So if you start your program with the gnome terminal then the gnome terminal is the parent. If you wait for gnome to display the prompt from your default terminal and then invoke your FPC program then it will be your default shell. That is a matter of walking up the process tree.

@MarkMLI:
The idea is that when you know which DE terminal was used that you are able to delve into its configuration settings. But in case the distinction is enough then that will probably do as well (which was indeed my first line of thought) I can't decide  for OP what is sufficient for OP's situation (not a lot of context there).

TRon

  • Hero Member
  • *****
  • Posts: 2432
Re: [SOLVED] How to detect the configured codepage of my console (Linux)?
« Reply #38 on: January 20, 2023, 11:40:26 am »
Unfortunately in my both terminals the result is
Then you have not invoked your program from the gnome terminal but from your default shell.

Could you elaborate on how you invoke your console written program ?

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: [SOLVED] How to detect the configured codepage of my console (Linux)?
« Reply #39 on: January 20, 2023, 11:50:59 am »
The DISPLAY variable contains in both terminals ":0", so there is no difference.

Except that it won't be defined if you're invoking the program from a non-graphical system console, which appears to be one of the things that at least some people think you're asking. I'd also expect it to be different if you're connecting via SSH etc. (but not VNC).

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

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: [SOLVED] How to detect the configured codepage of my console (Linux)?
« Reply #40 on: January 20, 2023, 12:22:49 pm »
Unfortunately in my both terminals the result is
Then you have not invoked your program from the gnome terminal but from your default shell.

Could you elaborate on how you invoke your console written program ?

I compiled your demo from reply #31. I started Gnome terminal via a desktop link as usual. Then I typed the name of the demo to start it.

TRon

  • Hero Member
  • *****
  • Posts: 2432
Re: [SOLVED] How to detect the configured codepage of my console (Linux)?
« Reply #41 on: January 20, 2023, 12:35:31 pm »
I compiled your demo from reply #31. I started Gnome terminal via a desktop link as usual. Then I typed the name of the demo to start it.
Thus, in practice and no matter what, your 'terminal' is always going to be your default shell. No wonder you are unable to see any difference with regards to codepage (for every invocation of your default shell it will be the settings of your default shell = same).

So what you should be looking for is not the parent but grandparent (as said simple mater of walking the tree).

Code: Pascal  [Select][+][-]
  1. program parenting;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   classes, sysutils, strutils, baseunix;
  7.  
  8. function GetProcessName(pid: TPID): string;
  9. begin
  10.   GetProcessName := fpReadLink('/proc/' + pid.ToString + '/exe');
  11. end;
  12.  
  13. function ReadLineFromFile(Filename: string): string;
  14. var
  15.   tf: TextFile;
  16. begin
  17.   AssignFile (tf, Filename);
  18.   Reset (tf);
  19.   ReadLn(tf, result);
  20.   CloseFile (tf)
  21. end;
  22.  
  23. procedure GetListOfParentsProcessNames(generations: integer; var pn: TStringlist);
  24. var
  25.   pid: TPID;
  26.   proc_stat_path: string;
  27.   stats: array of string;
  28. begin
  29.   pn.Clear;
  30.   pid := fpGetPID;
  31.  
  32.   while (pid<>0) and (generations>0) do
  33.   begin
  34.     proc_stat_path := '/proc/' + pid.ToString + '/stat';
  35.     stats := SplitString(ReadLineFromFile(proc_stat_path), ' ');
  36.     pid := stats[3].ToInteger;  // get pid of parent
  37.     pn.Add(GetProcessName(pid));
  38.     dec(generations);
  39.   end;
  40. end;
  41.  
  42. var
  43.   Lines: TStringList;
  44.   Line : string;
  45. begin
  46.   Lines := TStringList.Create;
  47.   GetListOfParentsProcessNames(2, Lines);
  48.   for Line in Lines do WriteLn(Line);
  49.   Lines.Free;
  50. end.
  51.  

Which, when i invoke the default DE terminal on my setup, displays for me:
Code: [Select]
$./parenting
/usr/bin/bash
/usr/bin/mate-terminal


Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: [SOLVED] How to detect the configured codepage of my console (Linux)?
« Reply #42 on: January 20, 2023, 01:08:55 pm »
So what you should be looking for is not the parent but grandparent (as said simple mater of walking the tree).

Yes, now it works. Thank you.

 

TinyPortal © 2005-2018