var_export_cmd.c
3.65 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-06-05 WillianChan first version
*/
#include <var_export.h>
static int ve_cmd_help(int argc, char **argv);
static int ve_find_module(int argc, char **argv);
static int ve_find_value(int argc, char **argv);
struct ve_cmd_des
{
const char *cmd;
int (*fun)(int argc, char **argv);
};
/* dcm cmd table */
static const struct ve_cmd_des cmd_tab[] =
{
{"module", ve_find_module},
{"value", ve_find_value},
};
static int ve_cmd_help(int argc, char **argv)
{
rt_kprintf("Usage:\n");
rt_kprintf("ve_find module <module> - Find by module name\n");
rt_kprintf("ve_find value <module> <identifier> - Find accurately\n");
return RT_EOK;
}
rt_inline void ve_object_split(int len)
{
while (len--) rt_kprintf("-");
}
static int ve_find_module(int argc, char **argv)
{
ve_iterator_t iter;
const ve_exporter_t *exporter;
ve_module_t module;
int maxlen = (RT_NAME_MAX * 2);
const char *item_title = "ve_module";
rt_kprintf("%-*.s identifier value\n", maxlen, item_title); ve_object_split(maxlen);
rt_kprintf(" ---------------- -----\n");
if (!ve_module_init(&module, argv[2]))
{
ve_iter_init(&module, &iter);
}
else
{
return -RT_ERROR;
}
while (1)
{
exporter = ve_iter_next(&iter);
if (exporter == RT_NULL)
{
return -RT_ERROR;
}
else
{
rt_kprintf("%-*.s %-*.s %d\n",
maxlen, exporter->module,
maxlen, exporter->identifier,
exporter->value);
}
}
}
static int ve_find_value(int argc, char **argv)
{
ve_iterator_t iter;
const ve_exporter_t *exporter;
ve_module_t module;
int maxlen = (RT_NAME_MAX * 2);
const char *item_title = "ve_module";
rt_kprintf("%-*.s identifier value\n", maxlen, item_title); ve_object_split(maxlen);
rt_kprintf(" ---------------- -----\n");
if (!ve_module_init(&module, argv[2]))
{
ve_iter_init(&module, &iter);
}
else
{
return -RT_ERROR;
}
while (1)
{
exporter = ve_iter_next(&iter);
if (exporter == RT_NULL)
{
return -RT_ERROR;
}
else
{
if (!rt_strcmp(exporter->identifier, argv[3]))
{
rt_kprintf("%-*.s %-*.s %d\n",
maxlen, exporter->module,
maxlen, exporter->identifier,
exporter->value);
return RT_EOK;
}
}
}
}
static int ve_find(int argc, char **argv)
{
int i, resule = RT_EOK;
const struct ve_cmd_des *run_cmd = RT_NULL;
if (argc == 1)
{
ve_cmd_help(argc, argv);
return RT_EOK;
}
/* find command function */
for (i = 0; i < sizeof(cmd_tab) / sizeof(cmd_tab[0]); i++)
{
if (rt_strcmp(cmd_tab[i].cmd, argv[1]) == 0)
{
run_cmd = &cmd_tab[i];
break;
}
}
/* not find command function, print help information */
if (run_cmd == RT_NULL)
{
rt_kprintf("There is no command option named %s.\n", argv[1]);
ve_cmd_help(argc, argv);
return RT_EOK;
}
/* run command function */
if (run_cmd->fun != RT_NULL)
{
resule = run_cmd->fun(argc, argv);
}
if (resule)
{
ve_cmd_help(argc, argv);
}
return RT_EOK;
}
MSH_CMD_EXPORT(ve_find, find the specified export variable);