We must include windef.h before wtypes.h (directly or indirectly).
[wine.git] / misc / options.c
blob3b4c0fc8bb58e6c81a55a65fd7c8b62bc2fad2a7
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 /* Most Windows C/C++ compilers use something like this to */
29 /* access argc and argv globally: */
30 int _ARGC;
31 char **_ARGV;
33 /* default options */
34 struct options Options =
36 NULL, /* desktopGeometry */
37 NULL, /* display */
38 NULL, /* dllFlags */
39 FALSE, /* synchronous */
40 FALSE, /* Managed windows */
41 NULL /* Alternate config file name */
44 const char *argv0; /* the original argv[0] */
45 const char *full_argv0; /* the full path of argv[0] (if known) */
47 static char *inherit_str; /* options to pass to child processes */
49 static void out_of_memory(void) WINE_NORETURN;
50 static void out_of_memory(void)
52 MESSAGE( "Virtual memory exhausted\n" );
53 ExitProcess(1);
56 static char *xstrdup( const char *str )
58 char *ret = strdup( str );
59 if (!ret) out_of_memory();
60 return ret;
63 static void do_config( const char *arg );
64 static void do_debugmsg( const char *arg );
65 static void do_desktop( const char *arg );
66 static void do_display( const char *arg );
67 static void do_dll( const char *arg );
68 static void do_help( const char *arg );
69 static void do_language( const char *arg );
70 static void do_managed( const char *arg );
71 static void do_synchronous( const char *arg );
72 static void do_version( const char *arg );
74 static const struct option option_table[] =
76 { "config", 0, 1, 0, do_config,
77 "--config name Specify config file to use" },
78 { "debugmsg", 0, 1, 1, do_debugmsg,
79 "--debugmsg name Turn debugging-messages on or off" },
80 { "desktop", 0, 1, 1, do_desktop,
81 "--desktop geom Use a desktop window of the given geometry" },
82 { "display", 0, 1, 0, do_display,
83 "--display name Use the specified display" },
84 { "dll", 0, 1, 1, do_dll,
85 "--dll name Enable or disable built-in DLLs" },
86 { "dosver", 0, 1, 1, VERSION_ParseDosVersion,
87 "--dosver x.xx DOS version to imitate (e.g. 6.22). 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 " 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 (one of win31,win95,nt351,nt40)" },
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 char *p = argv[i];
252 if (*p++ != '-') continue; /* not an option */
253 if (*p && !p[1]) /* short name */
255 if (*p == '-') break; /* "--" option */
256 for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
258 else /* long name */
260 if (*p == '-') p++;
261 /* check for the long name */
262 for (opt = option_table; opt->longname; opt++)
263 if (!strcmp( p, opt->longname )) break;
265 if (!opt->longname) continue;
267 if (opt->has_arg && argv[i+1])
269 opt->func( argv[i+1] );
270 remove_options( argv, i, 2, opt->inherit );
272 else
274 opt->func( "" );
275 remove_options( argv, i, 1, opt->inherit );
277 i--;
281 /* inherit options from WINEOPTIONS variable */
282 static void inherit_options( char *buffer )
284 char *argv[256];
285 unsigned int n;
287 char *p = strtok( buffer, " \t" );
288 for (n = 0; n < sizeof(argv)/sizeof(argv[0])-1 && p; n++)
290 argv[n] = p;
291 p = strtok( NULL, " \t" );
293 argv[n] = NULL;
294 parse_options( argv );
295 if (argv[0]) /* an option remains */
297 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv[0] );
298 OPTIONS_Usage();
302 /***********************************************************************
303 * OPTIONS_Usage
305 void OPTIONS_Usage(void)
307 const struct option *opt;
308 MESSAGE( "Usage: %s [options] program_name [arguments]\n\n", argv0 );
309 MESSAGE( "Options:\n" );
310 for (opt = option_table; opt->longname; opt++) MESSAGE( " %s\n", opt->usage );
311 ExitProcess(0);
314 /***********************************************************************
315 * OPTIONS_ParseOptions
317 void OPTIONS_ParseOptions( char *argv[] )
319 char buffer[1024];
320 int i;
322 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer, sizeof(buffer) ) && buffer[0])
323 inherit_options( buffer );
325 parse_options( argv + 1 );
327 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str );
329 /* check if any option remains */
330 for (i = 1; argv[i]; i++)
332 if (!strcmp( argv[i], "--" ))
334 remove_options( argv, i, 1, 0 );
335 break;
337 if (argv[i][0] == '-')
339 MESSAGE( "Unknown option '%s'\n\n", argv[i] );
340 OPTIONS_Usage();
344 /* count the resulting arguments */
345 _ARGV = argv;
346 _ARGC = 0;
347 while (argv[_ARGC]) _ARGC++;