msi: Only copy the resulting string if the RegistryValue call succeeded.
[wine] / dlls / msi / tokenize.c
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** An tokenizer for SQL
13 **
14 ** This file contains C code that splits an SQL input string up into
15 ** individual tokens and sends those tokens one-by-one over to the
16 ** parser for analysis.
17 */
18
19 #include <ctype.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wine/unicode.h"
26 #include "query.h"
27 #include "sql.tab.h"
28
29 /*
30 ** All the keywords of the SQL language are stored as in a hash
31 ** table composed of instances of the following structure.
32 */
33 typedef struct Keyword Keyword;
34 struct Keyword {
35   const WCHAR *zName;             /* The keyword name */
36   int tokenType;           /* The token value for this keyword */
37 };
38
39 #define MAX_TOKEN_LEN 11
40
41 static const WCHAR ADD_W[] = { 'A','D','D',0 };
42 static const WCHAR ALTER_W[] = { 'A','L','T','E','R',0 };
43 static const WCHAR AND_W[] = { 'A','N','D',0 };
44 static const WCHAR BY_W[] = { 'B','Y',0 };
45 static const WCHAR CHAR_W[] = { 'C','H','A','R',0 };
46 static const WCHAR CHARACTER_W[] = { 'C','H','A','R','A','C','T','E','R',0 };
47 static const WCHAR CREATE_W[] = { 'C','R','E','A','T','E',0 };
48 static const WCHAR DELETE_W[] = { 'D','E','L','E','T','E',0 };
49 static const WCHAR DISTINCT_W[] = { 'D','I','S','T','I','N','C','T',0 };
50 static const WCHAR FREE_W[] = { 'F','R','E','E',0 };
51 static const WCHAR FROM_W[] = { 'F','R','O','M',0 };
52 static const WCHAR HOLD_W[] = { 'H','O','L','D',0 };
53 static const WCHAR INSERT_W[] = { 'I','N','S','E','R','T',0 };
54 static const WCHAR INT_W[] = { 'I','N','T',0 };
55 static const WCHAR INTEGER_W[] = { 'I','N','T','E','G','E','R',0 };
56 static const WCHAR INTO_W[] = { 'I','N','T','O',0 };
57 static const WCHAR IS_W[] = { 'I','S',0 };
58 static const WCHAR KEY_W[] = { 'K','E','Y',0 };
59 static const WCHAR LIKE_W[] = { 'L','I','K','E',0 };
60 static const WCHAR LOCALIZABLE_W[] = { 'L','O','C','A','L','I','Z','A','B','L','E',0 };
61 static const WCHAR LONG_W[] = { 'L','O','N','G',0 };
62 static const WCHAR LONGCHAR_W[] = { 'L','O','N','G','C','H','A','R',0 };
63 static const WCHAR NOT_W[] = { 'N','O','T',0 };
64 static const WCHAR NULL_W[] = { 'N','U','L','L',0 };
65 static const WCHAR OBJECT_W[] = { 'O','B','J','E','C','T',0 };
66 static const WCHAR OR_W[] = { 'O','R',0 };
67 static const WCHAR ORDER_W[] = { 'O','R','D','E','R',0 };
68 static const WCHAR PRIMARY_W[] = { 'P','R','I','M','A','R','Y',0 };
69 static const WCHAR SELECT_W[] = { 'S','E','L','E','C','T',0 };
70 static const WCHAR SET_W[] = { 'S','E','T',0 };
71 static const WCHAR SHORT_W[] = { 'S','H','O','R','T',0 };
72 static const WCHAR TABLE_W[] = { 'T','A','B','L','E',0 };
73 static const WCHAR TEMPORARY_W[] = { 'T','E','M','P','O','R','A','R','Y',0 };
74 static const WCHAR UPDATE_W[] = { 'U','P','D','A','T','E',0 };
75 static const WCHAR VALUES_W[] = { 'V','A','L','U','E','S',0 };
76 static const WCHAR WHERE_W[] = { 'W','H','E','R','E',0 };
77
78 /*
79 ** These are the keywords
80 ** They MUST be in alphabetical order
81 */
82 static const Keyword aKeywordTable[] = {
83   { ADD_W, TK_ADD },
84   { ALTER_W, TK_ALTER },
85   { AND_W, TK_AND },
86   { BY_W, TK_BY },
87   { CHAR_W, TK_CHAR },
88   { CHARACTER_W, TK_CHAR },
89   { CREATE_W, TK_CREATE },
90   { DELETE_W, TK_DELETE },
91   { DISTINCT_W, TK_DISTINCT },
92   { FREE_W, TK_FREE },
93   { FROM_W, TK_FROM },
94   { HOLD_W, TK_HOLD },
95   { INSERT_W, TK_INSERT },
96   { INT_W, TK_INT },
97   { INTEGER_W, TK_INT },
98   { INTO_W, TK_INTO },
99   { IS_W, TK_IS },
100   { KEY_W, TK_KEY },
101   { LIKE_W, TK_LIKE },
102   { LOCALIZABLE_W, TK_LOCALIZABLE },
103   { LONG_W, TK_LONG },
104   { LONGCHAR_W, TK_LONGCHAR },
105   { NOT_W, TK_NOT },
106   { NULL_W, TK_NULL },
107   { OBJECT_W, TK_OBJECT },
108   { OR_W, TK_OR },
109   { ORDER_W, TK_ORDER },
110   { PRIMARY_W, TK_PRIMARY },
111   { SELECT_W, TK_SELECT },
112   { SET_W, TK_SET },
113   { SHORT_W, TK_SHORT },
114   { TABLE_W, TK_TABLE },
115   { TEMPORARY_W, TK_TEMPORARY },
116   { UPDATE_W, TK_UPDATE },
117   { VALUES_W, TK_VALUES },
118   { WHERE_W, TK_WHERE },
119 };
120
121 #define KEYWORD_COUNT ( sizeof aKeywordTable/sizeof (Keyword) )
122
123 /*
124 ** Comparison function for binary search.
125 */
126 static int compKeyword(const void *m1, const void *m2){
127   const Keyword *k1 = m1, *k2 = m2;
128
129   return strcmpiW( k1->zName, k2->zName );
130 }
131
132 /*
133 ** This function looks up an identifier to determine if it is a
134 ** keyword.  If it is a keyword, the token code of that keyword is 
135 ** returned.  If the input is not a keyword, TK_ID is returned.
136 */
137 static int sqliteKeywordCode(const WCHAR *z, int n){
138   WCHAR str[MAX_TOKEN_LEN+1];
139   Keyword key, *r;
140
141   if( n>MAX_TOKEN_LEN )
142     return TK_ID;
143
144   memcpy( str, z, n*sizeof (WCHAR) );
145   str[n] = 0;
146   key.tokenType = 0;
147   key.zName = str;
148   r = bsearch( &key, aKeywordTable, KEYWORD_COUNT, sizeof (Keyword), compKeyword );
149   if( r )
150     return r->tokenType;
151   return TK_ID;
152 }
153
154
155 /*
156 ** If X is a character that can be used in an identifier then
157 ** isIdChar[X] will be 1.  Otherwise isIdChar[X] will be 0.
158 **
159 ** In this implementation, an identifier can be a string of
160 ** alphabetic characters, digits, and "_" plus any character
161 ** with the high-order bit set.  The latter rule means that
162 ** any sequence of UTF-8 characters or characters taken from
163 ** an extended ISO8859 character set can form an identifier.
164 */
165 static const char isIdChar[] = {
166 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
167     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
168     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
169     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
170     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
171     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
172     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
173     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
174     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
175     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 8x */
176     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 9x */
177     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Ax */
178     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Bx */
179     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Cx */
180     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Dx */
181     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Ex */
182     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Fx */
183 };
184
185
186 /*
187 ** Return the length of the token that begins at z[0].  Return
188 ** -1 if the token is (or might be) incomplete.  Store the token
189 ** type in *tokenType before returning.
190 */
191 int sqliteGetToken(const WCHAR *z, int *tokenType){
192   int i;
193   switch( *z ){
194     case ' ': case '\t': case '\n': case '\f': case '\r': {
195       for(i=1; isspace(z[i]); i++){}
196       *tokenType = TK_SPACE;
197       return i;
198     }
199     case '-': {
200       if( z[1]==0 ) return -1;
201       *tokenType = TK_MINUS;
202       return 1;
203     }
204     case '(':
205       *tokenType = TK_LP;
206       return 1;
207     case ')':
208       *tokenType = TK_RP;
209       return 1;
210     case '*':
211       *tokenType = TK_STAR;
212       return 1;
213     case '=':
214       *tokenType = TK_EQ;
215       return 1;
216     case '<': {
217       if( z[1]=='=' ){
218         *tokenType = TK_LE;
219         return 2;
220       }else if( z[1]=='>' ){
221         *tokenType = TK_NE;
222         return 2;
223       }else{
224         *tokenType = TK_LT;
225         return 1;
226       }
227     }
228     case '>': {
229       if( z[1]=='=' ){
230         *tokenType = TK_GE;
231         return 2;
232       }else{
233         *tokenType = TK_GT;
234         return 1;
235       }
236     }
237     case '!': {
238       if( z[1]!='=' ){
239         *tokenType = TK_ILLEGAL;
240         return 2;
241       }else{
242         *tokenType = TK_NE;
243         return 2;
244       }
245     }
246     case '?':
247       *tokenType = TK_WILDCARD;
248       return 1;
249     case ',':
250       *tokenType = TK_COMMA;
251       return 1;
252     case '`': case '\'': {
253       int delim = z[0];
254       for(i=1; z[i]; i++){
255         if( z[i]==delim ){
256           if( z[i+1]==delim ){
257             i++;
258           }else{
259             break;
260           }
261         }
262       }
263       if( z[i] ) i++;
264       if( delim == '`' )
265         *tokenType = TK_ID;
266       else
267         *tokenType = TK_STRING;
268       return i;
269     }
270     case '.': {
271       if( !isdigit(z[1]) ){
272         *tokenType = TK_DOT;
273         return 1;
274       }
275       /* Fall thru into the next case */
276     }
277     case '0': case '1': case '2': case '3': case '4':
278     case '5': case '6': case '7': case '8': case '9': {
279       *tokenType = TK_INTEGER;
280       for(i=1; isdigit(z[i]); i++){}
281       return i;
282     }
283     case '[': {
284       for(i=1; z[i] && z[i-1]!=']'; i++){}
285       *tokenType = TK_ID;
286       return i;
287     }
288     default: {
289       if( !isIdChar[*z] ){
290         break;
291       }
292       for(i=1; isIdChar[z[i]]; i++){}
293       *tokenType = sqliteKeywordCode(z, i);
294       return i;
295     }
296   }
297   *tokenType = TK_ILLEGAL;
298   return 1;
299 }