Added LGPL standard comment, and copyright notices where necessary.
[wine] / misc / options.c
1 /*
2  * Option parsing
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "ntddk.h"
28 #include "wine/library.h"
29 #include "wine/version.h"
30 #include "options.h"
31 #include "module.h"
32 #include "wine/debug.h"
33
34 struct option_descr
35 {
36     const char *longname;
37     char        shortname;
38     int         has_arg;
39     int         inherit;
40     void      (*func)( const char *arg );
41     const char *usage;
42 };
43
44 /* default options */
45 struct options Options =
46 {
47     FALSE           /* Managed windows */
48 };
49
50 const char *argv0;       /* the original argv[0] */
51 const char *full_argv0;  /* the full path of argv[0] (if known) */
52
53 static char *inherit_str;  /* options to pass to child processes */
54
55 static void out_of_memory(void) WINE_NORETURN;
56 static void out_of_memory(void)
57 {
58     MESSAGE( "Virtual memory exhausted\n" );
59     ExitProcess(1);
60 }
61
62 static void do_debugmsg( const char *arg );
63 static void do_help( const char *arg );
64 static void do_managed( const char *arg );
65 static void do_version( const char *arg );
66
67 static const struct option_descr option_table[] =
68 {
69     { "debugmsg",     0, 1, 1, do_debugmsg,
70       "--debugmsg name  Turn debugging-messages on or off" },
71     { "dll",          0, 1, 1, MODULE_AddLoadOrderOption,
72       "--dll name       Enable or disable built-in DLLs" },
73     { "dosver",       0, 1, 1, VERSION_ParseDosVersion,
74       "--dosver x.xx    DOS version to imitate (e.g. 6.22)\n"
75       "                    Only valid with --winver win31" },
76     { "help",       'h', 0, 0, do_help,
77       "--help,-h        Show this help message" },
78     { "managed",      0, 0, 0, do_managed,
79       "--managed        Allow the window manager to manage created windows" },
80     { "version",    'v', 0, 0, do_version,
81       "--version,-v     Display the Wine version" },
82     { "winver",       0, 1, 1, VERSION_ParseWinVersion,
83       "--winver         Version to imitate (win95,win98,winme,nt351,nt40,win2k,winxp,win20,win30,win31)" },
84     { NULL,           0, 0, 0, NULL, NULL }  /* terminator */
85 };
86
87
88 static void do_help( const char *arg )
89 {
90     OPTIONS_Usage();
91 }
92
93 static void do_version( const char *arg )
94 {
95     MESSAGE( "%s\n", WINE_RELEASE_INFO );
96     ExitProcess(0);
97 }
98
99 static void do_managed( const char *arg )
100 {
101     Options.managed = TRUE;
102 }
103
104 static void do_debugmsg( const char *arg )
105 {
106     static const char * const debug_class_names[__WINE_DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
107
108     char *opt, *options = strdup(arg);
109     int i;
110     /* defined in relay32/relay386.c */
111     extern char **debug_relay_includelist;
112     extern char **debug_relay_excludelist;
113     /* defined in relay32/snoop.c */
114     extern char **debug_snoop_includelist;
115     extern char **debug_snoop_excludelist;
116
117     if (!(opt = strtok( options, "," ))) goto error;
118     do
119     {
120         unsigned char set = 0, clear = 0;
121         char *p = strchr( opt, '+' );
122         if (!p) p = strchr( opt, '-' );
123         if (!p || !p[1]) goto error;
124         if (p > opt)
125         {
126             for (i = 0; i < __WINE_DBCL_COUNT; i++)
127             {
128                 int len = strlen(debug_class_names[i]);
129                 if (len != (p - opt)) continue;
130                 if (!memcmp( opt, debug_class_names[i], len ))  /* found it */
131                 {
132                     if (*p == '+') set |= 1 << i;
133                     else clear |= 1 << i;
134                     break;
135                 }
136             }
137             if (i == __WINE_DBCL_COUNT) goto error;  /* class name not found */
138         }
139         else
140         {
141             if (*p == '+') set = ~0;
142             else clear = ~0;
143             if (!strncasecmp(p+1, "relay=", 6) ||
144                 !strncasecmp(p+1, "snoop=", 6))
145                 {
146                     int i, l;
147                     char *s, *s2, ***output, c;
148
149                     if (strchr(p,','))
150                         l=strchr(p,',')-p;
151                     else
152                         l=strlen(p);
153                     set = ~0;
154                     clear = 0;
155                     output = (*p == '+') ?
156                         ((*(p+1) == 'r') ?
157                          &debug_relay_includelist :
158                          &debug_snoop_includelist) :
159                         ((*(p+1) == 'r') ?
160                          &debug_relay_excludelist :
161                          &debug_snoop_excludelist);
162                     s = p + 7;
163                     /* if there are n ':', there are n+1 modules, and we need
164                        n+2 slots, last one being for the sentinel (NULL) */
165                     i = 2;      
166                     while((s = strchr(s, ':'))) i++, s++;
167                     *output = malloc(sizeof(char **) * i);
168                     i = 0;
169                     s = p + 7;
170                     while((s2 = strchr(s, ':'))) {
171                         c = *s2;
172                         *s2 = '\0';
173                         *((*output)+i) = _strupr(strdup(s));
174                         *s2 = c;
175                         s = s2 + 1;
176                         i++;
177                     }
178                     c = *(p + l);
179                     *(p + l) = '\0';
180                     *((*output)+i) = _strupr(strdup(s));
181                     *(p + l) = c;
182                     *((*output)+i+1) = NULL;
183                     *(p + 6) = '\0';
184                 }
185         }
186         p++;
187         if (!strcmp( p, "all" )) p = "";  /* empty string means all */
188         wine_dbg_add_option( p, set, clear );
189         opt = strtok( NULL, "," );
190     } while(opt);
191
192     free( options );
193     return;
194
195  error:
196     MESSAGE("wine: Syntax: --debugmsg [class]+xxx,...  or "
197             "-debugmsg [class]-xxx,...\n");
198     MESSAGE("Example: --debugmsg +all,warn-heap\n"
199             "  turn on all messages except warning heap messages\n");
200     MESSAGE("Available message classes:\n");
201     for( i = 0; i < __WINE_DBCL_COUNT; i++) MESSAGE( "%-9s", debug_class_names[i] );
202     MESSAGE("\n\n");
203     ExitProcess(1);
204 }
205
206
207 static void remove_options( char *argv[], int pos, int count, int inherit )
208 {
209     if (inherit)
210     {
211         int i, len = 0;
212         for (i = 0; i < count; i++) len += strlen(argv[pos+i]) + 1;
213         if (inherit_str)
214         {
215             if (!(inherit_str = realloc( inherit_str, strlen(inherit_str) + 1 + len )))
216                 out_of_memory();
217             strcat( inherit_str, " " );
218         }
219         else
220         {
221             if (!(inherit_str = malloc( len ))) out_of_memory();
222             inherit_str[0] = 0;
223         }
224         for (i = 0; i < count; i++)
225         {
226             strcat( inherit_str, argv[pos+i] );
227             if (i < count-1) strcat( inherit_str, " " );
228         }
229     }
230     while ((argv[pos] = argv[pos+count])) pos++;
231 }
232
233 /* parse options from the argv array and remove all the recognized ones */
234 static void parse_options( char *argv[] )
235 {
236     const struct option_descr *opt;
237     int i;
238
239     for (i = 0; argv[i]; i++)
240     {
241         const char *equalarg = NULL;
242         char *p = argv[i];
243         if (*p++ != '-') continue;  /* not an option */
244         if (*p && !p[1]) /* short name */
245         {
246             if (*p == '-') break; /* "--" option */
247             for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
248         }
249         else  /* long name */
250         {
251             const char *equal = strchr  (p, '=');
252             if (*p == '-') p++;
253             /* check for the long name */
254             for (opt = option_table; opt->longname; opt++) {
255                 /* Plain --option */
256                 if (!strcmp( p, opt->longname )) break;
257
258                 /* --option=value */
259                 if (opt->has_arg &&
260                     equal &&
261                     strlen (opt->longname) == equal - p &&
262                     !strncmp (p, opt->longname, equal - p)) {
263                         equalarg = equal + 1;
264                         break;
265                     }
266             }
267         }
268         if (!opt->longname) continue;
269
270         if (equalarg)
271         {
272             opt->func( equalarg );
273             remove_options( argv, i, 1, opt->inherit );
274         }
275         else if (opt->has_arg && argv[i+1])
276         {
277             opt->func( argv[i+1] );
278             remove_options( argv, i, 2, opt->inherit );
279         }
280         else
281         {
282             opt->func( "" );
283             remove_options( argv, i, 1, opt->inherit );
284         }
285         i--;
286     }
287 }
288
289 /* inherit options from WINEOPTIONS variable */
290 static void inherit_options( char *buffer )
291 {
292     char *argv[256];
293     unsigned int n;
294
295     char *p = strtok( buffer, " \t" );
296     for (n = 0; n < sizeof(argv)/sizeof(argv[0])-1 && p; n++)
297     {
298         argv[n] = p;
299         p = strtok( NULL, " \t" );
300     }
301     argv[n] = NULL;
302     parse_options( argv );
303     if (argv[0])  /* an option remains */
304     {
305         MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv[0] );
306         OPTIONS_Usage();
307     }
308 }
309
310 /***********************************************************************
311  *              OPTIONS_Usage
312  */
313 void OPTIONS_Usage(void)
314 {
315     const struct option_descr *opt;
316     MESSAGE( "%s\n\n", WINE_RELEASE_INFO );
317     MESSAGE( "Usage: %s [options] [--] program_name [arguments]\n", argv0 );
318     MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
319     MESSAGE( "Options:\n" );
320     for (opt = option_table; opt->longname; opt++) MESSAGE( "   %s\n", opt->usage );
321     ExitProcess(0);
322 }
323
324 /***********************************************************************
325  *              OPTIONS_ParseOptions
326  */
327 void OPTIONS_ParseOptions( char *argv[] )
328 {
329     char buffer[1024];
330     int i;
331
332     if (GetEnvironmentVariableA( "WINEOPTIONS", buffer, sizeof(buffer) ) && buffer[0])
333         inherit_options( buffer );
334
335     parse_options( argv + 1 );
336
337     SetEnvironmentVariableA( "WINEOPTIONS", inherit_str );
338
339     /* check if any option remains */
340     for (i = 1; argv[i]; i++)
341     {
342         if (!strcmp( argv[i], "--" ))
343         {
344             remove_options( argv, i, 1, 0 );
345             break;
346         }
347         if (argv[i][0] == '-')
348         {
349             MESSAGE( "Unknown option '%s'\n\n", argv[i] );
350             OPTIONS_Usage();
351         }
352     }
353 }