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 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
.punycode_encode_given
? 1 : 0) +
50 (args_info
.punycode_decode_given
? 1 : 0) +
51 (args_info
.idna_to_ascii_given
? 1 : 0) +
52 (args_info
.idna_to_unicode_given
? 1 : 0) != 1)
54 fprintf(stderr
, "%s: One of -e, -d, -a or -u must be specified.\n",
56 cmdline_parser_print_help();
60 if (!args_info
.quiet_given
)
61 fprintf(stderr
, "%s %s\n" GREETING
, PACKAGE
, VERSION
);
63 if (args_info
.debug_given
)
64 fprintf(stderr
, "system locale uses charset `%s'.\n",
65 stringprep_locale_charset());
69 if (fgets(readbuf
, BUFSIZ
, stdin
) == NULL
)
71 sprintf(readbuf
, "%s: fgets() failed: ", argv
[0]);
77 if (readbuf
[strlen(readbuf
)-1] == '\n')
78 readbuf
[strlen(readbuf
)-1] = '\0';
80 if (args_info
.punycode_encode_given
)
84 p
= stringprep_locale_to_utf8 (readbuf
);
87 fprintf(stderr
, "%s: could not convert from %s to UTF-8.\n",
88 argv
[0], stringprep_locale_charset());
92 q
= stringprep_utf8_to_ucs4 (p
, -1, &len
);
96 fprintf(stderr
, "%s: could not convert from UTF-8 to UCS-4.\n",
101 if (args_info
.debug_given
)
104 for (i
= 0; i
< len
; i
++)
105 fprintf(stderr
, "input[%d] = U+%04x\n", i
, q
[i
]);
109 rc
= punycode_encode (len
, q
, NULL
, &len2
, readbuf
);
110 if (rc
!= PUNYCODE_SUCCESS
)
112 fprintf(stderr
, "%s: punycode_encode() failed with error %d.\n",
117 readbuf
[len2
] = '\0';
119 p
= stringprep_utf8_to_locale (readbuf
);
122 fprintf(stderr
, "%s: could not convert from UTF-8 to %s.\n",
123 argv
[0], stringprep_locale_charset());
127 fprintf(stdout
, "%s\n", p
);
132 if (args_info
.punycode_decode_given
)
137 q
= (unsigned long*) malloc(len
* sizeof(q
[0]));
140 sprintf(readbuf
, "%s: malloc() failed: ", argv
[0]);
145 rc
= punycode_decode (strlen(readbuf
), readbuf
, &len
, q
, NULL
);
146 if (rc
!= PUNYCODE_SUCCESS
)
149 fprintf(stderr
, "%s: punycode_decode() failed with error %d.\n",
154 if (args_info
.debug_given
)
157 for (i
= 0; i
< len
; i
++)
158 fprintf(stderr
, "output[%d] = U+%04x\n", i
, q
[i
]);
162 p
= stringprep_ucs4_to_utf8 (q
, -1, NULL
, NULL
);
166 fprintf(stderr
, "%s: could not convert from UCS-4 to UTF-8.\n",
171 r
= stringprep_utf8_to_locale (p
);
175 fprintf(stderr
, "%s: could not convert from UTF-8 to %s.\n",
176 argv
[0], stringprep_locale_charset());
180 fprintf(stdout
, "%s\n", r
);
185 if (args_info
.idna_to_ascii_given
)
187 rc
= idna_to_ascii_from_locale (readbuf
, &p
,
188 args_info
.allow_unassigned_given
,
189 args_info
.usestd3asciirules_given
);
190 if (rc
!= IDNA_SUCCESS
)
192 fprintf(stderr
, "%s: idna_to_ascii_from_locale() failed "
193 "with error %d.\n", argv
[0], rc
);
196 fprintf(stdout
, "%s\n", p
);
201 if (args_info
.idna_to_unicode_given
)
203 rc
= idna_to_unicode_locale_from_locale
204 (readbuf
, &p
, args_info
.allow_unassigned_given
,
205 args_info
.usestd3asciirules_given
);
206 if (rc
!= IDNA_SUCCESS
)
208 fprintf(stderr
, "%s: idna_to_unicode_locale_from_locale() "
209 "failed with error %d.\n", argv
[0], rc
);
212 fprintf(stdout
, "%s\n", p
);
218 while (!feof(stdin
) && !ferror(stdin
));