msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / kernel32 / kernel16.c
blobb90405797caa2c98eb6cc2e8e65359dfc69f58f3
1 /*
2 * 16-bit kernel initialization code
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winternl.h"
27 #include "wownt32.h"
29 #include "toolhelp.h"
30 #include "kernel_private.h"
31 #include "kernel16_private.h"
32 #include "wine/server.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(module);
37 /**************************************************************************
38 * DllEntryPoint (KERNEL.669)
40 BOOL WINAPI KERNEL_DllEntryPoint( DWORD reasion, HINSTANCE16 inst, WORD ds,
41 WORD heap, DWORD reserved1, WORD reserved2 )
43 static int done;
45 /* the entry point can be called multiple times */
46 if (done) return TRUE;
47 done = 1;
49 /* Initialize 16-bit thunking entry points */
50 if (!WOWTHUNK_Init()) return FALSE;
52 /* Initialize DOS memory */
53 if (!DOSMEM_Init()) return FALSE;
55 /* Initialize special KERNEL entry points */
57 NE_SetEntryPoint( inst, 178, GetWinFlags16() );
59 NE_SetEntryPoint( inst, 454, wine_get_cs() );
60 NE_SetEntryPoint( inst, 455, wine_get_ds() );
62 NE_SetEntryPoint( inst, 183, DOSMEM_0000H ); /* KERNEL.183: __0000H */
63 NE_SetEntryPoint( inst, 173, DOSMEM_BiosSysSeg ); /* KERNEL.173: __ROMBIOS */
64 NE_SetEntryPoint( inst, 193, DOSMEM_BiosDataSeg ); /* KERNEL.193: __0040H */
65 NE_SetEntryPoint( inst, 194, DOSMEM_BiosSysSeg ); /* KERNEL.194: __F000H */
67 /* Initialize KERNEL.THHOOK */
68 TASK_InstallTHHook(MapSL((SEGPTR)GetProcAddress16( inst, (LPCSTR)332 )));
70 /* Initialize the real-mode selector entry points */
71 #define SET_ENTRY_POINT( num, addr ) \
72 NE_SetEntryPoint( inst, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
73 DOSMEM_MapDosToLinear(addr), 0x10000, inst, \
74 WINE_LDT_FLAGS_DATA ))
76 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
77 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
78 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
79 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
80 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
81 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
82 #undef SET_ENTRY_POINT
84 /* Force loading of some dlls */
85 LoadLibrary16( "system.drv" );
87 return TRUE;
90 /***********************************************************************
91 * EnableDos (KERNEL.41)
92 * DisableDos (KERNEL.42)
93 * GetLastDiskChange (KERNEL.98)
94 * ValidateCodeSegments (KERNEL.100)
95 * KbdRst (KERNEL.123)
96 * EnableKernel (KERNEL.124)
97 * DisableKernel (KERNEL.125)
98 * ValidateFreeSpaces (KERNEL.200)
99 * K237 (KERNEL.237)
100 * BUNNY_351 (KERNEL.351)
101 * PIGLET_361 (KERNEL.361)
103 * Entry point for kernel functions that do nothing.
105 LONG WINAPI KERNEL_nop(void)
107 return 0;
111 /* thunk for 16-bit CreateThread */
112 struct thread_args
114 FARPROC16 proc;
115 DWORD param;
118 static DWORD CALLBACK start_thread16( LPVOID threadArgs )
120 struct thread_args args = *(struct thread_args *)threadArgs;
121 HeapFree( GetProcessHeap(), 0, threadArgs );
122 return K32WOWCallback16( (DWORD)args.proc, args.param );
125 /***********************************************************************
126 * CreateThread16 (KERNEL.441)
128 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
129 FARPROC16 start, SEGPTR param,
130 DWORD flags, LPDWORD id )
132 struct thread_args *args = HeapAlloc( GetProcessHeap(), 0, sizeof(*args) );
133 if (!args) return INVALID_HANDLE_VALUE;
134 args->proc = start;
135 args->param = param;
136 return CreateThread( sa, stack, start_thread16, args, flags, id );
140 /***********************************************************************
141 * wait_input_idle
143 * user32.WaitForInputIdle releases the win16 lock, so here is a replacement.
145 static DWORD wait_input_idle( HANDLE process, DWORD timeout )
147 DWORD ret;
148 HANDLE handles[2];
150 handles[0] = process;
151 SERVER_START_REQ( get_process_idle_event )
153 req->handle = wine_server_obj_handle(process);
154 if (!(ret = wine_server_call_err( req ))) handles[1] = wine_server_ptr_handle( reply->event );
156 SERVER_END_REQ;
157 if (ret) return WAIT_FAILED; /* error */
158 if (!handles[1]) return 0; /* no event to wait on */
160 return WaitForMultipleObjects( 2, handles, FALSE, timeout );
164 /**************************************************************************
165 * WINOLDAP entry point
167 void WINAPI WINOLDAP_EntryPoint( CONTEXT86 *context )
169 PDB16 *psp;
170 INT len;
171 LPSTR cmdline;
172 PROCESS_INFORMATION info;
173 STARTUPINFOA startup;
174 DWORD count, exit_code = 1;
176 InitTask16( context );
178 TRACE( "(ds=%x es=%x fs=%x gs=%x, bx=%04x cx=%04x di=%04x si=%x)\n",
179 context->SegDs, context->SegEs, context->SegFs, context->SegGs,
180 context->Ebx, context->Ecx, context->Edi, context->Esi );
182 psp = GlobalLock16( context->SegEs );
183 len = psp->cmdLine[0];
184 cmdline = HeapAlloc( GetProcessHeap(), 0, len + 1 );
185 memcpy( cmdline, psp->cmdLine + 1, len );
186 cmdline[len] = 0;
188 memset( &startup, 0, sizeof(startup) );
189 startup.cb = sizeof(startup);
191 if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE,
192 0, NULL, NULL, &startup, &info ))
194 /* Give 10 seconds to the app to come up */
195 if (wait_input_idle( info.hProcess, 10000 ) == WAIT_FAILED)
196 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
197 ReleaseThunkLock( &count );
199 WaitForSingleObject( info.hProcess, INFINITE );
200 GetExitCodeProcess( info.hProcess, &exit_code );
201 CloseHandle( info.hThread );
202 CloseHandle( info.hProcess );
204 else
205 ReleaseThunkLock( &count );
207 HeapFree( GetProcessHeap(), 0, cmdline );
208 ExitThread( exit_code );
212 /**************************************************************************
213 * WINHELP entry point
215 * FIXME: should go into winhlp32.exe, but we don't support 16-bit modules in executables yet.
217 void WINAPI WINHELP_EntryPoint( CONTEXT86 *context )
219 static const WCHAR winhlp32W[] = {'\\','w','i','n','h','l','p','3','2','.','e','x','e',0};
220 PDB16 *psp;
221 INT len, total;
222 WCHAR *cmdline, *p;
223 PROCESS_INFORMATION info;
224 STARTUPINFOW startup;
225 DWORD count, exit_code = 1;
227 InitTask16( context );
229 TRACE( "(ds=%x es=%x fs=%x gs=%x, bx=%04x cx=%04x di=%04x si=%x)\n",
230 context->SegDs, context->SegEs, context->SegFs, context->SegGs,
231 context->Ebx, context->Ecx, context->Edi, context->Esi );
233 psp = GlobalLock16( context->SegEs );
234 len = MultiByteToWideChar( CP_ACP, 0, (char *)psp->cmdLine + 1, psp->cmdLine[0], NULL, 0 );
235 total = (GetSystemDirectoryW( NULL, 0 ) + len + 1) * sizeof(WCHAR) + sizeof(winhlp32W);
236 cmdline = HeapAlloc( GetProcessHeap(), 0, total );
237 GetSystemDirectoryW( cmdline, total );
238 lstrcatW( cmdline, winhlp32W );
239 p = cmdline + lstrlenW(cmdline);
240 if (len)
242 *p++ = ' ';
243 MultiByteToWideChar( CP_ACP, 0, (char *)psp->cmdLine + 1, psp->cmdLine[0], p, len );
244 p[len] = 0;
247 memset( &startup, 0, sizeof(startup) );
248 startup.cb = sizeof(startup);
250 if (CreateProcessW( NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info ))
252 /* Give 10 seconds to the app to come up */
253 if (wait_input_idle( info.hProcess, 10000 ) == WAIT_FAILED)
254 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
255 ReleaseThunkLock( &count );
257 WaitForSingleObject( info.hProcess, INFINITE );
258 GetExitCodeProcess( info.hProcess, &exit_code );
259 CloseHandle( info.hThread );
260 CloseHandle( info.hProcess );
262 else
263 ReleaseThunkLock( &count );
265 HeapFree( GetProcessHeap(), 0, cmdline );
266 ExitThread( exit_code );
269 /***********************************************************************
270 * _DebugOutput (KERNEL.328)
272 void WINAPIV _DebugOutput( WORD flags, LPCSTR spec, VA_LIST16 valist )
274 char caller[101];
276 /* Decode caller address */
277 if (!GetModuleName16( GetExePtr(CURRENT_STACK16->cs), caller, sizeof(caller) ))
278 sprintf( caller, "%04X:%04X", CURRENT_STACK16->cs, CURRENT_STACK16->ip );
280 /* FIXME: cannot use wvsnprintf16 from kernel */
281 /* wvsnprintf16( temp, sizeof(temp), spec, valist ); */
283 /* Output */
284 FIXME("%s %04x %s\n", caller, flags, debugstr_a(spec) );