Add.
[libidn.git] / idn.c
blobeb0ce3595716c3dfb86aff91057167e02a18d5eb
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
22 #include <stdio.h>
23 #include <unistd.h>
25 #include <stringprep.h>
26 #include <punycode.h>
27 #include <idna.h>
29 #include "idn_cmd.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"\
37 int
38 main (int argc, char *argv[])
40 struct gengetopt_args_info args_info;
41 char readbuf[BUFSIZ];
42 char *p, *r;
43 unsigned long *q;
44 int rc;
46 if (cmdline_parser(argc, argv, &args_info) != 0)
47 return 1;
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",
55 argv[0]);
56 cmdline_parser_print_help();
57 return 1;
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]);
72 if (!feof(stdin))
73 perror(readbuf);
74 return 1;
77 if (readbuf[strlen(readbuf)-1] == '\n')
78 readbuf[strlen(readbuf)-1] = '\0';
80 if (args_info.punycode_encode_given)
82 size_t len, len2;
84 p = stringprep_locale_to_utf8 (readbuf);
85 if (!p)
87 fprintf(stderr, "%s: could not convert from %s to UTF-8.\n",
88 argv[0], stringprep_locale_charset());
89 return 1;
92 q = stringprep_utf8_to_ucs4 (p, -1, &len);
93 free(p);
94 if (!q)
96 fprintf(stderr, "%s: could not convert from UTF-8 to UCS-4.\n",
97 argv[0]);
98 return 1;
101 if (args_info.debug_given)
103 size_t i;
104 for (i = 0; i < len; i++)
105 fprintf(stderr, "input[%d] = U+%04x\n", i, q[i]);
108 len2 = BUFSIZ;
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",
113 argv[0], rc);
114 return 1;
117 readbuf[len2] = '\0';
119 p = stringprep_utf8_to_locale (readbuf);
120 if (!p)
122 fprintf(stderr, "%s: could not convert from UTF-8 to %s.\n",
123 argv[0], stringprep_locale_charset());
124 return 1;
127 fprintf(stdout, "%s\n", p);
129 free(p);
132 if (args_info.punycode_decode_given)
134 size_t len, len2;
136 len = BUFSIZ;
137 q = (unsigned long*) malloc(len * sizeof(q[0]));
138 if (!q)
140 sprintf(readbuf, "%s: malloc() failed: ", argv[0]);
141 perror(readbuf);
142 return 1;
145 rc = punycode_decode (strlen(readbuf), readbuf, &len, q, NULL);
146 if (rc != PUNYCODE_SUCCESS)
148 free(q);
149 fprintf(stderr, "%s: punycode_decode() failed with error %d.\n",
150 argv[0], rc);
151 return 1;
154 if (args_info.debug_given)
156 size_t i;
157 for (i = 0; i < len; i++)
158 fprintf(stderr, "output[%d] = U+%04x\n", i, q[i]);
161 q[len] = 0;
162 p = stringprep_ucs4_to_utf8 (q, -1, NULL, NULL);
163 if (!p)
165 free(q);
166 fprintf(stderr, "%s: could not convert from UCS-4 to UTF-8.\n",
167 argv[0]);
168 return 1;
171 r = stringprep_utf8_to_locale (p);
172 free(p);
173 if (!r)
175 fprintf(stderr, "%s: could not convert from UTF-8 to %s.\n",
176 argv[0], stringprep_locale_charset());
177 return 1;
180 fprintf(stdout, "%s\n", r);
182 free(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);
194 return 1;
196 fprintf(stdout, "%s\n", p);
198 free(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);
210 return 1;
212 fprintf(stdout, "%s\n", p);
214 free(p);
218 while (!feof(stdin) && !ferror(stdin));
220 return 0;