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