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 */
37 NULL
/* Alternate config file name */
40 const char *argv0
; /* the original argv[0] */
41 const char *full_argv0
; /* the full path of argv[0] (if known) */
43 static char *inherit_str
; /* options to pass to child processes */
45 static int app_argc
; /* argc/argv to pass to application */
46 static char **app_argv
;
47 static WCHAR
**app_wargv
;
49 static void out_of_memory(void) WINE_NORETURN
;
50 static void out_of_memory(void)
52 MESSAGE( "Virtual memory exhausted\n" );
56 static char *xstrdup( const char *str
)
58 char *ret
= strdup( str
);
59 if (!ret
) out_of_memory();
63 static void do_config( const char *arg
);
64 static void do_debugmsg( const char *arg
);
65 static void do_desktop( const char *arg
);
66 static void do_display( const char *arg
);
67 static void do_dll( const char *arg
);
68 static void do_help( const char *arg
);
69 static void do_language( const char *arg
);
70 static void do_managed( const char *arg
);
71 static void do_synchronous( const char *arg
);
72 static void do_version( const char *arg
);
74 static const struct option option_table
[] =
76 { "config", 0, 1, 0, do_config
,
77 "--config name Specify config file to use" },
78 { "debugmsg", 0, 1, 1, do_debugmsg
,
79 "--debugmsg name Turn debugging-messages on or off" },
80 { "desktop", 0, 1, 1, do_desktop
,
81 "--desktop geom Use a desktop window of the given geometry" },
82 { "display", 0, 1, 0, do_display
,
83 "--display name Use the specified display" },
84 { "dll", 0, 1, 1, do_dll
,
85 "--dll name Enable or disable built-in DLLs" },
86 { "dosver", 0, 1, 1, VERSION_ParseDosVersion
,
87 "--dosver x.xx DOS version to imitate (e.g. 6.22)\n"
88 " Only valid with --winver win31" },
89 { "help", 'h', 0, 0, do_help
,
90 "--help,-h Show this help message" },
91 { "language", 0, 1, 1, do_language
,
92 "--language xx Set the language (one of Br,Ca,Cs,Cy,Da,De,En,Eo,Es,Fi,Fr,Ga,Gd,Gv,\n"
93 " He,Hr,Hu,It,Ja,Ko,Kw,Nl,No,Pl,Pt,Sk,Sv,Ru,Wa)" },
94 { "managed", 0, 0, 0, do_managed
,
95 "--managed Allow the window manager to manage created windows" },
96 { "synchronous", 0, 0, 1, do_synchronous
,
97 "--synchronous Turn on synchronous display mode" },
98 { "version", 'v', 0, 0, do_version
,
99 "--version,-v Display the Wine version" },
100 { "winver", 0, 1, 1, VERSION_ParseWinVersion
,
101 "--winver Version to imitate (win95,nt40,win31,nt2k,win98,nt351,win30,win20)" },
102 { NULL
, 0, 0, 0, NULL
, NULL
} /* terminator */
106 static void do_help( const char *arg
)
111 static void do_version( const char *arg
)
113 MESSAGE( "%s\n", WINE_RELEASE_INFO
);
117 static void do_synchronous( const char *arg
)
119 Options
.synchronous
= TRUE
;
122 static void do_desktop( const char *arg
)
124 Options
.desktopGeometry
= xstrdup( arg
);
127 static void do_display( const char *arg
)
129 Options
.display
= xstrdup( arg
);
132 static void do_dll( const char *arg
)
134 if (Options
.dllFlags
)
136 Options
.dllFlags
= (char *) realloc ( Options
.dllFlags
,
137 strlen ( Options
.dllFlags
) + strlen ( arg
) + 2 );
138 if ( !Options
.dllFlags
) out_of_memory();
139 strcat ( Options
.dllFlags
, "+" );
140 strcat ( Options
.dllFlags
, arg
);
144 Options
.dllFlags
= xstrdup( arg
);
148 static void do_language( const char *arg
)
150 SetEnvironmentVariableA( "LANGUAGE", arg
);
153 static void do_managed( const char *arg
)
155 Options
.managed
= TRUE
;
158 static void do_config( const char *arg
)
160 Options
.configFileName
= xstrdup( arg
);
163 static void do_debugmsg( const char *arg
)
165 static const char * const debug_class_names
[__DBCL_COUNT
] = { "fixme", "err", "warn", "trace" };
167 char *opt
, *options
= strdup(arg
);
169 /* defined in relay32/relay386.c */
170 extern char **debug_relay_includelist
;
171 extern char **debug_relay_excludelist
;
172 /* defined in relay32/snoop.c */
173 extern char **debug_snoop_includelist
;
174 extern char **debug_snoop_excludelist
;
176 if (!(opt
= strtok( options
, "," ))) goto error
;
179 unsigned char set
= 0, clear
= 0;
180 char *p
= strchr( opt
, '+' );
181 if (!p
) p
= strchr( opt
, '-' );
182 if (!p
|| !p
[1]) goto error
;
185 for (i
= 0; i
< __DBCL_COUNT
; i
++)
187 int len
= strlen(debug_class_names
[i
]);
188 if (len
!= (p
- opt
)) continue;
189 if (!memcmp( opt
, debug_class_names
[i
], len
)) /* found it */
191 if (*p
== '+') set
|= 1 << i
;
192 else clear
|= 1 << i
;
196 if (i
== __DBCL_COUNT
) goto error
; /* class name not found */
200 if (*p
== '+') set
= ~0;
202 if (!strncasecmp(p
+1, "relay=", 6) ||
203 !strncasecmp(p
+1, "snoop=", 6))
206 char *s
, *s2
, ***output
, c
;
214 output
= (*p
== '+') ?
216 &debug_relay_includelist
:
217 &debug_snoop_includelist
) :
219 &debug_relay_excludelist
:
220 &debug_snoop_excludelist
);
222 /* if there are n ':', there are n+1 modules, and we need
223 n+2 slots, last one being for the sentinel (NULL) */
225 while((s
= strchr(s
, ':'))) i
++, s
++;
226 *output
= malloc(sizeof(char **) * i
);
229 while((s2
= strchr(s
, ':'))) {
232 *((*output
)+i
) = _strupr(strdup(s
));
239 *((*output
)+i
) = _strupr(strdup(s
));
241 *((*output
)+i
+1) = NULL
;
246 if (!strcmp( p
, "all" )) p
= ""; /* empty string means all */
247 wine_dbg_add_option( p
, set
, clear
);
248 opt
= strtok( NULL
, "," );
255 MESSAGE("wine: Syntax: --debugmsg [class]+xxx,... or "
256 "-debugmsg [class]-xxx,...\n");
257 MESSAGE("Example: --debugmsg +all,warn-heap\n"
258 " turn on all messages except warning heap messages\n");
259 MESSAGE("Available message classes:\n");
260 for( i
= 0; i
< __DBCL_COUNT
; i
++) MESSAGE( "%-9s", debug_class_names
[i
] );
266 static void remove_options( char *argv
[], int pos
, int count
, int inherit
)
271 for (i
= 0; i
< count
; i
++) len
+= strlen(argv
[pos
+i
]) + 1;
274 if (!(inherit_str
= realloc( inherit_str
, strlen(inherit_str
) + 1 + len
)))
276 strcat( inherit_str
, " " );
280 if (!(inherit_str
= malloc( len
))) out_of_memory();
283 for (i
= 0; i
< count
; i
++)
285 strcat( inherit_str
, argv
[pos
+i
] );
286 if (i
< count
-1) strcat( inherit_str
, " " );
289 while ((argv
[pos
] = argv
[pos
+count
])) pos
++;
292 /* parse options from the argv array and remove all the recognized ones */
293 static void parse_options( char *argv
[] )
295 const struct option
*opt
;
298 for (i
= 0; argv
[i
]; i
++)
300 const char *equalarg
= NULL
;
302 if (*p
++ != '-') continue; /* not an option */
303 if (*p
&& !p
[1]) /* short name */
305 if (*p
== '-') break; /* "--" option */
306 for (opt
= option_table
; opt
->longname
; opt
++) if (opt
->shortname
== *p
) break;
310 const char *equal
= strchr (p
, '=');
312 /* check for the long name */
313 for (opt
= option_table
; opt
->longname
; opt
++) {
315 if (!strcmp( p
, opt
->longname
)) break;
320 strlen (opt
->longname
) == equal
- p
&&
321 !strncmp (p
, opt
->longname
, equal
- p
)) {
322 equalarg
= equal
+ 1;
327 if (!opt
->longname
) continue;
331 opt
->func( equalarg
);
332 remove_options( argv
, i
, 1, opt
->inherit
);
334 else if (opt
->has_arg
&& argv
[i
+1])
336 opt
->func( argv
[i
+1] );
337 remove_options( argv
, i
, 2, opt
->inherit
);
342 remove_options( argv
, i
, 1, opt
->inherit
);
348 /* inherit options from WINEOPTIONS variable */
349 static void inherit_options( char *buffer
)
354 char *p
= strtok( buffer
, " \t" );
355 for (n
= 0; n
< sizeof(argv
)/sizeof(argv
[0])-1 && p
; n
++)
358 p
= strtok( NULL
, " \t" );
361 parse_options( argv
);
362 if (argv
[0]) /* an option remains */
364 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv
[0] );
369 /***********************************************************************
372 void OPTIONS_Usage(void)
374 const struct option
*opt
;
375 MESSAGE( "Usage: %s [options] [--] program_name [arguments]\n", argv0
);
376 MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
377 MESSAGE( "Options:\n" );
378 for (opt
= option_table
; opt
->longname
; opt
++) MESSAGE( " %s\n", opt
->usage
);
382 /***********************************************************************
383 * OPTIONS_ParseOptions
385 void OPTIONS_ParseOptions( char *argv
[] )
390 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer
, sizeof(buffer
) ) && buffer
[0])
391 inherit_options( buffer
);
393 parse_options( argv
+ 1 );
395 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str
);
397 /* check if any option remains */
398 for (i
= 1; argv
[i
]; i
++)
400 if (!strcmp( argv
[i
], "--" ))
402 remove_options( argv
, i
, 1, 0 );
405 if (argv
[i
][0] == '-')
407 MESSAGE( "Unknown option '%s'\n\n", argv
[i
] );
412 /* count the resulting arguments */
415 while (argv
[app_argc
]) app_argc
++;
419 /***********************************************************************
420 * __wine_get_main_args
422 * Return the argc/argv that the application should see.
423 * Used by the startup code generated in the .spec.c file.
425 int __wine_get_main_args( char ***argv
)
432 /***********************************************************************
433 * __wine_get_wmain_args
435 * Same as __wine_get_main_args but for Unicode.
437 int __wine_get_wmain_args( WCHAR
***argv
)
445 for (i
= 0; i
< app_argc
; i
++)
446 total
+= MultiByteToWideChar( CP_ACP
, 0, app_argv
[i
], -1, NULL
, 0 );
448 app_wargv
= HeapAlloc( GetProcessHeap(), 0,
449 total
* sizeof(WCHAR
) + (app_argc
+ 1) * sizeof(*app_wargv
) );
450 p
= (WCHAR
*)(app_wargv
+ app_argc
+ 1);
451 for (i
= 0; i
< app_argc
; i
++)
453 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, app_argv
[i
], -1, p
, total
);
458 app_wargv
[app_argc
] = NULL
;