Some other synchronisation issues.
[wine.git] / misc / options.c
bloba181974b25382ca78b52577ebb6fbb80e5080210
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 const char *argv0; /* the original argv[0] */
44 const char *full_argv0; /* the full path of argv[0] (if known) */
46 static char *inherit_str; /* options to pass to child processes */
48 static void out_of_memory(void) WINE_NORETURN;
49 static void out_of_memory(void)
51 MESSAGE( "Virtual memory exhausted\n" );
52 ExitProcess(1);
55 static void do_debugmsg( const char *arg );
56 static void do_help( const char *arg );
57 static void do_version( const char *arg );
59 static const struct option_descr option_table[] =
61 { "debugmsg", 0, 1, 1, do_debugmsg,
62 "--debugmsg name Turn debugging-messages on or off" },
63 { "dll", 0, 1, 1, MODULE_AddLoadOrderOption,
64 "--dll name Enable or disable built-in DLLs" },
65 { "help", 'h', 0, 0, do_help,
66 "--help,-h Show this help message" },
67 { "version", 'v', 0, 0, do_version,
68 "--version,-v Display the Wine version" },
69 { NULL, 0, 0, 0, NULL, NULL } /* terminator */
73 static void do_help( const char *arg )
75 OPTIONS_Usage();
78 static void do_version( const char *arg )
80 MESSAGE( "%s\n", PACKAGE_STRING );
81 ExitProcess(0);
84 static void do_debugmsg( const char *arg )
86 static const char * const debug_class_names[__WINE_DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
88 char *opt, *options = strdup(arg);
89 int i;
90 /* defined in relay32/relay386.c */
91 extern char **debug_relay_includelist;
92 extern char **debug_relay_excludelist;
93 /* defined in relay32/snoop.c */
94 extern char **debug_snoop_includelist;
95 extern char **debug_snoop_excludelist;
97 if (!(opt = strtok( options, "," ))) goto error;
100 unsigned char set = 0, clear = 0;
101 char *p = strchr( opt, '+' );
102 if (!p) p = strchr( opt, '-' );
103 if (!p || !p[1]) goto error;
104 if (p > opt)
106 for (i = 0; i < __WINE_DBCL_COUNT; i++)
108 int len = strlen(debug_class_names[i]);
109 if (len != (p - opt)) continue;
110 if (!memcmp( opt, debug_class_names[i], len )) /* found it */
112 if (*p == '+') set |= 1 << i;
113 else clear |= 1 << i;
114 break;
117 if (i == __WINE_DBCL_COUNT) goto error; /* class name not found */
119 else
121 if (*p == '+') set = ~0;
122 else clear = ~0;
123 if (!strncasecmp(p+1, "relay=", 6) ||
124 !strncasecmp(p+1, "snoop=", 6))
126 int i, l;
127 char *s, *s2, ***output, c;
129 if (strchr(p,','))
130 l=strchr(p,',')-p;
131 else
132 l=strlen(p);
133 set = ~0;
134 clear = 0;
135 output = (*p == '+') ?
136 ((*(p+1) == 'r') ?
137 &debug_relay_includelist :
138 &debug_snoop_includelist) :
139 ((*(p+1) == 'r') ?
140 &debug_relay_excludelist :
141 &debug_snoop_excludelist);
142 s = p + 7;
143 /* if there are n ':', there are n+1 modules, and we need
144 n+2 slots, last one being for the sentinel (NULL) */
145 i = 2;
146 while((s = strchr(s, ':'))) i++, s++;
147 *output = malloc(sizeof(char **) * i);
148 i = 0;
149 s = p + 7;
150 while((s2 = strchr(s, ':'))) {
151 c = *s2;
152 *s2 = '\0';
153 *((*output)+i) = _strupr(strdup(s));
154 *s2 = c;
155 s = s2 + 1;
156 i++;
158 c = *(p + l);
159 *(p + l) = '\0';
160 *((*output)+i) = _strupr(strdup(s));
161 *(p + l) = c;
162 *((*output)+i+1) = NULL;
163 *(p + 6) = '\0';
166 p++;
167 if (!strcmp( p, "all" )) p = ""; /* empty string means all */
168 wine_dbg_add_option( p, set, clear );
169 opt = strtok( NULL, "," );
170 } while(opt);
172 free( options );
173 return;
175 error:
176 MESSAGE("wine: Syntax: --debugmsg [class]+xxx,... or "
177 "-debugmsg [class]-xxx,...\n");
178 MESSAGE("Example: --debugmsg +all,warn-heap\n"
179 " turn on all messages except warning heap messages\n");
180 MESSAGE("Available message classes:\n");
181 for( i = 0; i < __WINE_DBCL_COUNT; i++) MESSAGE( "%-9s", debug_class_names[i] );
182 MESSAGE("\n\n");
183 ExitProcess(1);
187 static void remove_options( char *argv[], int pos, int count, int inherit )
189 if (inherit)
191 int i, len = 0;
192 for (i = 0; i < count; i++) len += strlen(argv[pos+i]) + 1;
193 if (inherit_str)
195 if (!(inherit_str = realloc( inherit_str, strlen(inherit_str) + 1 + len )))
196 out_of_memory();
197 strcat( inherit_str, " " );
199 else
201 if (!(inherit_str = malloc( len ))) out_of_memory();
202 inherit_str[0] = 0;
204 for (i = 0; i < count; i++)
206 strcat( inherit_str, argv[pos+i] );
207 if (i < count-1) strcat( inherit_str, " " );
210 while ((argv[pos] = argv[pos+count])) pos++;
213 /* parse options from the argv array and remove all the recognized ones */
214 static void parse_options( char *argv[] )
216 const struct option_descr *opt;
217 int i;
219 for (i = 0; argv[i]; i++)
221 const char *equalarg = NULL;
222 char *p = argv[i];
223 if (*p++ != '-') continue; /* not an option */
224 if (*p && !p[1]) /* short name */
226 if (*p == '-') break; /* "--" option */
227 for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
229 else /* long name */
231 const char *equal = strchr (p, '=');
232 if (*p == '-') p++;
233 /* check for the long name */
234 for (opt = option_table; opt->longname; opt++) {
235 /* Plain --option */
236 if (!strcmp( p, opt->longname )) break;
238 /* --option=value */
239 if (opt->has_arg &&
240 equal &&
241 strlen (opt->longname) == equal - p &&
242 !strncmp (p, opt->longname, equal - p)) {
243 equalarg = equal + 1;
244 break;
248 if (!opt->longname) continue;
250 if (equalarg)
252 opt->func( equalarg );
253 remove_options( argv, i, 1, opt->inherit );
255 else if (opt->has_arg && argv[i+1])
257 opt->func( argv[i+1] );
258 remove_options( argv, i, 2, opt->inherit );
260 else
262 opt->func( "" );
263 remove_options( argv, i, 1, opt->inherit );
265 i--;
269 /* inherit options from WINEOPTIONS variable */
270 static void inherit_options( char *buffer )
272 char *argv[256];
273 unsigned int n;
275 char *p = strtok( buffer, " \t" );
276 for (n = 0; n < sizeof(argv)/sizeof(argv[0])-1 && p; n++)
278 argv[n] = p;
279 p = strtok( NULL, " \t" );
281 argv[n] = NULL;
282 parse_options( argv );
283 if (argv[0]) /* an option remains */
285 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv[0] );
286 OPTIONS_Usage();
290 /***********************************************************************
291 * OPTIONS_Usage
293 void OPTIONS_Usage(void)
295 const struct option_descr *opt;
296 MESSAGE( "%s\n\n", PACKAGE_STRING );
297 MESSAGE( "Usage: %s [options] [--] program_name [arguments]\n", argv0 );
298 MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
299 MESSAGE( "Options:\n" );
300 for (opt = option_table; opt->longname; opt++) MESSAGE( " %s\n", opt->usage );
301 ExitProcess(0);
304 /***********************************************************************
305 * OPTIONS_ParseOptions
307 void OPTIONS_ParseOptions( char *argv[] )
309 char buffer[1024];
310 int i;
312 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer, sizeof(buffer) ) && buffer[0])
313 inherit_options( buffer );
314 if (!argv) return;
316 parse_options( argv + 1 );
318 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str );
320 /* check if any option remains */
321 for (i = 1; argv[i]; i++)
323 if (!strcmp( argv[i], "--" ))
325 remove_options( argv, i, 1, 0 );
326 break;
328 if (argv[i][0] == '-')
330 MESSAGE( "Unknown option '%s'\n\n", argv[i] );
331 OPTIONS_Usage();