iconv: Bail out of the loop when an illegal sequence of bytes occurs.
[elinks/elinks-j605.git] / src / osdep / win32 / win32.c
blob134b8c8bb4b98e858ec1e998fef92fb3e6a5ce2b
1 /* Win32 support fo ELinks. It has pretty different life than rest of ELinks. */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 /* Get SHGFP_TYPE_CURRENT from <shlobj.h>. */
8 #define _WIN32_IE 0x500
10 #include <windows.h>
11 #include <shlobj.h>
13 #include "osdep/system.h"
15 #include <fcntl.h>
16 #include <io.h>
17 #include <process.h>
18 #include <signal.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #ifdef HAVE_LOCALE_H
23 #include <locale.h>
24 #endif
26 #include "elinks.h"
28 #include "main/select.h"
29 #include "osdep/win32/win32.h"
30 #include "osdep/osdep.h"
31 #include "terminal/terminal.h"
34 void
35 init_osdep(void)
37 #ifdef CONFIG_OS_WIN32
38 WSADATA ws;
39 WORD ver = MAKEWORD(1,1);
40 #ifdef CONFIG_IPV6
41 ver = MAKEWORD(2,0);;
42 #endif
43 if (WSAStartup(ver,&ws) != 0) {
44 printf("Failed to initialise Winsock ver %d.%d\n", ver >> 8, ver & 255);
45 exit(-1);
47 #endif
48 #ifdef HAVE_LOCALE_H
49 setlocale(LC_ALL, "");
50 #endif
51 #ifdef CONFIG_IDN
53 char buf[60];
54 UINT cp = GetACP();
56 if (!getenv("CHARSET") && cp > 0)
58 snprintf (buf, sizeof(buf), "CHARSET=cp%u", cp);
59 putenv (buf);
62 #endif
65 void
66 terminate_osdep(void)
71 int
72 get_system_env(void)
74 return (0);
77 void
78 handle_terminal_resize(int fd, void (*fn)())
82 void
83 unhandle_terminal_resize(int fd)
87 void
88 get_terminal_size(int fd, int *x, int *y)
90 CONSOLE_SCREEN_BUFFER_INFO csbi;
92 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
94 if (!*x) {
95 *x = get_e("COLUMNS");
96 if (!*x)
97 *x = csbi.srWindow.Right - csbi.srWindow.Left + 1;
99 if (!*y) {
100 *y = get_e("LINES");
101 if (!*y)
102 *y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
108 exe(unsigned char *path)
110 int rc;
111 unsigned char *shell = get_shell();
112 unsigned char *x = *path != '"' ? " /c start /wait " : " /c start /wait \"\" ";
113 unsigned char *p = malloc((strlen(shell) + strlen(x) + strlen(path)) * 2 + 1);
115 if (!p)
116 return -1;
118 strcpy(p, shell);
119 strcat(p, x);
120 strcat(p, path);
121 x = p;
122 while (*x) {
123 if (*x == '\\') {
124 memmove(x + 1, x, strlen(x) + 1);
125 x++;
127 x++;
129 rc = system(p);
131 free(p);
133 return rc;
138 get_ctl_handle(void)
140 return get_input_handle();
144 #if defined(HAVE_BEGINTHREAD)
146 struct tdata {
147 void (*fn)(void *, int);
148 int h;
149 unsigned char data[1];
152 extern void bgt(struct tdata *t);
155 start_thread(void (*fn)(void *, int), void *ptr, int l)
157 int p[2];
158 struct tdata *t;
160 if (c_pipe(p) < 0)
161 return -1;
163 t = malloc(sizeof(*t) + l);
164 if (!t)
165 return -1;
166 t->fn = fn;
167 t->h = p[1];
168 memcpy(t->data, ptr, l);
169 if (_beginthread((void (*)(void *)) bgt, 65536, t) == -1) {
170 close(p[0]);
171 close(p[1]);
172 mem_free(t);
173 return -1;
176 return p[0];
178 #endif
182 get_input_handle(void)
184 static HANDLE hStdIn = INVALID_HANDLE_VALUE;
186 if (hStdIn == INVALID_HANDLE_VALUE) {
187 DWORD dwMode;
189 SetConsoleTitle("ELinks - Console mode browser");
191 hStdIn = GetStdHandle(STD_INPUT_HANDLE);
192 GetConsoleMode(hStdIn, &dwMode);
193 dwMode &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
194 dwMode |= (ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);
195 SetConsoleMode(hStdIn, dwMode);
197 return (int) hStdIn;
201 get_output_handle(void)
203 static HANDLE hStdOut = INVALID_HANDLE_VALUE;
205 if (hStdOut == INVALID_HANDLE_VALUE)
206 hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
207 return (int) hStdOut;
211 gettimeofday(struct timeval* p, void* tz)
213 union {
214 long long ns100; /*time since 1 Jan 1601 in 100ns units */
215 FILETIME ft;
216 } _now;
218 assertm (p != NULL);
219 GetSystemTimeAsFileTime (&_now.ft);
220 p->tv_usec = (long) ((_now.ns100 / 10LL) % 1000000LL);
221 p->tv_sec = (long) ((_now.ns100 - 116444736000000000LL) / 10000000LL);
222 return 0;
227 mkstemp(char *template)
229 char pathname[MAX_PATH];
231 /* Get the directory for temp files */
232 GetTempPath(MAX_PATH, pathname);
234 /* Create a temporary file. */
235 GetTempFileName(pathname, "ABC", 0, template);
237 return open(template, O_WRONLY | O_BINARY | O_EXCL);
241 tcgetattr(int fd, struct termios *_termios_p)
243 (void) fd;
244 (void) _termios_p;
245 return 0;
249 tcsetattr(int fd, int _optional_actions, const struct termios *_termios_p)
251 (void) fd;
252 (void) _optional_actions;
253 (void) _termios_p;
254 return 0;
257 #ifdef ENABLE_NLS
259 gettext__parse(void *arg)
261 return 0;
263 #endif
265 unsigned char *
266 user_appdata_directory(void)
268 #if _WIN32_WINNT >= 0x0500
269 HWND hwnd = GetConsoleWindow();
270 #else
271 HWND hwnd = NULL;
272 #endif
273 char path[MAX_PATH];
274 HRESULT hr;
276 hr = SHGetFolderPath(hwnd, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, path);
277 if (hr == S_OK) /* Don't even allow S_FALSE. */
278 return stracpy(path);
279 else
280 return NULL;