+
+int utf8_encode (char *ptr, int ch)
+{
+
+ if (ch < 0x80)
+ {
+ ptr[0] = ch;
+ return 1;
+ }
+ else if (ch < 0x800)
+ {
+ ptr[0] = 0xc0 | (ch >> 6);
+ ptr[1] = 0x80 | (ch & 0x3f);
+ return 2;
+ }
+ else if (ch < 0x10000)
+ {
+ ptr[0] = 0xe0 | (ch >> 12);
+ ptr[1] = 0x80 | ((ch >> 6) & 0x3f);
+ ptr[2] = 0x80 | (ch & 0x3f);
+ return 3;
+ }
+ else if (ch < 0x1fffff)
+ {
+ ptr[0] = 0xf0 | (ch >> 18);
+ ptr[1] = 0x80 | ((ch >> 12) & 0x3f);
+ ptr[2] = 0x80 | ((ch >> 6) & 0x3f);
+ ptr[3] = 0x80 | (ch & 0x3f);
+ return 4;
+ }
+ return 0;
+}
+
+void
+utf8_emit (TTY * t, int ch)
+{
+ uint8_t buf[4];
+int i;
+ i=utf8_encode(buf,ch);
+ if (!i) return;
+
+ t->xmit (t, buf, i);
+}