macro.lex.l 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. %{ /* -*-C-*- */
  2. /*
  3. * Help Viewer
  4. *
  5. * Copyright 1996 Ulrich Schmid
  6. * Copyright 2002,2008 Eric Pouech
  7. *
  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.
  12. *
  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.
  17. *
  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
  21. */
  22. %}
  23. %option noinput nounput never-interactive 8bit
  24. %x quote
  25. %{
  26. #include <assert.h>
  27. #include <stdarg.h>
  28. #define YY_NO_UNISTD_H
  29. #include "windef.h"
  30. #include "winbase.h"
  31. #include "wingdi.h"
  32. #include "winuser.h"
  33. #include "winhelp.h"
  34. #include "wine/debug.h"
  35. WINE_DEFAULT_DEBUG_CHANNEL(winhelp);
  36. struct lex_data {
  37. LPCSTR macroptr;
  38. LPSTR strptr;
  39. int quote_stack[32];
  40. unsigned quote_stk_idx;
  41. LPSTR cache_string[32];
  42. int cache_used;
  43. WINHELP_WINDOW* window;
  44. };
  45. static struct lex_data* lex_data = NULL;
  46. struct lexret yylval;
  47. #define YY_INPUT(buf,result,max_size)\
  48. if ((result = *lex_data->macroptr ? 1 : 0)) buf[0] = *lex_data->macroptr++;
  49. %}
  50. %%
  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;
  53. [a-zA-Z][_0-9a-zA-Z]* return MACRO_Lookup(yytext, &yylval);
  54. \` |
  55. \" |
  56. \' |
  57. <quote>\` |
  58. <quote>\" |
  59. <quote>\' {
  60. if (lex_data->quote_stk_idx == 0 ||
  61. (yytext[0] == '\"' && lex_data->quote_stack[lex_data->quote_stk_idx - 1] != '\"') ||
  62. (yytext[0] == '`'))
  63. {
  64. /* opening a new one */
  65. if (lex_data->quote_stk_idx == 0)
  66. {
  67. assert(lex_data->cache_used < ARRAY_SIZE(lex_data->cache_string));
  68. lex_data->strptr = lex_data->cache_string[lex_data->cache_used] = HeapAlloc(GetProcessHeap(), 0, strlen(lex_data->macroptr) + 1);
  69. yylval.string = lex_data->strptr;
  70. lex_data->cache_used++;
  71. BEGIN(quote);
  72. }
  73. else *lex_data->strptr++ = yytext[0];
  74. lex_data->quote_stack[lex_data->quote_stk_idx++] = yytext[0];
  75. assert(lex_data->quote_stk_idx < ARRAY_SIZE(lex_data->quote_stack));
  76. }
  77. else
  78. {
  79. if (yytext[0] == '`') assert(0);
  80. /* close the current quote */
  81. if (--lex_data->quote_stk_idx == 0)
  82. {
  83. BEGIN INITIAL;
  84. *lex_data->strptr++ = '\0';
  85. return STRING;
  86. }
  87. else *lex_data->strptr++ = yytext[0];
  88. }
  89. }
  90. <quote>. *lex_data->strptr++ = yytext[0];
  91. <quote>\\. *lex_data->strptr++ = yytext[1];
  92. <quote><<EOF>> return 0;
  93. " "
  94. . return yytext[0];
  95. %%
  96. #if 0
  97. /* all code for testing macros */
  98. #include "winhelp.h"
  99. static CHAR szTestMacro[256];
  100. static LRESULT CALLBACK MACRO_TestDialogProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  101. {
  102. if (msg == WM_COMMAND && wParam == IDOK)
  103. {
  104. GetDlgItemText(hDlg, 99, szTestMacro, sizeof(szTestMacro));
  105. EndDialog(hDlg, IDOK);
  106. return TRUE;
  107. }
  108. return FALSE;
  109. }
  110. void macro_test(void)
  111. {
  112. WNDPROC lpfnDlg = MakeProcInstance(MACRO_TestDialogProc, Globals.hInstance);
  113. DialogBox(Globals.hInstance, STRING_DIALOG_TEST, Globals.active_win->hMainWnd, (DLGPROC)lpfnDlg);
  114. FreeProcInstance(lpfnDlg);
  115. macro = szTestMacro;
  116. }
  117. #endif
  118. /* small helper function for debug messages */
  119. static const char* ts(int t)
  120. {
  121. static char c[2] = {0,0};
  122. switch (t)
  123. {
  124. case EMPTY: return "EMPTY";
  125. case VOID_FUNCTION: return "VOID_FUNCTION";
  126. case BOOL_FUNCTION: return "BOOL_FUNCTION";
  127. case INTEGER: return "INTEGER";
  128. case STRING: return "STRING";
  129. case IDENTIFIER: return "IDENTIFIER";
  130. default: c[0] = (char)t; return c;
  131. }
  132. }
  133. static int MACRO_CallBoolFunc(void *fn, const char* args, void** ret);
  134. /******************************************************************
  135. * MACRO_CheckArgs
  136. *
  137. * checks number of arguments against prototype, and stores arguments on
  138. * stack pa for later call
  139. * returns -1 on error, otherwise the number of pushed parameters
  140. */
  141. static int MACRO_CheckArgs(void* pa[], unsigned max, const char* args)
  142. {
  143. int t;
  144. unsigned int len = 0, idx = 0;
  145. WINE_TRACE("Checking %s\n", debugstr_a(args));
  146. if (yylex() != '(') {WINE_WARN("missing (\n");return -1;}
  147. if (*args)
  148. {
  149. len = strlen(args);
  150. for (;;)
  151. {
  152. t = yylex();
  153. WINE_TRACE("Got %s <=> %c\n", debugstr_a(ts(t)), *args);
  154. switch (*args)
  155. {
  156. case 'S':
  157. if (t != STRING)
  158. {WINE_WARN("missing S\n");return -1;}
  159. pa[idx] = (void*)yylval.string;
  160. break;
  161. case 'U':
  162. case 'I':
  163. if (t != INTEGER)
  164. {WINE_WARN("missing U\n");return -1;}
  165. pa[idx] = LongToPtr(yylval.integer);
  166. break;
  167. case 'B':
  168. if (t != BOOL_FUNCTION)
  169. {WINE_WARN("missing B\n");return -1;}
  170. if (MACRO_CallBoolFunc(yylval.function, yylval.proto, &pa[idx]) == 0)
  171. return -1;
  172. break;
  173. default:
  174. WINE_WARN("unexpected %s while args is %c\n", debugstr_a(ts(t)), *args);
  175. return -1;
  176. }
  177. idx++;
  178. if (*++args == '\0') break;
  179. t = yylex();
  180. if (t == ')') goto CheckArgs_end;
  181. if (t != ',') {WINE_WARN("missing ,\n");return -1;}
  182. if (idx >= max) {WINE_FIXME("stack overflow (%d)\n", max);return -1;}
  183. }
  184. }
  185. if (yylex() != ')') {WINE_WARN("missing )\n");return -1;}
  186. CheckArgs_end:
  187. while (len > idx) pa[--len] = NULL;
  188. return idx;
  189. }
  190. /******************************************************************
  191. * MACRO_CallBoolFunc
  192. *
  193. * Invokes boolean function fn, which arguments are defined by args
  194. * stores bool result into ret
  195. */
  196. static int MACRO_CallBoolFunc(void *fn, const char* args, void** ret)
  197. {
  198. void* pa[2];
  199. int idx = MACRO_CheckArgs(pa, ARRAY_SIZE(pa), args);
  200. if (idx < 0) return 0;
  201. if (!fn) return 1;
  202. WINE_TRACE("calling with %u pmts\n", idx);
  203. switch (strlen(args))
  204. {
  205. case 0:
  206. {
  207. BOOL (WINAPI *func)(void) = fn;
  208. *ret = (void *)(ULONG_PTR)func();
  209. break;
  210. }
  211. case 1:
  212. {
  213. BOOL (WINAPI *func)(void *) = fn;
  214. *ret = (void *)(ULONG_PTR)func( pa[0]);
  215. break;
  216. }
  217. default: WINE_FIXME("NIY\n");
  218. }
  219. return 1;
  220. }
  221. /******************************************************************
  222. * MACRO_CallVoidFunc
  223. *
  224. *
  225. */
  226. static int MACRO_CallVoidFunc(void *fn, const char* args)
  227. {
  228. void* pa[6];
  229. int idx = MACRO_CheckArgs(pa, ARRAY_SIZE(pa), args);
  230. if (idx < 0) return 0;
  231. if (!fn) return 1;
  232. WINE_TRACE("calling %p with %u pmts\n", fn, idx);
  233. switch (strlen(args))
  234. {
  235. case 0:
  236. {
  237. void (WINAPI *func)(void) = fn;
  238. func();
  239. break;
  240. }
  241. case 1:
  242. {
  243. void (WINAPI *func)(void*) = fn;
  244. func( pa[0] );
  245. break;
  246. }
  247. case 2:
  248. {
  249. void (WINAPI *func)(void*,void*) = fn;
  250. func( pa[0], pa[1] );
  251. break;
  252. }
  253. case 3:
  254. {
  255. void (WINAPI *func)(void*,void*,void*) = fn;
  256. func( pa[0], pa[1], pa[2] );
  257. break;
  258. }
  259. case 4:
  260. {
  261. void (WINAPI *func)(void*,void*,void*,void*) = fn;
  262. func( pa[0], pa[1], pa[2], pa[3] );
  263. break;
  264. }
  265. case 5:
  266. {
  267. void (WINAPI *func)(void*,void*,void*,void*,void*) = fn;
  268. func( pa[0], pa[1], pa[2], pa[3], pa[4] );
  269. break;
  270. }
  271. case 6:
  272. {
  273. void (WINAPI *func)(void*,void*,void*,void*,void*,void*) = fn;
  274. func( pa[0], pa[1], pa[2], pa[3], pa[4], pa[5] );
  275. break;
  276. }
  277. default: WINE_FIXME("NIY\n");
  278. }
  279. return 1;
  280. }
  281. BOOL MACRO_ExecuteMacro(WINHELP_WINDOW* window, LPCSTR macro)
  282. {
  283. struct lex_data curr_lex_data, *prev_lex_data;
  284. BOOL ret = TRUE;
  285. int t;
  286. WINE_TRACE("%s\n", debugstr_a(macro));
  287. prev_lex_data = lex_data;
  288. lex_data = &curr_lex_data;
  289. memset(lex_data, 0, sizeof(*lex_data));
  290. lex_data->macroptr = macro;
  291. lex_data->window = WINHELP_GrabWindow(window);
  292. while ((t = yylex()) != EMPTY)
  293. {
  294. switch (t)
  295. {
  296. case VOID_FUNCTION:
  297. WINE_TRACE("got type void func(%s)\n", debugstr_a(yylval.proto));
  298. MACRO_CallVoidFunc(yylval.function, yylval.proto);
  299. break;
  300. case BOOL_FUNCTION:
  301. WINE_WARN("got type bool func(%s)\n", debugstr_a(yylval.proto));
  302. break;
  303. default:
  304. WINE_WARN("got unexpected type %s\n", debugstr_a(ts(t)));
  305. YY_FLUSH_BUFFER;
  306. ret = FALSE;
  307. goto done;
  308. }
  309. switch (t = yylex())
  310. {
  311. case EMPTY: goto done;
  312. case ';': break;
  313. default: ret = FALSE; YY_FLUSH_BUFFER; goto done;
  314. }
  315. }
  316. done:
  317. for (t = 0; t < lex_data->cache_used; t++)
  318. HeapFree(GetProcessHeap(), 0, lex_data->cache_string[t]);
  319. lex_data = prev_lex_data;
  320. WINHELP_ReleaseWindow(window);
  321. return ret;
  322. }
  323. WINHELP_WINDOW* MACRO_CurrentWindow(void)
  324. {
  325. return lex_data ? lex_data->window : Globals.active_win;
  326. }
  327. #ifndef yywrap
  328. int yywrap(void) { return 1; }
  329. #endif