source code taken from cacert-20100805.tar.bz2
[cacert-devel.git] / locale / cv.c
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <stdlib.h>
4 #include <string.h>
5 typedef unsigned char uchar;
6 typedef struct{char * nm; int v;} vp;
7 vp vpl[] = {
8 {"nbsp", 160}, {"lt",0x3c}, {"amp", 38},
9 {"eacute", 233}, {"egrave", 232}, {"ouml", 246},
10 {"alpha", 0x3b1}, {"beta", 0x3b2}, {"gamma", 0x3b3},
11 {"delta", 0x3b4}, {"Delta", 0x394},
12 {"sigma", 0x3c3}, {"Sigma", 0x3a3},
13 {"epsilon", 0x3b5}, {"zeta", 0x3b6},
14 {"theta", 0x3b8}, {"mu", 0x3bc},
15 {"phi", 0x3c6},
16 {"omega", 0x3c9},
17 {"lambda", 0x3bb}, {"rho", 0x3c1},
18 {"pi", 0x3c0}, {"Pi", 0x3a0},
19 {"ndash", 0x2013}, {"mdash", 0x2014},
20 {"and", 8743}, {"rarr", 8594}, {"forall", 0x2200},
21 {"sum", 8721}};
22 int cc = 0; // count of conversions.
23 static void Utf(int m, uint a){
24 if (a & m) {Utf(m>>1, a>>6); putchar(128 | a & 63);}
25 else putchar((m<<1)&255 | a);}
26 static void utf8(uint a){
27 if(a == '<') printf("%s", "&lt;");
28 else if(a == '&') printf("%s", "&amp;");
29 else if(a & -128) {++cc;
30 Utf(-32, a>>6); putchar(128 | a & 63);} else putchar(a);}
31 char * em[] = {"", "tag", "quoted string", "utf", "character ref"};
32 int lc = 1, cil = 0, tcc=0;
33 char gc(int x){char c = getchar();
34 if(c == EOF && feof(stdin)) {
35 if(x) fprintf(stderr, "file ended in %s\n", em[x]);
36 fprintf(stderr, "Converted %d characters\n", cc);
37 exit(0);}
38 if(c == 10 || c == 13) {tcc += cil; cil = 0; ++lc;}
39 ++cil; return c;}
40 void loc(){fprintf(stderr, "Ending at byte %d of line %d,"
41 "(or 0x%x in file):\n", cil, lc, tcc+cil);}
42 char gx(){char c = gc(3); if ((c&0xc0) != 0x80)
43 {loc(); fprintf(stderr, "Bad utf8 extension byte: %02X\n", c);}
44 return c;}
45 int main(int argc, char * * args){
46 int bk = argc == 2;
47 while(1){
48 int vx(int x){if((x & 0xffffffe0) == 0x80){
49 if(x == 150) return 8211;
50 if(x == 151) return 8212;
51 loc(); fprintf(stderr, "Invalid character: 0x%x=%d\n", x, x);}
52 return x;}
53 uchar c = gc(0);
54 if(c == '<'){putchar(c); while(1){char c = gc(1);
55 if(c == '"'){putchar(c); while(1){char c = gc(2);
56 if(c == '"'){putchar(c); break;}
57 else putchar(c);}}
58 else if(c == '>'){putchar(c); break;}
59 else putchar(c);}}
60 else if(bk && c > 127){int v=0, sc=0, C=c;
61 while(C&0x40){C <<=1; v = (v<<6) | gx() & 0x3f; ++sc;}
62 {int uc = vx(v | (0x3f>>sc & (int)c) << 6*sc);
63 {int k = sizeof(vpl)/sizeof(vp);
64 while(k--) if(uc == vpl[k].v)
65 {printf("&%s;", vpl[k].nm); goto end;}}
66 printf("&#x%x;", uc);}
67 end: ++cc;}
68 else if(!bk && c == '&') {char c = gc(4);
69 int gs(char c, int r){
70 int vd(char c){if('0' <= c && c <= '9') return c - '0';
71 {char lc = tolower(c);
72 if(r == 16 && 'a' <= lc && lc <= 'f') return lc - 'a' + 10;
73 loc();
74 fprintf(stderr, "Invalid digit folowing \"&#\" construct.");
75 exit(0);
76 return 0;}}
77 int k = vd(c);
78 while(1){char c = gc(4); if(c == ';') return k;
79 k = r*k + vd(c);}}
80 if(c == '#') {char c = gc(4);
81 utf8(vx(c == 'x' || c == 'X' ? gs('0', 16) : gs(c, 10)));}
82 else {int k = sizeof(vpl)/sizeof(vp);
83 char st[10]; st[0] = c;
84 {int n; for(n=1; n<10; ++n) {char c = gc(4);
85 if(c == ';') goto e1;
86 if(!isalpha(c)) break;
87 st[n] = c;}
88 loc(); fprintf(stderr, "%s reference\n",
89 n>10?"Verbose":"Invalid");
90 continue;
91 e1: st[n] = 0;
92 // loc(); fprintf(stderr, "string is <%s>.\n", st);
93 while(k--) if(!strcmp(st, vpl[k].nm)) {
94 utf8(vpl[k].v); break;}
95 if(k<0) {loc();
96 fprintf(stderr, "Unrecognized reference: &%s;\n", st);}}}}
97 else if(c > 127) {loc(); fprintf(stderr, "Non ASCII char.\n");}
98 else putchar(c);
99 }
100 return 0;
101 }
102