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