Improved Winelib apps initialisation code. No longer need to link
[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 /* Most Windows C/C++ compilers use something like this to */
26 /* access argc and argv globally: */
27 int _ARGC;
28 char **_ARGV;
29
30 static void do_config( const char *arg );
31 static void do_desktop( const char *arg );
32 static void do_display( const char *arg );
33 static void do_dll( const char *arg );
34 static void do_help( const char *arg );
35 static void do_managed( const char *arg );
36 static void do_synchronous( const char *arg );
37 static void do_version( const char *arg );
38
39 static const struct option option_table[] =
40 {
41     { "config",       0, 1, do_config,
42       "--config name    Specify config file to use" },
43     { "debugmsg",     0, 1, MAIN_ParseDebugOptions,
44       "--debugmsg name  Turn debugging-messages on or off" },
45     { "desktop",      0, 1, do_desktop,
46       "--desktop geom   Use a desktop window of the given geometry" },
47     { "display",      0, 1, do_display,
48       "--display name   Use the specified display" },
49     { "dll",          0, 1, do_dll,
50       "--dll name       Enable or disable built-in DLLs" },
51     { "dosver",       0, 1, VERSION_ParseDosVersion,
52       "--dosver x.xx    DOS version to imitate (e.g. 6.22). Only valid with --winver win31" },
53     { "help",       'h', 0, do_help,
54       "--help,-h        Show this help message" },
55     { "language",     0, 1, MAIN_ParseLanguageOption,
56       "--language xx    Set the language (one of Br,Ca,Cs,Cy,Da,De,En,Eo,Es,Fi,Fr,Ga,Gd,Gv\n"
57       "                    Hu,It,Ja,Ko,Kw,Nl,No,Pl,Pt,Sk,Sv,Ru,Wa)" },
58     { "managed",      0, 0, do_managed,
59       "--managed        Allow the window manager to manage created windows" },
60     { "synchronous",  0, 0, do_synchronous,
61       "--synchronous    Turn on synchronous display mode" },
62     { "version",    'v', 0, do_version,
63       "--version,-v     Display the Wine version" },
64     { "winver",       0, 1, VERSION_ParseWinVersion,
65       "--winver         Version to imitate (one of win31,win95,nt351,nt40)" },
66     { NULL, }  /* terminator */
67 };
68
69
70 static void do_help( const char *arg )
71 {
72     OPTIONS_Usage();
73 }
74
75 static void do_version( const char *arg )
76 {
77     MESSAGE( "%s\n", WINE_RELEASE_INFO );
78     ExitProcess(0);
79 }
80
81 static void do_synchronous( const char *arg )
82 {
83     Options.synchronous = TRUE;
84 }
85
86 static void do_desktop( const char *arg )
87 {
88     Options.desktopGeometry = strdup( arg );
89 }
90
91 static void do_display( const char *arg )
92 {
93     Options.display = strdup( arg );
94 }
95
96 static void do_dll( const char *arg )
97 {
98     if (Options.dllFlags)
99     {
100         /* don't overwrite previous value. Should we
101          * automatically add the ',' between multiple DLLs ?
102          */
103         MESSAGE("Only one -dll flag is allowed. Use ',' between multiple DLLs\n");
104         ExitProcess(1);
105     }
106     Options.dllFlags = strdup( arg );
107 }
108
109 static void do_managed( const char *arg )
110 {
111     Options.managed = TRUE;
112 }
113
114 static void do_config( const char *arg )
115 {
116     Options.configFileName = strdup( arg );
117 }
118
119 static inline void remove_options( int *argc, char *argv[], int pos, int count )
120 {
121     while ((argv[pos] = argv[pos+count])) pos++;
122     *argc -= count;
123 }
124
125 /***********************************************************************
126  *              OPTIONS_Usage
127  */
128 void OPTIONS_Usage(void)
129 {
130     const struct option *opt;
131     MESSAGE( "Usage: %s [options] \"program_name [arguments]\"\n\n", argv0 );
132     MESSAGE( "Options:\n" );
133     for (opt = option_table; opt->longname; opt++) MESSAGE( "   %s\n", opt->usage );
134     ExitProcess(0);
135 }
136
137 /***********************************************************************
138  *              OPTIONS_ParseOptions
139  */
140 void OPTIONS_ParseOptions( int argc, char *argv[] )
141 {
142     const struct option *opt;
143     int i;
144
145     for (i = 1; argv[i]; i++)
146     {
147         char *p = argv[i];
148         if (*p++ != '-') continue;  /* not an option */
149         if (*p && !p[1]) /* short name */
150         {
151             if (*p == '-') break; /* "--" option */
152             for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break;
153         }
154         else  /* long name */
155         {
156             if (*p == '-') p++;
157             /* check for the long name */
158             for (opt = option_table; opt->longname; opt++)
159                 if (!strcmp( p, opt->longname )) break;
160         }
161         if (!opt->longname) continue;
162
163         if (opt->has_arg && argv[i+1])
164         {
165             opt->func( argv[i+1] );
166             remove_options( &argc, argv, i, 2 );
167         }
168         else
169         {
170             opt->func( "" );
171             remove_options( &argc, argv, i, 1 );
172         }
173         i--;
174     }
175
176     /* check if any option remains */
177     for (i = 1; argv[i]; i++)
178     {
179         if (!strcmp( argv[i], "--" ))
180         {
181             remove_options( &argc, argv, i, 1 );
182             break;
183         }
184         if (argv[i][0] == '-')
185         {
186             MESSAGE( "Unknown option '%s'\n", argv[i] );
187             OPTIONS_Usage();
188         }
189     }
190     Options.argc = argc;
191     Options.argv = argv;
192     _ARGC = argc;
193     _ARGV = argv;
194 }