Fixed crash when selecting the drive letter combo box.
[wine/wine64.git] / misc / options.c
blobb27a37c4e4179a2036c9ecebab5042f7bd177c01
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 "wine/debug.h"
35 struct option_descr
37 const char *longname;
38 char shortname;
39 int has_arg;
40 void (*func)( const char *arg );
41 const char *usage;
44 static void do_debugmsg( const char *arg );
45 static void do_help( const char *arg );
46 static void do_version( const char *arg );
48 static const struct option_descr option_table[] =
50 { "debugmsg", 0, 1, do_debugmsg,
51 "--debugmsg name No longer supported, use the WINEDEBUG variable instead" },
52 { "help", 'h', 0, do_help,
53 "--help,-h Show this help message" },
54 { "version", 'v', 0, do_version,
55 "--version,-v Display the Wine version" },
56 { NULL, 0, 0, NULL, NULL } /* terminator */
60 static void do_help( const char *arg )
62 OPTIONS_Usage();
65 static void do_version( const char *arg )
67 MESSAGE( "%s\n", PACKAGE_STRING );
68 ExitProcess(0);
71 static void do_debugmsg( const char *arg )
73 MESSAGE("Error: the --debugmsg option is no longer supported. You should use\n");
74 MESSAGE("the WINEDEBUG environment variable instead, like this:\n\n");
75 MESSAGE(" WINEDEBUG=%s wine ...\n\n", arg );
76 ExitProcess(1);
79 static inline void remove_options( char *argv[], int pos, int count )
81 while ((argv[pos] = argv[pos+count])) pos++;
84 /* parse options from the argv array and remove all the recognized ones */
85 static void parse_options( char *argv[] )
87 const struct option_descr *opt;
88 int i;
90 for (i = 0; argv[i]; i++)
92 const char *equalarg = NULL;
93 char *p = argv[i];
94 if (*p++ != '-') continue; /* not an option */
95 if (*p && !p[1]) /* short name */
97 if (*p == '-') break; /* "--" option */
98 for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
100 else /* long name */
102 const char *equal = strchr (p, '=');
103 if (*p == '-') p++;
104 /* check for the long name */
105 for (opt = option_table; opt->longname; opt++) {
106 /* Plain --option */
107 if (!strcmp( p, opt->longname )) break;
109 /* --option=value */
110 if (opt->has_arg &&
111 equal &&
112 strlen (opt->longname) == equal - p &&
113 !strncmp (p, opt->longname, equal - p)) {
114 equalarg = equal + 1;
115 break;
119 if (!opt->longname) continue;
121 if (equalarg)
123 opt->func( equalarg );
124 remove_options( argv, i, 1 );
126 else if (opt->has_arg && argv[i+1])
128 opt->func( argv[i+1] );
129 remove_options( argv, i, 2 );
131 else
133 opt->func( "" );
134 remove_options( argv, i, 1 );
136 i--;
140 /***********************************************************************
141 * OPTIONS_Usage
143 void OPTIONS_Usage(void)
145 const struct option_descr *opt;
146 MESSAGE( "%s\n\n", PACKAGE_STRING );
147 MESSAGE( "Usage: wine [options] [--] program_name [arguments]\n" );
148 MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
149 MESSAGE( "Options:\n" );
150 for (opt = option_table; opt->longname; opt++) MESSAGE( " %s\n", opt->usage );
151 ExitProcess(0);
154 /***********************************************************************
155 * OPTIONS_ParseOptions
157 void OPTIONS_ParseOptions( char *argv[] )
159 int i;
161 parse_options( argv + 1 );
163 /* check if any option remains */
164 for (i = 1; argv[i]; i++)
166 if (!strcmp( argv[i], "--" ))
168 remove_options( argv, i, 1 );
169 break;
171 if (argv[i][0] == '-')
173 MESSAGE( "Unknown option '%s'\n\n", argv[i] );
174 OPTIONS_Usage();