1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <locale.h>
#include <getopt.h>
#include <errno.h>
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif
#include <iconv.h>
#include "../hangul/hangul.h"
#include "../hangul/hangul-gettext.h"
#ifdef WORDS_BIGENDIAN
#define UCS4 "UCS-4BE"
#else
#define UCS4 "UCS-4LE"
#endif
static const char* program_name = "hangul";
static iconv_t cd_ucs4_to_utf8 = (iconv_t)-1;
static void
print_error(int status, int errnum, const char* format, ...)
{
va_list ap;
printf("%s: %s: ", program_name, strerror(errnum));
va_start(ap, format);
vprintf(format, ap);
va_end(ap);
fputc('\n', stdout);
}
static void
usage(int status)
{
if (status == EXIT_SUCCESS) {
printf(_("\
Usage: %s [OPTION]... [FILE]...\n\
"), program_name);
fputs(_("\
Convert string into korean characters according to korean keyboard layout.\n\
\n\
Mandatory arguments to long options are mandatory for short options too.\n\
-k, --keyboard=KEYBOARDID select keyboard layout by KEYBOARDID\n\
-l, --list list available keyboard and exit\n\
-i, --input=STRING use STRING as input instead of standard input\n\
-o, --output=FILE write result to FILE instead of standard output\n\
-s, --strict-order do not allow wrong input sequence\n\
"), stdout);
fputs(_("\
--help display this help and exit\n\
--version output version information and exit\n\
"), stdout);
fputs(_("\
\n\
With no FILE, or when FILE is -, read standard input.\n\
"), stdout);
printf(_("\
\n\
Examples:\n\
%s -i gksrmfdlqfur Convert specified string into korean characters\n\
and print them to standard output.\n\
%s Convert standard input into korean characters\n\
and print them to standard output.\n\
"),
program_name, program_name);
} else {
fprintf(stderr, _("Try `%s --help' for more information.\n"),
program_name);
}
exit(status);
}
static void
version()
{
printf("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
exit(EXIT_SUCCESS);
}
static void
list_keyboards()
{
unsigned i;
unsigned n;
n = hangul_ic_get_n_keyboards();
#if defined(ENABLE_NLS) && defined(HAVE_NL_LANGINFO)
if (n > 0) {
// hangul_ic_get_keyboard_name() 함수가 UTF-8 스트링으로 리턴하므로
// 여기서는 locale 인코딩으로 변환해야 한다.
// 그런데 iconv를 호출하여 변환하기가 번거로우므로
// bind_textdomain_codeset을 다시 호출하여 gettext가 리턴하는 스트링의
// 인코딩을 바꿔준다. 이렇게 하기 위해서는
// hangul_ic_get_keyboard_name()을 먼저 호출하여
// bind_textdomain_codeset() 함수가 불린후 다시 설정하도록 한다.
const char* codeset = nl_langinfo(CODESET);
if (codeset != NULL) {
hangul_ic_get_keyboard_name(0);
bind_textdomain_codeset(GETTEXT_PACKAGE, codeset);
}
}
#endif
for (i = 0; i < n; ++i) {
const char* id;
const char* name;
id = hangul_ic_get_keyboard_id(i);
name = hangul_ic_get_keyboard_name(i);
printf("%-12s %s\n", id, name);
}
exit(EXIT_SUCCESS);
}
static bool
on_hic_transition(HangulInputContext* ic,
ucschar c, const ucschar* preedit, void * data)
{
if (hangul_is_choseong(c)) {
if (hangul_ic_has_jungseong(ic) || hangul_ic_has_jongseong(ic))
return false;
} else if (hangul_is_jungseong(c)) {
if (hangul_ic_has_jongseong(ic))
return false;
}
return true;
}
size_t ucschar_strlen(const ucschar* str)
{
const ucschar* p = str;
while (p[0] != 0) {
p++;
}
return p - str;
}
static int
fputs_ucschar(const ucschar* str, FILE* stream)
{
char buf[512];
char* inbuf;
char* outbuf;
size_t inbytesleft;
size_t outbytesleft;
size_t len;
size_t res;
len = ucschar_strlen(str);
inbuf = (char*)str;
outbuf = buf;
inbytesleft = len * 4;
outbytesleft = sizeof(buf) - 1;
res = iconv(cd_ucs4_to_utf8, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
if (res == -1) {
if (errno == E2BIG) {
} else if (errno == EILSEQ) {
} else if (errno == EINVAL) {
}
}
*outbuf = '\0';
return fputs(buf, stream);
}
static void
hangul_process_with_string(HangulInputContext* ic, const char* input, FILE* output)
{
int r;
const ucschar* str;
while (*input != '\0') {
bool is_processed = hangul_ic_process(ic, *input);
str = hangul_ic_get_commit_string(ic);
if (str[0] != 0) {
r = fputs_ucschar(str, output);
if (r == EOF)
goto on_error;
}
if (!is_processed) {
r = fputc(*input, output);
if (r == EOF)
goto on_error;
}
input++;
}
str = hangul_ic_flush(ic);
if (str[0] != 0) {
r = fputs_ucschar(str, output);
if (r == EOF)
goto on_error;
}
r = fputs("\n", output);
if (r == EOF)
goto on_error;
return;
on_error:
print_error(0, errno, _("standard output"));
exit(EXIT_FAILURE);
}
static void
hangul_process(HangulInputContext* ic, FILE* input, FILE* output)
{
int r;
int c;
const ucschar* str;
c = fgetc(input);
while (c != EOF) {
bool res = hangul_ic_process(ic, c);
str = hangul_ic_get_commit_string(ic);
if (str[0] != 0) {
r = fputs_ucschar(str, output);
if (r == EOF)
goto on_error;
}
if (!res) {
r = fputc(c, output);
if (r == EOF)
goto on_error;
}
c = fgetc(input);
}
str = hangul_ic_flush(ic);
if (str[0] != 0) {
r = fputs_ucschar(str, output);
if (r == EOF)
goto on_error;
}
return;
on_error:
print_error(0, errno, _("standard output"));
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
int i;
int res;
const char* input_string;
const char* output_file;
const char* keyboard;
FILE* output;
HangulInputContext* ic;
bool strict_order = false;
#ifdef ENABLE_NLS
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
#endif
setlocale(LC_ALL, "");
res = EXIT_SUCCESS;
keyboard = "2";
input_string = NULL;
output_file = "-";
while (1) {
int c;
static struct option const long_options[] = {
{ "keyboard", required_argument, NULL, 'k' },
{ "list", no_argument, NULL, 'l' },
{ "input", required_argument, NULL, 'i' },
{ "output", required_argument, NULL, 'o' },
{ "strict-order",no_argument, NULL, 's' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{ NULL, 0, NULL, 0 }
};
c = getopt_long(argc, argv, "k:li:o:s", long_options, NULL);
if (c == -1)
break;
switch (c) {
case 'k':
keyboard = optarg;
break;
case 'l':
list_keyboards();
break;
case 'i':
input_string = optarg;
break;
case 'o':
output_file = optarg;
break;
case 's':
strict_order = true;
break;
case 'h':
usage(EXIT_SUCCESS);
break;
case 'v':
version();
break;
default:
usage(EXIT_FAILURE);
}
}
if (strcmp(output_file, "-") == 0) {
output = stdout;
} else {
output = fopen(output_file, "w");
if (output == NULL) {
print_error(EXIT_FAILURE, errno, "%s", output_file);
exit(EXIT_FAILURE);
}
}
cd_ucs4_to_utf8 = iconv_open("UTF-8", UCS4);
if (cd_ucs4_to_utf8 == (iconv_t)-1) {
print_error(EXIT_FAILURE, errno, _("conversion from %s to UTF-8"), UCS4);
exit(EXIT_FAILURE);
}
ic = hangul_ic_new(keyboard);
if (strict_order) {
hangul_ic_connect_callback(ic, "transition", on_hic_transition, NULL);
}
if (input_string != NULL) {
hangul_process_with_string(ic, input_string, output);
}
if (optind < argc) {
for (i = optind; i < argc; i++) {
FILE* input = NULL;
if (strcmp(argv[i], "-") == 0) {
input = stdin;
hangul_process(ic, input, output);
} else {
input = fopen(argv[i], "r");
if (input == NULL) {
print_error(0, errno, "%s", argv[i]);
} else {
hangul_process(ic, input, output);
fclose(input);
}
}
}
} else if (input_string == NULL) {
hangul_process(ic, stdin, output);
}
hangul_ic_delete(ic);
iconv_close(cd_ucs4_to_utf8);
if (strcmp(output_file, "-") != 0) {
fclose(output);
}
return res;
}
|