Fix.
[libidn.git] / src / idn.c
blob7f4251d26bc13bf7d5980565c88b788ad4c2d51e
1 /* idn.c Command line interface to the library
2 * Copyright (C) 2003 Simon Josefsson
4 * This file is part of GNU Libidn.
6 * GNU Libidn is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GNU Libidn is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Libidn; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "internal.h"
24 #include "idn_cmd.h"
26 #define GREETING "Copyright 2002, 2003 Simon Josefsson\n"\
27 "GNU Libidn comes with NO WARRANTY, to the extent permitted by law.\n"\
28 "You may redistribute copies of GNU Libidn under the terms of\n"\
29 "the GNU Lesser General Public License. For more information\n"\
30 "about these matters, see the file named COPYING.LIB.\n"\
32 int
33 main (int argc, char *argv[])
35 struct gengetopt_args_info args_info;
36 char readbuf[BUFSIZ];
37 char *p, *r;
38 uint32_t *q;
39 int rc;
41 if (cmdline_parser (argc, argv, &args_info) != 0)
42 return 1;
44 if ((args_info.stringprep_given ? 1 : 0) +
45 (args_info.punycode_encode_given ? 1 : 0) +
46 (args_info.punycode_decode_given ? 1 : 0) +
47 (args_info.idna_to_ascii_given ? 1 : 0) +
48 (args_info.idna_to_unicode_given ? 1 : 0) != 1)
50 fprintf (stderr, "%s: One of -s, -e, -d, -a or -u must be specified.\n",
51 argv[0]);
52 cmdline_parser_print_help ();
53 return 1;
56 if (!args_info.quiet_given)
57 fprintf (stderr, "%s %s\n" GREETING, PACKAGE, VERSION);
59 if (args_info.debug_given)
60 fprintf (stderr, "system locale uses charset `%s'.\n",
61 stringprep_locale_charset ());
65 if (fgets (readbuf, BUFSIZ, stdin) == NULL)
67 sprintf (readbuf, "%s: fgets() failed: ", argv[0]);
68 if (!feof (stdin))
69 perror (readbuf);
70 return 1;
73 if (readbuf[strlen (readbuf) - 1] == '\n')
74 readbuf[strlen (readbuf) - 1] = '\0';
76 if (args_info.stringprep_given)
78 p = stringprep_locale_to_utf8 (readbuf);
79 if (!p)
81 fprintf (stderr, "%s: could not convert from %s to UTF-8.\n",
82 argv[0], stringprep_locale_charset ());
83 return 1;
86 if (args_info.debug_given)
88 size_t i;
89 for (i = 0; p[i]; i++)
90 fprintf (stderr, "input[%d] = U+%04x\n", i, p[i] & 0xFFFF);
93 rc = stringprep_profile (p, &r,
94 args_info.profile_given ?
95 args_info.profile_arg : "Nameprep", 0);
96 free (p);
97 if (rc != STRINGPREP_OK)
99 fprintf (stderr,
100 "%s: stringprep_profile() failed with error %d.\n",
101 argv[0], rc);
102 return 1;
105 if (args_info.debug_given)
107 size_t i;
108 for (i = 0; r[i]; i++)
109 fprintf (stderr, "output[%d] = U+%04x\n", i, r[i] & 0xFFFF);
112 p = stringprep_utf8_to_locale (r);
113 if (!p)
115 fprintf (stderr, "%s: could not convert from UTF-8 to %s.\n",
116 argv[0], stringprep_locale_charset ());
117 return 1;
120 fprintf (stdout, "%s\n", p);
122 free (p);
125 if (args_info.punycode_encode_given)
127 size_t len, len2;
129 p = stringprep_locale_to_utf8 (readbuf);
130 if (!p)
132 fprintf (stderr, "%s: could not convert from %s to UTF-8.\n",
133 argv[0], stringprep_locale_charset ());
134 return 1;
137 q = stringprep_utf8_to_ucs4 (p, -1, &len);
138 free (p);
139 if (!q)
141 fprintf (stderr, "%s: could not convert from UTF-8 to UCS-4.\n",
142 argv[0]);
143 return 1;
146 if (args_info.debug_given)
148 size_t i;
149 for (i = 0; i < len; i++)
150 fprintf (stderr, "input[%d] = U+%04x\n", i, q[i] & 0xFFFF);
153 len2 = BUFSIZ;
154 rc = punycode_encode (len, q, NULL, &len2, readbuf);
155 if (rc != PUNYCODE_SUCCESS)
157 fprintf (stderr,
158 "%s: punycode_encode() failed with error %d.\n",
159 argv[0], rc);
160 return 1;
163 readbuf[len2] = '\0';
165 p = stringprep_utf8_to_locale (readbuf);
166 if (!p)
168 fprintf (stderr, "%s: could not convert from UTF-8 to %s.\n",
169 argv[0], stringprep_locale_charset ());
170 return 1;
173 fprintf (stdout, "%s\n", p);
175 free (p);
178 if (args_info.punycode_decode_given)
180 size_t len;
182 len = BUFSIZ;
183 q = (uint32_t *) malloc (len * sizeof (q[0]));
184 if (!q)
186 sprintf (readbuf, "%s: malloc() failed: ", argv[0]);
187 perror (readbuf);
188 return 1;
191 rc = punycode_decode (strlen (readbuf), readbuf, &len, q, NULL);
192 if (rc != PUNYCODE_SUCCESS)
194 free (q);
195 fprintf (stderr,
196 "%s: punycode_decode() failed with error %d.\n",
197 argv[0], rc);
198 return 1;
201 if (args_info.debug_given)
203 size_t i;
204 for (i = 0; i < len; i++)
205 fprintf (stderr, "output[%d] = U+%04x\n", i, q[i] & 0xFFFF);
208 q[len] = 0;
209 p = stringprep_ucs4_to_utf8 (q, -1, NULL, NULL);
210 if (!p)
212 free (q);
213 fprintf (stderr, "%s: could not convert from UCS-4 to UTF-8.\n",
214 argv[0]);
215 return 1;
218 r = stringprep_utf8_to_locale (p);
219 free (p);
220 if (!r)
222 fprintf (stderr, "%s: could not convert from UTF-8 to %s.\n",
223 argv[0], stringprep_locale_charset ());
224 return 1;
227 fprintf (stdout, "%s\n", r);
229 free (r);
232 if (args_info.idna_to_ascii_given)
234 p = stringprep_locale_to_utf8 (readbuf);
235 if (!p)
237 fprintf (stderr, "%s: could not convert from %s to UTF-8.\n",
238 argv[0], stringprep_locale_charset ());
239 return 1;
242 q = stringprep_utf8_to_ucs4 (p, -1, NULL);
243 if (!q)
245 free (p);
246 fprintf (stderr, "%s: could not convert from UCS-4 to UTF-8.\n",
247 argv[0]);
248 return 1;
250 if (args_info.debug_given)
252 size_t i;
253 for (i = 0; q[i]; i++)
254 fprintf (stderr, "input[%d] = U+%04x\n", i, q[i] & 0xFFFF);
257 rc = idna_to_ascii_4z (q, &r,
258 (args_info.allow_unassigned_given ?
259 IDNA_ALLOW_UNASSIGNED : 0) |
260 (args_info.usestd3asciirules_given ?
261 IDNA_USE_STD3_ASCII_RULES : 0));
262 free (q);
263 if (rc != IDNA_SUCCESS)
265 fprintf (stderr, "%s: idna_to_ascii_from_locale() failed "
266 "with error %d.\n", argv[0], rc);
267 return 1;
269 fprintf (stdout, "%s\n", r);
271 free (r);
274 if (args_info.idna_to_unicode_given)
276 p = stringprep_locale_to_utf8 (readbuf);
277 if (!p)
279 fprintf (stderr, "%s: could not convert from %s to UTF-8.\n",
280 argv[0], stringprep_locale_charset ());
281 return 1;
284 q = stringprep_utf8_to_ucs4 (p, -1, NULL);
285 if (!q)
287 free (p);
288 fprintf (stderr, "%s: could not convert from UCS-4 to UTF-8.\n",
289 argv[0]);
290 return 1;
292 if (args_info.debug_given)
294 size_t i;
295 for (i = 0; q[i]; i++)
296 fprintf (stderr, "input[%d] = U+%04x\n", i, q[i] & 0xFFFF);
298 free (q);
300 rc = idna_to_unicode_8z4z (p, &q,
301 (args_info.allow_unassigned_given ?
302 IDNA_ALLOW_UNASSIGNED : 0) |
303 (args_info.usestd3asciirules_given ?
304 IDNA_USE_STD3_ASCII_RULES : 0));
305 free (p);
306 if (rc != IDNA_SUCCESS)
308 fprintf (stderr, "%s: idna_to_unicode_locale_from_locale() "
309 "failed with error %d.\n", argv[0], rc);
310 return 1;
313 if (args_info.debug_given)
315 size_t i;
316 for (i = 0; q[i]; i++)
317 fprintf (stderr, "output[%d] = U+%04x\n", i, q[i] & 0xFFFF);
320 p = stringprep_ucs4_to_utf8 (q, -1, NULL, NULL);
321 if (!p)
323 free (q);
324 fprintf (stderr, "%s: could not convert from UCS-4 to UTF-8.\n",
325 argv[0]);
326 return 1;
329 r = stringprep_utf8_to_locale (p);
330 free (p);
331 if (!r)
333 fprintf (stderr, "%s: could not convert from UTF-8 to %s.\n",
334 argv[0], stringprep_locale_charset ());
335 return 1;
338 fprintf (stdout, "%s\n", r);
340 free (r);
344 while (!feof (stdin) && !ferror (stdin));
346 return 0;