From patchwork Thu Apr 23 16:04:02 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vivien Kraus X-Patchwork-Id: 133831 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id D25AD4BA23CA for ; Thu, 23 Apr 2026 16:06:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D25AD4BA23CA Authentication-Results: sourceware.org; dkim=pass (2048-bit key, secure) header.d=planete-kraus.eu header.i=@planete-kraus.eu header.a=rsa-sha1 header.s=albinoniA header.b=vEPcmhzz X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from planete-kraus.eu (planete-kraus.eu [IPv6:2a00:5881:4008:2810::309]) by sourceware.org (Postfix) with ESMTPS id BBC4C4B8A6BF for ; Thu, 23 Apr 2026 16:05:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BBC4C4B8A6BF Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=planete-kraus.eu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=planete-kraus.eu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BBC4C4B8A6BF Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:5881:4008:2810::309 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776960351; cv=none; b=N5XRh1wosxVyj1l0yTozUgEJJ6i32nBi9k6Fc4b3m5B8UgeCPuzafqtjmK1Iszt0yRk6GS6WsEXupmuW/NgeN34PaGBLjqkQN+dd7wVaLFB+kQUanQWzeWcXCYl6xK27BzvtnupkNzNaoyyoFNJR/4JUNn8uwA+aqJQUju2c5Zc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776960351; c=relaxed/simple; bh=NMcMsHbHULVSPirFE/HLRgnZymx18YTMD8aFuw0gTXc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=LX0/LLSRNbMgGWRvFCEef938V7eF7G/gTO47IcdYqyTRTM03gSBtW3VB/6FKTJHyfJzohj9FDMS3AOoKoiH2dEFUYKd3j9bdiUFg8vn1It3JBtFh8CrZvqvcZZ4/gyX2NPOIJZ3BTgD8ggQYN2sp7cjjJSZHrPnngjmzqi3yW8o= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BBC4C4B8A6BF Received: from planete-kraus.eu (localhost [127.0.0.1]) by planete-kraus.eu (OpenSMTPD) with ESMTP id 74f0defd; Thu, 23 Apr 2026 16:05:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=planete-kraus.eu; h=from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=albinoniA; bh=glAiG7k ZQcjLAKraBTQ475yvyTk=; b=vEPcmhzznVmWu6FloOSLLXt80iPqfOQQBiA5q+4 dcgmZKqHeSWmtJ1SQu511AtE60RQH66V376wV4J0ZUlUKTxj//hJUpM+0jQ34JuK q9/AwYRtn0S9zOn1thK6Tdc78efmh9IakiK9Z+XzBc/4HKHeRn69SZmqhuAlqDAH s8fLsgdg7IC99kCd7qhQ2y+sQuwRRz6JPFqn8tSz4b4iNscSTYcHfnC4vP262VAR 26YIp7gphIms/jSdfsLJGbrgh3Os50mIjh+8LwjMHBAyzKxa1/rJ6IkS1upVenPq +sGdyfDyz146VnOOGXdiBA7bcvP0nu1H0Vp567Aof2aKing== Received: by planete-kraus.eu (OpenSMTPD) with ESMTPSA id 1f57b6bd (TLSv1.3:TLS_CHACHA20_POLY1305_SHA256:256:NO); Thu, 23 Apr 2026 16:05:40 +0000 (UTC) From: Vivien Kraus To: adhemerval.zanella@linaro.org, libc-alpha@sourceware.org Cc: Vivien Kraus Subject: [PATCH v22 4/9] posix: let the getopt caller choose the textdomain for translation Date: Thu, 23 Apr 2026 18:04:02 +0200 Message-ID: X-Mailer: git-send-email 2.52.0 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, JMQ_SPF_NEUTRAL, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Using the same solution as for the option translation context, a new opttextdomain variable is defined. Note that all options in the call to getopt_long are looked up in the same domain. --- manual/argp.texi | 7 +++++-- manual/getopt.texi | 7 ++++++- posix/bits/getopt_ext.h | 3 ++- posix/getopt.c | 34 ++++++++++++++++++++++------------ posix/getopt1.c | 31 ++++++++++++++++++++++++------- posix/getopt_int.h | 9 +++++---- posix/tstgetoptl.c | 13 ++++++++++--- 7 files changed, 74 insertions(+), 30 deletions(-) diff --git a/manual/argp.texi b/manual/argp.texi index 97456ef20e..50d67b6c55 100644 --- a/manual/argp.texi +++ b/manual/argp.texi @@ -208,8 +208,11 @@ messages. @xref{Argp Help Filtering}. If non-zero, the strings used in the argp library are translated using the domain described by this string. If zero, the current default domain is used. The long option names are always translated with the -current default domain, and with the @samp{"command-line option"} -disambiguation string. +current default domain (not this one), and with the +@samp{"command-line option"} disambiguation string. This is because +all the option names, including those defined in sub-parsers, must be +in the same textdomain for @command{getopt} to process the options +correctly. @end table @end deftp diff --git a/manual/getopt.texi b/manual/getopt.texi index fe45ae55a3..bd58e1b7d8 100644 --- a/manual/getopt.texi +++ b/manual/getopt.texi @@ -238,7 +238,7 @@ was seen. @end table @end deftp -@deftypefun int getopt_long_enable_translations (const char *@var{msgctxt}) +@deftypefun int getopt_long_enable_translations (const char *@var{msgctxt}, const char *@var{textdomain}) @deftypefunx void getopt_long_disable_translations (void) @standards{GNU, getopt.h} @c FIXME: I copied that from getopt_long, but I don't understand @@ -263,6 +263,11 @@ should be a non-NULL string to disambiguate option name translations. Passing NULL, or calling @code{getopt_long_disable_translations()}, will disable option name translation. +Option names may be translated in a textdomain that is not currently +the default (@pxref{Interface to gettext, , The Interface, gettext, +the GNU Gettext manual}). If this is @code{NULL} (the default), the +translation will be searched in the current text domain. + @code{getopt_long_enable_translations} returns 0 on success, or -1 and sets errno. @end deftypefun diff --git a/posix/bits/getopt_ext.h b/posix/bits/getopt_ext.h index 8f065bbe9f..f40cb5046e 100644 --- a/posix/bits/getopt_ext.h +++ b/posix/bits/getopt_ext.h @@ -71,7 +71,8 @@ extern int getopt_long_only (int ___argc, char *__getopt_argv_const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind) __THROW __nonnull ((2, 3)); -extern int getopt_long_enable_translations (const char *__msgctxt) +extern int getopt_long_enable_translations (const char *__msgctxt, + const char *__textdomain) __attribute_warn_unused_result__; extern void getopt_long_disable_translations (void); diff --git a/posix/getopt.c b/posix/getopt.c index 6717449b5c..ae823eec29 100644 --- a/posix/getopt.c +++ b/posix/getopt.c @@ -185,19 +185,23 @@ exchange (char **argv, struct _getopt_data *d) /* Return true iff translation_context is not NULL, a translation for opt_name has been found and it matches the substring from argument, length argument_length. + + The translate function pointer is like dpgettext. */ static bool match_translated_option_name (char *(*translate) (const char *, const char *, - char **), + const char *, char **), const char *argument, size_t argument_length, const char *translation_context, + const char *opt_textdomain, const char *opt_name) { const char *translated = opt_name; char *translation_buffer = NULL; bool matches = false; if (translate != NULL) - translated = translate (translation_context, opt_name, &translation_buffer); + translated = translate (opt_textdomain, translation_context, + opt_name, &translation_buffer); if (strncmp (translated, argument, argument_length) != 0) matches = false; @@ -222,7 +226,7 @@ process_long_option (int argc, char **argv, const char *optstring, int long_only, struct _getopt_data *d, int print_errors, const char *prefix, char *(*translate) (const char *, const char *, - char **)) + const char *, char **)) { char *nameend; size_t namelen; @@ -254,9 +258,9 @@ process_long_option (int argc, char **argv, const char *optstring, /* Didn't find an exact match, try with translated option names. */ for (p = longopts, option_index = 0; p->name; p++, option_index++) - if (match_translated_option_name (translate, - d->__nextchar, namelen, - d->optctxt, p->name)) + if (match_translated_option_name (translate, d->__nextchar, namelen, + d->optctxt, d->opttextdomain, + p->name)) { /* Exact match found with translation. */ pfound = p; @@ -389,7 +393,8 @@ process_long_option (int argc, char **argv, const char *optstring, { if (print_errors) { - translated_option_name = translate (d->optctxt, pfound->name, + translated_option_name = translate (d->opttextdomain, d->optctxt, + pfound->name, &translation_buffer); if (strcmp (translated_option_name, pfound->name) != 0) /* Print both names of the option. */ @@ -418,7 +423,8 @@ process_long_option (int argc, char **argv, const char *optstring, { /* Same dichotomy as when the option does not allow an argument. */ - translated_option_name = translate (d->optctxt, pfound->name, + translated_option_name = translate (d->opttextdomain, d->optctxt, + pfound->name, &translation_buffer); if (strcmp (translated_option_name, pfound->name) != 0) fprintf (stderr, @@ -542,7 +548,8 @@ int _getopt_internal_r (int argc, char **argv, const char *optstring, const struct option *longopts, int *longind, int long_only, struct _getopt_data *d, int posixly_correct, - char *(*translate) (const char *, const char *, char **)) + char *(*translate) (const char *, const char *, + const char *, char **)) { int print_errors = d->opterr; @@ -778,14 +785,17 @@ int _getopt_internal (int argc, char **argv, const char *optstring, const struct option *longopts, int *longind, int long_only, int posixly_correct, - char *(*translate) (const char *, const char *, char **), - const char *ctxt) + char *(*translate) (const char *, const char *, + const char *, char **), + const char *ctxt, + const char *domain) { int result; getopt_data.optind = optind; getopt_data.opterr = opterr; getopt_data.optctxt = ctxt; + getopt_data.opttextdomain = domain; result = _getopt_internal_r (argc, argv, optstring, longopts, longind, long_only, &getopt_data, @@ -808,7 +818,7 @@ _getopt_internal (int argc, char **argv, const char *optstring, { \ return _getopt_internal (argc, (char **)argv, optstring, \ NULL, NULL, 0, POSIXLY_CORRECT, \ - NULL, NULL); \ + NULL, NULL, NULL); \ } #ifdef _LIBC diff --git a/posix/getopt1.c b/posix/getopt1.c index 87fe067655..cc844a0508 100644 --- a/posix/getopt1.c +++ b/posix/getopt1.c @@ -36,9 +36,15 @@ char *optctxt = NULL; +/* Callers store the textdomain in which the option names are to be + looked up. */ + +char *opttextdomain = NULL; + /* FIXME: use pgettext_expr. */ static char * -do_translate (const char *context, const char *msgid, char **allocated) +do_translate (const char *domain, const char *context, const char *msgid, + char **allocated) { char *full_msgid; const char *translated = msgid; @@ -51,7 +57,7 @@ do_translate (const char *context, const char *msgid, char **allocated) *allocated = full_msgid; if (output_length >= 0) { - translated = __dcgettext (NULL, full_msgid, LC_MESSAGES); + translated = __dcgettext (domain, full_msgid, LC_MESSAGES); if (strcmp (translated, full_msgid) == 0) { /* No translation for this context and message, so drop @@ -72,7 +78,8 @@ getopt_long (int argc, char *__getopt_argv_const *argv, const char *options, const struct option *long_options, int *opt_index) { return _getopt_internal (argc, (char **) argv, options, long_options, - opt_index, 0, 0, do_translate, optctxt); + opt_index, 0, 0, do_translate, + optctxt, opttextdomain); } int @@ -95,7 +102,8 @@ getopt_long_only (int argc, char *__getopt_argv_const *argv, const struct option *long_options, int *opt_index) { return _getopt_internal (argc, (char **) argv, options, long_options, - opt_index, 1, 0, do_translate, optctxt); + opt_index, 1, 0, do_translate, + optctxt, opttextdomain); } int @@ -111,18 +119,27 @@ static void disable_translations (void) { free (optctxt); + free (opttextdomain); optctxt = NULL; + opttextdomain = NULL; } int -getopt_long_enable_translations (const char *msgctxt) +getopt_long_enable_translations (const char *msgctxt, const char *textdomain) { disable_translations (); if (msgctxt != NULL) { optctxt = __strdup (msgctxt); - if (optctxt == NULL) - return -1; + if (textdomain) + opttextdomain = __strdup (textdomain); + if (optctxt == NULL + || (textdomain != NULL && opttextdomain == NULL)) + { + /* strdup failure */ + disable_translations (); + return -1; + } } return 0; } diff --git a/posix/getopt_int.h b/posix/getopt_int.h index fcfec242c1..a770776dc1 100644 --- a/posix/getopt_int.h +++ b/posix/getopt_int.h @@ -24,14 +24,14 @@ /* The translate argument here is optional (can be NULL), it is used to avoid depending on the gettext functions in the posix getopt - function. */ + function. It is like dpgettext. */ extern int _getopt_internal (int ___argc, char **___argv, const char *__shortopts, const struct option *__longopts, int *__longind, int __long_only, int __posixly_correct, char *(*translate) (const char *, const char *, - char **), - const char *__optctxt); + const char *, char **), + const char *__optctxt, const char *__optdomain); /* Reentrant versions which can handle parsing multiple argument @@ -74,6 +74,7 @@ struct _getopt_data int optopt; char *optarg; const char *optctxt; + const char *opttextdomain; /* Internal members. */ @@ -111,7 +112,7 @@ extern int _getopt_internal_r (int ___argc, char **___argv, int __long_only, struct _getopt_data *__data, int __posixly_correct, char *(*translate) (const char *, const char *, - char **)); + const char *, char **)); extern int _getopt_long_r (int ___argc, char **___argv, const char *__shortopts, diff --git a/posix/tstgetoptl.c b/posix/tstgetoptl.c index 1e970ad407..ad5755ddbd 100644 --- a/posix/tstgetoptl.c +++ b/posix/tstgetoptl.c @@ -49,8 +49,12 @@ prepare_localedir (void) TEST_VERIFY_EXIT (bindtextdomain ("tstgetoptl", OBJPFX "domaindir") != NULL); TEST_VERIFY_EXIT (textdomain ("tstgetoptl") != NULL); /* Check that the catalog is OK: */ - TEST_COMPARE_STRING (gettext (TRANSLATION_CONTEXT "\004" "color"), "colour"); - TEST_COMPARE_STRING (gettext (TRANSLATION_CONTEXT "\004" "flavor"), "flavour"); + TEST_COMPARE_STRING (dgettext ("tstgetoptl", + TRANSLATION_CONTEXT "\004" "color"), + "colour"); + TEST_COMPARE_STRING (dgettext ("tstgetoptl", + TRANSLATION_CONTEXT "\004" "flavor"), + "flavour"); } static char ** @@ -70,6 +74,7 @@ static void do_my_test (bool with_optctxt) { static const char *translation_context = TRANSLATION_CONTEXT; + static const char *translation_textdomain = "tstgetoptl"; int argc; char **argv = prepare_argv (&argc); static const struct option options[] = @@ -91,7 +96,9 @@ do_my_test (bool with_optctxt) bool found_flavor = false; if (with_optctxt) - TEST_VERIFY_EXIT (getopt_long_enable_translations (translation_context) == 0); + TEST_VERIFY_EXIT (getopt_long_enable_translations (translation_context, + translation_textdomain) + == 0); else getopt_long_disable_translations (); optind = 0;