1 a31db67d 2004-04-21 devnull #ifdef PLAN9
2 a31db67d 2004-04-21 devnull #include <u.h>
3 a31db67d 2004-04-21 devnull #include <libc.h>
4 a31db67d 2004-04-21 devnull #include <bio.h>
6 a31db67d 2004-04-21 devnull #include <stdio.h>
7 a31db67d 2004-04-21 devnull #include <unistd.h>
8 a31db67d 2004-04-21 devnull #include "plan9.h"
10 a31db67d 2004-04-21 devnull #include "hdr.h"
11 a31db67d 2004-04-21 devnull #include "conv.h"
12 a31db67d 2004-04-21 devnull #include "big5.h"
15 a31db67d 2004-04-21 devnull a state machine for interpreting big5 (hk format).
18 a31db67d 2004-04-21 devnull big5proc(int c, Rune **r, long input_loc)
20 a31db67d 2004-04-21 devnull static enum { state0, state1 } state = state0;
21 a31db67d 2004-04-21 devnull static int lastc;
22 a31db67d 2004-04-21 devnull long n, ch, f, cold = c;
24 a31db67d 2004-04-21 devnull switch(state)
26 a31db67d 2004-04-21 devnull case state0: /* idle state */
27 a31db67d 2004-04-21 devnull if(c < 0)
29 a31db67d 2004-04-21 devnull if(c >= 0xA1){
30 a31db67d 2004-04-21 devnull lastc = c;
31 a31db67d 2004-04-21 devnull state = state1;
34 a31db67d 2004-04-21 devnull if(c == 26)
35 a31db67d 2004-04-21 devnull c = '\n';
39 a31db67d 2004-04-21 devnull case state1: /* seen a font spec */
40 a31db67d 2004-04-21 devnull if(c >= 64 && c <= 126)
42 a31db67d 2004-04-21 devnull else if(c >= 161 && c <= 254)
43 a31db67d 2004-04-21 devnull c = c-161 + 63;
45 a31db67d 2004-04-21 devnull nerrors++;
46 a31db67d 2004-04-21 devnull if(squawk)
47 a31db67d 2004-04-21 devnull EPR "%s: bad big5 glyph (from 0x%x,0x%lx) near byte %ld in %s\n",
48 a31db67d 2004-04-21 devnull argv0, lastc, cold, input_loc, file);
49 a31db67d 2004-04-21 devnull if(!clean)
50 a31db67d 2004-04-21 devnull emit(BADMAP);
51 a31db67d 2004-04-21 devnull state = state0;
54 a31db67d 2004-04-21 devnull if(lastc >= 161 && lastc <= 254)
55 a31db67d 2004-04-21 devnull f = lastc - 161;
57 a31db67d 2004-04-21 devnull nerrors++;
58 a31db67d 2004-04-21 devnull if(squawk)
59 a31db67d 2004-04-21 devnull EPR "%s: bad big5 font %d (from 0x%x,0x%lx) near byte %ld in %s\n",
60 a31db67d 2004-04-21 devnull argv0, lastc-161, lastc, cold, input_loc, file);
61 a31db67d 2004-04-21 devnull if(!clean)
62 a31db67d 2004-04-21 devnull emit(BADMAP);
63 a31db67d 2004-04-21 devnull state = state0;
66 a31db67d 2004-04-21 devnull n = f*BIG5FONT + c;
67 a31db67d 2004-04-21 devnull if(n < BIG5MAX)
68 a31db67d 2004-04-21 devnull ch = tabbig5[n];
71 a31db67d 2004-04-21 devnull if(ch < 0){
72 a31db67d 2004-04-21 devnull nerrors++;
73 a31db67d 2004-04-21 devnull if(squawk)
74 a31db67d 2004-04-21 devnull EPR "%s: unknown big5 %ld (from 0x%x,0x%lx) near byte %ld in %s\n",
75 a31db67d 2004-04-21 devnull argv0, n, lastc, cold, input_loc, file);
76 a31db67d 2004-04-21 devnull if(!clean)
77 a31db67d 2004-04-21 devnull emit(BADMAP);
79 a31db67d 2004-04-21 devnull emit(ch);
80 a31db67d 2004-04-21 devnull state = state0;
85 a31db67d 2004-04-21 devnull big5_in(int fd, long *notused, struct convert *out)
87 a31db67d 2004-04-21 devnull Rune ob[N];
88 a31db67d 2004-04-21 devnull Rune *r, *re;
89 a31db67d 2004-04-21 devnull uchar ibuf[N];
90 a31db67d 2004-04-21 devnull int n, i;
91 a31db67d 2004-04-21 devnull long nin;
93 a31db67d 2004-04-21 devnull USED(notused);
95 a31db67d 2004-04-21 devnull re = ob+N-3;
97 a31db67d 2004-04-21 devnull while((n = read(fd, ibuf, sizeof ibuf)) > 0){
98 a31db67d 2004-04-21 devnull for(i = 0; i < n; i++){
99 a31db67d 2004-04-21 devnull big5proc(ibuf[i], &r, nin++);
100 a31db67d 2004-04-21 devnull if(r >= re){
101 a31db67d 2004-04-21 devnull OUT(out, ob, r-ob);
105 a31db67d 2004-04-21 devnull if(r > ob){
106 a31db67d 2004-04-21 devnull OUT(out, ob, r-ob);
110 a31db67d 2004-04-21 devnull big5proc(-1, &r, nin);
111 a31db67d 2004-04-21 devnull if(r > ob)
112 a31db67d 2004-04-21 devnull OUT(out, ob, r-ob);
113 536f9b83 2006-05-21 devnull OUT(out, ob, 0);
117 a31db67d 2004-04-21 devnull big5_out(Rune *base, int n, long *notused)
119 a31db67d 2004-04-21 devnull char *p;
122 a31db67d 2004-04-21 devnull static int first = 1;
124 a31db67d 2004-04-21 devnull USED(notused);
125 a31db67d 2004-04-21 devnull if(first){
126 a31db67d 2004-04-21 devnull first = 0;
127 a31db67d 2004-04-21 devnull for(i = 0; i < NRUNE; i++)
128 a31db67d 2004-04-21 devnull tab[i] = -1;
129 a31db67d 2004-04-21 devnull for(i = 0; i < BIG5MAX; i++)
130 a31db67d 2004-04-21 devnull if(tabbig5[i] != -1)
131 a31db67d 2004-04-21 devnull tab[tabbig5[i]] = i;
133 a31db67d 2004-04-21 devnull nrunes += n;
134 a31db67d 2004-04-21 devnull p = obuf;
135 a31db67d 2004-04-21 devnull for(i = 0; i < n; i++){
136 a31db67d 2004-04-21 devnull r = base[i];
137 a31db67d 2004-04-21 devnull if(r < 128)
138 a31db67d 2004-04-21 devnull *p++ = r;
140 a31db67d 2004-04-21 devnull if(tab[r] != -1){
141 a31db67d 2004-04-21 devnull r = tab[r];
142 a31db67d 2004-04-21 devnull if(r >= BIG5MAX){
143 cbeb0b26 2006-04-01 devnull *p++ = (char)0xA1;
144 a31db67d 2004-04-21 devnull *p++ = r-BIG5MAX;
145 a31db67d 2004-04-21 devnull continue;
146 a31db67d 2004-04-21 devnull } else {
147 a31db67d 2004-04-21 devnull *p++ = 0xA1 + (r/BIG5FONT);
148 a31db67d 2004-04-21 devnull r = r%BIG5FONT;
149 a31db67d 2004-04-21 devnull if(r <= 62) r += 64;
150 a31db67d 2004-04-21 devnull else r += 0xA1-63;
151 a31db67d 2004-04-21 devnull *p++ = r;
152 a31db67d 2004-04-21 devnull continue;
155 a31db67d 2004-04-21 devnull if(squawk)
156 a31db67d 2004-04-21 devnull EPR "%s: rune 0x%x not in output cs\n", argv0, r);
157 a31db67d 2004-04-21 devnull nerrors++;
158 a31db67d 2004-04-21 devnull if(clean)
159 a31db67d 2004-04-21 devnull continue;
160 a31db67d 2004-04-21 devnull *p++ = BYTEBADMAP;
163 a31db67d 2004-04-21 devnull noutput += p-obuf;
164 a31db67d 2004-04-21 devnull if(p > obuf)
165 a31db67d 2004-04-21 devnull write(1, obuf, p-obuf);