[PATCH v5 4/4] posix: let the getopt caller choose the textdomain for translation

Vivien Kraus vivien@planete-kraus.eu
Wed Jun 4 22:06:18 GMT 2025


Using the same solution as for the option translation context, a new
opttextdomain variable is defined.
---
 manual/argp.texi        |  7 +++++--
 manual/getopt.texi      | 11 +++++++++--
 posix/Versions          |  2 +-
 posix/bits/getopt_ext.h |  5 +++++
 posix/getopt.c          | 30 +++++++++++++++++++-----------
 posix/getopt1.c         | 15 +++++++++++----
 posix/getopt_int.h      | 11 +++++++----
 posix/tstgetoptl.c      |  9 +++------
 8 files changed, 60 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 15994400dd..604f27a073 100644
--- a/manual/getopt.texi
+++ b/manual/getopt.texi
@@ -59,8 +59,15 @@ names in the current textdomain. Since option names may be short words
 instead of long sentences, they may have different translations in
 other places of the program.  @xref{Contexts, , Using contexts for
 solving ambiguities, gettext, the GNU Gettext manual}, for more
-information.  If this is @code{NULL}, then the translated option names
-will not be processed.
+information.  If this is @code{NULL} (the default), then the
+translated option names will not be processed.
+@end deftypevar
+
+@deftypevar {const char *} opttextdomain
+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.
 @end deftypevar
 
 @deftypefun int getopt (int @var{argc}, char *const *@var{argv}, const char *@var{options})
diff --git a/posix/Versions b/posix/Versions
index 76af54306e..ae237ce9d4 100644
--- a/posix/Versions
+++ b/posix/Versions
@@ -160,7 +160,7 @@ libc {
     posix_spawn_file_actions_addtcsetpgrp_np;
   }
   GLIBC_2.42 {
-    optctxt;
+    optctxt; opttextdomain;
   }
   GLIBC_PRIVATE {
     __libc_fork; __libc_pread; __libc_pwrite;
diff --git a/posix/bits/getopt_ext.h b/posix/bits/getopt_ext.h
index 07d9407b64..6cd0932596 100644
--- a/posix/bits/getopt_ext.h
+++ b/posix/bits/getopt_ext.h
@@ -31,6 +31,11 @@ __BEGIN_DECLS
    names.  If unset, the option names will not be translated. */
 
 extern const char *optctxt;
+
+/* Callers store the textdomain to use to retrieve option names, or
+   NULL to use the current textdomain. */
+
+extern const char *opttextdomain;
 #endif
 
 /* Describe the long-named options requested by the application.
diff --git a/posix/getopt.c b/posix/getopt.c
index 28f0be03f8..28c8aa4bbf 100644
--- a/posix/getopt.c
+++ b/posix/getopt.c
@@ -185,16 +185,20 @@ exchange (char **argv, struct _getopt_data *d)
 /* Return 1 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 const int
-match_translated_option_name (char *(*translate) (const char *, const char *),
+match_translated_option_name (char *(*translate) (const char *, const char *,
+                                                  const 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;
   if (translate)
-    translated = translate (translation_context, opt_name);
+    translated = translate (opt_textdomain, translation_context, opt_name);
   return (!strncmp (translated, argument, argument_length)
           && argument_length == strlen (translated));
 }
@@ -212,7 +216,8 @@ process_long_option (int argc, char **argv, const char *optstring,
 		     const struct option *longopts, int *longind,
 		     int long_only, struct _getopt_data *d,
 		     int print_errors, const char *prefix,
-                     char *(*translate) (const char *, const char *))
+                     char *(*translate) (const char *, const char *,
+                                         const char *))
 {
   char *nameend;
   size_t namelen;
@@ -243,9 +248,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;
@@ -369,7 +374,7 @@ process_long_option (int argc, char **argv, const char *optstring,
   /* We have found a matching long option.  Consume it.  */
   d->optind++;
   d->__nextchar = NULL;
-  translated_option_name = translate (d->optctxt, pfound->name);
+  translated_option_name = translate (d->opttextdomain, d->optctxt, pfound->name);
   if (*nameend)
     {
       /* Don't test has_arg with >, because some C compilers don't
@@ -535,7 +540,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 *(*translate) (const char *, const char *,
+                                        const char *))
 {
   int print_errors = d->opterr;
 
@@ -771,14 +777,16 @@ 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 *),
-                  const char *ctxt)
+                  char *(*translate) (const char *, const char *, const 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,
@@ -801,7 +809,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 387ad8d8c9..401bd0ed68 100644
--- a/posix/getopt1.c
+++ b/posix/getopt1.c
@@ -34,9 +34,14 @@
 
 const char *optctxt = NULL;
 
+/* Callers store the textdomain in which the option names are to be
+   looked up. */
+
+const char *opttextdomain = NULL;
+
 /* FIXME: use pgettext_expr. */
 static char *
-do_translate (const char *context, const char *msgid)
+do_translate (const char *domain, const char *context, const char *msgid)
 {
   char *full_msgid;
   const char *translated = msgid;
@@ -49,7 +54,7 @@ do_translate (const char *context, const char *msgid)
           strcpy (full_msgid, context);
           full_msgid[strlen (context)] = '\004';
           strcpy (full_msgid + strlen (context) + 1, msgid);
-          translated = gettext (full_msgid);
+          translated = dgettext (domain, full_msgid);
           if (!strcmp (translated, full_msgid))
             {
               translated = msgid;
@@ -67,7 +72,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
@@ -90,7 +96,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
diff --git a/posix/getopt_int.h b/posix/getopt_int.h
index 1d091979c3..1deb6cad35 100644
--- a/posix/getopt_int.h
+++ b/posix/getopt_int.h
@@ -24,13 +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 *),
-                             const char *__optctxt);
+                             char *(*translate) (const char *, const char *,
+                                                 const char *),
+                             const char *__optctxt, const char *__optdomain);
 
 
 /* Reentrant versions which can handle parsing multiple argument
@@ -73,6 +74,7 @@ struct _getopt_data
   int optopt;
   char *optarg;
   const char *optctxt;
+  const char *opttextdomain;
 
   /* Internal members.  */
 
@@ -109,7 +111,8 @@ extern int _getopt_internal_r (int ___argc, char **___argv,
 			       const struct option *__longopts, int *__longind,
 			       int __long_only, struct _getopt_data *__data,
 			       int __posixly_correct,
-                               char *(*translate) (const char *, const char *));
+                               char *(*translate) (const char *, const char *,
+                                                   const char *));
 
 extern int _getopt_long_r (int ___argc, char **___argv,
 			   const char *__shortopts,
diff --git a/posix/tstgetoptl.c b/posix/tstgetoptl.c
index bffd56f47d..3580358a67 100644
--- a/posix/tstgetoptl.c
+++ b/posix/tstgetoptl.c
@@ -27,13 +27,8 @@ prepare_localedir (void)
       fputs ("Cannot call bindtextdomain.\n", stderr);
       return -1;
     }
-  if (textdomain ("tstgetoptl") == NULL)
-    {
-      fputs ("Cannot call textdomain.\n", stderr);
-      return -1;
-    }
   /* Check that the catalog is OK: */
-  if (strcmp (gettext (TRANSLATION_CONTEXT "\004" "color"), "colour") != 0)
+  if (strcmp (dgettext ("tstgetoptl", TRANSLATION_CONTEXT "\004" "color"), "colour") != 0)
     {
       fputs ("The mo file does not work.\n", stderr);
       return -1;
@@ -45,6 +40,7 @@ int
 main (int argc, char **argv)
 {
   static const char *translation_context = TRANSLATION_CONTEXT;
+  static const char *translation_textdomain = "tstgetoptl";
   static const struct option options[] =
     {
       {"required", required_argument, NULL, 'r'},
@@ -72,6 +68,7 @@ main (int argc, char **argv)
       return 1;
     }
   optctxt = translation_context;
+  opttextdomain = translation_textdomain;
   while ((c = getopt_long (argc, argv, "abc:", options, NULL)) >= 0)
     switch (c)
       {
-- 
2.49.0



More information about the Libc-alpha mailing list