]> sjero.net Git - wget/blob - src/mswindows.c
[svn] Logging system bugfixes and improvements.
[wget] / src / mswindows.c
1 /* mswindows.c -- Windows-specific support
2    Copyright (C) 1995, 1996, 1997, 1998  Free Software Foundation, Inc.
3
4 This file is part of GNU Wget.
5
6 GNU Wget is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 GNU Wget is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 /* #### Someone please document what these functions do!  */
21
22 #include <config.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <winsock.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <errno.h>
30
31 #include "wget.h"
32 #include "url.h"
33
34 #ifndef errno
35 extern int errno;
36 #endif
37
38 /* Defined in log.c.  */
39 void log_request_redirect_output PARAMS ((const char *));
40
41 static int windows_nt_p;
42
43
44 /* Emulation of Unix sleep.  */
45
46 unsigned int
47 sleep (unsigned seconds)
48 {
49   return SleepEx (1000 * seconds, TRUE) ? 0U : 1000 * seconds;
50 }
51
52 /* Emulation of Unix usleep().  This has a granularity of
53    milliseconds, but that's ok because:
54
55    a) Wget is only using it with milliseconds;
56
57    b) You can't rely on usleep's granularity anyway.  If a caller
58    expects usleep to respect every microsecond, he's in for a
59    surprise.  */
60
61 int
62 usleep (unsigned long usec)
63 {
64   SleepEx (usec / 1000, TRUE);
65   return 0;
66 }
67
68 static char *
69 read_registry (HKEY hkey, char *subkey, char *valuename, char *buf, int *len)
70 {
71   HKEY result;
72   DWORD size = *len;
73   DWORD type = REG_SZ;
74   if (RegOpenKeyEx (hkey, subkey, NULL, KEY_READ, &result) != ERROR_SUCCESS)
75     return NULL;
76   if (RegQueryValueEx (result, valuename, NULL, &type, buf, &size) != ERROR_SUCCESS)
77     buf = NULL;
78   *len = size;
79   RegCloseKey (result);
80   return buf;
81 }
82
83 void
84 windows_main_junk (int *argc, char **argv, char **exec_name)
85 {
86   char *p;
87
88   /* Remove .EXE from filename if it has one.  */
89   *exec_name = xstrdup (*exec_name);
90   p = strrchr (*exec_name, '.');
91   if (p)
92     *p = '\0';
93 }
94 \f
95 /* Winsock stuff. */
96
97 static void
98 ws_cleanup (void)
99 {
100   WSACleanup ();
101 }
102
103 static void
104 ws_hangup (void)
105 {
106   log_request_redirect_output ("CTRL+Break");
107 }
108
109 void
110 fork_to_background (void)
111 {
112   /* Whether we arrange our own version of opt.lfilename here.  */
113   int changedp = 0;
114
115   if (!opt.lfilename)
116     {
117       opt.lfilename = unique_name (DEFAULT_LOGFILE);
118       changedp = 1;
119     }
120   printf (_("Continuing in background.\n"));
121   if (changedp)
122     printf (_("Output will be written to `%s'.\n"), opt.lfilename);
123
124   ws_hangup ();
125   if (!windows_nt_p)
126     FreeConsole ();
127 }
128
129 static BOOL WINAPI
130 ws_handler (DWORD dwEvent)
131 {
132   switch (dwEvent)
133     {
134 #ifdef CTRLC_BACKGND
135     case CTRL_C_EVENT:
136 #endif
137 #ifdef CTRLBREAK_BACKGND
138     case CTRL_BREAK_EVENT:
139 #endif
140       fork_to_background ();
141       break;
142     case CTRL_SHUTDOWN_EVENT:
143     case CTRL_CLOSE_EVENT:
144     case CTRL_LOGOFF_EVENT:
145     default:
146       WSACleanup ();
147       return FALSE;
148     }
149   return TRUE;
150 }
151
152 void
153 ws_changetitle (char *url, int nurl)
154 {
155   char *title_buf;
156   if (!nurl)
157     return;
158
159   title_buf = (char *)alloca (strlen (url) + 20);
160   sprintf (title_buf, "Wget %s%s", url, nurl == 1 ? "" : " ...");
161   SetConsoleTitle (title_buf);
162 }
163
164 char *
165 ws_mypath (void)
166 {
167   static char *wspathsave;
168   char buffer[MAX_PATH];
169   char *ptr;
170
171   if (wspathsave)
172     {
173       return wspathsave;
174     }
175
176   GetModuleFileName (NULL, buffer, MAX_PATH);
177
178   ptr = strrchr (buffer, '\\');
179   if (ptr)
180     {
181       *(ptr + 1) = '\0';
182       wspathsave = (char*) xmalloc (strlen (buffer) + 1);
183       strcpy (wspathsave, buffer);
184     }
185   else
186     wspathsave = NULL;
187   return wspathsave;
188 }
189
190 void
191 ws_help (const char *name)
192 {
193   char *mypath = ws_mypath ();
194
195   if (mypath)
196     {
197       struct stat sbuf;
198       char *buf = (char *)alloca (strlen (mypath) + strlen (name) + 4 + 1);
199       sprintf (buf, "%s%s.HLP", mypath, name);
200       if (stat (buf, &sbuf) == 0) 
201         {
202           printf (_("Starting WinHelp %s\n"), buf);
203           WinHelp (NULL, buf, HELP_INDEX, NULL);
204         }
205       else
206         {
207           printf ("%s: %s\n", buf, strerror (errno));
208         }
209     }
210 }
211
212 void
213 ws_startup (void)
214 {
215   WORD requested;
216   WSADATA data;
217   int err;
218   OSVERSIONINFO os;
219
220   if (GetVersionEx (&os) == TRUE
221       && os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
222     windows_nt_p = 1;
223
224   requested = MAKEWORD (1, 1);
225   err = WSAStartup (requested, &data);
226
227   if (err != 0)
228     {
229       fprintf (stderr, _("%s: Couldn't find usable socket driver.\n"),
230                exec_name);
231       exit (1);
232     }
233
234   if (data.wVersion < requested)
235     {
236       fprintf (stderr, _("%s: Couldn't find usable socket driver.\n"),
237                exec_name);
238       WSACleanup ();
239       exit (1);
240     }
241   atexit (ws_cleanup);
242   SetConsoleCtrlHandler (ws_handler, TRUE);
243 }