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