No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / src / msgconv.c
blobefcd09fda948939c640f55e25dc30396f51d2695
1 /* Converts a translation catalog to a different character encoding.
2 Copyright (C) 2001-2005 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
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 2, or (at your option)
8 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, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include <getopt.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <locale.h>
30 #include "closeout.h"
31 #include "dir-list.h"
32 #include "error.h"
33 #include "error-progname.h"
34 #include "progname.h"
35 #include "relocatable.h"
36 #include "basename.h"
37 #include "message.h"
38 #include "read-po.h"
39 #include "write-po.h"
40 #include "msgl-iconv.h"
41 #include "localcharset.h"
42 #include "exit.h"
43 #include "gettext.h"
45 #define _(str) gettext (str)
48 /* Force output of PO file even if empty. */
49 static int force_po;
51 /* Target encoding. */
52 static const char *to_code;
54 /* Long options. */
55 static const struct option long_options[] =
57 { "add-location", no_argument, &line_comment, 1 },
58 { "directory", required_argument, NULL, 'D' },
59 { "escape", no_argument, NULL, 'E' },
60 { "force-po", no_argument, &force_po, 1 },
61 { "help", no_argument, NULL, 'h' },
62 { "indent", no_argument, NULL, 'i' },
63 { "no-escape", no_argument, NULL, 'e' },
64 { "no-location", no_argument, &line_comment, 0 },
65 { "no-wrap", no_argument, NULL, CHAR_MAX + 1 },
66 { "output-file", required_argument, NULL, 'o' },
67 { "properties-input", no_argument, NULL, 'P' },
68 { "properties-output", no_argument, NULL, 'p' },
69 { "sort-by-file", no_argument, NULL, 'F' },
70 { "sort-output", no_argument, NULL, 's' },
71 { "strict", no_argument, NULL, 'S' },
72 { "stringtable-input", no_argument, NULL, CHAR_MAX + 2 },
73 { "stringtable-output", no_argument, NULL, CHAR_MAX + 3 },
74 { "to-code", required_argument, NULL, 't' },
75 { "version", no_argument, NULL, 'V' },
76 { "width", required_argument, NULL, 'w', },
77 { NULL, 0, NULL, 0 }
81 /* Forward declaration of local functions. */
82 static void usage (int status)
83 #if defined __GNUC__ && ((__GNUC__ == 2 && __GNUC_MINOR__ >= 5) || __GNUC__ > 2)
84 __attribute__ ((noreturn))
85 #endif
89 int
90 main (int argc, char **argv)
92 int opt;
93 bool do_help;
94 bool do_version;
95 char *output_file;
96 const char *input_file;
97 msgdomain_list_ty *result;
98 input_syntax_ty output_syntax = syntax_po;
99 bool sort_by_filepos = false;
100 bool sort_by_msgid = false;
102 /* Set program name for messages. */
103 set_program_name (argv[0]);
104 error_print_progname = maybe_print_progname;
106 #ifdef HAVE_SETLOCALE
107 /* Set locale via LC_ALL. */
108 setlocale (LC_ALL, "");
109 #endif
111 /* Set the text message domain. */
112 bindtextdomain (PACKAGE, relocate (LOCALEDIR));
113 textdomain (PACKAGE);
115 /* Ensure that write errors on stdout are detected. */
116 atexit (close_stdout);
118 /* Set default values for variables. */
119 do_help = false;
120 do_version = false;
121 output_file = NULL;
122 input_file = NULL;
124 while ((opt = getopt_long (argc, argv, "D:eEFhio:pPst:Vw:", long_options,
125 NULL))
126 != EOF)
127 switch (opt)
129 case '\0': /* Long option. */
130 break;
132 case 'D':
133 dir_list_append (optarg);
134 break;
136 case 'e':
137 message_print_style_escape (false);
138 break;
140 case 'E':
141 message_print_style_escape (true);
142 break;
144 case 'F':
145 sort_by_filepos = true;
146 break;
148 case 'h':
149 do_help = true;
150 break;
152 case 'i':
153 message_print_style_indent ();
154 break;
156 case 'o':
157 output_file = optarg;
158 break;
160 case 'p':
161 message_print_syntax_properties ();
162 output_syntax = syntax_properties;
163 break;
165 case 'P':
166 input_syntax = syntax_properties;
167 break;
169 case 's':
170 sort_by_msgid = true;
171 break;
173 case 'S':
174 message_print_style_uniforum ();
175 break;
177 case 't':
178 to_code = optarg;
179 break;
181 case 'V':
182 do_version = true;
183 break;
185 case 'w':
187 int value;
188 char *endp;
189 value = strtol (optarg, &endp, 10);
190 if (endp != optarg)
191 message_page_width_set (value);
193 break;
195 case CHAR_MAX + 1: /* --no-wrap */
196 message_page_width_ignore ();
197 break;
199 case CHAR_MAX + 2: /* --stringtable-input */
200 input_syntax = syntax_stringtable;
201 break;
203 case CHAR_MAX + 3: /* --stringtable-output */
204 message_print_syntax_stringtable ();
205 break;
207 default:
208 usage (EXIT_FAILURE);
209 break;
212 /* Version information is requested. */
213 if (do_version)
215 printf ("%s (GNU %s) %s\n", basename (program_name), PACKAGE, VERSION);
216 /* xgettext: no-wrap */
217 printf (_("Copyright (C) %s Free Software Foundation, Inc.\n\
218 This is free software; see the source for copying conditions. There is NO\n\
219 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
221 "2001-2005");
222 printf (_("Written by %s.\n"), "Bruno Haible");
223 exit (EXIT_SUCCESS);
226 /* Help is requested. */
227 if (do_help)
228 usage (EXIT_SUCCESS);
230 /* Test whether we have an .po file name as argument. */
231 if (optind == argc)
232 input_file = "-";
233 else if (optind + 1 == argc)
234 input_file = argv[optind];
235 else
237 error (EXIT_SUCCESS, 0, _("at most one input file allowed"));
238 usage (EXIT_FAILURE);
241 /* Verify selected options. */
242 if (!line_comment && sort_by_filepos)
243 error (EXIT_FAILURE, 0, _("%s and %s are mutually exclusive"),
244 "--no-location", "--sort-by-file");
246 if (sort_by_msgid && sort_by_filepos)
247 error (EXIT_FAILURE, 0, _("%s and %s are mutually exclusive"),
248 "--sort-output", "--sort-by-file");
250 /* Default for target encoding is current locale's encoding. */
251 if (to_code == NULL)
252 to_code = locale_charset ();
254 /* Read input file. */
255 result = read_po_file (input_file);
257 /* Convert if and only if the output syntax supports different encodings. */
258 if (output_syntax != syntax_properties
259 && output_syntax != syntax_stringtable)
260 result = iconv_msgdomain_list (result, to_code, input_file);
262 /* Sort the results. */
263 if (sort_by_filepos)
264 msgdomain_list_sort_by_filepos (result);
265 else if (sort_by_msgid)
266 msgdomain_list_sort_by_msgid (result);
268 /* Write the merged message list out. */
269 msgdomain_list_print (result, output_file, force_po, false);
271 exit (EXIT_SUCCESS);
275 /* Display usage information and exit. */
276 static void
277 usage (int status)
279 if (status != EXIT_SUCCESS)
280 fprintf (stderr, _("Try `%s --help' for more information.\n"),
281 program_name);
282 else
284 printf (_("\
285 Usage: %s [OPTION] [INPUTFILE]\n\
286 "), program_name);
287 printf ("\n");
288 printf (_("\
289 Converts a translation catalog to a different character encoding.\n\
290 "));
291 printf ("\n");
292 printf (_("\
293 Mandatory arguments to long options are mandatory for short options too.\n"));
294 printf ("\n");
295 printf (_("\
296 Input file location:\n"));
297 printf (_("\
298 INPUTFILE input PO file\n"));
299 printf (_("\
300 -D, --directory=DIRECTORY add DIRECTORY to list for input files search\n"));
301 printf (_("\
302 If no input file is given or if it is -, standard input is read.\n"));
303 printf ("\n");
304 printf (_("\
305 Output file location:\n"));
306 printf (_("\
307 -o, --output-file=FILE write output to specified file\n"));
308 printf (_("\
309 The results are written to standard output if no output file is specified\n\
310 or if it is -.\n"));
311 printf ("\n");
312 printf (_("\
313 Conversion target:\n"));
314 printf (_("\
315 -t, --to-code=NAME encoding for output\n"));
316 printf (_("\
317 The default encoding is the current locale's encoding.\n"));
318 printf ("\n");
319 printf (_("\
320 Input file syntax:\n"));
321 printf (_("\
322 -P, --properties-input input file is in Java .properties syntax\n"));
323 printf (_("\
324 --stringtable-input input file is in NeXTstep/GNUstep .strings syntax\n"));
325 printf ("\n");
326 printf (_("\
327 Output details:\n"));
328 printf (_("\
329 -e, --no-escape do not use C escapes in output (default)\n"));
330 printf (_("\
331 -E, --escape use C escapes in output, no extended chars\n"));
332 printf (_("\
333 --force-po write PO file even if empty\n"));
334 printf (_("\
335 -i, --indent indented output style\n"));
336 printf (_("\
337 --no-location suppress '#: filename:line' lines\n"));
338 printf (_("\
339 --add-location preserve '#: filename:line' lines (default)\n"));
340 printf (_("\
341 --strict strict Uniforum output style\n"));
342 printf (_("\
343 -p, --properties-output write out a Java .properties file\n"));
344 printf (_("\
345 --stringtable-output write out a NeXTstep/GNUstep .strings file\n"));
346 printf (_("\
347 -w, --width=NUMBER set output page width\n"));
348 printf (_("\
349 --no-wrap do not break long message lines, longer than\n\
350 the output page width, into several lines\n"));
351 printf (_("\
352 -s, --sort-output generate sorted output\n"));
353 printf (_("\
354 -F, --sort-by-file sort output by file location\n"));
355 printf ("\n");
356 printf (_("\
357 Informative output:\n"));
358 printf (_("\
359 -h, --help display this help and exit\n"));
360 printf (_("\
361 -V, --version output version information and exit\n"));
362 printf ("\n");
363 fputs (_("Report bugs to <bug-gnu-gettext@gnu.org>.\n"),
364 stdout);
367 exit (status);