The Windows HANDLE type, despite being a `void *', does not actually
[u/mdw/putty] / windows / window.c
index a21c4ed..d58111f 100644 (file)
@@ -7,6 +7,7 @@
 #include <stdlib.h>
 #include <ctype.h>
 #include <time.h>
+#include <limits.h>
 #include <assert.h>
 
 #define PUTTY_DO_GLOBALS              /* actually _define_ globals */
@@ -114,7 +115,7 @@ static Backend *back;
 static void *backhandle;
 
 static struct unicode_data ucsdata;
-static int session_closed;
+static int must_close_session, session_closed;
 static int reconfiguring = FALSE;
 
 static const struct telnet_special *specials = NULL;
@@ -234,7 +235,7 @@ static void start_backend(void)
     if (error) {
        char *str = dupprintf("%s Error", appname);
        sprintf(msg, "Unable to open connection to\n"
-               "%.800s\n" "%s", cfg.host, error);
+               "%.800s\n" "%s", cfg_dest(&cfg), error);
        MessageBox(NULL, msg, str, MB_ICONERROR | MB_OK);
        sfree(str);
        exit(0);
@@ -408,7 +409,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
                i--;
            p[i] = '\0';
            do_defaults(p + 1, &cfg);
-           if (!*cfg.host && !do_config()) {
+           if (!cfg_launchable(&cfg) && !do_config()) {
                cleanup_exit(0);
            }
        } else if (*p == '&') {
@@ -553,7 +554,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
 
        cmdline_run_saved(&cfg);
 
-       if (!*cfg.host && !do_config()) {
+       if (!cfg_launchable(&cfg) && !do_config()) {
            cleanup_exit(0);
        }
 
@@ -828,14 +829,14 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
        if ((unsigned)(n - WAIT_OBJECT_0) < (unsigned)nhandles) {
            handle_got_event(handles[n - WAIT_OBJECT_0]);
            sfree(handles);
+           if (must_close_session)
+               close_session();
            continue;
        }
 
        sfree(handles);
 
-       if (GetMessage(&msg, NULL, 0, 0) != 1)
-           break;
-       do {
+       while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
            if (msg.message == WM_QUIT)
                goto finished;         /* two-level break */
 
@@ -847,7 +848,9 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
             * we've delayed, reading the socket, writing, and repainting
             * the window.
             */
-       } while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE));
+           if (must_close_session)
+               close_session();
+       }
 
        /* The messages seem unreliable; especially if we're being tricky */
        term_set_focus(term, GetForegroundWindow() == hwnd);
@@ -1069,7 +1072,7 @@ void connection_fatal(void *frontend, char *fmt, ...)
     if (cfg.close_on_exit == FORCE_ON)
        PostQuitMessage(1);
     else {
-       close_session();
+       must_close_session = TRUE;
     }
 }
 
@@ -1097,27 +1100,13 @@ static void enact_netevent(WPARAM wParam, LPARAM lParam)
 {
     static int reentering = 0;
     extern int select_result(WPARAM, LPARAM);
-    int ret;
 
     if (reentering)
        return;                        /* don't unpend the pending */
 
     reentering = 1;
-    ret = select_result(wParam, lParam);
+    select_result(wParam, lParam);
     reentering = 0;
-
-    if (ret == 0 && !session_closed) {
-       /* Abnormal exits will already have set session_closed and taken
-        * appropriate action. */
-       if (cfg.close_on_exit == FORCE_ON ||
-           cfg.close_on_exit == AUTO) PostQuitMessage(0);
-       else {
-           close_session();
-           session_closed = TRUE;
-           MessageBox(hwnd, "Connection closed by remote host",
-                      appname, MB_OK | MB_ICONINFORMATION);
-       }
-    }
 }
 
 /*
@@ -1862,7 +1851,29 @@ static int is_shift_pressed(void)
 
 static int resizing;
 
-void notify_remote_exit(void *fe) { /* stub not needed in this frontend */ }
+void notify_remote_exit(void *fe)
+{
+    int exitcode;
+
+    if (!session_closed &&
+        (exitcode = back->exitcode(backhandle)) >= 0) {
+       /* Abnormal exits will already have set session_closed and taken
+        * appropriate action. */
+       if (cfg.close_on_exit == FORCE_ON ||
+           (cfg.close_on_exit == AUTO && exitcode != INT_MAX)) {
+           PostQuitMessage(0);
+       } else {
+           must_close_session = TRUE;
+           session_closed = TRUE;
+           /* exitcode == INT_MAX indicates that the connection was closed
+            * by a fatal error, so an error box will be coming our way and
+            * we should not generate this informational one. */
+           if (exitcode != INT_MAX)
+               MessageBox(hwnd, "Connection closed by remote host",
+                          appname, MB_OK | MB_ICONINFORMATION);
+       }
+    }
+}
 
 void timer_change_notify(long next)
 {
@@ -1953,11 +1964,11 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
                    sa.nLength = sizeof(sa);
                    sa.lpSecurityDescriptor = NULL;
                    sa.bInheritHandle = TRUE;
-                   filemap = CreateFileMapping((HANDLE) 0xFFFFFFFF,
+                   filemap = CreateFileMapping(INVALID_HANDLE_VALUE,
                                                &sa,
                                                PAGE_READWRITE,
                                                0, sizeof(Config), NULL);
-                   if (filemap) {
+                   if (filemap && filemap != INVALID_HANDLE_VALUE) {
                        p = (Config *) MapViewOfFile(filemap,
                                                     FILE_MAP_WRITE,
                                                     0, 0, sizeof(Config));