1 /* ngettext - retrieve plural form string from message catalog and print it.
2 Copyright (C) 1995-1997, 2000-2007, 2015 Free Software Foundation,
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.
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.
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/>. */
33 #include "relocatable.h"
36 #include "propername.h"
39 #define _(str) gettext (str)
41 /* If true, expand escape sequences in strings before looking in the
46 static const struct option long_options[] =
48 { "domain", required_argument, NULL, 'd' },
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))
60 static const char *expand_escape (const char *str);
63 main (int argc, char *argv[])
67 const char *msgid_plural;
71 /* Default values for command line options. */
73 bool do_version = false;
74 const char *domain = getenv ("TEXTDOMAIN");
75 const char *domaindir = getenv ("TEXTDOMAINDIR");
78 /* Set program name for message texts. */
79 set_program_name (argv[0]);
82 /* Set locale via LC_ALL. */
83 setlocale (LC_ALL, "");
86 /* Set the text message domain. */
87 bindtextdomain (PACKAGE, relocate (LOCALEDIR));
90 /* Ensure that write errors on stdout are detected. */
91 atexit (close_stdout);
93 /* Parse command line options. */
94 while ((optchar = getopt_long (argc, argv, "+d:eEhV", long_options, NULL))
98 case '\0': /* Long option. */
107 /* Ignore. Just for compatibility. */
116 usage (EXIT_FAILURE);
119 /* Version information is requested. */
122 printf ("%s (GNU %s) %s\n", basename (program_name), PACKAGE, VERSION);
123 /* xgettext: no-wrap */
124 printf (_("Copyright (C) %s Free Software Foundation, Inc.\n\
125 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n\
126 This is free software: you are free to change and redistribute it.\n\
127 There is NO WARRANTY, to the extent permitted by law.\n\
129 "1995-1997, 2000-2007");
130 printf (_("Written by %s.\n"), proper_name ("Ulrich Drepper"));
134 /* Help is requested. */
136 usage (EXIT_SUCCESS);
138 /* More optional command line options. */
139 switch (argc - optind)
142 error (EXIT_FAILURE, 0, _("too many arguments"));
145 domain = argv[optind++];
154 error (EXIT_FAILURE, 0, _("missing arguments"));
157 /* Now the mandatory command line options. */
158 msgid = argv[optind++];
159 msgid_plural = argv[optind++];
160 count = argv[optind++];
167 unsigned long tmp_val;
170 tmp_val = strtoul (count, &endp, 10);
171 if (errno == 0 && count[0] != '\0' && endp[0] == '\0')
174 /* When COUNT is not valid, use plural. */
178 /* Expand escape sequences if enabled. */
181 msgid = expand_escape (msgid);
182 msgid_plural = expand_escape (msgid_plural);
185 /* If no domain name is given we don't translate, and we use English
186 plural form handling. */
187 if (domain == NULL || domain[0] == '\0')
188 fputs (n == 1 ? msgid : msgid_plural, stdout);
191 /* Bind domain to appropriate directory. */
192 if (domaindir != NULL && domaindir[0] != '\0')
193 bindtextdomain (domain, domaindir);
195 /* Write out the result. */
196 fputs (dngettext (domain, msgid, msgid_plural, n), stdout);
203 /* Display usage information and exit. */
207 if (status != EXIT_SUCCESS)
208 fprintf (stderr, _("Try '%s --help' for more information.\n"),
212 /* xgettext: no-wrap */
214 Usage: %s [OPTION] [TEXTDOMAIN] MSGID MSGID-PLURAL COUNT\n\
217 /* xgettext: no-wrap */
219 Display native language translation of a textual message whose grammatical\n\
220 form depends on a number.\n"));
222 /* xgettext: no-wrap */
224 -d, --domain=TEXTDOMAIN retrieve translated message from TEXTDOMAIN\n\
225 -e enable expansion of some escape sequences\n\
226 -E (ignored for compatibility)\n\
227 -h, --help display this help and exit\n\
228 -V, --version display version information and exit\n\
229 [TEXTDOMAIN] retrieve translated message from TEXTDOMAIN\n\
230 MSGID MSGID-PLURAL translate MSGID (singular) / MSGID-PLURAL (plural)\n\
231 COUNT choose singular/plural form based on this value\n"));
233 /* xgettext: no-wrap */
235 If the TEXTDOMAIN parameter is not given, the domain is determined from the\n\
236 environment variable TEXTDOMAIN. If the message catalog is not found in the\n\
237 regular directory, another location can be specified with the environment\n\
238 variable TEXTDOMAINDIR.\n\
239 Standard search directory: %s\n"),
240 getenv ("IN_HELP2MAN") == NULL ? LOCALEDIR : "@localedir@");
242 /* TRANSLATORS: The placeholder indicates the bug-reporting address
243 for this package. Please add _another line_ saying
244 "Report translation bugs to <...>\n" with the address for translation
245 bugs (typically your translation team's web or email address). */
246 fputs (_("Report bugs to <bug-gnu-gettext@gnu.org>.\n"), stdout);
253 /* Expand some escape sequences found in the argument string. */
255 expand_escape (const char *str)
258 const char *cp = str;
262 while (cp[0] != '\0' && cp[0] != '\\')
266 /* Found a backslash. */
269 if (strchr ("abcfnrtv\\01234567", cp[1]) != NULL)
274 retval = XNMALLOC (strlen (str), char);
276 rp = retval + (cp - str);
277 memcpy (retval, str, cp - str);
281 /* Here cp[0] == '\\'. */
284 case 'a': /* alert */
288 case 'b': /* backspace */
292 case 'f': /* form feed */
296 case 'n': /* new line */
300 case 'r': /* carriage return */
304 case 't': /* horizontal tab */
308 case 'v': /* vertical tab */
316 case '0': case '1': case '2': case '3':
317 case '4': case '5': case '6': case '7':
319 int ch = *cp++ - '0';
321 if (*cp >= '0' && *cp <= '7')
326 if (*cp >= '0' && *cp <= '7')
340 while (cp[0] != '\0' && cp[0] != '\\')
343 while (cp[0] != '\0');
345 /* Terminate string. */
348 return (const char *) retval;