Fixes for -DDRAFT.
[libidn.git] / idn.c
blob380da896c66e8e29ce94357c2216023ec78fd0f6
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 if (args_info.debug_given)
248 size_t i;
249 for (i = 0; p[i]; i++)
250 fprintf(stderr, "input[%d] = U+%04x\n", i, p[i] & 0xFFFF);
253 rc = idna_to_ascii_from_utf8 (p, &r,
254 args_info.allow_unassigned_given,
255 args_info.usestd3asciirules_given);
256 free(p);
257 if (rc != IDNA_SUCCESS)
259 fprintf(stderr, "%s: idna_to_ascii_from_locale() failed "
260 "with error %d.\n", argv[0], rc);
261 return 1;
263 fprintf(stdout, "%s\n", r);
265 free(r);
268 if (args_info.idna_to_unicode_given)
270 p = stringprep_locale_to_utf8 (readbuf);
271 if (!p)
273 fprintf(stderr, "%s: could not convert from %s to UTF-8.\n",
274 argv[0], stringprep_locale_charset());
275 return 1;
278 if (args_info.debug_given)
280 size_t i;
281 for (i = 0; p[i]; i++)
282 fprintf(stderr, "input[%d] = U+%04x\n", i, p[i] & 0xFFFF);
285 rc = idna_to_unicode_utf8_from_utf8
286 (p, &r, args_info.allow_unassigned_given,
287 args_info.usestd3asciirules_given);
288 free(p);
289 if (rc != IDNA_SUCCESS)
291 fprintf(stderr, "%s: idna_to_unicode_locale_from_locale() "
292 "failed with error %d.\n", argv[0], rc);
293 return 1;
296 if (args_info.debug_given)
298 size_t i;
299 for (i = 0; r[i]; i++)
300 fprintf(stderr, "output[%d] = U+%04x\n", i, r[i] & 0xFFFF);
303 fprintf(stdout, "%s\n", r);
305 free(r);
309 while (!feof(stdin) && !ferror(stdin));
311 return 0;