-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcligen_parsetree.c
651 lines (606 loc) · 15.8 KB
/
cligen_parsetree.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
***** 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 *****
* Notes:
* - The main prefix is "pt_" in this API, but a few have "cligen_parsetree_" pt_free
* have both, consider changing to "cligen_pt_" ?
* - There is some inconsistency wheen freeing sub-objects in this API.
*/
#include "cligen_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#ifdef HAVE_STRVERSCMP
#define _GNU_SOURCE
#define __USE_GNU
#include <string.h>
#undef _GNU_SOURCE
#undef __USE_GNU
#else /* HAVE_STRVERSCMP */
#include <string.h>
#endif /* HAVE_STRVERSCMP */
#include <errno.h>
#include "cligen_buf.h"
#include "cligen_cv.h"
#include "cligen_cvec.h"
#include "cligen_parsetree.h"
#include "cligen_pt_head.h"
#include "cligen_callback.h"
#include "cligen_object.h"
#include "cligen_io.h"
#include "cligen_result.h"
#include "cligen_read.h"
#include "cligen_parse.h"
#include "cligen_handle.h"
#include "cligen_getline.h"
/* Private definition of parsetree. Public is defined in cligen_parsetree.h
*
* @see parse_tree_list which is the upper level of a parse-tree
*/
struct parse_tree{
struct cg_obj **pt_vec; /* vector of pointers to parse-tree nodes */
unsigned int pt_len; /* length of vector */
char pt_set; /* Parse-tree is a SET */
};
static int
pt_stats_one(parse_tree *pt,
size_t *szp)
{
size_t sz = 0;
sz += sizeof(struct parse_tree);
sz += pt->pt_len*sizeof(struct cg_obj*);
if (szp)
*szp = sz;
return 0;
}
/*! Return statistics of a CLIgen objects of this parsetree recursively
*
* @param[in] pt Parsetree object
* @param[out] nrp Number of CLIgen objects recursively
* @param[out] szp Size of this pt + objects recursively
* @retval 0 OK
* @retval -1 Error
*/
int
pt_stats(parse_tree *pt,
uint64_t *nrp,
size_t *szp)
{
cg_obj *co;
size_t sz = 0;
int i;
*nrp += 1;
pt_stats_one(pt, &sz);
if (szp)
*szp += sz;
for (i=0; i<pt_len_get(pt); i++){
if ((co = pt_vec_i_get(pt, i)) != NULL)
co_stats(co, nrp, szp);
}
return 0;
}
/*! Access function to get the i:th CLIgen object child of a parse-tree
*
* @param[in] pt Parse tree
* @param[in] i Which object to return
*/
cg_obj *
pt_vec_i_get(parse_tree *pt,
int i)
{
struct cg_obj **ptvec;
if (pt == NULL || i<0 || i>pt->pt_len){
errno = EINVAL;
return NULL;
}
if (pt->pt_vec != NULL){
ptvec = pt->pt_vec;
return ptvec[i];
}
else
return NULL;
}
/*! Clear the i:th CLIgen object child of a parse-tree (without freeing existing)
*
* @param[in] pt Parse tree
* @param[in] i Which object to return
*/
int
pt_vec_i_clear(parse_tree *pt,
int i)
{
if (pt == NULL){
errno = EINVAL;
return -1;
}
if (i<0 || i >= pt_len_get(pt)){
errno = EINVAL;
return -1;
}
if (pt->pt_vec == NULL){
errno = EFAULT;
return -1;
}
pt->pt_vec[i] = NULL;
return 0;
}
/*! Insert the i:th CLIgen object child of a parse-tree
*
* @param[in] pt Parse tree
* @param[in] i Which position to insert
* @param[in] co Object to insert (can be NULL)
*/
int
pt_vec_i_insert(parse_tree *pt,
int i,
cg_obj *co)
{
int retval = -1;
size_t size;
if (pt == NULL){
errno = EINVAL;
goto done;
}
if (i<0 || i > pt_len_get(pt)){
errno = EINVAL;
return -1;
}
if (pt_realloc(pt) < 0)
goto done;
if ((size = (pt_len_get(pt) - (i+1))*sizeof(cg_obj*)) != 0)
memmove(&pt->pt_vec[i+1],
&pt->pt_vec[i],
size);
pt->pt_vec[i] = co;
retval = 0;
done:
return retval;
}
int
pt_vec_append(parse_tree *pt,
cg_obj *co)
{
return pt_vec_i_insert(pt, pt_len_get(pt), co);
}
/*! Remove and delete a single cligen object from a parsetree
*/
int
pt_vec_i_delete(parse_tree *pt,
int i,
int recurse)
{
int retval = -1;
size_t size;
cg_obj *co;
if (pt == NULL){
errno = EINVAL;
goto done;
}
if (i < 0 || i >= pt_len_get(pt)){
errno = EINVAL;
goto done;
}
if (pt->pt_vec == NULL){
errno = EFAULT;
goto done;
}
co = pt->pt_vec[i];
pt->pt_vec[i] = NULL;
co_free(co, recurse);
if ((size = (pt_len_get(pt) - (i+1))*sizeof(cg_obj*)) != 0)
memmove(&pt->pt_vec[i],
&pt->pt_vec[i+1],
size);
pt->pt_len--;
retval = 0;
done:
return retval;
}
/*! Access function to get a CLIgen objects child tree vector
*
* @param[in] co CLIgen parse object
*/
unsigned int
pt_len_get(parse_tree *pt)
{
if (pt == NULL){
errno = EINVAL;
return -1;
}
return pt->pt_len;
}
int
pt_sets_get(parse_tree *pt)
{
if (pt == NULL){
errno = EINVAL;
return -1;
}
return pt->pt_set;
}
int
pt_sets_set(parse_tree *pt,
int sets)
{
if (pt == NULL){
errno = EINVAL;
return -1;
}
pt->pt_set = sets;
return 0;
}
/*! Allocate a new parsetree
*
* @see pt_free
*/
parse_tree *
pt_new(void)
{
parse_tree *pt = NULL;
if ((pt = malloc(sizeof(parse_tree))) == NULL)
return NULL;
memset(pt, 0, sizeof(parse_tree));
return pt;
}
/*! Enlarge the child-vector (pattern) of a parse-tree
*
* @param[in] pt Cligen object vector
* @retval 0 OK
* @retval -1 Error
* Suppose we have a pattern pt, with lists of cg_obj's 1..4:
* pt -> .-.-.-.
* | | | |
* v v v v
* 1 2 3 4
* and a new cg_obj 5,
* then reallocate pt for another list of cg_objs and copy it into the structure:
* pt -> .-.-.-.-.
* | | | | |
* v v v v v
* 1 2 3 4 5
*/
int
pt_realloc(parse_tree *pt)
{
pt->pt_len++;
/* Allocate larger cg_obj vector */
if ((pt->pt_vec = realloc(pt->pt_vec, (pt->pt_len)*sizeof(cg_obj *))) == 0)
return -1;
pt->pt_vec[pt->pt_len - 1] = NULL; /* init field */
return 0;
}
/*! Recursively copy a parse-tree.
*
* No common pointers between the two structures
* @param[in] pt Original parse-tree
* @param[in] parent The parent of the new parsetree. Need not be same as parent of the orignal
* @param[in] flags Copy flags
* @param[out] ptn New parse-tree (need to be already created on entry)
* @retval 0 OK
* @retval -1 Error
* @see pt_dup
*/
int
pt_copy(parse_tree *pt,
cg_obj *co_parent,
uint32_t flags,
parse_tree *ptn)
{
int retval = -1;
int i;
int j;
cg_obj *co;
if (pt == NULL || ptn == NULL){
errno = EINVAL;
goto done;
}
pt_sets_set(ptn, pt_sets_get(pt));
/* subtract tree-references, which are instances of other trees */
for (i=0; i<pt_len_get(pt); i++){
if ((co = pt_vec_i_get(pt,i)) && co_flags_get(co, CO_FLAGS_TOPOFTREE))
;
else
ptn->pt_len++;
}
if (pt_len_get(ptn) &&
(ptn->pt_vec = (cg_obj **)malloc(pt_len_get(ptn)*sizeof(cg_obj *))) == NULL){
fprintf(stderr, "%s: malloc: %s\n", __FUNCTION__, strerror(errno));
goto done;
}
j=0;
for (i=0; i<pt_len_get(pt); i++){
if ((co = pt_vec_i_get(pt, i)) != NULL){
if (!co_flags_get(co, CO_FLAGS_TOPOFTREE))
if (co_copy(co, co_parent, flags, &ptn->pt_vec[j++]) < 0)
goto done;
}
else
ptn->pt_vec[j++] = NULL;
}
retval = 0;
done:
return retval;
}
/*! Duplicate a parse-tree recursively
*
* @param[in] pt Original parse-tree
* @param[in] flags Copy flags
* @param[in] parent The parent of the new parsetree. Need not be same as parent of the orignal
* @retval ptnp New parse-tree
* @retval NULL Error
*/
parse_tree *
pt_dup(parse_tree *pt,
cg_obj *cop,
uint32_t flags)
{
parse_tree *ptn = NULL;
if (pt == NULL){
errno = EINVAL;
goto done;
}
if ((ptn = pt_new()) == NULL)
goto done;
if (pt_copy(pt, cop, flags, ptn) < 0){
free(ptn);
ptn = NULL;
goto done;
}
done:
return ptn;
}
/*! Recursively merge two parse-trees: pt1 into pt0
*
* @param[in,out] pt0 parse-tree 0. On exit contains pt1 too
* @param[in] parent Parent of pt0
* @param[in] pt1 parse-tree 1. Merge this into pt0
* @retval 0 OK
* @retval -1 Error
*/
int
cligen_parsetree_merge(parse_tree *pt0,
cg_obj *parent,
parse_tree *pt1)
{
cg_obj *co0=NULL;
cg_obj *co1;
cg_obj *co1c;
int i;
int j;
int retval = -1;
int exist;
for (j=0; j<pt_len_get(pt1); j++){
co1 = pt_vec_i_get(pt1, j);
exist = 0;
for (i=0; i<pt_len_get(pt0); i++){
co0 = pt_vec_i_get(pt0, i);
if (co0 == NULL && co1 == NULL){
exist = 1;
break;
}
if (co0 && co1 && co_eq(co0, co1)==0){
if (co0->co_callbacks == NULL && co1->co_callbacks != NULL){
/* Cornercase: co0 callback is NULL and co1 callback is not
* Copy from co1 to co0
*/
if (co_callback_copy(co1->co_callbacks, &co0->co_callbacks) < 0)
goto done;
}
exist = 1;
break;
}
}
if (co1==NULL){ /* empty */
if (exist)
continue;
if (pt_realloc(pt0) < 0)
goto done;
pt0->pt_vec[pt_len_get(pt0)-1] = NULL;
continue;
}
if (exist){
if (cligen_parsetree_merge(co_pt_get(co0), co0, co_pt_get(co1)) < 0)
goto done;
}
else{
if (pt_realloc(pt0) < 0)
goto done;
if (co_copy(co1, parent, 0x0, &co1c) < 0)
goto done;
pt0->pt_vec[pt_len_get(pt0)-1] = co1c;
}
}
cligen_parsetree_sort(pt0, 0);
retval = 0;
done:
return retval;
}
/*! Help function to qsort for sorting entries in pattern file.
*
* @param[in] arg1
* @param[in] arg2
* @retval 0 If equal
* @retval <0 If arg1 is less than arg2
* @retval >0 If arg1 is greater than arg2
*/
static int
co_cmp(const void* arg1,
const void* arg2)
{
cg_obj* co1 = *(cg_obj**)arg1;
cg_obj* co2 = *(cg_obj**)arg2;
if (co1 == NULL){
if (co2 == NULL)
return 0;
else
return -1;
}
else if (co2 == NULL)
return 1;
else
return co_eq(co1, co2);
}
/*! Sort CLIgen parse-tree, optionally recursive
*
* @param[in] The CLIgen parse-tree
* @param[in] recursive. If set sort recursive calls
* @retval void
*/
void
cligen_parsetree_sort(parse_tree *pt,
int recursive)
{
cg_obj *co;
int i;
parse_tree *pt1;
qsort(pt->pt_vec, pt_len_get(pt), sizeof(cg_obj*), co_cmp);
for (i=0; i<pt_len_get(pt); i++){
if ((co = pt_vec_i_get(pt, i)) == NULL)
continue;
if (co_flags_get(co, CO_FLAGS_MARK) == 0){ /* not recursive */
co_flags_set(co, CO_FLAGS_MARK);
pt1 = co_pt_get(co);
if (pt1 && recursive){
cligen_parsetree_sort(pt1, 1);
}
co_flags_reset(co, CO_FLAGS_MARK);
}
}
}
/*! Free all parse-tree nodes of the parse-tree,
*
* @param[in] pt CLIgen parse-tree
* @param[in] recursive If 0 free pt and objects only, if 1 free recursive
* @retval 0 OK
* @retval -1 Error
*/
int
pt_free(parse_tree *pt,
int recursive)
{
int i;
cg_obj *co;
if (pt == NULL){
errno = EINVAL;
return -1;
}
if (pt->pt_vec != NULL){
for (i=0; i<pt_len_get(pt); i++)
if ((co = pt_vec_i_get(pt, i)) != NULL)
co_free(co, recursive);
free(pt->pt_vec);
}
pt->pt_len = 0;
free(pt);
return 0;
}
int
cligen_parsetree_free(parse_tree *pt,
int recursive)
{
return pt_free(pt, recursive);
}
/*! Trunc parse-tree to specific length
*
* Keep "len" objects, free the rest and reallocate the vector
* @param[in] pt CLIgen parse-tree
* @retval 0 OK
* @retval -1 Error
*/
int
pt_trunc(parse_tree *pt,
int len)
{
int i;
cg_obj *co;
if (pt == NULL){
errno = EINVAL;
return -1;
}
if (len == 0 || len > pt->pt_len){
errno = EINVAL;
return -1;
}
if (len < pt->pt_len){
for (i=len; i<pt_len_get(pt); i++){
if ((co = pt_vec_i_get(pt, i)) != NULL)
co_free(co, 0);
}
if ((pt->pt_vec = realloc(pt->pt_vec, (len)*sizeof(cg_obj *))) == 0)
return -1;
pt->pt_len = len;
}
return 0;
}
/*! Apply a function call recursively on all cg_obj:s in a parse-tree
*
* Recursively traverse all cg_obj in a parse-tree and apply fn(arg) for each
* object found. The function is called with the cg_obj and an argument as args.
* @param[in] pt CLIgen parse-tree
* @param[in] fn Function to apply
* @param[in] depth 0: only this level, n : n levels
* @param[in] arg Argument to function
* @retval 0 OK (all applied function calls return 0)
* @retval -1 Error (one applied function call return -1)
* @code
* parse_tree *pt;
* if (pt_apply(pt, fn, INT32_MAX, (void*)42) < 0)
* err;
* @endcode
*/
int
pt_apply(parse_tree *pt,
cg_applyfn_t fn,
int depth,
void *arg)
{
cg_obj *co;
int i;
int retval = -1;
int ret;
if (pt->pt_vec == NULL)
return 0;
if (depth)
for (i=0; i<pt_len_get(pt); i++){
if ((co = pt_vec_i_get(pt, i)) == NULL)
continue;
if ((ret = fn(co, arg)) < 0)
goto done;
if (ret == 1) /* done */
break;
if (pt_apply(co_pt_get(co), fn, depth-1, arg) < 0)
goto done;
}
retval = 0;
done:
return retval;
}