Is GOption ascii-only on linux?

Hello everyone,

I think GOption is ascii-only on linux (though I’m often wrong!). For example, with this test program:

/* compile with
 *  gcc -Wall try354.c $(pkg-config glib-2.0 --cflags --libs)
 */

#include <stdio.h>
#include <glib.h>

static char *main_option_expression = NULL;

static GOptionEntry main_batch_options[] = {
    { "expression", 'e', 0, G_OPTION_ARG_STRING, &main_option_expression,
        "evaluate and print EXPRESSION", "EXPRESSION" },
    { NULL }
};

int
main(int argc, char **argv)
{   
    GOptionContext *context = g_option_context_new("goption-test");
    GOptionGroup *main_group = g_option_group_new(NULL, NULL, NULL, NULL, NULL);
    g_option_group_add_entries(main_group, main_batch_options);
    g_option_context_set_main_group(context, main_group);

    GError *error = NULL;
    if (!g_option_context_parse(context, &argc, &argv, &error)) {
        if (error) {
            fprintf(stderr, "%s\n", error->message);
            g_error_free(error);
        }
        
        exit(1);
    }

    if (main_option_expression) 
        printf("expression = %s\n", main_option_expression);
    
    return 0;
}
```

I see:

$ ./a.out -e 12
expression = 12
$ ./a.out -e 😀
Invalid byte sequence in conversion input
$

Looking at goption.c:1150, it has:

case G_OPTION_ARG_STRING:
  {
    gchar *data;

#ifdef G_OS_WIN32
    if (!context->strv_mode)
      data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
    else
      data = g_strdup (value);
#else
    data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
#endif

ie. on non-win32 platforms it always calls g_locale_to_utf8(), and the default locale is "ANSI_X3.4-1968", which will flag an error for utf8 input. On Windows, you can use g_option_context_parse_strv() instead and avoid this conversion, but on linux it seems to be ASCII only.

Is there a way around this?

(edit: this was with glib 2.88 on ubuntu 26.04, but glib main seems to be the same in this respect)

Oh, I suppose I could tag the argument as G_OPTION_ARG_FILENAME, but that seems very unintuitive, and maybe it would give the wrong behaviour on windows?

You probably need to call setlocale(NULL) at the top of main(), as you would for any program that supports internationalization?

I’ve never heard of that locale. In general, GNOME programs will often crash or misbehave if the locale is not a UTF-8 locale.

Ah interesting! I tried adding:

setlocale(LC_ALL, NULL);

to the start of main(), but it still fails with “Invalid byte sequence in conversion input”.

and the default locale is "ANSI_X3.4-1968",

Sorry, I misspoke, I meant the default charset on this system. I poked about a bit and it’s what I see for nl_langinfo() in glib/libcharset/localcharset.c:

289	  codeset = nl_langinfo (CODESET);
(gdb) n
431	  return codeset;
(gdb) p codeset
$3 = 0x7ffff7bdfce8 <_nl_C_codeset> "ANSI_X3.4-1968"
(gdb) p CODESET
$4 = _NL_CTYPE_CODESET_NAME
(gdb)

My $LANG is en_GB.UTF-8so it should be a utf8 locale, I think.

I could try to reproduce this in a dockerfile if that would help.

(edit: this is Ubuntu 26.04, if that helps)

setlocale(LC_ALL, NULL);

according to setlocale(3) it’s supposed to be setlocale(LC_ALL, "")

int main(int argc, char **argv) {   
  g_print("locale: %s\n", setlocale(LC_ALL, ""));
  ...
% LANG=en_GB.UTF-8 ./a.out -e 😀
locale: en_GB.UTF-8
expression = 😀

That’s it! Thank you very much @yvs2014 and @mcatanzaro

Thanks for the correction!