File: tstngettext.c

package info (click to toggle)
gettext 0.23.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 168,104 kB
  • sloc: ansic: 532,579; sh: 68,252; perl: 28,011; makefile: 9,068; lisp: 3,184; yacc: 1,055; java: 615; cs: 589; cpp: 397; objc: 343; sed: 79; tcl: 63; xml: 40; pascal: 11; awk: 7; php: 7
file content (275 lines) | stat: -rw-r--r-- 7,966 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* ngettext - retrieve plural form strings from message catalog and print them.
   Copyright (C) 1995-2024 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
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <errno.h>

#include <error.h>
#include "attribute.h"
#include "noreturn.h"
#include "closeout.h"
#include "progname.h"
#include "relocatable.h"
#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
#include "libgnuintl.h"

#if defined _WIN32 && !defined __CYGWIN__
# undef setlocale
# define setlocale fake_setlocale
extern char *setlocale (int category, const char *locale);
#endif

#define _(str) gettext (str)

/* Long options.  */
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;

  /* Default values for command line options.  */
  bool do_help = false;
  bool do_thread = false;
  bool do_version = false;
  bool environ_changed = false;
  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]);

  /* Set locale via LC_ALL.  */
  setlocale (LC_ALL, "");

  /* Set the text message domain.  */
  bindtextdomain (PACKAGE, relocate (LOCALEDIR));
  textdomain (PACKAGE);

  /* Ensure that write errors on stdout are detected.  */
  atexit (close_stdout);

  /* Parse command line options.  */
  while ((optchar = getopt_long (argc, argv, "+d:htV", long_options, NULL))
         != EOF)
    switch (optchar)
    {
    case '\0':          /* Long option.  */
      break;
    case 'd':
      context.domain = optarg;
      break;
    case 'h':
      do_help = true;
      break;
    case 't':
      do_thread = true;
      break;
    case 'V':
      do_version = true;
      break;
    case '=':
      {
        /* Undocumented option --env sets an environment variable.  */
        char *separator = strchr (optarg, '=');
        if (separator != NULL)
          {
            *separator = '\0';
            xsetenv (optarg, separator + 1, 1);
            environ_changed = true;
            break;
          }
      }
      FALLTHROUGH;
    default:
      usage (EXIT_FAILURE);
    }

  if (environ_changed)
    /* Set locale again via LC_ALL.  */
    setlocale (LC_ALL, "");

  /* Version information is requested.  */
  if (do_version)
    {
      printf ("%s (GNU %s) %s\n", last_component (program_name),
              PACKAGE, VERSION);
      /* xgettext: no-wrap */
      printf (_("Copyright (C) %s Free Software Foundation, Inc.\n\
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-2023", "https://gnu.org/licenses/gpl.html");
      printf (_("Written by %s.\n"), proper_name ("Ulrich Drepper"));
      exit (EXIT_SUCCESS);
    }

  /* Help is requested.  */
  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"));

  /* Now the mandatory command line options.  */
  msgid = argv[optind++];
  msgid_plural = argv[optind++];

  /* If no domain name is given we print the original string.
     We mark this assigning NULL to domain.  */
  if (context->domain == NULL || context->domain[0] == '\0')
    context->domain = NULL;
  else
    /* Bind domain to appropriate directory.  */
    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.  */
  while (optind < argc)
    {
      count = argv[optind++];

      {
        char *endp;
        unsigned long tmp_val;

        errno = 0;
        tmp_val = strtoul (count, &endp, 10);
        if (errno == 0 && count[0] != '\0' && endp[0] == '\0')
          n = tmp_val;
        else
          /* When COUNT is not valid, use plural.  */
          n = 99;
      }

      /* If no domain name is given we don't translate, and we use English
         plural form handling.  */
      if (context->domain == NULL)
        fputs (n == 1 ? msgid : msgid_plural, stdout);
      else
        /* Write out the result.  */
        fputs (dngettext (context->domain, msgid, msgid_plural, n), stdout);
    }

  exit (EXIT_SUCCESS);
}


/* Display usage information and exit.  */
static void
usage (int status)
{
  if (status != EXIT_SUCCESS)
    fprintf (stderr, _("Try '%s --help' for more information.\n"),
             program_name);
  else
    {
      /* xgettext: no-wrap */
      printf (_("\
Usage: %s [OPTION] MSGID MSGID-PLURAL COUNT...\n\
  -d, --domain=TEXTDOMAIN   retrieve translated message from TEXTDOMAIN\n\
  -h, --help                display this help and exit\n\
  -V, --version             display version information and exit\n\
  MSGID MSGID-PLURAL        translate MSGID (singular) / MSGID-PLURAL (plural)\n\
  COUNT                     choose singular/plural form based on this value\n"),
              program_name);
      /* xgettext: no-wrap */
      printf (_("\
\n\
If the TEXTDOMAIN parameter is not given, the domain is determined from the\n\
environment variable TEXTDOMAIN.  If the message catalog is not found in the\n\
regular directory, another location can be specified with the environment\n\
variable TEXTDOMAINDIR.\n\
Standard search directory: %s\n"), LOCALEDIR);
      /* TRANSLATORS: The first placeholder is the web address of the Savannah
         project of this package.  The second placeholder is the bug-reporting
         email address for this package.  Please add _another line_ saying
         "Report translation bugs to <...>\n" with the address for translation
         bugs (typically your translation team's web or email address).  */
      printf(_("\
Report bugs in the bug tracker at <%s>\n\
or by email to <%s>.\n"),
             "https://savannah.gnu.org/projects/gettext",
             "bug-gettext@gnu.org");
    }

  exit (status);
}