Fixed off-by-one error in string allocation.
[wine.git] / misc / options.c
blobea5729a33260aaae1c89e57b9cb815c34bd9f06d
1 /*
2 * Option parsing
4 * Copyright 2000 Alexandre Julliard
5 */
7 #include "config.h"
8 #include <string.h>
9 #include <stdlib.h>
11 #include "winbase.h"
12 #include "main.h"
13 #include "options.h"
14 #include "version.h"
15 #include "debugtools.h"
17 struct option
19 const char *longname;
20 char shortname;
21 int has_arg;
22 int inherit;
23 void (*func)( const char *arg );
24 const char *usage;
27 /* Most Windows C/C++ compilers use something like this to */
28 /* access argc and argv globally: */
29 int _ARGC;
30 char **_ARGV;
32 /* default options */
33 struct options Options =
35 NULL, /* desktopGeometry */
36 NULL, /* display */
37 NULL, /* dllFlags */
38 FALSE, /* synchronous */
39 0, /* language */
40 FALSE, /* Managed windows */
41 NULL /* Alternate config file name */
44 const char *argv0;
46 static char *inherit_str; /* options to pass to child processes */
48 static void out_of_memory(void) WINE_NORETURN;
49 static void out_of_memory(void)
51 MESSAGE( "Virtual memory exhausted\n" );
52 ExitProcess(1);
55 static char *xstrdup( const char *str )
57 char *ret = strdup( str );
58 if (!ret) out_of_memory();
59 return ret;
62 static void do_config( 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_managed( const char *arg );
68 static void do_synchronous( const char *arg );
69 static void do_version( const char *arg );
71 static const struct option option_table[] =
73 { "config", 0, 1, 0, do_config,
74 "--config name Specify config file to use" },
75 { "debugmsg", 0, 1, 1, MAIN_ParseDebugOptions,
76 "--debugmsg name Turn debugging-messages on or off" },
77 { "desktop", 0, 1, 1, do_desktop,
78 "--desktop geom Use a desktop window of the given geometry" },
79 { "display", 0, 1, 0, do_display,
80 "--display name Use the specified display" },
81 { "dll", 0, 1, 1, do_dll,
82 "--dll name Enable or disable built-in DLLs" },
83 { "dosver", 0, 1, 1, VERSION_ParseDosVersion,
84 "--dosver x.xx DOS version to imitate (e.g. 6.22). Only valid with --winver win31" },
85 { "help", 'h', 0, 0, do_help,
86 "--help,-h Show this help message" },
87 { "language", 0, 1, 1, MAIN_ParseLanguageOption,
88 "--language xx Set the language (one of Br,Ca,Cs,Cy,Da,De,En,Eo,Es,Fi,Fr,Ga,Gd,Gv,\n"
89 " Hr,Hu,It,Ja,Ko,Kw,Nl,No,Pl,Pt,Sk,Sv,Ru,Wa)" },
90 { "managed", 0, 0, 0, do_managed,
91 "--managed Allow the window manager to manage created windows" },
92 { "synchronous", 0, 0, 1, do_synchronous,
93 "--synchronous Turn on synchronous display mode" },
94 { "version", 'v', 0, 0, do_version,
95 "--version,-v Display the Wine version" },
96 { "winver", 0, 1, 1, VERSION_ParseWinVersion,
97 "--winver Version to imitate (one of win31,win95,nt351,nt40)" },
98 { NULL, } /* terminator */
102 static void do_help( const char *arg )
104 OPTIONS_Usage();
107 static void do_version( const char *arg )
109 MESSAGE( "%s\n", WINE_RELEASE_INFO );
110 ExitProcess(0);
113 static void do_synchronous( const char *arg )
115 Options.synchronous = TRUE;
118 static void do_desktop( const char *arg )
120 Options.desktopGeometry = xstrdup( arg );
123 static void do_display( const char *arg )
125 Options.display = xstrdup( arg );
128 static void do_dll( const char *arg )
130 if (Options.dllFlags)
132 /* don't overwrite previous value. Should we
133 * automatically add the ',' between multiple DLLs ?
135 MESSAGE("Only one -dll flag is allowed. Use ',' between multiple DLLs\n");
136 ExitProcess(1);
138 Options.dllFlags = xstrdup( arg );
141 static void do_managed( const char *arg )
143 Options.managed = TRUE;
146 static void do_config( const char *arg )
148 Options.configFileName = xstrdup( arg );
151 static void remove_options( char *argv[], int pos, int count, int inherit )
153 if (inherit)
155 int i, len = 0;
156 for (i = 0; i < count; i++) len += strlen(argv[pos+i]) + 1;
157 if (inherit_str)
159 if (!(inherit_str = realloc( inherit_str, strlen(inherit_str) + 1 + len )))
160 out_of_memory();
161 strcat( inherit_str, " " );
163 else
165 if (!(inherit_str = malloc( len ))) out_of_memory();
166 inherit_str[0] = 0;
168 for (i = 0; i < count; i++)
170 strcat( inherit_str, argv[pos+i] );
171 if (i < count-1) strcat( inherit_str, " " );
174 while ((argv[pos] = argv[pos+count])) pos++;
177 /* parse options from the argv array and remove all the recognized ones */
178 static void parse_options( char *argv[] )
180 const struct option *opt;
181 int i;
183 for (i = 0; argv[i]; i++)
185 char *p = argv[i];
186 if (*p++ != '-') continue; /* not an option */
187 if (*p && !p[1]) /* short name */
189 if (*p == '-') break; /* "--" option */
190 for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
192 else /* long name */
194 if (*p == '-') p++;
195 /* check for the long name */
196 for (opt = option_table; opt->longname; opt++)
197 if (!strcmp( p, opt->longname )) break;
199 if (!opt->longname) continue;
201 if (opt->has_arg && argv[i+1])
203 opt->func( argv[i+1] );
204 remove_options( argv, i, 2, opt->inherit );
206 else
208 opt->func( "" );
209 remove_options( argv, i, 1, opt->inherit );
211 i--;
215 /* inherit options from WINEOPTIONS variable */
216 static void inherit_options( char *buffer )
218 char *argv[256];
219 int i;
221 char *p = strtok( buffer, " \t" );
222 for (i = 0; i < sizeof(argv)/sizeof(argv[0])-1 && p; i++)
224 argv[i] = p;
225 p = strtok( NULL, " \t" );
227 argv[i] = NULL;
228 parse_options( argv );
229 if (argv[0]) /* an option remains */
231 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv[0] );
232 OPTIONS_Usage();
236 /***********************************************************************
237 * OPTIONS_Usage
239 void OPTIONS_Usage(void)
241 const struct option *opt;
242 MESSAGE( "Usage: %s [options] program_name [arguments]\n\n", argv0 );
243 MESSAGE( "Options:\n" );
244 for (opt = option_table; opt->longname; opt++) MESSAGE( " %s\n", opt->usage );
245 ExitProcess(0);
248 /***********************************************************************
249 * OPTIONS_ParseOptions
251 void OPTIONS_ParseOptions( char *argv[] )
253 char buffer[1024];
254 int i;
256 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer, sizeof(buffer) ) && buffer[0])
257 inherit_options( buffer );
259 parse_options( argv + 1 );
261 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str );
263 /* check if any option remains */
264 for (i = 1; argv[i]; i++)
266 if (!strcmp( argv[i], "--" ))
268 remove_options( argv, i, 1, 0 );
269 break;
271 if (argv[i][0] == '-')
273 MESSAGE( "Unknown option '%s'\n\n", argv[i] );
274 OPTIONS_Usage();
278 /* count the resulting arguments */
279 _ARGV = argv;
280 _ARGC = 0;
281 while (argv[_ARGC]) _ARGC++;