-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcligen_result.c
273 lines (240 loc) · 6.02 KB
/
cligen_result.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
***** BEGIN LICENSE BLOCK *****
Copyright (C) 2001-2022 Olof Hagsand
This file is part of CLIgen.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 or later (the "GPL"),
in which case the provisions of the GPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of the GPL, and not to allow others to
use your version of this file under the terms of Apache License version 2, indicate
your decision by deleting the provisions above and replace them with the
notice and other provisions required by the GPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the Apache License version 2 or the GPL.
***** END LICENSE BLOCK *****
* Result generated when matching and used inyternally in cligen
* This include file is used internally in cligen, not part of API
* The C struct is not exposed outside the .c file
*/
#include "cligen_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <assert.h>
#include "cligen_buf.h"
#include "cligen_cv.h"
#include "cligen_cvec.h"
#include "cligen_callback.h"
#include "cligen_object.h"
#include "cligen_parsetree.h"
#include "cligen_result.h"
/*! Result vector from match_pattern_* family of functions
*/
struct match_result{
parse_tree *mr_pt;
char *mr_reason; /* Error reason if mr_len=0. Can also be carried by a mr_len!=0
* to store first error in case it is needed in a later error */
int mr_level;
int mr_last;
char *mr_token; /* Direct, not copied */
cg_obj *mr_co_match_orig; /* Kludge, save (latest) matched object, see
mr_flags_set_co_match() */
};
int
mr_pt_len_get(match_result *mr)
{
if (mr->mr_pt == NULL)
return 0;
return pt_len_get(mr->mr_pt);
}
/*! Reset parse-tree vector, DONT free any co-objects
*/
int
mr_pt_reset(match_result *mr)
{
pt_free(mr->mr_pt, 0);
if ((mr->mr_pt = pt_new()) == NULL)
return -1;
return 0;
}
/*! Truncate parse-tree vector: free all extra elements, keep nr of heading elements
*
* the objects in the tail of the pt tree is freed.
* @param[in] mr
* @param[in] len >0
*/
int
mr_pt_trunc(match_result *mr,
int len)
{
return pt_trunc(mr->mr_pt, len);
}
/*! Copy co and append it to result parse-tree
*/
int
mr_pt_append(match_result *mr,
cg_obj *co,
char *token)
{
cg_obj *co1 = NULL;
if (co_copy1(co, NULL, 0, 0x0, &co1) < 0)
return -1;
mr->mr_co_match_orig = co;
mr->mr_token = token;
return pt_vec_append(mr->mr_pt, co1);
}
cg_obj*
mr_pt_i_get(match_result *mr,
int i)
{
return pt_vec_i_get(mr->mr_pt, i);
}
parse_tree*
mr_pt_get(match_result *mr)
{
return mr->mr_pt;
}
char*
mr_reason_get(match_result *mr)
{
return mr->mr_reason;
}
/*! Reset/empty matchvec of indexes by g and incrementing vector
*
* @param[in,out] mr Match result struct
* @param[in] reason Malloced string (consumed here)
*/
int
mr_reason_set(match_result *mr,
char *reason)
{
if (mr->mr_reason)
free(mr->mr_reason);
mr->mr_reason = reason;
return 0;
}
int
mr_level_get(match_result *mr)
{
return mr->mr_level;
}
int
mr_level_set(match_result *mr,
int level)
{
mr->mr_level = level;
return 0;
}
char *
mr_token_get(match_result *mr)
{
return mr->mr_token;
}
int
mr_last_get(match_result *mr)
{
return mr->mr_last;
}
int
mr_last_set(match_result *mr)
{
mr->mr_last = 1;
return 0;
}
/*! Move an error reason from one mr to the next
*
* There is a case for keeping the first error reason in case of multiple
*/
int
mr_mv_reason(match_result *from,
match_result *to)
{
char *reason;
if ((reason = from->mr_reason) != NULL &&
to->mr_reason == NULL){
to->mr_reason = reason;
from->mr_reason = NULL;
}
return 0;
}
/*! Create new CLIgen match result
*
* @see mr_free
*/
match_result *
mr_new(void)
{
match_result *mr;
if ((mr = malloc(sizeof(*mr))) == NULL)
return NULL;
memset(mr, 0, sizeof(*mr));
if ((mr->mr_pt = pt_new()) == NULL){
free(mr);
return NULL;
}
return mr;
}
/*! Free a return structure
*
* Dont free the parse tree mr_parsetree XXX why?
*/
int
mr_free(match_result *mr)
{
if (mr->mr_pt){
pt_free(mr->mr_pt, 0);
}
if (mr->mr_reason)
free(mr->mr_reason);
free(mr);
return 0;
}
/*! Map from match-result to cligen-result
*
* Could probably collapse the two concepts into one, but there is some history
*/
cligen_result
mr2result(match_result *mr)
{
switch (mr_pt_len_get(mr)){
case -1: /* shouldnt happen */
return CG_ERROR;
break;
case 0:
return CG_NOMATCH;
break;
case 1:
return CG_MATCH;
break;
default:
return CG_MULTIPLE;
break;
}
}
/*! Set CO_FLAGS_MATCH
*
* Kludge to mark both the copy and the most recently matched object
*/
int
mr_flags_set_co_match(match_result *mr,
cg_obj *co)
{
co_flags_set(mr->mr_co_match_orig, CO_FLAGS_MATCH);
co_flags_set(co, CO_FLAGS_MATCH);
return 0;
}