Avoid calling the *Rect USER functions from inside GDI.
[wine] / misc / options.c
1 /*
2  * Option parsing
3  *
4  * Copyright 2000 Alexandre Julliard
5  */
6
7 #include "config.h"
8 #include <string.h>
9
10 #include "winbase.h"
11 #include "main.h"
12 #include "options.h"
13 #include "version.h"
14 #include "debugtools.h"
15
16 struct option
17 {
18     const char *longname;
19     char        shortname;
20     int         has_arg;
21     void      (*func)( const char *arg );
22     const char *usage;
23 };
24
25 static void do_config( const char *arg );
26 static void do_debug( const char *arg );
27 static void do_desktop( const char *arg );
28 static void do_display( const char *arg );
29 static void do_dll( const char *arg );
30 static void do_failreadonly( const char *arg );
31 static void do_help( const char *arg );
32 static void do_managed( const char *arg );
33 static void do_synchronous( const char *arg );
34 static void do_version( const char *arg );
35
36 static const struct option option_table[] =
37 {
38     { "config",       0, 1, do_config,
39       "--config name    Specify config file to use" },
40     { "debug",        0, 0, do_debug,
41       "--debug          Enter debugger before starting application" },
42     { "debugmsg",     0, 1, MAIN_ParseDebugOptions,
43       "--debugmsg name  Turn debugging-messages on or off" },
44     { "desktop",      0, 1, do_desktop,
45       "--desktop geom   Use a desktop window of the given geometry" },
46     { "display",      0, 1, do_display,
47       "--display name   Use the specified display" },
48     { "dll",          0, 1, do_dll,
49       "--dll name       Enable or disable built-in DLLs" },
50     { "dosver",       0, 1, VERSION_ParseDosVersion,
51       "--dosver x.xx    DOS version to imitate (e.g. 6.22). Only valid with --winver win31" },
52     { "failreadonly", 0, 0, do_failreadonly,
53       "--failreadonly   Read only files may not be opened in write mode" },
54     { "help",       'h', 0, do_help,
55       "--help,-h        Show this help message" },
56     { "language",     0, 1, MAIN_ParseLanguageOption,
57       "--language xx    Set the language (one of Br,Ca,Cs,Cy,Da,De,En,Eo,Es,Fi,Fr,Ga,Gd,Gv\n"
58       "                    Hu,It,Ja,Ko,Kw,Nl,No,Pl,Pt,Sk,Sv,Ru,Wa)" },
59     { "managed",      0, 0, do_managed,
60       "--managed        Allow the window manager to manage created windows" },
61     { "synchronous",  0, 0, do_synchronous,
62       "--synchronous    Turn on synchronous display mode" },
63     { "version",    'v', 0, do_version,
64       "--version,-v     Display the Wine version" },
65     { "winver",       0, 1, VERSION_ParseWinVersion,
66       "--winver         Version to imitate (one of win31,win95,nt351,nt40)" },
67     { NULL, }  /* terminator */
68 };
69
70
71 static void do_help( const char *arg )
72 {
73     OPTIONS_Usage();
74 }
75
76 static void do_version( const char *arg )
77 {
78     MESSAGE( "%s\n", WINE_RELEASE_INFO );
79     ExitProcess(0);
80 }
81
82 static void do_synchronous( const char *arg )
83 {
84     Options.synchronous = TRUE;
85 }
86
87 static void do_debug( const char *arg )
88 {
89     Options.debug = TRUE;
90 }
91
92 static void do_failreadonly( const char *arg )
93 {
94     Options.failReadOnly = TRUE;
95 }
96
97 static void do_desktop( const char *arg )
98 {
99     Options.desktopGeometry = strdup( arg );
100 }
101
102 static void do_display( const char *arg )
103 {
104     Options.display = strdup( arg );
105 }
106
107 static void do_dll( const char *arg )
108 {
109     if (Options.dllFlags)
110     {
111         /* don't overwrite previous value. Should we
112          * automatically add the ',' between multiple DLLs ?
113          */
114         MESSAGE("Only one -dll flag is allowed. Use ',' between multiple DLLs\n");
115         ExitProcess(1);
116     }
117     Options.dllFlags = strdup( arg );
118 }
119
120 static void do_managed( const char *arg )
121 {
122     Options.managed = TRUE;
123 }
124
125 static void do_config( const char *arg )
126 {
127     Options.configFileName = strdup( arg );
128 }
129
130 static inline void remove_options( int *argc, char *argv[], int pos, int count )
131 {
132     while ((argv[pos] = argv[pos+count])) pos++;
133     *argc -= count;
134 }
135
136 /***********************************************************************
137  *              OPTIONS_Usage
138  */
139 void OPTIONS_Usage(void)
140 {
141     const struct option *opt;
142     MESSAGE( "Usage: %s [options] \"program_name [arguments]\"\n\n", argv0 );
143     MESSAGE( "Options:\n" );
144     for (opt = option_table; opt->longname; opt++) MESSAGE( "   %s\n", opt->usage );
145     ExitProcess(0);
146 }
147
148 /***********************************************************************
149  *              OPTIONS_ParseOptions
150  */
151 void OPTIONS_ParseOptions( int argc, char *argv[] )
152 {
153     const struct option *opt;
154     int i;
155
156     for (i = 1; argv[i]; i++)
157     {
158         char *p = argv[i];
159         if (*p++ != '-') continue;  /* not an option */
160         if (*p && !p[1]) /* short name */
161         {
162             if (*p == '-') break; /* "--" option */
163             for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
164         }
165         else  /* long name */
166         {
167             if (*p == '-') p++;
168             /* check for the long name */
169             for (opt = option_table; opt->longname; opt++)
170                 if (!strcmp( p, opt->longname )) break;
171         }
172         if (!opt->longname) continue;
173
174         if (opt->has_arg && argv[i+1])
175         {
176             opt->func( argv[i+1] );
177             remove_options( &argc, argv, i, 2 );
178         }
179         else
180         {
181             opt->func( "" );
182             remove_options( &argc, argv, i, 1 );
183         }
184         i--;
185     }
186
187     /* check if any option remains */
188     for (i = 1; argv[i]; i++)
189     {
190         if (!strcmp( argv[i], "--" ))
191         {
192             remove_options( &argc, argv, i, 1 );
193             break;
194         }
195         if (argv[i][0] == '-')
196         {
197             MESSAGE( "Unknown option '%s'\n", argv[i] );
198             OPTIONS_Usage();
199         }
200     }
201     Options.argc = argc;
202     Options.argv = argv;
203 }