Release 980315
[wine/multimedia.git] / if1632 / builtin.c
blob905c9823dbdd37aaea5c3d826e487077f1d6fc35
1 /*
2 * Built-in modules
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <ctype.h>
9 #include <string.h>
10 #include "windows.h"
11 #include "builtin32.h"
12 #include "gdi.h"
13 #include "global.h"
14 #include "heap.h"
15 #include "module.h"
16 #include "miscemu.h"
17 #include "neexe.h"
18 #include "stackframe.h"
19 #include "user.h"
20 #include "process.h"
21 #include "debug.h"
23 /* Built-in modules descriptors */
24 /* Don't change these structures! (see tools/build.c) */
26 typedef struct
28 const char *name; /* DLL name */
29 void *module_start; /* 32-bit address of the module data */
30 int module_size; /* Size of the module data */
31 const BYTE *code_start; /* 32-bit address of DLL code */
32 const BYTE *data_start; /* 32-bit address of DLL data */
33 } WIN16_DESCRIPTOR;
35 typedef struct
37 const WIN16_DESCRIPTOR *descr; /* DLL descriptor */
38 int flags; /* flags (see below) */
39 } BUILTIN16_DLL;
41 /* DLL flags */
42 #define DLL_FLAG_NOT_USED 0x01 /* Use original Windows DLL if possible */
43 #define DLL_FLAG_ALWAYS_USED 0x02 /* Always use built-in DLL */
45 /* 16-bit DLLs */
47 extern const WIN16_DESCRIPTOR COMMDLG_Descriptor;
48 extern const WIN16_DESCRIPTOR COMPOBJ_Descriptor;
49 extern const WIN16_DESCRIPTOR DDEML_Descriptor;
50 extern const WIN16_DESCRIPTOR GDI_Descriptor;
51 extern const WIN16_DESCRIPTOR KERNEL_Descriptor;
52 extern const WIN16_DESCRIPTOR KEYBOARD_Descriptor;
53 extern const WIN16_DESCRIPTOR LZEXPAND_Descriptor;
54 extern const WIN16_DESCRIPTOR MMSYSTEM_Descriptor;
55 extern const WIN16_DESCRIPTOR MOUSE_Descriptor;
56 extern const WIN16_DESCRIPTOR OLE2CONV_Descriptor;
57 extern const WIN16_DESCRIPTOR OLE2DISP_Descriptor;
58 extern const WIN16_DESCRIPTOR OLE2NLS_Descriptor;
59 extern const WIN16_DESCRIPTOR OLE2PROX_Descriptor;
60 extern const WIN16_DESCRIPTOR OLE2THK_Descriptor;
61 extern const WIN16_DESCRIPTOR OLE2_Descriptor;
62 extern const WIN16_DESCRIPTOR OLECLI_Descriptor;
63 extern const WIN16_DESCRIPTOR OLESVR_Descriptor;
64 extern const WIN16_DESCRIPTOR SHELL_Descriptor;
65 extern const WIN16_DESCRIPTOR SOUND_Descriptor;
66 extern const WIN16_DESCRIPTOR STORAGE_Descriptor;
67 extern const WIN16_DESCRIPTOR STRESS_Descriptor;
68 extern const WIN16_DESCRIPTOR SYSTEM_Descriptor;
69 extern const WIN16_DESCRIPTOR TOOLHELP_Descriptor;
70 extern const WIN16_DESCRIPTOR TYPELIB_Descriptor;
71 extern const WIN16_DESCRIPTOR USER_Descriptor;
72 extern const WIN16_DESCRIPTOR VER_Descriptor;
73 extern const WIN16_DESCRIPTOR W32SYS_Descriptor;
74 extern const WIN16_DESCRIPTOR WIN32S16_Descriptor;
75 extern const WIN16_DESCRIPTOR WIN87EM_Descriptor;
76 extern const WIN16_DESCRIPTOR WINASPI_Descriptor;
77 extern const WIN16_DESCRIPTOR WINDEBUG_Descriptor;
78 extern const WIN16_DESCRIPTOR WING_Descriptor;
79 extern const WIN16_DESCRIPTOR WINSOCK_Descriptor;
80 extern const WIN16_DESCRIPTOR WPROCS_Descriptor;
82 /* Table of all built-in DLLs */
84 static BUILTIN16_DLL BuiltinDLLs[] =
86 { &KERNEL_Descriptor, DLL_FLAG_ALWAYS_USED },
87 { &USER_Descriptor, DLL_FLAG_ALWAYS_USED },
88 { &GDI_Descriptor, DLL_FLAG_ALWAYS_USED },
89 { &SYSTEM_Descriptor, DLL_FLAG_ALWAYS_USED },
90 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
91 { &WINDEBUG_Descriptor, DLL_FLAG_ALWAYS_USED },
92 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
93 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
94 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
95 { &KEYBOARD_Descriptor, 0 },
96 { &LZEXPAND_Descriptor, 0 },
97 { &MMSYSTEM_Descriptor, 0 },
98 { &MOUSE_Descriptor, 0 },
99 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
100 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
101 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
102 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
103 { &OLE2THK_Descriptor, DLL_FLAG_NOT_USED },
104 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
105 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
106 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
107 { &SHELL_Descriptor, 0 },
108 { &SOUND_Descriptor, 0 },
109 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
110 { &STRESS_Descriptor, 0 },
111 { &TOOLHELP_Descriptor, 0 },
112 { &TYPELIB_Descriptor, 0 },
113 { &VER_Descriptor, 0 },
114 { &W32SYS_Descriptor, 0 },
115 { &WIN32S16_Descriptor, 0 },
116 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
117 { &WINASPI_Descriptor, 0 },
118 { &WING_Descriptor, 0 },
119 { &WINSOCK_Descriptor, 0 },
120 /* Last entry */
121 { NULL, 0 }
124 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
125 #define FIRST_INTERRUPT_ORDINAL 100
128 /***********************************************************************
129 * BUILTIN_DoLoadModule16
131 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
132 * and BUILTIN_Init.
134 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
136 NE_MODULE *pModule;
137 int minsize;
138 SEGTABLEENTRY *pSegTable;
140 HMODULE16 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
141 descr->module_size, 0,
142 FALSE, FALSE, FALSE, NULL );
143 if (!hModule) return 0;
144 FarSetOwner( hModule, hModule );
146 TRACE(module, "Built-in %s: hmodule=%04x\n",
147 descr->name, hModule );
148 pModule = (NE_MODULE *)GlobalLock16( hModule );
149 pModule->self = hModule;
151 /* Allocate the code segment */
153 pSegTable = NE_SEG_TABLE( pModule );
154 pSegTable->selector = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
155 pSegTable->minsize, hModule,
156 TRUE, TRUE, FALSE, NULL );
157 if (!pSegTable->selector) return 0;
158 pSegTable++;
160 /* Allocate the data segment */
162 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
163 minsize += pModule->heap_size;
164 if (minsize > 0x10000) minsize = 0x10000;
165 pSegTable->selector = GLOBAL_Alloc( GMEM_FIXED, minsize,
166 hModule, FALSE, FALSE, FALSE );
167 if (!pSegTable->selector) return 0;
168 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->selector ),
169 descr->data_start, pSegTable->minsize);
170 if (pModule->heap_size)
171 LocalInit( pSegTable->selector, pSegTable->minsize, minsize );
173 MODULE_RegisterModule( pModule );
174 return hModule;
178 /***********************************************************************
179 * BUILTIN_Init
181 * Load all built-in modules marked as 'always used'.
183 BOOL32 BUILTIN_Init(void)
185 BUILTIN16_DLL *dll;
186 NE_MODULE *pModule;
187 WORD vector;
188 HMODULE16 hModule;
190 fnBUILTIN_LoadModule = BUILTIN_LoadModule;
192 for (dll = BuiltinDLLs; dll->descr; dll++)
194 if (dll->flags & DLL_FLAG_ALWAYS_USED)
195 if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
198 /* Set the USER and GDI heap selectors */
200 pModule = MODULE_GetPtr( GetModuleHandle16( "USER" ));
201 USER_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
202 pModule = MODULE_GetPtr( GetModuleHandle16( "GDI" ));
203 GDI_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
205 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
207 hModule = GetModuleHandle16( "KERNEL" );
208 MODULE_SetEntryPoint( hModule, 178, GetWinFlags() );
210 /* Initialize the real-mode selector entry points */
212 DOSMEM_InitExports( hModule );
214 /* Set interrupt vectors from entry points in WPROCS.DLL */
216 hModule = GetModuleHandle16( "WPROCS" );
217 for (vector = 0; vector < 256; vector++)
219 FARPROC16 proc = MODULE_GetEntryPoint( hModule,
220 FIRST_INTERRUPT_ORDINAL+vector);
221 assert(proc);
222 INT_SetHandler( vector, proc );
225 return TRUE;
229 /***********************************************************************
230 * BUILTIN_LoadModule
232 * Load a built-in module. If the 'force' parameter is FALSE, we only
233 * load the module if it has not been disabled via the -dll option.
235 HMODULE32 BUILTIN_LoadModule( LPCSTR name, BOOL32 force )
237 BUILTIN16_DLL *table;
238 char dllname[16], *p;
240 /* Fix the name in case we have a full path and extension */
242 if ((p = strrchr( name, '\\' ))) name = p + 1;
243 lstrcpyn32A( dllname, name, sizeof(dllname) );
244 if ((p = strrchr( dllname, '.' ))) *p = '\0';
246 for (table = BuiltinDLLs; table->descr; table++)
247 if (!lstrcmpi32A( table->descr->name, dllname )) break;
248 if (!table->descr) return BUILTIN32_LoadModule( name, force,
249 PROCESS_Current() );
250 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
252 return BUILTIN_DoLoadModule16( table->descr );
256 /***********************************************************************
257 * BUILTIN_GetEntryPoint16
259 * Return the ordinal and name corresponding to a CS:IP address.
260 * This is used only by relay debugging.
262 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
264 static char buffer[80];
265 WORD ordinal, i, max_offset;
266 register BYTE *p;
267 NE_MODULE *pModule;
269 if (!(pModule = MODULE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
270 return NULL;
272 /* Search for the ordinal */
274 p = (BYTE *)pModule + pModule->entry_table;
275 max_offset = 0;
276 ordinal = 1;
277 *pOrd = 0;
278 while (*p)
280 switch(p[1])
282 case 0: /* unused */
283 ordinal += *p;
284 p += 2;
285 break;
286 case 1: /* code segment */
287 i = *p;
288 p += 2;
289 while (i-- > 0)
291 p++;
292 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
294 max_offset = *(WORD *)p;
295 *pOrd = ordinal;
297 p += 2;
298 ordinal++;
300 break;
301 case 0xff: /* moveable (should not happen in built-in modules) */
302 fprintf( stderr, "Built-in module has moveable entry\n" );
303 ordinal += *p;
304 p += 2 + *p * 6;
305 break;
306 default: /* other segment */
307 ordinal += *p;
308 p += 2 + *p * 3;
309 break;
313 /* Search for the name in the resident names table */
314 /* (built-in modules have no non-resident table) */
316 p = (BYTE *)pModule + pModule->name_table;
317 while (*p)
319 p += *p + 1 + sizeof(WORD);
320 if (*(WORD *)(p + *p + 1) == *pOrd) break;
323 sprintf( buffer, "%.*s.%d: %.*s",
324 *((BYTE *)pModule + pModule->name_table),
325 (char *)pModule + pModule->name_table + 1,
326 *pOrd, *p, (char *)(p + 1) );
327 return buffer;
331 /**********************************************************************
332 * BUILTIN_DefaultIntHandler
334 * Default interrupt handler.
336 void BUILTIN_DefaultIntHandler( CONTEXT *context )
338 WORD ordinal;
339 STACK16FRAME *frame = CURRENT_STACK16;
340 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
341 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
345 /***********************************************************************
346 * BUILTIN_ParseDLLOptions
348 * Set runtime DLL usage flags
350 BOOL32 BUILTIN_ParseDLLOptions( const char *str )
352 BUILTIN16_DLL *dll;
353 const char *p;
355 while (*str)
357 while (*str && isspace(*str)) str++;
358 if (!*str) return TRUE;
359 if ((*str != '+') && (*str != '-')) return FALSE;
360 str++;
361 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
362 while ((p > str) && isspace(p[-1])) p--;
363 if (p == str) return FALSE;
364 for (dll = BuiltinDLLs; dll->descr; dll++)
366 if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
368 if (dll->descr->name[(int)(p-str)]) /* only partial match */
369 continue;
370 if (str[-1] == '-')
372 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
373 dll->flags |= DLL_FLAG_NOT_USED;
375 else dll->flags &= ~DLL_FLAG_NOT_USED;
376 break;
379 if (!dll->descr)
380 if (!BUILTIN32_EnableDLL( str, (int)(p - str), (str[-1] == '+') ))
381 return FALSE;
382 str = p;
383 while (*str && (isspace(*str) || (*str == ','))) str++;
385 return TRUE;
389 /***********************************************************************
390 * BUILTIN_PrintDLLs
392 * Print the list of built-in DLLs that can be disabled.
394 void BUILTIN_PrintDLLs(void)
396 int i;
397 BUILTIN16_DLL *dll;
399 fprintf(stderr,"Example: -dll -ole2 Do not use emulated OLE2.DLL\n");
400 fprintf(stderr,"Available Win16 DLLs:\n");
401 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
403 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
404 fprintf( stderr, "%-9s%c", dll->descr->name,
405 ((++i) % 8) ? ' ' : '\n' );
407 fprintf(stderr,"\n");
408 BUILTIN32_PrintDLLs();