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)
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. */
33 #include "error-progname.h"
35 #include "relocatable.h"
40 #include "msgl-iconv.h"
41 #include "localcharset.h"
45 #define _(str) gettext (str)
48 /* Force output of PO file even if empty. */
51 /* Target encoding. */
52 static const char *to_code
;
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', },
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
))
90 main (int argc
, char **argv
)
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
, "");
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. */
124 while ((opt
= getopt_long (argc
, argv
, "D:eEFhio:pPst:Vw:", long_options
,
129 case '\0': /* Long option. */
133 dir_list_append (optarg
);
137 message_print_style_escape (false);
141 message_print_style_escape (true);
145 sort_by_filepos
= true;
153 message_print_style_indent ();
157 output_file
= optarg
;
161 message_print_syntax_properties ();
162 output_syntax
= syntax_properties
;
166 input_syntax
= syntax_properties
;
170 sort_by_msgid
= true;
174 message_print_style_uniforum ();
189 value
= strtol (optarg
, &endp
, 10);
191 message_page_width_set (value
);
195 case CHAR_MAX
+ 1: /* --no-wrap */
196 message_page_width_ignore ();
199 case CHAR_MAX
+ 2: /* --stringtable-input */
200 input_syntax
= syntax_stringtable
;
203 case CHAR_MAX
+ 3: /* --stringtable-output */
204 message_print_syntax_stringtable ();
208 usage (EXIT_FAILURE
);
212 /* Version information is requested. */
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\
222 printf (_("Written by %s.\n"), "Bruno Haible");
226 /* Help is requested. */
228 usage (EXIT_SUCCESS
);
230 /* Test whether we have an .po file name as argument. */
233 else if (optind
+ 1 == argc
)
234 input_file
= argv
[optind
];
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. */
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. */
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);
275 /* Display usage information and exit. */
279 if (status
!= EXIT_SUCCESS
)
280 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
285 Usage: %s [OPTION] [INPUTFILE]\n\
289 Converts a translation catalog to a different character encoding.\n\
293 Mandatory arguments to long options are mandatory for short options too.\n"));
296 Input file location:\n"));
298 INPUTFILE input PO file\n"));
300 -D, --directory=DIRECTORY add DIRECTORY to list for input files search\n"));
302 If no input file is given or if it is -, standard input is read.\n"));
305 Output file location:\n"));
307 -o, --output-file=FILE write output to specified file\n"));
309 The results are written to standard output if no output file is specified\n\
313 Conversion target:\n"));
315 -t, --to-code=NAME encoding for output\n"));
317 The default encoding is the current locale's encoding.\n"));
320 Input file syntax:\n"));
322 -P, --properties-input input file is in Java .properties syntax\n"));
324 --stringtable-input input file is in NeXTstep/GNUstep .strings syntax\n"));
327 Output details:\n"));
329 -e, --no-escape do not use C escapes in output (default)\n"));
331 -E, --escape use C escapes in output, no extended chars\n"));
333 --force-po write PO file even if empty\n"));
335 -i, --indent indented output style\n"));
337 --no-location suppress '#: filename:line' lines\n"));
339 --add-location preserve '#: filename:line' lines (default)\n"));
341 --strict strict Uniforum output style\n"));
343 -p, --properties-output write out a Java .properties file\n"));
345 --stringtable-output write out a NeXTstep/GNUstep .strings file\n"));
347 -w, --width=NUMBER set output page width\n"));
349 --no-wrap do not break long message lines, longer than\n\
350 the output page width, into several lines\n"));
352 -s, --sort-output generate sorted output\n"));
354 -F, --sort-by-file sort output by file location\n"));
357 Informative output:\n"));
359 -h, --help display this help and exit\n"));
361 -V, --version output version information and exit\n"));
363 fputs (_("Report bugs to <bug-gnu-gettext@gnu.org>.\n"),