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
:
103 if (rc
!= STRINGPREP_OK
)
106 "%s: stringprep_profile() failed with error %d.\n",
111 if (args_info
.debug_given
)
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
);
121 fprintf(stderr
, "%s: could not convert from UTF-8 to %s.\n",
122 argv
[0], stringprep_locale_charset());
126 fprintf(stdout
, "%s\n", p
);
131 if (args_info
.punycode_encode_given
)
135 p
= stringprep_locale_to_utf8 (readbuf
);
138 fprintf(stderr
, "%s: could not convert from %s to UTF-8.\n",
139 argv
[0], stringprep_locale_charset());
143 q
= stringprep_utf8_to_ucs4 (p
, -1, &len
);
147 fprintf(stderr
, "%s: could not convert from UTF-8 to UCS-4.\n",
152 if (args_info
.debug_given
)
155 for (i
= 0; i
< len
; i
++)
156 fprintf(stderr
, "input[%d] = U+%04x\n", i
, q
[i
] & 0xFFFF);
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",
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
= (unsigned long*) 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
)
200 fprintf(stderr
, "%s: punycode_decode() failed with error %d.\n",
205 if (args_info
.debug_given
)
208 for (i
= 0; i
< len
; i
++)
209 fprintf(stderr
, "output[%d] = U+%04x\n", i
, q
[i
] & 0xFFFF);
213 p
= stringprep_ucs4_to_utf8 (q
, -1, NULL
, NULL
);
217 fprintf(stderr
, "%s: could not convert from UCS-4 to UTF-8.\n",
222 r
= stringprep_utf8_to_locale (p
);
226 fprintf(stderr
, "%s: could not convert from UTF-8 to %s.\n",
227 argv
[0], stringprep_locale_charset());
231 fprintf(stdout
, "%s\n", r
);
236 if (args_info
.idna_to_ascii_given
)
238 p
= stringprep_locale_to_utf8 (readbuf
);
241 fprintf(stderr
, "%s: could not convert from %s to UTF-8.\n",
242 argv
[0], stringprep_locale_charset());
246 if (args_info
.debug_given
)
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
);
257 if (rc
!= IDNA_SUCCESS
)
259 fprintf(stderr
, "%s: idna_to_ascii_from_locale() failed "
260 "with error %d.\n", argv
[0], rc
);
263 fprintf(stdout
, "%s\n", r
);
268 if (args_info
.idna_to_unicode_given
)
270 p
= stringprep_locale_to_utf8 (readbuf
);
273 fprintf(stderr
, "%s: could not convert from %s to UTF-8.\n",
274 argv
[0], stringprep_locale_charset());
278 if (args_info
.debug_given
)
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
);
289 if (rc
!= IDNA_SUCCESS
)
291 fprintf(stderr
, "%s: idna_to_unicode_locale_from_locale() "
292 "failed with error %d.\n", argv
[0], rc
);
296 if (args_info
.debug_given
)
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
);
309 while (!feof(stdin
) && !ferror(stdin
));