mirror of https://github.com/OpenTTD/OpenTTD
(svn r22098) -Codechange: Have an array of option data, use it in the option parsing routine. Adapt openttd option processing too.
parent
b0b1c0dea9
commit
8c2fc45153
|
@ -16,56 +16,75 @@
|
||||||
* Find the next option.
|
* Find the next option.
|
||||||
* @return Function returns one
|
* @return Function returns one
|
||||||
* - An option letter if it found another option.
|
* - An option letter if it found another option.
|
||||||
* - -1 if option processing is finished.
|
* - -1 if option processing is finished. Inspect #argv and #numleft to find the command line arguments.
|
||||||
* - -2 if an error was encountered.
|
* - -2 if an error was encountered.
|
||||||
*/
|
*/
|
||||||
int GetOptData::GetOpt()
|
int GetOptData::GetOpt()
|
||||||
{
|
{
|
||||||
|
const OptionData *odata;
|
||||||
|
|
||||||
char *s = this->cont;
|
char *s = this->cont;
|
||||||
if (s != NULL) {
|
if (s == NULL) {
|
||||||
goto md_continue_here;
|
if (this->numleft == 0) return -1; // No arguments left -> finished.
|
||||||
|
|
||||||
|
s = this->argv[0];
|
||||||
|
if (*s != '-') return -1; // No leading '-' -> not an option -> finished.
|
||||||
|
|
||||||
|
this->argv++;
|
||||||
|
this->numleft--;
|
||||||
|
|
||||||
|
/* Is it a long option? */
|
||||||
|
for (odata = this->options; odata->flags != ODF_END; odata++) {
|
||||||
|
if (odata->longname != NULL && !strcmp(odata->longname, s)) { // Long options always use the entire argument.
|
||||||
|
this->cont = NULL;
|
||||||
|
goto set_optval;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s++; // Skip leading '-'.
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
/* Is it a short option? */
|
||||||
if (--this->numleft < 0) return -1;
|
for (odata = this->options; odata->flags != ODF_END; odata++) {
|
||||||
|
if (odata->shortname != '\0' && *s == odata->shortname) {
|
||||||
|
this->cont = (s[1] != '\0') ? s + 1 : NULL;
|
||||||
|
|
||||||
s = *this->argv++;
|
set_optval: // Handle option value of *odata .
|
||||||
if (*s == '-') {
|
this->opt = NULL;
|
||||||
md_continue_here:;
|
switch (odata->flags) {
|
||||||
s++;
|
case ODF_NO_VALUE:
|
||||||
if (*s != 0) {
|
return odata->id;
|
||||||
const char *r;
|
|
||||||
/* Found argument, try to locate it in options. */
|
case ODF_HAS_VALUE:
|
||||||
if (*s == ':' || (r = strchr(this->options, *s)) == NULL) {
|
if (this->cont != NULL) { // Remainder of the argument is the option value.
|
||||||
/* ERROR! */
|
this->opt = this->cont;
|
||||||
return -2;
|
this->cont = NULL;
|
||||||
}
|
return odata->id;
|
||||||
if (r[1] == ':') {
|
|
||||||
char *t;
|
|
||||||
/* Item wants an argument. Check if the argument follows, or if it comes as a separate arg. */
|
|
||||||
if (!*(t = s + 1)) {
|
|
||||||
/* It comes as a separate arg. Check if out of args? */
|
|
||||||
if (--this->numleft < 0 || *(t = *this->argv) == '-') {
|
|
||||||
/* Check if item is optional? */
|
|
||||||
if (r[2] != ':') return -2;
|
|
||||||
this->numleft++;
|
|
||||||
t = NULL;
|
|
||||||
} else {
|
|
||||||
this->argv++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
this->opt = t;
|
if (this->numleft == 0) return -2; // Missing the option value.
|
||||||
this->cont = NULL;
|
this->opt = this->argv[0];
|
||||||
return *s;
|
this->argv++;
|
||||||
}
|
this->numleft--;
|
||||||
this->opt = NULL;
|
return odata->id;
|
||||||
this->cont = s;
|
|
||||||
return *s;
|
case ODF_OPTIONAL_VALUE:
|
||||||
|
if (this->cont != NULL) { // Remainder of the argument is the option value.
|
||||||
|
this->opt = this->cont;
|
||||||
|
this->cont = NULL;
|
||||||
|
return odata->id;
|
||||||
|
}
|
||||||
|
if (this->numleft > 0 && this->argv[0][0] != '-') {
|
||||||
|
this->opt = this->argv[0];
|
||||||
|
this->argv++;
|
||||||
|
this->numleft--;
|
||||||
|
}
|
||||||
|
return odata->id;
|
||||||
|
|
||||||
|
default: NOT_REACHED();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
/* This is currently not supported. */
|
|
||||||
return -2;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return -2; // No other ways to interpret the text -> error.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,13 +12,29 @@
|
||||||
#ifndef GETOPTDATA_H
|
#ifndef GETOPTDATA_H
|
||||||
#define GETOPTDATA_H
|
#define GETOPTDATA_H
|
||||||
|
|
||||||
|
/** Flags of an option. */
|
||||||
|
enum OptionDataFlags {
|
||||||
|
ODF_NO_VALUE, ///< A plain option (no value attached to it).
|
||||||
|
ODF_HAS_VALUE, ///< An option with a value.
|
||||||
|
ODF_OPTIONAL_VALUE, ///< An option with an optional value.
|
||||||
|
ODF_END, ///< Terminator (data is not parsed further).
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Data of an option. */
|
||||||
|
struct OptionData {
|
||||||
|
byte id; ///< Unique identification of this option data, often the same as #shortname.
|
||||||
|
char shortname; ///< Short option letter if available, else use \c '\0'.
|
||||||
|
uint16 flags; ///< Option data flags. @see OptionDataFlags
|
||||||
|
const char *longname; ///< Long option name including '-'/'--' prefix, use \c NULL if not available.
|
||||||
|
};
|
||||||
|
|
||||||
/** Data storage for parsing command line options. */
|
/** Data storage for parsing command line options. */
|
||||||
struct GetOptData {
|
struct GetOptData {
|
||||||
char *opt; ///< Option value, if available (else \c NULL).
|
char *opt; ///< Option value, if available (else \c NULL).
|
||||||
int numleft; ///< Number of arguments left in #argv.
|
int numleft; ///< Number of arguments left in #argv.
|
||||||
char **argv; ///< Remaining command line arguments.
|
char **argv; ///< Remaining command line arguments.
|
||||||
const char *options; ///< Command line option descriptions.
|
const OptionData *options; ///< Command line option descriptions.
|
||||||
char *cont; ///< Next call to #MyGetOpt should start here (in the middle of an argument).
|
char *cont; ///< Next call to #MyGetOpt should start here (in the middle of an argument).
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor of the data store.
|
* Constructor of the data store.
|
||||||
|
@ -26,7 +42,7 @@ struct GetOptData {
|
||||||
* @param argv Command line arguments, excluding the program name.
|
* @param argv Command line arguments, excluding the program name.
|
||||||
* @param options Command line option descriptions.
|
* @param options Command line option descriptions.
|
||||||
*/
|
*/
|
||||||
GetOptData(int argc, char **argv, const char *options) :
|
GetOptData(int argc, char **argv, const OptionData *options) :
|
||||||
opt(NULL),
|
opt(NULL),
|
||||||
numleft(argc),
|
numleft(argc),
|
||||||
argv(argv),
|
argv(argv),
|
||||||
|
@ -38,4 +54,59 @@ struct GetOptData {
|
||||||
int GetOpt();
|
int GetOpt();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* General macro for creating an option.
|
||||||
|
* @param id Identification of the option.
|
||||||
|
* @param shortname Short option name. Use \c '\0' if not used.
|
||||||
|
* @param longname Long option name including leading '-' or '--'. Use \c NULL if not used.
|
||||||
|
* @param flags Flags of the option.
|
||||||
|
*/
|
||||||
|
#define GETOPT_GENERAL(id, shortname, longname, flags) { id, shortname, flags, longname }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Short option without value.
|
||||||
|
* @param shortname Short option name. Use \c '\0' if not used.
|
||||||
|
* @param longname Long option name including leading '-' or '--'. Use \c NULL if not used.
|
||||||
|
*/
|
||||||
|
#define GETOPT_NOVAL(shortname, longname) GETOPT_GENERAL(shortname, shortname, longname, ODF_NO_VALUE)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Short option with value.
|
||||||
|
* @param shortname Short option name. Use \c '\0' if not used.
|
||||||
|
* @param longname Long option name including leading '-' or '--'. Use \c NULL if not used.
|
||||||
|
*/
|
||||||
|
#define GETOPT_VALUE(shortname, longname) GETOPT_GENERAL(shortname, shortname, longname, ODF_HAS_VALUE)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Short option with optional value.
|
||||||
|
* @param shortname Short option name. Use \c '\0' if not used.
|
||||||
|
* @param longname Long option name including leading '-' or '--'. Use \c NULL if not used.
|
||||||
|
* @note Options with optional values are hopelessly ambiguous, eg "-opt -value", avoid them.
|
||||||
|
*/
|
||||||
|
#define GETOPT_OPTVAL(shortname, longname) GETOPT_GENERAL(shortname, shortname, longname, ODF_OPTIONAL_VALUE)
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Short option without value.
|
||||||
|
* @param shortname Short option name. Use \c '\0' if not used.
|
||||||
|
*/
|
||||||
|
#define GETOPT_SHORT_NOVAL(shortname) GETOPT_NOVAL(shortname, NULL)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Short option with value.
|
||||||
|
* @param shortname Short option name. Use \c '\0' if not used.
|
||||||
|
*/
|
||||||
|
#define GETOPT_SHORT_VALUE(shortname) GETOPT_VALUE(shortname, NULL)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Short option with optional value.
|
||||||
|
* @param shortname Short option name. Use \c '\0' if not used.
|
||||||
|
* @note Options with optional values are hopelessly ambiguous, eg "-opt -value", avoid them.
|
||||||
|
*/
|
||||||
|
#define GETOPT_SHORT_OPTVAL(shortname) GETOPT_OPTVAL(shortname, NULL)
|
||||||
|
|
||||||
|
/** Option terminator. */
|
||||||
|
#define GETOPT_END() { '\0', '\0', ODF_END, NULL}
|
||||||
|
|
||||||
|
|
||||||
#endif /* GETOPTDATA_H */
|
#endif /* GETOPTDATA_H */
|
||||||
|
|
|
@ -342,10 +342,41 @@ void MakeNewgameSettingsLive()
|
||||||
extern void DedicatedFork();
|
extern void DedicatedFork();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/** Options of OpenTTD. */
|
||||||
|
static const OptionData _options[] = {
|
||||||
|
GETOPT_SHORT_VALUE('I'),
|
||||||
|
GETOPT_SHORT_VALUE('S'),
|
||||||
|
GETOPT_SHORT_VALUE('M'),
|
||||||
|
GETOPT_SHORT_VALUE('m'),
|
||||||
|
GETOPT_SHORT_VALUE('s'),
|
||||||
|
GETOPT_SHORT_VALUE('v'),
|
||||||
|
GETOPT_SHORT_VALUE('b'),
|
||||||
|
#if defined(ENABLE_NETWORK)
|
||||||
|
GETOPT_SHORT_OPTVAL('D'),
|
||||||
|
GETOPT_SHORT_OPTVAL('n'),
|
||||||
|
GETOPT_SHORT_VALUE('l'),
|
||||||
|
GETOPT_SHORT_VALUE('p'),
|
||||||
|
GETOPT_SHORT_VALUE('P'),
|
||||||
|
#if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32)
|
||||||
|
GETOPT_SHORT_NOVAL('f'),
|
||||||
|
#endif
|
||||||
|
#endif /* ENABLE_NETWORK */
|
||||||
|
GETOPT_SHORT_VALUE('r'),
|
||||||
|
GETOPT_SHORT_VALUE('t'),
|
||||||
|
GETOPT_SHORT_OPTVAL('d'),
|
||||||
|
GETOPT_SHORT_NOVAL('e'),
|
||||||
|
GETOPT_SHORT_OPTVAL('i'),
|
||||||
|
GETOPT_SHORT_OPTVAL('g'),
|
||||||
|
GETOPT_SHORT_VALUE('G'),
|
||||||
|
GETOPT_SHORT_VALUE('c'),
|
||||||
|
GETOPT_SHORT_NOVAL('x'),
|
||||||
|
GETOPT_SHORT_NOVAL('h'),
|
||||||
|
GETOPT_END()
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
int ttd_main(int argc, char *argv[])
|
int ttd_main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int i;
|
|
||||||
const char *optformat;
|
|
||||||
char *musicdriver = NULL;
|
char *musicdriver = NULL;
|
||||||
char *sounddriver = NULL;
|
char *sounddriver = NULL;
|
||||||
char *videodriver = NULL;
|
char *videodriver = NULL;
|
||||||
|
@ -376,18 +407,9 @@ int ttd_main(int argc, char *argv[])
|
||||||
_switch_mode_errorstr = INVALID_STRING_ID;
|
_switch_mode_errorstr = INVALID_STRING_ID;
|
||||||
_config_file = NULL;
|
_config_file = NULL;
|
||||||
|
|
||||||
/* The last param of the following function means this:
|
GetOptData mgo(argc - 1, argv + 1, _options);
|
||||||
* a letter means: it accepts that param (e.g.: -h)
|
|
||||||
* a ':' behind it means: it need a param (e.g.: -m<driver>)
|
|
||||||
* a '::' behind it means: it can optional have a param (e.g.: -d<debug>) */
|
|
||||||
optformat = "m:s:v:b:hD::n::ei::I:S:M:t:d::r:g::G:c:xl:p:P:"
|
|
||||||
#if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32)
|
|
||||||
"f"
|
|
||||||
#endif
|
|
||||||
;
|
|
||||||
|
|
||||||
GetOptData mgo(argc - 1, argv + 1, optformat);
|
|
||||||
|
|
||||||
|
int i;
|
||||||
while ((i = mgo.GetOpt()) != -1) {
|
while ((i = mgo.GetOpt()) != -1) {
|
||||||
switch (i) {
|
switch (i) {
|
||||||
case 'I': free(graphics_set); graphics_set = strdup(mgo.opt); break;
|
case 'I': free(graphics_set); graphics_set = strdup(mgo.opt); break;
|
||||||
|
@ -476,18 +498,25 @@ int ttd_main(int argc, char *argv[])
|
||||||
case 'G': generation_seed = atoi(mgo.opt); break;
|
case 'G': generation_seed = atoi(mgo.opt); break;
|
||||||
case 'c': _config_file = strdup(mgo.opt); break;
|
case 'c': _config_file = strdup(mgo.opt); break;
|
||||||
case 'x': save_config = false; break;
|
case 'x': save_config = false; break;
|
||||||
case -2:
|
|
||||||
case 'h':
|
case 'h':
|
||||||
/* The next two functions are needed to list the graphics sets.
|
i = -2; // Force printing of help.
|
||||||
* We can't do them earlier because then we can't show it on
|
break;
|
||||||
* the debug console as that hasn't been configured yet. */
|
|
||||||
DeterminePaths(argv[0]);
|
|
||||||
BaseGraphics::FindSets();
|
|
||||||
BaseSounds::FindSets();
|
|
||||||
BaseMusic::FindSets();
|
|
||||||
ShowHelp();
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
if (i == -2) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == -2 || mgo.numleft > 0) {
|
||||||
|
/* Either the user typed '-h', he made an error, or he added unrecognized command line arguments.
|
||||||
|
* In all cases, print the help, and exit.
|
||||||
|
*
|
||||||
|
* The next two functions are needed to list the graphics sets. We can't do them earlier
|
||||||
|
* because then we cannot show it on the debug console as that hasn't been configured yet. */
|
||||||
|
DeterminePaths(argv[0]);
|
||||||
|
BaseGraphics::FindSets();
|
||||||
|
BaseSounds::FindSets();
|
||||||
|
BaseMusic::FindSets();
|
||||||
|
ShowHelp();
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(WINCE) && defined(_DEBUG)
|
#if defined(WINCE) && defined(_DEBUG)
|
||||||
|
|
Loading…
Reference in New Issue