1 /* Copyright (c) 2000, 2002-2004, 2007, 2008 MySQL AB
2    Use is subject to license terms
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
16  */
17 
18 /*
19   Written by Alexander Barkov to check what
20   a charset is in your favorite web browser
21 */
22 
23 #include <my_global.h>
24 #include <m_ctype.h>
25 #include <my_sys.h>
26 #include <mysql_version.h>
27 
28 #include <stdio.h>
29 
30 typedef struct char_info_st
31 {
32   int cod;
33   int srt;
34   int uni;
35   int low;
36   int upp;
37   int ctp;
38 } MY_CH;
39 
chcmp(const void * vf,const void * vs)40 static int chcmp(const void *vf, const void *vs)
41 {
42   const MY_CH *f=vf;
43   const MY_CH *s=vs;
44 
45   return f->srt-s->srt ? f->srt-s->srt : f->uni-s->uni;
46 }
47 
print_cs(CHARSET_INFO * cs)48 static void print_cs(CHARSET_INFO *cs)
49 {
50   uint  i;
51   int   srt;
52   int   clr=0;
53   MY_CH ch[256];
54 
55   printf("<HTML>\n");
56   printf("<HEAD>\n");
57   printf("</HEAD>\n");
58   printf("<BODY><PRE>\n");
59   printf("Charset %s\n",cs->name);
60 
61   printf("<TABLE>\n");
62   printf("<TR><TH>Code<TH>Uni<TH>Sort<TH>Ctype<TH>Ch<TH>Lo<TH>Up</TR>");
63 
64   for (i=0; i<256; i++)
65   {
66     ch[i].cod=i;
67     ch[i].srt=cs->sort_order ? cs->sort_order[i] : i;
68     ch[i].uni=cs->tab_to_uni[i];
69     ch[i].low=cs->tab_to_uni[cs->to_lower[i]];
70     ch[i].upp=cs->tab_to_uni[cs->to_upper[i]];
71     ch[i].ctp=cs->ctype[i+1];
72   }
73 
74   qsort(ch,256,sizeof(MY_CH),&chcmp);
75   srt=ch[0].srt;
76 
77   for (i=0; i<256; i++)
78   {
79     clr = (srt!=ch[i].srt) ? !clr : clr;
80 
81     printf("<TR bgcolor=#%s>",clr ? "DDDDDD" : "EEEE99");
82     printf("<TD>%02X",ch[i].cod);
83     printf("<TD>%04X",ch[i].uni);
84     printf("<TD>%02X",ch[i].srt);
85 
86     printf("<TD>%s%s%s%s%s%s%s%s",
87     		ch[i].ctp & _MY_U ? "U" : "",
88     		ch[i].ctp & _MY_L ? "L" : "",
89     		ch[i].ctp & _MY_NMR ? "N" : "",
90     		ch[i].ctp & _MY_SPC ? "S" : "",
91     		ch[i].ctp & _MY_PNT ? "P" : "",
92     		ch[i].ctp & _MY_CTR ? "C" : "",
93     		ch[i].ctp & _MY_B ? "B" : "",
94     		ch[i].ctp & _MY_X ? "X" : "");
95 
96     if ((ch[i].uni >= 0x80) && (ch[i].uni <= 0x9F))
97     {
98       /*
99        Control characters 0x0080..0x009F are dysplayed by some
100        browers as if they were letters. Don't print them to
101        avoid confusion.
102       */
103       printf("<TD>ctrl<TD>ctrl<TD>ctrl");
104     }
105     else
106     {
107       printf("<TD>&#%d;",ch[i].uni);
108       printf("<TD>&#%d;",ch[i].low);
109       printf("<TD>&#%d;",ch[i].upp);
110     }
111     printf("</TR>\n");
112     srt=ch[i].srt;
113   }
114   printf("</TABLE>\n");
115   printf("</PRE></BODY>\n");
116   printf("</HTML>\n");
117 }
118 
print_index()119 static void print_index()
120 {
121   CHARSET_INFO **cs;
122   int clr=0;
123 
124   get_charset_by_name("",MYF(0));	/* To execute init_available_charsets */
125 
126   printf("All charsets\n");
127   printf("<table border=1>\n");
128   printf("<tr bgcolor=EEEE99><th>ID<th>Charset<th>Collation<th>Def<th>Bin<th>Com<th>Comment\n");
129   for (cs=all_charsets ; cs < all_charsets+256; cs++)
130   {
131     if (!cs[0])
132       continue;
133     printf("<tr bgcolor=#%s><td><a href=\"?%s\">%d</a><td>%s<td>%s<td>%s<td>%s<td>%s<td>%s\n",
134     	   (clr= !clr) ? "DDDDDD" : "EEEE99",
135     	   cs[0]->name,cs[0]->number,cs[0]->csname,
136     	   cs[0]->name,
137     	   (cs[0]->state & MY_CS_PRIMARY)  ? "def " : "&nbsp;",
138     	   (cs[0]->state & MY_CS_BINSORT)  ? "bin " : "&nbsp;",
139     	   (cs[0]->state & MY_CS_COMPILED) ? "com " : "&nbsp;",
140     	   cs[0]->comment);
141   }
142   printf("</table>\n");
143 }
144 
main(int argc,char ** argv)145 int main(int argc, char **argv) {
146   const char *the_set = NULL;
147   int argcnt = 1;
148   CHARSET_INFO *cs;
149 
150   if (getenv("SCRIPT_NAME"))
151   {
152     printf("Content-Type: text/html\r\n\r\n");
153   }
154   my_init();
155 
156   if (argc > argcnt && argv[argcnt][0] == '-' && argv[argcnt][1] == '#')
157   {
158     DBUG_PUSH(argv[argcnt++]+2);
159   }
160 
161   if (argc > argcnt)
162     the_set = argv[argcnt++];
163 
164   if (argc > argcnt)
165     charsets_dir = argv[argcnt++];
166 
167   if (!the_set)
168   {
169     print_index();
170     return 0;
171   }
172 
173   if (!(cs= get_charset_by_name(the_set, MYF(MY_WME))))
174     return 1;
175 
176   print_cs(cs);
177 
178   return 0;
179 }
180