1 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23 /* Prints case-convert and sort-convert tabell on stdout. This is used to
24 make _ctype.c easyer */
25
26 #ifdef NDEBUG
27 #undef NDEBUG
28 #endif
29
30 #include <my_global.h>
31 #include <ctype.h>
32 #include <my_sys.h>
33 #include "m_string.h"
34
35 uchar to_upper[256];
36 uchar to_lower[256], sort_order[256];
37
38 static int ascii_output=1;
39 static string tab_names[]={ "to_lower[]={","to_upper[]={","sort_order[]={" };
40 static uchar* tabell[]= {to_lower,to_upper,sort_order};
41
42 void get_options(),init_case_convert();
43
main(argc,argv)44 main(argc,argv)
45 int argc;
46 char *argv[];
47 {
48 int i,j,ch;
49 DBUG_ENTER ("main");
50 DBUG_PROCESS (argv[0]);
51
52 get_options(&argc,&argv);
53 init_case_convert();
54 puts("Tabells for caseconverts and sorttest of characters\n");
55 for (i=0 ; i < 3 ; i++)
56 {
57 printf("uchar %s\n",tab_names[i]);
58 for (j=0 ; j <= 255 ; j++)
59 {
60 ch=(int) tabell[i][j];
61 if (ascii_output && isprint(ch) && ! (ch & 128))
62 {
63 if (strchr("\\'",(char) ch))
64 printf("'\\%c', ",ch);
65 else
66 printf("'%c', ",ch);
67 }
68 else
69 printf("'\\%03o',",ch);
70 if ((j+1 & 7) == 0)
71 puts("");
72 }
73 puts("};\n");
74 }
75 DBUG_RETURN(0);
76 } /* main */
77
78 /* Read options */
79
get_options(argc,argv)80 void get_options(argc,argv)
81 int *argc;
82 char **argv[];
83 {
84 int help,version;
85 char *pos,*progname;
86
87 progname= (*argv)[0];
88 help=0; ascii_output=1;
89 while (--*argc >0 && *(pos = *(++*argv)) == '-' )
90 {
91 while (*++pos)
92 {
93 version=0;
94 switch(*pos) {
95 case 'n': /* Numeric output */
96 ascii_output=0;
97 break;
98 case '#':
99 DBUG_PUSH (++pos);
100 *(pos--) = '\0'; /* Skippa argument */
101 break;
102 case 'V':
103 version=1;
104 case 'I':
105 case '?':
106 printf("%s Ver 1.0\n",progname);
107 if (version)
108 break;
109 puts("Output tabells of to_lower[], to_upper[] and sortorder[]\n");
110 printf("Usage: %s [-n?I]\n",progname);
111 puts("Options: -? or -I \"Info\" -n \"numeric output\"");
112 break;
113 default:
114 fprintf(stderr,"illegal option: -%c\n",*pos);
115 break;
116 }
117 }
118 }
119 return;
120 } /* get_options */
121
122
123 /* set up max character for which isupper() and toupper() gives */
124 /* right answer. Is usually 127 or 255 */
125
126 #define MAX_CHAR_OK 127 /* 7 Bit ascii */
127
128 /* Initiate arrays for case-conversation */
129
init_case_convert()130 void init_case_convert()
131 {
132 int16 i;
133 uchar *higher_pos,*lower_pos;
134 DBUG_ENTER("init_case_convert");
135
136 for (i=0 ; i <= MAX_CHAR_OK ; i++)
137 {
138 to_upper[i]= sort_order[i]= (islower(i) ? toupper(i) : (char) i);
139 to_lower[i]= (isupper(i) ? tolower(i) : (char) i);
140 }
141 #if MAX_CHAR_OK != 255
142 for (i--; i++ < 255 ;)
143 to_upper[i]= sort_order[i]= to_lower[i]= (char) i;
144 #endif
145
146 higher_pos= (uchar *) "[]\\@^";
147 lower_pos= (uchar *) "{}|`~";
148
149 while (*higher_pos)
150 {
151 to_upper[ *lower_pos ] = sort_order[ *lower_pos ] = (char) *higher_pos;
152 to_lower[ *higher_pos++ ] = (char) *lower_pos++;
153 }
154
155 /* sets upp sortorder; higer_pos character (upper and lower) is */
156 /* changed to lower_pos character */
157
158 higher_pos= (uchar *) "][\\~`"; /* R{tt ordning p} tecknen */
159 lower_pos= (uchar *) "[\\]YE"; /* Ordning enligt ascii */
160
161 while (*higher_pos)
162 {
163 sort_order[ *higher_pos ] =
164 sort_order[(uchar)to_lower[*higher_pos]] = *lower_pos;
165 higher_pos++; lower_pos++;
166 }
167 DBUG_VOID_RETURN;
168 } /* init_case_convert */
169