Separated the MZ loader and core DOS VM into dlls/winedos.
[wine.git] / misc / options.c
blobca8d99a8556ef8317c3dee8d00dbd6ec13495645
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 "wine/library.h"
14 #include "options.h"
15 #include "version.h"
16 #include "debugtools.h"
18 struct option
20 const char *longname;
21 char shortname;
22 int has_arg;
23 int inherit;
24 void (*func)( const char *arg );
25 const char *usage;
28 /* default options */
29 struct options Options =
31 NULL, /* desktopGeometry */
32 NULL, /* display */
33 NULL, /* dllFlags */
34 FALSE, /* synchronous */
35 FALSE, /* Managed windows */
36 NULL /* Alternate config file name */
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_config( const char *arg );
63 static void do_debugmsg( const char *arg );
64 static void do_desktop( const char *arg );
65 static void do_display( const char *arg );
66 static void do_dll( const char *arg );
67 static void do_help( const char *arg );
68 static void do_language( const char *arg );
69 static void do_managed( const char *arg );
70 static void do_synchronous( const char *arg );
71 static void do_version( const char *arg );
73 static const struct option option_table[] =
75 { "config", 0, 1, 0, do_config,
76 "--config name Specify config file to use" },
77 { "debugmsg", 0, 1, 1, do_debugmsg,
78 "--debugmsg name Turn debugging-messages on or off" },
79 { "desktop", 0, 1, 1, do_desktop,
80 "--desktop geom Use a desktop window of the given geometry" },
81 { "display", 0, 1, 0, do_display,
82 "--display name Use the specified display" },
83 { "dll", 0, 1, 1, do_dll,
84 "--dll name Enable or disable built-in DLLs" },
85 { "dosver", 0, 1, 1, VERSION_ParseDosVersion,
86 "--dosver x.xx DOS version to imitate (e.g. 6.22)\n"
87 " Only valid with --winver win31" },
88 { "help", 'h', 0, 0, do_help,
89 "--help,-h Show this help message" },
90 { "language", 0, 1, 1, do_language,
91 "--language xx Set the language (one of Br,Ca,Cs,Cy,Da,De,En,Eo,Es,Fi,Fr,Ga,Gd,Gv,\n"
92 " He,Hr,Hu,It,Ja,Ko,Kw,Nl,No,Pl,Pt,Sk,Sv,Ru,Wa)" },
93 { "managed", 0, 0, 0, do_managed,
94 "--managed Allow the window manager to manage created windows" },
95 { "synchronous", 0, 0, 1, do_synchronous,
96 "--synchronous Turn on synchronous display mode" },
97 { "version", 'v', 0, 0, do_version,
98 "--version,-v Display the Wine version" },
99 { "winver", 0, 1, 1, VERSION_ParseWinVersion,
100 "--winver Version to imitate (win95,nt40,win31,nt2k,win98,nt351,win30,win20)" },
101 { NULL, 0, 0, 0, NULL, NULL } /* terminator */
105 static void do_help( const char *arg )
107 OPTIONS_Usage();
110 static void do_version( const char *arg )
112 MESSAGE( "%s\n", WINE_RELEASE_INFO );
113 ExitProcess(0);
116 static void do_synchronous( const char *arg )
118 Options.synchronous = TRUE;
121 static void do_desktop( const char *arg )
123 Options.desktopGeometry = xstrdup( arg );
126 static void do_display( const char *arg )
128 Options.display = xstrdup( arg );
131 static void do_dll( const char *arg )
133 if (Options.dllFlags)
135 Options.dllFlags = (char *) realloc ( Options.dllFlags,
136 strlen ( Options.dllFlags ) + strlen ( arg ) + 2 );
137 if ( !Options.dllFlags ) out_of_memory();
138 strcat ( Options.dllFlags, "+" );
139 strcat ( Options.dllFlags, arg );
141 else
143 Options.dllFlags = xstrdup( arg );
147 static void do_language( const char *arg )
149 SetEnvironmentVariableA( "LANGUAGE", arg );
152 static void do_managed( const char *arg )
154 Options.managed = TRUE;
157 static void do_config( const char *arg )
159 Options.configFileName = xstrdup( arg );
162 static void do_debugmsg( const char *arg )
164 static const char * const debug_class_names[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
166 char *opt, *options = strdup(arg);
167 int i;
169 if (!(opt = strtok( options, "," ))) goto error;
172 unsigned char set = 0, clear = 0;
173 char *p = strchr( opt, '+' );
174 if (!p) p = strchr( opt, '-' );
175 if (!p || !p[1]) goto error;
176 if (p > opt)
178 for (i = 0; i < __DBCL_COUNT; i++)
180 int len = strlen(debug_class_names[i]);
181 if (len != (p - opt)) continue;
182 if (!memcmp( opt, debug_class_names[i], len )) /* found it */
184 if (*p == '+') set |= 1 << i;
185 else clear |= 1 << i;
186 break;
189 if (i == __DBCL_COUNT) goto error; /* class name not found */
191 else
193 if (*p == '+') set = ~0;
194 else clear = ~0;
196 p++;
197 if (!strcmp( p, "all" )) p = ""; /* empty string means all */
198 wine_dbg_add_option( p, set, clear );
199 opt = strtok( NULL, "," );
200 } while(opt);
202 free( options );
203 return;
205 error:
206 MESSAGE("wine: Syntax: --debugmsg [class]+xxx,... or "
207 "-debugmsg [class]-xxx,...\n");
208 MESSAGE("Example: --debugmsg +all,warn-heap\n"
209 " turn on all messages except warning heap messages\n");
210 MESSAGE("Available message classes:\n");
211 for( i = 0; i < __DBCL_COUNT; i++) MESSAGE( "%-9s", debug_class_names[i] );
212 MESSAGE("\n\n");
213 ExitProcess(1);
217 static void remove_options( char *argv[], int pos, int count, int inherit )
219 if (inherit)
221 int i, len = 0;
222 for (i = 0; i < count; i++) len += strlen(argv[pos+i]) + 1;
223 if (inherit_str)
225 if (!(inherit_str = realloc( inherit_str, strlen(inherit_str) + 1 + len )))
226 out_of_memory();
227 strcat( inherit_str, " " );
229 else
231 if (!(inherit_str = malloc( len ))) out_of_memory();
232 inherit_str[0] = 0;
234 for (i = 0; i < count; i++)
236 strcat( inherit_str, argv[pos+i] );
237 if (i < count-1) strcat( inherit_str, " " );
240 while ((argv[pos] = argv[pos+count])) pos++;
243 /* parse options from the argv array and remove all the recognized ones */
244 static void parse_options( char *argv[] )
246 const struct option *opt;
247 int i;
249 for (i = 0; argv[i]; i++)
251 const char *equalarg = NULL;
252 char *p = argv[i];
253 if (*p++ != '-') continue; /* not an option */
254 if (*p && !p[1]) /* short name */
256 if (*p == '-') break; /* "--" option */
257 for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
259 else /* long name */
261 const char *equal = strchr (p, '=');
262 if (*p == '-') p++;
263 /* check for the long name */
264 for (opt = option_table; opt->longname; opt++) {
265 /* Plain --option */
266 if (!strcmp( p, opt->longname )) break;
268 /* --option=value */
269 if (opt->has_arg &&
270 equal &&
271 strlen (opt->longname) == equal - p &&
272 !strncmp (p, opt->longname, equal - p)) {
273 equalarg = equal + 1;
274 break;
278 if (!opt->longname) continue;
280 if (equalarg)
282 opt->func( equalarg );
283 remove_options( argv, i, 1, opt->inherit );
285 else if (opt->has_arg && argv[i+1])
287 opt->func( argv[i+1] );
288 remove_options( argv, i, 2, opt->inherit );
290 else
292 opt->func( "" );
293 remove_options( argv, i, 1, opt->inherit );
295 i--;
299 /* inherit options from WINEOPTIONS variable */
300 static void inherit_options( char *buffer )
302 char *argv[256];
303 unsigned int n;
305 char *p = strtok( buffer, " \t" );
306 for (n = 0; n < sizeof(argv)/sizeof(argv[0])-1 && p; n++)
308 argv[n] = p;
309 p = strtok( NULL, " \t" );
311 argv[n] = NULL;
312 parse_options( argv );
313 if (argv[0]) /* an option remains */
315 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv[0] );
316 OPTIONS_Usage();
320 /***********************************************************************
321 * OPTIONS_Usage
323 void OPTIONS_Usage(void)
325 const struct option *opt;
326 MESSAGE( "Usage: %s [options] [--] program_name [arguments]\n", argv0 );
327 MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
328 MESSAGE( "Options:\n" );
329 for (opt = option_table; opt->longname; opt++) MESSAGE( " %s\n", opt->usage );
330 ExitProcess(0);
333 /***********************************************************************
334 * OPTIONS_ParseOptions
336 void OPTIONS_ParseOptions( char *argv[] )
338 char buffer[1024];
339 int i;
341 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer, sizeof(buffer) ) && buffer[0])
342 inherit_options( buffer );
344 parse_options( argv + 1 );
346 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str );
348 /* check if any option remains */
349 for (i = 1; argv[i]; i++)
351 if (!strcmp( argv[i], "--" ))
353 remove_options( argv, i, 1, 0 );
354 break;
356 if (argv[i][0] == '-')
358 MESSAGE( "Unknown option '%s'\n\n", argv[i] );
359 OPTIONS_Usage();
363 /* count the resulting arguments */
364 app_argv = argv;
365 app_argc = 0;
366 while (argv[app_argc]) app_argc++;
370 /***********************************************************************
371 * __wine_get_main_args
373 * Return the argc/argv that the application should see.
374 * Used by the startup code generated in the .spec.c file.
376 int __wine_get_main_args( char ***argv )
378 *argv = app_argv;
379 return app_argc;
383 /***********************************************************************
384 * __wine_get_wmain_args
386 * Same as __wine_get_main_args but for Unicode.
388 int __wine_get_wmain_args( WCHAR ***argv )
390 if (!app_wargv)
392 int i;
393 WCHAR *p;
394 DWORD total = 0;
396 for (i = 0; i < app_argc; i++)
397 total += MultiByteToWideChar( CP_ACP, 0, app_argv[i], -1, NULL, 0 );
399 app_wargv = HeapAlloc( GetProcessHeap(), 0,
400 total * sizeof(WCHAR) + (app_argc + 1) * sizeof(*app_wargv) );
401 p = (WCHAR *)(app_wargv + app_argc + 1);
402 for (i = 0; i < app_argc; i++)
404 DWORD len = MultiByteToWideChar( CP_ACP, 0, app_argv[i], -1, p, total );
405 app_wargv[i] = p;
406 p += len;
407 total -= len;
409 app_wargv[app_argc] = NULL;
411 *argv = app_wargv;
412 return app_argc;