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
31 #define YY_NO_UNISTD_H
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
48 unsigned quote_stk_idx;
49 LPSTR cache_string[32];
51 WINHELP_WINDOW* window;
53 static struct lex_data* lex_data = NULL;
57 #define YY_INPUT(buf,result,max_size)\
58 if ((result = *lex_data->macroptr ? 1 : 0)) buf[0] = *lex_data->macroptr++;
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;
66 [a-zA-Z][_0-9a-zA-Z]* return MACRO_Lookup(yytext, &yylval);
74 if (lex_data->quote_stk_idx == 0 ||
75 (yytext[0] == '\"' && lex_data->quote_stack[lex_data->quote_stk_idx - 1] != '\"') ||
78 /* opening a new one */
79 if (lex_data->quote_stk_idx == 0)
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++;
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]));
93 if (yytext[0] == '`') assert(0);
94 /* close the current quote */
95 if (--lex_data->quote_stk_idx == 0)
98 *lex_data->strptr++ = '\0';
101 else *lex_data->strptr++ = yytext[0];
105 <quote>. *lex_data->strptr++ = yytext[0];
106 <quote>\\. *lex_data->strptr++ = yytext[1];
107 <quote><<EOF>> return 0;
114 /* all code for testing macros */
116 static CHAR szTestMacro[256];
118 static LRESULT CALLBACK MACRO_TestDialogProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
120 if (msg == WM_COMMAND && wParam == IDOK)
122 GetDlgItemText(hDlg, 99, szTestMacro, sizeof(szTestMacro));
123 EndDialog(hDlg, IDOK);
129 void macro_test(void)
131 WNDPROC lpfnDlg = MakeProcInstance(MACRO_TestDialogProc, Globals.hInstance);
132 DialogBox(Globals.hInstance, STRING_DIALOG_TEST, Globals.active_win->hMainWnd, (DLGPROC)lpfnDlg);
133 FreeProcInstance(lpfnDlg);
138 /* small helper function for debug messages */
139 static const char* ts(int t)
141 static char c[2] = {0,0};
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;
155 static int MACRO_CallBoolFunc(void *fn, const char* args, void** ret);
157 /******************************************************************
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
164 static int MACRO_CheckArgs(void* pa[], unsigned max, const char* args)
167 unsigned int len = 0, idx = 0;
169 WINE_TRACE("Checking %s\n", args);
171 if (yylex() != '(') {WINE_WARN("missing (\n");return -1;}
179 WINE_TRACE("Got %s <=> %c\n", ts(t), *args);
185 {WINE_WARN("missing S\n");return -1;}
186 pa[idx] = (void*)yylval.string;
191 {WINE_WARN("missing U\n");return -1;}
192 pa[idx] = LongToPtr(yylval.integer);
195 if (t != BOOL_FUNCTION)
196 {WINE_WARN("missing B\n");return -1;}
197 if (MACRO_CallBoolFunc(yylval.function, yylval.proto, &pa[idx]) == 0)
201 WINE_WARN("unexpected %s while args is %c\n", ts(t), *args);
205 if (*++args == '\0') break;
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;}
212 if (yylex() != ')') {WINE_WARN("missing )\n");return -1;}
215 while (len > idx) pa[--len] = NULL;
219 /******************************************************************
222 * Invokes boolean function fn, which arguments are defined by args
223 * stores bool result into ret
225 static int MACRO_CallBoolFunc(void *fn, const char* args, void** ret)
228 int idx = MACRO_CheckArgs(pa, sizeof(pa)/sizeof(pa[0]), args);
230 if (idx < 0) return 0;
233 WINE_TRACE("calling with %u pmts\n", idx);
235 switch (strlen(args))
239 BOOL (WINAPI *func)(void) = fn;
240 *ret = (void *)(ULONG_PTR)func();
245 BOOL (WINAPI *func)(void *) = fn;
246 *ret = (void *)(ULONG_PTR)func( pa[0]);
249 default: WINE_FIXME("NIY\n");
255 /******************************************************************
260 static int MACRO_CallVoidFunc(void *fn, const char* args)
263 int idx = MACRO_CheckArgs(pa, sizeof(pa)/sizeof(pa[0]), args);
265 if (idx < 0) return 0;
268 WINE_TRACE("calling %p with %u pmts\n", fn, idx);
270 switch (strlen(args))
274 void (WINAPI *func)(void) = fn;
280 void (WINAPI *func)(void*) = fn;
286 void (WINAPI *func)(void*,void*) = fn;
287 func( pa[0], pa[1] );
292 void (WINAPI *func)(void*,void*,void*) = fn;
293 func( pa[0], pa[1], pa[2] );
298 void (WINAPI *func)(void*,void*,void*,void*) = fn;
299 func( pa[0], pa[1], pa[2], pa[3] );
304 void (WINAPI *func)(void*,void*,void*,void*,void*) = fn;
305 func( pa[0], pa[1], pa[2], pa[3], pa[4] );
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] );
314 default: WINE_FIXME("NIY\n");
320 BOOL MACRO_ExecuteMacro(WINHELP_WINDOW* window, LPCSTR macro)
322 struct lex_data curr_lex_data, *prev_lex_data;
326 WINE_TRACE("%s\n", wine_dbgstr_a(macro));
328 prev_lex_data = lex_data;
329 lex_data = &curr_lex_data;
331 memset(lex_data, 0, sizeof(*lex_data));
332 lex_data->macroptr = macro;
333 lex_data->window = WINHELP_GrabWindow(window);
335 while ((t = yylex()) != EMPTY)
340 WINE_TRACE("got type void func(%s)\n", yylval.proto);
341 MACRO_CallVoidFunc(yylval.function, yylval.proto);
344 WINE_WARN("got type bool func(%s)\n", yylval.proto);
347 WINE_WARN("got unexpected type %s\n", ts(t));
354 case EMPTY: goto done;
356 default: ret = FALSE; YY_FLUSH_BUFFER; goto 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);
369 WINHELP_WINDOW* MACRO_CurrentWindow(void)
371 return lex_data ? lex_data->window : Globals.active_win;
375 int yywrap(void) { return 1; }