[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Bug-wget] [Win32 Patch] console-close events
From: |
Gisle Vanem |
Subject: |
[Bug-wget] [Win32 Patch] console-close events |
Date: |
Mon, 9 Jun 2014 16:59:08 +0200 |
On Windows, when closing the console, logging off or shutting down the
system, the OS sends the proper events to registered console-handlers.
Wget has a ws_handler() in mswindows.c that can handle these events
to cleanup it's tracks. This patch doesn't do anything other than cleanup()
may do. It could be used to delete any possibly truncated files. What does
Wget do (if anything) on other platforms? E.g. a ' ^Z' with no 'bg/fg' on Unix?
Here is a simple patch that I use. Feel free to add it:
--- src/mswindows.c.orig 2014-06-07 11:32:11 +0000
+++ src/mswindows.c 2014-06-09 16:24:08 +0000
@@ -42,6 +42,7 @@
#include "utils.h"
#include "url.h"
+#include "init.h"
#ifndef ES_SYSTEM_REQUIRED
#define ES_SYSTEM_REQUIRED 0x00000001
@@ -337,6 +338,17 @@
/* If we get here, we're the child. */
}
+/* Return the name for the console-events we might receive. */
+static const char *ws_event_name (DWORD event)
+{
+ return (event == CTRL_C_EVENT ? "CTRL_C_EVENT" :
+ event == CTRL_BREAK_EVENT ? "CTRL_BREAK_EVENT" :
+ event == CTRL_CLOSE_EVENT ? "CTRL_CLOSE_EVENT" :
+ event == CTRL_LOGOFF_EVENT ? "CTRL_LOGOFF_EVENT" :
+ event == CTRL_SHUTDOWN_EVENT ? "CTRL_SHUTDOWN_EVENT" :
+ "?");
+}
+
static BOOL WINAPI
ws_handler (DWORD dwEvent)
{
@@ -352,6 +364,16 @@
ws_hangup ("CTRL+Break");
return TRUE;
#endif
+ case CTRL_CLOSE_EVENT:
+ case CTRL_LOGOFF_EVENT:
+ case CTRL_SHUTDOWN_EVENT:
+ MessageBeep (MB_OK);
+ logprintf (LOG_NOTQUIET, _("\n%s: Got %s. Performing cleanup.\n"),
+ exec_name, ws_event_name(dwEvent));
+ cleanup();
+ Sleep(500);
+ return TRUE;
+
default:
return FALSE;
}
-----------
Tested with MingW (since I have no MSVC-version of gnulib) and a
CTRL_CLOSE_EVENT:
wget -d -O NUL -o log ftp://ftp.uib.no/pub/users/hmifs/Making%%20rain.m4v
Press X to quit the console. Contents of 'log' afterwards:
...
Length: 409762194 (391M) (unauthoritative)
0K .......... .......... .......... .......... .......... 0% 106K 62m51s
50K .......... .......... .......... .......... .......... 0% 213K 47m5s
100K .......... .......... .......... ........
wget: Got CTRL_CLOSE_EVENT. Performing cleanup.
If I use redirection, the 4NT/CMD shell seems flaky in flushing it's output.
So I feel Wget should be better than any Win-shells.
Ref:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686016(v=vs.85).aspx
PS. The above Sleep() seems to be ignored by WinCon. At least I failed to
make it sleep more than ~500 msec.
--gv
- [Bug-wget] [Win32 Patch] console-close events,
Gisle Vanem <=