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(FARPROC 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(FARPROC 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))
237 case 0: *ret = (void*)(fn)(); break;
238 case 1: *ret = (void*)(fn)(pa[0]); break;
239 default: WINE_FIXME("NIY\n");
245 /******************************************************************
250 static int MACRO_CallVoidFunc(FARPROC fn, const char* args)
253 int idx = MACRO_CheckArgs(pa, sizeof(pa)/sizeof(pa[0]), args);
255 if (idx < 0) return 0;
258 WINE_TRACE("calling %p with %u pmts\n", fn, idx);
260 switch (strlen(args))
262 case 0: (fn)(); break;
263 case 1: (fn)(pa[0]); break;
264 case 2: (fn)(pa[0],pa[1]); break;
265 case 3: (fn)(pa[0],pa[1],pa[2]); break;
266 case 4: (fn)(pa[0],pa[1],pa[2],pa[3]); break;
267 case 5: (fn)(pa[0],pa[1],pa[2],pa[3],pa[4]); break;
268 case 6: (fn)(pa[0],pa[1],pa[2],pa[3],pa[4],pa[5]); break;
269 default: WINE_FIXME("NIY\n");
275 BOOL MACRO_ExecuteMacro(WINHELP_WINDOW* window, LPCSTR macro)
277 struct lex_data curr_lex_data, *prev_lex_data;
281 WINE_TRACE("%s\n", wine_dbgstr_a(macro));
283 prev_lex_data = lex_data;
284 lex_data = &curr_lex_data;
286 memset(lex_data, 0, sizeof(*lex_data));
287 lex_data->macroptr = macro;
288 lex_data->window = window;
290 while ((t = yylex()) != EMPTY)
295 WINE_TRACE("got type void func(%s)\n", yylval.proto);
296 MACRO_CallVoidFunc(yylval.function, yylval.proto);
299 WINE_WARN("got type bool func(%s)\n", yylval.proto);
302 WINE_WARN("got unexpected type %s\n", ts(t));
309 case EMPTY: goto done;
311 default: ret = FALSE; YY_FLUSH_BUFFER; goto done;
316 for (t = 0; t < lex_data->cache_used; t++)
317 HeapFree(GetProcessHeap(), 0, lex_data->cache_string[t]);
318 lex_data = prev_lex_data;
323 WINHELP_WINDOW* MACRO_CurrentWindow(void)
325 return lex_data ? lex_data->window : Globals.active_win;
329 int yywrap(void) { return 1; }