]> granicus.if.org Git - nethack/commitdiff
Small improvements to fuzzer for NetHackW.
authorBart House <bart@barthouse.com>
Sat, 15 Dec 2018 22:49:59 +0000 (14:49 -0800)
committerBart House <bart@barthouse.com>
Sat, 15 Dec 2018 22:49:59 +0000 (14:49 -0800)
Can toggle fuzzer on/off using "Pause" key if attached to debugger.
Extended command selected randomly.

include/func_tab.h
src/cmd.c
win/win32/mhdlg.c
win/win32/mhmain.c
win/win32/mhmenu.c
win/win32/mhtext.c
win/win32/mswproc.c

index 79daef1de3cce0d8d31bed5ee638909582dcbe61..9747253e11022ef9286b2367380d17b573167192 100644 (file)
@@ -22,5 +22,6 @@ struct ext_func_tab {
 };
 
 extern struct ext_func_tab extcmdlist[];
+extern int extcmdlist_length;
 
 #endif /* FUNC_TAB_H */
index 2049aec5a3ac09cbb1bf58c25c0d77403ca94af1..1c66ec439e0ce909a9e831f8917696b747c1ba93 100644 (file)
--- 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;
index bb92d53187edef15440d6a67742c1fa9b6d088f1..34d5ce29903c385307a7b156879f0009d7cc05f3 100644 (file)
@@ -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;
 
index a8742b03fe126a250cfe0df07a5d42b272deef51..e775f12a79aed25758c9951f94bbe2dfef5aa55d 100644 (file)
@@ -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;
 
     }
 }
index 34fe28a1a9432d2a22d7bc620e1598fbe3fc042e..62e78480f11c1dc85423634cdd8457bf4dacd253 100644 (file)
@@ -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)
index 14092d52f81d2b020811f713896d5bc523309002..0ff816f8a93a660ca2be26dfa789e1078f93758c 100644 (file)
@@ -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)
index 0a6cde13d75aa78b64732d6afb741c0b1fe72642..8393ea3e08a22b8c5c7e58a15192e73a70c5a6eb 100644 (file)
@@ -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