LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   Query on /etc/profile.d/glib2.sh (https://www.linuxquestions.org/questions/slackware-14/query-on-etc-profile-d-glib2-sh-4175593667/)

GazL 11-16-2016 02:50 PM

Query on /etc/profile.d/glib2.sh
 
Code:

#!/bin/sh
#
# Description:  This script sets the environment variables G_FILENAME_ENCODING
# and G_BROKEN_FILENAMES for the glib-2.0 library.
#
# G_FILENAME_ENCODING
#      This environment variable can be set to a comma-separated list of
#      character set names.  GLib assumes that filenames are encoded in the
#      first character set from that list rather than in UTF-8
.  The special
#      token "@locale" can be used to specify the character set for the
#      current locale.
#
# G_BROKEN_FILENAMES
#      If this environment variable is set, GLib assumes that filenames are
#      in the locale encoding rather than in UTF-8.

# If the LANG you have set contains any form of "UTF", we will guess you are
# using a UTF-8 locale.  Hopefully we're correct.
if echo $LANG | grep -iq UTF ; then
  export G_FILENAME_ENCODING="@locale"
fi


# It doesn't hurt to export this since G_FILENAME_ENCODING takes priority
# over G_BROKEN_FILENAMES:
export G_BROKEN_FILENAMES=1

That "rather than utf8" comment suggests that utf8 is the default, in which case it seems like that if statement is actually the wrong way around and needs to be negated: if the default is to assume utf-8 then you'd only need to set it if you're not using a utf8 locale. No?

Or, given that @locale sounds like it ought to work for both utf8 and non-utf8 locales, why not just set it to @locale which should work in both cases.

I freely admit that I know very little about glib. Am I looking at this wrong?


update:

After reading this it sounds like G_FILENAME_ENCODING='@locale' and G_BROKEN_FILENAMES=1 do the same thing?

GazL 11-17-2016 08:48 AM

Actually after doing some reading on the subject, I've come to the conclusion that it's all just broken anyway and the only sensible thing to do is use a utf8 locale, in which case non of these settings are of any use anyway.

Didier Spaier 11-17-2016 09:26 AM

... And the names of many locales encoded in UTF-8 do not include UTF or utf.

This script lists them:
Code:

#/bin/sh
for i in $(locale -a); do
    if printf $i|grep -viq utf8; then
        if LANG=$i locale -k charmap|grep -q UTF-8; then
            echo $i
        fi
    fi
done


GazL 11-17-2016 09:35 AM

Yep, I'd already changed libglib.sh to be:

Code:

if [ "$(locale -k charmap)" = 'charmap="UTF-8"' ]; then
  export G_FILENAME_ENCODING="UTF-8"
else
  export G_BROKEN_FILENAMES=1
fi

Which would allow individual users who are using non-utf8 locales on a primarily utf8 based system to have the filenames converted for them, but then I realised that as this only effects glib, all the other utilities and non-glib programs would still have to deal with incompatible charsets and that it doesn't really get you anywhere unless you only ever use glib applications.

At which point, I lost heart and went to eat some chocolate to make me feel better.

GazL 06-13-2021 08:03 AM

This thread got mentioned in another thread, so I thought it best to reply here in the original topic to keep things tidy.

Quote:

Originally Posted by NonNonBa (Post 6258438)
Thank you. But at the end, GazL just improves the way they are written, not enlightening their logic he finds confusing too.

The logic of the above code snippet is essentially correct, however, it's unnecessarily complicated as in practice it'd be no different than just setting G_FILENAME_ENCODING='@locale' regardless of locale used.

G_BROKEN_FILENAMES=1 is just an older and misleadingly named way of specifying G_FILENAME_ENCODING=@locale. They do exactly the same thing.

One only really needs to set this when not using a utf-8 locale as the default behaviour is to run as if G_FILENAME_ENCODING=UTF-8,@locale was specified, which in the case of utf-8 locales is essentially the same as above.

The reason why non-utf8 locales will want to set G_FILENAME_ENCODING=@locale is that it's possible for some non-utf8 sequence to be interpreted as a valid utf8 sequence, so it's safest to prevent that.

P.S. this is really just revisiting some of the things I said in post #1, only I'm a little more confident in my understand of all this 4 years later. ;)

NonNonBa 06-14-2021 05:33 AM

Quote:

Originally Posted by GazL (Post 6258631)
The logic of the above code snippet is essentially correct

Not really. As you say, the only case you can get any benefit from G_FILENAME_ENCODING is when you are not using UTF-8. So, if one doesn't want to bloat the environment by always setting it, the only sound code should IMHO rather be:

Code:

if [ ! "$(locale -k charmap)" = 'charmap="UTF-8"' ]; then
  # Let's be conservative, keeping G_BROKEN_FILENAMES.
  export G_FILENAME_ENCODING="@locale" G_BROKEN_FILENAMES=1
fi

Otherwise, your setup is already as glib expects. :)

GazL 06-14-2021 06:02 AM

You don't need to set both. One or the other will suffice.

As you say, if you care about environment size then you may want to also wrap it in a conditional so that it's not taking up env space when unneeded. Personally I just chmod -x this file.

NonNonBa 06-14-2021 12:24 PM

Deleted: didn't make sense; you're right GazL.


All times are GMT -5. The time now is 11:06 AM.