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
25 #include <stringprep.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"\
38 main (int argc
, char *argv
[])
40 struct gengetopt_args_info args_info
;
46 if (cmdline_parser (argc
, argv
, &args_info
) != 0)
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",
57 cmdline_parser_print_help ();
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]);
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
);
86 fprintf (stderr
, "%s: could not convert from %s to UTF-8.\n",
87 argv
[0], stringprep_locale_charset ());
91 if (args_info
.debug_given
)
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);
102 if (rc
!= STRINGPREP_OK
)
105 "%s: stringprep_profile() failed with error %d.\n",
110 if (args_info
.debug_given
)
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
);
120 fprintf (stderr
, "%s: could not convert from UTF-8 to %s.\n",
121 argv
[0], stringprep_locale_charset ());
125 fprintf (stdout
, "%s\n", p
);
130 if (args_info
.punycode_encode_given
)
134 p
= stringprep_locale_to_utf8 (readbuf
);
137 fprintf (stderr
, "%s: could not convert from %s to UTF-8.\n",
138 argv
[0], stringprep_locale_charset ());
142 q
= stringprep_utf8_to_ucs4 (p
, -1, &len
);
146 fprintf (stderr
, "%s: could not convert from UTF-8 to UCS-4.\n",
151 if (args_info
.debug_given
)
154 for (i
= 0; i
< len
; i
++)
155 fprintf (stderr
, "input[%d] = U+%04x\n", i
, q
[i
] & 0xFFFF);
159 rc
= punycode_encode (len
, q
, NULL
, &len2
, readbuf
);
160 if (rc
!= PUNYCODE_SUCCESS
)
163 "%s: punycode_encode() failed with error %d.\n",
168 readbuf
[len2
] = '\0';
170 p
= stringprep_utf8_to_locale (readbuf
);
173 fprintf (stderr
, "%s: could not convert from UTF-8 to %s.\n",
174 argv
[0], stringprep_locale_charset ());
178 fprintf (stdout
, "%s\n", p
);
183 if (args_info
.punycode_decode_given
)
188 q
= (uint32_t *) malloc (len
* sizeof (q
[0]));
191 sprintf (readbuf
, "%s: malloc() failed: ", argv
[0]);
196 rc
= punycode_decode (strlen (readbuf
), readbuf
, &len
, q
, NULL
);
197 if (rc
!= PUNYCODE_SUCCESS
)
201 "%s: punycode_decode() failed with error %d.\n",
206 if (args_info
.debug_given
)
209 for (i
= 0; i
< len
; i
++)
210 fprintf (stderr
, "output[%d] = U+%04x\n", i
, q
[i
] & 0xFFFF);
214 p
= stringprep_ucs4_to_utf8 (q
, -1, NULL
, NULL
);
218 fprintf (stderr
, "%s: could not convert from UCS-4 to UTF-8.\n",
223 r
= stringprep_utf8_to_locale (p
);
227 fprintf (stderr
, "%s: could not convert from UTF-8 to %s.\n",
228 argv
[0], stringprep_locale_charset ());
232 fprintf (stdout
, "%s\n", r
);
237 if (args_info
.idna_to_ascii_given
)
239 p
= stringprep_locale_to_utf8 (readbuf
);
242 fprintf (stderr
, "%s: could not convert from %s to UTF-8.\n",
243 argv
[0], stringprep_locale_charset ());
247 q
= stringprep_utf8_to_ucs4 (p
, -1, NULL
);
251 fprintf (stderr
, "%s: could not convert from UCS-4 to UTF-8.\n",
255 if (args_info
.debug_given
)
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));
268 if (rc
!= IDNA_SUCCESS
)
270 fprintf (stderr
, "%s: idna_to_ascii_from_locale() failed "
271 "with error %d.\n", argv
[0], rc
);
274 fprintf (stdout
, "%s\n", r
);
279 if (args_info
.idna_to_unicode_given
)
281 p
= stringprep_locale_to_utf8 (readbuf
);
284 fprintf (stderr
, "%s: could not convert from %s to UTF-8.\n",
285 argv
[0], stringprep_locale_charset ());
289 q
= stringprep_utf8_to_ucs4 (p
, -1, NULL
);
293 fprintf (stderr
, "%s: could not convert from UCS-4 to UTF-8.\n",
297 if (args_info
.debug_given
)
300 for (i
= 0; q
[i
]; i
++)
301 fprintf (stderr
, "input[%d] = U+%04x\n", i
, q
[i
] & 0xFFFF);
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));
311 if (rc
!= IDNA_SUCCESS
)
313 fprintf (stderr
, "%s: idna_to_unicode_locale_from_locale() "
314 "failed with error %d.\n", argv
[0], rc
);
318 if (args_info
.debug_given
)
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
);
329 fprintf (stderr
, "%s: could not convert from UCS-4 to UTF-8.\n",
334 r
= stringprep_utf8_to_locale (p
);
338 fprintf (stderr
, "%s: could not convert from UTF-8 to %s.\n",
339 argv
[0], stringprep_locale_charset ());
343 fprintf (stdout
, "%s\n", r
);
349 while (!feof (stdin
) && !ferror (stdin
));