Change FONTOBJ to use LOGFONTW rather than LOGFONT16.
[wine] / loader / loadorder.c
1 /*
2  * Module/Library loadorder
3  *
4  * Copyright 1999 Bertho Stultiens
5  */
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
10
11 #include "config.h"
12 #include "windef.h"
13 #include "options.h"
14 #include "loadorder.h"
15 #include "heap.h"
16 #include "file.h"
17 #include "module.h"
18 #include "debugtools.h"
19
20 DEFAULT_DEBUG_CHANNEL(module);
21
22
23 /* #define DEBUG_LOADORDER */
24
25 #define LOADORDER_ALLOC_CLUSTER 32      /* Allocate with 32 entries at a time */
26
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;
31
32 /* DLL order is irrelevant ! Gets sorted later. */
33 static struct tagDllOverride {
34         char *key,*value;
35 } DefaultDllOverrides[] = {
36         /* "system" DLLs */
37         {"kernel32,gdi32,user32",       "builtin"},
38         {"krnl386,gdi,user",            "builtin"},
39         {"toolhelp",                    "builtin"},
40         {"windebug",                    "native,builtin"},
41         {"system,display",              "builtin"},
42         {"w32skrnl,wow32",              "builtin"},
43         {"advapi32,crtdll,ntdll",       "builtin,native"},
44         {"lz32,lzexpand",               "builtin,native"},
45         {"version,ver",                 "builtin,native"},
46         {"msvcrt",                      "native,builtin"},
47         /* "new" interface */
48         {"comdlg32,commdlg",            "builtin,native"},
49         {"shell32,shell",               "builtin,native"},
50         {"shlwapi",                     "native,builtin"},
51         {"shfolder",                    "builtin,native"},
52         {"comctl32,commctrl",           "builtin,native"},
53         /* network */
54         {"wsock32,ws2_32,winsock",      "builtin"},
55         {"icmp",                        "builtin"},
56         /* multimedia */
57         {"ddraw,dinput,dsound",         "builtin,native"},
58         {"winmm,mmsystem",              "builtin"},
59         {"msvfw32,msvideo",             "builtin,native"},
60         {"mcicda.drv,mciseq.drv",       "builtin,native"},
61         {"mciwave.drv",                 "builtin,native"},
62         {"mciavi.drv,mcianim.drv",      "native,builtin"},
63         {"msacm.drv,midimap.drv",       "builtin,native"},
64         {"msacm,msacm32",               "builtin,native"},
65         {"opengl32",                    "builtin,native"},
66         /* we have to use libglideXx.so instead of glideXx.dll ... */
67         {"glide2x,glide3x",             "so,native"},
68         /* other stuff */
69         {"mpr,winspool.drv",            "builtin,native"},
70         {"wnaspi32,winaspi",            "builtin"},
71         {"odbc32",                      "builtin"},
72         {"rpcrt4",                      "native,builtin"},
73         /* non-windows DLLs */
74         {"wineps,wprocs,x11drv",        "builtin"},
75         {NULL,NULL},
76 };
77
78 static const struct tagDllPair {
79     const char *dll1, *dll2;
80 } DllPairs[] = {
81     { "krnl386",  "kernel32" },
82     { "gdi",      "gdi32" },
83     { "user",     "user32" },
84     { "commdlg",  "comdlg32" },
85     { "commctrl", "comctl32" },
86     { "ver",      "version" },
87     { "shell",    "shell32" },
88     { "lzexpand", "lz32" },
89     { "mmsystem", "winmm" },
90     { "msvideo",  "msvfw32" },
91     { "msacm",    "msacm32" },
92     { "winsock",  "wsock32" },
93     { NULL,       NULL }
94 };
95
96 /***************************************************************************
97  *      cmp_sort_func   (internal, static)
98  *
99  * Sorting and comparing function used in sort and search of loadorder
100  * entries.
101  */
102 static int cmp_sort_func(const void *s1, const void *s2)
103 {
104     return FILE_strcasecmp(((module_loadorder_t *)s1)->modulename,
105                            ((module_loadorder_t *)s2)->modulename);
106 }
107
108
109 /***************************************************************************
110  *      get_tok (internal, static)
111  *
112  * strtok wrapper for non-destructive buffer writing.
113  * NOTE: strtok is not reentrant and therefore this code is neither.
114  */
115 static char *get_tok(const char *str, const char *delim)
116 {
117         static char *buf = NULL;
118         char *cptr;
119
120         if(!str && !buf)
121                 return NULL;
122
123         if(str && buf)
124         {
125                 HeapFree(GetProcessHeap(), 0, buf);
126                 buf = NULL;
127         }
128
129         if(str && !buf)
130         {
131                 buf = HEAP_strdupA(GetProcessHeap(), 0, str);
132                 cptr = strtok(buf, delim);
133         }
134         else
135         {
136                 cptr = strtok(NULL, delim);
137         }
138
139         if(!cptr)
140         {
141                 HeapFree(GetProcessHeap(), 0, buf);
142                 buf = NULL;
143         }
144         return cptr;
145 }
146
147
148 /***************************************************************************
149  *      ParseLoadOrder  (internal, static)
150  *
151  * Parses the loadorder options from the configuration and puts it into
152  * a structure.
153  */
154 static BOOL ParseLoadOrder(char *order, module_loadorder_t *mlo)
155 {
156     static int warn;
157         char *cptr;
158         int n = 0;
159
160         memset(mlo->loadorder, 0, sizeof(mlo->loadorder));
161
162         cptr = get_tok(order, ", \t");
163         while(cptr)
164         {
165                 char type = MODULE_LOADORDER_INVALID;
166
167                 if(n >= MODULE_LOADORDER_NTYPES)
168                 {
169                         ERR("More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES);
170                         break;
171                 }
172
173                 switch(*cptr)
174                 {
175                 case 'N':       /* Native */
176                 case 'n': type = MODULE_LOADORDER_DLL; break;
177
178                 case 'E':       /* Elfdll */
179                 case 'e':
180                     if (!warn++) MESSAGE("Load order 'elfdll' no longer supported, ignored\n");
181                     break;
182                 case 'S':       /* So */
183                 case 's': type = MODULE_LOADORDER_SO; break;
184
185                 case 'B':       /* Builtin */
186                 case 'b': type = MODULE_LOADORDER_BI; break;
187
188                 default:
189                         ERR("Invalid load order module-type '%s', ignored\n", cptr);
190                 }
191
192                 if(type != MODULE_LOADORDER_INVALID)
193                 {
194                         mlo->loadorder[n++] = type;
195                 }
196                 cptr = get_tok(NULL, ", \t");
197         }
198         return TRUE;
199 }
200
201
202 /***************************************************************************
203  *      AddLoadOrder    (internal, static)
204  *
205  * Adds an entry in the list of overrides. If the entry exists, then the
206  * override parameter determines whether it will be overwritten.
207  */
208 static BOOL AddLoadOrder(module_loadorder_t *plo, BOOL override)
209 {
210         int i;
211
212         /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
213
214         for(i = 0; i < nmodule_loadorder; i++)
215         {
216                 if(!cmp_sort_func(plo, &module_loadorder[i]))
217                 {
218                         if(!override)
219                                 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo->modulename);
220                         else
221                                 memcpy(module_loadorder[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
222                         return TRUE;
223                 }
224         }
225
226         if(nmodule_loadorder >= nmodule_loadorder_alloc)
227         {
228                 /* No space in current array, make it larger */
229                 nmodule_loadorder_alloc += LOADORDER_ALLOC_CLUSTER;
230                 module_loadorder = (module_loadorder_t *)HeapReAlloc(GetProcessHeap(),
231                                                                      0,
232                                                                      module_loadorder,
233                                                                      nmodule_loadorder_alloc * sizeof(module_loadorder_t));
234                 if(!module_loadorder)
235                 {
236                         MESSAGE("Virtual memory exhausted\n");
237                         exit(1);
238                 }
239         }
240         memcpy(module_loadorder[nmodule_loadorder].loadorder, plo->loadorder, sizeof(plo->loadorder));
241         module_loadorder[nmodule_loadorder].modulename = HEAP_strdupA(GetProcessHeap(), 0, plo->modulename);
242         nmodule_loadorder++;
243         return TRUE;
244 }
245
246
247 /***************************************************************************
248  *      AddLoadOrderSet (internal, static)
249  *
250  * Adds a set of entries in the list of overrides from the key parameter.
251  * If the entry exists, then the override parameter determines whether it
252  * will be overwritten.
253  */
254 static BOOL AddLoadOrderSet(char *key, char *order, BOOL override)
255 {
256         module_loadorder_t ldo;
257         char *cptr;
258
259         /* Parse the loadorder before the rest because strtok is not reentrant */
260         if(!ParseLoadOrder(order, &ldo))
261                 return FALSE;
262
263         cptr = get_tok(key, ", \t");
264         while(cptr)
265         {
266                 char *ext = strrchr(cptr, '.');
267                 if(ext)
268                 {
269                         if(strlen(ext) == 4 &&
270                            (!FILE_strcasecmp(ext, ".dll") || !FILE_strcasecmp(ext, ".exe")))
271                                 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr);
272                 }
273
274                 ldo.modulename = cptr;
275                 if(!AddLoadOrder(&ldo, override))
276                         return FALSE;
277                 cptr = get_tok(NULL, ", \t");
278         }
279         return TRUE;
280 }
281
282
283 /***************************************************************************
284  *      ParseCommandlineOverrides       (internal, static)
285  *
286  * The commandline is in the form:
287  * name[,name,...]=native[,b,...][+...]
288  */
289 static BOOL ParseCommandlineOverrides(void)
290 {
291         char *cpy;
292         char *key;
293         char *next;
294         char *value;
295         BOOL retval = TRUE;
296
297         if(!Options.dllFlags)
298                 return TRUE;
299
300         cpy = HEAP_strdupA(GetProcessHeap(), 0, Options.dllFlags);
301         key = cpy;
302         next = key;
303         for(; next; key = next)
304         {
305                 next = strchr(key, '+');
306                 if(next)
307                 {
308                         *next = '\0';
309                         next++;
310                 }
311                 value = strchr(key, '=');
312                 if(!value)
313                 {
314                         retval = FALSE;
315                         goto endit;
316                 }
317                 *value = '\0';
318                 value++;
319
320                 TRACE("Commandline override '%s' = '%s'\n", key, value);
321                 
322                 if(!AddLoadOrderSet(key, value, TRUE))
323                 {
324                         retval = FALSE;
325                         goto endit;
326                 }
327         }
328 endit:
329         HeapFree(GetProcessHeap(), 0, cpy);
330         return retval;;
331 }
332
333
334 /***************************************************************************
335  *      MODULE_InitLoadOrder    (internal)
336  *
337  * Initialize the load order from the wine.conf file.
338  * The section has the following format:
339  * Section:
340  *      [DllDefaults]
341  *
342  * Keys:
343  *      DefaultLoadOrder=native,so,builtin
344  * A comma separated list of module types to try to load in that specific
345  * order. The DefaultLoadOrder key is used as a fallback when a module is
346  * not specified explicitly. If the DefaultLoadOrder key is not found, 
347  * then the order "dll,so,bi" is used
348  * The possible module types are:
349  *      - native        Native windows dll files
350  *      - so            Native .so libraries mapped to dlls
351  *      - builtin       Built-in modules
352  *
353  * Case is not important and only the first letter of each type is enough to
354  * identify the type n[ative], s[o], b[uiltin]. Also whitespace is
355  * ignored.
356  * E.g.:
357  *      n,s , b
358  * is equal to:
359  *      native,so,builtin
360  *
361  * Section:
362  *      [DllOverrides]
363  *
364  * Keys:
365  * There are no explicit keys defined other than module/library names. A comma
366  * separated list of modules is followed by an assignment of the load-order
367  * for these specific modules. See above for possible types. You should not
368  * specify an extension.
369  * Examples:
370  * kernel32, gdi32, user32 = builtin
371  * kernel, gdi, user = builtin
372  * comdlg32 = native, builtin
373  * commdlg = native, builtin
374  * version, ver = native, builtin
375  *
376  */
377
378 #define BUFFERSIZE      1024
379
380 BOOL MODULE_InitLoadOrder(void)
381 {
382         char buffer[BUFFERSIZE];
383         char key[256];
384         int nbuffer;
385         int idx;
386         const struct tagDllPair *dllpair;
387
388         /* Get the default load order */
389         nbuffer = PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,s", buffer, sizeof(buffer));
390         if(!nbuffer)
391         {
392                 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
393                 return FALSE;
394         }
395
396         TRACE("Setting default loadorder=%s\n", buffer);
397
398         if(!ParseLoadOrder(buffer, &default_loadorder))
399                 return FALSE;
400         default_loadorder.modulename = "<none>";
401
402         {
403             int i;
404             for (i=0;DefaultDllOverrides[i].key;i++)
405                 AddLoadOrderSet(
406                     DefaultDllOverrides[i].key,
407                     DefaultDllOverrides[i].value,
408                     FALSE
409                 );
410         }
411
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)))
416         {
417             TRACE("Key '%s' uses override '%s'\n", key, buffer);
418             if(!AddLoadOrderSet(key, buffer, TRUE))
419                 return FALSE;
420         }
421
422         /* Add the commandline overrides to the pool */
423         if(!ParseCommandlineOverrides())
424         {
425                 MESSAGE(        "Syntax: -dll name[,name[,...]]={native|so|builtin}[,{n|s|b}[,...]][+...]\n"
426                         "    - 'name' is the name of any dll without extension\n"
427                         "    - the order of loading (native, 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"
433                    );
434                 return FALSE;
435         }
436
437         /* Sort the array for quick lookup */
438         qsort(module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
439
440         /* Check the pairs of dlls */
441         dllpair = DllPairs;
442         while (dllpair->dll1)
443         {
444             module_loadorder_t *plo1, *plo2;
445             plo1 = MODULE_GetLoadOrder(dllpair->dll1, FALSE);
446             plo2 = MODULE_GetLoadOrder(dllpair->dll2, FALSE);
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++;
451         }
452
453         if(TRACE_ON(module))
454         {
455                 int i, j;
456                 static char types[] = "-NSB";
457
458                 for(i = 0; i < nmodule_loadorder; i++)
459                 {
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");
464                 }
465         }
466
467         return TRUE;
468 }
469
470
471 /***************************************************************************
472  *      MODULE_GetLoadOrder     (internal)
473  *
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.
478  */
479 module_loadorder_t *MODULE_GetLoadOrder(const char *path, BOOL win32 )
480 {
481         module_loadorder_t lo, *tmp;
482         char fname[256];
483         char sysdir[MAX_PATH+1];
484         char *cptr;
485         char *name;
486         int len;
487
488         TRACE("looking for %s\n", path);
489         
490         assert(path != NULL);
491
492         if ( ! GetSystemDirectoryA ( sysdir, MAX_PATH ) ) 
493           return &default_loadorder; /* Hmmm ... */
494
495         /* Strip path information for 16 bit modules or if the module 
496            resides in the system directory */
497         if ( !win32 || !FILE_strncasecmp ( sysdir, path, strlen (sysdir) ) )
498         {
499         
500             cptr = strrchr(path, '\\');
501             if(!cptr)
502                 name = strrchr(path, '/');
503             else
504                 name = strrchr(cptr, '/');
505             
506             if(!name)
507                 name = cptr ? cptr+1 : (char *)path;
508             else
509                 name++;
510             
511             if((cptr = strchr(name, ':')) != NULL)      /* Also strip drive if in format 'C:MODULE.DLL' */
512                 name = cptr+1;
513         }
514         else 
515           name = (char *)path;
516     
517         len = strlen(name);
518         if(len >= sizeof(fname) || len <= 0)
519         {
520              ERR("Path '%s' -> '%s' reduces to zilch or just too large...\n", path, name);
521              return &default_loadorder;
522         }
523
524         strcpy(fname, name);
525         if(len >= 4 && (!FILE_strcasecmp(fname+len-4, ".dll") || !FILE_strcasecmp(fname+len-4, ".exe")))
526                 fname[len-4] = '\0';
527
528         lo.modulename = fname;
529         tmp = bsearch(&lo, module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
530
531         TRACE("Looking for '%s' (%s), found '%s'\n", path, fname, tmp ? tmp->modulename : "<nothing>");
532
533         if(!tmp)
534                 return &default_loadorder;
535         return tmp;
536 }
537