4 * Copyright 2000 Alexandre Julliard
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.
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.
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
31 #include "wine/library.h"
34 #include "wine/debug.h"
42 void (*func
)( const char *arg
);
46 const char *argv0
; /* the original argv[0] */
47 const char *full_argv0
; /* the full path of argv[0] (if known) */
49 static char *inherit_str
; /* options to pass to child processes */
51 static void DECLSPEC_NORETURN
out_of_memory(void);
52 static void out_of_memory(void)
54 MESSAGE( "Virtual memory exhausted\n" );
58 static void do_debugmsg( const char *arg
);
59 static void do_help( const char *arg
);
60 static void do_version( const char *arg
);
62 static const struct option_descr option_table
[] =
64 { "debugmsg", 0, 1, 1, do_debugmsg
,
65 "--debugmsg name Turn debugging-messages on or off" },
66 { "dll", 0, 1, 1, MODULE_AddLoadOrderOption
,
67 "--dll name Enable or disable built-in DLLs" },
68 { "help", 'h', 0, 0, do_help
,
69 "--help,-h Show this help message" },
70 { "version", 'v', 0, 0, do_version
,
71 "--version,-v Display the Wine version" },
72 { NULL
, 0, 0, 0, NULL
, NULL
} /* terminator */
76 static void do_help( const char *arg
)
81 static void do_version( const char *arg
)
83 MESSAGE( "%s\n", PACKAGE_STRING
);
87 static void do_debugmsg( const char *arg
)
89 if (wine_dbg_parse_options( arg
))
91 MESSAGE("%s: Syntax: --debugmsg [class]+xxx,... or -debugmsg [class]-xxx,...\n", argv0
);
92 MESSAGE("Example: --debugmsg +all,warn-heap\n"
93 " turn on all messages except warning heap messages\n");
94 MESSAGE("Available message classes: err, warn, fixme, trace\n\n");
100 static void remove_options( char *argv
[], int pos
, int count
, int inherit
)
105 for (i
= 0; i
< count
; i
++) len
+= strlen(argv
[pos
+i
]) + 1;
108 if (!(inherit_str
= realloc( inherit_str
, strlen(inherit_str
) + 1 + len
)))
110 strcat( inherit_str
, " " );
114 if (!(inherit_str
= malloc( len
))) out_of_memory();
117 for (i
= 0; i
< count
; i
++)
119 strcat( inherit_str
, argv
[pos
+i
] );
120 if (i
< count
-1) strcat( inherit_str
, " " );
123 while ((argv
[pos
] = argv
[pos
+count
])) pos
++;
126 /* parse options from the argv array and remove all the recognized ones */
127 static void parse_options( char *argv
[] )
129 const struct option_descr
*opt
;
132 for (i
= 0; argv
[i
]; i
++)
134 const char *equalarg
= NULL
;
136 if (*p
++ != '-') continue; /* not an option */
137 if (*p
&& !p
[1]) /* short name */
139 if (*p
== '-') break; /* "--" option */
140 for (opt
= option_table
; opt
->longname
; opt
++) if (opt
->shortname
== *p
) break;
144 const char *equal
= strchr (p
, '=');
146 /* check for the long name */
147 for (opt
= option_table
; opt
->longname
; opt
++) {
149 if (!strcmp( p
, opt
->longname
)) break;
154 strlen (opt
->longname
) == equal
- p
&&
155 !strncmp (p
, opt
->longname
, equal
- p
)) {
156 equalarg
= equal
+ 1;
161 if (!opt
->longname
) continue;
165 opt
->func( equalarg
);
166 remove_options( argv
, i
, 1, opt
->inherit
);
168 else if (opt
->has_arg
&& argv
[i
+1])
170 opt
->func( argv
[i
+1] );
171 remove_options( argv
, i
, 2, opt
->inherit
);
176 remove_options( argv
, i
, 1, opt
->inherit
);
182 /* inherit options from WINEOPTIONS variable */
183 static void inherit_options( char *buffer
)
188 char *p
= strtok( buffer
, " \t" );
189 for (n
= 0; n
< sizeof(argv
)/sizeof(argv
[0])-1 && p
; n
++)
192 p
= strtok( NULL
, " \t" );
195 parse_options( argv
);
196 if (argv
[0]) /* an option remains */
198 MESSAGE( "Unknown option '%s' in WINEOPTIONS variable\n\n", argv
[0] );
203 /***********************************************************************
206 void OPTIONS_Usage(void)
208 const struct option_descr
*opt
;
209 MESSAGE( "%s\n\n", PACKAGE_STRING
);
210 MESSAGE( "Usage: %s [options] [--] program_name [arguments]\n", argv0
);
211 MESSAGE("The -- has to be used if you specify arguments (of the program)\n\n");
212 MESSAGE( "Options:\n" );
213 for (opt
= option_table
; opt
->longname
; opt
++) MESSAGE( " %s\n", opt
->usage
);
217 /***********************************************************************
218 * OPTIONS_ParseOptions
220 void OPTIONS_ParseOptions( char *argv
[] )
225 if (GetEnvironmentVariableA( "WINEOPTIONS", buffer
, sizeof(buffer
) ) && buffer
[0])
226 inherit_options( buffer
);
229 parse_options( argv
+ 1 );
231 SetEnvironmentVariableA( "WINEOPTIONS", inherit_str
);
233 /* check if any option remains */
234 for (i
= 1; argv
[i
]; i
++)
236 if (!strcmp( argv
[i
], "--" ))
238 remove_options( argv
, i
, 1, 0 );
241 if (argv
[i
][0] == '-')
243 MESSAGE( "Unknown option '%s'\n\n", argv
[i
] );