Added alias capability to --winver.
[wine] / misc / options.c
1 /*
2  * Option parsing
3  *
4  * Copyright 2000 Alexandre Julliard
5  */
6
7 #include "config.h"
8 #include <string.h>
9 #include <stdlib.h>
10
11 #include "winbase.h"
12 #include "wine/library.h"
13 #include "main.h"
14 #include "options.h"
15 #include "version.h"
16 #include "debugtools.h"
17
18 struct option
19 {
20     const char *longname;
21     char        shortname;
22     int         has_arg;
23     int         inherit;
24     void      (*func)( const char *arg );
25     const char *usage;
26 };
27
28 /* default options */
29 struct options Options =
30 {
31     NULL,           /* desktopGeometry */
32     NULL,           /* display */
33     NULL,           /* dllFlags */
34     FALSE,          /* synchronous */
35     FALSE,          /* Managed windows */
36     NULL            /* Alternate config file name */
37 };
38
39 const char *argv0;       /* the original argv[0] */
40 const char *full_argv0;  /* the full path of argv[0] (if known) */
41
42 static char *inherit_str;  /* options to pass to child processes */
43
44 static int app_argc;       /* argc/argv to pass to application */
45 static char **app_argv;
46
47 static void out_of_memory(void) WINE_NORETURN;
48 static void out_of_memory(void)
49 {
50     MESSAGE( "Virtual memory exhausted\n" );
51     ExitProcess(1);
52 }
53
54 static char *xstrdup( const char *str )
55 {
56     char *ret = strdup( str );
57     if (!ret) out_of_memory();
58     return ret;
59 }
60
61 static void do_config( const char *arg );
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 );
71
72 static const struct option option_table[] =
73 {
74     { "config",       0, 1, 0, do_config,
75       "--config name    Specify config file to use" },
76     { "debugmsg",     0, 1, 1, do_debugmsg,
77       "--debugmsg name  Turn debugging-messages on or off" },
78     { "desktop",      0, 1, 1, do_desktop,
79       "--desktop geom   Use a desktop window of the given geometry" },
80     { "display",      0, 1, 0, do_display,
81       "--display name   Use the specified display" },
82     { "dll",          0, 1, 1, do_dll,
83       "--dll name       Enable or disable built-in DLLs" },
84     { "dosver",       0, 1, 1, VERSION_ParseDosVersion,
85       "--dosver x.xx    DOS version to imitate (e.g. 6.22)\n"
86       "                    Only valid with --winver win31" },
87     { "help",       'h', 0, 0, do_help,
88       "--help,-h        Show this help message" },
89     { "language",     0, 1, 1, do_language,
90       "--language xx    Set the language (one of Br,Ca,Cs,Cy,Da,De,En,Eo,Es,Fi,Fr,Ga,Gd,Gv,\n"
91       "                    Hr,Hu,It,Ja,Ko,Kw,Nl,No,Pl,Pt,Sk,Sv,Ru,Wa)" },
92     { "managed",      0, 0, 0, do_managed,
93       "--managed        Allow the window manager to manage created windows" },
94     { "synchronous",  0, 0, 1, do_synchronous,
95       "--synchronous    Turn on synchronous display mode" },
96     { "version",    'v', 0, 0, do_version,
97       "--version,-v     Display the Wine version" },
98     { "winver",       0, 1, 1, VERSION_ParseWinVersion,
99       "--winver         Version to imitate (win95,nt40,win31,nt2k,win98,nt351,win30,win20)" },
100     { NULL,           0, 0, 0, NULL, NULL }  /* terminator */
101 };
102
103
104 static void do_help( const char *arg )
105 {
106     OPTIONS_Usage();
107 }
108
109 static void do_version( const char *arg )
110 {
111     MESSAGE( "%s\n", WINE_RELEASE_INFO );
112     ExitProcess(0);
113 }
114
115 static void do_synchronous( const char *arg )
116 {
117     Options.synchronous = TRUE;
118 }
119
120 static void do_desktop( const char *arg )
121 {
122     Options.desktopGeometry = xstrdup( arg );
123 }
124
125 static void do_display( const char *arg )
126 {
127     Options.display = xstrdup( arg );
128 }
129
130 static void do_dll( const char *arg )
131 {
132     if (Options.dllFlags)
133     {
134         Options.dllFlags = (char *) realloc ( Options.dllFlags, 
135                                             strlen ( Options.dllFlags ) + strlen ( arg ) + 2 );
136         if ( !Options.dllFlags ) out_of_memory(); 
137         strcat ( Options.dllFlags, "+" );
138         strcat ( Options.dllFlags, arg );
139     }
140     else 
141     {
142         Options.dllFlags = xstrdup( arg );
143     }
144 }
145
146 static void do_language( const char *arg )
147 {
148     SetEnvironmentVariableA( "LANGUAGE", arg );
149 }
150
151 static void do_managed( const char *arg )
152 {
153     Options.managed = TRUE;
154 }
155
156 static void do_config( const char *arg )
157 {
158     Options.configFileName = xstrdup( arg );
159 }
160
161 static void do_debugmsg( const char *arg )
162 {
163     static const char * const debug_class_names[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
164
165     char *opt, *options = strdup(arg);
166     int i;
167
168     if (!(opt = strtok( options, "," ))) goto error;
169     do
170     {
171         unsigned char set = 0, clear = 0;
172         char *p = strchr( opt, '+' );
173         if (!p) p = strchr( opt, '-' );
174         if (!p || !p[1]) goto error;
175         if (p > opt)
176         {
177             for (i = 0; i < __DBCL_COUNT; i++)
178             {
179                 int len = strlen(debug_class_names[i]);
180                 if (len != (p - opt)) continue;
181                 if (!memcmp( opt, debug_class_names[i], len ))  /* found it */
182                 {
183                     if (*p == '+') set |= 1 << i;
184                     else clear |= 1 << i;
185                     break;
186                 }
187             }
188             if (i == __DBCL_COUNT) goto error;  /* class name not found */
189         }
190         else
191         {
192             if (*p == '+') set = ~0;
193             else clear = ~0;
194         }
195         p++;
196         if (!strcmp( p, "all" )) p = "";  /* empty string means all */
197         wine_dbg_add_option( p, set, clear );
198         opt = strtok( NULL, "," );
199     } while(opt);
200
201     free( options );
202     return;
203
204  error:
205     MESSAGE("wine: Syntax: --debugmsg [class]+xxx,...  or "
206             "-debugmsg [class]-xxx,...\n");
207     MESSAGE("Example: --debugmsg +all,warn-heap\n"
208             "  turn on all messages except warning heap messages\n");
209     MESSAGE("Available message classes:\n");
210     for( i = 0; i < __DBCL_COUNT; i++) MESSAGE( "%-9s", debug_class_names[i] );
211     MESSAGE("\n\n");
212     ExitProcess(1);
213 }
214
215
216 static void remove_options( char *argv[], int pos, int count, int inherit )
217 {
218     if (inherit)
219     {
220         int i, len = 0;
221         for (i = 0; i < count; i++) len += strlen(argv[pos+i]) + 1;
222         if (inherit_str)
223         {
224             if (!(inherit_str = realloc( inherit_str, strlen(inherit_str) + 1 + len )))
225                 out_of_memory();
226             strcat( inherit_str, " " );
227         }
228         else
229         {
230             if (!(inherit_str = malloc( len ))) out_of_memory();
231             inherit_str[0] = 0;
232         }
233         for (i = 0; i < count; i++)
234         {
235             strcat( inherit_str, argv[pos+i] );
236             if (i < count-1) strcat( inherit_str, " " );
237         }
238     }
239     while ((argv[pos] = argv[pos+count])) pos++;
240 }
241
242 /* parse options from the argv array and remove all the recognized ones */
243 static void parse_options( char *argv[] )
244 {
245     const struct option *opt;
246     int i;
247
248     for (i = 0; argv[i]; i++)
249     {
250         char *p = argv[i];
251         if (*p++ != '-') continue;  /* not an option */
252         if (*p && !p[1]) /* short name */
253         {
254             if (*p == '-') break; /* "--" option */
255             for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
256         }
257         else  /* long name */
258         {
259             if (*p == '-') p++;
260             /* check for the long name */
261             for (opt = option_table; opt->longname; opt++)
262                 if (!strcmp( p, opt->longname )) break;
263         }
264         if (!opt->longname) continue;
265
266         if (opt->has_arg && argv[i+1])
267         {
268             opt->func( argv[i+1] );
269             remove_options( argv, i, 2, opt->inherit );
270         }
271         else
272         {
273             opt->func( "" );
274             remove_options( argv, i, 1, opt->inherit );
275         }
276         i--;
277     }
278 }
279
280 /* inherit options from WINEOPTIONS variable */
281 static void inherit_options( char *buffer )
282 {
283     char *argv[256];
284     unsigned int n;
285
286     char *p = strtok( buffer, " \t" );
287     for (n = 0; n < sizeof(argv)/sizeof(argv[0])-1 && p; n++)
288     {
289         argv[n] = p;
290         p = strtok( NULL, " \t" );
291     }
292     argv[n] = NULL;
293     parse_options( argv );
294     if (argv[0])  /* an option remains */
295     {
296         MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv[0] );
297         OPTIONS_Usage();
298     }
299 }
300
301 /***********************************************************************
302  *              OPTIONS_Usage
303  */
304 void OPTIONS_Usage(void)
305 {
306     const struct option *opt;
307     MESSAGE( "Usage: %s [options] program_name [arguments]\n\n", argv0 );
308     MESSAGE( "Options:\n" );
309     for (opt = option_table; opt->longname; opt++) MESSAGE( "   %s\n", opt->usage );
310     ExitProcess(0);
311 }
312
313 /***********************************************************************
314  *              OPTIONS_ParseOptions
315  */
316 void OPTIONS_ParseOptions( char *argv[] )
317 {
318     char buffer[1024];
319     int i;
320
321     if (GetEnvironmentVariableA( "WINEOPTIONS", buffer, sizeof(buffer) ) && buffer[0])
322         inherit_options( buffer );
323
324     parse_options( argv + 1 );
325
326     SetEnvironmentVariableA( "WINEOPTIONS", inherit_str );
327
328     /* check if any option remains */
329     for (i = 1; argv[i]; i++)
330     {
331         if (!strcmp( argv[i], "--" ))
332         {
333             remove_options( argv, i, 1, 0 );
334             break;
335         }
336         if (argv[i][0] == '-')
337         {
338             MESSAGE( "Unknown option '%s'\n\n", argv[i] );
339             OPTIONS_Usage();
340         }
341     }
342
343     /* count the resulting arguments */
344     app_argv = argv;
345     app_argc = 0;
346     while (argv[app_argc]) app_argc++;
347 }
348
349
350 /***********************************************************************
351  *              __wine_get_main_args
352  *
353  * Return the argc/argv that the application should see.
354  * Used by the startup code generated in the .spec.c file.
355  */
356 int __wine_get_main_args( char ***argv )
357 {
358     *argv = app_argv;
359     return app_argc;
360 }
361