For Linux G'MIC users (I don't have Linux, and I'm not sure what I'm doing here): Return exit code from bash

Is this the proper way of sending exit code accordingly to locale used within Linux?

foo_exit:
exec 1,"enc=\$(locale charmap); case \$enc in UTF-8) exit 0;; ISO-8859-1) exit 1;; ISO-8859-15) exit 2;; IBM850) exit 3;; IBM858) exit 4;; IBM437) exit 5;; WINDOWS-1252) exit 6;; *) exit 255;; esac"
e ${}

I’m working on a string processing library, and I just got a way to map cli inputs into their expected value as G’MIC internally use unicode (it seems so) as I check how input are translated. Or maybe it’s translating to Windows-1251 for non-cli context in some cases.

For Windows, I got it to work:
Use this exec instead for Windows

exec 1,"powershell -Command \"exit [int]((chcp) -replace '\\D+(\\d+).*','"$"1')\""

EDIT:

And this for Linux?

foo_exec:
exec 1,"bash -c 'enc=\$(locale charmap); declare -A m=([UTF-8]=0 [ISO-8859-1]=1
[ISO-8859-15]=2 [IBM850]=3 [IBM858]=4 [IBM437]=5 [WINDOWS-1252]=6); exit
\${m[\$enc]:-255}'" e {${}>0xff?${}>>8:${}}

first of all what high level problem are you trying to solve?

Detecting codepage used by the terminal itself and I used exit code to transfer that info to G’MIC.

TBH i would just parse the string that you get within your gmic script.

also:

LC_ALL=C locale charmap
ANSI_X3.4-1968

But, how? I did it this way because G’MIC exec can only return numbers. I’ll add more charmaps. Not sure what else I can add, as my main targets are US/Europe/Latin America.

Both Bash parts look OK to me, be it the case-based strategy or the associative-array-based one.

Technically exit codes are not really used for that kind of stuff (basically anything ≠0 is for errors), but I guess you’re in a weird context anyway, so…

wouldnt it be cleaner to have access to environment variables within gmic scripts?

And faster too. Mine runs in .179 s and the bottle neck is in exec. But, it doesn’t exist. However, I’m still keeping it regardless of speed.

Results (Top is using my code, bottom is using direct G’MIC conversion):

C:\Windows\System32>gmic e {`[${rep_txt2uints\ ë}]`}
[gmic]./ Start G'MIC interpreter (v.3.6.1).
ë
[gmic]./ End G'MIC interpreter.

C:\Windows\System32>gmic e {`'ë'`}
[gmic]./ Start G'MIC interpreter (v.3.6.1).
δ
[gmic]./ End G'MIC interpreter.