No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / tests / tstngettext.c
blobfe646bd2bf5bdcf8d25e19f3c57c34b134253ee3
1 /* ngettext - retrieve plural form strings from message catalog and print them.
2 Copyright (C) 1995-1997, 2000-2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <getopt.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <locale.h>
27 #include <errno.h>
29 #include "closeout.h"
30 #include "error.h"
31 #include "progname.h"
32 #include "relocatable.h"
33 #include "basename.h"
34 #include "exit.h"
35 #include "xsetenv.h"
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"
42 #define _(str) gettext (str)
44 /* Long options. */
45 static const struct option long_options[] =
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 }
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
61 int
62 main (int argc, char *argv[])
64 int optchar;
65 const char *msgid;
66 const char *msgid_plural;
67 const char *count;
68 unsigned long n;
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");
77 /* Set program name for message texts. */
78 set_program_name (argv[0]);
80 #ifdef HAVE_SETLOCALE
81 /* Set locale via LC_ALL. */
82 setlocale (LC_ALL, "");
83 #endif
85 /* Set the text message domain. */
86 bindtextdomain (PACKAGE, relocate (LOCALEDIR));
87 textdomain (PACKAGE);
89 /* Ensure that write errors on stdout are detected. */
90 atexit (close_stdout);
92 /* Parse command line options. */
93 while ((optchar = getopt_long (argc, argv, "+d:hV", long_options, NULL))
94 != EOF)
95 switch (optchar)
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 '=':
110 /* Undocumented option --env sets an environment variable. */
111 char *separator = strchr (optarg, '=');
112 if (separator != NULL)
114 *separator = '\0';
115 xsetenv (optarg, separator + 1, 1);
116 environ_changed = true;
117 break;
120 /*FALLTHROUGH*/
121 default:
122 usage (EXIT_FAILURE);
125 #ifdef HAVE_SETLOCALE
126 if (environ_changed)
127 /* Set locale again via LC_ALL. */
128 setlocale (LC_ALL, "");
129 #endif
131 /* Version information is requested. */
132 if (do_version)
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 This is free software; see the source for copying conditions. There is NO\n\
138 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
140 "1995-1997, 2000-2005");
141 printf (_("Written by %s.\n"), "Ulrich Drepper");
142 exit (EXIT_SUCCESS);
145 /* Help is requested. */
146 if (do_help)
147 usage (EXIT_SUCCESS);
149 /* More optional command line options. */
150 if (argc - optind <= 2)
151 error (EXIT_FAILURE, 0, _("missing arguments"));
153 /* Now the mandatory command line options. */
154 msgid = argv[optind++];
155 msgid_plural = argv[optind++];
157 /* If no domain name is given we print the original string.
158 We mark this assigning NULL to domain. */
159 if (domain == NULL || domain[0] == '\0')
160 domain = NULL;
161 else
162 /* Bind domain to appropriate directory. */
163 if (domaindir != NULL && domaindir[0] != '\0')
164 bindtextdomain (domain, domaindir);
166 /* To speed up the plural-2 test, we accept more than one COUNT in one
167 call. */
168 while (optind < argc)
170 count = argv[optind++];
173 char *endp;
174 unsigned long tmp_val;
176 errno = 0;
177 tmp_val = strtoul (count, &endp, 10);
178 if (errno == 0 && count[0] != '\0' && endp[0] == '\0')
179 n = tmp_val;
180 else
181 /* When COUNT is not valid, use plural. */
182 n = 99;
185 /* If no domain name is given we don't translate, and we use English
186 plural form handling. */
187 if (domain == NULL)
188 fputs (n == 1 ? msgid : msgid_plural, stdout);
189 else
190 /* Write out the result. */
191 fputs (dngettext (domain, msgid, msgid_plural, n), stdout);
194 exit (EXIT_SUCCESS);
198 /* Display usage information and exit. */
199 static void
200 usage (int status)
202 if (status != EXIT_SUCCESS)
203 fprintf (stderr, _("Try `%s --help' for more information.\n"),
204 program_name);
205 else
207 /* xgettext: no-wrap */
208 printf (_("\
209 Usage: %s [OPTION] MSGID MSGID-PLURAL COUNT...\n\
210 -d, --domain=TEXTDOMAIN retrieve translated message from TEXTDOMAIN\n\
211 -h, --help display this help and exit\n\
212 -V, --version display version information and exit\n\
213 MSGID MSGID-PLURAL translate MSGID (singular) / MSGID-PLURAL (plural)\n\
214 COUNT choose singular/plural form based on this value\n"),
215 program_name);
216 /* xgettext: no-wrap */
217 printf (_("\
219 If the TEXTDOMAIN parameter is not given, the domain is determined from the\n\
220 environment variable TEXTDOMAIN. If the message catalog is not found in the\n\
221 regular directory, another location can be specified with the environment\n\
222 variable TEXTDOMAINDIR.\n\
223 Standard search directory: %s\n"), LOCALEDIR);
224 fputs (_("Report bugs to <bug-gnu-gettext@gnu.org>.\n"), stdout);
227 exit (status);