[v22,4/9] posix: let the getopt caller choose the textdomain for translation

Message ID f5ce925d786638e616ef665c5f49980958675283.1776957778.git.vivien@planete-kraus.eu (mailing list archive)
State New
Headers
Series Support translated long option names in getopt and argp |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

Vivien Kraus April 23, 2026, 4:04 p.m. UTC
  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(-)
  

Patch

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;