Merge trunk head (r43756)
[reactos.git] / reactos / base / applications / winhlp32 / macro.lex.l
1 %{ /* -*-C-*- */
2 /*
3 * Help Viewer
4 *
5 * Copyright 1996 Ulrich Schmid
6 * Copyright 2002,2008 Eric Pouech
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22 %}
23 %option noinput nounput interactive 8bit
24 %x quote
25 %{
26 #include "config.h"
27 #include <assert.h>
28 #include <stdarg.h>
29
30 #ifndef HAVE_UNISTD_H
31 #define YY_NO_UNISTD_H
32 #endif
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "winhelp.h"
39
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
43
44 struct lex_data {
45 LPCSTR macroptr;
46 LPSTR strptr;
47 int quote_stack[32];
48 unsigned quote_stk_idx;
49 LPSTR cache_string[32];
50 int cache_used;
51 WINHELP_WINDOW* window;
52 };
53 static struct lex_data* lex_data = NULL;
54
55 struct lexret yylval;
56
57 #define YY_INPUT(buf,result,max_size)\
58 if ((result = *lex_data->macroptr ? 1 : 0)) buf[0] = *lex_data->macroptr++;
59
60 %}
61 %%
62
63 [-+]?[0-9]+ yylval.integer = strtol(yytext, NULL, 10); return INTEGER;
64 [-+]?0[xX][0-9a-f]+ yylval.integer = strtol(yytext, NULL, 16); return INTEGER;
65
66 [a-zA-Z][_0-9a-zA-Z]* return MACRO_Lookup(yytext, &yylval);
67
68 \` |
69 \" |
70 \' |
71 <quote>\` |
72 <quote>\" |
73 <quote>\' {
74 if (lex_data->quote_stk_idx == 0 ||
75 (yytext[0] == '\"' && lex_data->quote_stack[lex_data->quote_stk_idx - 1] != '\"') ||
76 (yytext[0] == '`'))
77 {
78 /* opening a new one */
79 if (lex_data->quote_stk_idx == 0)
80 {
81 assert(lex_data->cache_used < sizeof(lex_data->cache_string) / sizeof(lex_data->cache_string[0]));
82 lex_data->strptr = lex_data->cache_string[lex_data->cache_used] = HeapAlloc(GetProcessHeap(), 0, strlen(lex_data->macroptr) + 1);
83 yylval.string = lex_data->strptr;
84 lex_data->cache_used++;
85 BEGIN(quote);
86 }
87 else *lex_data->strptr++ = yytext[0];
88 lex_data->quote_stack[lex_data->quote_stk_idx++] = yytext[0];
89 assert(lex_data->quote_stk_idx < sizeof(lex_data->quote_stack) / sizeof(lex_data->quote_stack[0]));
90 }
91 else
92 {
93 if (yytext[0] == '`') assert(0);
94 /* close the current quote */
95 if (--lex_data->quote_stk_idx == 0)
96 {
97 BEGIN INITIAL;
98 *lex_data->strptr++ = '\0';
99 return STRING;
100 }
101 else *lex_data->strptr++ = yytext[0];
102 }
103 }
104
105 <quote>. *lex_data->strptr++ = yytext[0];
106 <quote>\\. *lex_data->strptr++ = yytext[1];
107 <quote><<EOF>> return 0;
108
109 " "
110 . return yytext[0];
111 %%
112
113 #if 0
114 /* all code for testing macros */
115 #include "winhelp.h"
116 static CHAR szTestMacro[256];
117
118 static LRESULT CALLBACK MACRO_TestDialogProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
119 {
120 if (msg == WM_COMMAND && wParam == IDOK)
121 {
122 GetDlgItemText(hDlg, 99, szTestMacro, sizeof(szTestMacro));
123 EndDialog(hDlg, IDOK);
124 return TRUE;
125 }
126 return FALSE;
127 }
128
129 void macro_test(void)
130 {
131 WNDPROC lpfnDlg = MakeProcInstance(MACRO_TestDialogProc, Globals.hInstance);
132 DialogBox(Globals.hInstance, STRING_DIALOG_TEST, Globals.active_win->hMainWnd, (DLGPROC)lpfnDlg);
133 FreeProcInstance(lpfnDlg);
134 macro = szTestMacro;
135 }
136 #endif
137
138 /* small helper function for debug messages */
139 static const char* ts(int t)
140 {
141 static char c[2] = {0,0};
142
143 switch (t)
144 {
145 case EMPTY: return "EMPTY";
146 case VOID_FUNCTION: return "VOID_FUNCTION";
147 case BOOL_FUNCTION: return "BOOL_FUNCTION";
148 case INTEGER: return "INTEGER";
149 case STRING: return "STRING";
150 case IDENTIFIER: return "IDENTIFIER";
151 default: c[0] = (char)t; return c;
152 }
153 }
154
155 static int MACRO_CallBoolFunc(void *fn, const char* args, void** ret);
156
157 /******************************************************************
158 * MACRO_CheckArgs
159 *
160 * checks number of arguments against prototype, and stores arguments on
161 * stack pa for later call
162 * returns -1 on error, otherwise the number of pushed parameters
163 */
164 static int MACRO_CheckArgs(void* pa[], unsigned max, const char* args)
165 {
166 int t;
167 unsigned int len = 0, idx = 0;
168
169 WINE_TRACE("Checking %s\n", args);
170
171 if (yylex() != '(') {WINE_WARN("missing (\n");return -1;}
172
173 if (*args)
174 {
175 len = strlen(args);
176 for (;;)
177 {
178 t = yylex();
179 WINE_TRACE("Got %s <=> %c\n", ts(t), *args);
180
181 switch (*args)
182 {
183 case 'S':
184 if (t != STRING)
185 {WINE_WARN("missing S\n");return -1;}
186 pa[idx] = (void*)yylval.string;
187 break;
188 case 'U':
189 case 'I':
190 if (t != INTEGER)
191 {WINE_WARN("missing U\n");return -1;}
192 pa[idx] = LongToPtr(yylval.integer);
193 break;
194 case 'B':
195 if (t != BOOL_FUNCTION)
196 {WINE_WARN("missing B\n");return -1;}
197 if (MACRO_CallBoolFunc(yylval.function, yylval.proto, &pa[idx]) == 0)
198 return -1;
199 break;
200 default:
201 WINE_WARN("unexpected %s while args is %c\n", ts(t), *args);
202 return -1;
203 }
204 idx++;
205 if (*++args == '\0') break;
206 t = yylex();
207 if (t == ')') goto CheckArgs_end;
208 if (t != ',') {WINE_WARN("missing ,\n");return -1;}
209 if (idx >= max) {WINE_FIXME("stack overflow (%d)\n", max);return -1;}
210 }
211 }
212 if (yylex() != ')') {WINE_WARN("missing )\n");return -1;}
213
214 CheckArgs_end:
215 while (len > idx) pa[--len] = NULL;
216 return idx;
217 }
218
219 /******************************************************************
220 * MACRO_CallBoolFunc
221 *
222 * Invokes boolean function fn, which arguments are defined by args
223 * stores bool result into ret
224 */
225 static int MACRO_CallBoolFunc(void *fn, const char* args, void** ret)
226 {
227 void* pa[2];
228 int idx = MACRO_CheckArgs(pa, sizeof(pa)/sizeof(pa[0]), args);
229
230 if (idx < 0) return 0;
231 if (!fn) return 1;
232
233 WINE_TRACE("calling with %u pmts\n", idx);
234
235 switch (strlen(args))
236 {
237 case 0:
238 {
239 BOOL (WINAPI *func)(void) = fn;
240 *ret = (void *)(ULONG_PTR)func();
241 break;
242 }
243 case 1:
244 {
245 BOOL (WINAPI *func)(void *) = fn;
246 *ret = (void *)(ULONG_PTR)func( pa[0]);
247 break;
248 }
249 default: WINE_FIXME("NIY\n");
250 }
251
252 return 1;
253 }
254
255 /******************************************************************
256 * MACRO_CallVoidFunc
257 *
258 *
259 */
260 static int MACRO_CallVoidFunc(void *fn, const char* args)
261 {
262 void* pa[6];
263 int idx = MACRO_CheckArgs(pa, sizeof(pa)/sizeof(pa[0]), args);
264
265 if (idx < 0) return 0;
266 if (!fn) return 1;
267
268 WINE_TRACE("calling %p with %u pmts\n", fn, idx);
269
270 switch (strlen(args))
271 {
272 case 0:
273 {
274 void (WINAPI *func)(void) = fn;
275 func();
276 break;
277 }
278 case 1:
279 {
280 void (WINAPI *func)(void*) = fn;
281 func( pa[0] );
282 break;
283 }
284 case 2:
285 {
286 void (WINAPI *func)(void*,void*) = fn;
287 func( pa[0], pa[1] );
288 break;
289 }
290 case 3:
291 {
292 void (WINAPI *func)(void*,void*,void*) = fn;
293 func( pa[0], pa[1], pa[2] );
294 break;
295 }
296 case 4:
297 {
298 void (WINAPI *func)(void*,void*,void*,void*) = fn;
299 func( pa[0], pa[1], pa[2], pa[3] );
300 break;
301 }
302 case 5:
303 {
304 void (WINAPI *func)(void*,void*,void*,void*,void*) = fn;
305 func( pa[0], pa[1], pa[2], pa[3], pa[4] );
306 break;
307 }
308 case 6:
309 {
310 void (WINAPI *func)(void*,void*,void*,void*,void*,void*) = fn;
311 func( pa[0], pa[1], pa[2], pa[3], pa[4], pa[5] );
312 break;
313 }
314 default: WINE_FIXME("NIY\n");
315 }
316
317 return 1;
318 }
319
320 BOOL MACRO_ExecuteMacro(WINHELP_WINDOW* window, LPCSTR macro)
321 {
322 struct lex_data curr_lex_data, *prev_lex_data;
323 BOOL ret = TRUE;
324 int t;
325
326 WINE_TRACE("%s\n", wine_dbgstr_a(macro));
327
328 prev_lex_data = lex_data;
329 lex_data = &curr_lex_data;
330
331 memset(lex_data, 0, sizeof(*lex_data));
332 lex_data->macroptr = macro;
333 lex_data->window = WINHELP_GrabWindow(window);
334
335 while ((t = yylex()) != EMPTY)
336 {
337 switch (t)
338 {
339 case VOID_FUNCTION:
340 WINE_TRACE("got type void func(%s)\n", yylval.proto);
341 MACRO_CallVoidFunc(yylval.function, yylval.proto);
342 break;
343 case BOOL_FUNCTION:
344 WINE_WARN("got type bool func(%s)\n", yylval.proto);
345 break;
346 default:
347 WINE_WARN("got unexpected type %s\n", ts(t));
348 YY_FLUSH_BUFFER;
349 ret = FALSE;
350 goto done;
351 }
352 switch (t = yylex())
353 {
354 case EMPTY: goto done;
355 case ';': break;
356 default: ret = FALSE; YY_FLUSH_BUFFER; goto done;
357 }
358 }
359
360 done:
361 for (t = 0; t < lex_data->cache_used; t++)
362 HeapFree(GetProcessHeap(), 0, lex_data->cache_string[t]);
363 lex_data = prev_lex_data;
364 WINHELP_ReleaseWindow(window);
365
366 return ret;
367 }
368
369 WINHELP_WINDOW* MACRO_CurrentWindow(void)
370 {
371 return lex_data ? lex_data->window : Globals.active_win;
372 }
373
374 #ifndef yywrap
375 int yywrap(void) { return 1; }
376 #endif