Skip to content

Commit

Permalink
ports/posix: Remove --file option, add --interactive option
Browse files Browse the repository at this point in the history
This makes snek work more like python3 -- when the --interactive
option (aka -i) is passed, along with a program.py name, snek will
evaluate the program file and then enter interactive mode for further
work.

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed May 18, 2020
1 parent 8e703be commit ac71161
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
42 changes: 19 additions & 23 deletions ports/posix/snek-main.c
Expand Up @@ -21,15 +21,15 @@ FILE *snek_posix_input;

static const struct option options[] = {
{ .name = "version", .has_arg = 0, .val = 'v' },
{ .name = "file", .has_arg = 1, .val = 'f' },
{ .name = "interactive", .has_arg = 0, .val = 'i' },
{ .name = "help", .has_arg = 0, .val = '?' },
{ .name = NULL, .has_arg = 0, .val = 0 },
};

static void
usage (char *program, int val)
{
fprintf(stderr, "usage: %s [--version] [--help] [--file <file.py>] <program.py>\n", program);
fprintf(stderr, "usage: %s [--version] [--help] [--interactive] <program.py>\n", program);
exit(val);
}

Expand Down Expand Up @@ -71,16 +71,17 @@ int
main (int argc, char **argv)
{
int c;
char *file = NULL;
bool do_interactive = true;
bool interactive_flag = false;

while ((c = getopt_long(argc, argv, "v?f:", options, NULL)) != -1) {
while ((c = getopt_long(argc, argv, "v?i", options, NULL)) != -1) {
switch (c) {
case 'v':
printf("%s version %s\n", argv[0], SNEK_VERSION);
exit(0);
break;
case 'f':
file = optarg;
case 'i':
interactive_flag = true;
break;
case '?':
usage(argv[0], 0);
Expand All @@ -93,15 +94,7 @@ main (int argc, char **argv)

snek_init();

if (file) {
snek_file = file;
snek_posix_input = fopen(snek_file, "r");
if (!snek_posix_input) {
perror(snek_file);
exit(1);
}
snek_parse();
}
bool ret = true;

if (argv[optind]) {
snek_file = argv[optind];
Expand All @@ -110,18 +103,21 @@ main (int argc, char **argv)
perror(snek_file);
exit(1);
}
} else {
if (snek_parse() != snek_parse_success)
ret = false;
fclose(snek_posix_input);
do_interactive = interactive_flag;
}

if (do_interactive) {
printf("Welcome to Snek version %s\n", SNEK_VERSION);
snek_file = "<stdin>";
snek_posix_input = stdin;
snek_interactive = true;
printf("Welcome to Snek version %s\n", SNEK_VERSION);
if (snek_parse() != snek_parse_success)
ret = false;
printf("\n");
}

bool ret = snek_parse() == snek_parse_success;

if (snek_posix_input == stdin)
printf("\n");
else
fclose(snek_posix_input);
return ret ? 0 : 1;
}
21 changes: 18 additions & 3 deletions ports/posix/snek.1
Expand Up @@ -15,14 +15,29 @@
.SH NAME
snek \- Snek Programming Language
.SH SYNOPSIS
.B "snek" {program.py}
.B "snek" [--version|-v] [--help|-?] [--interactive|-i] [program.py]
.SH DESCRIPTION
.I snek
is a small Python-derivative suitable for embedded computers. This
host version is largely designed to help test the Snek system and
applications on larger machines
.SH COMMAND LINE OPTIONS
.TP
\--version or \-v
Reports the version number to stdout and then exits.
.TP
\--help or \-?
Prints usage information to stdout and then exits.
.TP
\--interactive or \-i
When a program is specified on the command line, enter interactive
mode after executing that program.
.SH USAGE
If you specify a program file on the command line, snek will load and
execute this instead of reading from the command line.
When a program is specified on the command line, snek runs it. Then,
if the --interactive flag is passed, it enters interactive
mode. Otherwise, it exits.
.P
When no program is specified on the command line, snek enters
interactive mode.
.SH AUTHOR
Keith Packard

0 comments on commit ac71161

Please sign in to comment.