diff options
| author | Sebastiano Tronto <sebastiano.tronto@gmail.com> | 2021-12-27 12:43:14 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano.tronto@gmail.com> | 2021-12-27 12:43:14 +0100 |
| commit | 2332679ad64e3e0318ca812421190156edec9186 (patch) | |
| tree | 35a0d1fee1f9e57c973640c599d1e2896e44fd45 | |
| parent | 8cec37cb3e490b06639f2e05df8b947a9333d5d2 (diff) | |
| download | nissy-2332679ad64e3e0318ca812421190156edec9186.tar.gz nissy-2332679ad64e3e0318ca812421190156edec9186.zip | |
Added option -i to accept scramble(s) from stdin, very useful for batch mode
| -rw-r--r-- | doc/nissy.1 | 8 | ||||
| -rwxr-xr-x | nissy | bin | 0 -> 322576 bytes | |||
| -rw-r--r-- | src/commands.c | 25 | ||||
| -rw-r--r-- | src/cubetypes.h | 2 | ||||
| -rw-r--r-- | src/shell.c | 24 | ||||
| -rw-r--r-- | src/shell.h | 2 |
6 files changed, 44 insertions, 17 deletions
diff --git a/doc/nissy.1 b/doc/nissy.1 index c725c0a..2d2836c 100644 --- a/doc/nissy.1 +++ b/doc/nissy.1 | |||
| @@ -192,6 +192,13 @@ Display version information. | |||
| 192 | . | 192 | . |
| 193 | .El | 193 | .El |
| 194 | . | 194 | . |
| 195 | .Sh SCRAMBLES | ||
| 196 | All the commands above that accept a scramble also accept a | ||
| 197 | .Fl Nm i | ||
| 198 | option with no arguments. | ||
| 199 | If this option is given, multiple scrambles are read from standard | ||
| 200 | input (one per line) until and EOF is found, at which point stdin is cleared. | ||
| 201 | . | ||
| 195 | .Sh ENVIRONMENT | 202 | .Sh ENVIRONMENT |
| 196 | Data is stored in the folder pointed to by | 203 | Data is stored in the folder pointed to by |
| 197 | .Nm $NISSYDATA. | 204 | .Nm $NISSYDATA. |
| @@ -203,7 +210,6 @@ is used instead. If none of this environment variables is defined | |||
| 203 | (e.g. in a non-UNIX system), the current folder is used. | 210 | (e.g. in a non-UNIX system), the current folder is used. |
| 204 | . | 211 | . |
| 205 | .Sh EXAMPLES | 212 | .Sh EXAMPLES |
| 206 | . | ||
| 207 | The command: | 213 | The command: |
| 208 | .Dl nissy solve -v \(dqR\(aqU\(aqFD2L2FR2U2R2BD2LB2D\(aqB2L\(aqR\(aqBD2BU2LU2R\(aqU\(aqF\(dq | 214 | .Dl nissy solve -v \(dqR\(aqU\(aqFD2L2FR2U2R2BD2LB2D\(aqB2L\(aqR\(aqBD2BU2LU2R\(aqU\(aqF\(dq |
| 209 | . | 215 | . |
| Binary files differ | |||
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 @@ | |||
| 4 | 4 | ||
| 5 | CommandArgs * gen_parse_args(int c, char **v); | 5 | CommandArgs * gen_parse_args(int c, char **v); |
| 6 | CommandArgs * help_parse_args(int c, char **v); | 6 | CommandArgs * help_parse_args(int c, char **v); |
| 7 | CommandArgs * print_parse_args(int c, char **v); | ||
| 8 | CommandArgs * parse_only_scramble(int c, char **v); | 7 | CommandArgs * parse_only_scramble(int c, char **v); |
| 9 | CommandArgs * parse_no_arg(int c, char **v); | 8 | CommandArgs * parse_no_arg(int c, char **v); |
| 10 | CommandArgs * solve_parse_args(int c, char **v); | 9 | CommandArgs * solve_parse_args(int c, char **v); |
| @@ -92,7 +91,7 @@ print_cmd = { | |||
| 92 | .name = "print", | 91 | .name = "print", |
| 93 | .usage = "print SCRAMBLE", | 92 | .usage = "print SCRAMBLE", |
| 94 | .description = "Print written description of the cube", | 93 | .description = "Print written description of the cube", |
| 95 | .parse_args = print_parse_args, | 94 | .parse_args = parse_only_scramble, |
| 96 | .exec = print_exec, | 95 | .exec = print_exec, |
| 97 | }; | 96 | }; |
| 98 | 97 | ||
| @@ -235,6 +234,8 @@ solve_parse_args(int c, char **v) | |||
| 235 | infinitesols = true; | 234 | infinitesols = true; |
| 236 | } else if (!strcmp(v[i], "-N")) { | 235 | } else if (!strcmp(v[i], "-N")) { |
| 237 | a->opts->can_niss = true; | 236 | a->opts->can_niss = true; |
| 237 | } else if (!strcmp(v[i], "-i")) { | ||
| 238 | a->scrstdin = true; | ||
| 238 | } else if (!strcmp(v[i], "-v")) { | 239 | } else if (!strcmp(v[i], "-v")) { |
| 239 | a->opts->verbose = true; | 240 | a->opts->verbose = true; |
| 240 | } else if (!strcmp(v[i], "-a")) { | 241 | } else if (!strcmp(v[i], "-a")) { |
| @@ -251,7 +252,7 @@ solve_parse_args(int c, char **v) | |||
| 251 | if (infinitesols && !fixedmsols) | 252 | if (infinitesols && !fixedmsols) |
| 252 | a->opts->max_solutions = 1000000; /* 1M = +infty */ | 253 | a->opts->max_solutions = 1000000; /* 1M = +infty */ |
| 253 | 254 | ||
| 254 | a->success = read_scramble(c-i, &v[i], a); | 255 | a->success = (a->scrstdin && i == c) || read_scramble(c-i, &v[i], a); |
| 255 | return a; | 256 | return a; |
| 256 | } | 257 | } |
| 257 | 258 | ||
| @@ -338,7 +339,12 @@ parse_only_scramble(int c, char **v) | |||
| 338 | { | 339 | { |
| 339 | CommandArgs *a = new_args(); | 340 | CommandArgs *a = new_args(); |
| 340 | 341 | ||
| 341 | a->success = read_scramble(c, v, a); | 342 | if (!strcmp(v[0], "-i")) { |
| 343 | a->scrstdin = true; | ||
| 344 | a->success = c == 1; | ||
| 345 | } else { | ||
| 346 | a->success = read_scramble(c, v, a); | ||
| 347 | } | ||
| 342 | 348 | ||
| 343 | return a; | 349 | return a; |
| 344 | } | 350 | } |
| @@ -353,16 +359,6 @@ parse_no_arg(int c, char **v) | |||
| 353 | return a; | 359 | return a; |
| 354 | } | 360 | } |
| 355 | 361 | ||
| 356 | CommandArgs * | ||
| 357 | print_parse_args(int c, char **v) | ||
| 358 | { | ||
| 359 | CommandArgs *a = new_args(); | ||
| 360 | |||
| 361 | a->success = read_scramble(c, v, a); | ||
| 362 | |||
| 363 | return a; | ||
| 364 | } | ||
| 365 | |||
| 366 | /* Exec functions implementation *********************************************/ | 362 | /* Exec functions implementation *********************************************/ |
| 367 | 363 | ||
| 368 | static void | 364 | static void |
| @@ -611,6 +607,7 @@ new_args() | |||
| 611 | CommandArgs *args = malloc(sizeof(CommandArgs)); | 607 | CommandArgs *args = malloc(sizeof(CommandArgs)); |
| 612 | 608 | ||
| 613 | args->success = false; | 609 | args->success = false; |
| 610 | args->scrstdin = false; | ||
| 614 | args->scramble = NULL; /* initialized in read_scramble */ | 611 | args->scramble = NULL; /* initialized in read_scramble */ |
| 615 | args->opts = malloc(sizeof(SolveOptions)); | 612 | args->opts = malloc(sizeof(SolveOptions)); |
| 616 | 613 | ||
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 | |||
| 158 | Command * command; /* For help */ | 158 | Command * command; /* For help */ |
| 159 | int n; | 159 | int n; |
| 160 | int scrt; | 160 | int scrt; |
| 161 | bool scrstdin; | ||
| 162 | bool header; | ||
| 161 | }; | 163 | }; |
| 162 | 164 | ||
| 163 | struct | 165 | 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 | |||
| 32 | exec_args(int c, char **v) | 32 | exec_args(int c, char **v) |
| 33 | { | 33 | { |
| 34 | int i; | 34 | int i; |
| 35 | char line[MAXLINELEN]; | ||
| 35 | Command *cmd = NULL; | 36 | Command *cmd = NULL; |
| 36 | CommandArgs *args; | 37 | CommandArgs *args; |
| 38 | Alg *scramble; | ||
| 37 | 39 | ||
| 38 | for (i = 0; i < NCOMMANDS; i++) | 40 | for (i = 0; i < NCOMMANDS; i++) |
| 39 | if (commands[i] != NULL && !strcmp(v[0], commands[i]->name)) | 41 | if (commands[i] != NULL && !strcmp(v[0], commands[i]->name)) |
| @@ -50,7 +52,27 @@ exec_args(int c, char **v) | |||
| 50 | return; | 52 | return; |
| 51 | } | 53 | } |
| 52 | 54 | ||
| 53 | cmd->exec(args); | 55 | if (args->scrstdin) { |
| 56 | while (true) { | ||
| 57 | if (fgets(line, MAXLINELEN, stdin) == NULL) { | ||
| 58 | clearerr(stdin); | ||
| 59 | break; | ||
| 60 | } | ||
| 61 | |||
| 62 | scramble = new_alg(line); | ||
| 63 | |||
| 64 | printf(">>> Line: %s", line); | ||
| 65 | |||
| 66 | if (scramble != NULL && scramble->len > 0) { | ||
| 67 | args->scramble = scramble; | ||
| 68 | cmd->exec(args); | ||
| 69 | free_alg(scramble); | ||
| 70 | args->scramble = NULL; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | } else { | ||
| 74 | cmd->exec(args); | ||
| 75 | } | ||
| 54 | free_args(args); | 76 | free_args(args); |
| 55 | } | 77 | } |
| 56 | 78 | ||
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 @@ | |||
| 8 | #define MAXNTOKENS 255 | 8 | #define MAXNTOKENS 255 |
| 9 | 9 | ||
| 10 | void exec_args(int c, char **v); | 10 | void exec_args(int c, char **v); |
| 11 | void launch(bool prompt); | 11 | void launch(bool batchmode); |
| 12 | 12 | ||
| 13 | #endif | 13 | #endif |
