blob: 639c9e6ea143e2ea2b5ae9bd990730c715f4fa37 [file] [log] [blame]
Alexandre Julliardfe085682000-03-18 21:56:10 +00001/*
2 * Option parsing
3 *
4 * Copyright 2000 Alexandre Julliard
5 */
6
7#include "config.h"
8#include <string.h>
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +00009#include <stdlib.h>
Alexandre Julliardfe085682000-03-18 21:56:10 +000010
11#include "winbase.h"
Alexandre Julliard909eff92000-12-15 03:38:11 +000012#include "winnls.h"
Guy L. Albertelli8d91b502001-01-04 19:37:37 +000013#include "ntddk.h"
Alexandre Julliard85423c62000-11-08 04:28:54 +000014#include "wine/library.h"
Alexandre Julliardfe085682000-03-18 21:56:10 +000015#include "options.h"
Alexandre Julliardf899ef02001-07-23 00:04:00 +000016#include "module.h"
Alexandre Julliardfe085682000-03-18 21:56:10 +000017#include "version.h"
18#include "debugtools.h"
19
Alexandre Julliardca43a642001-01-10 23:56:59 +000020struct option_descr
Alexandre Julliardfe085682000-03-18 21:56:10 +000021{
22 const char *longname;
23 char shortname;
24 int has_arg;
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +000025 int inherit;
Alexandre Julliardfe085682000-03-18 21:56:10 +000026 void (*func)( const char *arg );
27 const char *usage;
28};
29
Alexandre Julliardc192ba22000-05-29 21:25:10 +000030/* default options */
31struct options Options =
32{
Alexandre Julliardca43a642001-01-10 23:56:59 +000033 FALSE /* Managed windows */
Alexandre Julliardc192ba22000-05-29 21:25:10 +000034};
35
Alexandre Julliarda3e0cfc2000-07-16 18:21:34 +000036const char *argv0; /* the original argv[0] */
37const char *full_argv0; /* the full path of argv[0] (if known) */
Alexandre Julliardc192ba22000-05-29 21:25:10 +000038
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +000039static char *inherit_str; /* options to pass to child processes */
40
41static void out_of_memory(void) WINE_NORETURN;
42static void out_of_memory(void)
43{
44 MESSAGE( "Virtual memory exhausted\n" );
45 ExitProcess(1);
46}
47
Alexandre Julliard94613ab2000-11-05 04:51:34 +000048static void do_debugmsg( const char *arg );
Alexandre Julliardfe085682000-03-18 21:56:10 +000049static void do_help( const char *arg );
50static void do_managed( const char *arg );
Alexandre Julliardfe085682000-03-18 21:56:10 +000051static void do_version( const char *arg );
52
Alexandre Julliardca43a642001-01-10 23:56:59 +000053static const struct option_descr option_table[] =
Alexandre Julliardfe085682000-03-18 21:56:10 +000054{
Alexandre Julliard94613ab2000-11-05 04:51:34 +000055 { "debugmsg", 0, 1, 1, do_debugmsg,
Alexandre Julliardfe085682000-03-18 21:56:10 +000056 "--debugmsg name Turn debugging-messages on or off" },
Alexandre Julliardb9c9cdc2001-03-20 02:11:08 +000057 { "dll", 0, 1, 1, MODULE_AddLoadOrderOption,
Alexandre Julliardfe085682000-03-18 21:56:10 +000058 "--dll name Enable or disable built-in DLLs" },
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +000059 { "dosver", 0, 1, 1, VERSION_ParseDosVersion,
Andreas Mohrcb454382000-11-27 01:39:05 +000060 "--dosver x.xx DOS version to imitate (e.g. 6.22)\n"
61 " Only valid with --winver win31" },
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +000062 { "help", 'h', 0, 0, do_help,
Alexandre Julliardfe085682000-03-18 21:56:10 +000063 "--help,-h Show this help message" },
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +000064 { "managed", 0, 0, 0, do_managed,
Alexandre Julliardfe085682000-03-18 21:56:10 +000065 "--managed Allow the window manager to manage created windows" },
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +000066 { "version", 'v', 0, 0, do_version,
Alexandre Julliardfe085682000-03-18 21:56:10 +000067 "--version,-v Display the Wine version" },
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +000068 { "winver", 0, 1, 1, VERSION_ParseWinVersion,
Francois Gouget4ae71952001-11-06 00:49:48 +000069 "--winver Version to imitate (win95,win98,winme,nt351,nt40,win2k,winxp,win20,win30,win31)" },
Patrik Stridvalla9f6a9d2000-10-24 02:22:16 +000070 { NULL, 0, 0, 0, NULL, NULL } /* terminator */
Alexandre Julliardfe085682000-03-18 21:56:10 +000071};
72
73
74static void do_help( const char *arg )
75{
76 OPTIONS_Usage();
77}
78
79static void do_version( const char *arg )
80{
81 MESSAGE( "%s\n", WINE_RELEASE_INFO );
82 ExitProcess(0);
83}
84
Alexandre Julliardfe085682000-03-18 21:56:10 +000085static void do_managed( const char *arg )
86{
87 Options.managed = TRUE;
88}
89
Alexandre Julliard94613ab2000-11-05 04:51:34 +000090static void do_debugmsg( const char *arg )
91{
Alexandre Julliard94613ab2000-11-05 04:51:34 +000092 static const char * const debug_class_names[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
93
94 char *opt, *options = strdup(arg);
95 int i;
Guy L. Albertelli8d91b502001-01-04 19:37:37 +000096 /* defined in relay32/relay386.c */
97 extern char **debug_relay_includelist;
98 extern char **debug_relay_excludelist;
99 /* defined in relay32/snoop.c */
100 extern char **debug_snoop_includelist;
101 extern char **debug_snoop_excludelist;
Alexandre Julliard94613ab2000-11-05 04:51:34 +0000102
103 if (!(opt = strtok( options, "," ))) goto error;
104 do
105 {
106 unsigned char set = 0, clear = 0;
107 char *p = strchr( opt, '+' );
108 if (!p) p = strchr( opt, '-' );
109 if (!p || !p[1]) goto error;
110 if (p > opt)
111 {
112 for (i = 0; i < __DBCL_COUNT; i++)
113 {
114 int len = strlen(debug_class_names[i]);
115 if (len != (p - opt)) continue;
116 if (!memcmp( opt, debug_class_names[i], len )) /* found it */
117 {
118 if (*p == '+') set |= 1 << i;
119 else clear |= 1 << i;
120 break;
121 }
122 }
123 if (i == __DBCL_COUNT) goto error; /* class name not found */
124 }
125 else
126 {
127 if (*p == '+') set = ~0;
128 else clear = ~0;
Guy L. Albertelli8d91b502001-01-04 19:37:37 +0000129 if (!strncasecmp(p+1, "relay=", 6) ||
130 !strncasecmp(p+1, "snoop=", 6))
131 {
132 int i, l;
133 char *s, *s2, ***output, c;
134
135 if (strchr(p,','))
136 l=strchr(p,',')-p;
137 else
138 l=strlen(p);
139 set = ~0;
140 clear = 0;
141 output = (*p == '+') ?
142 ((*(p+1) == 'r') ?
143 &debug_relay_includelist :
144 &debug_snoop_includelist) :
145 ((*(p+1) == 'r') ?
146 &debug_relay_excludelist :
147 &debug_snoop_excludelist);
148 s = p + 7;
149 /* if there are n ':', there are n+1 modules, and we need
150 n+2 slots, last one being for the sentinel (NULL) */
151 i = 2;
152 while((s = strchr(s, ':'))) i++, s++;
153 *output = malloc(sizeof(char **) * i);
154 i = 0;
155 s = p + 7;
156 while((s2 = strchr(s, ':'))) {
157 c = *s2;
158 *s2 = '\0';
159 *((*output)+i) = _strupr(strdup(s));
160 *s2 = c;
161 s = s2 + 1;
162 i++;
163 }
164 c = *(p + l);
165 *(p + l) = '\0';
166 *((*output)+i) = _strupr(strdup(s));
167 *(p + l) = c;
168 *((*output)+i+1) = NULL;
169 *(p + 6) = '\0';
170 }
Alexandre Julliard94613ab2000-11-05 04:51:34 +0000171 }
172 p++;
173 if (!strcmp( p, "all" )) p = ""; /* empty string means all */
174 wine_dbg_add_option( p, set, clear );
175 opt = strtok( NULL, "," );
176 } while(opt);
177
178 free( options );
179 return;
180
181 error:
182 MESSAGE("wine: Syntax: --debugmsg [class]+xxx,... or "
183 "-debugmsg [class]-xxx,...\n");
184 MESSAGE("Example: --debugmsg +all,warn-heap\n"
185 " turn on all messages except warning heap messages\n");
186 MESSAGE("Available message classes:\n");
187 for( i = 0; i < __DBCL_COUNT; i++) MESSAGE( "%-9s", debug_class_names[i] );
188 MESSAGE("\n\n");
189 ExitProcess(1);
190}
191
192
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +0000193static void remove_options( char *argv[], int pos, int count, int inherit )
Alexandre Julliardfe085682000-03-18 21:56:10 +0000194{
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +0000195 if (inherit)
196 {
197 int i, len = 0;
198 for (i = 0; i < count; i++) len += strlen(argv[pos+i]) + 1;
199 if (inherit_str)
200 {
Alexandre Julliard45fccb82000-06-07 02:03:54 +0000201 if (!(inherit_str = realloc( inherit_str, strlen(inherit_str) + 1 + len )))
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +0000202 out_of_memory();
203 strcat( inherit_str, " " );
204 }
205 else
206 {
207 if (!(inherit_str = malloc( len ))) out_of_memory();
208 inherit_str[0] = 0;
209 }
210 for (i = 0; i < count; i++)
211 {
212 strcat( inherit_str, argv[pos+i] );
213 if (i < count-1) strcat( inherit_str, " " );
214 }
215 }
Alexandre Julliardfe085682000-03-18 21:56:10 +0000216 while ((argv[pos] = argv[pos+count])) pos++;
Alexandre Julliardfe085682000-03-18 21:56:10 +0000217}
218
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +0000219/* parse options from the argv array and remove all the recognized ones */
220static void parse_options( char *argv[] )
Alexandre Julliardfe085682000-03-18 21:56:10 +0000221{
Alexandre Julliardca43a642001-01-10 23:56:59 +0000222 const struct option_descr *opt;
Alexandre Julliardfe085682000-03-18 21:56:10 +0000223 int i;
224
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +0000225 for (i = 0; argv[i]; i++)
Alexandre Julliardfe085682000-03-18 21:56:10 +0000226 {
Morten Welinder87093f52000-12-18 03:49:49 +0000227 const char *equalarg = NULL;
Alexandre Julliardfe085682000-03-18 21:56:10 +0000228 char *p = argv[i];
229 if (*p++ != '-') continue; /* not an option */
230 if (*p && !p[1]) /* short name */
231 {
232 if (*p == '-') break; /* "--" option */
233 for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
234 }
235 else /* long name */
236 {
Morten Welinder87093f52000-12-18 03:49:49 +0000237 const char *equal = strchr (p, '=');
Alexandre Julliardfe085682000-03-18 21:56:10 +0000238 if (*p == '-') p++;
239 /* check for the long name */
Morten Welinder87093f52000-12-18 03:49:49 +0000240 for (opt = option_table; opt->longname; opt++) {
241 /* Plain --option */
Alexandre Julliardfe085682000-03-18 21:56:10 +0000242 if (!strcmp( p, opt->longname )) break;
Morten Welinder87093f52000-12-18 03:49:49 +0000243
244 /* --option=value */
245 if (opt->has_arg &&
246 equal &&
247 strlen (opt->longname) == equal - p &&
248 !strncmp (p, opt->longname, equal - p)) {
249 equalarg = equal + 1;
250 break;
251 }
252 }
Alexandre Julliardfe085682000-03-18 21:56:10 +0000253 }
254 if (!opt->longname) continue;
255
Morten Welinder87093f52000-12-18 03:49:49 +0000256 if (equalarg)
257 {
258 opt->func( equalarg );
259 remove_options( argv, i, 1, opt->inherit );
260 }
261 else if (opt->has_arg && argv[i+1])
Alexandre Julliardfe085682000-03-18 21:56:10 +0000262 {
263 opt->func( argv[i+1] );
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +0000264 remove_options( argv, i, 2, opt->inherit );
Alexandre Julliardfe085682000-03-18 21:56:10 +0000265 }
266 else
267 {
268 opt->func( "" );
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +0000269 remove_options( argv, i, 1, opt->inherit );
Alexandre Julliardfe085682000-03-18 21:56:10 +0000270 }
271 i--;
272 }
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +0000273}
274
275/* inherit options from WINEOPTIONS variable */
276static void inherit_options( char *buffer )
277{
278 char *argv[256];
Alexandre Julliard908464d2000-11-01 03:11:12 +0000279 unsigned int n;
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +0000280
281 char *p = strtok( buffer, " \t" );
Alexandre Julliard908464d2000-11-01 03:11:12 +0000282 for (n = 0; n < sizeof(argv)/sizeof(argv[0])-1 && p; n++)
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +0000283 {
Alexandre Julliard908464d2000-11-01 03:11:12 +0000284 argv[n] = p;
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +0000285 p = strtok( NULL, " \t" );
286 }
Alexandre Julliard908464d2000-11-01 03:11:12 +0000287 argv[n] = NULL;
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +0000288 parse_options( argv );
289 if (argv[0]) /* an option remains */
290 {
291 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv[0] );
292 OPTIONS_Usage();
293 }
294}
295
296/***********************************************************************
297 * OPTIONS_Usage
298 */
299void OPTIONS_Usage(void)
300{
Alexandre Julliardca43a642001-01-10 23:56:59 +0000301 const struct option_descr *opt;
Alexandre Julliard26320d12001-03-23 23:45:45 +0000302 MESSAGE( "%s\n\n", WINE_RELEASE_INFO );
Andreas Mohr1f192c12000-12-15 21:28:47 +0000303 MESSAGE( "Usage: %s [options] [--] program_name [arguments]\n", argv0 );
304 MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +0000305 MESSAGE( "Options:\n" );
306 for (opt = option_table; opt->longname; opt++) MESSAGE( " %s\n", opt->usage );
307 ExitProcess(0);
308}
309
310/***********************************************************************
311 * OPTIONS_ParseOptions
312 */
313void OPTIONS_ParseOptions( char *argv[] )
314{
315 char buffer[1024];
316 int i;
317
318 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer, sizeof(buffer) ) && buffer[0])
319 inherit_options( buffer );
320
321 parse_options( argv + 1 );
322
323 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str );
Alexandre Julliardfe085682000-03-18 21:56:10 +0000324
325 /* check if any option remains */
326 for (i = 1; argv[i]; i++)
327 {
328 if (!strcmp( argv[i], "--" ))
329 {
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +0000330 remove_options( argv, i, 1, 0 );
Alexandre Julliardfe085682000-03-18 21:56:10 +0000331 break;
332 }
333 if (argv[i][0] == '-')
334 {
Alexandre Julliardeeaae3c2000-05-30 17:51:44 +0000335 MESSAGE( "Unknown option '%s'\n\n", argv[i] );
Alexandre Julliardfe085682000-03-18 21:56:10 +0000336 OPTIONS_Usage();
337 }
338 }
Alexandre Julliard909eff92000-12-15 03:38:11 +0000339}