Imported Upstream version 0.19.7
[platform/upstream/gettext.git] / gettext-tools / tests / tstngettext.c
1 /* ngettext - retrieve plural form strings from message catalog and print them.
2    Copyright (C) 1995-1997, 2000-2007, 2012, 2015 Free Software
3    Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <getopt.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <locale.h>
27 #include <errno.h>
28
29 #include "closeout.h"
30 #include "error.h"
31 #include "progname.h"
32 #include "relocatable.h"
33 #include "basename.h"
34 #include "propername.h"
35 #include "xsetenv.h"
36
37 #define HAVE_SETLOCALE 1
38 /* Make sure we use the included libintl, not the system's one. */
39 #undef _LIBINTL_H
40 #include "libgnuintl.h"
41
42 #define _(str) gettext (str)
43
44 /* Long options.  */
45 static const struct option long_options[] =
46 {
47   { "domain", required_argument, NULL, 'd' },
48   { "env", required_argument, NULL, '=' },
49   { "help", no_argument, NULL, 'h' },
50   { "version", no_argument, NULL, 'V' },
51   { NULL, 0, NULL, 0 }
52 };
53
54 /* Forward declaration of local functions.  */
55 static void usage (int __status)
56 #if defined __GNUC__ && ((__GNUC__ == 2 && __GNUC_MINOR__ >= 5) || __GNUC__ > 2)
57      __attribute__ ((noreturn))
58 #endif
59 ;
60
61 int
62 main (int argc, char *argv[])
63 {
64   int optchar;
65   const char *msgid;
66   const char *msgid_plural;
67   const char *count;
68   unsigned long n;
69
70   /* Default values for command line options.  */
71   bool do_help = false;
72   bool do_version = false;
73   bool environ_changed = false;
74   const char *domain = getenv ("TEXTDOMAIN");
75   const char *domaindir = getenv ("TEXTDOMAINDIR");
76
77   /* Set program name for message texts.  */
78   set_program_name (argv[0]);
79
80 #ifdef HAVE_SETLOCALE
81   /* Set locale via LC_ALL.  */
82   setlocale (LC_ALL, "");
83 #endif
84
85   /* Set the text message domain.  */
86   bindtextdomain (PACKAGE, relocate (LOCALEDIR));
87   textdomain (PACKAGE);
88
89   /* Ensure that write errors on stdout are detected.  */
90   atexit (close_stdout);
91
92   /* Parse command line options.  */
93   while ((optchar = getopt_long (argc, argv, "+d:hV", long_options, NULL))
94          != EOF)
95     switch (optchar)
96     {
97     case '\0':          /* Long option.  */
98       break;
99     case 'd':
100       domain = optarg;
101       break;
102     case 'h':
103       do_help = true;
104       break;
105     case 'V':
106       do_version = true;
107       break;
108     case '=':
109       {
110         /* Undocumented option --env sets an environment variable.  */
111         char *separator = strchr (optarg, '=');
112         if (separator != NULL)
113           {
114             *separator = '\0';
115             xsetenv (optarg, separator + 1, 1);
116             environ_changed = true;
117             break;
118           }
119       }
120       /*FALLTHROUGH*/
121     default:
122       usage (EXIT_FAILURE);
123     }
124
125 #ifdef HAVE_SETLOCALE
126   if (environ_changed)
127     /* Set locale again via LC_ALL.  */
128     setlocale (LC_ALL, "");
129 #endif
130
131   /* Version information is requested.  */
132   if (do_version)
133     {
134       printf ("%s (GNU %s) %s\n", basename (program_name), PACKAGE, VERSION);
135       /* xgettext: no-wrap */
136       printf (_("Copyright (C) %s Free Software Foundation, Inc.\n\
137 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n\
138 This is free software: you are free to change and redistribute it.\n\
139 There is NO WARRANTY, to the extent permitted by law.\n\
140 "),
141               "1995-1997, 2000-2006");
142       printf (_("Written by %s.\n"), proper_name ("Ulrich Drepper"));
143       exit (EXIT_SUCCESS);
144     }
145
146   /* Help is requested.  */
147   if (do_help)
148     usage (EXIT_SUCCESS);
149
150   /* More optional command line options.  */
151   if (argc - optind <= 2)
152     error (EXIT_FAILURE, 0, _("missing arguments"));
153
154   /* Now the mandatory command line options.  */
155   msgid = argv[optind++];
156   msgid_plural = argv[optind++];
157
158   /* If no domain name is given we print the original string.
159      We mark this assigning NULL to domain.  */
160   if (domain == NULL || domain[0] == '\0')
161     domain = NULL;
162   else
163     /* Bind domain to appropriate directory.  */
164     if (domaindir != NULL && domaindir[0] != '\0')
165       bindtextdomain (domain, domaindir);
166
167   /* To speed up the plural-2 test, we accept more than one COUNT in one
168      call.  */
169   while (optind < argc)
170     {
171       count = argv[optind++];
172
173       {
174         char *endp;
175         unsigned long tmp_val;
176
177         errno = 0;
178         tmp_val = strtoul (count, &endp, 10);
179         if (errno == 0 && count[0] != '\0' && endp[0] == '\0')
180           n = tmp_val;
181         else
182           /* When COUNT is not valid, use plural.  */
183           n = 99;
184       }
185
186       /* If no domain name is given we don't translate, and we use English
187          plural form handling.  */
188       if (domain == NULL)
189         fputs (n == 1 ? msgid : msgid_plural, stdout);
190       else
191         /* Write out the result.  */
192         fputs (dngettext (domain, msgid, msgid_plural, n), stdout);
193     }
194
195   exit (EXIT_SUCCESS);
196 }
197
198
199 /* Display usage information and exit.  */
200 static void
201 usage (int status)
202 {
203   if (status != EXIT_SUCCESS)
204     fprintf (stderr, _("Try '%s --help' for more information.\n"),
205              program_name);
206   else
207     {
208       /* xgettext: no-wrap */
209       printf (_("\
210 Usage: %s [OPTION] MSGID MSGID-PLURAL COUNT...\n\
211   -d, --domain=TEXTDOMAIN   retrieve translated message from TEXTDOMAIN\n\
212   -h, --help                display this help and exit\n\
213   -V, --version             display version information and exit\n\
214   MSGID MSGID-PLURAL        translate MSGID (singular) / MSGID-PLURAL (plural)\n\
215   COUNT                     choose singular/plural form based on this value\n"),
216               program_name);
217       /* xgettext: no-wrap */
218       printf (_("\
219 \n\
220 If the TEXTDOMAIN parameter is not given, the domain is determined from the\n\
221 environment variable TEXTDOMAIN.  If the message catalog is not found in the\n\
222 regular directory, another location can be specified with the environment\n\
223 variable TEXTDOMAINDIR.\n\
224 Standard search directory: %s\n"), LOCALEDIR);
225       /* TRANSLATORS: The placeholder indicates the bug-reporting address
226          for this package.  Please add _another line_ saying
227          "Report translation bugs to <...>\n" with the address for translation
228          bugs (typically your translation team's web or email address).  */
229       fputs (_("Report bugs to <bug-gnu-gettext@gnu.org>.\n"), stdout);
230     }
231
232   exit (status);
233 }