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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
32 static LPCSTR macroptr;
34 static int quote_stack[32];
35 static int quote_stk_idx = 0;
38 #define YY_INPUT(buf,result,max_size)\
39 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(BOOL (*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)
149 WINE_TRACE("Checking %s\n", args);
151 if (yylex() != '(') {WINE_WARN("missing (\n");return -1;}
158 WINE_TRACE("Got %s <=> %c\n", ts(t), *args);
164 {WINE_WARN("missing S\n");return -1;}
165 pa[idx] = (void*)yylval.string;
170 {WINE_WARN("missing U\n");return -1;}
171 pa[idx] = (void*)yylval.integer;
174 if (t != BOOL_FUNCTION)
175 {WINE_WARN("missing B\n");return -1;}
176 if (MACRO_CallBoolFunc(yylval.bool_function, yylval.proto, &pa[idx]) == 0)
180 WINE_WARN("unexpected %s while args is %c\n", ts(t), *args);
184 if (*++args == '\0') break;
185 if (yylex() != ',') {WINE_WARN("missing ,\n");return -1;}
186 if (idx == max) {WINE_FIXME("stack overflow (%d)\n", max);return -1;}
189 if (yylex() != ')') {WINE_WARN("missing )\n");return -1;}
193 /******************************************************************
196 * Invokes boolean function fn, which arguments are defined by args
197 * stores bool result into ret
199 static int MACRO_CallBoolFunc(BOOL (*fn)(), const char* args, void** ret)
202 int idx = MACRO_CheckArgs(pa, sizeof(pa)/sizeof(pa[0]), args);
204 if (idx == -1) return 0;
207 WINE_TRACE("calling with %u pmts\n", idx);
211 case 0: *ret = (void*)(fn)(); break;
212 case 1: *ret = (void*)(fn)(pa[0]); break;
213 default: WINE_FIXME("NIY\n");
219 /******************************************************************
224 static int MACRO_CallVoidFunc(void (*fn)(), const char* args)
227 int idx = MACRO_CheckArgs(pa, sizeof(pa)/sizeof(pa[0]), args);
229 if (idx == -1) return 0;
232 WINE_TRACE("calling with %u pmts\n", idx);
236 case 0: (fn)(); break;
237 case 1: (fn)(pa[0]); break;
238 case 2: (fn)(pa[0],pa[1]); break;
239 case 3: (fn)(pa[0],pa[1],pa[2]); break;
240 case 4: (fn)(pa[0],pa[1],pa[2],pa[3]); break;
241 case 5: (fn)(pa[0],pa[1],pa[2],pa[3],pa[4]); break;
242 case 6: (fn)(pa[0],pa[1],pa[2],pa[3],pa[4],pa[5]); break;
243 default: WINE_FIXME("NIY\n");
249 BOOL MACRO_ExecuteMacro(LPCSTR macro)
253 WINE_TRACE("%s\n", wine_dbgstr_a(macro));
257 while ((t = yylex()) != EMPTY)
262 WINE_TRACE("got type void func(%s)\n", yylval.proto);
263 MACRO_CallVoidFunc(yylval.void_function, yylval.proto);
266 WINE_WARN("got type bool func(%s)\n", yylval.proto);
269 WINE_WARN("got unexpected type %s\n", ts(t));
274 case EMPTY: return 1;
282 HeapFree(GetProcessHeap(), 0, strptr);
291 int yywrap(void) { return 1; }