GTK-DOC doesn't like enum return types.
[libidn.git] / idn.c
blob36795db03f192af5e9b630c16185194ee9f68ea4
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 <stdio.h>
23 #include <unistd.h>
25 #include <stringprep.h>
26 #include <punycode.h>
27 #include <idna.h>
29 #include "idn_cmd.h"
31 #define GREETING "Copyright 2002, 2003 Simon Josefsson\n"\
32 "GNU Libidn comes with NO WARRANTY, to the extent permitted by law.\n"\
33 "You may redistribute copies of GNU Libidn under the terms of\n"\
34 "the GNU Lesser General Public License. For more information\n"\
35 "about these matters, see the file named COPYING.LIB.\n"\
37 int
38 main (int argc, char *argv[])
40 struct gengetopt_args_info args_info;
41 char readbuf[BUFSIZ];
42 char *p, *r;
43 uint32_t *q;
44 int rc;
46 if (cmdline_parser (argc, argv, &args_info) != 0)
47 return 1;
49 if ((args_info.stringprep_given ? 1 : 0) +
50 (args_info.punycode_encode_given ? 1 : 0) +
51 (args_info.punycode_decode_given ? 1 : 0) +
52 (args_info.idna_to_ascii_given ? 1 : 0) +
53 (args_info.idna_to_unicode_given ? 1 : 0) != 1)
55 fprintf (stderr, "%s: One of -s, -e, -d, -a or -u must be specified.\n",
56 argv[0]);
57 cmdline_parser_print_help ();
58 return 1;
61 if (!args_info.quiet_given)
62 fprintf (stderr, "%s %s\n" GREETING, PACKAGE, VERSION);
64 if (args_info.debug_given)
65 fprintf (stderr, "system locale uses charset `%s'.\n",
66 stringprep_locale_charset ());
70 if (fgets (readbuf, BUFSIZ, stdin) == NULL)
72 sprintf (readbuf, "%s: fgets() failed: ", argv[0]);
73 if (!feof (stdin))
74 perror (readbuf);
75 return 1;
78 if (readbuf[strlen (readbuf) - 1] == '\n')
79 readbuf[strlen (readbuf) - 1] = '\0';
81 if (args_info.stringprep_given)
83 p = stringprep_locale_to_utf8 (readbuf);
84 if (!p)
86 fprintf (stderr, "%s: could not convert from %s to UTF-8.\n",
87 argv[0], stringprep_locale_charset ());
88 return 1;
91 if (args_info.debug_given)
93 size_t i;
94 for (i = 0; p[i]; i++)
95 fprintf (stderr, "input[%d] = U+%04x\n", i, p[i] & 0xFFFF);
98 rc = stringprep_profile (p, &r,
99 args_info.profile_given ?
100 args_info.profile_arg : "Nameprep", 0);
101 free (p);
102 if (rc != STRINGPREP_OK)
104 fprintf (stderr,
105 "%s: stringprep_profile() failed with error %d.\n",
106 argv[0], rc);
107 return 1;
110 if (args_info.debug_given)
112 size_t i;
113 for (i = 0; r[i]; i++)
114 fprintf (stderr, "output[%d] = U+%04x\n", i, r[i] & 0xFFFF);
117 p = stringprep_utf8_to_locale (r);
118 if (!p)
120 fprintf (stderr, "%s: could not convert from UTF-8 to %s.\n",
121 argv[0], stringprep_locale_charset ());
122 return 1;
125 fprintf (stdout, "%s\n", p);
127 free (p);
130 if (args_info.punycode_encode_given)
132 size_t len, len2;
134 p = stringprep_locale_to_utf8 (readbuf);
135 if (!p)
137 fprintf (stderr, "%s: could not convert from %s to UTF-8.\n",
138 argv[0], stringprep_locale_charset ());
139 return 1;
142 q = stringprep_utf8_to_ucs4 (p, -1, &len);
143 free (p);
144 if (!q)
146 fprintf (stderr, "%s: could not convert from UTF-8 to UCS-4.\n",
147 argv[0]);
148 return 1;
151 if (args_info.debug_given)
153 size_t i;
154 for (i = 0; i < len; i++)
155 fprintf (stderr, "input[%d] = U+%04x\n", i, q[i] & 0xFFFF);
158 len2 = BUFSIZ;
159 rc = punycode_encode (len, q, NULL, &len2, readbuf);
160 if (rc != PUNYCODE_SUCCESS)
162 fprintf (stderr,
163 "%s: punycode_encode() failed with error %d.\n",
164 argv[0], rc);
165 return 1;
168 readbuf[len2] = '\0';
170 p = stringprep_utf8_to_locale (readbuf);
171 if (!p)
173 fprintf (stderr, "%s: could not convert from UTF-8 to %s.\n",
174 argv[0], stringprep_locale_charset ());
175 return 1;
178 fprintf (stdout, "%s\n", p);
180 free (p);
183 if (args_info.punycode_decode_given)
185 size_t len, len2;
187 len = BUFSIZ;
188 q = (uint32_t *) malloc (len * sizeof (q[0]));
189 if (!q)
191 sprintf (readbuf, "%s: malloc() failed: ", argv[0]);
192 perror (readbuf);
193 return 1;
196 rc = punycode_decode (strlen (readbuf), readbuf, &len, q, NULL);
197 if (rc != PUNYCODE_SUCCESS)
199 free (q);
200 fprintf (stderr,
201 "%s: punycode_decode() failed with error %d.\n",
202 argv[0], rc);
203 return 1;
206 if (args_info.debug_given)
208 size_t i;
209 for (i = 0; i < len; i++)
210 fprintf (stderr, "output[%d] = U+%04x\n", i, q[i] & 0xFFFF);
213 q[len] = 0;
214 p = stringprep_ucs4_to_utf8 (q, -1, NULL, NULL);
215 if (!p)
217 free (q);
218 fprintf (stderr, "%s: could not convert from UCS-4 to UTF-8.\n",
219 argv[0]);
220 return 1;
223 r = stringprep_utf8_to_locale (p);
224 free (p);
225 if (!r)
227 fprintf (stderr, "%s: could not convert from UTF-8 to %s.\n",
228 argv[0], stringprep_locale_charset ());
229 return 1;
232 fprintf (stdout, "%s\n", r);
234 free (r);
237 if (args_info.idna_to_ascii_given)
239 p = stringprep_locale_to_utf8 (readbuf);
240 if (!p)
242 fprintf (stderr, "%s: could not convert from %s to UTF-8.\n",
243 argv[0], stringprep_locale_charset ());
244 return 1;
247 q = stringprep_utf8_to_ucs4 (p, -1, NULL);
248 if (!q)
250 free (p);
251 fprintf (stderr, "%s: could not convert from UCS-4 to UTF-8.\n",
252 argv[0]);
253 return 1;
255 if (args_info.debug_given)
257 size_t i;
258 for (i = 0; q[i]; i++)
259 fprintf (stderr, "input[%d] = U+%04x\n", i, q[i] & 0xFFFF);
262 rc = idna_to_ascii_4z (q, &r,
263 (args_info.allow_unassigned_given ?
264 IDNA_ALLOW_UNASSIGNED : 0) |
265 (args_info.usestd3asciirules_given ?
266 IDNA_USE_STD3_ASCII_RULES : 0));
267 free (q);
268 if (rc != IDNA_SUCCESS)
270 fprintf (stderr, "%s: idna_to_ascii_from_locale() failed "
271 "with error %d.\n", argv[0], rc);
272 return 1;
274 fprintf (stdout, "%s\n", r);
276 free (r);
279 if (args_info.idna_to_unicode_given)
281 p = stringprep_locale_to_utf8 (readbuf);
282 if (!p)
284 fprintf (stderr, "%s: could not convert from %s to UTF-8.\n",
285 argv[0], stringprep_locale_charset ());
286 return 1;
289 q = stringprep_utf8_to_ucs4 (p, -1, NULL);
290 if (!q)
292 free (p);
293 fprintf (stderr, "%s: could not convert from UCS-4 to UTF-8.\n",
294 argv[0]);
295 return 1;
297 if (args_info.debug_given)
299 size_t i;
300 for (i = 0; q[i]; i++)
301 fprintf (stderr, "input[%d] = U+%04x\n", i, q[i] & 0xFFFF);
303 free (q);
305 rc = idna_to_unicode_8z4z (p, &q,
306 (args_info.allow_unassigned_given ?
307 IDNA_ALLOW_UNASSIGNED : 0) |
308 (args_info.usestd3asciirules_given ?
309 IDNA_USE_STD3_ASCII_RULES : 0));
310 free (p);
311 if (rc != IDNA_SUCCESS)
313 fprintf (stderr, "%s: idna_to_unicode_locale_from_locale() "
314 "failed with error %d.\n", argv[0], rc);
315 return 1;
318 if (args_info.debug_given)
320 size_t i;
321 for (i = 0; q[i]; i++)
322 fprintf (stderr, "output[%d] = U+%04x\n", i, q[i] & 0xFFFF);
325 p = stringprep_ucs4_to_utf8 (q, -1, NULL, NULL);
326 if (!p)
328 free (q);
329 fprintf (stderr, "%s: could not convert from UCS-4 to UTF-8.\n",
330 argv[0]);
331 return 1;
334 fprintf (stdout, "%s\n", p);
336 free (p);
340 while (!feof (stdin) && !ferror (stdin));
342 return 0;