5 * Copyright 1996 Ulrich Schmid
6 * Copyright 2002,2008 Eric Pouech
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.
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.
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
23 %option noinput nounput interactive 8bit
30 #define YY_NO_UNISTD_H
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
45 unsigned quote_stk_idx;
46 LPSTR cache_string[32];
48 WINHELP_WINDOW* window;
50 static struct lex_data* lex_data = NULL;
54 #define YY_INPUT(buf,result,max_size)\
55 if ((result = *lex_data->macroptr ? 1 : 0)) buf[0] = *lex_data->macroptr++;
60 [-+]?[0-9]+ yylval.integer = strtol(yytext, NULL, 10); return INTEGER;
61 [-+]?0[xX][0-9a-f]+ yylval.integer = strtol(yytext, NULL, 16); return INTEGER;
63 [a-zA-Z][_0-9a-zA-Z]* return MACRO_Lookup(yytext, &yylval);
71 if (lex_data->quote_stk_idx == 0 ||
72 (yytext[0] == '\"' && lex_data->quote_stack[lex_data->quote_stk_idx - 1] != '\"') ||
75 /* opening a new one */
76 if (lex_data->quote_stk_idx == 0)
78 assert(lex_data->cache_used < sizeof(lex_data->cache_string) / sizeof(lex_data->cache_string[0]));
79 lex_data->strptr = lex_data->cache_string[lex_data->cache_used] = HeapAlloc(GetProcessHeap(), 0, strlen(lex_data->macroptr) + 1);
80 yylval.string = lex_data->strptr;
81 lex_data->cache_used++;
84 else *lex_data->strptr++ = yytext[0];
85 lex_data->quote_stack[lex_data->quote_stk_idx++] = yytext[0];
86 assert(lex_data->quote_stk_idx < sizeof(lex_data->quote_stack) / sizeof(lex_data->quote_stack[0]));
90 if (yytext[0] == '`') assert(0);
91 /* close the current quote */
92 if (--lex_data->quote_stk_idx == 0)
95 *lex_data->strptr++ = '\0';
98 else *lex_data->strptr++ = yytext[0];
102 <quote>. *lex_data->strptr++ = yytext[0];
103 <quote>\\. *lex_data->strptr++ = yytext[1];
104 <quote><<EOF>> return 0;
111 /* all code for testing macros */
113 static CHAR szTestMacro[256];
115 static LRESULT CALLBACK MACRO_TestDialogProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
117 if (msg == WM_COMMAND && wParam == IDOK)
119 GetDlgItemText(hDlg, 99, szTestMacro, sizeof(szTestMacro));
120 EndDialog(hDlg, IDOK);
126 void macro_test(void)
128 WNDPROC lpfnDlg = MakeProcInstance(MACRO_TestDialogProc, Globals.hInstance);
129 DialogBox(Globals.hInstance, STRING_DIALOG_TEST, Globals.active_win->hMainWnd, (DLGPROC)lpfnDlg);
130 FreeProcInstance(lpfnDlg);
135 /* small helper function for debug messages */
136 static const char* ts(int t)
138 static char c[2] = {0,0};
142 case EMPTY: return "EMPTY";
143 case VOID_FUNCTION: return "VOID_FUNCTION";
144 case BOOL_FUNCTION: return "BOOL_FUNCTION";
145 case INTEGER: return "INTEGER";
146 case STRING: return "STRING";
147 case IDENTIFIER: return "IDENTIFIER";
148 default: c[0] = (char)t; return c;
152 static int MACRO_CallBoolFunc(void *fn, const char* args, void** ret);
154 /******************************************************************
157 * checks number of arguments against prototype, and stores arguments on
158 * stack pa for later call
159 * returns -1 on error, otherwise the number of pushed parameters
161 static int MACRO_CheckArgs(void* pa[], unsigned max, const char* args)
164 unsigned int len = 0, idx = 0;
166 WINE_TRACE("Checking %s\n", args);
168 if (yylex() != '(') {WINE_WARN("missing (\n");return -1;}
176 WINE_TRACE("Got %s <=> %c\n", ts(t), *args);
182 {WINE_WARN("missing S\n");return -1;}
183 pa[idx] = (void*)yylval.string;
188 {WINE_WARN("missing U\n");return -1;}
189 pa[idx] = LongToPtr(yylval.integer);
192 if (t != BOOL_FUNCTION)
193 {WINE_WARN("missing B\n");return -1;}
194 if (MACRO_CallBoolFunc(yylval.function, yylval.proto, &pa[idx]) == 0)
198 WINE_WARN("unexpected %s while args is %c\n", ts(t), *args);
202 if (*++args == '\0') break;
204 if (t == ')') goto CheckArgs_end;
205 if (t != ',') {WINE_WARN("missing ,\n");return -1;}
206 if (idx >= max) {WINE_FIXME("stack overflow (%d)\n", max);return -1;}
209 if (yylex() != ')') {WINE_WARN("missing )\n");return -1;}
212 while (len > idx) pa[--len] = NULL;
216 /******************************************************************
219 * Invokes boolean function fn, which arguments are defined by args
220 * stores bool result into ret
222 static int MACRO_CallBoolFunc(void *fn, const char* args, void** ret)
225 int idx = MACRO_CheckArgs(pa, sizeof(pa)/sizeof(pa[0]), args);
227 if (idx < 0) return 0;
230 WINE_TRACE("calling with %u pmts\n", idx);
232 switch (strlen(args))
236 BOOL (WINAPI *func)(void) = fn;
237 *ret = (void *)(ULONG_PTR)func();
242 BOOL (WINAPI *func)(void *) = fn;
243 *ret = (void *)(ULONG_PTR)func( pa[0]);
246 default: WINE_FIXME("NIY\n");
252 /******************************************************************
257 static int MACRO_CallVoidFunc(void *fn, const char* args)
260 int idx = MACRO_CheckArgs(pa, sizeof(pa)/sizeof(pa[0]), args);
262 if (idx < 0) return 0;
265 WINE_TRACE("calling %p with %u pmts\n", fn, idx);
267 switch (strlen(args))
271 void (WINAPI *func)(void) = fn;
277 void (WINAPI *func)(void*) = fn;
283 void (WINAPI *func)(void*,void*) = fn;
284 func( pa[0], pa[1] );
289 void (WINAPI *func)(void*,void*,void*) = fn;
290 func( pa[0], pa[1], pa[2] );
295 void (WINAPI *func)(void*,void*,void*,void*) = fn;
296 func( pa[0], pa[1], pa[2], pa[3] );
301 void (WINAPI *func)(void*,void*,void*,void*,void*) = fn;
302 func( pa[0], pa[1], pa[2], pa[3], pa[4] );
307 void (WINAPI *func)(void*,void*,void*,void*,void*,void*) = fn;
308 func( pa[0], pa[1], pa[2], pa[3], pa[4], pa[5] );
311 default: WINE_FIXME("NIY\n");
317 BOOL MACRO_ExecuteMacro(WINHELP_WINDOW* window, LPCSTR macro)
319 struct lex_data curr_lex_data, *prev_lex_data;
323 WINE_TRACE("%s\n", wine_dbgstr_a(macro));
325 prev_lex_data = lex_data;
326 lex_data = &curr_lex_data;
328 memset(lex_data, 0, sizeof(*lex_data));
329 lex_data->macroptr = macro;
330 lex_data->window = WINHELP_GrabWindow(window);
332 while ((t = yylex()) != EMPTY)
337 WINE_TRACE("got type void func(%s)\n", yylval.proto);
338 MACRO_CallVoidFunc(yylval.function, yylval.proto);
341 WINE_WARN("got type bool func(%s)\n", yylval.proto);
344 WINE_WARN("got unexpected type %s\n", ts(t));
351 case EMPTY: goto done;
353 default: ret = FALSE; YY_FLUSH_BUFFER; goto done;
358 for (t = 0; t < lex_data->cache_used; t++)
359 HeapFree(GetProcessHeap(), 0, lex_data->cache_string[t]);
360 lex_data = prev_lex_data;
361 WINHELP_ReleaseWindow(window);
366 WINHELP_WINDOW* MACRO_CurrentWindow(void)
368 return lex_data ? lex_data->window : Globals.active_win;
372 int yywrap(void) { return 1; }