forked from clicon/cligen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cligen_var.c
3107 lines (2938 loc) · 73.3 KB
/
cligen_var.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-2018 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 variables - cgv
cgv:s are created when parsing an input string as instances of cg_obj variable
when matching.
Note that a cg_obj is a syntax object and contains a part that specifies cgv:s called cov
*/
#include "cligen_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#ifdef HAVE_REGEX_H
#include <regex.h>
#endif
#include "cligen_buf.h"
#include "cligen_var.h"
#include "cligen_cvec.h"
#include "cligen_gen.h"
#include "cligen_io.h"
#include "cligen_match.h"
#include "cligen_cv.h"
#include "getline.h"
/*
* URL protocol strings
*/
static char *cg_urlprotostr[] = {
NULL,
"file",
"flash",
"tftp",
"ftp",
"telnet",
"http",
"ssh",
NULL
};
/*! Get name of cligen variable cv
* @param[in] cv CLIgen variable
* @retval name Name of cv
*/
char *
cv_name_get(cg_var *cv)
{
return cv->var_name;
}
/*! Set new CLIgen varable name.
* Free previous string if existing.
* @param[in] cv CLIgen variable
* @param[in] s0 New name
* @retval s0 Return the new name
* @retval NULL Error
*/
char *
cv_name_set(cg_var *cv,
char *s0)
{
char *s1 = NULL;
/* Duplicate s0. Must be done before a free, in case s0 is part of the original */
if (s0){
if ((s1 = strdup(s0)) == NULL)
return NULL; /* error in errno */
}
if (cv->var_name != NULL)
free(cv->var_name);
cv->var_name = s1;
return s1;
}
/*! Get cv type
* @param[in] cv CLIgen variable
* @retval type Type of cv
*/
enum cv_type
cv_type_get(cg_var *cv)
{
return cv->var_type;
}
/*! Set new CLIgen type.
* @param[in] cv CLIgen variable
* @param[in] t New type
* @retval t New type
*/
enum cv_type
cv_type_set(cg_var *cv,
enum cv_type x)
{
return (cv->var_type = x);
}
/*! Get cv const flag
* @param[in] cv CLIgen variable
* @retval type Type of cv
*/
char
cv_const_get(cg_var *cv)
{
return cv->var_const;
}
/*! Set CLIgen const flag.
* @param[in] cv CLIgen variable
* @param[in] t New type
* @retval t New type
*/
char
cv_const_set(cg_var *cv,
int c)
{
return (cv->var_const = c);
}
/*! Get application-specific cv flag
* @param[in] cv CLIgen variable
*/
char
cv_flag(cg_var *cv,
char mask)
{
return cv->var_flag & mask;
}
/*! Clear application-specific cv flag
* @param[in] cv CLIgen variable
*/
char
cv_flag_clr(cg_var *cv,
char mask)
{
return cv->var_flag ^= mask;
}
/*! Set application-specific cv flag
* @param[in] cv CLIgen variable
*/
char
cv_flag_set(cg_var *cv,
char mask)
{
return cv->var_flag |= mask;
}
/*! Get value of cv without specific type set
* @param[in] cv CLIgen variable
*/
void *
cv_value_get(cg_var *cv)
{
return &cv->u;
}
/*! Get boolean value of cv
* @param[in] cv CLIgen variable
*/
char
cv_bool_get(cg_var *cv)
{
return ((cv)->u.varu_bool);
}
/*! Set boolean value of cv
* @param[in] cv CLIgen variable
*/
char
cv_bool_set(cg_var *cv,
char x)
{
return (cv->u.varu_bool = x);
}
/*! Get 8-bit integer value of cv
* @param[in] cv CLIgen variable
*/
int8_t
cv_int8_get(cg_var *cv)
{
return ((cv)->u.varu_int8);
}
/*! Set 8-bit integer value of cv
* @param[in] cv CLIgen variable
*/
int8_t
cv_int8_set(cg_var *cv,
int8_t x)
{
return (cv->u.varu_int8 = x);
}
/*! Get 16-bit integer value of cv
* @param[in] cv CLIgen variable
*/
int16_t
cv_int16_get(cg_var *cv)
{
return ((cv)->u.varu_int16);
}
/*! Set 16-bit integer value of cv
* @param[in] cv CLIgen variable
*/
int16_t
cv_int16_set(cg_var *cv,
int16_t x)
{
return (cv->u.varu_int16 = x);
}
/*! Get 32-bit integer value of cv
* @param[in] cv CLIgen variable
*/
int32_t
cv_int32_get(cg_var *cv)
{
return ((cv)->u.varu_int32);
}
/*! Set 32-bit integer value of cv
* @param[in] cv CLIgen variable
*/
int32_t
cv_int32_set(cg_var *cv,
int32_t x)
{
return (cv->u.varu_int32 = x);
}
/*! Get 64-bit integer value of cv
* @param[in] cv CLIgen variable
*/
int64_t
cv_int64_get(cg_var *cv)
{
return ((cv)->u.varu_int64);
}
/*! Set 64-bit integer value of cv
* @param[in] cv CLIgen variable
*/
int64_t
cv_int64_set(cg_var *cv,
int64_t x)
{
return (cv->u.varu_int64 = x);
}
/*! Get 8-bit unsigned integer value of cv
* @param[in] cv CLIgen variable
*/
uint8_t
cv_uint8_get(cg_var *cv)
{
return ((cv)->u.varu_uint8);
}
/*! Set 8-bit unsigned integer value of cv
* @param[in] cv CLIgen variable
*/
uint8_t
cv_uint8_set(cg_var *cv,
uint8_t x)
{
return (cv->u.varu_uint8 = x);
}
/*! Get 16-bit unsigned integer value of cv
* @param[in] cv CLIgen variable
*/
uint16_t
cv_uint16_get(cg_var *cv)
{
return ((cv)->u.varu_uint16);
}
/*! Set 16-bit unsigned integer value of cv
* @param[in] cv CLIgen variable
*/
uint16_t
cv_uint16_set(cg_var *cv,
uint16_t x)
{
return (cv->u.varu_uint16 = x);
}
/*! Get 32-bit unsigned integer value of cv
* @param[in] cv CLIgen variable
*/
uint32_t
cv_uint32_get(cg_var *cv)
{
return ((cv)->u.varu_uint32);
}
/*! Set 32-bit unsigned integer value of cv
* @param[in] cv CLIgen variable
*/
uint32_t
cv_uint32_set(cg_var *cv,
uint32_t x)
{
return (cv->u.varu_uint32 = x);
}
/*! Get 64-bit unsigned integer value of cv
* @param[in] cv CLIgen variable
*/
uint64_t
cv_uint64_get(cg_var *cv)
{
return ((cv)->u.varu_uint64);
}
/*! Set 64-bit unsigned integer value of cv
* @param[in] cv CLIgen variable
*/
uint64_t
cv_uint64_set(cg_var *cv,
uint64_t x)
{
return (cv->u.varu_uint64 = x);
}
/*! Get n-value of decimal-64 of cv (eg exponent)
* @param[in] cv CLIgen variable
*/
uint8_t
cv_dec64_n_get(cg_var *cv)
{
return ((cv)->var_dec64_n);
}
/*! Set n-value of decimal-64 of cv (eg exponent)
* @param[in] cv CLIgen variable
* XXX range check? 1..18
*/
uint8_t
cv_dec64_n_set(cg_var *cv,
uint8_t x)
{
return (cv->var_dec64_n = x);
}
/*! Get i-value of decimal-64 of cv (eg base)
* @param[in] cv CLIgen variable
*/
int64_t
cv_dec64_i_get(cg_var *cv)
{
return ((cv)->var_dec64_i);
}
/*! Set i-value of decimal-64 of cv (eg base)
* @param[in] cv CLIgen variable
*/
int64_t
cv_dec64_i_set(cg_var *cv,
int64_t x)
{
return (cv->var_dec64_i = x);
}
/*! Get pointer to cv string.
*
* @param[in] cv CLIgen variable
* String can be modified in-line but must call _set function to reallocate.
*/
char *
cv_string_get(cg_var *cv)
{
return ((cv)->u.varu_string);
}
/*! Allocate new string from original. Malloc new string and free previous
* @param[in] cv CLIgen variable
*/
char *
cv_string_set(cg_var *cv,
char *s0)
{
char *s1 = NULL;
/* Duplicate s0. Must be done before a free, in case s0 is part of the original */
if (s0){
if ((s1 = strdup(s0)) == NULL)
return NULL; /* error in errno */
}
if (cv->u.varu_string != NULL)
free(cv->u.varu_string);
cv->u.varu_string = s1;
return s1;
}
/*! Get ipv4addr, pointer returned, can be used to set value.
* @param[in] cv CLIgen variable
*/
struct in_addr *
cv_ipv4addr_get(cg_var *cv)
{
return &cv->u.varu_ipv4addr.varipv4_ipv4addr;
}
/*! Get ipv4addr length of cv
* @param[in] cv CLIgen variable
*/
uint8_t
cv_ipv4masklen_get(cg_var *cv)
{
return cv->u.varu_ipv4addr.varipv4_masklen;
}
/*! Get ipv6addr, pointer returned, can be used to set value.
* @param[in] cv CLIgen variable
*/
struct in6_addr *
cv_ipv6addr_get(cg_var *cv)
{
return &cv->u.varu_ipv6addr.varipv6_ipv6addr;
}
/*! Get ipv6addr length of cv
* @param[in] cv CLIgen variable
*/
uint8_t
cv_ipv6masklen_get(cg_var *cv)
{
return cv->u.varu_ipv6addr.varipv6_masklen;
}
/*! Returns a pointer to 6-byte mac-address array.
*
* @param[in] cv CLIgen variable
* This can be used to set the address too
*/
char *
cv_mac_get(cg_var *cv)
{
return cv->u.varu_macaddr;
}
/*! Returns a pointer to uuid byte array.
*
* @param[in] cv CLIgen variable
* This can be used to set the uuid too.
*/
unsigned char *
cv_uuid_get(cg_var *cv)
{
return cv->u.varu_uuid;
}
/*! Set uuid value
* @param[in] cv CLIgen variable
*/
unsigned char *
cv_uuid_set(cg_var *cv,
unsigned char *u)
{
memcpy((char*)&cv->u.varu_uuid, u, 16);
return cv->u.varu_uuid;
}
/*! Returns a struct timeval by value.
* @param[in] cv CLIgen variable
*/
struct timeval
cv_time_get(cg_var *cv)
{
return cv->u.varu_time;
}
/*! Set timeval struct
* @param[in] cv CLIgen variable
* Returns a struct timeval by value.
*/
struct timeval
cv_time_set(cg_var *cv,
struct timeval t)
{
cv->u.varu_time = t;
return t;
}
/*! Get pointer to URL proto string.
*
* @param[in] cv CLIgen variable
* String can be modified in-line but must call _set function to reallocate.
*/
char *
cv_urlproto_get(cg_var *cv)
{
return (cv)->u.varu_url.varurl_proto;
}
/*! Set URL protocol
* malloc new string from original. Free previous string if existing.
* @param[in] cv CLIgen variable
*/
char *
cv_urlproto_set(cg_var *cv,
char *s0)
{
char *s1 = NULL;
/* Duplicate s0. Must be done before a free, in case s0 is part of the original */
if (s0){
if ((s1 = strdup(s0)) == NULL)
return NULL; /* error in errno */
}
if (cv->u.varu_url.varurl_proto != NULL)
free(cv->u.varu_url.varurl_proto);
cv->u.varu_url.varurl_proto = s1;
return s1;
}
/*! Get pointer to URL address string.
*
* @param[in] cv CLIgen variable
* String can be modified in-line but must call _set function to reallocate.
*/
char *
cv_urladdr_get(cg_var *cv)
{
return (cv)->u.varu_url.varurl_addr;
}
/*! Set URL address
* @param[in] cv CLIgen variable
* malloc new string from original. Free previous string if existing.
*/
char *
cv_urladdr_set(cg_var *cv,
char *s0)
{
char *s1 = NULL;
/* Duplicate s0. Must be done before a free, in case s0 is part of the original */
if (s0){
if ((s1 = strdup(s0)) == NULL)
return NULL; /* error in errno */
}
if (cv->u.varu_url.varurl_addr != NULL)
free(cv->u.varu_url.varurl_addr);
cv->u.varu_url.varurl_addr = s1;
return s1;
}
/*! Get pointer to URL path string.
*
* @param[in] cv CLIgen variable
* String can be modified in-line but must call _set function to reallocate.
*/
char *
cv_urlpath_get(cg_var *cv)
{
return (cv)->u.varu_url.varurl_path;
}
/*! Set URL path
* @param[in] cv CLIgen variable
* malloc new string from original. Free previous string if existing.
*/
char *
cv_urlpath_set(cg_var *cv,
char *s0)
{
char *s1 = NULL;
/* Duplicate s0. Must be done before a free, in case s0 is part of the original */
if (s0){
if ((s1 = strdup(s0)) == NULL)
return NULL; /* error in errno */
}
if (cv->u.varu_url.varurl_path != NULL)
free(cv->u.varu_url.varurl_path);
cv->u.varu_url.varurl_path = s1;
return s1;
}
/*! Get pointer to URL user string.
*
* @param[in] cv CLIgen variable
* String can be modified in-line but must call _set function to reallocate.
*/
char *
cv_urluser_get(cg_var *cv)
{
return (cv)->u.varu_url.varurl_user;
}
/*! Set URL user
* @param[in] cv CLIgen variable
* malloc new string from original. Free previous string if existing.
*/
char *
cv_urluser_set(cg_var *cv,
char *s0)
{
char *s1 = NULL;
/* Duplicate s0. Must be done before a free, in case s0 is part of the original */
if (s0){
if ((s1 = strdup(s0)) == NULL)
return NULL; /* error in errno */
}
if (cv->u.varu_url.varurl_user != NULL)
free(cv->u.varu_url.varurl_user);
cv->u.varu_url.varurl_user = s1;
return s1;
}
/*! Get pointer to URL passwd string.
*
* @param[in] cv CLIgen variable
* String can be modified in-line but must call _set function to reallocate.
*/
char *
cv_urlpasswd_get(cg_var *cv)
{
return (cv)->u.varu_url.varurl_passwd;
}
/*! Set URL password
* @param[in] cv CLIgen variable
* malloc new string from original. Free previous string if existing.
*/
char *
cv_urlpasswd_set(cg_var *cv,
char *s0)
{
char *s1 = NULL;
/* Duplicate s0. Must be done before a free, in case s0 is part of the original */
if (s0){
if ((s1 = strdup(s0)) == NULL)
return NULL; /* error in errno */
}
if (cv->u.varu_url.varurl_passwd != NULL)
free(cv->u.varu_url.varurl_passwd);
cv->u.varu_url.varurl_passwd = s1;
return s1;
}
/*! Parse an int8 number and check for errors
* @param[in] str String containing number to parse
* @param[out] val Value on success
* @param[out] reason Error string on failure
* @retval -1 : Error (fatal), with errno set to indicate error
* @retval 0 : Validation not OK, malloced reason is returned
* @retval 1 : Validation OK, value returned in val parameter
*/
static int
parse_int8(char *str,
int8_t *val,
char **reason)
{
int64_t i;
int retval;
if ((retval = parse_int64(str, &i, reason)) != 1)
goto done;
if (i > 127 || i < -128){
if (reason != NULL)
if ((*reason = cligen_reason("%s is out of range(type is int8)", str)) == NULL){
retval = -1; /* malloc */
goto done;
}
retval = 0;
goto done;
}
*val = (int8_t)i;
done:
return retval;
}
/*! Parse an int16 number and check for errors
* @param[in] str String containing number to parse
* @param[out] val Value on success
* @param[out] reason Error string on failure
* @retval -1 : Error (fatal), with errno set to indicate error
* @retval 0 : Validation not OK, malloced reason is returned
* @retval 1 : Validation OK, value returned in val parameter
*/
static int
parse_int16(char *str,
int16_t *val,
char **reason)
{
int64_t i;
int retval;
if ((retval = parse_int64(str, &i, reason)) != 1)
goto done;
if (i > 32676 || i < -32768){
if (reason != NULL)
if ((*reason = cligen_reason("%s is out of range(type is int16)", str)) == NULL){
retval = -1;
goto done;
}
retval = 0;
goto done;
}
*val = (int16_t)i;
done:
return retval;
}
/*! Parse an int32 number and check for errors
* @param[in] str String containing number to parse
* @param[out] val Value on success
* @param[out] reason Error string on failure
* @retval -1 : Error (fatal), with errno set to indicate error
* @retval 0 : Validation not OK, malloced reason is returned
* @retval 1 : Validation OK, value returned in val parameter
*/
int
parse_int32(char *str,
int32_t *val,
char **reason)
{
int64_t i;
int retval;
if ((retval = parse_int64(str, &i, reason)) != 1)
goto done;
if (i > INT_MAX || i < INT_MIN){
if (reason != NULL)
if ((*reason = cligen_reason("%s is out of range(type is int32)", str)) == NULL){
retval = -1; /* malloc */
goto done;
}
retval = 0;
goto done;
}
*val = (int32_t)i;
done:
return retval;
}
/*! Parse an int64 number with explicit base and check for errors
* @param[in] str String containing number to parse
* @parame[in] base If base is zero or 16, the string may include a "0x" prefix,
* the number will be read in base 16; otherwise, a zero base
* is taken as 10 (decimal) unless the next character is '0',
* in which case it is taken as 8 (octal).
* @param[out] val Value on success
* @param[out] reason Error string on failure
* @retval -1 : Error (fatal), with errno set to indicate error
* @retval 0 : Validation not OK, malloced reason is returned
* @retval 1 : Validation OK, value returned in val parameter
*/
int
parse_int64_base(char *str,
int base,
int64_t *val,
char **reason)
{
int64_t i;
char *ep;
int retval = -1;
errno = 0;
i = strtoll(str, &ep, base);
if (str[0] == '\0' || *ep != '\0'){
if (reason != NULL)
if ((*reason = cligen_reason("'%s' is not a number", str)) == NULL){
retval = -1;
goto done;
}
retval = 0;
goto done;
}
if (errno != 0){
if ((i == INT64_MIN || i == INT64_MAX) && errno == ERANGE){
errno = 0;
if (reason != NULL)
if ((*reason = cligen_reason("%s is out of range (type is int64)", str)) == NULL){
retval = -1; /* malloc */
goto done;
}
retval = 0;
goto done;
}
else{
if ((*reason = cligen_reason("%s: %s", str, strerror(errno))) == NULL){
errno = 0;
retval = -1;
goto done;
}
}
errno = 0;
} /* if errno */
*val = i;
retval = 1; /* OK */
done:
return retval;
}
/*! Parse an int64 number and check for errors
* @param[in] str String containing number to parse
* @param[out] val Value on success
* @param[out] reason Error string on failure
* @retval -1 : Error (fatal), with errno set to indicate error
* @retval 0 : Validation not OK, malloced reason is returned
* @retval 1 : Validation OK, value returned in val parameter
*/
int
parse_int64(char *str,
int64_t *val,
char **reason)
{
return parse_int64_base(str, 0, val, reason);
}
/*! Parse an uint8 number and check for errors
* @param[in] str String containing number to parse
* @param[out] val Value on success
* @param[out] reason Error string on failure
* @retval -1 : Error (fatal), with errno set to indicate error
* @retval 0 : Validation not OK, malloced reason is returned
* @retval 1 : Validation OK, value returned in val parameter
*/
int
parse_uint8(char *str,
uint8_t *val,
char **reason)
{
uint64_t i;
int retval;
if ((retval = parse_uint64(str, &i, reason)) != 1)
goto done;
if (i > 255){
if (reason != NULL)
if ((*reason = cligen_reason("%s is out of range(type is uint8)", str)) == NULL){
retval = -1; /* malloc */
goto done;
}
retval = 0;
goto done;
}
*val = (uint8_t)i;
done:
return retval;
}
/*! Parse an uint16 number and check for errors
* @param[in] str String containing number to parse
* @param[out] val Value on success
* @param[out] reason Error string on failure
* @retval -1 : Error (fatal), with errno set to indicate error
* @retval 0 : Validation not OK, malloced reason is returned
* @retval 1 : Validation OK, value returned in val parameter
*/
static int
parse_uint16(char *str,
uint16_t *val,
char **reason)
{
uint64_t i;
int retval;
if ((retval = parse_uint64(str, &i, reason)) != 1)
goto done;
if (i > 65535){
if (reason != NULL)
if ((*reason = cligen_reason("%s is out of range(type is uint16)", str)) == NULL){
retval = -1; /* malloc */
goto done;
}
retval = 0;
goto done;
}
*val = (uint16_t)i;
done:
return retval;
}
/*! Parse an uint32 number and check for errors
* @param[in] str String containing number to parse
* @param[out] val Value on success
* @param[out] reason Error string on failure
* @retval -1 : Error (fatal), with errno set to indicate error
* @retval 0 : Validation not OK, malloced reason is returned
* @retval 1 : Validation OK, value returned in val parameter
*/
static int
parse_uint32(char *str,
uint32_t *val,
char **reason)
{
uint64_t i;
int retval;
if ((retval = parse_uint64(str, &i, reason)) != 1)
goto done;
if (i > UINT_MAX){
if (reason != NULL)
if ((*reason = cligen_reason("%s is out of range(type is uint32)", str)) == NULL){
retval = -1; /* malloc */
goto done;
}
retval = 0;
goto done;
}
*val = (uint32_t)i;
done:
return retval;
}
/*! Parse an uint64 number and check for errors
* @param[in] str String containing number to parse
* @param[out] val Value on success
* @param[out] reason Error string on failure (if given)
* @retval -1 : Error (fatal), with errno set to indicate error
* @retval 0 : Validation not OK, malloced reason is returned
* @retval 1 : Validation OK, value returned in val parameter
* @note: we have to detect a minus sign ourselves,....
*/
int
parse_uint64(char *str,
uint64_t *val,
char **reason)
{
uint64_t i;
char *ep;
int retval = -1;
errno = 0;
i = strtoull(str, &ep, 0);
if (str[0] == '\0' || *ep != '\0'){
if (reason != NULL)
if ((*reason = cligen_reason("'%s' is not a number", str)) == NULL){
retval = -1; /* malloc */
goto done;
}
retval = 0;
goto done;
}
if (errno != 0){
if (i == UINT64_MAX && errno == ERANGE){
if (reason != NULL)
if ((*reason = cligen_reason("%s is out of range (type is uint64)", str)) == NULL){
retval = -1; /* malloc */
goto done;
}
retval = 0;