@@ -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
@@ -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
@@ -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);
@@ -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
@@ -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;
}
@@ -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,
@@ -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;