5 * Copyright 1996 Ulrich Schmid
6 * Copyright 2002 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 nounput interactive 8bit
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
33 static LPCSTR macroptr;
35 static int quote_stack[32];
36 static int quote_stk_idx = 0;
39 #define YY_INPUT(buf,result,max_size)\
40 if ((result = *macroptr ? 1 : 0)) buf[0] = *macroptr++;
45 [-+]?[0-9]+ yylval.integer = strtol(yytext, NULL, 10); return INTEGER;
46 [-+]?0[xX][0-9a-f]+ yylval.integer = strtol(yytext, NULL, 16); return INTEGER;
48 [a-zA-Z][_0-9a-zA-Z]* return MACRO_Lookup(yytext, &yylval);
56 if (quote_stk_idx == 0 ||
57 (yytext[0] == '\"' && quote_stack[quote_stk_idx - 1] != '\"') ||
60 /* opening a new one */
61 if (quote_stk_idx == 0)
63 strptr = HeapAlloc(GetProcessHeap(), 0, strlen(macroptr) + 1);
64 yylval.string = strptr;
67 else *strptr++ = yytext[0];
68 quote_stack[quote_stk_idx++] = yytext[0];
69 assert(quote_stk_idx < sizeof(quote_stack) / sizeof(quote_stack[0]));
73 if (yytext[0] == '`') assert(0);
74 /* close the current quote */
75 if (--quote_stk_idx == 0)
81 else *strptr++ = yytext[0];
85 <quote>. *strptr++ = yytext[0];
86 <quote>\\. *strptr++ = yytext[1];
87 <quote><<EOF>> return 0;
94 /* all code for testing macros */
96 static CHAR szTestMacro[256];
98 static LRESULT CALLBACK MACRO_TestDialogProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
100 if (msg == WM_COMMAND && wParam == IDOK)
102 GetDlgItemText(hDlg, 99, szTestMacro, sizeof(szTestMacro));
103 EndDialog(hDlg, IDOK);
109 void macro_test(void)
111 WNDPROC lpfnDlg = MakeProcInstance(MACRO_TestDialogProc, Globals.hInstance);
112 DialogBox(Globals.hInstance, STRING_DIALOG_TEST, Globals.active_win->hMainWnd, (DLGPROC)lpfnDlg);
113 FreeProcInstance(lpfnDlg);
118 /* small helper function for debug messages */
119 static const char* ts(int t)
121 static char c[2] = {0,0};
125 case EMPTY: return "EMPTY";
126 case VOID_FUNCTION: return "VOID_FUNCTION";
127 case BOOL_FUNCTION: return "BOOL_FUNCTION";
128 case INTEGER: return "INTEGER";
129 case STRING: return "STRING";
130 case IDENTIFIER: return "IDENTIFIER";
131 default: c[0] = (char)t; return c;
135 static int MACRO_CallBoolFunc(FARPROC fn, const char* args, void** ret);
137 /******************************************************************
140 * checks number of arguments against prototype, and stores arguments on
141 * stack pa for later call
142 * returns -1 on error, otherwise the number of pushed parameters
144 static int MACRO_CheckArgs(void* pa[], unsigned max, const char* args)
147 int len = 0, idx = 0;
149 WINE_TRACE("Checking %s\n", args);
151 if (yylex() != '(') {WINE_WARN("missing (\n");return -1;}
159 WINE_TRACE("Got %s <=> %c\n", ts(t), *args);
165 {WINE_WARN("missing S\n");return -1;}
166 pa[idx] = (void*)yylval.string;
171 {WINE_WARN("missing U\n");return -1;}
172 pa[idx] = (void*)yylval.integer;
175 if (t != BOOL_FUNCTION)
176 {WINE_WARN("missing B\n");return -1;}
177 if (MACRO_CallBoolFunc(yylval.function, yylval.proto, &pa[idx]) == 0)
181 WINE_WARN("unexpected %s while args is %c\n", ts(t), *args);
185 if (*++args == '\0') break;
187 if (t == ')') goto CheckArgs_end;
188 if (t != ',') {WINE_WARN("missing ,\n");return -1;}
189 if (idx >= max) {WINE_FIXME("stack overflow (%d)\n", max);return -1;}
192 if (yylex() != ')') {WINE_WARN("missing )\n");return -1;}
195 while (len > idx) pa[--len] = NULL;
199 /******************************************************************
202 * Invokes boolean function fn, which arguments are defined by args
203 * stores bool result into ret
205 static int MACRO_CallBoolFunc(FARPROC fn, const char* args, void** ret)
208 int idx = MACRO_CheckArgs(pa, sizeof(pa)/sizeof(pa[0]), args);
210 if (idx < 0) return 0;
213 WINE_TRACE("calling with %u pmts\n", idx);
215 switch (strlen(args))
217 case 0: *ret = (void*)(fn)(); break;
218 case 1: *ret = (void*)(fn)(pa[0]); break;
219 default: WINE_FIXME("NIY\n");
225 /******************************************************************
230 static int MACRO_CallVoidFunc(FARPROC fn, const char* args)
233 int idx = MACRO_CheckArgs(pa, sizeof(pa)/sizeof(pa[0]), args);
235 if (idx < 0) return 0;
238 WINE_TRACE("calling %p with %u pmts\n", fn, idx);
240 switch (strlen(args))
242 case 0: (fn)(); break;
243 case 1: (fn)(pa[0]); break;
244 case 2: (fn)(pa[0],pa[1]); break;
245 case 3: (fn)(pa[0],pa[1],pa[2]); break;
246 case 4: (fn)(pa[0],pa[1],pa[2],pa[3]); break;
247 case 5: (fn)(pa[0],pa[1],pa[2],pa[3],pa[4]); break;
248 case 6: (fn)(pa[0],pa[1],pa[2],pa[3],pa[4],pa[5]); break;
249 default: WINE_FIXME("NIY\n");
255 BOOL MACRO_ExecuteMacro(LPCSTR macro)
259 WINE_TRACE("%s\n", wine_dbgstr_a(macro));
263 while ((t = yylex()) != EMPTY)
268 WINE_TRACE("got type void func(%s)\n", yylval.proto);
269 MACRO_CallVoidFunc(yylval.function, yylval.proto);
272 WINE_WARN("got type bool func(%s)\n", yylval.proto);
275 WINE_WARN("got unexpected type %s\n", ts(t));
280 case EMPTY: return 1;
286 HeapFree(GetProcessHeap(), 0, strptr);
294 int yywrap(void) { return 1; }