Terminating DOS program in real mode now calls ExitThread which is
[wine/multimedia.git] / misc / options.c
blob8f596adbdbafe0aa648043a5f66bb270ce20fa38
1 /*
2 * Option parsing
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include <string.h>
23 #include <stdlib.h>
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "ntddk.h"
28 #include "wine/library.h"
29 #include "options.h"
30 #include "module.h"
31 #include "wine/debug.h"
33 struct option_descr
35 const char *longname;
36 char shortname;
37 int has_arg;
38 int inherit;
39 void (*func)( const char *arg );
40 const char *usage;
43 /* default options */
44 struct options Options =
46 FALSE /* Managed windows */
49 const char *argv0; /* the original argv[0] */
50 const char *full_argv0; /* the full path of argv[0] (if known) */
52 static char *inherit_str; /* options to pass to child processes */
54 static void out_of_memory(void) WINE_NORETURN;
55 static void out_of_memory(void)
57 MESSAGE( "Virtual memory exhausted\n" );
58 ExitProcess(1);
61 static void do_debugmsg( const char *arg );
62 static void do_help( const char *arg );
63 static void do_managed( const char *arg );
64 static void do_version( const char *arg );
66 static const struct option_descr option_table[] =
68 { "debugmsg", 0, 1, 1, do_debugmsg,
69 "--debugmsg name Turn debugging-messages on or off" },
70 { "dll", 0, 1, 1, MODULE_AddLoadOrderOption,
71 "--dll name Enable or disable built-in DLLs" },
72 { "dosver", 0, 1, 1, VERSION_ParseDosVersion,
73 "--dosver x.xx DOS version to imitate (e.g. 6.22)\n"
74 " Only valid with --winver win31" },
75 { "help", 'h', 0, 0, do_help,
76 "--help,-h Show this help message" },
77 { "managed", 0, 0, 0, do_managed,
78 "--managed Allow the window manager to manage created windows" },
79 { "version", 'v', 0, 0, do_version,
80 "--version,-v Display the Wine version" },
81 { "winver", 0, 1, 1, VERSION_ParseWinVersion,
82 "--winver Version to imitate (win95,win98,winme,nt351,nt40,win2k,winxp,win20,win30,win31)" },
83 { NULL, 0, 0, 0, NULL, NULL } /* terminator */
87 static void do_help( const char *arg )
89 OPTIONS_Usage();
92 static void do_version( const char *arg )
94 MESSAGE( "%s\n", PACKAGE_STRING );
95 ExitProcess(0);
98 static void do_managed( const char *arg )
100 Options.managed = TRUE;
103 static void do_debugmsg( const char *arg )
105 static const char * const debug_class_names[__WINE_DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
107 char *opt, *options = strdup(arg);
108 int i;
109 /* defined in relay32/relay386.c */
110 extern char **debug_relay_includelist;
111 extern char **debug_relay_excludelist;
112 /* defined in relay32/snoop.c */
113 extern char **debug_snoop_includelist;
114 extern char **debug_snoop_excludelist;
116 if (!(opt = strtok( options, "," ))) goto error;
119 unsigned char set = 0, clear = 0;
120 char *p = strchr( opt, '+' );
121 if (!p) p = strchr( opt, '-' );
122 if (!p || !p[1]) goto error;
123 if (p > opt)
125 for (i = 0; i < __WINE_DBCL_COUNT; i++)
127 int len = strlen(debug_class_names[i]);
128 if (len != (p - opt)) continue;
129 if (!memcmp( opt, debug_class_names[i], len )) /* found it */
131 if (*p == '+') set |= 1 << i;
132 else clear |= 1 << i;
133 break;
136 if (i == __WINE_DBCL_COUNT) goto error; /* class name not found */
138 else
140 if (*p == '+') set = ~0;
141 else clear = ~0;
142 if (!strncasecmp(p+1, "relay=", 6) ||
143 !strncasecmp(p+1, "snoop=", 6))
145 int i, l;
146 char *s, *s2, ***output, c;
148 if (strchr(p,','))
149 l=strchr(p,',')-p;
150 else
151 l=strlen(p);
152 set = ~0;
153 clear = 0;
154 output = (*p == '+') ?
155 ((*(p+1) == 'r') ?
156 &debug_relay_includelist :
157 &debug_snoop_includelist) :
158 ((*(p+1) == 'r') ?
159 &debug_relay_excludelist :
160 &debug_snoop_excludelist);
161 s = p + 7;
162 /* if there are n ':', there are n+1 modules, and we need
163 n+2 slots, last one being for the sentinel (NULL) */
164 i = 2;
165 while((s = strchr(s, ':'))) i++, s++;
166 *output = malloc(sizeof(char **) * i);
167 i = 0;
168 s = p + 7;
169 while((s2 = strchr(s, ':'))) {
170 c = *s2;
171 *s2 = '\0';
172 *((*output)+i) = _strupr(strdup(s));
173 *s2 = c;
174 s = s2 + 1;
175 i++;
177 c = *(p + l);
178 *(p + l) = '\0';
179 *((*output)+i) = _strupr(strdup(s));
180 *(p + l) = c;
181 *((*output)+i+1) = NULL;
182 *(p + 6) = '\0';
185 p++;
186 if (!strcmp( p, "all" )) p = ""; /* empty string means all */
187 wine_dbg_add_option( p, set, clear );
188 opt = strtok( NULL, "," );
189 } while(opt);
191 free( options );
192 return;
194 error:
195 MESSAGE("wine: Syntax: --debugmsg [class]+xxx,... or "
196 "-debugmsg [class]-xxx,...\n");
197 MESSAGE("Example: --debugmsg +all,warn-heap\n"
198 " turn on all messages except warning heap messages\n");
199 MESSAGE("Available message classes:\n");
200 for( i = 0; i < __WINE_DBCL_COUNT; i++) MESSAGE( "%-9s", debug_class_names[i] );
201 MESSAGE("\n\n");
202 ExitProcess(1);
206 static void remove_options( char *argv[], int pos, int count, int inherit )
208 if (inherit)
210 int i, len = 0;
211 for (i = 0; i < count; i++) len += strlen(argv[pos+i]) + 1;
212 if (inherit_str)
214 if (!(inherit_str = realloc( inherit_str, strlen(inherit_str) + 1 + len )))
215 out_of_memory();
216 strcat( inherit_str, " " );
218 else
220 if (!(inherit_str = malloc( len ))) out_of_memory();
221 inherit_str[0] = 0;
223 for (i = 0; i < count; i++)
225 strcat( inherit_str, argv[pos+i] );
226 if (i < count-1) strcat( inherit_str, " " );
229 while ((argv[pos] = argv[pos+count])) pos++;
232 /* parse options from the argv array and remove all the recognized ones */
233 static void parse_options( char *argv[] )
235 const struct option_descr *opt;
236 int i;
238 for (i = 0; argv[i]; i++)
240 const char *equalarg = NULL;
241 char *p = argv[i];
242 if (*p++ != '-') continue; /* not an option */
243 if (*p && !p[1]) /* short name */
245 if (*p == '-') break; /* "--" option */
246 for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
248 else /* long name */
250 const char *equal = strchr (p, '=');
251 if (*p == '-') p++;
252 /* check for the long name */
253 for (opt = option_table; opt->longname; opt++) {
254 /* Plain --option */
255 if (!strcmp( p, opt->longname )) break;
257 /* --option=value */
258 if (opt->has_arg &&
259 equal &&
260 strlen (opt->longname) == equal - p &&
261 !strncmp (p, opt->longname, equal - p)) {
262 equalarg = equal + 1;
263 break;
267 if (!opt->longname) continue;
269 if (equalarg)
271 opt->func( equalarg );
272 remove_options( argv, i, 1, opt->inherit );
274 else if (opt->has_arg && argv[i+1])
276 opt->func( argv[i+1] );
277 remove_options( argv, i, 2, opt->inherit );
279 else
281 opt->func( "" );
282 remove_options( argv, i, 1, opt->inherit );
284 i--;
288 /* inherit options from WINEOPTIONS variable */
289 static void inherit_options( char *buffer )
291 char *argv[256];
292 unsigned int n;
294 char *p = strtok( buffer, " \t" );
295 for (n = 0; n < sizeof(argv)/sizeof(argv[0])-1 && p; n++)
297 argv[n] = p;
298 p = strtok( NULL, " \t" );
300 argv[n] = NULL;
301 parse_options( argv );
302 if (argv[0]) /* an option remains */
304 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv[0] );
305 OPTIONS_Usage();
309 /***********************************************************************
310 * OPTIONS_Usage
312 void OPTIONS_Usage(void)
314 const struct option_descr *opt;
315 MESSAGE( "%s\n\n", PACKAGE_STRING );
316 MESSAGE( "Usage: %s [options] [--] program_name [arguments]\n", argv0 );
317 MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
318 MESSAGE( "Options:\n" );
319 for (opt = option_table; opt->longname; opt++) MESSAGE( " %s\n", opt->usage );
320 ExitProcess(0);
323 /***********************************************************************
324 * OPTIONS_ParseOptions
326 void OPTIONS_ParseOptions( char *argv[] )
328 char buffer[1024];
329 int i;
331 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer, sizeof(buffer) ) && buffer[0])
332 inherit_options( buffer );
333 if (!argv) return;
335 parse_options( argv + 1 );
337 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str );
339 /* check if any option remains */
340 for (i = 1; argv[i]; i++)
342 if (!strcmp( argv[i], "--" ))
344 remove_options( argv, i, 1, 0 );
345 break;
347 if (argv[i][0] == '-')
349 MESSAGE( "Unknown option '%s'\n\n", argv[i] );
350 OPTIONS_Usage();