Undefined CHAR_MAX and CHAR_MIN (#defined by NetBSD's system headers).
[wine/multimedia.git] / if1632 / builtin.c
blobdf96d3ac667770965ecd5b44d30d1b0059cb6a0b
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 "snoop.h"
22 #include "task.h"
23 #include "debug.h"
25 /* Built-in modules descriptors */
26 /* Don't change these structures! (see tools/build.c) */
28 typedef struct
30 const char *name; /* DLL name */
31 void *module_start; /* 32-bit address of the module data */
32 int module_size; /* Size of the module data */
33 const BYTE *code_start; /* 32-bit address of DLL code */
34 const BYTE *data_start; /* 32-bit address of DLL data */
35 } WIN16_DESCRIPTOR;
37 typedef struct
39 const WIN16_DESCRIPTOR *descr; /* DLL descriptor */
40 int flags; /* flags (see below) */
41 } BUILTIN16_DLL;
43 /* DLL flags */
44 #define DLL_FLAG_NOT_USED 0x01 /* Use original Windows DLL if possible */
45 #define DLL_FLAG_ALWAYS_USED 0x02 /* Always use built-in DLL */
47 /* 16-bit DLLs */
49 extern const WIN16_DESCRIPTOR AVIFILE_Descriptor;
50 extern const WIN16_DESCRIPTOR COMM_Descriptor;
51 extern const WIN16_DESCRIPTOR COMMDLG_Descriptor;
52 extern const WIN16_DESCRIPTOR COMPOBJ_Descriptor;
53 extern const WIN16_DESCRIPTOR DDEML_Descriptor;
54 extern const WIN16_DESCRIPTOR DISPDIB_Descriptor;
55 extern const WIN16_DESCRIPTOR DISPLAY_Descriptor;
56 extern const WIN16_DESCRIPTOR GDI_Descriptor;
57 extern const WIN16_DESCRIPTOR KERNEL_Descriptor;
58 extern const WIN16_DESCRIPTOR KEYBOARD_Descriptor;
59 extern const WIN16_DESCRIPTOR LZEXPAND_Descriptor;
60 extern const WIN16_DESCRIPTOR MMSYSTEM_Descriptor;
61 extern const WIN16_DESCRIPTOR MOUSE_Descriptor;
62 extern const WIN16_DESCRIPTOR MSACM_Descriptor;
63 extern const WIN16_DESCRIPTOR MSVIDEO_Descriptor;
64 extern const WIN16_DESCRIPTOR OLE2CONV_Descriptor;
65 extern const WIN16_DESCRIPTOR OLE2DISP_Descriptor;
66 extern const WIN16_DESCRIPTOR OLE2NLS_Descriptor;
67 extern const WIN16_DESCRIPTOR OLE2PROX_Descriptor;
68 extern const WIN16_DESCRIPTOR OLE2THK_Descriptor;
69 extern const WIN16_DESCRIPTOR OLE2_Descriptor;
70 extern const WIN16_DESCRIPTOR OLECLI_Descriptor;
71 extern const WIN16_DESCRIPTOR OLESVR_Descriptor;
72 extern const WIN16_DESCRIPTOR RASAPI16_Descriptor;
73 extern const WIN16_DESCRIPTOR SHELL_Descriptor;
74 extern const WIN16_DESCRIPTOR SOUND_Descriptor;
75 extern const WIN16_DESCRIPTOR STORAGE_Descriptor;
76 extern const WIN16_DESCRIPTOR STRESS_Descriptor;
77 extern const WIN16_DESCRIPTOR SYSTEM_Descriptor;
78 extern const WIN16_DESCRIPTOR TOOLHELP_Descriptor;
79 extern const WIN16_DESCRIPTOR TYPELIB_Descriptor;
80 extern const WIN16_DESCRIPTOR USER_Descriptor;
81 extern const WIN16_DESCRIPTOR VER_Descriptor;
82 extern const WIN16_DESCRIPTOR W32SYS_Descriptor;
83 extern const WIN16_DESCRIPTOR WIN32S16_Descriptor;
84 extern const WIN16_DESCRIPTOR WIN87EM_Descriptor;
85 extern const WIN16_DESCRIPTOR WINASPI_Descriptor;
86 extern const WIN16_DESCRIPTOR WINDEBUG_Descriptor;
87 extern const WIN16_DESCRIPTOR WINEPS_Descriptor;
88 extern const WIN16_DESCRIPTOR WING_Descriptor;
89 extern const WIN16_DESCRIPTOR WINSOCK_Descriptor;
90 extern const WIN16_DESCRIPTOR WPROCS_Descriptor;
92 /* Table of all built-in DLLs */
94 static BUILTIN16_DLL BuiltinDLLs[] =
96 { &KERNEL_Descriptor, 0 },
97 { &USER_Descriptor, 0 },
98 { &GDI_Descriptor, 0 },
99 { &SYSTEM_Descriptor, DLL_FLAG_ALWAYS_USED },
100 { &DISPLAY_Descriptor, DLL_FLAG_ALWAYS_USED },
101 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
102 { &WINDEBUG_Descriptor, DLL_FLAG_NOT_USED },
103 { &AVIFILE_Descriptor, DLL_FLAG_NOT_USED },
104 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
105 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
106 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
107 { &DISPDIB_Descriptor, 0 },
108 { &KEYBOARD_Descriptor, 0 },
109 { &COMM_Descriptor, 0 },
110 { &LZEXPAND_Descriptor, 0 },
111 { &MMSYSTEM_Descriptor, 0 },
112 { &MOUSE_Descriptor, 0 },
113 { &MSACM_Descriptor, 0 },
114 { &MSVIDEO_Descriptor, 0 },
115 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
116 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
117 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
118 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
119 { &OLE2THK_Descriptor, DLL_FLAG_NOT_USED },
120 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
121 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
122 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
123 { &RASAPI16_Descriptor, 0 },
124 { &SHELL_Descriptor, 0 },
125 { &SOUND_Descriptor, 0 },
126 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
127 { &STRESS_Descriptor, 0 },
128 { &TOOLHELP_Descriptor, 0 },
129 { &TYPELIB_Descriptor, DLL_FLAG_NOT_USED },
130 { &VER_Descriptor, 0 },
131 { &W32SYS_Descriptor, DLL_FLAG_NOT_USED },
132 { &WIN32S16_Descriptor, DLL_FLAG_NOT_USED },
133 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
134 { &WINASPI_Descriptor, 0 },
135 { &WINEPS_Descriptor, DLL_FLAG_ALWAYS_USED },
136 { &WING_Descriptor, 0 },
137 { &WINSOCK_Descriptor, 0 },
138 /* Last entry */
139 { NULL, 0 }
142 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
143 #define FIRST_INTERRUPT_ORDINAL 100
146 /***********************************************************************
147 * BUILTIN_DoLoadModule16
149 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
150 * and BUILTIN_Init.
152 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
154 NE_MODULE *pModule;
155 int minsize;
156 SEGTABLEENTRY *pSegTable;
158 HMODULE16 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
159 descr->module_size, 0,
160 FALSE, FALSE, FALSE, NULL );
161 if (!hModule) return 0;
162 FarSetOwner( hModule, hModule );
164 TRACE(module, "Built-in %s: hmodule=%04x\n",
165 descr->name, hModule );
166 pModule = (NE_MODULE *)GlobalLock16( hModule );
167 pModule->self = hModule;
169 /* Allocate the code segment */
171 pSegTable = NE_SEG_TABLE( pModule );
172 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
173 pSegTable->minsize, hModule,
174 TRUE, TRUE, FALSE, NULL );
175 if (!pSegTable->hSeg) return 0;
176 pSegTable++;
178 /* Allocate the data segment */
180 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
181 minsize += pModule->heap_size;
182 if (minsize > 0x10000) minsize = 0x10000;
183 pSegTable->hSeg = GLOBAL_Alloc( GMEM_FIXED, minsize,
184 hModule, FALSE, FALSE, FALSE );
185 if (!pSegTable->hSeg) return 0;
186 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
187 descr->data_start, pSegTable->minsize);
188 if (pModule->heap_size)
189 LocalInit( GlobalHandleToSel(pSegTable->hSeg),
190 pSegTable->minsize, minsize );
192 NE_RegisterModule( pModule );
193 return hModule;
197 /***********************************************************************
198 * BUILTIN_Init
200 * Load all built-in modules marked as 'always used'.
202 BOOL32 BUILTIN_Init(void)
204 BUILTIN16_DLL *dll;
205 WORD vector;
206 HMODULE16 hModule;
208 fnBUILTIN_LoadModule = BUILTIN_LoadModule;
210 for (dll = BuiltinDLLs; dll->descr; dll++)
212 if (dll->flags & DLL_FLAG_ALWAYS_USED)
213 if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
216 /* Set interrupt vectors from entry points in WPROCS.DLL */
218 hModule = GetModuleHandle16( "WPROCS" );
219 for (vector = 0; vector < 256; vector++)
221 FARPROC16 proc = NE_GetEntryPoint( hModule,
222 FIRST_INTERRUPT_ORDINAL + vector );
223 assert(proc);
224 INT_SetPMHandler( vector, proc );
227 SNOOP16_Init();
229 return TRUE;
233 /***********************************************************************
234 * BUILTIN_LoadModule
236 * Load a built-in module. If the 'force' parameter is FALSE, we only
237 * load the module if it has not been disabled via the -dll option.
239 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL32 force )
241 BUILTIN16_DLL *table;
242 char dllname[16], *p;
244 /* Fix the name in case we have a full path and extension */
246 if ((p = strrchr( name, '\\' ))) name = p + 1;
247 lstrcpyn32A( dllname, name, sizeof(dllname) );
248 if ((p = strrchr( dllname, '.' ))) *p = '\0';
250 for (table = BuiltinDLLs; table->descr; table++)
251 if (!lstrcmpi32A( table->descr->name, dllname )) break;
252 if (!table->descr) return 0;
253 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
255 return BUILTIN_DoLoadModule16( table->descr );
259 /***********************************************************************
260 * BUILTIN_GetEntryPoint16
262 * Return the ordinal and name corresponding to a CS:IP address.
263 * This is used only by relay debugging.
265 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
267 static char buffer[80];
268 WORD ordinal, i, max_offset;
269 register BYTE *p;
270 NE_MODULE *pModule;
272 if (!(pModule = NE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
273 return NULL;
275 /* Search for the ordinal */
277 p = (BYTE *)pModule + pModule->entry_table;
278 max_offset = 0;
279 ordinal = 1;
280 *pOrd = 0;
281 while (*p)
283 switch(p[1])
285 case 0: /* unused */
286 ordinal += *p;
287 p += 2;
288 break;
289 case 1: /* code segment */
290 i = *p;
291 p += 2;
292 while (i-- > 0)
294 p++;
295 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
297 max_offset = *(WORD *)p;
298 *pOrd = ordinal;
300 p += 2;
301 ordinal++;
303 break;
304 case 0xff: /* moveable (should not happen in built-in modules) */
305 TRACE( relay, "Built-in module has moveable entry\n" );
306 ordinal += *p;
307 p += 2 + *p * 6;
308 break;
309 default: /* other segment */
310 ordinal += *p;
311 p += 2 + *p * 3;
312 break;
316 /* Search for the name in the resident names table */
317 /* (built-in modules have no non-resident table) */
319 p = (BYTE *)pModule + pModule->name_table;
320 while (*p)
322 p += *p + 1 + sizeof(WORD);
323 if (*(WORD *)(p + *p + 1) == *pOrd) break;
326 sprintf( buffer, "%.*s.%d: %.*s",
327 *((BYTE *)pModule + pModule->name_table),
328 (char *)pModule + pModule->name_table + 1,
329 *pOrd, *p, (char *)(p + 1) );
330 return buffer;
334 /**********************************************************************
335 * BUILTIN_DefaultIntHandler
337 * Default interrupt handler.
339 void BUILTIN_DefaultIntHandler( CONTEXT *context )
341 WORD ordinal;
342 STACK16FRAME *frame = CURRENT_STACK16;
343 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
344 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
348 /***********************************************************************
349 * BUILTIN_ParseDLLOptions
351 * Set runtime DLL usage flags
353 BOOL32 BUILTIN_ParseDLLOptions( const char *str )
355 BUILTIN16_DLL *dll;
356 const char *p;
358 while (*str)
360 while (*str && isspace(*str)) str++;
361 if (!*str) return TRUE;
362 if ((*str != '+') && (*str != '-')) return FALSE;
363 str++;
364 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
365 while ((p > str) && isspace(p[-1])) p--;
366 if (p == str) return FALSE;
367 for (dll = BuiltinDLLs; dll->descr; dll++)
369 if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
371 if (dll->descr->name[(int)(p-str)]) /* only partial match */
372 continue;
373 if (str[-1] == '-')
375 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
376 dll->flags |= DLL_FLAG_NOT_USED;
378 else dll->flags &= ~DLL_FLAG_NOT_USED;
379 break;
382 if (!dll->descr)
383 if (!BUILTIN32_EnableDLL( str, (int)(p - str), (str[-1] == '+') ))
384 return FALSE;
385 str = p;
386 while (*str && (isspace(*str) || (*str == ','))) str++;
388 return TRUE;
392 /***********************************************************************
393 * BUILTIN_PrintDLLs
395 * Print the list of built-in DLLs that can be disabled.
397 void BUILTIN_PrintDLLs(void)
399 int i;
400 BUILTIN16_DLL *dll;
402 MSG("Example: -dll -ole2 Do not use emulated OLE2.DLL\n");
403 MSG("Available Win16 DLLs:\n");
404 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
406 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
407 MSG("%-9s%c", dll->descr->name,
408 ((++i) % 8) ? ' ' : '\n' );
410 MSG("\n");
411 BUILTIN32_PrintDLLs();