nissy-nx

A Rubik's cube optimal solver
git clone https://git.tronto.net/nissy-nx
Download | Log | Files | Refs | README | LICENSE

commit 2332679ad64e3e0318ca812421190156edec9186
parent 8cec37cb3e490b06639f2e05df8b947a9333d5d2
Author: Sebastiano Tronto <sebastiano.tronto@gmail.com>
Date:   Mon, 27 Dec 2021 12:43:14 +0100

Added option -i to accept scramble(s) from stdin, very useful for batch mode

Diffstat:
Mdoc/nissy.1 | 8+++++++-
Anissy | 0
Msrc/commands.c | 25+++++++++++--------------
Msrc/cubetypes.h | 2++
Msrc/shell.c | 24+++++++++++++++++++++++-
Msrc/shell.h | 2+-
6 files changed, 44 insertions(+), 17 deletions(-)

diff --git a/doc/nissy.1 b/doc/nissy.1 @@ -192,6 +192,13 @@ Display version information. . .El . +.Sh SCRAMBLES +All the commands above that accept a scramble also accept a +.Fl Nm i +option with no arguments. +If this option is given, multiple scrambles are read from standard +input (one per line) until and EOF is found, at which point stdin is cleared. +. .Sh ENVIRONMENT Data is stored in the folder pointed to by .Nm $NISSYDATA. @@ -203,7 +210,6 @@ is used instead. If none of this environment variables is defined (e.g. in a non-UNIX system), the current folder is used. . .Sh EXAMPLES -. The command: .Dl nissy solve -v \(dqR\(aqU\(aqFD2L2FR2U2R2BD2LB2D\(aqB2L\(aqR\(aqBD2BU2LU2R\(aqU\(aqF\(dq . diff --git a/nissy b/nissy Binary files differ. diff --git 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 @@ -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 @@ -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 @@ -8,6 +8,6 @@ #define MAXNTOKENS 255 void exec_args(int c, char **v); -void launch(bool prompt); +void launch(bool batchmode); #endif