Extend runtime option parsing

This commit is contained in:
Lassi Kortela 2019-10-13 20:37:53 +03:00
parent 150d6ff855
commit 3e3ea1ca53
1 changed files with 50 additions and 24 deletions

View File

@ -43,6 +43,25 @@ static const char usage_message[] =
"usage: upscheme [options] script-file [script-arg...]" "usage: upscheme [options] script-file [script-arg...]"
"\n"; "\n";
static const char runtime_usage_message[] =
"usage: upscheme [-:option,option...] ..."
"\n"
"The -: flag sets Up Scheme runtime options. An option is one of:"
"\n"
"\n"
"null start in an environment with only import and cond-expand"
"\n"
"r7rs start in R7RS environment with no Up Scheme extensions"
"\n"
"unstable start with the very latest in-development Up Scheme extensions"
"\n"
"debug set debugging options"
"\n"
"search set module search path"
"\n"
"help show this help"
"\n";
static int evalflag; static int evalflag;
static int helpflag; static int helpflag;
static int versionflag; static int versionflag;
@ -56,6 +75,14 @@ static void generic_usage(FILE *out, int status)
static void usage(void) { generic_usage(stderr, 2); } static void usage(void) { generic_usage(stderr, 2); }
static void generic_runtime_usage(FILE *out, int status)
{
fprintf(out, "%s", runtime_usage_message);
exit(status);
}
static void runtime_usage(void) { generic_runtime_usage(stderr, 2); }
#define VERSION_STRING "0.1.0" #define VERSION_STRING "0.1.0"
static void version(void) static void version(void)
@ -83,50 +110,51 @@ static void runtime_option(const char *name, const char *value)
{ {
if (!strcmp("null", name)) { if (!strcmp("null", name)) {
if (value) if (value)
usage(); runtime_usage();
boot_env = BOOT_ENV_NULL; boot_env = BOOT_ENV_NULL;
} else if (!strcmp("r7rs", name)) { } else if (!strcmp("r7rs", name)) {
if (value) if (value)
usage(); runtime_usage();
boot_env = BOOT_ENV_R7RS; boot_env = BOOT_ENV_R7RS;
} else if (!strcmp("unstable", name)) { } else if (!strcmp("unstable", name)) {
if (value) if (value)
usage(); runtime_usage();
boot_env = BOOT_ENV_UNSTABLE; boot_env = BOOT_ENV_UNSTABLE;
} else if (!strcmp("debug", name)) { } else if (!strcmp("debug", name)) {
if (!value) if (!value)
usage(); runtime_usage();
} else if (!strcmp("search", name)) { } else if (!strcmp("search", name)) {
if (!value) if (!value)
usage(); runtime_usage();
} else if (!strcmp("help", name)) {
generic_runtime_usage(stdout, 0);
} else { } else {
usage(); runtime_usage();
} }
} }
static void runtime_options(const char *arg) static void runtime_options(const char *arg)
{ {
char *whole;
char *name; char *name;
char *value; char *value;
char *whole;
char *limit; char *limit;
if (!(name = whole = strdup(arg))) { if (!(whole = strdup(arg))) {
usage(); // TODO: out of memory runtime_usage(); // TODO: out of memory
} }
while (name[0]) { for (name = whole; name; name = limit) {
if (!(limit = strchr(name, ','))) { if ((limit = strchr(name, ','))) {
limit = strchr(name, 0); *limit++ = 0;
} }
if ((value = strchr(name, '='))) { if ((value = strchr(name, '='))) {
if (value < limit) { *value++ = 0;
*value++ = 0; }
} else { if (*name) {
value = 0; runtime_option(name, value);
} } else if (value) {
runtime_usage();
} }
runtime_option(name, value);
name = limit;
} }
free(whole); free(whole);
} }
@ -143,11 +171,6 @@ static char **short_option(char **argv, int option)
case 'V': case 'V':
versionflag = 1; versionflag = 1;
break; break;
case ':':
if (!argv[0])
usage();
runtime_options(*argv++);
break;
default: default:
usage(); usage();
break; break;
@ -171,6 +194,9 @@ static char **parse_command_line_flags(char **argv)
argv++; argv++;
argv = long_option(argv, arg); argv = long_option(argv, arg);
} }
} else if (arg[1] == ':') {
argv++;
runtime_options(&arg[2]);
} else if (!arg[1]) { } else if (!arg[1]) {
break; break;
} else { } else {