The debugger "Auto" registry value should be a string.
[wine/multimedia.git] / misc / options.c
bloba79b1479faa22929c2a9925cd85e55df10836246
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 "wine/library.h"
13 #include "main.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;
47 static void out_of_memory(void) WINE_NORETURN;
48 static void out_of_memory(void)
50 MESSAGE( "Virtual memory exhausted\n" );
51 ExitProcess(1);
54 static char *xstrdup( const char *str )
56 char *ret = strdup( str );
57 if (!ret) out_of_memory();
58 return ret;
61 static void do_config( const char *arg );
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 option_table[] =
74 { "config", 0, 1, 0, do_config,
75 "--config name Specify config file to use" },
76 { "debugmsg", 0, 1, 1, do_debugmsg,
77 "--debugmsg name Turn debugging-messages on or off" },
78 { "desktop", 0, 1, 1, do_desktop,
79 "--desktop geom Use a desktop window of the given geometry" },
80 { "display", 0, 1, 0, do_display,
81 "--display name Use the specified display" },
82 { "dll", 0, 1, 1, do_dll,
83 "--dll name Enable or disable built-in DLLs" },
84 { "dosver", 0, 1, 1, VERSION_ParseDosVersion,
85 "--dosver x.xx DOS version to imitate (e.g. 6.22). Only valid with --winver win31" },
86 { "help", 'h', 0, 0, do_help,
87 "--help,-h Show this help message" },
88 { "language", 0, 1, 1, do_language,
89 "--language xx Set the language (one of Br,Ca,Cs,Cy,Da,De,En,Eo,Es,Fi,Fr,Ga,Gd,Gv,\n"
90 " Hr,Hu,It,Ja,Ko,Kw,Nl,No,Pl,Pt,Sk,Sv,Ru,Wa)" },
91 { "managed", 0, 0, 0, do_managed,
92 "--managed Allow the window manager to manage created windows" },
93 { "synchronous", 0, 0, 1, do_synchronous,
94 "--synchronous Turn on synchronous display mode" },
95 { "version", 'v', 0, 0, do_version,
96 "--version,-v Display the Wine version" },
97 { "winver", 0, 1, 1, VERSION_ParseWinVersion,
98 "--winver Version to imitate (one of win31,win95,nt351,nt40)" },
99 { NULL, 0, 0, 0, NULL, NULL } /* terminator */
103 static void do_help( const char *arg )
105 OPTIONS_Usage();
108 static void do_version( const char *arg )
110 MESSAGE( "%s\n", WINE_RELEASE_INFO );
111 ExitProcess(0);
114 static void do_synchronous( const char *arg )
116 Options.synchronous = TRUE;
119 static void do_desktop( const char *arg )
121 Options.desktopGeometry = xstrdup( arg );
124 static void do_display( const char *arg )
126 Options.display = xstrdup( arg );
129 static void do_dll( const char *arg )
131 if (Options.dllFlags)
133 Options.dllFlags = (char *) realloc ( Options.dllFlags,
134 strlen ( Options.dllFlags ) + strlen ( arg ) + 2 );
135 if ( !Options.dllFlags ) out_of_memory();
136 strcat ( Options.dllFlags, "+" );
137 strcat ( Options.dllFlags, arg );
139 else
141 Options.dllFlags = xstrdup( arg );
145 static void do_language( const char *arg )
147 SetEnvironmentVariableA( "LANGUAGE", arg );
150 static void do_managed( const char *arg )
152 Options.managed = TRUE;
155 static void do_config( const char *arg )
157 Options.configFileName = xstrdup( arg );
160 static void do_debugmsg( const char *arg )
162 static const char * const debug_class_names[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
164 char *opt, *options = strdup(arg);
165 int i;
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;
194 p++;
195 if (!strcmp( p, "all" )) p = ""; /* empty string means all */
196 wine_dbg_add_option( p, set, clear );
197 opt = strtok( NULL, "," );
198 } while(opt);
200 free( options );
201 return;
203 error:
204 MESSAGE("wine: Syntax: --debugmsg [class]+xxx,... or "
205 "-debugmsg [class]-xxx,...\n");
206 MESSAGE("Example: --debugmsg +all,warn-heap\n"
207 " turn on all messages except warning heap messages\n");
208 MESSAGE("Available message classes:\n");
209 for( i = 0; i < __DBCL_COUNT; i++) MESSAGE( "%-9s", debug_class_names[i] );
210 MESSAGE("\n\n");
211 ExitProcess(1);
215 static void remove_options( char *argv[], int pos, int count, int inherit )
217 if (inherit)
219 int i, len = 0;
220 for (i = 0; i < count; i++) len += strlen(argv[pos+i]) + 1;
221 if (inherit_str)
223 if (!(inherit_str = realloc( inherit_str, strlen(inherit_str) + 1 + len )))
224 out_of_memory();
225 strcat( inherit_str, " " );
227 else
229 if (!(inherit_str = malloc( len ))) out_of_memory();
230 inherit_str[0] = 0;
232 for (i = 0; i < count; i++)
234 strcat( inherit_str, argv[pos+i] );
235 if (i < count-1) strcat( inherit_str, " " );
238 while ((argv[pos] = argv[pos+count])) pos++;
241 /* parse options from the argv array and remove all the recognized ones */
242 static void parse_options( char *argv[] )
244 const struct option *opt;
245 int i;
247 for (i = 0; argv[i]; i++)
249 char *p = argv[i];
250 if (*p++ != '-') continue; /* not an option */
251 if (*p && !p[1]) /* short name */
253 if (*p == '-') break; /* "--" option */
254 for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
256 else /* long name */
258 if (*p == '-') p++;
259 /* check for the long name */
260 for (opt = option_table; opt->longname; opt++)
261 if (!strcmp( p, opt->longname )) break;
263 if (!opt->longname) continue;
265 if (opt->has_arg && argv[i+1])
267 opt->func( argv[i+1] );
268 remove_options( argv, i, 2, opt->inherit );
270 else
272 opt->func( "" );
273 remove_options( argv, i, 1, opt->inherit );
275 i--;
279 /* inherit options from WINEOPTIONS variable */
280 static void inherit_options( char *buffer )
282 char *argv[256];
283 unsigned int n;
285 char *p = strtok( buffer, " \t" );
286 for (n = 0; n < sizeof(argv)/sizeof(argv[0])-1 && p; n++)
288 argv[n] = p;
289 p = strtok( NULL, " \t" );
291 argv[n] = NULL;
292 parse_options( argv );
293 if (argv[0]) /* an option remains */
295 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv[0] );
296 OPTIONS_Usage();
300 /***********************************************************************
301 * OPTIONS_Usage
303 void OPTIONS_Usage(void)
305 const struct option *opt;
306 MESSAGE( "Usage: %s [options] program_name [arguments]\n\n", argv0 );
307 MESSAGE( "Options:\n" );
308 for (opt = option_table; opt->longname; opt++) MESSAGE( " %s\n", opt->usage );
309 ExitProcess(0);
312 /***********************************************************************
313 * OPTIONS_ParseOptions
315 void OPTIONS_ParseOptions( char *argv[] )
317 char buffer[1024];
318 int i;
320 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer, sizeof(buffer) ) && buffer[0])
321 inherit_options( buffer );
323 parse_options( argv + 1 );
325 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str );
327 /* check if any option remains */
328 for (i = 1; argv[i]; i++)
330 if (!strcmp( argv[i], "--" ))
332 remove_options( argv, i, 1, 0 );
333 break;
335 if (argv[i][0] == '-')
337 MESSAGE( "Unknown option '%s'\n\n", argv[i] );
338 OPTIONS_Usage();
342 /* count the resulting arguments */
343 app_argv = argv;
344 app_argc = 0;
345 while (argv[app_argc]) app_argc++;
349 /***********************************************************************
350 * __wine_get_main_args
352 * Return the argc/argv that the application should see.
353 * Used by the startup code generated in the .spec.c file.
355 int __wine_get_main_args( char ***argv )
357 *argv = app_argv;
358 return app_argc;