Add command line interface to library.
[libidn.git] / idn.c
blobd6af99b0e5d1388ab1229a4cb8459fa8ec3e2d61
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 int
32 main (int argc, char *argv[])
34 struct gengetopt_args_info args_info;
35 char readbuf[BUFSIZ];
36 char *p, *r;
37 unsigned long *q;
38 int rc;
40 if (cmdline_parser(argc, argv, &args_info) != 0)
41 return 1;
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",
49 argv[0]);
50 return 1;
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 "
64 "by law.\n"
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]);
75 if (!feof(stdin))
76 perror(readbuf);
77 return 1;
80 if (readbuf[strlen(readbuf)-1] == '\n')
81 readbuf[strlen(readbuf)-1] = '\0';
83 if (args_info.punycode_encode_given)
85 size_t len, len2;
87 p = stringprep_locale_to_utf8 (readbuf);
88 if (!p)
90 fprintf(stderr, "%s: could not convert from %s to UTF-8.\n",
91 argv[0], stringprep_locale_charset());
92 return 1;
95 q = stringprep_utf8_to_ucs4 (p, -1, &len);
96 free(p);
97 if (!q)
99 fprintf(stderr, "%s: could not convert from UTF-8 to UCS-4.\n",
100 argv[0]);
101 return 1;
104 if (args_info.debug_given)
106 size_t i;
107 for (i = 0; i < len; i++)
108 fprintf(stderr, "input[%d] = U+%04x\n", i, q[i]);
111 len2 = BUFSIZ;
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",
116 argv[0], rc);
117 return 1;
120 readbuf[len2] = '\0';
122 p = stringprep_utf8_to_locale (readbuf);
123 if (!p)
125 fprintf(stderr, "%s: could not convert from UTF-8 to %s.\n",
126 argv[0], stringprep_locale_charset());
127 return 1;
130 fprintf(stdout, "%s\n", p);
132 free(p);
135 if (args_info.punycode_decode_given)
137 size_t len, len2;
139 len = BUFSIZ;
140 q = (unsigned long*) malloc(len * sizeof(q[0]));
141 if (!q)
143 sprintf(readbuf, "%s: malloc() failed: ", argv[0]);
144 perror(readbuf);
145 return 1;
148 rc = punycode_decode (strlen(readbuf), readbuf, &len, q, NULL);
149 if (rc != PUNYCODE_SUCCESS)
151 free(q);
152 fprintf(stderr, "%s: punycode_decode() failed with error %d.\n",
153 argv[0], rc);
154 return 1;
157 if (args_info.debug_given)
159 size_t i;
160 for (i = 0; i < len; i++)
161 fprintf(stderr, "output[%d] = U+%04x\n", i, q[i]);
164 q[len] = 0;
165 p = stringprep_ucs4_to_utf8 (q, -1, NULL, NULL);
166 if (!p)
168 free(q);
169 fprintf(stderr, "%s: could not convert from UCS-4 to UTF-8.\n",
170 argv[0]);
171 return 1;
174 r = stringprep_utf8_to_locale (p);
175 free(p);
176 if (!r)
178 fprintf(stderr, "%s: could not convert from UTF-8 to %s.\n",
179 argv[0], stringprep_locale_charset());
180 return 1;
183 fprintf(stdout, "%s\n", r);
185 free(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);
197 return 1;
199 fprintf(stdout, "%s\n", p);
201 free(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);
213 return 1;
215 fprintf(stdout, "%s\n", p);
217 free(p);
221 while (!feof(stdin) && !ferror(stdin));
223 return 0;