For a radio button even if the initial style includes WS_TABSTOP the
[wine/dcerpc.git] / misc / options.c
blob639c9e6ea143e2ea2b5ae9bd990730c715f4fa37
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 "module.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 void out_of_memory(void) WINE_NORETURN;
42 static void out_of_memory(void)
44 MESSAGE( "Virtual memory exhausted\n" );
45 ExitProcess(1);
48 static void do_debugmsg( const char *arg );
49 static void do_help( const char *arg );
50 static void do_managed( const char *arg );
51 static void do_version( const char *arg );
53 static const struct option_descr option_table[] =
55 { "debugmsg", 0, 1, 1, do_debugmsg,
56 "--debugmsg name Turn debugging-messages on or off" },
57 { "dll", 0, 1, 1, MODULE_AddLoadOrderOption,
58 "--dll name Enable or disable built-in DLLs" },
59 { "dosver", 0, 1, 1, VERSION_ParseDosVersion,
60 "--dosver x.xx DOS version to imitate (e.g. 6.22)\n"
61 " Only valid with --winver win31" },
62 { "help", 'h', 0, 0, do_help,
63 "--help,-h Show this help message" },
64 { "managed", 0, 0, 0, do_managed,
65 "--managed Allow the window manager to manage created windows" },
66 { "version", 'v', 0, 0, do_version,
67 "--version,-v Display the Wine version" },
68 { "winver", 0, 1, 1, VERSION_ParseWinVersion,
69 "--winver Version to imitate (win95,win98,winme,nt351,nt40,win2k,winxp,win20,win30,win31)" },
70 { NULL, 0, 0, 0, NULL, NULL } /* terminator */
74 static void do_help( const char *arg )
76 OPTIONS_Usage();
79 static void do_version( const char *arg )
81 MESSAGE( "%s\n", WINE_RELEASE_INFO );
82 ExitProcess(0);
85 static void do_managed( const char *arg )
87 Options.managed = TRUE;
90 static void do_debugmsg( const char *arg )
92 static const char * const debug_class_names[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
94 char *opt, *options = strdup(arg);
95 int i;
96 /* 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;
103 if (!(opt = strtok( options, "," ))) goto error;
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)
112 for (i = 0; i < __DBCL_COUNT; i++)
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 */
118 if (*p == '+') set |= 1 << i;
119 else clear |= 1 << i;
120 break;
123 if (i == __DBCL_COUNT) goto error; /* class name not found */
125 else
127 if (*p == '+') set = ~0;
128 else clear = ~0;
129 if (!strncasecmp(p+1, "relay=", 6) ||
130 !strncasecmp(p+1, "snoop=", 6))
132 int i, l;
133 char *s, *s2, ***output, c;
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++;
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';
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);
178 free( options );
179 return;
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);
193 static void remove_options( char *argv[], int pos, int count, int inherit )
195 if (inherit)
197 int i, len = 0;
198 for (i = 0; i < count; i++) len += strlen(argv[pos+i]) + 1;
199 if (inherit_str)
201 if (!(inherit_str = realloc( inherit_str, strlen(inherit_str) + 1 + len )))
202 out_of_memory();
203 strcat( inherit_str, " " );
205 else
207 if (!(inherit_str = malloc( len ))) out_of_memory();
208 inherit_str[0] = 0;
210 for (i = 0; i < count; i++)
212 strcat( inherit_str, argv[pos+i] );
213 if (i < count-1) strcat( inherit_str, " " );
216 while ((argv[pos] = argv[pos+count])) pos++;
219 /* parse options from the argv array and remove all the recognized ones */
220 static void parse_options( char *argv[] )
222 const struct option_descr *opt;
223 int i;
225 for (i = 0; argv[i]; i++)
227 const char *equalarg = NULL;
228 char *p = argv[i];
229 if (*p++ != '-') continue; /* not an option */
230 if (*p && !p[1]) /* short name */
232 if (*p == '-') break; /* "--" option */
233 for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
235 else /* long name */
237 const char *equal = strchr (p, '=');
238 if (*p == '-') p++;
239 /* check for the long name */
240 for (opt = option_table; opt->longname; opt++) {
241 /* Plain --option */
242 if (!strcmp( p, opt->longname )) break;
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;
254 if (!opt->longname) continue;
256 if (equalarg)
258 opt->func( equalarg );
259 remove_options( argv, i, 1, opt->inherit );
261 else if (opt->has_arg && argv[i+1])
263 opt->func( argv[i+1] );
264 remove_options( argv, i, 2, opt->inherit );
266 else
268 opt->func( "" );
269 remove_options( argv, i, 1, opt->inherit );
271 i--;
275 /* inherit options from WINEOPTIONS variable */
276 static void inherit_options( char *buffer )
278 char *argv[256];
279 unsigned int n;
281 char *p = strtok( buffer, " \t" );
282 for (n = 0; n < sizeof(argv)/sizeof(argv[0])-1 && p; n++)
284 argv[n] = p;
285 p = strtok( NULL, " \t" );
287 argv[n] = NULL;
288 parse_options( argv );
289 if (argv[0]) /* an option remains */
291 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv[0] );
292 OPTIONS_Usage();
296 /***********************************************************************
297 * OPTIONS_Usage
299 void OPTIONS_Usage(void)
301 const struct option_descr *opt;
302 MESSAGE( "%s\n\n", WINE_RELEASE_INFO );
303 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");
305 MESSAGE( "Options:\n" );
306 for (opt = option_table; opt->longname; opt++) MESSAGE( " %s\n", opt->usage );
307 ExitProcess(0);
310 /***********************************************************************
311 * OPTIONS_ParseOptions
313 void OPTIONS_ParseOptions( char *argv[] )
315 char buffer[1024];
316 int i;
318 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer, sizeof(buffer) ) && buffer[0])
319 inherit_options( buffer );
321 parse_options( argv + 1 );
323 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str );
325 /* check if any option remains */
326 for (i = 1; argv[i]; i++)
328 if (!strcmp( argv[i], "--" ))
330 remove_options( argv, i, 1, 0 );
331 break;
333 if (argv[i][0] == '-')
335 MESSAGE( "Unknown option '%s'\n\n", argv[i] );
336 OPTIONS_Usage();