]> sjero.net Git - wget/blob - src/mswindows.c
[svn] Update copyright notices.
[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 redirect_output (const char *);
40
41 static int windows_nt_p;
42
43
44 /* Emulation of Unix sleep.  */
45 unsigned int
46 sleep (unsigned seconds)
47 {
48   return SleepEx (1000 * seconds, TRUE) ? 0U : 1000 * seconds;
49 }
50
51 static char *
52 read_registry (HKEY hkey, char *subkey, char *valuename, char *buf, int *len)
53 {
54   HKEY result;
55   DWORD size = *len;
56   DWORD type = REG_SZ;
57   if (RegOpenKeyEx (hkey, subkey, NULL, KEY_READ, &result) != ERROR_SUCCESS)
58     return NULL;
59   if (RegQueryValueEx (result, valuename, NULL, &type, buf, &size) != ERROR_SUCCESS)
60     buf = NULL;
61   *len = size;
62   RegCloseKey (result);
63   return buf;
64 }
65
66 void
67 windows_main_junk (int *argc, char **argv, char **exec_name)
68 {
69   char *p;
70
71   /* Remove .EXE from filename if it has one.  */
72   *exec_name = xstrdup (*exec_name);
73   p = strrchr (*exec_name, '.');
74   if (p)
75     *p = '\0';
76 }
77 \f
78 /* Winsock stuff. */
79
80 static void
81 ws_cleanup (void)
82 {
83   WSACleanup ();
84 }
85
86 static void
87 ws_hangup (void)
88 {
89   redirect_output (_("\n\
90 CTRL+Break received, redirecting output to `%s'.\n\
91 Execution continued in background.\n\
92 You may stop Wget by pressing CTRL+ALT+DELETE.\n"));
93 }
94
95 void
96 fork_to_background (void)
97 {
98   /* Whether we arrange our own version of opt.lfilename here.  */
99   int changedp = 0;
100
101   if (!opt.lfilename)
102     {
103       opt.lfilename = unique_name (DEFAULT_LOGFILE);
104       changedp = 1;
105     }
106   printf (_("Continuing in background.\n"));
107   if (changedp)
108     printf (_("Output will be written to `%s'.\n"), opt.lfilename);
109
110   ws_hangup ();
111   if (!windows_nt_p)
112     FreeConsole ();
113 }
114
115 static BOOL WINAPI
116 ws_handler (DWORD dwEvent)
117 {
118   switch (dwEvent)
119     {
120 #ifdef CTRLC_BACKGND
121     case CTRL_C_EVENT:
122 #endif
123 #ifdef CTRLBREAK_BACKGND
124     case CTRL_BREAK_EVENT:
125 #endif
126       fork_to_background ();
127       break;
128     case CTRL_SHUTDOWN_EVENT:
129     case CTRL_CLOSE_EVENT:
130     case CTRL_LOGOFF_EVENT:
131     default:
132       WSACleanup ();
133       return FALSE;
134     }
135   return TRUE;
136 }
137
138 void
139 ws_changetitle (char *url, int nurl)
140 {
141   char *title_buf;
142   if (!nurl)
143     return;
144
145   title_buf = (char *)alloca (strlen (url) + 20);
146   sprintf (title_buf, "Wget %s%s", url, nurl == 1 ? "" : " ...");
147   SetConsoleTitle (title_buf);
148 }
149
150 char *
151 ws_mypath (void)
152 {
153   static char *wspathsave;
154   char buffer[MAX_PATH];
155   char *ptr;
156
157   if (wspathsave)
158     {
159       return wspathsave;
160     }
161
162   GetModuleFileName (NULL, buffer, MAX_PATH);
163
164   ptr = strrchr (buffer, '\\');
165   if (ptr)
166     {
167       *(ptr + 1) = '\0';
168       wspathsave = (char*) xmalloc (strlen (buffer) + 1);
169       strcpy (wspathsave, buffer);
170     }
171   else
172     wspathsave = NULL;
173   return wspathsave;
174 }
175
176 void
177 ws_help (const char *name)
178 {
179   char *mypath = ws_mypath ();
180
181   if (mypath)
182     {
183       struct stat sbuf;
184       char *buf = (char *)alloca (strlen (mypath) + strlen (name) + 4 + 1);
185       sprintf (buf, "%s%s.HLP", mypath, name);
186       if (stat (buf, &sbuf) == 0) 
187         {
188           printf (_("Starting WinHelp %s\n"), buf);
189           WinHelp (NULL, buf, HELP_INDEX, NULL);
190         }
191       else
192         {
193           printf ("%s: %s\n", buf, strerror (errno));
194         }
195     }
196 }
197
198 void
199 ws_startup (void)
200 {
201   WORD requested;
202   WSADATA data;
203   int err;
204   OSVERSIONINFO os;
205
206   if (GetVersionEx (&os) == TRUE
207       && os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
208     windows_nt_p = 1;
209
210   requested = MAKEWORD (1, 1);
211   err = WSAStartup (requested, &data);
212
213   if (err != 0)
214     {
215       fprintf (stderr, _("%s: Couldn't find usable socket driver.\n"),
216                exec_name);
217       exit (1);
218     }
219
220   if (data.wVersion < requested)
221     {
222       fprintf (stderr, _("%s: Couldn't find usable socket driver.\n"),
223                exec_name);
224       WSACleanup ();
225       exit (1);
226     }
227   atexit (ws_cleanup);
228   SetConsoleCtrlHandler (ws_handler, TRUE);
229 }