Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Option parsing |
| 3 | * |
| 4 | * Copyright 2000 Alexandre Julliard |
| 5 | */ |
| 6 | |
| 7 | #include "config.h" |
| 8 | #include <string.h> |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 9 | #include <stdlib.h> |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 10 | |
| 11 | #include "winbase.h" |
Alexandre Julliard | 909eff9 | 2000-12-15 03:38:11 +0000 | [diff] [blame] | 12 | #include "winnls.h" |
Guy L. Albertelli | 8d91b50 | 2001-01-04 19:37:37 +0000 | [diff] [blame] | 13 | #include "ntddk.h" |
Alexandre Julliard | 85423c6 | 2000-11-08 04:28:54 +0000 | [diff] [blame] | 14 | #include "wine/library.h" |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 15 | #include "options.h" |
Alexandre Julliard | f899ef0 | 2001-07-23 00:04:00 +0000 | [diff] [blame] | 16 | #include "module.h" |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 17 | #include "version.h" |
| 18 | #include "debugtools.h" |
| 19 | |
Alexandre Julliard | ca43a64 | 2001-01-10 23:56:59 +0000 | [diff] [blame] | 20 | struct option_descr |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 21 | { |
| 22 | const char *longname; |
| 23 | char shortname; |
| 24 | int has_arg; |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 25 | int inherit; |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 26 | void (*func)( const char *arg ); |
| 27 | const char *usage; |
| 28 | }; |
| 29 | |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 30 | /* default options */ |
| 31 | struct options Options = |
| 32 | { |
Alexandre Julliard | ca43a64 | 2001-01-10 23:56:59 +0000 | [diff] [blame] | 33 | FALSE /* Managed windows */ |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
Alexandre Julliard | a3e0cfc | 2000-07-16 18:21:34 +0000 | [diff] [blame] | 36 | const char *argv0; /* the original argv[0] */ |
| 37 | const char *full_argv0; /* the full path of argv[0] (if known) */ |
Alexandre Julliard | c192ba2 | 2000-05-29 21:25:10 +0000 | [diff] [blame] | 38 | |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 39 | static char *inherit_str; /* options to pass to child processes */ |
| 40 | |
| 41 | static void out_of_memory(void) WINE_NORETURN; |
| 42 | static void out_of_memory(void) |
| 43 | { |
| 44 | MESSAGE( "Virtual memory exhausted\n" ); |
| 45 | ExitProcess(1); |
| 46 | } |
| 47 | |
Alexandre Julliard | 94613ab | 2000-11-05 04:51:34 +0000 | [diff] [blame] | 48 | static void do_debugmsg( const char *arg ); |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 49 | static void do_help( const char *arg ); |
| 50 | static void do_managed( const char *arg ); |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 51 | static void do_version( const char *arg ); |
| 52 | |
Alexandre Julliard | ca43a64 | 2001-01-10 23:56:59 +0000 | [diff] [blame] | 53 | static const struct option_descr option_table[] = |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 54 | { |
Alexandre Julliard | 94613ab | 2000-11-05 04:51:34 +0000 | [diff] [blame] | 55 | { "debugmsg", 0, 1, 1, do_debugmsg, |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 56 | "--debugmsg name Turn debugging-messages on or off" }, |
Alexandre Julliard | b9c9cdc | 2001-03-20 02:11:08 +0000 | [diff] [blame] | 57 | { "dll", 0, 1, 1, MODULE_AddLoadOrderOption, |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 58 | "--dll name Enable or disable built-in DLLs" }, |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 59 | { "dosver", 0, 1, 1, VERSION_ParseDosVersion, |
Andreas Mohr | cb45438 | 2000-11-27 01:39:05 +0000 | [diff] [blame] | 60 | "--dosver x.xx DOS version to imitate (e.g. 6.22)\n" |
| 61 | " Only valid with --winver win31" }, |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 62 | { "help", 'h', 0, 0, do_help, |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 63 | "--help,-h Show this help message" }, |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 64 | { "managed", 0, 0, 0, do_managed, |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 65 | "--managed Allow the window manager to manage created windows" }, |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 66 | { "version", 'v', 0, 0, do_version, |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 67 | "--version,-v Display the Wine version" }, |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 68 | { "winver", 0, 1, 1, VERSION_ParseWinVersion, |
Francois Gouget | 4ae7195 | 2001-11-06 00:49:48 +0000 | [diff] [blame] | 69 | "--winver Version to imitate (win95,win98,winme,nt351,nt40,win2k,winxp,win20,win30,win31)" }, |
Patrik Stridvall | a9f6a9d | 2000-10-24 02:22:16 +0000 | [diff] [blame] | 70 | { NULL, 0, 0, 0, NULL, NULL } /* terminator */ |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | |
| 74 | static void do_help( const char *arg ) |
| 75 | { |
| 76 | OPTIONS_Usage(); |
| 77 | } |
| 78 | |
| 79 | static void do_version( const char *arg ) |
| 80 | { |
| 81 | MESSAGE( "%s\n", WINE_RELEASE_INFO ); |
| 82 | ExitProcess(0); |
| 83 | } |
| 84 | |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 85 | static void do_managed( const char *arg ) |
| 86 | { |
| 87 | Options.managed = TRUE; |
| 88 | } |
| 89 | |
Alexandre Julliard | 94613ab | 2000-11-05 04:51:34 +0000 | [diff] [blame] | 90 | static void do_debugmsg( const char *arg ) |
| 91 | { |
Alexandre Julliard | 94613ab | 2000-11-05 04:51:34 +0000 | [diff] [blame] | 92 | static const char * const debug_class_names[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" }; |
| 93 | |
| 94 | char *opt, *options = strdup(arg); |
| 95 | int i; |
Guy L. Albertelli | 8d91b50 | 2001-01-04 19:37:37 +0000 | [diff] [blame] | 96 | /* defined in relay32/relay386.c */ |
| 97 | extern char **debug_relay_includelist; |
| 98 | extern char **debug_relay_excludelist; |
| 99 | /* defined in relay32/snoop.c */ |
| 100 | extern char **debug_snoop_includelist; |
| 101 | extern char **debug_snoop_excludelist; |
Alexandre Julliard | 94613ab | 2000-11-05 04:51:34 +0000 | [diff] [blame] | 102 | |
| 103 | if (!(opt = strtok( options, "," ))) goto error; |
| 104 | do |
| 105 | { |
| 106 | unsigned char set = 0, clear = 0; |
| 107 | char *p = strchr( opt, '+' ); |
| 108 | if (!p) p = strchr( opt, '-' ); |
| 109 | if (!p || !p[1]) goto error; |
| 110 | if (p > opt) |
| 111 | { |
| 112 | for (i = 0; i < __DBCL_COUNT; i++) |
| 113 | { |
| 114 | int len = strlen(debug_class_names[i]); |
| 115 | if (len != (p - opt)) continue; |
| 116 | if (!memcmp( opt, debug_class_names[i], len )) /* found it */ |
| 117 | { |
| 118 | if (*p == '+') set |= 1 << i; |
| 119 | else clear |= 1 << i; |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | if (i == __DBCL_COUNT) goto error; /* class name not found */ |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | if (*p == '+') set = ~0; |
| 128 | else clear = ~0; |
Guy L. Albertelli | 8d91b50 | 2001-01-04 19:37:37 +0000 | [diff] [blame] | 129 | if (!strncasecmp(p+1, "relay=", 6) || |
| 130 | !strncasecmp(p+1, "snoop=", 6)) |
| 131 | { |
| 132 | int i, l; |
| 133 | char *s, *s2, ***output, c; |
| 134 | |
| 135 | if (strchr(p,',')) |
| 136 | l=strchr(p,',')-p; |
| 137 | else |
| 138 | l=strlen(p); |
| 139 | set = ~0; |
| 140 | clear = 0; |
| 141 | output = (*p == '+') ? |
| 142 | ((*(p+1) == 'r') ? |
| 143 | &debug_relay_includelist : |
| 144 | &debug_snoop_includelist) : |
| 145 | ((*(p+1) == 'r') ? |
| 146 | &debug_relay_excludelist : |
| 147 | &debug_snoop_excludelist); |
| 148 | s = p + 7; |
| 149 | /* if there are n ':', there are n+1 modules, and we need |
| 150 | n+2 slots, last one being for the sentinel (NULL) */ |
| 151 | i = 2; |
| 152 | while((s = strchr(s, ':'))) i++, s++; |
| 153 | *output = malloc(sizeof(char **) * i); |
| 154 | i = 0; |
| 155 | s = p + 7; |
| 156 | while((s2 = strchr(s, ':'))) { |
| 157 | c = *s2; |
| 158 | *s2 = '\0'; |
| 159 | *((*output)+i) = _strupr(strdup(s)); |
| 160 | *s2 = c; |
| 161 | s = s2 + 1; |
| 162 | i++; |
| 163 | } |
| 164 | c = *(p + l); |
| 165 | *(p + l) = '\0'; |
| 166 | *((*output)+i) = _strupr(strdup(s)); |
| 167 | *(p + l) = c; |
| 168 | *((*output)+i+1) = NULL; |
| 169 | *(p + 6) = '\0'; |
| 170 | } |
Alexandre Julliard | 94613ab | 2000-11-05 04:51:34 +0000 | [diff] [blame] | 171 | } |
| 172 | p++; |
| 173 | if (!strcmp( p, "all" )) p = ""; /* empty string means all */ |
| 174 | wine_dbg_add_option( p, set, clear ); |
| 175 | opt = strtok( NULL, "," ); |
| 176 | } while(opt); |
| 177 | |
| 178 | free( options ); |
| 179 | return; |
| 180 | |
| 181 | error: |
| 182 | MESSAGE("wine: Syntax: --debugmsg [class]+xxx,... or " |
| 183 | "-debugmsg [class]-xxx,...\n"); |
| 184 | MESSAGE("Example: --debugmsg +all,warn-heap\n" |
| 185 | " turn on all messages except warning heap messages\n"); |
| 186 | MESSAGE("Available message classes:\n"); |
| 187 | for( i = 0; i < __DBCL_COUNT; i++) MESSAGE( "%-9s", debug_class_names[i] ); |
| 188 | MESSAGE("\n\n"); |
| 189 | ExitProcess(1); |
| 190 | } |
| 191 | |
| 192 | |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 193 | static void remove_options( char *argv[], int pos, int count, int inherit ) |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 194 | { |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 195 | if (inherit) |
| 196 | { |
| 197 | int i, len = 0; |
| 198 | for (i = 0; i < count; i++) len += strlen(argv[pos+i]) + 1; |
| 199 | if (inherit_str) |
| 200 | { |
Alexandre Julliard | 45fccb8 | 2000-06-07 02:03:54 +0000 | [diff] [blame] | 201 | if (!(inherit_str = realloc( inherit_str, strlen(inherit_str) + 1 + len ))) |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 202 | out_of_memory(); |
| 203 | strcat( inherit_str, " " ); |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | if (!(inherit_str = malloc( len ))) out_of_memory(); |
| 208 | inherit_str[0] = 0; |
| 209 | } |
| 210 | for (i = 0; i < count; i++) |
| 211 | { |
| 212 | strcat( inherit_str, argv[pos+i] ); |
| 213 | if (i < count-1) strcat( inherit_str, " " ); |
| 214 | } |
| 215 | } |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 216 | while ((argv[pos] = argv[pos+count])) pos++; |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 219 | /* parse options from the argv array and remove all the recognized ones */ |
| 220 | static void parse_options( char *argv[] ) |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 221 | { |
Alexandre Julliard | ca43a64 | 2001-01-10 23:56:59 +0000 | [diff] [blame] | 222 | const struct option_descr *opt; |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 223 | int i; |
| 224 | |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 225 | for (i = 0; argv[i]; i++) |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 226 | { |
Morten Welinder | 87093f5 | 2000-12-18 03:49:49 +0000 | [diff] [blame] | 227 | const char *equalarg = NULL; |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 228 | char *p = argv[i]; |
| 229 | if (*p++ != '-') continue; /* not an option */ |
| 230 | if (*p && !p[1]) /* short name */ |
| 231 | { |
| 232 | if (*p == '-') break; /* "--" option */ |
| 233 | for (opt = option_table; opt->longname; opt++) if (opt->shortname == *p) break; |
| 234 | } |
| 235 | else /* long name */ |
| 236 | { |
Morten Welinder | 87093f5 | 2000-12-18 03:49:49 +0000 | [diff] [blame] | 237 | const char *equal = strchr (p, '='); |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 238 | if (*p == '-') p++; |
| 239 | /* check for the long name */ |
Morten Welinder | 87093f5 | 2000-12-18 03:49:49 +0000 | [diff] [blame] | 240 | for (opt = option_table; opt->longname; opt++) { |
| 241 | /* Plain --option */ |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 242 | if (!strcmp( p, opt->longname )) break; |
Morten Welinder | 87093f5 | 2000-12-18 03:49:49 +0000 | [diff] [blame] | 243 | |
| 244 | /* --option=value */ |
| 245 | if (opt->has_arg && |
| 246 | equal && |
| 247 | strlen (opt->longname) == equal - p && |
| 248 | !strncmp (p, opt->longname, equal - p)) { |
| 249 | equalarg = equal + 1; |
| 250 | break; |
| 251 | } |
| 252 | } |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 253 | } |
| 254 | if (!opt->longname) continue; |
| 255 | |
Morten Welinder | 87093f5 | 2000-12-18 03:49:49 +0000 | [diff] [blame] | 256 | if (equalarg) |
| 257 | { |
| 258 | opt->func( equalarg ); |
| 259 | remove_options( argv, i, 1, opt->inherit ); |
| 260 | } |
| 261 | else if (opt->has_arg && argv[i+1]) |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 262 | { |
| 263 | opt->func( argv[i+1] ); |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 264 | remove_options( argv, i, 2, opt->inherit ); |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 265 | } |
| 266 | else |
| 267 | { |
| 268 | opt->func( "" ); |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 269 | remove_options( argv, i, 1, opt->inherit ); |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 270 | } |
| 271 | i--; |
| 272 | } |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* inherit options from WINEOPTIONS variable */ |
| 276 | static void inherit_options( char *buffer ) |
| 277 | { |
| 278 | char *argv[256]; |
Alexandre Julliard | 908464d | 2000-11-01 03:11:12 +0000 | [diff] [blame] | 279 | unsigned int n; |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 280 | |
| 281 | char *p = strtok( buffer, " \t" ); |
Alexandre Julliard | 908464d | 2000-11-01 03:11:12 +0000 | [diff] [blame] | 282 | for (n = 0; n < sizeof(argv)/sizeof(argv[0])-1 && p; n++) |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 283 | { |
Alexandre Julliard | 908464d | 2000-11-01 03:11:12 +0000 | [diff] [blame] | 284 | argv[n] = p; |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 285 | p = strtok( NULL, " \t" ); |
| 286 | } |
Alexandre Julliard | 908464d | 2000-11-01 03:11:12 +0000 | [diff] [blame] | 287 | argv[n] = NULL; |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 288 | parse_options( argv ); |
| 289 | if (argv[0]) /* an option remains */ |
| 290 | { |
| 291 | MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv[0] ); |
| 292 | OPTIONS_Usage(); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /*********************************************************************** |
| 297 | * OPTIONS_Usage |
| 298 | */ |
| 299 | void OPTIONS_Usage(void) |
| 300 | { |
Alexandre Julliard | ca43a64 | 2001-01-10 23:56:59 +0000 | [diff] [blame] | 301 | const struct option_descr *opt; |
Alexandre Julliard | 26320d1 | 2001-03-23 23:45:45 +0000 | [diff] [blame] | 302 | MESSAGE( "%s\n\n", WINE_RELEASE_INFO ); |
Andreas Mohr | 1f192c1 | 2000-12-15 21:28:47 +0000 | [diff] [blame] | 303 | MESSAGE( "Usage: %s [options] [--] program_name [arguments]\n", argv0 ); |
| 304 | MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n"); |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 305 | MESSAGE( "Options:\n" ); |
| 306 | for (opt = option_table; opt->longname; opt++) MESSAGE( " %s\n", opt->usage ); |
| 307 | ExitProcess(0); |
| 308 | } |
| 309 | |
| 310 | /*********************************************************************** |
| 311 | * OPTIONS_ParseOptions |
| 312 | */ |
| 313 | void OPTIONS_ParseOptions( char *argv[] ) |
| 314 | { |
| 315 | char buffer[1024]; |
| 316 | int i; |
| 317 | |
| 318 | if (GetEnvironmentVariableA( "WINEOPTIONS", buffer, sizeof(buffer) ) && buffer[0]) |
| 319 | inherit_options( buffer ); |
| 320 | |
| 321 | parse_options( argv + 1 ); |
| 322 | |
| 323 | SetEnvironmentVariableA( "WINEOPTIONS", inherit_str ); |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 324 | |
| 325 | /* check if any option remains */ |
| 326 | for (i = 1; argv[i]; i++) |
| 327 | { |
| 328 | if (!strcmp( argv[i], "--" )) |
| 329 | { |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 330 | remove_options( argv, i, 1, 0 ); |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 331 | break; |
| 332 | } |
| 333 | if (argv[i][0] == '-') |
| 334 | { |
Alexandre Julliard | eeaae3c | 2000-05-30 17:51:44 +0000 | [diff] [blame] | 335 | MESSAGE( "Unknown option '%s'\n\n", argv[i] ); |
Alexandre Julliard | fe08568 | 2000-03-18 21:56:10 +0000 | [diff] [blame] | 336 | OPTIONS_Usage(); |
| 337 | } |
| 338 | } |
Alexandre Julliard | 909eff9 | 2000-12-15 03:38:11 +0000 | [diff] [blame] | 339 | } |