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
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"\
33 main (int argc
, char *argv
[])
35 struct gengetopt_args_info args_info
;
41 if (cmdline_parser (argc
, argv
, &args_info
) != 0)
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",
52 cmdline_parser_print_help ();
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]);
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
);
81 fprintf (stderr
, "%s: could not convert from %s to UTF-8.\n",
82 argv
[0], stringprep_locale_charset ());
86 if (args_info
.debug_given
)
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);
97 if (rc
!= STRINGPREP_OK
)
100 "%s: stringprep_profile() failed with error %d.\n",
105 if (args_info
.debug_given
)
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
);
115 fprintf (stderr
, "%s: could not convert from UTF-8 to %s.\n",
116 argv
[0], stringprep_locale_charset ());
120 fprintf (stdout
, "%s\n", p
);
125 if (args_info
.punycode_encode_given
)
129 p
= stringprep_locale_to_utf8 (readbuf
);
132 fprintf (stderr
, "%s: could not convert from %s to UTF-8.\n",
133 argv
[0], stringprep_locale_charset ());
137 q
= stringprep_utf8_to_ucs4 (p
, -1, &len
);
141 fprintf (stderr
, "%s: could not convert from UTF-8 to UCS-4.\n",
146 if (args_info
.debug_given
)
149 for (i
= 0; i
< len
; i
++)
150 fprintf (stderr
, "input[%d] = U+%04x\n", i
, q
[i
] & 0xFFFF);
154 rc
= punycode_encode (len
, q
, NULL
, &len2
, readbuf
);
155 if (rc
!= PUNYCODE_SUCCESS
)
158 "%s: punycode_encode() failed with error %d.\n",
163 readbuf
[len2
] = '\0';
165 p
= stringprep_utf8_to_locale (readbuf
);
168 fprintf (stderr
, "%s: could not convert from UTF-8 to %s.\n",
169 argv
[0], stringprep_locale_charset ());
173 fprintf (stdout
, "%s\n", p
);
178 if (args_info
.punycode_decode_given
)
183 q
= (uint32_t *) malloc (len
* sizeof (q
[0]));
186 sprintf (readbuf
, "%s: malloc() failed: ", argv
[0]);
191 rc
= punycode_decode (strlen (readbuf
), readbuf
, &len
, q
, NULL
);
192 if (rc
!= PUNYCODE_SUCCESS
)
196 "%s: punycode_decode() failed with error %d.\n",
201 if (args_info
.debug_given
)
204 for (i
= 0; i
< len
; i
++)
205 fprintf (stderr
, "output[%d] = U+%04x\n", i
, q
[i
] & 0xFFFF);
209 p
= stringprep_ucs4_to_utf8 (q
, -1, NULL
, NULL
);
213 fprintf (stderr
, "%s: could not convert from UCS-4 to UTF-8.\n",
218 r
= stringprep_utf8_to_locale (p
);
222 fprintf (stderr
, "%s: could not convert from UTF-8 to %s.\n",
223 argv
[0], stringprep_locale_charset ());
227 fprintf (stdout
, "%s\n", r
);
232 if (args_info
.idna_to_ascii_given
)
234 p
= stringprep_locale_to_utf8 (readbuf
);
237 fprintf (stderr
, "%s: could not convert from %s to UTF-8.\n",
238 argv
[0], stringprep_locale_charset ());
242 q
= stringprep_utf8_to_ucs4 (p
, -1, NULL
);
246 fprintf (stderr
, "%s: could not convert from UCS-4 to UTF-8.\n",
250 if (args_info
.debug_given
)
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));
263 if (rc
!= IDNA_SUCCESS
)
265 fprintf (stderr
, "%s: idna_to_ascii_from_locale() failed "
266 "with error %d.\n", argv
[0], rc
);
269 fprintf (stdout
, "%s\n", r
);
274 if (args_info
.idna_to_unicode_given
)
276 p
= stringprep_locale_to_utf8 (readbuf
);
279 fprintf (stderr
, "%s: could not convert from %s to UTF-8.\n",
280 argv
[0], stringprep_locale_charset ());
284 q
= stringprep_utf8_to_ucs4 (p
, -1, NULL
);
288 fprintf (stderr
, "%s: could not convert from UCS-4 to UTF-8.\n",
292 if (args_info
.debug_given
)
295 for (i
= 0; q
[i
]; i
++)
296 fprintf (stderr
, "input[%d] = U+%04x\n", i
, q
[i
] & 0xFFFF);
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));
306 if (rc
!= IDNA_SUCCESS
)
308 fprintf (stderr
, "%s: idna_to_unicode_locale_from_locale() "
309 "failed with error %d.\n", argv
[0], rc
);
313 if (args_info
.debug_given
)
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
);
324 fprintf (stderr
, "%s: could not convert from UCS-4 to UTF-8.\n",
329 r
= stringprep_utf8_to_locale (p
);
333 fprintf (stderr
, "%s: could not convert from UTF-8 to %s.\n",
334 argv
[0], stringprep_locale_charset ());
338 fprintf (stdout
, "%s\n", r
);
344 while (!feof (stdin
) && !ferror (stdin
));