Fixed missing wine_tsx11_unlock() on error path.
[wine/multimedia.git] / misc / options.c
blob48691a197a56c6ef4d5d7b86d89d2101ea1d8e9e
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 const char *argv0; /* the original argv[0] */
47 const char *full_argv0; /* the full path of argv[0] (if known) */
49 static char *inherit_str; /* options to pass to child processes */
51 static void DECLSPEC_NORETURN out_of_memory(void);
52 static void out_of_memory(void)
54 MESSAGE( "Virtual memory exhausted\n" );
55 ExitProcess(1);
58 static void do_debugmsg( const char *arg );
59 static void do_dll( const char *arg );
60 static void do_help( const char *arg );
61 static void do_version( const char *arg );
63 static const struct option_descr option_table[] =
65 { "debugmsg", 0, 1, 1, do_debugmsg,
66 "--debugmsg name Turn debugging-messages on or off" },
67 { "dll", 0, 1, 1, do_dll,
68 "--dll name This option is no longer supported" },
69 { "help", 'h', 0, 0, do_help,
70 "--help,-h Show this help message" },
71 { "version", 'v', 0, 0, do_version,
72 "--version,-v Display the Wine version" },
73 { NULL, 0, 0, 0, NULL, NULL } /* terminator */
77 static void do_help( const char *arg )
79 OPTIONS_Usage();
82 static void do_version( const char *arg )
84 MESSAGE( "%s\n", PACKAGE_STRING );
85 ExitProcess(0);
88 static void do_debugmsg( const char *arg )
90 if (wine_dbg_parse_options( arg ))
92 MESSAGE("%s: Syntax: --debugmsg [class]+xxx,... or -debugmsg [class]-xxx,...\n", argv0);
93 MESSAGE("Example: --debugmsg +all,warn-heap\n"
94 " turn on all messages except warning heap messages\n");
95 MESSAGE("Available message classes: err, warn, fixme, trace\n\n");
96 ExitProcess(1);
100 static void do_dll( const char *arg )
102 MESSAGE("The --dll option has been removed, you should use\n"
103 "the WINEDLLOVERRIDES environment variable instead.\n"
104 "To see a help message, run:\n"
105 " WINEDLLOVERRIDES=help wine <program.exe>\n");
106 ExitProcess(1);
109 static void remove_options( char *argv[], int pos, int count, int inherit )
111 if (inherit)
113 int i, len = 0;
114 for (i = 0; i < count; i++) len += strlen(argv[pos+i]) + 1;
115 if (inherit_str)
117 if (!(inherit_str = realloc( inherit_str, strlen(inherit_str) + 1 + len )))
118 out_of_memory();
119 strcat( inherit_str, " " );
121 else
123 if (!(inherit_str = malloc( len ))) out_of_memory();
124 inherit_str[0] = 0;
126 for (i = 0; i < count; i++)
128 strcat( inherit_str, argv[pos+i] );
129 if (i < count-1) strcat( inherit_str, " " );
132 while ((argv[pos] = argv[pos+count])) pos++;
135 /* parse options from the argv array and remove all the recognized ones */
136 static void parse_options( char *argv[] )
138 const struct option_descr *opt;
139 int i;
141 for (i = 0; argv[i]; i++)
143 const char *equalarg = NULL;
144 char *p = argv[i];
145 if (*p++ != '-') continue; /* not an option */
146 if (*p && !p[1]) /* short name */
148 if (*p == '-') break; /* "--" option */
149 for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
151 else /* long name */
153 const char *equal = strchr (p, '=');
154 if (*p == '-') p++;
155 /* check for the long name */
156 for (opt = option_table; opt->longname; opt++) {
157 /* Plain --option */
158 if (!strcmp( p, opt->longname )) break;
160 /* --option=value */
161 if (opt->has_arg &&
162 equal &&
163 strlen (opt->longname) == equal - p &&
164 !strncmp (p, opt->longname, equal - p)) {
165 equalarg = equal + 1;
166 break;
170 if (!opt->longname) continue;
172 if (equalarg)
174 opt->func( equalarg );
175 remove_options( argv, i, 1, opt->inherit );
177 else if (opt->has_arg && argv[i+1])
179 opt->func( argv[i+1] );
180 remove_options( argv, i, 2, opt->inherit );
182 else
184 opt->func( "" );
185 remove_options( argv, i, 1, opt->inherit );
187 i--;
191 /* inherit options from WINEOPTIONS variable */
192 static void inherit_options( char *buffer )
194 char *argv[256];
195 unsigned int n;
197 char *p = strtok( buffer, " \t" );
198 for (n = 0; n < sizeof(argv)/sizeof(argv[0])-1 && p; n++)
200 argv[n] = p;
201 p = strtok( NULL, " \t" );
203 argv[n] = NULL;
204 parse_options( argv );
205 if (argv[0]) /* an option remains */
207 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv[0] );
208 OPTIONS_Usage();
212 /***********************************************************************
213 * OPTIONS_Usage
215 void OPTIONS_Usage(void)
217 const struct option_descr *opt;
218 MESSAGE( "%s\n\n", PACKAGE_STRING );
219 MESSAGE( "Usage: %s [options] [--] program_name [arguments]\n", argv0 );
220 MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
221 MESSAGE( "Options:\n" );
222 for (opt = option_table; opt->longname; opt++) MESSAGE( " %s\n", opt->usage );
223 ExitProcess(0);
226 /***********************************************************************
227 * OPTIONS_ParseOptions
229 void OPTIONS_ParseOptions( char *argv[] )
231 char buffer[1024];
232 int i;
234 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer, sizeof(buffer) ) && buffer[0])
235 inherit_options( buffer );
236 if (!argv) return;
238 parse_options( argv + 1 );
240 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str );
242 /* check if any option remains */
243 for (i = 1; argv[i]; i++)
245 if (!strcmp( argv[i], "--" ))
247 remove_options( argv, i, 1, 0 );
248 break;
250 if (argv[i][0] == '-')
252 MESSAGE( "Unknown option '%s'\n\n", argv[i] );
253 OPTIONS_Usage();