Better defaults for heap and stack sizes.
[wine/wine64.git] / misc / options.c
blobb799914f9c67fa313bd8b4dacc8e4d5dd9fbed0a
1 /*
2 * Option parsing
4 * Copyright 2000 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include <stdarg.h>
23 #include <string.h>
24 #include <stdlib.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "winreg.h"
30 #include "winternl.h"
31 #include "wine/library.h"
32 #include "options.h"
33 #include "module.h"
34 #include "wine/debug.h"
36 struct option_descr
38 const char *longname;
39 char shortname;
40 int has_arg;
41 int inherit;
42 void (*func)( const char *arg );
43 const char *usage;
46 static char *inherit_str; /* options to pass to child processes */
48 static void DECLSPEC_NORETURN out_of_memory(void);
49 static void out_of_memory(void)
51 MESSAGE( "Virtual memory exhausted\n" );
52 ExitProcess(1);
55 static void do_debugmsg( const char *arg );
56 static void do_dll( const char *arg );
57 static void do_help( const char *arg );
58 static void do_version( const char *arg );
60 static const struct option_descr option_table[] =
62 { "debugmsg", 0, 1, 1, do_debugmsg,
63 "--debugmsg name Turn debugging-messages on or off" },
64 { "dll", 0, 1, 1, do_dll,
65 "--dll name This option is no longer supported" },
66 { "help", 'h', 0, 0, do_help,
67 "--help,-h Show this help message" },
68 { "version", 'v', 0, 0, do_version,
69 "--version,-v Display the Wine version" },
70 { NULL, 0, 0, 0, NULL, NULL } /* terminator */
74 static void do_help( const char *arg )
76 OPTIONS_Usage();
79 static void do_version( const char *arg )
81 MESSAGE( "%s\n", PACKAGE_STRING );
82 ExitProcess(0);
85 static void do_debugmsg( const char *arg )
87 if (wine_dbg_parse_options( arg ))
89 MESSAGE("wine: Syntax: --debugmsg [class]+xxx,... or -debugmsg [class]-xxx,...\n");
90 MESSAGE("Example: --debugmsg +all,warn-heap\n"
91 " turn on all messages except warning heap messages\n");
92 MESSAGE("Available message classes: err, warn, fixme, trace\n\n");
93 ExitProcess(1);
97 static void do_dll( const char *arg )
99 MESSAGE("The --dll option has been removed, you should use\n"
100 "the WINEDLLOVERRIDES environment variable instead.\n"
101 "To see a help message, run:\n"
102 " WINEDLLOVERRIDES=help wine <program.exe>\n");
103 ExitProcess(1);
106 static void remove_options( char *argv[], int pos, int count, int inherit )
108 if (inherit)
110 int i, len = 0;
111 for (i = 0; i < count; i++) len += strlen(argv[pos+i]) + 1;
112 if (inherit_str)
114 if (!(inherit_str = realloc( inherit_str, strlen(inherit_str) + 1 + len )))
115 out_of_memory();
116 strcat( inherit_str, " " );
118 else
120 if (!(inherit_str = malloc( len ))) out_of_memory();
121 inherit_str[0] = 0;
123 for (i = 0; i < count; i++)
125 strcat( inherit_str, argv[pos+i] );
126 if (i < count-1) strcat( inherit_str, " " );
129 while ((argv[pos] = argv[pos+count])) pos++;
132 /* parse options from the argv array and remove all the recognized ones */
133 static void parse_options( char *argv[] )
135 const struct option_descr *opt;
136 int i;
138 for (i = 0; argv[i]; i++)
140 const char *equalarg = NULL;
141 char *p = argv[i];
142 if (*p++ != '-') continue; /* not an option */
143 if (*p && !p[1]) /* short name */
145 if (*p == '-') break; /* "--" option */
146 for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
148 else /* long name */
150 const char *equal = strchr (p, '=');
151 if (*p == '-') p++;
152 /* check for the long name */
153 for (opt = option_table; opt->longname; opt++) {
154 /* Plain --option */
155 if (!strcmp( p, opt->longname )) break;
157 /* --option=value */
158 if (opt->has_arg &&
159 equal &&
160 strlen (opt->longname) == equal - p &&
161 !strncmp (p, opt->longname, equal - p)) {
162 equalarg = equal + 1;
163 break;
167 if (!opt->longname) continue;
169 if (equalarg)
171 opt->func( equalarg );
172 remove_options( argv, i, 1, opt->inherit );
174 else if (opt->has_arg && argv[i+1])
176 opt->func( argv[i+1] );
177 remove_options( argv, i, 2, opt->inherit );
179 else
181 opt->func( "" );
182 remove_options( argv, i, 1, opt->inherit );
184 i--;
188 /* inherit options from WINEOPTIONS variable */
189 static void inherit_options( char *buffer )
191 char *argv[256];
192 unsigned int n;
194 char *p = strtok( buffer, " \t" );
195 for (n = 0; n < sizeof(argv)/sizeof(argv[0])-1 && p; n++)
197 argv[n] = p;
198 p = strtok( NULL, " \t" );
200 argv[n] = NULL;
201 parse_options( argv );
202 if (argv[0]) /* an option remains */
204 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv[0] );
205 OPTIONS_Usage();
209 /***********************************************************************
210 * OPTIONS_Usage
212 void OPTIONS_Usage(void)
214 const struct option_descr *opt;
215 MESSAGE( "%s\n\n", PACKAGE_STRING );
216 MESSAGE( "Usage: wine [options] [--] program_name [arguments]\n" );
217 MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
218 MESSAGE( "Options:\n" );
219 for (opt = option_table; opt->longname; opt++) MESSAGE( " %s\n", opt->usage );
220 ExitProcess(0);
223 /***********************************************************************
224 * OPTIONS_ParseOptions
226 void OPTIONS_ParseOptions( char *argv[] )
228 char buffer[1024];
229 int i;
231 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer, sizeof(buffer) ) && buffer[0])
232 inherit_options( buffer );
233 if (!argv) return;
235 parse_options( argv + 1 );
237 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str );
239 /* check if any option remains */
240 for (i = 1; argv[i]; i++)
242 if (!strcmp( argv[i], "--" ))
244 remove_options( argv, i, 1, 0 );
245 break;
247 if (argv[i][0] == '-')
249 MESSAGE( "Unknown option '%s'\n\n", argv[i] );
250 OPTIONS_Usage();