]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/tcs/conv_big5.c
ip/ipconfig: use ewrite() to enable routing command for sendra
[plan9front.git] / sys / src / cmd / tcs / conv_big5.c
1 #include        <u.h>
2 #include        <libc.h>
3 #include        <bio.h>
4 #include        "hdr.h"
5 #include        "conv.h"
6 #include        "big5.h"
7
8 /*
9         a state machine for interpreting big5 (hk format).
10 */
11 void
12 big5proc(int c, Rune **r, long input_loc)
13 {
14         static enum { state0, state1 } state = state0;
15         static int lastc;
16         long n, ch, f, cold = c;
17
18         switch(state)
19         {
20         case state0:    /* idle state */
21                 if(c < 0)
22                         return;
23                 if(c >= 0xA1){
24                         lastc = c;
25                         state = state1;
26                         return;
27                 }
28                 if(c == 26)
29                         c = '\n';
30                 emit(c);
31                 return;
32
33         case state1:    /* seen a font spec */
34                 if(c >= 64 && c <= 126)
35                         c -= 64;
36                 else if(c >= 161 && c <= 254)
37                         c = c-161 + 63;
38                 else {
39                         nerrors++;
40                         if(squawk)
41                                 warn("bad big5 glyph (from 0x%x,0x%lx) near byte %ld in %s",
42                                         lastc, cold, input_loc, file);
43                         if(!clean)
44                                 emit(BADMAP);
45                         state = state0;
46                         return;
47                 }
48                 if(lastc >= 161 && lastc <= 254)
49                         f = lastc - 161;
50                 else {
51                         nerrors++;
52                         if(squawk)
53                                 warn("bad big5 font %d (from 0x%x,0x%lx) near byte %ld in %s",
54                                         lastc-161, lastc, cold, input_loc, file);
55                         if(!clean)
56                                 emit(BADMAP);
57                         state = state0;
58                         return;
59                 }
60                 n = f*BIG5FONT + c;
61                 if(n < BIG5MAX)
62                         ch = tabbig5[n];
63                 else
64                         ch = -1;
65                 if(ch < 0){
66                         nerrors++;
67                         if(squawk)
68                                 warn("unknown big5 %ld (from 0x%x,0x%lx) near byte %ld in %s",
69                                         n, lastc, cold, input_loc, file);
70                         if(!clean)
71                                 emit(BADMAP);
72                 } else
73                         emit(ch);
74                 state = state0;
75         }
76 }
77
78 void
79 big5_in(int fd, long *, struct convert *out)
80 {
81         Rune ob[N];
82         Rune *r, *re;
83         uchar ibuf[N];
84         int n, i;
85         long nin;
86
87         r = ob;
88         re = ob+N-3;
89         nin = 0;
90         while((n = read(fd, ibuf, sizeof ibuf)) > 0){
91                 for(i = 0; i < n; i++){
92                         big5proc(ibuf[i], &r, nin++);
93                         if(r >= re){
94                                 OUT(out, ob, r-ob);
95                                 r = ob;
96                         }
97                 }
98                 if(r > ob){
99                         OUT(out, ob, r-ob);
100                         r = ob;
101                 }
102         }
103         big5proc(-1, &r, nin);
104         if(r > ob)
105                 OUT(out, ob, r-ob);
106         OUT(out, ob, 0);
107 }
108
109 void
110 big5_out(Rune *base, int n, long *)
111 {
112         char *p;
113         int i;
114         Rune r;
115         static int first = 1;
116
117         if(first){
118                 first = 0;
119                 for(i = 0; i < NRUNE; i++)
120                         tab[i] = -1;
121                 for(i = 0; i < BIG5MAX; i++)
122                         if(tabbig5[i] != -1)
123                                 tab[tabbig5[i]] = i;
124         }
125         nrunes += n;
126         p = obuf;
127         for(i = 0; i < n; i++){
128                 r = base[i];
129                 if(r < 128)
130                         *p++ = r;
131                 else {
132                         if(r < NRUNE && tab[r] != -1){
133                                 r = tab[r];
134                                 if(r >= BIG5MAX){
135                                         *p++ = 0xA1;
136                                         *p++ = r-BIG5MAX;
137                                         continue;
138                                 } else {
139                                         *p++ = 0xA1 + (r/BIG5FONT);
140                                         r = r%BIG5FONT;
141                                         if(r <= 62) r += 64;
142                                         else r += 0xA1-63;
143                                         *p++ = r;
144                                         continue;
145                                 }
146                         }
147                         if(squawk)
148                                 warn("rune 0x%x not in output cs", r);
149                         nerrors++;
150                         if(clean)
151                                 continue;
152                         *p++ = BYTEBADMAP;
153                 }
154         }
155         noutput += p-obuf;
156         if(p > obuf)
157                 write(1, obuf, p-obuf);
158 }