Release 980104
[wine/multimedia.git] / if1632 / builtin.c
blob7076b1e6fbf7943688e17001b75e1cecd772ec4f
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 "stddebug.h"
22 #include "debug.h"
24 /* Built-in modules descriptors */
25 /* Don't change these structures! (see tools/build.c) */
27 typedef struct
29 const char *name; /* DLL name */
30 void *module_start; /* 32-bit address of the module data */
31 int module_size; /* Size of the module data */
32 const BYTE *code_start; /* 32-bit address of DLL code */
33 const BYTE *data_start; /* 32-bit address of DLL data */
34 } WIN16_DESCRIPTOR;
36 typedef struct
38 const WIN16_DESCRIPTOR *descr; /* DLL descriptor */
39 int flags; /* flags (see below) */
40 } BUILTIN16_DLL;
42 /* DLL flags */
43 #define DLL_FLAG_NOT_USED 0x01 /* Use original Windows DLL if possible */
44 #define DLL_FLAG_ALWAYS_USED 0x02 /* Always use built-in DLL */
46 /* 16-bit DLLs */
48 extern const WIN16_DESCRIPTOR COMMDLG_Descriptor;
49 extern const WIN16_DESCRIPTOR COMPOBJ_Descriptor;
50 extern const WIN16_DESCRIPTOR DDEML_Descriptor;
51 extern const WIN16_DESCRIPTOR GDI_Descriptor;
52 extern const WIN16_DESCRIPTOR KERNEL_Descriptor;
53 extern const WIN16_DESCRIPTOR KEYBOARD_Descriptor;
54 extern const WIN16_DESCRIPTOR LZEXPAND_Descriptor;
55 extern const WIN16_DESCRIPTOR MMSYSTEM_Descriptor;
56 extern const WIN16_DESCRIPTOR MOUSE_Descriptor;
57 extern const WIN16_DESCRIPTOR OLE2CONV_Descriptor;
58 extern const WIN16_DESCRIPTOR OLE2DISP_Descriptor;
59 extern const WIN16_DESCRIPTOR OLE2NLS_Descriptor;
60 extern const WIN16_DESCRIPTOR OLE2PROX_Descriptor;
61 extern const WIN16_DESCRIPTOR OLE2THK_Descriptor;
62 extern const WIN16_DESCRIPTOR OLE2_Descriptor;
63 extern const WIN16_DESCRIPTOR OLECLI_Descriptor;
64 extern const WIN16_DESCRIPTOR OLESVR_Descriptor;
65 extern const WIN16_DESCRIPTOR SHELL_Descriptor;
66 extern const WIN16_DESCRIPTOR SOUND_Descriptor;
67 extern const WIN16_DESCRIPTOR STORAGE_Descriptor;
68 extern const WIN16_DESCRIPTOR STRESS_Descriptor;
69 extern const WIN16_DESCRIPTOR SYSTEM_Descriptor;
70 extern const WIN16_DESCRIPTOR TOOLHELP_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 { &VER_Descriptor, 0 },
113 { &W32SYS_Descriptor, 0 },
114 { &WIN32S16_Descriptor, 0 },
115 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
116 { &WINASPI_Descriptor, 0 },
117 { &WING_Descriptor, 0 },
118 { &WINSOCK_Descriptor, 0 },
119 /* Last entry */
120 { NULL, 0 }
123 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
124 #define FIRST_INTERRUPT_ORDINAL 100
127 /***********************************************************************
128 * BUILTIN_DoLoadModule16
130 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
131 * and BUILTIN_Init.
133 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
135 NE_MODULE *pModule;
136 int minsize;
137 SEGTABLEENTRY *pSegTable;
139 HMODULE16 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
140 descr->module_size, 0,
141 FALSE, FALSE, FALSE, NULL );
142 if (!hModule) return 0;
143 FarSetOwner( hModule, hModule );
145 dprintf_module( stddeb, "Built-in %s: hmodule=%04x\n",
146 descr->name, hModule );
147 pModule = (NE_MODULE *)GlobalLock16( hModule );
148 pModule->self = hModule;
150 /* Allocate the code segment */
152 pSegTable = NE_SEG_TABLE( pModule );
153 pSegTable->selector = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
154 pSegTable->minsize, hModule,
155 TRUE, TRUE, FALSE, NULL );
156 if (!pSegTable->selector) return 0;
157 pSegTable++;
159 /* Allocate the data segment */
161 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
162 minsize += pModule->heap_size;
163 if (minsize > 0x10000) minsize = 0x10000;
164 pSegTable->selector = GLOBAL_Alloc( GMEM_FIXED, minsize,
165 hModule, FALSE, FALSE, FALSE );
166 if (!pSegTable->selector) return 0;
167 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->selector ),
168 descr->data_start, pSegTable->minsize);
169 if (pModule->heap_size)
170 LocalInit( pSegTable->selector, pSegTable->minsize, minsize );
172 MODULE_RegisterModule( pModule );
173 return hModule;
177 /***********************************************************************
178 * BUILTIN_Init
180 * Load all built-in modules marked as 'always used'.
182 BOOL32 BUILTIN_Init(void)
184 BUILTIN16_DLL *dll;
185 NE_MODULE *pModule;
186 WORD vector;
187 HMODULE16 hModule;
189 for (dll = BuiltinDLLs; dll->descr; dll++)
191 if (dll->flags & DLL_FLAG_ALWAYS_USED)
192 if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
195 /* Set the USER and GDI heap selectors */
197 pModule = MODULE_GetPtr( GetModuleHandle16( "USER" ));
198 USER_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
199 pModule = MODULE_GetPtr( GetModuleHandle16( "GDI" ));
200 GDI_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
202 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
204 hModule = GetModuleHandle16( "KERNEL" );
205 MODULE_SetEntryPoint( hModule, 178, GetWinFlags() );
207 /* Initialize the real-mode selector entry points */
209 DOSMEM_InitExports( hModule );
211 /* Set interrupt vectors from entry points in WPROCS.DLL */
213 hModule = GetModuleHandle16( "WPROCS" );
214 for (vector = 0; vector < 256; vector++)
216 FARPROC16 proc = MODULE_GetEntryPoint( hModule,
217 FIRST_INTERRUPT_ORDINAL+vector);
218 assert(proc);
219 INT_SetHandler( vector, proc );
222 return TRUE;
226 /***********************************************************************
227 * BUILTIN_LoadModule
229 * Load a built-in module. If the 'force' parameter is FALSE, we only
230 * load the module if it has not been disabled via the -dll option.
232 HMODULE32 BUILTIN_LoadModule( LPCSTR name, BOOL32 force )
234 BUILTIN16_DLL *table;
235 char dllname[16], *p;
237 /* Fix the name in case we have a full path and extension */
239 if ((p = strrchr( name, '\\' ))) name = p + 1;
240 lstrcpyn32A( dllname, name, sizeof(dllname) );
241 if ((p = strrchr( dllname, '.' ))) *p = '\0';
243 for (table = BuiltinDLLs; table->descr; table++)
244 if (!lstrcmpi32A( table->descr->name, dllname )) break;
245 if (!table->descr) return BUILTIN32_LoadModule( name, force );
246 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
248 return BUILTIN_DoLoadModule16( table->descr );
252 /***********************************************************************
253 * BUILTIN_GetEntryPoint16
255 * Return the ordinal and name corresponding to a CS:IP address.
256 * This is used only by relay debugging.
258 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
260 static char buffer[80];
261 WORD ordinal, i, max_offset;
262 register BYTE *p;
263 NE_MODULE *pModule;
265 if (!(pModule = MODULE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
266 return NULL;
268 /* Search for the ordinal */
270 p = (BYTE *)pModule + pModule->entry_table;
271 max_offset = 0;
272 ordinal = 1;
273 *pOrd = 0;
274 while (*p)
276 switch(p[1])
278 case 0: /* unused */
279 ordinal += *p;
280 p += 2;
281 break;
282 case 1: /* code segment */
283 i = *p;
284 p += 2;
285 while (i-- > 0)
287 p++;
288 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
290 max_offset = *(WORD *)p;
291 *pOrd = ordinal;
293 p += 2;
294 ordinal++;
296 break;
297 case 0xff: /* moveable (should not happen in built-in modules) */
298 fprintf( stderr, "Built-in module has moveable entry\n" );
299 ordinal += *p;
300 p += 2 + *p * 6;
301 break;
302 default: /* other segment */
303 ordinal += *p;
304 p += 2 + *p * 3;
305 break;
309 /* Search for the name in the resident names table */
310 /* (built-in modules have no non-resident table) */
312 p = (BYTE *)pModule + pModule->name_table;
313 while (*p)
315 p += *p + 1 + sizeof(WORD);
316 if (*(WORD *)(p + *p + 1) == *pOrd) break;
319 sprintf( buffer, "%.*s.%d: %.*s",
320 *((BYTE *)pModule + pModule->name_table),
321 (char *)pModule + pModule->name_table + 1,
322 *pOrd, *p, (char *)(p + 1) );
323 return buffer;
327 /**********************************************************************
328 * BUILTIN_DefaultIntHandler
330 * Default interrupt handler.
332 void BUILTIN_DefaultIntHandler( CONTEXT *context )
334 WORD ordinal;
335 STACK16FRAME *frame = CURRENT_STACK16;
336 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
337 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
341 /***********************************************************************
342 * BUILTIN_ParseDLLOptions
344 * Set runtime DLL usage flags
346 BOOL32 BUILTIN_ParseDLLOptions( const char *str )
348 BUILTIN16_DLL *dll;
349 const char *p;
351 while (*str)
353 while (*str && isspace(*str)) str++;
354 if (!*str) return TRUE;
355 if ((*str != '+') && (*str != '-')) return FALSE;
356 str++;
357 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
358 while ((p > str) && isspace(p[-1])) p--;
359 if (p == str) return FALSE;
360 for (dll = BuiltinDLLs; dll->descr; dll++)
362 if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
364 if (str[-1] == '-')
366 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
367 dll->flags |= DLL_FLAG_NOT_USED;
369 else dll->flags &= ~DLL_FLAG_NOT_USED;
370 break;
373 if (!dll->descr)
374 if (!BUILTIN32_EnableDLL( str, (int)(p - str), (str[-1] == '+') ))
375 return FALSE;
376 str = p;
377 while (*str && (isspace(*str) || (*str == ','))) str++;
379 return TRUE;
383 /***********************************************************************
384 * BUILTIN_PrintDLLs
386 * Print the list of built-in DLLs that can be disabled.
388 void BUILTIN_PrintDLLs(void)
390 int i;
391 BUILTIN16_DLL *dll;
393 fprintf(stderr,"Example: -dll -ole2 Do not use emulated OLE2.DLL\n");
394 fprintf(stderr,"Available Win16 DLLs:\n");
395 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
397 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
398 fprintf( stderr, "%-9s%c", dll->descr->name,
399 ((++i) % 8) ? ' ' : '\n' );
401 fprintf(stderr,"\n");
402 BUILTIN32_PrintDLLs();