Nico Huber | a705043 | 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_prefixes[] = { "flashprog-", "flash", "f" }; |
| 23 | |
| 24 | static const struct { |
| 25 | const char *name; |
| 26 | int (*main)(int argc, char *argv[]); |
| 27 | } commands[] = { |
| 28 | { "prog", flashprog_classic_main }, |
Nico Huber | 1f693db | 2023-02-11 18:28:33 +0100 | [diff] [blame] | 29 | { "cfg", flashprog_config_main }, |
| 30 | { "config", flashprog_config_main }, |
Nico Huber | a705043 | 2023-02-11 18:01:26 +0100 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static void usage(const char *const name) |
| 34 | { |
| 35 | fprintf(stderr, "\nUsage: %s [<command>] [<argument>...]\n", name); |
| 36 | fprintf(stderr, "\nWhere <command> can be\n\n" |
| 37 | " prog Standard memory operations\n" |
| 38 | " (read/erase/write/verify)\n" |
Nico Huber | 1f693db | 2023-02-11 18:28:33 +0100 | [diff] [blame] | 39 | " cfg | config Status/config register operations\n" |
Nico Huber | a705043 | 2023-02-11 18:01:26 +0100 | [diff] [blame] | 40 | "\n" |
| 41 | "The default is 'prog'. See `%s <command> --help`\n" |
| 42 | "for further instructions.\n\n", name); |
| 43 | exit(1); |
| 44 | } |
| 45 | |
| 46 | static int combine_argv01(char *argv[]) |
| 47 | { |
| 48 | const size_t len = strlen(argv[0]) + 1 + strlen(argv[1]) + 1; |
| 49 | char *const argv0 = malloc(len); |
| 50 | if (!argv0) { |
| 51 | fprintf(stderr, "Out of memory!\n"); |
| 52 | return 1; |
| 53 | } |
| 54 | snprintf(argv0, len, "%s %s", argv[0], argv[1]); |
| 55 | argv[1] = argv0; |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | int main(int argc, char *argv[]) |
| 60 | { |
| 61 | const char *cmd; |
| 62 | size_t i; |
| 63 | |
| 64 | print_version(); |
| 65 | print_banner(); |
| 66 | |
| 67 | if (argc < 1) |
| 68 | usage("flashprog"); |
| 69 | |
| 70 | /* Turn something like `./flashprog-cmd` into `flashprog-cmd`: */ |
| 71 | const char *const slash = strrchr(argv[0], '/'); |
| 72 | if (slash) |
| 73 | cmd = slash + 1; |
| 74 | else |
| 75 | cmd = argv[0]; |
| 76 | |
| 77 | if (strcmp(cmd, "flashprog")) { |
| 78 | /* Strip command prefix, i.e. `flashprog-cmd` becomes `cmd`: */ |
| 79 | for (i = 0; i < ARRAY_SIZE(command_prefixes); ++i) { |
| 80 | if (!strncmp(cmd, command_prefixes[i], |
| 81 | strlen(command_prefixes[i]))) { |
| 82 | cmd += strlen(command_prefixes[i]); |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /* Run `cmd` if found: */ |
| 89 | for (i = 0; i < ARRAY_SIZE(commands); ++i) { |
| 90 | if (!strcmp(cmd, commands[i].name)) |
| 91 | return commands[i].main(argc, argv); |
| 92 | } |
| 93 | |
| 94 | /* Try to find command as first argument in argv[1]: */ |
| 95 | for (i = 0; argc >= 2 && 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 | /* Bail if first argument looks like an |
| 105 | unknown command (i.e. not starting with `-'): */ |
| 106 | if (argc >= 2 && argv[1][0] != '-') |
| 107 | usage(argv[0]); |
| 108 | |
| 109 | /* We're still here? Fall back to classic cli: */ |
| 110 | return flashprog_classic_main(argc, argv); |
| 111 | } |