4 * Copyright 2000 Alexandre Julliard
14 #include "wine/library.h"
17 #include "debugtools.h"
25 void (*func
)( const char *arg
);
30 struct options Options
=
32 NULL
, /* desktopGeometry */
35 FALSE
, /* synchronous */
36 FALSE
/* Managed windows */
39 const char *argv0
; /* the original argv[0] */
40 const char *full_argv0
; /* the full path of argv[0] (if known) */
42 static char *inherit_str
; /* options to pass to child processes */
44 static int app_argc
; /* argc/argv to pass to application */
45 static char **app_argv
;
46 static WCHAR
**app_wargv
;
48 static void out_of_memory(void) WINE_NORETURN
;
49 static void out_of_memory(void)
51 MESSAGE( "Virtual memory exhausted\n" );
55 static char *xstrdup( const char *str
)
57 char *ret
= strdup( str
);
58 if (!ret
) out_of_memory();
62 static void do_debugmsg( const char *arg
);
63 static void do_desktop( const char *arg
);
64 static void do_display( const char *arg
);
65 static void do_dll( const char *arg
);
66 static void do_help( const char *arg
);
67 static void do_language( const char *arg
);
68 static void do_managed( const char *arg
);
69 static void do_synchronous( const char *arg
);
70 static void do_version( const char *arg
);
72 static const struct option_descr option_table
[] =
74 { "debugmsg", 0, 1, 1, do_debugmsg
,
75 "--debugmsg name Turn debugging-messages on or off" },
76 { "desktop", 0, 1, 1, do_desktop
,
77 "--desktop geom Use a desktop window of the given geometry" },
78 { "display", 0, 1, 0, do_display
,
79 "--display name Use the specified display" },
80 { "dll", 0, 1, 1, do_dll
,
81 "--dll name Enable or disable built-in DLLs" },
82 { "dosver", 0, 1, 1, VERSION_ParseDosVersion
,
83 "--dosver x.xx DOS version to imitate (e.g. 6.22)\n"
84 " Only valid with --winver win31" },
85 { "help", 'h', 0, 0, do_help
,
86 "--help,-h Show this help message" },
87 { "language", 0, 1, 1, do_language
,
88 "--language xx Set the language (one of Br,Ca,Cs,Cy,Da,De,En,Eo,Es,Fi,Fr,Ga,Gd,Gv,\n"
89 " He,Hr,Hu,It,Ja,Ko,Kw,Ms,Nl,No,Pl,Pt,Sk,Sv,Ru,Wa)" },
90 { "managed", 0, 0, 0, do_managed
,
91 "--managed Allow the window manager to manage created windows" },
92 { "synchronous", 0, 0, 1, do_synchronous
,
93 "--synchronous Turn on synchronous display mode" },
94 { "version", 'v', 0, 0, do_version
,
95 "--version,-v Display the Wine version" },
96 { "winver", 0, 1, 1, VERSION_ParseWinVersion
,
97 "--winver Version to imitate (win95,nt40,win31,nt2k,win98,nt351,win30,win20)" },
98 { NULL
, 0, 0, 0, NULL
, NULL
} /* terminator */
102 static void do_help( const char *arg
)
107 static void do_version( const char *arg
)
109 MESSAGE( "%s\n", WINE_RELEASE_INFO
);
113 static void do_synchronous( const char *arg
)
115 Options
.synchronous
= TRUE
;
118 static void do_desktop( const char *arg
)
120 Options
.desktopGeometry
= xstrdup( arg
);
123 static void do_display( const char *arg
)
125 Options
.display
= xstrdup( arg
);
128 static void do_dll( const char *arg
)
130 if (Options
.dllFlags
)
132 Options
.dllFlags
= (char *) realloc ( Options
.dllFlags
,
133 strlen ( Options
.dllFlags
) + strlen ( arg
) + 2 );
134 if ( !Options
.dllFlags
) out_of_memory();
135 strcat ( Options
.dllFlags
, "+" );
136 strcat ( Options
.dllFlags
, arg
);
140 Options
.dllFlags
= xstrdup( arg
);
144 static void do_language( const char *arg
)
146 SetEnvironmentVariableA( "LANGUAGE", arg
);
149 static void do_managed( const char *arg
)
151 Options
.managed
= TRUE
;
154 static void do_debugmsg( const char *arg
)
156 static const char * const debug_class_names
[__DBCL_COUNT
] = { "fixme", "err", "warn", "trace" };
158 char *opt
, *options
= strdup(arg
);
160 /* defined in relay32/relay386.c */
161 extern char **debug_relay_includelist
;
162 extern char **debug_relay_excludelist
;
163 /* defined in relay32/snoop.c */
164 extern char **debug_snoop_includelist
;
165 extern char **debug_snoop_excludelist
;
167 if (!(opt
= strtok( options
, "," ))) goto error
;
170 unsigned char set
= 0, clear
= 0;
171 char *p
= strchr( opt
, '+' );
172 if (!p
) p
= strchr( opt
, '-' );
173 if (!p
|| !p
[1]) goto error
;
176 for (i
= 0; i
< __DBCL_COUNT
; i
++)
178 int len
= strlen(debug_class_names
[i
]);
179 if (len
!= (p
- opt
)) continue;
180 if (!memcmp( opt
, debug_class_names
[i
], len
)) /* found it */
182 if (*p
== '+') set
|= 1 << i
;
183 else clear
|= 1 << i
;
187 if (i
== __DBCL_COUNT
) goto error
; /* class name not found */
191 if (*p
== '+') set
= ~0;
193 if (!strncasecmp(p
+1, "relay=", 6) ||
194 !strncasecmp(p
+1, "snoop=", 6))
197 char *s
, *s2
, ***output
, c
;
205 output
= (*p
== '+') ?
207 &debug_relay_includelist
:
208 &debug_snoop_includelist
) :
210 &debug_relay_excludelist
:
211 &debug_snoop_excludelist
);
213 /* if there are n ':', there are n+1 modules, and we need
214 n+2 slots, last one being for the sentinel (NULL) */
216 while((s
= strchr(s
, ':'))) i
++, s
++;
217 *output
= malloc(sizeof(char **) * i
);
220 while((s2
= strchr(s
, ':'))) {
223 *((*output
)+i
) = _strupr(strdup(s
));
230 *((*output
)+i
) = _strupr(strdup(s
));
232 *((*output
)+i
+1) = NULL
;
237 if (!strcmp( p
, "all" )) p
= ""; /* empty string means all */
238 wine_dbg_add_option( p
, set
, clear
);
239 opt
= strtok( NULL
, "," );
246 MESSAGE("wine: Syntax: --debugmsg [class]+xxx,... or "
247 "-debugmsg [class]-xxx,...\n");
248 MESSAGE("Example: --debugmsg +all,warn-heap\n"
249 " turn on all messages except warning heap messages\n");
250 MESSAGE("Available message classes:\n");
251 for( i
= 0; i
< __DBCL_COUNT
; i
++) MESSAGE( "%-9s", debug_class_names
[i
] );
257 static void remove_options( char *argv
[], int pos
, int count
, int inherit
)
262 for (i
= 0; i
< count
; i
++) len
+= strlen(argv
[pos
+i
]) + 1;
265 if (!(inherit_str
= realloc( inherit_str
, strlen(inherit_str
) + 1 + len
)))
267 strcat( inherit_str
, " " );
271 if (!(inherit_str
= malloc( len
))) out_of_memory();
274 for (i
= 0; i
< count
; i
++)
276 strcat( inherit_str
, argv
[pos
+i
] );
277 if (i
< count
-1) strcat( inherit_str
, " " );
280 while ((argv
[pos
] = argv
[pos
+count
])) pos
++;
283 /* parse options from the argv array and remove all the recognized ones */
284 static void parse_options( char *argv
[] )
286 const struct option_descr
*opt
;
289 for (i
= 0; argv
[i
]; i
++)
291 const char *equalarg
= NULL
;
293 if (*p
++ != '-') continue; /* not an option */
294 if (*p
&& !p
[1]) /* short name */
296 if (*p
== '-') break; /* "--" option */
297 for (opt
= option_table
; opt
->longname
; opt
++) if (opt
->shortname
== *p
) break;
301 const char *equal
= strchr (p
, '=');
303 /* check for the long name */
304 for (opt
= option_table
; opt
->longname
; opt
++) {
306 if (!strcmp( p
, opt
->longname
)) break;
311 strlen (opt
->longname
) == equal
- p
&&
312 !strncmp (p
, opt
->longname
, equal
- p
)) {
313 equalarg
= equal
+ 1;
318 if (!opt
->longname
) continue;
322 opt
->func( equalarg
);
323 remove_options( argv
, i
, 1, opt
->inherit
);
325 else if (opt
->has_arg
&& argv
[i
+1])
327 opt
->func( argv
[i
+1] );
328 remove_options( argv
, i
, 2, opt
->inherit
);
333 remove_options( argv
, i
, 1, opt
->inherit
);
339 /* inherit options from WINEOPTIONS variable */
340 static void inherit_options( char *buffer
)
345 char *p
= strtok( buffer
, " \t" );
346 for (n
= 0; n
< sizeof(argv
)/sizeof(argv
[0])-1 && p
; n
++)
349 p
= strtok( NULL
, " \t" );
352 parse_options( argv
);
353 if (argv
[0]) /* an option remains */
355 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv
[0] );
360 /***********************************************************************
363 void OPTIONS_Usage(void)
365 const struct option_descr
*opt
;
366 MESSAGE( "Usage: %s [options] [--] program_name [arguments]\n", argv0
);
367 MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
368 MESSAGE( "Options:\n" );
369 for (opt
= option_table
; opt
->longname
; opt
++) MESSAGE( " %s\n", opt
->usage
);
373 /***********************************************************************
374 * OPTIONS_ParseOptions
376 void OPTIONS_ParseOptions( char *argv
[] )
381 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer
, sizeof(buffer
) ) && buffer
[0])
382 inherit_options( buffer
);
384 parse_options( argv
+ 1 );
386 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str
);
388 /* check if any option remains */
389 for (i
= 1; argv
[i
]; i
++)
391 if (!strcmp( argv
[i
], "--" ))
393 remove_options( argv
, i
, 1, 0 );
396 if (argv
[i
][0] == '-')
398 MESSAGE( "Unknown option '%s'\n\n", argv
[i
] );
403 /* count the resulting arguments */
406 while (argv
[app_argc
]) app_argc
++;
410 /***********************************************************************
411 * __wine_get_main_args
413 * Return the argc/argv that the application should see.
414 * Used by the startup code generated in the .spec.c file.
416 int __wine_get_main_args( char ***argv
)
423 /***********************************************************************
424 * __wine_get_wmain_args
426 * Same as __wine_get_main_args but for Unicode.
428 int __wine_get_wmain_args( WCHAR
***argv
)
436 for (i
= 0; i
< app_argc
; i
++)
437 total
+= MultiByteToWideChar( CP_ACP
, 0, app_argv
[i
], -1, NULL
, 0 );
439 app_wargv
= HeapAlloc( GetProcessHeap(), 0,
440 total
* sizeof(WCHAR
) + (app_argc
+ 1) * sizeof(*app_wargv
) );
441 p
= (WCHAR
*)(app_wargv
+ app_argc
+ 1);
442 for (i
= 0; i
< app_argc
; i
++)
444 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, app_argv
[i
], -1, p
, total
);
449 app_wargv
[app_argc
] = NULL
;