-
Notifications
You must be signed in to change notification settings - Fork 37
/
cligen_object.c
1262 lines (1193 loc) · 33.2 KB
/
cligen_object.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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
***** 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 *****
*/
#include "cligen_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <inttypes.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.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"
/* Stats: nr of created cligen objects */
uint64_t _co_created = 0;
uint64_t _co_count = 0;
/*! Return number of created and existing cligen objects
*
* @param[out] created Number of created CLIgen objects (ever)
* @param[out] nr Number of existing CLIgen objects (created - freed)
*/
int
co_stats_global(uint64_t *created,
uint64_t *nr)
{
*created = _co_created;
*nr = _co_count;
return 0;
}
/*! Return the alloced memory of a single CLIgen object
*
* @param[in] y YANG object
* @param[out] szp Size of this YANG obj
* @retval 0 OK
* (baseline: )
*/
static int
co_stats_one(cg_obj *co,
size_t *szp)
{
size_t sz = 0;
struct cg_callback *cc;
struct cg_varspec *cgs;
sz += sizeof(struct cg_obj);
sz += co->co_pt_len*sizeof(struct parse_tree*);
if (co->co_command)
sz += strlen(co->co_command) + 1;
if (co->co_prefix)
sz += strlen(co->co_prefix) + 1;
for (cc = co->co_callbacks; cc; cc=cc->cc_next)
sz += co_callback_size(cc);
if (co->co_cvec)
sz += cvec_size(co->co_cvec);
if (co->co_filter)
sz += cvec_size(co->co_filter);
if (co->co_helpstring)
sz += strlen(co->co_helpstring) + 1;
/* XXX union */
if (co->co_type == CO_VARIABLE){
cgs = &co->u.cou_var;
if (cgs->cgs_show)
sz += strlen(cgs->cgs_show) + 1;
if (cgs->cgs_expand_fn_str)
sz += strlen(cgs->cgs_expand_fn_str) + 1;
if (cgs->cgs_expand_fn_vec)
sz += cvec_size(cgs->cgs_expand_fn_vec);
if (cgs->cgs_translate_fn_str)
sz += strlen(cgs->cgs_translate_fn_str) + 1;
if (cgs->cgs_choice)
sz += strlen(cgs->cgs_choice) + 1;
if (cgs->cgs_rangecvv_low)
sz += cvec_size(cgs->cgs_rangecvv_low);
if (cgs->cgs_rangecvv_upp)
sz += cvec_size(cgs->cgs_rangecvv_upp);
if (cgs->cgs_regex)
sz += cvec_size(cgs->cgs_regex);
}
if (szp)
*szp = sz;
return 0;
}
/*! Return statistics of a CLIgen object recursively
*
* @param[in] co CLIgen object
* @param[out] szp Size of this co recursively
* @retval 0 OK
* @retval -1 Error
*/
int
co_stats(cg_obj *co,
uint64_t *nrp,
size_t *szp)
{
int retval = -1;
size_t sz = 0;
parse_tree *pt;
int i;
if (co == NULL){
errno = EINVAL;
goto done;
}
*nrp += 1;
co_stats_one(co, &sz);
if (szp)
*szp += sz;
for (i=0; i<co->co_pt_len; i++){
if ((pt = co->co_ptvec[i]) != NULL){
pt_stats(pt, nrp, szp);
}
}
retval = 0;
done:
return retval;
}
/* Access macro */
cg_obj*
co_up(cg_obj *co)
{
return co->co_prev;
}
int
co_up_set(cg_obj *co,
cg_obj *cop)
{
co->co_prev = cop;
return 0;
}
/*! Return top-of-tree (ancestor)
*/
cg_obj*
co_top(cg_obj *co0)
{
cg_obj *co = co0;
cg_obj *co1;
while ((co1 = co_up(co)) != NULL)
co = co1;
return co;
}
static int
co_pt_realloc(cg_obj *co)
{
int retval = -1;
if (co->co_pt_len == 0){
co->co_pt_len++;
// if ((co->co_ptvec = realloc(co->co_ptvec, (co->co_pt_len)*sizeof(parse_tree *))) == 0)
if ((co->co_ptvec = calloc(co->co_pt_len, sizeof(parse_tree *))) == 0)
goto done;
}
retval = 0;
done:
return retval;
}
/*! Access function to get a CLIgen objects parse-tree head
*
* @param[in] co CLIgen parse object
* @retval pt parse-tree
* @retval NULL Error or no such parsetree
*/
parse_tree *
co_pt_get(cg_obj *co)
{
if (co == NULL){
errno = EINVAL;
return NULL;
}
return co->co_ptvec?co->co_ptvec[0]:NULL;
}
/*! Set a CLIgen objects parse-tree head
*
* @param[in] co CLIgen parse object
* @param[in] pt CLIgen parse tree
* @retval 0 OK
* @retval -1 Error
*/
int
co_pt_set(cg_obj *co,
parse_tree *pt)
{
if (co == NULL){
errno = EINVAL;
return -1;
}
if (co->co_pt_len == 0){
if (co_pt_realloc(co) < 0)
return -1;
}
else {
if (co->co_ptvec[0])
pt_free(co->co_ptvec[0], 1);
}
co->co_ptvec[0] = pt;
return 0;
}
/*! Clear a CLIgen objects parse-tree head (dont free old)
*
* @param[in] co CLIgen parse object
* @param[in] pt CLIgen parse tree
* @retval 0 OK
* @retval -1 Error
* @see co_pt_set
*/
int
co_pt_clear(cg_obj *co)
{
if (co == NULL){
errno = EINVAL;
return -1;
}
if (co->co_pt_len == 0){
if (co_pt_realloc(co) < 0)
return -1;
}
co->co_ptvec[0] = NULL;
return 0;
}
void
co_flags_set(cg_obj *co,
uint32_t flag)
{
co->co_flags |= flag;
}
void
co_flags_reset(cg_obj *co,
uint32_t flag)
{
co->co_flags &= ~flag;
}
int
co_flags_get(cg_obj *co,
uint32_t flag)
{
return (co->co_flags & flag) ? 1 : 0;
}
int
co_sets_get(cg_obj *co)
{
parse_tree *pt;
if ((pt = co_pt_get(co)) != NULL)
return pt_sets_get(pt);
else
return 0;
}
void
co_sets_set(cg_obj *co,
int sets)
{
parse_tree *pt;
if ((pt = co_pt_get(co)) != NULL)
pt_sets_set(pt, sets);
}
char*
co_prefix_get(cg_obj *co)
{
return co->co_prefix;
}
int
co_prefix_set(cg_obj *co,
char *prefix)
{
if (co->co_prefix != NULL){
free(co->co_prefix);
co->co_prefix = NULL;
}
if (prefix &&
(co->co_prefix = strdup(prefix)) == NULL)
return -1;
return 0;
}
/*! Add new filter from cvv, copy
*/
cvec *
co_filter_set(cg_obj *co,
cvec *cvv)
{
co->co_filter = cvec_dup(cvv);
return co->co_filter;
}
/*! Assign a preference to a cligen variable object
*
* Prefer more specific commands/variables if you have to choose from several.
* @param[in] co Cligen obe
* @retval pref Preference: positive integer
* The preference is (higher more preferred):
* command / keyword
* (ip|mac)
* decimal64
* int8 with range , uint8 with range
* ...
* int64 with range , uint64 with range
* int8 , uint8
* ...
* int64 , uint64
* interface
* regexp
* string
* rest
* Note in a choice: <int32>|<uint16>, uint16 is preferred.
* XXX: It does not cover: <int32 range:0-12>|<int32 range:6-18>
*/
static int
cov_pref(cg_obj *co)
{
int pref = 0;
switch (co->co_vtype){
case CGV_ERR:
pref = 0; /* Illegal */
break;
/* ints in range 22-60 */
case CGV_INT8:
if (co->co_rangelen)
pref = 60;
else
pref = 52;
break;
case CGV_INT16:
if (co->co_rangelen)
pref = 58;
else
pref = 50;
break;
case CGV_INT32:
if (co->co_rangelen)
pref = 56;
else
pref = 48;
break;
case CGV_INT64:
if (co->co_rangelen)
pref = 54;
else
pref = 46;
break;
case CGV_UINT8:
if (co->co_rangelen)
pref = 59;
else
pref = 51;
break;
case CGV_UINT16:
if (co->co_rangelen)
pref = 57;
else
pref = 49;
break;
case CGV_UINT32:
if (co->co_rangelen)
pref = 55;
else
pref = 47;
break;
case CGV_UINT64:
if (co->co_rangelen)
pref = 53;
else
pref = 45;
break;
case CGV_DEC64:
pref = 62;
break;
case CGV_BOOL:
pref = 12;
break;
case CGV_REST:
pref = 1;
break;
case CGV_STRING:
if (co->co_expand_fn_str != NULL)
pref = 8;
else if (co->co_regex)
pref = 7;
else
pref = 5;
break;
case CGV_INTERFACE:
pref = 10;
break;
case CGV_IPV4ADDR:
case CGV_IPV4PFX:
pref = 70;
break;
case CGV_IPV6ADDR:
case CGV_IPV6PFX:
pref = 71;
break;
case CGV_MACADDR:
pref = 72;
break;
case CGV_URL:
pref = 20;
break;
case CGV_UUID:
pref = 73;
break;
case CGV_TIME:
pref = 74;
break;
case CGV_VOID: /* N/A */
break;
case CGV_EMPTY:
break;
}
return pref;
}
/*! Assign a preference to a cligen object
*
* @param[in] co cligen_object
* @param[in] exact If this match is exact (only applies to CO_COMMAND)
* @retval pref Preference: positive integer
*
* The higher the better
* if you have to choose from several.
* The preference is:
* command > ip|mac > int > interface > string > expand > rest
* 'expand' is a command with not exact match that is derived from a <expand> or <choice>
*/
int
co_pref(cg_obj *co,
int exact)
{
int pref = 0;;
if (co->co_preference > 0)
pref = co->co_preference;
else
switch (co->co_type){
case CO_COMMAND:
/* Give full preference to exact command match, low to partial (prefix) command match */
if (exact == 0)
pref = 3;
else
pref = 100;
break;
case CO_VARIABLE:
pref = cov_pref(co);
break;
case CO_REFERENCE:
case CO_EMPTY:
break;
}
return pref;
}
/*! Return size of cligen object
*/
size_t
co_size(enum cg_objtype type)
{
size_t size;
if (type == CO_VARIABLE) // reference since it may be replaced
size = sizeof(cg_obj);
else
size = sizeof(struct cg_obj_common);
return size;
}
/*! Malloc a CLIgen object.
*
* @param[in] type Type of cligen object
* @retval co OK
* @retval NULL Error
*/
cg_obj *
co_new_only(enum cg_objtype type)
{
cg_obj *co;
size_t size;
size = co_size(type);
if ((co = malloc(size)) == NULL)
return NULL;
memset(co, 0, size);
co->co_type = type;
_co_count++;
_co_created++;
return co;
}
/*! Create new cligen parse-tree command object
*
* That is, a cligen parse-tree object with type == CO_COMMAND (not variable)
* @param[in] cmd Initial command value
* @param[in] prev parent object (or NULL)
* @retval co Created cligen object. Free with co_free()
* @retval NULL Error
* @see cov_new
* @see co_free
*/
cg_obj *
co_new(char *cmd,
cg_obj *parent)
{
cg_obj *co;
parse_tree *pt;
if ((co = co_new_only(CO_COMMAND)) == NULL)
return NULL;
if (cmd)
co->co_command = strdup(cmd);
co_up_set(co, parent);
/* parse-tree created implicitly */
if ((pt = pt_new()) == NULL){
free(co);
return NULL;
}
if (co_pt_set(co, pt) < 0){
free(pt);
free(co);
return NULL;
}
return co;
}
/*! Create new cligen parse-tree variable object
*
* That is, a cligen parse-tree object with type == CO_VARIABLE
* @param[in] cvtype Cligen variable type
* @param[in] parent parent object (or NULL)
* @retval co Created cligen object. Free with co_free()
* @retval NULL Error
* @see co_new
* @see co_free
*/
cg_obj *
cov_new(enum cv_type cvtype,
cg_obj *parent)
{
cg_obj *co;
parse_tree *pt;
if ((co = co_new_only(CO_VARIABLE)) == NULL)
return NULL;
co->co_vtype = cvtype;
if (parent)
co_up_set(co, parent);
co->co_dec64_n = CGV_DEC64_N_DEFAULT;
/* parse-tree created implicitly */
if ((pt = pt_new()) == NULL){
free(co);
return NULL;
}
if (co_pt_set(co, pt) < 0){
free(pt);
free(co);
return NULL;
}
return co;
}
/*! Recursively copy a cligen object.
*
* @param[in] co The object to copy from
* @param[in] parent The parent of the new object, need not be same as parent of co
* @param[in] flags Copy flagst
* @param[out] conp Pointer to the object to copy to (is allocated)
* @retval 0 OK
* @retval -1 Error
* @see co_expand_sub
* @see co_copy1 For non-recursive
*/
int
co_copy(cg_obj *co,
cg_obj *parent,
uint32_t flags,
cg_obj **conp)
{
int retval = -1;
cg_obj *con = NULL;
parse_tree *pt;
parse_tree *ptn;
size_t size;
if ((con = co_new_only(co->co_type)) == NULL)
goto done;
size = co_size(con->co_type);
memcpy(con, co, size);
con->co_ptvec = NULL;
con->co_pt_len = 0;
con->co_ref = NULL;
/* If called from pt_expand_treeref: the copy (of a tree instance) points to the original tree
*/
if (flags & CO_COPY_FLAGS_TREEREF)
con->co_treeref_orig = co;
co_flags_reset(con, CO_FLAGS_MARK);
/* Replace all pointers */
co_up_set(con, parent);
if (co->co_command)
if ((con->co_command = strdup(co->co_command)) == NULL)
goto done;
if (co->co_prefix)
if ((con->co_prefix = strdup(co->co_prefix)) == NULL)
goto done;
if (co_callback_copy(co->co_callbacks, &con->co_callbacks) < 0)
goto done;
if (co->co_cvec)
con->co_cvec = cvec_dup(co->co_cvec);
if (co->co_filter)
co_filter_set(con, co->co_filter);
if ((pt = co_pt_get(co)) != NULL){
if ((ptn = pt_dup(pt, con, flags)) == NULL) /* sets a new pt under con */
goto done;
if (co_pt_set(con, ptn) < 0)
goto done;
}
if (co->co_helpstring)
if ((con->co_helpstring = strdup(co->co_helpstring)) == NULL)
goto done;
if (co->co_type == CO_VARIABLE){
if (co->co_expand_fn_str)
if ((con->co_expand_fn_str = strdup(co->co_expand_fn_str)) == NULL)
goto done;
if (co->co_translate_fn_str)
if ((con->co_translate_fn_str = strdup(co->co_translate_fn_str)) == NULL)
goto done;
if (co->co_show)
if ((con->co_show = strdup(co->co_show)) == NULL)
goto done;
if (co->co_rangecvv_low)
if ((con->co_rangecvv_low = cvec_dup(co->co_rangecvv_low)) == NULL)
goto done;
if (co->co_rangecvv_upp)
if ((con->co_rangecvv_upp = cvec_dup(co->co_rangecvv_upp)) == NULL)
goto done;
if (co->co_expand_fn_vec)
if ((con->co_expand_fn_vec = cvec_dup(co->co_expand_fn_vec)) == NULL)
goto done;
if (co->co_choice){
if ((con->co_choice = strdup(co->co_choice)) == NULL)
goto done;
}
if (co->co_regex){
if ((con->co_regex = cvec_dup(co->co_regex)) == NULL)
goto done;
}
} /* VARIABLE */
*conp = con;
con = NULL;
retval = 0;
done:
if (con)
co_free(con, 0);
return retval;
}
/*! Copy a single cligen object, non-recursively
*
* CLIgen objects are sometimes created (eg expand) and point into an existing parse-tree
* structure. To free such a co, the regular co_copy cannot be used since it would
* free a pt structure still being use by other co:s.
* working
* @param[in] co The object to copy from
* @param[in] parent The parent of the new object, need not be same as parent of co
* @param[in] recursive If set copy recursive, otherwise, only first level object
* @param[in] flags Copy flags
* @param[out] conp Pointer to the object to copy to (is allocated)
* @retval 0 OK
* @retval -1 Error
* @see co_copy recursive copy: If recurse is 1, this function is equivalent
* @note mark & refdone flags are cleared
* @note co_ref is cleared in the recursive case. (This seems ad-hoc)
*/
int
co_copy1(cg_obj *co,
cg_obj *parent,
int recursive,
uint32_t flags,
cg_obj **conp)
{
int retval = -1;
cg_obj *con = NULL;
parse_tree *pt;
parse_tree *ptn;
size_t size;
if ((con = co_new_only(co->co_type)) == NULL)
goto done;
size = co_size(con->co_type);
memcpy(con, co, size);
con->co_ptvec = NULL;
con->co_pt_len = 0;
/* If called from pt_expand_treeref: the copy (of a tree instance) points to the original tree
*/
if (flags & CO_COPY_FLAGS_TREEREF)
con->co_treeref_orig = co;
co_flags_reset(con, CO_FLAGS_MARK);
/* Replace all pointers */
co_up_set(con, parent);
if (co->co_command)
if ((con->co_command = strdup(co->co_command)) == NULL)
goto done;
if (co->co_prefix)
if ((con->co_prefix = strdup(co->co_prefix)) == NULL)
goto done;
if (co_callback_copy(co->co_callbacks, &con->co_callbacks) < 0)
goto done;
if (co->co_cvec)
con->co_cvec = cvec_dup(co->co_cvec);
if (co->co_filter)
co_filter_set(con, co->co_filter);
if ((pt = co_pt_get(co)) != NULL){
/* Here this function differs from co_copy */
if (recursive){
if ((ptn = pt_dup(pt, con, 0x0)) == NULL) /* sets a new pt under con */
goto done;
if (co_pt_set(con, ptn) < 0)
goto done;
}
else {
if (co_pt_set(con, pt) < 0)
goto done;
}
}
if (co->co_helpstring)
if ((con->co_helpstring = strdup(co->co_helpstring)) == NULL)
goto done;
if (co->co_type == CO_VARIABLE){
if (co->co_expand_fn_str)
if ((con->co_expand_fn_str = strdup(co->co_expand_fn_str)) == NULL)
goto done;
if (co->co_translate_fn_str)
if ((con->co_translate_fn_str = strdup(co->co_translate_fn_str)) == NULL)
goto done;
if (co->co_show)
if ((con->co_show = strdup(co->co_show)) == NULL)
goto done;
if (co->co_rangecvv_low)
if ((con->co_rangecvv_low = cvec_dup(co->co_rangecvv_low)) == NULL)
goto done;
if (co->co_rangecvv_upp)
if ((con->co_rangecvv_upp = cvec_dup(co->co_rangecvv_upp)) == NULL)
goto done;
if (co->co_expand_fn_vec)
if ((con->co_expand_fn_vec = cvec_dup(co->co_expand_fn_vec)) == NULL)
goto done;
if (co->co_choice){
if ((con->co_choice = strdup(co->co_choice)) == NULL)
goto done;
}
if (co->co_regex){
if ((con->co_regex = cvec_dup(co->co_regex)) == NULL)
goto done;
}
} /* VARIABLE */
*conp = con;
con = NULL;
retval = 0;
done:
if (con)
co_free(con, 0);
return retval;
}
/*! Compare two strings, extends strcmp
*
* Basically strcmp but there are some complexities which one may enable.
* Also handles NULL (NULL < all strings)
* @param[in] s1
* @param[in] s2
* @retval 0 Equal
* @retval <0 Str1 is less than str2
* @retval >0 Str1 is greater than str2
*
* strcmp orders: 1 10 2
* wheras strverscmp orders: 1 2 10
* but:
* strcmp orders: 1b 16 6b
* wheras strverscmp orders: 1b 6b 16
* If we use strverscmp we also must use it in e.g. complete
*/
static inline int
str_cmp(char *s1,
char *s2)
{
if (s1 == NULL && s2 == NULL)
return 0;
if (s1 == NULL) /* empty string first */
return -1;
if (s2 == NULL)
return 1;
/*
* XXX: the cligen handler code uses NULL here which is wrong, but those
* options are for now global settings.
*/
#ifdef HAVE_STRVERSCMP
if (cligen_lexicalorder(NULL))
return strverscmp(s1, s2); /* can't combine lexicalorder and ignorecase */
else
return cligen_ignorecase(NULL) ? strcasecmp(s1, s2) : strcmp(s1, s2);
#else /* HAVE_STRVERSCMP */
return cligen_ignorecase(NULL) ? strcasecmp(s1, s2) : strcmp(s1, s2);
#endif /* HAVE_STRVERSCMP */
}
/*! Check if two cligen objects (cg_obj) are equal
*
* Two cligen objects are equal if they have:
* - same type (variable or command)
* - if command, they should have same command name
* - if they are a variable, they should also have:
* + same variable type.
* + same expand, choice, range and regexp options.
* @param[in] co1
* @param[in] co2
* @retval 0 If equal
* @retval <0 If co1 is less than co2
* @retval >0 If co1 is greater than co2
* @see str_cmp
* XXX co_prefix is not examined
*/
int
co_eq(cg_obj *co1,
cg_obj *co2)
{
int eq;
/* eq == 0 means equal */
eq = !(co1->co_type == co2->co_type);
if (eq){ /* Unequal type of command, variable and reference.
but need to check special case,
if the variable is a KEYWORD, then it can be eq to a command. */
/* Let References be last (more than) everything else */
if (co1->co_type == CO_REFERENCE){
eq = 1;
goto done;
}
if (co2->co_type == CO_REFERENCE){
eq = -1;
goto done;
}
/* EMPTY shoyuld always be first */
if (co1->co_type == CO_EMPTY){
eq = -1;
goto done;
}
if (co2->co_type == CO_EMPTY){
eq = 1;
goto done;
}
/* Here one is command and one is variable */
eq = strcmp(co1->co_command, co2->co_command);
goto done;
}
switch (co1->co_type){
case CO_COMMAND:
case CO_REFERENCE:
eq = str_cmp(co1->co_command, co2->co_command);
break;
case CO_VARIABLE:
eq = (co1->co_vtype == co2->co_vtype)?0:(co1->co_vtype < co2->co_vtype)?-1:1;
/* Same variable type */
if (eq != 0)
goto done;
/* Examine expand: at least one set, and then strcmp */
if (co1->co_expand_fn_str!=NULL || co2->co_expand_fn_str!=NULL){
eq = str_cmp(co1->co_expand_fn_str, co2->co_expand_fn_str);
goto done;
}
/* Should we examine co_translate_fn_str? */
/* Examine choice: at least one set, and then strcmp */
if (co1->co_choice!=NULL || co2->co_choice!=NULL){
eq = str_cmp(co1->co_choice, co2->co_choice);
goto done;
}
/* Examine regexp, at least one set, and then strcmp */
if (co1->co_regex!=NULL || co2->co_regex!=NULL){
cg_var *cv1, *cv2;
if (co1->co_regex == NULL)
eq = -1;
else if (co2->co_regex == NULL)
eq = 1;
else{
int i, min;
min = cvec_len(co1->co_regex)<cvec_len(co2->co_regex)?cvec_len(co1->co_regex):cvec_len(co2->co_regex);
for (i=0; i<min; i++){
cv1 = cvec_i(co1->co_regex, i);
cv2 = cvec_i(co2->co_regex, i);
if ((eq = str_cmp(cv_string_get(cv1), cv_string_get(cv2))) != 0)
goto done;
}
if (cvec_len(co1->co_regex) < cvec_len(co2->co_regex))
eq = -1;
else if (cvec_len(co1->co_regex) > cvec_len(co2->co_regex))
eq = 1;
else
eq = 0;
}
if (eq)
goto done;
}
/* Examine int and range */
if (cv_isint(co1->co_vtype) || cv_isstring(co1->co_vtype)) {
int i;
cg_var *cv1, *cv2;
if ((eq = co1->co_rangelen - co2->co_rangelen) != 0)
goto done;
/* either both 0 or both same length */
for (i=0; i<co1->co_rangelen; i++){
cv1 = cvec_i(co1->co_rangecvv_low, i);
cv2 = cvec_i(co2->co_rangecvv_low, i);
if ((eq = cv_cmp(cv1, cv2)) != 0)
goto done;
cv1 = cvec_i(co1->co_rangecvv_upp, i);
cv2 = cvec_i(co2->co_rangecvv_upp, i);
if ((eq = cv_cmp(cv1, cv2)) != 0)
goto done;
}
} /* range */
break;
case CO_EMPTY:
eq = 0;
break;
}
done:
return eq;