- fix DirectSound Creation by dmusic (fix recent Unreal2 regression)
[wine] / misc / options.c
1 /*
2  * Option parsing
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include <stdarg.h>
23 #include <string.h>
24 #include <stdlib.h>
25
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"
34
35 struct option_descr
36 {
37     const char *longname;
38     char        shortname;
39     int         has_arg;
40     void      (*func)( const char *arg );
41     const char *usage;
42 };
43
44 static void do_debugmsg( const char *arg );
45 static void do_help( const char *arg );
46 static void do_version( const char *arg );
47
48 static const struct option_descr option_table[] =
49 {
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 */
57 };
58
59
60 static void do_help( const char *arg )
61 {
62     OPTIONS_Usage();
63 }
64
65 static void do_version( const char *arg )
66 {
67     MESSAGE( "%s\n", PACKAGE_STRING );
68     ExitProcess(0);
69 }
70
71 static void do_debugmsg( const char *arg )
72 {
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);
77 }
78
79 static inline void remove_options( char *argv[], int pos, int count )
80 {
81     while ((argv[pos] = argv[pos+count])) pos++;
82 }
83
84 /* parse options from the argv array and remove all the recognized ones */
85 static void parse_options( char *argv[] )
86 {
87     const struct option_descr *opt;
88     int i;
89
90     for (i = 0; argv[i]; i++)
91     {
92         const char *equalarg = NULL;
93         char *p = argv[i];
94         if (*p++ != '-') continue;  /* not an option */
95         if (*p && !p[1]) /* short name */
96         {
97             if (*p == '-') break; /* "--" option */
98             for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
99         }
100         else  /* long name */
101         {
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;
108
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;
116                     }
117             }
118         }
119         if (!opt->longname) continue;
120
121         if (equalarg)
122         {
123             opt->func( equalarg );
124             remove_options( argv, i, 1 );
125         }
126         else if (opt->has_arg && argv[i+1])
127         {
128             opt->func( argv[i+1] );
129             remove_options( argv, i, 2 );
130         }
131         else
132         {
133             opt->func( "" );
134             remove_options( argv, i, 1 );
135         }
136         i--;
137     }
138 }
139
140 /***********************************************************************
141  *              OPTIONS_Usage
142  */
143 void OPTIONS_Usage(void)
144 {
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);
152 }
153
154 /***********************************************************************
155  *              OPTIONS_ParseOptions
156  */
157 void OPTIONS_ParseOptions( char *argv[] )
158 {
159     int i;
160
161     parse_options( argv + 1 );
162
163     /* check if any option remains */
164     for (i = 1; argv[i]; i++)
165     {
166         if (!strcmp( argv[i], "--" ))
167         {
168             remove_options( argv, i, 1 );
169             break;
170         }
171         if (argv[i][0] == '-')
172         {
173             MESSAGE( "Unknown option '%s'\n\n", argv[i] );
174             OPTIONS_Usage();
175         }
176     }
177 }