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>
32 main (int argc
, char *argv
[])
34 struct gengetopt_args_info args_info
;
40 if (cmdline_parser(argc
, argv
, &args_info
) != 0)
43 if ((args_info
.punycode_encode_given
? 1 : 0) +
44 (args_info
.punycode_decode_given
? 1 : 0) +
45 (args_info
.idna_to_ascii_given
? 1 : 0) +
46 (args_info
.idna_to_unicode_given
? 1 : 0) != 1)
48 fprintf(stderr
, "%s: One of -e, -d, -a or -u must be specified.\n",
53 if (args_info
.debug_given
)
55 fprintf(stderr
, "system locale uses charset `%s'.\n",
56 stringprep_locale_charset());
59 if (!args_info
.quiet_given
)
61 fprintf(stderr
, "%s %s\n", PACKAGE
, VERSION
);
62 fprintf(stderr
, "Copyright 2003 Simon Josefsson\n"
63 "GNU Libidn comes with NO WARRANTY, to the extent permitted "
65 "You may redistribute copies of GNU Libidn under the terms of\n"
66 "the GNU Lesser General Public License. For more information\n"
67 "about these matters, see the file named COPYING.LIB.\n");
72 if (fgets(readbuf
, BUFSIZ
, stdin
) == NULL
)
74 sprintf(readbuf
, "%s: fgets() failed: ", argv
[0]);
80 if (readbuf
[strlen(readbuf
)-1] == '\n')
81 readbuf
[strlen(readbuf
)-1] = '\0';
83 if (args_info
.punycode_encode_given
)
87 p
= stringprep_locale_to_utf8 (readbuf
);
90 fprintf(stderr
, "%s: could not convert from %s to UTF-8.\n",
91 argv
[0], stringprep_locale_charset());
95 q
= stringprep_utf8_to_ucs4 (p
, -1, &len
);
99 fprintf(stderr
, "%s: could not convert from UTF-8 to UCS-4.\n",
104 if (args_info
.debug_given
)
107 for (i
= 0; i
< len
; i
++)
108 fprintf(stderr
, "input[%d] = U+%04x\n", i
, q
[i
]);
112 rc
= punycode_encode (len
, q
, NULL
, &len2
, readbuf
);
113 if (rc
!= PUNYCODE_SUCCESS
)
115 fprintf(stderr
, "%s: punycode_encode() failed with error %d.\n",
120 readbuf
[len2
] = '\0';
122 p
= stringprep_utf8_to_locale (readbuf
);
125 fprintf(stderr
, "%s: could not convert from UTF-8 to %s.\n",
126 argv
[0], stringprep_locale_charset());
130 fprintf(stdout
, "%s\n", p
);
135 if (args_info
.punycode_decode_given
)
140 q
= (unsigned long*) malloc(len
* sizeof(q
[0]));
143 sprintf(readbuf
, "%s: malloc() failed: ", argv
[0]);
148 rc
= punycode_decode (strlen(readbuf
), readbuf
, &len
, q
, NULL
);
149 if (rc
!= PUNYCODE_SUCCESS
)
152 fprintf(stderr
, "%s: punycode_decode() failed with error %d.\n",
157 if (args_info
.debug_given
)
160 for (i
= 0; i
< len
; i
++)
161 fprintf(stderr
, "output[%d] = U+%04x\n", i
, q
[i
]);
165 p
= stringprep_ucs4_to_utf8 (q
, -1, NULL
, NULL
);
169 fprintf(stderr
, "%s: could not convert from UCS-4 to UTF-8.\n",
174 r
= stringprep_utf8_to_locale (p
);
178 fprintf(stderr
, "%s: could not convert from UTF-8 to %s.\n",
179 argv
[0], stringprep_locale_charset());
183 fprintf(stdout
, "%s\n", r
);
188 if (args_info
.idna_to_ascii_given
)
190 rc
= idna_to_ascii_from_locale (readbuf
, &p
,
191 args_info
.allow_unassigned_given
,
192 args_info
.usestd3asciirules_given
);
193 if (rc
!= IDNA_SUCCESS
)
195 fprintf(stderr
, "%s: idna_to_ascii_from_locale() failed "
196 "with error %d.\n", argv
[0], rc
);
199 fprintf(stdout
, "%s\n", p
);
204 if (args_info
.idna_to_unicode_given
)
206 rc
= idna_to_unicode_locale_from_locale
207 (readbuf
, &p
, args_info
.allow_unassigned_given
,
208 args_info
.usestd3asciirules_given
);
209 if (rc
!= IDNA_SUCCESS
)
211 fprintf(stderr
, "%s: idna_to_unicode_locale_from_locale() "
212 "failed with error %d.\n", argv
[0], rc
);
215 fprintf(stdout
, "%s\n", p
);
221 while (!feof(stdin
) && !ferror(stdin
));