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