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