[SOLVED] How to detect the configured codepage of my console (Linux)? lazarus / 32-Linux ---------------------------------------------------------------------- Hartmut · 2023-01-17 17:56 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. ---------------------------------------------------------------------- MarkMLl · 2023-01-17 18:30 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 ---------------------------------------------------------------------- tetrastes · 2023-01-17 20:17 Quote from: Hartmut on January 17, 2023, 06:56:48 pmI 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 · 2023-01-17 20:58 Quote from: tetrastes on January 17, 2023, 09:17:22 pmAdd uses cwstring (see https://wiki.freepascal.org/FPC_Unicode_support#DefaultS ystemCodePage). Good point. I thought Lazarus did that automatically under some circumstances? MarkMLl ---------------------------------------------------------------------- Bart · 2023-01-17 22:36 Quote from: MarkMLl on January 17, 2023, 09:58:47 pmQuote from: tetrastes on January 17, 2023, 09:17:22 pmAdd 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 · 2023-01-18 10:19 Quote from: MarkMLl on January 17, 2023, 07:30:59 pmThe 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. Quote from: tetrastes on January 17, 2023, 09:17:22 pmAdd uses cwstring (see https://wiki.freepascal.org/FPC_Unicode_support#DefaultS ystemCodePage). 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 · 2023-01-18 10:45 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 ---------------------------------------------------------------------- KodeZwerg · 2023-01-18 10:52 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! ---------------------------------------------------------------------- Thaddy · 2023-01-18 10:59 type "export" in a console. The configuration can be used in Tprocess and parsed. or:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetActiveCodepage: string;begin  result := GetEnvironmentVariable('LC_ALL');  if result= '' then    result:= GetEnvironmentVariable('LC_CTYPE') else  if result= '' then    result:= GetEnvironmentVariable('LANG') else  if result= '' then    result := GetEnvironmentVariable('LANGUAGE') else  if result = '' then    result:= GetEnvironmentVariable('LC_MESSAGES') else  if result= '' then    result:= 'UTF-8'; // by no means fool proofend; ---------------------------------------------------------------------- MarkMLl · 2023-01-18 11:08 Quote from: Thaddy on January 18, 2023, 11:59:09 amtype "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 ---------------------------------------------------------------------- Thaddy · 2023-01-18 11:10 Ok, Mark... I added a function to my reply... Find the fool.... Or simply use Tprocess and call locale -a ---------------------------------------------------------------------- Hartmut · 2023-01-18 11:46 Quote from: MarkMLl on January 18, 2023, 11:45:24 amDoes  locale charmap  show anything useful? Command "locale charmap" shows in both consoles "UTF-8", so there is no difference. Quote from: Thaddy on January 18, 2023, 11:59:09 amtype "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. QuoteCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetActiveCodepage: string;begin  result := GetEnvironmentVariable('LC_ALL');  if result= '' then    result:= GetEnvironmentVariable('LC_CTYPE') else  if result= '' then    result:= GetEnvironmentVariable('LANG') else  if result= '' then    result := GetEnvironmentVariable('LANGUAGE') else  if result = '' then    result:= GetEnvironmentVariable('LC_MESSAGES') else  if result= '' then    result:= 'UTF-8'; // by no means fool proofend; Unfortunately all this environment variables either contain the same or do not exist in both consoles. ---------------------------------------------------------------------- Thaddy · 2023-01-18 11:48 II am aware of that. Alas I had to write "by no means fool proof" ---------------------------------------------------------------------- Hartmut · 2023-01-18 11:50 Quote from: Thaddy on January 18, 2023, 12:10:35 pmOr simply use Tprocess and call locale -a Command "locale -a" returns the same result in both consoles. ---------------------------------------------------------------------- MarkMLl · 2023-01-18 12:05 Quote from: Hartmut on January 18, 2023, 12:46:21 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 ---------------------------------------------------------------------- tetrastes · 2023-01-18 12:13 @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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ locale -c charmaplocale: Cannot set LC_CTYPE to default locale: No such file or directorylocale: Cannot set LC_MESSAGES to default locale: No such file or directorylocale: Cannot set LC_ALL to default locale: No such file or directoryLC_CTYPEANSI_X3.4-1968which is the same as 20127. ---------------------------------------------------------------------- Hartmut · 2023-01-18 12:47 Thank you tetrastes for your reply. Quote from: tetrastes on January 18, 2023, 01:13:56 pmHow 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. QuoteBut 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i CC.UTF- 8de_AT.utf8de_BE.utf8de_CH.utf8de_DE.utf8de_IT.utf8de_LI.utf8de_LU.utf 8en_AGen_AG.utf8en_AU.utf8en_BW.utf8en_CA.utf8en_DK.utf8en_GB.utf8en_H K.utf8en_IE.utf8en_ILen_IL.utf8en_INen_IN.utf8en_NGen_NG.utf8en_NZ.utf 8en_PH.utf8en_SG.utf8en_US.utf8en_ZA.utf8en_ZMen_ZM.utf8en_ZW.utf8POSI X ---------------------------------------------------------------------- tetrastes · 2023-01-18 13:04 Quote from: Hartmut on January 18, 2023, 01:47:22 pmThank you tetrastes for your reply. Quote from: tetrastes on January 18, 2023, 01:13:56 pmHow 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 from: Hartmut on January 18, 2023, 01:47:22 pm QuoteBut 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 · 2023-01-18 15:04 Quote from: tetrastes on January 18, 2023, 02:04:33 pmQuote from: Hartmut on January 18, 2023, 01:47:22 pmI 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 · 2023-01-18 18:58 Quote from: Hartmut on January 18, 2023, 04:04:40 pmHmm, how would you detect the 2 different codepages? Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ LANG=C; export LANG$ locale -c charmapLC_CTYPEANSI_X3.4-1968Your code (with uses cwstring) outputs 20127. This is correct. Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ LANG=C.utf8; export LANG$ locale -c charmapLC_CTYPEUTF-8Your 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. ---------------------------------------------------------------------- MarkMLl · 2023-01-18 20:00 Quote from: tetrastes on January 18, 2023, 07:58:23 pmIf 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 ---------------------------------------------------------------------- tetrastes · 2023-01-18 20:44 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 · 2023-01-18 20:53 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 ---------------------------------------------------------------------- tetrastes · 2023-01-18 22:09 Quote from: MarkMLl on January 18, 2023, 09:53:05 pmOP: 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: QuoteI 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 · 2023-01-19 11:37 Quote from: tetrastes on January 18, 2023, 07:58:23 pmQuote from: Hartmut on January 18, 2023, 04:04:40 pmHmm, how would you detect the 2 different codepages? Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ LANG=C; export LANG$ locale -c charmapLC_CTYPEANSI_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? Quote from: MarkMLl on January 18, 2023, 09:00:02 pmQuote from: tetrastes on January 18, 2023, 07:58:23 pmIf 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 · 2023-01-19 13:52 Quote from: Hartmut on January 19, 2023, 12:37:31 pmQuote from: tetrastes on January 18, 2023, 07:58:23 pmQuote from: Hartmut on January 18, 2023, 04:04:40 pmHmm, how would you detect the 2 different codepages? Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ LANG=C; export LANG$ locale -c charmapLC_CTYPEANSI_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. ---------------------------------------------------------------------- Hartmut · 2023-01-19 14:04 Quote from: tetrastes on January 19, 2023, 02:52:16 pmI'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 · 2023-01-19 17:58 Quote from: Hartmut on January 19, 2023, 03:04:51 pmSorry 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses Dos; begin    if GetEnv('GNOME_TERMINAL_SCREEN') = '' then        writeln('I am running at some other Terminal')    else        writeln('I am running at GNOME Terminal');end. ---------------------------------------------------------------------- dsiders · 2023-01-19 18:31 Quote from: tetrastes on January 19, 2023, 06:58:08 pmQuote from: Hartmut on January 19, 2023, 03:04:51 pmSorry 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses Dos; begin    if GetEnv('GNOME_TERMINAL_SCREEN') = '' then        writeln('I am running at some other Terminal')    else        writeln('I am running at GNOME Terminal');end. Isn't this what the TERM_PROGRAM environment variable is for? ---------------------------------------------------------------------- tetrastes · 2023-01-19 18:41 Quote from: dsiders on January 19, 2023, 07:31:53 pmIsn't this what the TERM_PROGRAM environment variable is for? I don't have this environment variable at my system. ---------------------------------------------------------------------- Hartmut · 2023-01-20 07:26 Quote from: tetrastes on January 19, 2023, 06:58:08 pmSomething like this? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses Dos; begin    if GetEnv('GNOME_TERMINAL_SCREEN') = '' then        writeln('I am running at some other Terminal')    else        writeln('I am running at GNOME Terminal');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. Quote from: tetrastes on January 19, 2023, 07:41:18 pmQuote from: dsiders on January 19, 2023, 07:31:53 pmIsn'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 · 2023-01-20 09:11 Quote from: Hartmut on January 17, 2023, 06:56:48 pm... 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program parent; {$mode objfpc}{$H+} uses  sysutils, baseunix; function GetParentProcessName: string;begin  GetParentProcessName := fpReadLink('/proc/' + fpGetppid.ToString + '/exe');end; begin  writeln('name of parent process = ', GetParentPRocessName);end. ---------------------------------------------------------------------- tetrastes · 2023-01-20 09:28 Quote from: TRon on January 20, 2023, 10:11:50 amQuote from: Hartmut on January 17, 2023, 06:56:48 pm... 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program parent; {$mode objfpc}{$H+} uses  sysutils, baseunix; function GetParentProcessName: string;begin  GetParentProcessName := fpReadLink('/proc/' + fpGetppid.ToString + '/exe');end; begin  writeln('name of parent process = ', GetParentPRocessName);end.   Have you tried your code? It outputs Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i name of parent process = /usr/bin/bashat any terminal. ---------------------------------------------------------------------- TRon · 2023-01-20 09:35 Quote from: tetrastes on January 20, 2023, 10:28:48 amHave you tried your code? It outputs Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i name of parent process = /usr/bin/bashat 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 · 2023-01-20 09:50 Quote from: TRon on January 20, 2023, 10:35:24 amQuote from: tetrastes on January 20, 2023, 10:28:48 amHave you tried your code? It outputs Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i name of parent process = /usr/bin/bashat 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 · 2023-01-20 10:01 Quote from: tetrastes on January 20, 2023, 10:50:31 amQuote from: TRon on January 20, 2023, 10:35:24 amYes, 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 ---------------------------------------------------------------------- Hartmut · 2023-01-20 10:33 Quote from: TRon on January 20, 2023, 10:11:50 amIf 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program parent; {$mode objfpc}{$H+} uses  sysutils, baseunix; function GetParentProcessName: string;begin  GetParentProcessName := fpReadLink('/proc/' + fpGetppid.ToString + '/exe');end; begin  writeln('name of parent process = ', GetParentPRocessName);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. Quote from: MarkMLl on January 20, 2023, 11:01:17 amAn 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 · 2023-01-20 10:38 Quote from: tetrastes on January 20, 2023, 10:50:31 amMaybe, 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 · 2023-01-20 10:40 Quote from: Hartmut on January 20, 2023, 11:33:01 amUnfortunately 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 · 2023-01-20 10:50 Quote from: Hartmut on January 20, 2023, 11:33:01 amThe 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 ---------------------------------------------------------------------- Hartmut · 2023-01-20 11:22 Quote from: TRon on January 20, 2023, 11:40:26 amQuote from: Hartmut on January 20, 2023, 11:33:01 amUnfortunately 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 · 2023-01-20 11:35 Quote from: Hartmut on January 20, 2023, 12:22:49 pmI 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program parenting; {$mode objfpc}{$H+} uses  classes, sysutils, strutils, baseunix; function GetProcessName(pid: TPID): string;begin  GetProcessName := fpReadLink('/proc/' + pid.ToString + '/exe');end; function ReadLineFromFile(Filename: string): string;var  tf: TextFile;begin  AssignFile (tf, Filename);  Reset (tf);  ReadLn(tf, result);  CloseFile (tf)end; procedure GetListOfParentsProcessNames(generations: integer; var pn: TStringlist);var  pid: TPID;  proc_stat_path: string;  stats: array of string;begin  pn.Clear;  pid := fpGetPID;   while (pid<>0) and (generations>0) do  begin    proc_stat_path := '/proc/' + pid.ToString + '/stat';    stats := SplitString(ReadLineFromFile(proc_stat_path), ' ');    pid := stats[3].ToInteger;  // get pid of parent    pn.Add(GetProcessName(pid));    dec(generations);  end;end; var  Lines: TStringList;  Line : string;begin  Lines := TStringList.Create;  GetListOfParentsProcessNames(2, Lines);  for Line in Lines do WriteLn(Line);  Lines.Free;end.  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 · 2023-01-20 12:08 Quote from: TRon on January 20, 2023, 12:35:31 pmSo 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.