forked from clicon/cligen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cligen_cvec.c
971 lines (892 loc) · 22.9 KB
/
cligen_cvec.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
/*
***** 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 *****
CLIgen variable vectors - cvec
*/
#include "cligen_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#define __USE_GNU /* isblank() */
#include <ctype.h>
#ifndef isblank
#define isblank(c) (c==' ')
#endif /* isblank */
#include "cligen_buf.h"
#include "cligen_cv.h"
#include "cligen_cvec.h"
#include "cligen_parsetree.h"
#include "cligen_callback.h"
#include "cligen_object.h"
#include "cligen_io.h"
#include "cligen_getline.h"
#include "cligen_cv_internal.h"
#include "cligen_cvec_internal.h"
/*! A malloc version that aligns on 4 bytes. To avoid warning from valgrind */
#define align4(s) (((s)/4)*4 + 4)
/*! A strdup version that aligns on 4 bytes. To avoid warning from valgrind */
static inline char * strdup4(char *str)
{
char *dup;
int len;
len = align4(strlen(str)+1);
if ((dup = malloc(len)) == NULL)
return NULL;
strcpy(dup, str);
return dup;
}
/*! Create and initialize a new cligen variable vector (cvec)
*
* Each individual cv initialized with CGV_ERR and no value.
* Returned cvec needs to be freed with cvec_free().
*
* @param[in] len Number of cv elements. Can be zero and elements added incrementally.
* @retval NULL errno set
* @retval cvv allocated cligen var vector
* @see cvec_init
*/
cvec *
cvec_new(int len)
{
cvec *cvv;
if ((cvv = malloc(sizeof(*cvv))) == NULL)
return NULL;
memset(cvv, 0, sizeof(*cvv));
if (cvec_init(cvv, len) < 0){
free(cvv);
return NULL;
}
return cvv;
}
/*! Create a new vector, initialize the first element to the contents of 'var'
*
* @param[in] var cg_var to clone and add to vector
* @retval cvec allocated cvec
*/
cvec *
cvec_from_var(cg_var *cv)
{
cvec *newvec = NULL;
cg_var *tail = NULL;
if (cv && (newvec = cvec_new(0))) {
if ((tail = cvec_append_var(newvec, cv)) == NULL) {
cvec_free(newvec);
newvec = NULL;
}
}
return newvec;
}
/*! Free a cligen variable vector (cvec)
*
* Reset and free a cligen vector as previously created by cvec_new(). this includes
* freeing all cv:s that the cvec consists of.
* @param[in] cvv Cligen variable vector
* @see cvec_new
*/
int
cvec_free(cvec *cvv)
{
if (cvv) {
cvec_reset(cvv);
free(cvv);
}
return 0;
}
/*! Initialize a cligen variable vector (cvec) with 'len' numbers of variables.
*
* Each individual cv initialized with CGV_ERR and no value.
*
* @param[in] cvv Cligen variable vector
* @param[in] len Number of cv elements. Can be zero and elements added incrementally.
* @see cvec_new
*/
int
cvec_init(cvec *cvv,
int len)
{
cvv->vr_len = len;
if (len && (cvv->vr_vec = calloc(cvv->vr_len, sizeof(cg_var))) == NULL)
return -1;
return 0;
}
/*! Reset cligen variable vector resetting it to an initial state as returned by cvec_new
*
* @param[in] cvv Cligen variable vector
* @see also cvec_free. But this function does not actually free the cvec.
*/
int
cvec_reset(cvec *cvv)
{
cg_var *cv = NULL;
if (cvv == NULL)
return 0;
while ((cv = cvec_each(cvv, cv)) != NULL)
cv_reset(cv);
if (cvv->vr_vec)
free(cvv->vr_vec);
if (cvv->vr_name)
free(cvv->vr_name);
memset(cvv, 0, sizeof(*cvv));
return 0;
}
/*! Given a cv in a cligen variable vector (cvec) return the next cv.
*
* @param[in] cvv The cligen variable vector
* @param[in] cv0 Return element after this, or first element if this is NULL
* Given an element (cv0) in a cligen variable vector (cvec) return the next element.
* @retval cv Next element
*/
cg_var *
cvec_next(cvec *cvv,
cg_var *cv0)
{
cg_var *cv = NULL;
int i;
if (cvv == NULL)
return NULL;
if (cv0 == NULL)
cv = cvv->vr_vec;
else {
i = cv0 - cvv->vr_vec;
if (i < cvv->vr_len-1)
cv = cv0 + 1;
}
return cv;
}
/*! Append a new cligen variable (cv) to cligen variable vector (cvec) and return it.
*
* @param[in] cvv Cligen variable vector
* @param[in] type Append a new cv to the vector with this type
* @retval NULL Error
* @retval cv The new cligen variable
* @see cv_new but this is allocated contiguosly as a part of a cvec.
* @see cvec_append_var
* @see cvec_add_string utility function for appending a string cv
*/
cg_var *
cvec_add(cvec *cvv,
enum cv_type type)
{
int len;
cg_var *cv;
if (cvv == NULL){
errno = EINVAL;
return NULL;
}
len = cvv->vr_len + 1;
if ((cvv->vr_vec = realloc(cvv->vr_vec, len*sizeof(cg_var))) == NULL)
return NULL;
cvv->vr_len = len;
cv = cvec_i(cvv, len-1);
memset(cv, 0, sizeof(*cv));
cv->var_type = type;
return cv;
}
/*! Append a new var that is a clone of data in 'cv' to the vector, return it
* @param[in] cvv Cligen variable vector
* @param[in] cv Append this cligen variable to vector. Note that it is copied.
* @retval NULL Error
* @retval tail Return the new last tail variable (copy of cv)
*/
cg_var *
cvec_append_var(cvec *cvv,
cg_var *cv)
{
cg_var *tail = NULL;
if (cvv && cv && (tail = cvec_add(cvv, cv_type_get(cv)))) {
if (cv_cp(tail, cv) < 0) {
cvec_del(cvv, tail);
tail = NULL;
}
}
return tail;
}
/*! Delete a cv variable from a cvec. Note: cv is not reset & cv may be stale!
*
* @param[in] cvv Cligen variable vector
* @param[in] del variable to delete
*
* @note This is a dangerous command since the cv it deletes (such as created by
* cvec_add) may have been modified with realloc (eg cvec_add/delete) and
* therefore can not be used as a reference. Safer methods are to use
* cvec_find/cvec_i to find a cv and then to immediately remove it.
*/
int
cvec_del(cvec *cvv,
cg_var *del)
{
int i;
cg_var *cv;
if (cvec_len(cvv) == 0)
return 0;
i = 0;
cv = NULL;
while ((cv = cvec_each(cvv, cv)) != NULL) {
if (cv == del)
break;
i++;
}
if (i >= cvec_len(cvv)) /* Not found !?! */
return cvec_len(cvv);
if (i != cvec_len(cvv)-1) /* If not last entry, move the remaining cv's */
memmove(&cvv->vr_vec[i], &cvv->vr_vec[i+1],
(cvv->vr_len-i-1) * sizeof(cvv->vr_vec[0]));
cvv->vr_len--;
cvv->vr_vec = realloc(cvv->vr_vec, cvv->vr_len*sizeof(cvv->vr_vec[0])); /* Shrink should not fail? */
return cvec_len(cvv);
}
/*! Delete a cv variable from a cvec using index i
*
* @param[in] cvv Cligen variable vector
* @param[in] del variable to delete
* @retval n New cvec length
* @retval -1 Error
*
* @note This is a dangerous command since the cv it deletes (such as created by
* cvec_add) may have been modified with realloc (eg cvec_add/delete) and
* therefore can not be used as a reference. Safer methods are to use
* cvec_find/cvec_i to find a cv and then to immediately remove it.
* @note does not deallocate the cv, you may need to do it with cv_reset
*/
int
cvec_del_i(cvec *cvv,
int i)
{
if (cvec_len(cvv) == 0 || cvec_len(cvv) < i){
errno = EINVAL;
return -1;
}
if (i != cvec_len(cvv)-1) /* If not last entry, move the remaining cv's */
memmove(&cvv->vr_vec[i], &cvv->vr_vec[i+1],
(cvv->vr_len-i-1) * sizeof(cvv->vr_vec[0]));
cvv->vr_len--;
return cvec_len(cvv);
}
/*! Return allocated length of a cvec.
* @param[in] cvv Cligen variable vector
*/
int
cvec_len(cvec *cvv)
{
if (cvv == NULL)
return 0;
return cvv->vr_len;
}
/*! Return i:th element of cligen variable vector cvec.
* @param[in] cvv Cligen variable vector
* @param[in] i Order of element to get
*/
cg_var *
cvec_i(cvec *cvv,
int i)
{
if (!cvv) {
return NULL;
}
if (i < cvv->vr_len)
return &cvv->vr_vec[i];
return NULL;
}
/*! Return string value of i:th element of cligen variable vector. Helper function.
* @param[in] cvv Cligen variable vector
* @param[in] i Order of element to get
* @retval str String value of i:th element
* @retval NULL Element does not exist
*/
char *
cvec_i_str(cvec *cvv,
int i)
{
cg_var *cv;
if ((cv = cvec_i(cvv, i)) == NULL)
return NULL;
return cv_string_get(cv);
}
/*! Iterate through all cligen variables in a cvec list
*
* @param[in] cvv Cligen variable vector
* @param[in] prev Last cgv (or NULL)
* @retval cv Next variable structure.
* @retval NULL When end of list reached.
* @code
* cg_var *cv = NULL;
* while ((cv = cvec_each(cvv, cv)) != NULL)
* ...
* @endcode
* @see cvec_each1 Skip first
*/
cg_var *
cvec_each(cvec *cvv,
cg_var *prev)
{
if (cvv == NULL)
return NULL;
if (prev == NULL){ /* Initialization */
if (cvv->vr_len > 0)
return &cvv->vr_vec[0];
else
return NULL;
}
return cvec_next(cvv, prev);
}
/*! Iterate through all except first cligen variables in a cvec list
*
* @param[in] cvv Cligen variable vector
* @param[in] prev Last cgv (or NULL)
* @retval cv Next variable structure.
* @retval NULL When end of list reached.
* Common in many cvecs where [0] is the command-line and all
* others are arguments.
* @see cvec_each For all elements, dont skip first
*/
cg_var *
cvec_each1(cvec *cvv,
cg_var *prev)
{
if (cvv == NULL)
return NULL;
if (prev == NULL){ /* Initialization */
if (cvv->vr_len > 1)
return &cvv->vr_vec[1];
else
return NULL;
}
return cvec_next(cvv, prev);
}
/*! Create a new cvec by copying from an original
*
* @param[in] old The cvec to copy from
* @retval new The cvec copy. Free this with cvec_free
* @retval NULL Error
* The new cvec needs to be freed by cvec_free().
* One can make a cvec_cp() as well but it is a little trickier to match vr_vec.
*/
cvec *
cvec_dup(cvec *old)
{
cvec *new;
cg_var *cv0 = NULL;
cg_var *cv1;
int i;
if (old == NULL)
return NULL;
if ((new = cvec_new(old->vr_len)) == NULL)
return NULL;
if (old->vr_name &&
(new->vr_name = strdup4(old->vr_name)) == NULL){
free(new);
return NULL;
}
i = 0;
while ((cv0 = cvec_each(old, cv0)) != NULL) {
cv1 = cvec_i(new, i++);
cv_cp(cv1, cv0);
}
return new;
}
/*! Create a cv list with a single string element.
*
* @param[in] cmd Text string
* @retval NULL Error
* @retval cvv Cligen variable list
* Help function when creating cvec to cligen callbacks.
*/
cvec *
cvec_start(char *cmd)
{
cvec *cvec;
cg_var *cv;
if ((cvec = cvec_new(1)) == NULL)
return NULL;
cv = cvec_i(cvec, 0);
cv->var_type = CGV_REST;
cv_name_set(cv, "cmd"); /* the whole command string */
cv_string_set(cv, cmd); /* the whole command string */
return cvec;
}
/*! Pretty print cligen variable list to a file
* @param[in] f File to print to
* @param[in] cvv Cligen variable vector to print
* @see cvec2cbuf
*/
int
cvec_print(FILE *f,
cvec *cvv)
{
cg_var *cv = NULL;
char *name;
int i = 0;
if ((name = cvec_name_get(cvv)) != NULL)
fprintf(f, "%s:\n", name);
while ((cv = cvec_each(cvv, cv)) != NULL) {
name = cv_name_get(cv);
if (name)
fprintf(f, "%d : %s = ", i++, name);
else
fprintf(f, "%d : ", i++);
cv_print(f, cv);
fprintf(f, "\n");
}
return 0;
}
/*! Pretty print cligen variable list to a cligen buffer
* @param[out] cb Cligen buffer (should already be initialized w cbuf_new)
* @param[in] cvv Cligen variable vector to print
* @see cvec_print
*/
int
cvec2cbuf(cbuf *cb,
cvec *cvv)
{
cg_var *cv = NULL;
int i = 0;
char *s;
while ((cv = cvec_each(cvv, cv)) != NULL) {
if ((s = cv2str_dup(cv)) == NULL)
return -1;
cprintf(cb, "%d : %s = %s\n", i++, cv_name_get(cv), s);
free(s);
}
return 0;
}
/*! Return first cv in a cvec matching a name
*
* Given an CLIgen variable vector cvec, and the name of a variable, return the
* first matching entry.
* @param[in] cvv Cligen variable vector
* @param[in] name Name to match (can be NULL)
* @retval cv Element matching name. NULL
* @retval NULL Not found
* @see cvec_find_keyword
*/
cg_var *
cvec_find(cvec *cvv,
char *name)
{
cg_var *cv = NULL;
while ((cv = cvec_each(cvv, cv)) != NULL){
if (cv->var_name){
if (name != NULL && strcmp(cv->var_name, name) == 0)
return cv;
}
else if (name == NULL)
return cv;
}
return NULL;
}
/*! Return first keyword cv in a cvec matching a name
* @param[in] cvv Cligen variable vector
* @param[in] name Name to match
* @retval cv Element matching name. NULL
* @retval NULL Not found
* @see cvec_find
*/
cg_var *
cvec_find_keyword(cvec *cvv,
char *name)
{
cg_var *cv = NULL;
while ((cv = cvec_each(cvv, cv)) != NULL)
if (cv->var_name && strcmp(cv->var_name, name) == 0 && cv->var_const)
return cv;
return NULL;
}
/*! Return first non-keyword cv in a cvec matching a name
* @param[in] cvv Cligen variable vector
* @param[in] name Name to match
* @retval cv Element matching name. NULL
* @retval NULL Not found
* @see cvec_find
*/
cg_var *
cvec_find_var(cvec *cvv,
char *name)
{
cg_var *cv = NULL;
while ((cv = cvec_each(cvv, cv)) != NULL)
if (cv->var_name && strcmp(cv->var_name, name) == 0 && !cv->var_const)
return cv;
return NULL;
}
/*! Typed version of cvec_find that returns the string value.
*
* @param[in] cvv Cligen variable vector
* @param[in] name Name to match
* @retval cv Element matching name. NULL
* @retval NULL Not found
* @note Does not see the difference between not finding the cv, and finding one
* with wrong type - in both cases NULL is returned.
* @note The returned string must be copied since it points directly into the cv.
* @see cvec_find
*/
char *
cvec_find_str(cvec *cvv,
char *name)
{
cg_var *cv;
if ((cv = cvec_find(cvv, name)) != NULL && cv_isstring(cv->var_type))
return cv_string_get(cv);
return NULL;
}
/*! Get name of cligen variable vector
* @param[in] cvv Cligen variable vector
* @retval str The name of the cvec as a string, can be NULL, no copy
* @retval name Name of variable vector
*/
char *
cvec_name_get(cvec *cvv)
{
return cvv->vr_name;
}
/*! Allocate and set name of cligen variable vector, including NULL
* @param[in] cvv A cligen variable vector
* @param[in] name A string that is copied and used as a cvec name, or NULL
* @retval str The name of the cvec.
* The existing name, if any, is freed
*/
char *
cvec_name_set(cvec *cvv,
char *name)
{
char *s1 = NULL;
/* Duplicate name. Must be done before a free, in case name is part of the original */
if (name){
if ((s1 = strdup4(name)) == NULL)
return NULL; /* error in errno */
}
if (cvv->vr_name != NULL)
free(cvv->vr_name);
cvv->vr_name = s1;
return s1;
}
/*! Return the alloced memory of a CLIgen variable vector
*/
size_t
cvec_size(cvec *cvv)
{
size_t sz = 0;
cg_var *cv = NULL;
sz += sizeof(struct cvec);
if (cvv->vr_name)
sz += strlen(cvv->vr_name)+1;
cv = NULL;
while ((cv = cvec_each(cvv, cv)) != NULL)
sz += cv_size(cv);
return sz;
}
int
cligen_txt2cvv(char *str,
cvec **cvp)
{
int retval = -1;
int i;
int i0;
char c;
cvec *cvv = NULL;
cg_var *cv = NULL;
int whitespace = 1;
size_t len;
if ((cvv = cvec_new(0)) == NULL)
goto done;
len = strlen(str);
i0 = 0;
for (i=0; i<len; i++){
c = str[i];
if (whitespace && isblank(c))
i0 = i+1; /* skip */
else if (c == '\n'){
if ((cv = cvec_add(cvv, CGV_STRING)) == NULL)
goto done;
if (cv_strncpy(cv, &str[i0], i-i0) == NULL)
goto done;
i0 = i+1;
whitespace = 1;
}
else{
whitespace = 0;
}
}
/* There may be a case here where last char is \n */
if (i != i0){
if ((cv = cvec_add(cvv, CGV_STRING)) == NULL)
goto done;
if (cv_strncpy(cv, &str[i0], i-i0) == NULL)
goto done;
}
if (cvp){
if (*cvp != NULL)
cvec_free(*cvp);
*cvp = cvv;
cvv = NULL;
}
retval = 0;
done:
if (cvv)
cvec_free(cvv);
return retval;
}
/*! Given a string (s0), return the next token.
* The string is modified to return
* the remainder of the string after the identified token.
* A token is found either as characters delimited by one or many delimiters.
* Or as a pair of double-quotes(") with any characters in between.
* if there are trailing spaces after the token, trail is set to one.
* If string is NULL or "", NULL is returned.
* If empty token found, s0 is NULL
* @param[in] s0 String, the string is modified like strtok
* @param[out] token0 A malloced token. NOTE: token must be freed after use.
* @param[out] rest0 A remaining (rest) string. NOTE: NOT malloced.
* @param[out] leading0 If leading delimiters eg " thisisatoken"
* Example:
* s0 = " foo bar"
* results in token="foo", leading=1
*/
static int
next_token(char **s0,
char **token0,
char **rest0,
int *leading0)
{
char *s;
char *st;
char *token = NULL;
size_t len;
int quote=0;
int leading=0;
int escape = 0;
s = *s0;
if (s==NULL){
fprintf(stderr, "%s: null string\n", __FUNCTION__);
return -1;
}
for (s=*s0; *s; s++){ /* First iterate through delimiters */
if (index(CLIGEN_DELIMITERS, *s) == NULL)
break;
leading++;
}
if (rest0)
*rest0 = s;
if (*s && index(CLIGEN_QUOTES, *s) != NULL){
quote++;
s++;
}
st=s; /* token starts */
escape = 0;
for (; *s; s++){ /* Then find token */
if (quote){
if (index(CLIGEN_QUOTES, *s) != NULL)
break;
}
else{ /* backspace tokens for escaping delimiters */
if (escape)
escape = 0;
else{
if (*s == '\\')
escape++;
else
if (index(CLIGEN_DELIMITERS, *s) != NULL)
break;
}
}
}
if (quote && *s){
s++;
// fprintf(stderr, "s=\"%s\" %d %s\n", s, *s, index(CLIGEN_DELIMITERS, *s));
if (*s && index(CLIGEN_DELIMITERS, *s) == NULL){
;// cligen_reason("Quote token error");
}
len = (s-st)-1;
}
else{
if (quote){ /* Here we signalled error before but it is removed */
st--;
}
len = (s-st);
if (!len){
token = NULL;
*s0 = NULL;
goto done;
}
}
if ((token=malloc(len+1)) == NULL){
fprintf(stderr, "%s: malloc: %s\n", __FUNCTION__, strerror(errno));
return -1;
}
memcpy(token, st, len);
token[len] = '\0';
*s0 = s;
done:
*leading0 = leading;
*token0 = token;
return 0;
}
/*! Split a CLIgen command string into a cligen variable vector using delimeters and escape quotes
*
* @param[in] string String to split
* @param[out] cvtp CLIgen variable vector, containing all tokens.
* @param[out] cvrp CLIgen variable vector, containing the remaining strings.
* @retval 0 OK
* @retval -1 Error
* @code
* cvec *cvt = NULL;
* cvec *cvr = NULL;
* if (cligen_str2cvv("a=b&c=d", " \t", "\"", &cvt, &cvt) < 0)
* err;
* ...
* cvec_free(cvt);
* cvec_free(cvr);
* @endcode
* Example, input string "aa bb cc" (0th element is always whole string)
* cvp : ["aa bb cc", "aa", "bb", "cc"]
* cvr : ["aa bb cc", "aa bb cc", "bb cc", "cc"]
* @note both out cvv:s should be freed with cvec_free()
*/
int
cligen_str2cvv(char *string,
cvec **cvtp,
cvec **cvrp)
{
int retval = -1;
char *s;
char *sr;
char *s0 = NULL;;
cvec *cvt = NULL; /* token vector */
cvec *cvr = NULL; /* rest vector */
cg_var *cv;
char *t = NULL;
int trail;
int i;
if ((s0 = strdup(string)) == NULL)
goto done;
s = s0;
if ((cvt = cvec_start(string)) ==NULL)
goto done;
if ((cvr = cvec_start(string)) ==NULL)
goto done;
i = 0;
while (s != NULL) {
t = NULL;
if (next_token(&s, &t, &sr, &trail) < 0)
goto done;
/* If there is no token, stop,
* unless it is the intial token (empty string) OR there are trailing whitespace
* In these cases insert an empty "" token.
*/
if (t == NULL && !trail && i > 0)
break;
if ((cv = cvec_add(cvr, CGV_STRING)) == NULL)
goto done;
if (cv_string_set(cv, sr?sr:"") == NULL) /* XXX memleak */
goto done;
if ((cv = cvec_add(cvt, CGV_STRING)) == NULL)
goto done;
if (cv_string_set(cv, t?t:"") == NULL) /* XXX memleak */
goto done;
if (t){
free(t);
t = NULL;
}
i++;
}
retval = 0;
if (cvtp){
*cvtp = cvt;
cvt = NULL;
}
if (cvrp){
*cvrp = cvr;
cvr = NULL;
}
done:
if (t)
free(t);
if (s0)
free(s0);
if (cvt)
cvec_free(cvt);
if (cvr)
cvec_free(cvr);
return retval;
}
/*! Replace the original string in first position with expanded string
*
* @param[in,out] cvv Change first element
* @retval 0 OK
* @retval -1 Error
* @see https://github.com/clicon/cligen/issues/65
*/
int
cvec_expand_first(cvec *cvv)
{
int retval = -1;
cbuf *cb = NULL;
cg_var *cv;
int i;
if ((cb = cbuf_new()) == NULL)
goto done;
for (i=1; i<cvec_len(cvv); i++){
if (i>1)
cprintf(cb, " ");
if ((cv = cvec_i(cvv, i)) == NULL)
goto done;
cv2cbuf(cv, cb);
}
cv = cvec_i(cvv,0);
cv_string_set(cv, cbuf_get(cb));
retval = 0;
done:
if (cb)
cbuf_free(cb);
return retval;
}
/*! Exclude keys from a cvec destructively
*
* @param[in,out] cvv Remove all contant keywords
* @see https://github.com/clicon/cligen/issues/65
*/
int
cvec_exclude_keys(cvec *cvv)
{
cg_var *cv;
int i;
for (i=1; i<cvec_len(cvv);){
if ((cv = cvec_i(cvv, i)) != NULL &&
cv_const_get(cv)){
cv_reset(cv);
cvec_del_i(cvv, i);
continue;
}
i++;
}
return 0;
}