From: Bruno Haible Date: Sun, 1 Oct 2023 18:18:02 +0000 (+0200) Subject: tests: Add option --thread to tstgettext, tstngettext. X-Git-Tag: v0.23~326 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9d9bab873bbc1f2a1ac4ae509cfb38537daeb787;p=thirdparty%2Fgettext.git tests: Add option --thread to tstgettext, tstngettext. * gettext-tools/tests/tstgettext.c: Include glthread/thread.h. (inhibit_added_newline, do_expand): Remove variables. (long_options): Add --thread option. (struct worker_context): New type. (main): Move some local variables into a context. Recognize option --thread. At the end, invoke worker_thread. (worker_thread): New function, extracted from main. * gettext-tools/tests/tstngettext.c: Include glthread/thread.h. (long_options): Add --thread option. (struct worker_context): New type. (main): Move some local variables into a context. Recognize option --thread. At the end, invoke worker_thread. (worker_thread): New function, extracted from main. * gettext-tools/tests/Makefile.am (tstgettext_LDADD, tstngettext_LDADD): Add gnulib-lib/libtestsgnu.a and $(LIBMULTITHREAD). --- diff --git a/gettext-tools/tests/Makefile.am b/gettext-tools/tests/Makefile.am index c08b61e60..86de8b458 100644 --- a/gettext-tools/tests/Makefile.am +++ b/gettext-tools/tests/Makefile.am @@ -305,10 +305,10 @@ tstgettext_SOURCES = \ tstgettext.c ../../gettext-runtime/src/escapes.h \ setlocale.c tstgettext_CFLAGS = -DINSTALLDIR=\".\" -tstgettext_LDADD = ../gnulib-lib/libgettextlib.la $(LDADD) +tstgettext_LDADD = gnulib-lib/libtestsgnu.a ../gnulib-lib/libgettextlib.la $(LDADD) $(LIBMULTITHREAD) tstngettext_SOURCES = tstngettext.c setlocale.c tstngettext_CFLAGS = -DINSTALLDIR=\".\" -tstngettext_LDADD = ../gnulib-lib/libgettextlib.la $(LDADD) +tstngettext_LDADD = gnulib-lib/libtestsgnu.a ../gnulib-lib/libgettextlib.la $(LDADD) $(LIBMULTITHREAD) testlocale_SOURCES = testlocale.c intl_1_prg_SOURCES = intl-1-prg.c intl_1_prg_LDADD = ../gnulib-lib/libgettextlib.la $(LDADD) diff --git a/gettext-tools/tests/tstgettext.c b/gettext-tools/tests/tstgettext.c index a855783b0..2b96e2a37 100644 --- a/gettext-tools/tests/tstgettext.c +++ b/gettext-tools/tests/tstgettext.c @@ -1,6 +1,5 @@ /* gettext - retrieve text string from message catalog and print it. - Copyright (C) 1995-1997, 2000-2007, 2012, 2018-2020 Free Software - Foundation, Inc. + Copyright (C) 1995-2023 Free Software Foundation, Inc. Written by Ulrich Drepper , May 1995. This program is free software: you can redistribute it and/or modify @@ -37,6 +36,7 @@ #include "xalloc.h" #include "propername.h" #include "xsetenv.h" +#include "glthread/thread.h" #include "../../gettext-runtime/src/escapes.h" /* Make sure we use the included libintl, not the system's one. */ @@ -51,14 +51,6 @@ extern char *setlocale (int category, const char *locale); #define _(str) gettext (str) -/* If false, add newline after last string. This makes only sense in - the 'echo' emulation mode. */ -static bool inhibit_added_newline; - -/* If true, expand escape sequences in strings before looking in the - message catalog. */ -static bool do_expand; - /* Long options. */ static const struct option long_options[] = { @@ -66,28 +58,49 @@ static const struct option long_options[] = { "env", required_argument, NULL, '=' }, { "help", no_argument, NULL, 'h' }, { "shell-script", no_argument, NULL, 's' }, + { "thread", no_argument, NULL, 't' }, { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; /* Forward declaration of local functions. */ +_GL_NORETURN_FUNC static void *worker_thread (void *arg); _GL_NORETURN_FUNC static void usage (int status); +/* Argument passed to the worker_thread. */ +struct worker_context +{ + int argc; + char **argv; + bool do_shell; + const char *domain; + const char *domaindir; + /* If false, add newline after last string. This makes only sense in + the 'echo' emulation mode. */ + bool inhibit_added_newline; + /* If true, expand escape sequences in strings before looking in the + message catalog. */ + bool do_expand; +}; + int main (int argc, char *argv[]) { int optchar; - const char *msgid; /* Default values for command line options. */ bool do_help = false; - bool do_shell = false; + bool do_thread = false; bool do_version = false; bool environ_changed = false; - const char *domain = getenv ("TEXTDOMAIN"); - const char *domaindir = getenv ("TEXTDOMAINDIR"); - inhibit_added_newline = false; - do_expand = false; + struct worker_context context; + context.argc = argc; + context.argv = argv; + context.do_shell = false; + context.domain = getenv ("TEXTDOMAIN"); + context.domaindir = getenv ("TEXTDOMAINDIR"); + context.inhibit_added_newline = false; + context.do_expand = false; /* Set program name for message texts. */ set_program_name (argv[0]); @@ -103,17 +116,17 @@ main (int argc, char *argv[]) atexit (close_stdout); /* Parse command line options. */ - while ((optchar = getopt_long (argc, argv, "+d:eEhnsV", long_options, NULL)) + while ((optchar = getopt_long (argc, argv, "+d:eEhnstV", long_options, NULL)) != EOF) switch (optchar) { case '\0': /* Long option. */ break; case 'd': - domain = optarg; + context.domain = optarg; break; case 'e': - do_expand = true; + context.do_expand = true; break; case 'E': /* Ignore. Just for compatibility. */ @@ -122,10 +135,13 @@ main (int argc, char *argv[]) do_help = true; break; case 'n': - inhibit_added_newline = true; + context.inhibit_added_newline = true; break; case 's': - do_shell = true; + context.do_shell = true; + break; + case 't': + do_thread = true; break; case 'V': do_version = true; @@ -162,7 +178,7 @@ License GPLv3+: GNU GPL version 3 or later <%s>\n\ This is free software: you are free to change and redistribute it.\n\ There is NO WARRANTY, to the extent permitted by law.\n\ "), - "1995-1997, 2000-2006", "https://gnu.org/licenses/gpl.html"); + "1995-2023", "https://gnu.org/licenses/gpl.html"); printf (_("Written by %s.\n"), proper_name ("Ulrich Drepper")); exit (EXIT_SUCCESS); } @@ -171,9 +187,28 @@ There is NO WARRANTY, to the extent permitted by law.\n\ if (do_help) usage (EXIT_SUCCESS); + if (do_thread) + { + gl_thread_t thread = gl_thread_create (worker_thread, &context); + void *retval; + gl_thread_join (thread, &retval); + } + else + worker_thread (&context); +} + +static void * +worker_thread (void *arg) +{ + struct worker_context *context = arg; + int argc = context->argc; + char **argv = context->argv; + + const char *msgid; + /* We have two major modes: use following Uniforum spec and as internationalized 'echo' program. */ - if (!do_shell) + if (!context->do_shell) { /* We have to write a single strings translation to stdout. */ @@ -184,7 +219,7 @@ There is NO WARRANTY, to the extent permitted by law.\n\ error (EXIT_FAILURE, 0, _("too many arguments")); case 2: - domain = argv[optind++]; + context->domain = argv[optind++]; FALLTHROUGH; case 1: @@ -197,22 +232,22 @@ There is NO WARRANTY, to the extent permitted by law.\n\ msgid = argv[optind++]; /* Expand escape sequences if enabled. */ - if (do_expand) - msgid = expand_escapes (msgid, &inhibit_added_newline); + if (context->do_expand) + msgid = expand_escapes (msgid, &context->inhibit_added_newline); /* If no domain name is given we don't translate. */ - if (domain == NULL || domain[0] == '\0') + if (context->domain == NULL || context->domain[0] == '\0') { fputs (msgid, stdout); } else { /* Bind domain to appropriate directory. */ - if (domaindir != NULL && domaindir[0] != '\0') - bindtextdomain (domain, domaindir); + if (context->domaindir != NULL && context->domaindir[0] != '\0') + bindtextdomain (context->domain, context->domaindir); /* Write out the result. */ - fputs (dgettext (domain, msgid), stdout); + fputs (dgettext (context->domain, msgid), stdout); } } else @@ -221,12 +256,12 @@ There is NO WARRANTY, to the extent permitted by law.\n\ { /* If no domain name is given we print the original string. We mark this assigning NULL to domain. */ - if (domain == NULL || domain[0] == '\0') - domain = NULL; + if (context->domain == NULL || context->domain[0] == '\0') + context->domain = NULL; else /* Bind domain to appropriate directory. */ - if (domaindir != NULL && domaindir[0] != '\0') - bindtextdomain (domain, domaindir); + if (context->domaindir != NULL && context->domaindir[0] != '\0') + bindtextdomain (context->domain, context->domaindir); /* We have to simulate 'echo'. All arguments are strings. */ do @@ -234,11 +269,13 @@ There is NO WARRANTY, to the extent permitted by law.\n\ msgid = argv[optind++]; /* Expand escape sequences if enabled. */ - if (do_expand) - msgid = expand_escapes (msgid, &inhibit_added_newline); + if (context->do_expand) + msgid = expand_escapes (msgid, &context->inhibit_added_newline); /* Write out the result. */ - fputs (domain == NULL ? msgid : dgettext (domain, msgid), + fputs (context->domain == NULL + ? msgid + : dgettext (context->domain, msgid), stdout); /* We separate the arguments by a single ' '. */ @@ -249,7 +286,7 @@ There is NO WARRANTY, to the extent permitted by law.\n\ } /* If not otherwise told: add trailing newline. */ - if (!inhibit_added_newline) + if (!context->inhibit_added_newline) fputc ('\n', stdout); } diff --git a/gettext-tools/tests/tstngettext.c b/gettext-tools/tests/tstngettext.c index e9ce5c967..614d1ccd2 100644 --- a/gettext-tools/tests/tstngettext.c +++ b/gettext-tools/tests/tstngettext.c @@ -1,6 +1,5 @@ /* ngettext - retrieve plural form strings from message catalog and print them. - Copyright (C) 1995-1997, 2000-2007, 2012, 2018-2020 Free Software - Foundation, Inc. + Copyright (C) 1995-2023 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -36,6 +35,7 @@ #include "basename-lgpl.h" #include "propername.h" #include "xsetenv.h" +#include "glthread/thread.h" /* Make sure we use the included libintl, not the system's one. */ #undef _LIBINTL_H @@ -55,28 +55,39 @@ static const struct option long_options[] = { "domain", required_argument, NULL, 'd' }, { "env", required_argument, NULL, '=' }, { "help", no_argument, NULL, 'h' }, + { "thread", no_argument, NULL, 't' }, { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; /* Forward declaration of local functions. */ +_GL_NORETURN_FUNC static void *worker_thread (void *arg); _GL_NORETURN_FUNC static void usage (int __status); +/* Argument passed to the worker_thread. */ +struct worker_context +{ + int argc; + char **argv; + const char *domain; + const char *domaindir; +}; + int main (int argc, char *argv[]) { int optchar; - const char *msgid; - const char *msgid_plural; - const char *count; - unsigned long n; /* Default values for command line options. */ bool do_help = false; + bool do_thread = false; bool do_version = false; bool environ_changed = false; - const char *domain = getenv ("TEXTDOMAIN"); - const char *domaindir = getenv ("TEXTDOMAINDIR"); + struct worker_context context; + context.argc = argc; + context.argv = argv; + context.domain = getenv ("TEXTDOMAIN"); + context.domaindir = getenv ("TEXTDOMAINDIR"); /* Set program name for message texts. */ set_program_name (argv[0]); @@ -92,18 +103,21 @@ main (int argc, char *argv[]) atexit (close_stdout); /* Parse command line options. */ - while ((optchar = getopt_long (argc, argv, "+d:hV", long_options, NULL)) + while ((optchar = getopt_long (argc, argv, "+d:htV", long_options, NULL)) != EOF) switch (optchar) { case '\0': /* Long option. */ break; case 'd': - domain = optarg; + context.domain = optarg; break; case 'h': do_help = true; break; + case 't': + do_thread = true; + break; case 'V': do_version = true; break; @@ -139,7 +153,7 @@ License GPLv3+: GNU GPL version 3 or later <%s>\n\ This is free software: you are free to change and redistribute it.\n\ There is NO WARRANTY, to the extent permitted by law.\n\ "), - "1995-1997, 2000-2006", "https://gnu.org/licenses/gpl.html"); + "1995-2023", "https://gnu.org/licenses/gpl.html"); printf (_("Written by %s.\n"), proper_name ("Ulrich Drepper")); exit (EXIT_SUCCESS); } @@ -148,6 +162,28 @@ There is NO WARRANTY, to the extent permitted by law.\n\ if (do_help) usage (EXIT_SUCCESS); + if (do_thread) + { + gl_thread_t thread = gl_thread_create (worker_thread, &context); + void *retval; + gl_thread_join (thread, &retval); + } + else + worker_thread (&context); +} + +static void * +worker_thread (void *arg) +{ + struct worker_context *context = arg; + int argc = context->argc; + char **argv = context->argv; + + const char *msgid; + const char *msgid_plural; + const char *count; + unsigned long n; + /* More optional command line options. */ if (argc - optind <= 2) error (EXIT_FAILURE, 0, _("missing arguments")); @@ -158,12 +194,12 @@ There is NO WARRANTY, to the extent permitted by law.\n\ /* If no domain name is given we print the original string. We mark this assigning NULL to domain. */ - if (domain == NULL || domain[0] == '\0') - domain = NULL; + if (context->domain == NULL || context->domain[0] == '\0') + context->domain = NULL; else /* Bind domain to appropriate directory. */ - if (domaindir != NULL && domaindir[0] != '\0') - bindtextdomain (domain, domaindir); + if (context->domaindir != NULL && context->domaindir[0] != '\0') + bindtextdomain (context->domain, context->domaindir); /* To speed up the plural-2 test, we accept more than one COUNT in one call. */ @@ -186,11 +222,11 @@ There is NO WARRANTY, to the extent permitted by law.\n\ /* If no domain name is given we don't translate, and we use English plural form handling. */ - if (domain == NULL) + if (context->domain == NULL) fputs (n == 1 ? msgid : msgid_plural, stdout); else /* Write out the result. */ - fputs (dngettext (domain, msgid, msgid_plural, n), stdout); + fputs (dngettext (context->domain, msgid, msgid_plural, n), stdout); } exit (EXIT_SUCCESS);