Changed the size of property sheet template to be the same as the
[wine/wine-kai.git] / loader / loadorder.c
blobaf7a903776afee71edfc9d758ff656217244cc79
1 /*
2 * Module/Library loadorder
4 * Copyright 1999 Bertho Stultiens
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
11 #include "config.h"
12 #include "windef.h"
13 #include "options.h"
14 #include "loadorder.h"
15 #include "heap.h"
16 #include "module.h"
17 #include "elfdll.h"
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(module)
23 /* #define DEBUG_LOADORDER */
25 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
27 static module_loadorder_t default_loadorder;
28 static module_loadorder_t *module_loadorder = NULL;
29 static int nmodule_loadorder = 0;
30 static int nmodule_loadorder_alloc = 0;
32 static struct tagDllOverride {
33 char *key,*value;
34 } DefaultDllOverrides[] = {
35 {"kernel32,gdi32,user32", "builtin"},
36 {"krnl386,gdi,user", "builtin"},
37 {"toolhelp", "builtin"},
38 {"comdlg32,commdlg", "elfdll,builtin,native"},
39 {"version,ver", "elfdll,builtin,native"},
40 {"shell32,shell", "builtin,native"},
41 {"shlwapi", "native,builtin"},
42 {"lz32,lzexpand", "builtin,native"},
43 {"commctrl,comctl32", "builtin,native"},
44 {"wsock32,winsock", "builtin"},
45 {"advapi32,crtdll,ntdll", "builtin,native"},
46 {"mpr,winspool.drv", "builtin,native"},
47 {"ddraw,dinput,dsound", "builtin,native"},
48 {"winmm, mmsystem", "builtin"},
49 {"msvideo, msvfw32", "builtin, native"},
50 {"mcicda.drv, mciseq.drv", "builtin, native"},
51 {"mciwave.drv", "builtin, native"},
52 {"mciavi.drv, mcianim.drv", "native, builtin"},
53 {"msacm.drv, midimap.drv", "builtin, native"},
54 {"w32skrnl", "builtin"},
55 {"wnaspi32,wow32", "builtin"},
56 {"system,display,wprocs ", "builtin"},
57 {"wineps", "builtin"},
58 {"icmp", "builtin"},
59 /* we have to use libglide2x.so instead of glide2x.dll ... */
60 {"glide2x", "so,native"},
61 {"odbc32", "builtin"},
62 {"opengl32", "builtin,native"},
63 {"shfolder", "builtin,native"},
64 {NULL,NULL},
67 static const struct tagDllPair {
68 const char *dll1, *dll2;
69 } DllPairs[] = {
70 { "krnl386", "kernel32" },
71 { "gdi", "gdi32" },
72 { "user", "user32" },
73 { "commdlg", "comdlg32" },
74 { "commctrl", "comctl32" },
75 { "ver", "version" },
76 { "shell", "shell32" },
77 { "lzexpand", "lz32" },
78 { "mmsystem", "winmm" },
79 { "msvideo", "msvfw32" },
80 { "winsock", "wsock32" },
81 { NULL, NULL }
84 /***************************************************************************
85 * cmp_sort_func (internal, static)
87 * Sorting and comparing function used in sort and search of loadorder
88 * entries.
90 static int cmp_sort_func(const void *s1, const void *s2)
92 return strcasecmp(((module_loadorder_t *)s1)->modulename, ((module_loadorder_t *)s2)->modulename);
96 /***************************************************************************
97 * get_tok (internal, static)
99 * strtok wrapper for non-destructive buffer writing.
100 * NOTE: strtok is not reentrant and therefore this code is neither.
102 static char *get_tok(const char *str, const char *delim)
104 static char *buf = NULL;
105 char *cptr;
107 if(!str && !buf)
108 return NULL;
110 if(str && buf)
112 HeapFree(GetProcessHeap(), 0, buf);
113 buf = NULL;
116 if(str && !buf)
118 buf = HEAP_strdupA(GetProcessHeap(), 0, str);
119 cptr = strtok(buf, delim);
121 else
123 cptr = strtok(NULL, delim);
126 if(!cptr)
128 HeapFree(GetProcessHeap(), 0, buf);
129 buf = NULL;
131 return cptr;
135 /***************************************************************************
136 * ParseLoadOrder (internal, static)
138 * Parses the loadorder options from the configuration and puts it into
139 * a structure.
141 static BOOL ParseLoadOrder(char *order, module_loadorder_t *mlo)
143 char *cptr;
144 int n = 0;
146 memset(mlo->loadorder, 0, sizeof(mlo->loadorder));
148 cptr = get_tok(order, ", \t");
149 while(cptr)
151 char type = MODULE_LOADORDER_INVALID;
153 if(n >= MODULE_LOADORDER_NTYPES)
155 ERR("More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES);
156 break;
159 switch(*cptr)
161 case 'N': /* Native */
162 case 'n': type = MODULE_LOADORDER_DLL; break;
164 case 'E': /* Elfdll */
165 case 'e': type = MODULE_LOADORDER_ELFDLL; break;
167 case 'S': /* So */
168 case 's': type = MODULE_LOADORDER_SO; break;
170 case 'B': /* Builtin */
171 case 'b': type = MODULE_LOADORDER_BI; break;
173 default:
174 ERR("Invalid load order module-type '%s', ignored\n", cptr);
177 if(type != MODULE_LOADORDER_INVALID)
179 mlo->loadorder[n++] = type;
181 cptr = get_tok(NULL, ", \t");
183 return TRUE;
187 /***************************************************************************
188 * AddLoadOrder (internal, static)
190 * Adds an entry in the list of overrides. If the entry exists, then the
191 * override parameter determines whether it will be overwritten.
193 static BOOL AddLoadOrder(module_loadorder_t *plo, BOOL override)
195 int i;
197 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
199 for(i = 0; i < nmodule_loadorder; i++)
201 if(!cmp_sort_func(plo, &module_loadorder[i]))
203 if(!override)
204 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo->modulename);
205 else
206 memcpy(module_loadorder[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
207 return TRUE;
211 if(nmodule_loadorder >= nmodule_loadorder_alloc)
213 /* No space in current array, make it larger */
214 nmodule_loadorder_alloc += LOADORDER_ALLOC_CLUSTER;
215 module_loadorder = (module_loadorder_t *)HeapReAlloc(GetProcessHeap(),
217 module_loadorder,
218 nmodule_loadorder_alloc * sizeof(module_loadorder_t));
219 if(!module_loadorder)
221 MESSAGE("Virtual memory exhausted\n");
222 exit(1);
225 memcpy(module_loadorder[nmodule_loadorder].loadorder, plo->loadorder, sizeof(plo->loadorder));
226 module_loadorder[nmodule_loadorder].modulename = HEAP_strdupA(GetProcessHeap(), 0, plo->modulename);
227 nmodule_loadorder++;
228 return TRUE;
232 /***************************************************************************
233 * AddLoadOrderSet (internal, static)
235 * Adds a set of entries in the list of overrides from the key parameter.
236 * If the entry exists, then the override parameter determines whether it
237 * will be overwritten.
239 static BOOL AddLoadOrderSet(char *key, char *order, BOOL override)
241 module_loadorder_t ldo;
242 char *cptr;
244 /* Parse the loadorder before the rest because strtok is not reentrant */
245 if(!ParseLoadOrder(order, &ldo))
246 return FALSE;
248 cptr = get_tok(key, ", \t");
249 while(cptr)
251 char *ext = strrchr(cptr, '.');
252 if(ext)
254 if(strlen(ext) == 4 && (!strcasecmp(ext, ".dll") || !strcasecmp(ext, ".exe")))
255 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr);
258 ldo.modulename = cptr;
259 if(!AddLoadOrder(&ldo, override))
260 return FALSE;
261 cptr = get_tok(NULL, ", \t");
263 return TRUE;
267 /***************************************************************************
268 * ParseCommandlineOverrides (internal, static)
270 * The commandline is in the form:
271 * name[,name,...]=native[,b,...][:...]
273 static BOOL ParseCommandlineOverrides(void)
275 char *cpy;
276 char *key;
277 char *next;
278 char *value;
279 BOOL retval = TRUE;
281 if(!Options.dllFlags)
282 return TRUE;
284 cpy = HEAP_strdupA(GetProcessHeap(), 0, Options.dllFlags);
285 key = cpy;
286 next = key;
287 for(; next; key = next)
289 next = strchr(key, ':');
290 if(next)
292 *next = '\0';
293 next++;
295 value = strchr(key, '=');
296 if(!value)
298 retval = FALSE;
299 goto endit;
301 *value = '\0';
302 value++;
304 TRACE("Commandline override '%s' = '%s'\n", key, value);
306 if(!AddLoadOrderSet(key, value, TRUE))
308 retval = FALSE;
309 goto endit;
312 endit:
313 HeapFree(GetProcessHeap(), 0, cpy);
314 return retval;;
318 /***************************************************************************
319 * MODULE_InitLoadOrder (internal)
321 * Initialize the load order from the wine.conf file.
322 * The section has the following format:
323 * Section:
324 * [DllDefaults]
326 * Keys:
327 * EXTRA_LD_LIBRARY_PATH=/usr/local/lib/wine[:/more/path/to/search[:...]]
328 * The path will be appended to any existing LD_LIBRARY_PATH from the
329 * environment (see note in code below).
331 * DefaultLoadOrder=native,elfdll,so,builtin
332 * A comma separated list of module types to try to load in that specific
333 * order. The DefaultLoadOrder key is used as a fallback when a module is
334 * not specified explicitly. If the DefaultLoadOrder key is not found,
335 * then the order "dll,elfdll,so,bi" is used
336 * The possible module types are:
337 * - native Native windows dll files
338 * - elfdll Dlls encapsulated in .so libraries
339 * - so Native .so libraries mapped to dlls
340 * - builtin Built-in modules
342 * Case is not important and only the first letter of each type is enough to
343 * identify the type n[ative], e[lfdll], s[o], b[uiltin]. Also whitespace is
344 * ignored.
345 * E.g.:
346 * n,el ,s , b
347 * is equal to:
348 * native,elfdll,so,builtin
350 * Section:
351 * [DllOverrides]
353 * Keys:
354 * There are no explicit keys defined other than module/library names. A comma
355 * separated list of modules is followed by an assignment of the load-order
356 * for these specific modules. See above for possible types. You should not
357 * specify an extension.
358 * Examples:
359 * kernel32, gdi32, user32 = builtin
360 * kernel, gdi, user = builtin
361 * comdlg32 = elfdll, native, builtin
362 * commdlg = native, builtin
363 * version, ver = elfdll, native, builtin
367 #define BUFFERSIZE 1024
369 BOOL MODULE_InitLoadOrder(void)
371 char buffer[BUFFERSIZE];
372 char key[256];
373 int nbuffer;
374 int idx;
375 const struct tagDllPair *dllpair;
377 #if defined(HAVE_DL_API)
378 /* Get/set the new LD_LIBRARY_PATH */
379 nbuffer = PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer, sizeof(buffer));
381 if(nbuffer)
383 extra_ld_library_path = HEAP_strdupA(GetProcessHeap(), 0, buffer);
384 TRACE("Setting extra LD_LIBRARY_PATH=%s\n", buffer);
386 #endif
388 /* Get the default load order */
389 nbuffer = PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,e,s", buffer, sizeof(buffer));
390 if(!nbuffer)
392 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
393 return FALSE;
396 TRACE("Setting default loadorder=%s\n", buffer);
398 if(!ParseLoadOrder(buffer, &default_loadorder))
399 return FALSE;
400 default_loadorder.modulename = "<none>";
403 int i;
404 for (i=0;DefaultDllOverrides[i].key;i++)
405 AddLoadOrderSet(
406 DefaultDllOverrides[i].key,
407 DefaultDllOverrides[i].value,
408 FALSE
412 /* Read the explicitely defined orders for specific modules as an entire section */
413 idx = 0;
414 while (PROFILE_EnumWineIniString( "DllOverrides", idx++, key, sizeof(key),
415 buffer, sizeof(buffer)))
417 TRACE("Key '%s' uses override '%s'\n", key, buffer);
418 if(!AddLoadOrderSet(key, buffer, TRUE))
419 return FALSE;
422 /* Add the commandline overrides to the pool */
423 if(!ParseCommandlineOverrides())
425 MESSAGE( "Syntax: -dll name[,name[,...]]={native|elfdll|so|builtin}[,{n|e|s|b}[,...]][:...]\n"
426 " - 'name' is the name of any dll without extension\n"
427 " - the order of loading (native, elfdll, so and builtin) can be abbreviated\n"
428 " with the first letter\n"
429 " - different loadorders for different dlls can be specified by seperating the\n"
430 " commandline entries with a ':'\n"
431 " Example:\n"
432 " -dll comdlg32,commdlg=n:shell,shell32=b\n"
434 return FALSE;
437 /* Sort the array for quick lookup */
438 qsort(module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
440 /* Check the pairs of dlls */
441 dllpair = DllPairs;
442 while (dllpair->dll1)
444 module_loadorder_t *plo1, *plo2;
445 plo1 = MODULE_GetLoadOrder(dllpair->dll1);
446 plo2 = MODULE_GetLoadOrder(dllpair->dll2);
447 assert(plo1 && plo2);
448 if(memcmp(plo1->loadorder, plo2->loadorder, sizeof(plo1->loadorder)))
449 MESSAGE("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", dllpair->dll1, dllpair->dll2);
450 dllpair++;
453 if(TRACE_ON(module))
455 int i, j;
456 static char types[6] = "-NESB";
458 for(i = 0; i < nmodule_loadorder; i++)
460 DPRINTF("%3d: %-12s:", i, module_loadorder[i].modulename);
461 for(j = 0; j < MODULE_LOADORDER_NTYPES; j++)
462 DPRINTF(" %c", types[module_loadorder[i].loadorder[j] % (MODULE_LOADORDER_NTYPES+1)]);
463 DPRINTF("\n");
467 return TRUE;
471 /***************************************************************************
472 * MODULE_GetLoadOrder (internal)
474 * Locate the loadorder of a module.
475 * Any path is stripped from the path-argument and so are the extension
476 * '.dll' and '.exe'. A lookup in the table can yield an override for
477 * the specific dll. Otherwise the default load order is returned.
479 module_loadorder_t *MODULE_GetLoadOrder(const char *path)
481 module_loadorder_t lo, *tmp;
482 char fname[256];
483 char *cptr;
484 char *name;
485 int len;
487 TRACE("looking for %s\n", path);
489 assert(path != NULL);
491 /* Strip path information */
492 cptr = strrchr(path, '\\');
493 if(!cptr)
494 name = strrchr(path, '/');
495 else
496 name = strrchr(cptr, '/');
498 if(!name)
499 name = cptr ? cptr+1 : (char *)path;
500 else
501 name++;
503 if((cptr = strchr(name, ':')) != NULL) /* Also strip drive if in format 'C:MODULE.DLL' */
504 name = cptr+1;
506 len = strlen(name);
507 if(len >= sizeof(fname) || len <= 0)
509 ERR("Path '%s' -> '%s' reduces to zilch or just too large...\n", path, name);
510 return &default_loadorder;
513 strcpy(fname, name);
514 if(len >= 4 && (!lstrcmpiA(fname+len-4, ".dll") || !lstrcmpiA(fname+len-4, ".exe")))
515 fname[len-4] = '\0';
517 lo.modulename = fname;
518 tmp = bsearch(&lo, module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
520 TRACE("Looking for '%s' (%s), found '%s'\n", path, fname, tmp ? tmp->modulename : "<nothing>");
522 if(!tmp)
523 return &default_loadorder;
524 return tmp;