blob: 6a0cbe1eeaa57471f1017be785c4deb454cce9f4 [file] [log] [blame]
Nico Huber589cea62023-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_prefix = "flashprog-";
23
24static 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 Huber706c37f2023-02-11 18:28:33 +010030 { "cfg", flashprog_config_main },
31 { "config", flashprog_config_main },
Nico Huber589cea62023-02-11 18:01:26 +010032};
33
34static void usage(const char *const name)
35{
36 fprintf(stderr, "\nUsage: %s [<command>] [<argument>...]\n", name);
37 fprintf(stderr, "\nWhere <command> can be\n\n"
38 " mem[ory] Standard memory operations\n"
39 " (read/erase/write/verify)\n"
Nico Huber706c37f2023-02-11 18:28:33 +010040 " cfg|config Status/config register operations\n"
Nico Huber589cea62023-02-11 18:01:26 +010041 "\n"
42 "The default is 'memory'. See `%s <command> --help`\n"
43 "for further instructions.\n\n", name);
44 exit(1);
45}
46
47static int combine_argv01(char *argv[])
48{
49 const size_t len = strlen(argv[0]) + 1 + strlen(argv[1]) + 1;
50 char *const argv0 = malloc(len);
51 if (!argv0) {
52 fprintf(stderr, "Out of memory!\n");
53 return 1;
54 }
55 snprintf(argv0, len, "%s %s", argv[0], argv[1]);
56 argv[1] = argv0;
57 return 0;
58}
59
60int main(int argc, char *argv[])
61{
62 const char *cmd;
63 size_t i;
64
65 print_version();
66 print_banner();
67
68 if (argc < 1)
69 usage("flashprog");
70
71 /* Turn something like `./flashprog-cmd` into `flashprog-cmd`: */
72 const char *const slash = strrchr(argv[0], '/');
73 if (slash)
74 cmd = slash + 1;
75 else
76 cmd = argv[0];
77
78 /* Turn `flashprog-cmd` into `cmd`: */
79 if (!strncmp(cmd, command_prefix, strlen(command_prefix)))
80 cmd += strlen(command_prefix);
81
82 /* Run `cmd` if found: */
83 for (i = 0; i < ARRAY_SIZE(commands); ++i) {
84 if (!strcmp(cmd, commands[i].name))
85 return commands[i].main(argc, argv);
86 }
87
88 if (argc < 2)
89 usage(argv[0]);
90
91 /* Try to find command as first argument in argv[1]: */
92 for (i = 0; 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 /* We're still here? Fall back to classic cli: */
102 return flashprog_classic_main(argc, argv);
103}