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
28 #include "wine/library.h"
31 #include "wine/debug.h"
39 void (*func)( const char *arg );
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" );
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 { "dosver", 0, 1, 1, VERSION_ParseDosVersion,
66 "--dosver x.xx DOS version to imitate (e.g. 6.22)\n"
67 " Only valid with --winver win31" },
68 { "help", 'h', 0, 0, do_help,
69 "--help,-h Show this help message" },
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,win98,winme,nt351,nt40,win2k,winxp,win20,win30,win31)" },
74 { NULL, 0, 0, 0, NULL, NULL } /* terminator */
78 static void do_help( const char *arg )
83 static void do_version( const char *arg )
85 MESSAGE( "%s\n", PACKAGE_STRING );
89 static void do_debugmsg( const char *arg )
91 static const char * const debug_class_names[__WINE_DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
93 char *opt, *options = strdup(arg);
95 /* defined in relay32/relay386.c */
96 extern char **debug_relay_includelist;
97 extern char **debug_relay_excludelist;
98 /* defined in relay32/snoop.c */
99 extern char **debug_snoop_includelist;
100 extern char **debug_snoop_excludelist;
102 if (!(opt = strtok( options, "," ))) goto error;
105 unsigned char set = 0, clear = 0;
106 char *p = strchr( opt, '+' );
107 if (!p) p = strchr( opt, '-' );
108 if (!p || !p[1]) goto error;
111 for (i = 0; i < __WINE_DBCL_COUNT; i++)
113 int len = strlen(debug_class_names[i]);
114 if (len != (p - opt)) continue;
115 if (!memcmp( opt, debug_class_names[i], len )) /* found it */
117 if (*p == '+') set |= 1 << i;
118 else clear |= 1 << i;
122 if (i == __WINE_DBCL_COUNT) goto error; /* class name not found */
126 if (*p == '+') set = ~0;
128 if (!strncasecmp(p+1, "relay=", 6) ||
129 !strncasecmp(p+1, "snoop=", 6))
132 char *s, *s2, ***output, c;
140 output = (*p == '+') ?
142 &debug_relay_includelist :
143 &debug_snoop_includelist) :
145 &debug_relay_excludelist :
146 &debug_snoop_excludelist);
148 /* if there are n ':', there are n+1 modules, and we need
149 n+2 slots, last one being for the sentinel (NULL) */
151 while((s = strchr(s, ':'))) i++, s++;
152 *output = malloc(sizeof(char **) * i);
155 while((s2 = strchr(s, ':'))) {
158 *((*output)+i) = _strupr(strdup(s));
165 *((*output)+i) = _strupr(strdup(s));
167 *((*output)+i+1) = NULL;
172 if (!strcmp( p, "all" )) p = ""; /* empty string means all */
173 wine_dbg_add_option( p, set, clear );
174 opt = strtok( NULL, "," );
181 MESSAGE("wine: Syntax: --debugmsg [class]+xxx,... or "
182 "-debugmsg [class]-xxx,...\n");
183 MESSAGE("Example: --debugmsg +all,warn-heap\n"
184 " turn on all messages except warning heap messages\n");
185 MESSAGE("Available message classes:\n");
186 for( i = 0; i < __WINE_DBCL_COUNT; i++) MESSAGE( "%-9s", debug_class_names[i] );
192 static void remove_options( char *argv[], int pos, int count, int inherit )
197 for (i = 0; i < count; i++) len += strlen(argv[pos+i]) + 1;
200 if (!(inherit_str = realloc( inherit_str, strlen(inherit_str) + 1 + len )))
202 strcat( inherit_str, " " );
206 if (!(inherit_str = malloc( len ))) out_of_memory();
209 for (i = 0; i < count; i++)
211 strcat( inherit_str, argv[pos+i] );
212 if (i < count-1) strcat( inherit_str, " " );
215 while ((argv[pos] = argv[pos+count])) pos++;
218 /* parse options from the argv array and remove all the recognized ones */
219 static void parse_options( char *argv[] )
221 const struct option_descr *opt;
224 for (i = 0; argv[i]; i++)
226 const char *equalarg = NULL;
228 if (*p++ != '-') continue; /* not an option */
229 if (*p && !p[1]) /* short name */
231 if (*p == '-') break; /* "--" option */
232 for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
236 const char *equal = strchr (p, '=');
238 /* check for the long name */
239 for (opt = option_table; opt->longname; opt++) {
241 if (!strcmp( p, opt->longname )) break;
246 strlen (opt->longname) == equal - p &&
247 !strncmp (p, opt->longname, equal - p)) {
248 equalarg = equal + 1;
253 if (!opt->longname) continue;
257 opt->func( equalarg );
258 remove_options( argv, i, 1, opt->inherit );
260 else if (opt->has_arg && argv[i+1])
262 opt->func( argv[i+1] );
263 remove_options( argv, i, 2, opt->inherit );
268 remove_options( argv, i, 1, opt->inherit );
274 /* inherit options from WINEOPTIONS variable */
275 static void inherit_options( char *buffer )
280 char *p = strtok( buffer, " \t" );
281 for (n = 0; n < sizeof(argv)/sizeof(argv[0])-1 && p; n++)
284 p = strtok( NULL, " \t" );
287 parse_options( argv );
288 if (argv[0]) /* an option remains */
290 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv[0] );
295 /***********************************************************************
298 void OPTIONS_Usage(void)
300 const struct option_descr *opt;
301 MESSAGE( "%s\n\n", PACKAGE_STRING );
302 MESSAGE( "Usage: %s [options] [--] program_name [arguments]\n", argv0 );
303 MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
304 MESSAGE( "Options:\n" );
305 for (opt = option_table; opt->longname; opt++) MESSAGE( " %s\n", opt->usage );
309 /***********************************************************************
310 * OPTIONS_ParseOptions
312 void OPTIONS_ParseOptions( char *argv[] )
317 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer, sizeof(buffer) ) && buffer[0])
318 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 );
333 if (argv[i][0] == '-')
335 MESSAGE( "Unknown option '%s'\n\n", argv[i] );