aboutsummaryrefslogtreecommitdiff
path: root/src/commands.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands.c')
-rw-r--r--src/commands.c569
1 files changed, 0 insertions, 569 deletions
diff --git a/src/commands.c b/src/commands.c
deleted file mode 100644
index a9e8fc9..0000000
--- a/src/commands.c
+++ /dev/null
@@ -1,569 +0,0 @@
1#define COMMANDS_C
2
3#include "commands.h"
4
5static bool read_cs(CommandArgs *args, char *str);
6static bool read_scrtype(CommandArgs *args, char *str);
7static bool read_scramble(int c, char **v, CommandArgs *args);
8
9/* Arg parsing functions implementation **************************************/
10
11CommandArgs *
12solve_parse_args(int c, char **v)
13{
14 int i;
15 bool infinitesols, fixedmsols;
16 long val;
17
18 CommandArgs *a = new_args();
19
20 a->opts->min_moves = 0;
21 a->opts->max_moves = 20;
22 a->opts->max_solutions = 1;
23 a->opts->nthreads = 1;
24 a->opts->optimal = -1;
25 a->opts->can_niss = false;
26 a->opts->verbose = false;
27 a->opts->all = false;
28 a->opts->print_number = true;
29 a->opts->count_only = false;
30
31 fixedmsols = false;
32 infinitesols = false;
33
34 for (i = 0; i < c; i++) {
35 if (!strcmp(v[i], "-m") && i+1 < c) {
36 val = strtol(v[++i], NULL, 10);
37 if (val < 0 || val > 100) {
38 fprintf(stderr,
39 "Invalid min number of moves"
40 "(0 <= N <= 100).\n");
41 return a;
42 }
43 a->opts->min_moves = val;
44 } else if (!strcmp(v[i], "-M") && i+1 < c) {
45 val = strtol(v[++i], NULL, 10);
46 if (val < 0 || val > 100) {
47 fprintf(stderr,
48 "Invalid max number of moves"
49 "(0 <= N <= 100).\n");
50 return a;
51 }
52 a->opts->max_moves = val;
53 infinitesols = true;
54 } else if (!strcmp(v[i], "-t") && i+1 < c) {
55 val = strtol(v[++i], NULL, 10);
56 if (val < 1 || val > 64) {
57 fprintf(stderr,
58 "Invalid number of threads."
59 "1 <= t <= 64\n");
60 return a;
61 }
62 a->opts->nthreads = val;
63 } else if (!strcmp(v[i], "-n") && i+1 < c) {
64 val = strtol(v[++i], NULL, 10);
65 if (val < 1 || val > 1000000) {
66 fprintf(stderr,
67 "Invalid number of solutions.\n");
68 return a;
69 }
70 a->opts->max_solutions = val;
71 fixedmsols = true;
72 } else if (!strcmp(v[i], "-o")) {
73 a->opts->optimal = 0;
74 infinitesols = true;
75 } else if (!strcmp(v[i], "-O") && i+1 < c) {
76 val = strtol(v[++i], NULL, 10);
77 if (val < 0 || val > 100 ||
78 (val == 0 && strcmp("0", v[i]))) {
79 fprintf(stderr,
80 "Invalid max number of moves"
81 " (0 <= N <= 100).\n");
82 return a;
83 }
84 a->opts->optimal = val;
85 infinitesols = true;
86 } else if (!strcmp(v[i], "-N")) {
87 a->opts->can_niss = true;
88 } else if (!strcmp(v[i], "-i")) {
89 a->scrstdin = true;
90 } else if (!strcmp(v[i], "-v")) {
91 a->opts->verbose = true;
92 } else if (!strcmp(v[i], "-a")) {
93 a->opts->all = true;
94 } else if (!strcmp(v[i], "-p")) {
95 a->opts->print_number = false;
96 } else if (!strcmp(v[i], "-c")) {
97 a->opts->count_only = true;
98 } else if (!read_cs(a, v[i])) {
99 break;
100 }
101 }
102
103 if (infinitesols && !fixedmsols)
104 a->opts->max_solutions = 1000000; /* 1M = +infty */
105
106 a->success = (a->scrstdin && i == c) || read_scramble(c-i, &v[i], a);
107 return a;
108}
109
110CommandArgs *
111scramble_parse_args(int c, char **v)
112{
113 int i;
114 long val;
115
116 CommandArgs *a = new_args();
117
118 a->success = true;
119 a->n = 1;
120
121 for (i = 0; i < c; i++) {
122 if (!strcmp(v[i], "-n") && i+1 < c) {
123 val = strtol(v[++i], NULL, 10);
124 if (val < 1 || val > 1000000) {
125 fprintf(stderr,
126 "Invalid number of scrambles.\n");
127 a->success = false;
128 return a;
129 }
130 a->n = val;
131 } else if (!read_scrtype(a, v[i])) {
132 a->success = false;
133 return a;
134 }
135 }
136
137 return a;
138}
139
140CommandArgs *
141gen_parse_args(int c, char **v)
142{
143 int val;
144 CommandArgs *a = new_args();
145
146 a->opts->nthreads = 64;
147 a->success = false;
148
149 if (c == 0) {
150 a->success = true;
151 } else {
152 if (!strcmp(v[0], "-t") && c > 1) {
153 val = strtol(v[1], NULL, 10);
154 if (val < 1 || val > 64) {
155 fprintf(stderr,
156 "Invalid number of threads."
157 "1 <= t <= 64\n");
158 return a;
159 }
160 a->opts->nthreads = val;
161 a->success = true;
162 }
163 }
164
165 return a;
166}
167
168CommandArgs *
169help_parse_args(int c, char **v)
170{
171 int i;
172 CommandArgs *a = new_args();
173
174 if (c == 1) {
175 for (i = 0; commands[i] != NULL; i++)
176 if (!strcmp(v[0], commands[i]->name))
177 a->command = commands[i];
178 if (a->command == NULL)
179 fprintf(stderr, "%s: command not found\n", v[0]);
180 }
181
182 a->success = c == 0 || (c == 1 && a->command != NULL);
183 return a;
184}
185
186CommandArgs *
187parse_only_scramble(int c, char **v)
188{
189 CommandArgs *a = new_args();
190
191 if (!strcmp(v[0], "-i")) {
192 a->scrstdin = true;
193 a->success = c == 1;
194 } else {
195 a->success = read_scramble(c, v, a);
196 }
197
198 return a;
199}
200
201CommandArgs *
202parse_no_arg(int c, char **v)
203{
204 CommandArgs *a = new_args();
205
206 a->success = true;
207
208 return a;
209}
210
211/* Exec functions implementation *********************************************/
212
213void
214solve_exec(CommandArgs *args)
215{
216 Cube c;
217 AlgList *sols;
218 Solver *solver[99];
219 Threader *threader;
220
221 make_solved(&c);
222 apply_alg(args->scramble, &c);
223/* TODO: adjust */
224/* threader = &threader_single;*/
225 threader = &threader_eager;
226
227/* TODO: adjust */
228 int i;
229 for (i = 0; args->cs->step[i] != NULL; i++)
230 solver[i] = new_stepsolver_lazy(args->cs->step[i]);
231 solver[i] = NULL;
232 sols = solve(&c, args->opts, solver, threader);
233
234 if (args->opts->count_only)
235 printf("%d\n", sols->len);
236 else
237 print_alglist(sols, args->opts->print_number);
238
239 free_alglist(sols);
240}
241
242void
243scramble_exec(CommandArgs *args)
244{
245 Cube cube;
246 Alg *scr, *ruf, *aux;
247 int i, j, eo, ep, co, cp;
248 uint64_t ui, uj, uk;
249
250 srand(time(NULL));
251
252 for (i = 0; i < args->n; i++) {
253
254 if (!strcmp(args->scrtype, "dr")) {
255 ui = rand() % FACTORIAL8;
256 uj = rand() % FACTORIAL8;
257 uk = rand() % FACTORIAL4;
258
259 make_solved(&cube);
260 index_to_perm(ui, 8, cube.cp);
261 index_to_perm(uj, 8, cube.ep);
262 index_to_perm(uk, 4, cube.ep + 8);
263 for (j = 8; j < 12; j++)
264 cube.ep[j] += 8;
265 } else if (!strcmp(args->scrtype, "htr")) {
266 make_solved(&cube);
267 /* TODO */
268 } else {
269 ep = rand() % FACTORIAL12;
270 cp = rand() % FACTORIAL8;
271 eo = rand() % POW2TO11;
272 co = rand() % POW3TO7;
273
274 if (!strcmp(args->scrtype, "eo")) {
275 eo = 0;
276 } else if (!strcmp(args->scrtype, "corners")) {
277 eo = 0;
278 ep = 0;
279 } else if (!strcmp(args->scrtype, "edges")) {
280 co = 0;
281 cp = 0;
282 }
283
284 make_solved(&cube);
285 index_to_perm(ep, 12, cube.ep);
286 index_to_perm(cp, 8, cube.cp);
287 int_to_sum_zero_array(eo, 2, 12, cube.eo);
288 int_to_sum_zero_array(co, 3, 8, cube.co);
289 }
290
291 if (!is_admissible(&cube)) {
292 if (!strcmp(args->scrtype, "corners"))
293 swap(&cube.cp[UFR], &cube.cp[UFL]);
294 else
295 swap(&cube.ep[UF], &cube.ep[UB]);
296 }
297
298 /* TODO: can be optimized for htr and dr using htrfin, drfin */
299 /*
300 TODO: solve_2phase was removed
301 scr = solve_2phase(&cube, 1);
302 */
303
304 if (!strcmp(args->scrtype, "fmc")) {
305 aux = new_alg("");
306 copy_alg(scr, aux);
307 /* Trick to rufify for free: rotate the scramble *
308 * so that it does not start with F or end with R */
309 for (j = 0; j < NROTATIONS; j++) {
310 if (base_move(scr->move[0]) != F &&
311 base_move(scr->move[0]) != B &&
312 base_move(scr->move[scr->len-1]) != R &&
313 base_move(scr->move[scr->len-1]) != L)
314 break;
315 copy_alg(aux, scr);
316 transform_alg(j, scr);
317 }
318 copy_alg(scr, aux);
319 ruf = new_alg("R' U' F");
320 copy_alg(ruf, scr);
321 compose_alg(scr, aux);
322 compose_alg(scr, ruf);
323 free_alg(aux);
324 free_alg(ruf);
325 }
326 print_alg(scr, false);
327 free_alg(scr);
328 }
329}
330
331void
332gen_exec(CommandArgs *args)
333{
334/* TODO:
335 int i;
336
337 fprintf(stderr, "Generating coordinates...\n");
338 fprintf(stderr, "Generating pruning tables...\n");
339 for (i = 0; all_pd[i] != NULL; i++)
340 genptable(all_pd[i], args->opts->nthreads);
341*/
342
343 fprintf(stderr, "Done!\n");
344}
345
346void
347invert_exec(CommandArgs *args)
348{
349 Alg *inv;
350
351 inv = inverse_alg(args->scramble);
352 print_alg(inv, false);
353
354 free_alg(inv);
355}
356
357void
358steps_exec(CommandArgs *args)
359{
360 int i;
361
362 for (i = 0; csteps[i] != NULL; i++)
363 printf("%-15s %s\n", csteps[i]->shortname, csteps[i]->name);
364}
365
366void
367commands_exec(CommandArgs *args)
368{
369 int i;
370
371 for (i = 0; commands[i] != NULL; i++)
372 printf("%s\n", commands[i]->usage);
373
374}
375
376void
377freemem_exec(CommandArgs *args)
378{
379/* TODO:
380 int i;
381
382 for (i = 0; all_pd[i] != NULL; i++)
383 free_pd(all_pd[i]);
384
385 for (i = 0; all_sd[i] != NULL; i++)
386 free_sd(all_sd[i]);
387*/
388}
389
390void
391print_exec(CommandArgs *args)
392{
393 Cube c;
394
395 make_solved(&c);
396 apply_alg(args->scramble, &c);
397 print_cube(&c);
398}
399
400/*
401void
402twophase_exec(CommandArgs *args)
403{
404 Cube c;
405 Alg *sol;
406
407 make_solved(&c);
408 apply_alg(args->scramble, &c);
409 sol = solve_2phase(&c, 1);
410
411 print_alg(sol, false);
412 free_alg(sol);
413}
414*/
415
416void
417help_exec(CommandArgs *args)
418{
419 if (args->command == NULL) {
420 printf(
421 "Use the nissy command \"help COMMAND\" for a short "
422 "description of a specific command.\n"
423 "Use the nissy command \"commands\" for a list of "
424 "available commands.\n"
425 "See the manual page for more details. The manual"
426 " page is available with \"man nissy\" on a UNIX"
427 " system (such as Linux or MacOS) or in pdf and html"
428 " format in the docs folder.\n"
429 "Nissy is available for free at "
430 "https://nissy.tronto.net\n"
431 );
432 } else {
433 printf("Command %s: %s\nusage: %s\n", args->command->name,
434 args->command->description, args->command->usage);
435 }
436}
437
438void
439quit_exec(CommandArgs *args)
440{
441 exit(0);
442}
443
444void
445cleanup_exec(CommandArgs *args)
446{
447 Alg *alg;
448
449 alg = cleanup(args->scramble);
450 print_alg(alg, false);
451
452 free_alg(alg);
453}
454
455void
456unniss_exec(CommandArgs *args)
457{
458 Alg *aux;
459
460 aux = unniss(args->scramble);
461 print_alg(aux, false);
462 free(aux);
463}
464
465void
466version_exec(CommandArgs *args)
467{
468 printf(VERSION"\n");
469}
470
471/* Local functions implementation ********************************************/
472
473static bool
474read_scramble(int c, char **v, CommandArgs *args)
475{
476 int i, k, n;
477 unsigned int j;
478 char *algstr;
479
480 if (c < 1) {
481 fprintf(stderr, "Error: no scramble given?\n");
482 return false;
483 }
484
485 for(n = 0, i = 0; i < c; i++)
486 n += strlen(v[i]);
487
488 algstr = malloc((n + 1) * sizeof(char));
489 k = 0;
490 for (i = 0; i < c; i++)
491 for (j = 0; j < strlen(v[i]); j++)
492 algstr[k++] = v[i][j];
493 algstr[k] = 0;
494
495 args->scramble = new_alg(algstr);
496 free(algstr);
497
498 if (args->scramble->len == 0)
499 fprintf(stderr, "Error reading scramble\n");
500
501 return args->scramble->len > 0;
502}
503
504static bool
505read_scrtype(CommandArgs *args, char *str)
506{
507 int i;
508 static char *scrtypes[20] =
509 { "eo", "corners", "edges", "fmc", "dr", "htr", NULL };
510
511 for (i = 0; scrtypes[i] != NULL; i++) {
512 if (!strcmp(scrtypes[i], str)) {
513 strcpy(args->scrtype, scrtypes[i]);
514 return true;
515 }
516 }
517
518 return false;
519}
520
521static bool
522read_cs(CommandArgs *args, char *str)
523{
524 int i;
525
526 for (i = 0; csteps[i] != NULL; i++) {
527 if (!strcmp(csteps[i]->shortname, str)) {
528 args->cs = csteps[i];
529 return true;
530 }
531 }
532
533 return false;
534}
535
536/* Public functions implementation *******************************************/
537
538void
539free_args(CommandArgs *args)
540{
541 if (args == NULL)
542 return;
543
544 if (args->scramble != NULL)
545 free_alg(args->scramble);
546 if (args->opts != NULL)
547 free(args->opts);
548
549 /* step and command must not be freed, they are static! */
550
551 free(args);
552}
553
554CommandArgs *
555new_args()
556{
557 CommandArgs *args = malloc(sizeof(CommandArgs));
558
559 args->success = false;
560 args->scrstdin = false;
561 args->scramble = NULL; /* initialized in read_scramble */
562 args->opts = malloc(sizeof(SolveOptions));
563
564 /* step and command are static */
565 args->cs = csteps[0]; /* default: first step in list */
566 args->command = NULL;
567
568 return args;
569}

Generated with cgit - Back to sebastiano.tronto.net