Add unmodified doxygen configuration file, for future "diff"s.
[libidn.git] / idn.c
blob00c54091c766c18d2b4ab1f1e3665041a4de8b25
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 unsigned long *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 :
101 "Nameprep", 0);
102 free(p);
103 if (rc != STRINGPREP_OK)
105 fprintf(stderr,
106 "%s: stringprep_profile() failed with error %d.\n",
107 argv[0], rc);
108 return 1;
111 if (args_info.debug_given)
113 size_t i;
114 for (i = 0; r[i]; i++)
115 fprintf(stderr, "output[%d] = U+%04x\n", i, r[i] & 0xFFFF);
118 p = stringprep_utf8_to_locale (r);
119 if (!p)
121 fprintf(stderr, "%s: could not convert from UTF-8 to %s.\n",
122 argv[0], stringprep_locale_charset());
123 return 1;
126 fprintf(stdout, "%s\n", p);
128 free(p);
131 if (args_info.punycode_encode_given)
133 size_t len, len2;
135 p = stringprep_locale_to_utf8 (readbuf);
136 if (!p)
138 fprintf(stderr, "%s: could not convert from %s to UTF-8.\n",
139 argv[0], stringprep_locale_charset());
140 return 1;
143 q = stringprep_utf8_to_ucs4 (p, -1, &len);
144 free(p);
145 if (!q)
147 fprintf(stderr, "%s: could not convert from UTF-8 to UCS-4.\n",
148 argv[0]);
149 return 1;
152 if (args_info.debug_given)
154 size_t i;
155 for (i = 0; i < len; i++)
156 fprintf(stderr, "input[%d] = U+%04x\n", i, q[i] & 0xFFFF);
159 len2 = BUFSIZ;
160 rc = punycode_encode (len, q, NULL, &len2, readbuf);
161 if (rc != PUNYCODE_SUCCESS)
163 fprintf(stderr, "%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 = (unsigned long*) 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, "%s: punycode_decode() failed with error %d.\n",
201 argv[0], rc);
202 return 1;
205 if (args_info.debug_given)
207 size_t i;
208 for (i = 0; i < len; i++)
209 fprintf(stderr, "output[%d] = U+%04x\n", i, q[i] & 0xFFFF);
212 q[len] = 0;
213 p = stringprep_ucs4_to_utf8 (q, -1, NULL, NULL);
214 if (!p)
216 free(q);
217 fprintf(stderr, "%s: could not convert from UCS-4 to UTF-8.\n",
218 argv[0]);
219 return 1;
222 r = stringprep_utf8_to_locale (p);
223 free(p);
224 if (!r)
226 fprintf(stderr, "%s: could not convert from UTF-8 to %s.\n",
227 argv[0], stringprep_locale_charset());
228 return 1;
231 fprintf(stdout, "%s\n", r);
233 free(r);
236 if (args_info.idna_to_ascii_given)
238 p = stringprep_locale_to_utf8 (readbuf);
239 if (!p)
241 fprintf(stderr, "%s: could not convert from %s to UTF-8.\n",
242 argv[0], stringprep_locale_charset());
243 return 1;
246 q = stringprep_utf8_to_ucs4 (p, -1, NULL);
247 if (!q)
249 free(p);
250 fprintf(stderr, "%s: could not convert from UCS-4 to UTF-8.\n",
251 argv[0]);
252 return 1;
254 if (args_info.debug_given)
256 size_t i;
257 for (i = 0; q[i]; i++)
258 fprintf(stderr, "input[%d] = U+%04x\n", i, q[i] & 0xFFFF);
261 rc = idna_to_ascii_from_ucs4 (q, &r,
262 args_info.allow_unassigned_given,
263 args_info.usestd3asciirules_given);
264 free(q);
265 if (rc != IDNA_SUCCESS)
267 fprintf(stderr, "%s: idna_to_ascii_from_locale() failed "
268 "with error %d.\n", argv[0], rc);
269 return 1;
271 fprintf(stdout, "%s\n", r);
273 free(r);
276 if (args_info.idna_to_unicode_given)
278 p = stringprep_locale_to_utf8 (readbuf);
279 if (!p)
281 fprintf(stderr, "%s: could not convert from %s to UTF-8.\n",
282 argv[0], stringprep_locale_charset());
283 return 1;
286 q = stringprep_utf8_to_ucs4 (p, -1, NULL);
287 if (!q)
289 free(p);
290 fprintf(stderr, "%s: could not convert from UCS-4 to UTF-8.\n",
291 argv[0]);
292 return 1;
294 if (args_info.debug_given)
296 size_t i;
297 for (i = 0; q[i]; i++)
298 fprintf(stderr, "input[%d] = U+%04x\n", i, q[i] & 0xFFFF);
300 free(q);
302 rc = idna_to_unicode_ucs4_from_utf8
303 (p, &q, args_info.allow_unassigned_given,
304 args_info.usestd3asciirules_given);
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 fprintf(stdout, "%s\n", p);
331 free(p);
335 while (!feof(stdin) && !ferror(stdin));
337 return 0;