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