2019-08-17 17:09:43 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2019-08-09 12:00:17 -04:00
|
|
|
#include <assert.h>
|
2019-08-13 11:28:19 -04:00
|
|
|
#include <math.h>
|
2019-08-09 12:00:17 -04:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
2019-08-09 16:25:20 -04:00
|
|
|
#include <stdint.h>
|
2019-08-09 13:14:42 -04:00
|
|
|
#include <stdio.h>
|
2010-05-02 14:17:47 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-08-09 12:00:17 -04:00
|
|
|
|
2019-08-26 15:12:15 -04:00
|
|
|
#include "scheme.h"
|
2010-05-02 14:17:47 -04:00
|
|
|
|
2019-09-30 10:08:51 -04:00
|
|
|
#define BOOT_ENV_NULL 0
|
|
|
|
#define BOOT_ENV_R7RS 1
|
|
|
|
#define BOOT_ENV_UNSTABLE 2
|
|
|
|
|
2020-04-07 05:25:06 -04:00
|
|
|
static value_t argv_list(int argc, const char **argv)
|
2010-05-02 14:17:47 -04:00
|
|
|
{
|
|
|
|
int i;
|
2019-08-09 07:02:02 -04:00
|
|
|
value_t lst = FL_NIL, temp;
|
2010-05-02 14:17:47 -04:00
|
|
|
fl_gc_handle(&lst);
|
|
|
|
fl_gc_handle(&temp);
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = argc - 1; i >= 0; i--) {
|
2010-05-02 14:17:47 -04:00
|
|
|
temp = cvalue_static_cstring(argv[i]);
|
|
|
|
lst = fl_cons(temp, lst);
|
|
|
|
}
|
|
|
|
fl_free_gc_handles(2);
|
|
|
|
return lst;
|
|
|
|
}
|
|
|
|
|
2019-09-30 10:08:51 -04:00
|
|
|
static const char usage_message[] =
|
|
|
|
"usage: upscheme -h|--help"
|
|
|
|
"\n"
|
|
|
|
"usage: upscheme -V|--version"
|
|
|
|
"\n"
|
|
|
|
"usage: upscheme [options]"
|
|
|
|
"\n"
|
|
|
|
"usage: upscheme [options] -e expression..."
|
|
|
|
"\n"
|
|
|
|
"usage: upscheme [options] script-file [script-arg...]"
|
|
|
|
"\n";
|
|
|
|
|
2019-10-13 13:37:53 -04:00
|
|
|
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"
|
2019-10-14 12:18:57 -04:00
|
|
|
"version show version information"
|
|
|
|
"\n"
|
2019-10-13 13:37:53 -04:00
|
|
|
"help show this help"
|
|
|
|
"\n";
|
|
|
|
|
2020-04-07 05:25:06 -04:00
|
|
|
const char *script_file;
|
|
|
|
value_t os_command_line;
|
|
|
|
int command_line_offset;
|
|
|
|
|
2019-09-30 10:08:51 -04:00
|
|
|
static int evalflag;
|
|
|
|
static int helpflag;
|
|
|
|
static int versionflag;
|
|
|
|
static int boot_env;
|
|
|
|
|
|
|
|
static void generic_usage(FILE *out, int status)
|
|
|
|
{
|
|
|
|
fprintf(out, "%s", usage_message);
|
|
|
|
exit(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(void) { generic_usage(stderr, 2); }
|
|
|
|
|
2019-10-13 13:37:53 -04:00
|
|
|
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); }
|
|
|
|
|
2019-09-30 10:08:51 -04:00
|
|
|
static void version(void)
|
|
|
|
{
|
2019-10-13 17:43:31 -04:00
|
|
|
value_t list;
|
|
|
|
|
|
|
|
for (list = get_version_alist(); iscons(list); list = cdr_(list)) {
|
|
|
|
write_simple_defaults(ios_stdout, car_(list));
|
|
|
|
ios_putc('\n', ios_stdout);
|
|
|
|
}
|
2019-09-30 10:08:51 -04:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2020-04-07 03:22:38 -04:00
|
|
|
static const char **long_option(const char **argv, const char *option)
|
2019-09-30 10:08:51 -04:00
|
|
|
{
|
|
|
|
if (!strcmp(option, "--help")) {
|
|
|
|
helpflag = 1;
|
|
|
|
} else if (!strcmp(option, "--version")) {
|
|
|
|
versionflag = 1;
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void runtime_option(const char *name, const char *value)
|
2010-05-02 14:17:47 -04:00
|
|
|
{
|
2019-09-30 10:08:51 -04:00
|
|
|
if (!strcmp("null", name)) {
|
|
|
|
if (value)
|
2019-10-13 13:37:53 -04:00
|
|
|
runtime_usage();
|
2019-09-30 10:08:51 -04:00
|
|
|
boot_env = BOOT_ENV_NULL;
|
|
|
|
} else if (!strcmp("r7rs", name)) {
|
|
|
|
if (value)
|
2019-10-13 13:37:53 -04:00
|
|
|
runtime_usage();
|
2019-09-30 10:08:51 -04:00
|
|
|
boot_env = BOOT_ENV_R7RS;
|
|
|
|
} else if (!strcmp("unstable", name)) {
|
|
|
|
if (value)
|
2019-10-13 13:37:53 -04:00
|
|
|
runtime_usage();
|
2019-09-30 10:08:51 -04:00
|
|
|
boot_env = BOOT_ENV_UNSTABLE;
|
|
|
|
} else if (!strcmp("debug", name)) {
|
|
|
|
if (!value)
|
2019-10-13 13:37:53 -04:00
|
|
|
runtime_usage();
|
2019-09-30 10:08:51 -04:00
|
|
|
} else if (!strcmp("search", name)) {
|
|
|
|
if (!value)
|
2019-10-13 13:37:53 -04:00
|
|
|
runtime_usage();
|
2019-10-14 12:18:57 -04:00
|
|
|
} else if (!strcmp("version", name)) {
|
|
|
|
versionflag = 1;
|
2019-10-13 13:37:53 -04:00
|
|
|
} else if (!strcmp("help", name)) {
|
|
|
|
generic_runtime_usage(stdout, 0);
|
2019-09-30 10:08:51 -04:00
|
|
|
} else {
|
2019-10-13 13:37:53 -04:00
|
|
|
runtime_usage();
|
2019-09-30 10:08:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void runtime_options(const char *arg)
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *value;
|
2019-10-13 13:37:53 -04:00
|
|
|
char *whole;
|
2019-09-30 10:08:51 -04:00
|
|
|
char *limit;
|
|
|
|
|
2019-10-13 13:37:53 -04:00
|
|
|
if (!(whole = strdup(arg))) {
|
|
|
|
runtime_usage(); // TODO: out of memory
|
2019-09-30 10:08:51 -04:00
|
|
|
}
|
2019-10-13 13:37:53 -04:00
|
|
|
for (name = whole; name; name = limit) {
|
|
|
|
if ((limit = strchr(name, ','))) {
|
|
|
|
*limit++ = 0;
|
2019-09-30 10:08:51 -04:00
|
|
|
}
|
|
|
|
if ((value = strchr(name, '='))) {
|
2019-10-13 13:37:53 -04:00
|
|
|
*value++ = 0;
|
|
|
|
}
|
|
|
|
if (*name) {
|
|
|
|
runtime_option(name, value);
|
|
|
|
} else if (value) {
|
|
|
|
runtime_usage();
|
2019-09-30 10:08:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(whole);
|
|
|
|
}
|
|
|
|
|
2020-04-07 03:22:38 -04:00
|
|
|
static const char **short_option(const char **argv, int option)
|
2019-09-30 10:08:51 -04:00
|
|
|
{
|
|
|
|
switch (option) {
|
|
|
|
case 'e':
|
|
|
|
evalflag = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
helpflag = 1;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
versionflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
2020-04-07 03:22:38 -04:00
|
|
|
static const char **parse_command_line_flags(const char **argv)
|
2019-09-30 10:08:51 -04:00
|
|
|
{
|
2020-04-07 03:22:38 -04:00
|
|
|
const char *arg;
|
2019-09-30 10:08:51 -04:00
|
|
|
int option;
|
|
|
|
|
|
|
|
while ((arg = *argv)) {
|
|
|
|
if (arg[0] == '-') {
|
|
|
|
if (arg[1] == '-') {
|
|
|
|
if (arg[2] == '-') {
|
|
|
|
usage();
|
|
|
|
} else if (!arg[2]) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
argv++;
|
|
|
|
argv = long_option(argv, arg);
|
|
|
|
}
|
2019-10-13 13:37:53 -04:00
|
|
|
} else if (arg[1] == ':') {
|
|
|
|
argv++;
|
|
|
|
runtime_options(&arg[2]);
|
2019-09-30 10:08:51 -04:00
|
|
|
} else if (!arg[1]) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
argv++;
|
|
|
|
for (arg++; (option = *arg); arg++) {
|
|
|
|
argv = short_option(argv, option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2020-04-07 05:25:06 -04:00
|
|
|
const char **command_line;
|
|
|
|
|
|
|
|
command_line = parse_command_line_flags((const char **)(argv + 1));
|
2019-09-30 10:08:51 -04:00
|
|
|
if (helpflag) {
|
|
|
|
generic_usage(stdout, 0);
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
fl_init(512 * 1024);
|
|
|
|
{
|
2020-04-07 05:25:06 -04:00
|
|
|
fl_gc_handle(&os_command_line);
|
|
|
|
os_command_line = argv_list(argc, (const char **)argv);
|
|
|
|
command_line_offset = (command_line - (const char **)argv) / sizeof(*argv);
|
2019-08-21 15:08:21 -04:00
|
|
|
FL_TRY_EXTERN
|
|
|
|
{
|
2019-10-13 17:43:31 -04:00
|
|
|
if (versionflag) {
|
|
|
|
version();
|
|
|
|
}
|
2019-08-21 15:08:21 -04:00
|
|
|
if (fl_load_boot_image())
|
|
|
|
return 1;
|
2020-04-07 05:25:06 -04:00
|
|
|
script_file = command_line[0];
|
|
|
|
if (script_file) {
|
|
|
|
script_file = realpath(command_line[0], 0);
|
|
|
|
}
|
2019-08-21 15:08:21 -04:00
|
|
|
(void)fl_applyn(1, symbol_value(symbol("__start")),
|
2020-04-07 05:25:06 -04:00
|
|
|
os_command_line);
|
2019-08-21 15:08:21 -04:00
|
|
|
}
|
|
|
|
FL_CATCH_EXTERN
|
|
|
|
{
|
|
|
|
ios_puts("fatal error:\n", ios_stderr);
|
2019-08-25 15:39:35 -04:00
|
|
|
write_defaults_indent(ios_stderr, fl_lasterror);
|
2019-08-21 15:08:21 -04:00
|
|
|
ios_putc('\n', ios_stderr);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-05-02 14:17:47 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|