]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/tcs/tune.c
dc: fix off by one in stack overflow check (thanks BurnZeZ)
[plan9front.git] / sys / src / cmd / tcs / tune.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include "hdr.h"
5 #include "conv.h"
6
7 typedef struct Tmap Tmap;
8 struct Tmap
9 {
10         Rune u;
11         Rune t;
12 };
13
14 static Tmap t1[] =
15 {
16         {0x0b85/*அ*/, 0xe201/**/},
17         {0x0b86/*ஆ*/, 0xe202/**/},
18         {0x0b87/*இ*/, 0xe203/**/},
19         {0x0b88/*ஈ*/, 0xe204/**/},
20         {0x0b89/*உ*/, 0xe205/**/},
21         {0x0b8a/*ஊ*/, 0xe206/**/},
22         {0x0b8e/*எ*/, 0xe207/**/},
23         {0x0b8f/*ஏ*/, 0xe208/**/},
24         {0x0b90/*ஐ*/, 0xe209/**/},
25         {0x0b92/*ஒ*/, 0xe20a/**/},
26         {0x0b93/*ஓ*/, 0xe20b/**/},
27         {0x0b94/*ஔ*/, 0xe20c/**/},
28         {0x0b83/*ஃ*/, 0xe20d/**/}
29 };
30
31 static Rune t2[] =
32 {
33         0x0bcd/*்*/, 
34         0x0bcd/*்*/,  // filler
35         0x0bbe/*ா*/,
36         0x0bbf/*ி*/,
37         0x0bc0/*ீ*/,
38         0x0bc1/*ு*/,
39         0x0bc2/*ூ*/,
40         0x0bc6/*ெ*/,
41         0x0bc7/*ே*/,
42         0x0bc8/*ை*/,
43         0x0bca/*ொ*/,
44         0x0bcb/*ோ*/,
45         0x0bcc/*ௌ*/
46 };
47
48 static Tmap t3[] =
49 {
50         {0x0b95/*க*/, 0xe211/**/},
51         {0x0b99/*ங*/, 0xe221/**/},
52         {0x0b9a/*ச*/, 0xe231/**/},
53         {0x0b9c/*ஜ*/, 0xe331/**/},
54         {0x0b9e/*ஞ*/, 0xe241/**/},
55         {0x0b9f/*ட*/, 0xe251/**/},
56         {0x0ba3/*ண*/, 0xe261/**/},
57         {0x0ba4/*த*/, 0xe271/**/},
58         {0x0ba8/*ந*/, 0xe281/**/},
59         {0x0ba9/*ன*/, 0xe321/**/},
60         {0x0baa/*ப*/, 0xe291/**/},
61         {0x0bae/*ம*/, 0xe2a1/**/},
62         {0x0baf/*ய*/, 0xe2b1/**/},
63         {0x0bb0/*ர*/, 0xe2c1/**/},
64         {0x0bb1/*ற*/, 0xe311/**/},
65         {0x0bb2/*ல*/, 0xe2d1/**/},
66         {0x0bb3/*ள*/, 0xe301/**/},
67         {0x0bb4/*ழ*/, 0xe2f1/**/},
68         {0x0bb5/*வ*/, 0xe2e1/**/},
69         {0x0bb6/*ஶ*/, 0xe341/**/},
70         {0x0bb7/*ஷ*/, 0xe351/**/},
71         {0x0bb8/*ஸ*/, 0xe361/**/},
72         {0x0bb9/*ஹ*/, 0xe371/**/}
73 };
74
75 static Rune
76 findbytune(Tmap *tab, int size, Rune t)
77 {
78         int i;
79
80         for(i = 0; i < size; i++)
81                 if(tab[i].t == t)
82                         return tab[i].u;
83         return Runeerror;
84 }
85
86 static Rune
87 findbyuni(Tmap *tab, int size, Rune u)
88 {
89         int i;
90
91         for(i = 0; i < size; i++)
92                 if(tab[i].u == u)
93                         return tab[i].t;
94         return Runeerror;
95 }
96
97 static int
98 findindex(Rune *rstr, int size, Rune r)
99 {
100         int i;
101
102         for(i = 0; i < size; i++)
103                 if(rstr[i] == r)
104                         return i;
105         return -1;
106 }
107
108 void
109 tune_in(int fd, long *, struct convert *out)
110 {
111         Biobuf b;
112         Rune *r, *er, tr;
113         int c, i;
114         
115         r = runes;
116         er = runes+N-3;
117         Binit(&b, fd, OREAD);
118         while((c = Bgetrune(&b)) != Beof){
119                 ninput += b.runesize;
120                 if(r >= er){
121                         OUT(out, runes, r-runes);
122                         r = runes;
123                 }
124                 if(c>=0xe210/**/ && c <= 0xe38c/**/ && (i = c%16) < nelem(t2)){
125                         if(c >= 0xe380/**/){
126                                 *r++ = 0x0b95/*க*/;
127                                 *r++ = 0x0bcd/*்*/;
128                                 *r++ = 0x0bb7/*ஷ*/;
129                         }else
130                                 *r++ = findbytune(t3, nelem(t3), c-i+1);
131                         if(i != 1)
132                                 *r++ = t2[i];
133                 }else if((tr = findbytune(t1, nelem(t1), c)) != Runeerror)
134                         *r++ = tr;
135                 else switch(c){
136                         case 0xe3d0/**/:
137                                 *r++ = 0x0ba3/*ண*/; *r++ = 0x0bbe/*ா*/;
138                                 break;
139                         case 0xe3d1/**/:
140                                 *r++ = 0x0bb1/*ற*/; *r++ = 0x0bbe/*ா*/;
141                                 break;
142                         case 0xe3d2/**/:
143                                 *r++ = 0x0ba9/*ன*/; *r++ = 0x0bbe/*ா*/;
144                                 break;
145                         case 0xe3d4/**/:
146                                 *r++ = 0x0ba3/*ண*/; *r++ = 0x0bc8/*ை*/;
147                                 break;
148                         case 0xe3d5/**/:
149                                 *r++ = 0x0bb2/*ல*/; *r++ = 0x0bc8/*ை*/;
150                                 break;
151                         case 0xe3d6/**/:
152                                 *r++ = 0x0bb3/*ள*/; *r++ = 0x0bc8/*ை*/;
153                                 break;
154                         case 0xe3d7/**/:
155                                 *r++ = 0x0ba9/*ன*/; *r++ = 0x0bc8/*ை*/;
156                                 break;
157                         case 0xe38d/**/:
158                                 *r++ = 0x0bb6/*ஶ*/; *r++ = 0x0bcd/*்*/; *r++ = 0x0bb0/*ர*/; *r++ = 0x0bc0/*ீ*/;
159                                 break;
160                         default: 
161                                 if(c >= 0xe200 && c <= 0xe3ff){
162                                         if(squawk)
163                                                 EPR "%s: rune 0x%x not in output cs\n", argv0, c);
164                                         nerrors++;
165                                         if(clean)
166                                                 break;
167                                         c = BADMAP;
168                                 }
169                                 *r++ = c;
170                                 break;
171                 }
172         }
173         if(r > runes)
174                 OUT(out, runes, r-runes);
175         OUT(out, runes, 0);
176 }
177
178 void
179 tune_out(Rune *r, int n, long *)
180 {
181         static int state = 0;
182         static Rune lastr;
183         Rune *er, tr, rr;
184         char *p;
185         int i;
186
187         nrunes += n;
188         er = r+n;
189         for(p = obuf; r < er; r++){
190                 switch(state){
191                 case 0:
192                 case0:
193                         if((tr = findbyuni(t3, nelem(t3), *r)) != Runeerror){
194                                 lastr = tr;
195                                 state = 1;
196                         }else if(*r == 0x0b92/*ஒ*/){
197                                 lastr = 0xe20a/**/;
198                                 state = 3;
199                         }else if((tr = findbyuni(t1, nelem(t1), *r)) != Runeerror)
200                                 p += runetochar(p, &tr);
201                         else
202                                 p += runetochar(p, r);
203                         break;
204                 case 1:
205                 case1:
206                         if((i = findindex(t2, nelem(t2), *r)) != -1){
207                                 if(lastr && lastr != Runeerror)
208                                         lastr += i-1;
209                                 if(*r ==0x0bc6/*ெ*/)
210                                         state = 5;
211                                 else if(*r ==0x0bc7/*ே*/)
212                                         state = 4;
213                                 else if(lastr == 0xe210/**/)
214                                         state = 2;
215                                 else if(lastr == 0xe340/**/)
216                                         state = 6;
217                                 else{
218                                         if(lastr)
219                                                 p += runetochar(p, &lastr);
220                                         state = 0;
221                                 }
222                         }else if(lastr && lastr != Runeerror && (*r == 0x00b2/*²*/ || *r == 0x00b3/*³*/ || *r == 0x2074/*⁴*/)){
223                                 if(squawk)
224                                         EPR "%s: character <U+%.4X, U+%.4X> not in output cs\n", argv0, lastr, *r);
225                                 lastr = clean ? 0 : Runeerror;
226                                 nerrors++;
227                         }else{
228                                 if(lastr)
229                                         p += runetochar(p, &lastr);
230                                 state = 0;
231                                 goto case0;
232                         }
233                         break;
234                 case 2:
235                         if(*r == 0x0bb7/*ஷ*/){
236                                 lastr = 0xe381/**/;
237                                 state = 1;
238                                 break;
239                         }
240                         p += runetochar(p, &lastr);
241                         state = 0;
242                         goto case0;
243                 case 3:
244                         state = 0;
245                         if(*r == 0x0bd7/*ௗ*/){
246                                 rr = 0xe20c/**/;
247                                 p += runetochar(p, &rr);
248                                 break;
249                         }
250                         p += runetochar(p, &lastr);
251                         goto case0;
252                 case 4:
253                         state = 0;
254                         if(*r == 0x0bbe/*ா*/){
255                                 if(lastr){
256                                         if(lastr != Runeerror)
257                                                 lastr += 3;
258                                         p += runetochar(p, &lastr);
259                                 }
260                                 break;
261                         }
262                         if(lastr)
263                                 p += runetochar(p, &lastr);
264                         goto case0;
265                 case 5:
266                         state = 0;
267                         if(*r == 0x0bbe/*ா*/ || *r == 0x0bd7/*ௗ*/){
268                                 if(lastr){
269                                         if(lastr != Runeerror)
270                                                 lastr += *r == 0x0bbe/*ா*/ ? 3 : 5;
271                                         p += runetochar(p, &lastr);
272                                 }
273                                 break;
274                         }
275                         if(lastr)
276                                 p += runetochar(p, &lastr);
277                         goto case0;
278                 case 6:
279                         if(*r == 0x0bb0/*ர*/){
280                                 state = 7;
281                                 break;
282                         }
283                         p += runetochar(p, &lastr);
284                         state = 0;
285                         goto case0;
286                 case 7:
287                         if(*r == 0x0bc0/*ீ*/){
288                                 rr = 0xe38d/**/;
289                                 p += runetochar(p, &rr);
290                                 state = 0;
291                                 break;
292                         }
293                         p += runetochar(p, &lastr);
294                         lastr = 0xe2c1/**/;
295                         state = 1;
296                         goto case1;
297                 }
298         }
299         if(n == 0 && state != 0){
300                 if(lastr)
301                         p += runetochar(p, &lastr);
302                 state = 0;
303         }
304         noutput += p-obuf;
305         write(1, obuf, p-obuf);
306 }