4 * Copyright 2000 Alexandre Julliard
14 #include "wine/library.h"
18 #include "debugtools.h"
26 void (*func
)( const char *arg
);
31 struct options Options
=
33 FALSE
/* Managed windows */
36 const char *argv0
; /* the original argv[0] */
37 const char *full_argv0
; /* the full path of argv[0] (if known) */
39 static char *inherit_str
; /* options to pass to child processes */
41 static int app_argc
; /* argc/argv to pass to application */
42 static char **app_argv
;
43 static WCHAR
**app_wargv
;
45 static void out_of_memory(void) WINE_NORETURN
;
46 static void out_of_memory(void)
48 MESSAGE( "Virtual memory exhausted\n" );
52 static void do_debugmsg( const char *arg
);
53 static void do_help( const char *arg
);
54 static void do_managed( const char *arg
);
55 static void do_version( const char *arg
);
57 static const struct option_descr option_table
[] =
59 { "debugmsg", 0, 1, 1, do_debugmsg
,
60 "--debugmsg name Turn debugging-messages on or off" },
61 { "dll", 0, 1, 1, MODULE_AddLoadOrderOption
,
62 "--dll name Enable or disable built-in DLLs" },
63 { "dosver", 0, 1, 1, VERSION_ParseDosVersion
,
64 "--dosver x.xx DOS version to imitate (e.g. 6.22)\n"
65 " Only valid with --winver win31" },
66 { "help", 'h', 0, 0, do_help
,
67 "--help,-h Show this help message" },
68 { "managed", 0, 0, 0, do_managed
,
69 "--managed Allow the window manager to manage created windows" },
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,nt40,win31,nt2k,win98,nt351,win30,win20)" },
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", WINE_RELEASE_INFO
);
89 static void do_managed( const char *arg
)
91 Options
.managed
= TRUE
;
94 static void do_debugmsg( const char *arg
)
96 static const char * const debug_class_names
[__DBCL_COUNT
] = { "fixme", "err", "warn", "trace" };
98 char *opt
, *options
= strdup(arg
);
100 /* defined in relay32/relay386.c */
101 extern char **debug_relay_includelist
;
102 extern char **debug_relay_excludelist
;
103 /* defined in relay32/snoop.c */
104 extern char **debug_snoop_includelist
;
105 extern char **debug_snoop_excludelist
;
107 if (!(opt
= strtok( options
, "," ))) goto error
;
110 unsigned char set
= 0, clear
= 0;
111 char *p
= strchr( opt
, '+' );
112 if (!p
) p
= strchr( opt
, '-' );
113 if (!p
|| !p
[1]) goto error
;
116 for (i
= 0; i
< __DBCL_COUNT
; i
++)
118 int len
= strlen(debug_class_names
[i
]);
119 if (len
!= (p
- opt
)) continue;
120 if (!memcmp( opt
, debug_class_names
[i
], len
)) /* found it */
122 if (*p
== '+') set
|= 1 << i
;
123 else clear
|= 1 << i
;
127 if (i
== __DBCL_COUNT
) goto error
; /* class name not found */
131 if (*p
== '+') set
= ~0;
133 if (!strncasecmp(p
+1, "relay=", 6) ||
134 !strncasecmp(p
+1, "snoop=", 6))
137 char *s
, *s2
, ***output
, c
;
145 output
= (*p
== '+') ?
147 &debug_relay_includelist
:
148 &debug_snoop_includelist
) :
150 &debug_relay_excludelist
:
151 &debug_snoop_excludelist
);
153 /* if there are n ':', there are n+1 modules, and we need
154 n+2 slots, last one being for the sentinel (NULL) */
156 while((s
= strchr(s
, ':'))) i
++, s
++;
157 *output
= malloc(sizeof(char **) * i
);
160 while((s2
= strchr(s
, ':'))) {
163 *((*output
)+i
) = _strupr(strdup(s
));
170 *((*output
)+i
) = _strupr(strdup(s
));
172 *((*output
)+i
+1) = NULL
;
177 if (!strcmp( p
, "all" )) p
= ""; /* empty string means all */
178 wine_dbg_add_option( p
, set
, clear
);
179 opt
= strtok( NULL
, "," );
186 MESSAGE("wine: Syntax: --debugmsg [class]+xxx,... or "
187 "-debugmsg [class]-xxx,...\n");
188 MESSAGE("Example: --debugmsg +all,warn-heap\n"
189 " turn on all messages except warning heap messages\n");
190 MESSAGE("Available message classes:\n");
191 for( i
= 0; i
< __DBCL_COUNT
; i
++) MESSAGE( "%-9s", debug_class_names
[i
] );
197 static void remove_options( char *argv
[], int pos
, int count
, int inherit
)
202 for (i
= 0; i
< count
; i
++) len
+= strlen(argv
[pos
+i
]) + 1;
205 if (!(inherit_str
= realloc( inherit_str
, strlen(inherit_str
) + 1 + len
)))
207 strcat( inherit_str
, " " );
211 if (!(inherit_str
= malloc( len
))) out_of_memory();
214 for (i
= 0; i
< count
; i
++)
216 strcat( inherit_str
, argv
[pos
+i
] );
217 if (i
< count
-1) strcat( inherit_str
, " " );
220 while ((argv
[pos
] = argv
[pos
+count
])) pos
++;
223 /* parse options from the argv array and remove all the recognized ones */
224 static void parse_options( char *argv
[] )
226 const struct option_descr
*opt
;
229 for (i
= 0; argv
[i
]; i
++)
231 const char *equalarg
= NULL
;
233 if (*p
++ != '-') continue; /* not an option */
234 if (*p
&& !p
[1]) /* short name */
236 if (*p
== '-') break; /* "--" option */
237 for (opt
= option_table
; opt
->longname
; opt
++) if (opt
->shortname
== *p
) break;
241 const char *equal
= strchr (p
, '=');
243 /* check for the long name */
244 for (opt
= option_table
; opt
->longname
; opt
++) {
246 if (!strcmp( p
, opt
->longname
)) break;
251 strlen (opt
->longname
) == equal
- p
&&
252 !strncmp (p
, opt
->longname
, equal
- p
)) {
253 equalarg
= equal
+ 1;
258 if (!opt
->longname
) continue;
262 opt
->func( equalarg
);
263 remove_options( argv
, i
, 1, opt
->inherit
);
265 else if (opt
->has_arg
&& argv
[i
+1])
267 opt
->func( argv
[i
+1] );
268 remove_options( argv
, i
, 2, opt
->inherit
);
273 remove_options( argv
, i
, 1, opt
->inherit
);
279 /* inherit options from WINEOPTIONS variable */
280 static void inherit_options( char *buffer
)
285 char *p
= strtok( buffer
, " \t" );
286 for (n
= 0; n
< sizeof(argv
)/sizeof(argv
[0])-1 && p
; n
++)
289 p
= strtok( NULL
, " \t" );
292 parse_options( argv
);
293 if (argv
[0]) /* an option remains */
295 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv
[0] );
300 /***********************************************************************
303 void OPTIONS_Usage(void)
305 const struct option_descr
*opt
;
306 MESSAGE( "%s\n\n", WINE_RELEASE_INFO
);
307 MESSAGE( "Usage: %s [options] [--] program_name [arguments]\n", argv0
);
308 MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
309 MESSAGE( "Options:\n" );
310 for (opt
= option_table
; opt
->longname
; opt
++) MESSAGE( " %s\n", opt
->usage
);
314 /***********************************************************************
315 * OPTIONS_ParseOptions
317 void OPTIONS_ParseOptions( char *argv
[] )
322 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer
, sizeof(buffer
) ) && buffer
[0])
323 inherit_options( buffer
);
325 parse_options( argv
+ 1 );
327 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str
);
329 /* check if any option remains */
330 for (i
= 1; argv
[i
]; i
++)
332 if (!strcmp( argv
[i
], "--" ))
334 remove_options( argv
, i
, 1, 0 );
337 if (argv
[i
][0] == '-')
339 MESSAGE( "Unknown option '%s'\n\n", argv
[i
] );
344 /* count the resulting arguments */
347 while (argv
[app_argc
]) app_argc
++;
351 /***********************************************************************
352 * __wine_get_main_args (NTDLL.@)
354 * Return the argc/argv that the application should see.
355 * Used by the startup code generated in the .spec.c file.
357 int __wine_get_main_args( char ***argv
)
364 /***********************************************************************
365 * __wine_get_wmain_args (NTDLL.@)
367 * Same as __wine_get_main_args but for Unicode.
369 int __wine_get_wmain_args( WCHAR
***argv
)
377 for (i
= 0; i
< app_argc
; i
++)
378 total
+= MultiByteToWideChar( CP_ACP
, 0, app_argv
[i
], -1, NULL
, 0 );
380 app_wargv
= HeapAlloc( GetProcessHeap(), 0,
381 total
* sizeof(WCHAR
) + (app_argc
+ 1) * sizeof(*app_wargv
) );
382 p
= (WCHAR
*)(app_wargv
+ app_argc
+ 1);
383 for (i
= 0; i
< app_argc
; i
++)
385 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, app_argv
[i
], -1, p
, total
);
390 app_wargv
[app_argc
] = NULL
;