From 266b5e3891a637dda5a67abefe2fe6484d2b8bad Mon Sep 17 00:00:00 2001 From: Bart House Date: Sat, 15 Dec 2018 14:49:59 -0800 Subject: [PATCH] Small improvements to fuzzer for NetHackW. Can toggle fuzzer on/off using "Pause" key if attached to debugger. Extended command selected randomly. --- include/func_tab.h | 1 + src/cmd.c | 2 ++ win/win32/mhdlg.c | 9 ++++++ win/win32/mhmain.c | 19 +++++++++--- win/win32/mhmenu.c | 43 +++++++++++++++++++------ win/win32/mhtext.c | 45 ++++++++++++++++----------- win/win32/mswproc.c | 76 +++++++++++++++++++++++---------------------- 7 files changed, 125 insertions(+), 70 deletions(-) diff --git a/include/func_tab.h b/include/func_tab.h index 79daef1de..9747253e1 100644 --- a/include/func_tab.h +++ b/include/func_tab.h @@ -22,5 +22,6 @@ struct ext_func_tab { }; extern struct ext_func_tab extcmdlist[]; +extern int extcmdlist_length; #endif /* FUNC_TAB_H */ diff --git a/src/cmd.c b/src/cmd.c index 2049aec5a..1c66ec439 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -3406,6 +3406,8 @@ struct ext_func_tab extcmdlist[] = { { '\0', (char *) 0, (char *) 0, donull, 0, (char *) 0 } /* sentinel */ }; +int extcmdlist_length = SIZE(extcmdlist) - 1; + const char * key2extcmddesc(key) uchar key; diff --git a/win/win32/mhdlg.c b/win/win32/mhdlg.c index bb92d5318..34d5ce299 100644 --- a/win/win32/mhdlg.c +++ b/win/win32/mhdlg.c @@ -167,6 +167,15 @@ INT_PTR CALLBACK ExtCmdDlgProc(HWND, UINT, WPARAM, LPARAM); int mswin_ext_cmd_window(int *selection) { + if (iflags.debug_fuzzer) { + *selection = rn2(extcmdlist_length + 1) - 1; + + if (*selection != -1) + return IDOK; + else + return IDCANCEL; + } + INT_PTR ret; struct extcmd_data data; diff --git a/win/win32/mhmain.c b/win/win32/mhmain.c index a8742b03f..e775f12a7 100644 --- a/win/win32/mhmain.c +++ b/win/win32/mhmain.c @@ -195,7 +195,7 @@ static const char scanmap[] = { LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { - PNHMainWindow data; + PNHMainWindow data = (PNHMainWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA); switch (message) { case WM_CREATE: @@ -225,7 +225,6 @@ MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) break; case WM_KEYDOWN: { - data = (PNHMainWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA); /* translate arrow keys into nethack commands */ switch (wParam) { @@ -332,6 +331,15 @@ MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) NHEVENT_KBD(KEYTABLE(KEY_PLUS)); return 0; +#if defined(DEBUG) && defined(_MSC_VER) + case VK_PAUSE: + if (IsDebuggerPresent()) { + iflags.debug_fuzzer = !iflags.debug_fuzzer; + return 0; + } + break; +#endif + case VK_CLEAR: /* This is the '5' key */ NHEVENT_KBD(KEYTABLE(KEY_GOINTERESTING)); return 0; @@ -530,9 +538,10 @@ onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) child = GetNHApp()->windowlist[msg_param->wid].win; } break; - case MSNH_MSG_RANDOM_INPUT: - nhassert(0); // unexpected - break; + case MSNH_MSG_RANDOM_INPUT: { + nhassert(iflags.debug_fuzzer); + NHEVENT_KBD(randomkey()); + } break; } } diff --git a/win/win32/mhmenu.c b/win/win32/mhmenu.c index 34fe28a1a..62e78480f 100644 --- a/win/win32/mhmenu.c +++ b/win/win32/mhmenu.c @@ -689,14 +689,8 @@ onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) } break; case MSNH_MSG_RANDOM_INPUT: { - char c = randomkey(); - if (c == '\n') - PostMessage(hWnd, WM_COMMAND, MAKELONG(IDOK, 0), 0); - else if (c == '\033') - PostMessage(hWnd, WM_COMMAND, MAKELONG(IDCANCEL, 0), 0); - else - PostMessage(GetDlgItem(hWnd, IDC_TEXT_CONTROL), WM_CHAR, c, 0); - + PostMessage(GetMenuControl(hWnd), + WM_MSNH_COMMAND, MSNH_MSG_RANDOM_INPUT, 0); } break; } @@ -1601,6 +1595,7 @@ reset_menu_count(HWND hwndList, PNHMenuWindow data) LRESULT CALLBACK NHMenuListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { + HWND hWndParent = GetParent(hWnd); BOOL bUpdateFocusItem; /* we will redraw focused item whenever horizontal scrolling occurs @@ -1632,6 +1627,20 @@ NHMenuListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) SetFocus(GetNHApp()->hMainWnd); } return FALSE; + + case WM_MSNH_COMMAND: + if (wParam == MSNH_MSG_RANDOM_INPUT) { + char c = randomkey(); + if (c == '\n') + PostMessage(hWndParent, WM_COMMAND, MAKELONG(IDOK, 0), 0); + else if (c == '\033') + PostMessage(hWndParent, WM_COMMAND, MAKELONG(IDCANCEL, 0), 0); + else + PostMessage(hWnd, WM_CHAR, c, 0); + return 0; + } + break; + } /* update focused item */ @@ -1659,6 +1668,7 @@ NHMenuListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) LRESULT CALLBACK NHMenuTextWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { + HWND hWndParent = GetParent(hWnd); HDC hDC; RECT rc; @@ -1686,8 +1696,7 @@ NHMenuTextWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) && (si.nPos + (int) si.nPage) <= (si.nMax - si.nMin)) SendMessage(hWnd, EM_SCROLL, SB_PAGEDOWN, 0); else - PostMessage(GetParent(hWnd), WM_COMMAND, MAKELONG(IDOK, 0), - 0); + PostMessage(hWndParent, WM_COMMAND, MAKELONG(IDOK, 0), 0); return 0; } case VK_NEXT: @@ -1726,6 +1735,20 @@ NHMenuTextWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) case WM_SETFOCUS: HideCaret(hWnd); return 0; + + case WM_MSNH_COMMAND: + if (wParam == MSNH_MSG_RANDOM_INPUT) { + char c = randomkey(); + if (c == '\n') + PostMessage(hWndParent, WM_COMMAND, MAKELONG(IDOK, 0), 0); + else if (c == '\033') + PostMessage(hWndParent, WM_COMMAND, MAKELONG(IDCANCEL, 0), 0); + else + PostMessage(hWnd, WM_CHAR, c, 0); + return 0; + } + break; + } if (editControlWndProc) diff --git a/win/win32/mhtext.c b/win/win32/mhtext.c index 14092d52f..0ff816f8a 100644 --- a/win/win32/mhtext.c +++ b/win/win32/mhtext.c @@ -27,7 +27,6 @@ HWND mswin_init_text_window() { HWND ret; - PNHTextWindow data; RECT rt; /* get window position */ @@ -52,13 +51,6 @@ mswin_init_text_window() /* Set window caption */ SetWindowText(ret, "Text"); - /* create and set window data */ - data = (PNHTextWindow) malloc(sizeof(NHTextWindow)); - if (!data) - panic("out of memory"); - ZeroMemory(data, sizeof(NHTextWindow)); - SetWindowLongPtr(ret, GWLP_USERDATA, (LONG_PTR) data); - mswin_apply_window_style(ret); return ret; @@ -88,6 +80,12 @@ NHTextWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) switch (message) { case WM_INITDIALOG: { + data = (PNHTextWindow)malloc(sizeof(NHTextWindow)); + if (!data) + panic("out of memory"); + ZeroMemory(data, sizeof(NHTextWindow)); + SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)data); + HWND control = GetDlgItem(hWnd, IDC_TEXT_CONTROL); HDC hdc = GetDC(control); cached_font * font = mswin_get_font(NHW_TEXT, ATR_NONE, hdc, FALSE); @@ -177,7 +175,10 @@ NHTextWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0); } break; + } + + return FALSE; } @@ -214,14 +215,8 @@ onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam) } case MSNH_MSG_RANDOM_INPUT: { - char c = randomkey(); - if (c == '\n') - PostMessage(hWnd, WM_COMMAND, MAKELONG(IDOK, 0), 0); - else if (c == '\033') - PostMessage(hWnd, WM_COMMAND, MAKELONG(IDCANCEL, 0), 0); - else - PostMessage(GetDlgItem(hWnd, IDC_TEXT_CONTROL), WM_CHAR, c, 0); - + PostMessage(GetDlgItem(hWnd, IDC_TEXT_CONTROL), + WM_MSNH_COMMAND, MSNH_MSG_RANDOM_INPUT, 0); } break; @@ -278,6 +273,7 @@ LayoutText(HWND hWnd) LRESULT CALLBACK NHEditHookWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { + HWND hWndParent = GetParent(hWnd); HDC hDC; RECT rc; @@ -305,8 +301,7 @@ NHEditHookWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) && (si.nPos + (int) si.nPage) <= (si.nMax - si.nMin)) SendMessage(hWnd, EM_SCROLL, SB_PAGEDOWN, 0); else - PostMessage(GetParent(hWnd), WM_COMMAND, MAKELONG(IDOK, 0), - 0); + PostMessage(hWndParent, WM_COMMAND, MAKELONG(IDOK, 0), 0); return 0; } case VK_NEXT: @@ -346,6 +341,20 @@ NHEditHookWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) case WM_SETFOCUS: HideCaret(hWnd); return 0; + + case WM_MSNH_COMMAND: + if (wParam == MSNH_MSG_RANDOM_INPUT) { + char c = randomkey(); + if (c == '\n') + PostMessage(hWndParent, WM_COMMAND, MAKELONG(IDOK, 0), 0); + else if (c == '\033') + PostMessage(hWndParent, WM_COMMAND, MAKELONG(IDCANCEL, 0), 0); + else + PostMessage(hWnd, WM_CHAR, c, 0); + return 0; + } + break; + } if (editControlWndProc) diff --git a/win/win32/mswproc.c b/win/win32/mswproc.c index 0a6cde13d..8393ea3e0 100644 --- a/win/win32/mswproc.c +++ b/win/win32/mswproc.c @@ -2102,24 +2102,25 @@ mswin_main_loop() { MSG msg; - while (!mswin_have_input()) { - if (!iflags.debug_fuzzer || PeekMessage(&msg, NULL, 0, 0, FALSE)) { - if(GetMessage(&msg, NULL, 0, 0) != 0) { - if (GetNHApp()->regNetHackMode - || !TranslateAccelerator(msg.hwnd, GetNHApp()->hAccelTable, - &msg)) { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - } else { - /* WM_QUIT */ - break; - } - } else { - nhassert(iflags.debug_fuzzer); - NHEVENT_KBD(randomkey()); - } - } + while (!mswin_have_input()) { + if (!iflags.debug_fuzzer || PeekMessage(&msg, NULL, 0, 0, FALSE)) { + if(GetMessage(&msg, NULL, 0, 0) != 0) { + if (GetNHApp()->regNetHackMode + || !TranslateAccelerator(msg.hwnd, GetNHApp()->hAccelTable, + &msg)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } else { + /* WM_QUIT */ + break; + } + } else { + nhassert(iflags.debug_fuzzer); + PostMessage(GetNHApp()->hMainWnd, WM_MSNH_COMMAND, + MSNH_MSG_RANDOM_INPUT, 0); + } + } } /* clean up and quit */ @@ -2232,25 +2233,26 @@ mswin_popup_display(HWND hWnd, int *done_indicator) SetFocus(hWnd); /* go into message loop */ - while (IsWindow(hWnd) && (done_indicator == NULL || !*done_indicator)) { - if (!iflags.debug_fuzzer || PeekMessage(&msg, NULL, 0, 0, FALSE)) { - if(GetMessage(&msg, NULL, 0, 0) != 0) { - if (!IsDialogMessage(hWnd, &msg)) { - if (!TranslateAccelerator(msg.hwnd, GetNHApp()->hAccelTable, - &msg)) { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - } - } else { - /* WM_QUIT */ - break; - } - } else { - nhassert(iflags.debug_fuzzer); - PostMessage(hWnd, WM_MSNH_COMMAND, MSNH_MSG_RANDOM_INPUT, 0); - } - } + while (IsWindow(hWnd) && (done_indicator == NULL || !*done_indicator)) { + if (!iflags.debug_fuzzer || PeekMessage(&msg, NULL, 0, 0, FALSE)) { + if(GetMessage(&msg, NULL, 0, 0) != 0) { + if (msg.message == WM_MSNH_COMMAND || + !IsDialogMessage(hWnd, &msg)) { + if (!TranslateAccelerator(msg.hwnd, + GetNHApp()->hAccelTable, &msg)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } + } else { + /* WM_QUIT */ + break; + } + } else { + nhassert(iflags.debug_fuzzer); + PostMessage(hWnd, WM_MSNH_COMMAND, MSNH_MSG_RANDOM_INPUT, 0); + } + } } void