4 * Copyright 2000 Alexandre Julliard
13 #include "wine/library.h"
16 #include "debugtools.h"
24 void (*func
)( const char *arg
);
29 struct options Options
=
31 NULL
, /* desktopGeometry */
34 FALSE
, /* synchronous */
35 FALSE
, /* Managed windows */
36 NULL
/* Alternate config file name */
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_config( const char *arg
);
63 static void do_debugmsg( const char *arg
);
64 static void do_desktop( const char *arg
);
65 static void do_display( const char *arg
);
66 static void do_dll( const char *arg
);
67 static void do_help( const char *arg
);
68 static void do_language( const char *arg
);
69 static void do_managed( const char *arg
);
70 static void do_synchronous( const char *arg
);
71 static void do_version( const char *arg
);
73 static const struct option option_table
[] =
75 { "config", 0, 1, 0, do_config
,
76 "--config name Specify config file to use" },
77 { "debugmsg", 0, 1, 1, do_debugmsg
,
78 "--debugmsg name Turn debugging-messages on or off" },
79 { "desktop", 0, 1, 1, do_desktop
,
80 "--desktop geom Use a desktop window of the given geometry" },
81 { "display", 0, 1, 0, do_display
,
82 "--display name Use the specified display" },
83 { "dll", 0, 1, 1, do_dll
,
84 "--dll name Enable or disable built-in DLLs" },
85 { "dosver", 0, 1, 1, VERSION_ParseDosVersion
,
86 "--dosver x.xx DOS version to imitate (e.g. 6.22)\n"
87 " Only valid with --winver win31" },
88 { "help", 'h', 0, 0, do_help
,
89 "--help,-h Show this help message" },
90 { "language", 0, 1, 1, do_language
,
91 "--language xx Set the language (one of Br,Ca,Cs,Cy,Da,De,En,Eo,Es,Fi,Fr,Ga,Gd,Gv,\n"
92 " He,Hr,Hu,It,Ja,Ko,Kw,Nl,No,Pl,Pt,Sk,Sv,Ru,Wa)" },
93 { "managed", 0, 0, 0, do_managed
,
94 "--managed Allow the window manager to manage created windows" },
95 { "synchronous", 0, 0, 1, do_synchronous
,
96 "--synchronous Turn on synchronous display mode" },
97 { "version", 'v', 0, 0, do_version
,
98 "--version,-v Display the Wine version" },
99 { "winver", 0, 1, 1, VERSION_ParseWinVersion
,
100 "--winver Version to imitate (win95,nt40,win31,nt2k,win98,nt351,win30,win20)" },
101 { NULL
, 0, 0, 0, NULL
, NULL
} /* terminator */
105 static void do_help( const char *arg
)
110 static void do_version( const char *arg
)
112 MESSAGE( "%s\n", WINE_RELEASE_INFO
);
116 static void do_synchronous( const char *arg
)
118 Options
.synchronous
= TRUE
;
121 static void do_desktop( const char *arg
)
123 Options
.desktopGeometry
= xstrdup( arg
);
126 static void do_display( const char *arg
)
128 Options
.display
= xstrdup( arg
);
131 static void do_dll( const char *arg
)
133 if (Options
.dllFlags
)
135 Options
.dllFlags
= (char *) realloc ( Options
.dllFlags
,
136 strlen ( Options
.dllFlags
) + strlen ( arg
) + 2 );
137 if ( !Options
.dllFlags
) out_of_memory();
138 strcat ( Options
.dllFlags
, "+" );
139 strcat ( Options
.dllFlags
, arg
);
143 Options
.dllFlags
= xstrdup( arg
);
147 static void do_language( const char *arg
)
149 SetEnvironmentVariableA( "LANGUAGE", arg
);
152 static void do_managed( const char *arg
)
154 Options
.managed
= TRUE
;
157 static void do_config( const char *arg
)
159 Options
.configFileName
= xstrdup( arg
);
162 static void do_debugmsg( const char *arg
)
164 static const char * const debug_class_names
[__DBCL_COUNT
] = { "fixme", "err", "warn", "trace" };
166 char *opt
, *options
= strdup(arg
);
169 if (!(opt
= strtok( options
, "," ))) goto error
;
172 unsigned char set
= 0, clear
= 0;
173 char *p
= strchr( opt
, '+' );
174 if (!p
) p
= strchr( opt
, '-' );
175 if (!p
|| !p
[1]) goto error
;
178 for (i
= 0; i
< __DBCL_COUNT
; i
++)
180 int len
= strlen(debug_class_names
[i
]);
181 if (len
!= (p
- opt
)) continue;
182 if (!memcmp( opt
, debug_class_names
[i
], len
)) /* found it */
184 if (*p
== '+') set
|= 1 << i
;
185 else clear
|= 1 << i
;
189 if (i
== __DBCL_COUNT
) goto error
; /* class name not found */
193 if (*p
== '+') set
= ~0;
197 if (!strcmp( p
, "all" )) p
= ""; /* empty string means all */
198 wine_dbg_add_option( p
, set
, clear
);
199 opt
= strtok( NULL
, "," );
206 MESSAGE("wine: Syntax: --debugmsg [class]+xxx,... or "
207 "-debugmsg [class]-xxx,...\n");
208 MESSAGE("Example: --debugmsg +all,warn-heap\n"
209 " turn on all messages except warning heap messages\n");
210 MESSAGE("Available message classes:\n");
211 for( i
= 0; i
< __DBCL_COUNT
; i
++) MESSAGE( "%-9s", debug_class_names
[i
] );
217 static void remove_options( char *argv
[], int pos
, int count
, int inherit
)
222 for (i
= 0; i
< count
; i
++) len
+= strlen(argv
[pos
+i
]) + 1;
225 if (!(inherit_str
= realloc( inherit_str
, strlen(inherit_str
) + 1 + len
)))
227 strcat( inherit_str
, " " );
231 if (!(inherit_str
= malloc( len
))) out_of_memory();
234 for (i
= 0; i
< count
; i
++)
236 strcat( inherit_str
, argv
[pos
+i
] );
237 if (i
< count
-1) strcat( inherit_str
, " " );
240 while ((argv
[pos
] = argv
[pos
+count
])) pos
++;
243 /* parse options from the argv array and remove all the recognized ones */
244 static void parse_options( char *argv
[] )
246 const struct option
*opt
;
249 for (i
= 0; argv
[i
]; i
++)
251 const char *equalarg
= NULL
;
253 if (*p
++ != '-') continue; /* not an option */
254 if (*p
&& !p
[1]) /* short name */
256 if (*p
== '-') break; /* "--" option */
257 for (opt
= option_table
; opt
->longname
; opt
++) if (opt
->shortname
== *p
) break;
261 const char *equal
= strchr (p
, '=');
263 /* check for the long name */
264 for (opt
= option_table
; opt
->longname
; opt
++) {
266 if (!strcmp( p
, opt
->longname
)) break;
271 strlen (opt
->longname
) == equal
- p
&&
272 !strncmp (p
, opt
->longname
, equal
- p
)) {
273 equalarg
= equal
+ 1;
278 if (!opt
->longname
) continue;
282 opt
->func( equalarg
);
283 remove_options( argv
, i
, 1, opt
->inherit
);
285 else if (opt
->has_arg
&& argv
[i
+1])
287 opt
->func( argv
[i
+1] );
288 remove_options( argv
, i
, 2, opt
->inherit
);
293 remove_options( argv
, i
, 1, opt
->inherit
);
299 /* inherit options from WINEOPTIONS variable */
300 static void inherit_options( char *buffer
)
305 char *p
= strtok( buffer
, " \t" );
306 for (n
= 0; n
< sizeof(argv
)/sizeof(argv
[0])-1 && p
; n
++)
309 p
= strtok( NULL
, " \t" );
312 parse_options( argv
);
313 if (argv
[0]) /* an option remains */
315 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv
[0] );
320 /***********************************************************************
323 void OPTIONS_Usage(void)
325 const struct option
*opt
;
326 MESSAGE( "Usage: %s [options] [--] program_name [arguments]\n", argv0
);
327 MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
328 MESSAGE( "Options:\n" );
329 for (opt
= option_table
; opt
->longname
; opt
++) MESSAGE( " %s\n", opt
->usage
);
333 /***********************************************************************
334 * OPTIONS_ParseOptions
336 void OPTIONS_ParseOptions( char *argv
[] )
341 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer
, sizeof(buffer
) ) && buffer
[0])
342 inherit_options( buffer
);
344 parse_options( argv
+ 1 );
346 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str
);
348 /* check if any option remains */
349 for (i
= 1; argv
[i
]; i
++)
351 if (!strcmp( argv
[i
], "--" ))
353 remove_options( argv
, i
, 1, 0 );
356 if (argv
[i
][0] == '-')
358 MESSAGE( "Unknown option '%s'\n\n", argv
[i
] );
363 /* count the resulting arguments */
366 while (argv
[app_argc
]) app_argc
++;
370 /***********************************************************************
371 * __wine_get_main_args
373 * Return the argc/argv that the application should see.
374 * Used by the startup code generated in the .spec.c file.
376 int __wine_get_main_args( char ***argv
)
383 /***********************************************************************
384 * __wine_get_wmain_args
386 * Same as __wine_get_main_args but for Unicode.
388 int __wine_get_wmain_args( WCHAR
***argv
)
396 for (i
= 0; i
< app_argc
; i
++)
397 total
+= MultiByteToWideChar( CP_ACP
, 0, app_argv
[i
], -1, NULL
, 0 );
399 app_wargv
= HeapAlloc( GetProcessHeap(), 0,
400 total
* sizeof(WCHAR
) + (app_argc
+ 1) * sizeof(*app_wargv
) );
401 p
= (WCHAR
*)(app_wargv
+ app_argc
+ 1);
402 for (i
= 0; i
< app_argc
; i
++)
404 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, app_argv
[i
], -1, p
, total
);
409 app_wargv
[app_argc
] = NULL
;