Copy the first 128 colors from the default colormap to Wine's private
[wine.git] / misc / options.c
blobb86f882eeefbb266503b19a1cc5999fc28523b86
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 "loadorder.h"
17 #include "version.h"
18 #include "debugtools.h"
20 struct option_descr
22 const char *longname;
23 char shortname;
24 int has_arg;
25 int inherit;
26 void (*func)( const char *arg );
27 const char *usage;
30 /* default options */
31 struct options Options =
33 FALSE /* Managed windows */
36 const char *argv0; /* the original argv[0] */
37 const char *full_argv0; /* the full path of argv[0] (if known) */
39 static char *inherit_str; /* options to pass to child processes */
41 static int app_argc; /* argc/argv to pass to application */
42 static char **app_argv;
43 static WCHAR **app_wargv;
45 static void out_of_memory(void) WINE_NORETURN;
46 static void out_of_memory(void)
48 MESSAGE( "Virtual memory exhausted\n" );
49 ExitProcess(1);
52 static void do_debugmsg( const char *arg );
53 static void do_help( const char *arg );
54 static void do_managed( const char *arg );
55 static void do_version( const char *arg );
57 static const struct option_descr option_table[] =
59 { "debugmsg", 0, 1, 1, do_debugmsg,
60 "--debugmsg name Turn debugging-messages on or off" },
61 { "dll", 0, 1, 1, MODULE_AddLoadOrderOption,
62 "--dll name Enable or disable built-in DLLs" },
63 { "dosver", 0, 1, 1, VERSION_ParseDosVersion,
64 "--dosver x.xx DOS version to imitate (e.g. 6.22)\n"
65 " Only valid with --winver win31" },
66 { "help", 'h', 0, 0, do_help,
67 "--help,-h Show this help message" },
68 { "managed", 0, 0, 0, do_managed,
69 "--managed Allow the window manager to manage created windows" },
70 { "version", 'v', 0, 0, do_version,
71 "--version,-v Display the Wine version" },
72 { "winver", 0, 1, 1, VERSION_ParseWinVersion,
73 "--winver Version to imitate (win95,nt40,win31,nt2k,win98,nt351,win30,win20)" },
74 { NULL, 0, 0, 0, NULL, NULL } /* terminator */
78 static void do_help( const char *arg )
80 OPTIONS_Usage();
83 static void do_version( const char *arg )
85 MESSAGE( "%s\n", WINE_RELEASE_INFO );
86 ExitProcess(0);
89 static void do_managed( const char *arg )
91 Options.managed = TRUE;
94 static void do_debugmsg( const char *arg )
96 static const char * const debug_class_names[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
98 char *opt, *options = strdup(arg);
99 int i;
100 /* defined in relay32/relay386.c */
101 extern char **debug_relay_includelist;
102 extern char **debug_relay_excludelist;
103 /* defined in relay32/snoop.c */
104 extern char **debug_snoop_includelist;
105 extern char **debug_snoop_excludelist;
107 if (!(opt = strtok( options, "," ))) goto error;
110 unsigned char set = 0, clear = 0;
111 char *p = strchr( opt, '+' );
112 if (!p) p = strchr( opt, '-' );
113 if (!p || !p[1]) goto error;
114 if (p > opt)
116 for (i = 0; i < __DBCL_COUNT; i++)
118 int len = strlen(debug_class_names[i]);
119 if (len != (p - opt)) continue;
120 if (!memcmp( opt, debug_class_names[i], len )) /* found it */
122 if (*p == '+') set |= 1 << i;
123 else clear |= 1 << i;
124 break;
127 if (i == __DBCL_COUNT) goto error; /* class name not found */
129 else
131 if (*p == '+') set = ~0;
132 else clear = ~0;
133 if (!strncasecmp(p+1, "relay=", 6) ||
134 !strncasecmp(p+1, "snoop=", 6))
136 int i, l;
137 char *s, *s2, ***output, c;
139 if (strchr(p,','))
140 l=strchr(p,',')-p;
141 else
142 l=strlen(p);
143 set = ~0;
144 clear = 0;
145 output = (*p == '+') ?
146 ((*(p+1) == 'r') ?
147 &debug_relay_includelist :
148 &debug_snoop_includelist) :
149 ((*(p+1) == 'r') ?
150 &debug_relay_excludelist :
151 &debug_snoop_excludelist);
152 s = p + 7;
153 /* if there are n ':', there are n+1 modules, and we need
154 n+2 slots, last one being for the sentinel (NULL) */
155 i = 2;
156 while((s = strchr(s, ':'))) i++, s++;
157 *output = malloc(sizeof(char **) * i);
158 i = 0;
159 s = p + 7;
160 while((s2 = strchr(s, ':'))) {
161 c = *s2;
162 *s2 = '\0';
163 *((*output)+i) = _strupr(strdup(s));
164 *s2 = c;
165 s = s2 + 1;
166 i++;
168 c = *(p + l);
169 *(p + l) = '\0';
170 *((*output)+i) = _strupr(strdup(s));
171 *(p + l) = c;
172 *((*output)+i+1) = NULL;
173 *(p + 6) = '\0';
176 p++;
177 if (!strcmp( p, "all" )) p = ""; /* empty string means all */
178 wine_dbg_add_option( p, set, clear );
179 opt = strtok( NULL, "," );
180 } while(opt);
182 free( options );
183 return;
185 error:
186 MESSAGE("wine: Syntax: --debugmsg [class]+xxx,... or "
187 "-debugmsg [class]-xxx,...\n");
188 MESSAGE("Example: --debugmsg +all,warn-heap\n"
189 " turn on all messages except warning heap messages\n");
190 MESSAGE("Available message classes:\n");
191 for( i = 0; i < __DBCL_COUNT; i++) MESSAGE( "%-9s", debug_class_names[i] );
192 MESSAGE("\n\n");
193 ExitProcess(1);
197 static void remove_options( char *argv[], int pos, int count, int inherit )
199 if (inherit)
201 int i, len = 0;
202 for (i = 0; i < count; i++) len += strlen(argv[pos+i]) + 1;
203 if (inherit_str)
205 if (!(inherit_str = realloc( inherit_str, strlen(inherit_str) + 1 + len )))
206 out_of_memory();
207 strcat( inherit_str, " " );
209 else
211 if (!(inherit_str = malloc( len ))) out_of_memory();
212 inherit_str[0] = 0;
214 for (i = 0; i < count; i++)
216 strcat( inherit_str, argv[pos+i] );
217 if (i < count-1) strcat( inherit_str, " " );
220 while ((argv[pos] = argv[pos+count])) pos++;
223 /* parse options from the argv array and remove all the recognized ones */
224 static void parse_options( char *argv[] )
226 const struct option_descr *opt;
227 int i;
229 for (i = 0; argv[i]; i++)
231 const char *equalarg = NULL;
232 char *p = argv[i];
233 if (*p++ != '-') continue; /* not an option */
234 if (*p && !p[1]) /* short name */
236 if (*p == '-') break; /* "--" option */
237 for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
239 else /* long name */
241 const char *equal = strchr (p, '=');
242 if (*p == '-') p++;
243 /* check for the long name */
244 for (opt = option_table; opt->longname; opt++) {
245 /* Plain --option */
246 if (!strcmp( p, opt->longname )) break;
248 /* --option=value */
249 if (opt->has_arg &&
250 equal &&
251 strlen (opt->longname) == equal - p &&
252 !strncmp (p, opt->longname, equal - p)) {
253 equalarg = equal + 1;
254 break;
258 if (!opt->longname) continue;
260 if (equalarg)
262 opt->func( equalarg );
263 remove_options( argv, i, 1, opt->inherit );
265 else 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_descr *opt;
306 MESSAGE( "%s\n\n", WINE_RELEASE_INFO );
307 MESSAGE( "Usage: %s [options] [--] program_name [arguments]\n", argv0 );
308 MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
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 app_argv = argv;
346 app_argc = 0;
347 while (argv[app_argc]) app_argc++;
351 /***********************************************************************
352 * __wine_get_main_args
354 * Return the argc/argv that the application should see.
355 * Used by the startup code generated in the .spec.c file.
357 int __wine_get_main_args( char ***argv )
359 *argv = app_argv;
360 return app_argc;
364 /***********************************************************************
365 * __wine_get_wmain_args
367 * Same as __wine_get_main_args but for Unicode.
369 int __wine_get_wmain_args( WCHAR ***argv )
371 if (!app_wargv)
373 int i;
374 WCHAR *p;
375 DWORD total = 0;
377 for (i = 0; i < app_argc; i++)
378 total += MultiByteToWideChar( CP_ACP, 0, app_argv[i], -1, NULL, 0 );
380 app_wargv = HeapAlloc( GetProcessHeap(), 0,
381 total * sizeof(WCHAR) + (app_argc + 1) * sizeof(*app_wargv) );
382 p = (WCHAR *)(app_wargv + app_argc + 1);
383 for (i = 0; i < app_argc; i++)
385 DWORD len = MultiByteToWideChar( CP_ACP, 0, app_argv[i], -1, p, total );
386 app_wargv[i] = p;
387 p += len;
388 total -= len;
390 app_wargv[app_argc] = NULL;
392 *argv = app_wargv;
393 return app_argc;