Nico Huber | 589cea6 | 2023-02-11 18:01:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashprog project. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | |
| 19 | #include "flash.h" |
| 20 | #include "cli.h" |
| 21 | |
| 22 | static const char *const command_prefix = "flashprog-"; |
| 23 | |
| 24 | static const struct { |
| 25 | const char *name; |
| 26 | int (*main)(int argc, char *argv[]); |
| 27 | } commands[] = { |
| 28 | { "mem", flashprog_classic_main }, |
| 29 | { "memory", flashprog_classic_main }, |
Nico Huber | 706c37f | 2023-02-11 18:28:33 +0100 | [diff] [blame] | 30 | { "cfg", flashprog_config_main }, |
| 31 | { "config", flashprog_config_main }, |
Nico Huber | ab809be | 2023-02-11 18:28:33 +0100 | [diff] [blame] | 32 | { "wp", flashprog_wp_main }, |
| 33 | { "write-protect", flashprog_wp_main }, |
Nico Huber | 589cea6 | 2023-02-11 18:01:26 +0100 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | static void usage(const char *const name) |
| 37 | { |
| 38 | fprintf(stderr, "\nUsage: %s [<command>] [<argument>...]\n", name); |
| 39 | fprintf(stderr, "\nWhere <command> can be\n\n" |
| 40 | " mem[ory] Standard memory operations\n" |
| 41 | " (read/erase/write/verify)\n" |
Nico Huber | 706c37f | 2023-02-11 18:28:33 +0100 | [diff] [blame] | 42 | " cfg|config Status/config register operations\n" |
Nico Huber | ab809be | 2023-02-11 18:28:33 +0100 | [diff] [blame] | 43 | " wp|write-protect Write-protection operations\n" |
Nico Huber | 589cea6 | 2023-02-11 18:01:26 +0100 | [diff] [blame] | 44 | "\n" |
| 45 | "The default is 'memory'. See `%s <command> --help`\n" |
| 46 | "for further instructions.\n\n", name); |
| 47 | exit(1); |
| 48 | } |
| 49 | |
| 50 | static int combine_argv01(char *argv[]) |
| 51 | { |
| 52 | const size_t len = strlen(argv[0]) + 1 + strlen(argv[1]) + 1; |
| 53 | char *const argv0 = malloc(len); |
| 54 | if (!argv0) { |
| 55 | fprintf(stderr, "Out of memory!\n"); |
| 56 | return 1; |
| 57 | } |
| 58 | snprintf(argv0, len, "%s %s", argv[0], argv[1]); |
| 59 | argv[1] = argv0; |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | int main(int argc, char *argv[]) |
| 64 | { |
| 65 | const char *cmd; |
| 66 | size_t i; |
| 67 | |
| 68 | print_version(); |
| 69 | print_banner(); |
| 70 | |
| 71 | if (argc < 1) |
| 72 | usage("flashprog"); |
| 73 | |
| 74 | /* Turn something like `./flashprog-cmd` into `flashprog-cmd`: */ |
| 75 | const char *const slash = strrchr(argv[0], '/'); |
| 76 | if (slash) |
| 77 | cmd = slash + 1; |
| 78 | else |
| 79 | cmd = argv[0]; |
| 80 | |
| 81 | /* Turn `flashprog-cmd` into `cmd`: */ |
| 82 | if (!strncmp(cmd, command_prefix, strlen(command_prefix))) |
| 83 | cmd += strlen(command_prefix); |
| 84 | |
| 85 | /* Run `cmd` if found: */ |
| 86 | for (i = 0; i < ARRAY_SIZE(commands); ++i) { |
| 87 | if (!strcmp(cmd, commands[i].name)) |
| 88 | return commands[i].main(argc, argv); |
| 89 | } |
| 90 | |
| 91 | if (argc < 2) |
| 92 | usage(argv[0]); |
| 93 | |
| 94 | /* Try to find command as first argument in argv[1]: */ |
| 95 | for (i = 0; i < ARRAY_SIZE(commands); ++i) { |
| 96 | if (!strcmp(argv[1], commands[i].name)) { |
| 97 | /* Squash argv[0] into argv[1]: */ |
| 98 | if (combine_argv01(argv)) |
| 99 | return 1; |
| 100 | return commands[i].main(argc - 1, argv + 1); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /* We're still here? Fall back to classic cli: */ |
| 105 | return flashprog_classic_main(argc, argv); |
| 106 | } |