diff options
Diffstat (limited to '')
| -rw-r--r-- | old/2021-11-10-beforeremovingchecker/commands.c | 329 |
1 files changed, 0 insertions, 329 deletions
diff --git a/old/2021-11-10-beforeremovingchecker/commands.c b/old/2021-11-10-beforeremovingchecker/commands.c deleted file mode 100644 index b141229..0000000 --- a/old/2021-11-10-beforeremovingchecker/commands.c +++ /dev/null | |||
| @@ -1,329 +0,0 @@ | |||
| 1 | #include "commands.h" | ||
| 2 | |||
| 3 | /* Arg parsing functions *****************************************************/ | ||
| 4 | |||
| 5 | CommandArgs * solvestep_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_no_arg(int c, char **v); | ||
| 9 | |||
| 10 | /* Exec functions ************************************************************/ | ||
| 11 | |||
| 12 | static void solvestep_exec(CommandArgs *args); | ||
| 13 | static void steps_exec(CommandArgs *args); | ||
| 14 | static void commands_exec(CommandArgs *args); | ||
| 15 | static void print_exec(CommandArgs *args); | ||
| 16 | static void help_exec(CommandArgs *args); | ||
| 17 | static void quit_exec(CommandArgs *args); | ||
| 18 | |||
| 19 | /* Local functions ***********************************************************/ | ||
| 20 | |||
| 21 | static bool read_step(CommandArgs *args, char *str); | ||
| 22 | static bool read_scramble(int c, char **v, CommandArgs *args); | ||
| 23 | |||
| 24 | /* Commands ******************************************************************/ | ||
| 25 | |||
| 26 | Command | ||
| 27 | solvestep_cmd = { | ||
| 28 | .name = "solve", | ||
| 29 | .usage = "solve STEP [OPTIONS] SCRAMBLE", | ||
| 30 | .description = "Solve a step", | ||
| 31 | .parse_args = solvestep_parse_args, | ||
| 32 | .exec = solvestep_exec | ||
| 33 | }; | ||
| 34 | |||
| 35 | Command | ||
| 36 | steps_cmd = { | ||
| 37 | .name = "steps", | ||
| 38 | .usage = "steps", | ||
| 39 | .description = "List available steps", | ||
| 40 | .parse_args = parse_no_arg, | ||
| 41 | .exec = steps_exec | ||
| 42 | }; | ||
| 43 | |||
| 44 | Command | ||
| 45 | commands_cmd = { | ||
| 46 | .name = "commands", | ||
| 47 | .usage = "commands", | ||
| 48 | .description = "List available commands", | ||
| 49 | .parse_args = parse_no_arg, | ||
| 50 | .exec = commands_exec | ||
| 51 | }; | ||
| 52 | |||
| 53 | Command | ||
| 54 | print_cmd = { | ||
| 55 | .name = "print", | ||
| 56 | .usage = "print SCRAMBLE", | ||
| 57 | .description = "Print written description of the cube", | ||
| 58 | .parse_args = print_parse_args, | ||
| 59 | .exec = print_exec, | ||
| 60 | }; | ||
| 61 | |||
| 62 | Command | ||
| 63 | help_cmd = { | ||
| 64 | .name = "help", | ||
| 65 | .usage = "help [COMMAND]", | ||
| 66 | .description = "Display nissy manual page or help on specific command", | ||
| 67 | .parse_args = help_parse_args, | ||
| 68 | .exec = help_exec, | ||
| 69 | }; | ||
| 70 | |||
| 71 | Command | ||
| 72 | quit_cmd = { | ||
| 73 | .name = "quit", | ||
| 74 | .usage = "quit", | ||
| 75 | .description = "Quit nissy", | ||
| 76 | .parse_args = parse_no_arg, | ||
| 77 | .exec = quit_exec, | ||
| 78 | }; | ||
| 79 | |||
| 80 | Command *commands[NCOMMANDS] = { | ||
| 81 | &solvestep_cmd, | ||
| 82 | &steps_cmd, | ||
| 83 | &commands_cmd, | ||
| 84 | &help_cmd, | ||
| 85 | &print_cmd, | ||
| 86 | &quit_cmd | ||
| 87 | }; | ||
| 88 | |||
| 89 | /* Arg parsing functions implementation **************************************/ | ||
| 90 | |||
| 91 | CommandArgs * | ||
| 92 | solvestep_parse_args(int c, char **v) | ||
| 93 | { | ||
| 94 | int i; | ||
| 95 | long val; | ||
| 96 | |||
| 97 | CommandArgs *a = malloc(sizeof(CommandArgs)); | ||
| 98 | |||
| 99 | a->success = false; | ||
| 100 | a->opts = malloc(sizeof(SolveOptions)); | ||
| 101 | a->step = steps[0]; | ||
| 102 | a->command = NULL; | ||
| 103 | a->scramble = NULL; | ||
| 104 | |||
| 105 | a->opts->min_moves = 0; | ||
| 106 | a->opts->max_moves = 20; | ||
| 107 | a->opts->max_solutions = 1; | ||
| 108 | a->opts->optimal_only = false; | ||
| 109 | a->opts->can_niss = false; | ||
| 110 | a->opts->feedback = false; | ||
| 111 | a->opts->all = false; | ||
| 112 | a->opts->print_number = true; | ||
| 113 | |||
| 114 | for (i = 0; i < c; i++) { | ||
| 115 | if (!strcmp(v[i], "-m")) { | ||
| 116 | val = strtol(v[++i], NULL, 10); | ||
| 117 | if (val < 0 || val > 100) { | ||
| 118 | fprintf(stderr, | ||
| 119 | "Invalid min number of moves.\n"); | ||
| 120 | return a; | ||
| 121 | } | ||
| 122 | a->opts->min_moves = val; | ||
| 123 | } else if (!strcmp(v[i], "-M")) { | ||
| 124 | val = strtol(v[++i], NULL, 10); | ||
| 125 | if (val < 0 || val > 100) { | ||
| 126 | fprintf(stderr, | ||
| 127 | "Invalid max number of moves.\n"); | ||
| 128 | return a; | ||
| 129 | } | ||
| 130 | a->opts->max_moves = val; | ||
| 131 | } else if (!strcmp(v[i], "-s")) { | ||
| 132 | val = strtol(v[++i], NULL, 10); | ||
| 133 | if (val < 1 || val > 1000000) { | ||
| 134 | fprintf(stderr, | ||
| 135 | "Invalid number of solutions.\n"); | ||
| 136 | return a; | ||
| 137 | } | ||
| 138 | a->opts->max_solutions = val; | ||
| 139 | } else if (!strcmp(v[i], "-o")) { | ||
| 140 | a->opts->optimal_only = true; | ||
| 141 | } else if (!strcmp(v[i], "-n")) { | ||
| 142 | a->opts->can_niss = true; | ||
| 143 | } else if (!strcmp(v[i], "-v")) { | ||
| 144 | a->opts->feedback = true; | ||
| 145 | } else if (!strcmp(v[i], "-a")) { | ||
| 146 | a->opts->all = true; | ||
| 147 | } else if (!strcmp(v[i], "-p")) { | ||
| 148 | a->opts->print_number = false; | ||
| 149 | } else if (!read_step(a, v[i])) { | ||
| 150 | break; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | a->success = read_scramble(c-i, &v[i], a); | ||
| 155 | return a; | ||
| 156 | } | ||
| 157 | |||
| 158 | CommandArgs * | ||
| 159 | help_parse_args(int c, char **v) | ||
| 160 | { | ||
| 161 | int i; | ||
| 162 | CommandArgs *a = malloc(sizeof(CommandArgs)); | ||
| 163 | |||
| 164 | a->scramble = NULL; | ||
| 165 | a->opts = NULL; | ||
| 166 | a->step = NULL; | ||
| 167 | a->command = NULL; | ||
| 168 | |||
| 169 | if (c == 1) { | ||
| 170 | for (i = 0; i < NCOMMANDS; i++) | ||
| 171 | if (commands[i] != NULL && | ||
| 172 | !strcmp(v[0], commands[i]->name)) | ||
| 173 | a->command = commands[i]; | ||
| 174 | if (a->command == NULL) | ||
| 175 | fprintf(stderr, "%s: command not found\n", v[0]); | ||
| 176 | } | ||
| 177 | |||
| 178 | a->success = c == 0 || (c == 1 && a->command != NULL); | ||
| 179 | return a; | ||
| 180 | } | ||
| 181 | |||
| 182 | CommandArgs * | ||
| 183 | parse_no_arg(int c, char **v) | ||
| 184 | { | ||
| 185 | CommandArgs *a = malloc(sizeof(CommandArgs)); | ||
| 186 | |||
| 187 | a->scramble = NULL; | ||
| 188 | a->opts = NULL; | ||
| 189 | a->step = NULL; | ||
| 190 | a->command = NULL; | ||
| 191 | |||
| 192 | return a; | ||
| 193 | } | ||
| 194 | |||
| 195 | CommandArgs * | ||
| 196 | print_parse_args(int c, char **v) | ||
| 197 | { | ||
| 198 | CommandArgs *a = malloc(sizeof(CommandArgs)); | ||
| 199 | |||
| 200 | a->opts = NULL; | ||
| 201 | a->step = NULL; | ||
| 202 | a->command = NULL; | ||
| 203 | |||
| 204 | a->success = read_scramble(c-1, &v[1], a); | ||
| 205 | return a; | ||
| 206 | } | ||
| 207 | |||
| 208 | /* Exec functions implementation *********************************************/ | ||
| 209 | |||
| 210 | static void | ||
| 211 | solvestep_exec(CommandArgs *args) | ||
| 212 | { | ||
| 213 | Cube c = apply_alg(args->scramble, (Cube){0}); | ||
| 214 | AlgList *sols = solve(c, args->step, args->opts); | ||
| 215 | print_alglist(sols, args->opts->print_number); | ||
| 216 | free_alglist(sols); | ||
| 217 | } | ||
| 218 | |||
| 219 | static void | ||
| 220 | steps_exec(CommandArgs *args) | ||
| 221 | { | ||
| 222 | int i; | ||
| 223 | |||
| 224 | for (i = 0; i < NSTEPS && steps[i] != NULL; i++) | ||
| 225 | printf("%-15s %s\n", steps[i]->shortname, steps[i]->name); | ||
| 226 | } | ||
| 227 | |||
| 228 | static void | ||
| 229 | commands_exec(CommandArgs *args) | ||
| 230 | { | ||
| 231 | int i; | ||
| 232 | |||
| 233 | for (i = 0; i < NCOMMANDS && commands[i] != NULL; i++) | ||
| 234 | printf("%s\n", commands[i]->usage); | ||
| 235 | |||
| 236 | } | ||
| 237 | |||
| 238 | static void | ||
| 239 | print_exec(CommandArgs *args) | ||
| 240 | { | ||
| 241 | print_cube(apply_alg(args->scramble, (Cube){0})); | ||
| 242 | } | ||
| 243 | |||
| 244 | static void | ||
| 245 | help_exec(CommandArgs *args) | ||
| 246 | { | ||
| 247 | /* TODO: print full nissy manpage */ | ||
| 248 | if (args->command == NULL) { | ||
| 249 | printf("Type help COMMAND for information on a "); | ||
| 250 | printf("specific command.\n"); | ||
| 251 | printf("A more complete manual page is work in progress.\n"); | ||
| 252 | } else { | ||
| 253 | printf("Command %s: %s\nusage: %s\n", args->command->name, | ||
| 254 | args->command->description, args->command->usage); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | static void | ||
| 259 | quit_exec(CommandArgs *args) | ||
| 260 | { | ||
| 261 | exit(0); | ||
| 262 | } | ||
| 263 | |||
| 264 | /* Local functions implementation ********************************************/ | ||
| 265 | |||
| 266 | static bool | ||
| 267 | read_step(CommandArgs *args, char *str) | ||
| 268 | { | ||
| 269 | int i; | ||
| 270 | |||
| 271 | for (i = 0; i < NSTEPS; i++) { | ||
| 272 | if (steps[i] != NULL && !strcmp(steps[i]->shortname, str)) { | ||
| 273 | args->step = steps[i]; | ||
| 274 | return true; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | return false; | ||
| 279 | } | ||
| 280 | |||
| 281 | static bool | ||
| 282 | read_scramble(int c, char **v, CommandArgs *args) | ||
| 283 | { | ||
| 284 | int i, k, n; | ||
| 285 | unsigned int j; | ||
| 286 | char *algstr; | ||
| 287 | |||
| 288 | if (new_alg(v[0])->len == 0) { | ||
| 289 | fprintf(stderr, "%s: moves or option unrecognized\n", v[0]); | ||
| 290 | return false; | ||
| 291 | } | ||
| 292 | |||
| 293 | n = 0; | ||
| 294 | for(i = 0; i < c; i++) | ||
| 295 | n += strlen(v[i]); | ||
| 296 | |||
| 297 | algstr = malloc((n + 1) * sizeof(char)); | ||
| 298 | k = 0; | ||
| 299 | for (i = 0; i < c; i++) | ||
| 300 | for (j = 0; j < strlen(v[i]); j++) | ||
| 301 | algstr[k++] = v[i][j]; | ||
| 302 | algstr[k] = 0; | ||
| 303 | |||
| 304 | args->scramble = new_alg(algstr); | ||
| 305 | free(algstr); | ||
| 306 | |||
| 307 | if (args->scramble->len == 0) | ||
| 308 | fprintf(stderr, "Error reading scramble\n"); | ||
| 309 | |||
| 310 | return args->scramble->len > 0; | ||
| 311 | } | ||
| 312 | |||
| 313 | /* Public functions implementation *******************************************/ | ||
| 314 | |||
| 315 | void | ||
| 316 | free_args(CommandArgs *args) | ||
| 317 | { | ||
| 318 | if (args == NULL) | ||
| 319 | return; | ||
| 320 | |||
| 321 | if (args->scramble != NULL) | ||
| 322 | free_alg(args->scramble); | ||
| 323 | if (args->opts != NULL) | ||
| 324 | free(args->opts); | ||
| 325 | |||
| 326 | /* step and command must not be freed, they are static! */ | ||
| 327 | |||
| 328 | free(args); | ||
| 329 | } | ||
