Check for complete lcid or just the primary language id in the
[wine/wine-kai.git] / if1632 / builtin.c
blob4818aaf274a6937efffc6e1abbd0925168d5d656
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 "winbase.h"
11 #include "wine/winbase16.h"
12 #include "wine/winestring.h"
13 #include "builtin32.h"
14 #include "global.h"
15 #include "heap.h"
16 #include "module.h"
17 #include "miscemu.h"
18 #include "neexe.h"
19 #include "stackframe.h"
20 #include "user.h"
21 #include "process.h"
22 #include "snoop.h"
23 #include "task.h"
24 #include "debugtools.h"
25 #include "toolhelp.h"
27 DEFAULT_DEBUG_CHANNEL(module)
29 /* Built-in modules descriptors */
30 /* Don't change these structures! (see tools/build.c) */
32 typedef struct
34 const char *name; /* DLL name */
35 void *module_start; /* 32-bit address of the module data */
36 int module_size; /* Size of the module data */
37 const BYTE *code_start; /* 32-bit address of DLL code */
38 const BYTE *data_start; /* 32-bit address of DLL data */
39 } WIN16_DESCRIPTOR;
41 typedef struct
43 const WIN16_DESCRIPTOR *descr; /* DLL descriptor */
44 int flags; /* flags (see below) */
45 } BUILTIN16_DLL;
47 /* DLL flags */
48 #define DLL_FLAG_NOT_USED 0x01 /* Use original Windows DLL if possible */
49 #define DLL_FLAG_ALWAYS_USED 0x02 /* Always use built-in DLL */
51 /* 16-bit DLLs */
53 extern const WIN16_DESCRIPTOR AVIFILE_Descriptor;
54 extern const WIN16_DESCRIPTOR COMM_Descriptor;
55 extern const WIN16_DESCRIPTOR COMMDLG_Descriptor;
56 extern const WIN16_DESCRIPTOR COMPOBJ_Descriptor;
57 extern const WIN16_DESCRIPTOR DDEML_Descriptor;
58 extern const WIN16_DESCRIPTOR DISPDIB_Descriptor;
59 extern const WIN16_DESCRIPTOR DISPLAY_Descriptor;
60 extern const WIN16_DESCRIPTOR GDI_Descriptor;
61 extern const WIN16_DESCRIPTOR KERNEL_Descriptor;
62 extern const WIN16_DESCRIPTOR KEYBOARD_Descriptor;
63 extern const WIN16_DESCRIPTOR LZEXPAND_Descriptor;
64 extern const WIN16_DESCRIPTOR MMSYSTEM_Descriptor;
65 extern const WIN16_DESCRIPTOR MOUSE_Descriptor;
66 extern const WIN16_DESCRIPTOR MSACM_Descriptor;
67 extern const WIN16_DESCRIPTOR MSVIDEO_Descriptor;
68 extern const WIN16_DESCRIPTOR OLE2CONV_Descriptor;
69 extern const WIN16_DESCRIPTOR OLE2DISP_Descriptor;
70 extern const WIN16_DESCRIPTOR OLE2NLS_Descriptor;
71 extern const WIN16_DESCRIPTOR OLE2PROX_Descriptor;
72 extern const WIN16_DESCRIPTOR OLE2THK_Descriptor;
73 extern const WIN16_DESCRIPTOR OLE2_Descriptor;
74 extern const WIN16_DESCRIPTOR OLECLI_Descriptor;
75 extern const WIN16_DESCRIPTOR OLESVR_Descriptor;
76 extern const WIN16_DESCRIPTOR RASAPI16_Descriptor;
77 extern const WIN16_DESCRIPTOR SHELL_Descriptor;
78 extern const WIN16_DESCRIPTOR SOUND_Descriptor;
79 extern const WIN16_DESCRIPTOR STORAGE_Descriptor;
80 extern const WIN16_DESCRIPTOR STRESS_Descriptor;
81 extern const WIN16_DESCRIPTOR SYSTEM_Descriptor;
82 extern const WIN16_DESCRIPTOR TOOLHELP_Descriptor;
83 extern const WIN16_DESCRIPTOR TYPELIB_Descriptor;
84 extern const WIN16_DESCRIPTOR USER_Descriptor;
85 extern const WIN16_DESCRIPTOR VER_Descriptor;
86 extern const WIN16_DESCRIPTOR W32SYS_Descriptor;
87 extern const WIN16_DESCRIPTOR WIN32S16_Descriptor;
88 extern const WIN16_DESCRIPTOR WIN87EM_Descriptor;
89 extern const WIN16_DESCRIPTOR WINASPI_Descriptor;
90 extern const WIN16_DESCRIPTOR WINDEBUG_Descriptor;
91 extern const WIN16_DESCRIPTOR WINEPS_Descriptor;
92 extern const WIN16_DESCRIPTOR WING_Descriptor;
93 extern const WIN16_DESCRIPTOR WINSOCK_Descriptor;
94 extern const WIN16_DESCRIPTOR WPROCS_Descriptor;
96 /* Table of all built-in DLLs */
98 static BUILTIN16_DLL BuiltinDLLs[] =
100 { &KERNEL_Descriptor, 0 },
101 { &USER_Descriptor, 0 },
102 { &GDI_Descriptor, 0 },
103 { &SYSTEM_Descriptor, DLL_FLAG_ALWAYS_USED },
104 { &DISPLAY_Descriptor, DLL_FLAG_ALWAYS_USED },
105 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
106 { &WINDEBUG_Descriptor, DLL_FLAG_NOT_USED },
107 { &AVIFILE_Descriptor, DLL_FLAG_NOT_USED },
108 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
109 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
110 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
111 { &DISPDIB_Descriptor, 0 },
112 { &KEYBOARD_Descriptor, 0 },
113 { &COMM_Descriptor, 0 },
114 { &LZEXPAND_Descriptor, 0 },
115 { &MMSYSTEM_Descriptor, 0 },
116 { &MOUSE_Descriptor, 0 },
117 { &MSACM_Descriptor, 0 },
118 { &MSVIDEO_Descriptor, 0 },
119 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
120 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
121 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
122 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
123 { &OLE2THK_Descriptor, DLL_FLAG_NOT_USED },
124 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
125 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
126 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
127 { &RASAPI16_Descriptor, 0 },
128 { &SHELL_Descriptor, 0 },
129 { &SOUND_Descriptor, 0 },
130 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
131 { &STRESS_Descriptor, 0 },
132 { &TOOLHELP_Descriptor, 0 },
133 { &TYPELIB_Descriptor, DLL_FLAG_NOT_USED },
134 { &VER_Descriptor, 0 },
135 { &W32SYS_Descriptor, DLL_FLAG_NOT_USED },
136 { &WIN32S16_Descriptor, DLL_FLAG_NOT_USED },
137 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
138 { &WINASPI_Descriptor, 0 },
139 { &WINEPS_Descriptor, DLL_FLAG_ALWAYS_USED },
140 { &WING_Descriptor, 0 },
141 { &WINSOCK_Descriptor, 0 },
142 /* Last entry */
143 { NULL, 0 }
146 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
147 #define FIRST_INTERRUPT_ORDINAL 100
150 /***********************************************************************
151 * BUILTIN_DoLoadModule16
153 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
154 * and BUILTIN_Init.
156 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
158 NE_MODULE *pModule;
159 int minsize;
160 SEGTABLEENTRY *pSegTable;
162 HMODULE16 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
163 descr->module_size, 0,
164 FALSE, FALSE, FALSE, NULL );
165 if (!hModule) return 0;
166 FarSetOwner16( hModule, hModule );
168 TRACE("Built-in %s: hmodule=%04x\n",
169 descr->name, hModule );
170 pModule = (NE_MODULE *)GlobalLock16( hModule );
171 pModule->self = hModule;
173 /* Allocate the code segment */
175 pSegTable = NE_SEG_TABLE( pModule );
176 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
177 pSegTable->minsize, hModule,
178 TRUE, TRUE, FALSE, NULL );
179 if (!pSegTable->hSeg) return 0;
180 pSegTable++;
182 /* Allocate the data segment */
184 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
185 minsize += pModule->heap_size;
186 if (minsize > 0x10000) minsize = 0x10000;
187 pSegTable->hSeg = GLOBAL_Alloc( GMEM_FIXED, minsize,
188 hModule, FALSE, FALSE, FALSE );
189 if (!pSegTable->hSeg) return 0;
190 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
191 descr->data_start, pSegTable->minsize);
192 if (pModule->heap_size)
193 LocalInit16( GlobalHandleToSel16(pSegTable->hSeg),
194 pSegTable->minsize, minsize );
196 NE_RegisterModule( pModule );
197 return hModule;
201 /***********************************************************************
202 * BUILTIN_Init
204 * Load all built-in modules marked as 'always used'.
206 BOOL BUILTIN_Init(void)
208 BUILTIN16_DLL *dll;
209 WORD vector;
210 HMODULE16 hModule;
212 fnBUILTIN_LoadModule = BUILTIN_LoadModule;
214 for (dll = BuiltinDLLs; dll->descr; dll++)
216 if (dll->flags & DLL_FLAG_ALWAYS_USED)
217 if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
220 /* Set interrupt vectors from entry points in WPROCS.DLL */
222 hModule = GetModuleHandle16( "WPROCS" );
223 for (vector = 0; vector < 256; vector++)
225 FARPROC16 proc = NE_GetEntryPoint( hModule,
226 FIRST_INTERRUPT_ORDINAL + vector );
227 assert(proc);
228 INT_SetPMHandler( vector, proc );
231 SNOOP16_Init();
233 return TRUE;
237 /***********************************************************************
238 * BUILTIN_LoadModule
240 * Load a built-in module. If the 'force' parameter is FALSE, we only
241 * load the module if it has not been disabled via the -dll option.
243 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL force )
245 BUILTIN16_DLL *table;
246 char dllname[16], *p;
248 /* Fix the name in case we have a full path and extension */
250 if ((p = strrchr( name, '\\' ))) name = p + 1;
251 lstrcpynA( dllname, name, sizeof(dllname) );
252 if ((p = strrchr( dllname, '.' ))) *p = '\0';
254 for (table = BuiltinDLLs; table->descr; table++)
255 if (!lstrcmpiA( table->descr->name, dllname )) break;
256 if (!table->descr) return (HMODULE16)2;
257 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return (HMODULE16)2;
259 return BUILTIN_DoLoadModule16( table->descr );
263 /***********************************************************************
264 * BUILTIN_GetEntryPoint16
266 * Return the ordinal and name corresponding to a CS:IP address.
267 * This is used only by relay debugging.
269 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
271 static char buffer[80];
272 WORD i, max_offset;
273 register BYTE *p;
274 NE_MODULE *pModule;
275 ET_BUNDLE *bundle;
276 ET_ENTRY *entry;
278 if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16(cs) ))))
279 return NULL;
281 max_offset = 0;
282 *pOrd = 0;
283 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
286 entry = (ET_ENTRY *)((BYTE *)bundle+6);
287 for (i = bundle->first + 1; i <= bundle->last; i++)
289 if ((entry->offs <= ip)
290 && (entry->segnum == 1) /* code segment ? */
291 && (entry->offs >= max_offset))
293 max_offset = entry->offs;
294 *pOrd = i;
296 entry++;
298 } while ( (bundle->next)
299 && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
301 /* Search for the name in the resident names table */
302 /* (built-in modules have no non-resident table) */
304 p = (BYTE *)pModule + pModule->name_table;
305 while (*p)
307 p += *p + 1 + sizeof(WORD);
308 if (*(WORD *)(p + *p + 1) == *pOrd) break;
311 sprintf( buffer, "%.*s.%d: %.*s",
312 *((BYTE *)pModule + pModule->name_table),
313 (char *)pModule + pModule->name_table + 1,
314 *pOrd, *p, (char *)(p + 1) );
315 return buffer;
319 /**********************************************************************
320 * BUILTIN_DefaultIntHandler
322 * Default interrupt handler.
324 void BUILTIN_DefaultIntHandler( CONTEXT86 *context )
326 WORD ordinal;
327 STACK16FRAME *frame = CURRENT_STACK16;
328 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
329 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );