Win2000: If both WS_EX_LAYERED and WS_EX_TRANSPARENT styles are set,
[wine/dcerpc.git] / misc / options.c
blob7a661718f71e8a23d9be24a9303a30afe2dedbe6
1 /*
2 * Option parsing
4 * Copyright 2000 Alexandre Julliard
5 */
7 #include "config.h"
8 #include <string.h>
9 #include <stdlib.h>
11 #include "winbase.h"
12 #include "winnls.h"
13 #include "ntddk.h"
14 #include "wine/library.h"
15 #include "options.h"
16 #include "version.h"
17 #include "debugtools.h"
19 struct option_descr
21 const char *longname;
22 char shortname;
23 int has_arg;
24 int inherit;
25 void (*func)( const char *arg );
26 const char *usage;
29 /* default options */
30 struct options Options =
32 NULL, /* desktopGeometry */
33 NULL, /* display */
34 NULL, /* dllFlags */
35 FALSE, /* synchronous */
36 FALSE /* Managed windows */
39 const char *argv0; /* the original argv[0] */
40 const char *full_argv0; /* the full path of argv[0] (if known) */
42 static char *inherit_str; /* options to pass to child processes */
44 static int app_argc; /* argc/argv to pass to application */
45 static char **app_argv;
46 static WCHAR **app_wargv;
48 static void out_of_memory(void) WINE_NORETURN;
49 static void out_of_memory(void)
51 MESSAGE( "Virtual memory exhausted\n" );
52 ExitProcess(1);
55 static char *xstrdup( const char *str )
57 char *ret = strdup( str );
58 if (!ret) out_of_memory();
59 return ret;
62 static void do_debugmsg( const char *arg );
63 static void do_desktop( const char *arg );
64 static void do_display( const char *arg );
65 static void do_dll( const char *arg );
66 static void do_help( const char *arg );
67 static void do_language( const char *arg );
68 static void do_managed( const char *arg );
69 static void do_synchronous( const char *arg );
70 static void do_version( const char *arg );
72 static const struct option_descr option_table[] =
74 { "debugmsg", 0, 1, 1, do_debugmsg,
75 "--debugmsg name Turn debugging-messages on or off" },
76 { "desktop", 0, 1, 1, do_desktop,
77 "--desktop geom Use a desktop window of the given geometry" },
78 { "display", 0, 1, 0, do_display,
79 "--display name Use the specified display" },
80 { "dll", 0, 1, 1, do_dll,
81 "--dll name Enable or disable built-in DLLs" },
82 { "dosver", 0, 1, 1, VERSION_ParseDosVersion,
83 "--dosver x.xx DOS version to imitate (e.g. 6.22)\n"
84 " Only valid with --winver win31" },
85 { "help", 'h', 0, 0, do_help,
86 "--help,-h Show this help message" },
87 { "language", 0, 1, 1, do_language,
88 "--language xx Set the language (one of Br,Ca,Cs,Cy,Da,De,En,Eo,Es,Fi,Fr,Ga,Gd,Gv,\n"
89 " He,Hr,Hu,It,Ja,Ko,Kw,Ms,Nl,No,Pl,Pt,Sk,Sv,Ru,Wa)" },
90 { "managed", 0, 0, 0, do_managed,
91 "--managed Allow the window manager to manage created windows" },
92 { "synchronous", 0, 0, 1, do_synchronous,
93 "--synchronous Turn on synchronous display mode" },
94 { "version", 'v', 0, 0, do_version,
95 "--version,-v Display the Wine version" },
96 { "winver", 0, 1, 1, VERSION_ParseWinVersion,
97 "--winver Version to imitate (win95,nt40,win31,nt2k,win98,nt351,win30,win20)" },
98 { NULL, 0, 0, 0, NULL, NULL } /* terminator */
102 static void do_help( const char *arg )
104 OPTIONS_Usage();
107 static void do_version( const char *arg )
109 MESSAGE( "%s\n", WINE_RELEASE_INFO );
110 ExitProcess(0);
113 static void do_synchronous( const char *arg )
115 Options.synchronous = TRUE;
118 static void do_desktop( const char *arg )
120 Options.desktopGeometry = xstrdup( arg );
123 static void do_display( const char *arg )
125 Options.display = xstrdup( arg );
128 static void do_dll( const char *arg )
130 if (Options.dllFlags)
132 Options.dllFlags = (char *) realloc ( Options.dllFlags,
133 strlen ( Options.dllFlags ) + strlen ( arg ) + 2 );
134 if ( !Options.dllFlags ) out_of_memory();
135 strcat ( Options.dllFlags, "+" );
136 strcat ( Options.dllFlags, arg );
138 else
140 Options.dllFlags = xstrdup( arg );
144 static void do_language( const char *arg )
146 SetEnvironmentVariableA( "LANGUAGE", arg );
149 static void do_managed( const char *arg )
151 Options.managed = TRUE;
154 static void do_debugmsg( const char *arg )
156 static const char * const debug_class_names[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
158 char *opt, *options = strdup(arg);
159 int i;
160 /* defined in relay32/relay386.c */
161 extern char **debug_relay_includelist;
162 extern char **debug_relay_excludelist;
163 /* defined in relay32/snoop.c */
164 extern char **debug_snoop_includelist;
165 extern char **debug_snoop_excludelist;
167 if (!(opt = strtok( options, "," ))) goto error;
170 unsigned char set = 0, clear = 0;
171 char *p = strchr( opt, '+' );
172 if (!p) p = strchr( opt, '-' );
173 if (!p || !p[1]) goto error;
174 if (p > opt)
176 for (i = 0; i < __DBCL_COUNT; i++)
178 int len = strlen(debug_class_names[i]);
179 if (len != (p - opt)) continue;
180 if (!memcmp( opt, debug_class_names[i], len )) /* found it */
182 if (*p == '+') set |= 1 << i;
183 else clear |= 1 << i;
184 break;
187 if (i == __DBCL_COUNT) goto error; /* class name not found */
189 else
191 if (*p == '+') set = ~0;
192 else clear = ~0;
193 if (!strncasecmp(p+1, "relay=", 6) ||
194 !strncasecmp(p+1, "snoop=", 6))
196 int i, l;
197 char *s, *s2, ***output, c;
199 if (strchr(p,','))
200 l=strchr(p,',')-p;
201 else
202 l=strlen(p);
203 set = ~0;
204 clear = 0;
205 output = (*p == '+') ?
206 ((*(p+1) == 'r') ?
207 &debug_relay_includelist :
208 &debug_snoop_includelist) :
209 ((*(p+1) == 'r') ?
210 &debug_relay_excludelist :
211 &debug_snoop_excludelist);
212 s = p + 7;
213 /* if there are n ':', there are n+1 modules, and we need
214 n+2 slots, last one being for the sentinel (NULL) */
215 i = 2;
216 while((s = strchr(s, ':'))) i++, s++;
217 *output = malloc(sizeof(char **) * i);
218 i = 0;
219 s = p + 7;
220 while((s2 = strchr(s, ':'))) {
221 c = *s2;
222 *s2 = '\0';
223 *((*output)+i) = _strupr(strdup(s));
224 *s2 = c;
225 s = s2 + 1;
226 i++;
228 c = *(p + l);
229 *(p + l) = '\0';
230 *((*output)+i) = _strupr(strdup(s));
231 *(p + l) = c;
232 *((*output)+i+1) = NULL;
233 *(p + 6) = '\0';
236 p++;
237 if (!strcmp( p, "all" )) p = ""; /* empty string means all */
238 wine_dbg_add_option( p, set, clear );
239 opt = strtok( NULL, "," );
240 } while(opt);
242 free( options );
243 return;
245 error:
246 MESSAGE("wine: Syntax: --debugmsg [class]+xxx,... or "
247 "-debugmsg [class]-xxx,...\n");
248 MESSAGE("Example: --debugmsg +all,warn-heap\n"
249 " turn on all messages except warning heap messages\n");
250 MESSAGE("Available message classes:\n");
251 for( i = 0; i < __DBCL_COUNT; i++) MESSAGE( "%-9s", debug_class_names[i] );
252 MESSAGE("\n\n");
253 ExitProcess(1);
257 static void remove_options( char *argv[], int pos, int count, int inherit )
259 if (inherit)
261 int i, len = 0;
262 for (i = 0; i < count; i++) len += strlen(argv[pos+i]) + 1;
263 if (inherit_str)
265 if (!(inherit_str = realloc( inherit_str, strlen(inherit_str) + 1 + len )))
266 out_of_memory();
267 strcat( inherit_str, " " );
269 else
271 if (!(inherit_str = malloc( len ))) out_of_memory();
272 inherit_str[0] = 0;
274 for (i = 0; i < count; i++)
276 strcat( inherit_str, argv[pos+i] );
277 if (i < count-1) strcat( inherit_str, " " );
280 while ((argv[pos] = argv[pos+count])) pos++;
283 /* parse options from the argv array and remove all the recognized ones */
284 static void parse_options( char *argv[] )
286 const struct option_descr *opt;
287 int i;
289 for (i = 0; argv[i]; i++)
291 const char *equalarg = NULL;
292 char *p = argv[i];
293 if (*p++ != '-') continue; /* not an option */
294 if (*p && !p[1]) /* short name */
296 if (*p == '-') break; /* "--" option */
297 for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
299 else /* long name */
301 const char *equal = strchr (p, '=');
302 if (*p == '-') p++;
303 /* check for the long name */
304 for (opt = option_table; opt->longname; opt++) {
305 /* Plain --option */
306 if (!strcmp( p, opt->longname )) break;
308 /* --option=value */
309 if (opt->has_arg &&
310 equal &&
311 strlen (opt->longname) == equal - p &&
312 !strncmp (p, opt->longname, equal - p)) {
313 equalarg = equal + 1;
314 break;
318 if (!opt->longname) continue;
320 if (equalarg)
322 opt->func( equalarg );
323 remove_options( argv, i, 1, opt->inherit );
325 else if (opt->has_arg && argv[i+1])
327 opt->func( argv[i+1] );
328 remove_options( argv, i, 2, opt->inherit );
330 else
332 opt->func( "" );
333 remove_options( argv, i, 1, opt->inherit );
335 i--;
339 /* inherit options from WINEOPTIONS variable */
340 static void inherit_options( char *buffer )
342 char *argv[256];
343 unsigned int n;
345 char *p = strtok( buffer, " \t" );
346 for (n = 0; n < sizeof(argv)/sizeof(argv[0])-1 && p; n++)
348 argv[n] = p;
349 p = strtok( NULL, " \t" );
351 argv[n] = NULL;
352 parse_options( argv );
353 if (argv[0]) /* an option remains */
355 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv[0] );
356 OPTIONS_Usage();
360 /***********************************************************************
361 * OPTIONS_Usage
363 void OPTIONS_Usage(void)
365 const struct option_descr *opt;
366 MESSAGE( "Usage: %s [options] [--] program_name [arguments]\n", argv0 );
367 MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
368 MESSAGE( "Options:\n" );
369 for (opt = option_table; opt->longname; opt++) MESSAGE( " %s\n", opt->usage );
370 ExitProcess(0);
373 /***********************************************************************
374 * OPTIONS_ParseOptions
376 void OPTIONS_ParseOptions( char *argv[] )
378 char buffer[1024];
379 int i;
381 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer, sizeof(buffer) ) && buffer[0])
382 inherit_options( buffer );
384 parse_options( argv + 1 );
386 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str );
388 /* check if any option remains */
389 for (i = 1; argv[i]; i++)
391 if (!strcmp( argv[i], "--" ))
393 remove_options( argv, i, 1, 0 );
394 break;
396 if (argv[i][0] == '-')
398 MESSAGE( "Unknown option '%s'\n\n", argv[i] );
399 OPTIONS_Usage();
403 /* count the resulting arguments */
404 app_argv = argv;
405 app_argc = 0;
406 while (argv[app_argc]) app_argc++;
410 /***********************************************************************
411 * __wine_get_main_args
413 * Return the argc/argv that the application should see.
414 * Used by the startup code generated in the .spec.c file.
416 int __wine_get_main_args( char ***argv )
418 *argv = app_argv;
419 return app_argc;
423 /***********************************************************************
424 * __wine_get_wmain_args
426 * Same as __wine_get_main_args but for Unicode.
428 int __wine_get_wmain_args( WCHAR ***argv )
430 if (!app_wargv)
432 int i;
433 WCHAR *p;
434 DWORD total = 0;
436 for (i = 0; i < app_argc; i++)
437 total += MultiByteToWideChar( CP_ACP, 0, app_argv[i], -1, NULL, 0 );
439 app_wargv = HeapAlloc( GetProcessHeap(), 0,
440 total * sizeof(WCHAR) + (app_argc + 1) * sizeof(*app_wargv) );
441 p = (WCHAR *)(app_wargv + app_argc + 1);
442 for (i = 0; i < app_argc; i++)
444 DWORD len = MultiByteToWideChar( CP_ACP, 0, app_argv[i], -1, p, total );
445 app_wargv[i] = p;
446 p += len;
447 total -= len;
449 app_wargv[app_argc] = NULL;
451 *argv = app_wargv;
452 return app_argc;