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)
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. */
32 #include "relocatable.h"
37 #define HAVE_SETLOCALE 1
38 /* Make sure we use the included libintl, not the system's one. */
40 #include "libgnuintl.h"
42 #define _(str) gettext (str)
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' },
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
))
62 main (int argc
, char *argv
[])
66 const char *msgid_plural
;
70 /* Default values for command line options. */
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]);
81 /* Set locale via LC_ALL. */
82 setlocale (LC_ALL
, "");
85 /* Set the text message domain. */
86 bindtextdomain (PACKAGE
, relocate (LOCALEDIR
));
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
))
97 case '\0': /* Long option. */
110 /* Undocumented option --env sets an environment variable. */
111 char *separator
= strchr (optarg
, '=');
112 if (separator
!= NULL
)
115 xsetenv (optarg
, separator
+ 1, 1);
116 environ_changed
= true;
122 usage (EXIT_FAILURE
);
125 #ifdef HAVE_SETLOCALE
127 /* Set locale again via LC_ALL. */
128 setlocale (LC_ALL
, "");
131 /* Version information is requested. */
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");
145 /* Help is requested. */
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')
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
168 while (optind
< argc
)
170 count
= argv
[optind
++];
174 unsigned long tmp_val
;
177 tmp_val
= strtoul (count
, &endp
, 10);
178 if (errno
== 0 && count
[0] != '\0' && endp
[0] == '\0')
181 /* When COUNT is not valid, use plural. */
185 /* If no domain name is given we don't translate, and we use English
186 plural form handling. */
188 fputs (n
== 1 ? msgid
: msgid_plural
, stdout
);
190 /* Write out the result. */
191 fputs (dngettext (domain
, msgid
, msgid_plural
, n
), stdout
);
198 /* Display usage information and exit. */
202 if (status
!= EXIT_SUCCESS
)
203 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
207 /* xgettext: no-wrap */
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"),
216 /* xgettext: no-wrap */
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
);