-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcommand_parser.cpp
More file actions
152 lines (134 loc) · 3.25 KB
/
command_parser.cpp
File metadata and controls
152 lines (134 loc) · 3.25 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
// SPDX-License-Identifier: Apache-2.0
#include <command_parser.h>
#include <string>
#include <cstring>
#include <ykush_help.h>
#include <cstdlib>
#include <iostream>
CommandLine::CommandLine()
{
command.options = NULL;
}
CommandLine::~CommandLine()
{
//free command line structures
struct command_option *cur_opt = command.options;
while (cur_opt) {
struct command_option *next_opt = cur_opt->next;
free(cur_opt->name);
struct command_parameter *cur_param = cur_opt->parameters;
while (cur_param) {
struct command_parameter *next_param = cur_param->next;
free(cur_param->value);
free(cur_param);
cur_param = next_param;
}
free(cur_opt);
cur_opt = next_opt;
}
}
int CommandLine::parse(int argc, char **argv)
{
command.app_name = argv[0];
if (argc < 2)
return 1;
int i = 1;
struct command_option *cur_opt = NULL;
struct command_option *root_opt = NULL;
bool exit_while;
while (i < argc) {
exit_while = false;
if (std::strlen(argv[i]) > 1) {
if ((i == 1) && (argv[1][0] != '-')) {
/* Board name */
struct command_option *tmp_opt;
tmp_opt = (struct command_option *)
std::calloc(1,
sizeof(struct command_option));
if (cur_opt) {
cur_opt->next = tmp_opt;
cur_opt = cur_opt->next;
} else {
root_opt = tmp_opt;
cur_opt = root_opt;
}
tmp_opt->name = (char *)
std::calloc(std::strlen(argv[i]),
sizeof(char));
std::memcpy(tmp_opt->name,
argv[i],
std::strlen(argv[i]));
}
if (argv[i][0] == '-') {
/* Add new option to command.options. */
struct command_option *tmp_opt;
tmp_opt = (struct command_option *)
std::calloc(1,
sizeof(struct command_option));
if (cur_opt) {
cur_opt->next = tmp_opt;
cur_opt = cur_opt->next;
} else {
root_opt = tmp_opt;
cur_opt = root_opt;
}
tmp_opt->name = (char *)
std::calloc(std::strlen(argv[i]),
sizeof(char));
std::memcpy(tmp_opt->name,
argv[i],
std::strlen(argv[i]));
/* Get option parameters, if they exist. */
struct command_parameter *cur_param = NULL;
struct command_parameter *root_param = NULL;
while ((i < (argc - 1)) && !exit_while) {
i++;
if (argv[i][0] == '-') {
i--;
exit_while = true;
} else {
/* Parameter. */
//add parameter to option
struct command_parameter *tmp_param;
tmp_param = (struct command_parameter *)
std::calloc(1,
sizeof(struct command_parameter));
if (cur_param) {
cur_param->next = tmp_param;
cur_param = cur_param->next;
} else {
root_param = tmp_param;
cur_param = root_param;
}
cur_param->value = (char *)
std::calloc(std::strlen(argv[i]),
sizeof(char));
std::memcpy(cur_param->value,
argv[i],
std::strlen(argv[i]));
}
}
cur_opt->parameters = root_param;
}
}
i++;
}
command.options = root_opt;
return 0;
}
/*
* Returns 1 if board_name is in the options,
* and 0 if not.
*/
int CommandLine::is_board(std::string board_name)
{
int i = 0;
struct command_option *cur_opt;
cur_opt = command.options;
while (cur_opt) {
if (board_name.compare(cur_opt->name) == 0)
return 1;
cur_opt = cur_opt->next;
}
return 0;
}