[PATCH v5 2/4] posix: let the getopt caller set the translation context
Vivien Kraus
vivien@planete-kraus.eu
Wed Jun 4 22:06:16 GMT 2025
Option names are typically one word, so they could be translated
differently in different parts of the program. The use of a context
lets the translator pick the most appropriate translation when used in
the command-line.
Another possibility would be to prepend two dashes to the option name
before translation, such that it would be obvious this is the
command-line option name. However, it would be difficult to mark this
string for translation (to be processed by xgettext).
The context is now mandatory.
pgettext_expr is not available yet, so we use a custom function to
combine the context and the long option name.
This creates a new global variable / reentrant state field, optctxt,
so that the caller can override it.
---
manual/getopt.texi | 14 +++++++++++++-
posix/Versions | 3 +++
posix/bits/getopt_ext.h | 7 +++++++
posix/getopt.c | 27 ++++++++++++++++----------
posix/getopt1.c | 43 +++++++++++++++++++++++++++++++++++++----
posix/getopt_int.h | 6 ++++--
posix/tstgetoptl.c | 6 +++++-
posix/tstgetoptl.po | 2 ++
8 files changed, 90 insertions(+), 18 deletions(-)
diff --git a/manual/getopt.texi b/manual/getopt.texi
index 36d409e1f7..15994400dd 100644
--- a/manual/getopt.texi
+++ b/manual/getopt.texi
@@ -53,6 +53,16 @@ This variable is set by @code{getopt} to point at the value of the
option argument, for those options that accept arguments.
@end deftypevar
+@deftypevar {const char *} optctxt
+In order to match translated option names, @code{getopt} looks the
+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.
+@end deftypevar
+
@deftypefun int getopt (int @var{argc}, char *const *@var{argv}, const char *@var{options})
@standards{POSIX.2, unistd.h}
@safety{@prelim{}@mtunsafe{@mtasurace{:getopt} @mtsenv{}}@asunsafe{@ascuheap{} @ascuintl{} @asulock{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
@@ -215,7 +225,9 @@ The @code{struct option} structure has these fields:
@item const char *name
This field is the name of the option. It is a string. In order for
@command{getopt_long} to accept either the long option name or its
-translated form, you should mark this string for translation.
+translated form, you should mark this string for translation with a
+translation context, and set @code{optctxt} to the translation
+context.
@item int has_arg
This field says whether the option takes an argument. It is an integer,
diff --git a/posix/Versions b/posix/Versions
index 0624d24bcc..76af54306e 100644
--- a/posix/Versions
+++ b/posix/Versions
@@ -159,6 +159,9 @@ libc {
GLIBC_2.35 {
posix_spawn_file_actions_addtcsetpgrp_np;
}
+ GLIBC_2.42 {
+ optctxt;
+ }
GLIBC_PRIVATE {
__libc_fork; __libc_pread; __libc_pwrite;
__nanosleep_nocancel; __pause_nocancel;
diff --git a/posix/bits/getopt_ext.h b/posix/bits/getopt_ext.h
index 42be8ec38e..07d9407b64 100644
--- a/posix/bits/getopt_ext.h
+++ b/posix/bits/getopt_ext.h
@@ -26,6 +26,13 @@
__BEGIN_DECLS
+#ifdef __USE_GNU
+/* Callers store the translation context in which to retrieve option
+ names. If unset, the option names will not be translated. */
+
+extern const char *optctxt;
+#endif
+
/* Describe the long-named options requested by the application.
The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
of 'struct option' terminated by an element containing a name which is
diff --git a/posix/getopt.c b/posix/getopt.c
index 9540821e1f..28f0be03f8 100644
--- a/posix/getopt.c
+++ b/posix/getopt.c
@@ -182,17 +182,19 @@ exchange (char **argv, struct _getopt_data *d)
d->__last_nonopt = d->optind;
}
-/* Return 1 iff a translation for opt_name has been found and it
- matches the substring from argument, length argument_length.
+/* 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.
*/
static const int
-match_translated_option_name (char *(*translate) (const char *msgid),
+match_translated_option_name (char *(*translate) (const char *, const char *),
const char *argument, size_t argument_length,
+ const char *translation_context,
const char *opt_name)
{
const char *translated = opt_name;
if (translate)
- translated = translate (opt_name);
+ translated = translate (translation_context, opt_name);
return (!strncmp (translated, argument, argument_length)
&& argument_length == strlen (translated));
}
@@ -210,7 +212,7 @@ 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 *msgid))
+ char *(*translate) (const char *, const char *))
{
char *nameend;
size_t namelen;
@@ -241,7 +243,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, p->name))
+ if (match_translated_option_name (translate,
+ d->__nextchar, namelen,
+ d->optctxt, p->name))
{
/* Exact match found with translation. */
pfound = p;
@@ -365,7 +369,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 (pfound->name);
+ translated_option_name = translate (d->optctxt, pfound->name);
if (*nameend)
{
/* Don't test has_arg with >, because some C compilers don't
@@ -531,7 +535,7 @@ 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 *msgid))
+ char *(*translate) (const char *, const char *))
{
int print_errors = d->opterr;
@@ -766,12 +770,15 @@ _getopt_internal_r (int argc, char **argv, const char *optstring,
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 *))
+ int posixly_correct,
+ char *(*translate) (const char *, const char *),
+ const char *ctxt)
{
int result;
getopt_data.optind = optind;
getopt_data.opterr = opterr;
+ getopt_data.optctxt = ctxt;
result = _getopt_internal_r (argc, argv, optstring, longopts,
longind, long_only, &getopt_data,
@@ -794,7 +801,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); \
}
#ifdef _LIBC
diff --git a/posix/getopt1.c b/posix/getopt1.c
index 6bf0087344..387ad8d8c9 100644
--- a/posix/getopt1.c
+++ b/posix/getopt1.c
@@ -26,13 +26,48 @@
#include "getopt.h"
#include "getopt_int.h"
+#include <stdlib.h>
+#include <string.h>
+
+/* Callers store an optional context to enable option name
+ translation. */
+
+const char *optctxt = NULL;
+
+/* FIXME: use pgettext_expr. */
+static char *
+do_translate (const char *context, const char *msgid)
+{
+ char *full_msgid;
+ const char *translated = msgid;
+
+ if (context)
+ {
+ full_msgid = malloc (strlen (context) + 1 /* ^D */ + strlen (msgid) + 1);
+ if (msgid)
+ {
+ strcpy (full_msgid, context);
+ full_msgid[strlen (context)] = '\004';
+ strcpy (full_msgid + strlen (context) + 1, msgid);
+ translated = gettext (full_msgid);
+ if (!strcmp (translated, full_msgid))
+ {
+ translated = msgid;
+ }
+ }
+ free (full_msgid);
+ }
+ else
+ translated = msgid;
+ return (char *) translated;
+}
int
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, gettext);
+ opt_index, 0, 0, do_translate, optctxt);
}
int
@@ -41,7 +76,7 @@ _getopt_long_r (int argc, char **argv, const char *options,
struct _getopt_data *d)
{
return _getopt_internal_r (argc, argv, options, long_options, opt_index,
- 0, d, 0, gettext);
+ 0, d, 0, do_translate);
}
/* Like getopt_long, but '-' as well as '--' can indicate a long option.
@@ -55,7 +90,7 @@ 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, gettext);
+ opt_index, 1, 0, do_translate, optctxt);
}
int
@@ -64,7 +99,7 @@ _getopt_long_only_r (int argc, char **argv, const char *options,
struct _getopt_data *d)
{
return _getopt_internal_r (argc, argv, options, long_options, opt_index,
- 1, d, 0, gettext);
+ 1, d, 0, do_translate);
}
diff --git a/posix/getopt_int.h b/posix/getopt_int.h
index b15b21256a..1d091979c3 100644
--- a/posix/getopt_int.h
+++ b/posix/getopt_int.h
@@ -29,7 +29,8 @@ 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 *msgid));
+ char *(*translate) (const char *, const char *),
+ const char *__optctxt);
/* Reentrant versions which can handle parsing multiple argument
@@ -71,6 +72,7 @@ struct _getopt_data
int opterr;
int optopt;
char *optarg;
+ const char *optctxt;
/* Internal members. */
@@ -107,7 +109,7 @@ 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 *msgid));
+ char *(*translate) (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 04b07093e4..bffd56f47d 100644
--- a/posix/tstgetoptl.c
+++ b/posix/tstgetoptl.c
@@ -15,6 +15,8 @@
precedence over translated options, by translated "optional" as
"required". */
+#define TRANSLATION_CONTEXT "command-line option"
+
static int
prepare_localedir (void)
{
@@ -31,7 +33,7 @@ prepare_localedir (void)
return -1;
}
/* Check that the catalog is OK: */
- if (strcmp (gettext ("color"), "colour") != 0)
+ if (strcmp (gettext (TRANSLATION_CONTEXT "\004" "color"), "colour") != 0)
{
fputs ("The mo file does not work.\n", stderr);
return -1;
@@ -42,6 +44,7 @@ prepare_localedir (void)
int
main (int argc, char **argv)
{
+ static const char *translation_context = TRANSLATION_CONTEXT;
static const struct option options[] =
{
{"required", required_argument, NULL, 'r'},
@@ -68,6 +71,7 @@ main (int argc, char **argv)
fputs ("Error while setting up localedir.\n", stderr);
return 1;
}
+ optctxt = translation_context;
while ((c = getopt_long (argc, argv, "abc:", options, NULL)) >= 0)
switch (c)
{
diff --git a/posix/tstgetoptl.po b/posix/tstgetoptl.po
index 25cd595790..e060c0d6e3 100644
--- a/posix/tstgetoptl.po
+++ b/posix/tstgetoptl.po
@@ -16,10 +16,12 @@ msgstr ""
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: xxx.c:yy
+msgctxt "command-line option"
msgid "color"
msgstr "colour"
# This is to make sure the translator cannot redirect options.
#: xxx.c:yy
+msgctxt "command-line option"
msgid "optional"
msgstr "required"
--
2.49.0
More information about the Libc-alpha
mailing list