1 /* idn.c Command line interface to the library
2 * Copyright (C) 2003, 2004 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
30 #include <stringprep.h>
39 #define GREETING "Copyright 2002, 2003, 2004 Simon Josefsson.\n" \
40 "GNU Libidn comes with NO WARRANTY, to the extent permitted by law.\n" \
41 "You may redistribute copies of GNU Libidn under the terms of\n" \
42 "the GNU Lesser General Public License. For more information\n" \
43 "about these matters, see the file named COPYING.LIB.\n"
46 main (int argc
, char *argv
[])
48 struct gengetopt_args_info args_info
;
55 if (cmdline_parser (argc
, argv
, &args_info
) != 0)
58 if (!args_info
.stringprep_given
&&
59 !args_info
.punycode_encode_given
&& !args_info
.punycode_decode_given
&&
60 !args_info
.idna_to_ascii_given
&& !args_info
.idna_to_unicode_given
)
61 args_info
.idna_to_ascii_given
= 1;
63 if ((args_info
.stringprep_given
? 1 : 0) +
64 (args_info
.punycode_encode_given
? 1 : 0) +
65 (args_info
.punycode_decode_given
? 1 : 0) +
66 (args_info
.idna_to_ascii_given
? 1 : 0) +
67 (args_info
.idna_to_unicode_given
? 1 : 0) != 1)
70 "%s: Only one of -s, -e, -d, -a or -u can be specified.\n",
72 cmdline_parser_print_help ();
76 if (!args_info
.quiet_given
)
77 fprintf (stderr
, "%s %s\n" GREETING
, PACKAGE
, VERSION
);
79 if (args_info
.debug_given
)
80 fprintf (stderr
, "system locale uses charset `%s'.\n",
81 stringprep_locale_charset ());
83 if (!args_info
.quiet_given
&& args_info
.inputs_num
== 0)
84 fprintf (stderr
, "Type each input string on a line by itself, "
85 "terminated by a newline character.\n");
89 if (cmdn
< args_info
.inputs_num
)
91 strncpy (readbuf
, args_info
.inputs
[cmdn
++], BUFSIZ
- 1);
92 readbuf
[BUFSIZ
- 1] = '\0';
94 else if (fgets (readbuf
, BUFSIZ
, stdin
) == NULL
)
96 sprintf (readbuf
, "%s: fgets() failed: ", argv
[0]);
102 if (readbuf
[strlen (readbuf
) - 1] == '\n')
103 readbuf
[strlen (readbuf
) - 1] = '\0';
105 if (args_info
.stringprep_given
)
107 p
= stringprep_locale_to_utf8 (readbuf
);
110 fprintf (stderr
, "%s: could not convert from %s to UTF-8.\n",
111 argv
[0], stringprep_locale_charset ());
115 q
= stringprep_utf8_to_ucs4 (p
, -1, NULL
);
119 fprintf (stderr
, "%s: could not convert from UTF-8 to UCS-4.\n",
124 if (args_info
.debug_given
)
127 for (i
= 0; q
[i
]; i
++)
128 fprintf (stderr
, "input[%d] = U+%04x\n", i
, q
[i
]);
132 rc
= stringprep_profile (p
, &r
,
133 args_info
.profile_given
?
134 args_info
.profile_arg
: "Nameprep", 0);
136 if (rc
!= STRINGPREP_OK
)
139 "%s: stringprep_profile() failed with error %d.\n",
144 q
= stringprep_utf8_to_ucs4 (r
, -1, NULL
);
148 fprintf (stderr
, "%s: could not convert from UTF-8 to UCS-4.\n",
153 if (args_info
.debug_given
)
156 for (i
= 0; q
[i
]; i
++)
157 fprintf (stderr
, "output[%d] = U+%04x\n", i
, q
[i
]);
161 p
= stringprep_utf8_to_locale (r
);
165 fprintf (stderr
, "%s: could not convert from UTF-8 to %s.\n",
166 argv
[0], stringprep_locale_charset ());
170 fprintf (stdout
, "%s\n", p
);
175 if (args_info
.punycode_encode_given
)
179 p
= stringprep_locale_to_utf8 (readbuf
);
182 fprintf (stderr
, "%s: could not convert from %s to UTF-8.\n",
183 argv
[0], stringprep_locale_charset ());
187 q
= stringprep_utf8_to_ucs4 (p
, -1, &len
);
191 fprintf (stderr
, "%s: could not convert from UTF-8 to UCS-4.\n",
196 if (args_info
.debug_given
)
199 for (i
= 0; i
< len
; i
++)
200 fprintf (stderr
, "input[%d] = U+%04x\n", i
, q
[i
]);
204 rc
= punycode_encode (len
, q
, NULL
, &len2
, readbuf
);
206 if (rc
!= PUNYCODE_SUCCESS
)
209 "%s: punycode_encode() failed with error %d.\n",
214 readbuf
[len2
] = '\0';
216 p
= stringprep_utf8_to_locale (readbuf
);
219 fprintf (stderr
, "%s: could not convert from UTF-8 to %s.\n",
220 argv
[0], stringprep_locale_charset ());
224 fprintf (stdout
, "%s\n", p
);
229 if (args_info
.punycode_decode_given
)
234 q
= (uint32_t *) malloc (len
* sizeof (q
[0]));
237 sprintf (readbuf
, "%s: malloc() failed: ", argv
[0]);
243 rc
= punycode_decode (strlen (readbuf
), readbuf
, &len
, q
, NULL
);
244 if (rc
!= PUNYCODE_SUCCESS
)
248 "%s: punycode_decode() failed with error %d.\n",
253 if (args_info
.debug_given
)
256 for (i
= 0; i
< len
; i
++)
257 fprintf (stderr
, "output[%d] = U+%04x\n", i
, q
[i
]);
261 r
= stringprep_ucs4_to_utf8 (q
, -1, NULL
, NULL
);
265 fprintf (stderr
, "%s: could not convert from UCS-4 to UTF-8.\n",
270 p
= stringprep_utf8_to_locale (r
);
274 fprintf (stderr
, "%s: could not convert from UTF-8 to %s.\n",
275 argv
[0], stringprep_locale_charset ());
279 fprintf (stdout
, "%s\n", p
);
284 if (args_info
.idna_to_ascii_given
)
286 p
= stringprep_locale_to_utf8 (readbuf
);
289 fprintf (stderr
, "%s: could not convert from %s to UTF-8.\n",
290 argv
[0], stringprep_locale_charset ());
293 q
= stringprep_utf8_to_ucs4 (p
, -1, NULL
);
297 fprintf (stderr
, "%s: could not convert from UCS-4 to UTF-8.\n",
302 if (args_info
.debug_given
)
305 for (i
= 0; q
[i
]; i
++)
306 fprintf (stderr
, "input[%d] = U+%04x\n", i
, q
[i
]);
310 if (args_info
.tld_flag
)
312 const Tld_table
*tld
;
315 rc
= tld_check_8z (p
, &errpos
, NULL
);
316 if (rc
== TLD_INVALID
)
319 "%s: string rejected by TLD test (pos %d): %s\n",
325 if (rc
!= TLD_SUCCESS
)
328 "%s: tld_check_lz(%s) failed with error %d.\n",
337 rc
= idna_to_ascii_4z (q
, &p
,
338 (args_info
.allow_unassigned_given
?
339 IDNA_ALLOW_UNASSIGNED
: 0) |
340 (args_info
.usestd3asciirules_given
?
341 IDNA_USE_STD3_ASCII_RULES
: 0));
343 if (rc
!= IDNA_SUCCESS
)
345 fprintf (stderr
, "%s: idna_to_ascii_4z() failed "
346 "with error %d.\n", argv
[0], rc
);
350 if (args_info
.debug_given
)
353 for (i
= 0; p
[i
]; i
++)
354 fprintf (stderr
, "output[%d] = U+%04x\n", i
, p
[i
]);
357 fprintf (stdout
, "%s\n", p
);
362 if (args_info
.idna_to_unicode_given
)
364 p
= stringprep_locale_to_utf8 (readbuf
);
367 fprintf (stderr
, "%s: could not convert from %s to UTF-8.\n",
368 argv
[0], stringprep_locale_charset ());
372 q
= stringprep_utf8_to_ucs4 (p
, -1, NULL
);
376 fprintf (stderr
, "%s: could not convert from UCS-4 to UTF-8.\n",
381 if (args_info
.debug_given
)
384 for (i
= 0; q
[i
]; i
++)
385 fprintf (stderr
, "input[%d] = U+%04x\n", i
, q
[i
]);
390 if (args_info
.tld_flag
)
392 const Tld_table
*tld
;
395 rc
= tld_check_8z (p
, &errpos
, NULL
);
396 if (rc
== TLD_INVALID
)
399 "%s: string rejected by TLD test (pos %d): %s\n",
405 if (rc
!= TLD_SUCCESS
)
408 "%s: tld_check_lz(%s) failed with error %d.\n",
416 rc
= idna_to_unicode_8z4z (p
, &q
,
417 (args_info
.allow_unassigned_given
?
418 IDNA_ALLOW_UNASSIGNED
: 0) |
419 (args_info
.usestd3asciirules_given
?
420 IDNA_USE_STD3_ASCII_RULES
: 0));
422 if (rc
!= IDNA_SUCCESS
)
424 fprintf (stderr
, "%s: idna_to_unicode_8z4z() "
425 "failed with error %d.\n", argv
[0], rc
);
429 if (args_info
.debug_given
)
432 for (i
= 0; q
[i
]; i
++)
433 fprintf (stderr
, "output[%d] = U+%04x\n", i
, q
[i
]);
436 r
= stringprep_ucs4_to_utf8 (q
, -1, NULL
, NULL
);
440 fprintf (stderr
, "%s: could not convert from UTF-8 to UCS-4.\n",
445 p
= stringprep_utf8_to_locale (r
);
449 fprintf (stderr
, "%s: could not convert from UTF-8 to %s.\n",
450 argv
[0], stringprep_locale_charset ());
454 fprintf (stdout
, "%s\n", p
);
459 while (!feof (stdin
) && !ferror (stdin
) && (args_info
.inputs_num
== 0 ||
460 cmdn
< args_info
.inputs_num
));