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
30 #define YY_NO_UNISTD_H
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
39 static LPCSTR macroptr;
41 static int quote_stack[32];
42 static unsigned int quote_stk_idx = 0;
45 #define YY_INPUT(buf,result,max_size)\
46 if ((result = *macroptr ? 1 : 0)) buf[0] = *macroptr++;
51 [-+]?[0-9]+ yylval.integer = strtol(yytext, NULL, 10); return INTEGER;
52 [-+]?0[xX][0-9a-f]+ yylval.integer = strtol(yytext, NULL, 16); return INTEGER;
54 [a-zA-Z][_0-9a-zA-Z]* return MACRO_Lookup(yytext, &yylval);
62 if (quote_stk_idx == 0 ||
63 (yytext[0] == '\"' && quote_stack[quote_stk_idx - 1] != '\"') ||
66 /* opening a new one */
67 if (quote_stk_idx == 0)
69 strptr = HeapAlloc(GetProcessHeap(), 0, strlen(macroptr) + 1);
70 yylval.string = strptr;
73 else *strptr++ = yytext[0];
74 quote_stack[quote_stk_idx++] = yytext[0];
75 assert(quote_stk_idx < sizeof(quote_stack) / sizeof(quote_stack[0]));
79 if (yytext[0] == '`') assert(0);
80 /* close the current quote */
81 if (--quote_stk_idx == 0)
87 else *strptr++ = yytext[0];
91 <quote>. *strptr++ = yytext[0];
92 <quote>\\. *strptr++ = yytext[1];
93 <quote><<EOF>> return 0;
100 /* all code for testing macros */
102 static CHAR szTestMacro[256];
104 static LRESULT CALLBACK MACRO_TestDialogProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
106 if (msg == WM_COMMAND && wParam == IDOK)
108 GetDlgItemText(hDlg, 99, szTestMacro, sizeof(szTestMacro));
109 EndDialog(hDlg, IDOK);
115 void macro_test(void)
117 WNDPROC lpfnDlg = MakeProcInstance(MACRO_TestDialogProc, Globals.hInstance);
118 DialogBox(Globals.hInstance, STRING_DIALOG_TEST, Globals.active_win->hMainWnd, (DLGPROC)lpfnDlg);
119 FreeProcInstance(lpfnDlg);
124 /* small helper function for debug messages */
125 static const char* ts(int t)
127 static char c[2] = {0,0};
131 case EMPTY: return "EMPTY";
132 case VOID_FUNCTION: return "VOID_FUNCTION";
133 case BOOL_FUNCTION: return "BOOL_FUNCTION";
134 case INTEGER: return "INTEGER";
135 case STRING: return "STRING";
136 case IDENTIFIER: return "IDENTIFIER";
137 default: c[0] = (char)t; return c;
141 static int MACRO_CallBoolFunc(FARPROC fn, const char* args, void** ret);
143 /******************************************************************
146 * checks number of arguments against prototype, and stores arguments on
147 * stack pa for later call
148 * returns -1 on error, otherwise the number of pushed parameters
150 static int MACRO_CheckArgs(void* pa[], unsigned max, const char* args)
153 unsigned int len = 0, idx = 0;
155 WINE_TRACE("Checking %s\n", args);
157 if (yylex() != '(') {WINE_WARN("missing (\n");return -1;}
165 WINE_TRACE("Got %s <=> %c\n", ts(t), *args);
171 {WINE_WARN("missing S\n");return -1;}
172 pa[idx] = (void*)yylval.string;
177 {WINE_WARN("missing U\n");return -1;}
178 pa[idx] = LongToPtr(yylval.integer);
181 if (t != BOOL_FUNCTION)
182 {WINE_WARN("missing B\n");return -1;}
183 if (MACRO_CallBoolFunc(yylval.function, yylval.proto, &pa[idx]) == 0)
187 WINE_WARN("unexpected %s while args is %c\n", ts(t), *args);
191 if (*++args == '\0') break;
193 if (t == ')') goto CheckArgs_end;
194 if (t != ',') {WINE_WARN("missing ,\n");return -1;}
195 if (idx >= max) {WINE_FIXME("stack overflow (%d)\n", max);return -1;}
198 if (yylex() != ')') {WINE_WARN("missing )\n");return -1;}
201 while (len > idx) pa[--len] = NULL;
205 /******************************************************************
208 * Invokes boolean function fn, which arguments are defined by args
209 * stores bool result into ret
211 static int MACRO_CallBoolFunc(FARPROC fn, const char* args, void** ret)
214 int idx = MACRO_CheckArgs(pa, sizeof(pa)/sizeof(pa[0]), args);
216 if (idx < 0) return 0;
219 WINE_TRACE("calling with %u pmts\n", idx);
221 switch (strlen(args))
223 case 0: *ret = (void*)(fn)(); break;
224 case 1: *ret = (void*)(fn)(pa[0]); break;
225 default: WINE_FIXME("NIY\n");
231 /******************************************************************
236 static int MACRO_CallVoidFunc(FARPROC fn, const char* args)
239 int idx = MACRO_CheckArgs(pa, sizeof(pa)/sizeof(pa[0]), args);
241 if (idx < 0) return 0;
244 WINE_TRACE("calling %p with %u pmts\n", fn, idx);
246 switch (strlen(args))
248 case 0: (fn)(); break;
249 case 1: (fn)(pa[0]); break;
250 case 2: (fn)(pa[0],pa[1]); break;
251 case 3: (fn)(pa[0],pa[1],pa[2]); break;
252 case 4: (fn)(pa[0],pa[1],pa[2],pa[3]); break;
253 case 5: (fn)(pa[0],pa[1],pa[2],pa[3],pa[4]); break;
254 case 6: (fn)(pa[0],pa[1],pa[2],pa[3],pa[4],pa[5]); break;
255 default: WINE_FIXME("NIY\n");
261 BOOL MACRO_ExecuteMacro(LPCSTR macro)
265 WINE_TRACE("%s\n", wine_dbgstr_a(macro));
269 while ((t = yylex()) != EMPTY)
274 WINE_TRACE("got type void func(%s)\n", yylval.proto);
275 MACRO_CallVoidFunc(yylval.function, yylval.proto);
278 WINE_WARN("got type bool func(%s)\n", yylval.proto);
281 WINE_WARN("got unexpected type %s\n", ts(t));
286 case EMPTY: return 1;
292 HeapFree(GetProcessHeap(), 0, strptr);
300 int yywrap(void) { return 1; }