1709795f |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <ctype.h> |
4 | |
5 | #include <time.h> |
6 | #include "putty.h" |
887035a5 |
7 | #include "terminal.h" |
1709795f |
8 | #include "misc.h" |
9 | |
10 | /* |
11 | * Unix Unicode-handling routines. |
12 | * |
13 | * FIXME: currently trivial stub versions assuming all codepages |
14 | * are ISO8859-1. |
15 | */ |
16 | |
17 | void lpage_send(int codepage, char *buf, int len, int interactive) |
18 | { |
19 | ldisc_send(buf, len, interactive); |
20 | } |
21 | |
22 | void luni_send(wchar_t * widebuf, int len, int interactive) |
23 | { |
24 | static char *linebuffer = 0; |
25 | static int linesize = 0; |
887035a5 |
26 | int ratio = (in_utf(term))?6:1; |
1709795f |
27 | int i; |
28 | char *p; |
29 | |
30 | if (len * ratio > linesize) { |
31 | sfree(linebuffer); |
32 | linebuffer = smalloc(len * ratio * 2 * sizeof(wchar_t)); |
33 | linesize = len * ratio * 2; |
34 | } |
35 | |
887035a5 |
36 | if (in_utf(term)) { |
1709795f |
37 | /* UTF is a simple algorithm */ |
38 | for (p = linebuffer, i = 0; i < len; i++) { |
39 | wchar_t ch = widebuf[i]; |
40 | |
41 | if ((ch&0xF800) == 0xD800) ch = '.'; |
42 | |
43 | if (ch < 0x80) { |
44 | *p++ = (char) (ch); |
45 | } else if (ch < 0x800) { |
46 | *p++ = (0xC0 | (ch >> 6)); |
47 | *p++ = (0x80 | (ch & 0x3F)); |
48 | } else if (ch < 0x10000) { |
49 | *p++ = (0xE0 | (ch >> 12)); |
50 | *p++ = (0x80 | ((ch >> 6) & 0x3F)); |
51 | *p++ = (0x80 | (ch & 0x3F)); |
52 | } else if (ch < 0x200000) { |
53 | *p++ = (0xF0 | (ch >> 18)); |
54 | *p++ = (0x80 | ((ch >> 12) & 0x3F)); |
55 | *p++ = (0x80 | ((ch >> 6) & 0x3F)); |
56 | *p++ = (0x80 | (ch & 0x3F)); |
57 | } else if (ch < 0x4000000) { |
58 | *p++ = (0xF8 | (ch >> 24)); |
59 | *p++ = (0x80 | ((ch >> 18) & 0x3F)); |
60 | *p++ = (0x80 | ((ch >> 12) & 0x3F)); |
61 | *p++ = (0x80 | ((ch >> 6) & 0x3F)); |
62 | *p++ = (0x80 | (ch & 0x3F)); |
63 | } else { |
64 | *p++ = (0xFC | (ch >> 30)); |
65 | *p++ = (0x80 | ((ch >> 24) & 0x3F)); |
66 | *p++ = (0x80 | ((ch >> 18) & 0x3F)); |
67 | *p++ = (0x80 | ((ch >> 12) & 0x3F)); |
68 | *p++ = (0x80 | ((ch >> 6) & 0x3F)); |
69 | *p++ = (0x80 | (ch & 0x3F)); |
70 | } |
71 | } |
72 | } else { |
73 | for (p = linebuffer, i = 0; i < len; i++) { |
74 | wchar_t ch = widebuf[i]; |
75 | if (ch < 0x100) |
76 | *p++ = (char) ch; |
77 | else |
78 | *p++ = '.'; |
79 | } |
80 | } |
81 | if (p > linebuffer) |
82 | ldisc_send(linebuffer, p - linebuffer, interactive); |
83 | } |
84 | |
85 | int is_dbcs_leadbyte(int codepage, char byte) |
86 | { |
87 | return 0; /* we don't do DBCS */ |
88 | } |
89 | |
90 | int mb_to_wc(int codepage, int flags, char *mbstr, int mblen, |
91 | wchar_t *wcstr, int wclen) |
92 | { |
93 | int ret = 0; |
94 | while (mblen > 0 && wclen > 0) { |
95 | *wcstr++ = (unsigned char) *mbstr++; |
e6346999 |
96 | mblen--, wclen--, ret++; |
97 | } |
98 | return ret; /* FIXME: check error codes! */ |
99 | } |
100 | |
101 | int wc_to_mb(int codepage, int flags, wchar_t *wcstr, int wclen, |
102 | char *mbstr, int mblen, char *defchr, int *defused) |
103 | { |
104 | int ret = 0; |
105 | if (defused) |
106 | *defused = 0; |
107 | while (mblen > 0 && wclen > 0) { |
108 | if (*wcstr >= 0x100) { |
109 | if (defchr) |
110 | *mbstr++ = *defchr; |
111 | else |
112 | *mbstr++ = '\xBF'; |
113 | if (defused) |
114 | *defused = 1; |
115 | } else |
116 | *mbstr++ = (unsigned char) *wcstr; |
117 | wcstr++; |
118 | mblen--, wclen--, ret++; |
1709795f |
119 | } |
120 | return ret; /* FIXME: check error codes! */ |
121 | } |
122 | |
123 | void init_ucs(void) |
124 | { |
125 | int i; |
126 | /* Find the line control characters. FIXME: this is not right. */ |
127 | for (i = 0; i < 256; i++) |
128 | if (i < ' ' || (i >= 0x7F && i < 0xA0)) |
129 | unitab_ctrl[i] = i; |
130 | else |
131 | unitab_ctrl[i] = 0xFF; |
132 | |
133 | for (i = 0; i < 256; i++) { |
134 | unitab_line[i] = unitab_scoacs[i] = i; |
ec684ed4 |
135 | unitab_xterm[i] = (i >= 0x5F && i < 0x7F) ? ((i+1) & 0x1F) : i; |
1709795f |
136 | } |
126ce234 |
137 | } |