From 2332679ad64e3e0318ca812421190156edec9186 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 27 Dec 2021 12:43:14 +0100 Subject: Added option -i to accept scramble(s) from stdin, very useful for batch mode --- src/commands.c | 25 +++++++++++-------------- src/cubetypes.h | 2 ++ src/shell.c | 24 +++++++++++++++++++++++- src/shell.h | 2 +- 4 files changed, 37 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/commands.c b/src/commands.c index fc63e7c..9ecc9c7 100644 --- a/src/commands.c +++ b/src/commands.c @@ -4,7 +4,6 @@ CommandArgs * gen_parse_args(int c, char **v); CommandArgs * help_parse_args(int c, char **v); -CommandArgs * print_parse_args(int c, char **v); CommandArgs * parse_only_scramble(int c, char **v); CommandArgs * parse_no_arg(int c, char **v); CommandArgs * solve_parse_args(int c, char **v); @@ -92,7 +91,7 @@ print_cmd = { .name = "print", .usage = "print SCRAMBLE", .description = "Print written description of the cube", - .parse_args = print_parse_args, + .parse_args = parse_only_scramble, .exec = print_exec, }; @@ -235,6 +234,8 @@ solve_parse_args(int c, char **v) infinitesols = true; } else if (!strcmp(v[i], "-N")) { a->opts->can_niss = true; + } else if (!strcmp(v[i], "-i")) { + a->scrstdin = true; } else if (!strcmp(v[i], "-v")) { a->opts->verbose = true; } else if (!strcmp(v[i], "-a")) { @@ -251,7 +252,7 @@ solve_parse_args(int c, char **v) if (infinitesols && !fixedmsols) a->opts->max_solutions = 1000000; /* 1M = +infty */ - a->success = read_scramble(c-i, &v[i], a); + a->success = (a->scrstdin && i == c) || read_scramble(c-i, &v[i], a); return a; } @@ -338,7 +339,12 @@ parse_only_scramble(int c, char **v) { CommandArgs *a = new_args(); - a->success = read_scramble(c, v, a); + if (!strcmp(v[0], "-i")) { + a->scrstdin = true; + a->success = c == 1; + } else { + a->success = read_scramble(c, v, a); + } return a; } @@ -353,16 +359,6 @@ parse_no_arg(int c, char **v) return a; } -CommandArgs * -print_parse_args(int c, char **v) -{ - CommandArgs *a = new_args(); - - a->success = read_scramble(c, v, a); - - return a; -} - /* Exec functions implementation *********************************************/ static void @@ -611,6 +607,7 @@ new_args() CommandArgs *args = malloc(sizeof(CommandArgs)); args->success = false; + args->scrstdin = false; args->scramble = NULL; /* initialized in read_scramble */ args->opts = malloc(sizeof(SolveOptions)); diff --git a/src/cubetypes.h b/src/cubetypes.h index 20e028b..8ba1341 100644 --- a/src/cubetypes.h +++ b/src/cubetypes.h @@ -158,6 +158,8 @@ commandargs Command * command; /* For help */ int n; int scrt; + bool scrstdin; + bool header; }; struct diff --git a/src/shell.c b/src/shell.c index 5fb18bc..2cb3719 100644 --- a/src/shell.c +++ b/src/shell.c @@ -32,8 +32,10 @@ void exec_args(int c, char **v) { int i; + char line[MAXLINELEN]; Command *cmd = NULL; CommandArgs *args; + Alg *scramble; for (i = 0; i < NCOMMANDS; i++) if (commands[i] != NULL && !strcmp(v[0], commands[i]->name)) @@ -50,7 +52,27 @@ exec_args(int c, char **v) return; } - cmd->exec(args); + if (args->scrstdin) { + while (true) { + if (fgets(line, MAXLINELEN, stdin) == NULL) { + clearerr(stdin); + break; + } + + scramble = new_alg(line); + + printf(">>> Line: %s", line); + + if (scramble != NULL && scramble->len > 0) { + args->scramble = scramble; + cmd->exec(args); + free_alg(scramble); + args->scramble = NULL; + } + } + } else { + cmd->exec(args); + } free_args(args); } diff --git a/src/shell.h b/src/shell.h index bb85ba2..80f86ba 100644 --- a/src/shell.h +++ b/src/shell.h @@ -8,6 +8,6 @@ #define MAXNTOKENS 255 void exec_args(int c, char **v); -void launch(bool prompt); +void launch(bool batchmode); #endif -- cgit v1.3