-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
98 lines (80 loc) · 1.61 KB
/
main.c
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
/*
* Copyright (C) 2016 Masahiro Yamada <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cmd.h"
#include "errno.h"
static void show_help(void);
static int do_help(int argc, char *argv[])
{
show_help();
return 0;
}
struct command cmd_help = {
.name = "help",
.help = "show help",
.long_help = "help [<command>]",
.func = do_help,
};
static struct command *cmd_table[] = {
&cmd_help,
&cmd_i2c,
&cmd_mm,
&cmd_mw,
&cmd_md,
&cmd_gettimeofday,
NULL,
};
static void show_help(void)
{
struct command **cmd;
printf("Usage: %s function [arguments...]\n"
" or: function [arguments...]\n"
"\n"
"Currently defined functions:\n",
BIN_NAME);
for (cmd = cmd_table; *cmd; cmd++)
printf(" %s\t\t%s\n", (*cmd)->name, (*cmd)->help);
}
int main(int argc, char *argv[])
{
struct command **cmd;
char *cmdname, *s;
size_t cmdlen;
int ret;
s = strrchr(argv[0], '/');
if (s)
argv[0] = s + 1;
if (!strcmp(argv[0], BIN_NAME)) {
argc--;
argv++;
}
if (argc < 1)
goto help;
cmdname = argv[0];
s = strchr(cmdname, '.');
if (s) {
cmdlen = s - cmdname;
argv[0] = s + 1; /* sub-command */
} else {
cmdlen = strlen(cmdname);
argv[0] = cmdname + cmdlen;
}
for (cmd = cmd_table; *cmd; cmd++) {
if (!strncmp((*cmd)->name, cmdname, cmdlen)) {
ret = (*cmd)->func(argc, argv);
if (ret == -EINVAL) {
printf("Usage:\n %s\n", (*cmd)->long_help);
}
exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
}
}
printf("%s: applet not found\n", cmdname);
help:
show_help();
exit(EXIT_FAILURE);
}