1 /* 2 ***** BEGIN LICENSE BLOCK ***** 3 4 Copyright (C) 2001-2020 Olof Hagsand 5 6 This file is part of CLIgen. 7 8 Licensed under the Apache License, Version 2.0 (the "License"); 9 you may not use this file except in compliance with the License. 10 You may obtain a copy of the License at 11 12 http://www.apache.org/licenses/LICENSE-2.0 13 14 Unless required by applicable law or agreed to in writing, software 15 distributed under the License is distributed on an "AS IS" BASIS, 16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 See the License for the specific language governing permissions and 18 limitations under the License. 19 20 Alternatively, the contents of this file may be used under the terms of 21 the GNU General Public License Version 2 or later (the "GPL"), 22 in which case the provisions of the GPL are applicable instead 23 of those above. If you wish to allow use of your version of this file only 24 under the terms of the GPL, and not to allow others to 25 use your version of this file under the terms of Apache License version 2, indicate 26 your decision by deleting the provisions above and replace them with the 27 notice and other provisions required by the GPL. If you do not delete 28 the provisions above, a recipient may use your version of this file under 29 the terms of any one of the Apache License version 2 or the GPL. 30 31 ***** END LICENSE BLOCK ***** 32 33 34 CLIgen variables - cgv 35 cgv:s are created when parsing an input string as instances of cg_obj variable 36 when matching. 37 Note that a cg_obj is a syntax object and contains a part that specifies cgv:s called cov 38 */ 39 40 #ifndef _CLIGEN_CV_H_ 41 #define _CLIGEN_CV_H_ 42 43 #include <stdint.h> /* int64 requires stdint */ 44 45 /* 46 * Macros and constants 47 * CLIgen flags defined are in the range 0x01 -0x0f 48 * An application can use any flags above that 49 */ 50 #define V_INVERT 0x01 /* Used by regexp code as inverted regexps */ 51 52 typedef unsigned char uuid_t[16]; 53 54 /* 55 * First, built in types, 56 * Some types have their values in-line (eg in the cgv struct), others 57 * have pointers to the value. 58 * Cgvs with pointers are: string, interface, rest, choice, 59 * expand. 60 */ 61 enum cv_type{ 62 CGV_ERR=0, /* Invalid */ 63 CGV_INT8, /* 8-bit signed integer / char */ 64 CGV_INT16, /* 16-bit signed integer */ 65 CGV_INT32, /* 32-bit signed integer */ 66 CGV_INT64, /* 32-bit signed integer */ 67 CGV_UINT8, /* 8-bit unsigned integer / char */ 68 CGV_UINT16, /* 16-bit unsigned integer */ 69 CGV_UINT32, /* 32-bit unsigned integer */ 70 CGV_UINT64, /* 32-bit unsigned integer */ 71 CGV_DEC64, /* 64-bit signed decimal number */ 72 CGV_BOOL, /* 1-bit boolean value */ 73 CGV_REST, /* Rest of line, not parsed */ 74 CGV_STRING, /* Null-terminated character string */ 75 CGV_INTERFACE, /* name of interface CISCO style: eg eth0, eth0/24 */ 76 CGV_IPV4ADDR, /* Address: 1.2.3.4 */ 77 CGV_IPV4PFX, /* Prefix: 1.2.3.4/34 */ 78 CGV_IPV6ADDR, /* Address: 2001:0db8:85a3:0042:0000:8a2e:0370:7334 */ 79 CGV_IPV6PFX, /* Prefix: 2001:0db8:85a3:0042:0000:8a2e:0370:7334/48 */ 80 CGV_MACADDR, /* f0:de:f1:1b:10:47 */ 81 CGV_URL, /* <proto>://[<user>[:<passwd>]@]<addr>[/<path>] */ 82 CGV_UUID, /* Universally Unique Identifier: 550e8400-e29b-41d4-a716-446655440000 */ 83 CGV_TIME, /* ISO 8601 date+timestamp: 2008-09-21T18:57:21.003 (extended format) */ 84 CGV_VOID, /* Pointer to external data. Notes: not freed on cv_free; 85 not null-terminated string, cv_cp/dup will retain pointer */ 86 CGV_EMPTY, /* A type without a value */ 87 }; 88 89 /* cv is one of the int-types */ 90 #define cv_isint(t)((t)==CGV_INT8 || (t)==CGV_INT16|| \ 91 (t)==CGV_INT32 || (t)==CGV_INT64|| \ 92 (t)==CGV_UINT8 || (t)==CGV_UINT16|| \ 93 (t)==CGV_UINT32 || (t)==CGV_UINT64) 94 95 /* No pointers to value */ 96 #define cv_inline(t)((t)==CGV_ERR || cv_isint(t)|| \ 97 (t)==CGV_DEC64 || (t)==CGV_BOOL|| \ 98 (t)==CGV_IPV4ADDR || (t)==CGV_IPV4PFX|| \ 99 (t)==CGV_IPV6ADDR || (t)==CGV_IPV6PFX|| \ 100 (t)==CGV_MACADDR || (t)==CGV_UUID|| \ 101 (t)==CGV_TIME || (t)==CGV_EMPTY) 102 103 /* var_string is set to something meaningful */ 104 #define cv_isstring(t)((t)==CGV_STRING||(t)==CGV_REST|| \ 105 (t)==CGV_INTERFACE) 106 107 #define cv_typemax(t) 108 109 /* 110 * Cligen Variable structure 111 * cg_var / cv 112 * A cg_var is a variable instantiation, ie it has a name and a value and is typed. 113 * The type determines how the values are stored. Some values are stored in-line 114 * using a union structure (such as int) while others uses pointers (eg string). 115 * It is different from cg_obj of type CO_VARIABLE in that the cg_obj is a 116 * specification. 117 */ 118 typedef struct cg_var cg_var; 119 120 struct cg_obj; /* forward declaration. Original in cligen_object.h */ 121 122 struct cg_varspec; /* forward declaration. Original in cligen_object.h */ 123 124 /* 125 * Prototypes 126 */ 127 char *cv_name_get(cg_var *cv); 128 char *cv_name_set(cg_var *cv, char *s0); 129 enum cv_type cv_type_get(cg_var *cv); 130 enum cv_type cv_type_set(cg_var *cv, enum cv_type x); 131 char cv_const_get(cg_var *cv); 132 char cv_const_set(cg_var *cv, int c); 133 134 char cv_flag(cg_var *cv, char mask); 135 char cv_flag_clr(cg_var *cv, char mask); 136 char cv_flag_set(cg_var *cv, char mask); 137 138 void *cv_value_get(cg_var *cv); 139 140 char cv_bool_get(cg_var *cv); 141 char cv_bool_set(cg_var *cv, char x); 142 int8_t cv_int8_get(cg_var *cv); 143 int8_t cv_int8_set(cg_var *cv, int8_t x); 144 int16_t cv_int16_get(cg_var *cv); 145 int16_t cv_int16_set(cg_var *cv, int16_t x); 146 int32_t cv_int32_get(cg_var *cv); 147 int32_t cv_int32_set(cg_var *cv, int32_t x); 148 int64_t cv_int64_get(cg_var *cv); 149 int64_t cv_int64_set(cg_var *cv, int64_t x); 150 151 uint8_t cv_uint8_get(cg_var *cv); 152 uint8_t cv_uint8_set(cg_var *cv, uint8_t x); 153 uint16_t cv_uint16_get(cg_var *cv); 154 uint16_t cv_uint16_set(cg_var *cv, uint16_t x); 155 uint32_t cv_uint32_get(cg_var *cv); 156 uint32_t cv_uint32_set(cg_var *cv, uint32_t x); 157 uint64_t cv_uint64_get(cg_var *cv); 158 uint64_t cv_uint64_set(cg_var *cv, uint64_t x); 159 160 uint8_t cv_dec64_n_get(cg_var *cv); 161 uint8_t cv_dec64_n_set(cg_var *cv, uint8_t x); 162 int64_t cv_dec64_i_get(cg_var *cv); 163 int64_t cv_dec64_i_set(cg_var *cv, int64_t x); 164 165 char *cv_string_get(cg_var *cv); 166 char *cv_string_set(cg_var *cv, char *s0); 167 char *cv_strncpy(cg_var *cv, char *s0, size_t n); 168 struct in_addr *cv_ipv4addr_get(cg_var *cv); 169 struct in_addr *cv_ipv4addr_set(cg_var *cv, struct in_addr *addr); 170 uint8_t cv_ipv4masklen_get(cg_var *cv); 171 uint8_t cv_ipv4masklen_set(cg_var *cv, uint8_t masklen); 172 struct in6_addr *cv_ipv6addr_get(cg_var *cv); 173 uint8_t cv_ipv6masklen_get(cg_var *cv); 174 char *cv_mac_get(cg_var *cv); 175 unsigned char *cv_uuid_get(cg_var *cv); 176 unsigned char *cv_uuid_set(cg_var *cv, unsigned char *u); 177 struct timeval cv_time_get(cg_var *cv); 178 struct timeval cv_time_set(cg_var *cv, struct timeval t); 179 void *cv_void_get(cg_var *cv); 180 int cv_void_set(cg_var *cv, void *p); 181 char *cv_urlproto_get(cg_var *cv); 182 char *cv_urlproto_set(cg_var *cv, char *s0); 183 char *cv_urladdr_get(cg_var *cv); 184 char *cv_urladdr_set(cg_var *cv, char *s0); 185 char *cv_urlpath_get(cg_var *cv); 186 char *cv_urlpath_set(cg_var *cv, char *s0); 187 char *cv_urluser_get(cg_var *cv); 188 char *cv_urluser_set(cg_var *cv, char *s0); 189 char *cv_urlpasswd_get(cg_var *cv); 190 char *cv_urlpasswd_set(cg_var *cv, char *s0); 191 192 int parse_int8(char *str, int8_t *val, char **reason); 193 int parse_int16(char *str, int16_t *val, char **reason); 194 int parse_int32(char *str, int32_t *val, char **reason); 195 int parse_int64(char *str, int64_t *val, char **reason); 196 int parse_uint8(char *str, uint8_t *val, char **reason); 197 int parse_uint16(char *str, uint16_t *val, char **reason); 198 int parse_uint32(char *str, uint32_t *val, char **reason); 199 int parse_uint64(char *str, uint64_t *val, char **reason); 200 int parse_dec64(char *str, uint8_t n, int64_t *dec64_i, char **reason); 201 202 int str2urlproto(char *str); 203 int str2uuid(char *in, uuid_t u); 204 int uuid2str(uuid_t u, char *in, int len); 205 int cligen_tonum(int n, char *s); 206 int str2time(char *in, struct timeval *tv); 207 int time2str(struct timeval tv, char *fmt, int len); 208 209 enum cv_type cv_str2type(char *str); 210 char *cv_type2str(enum cv_type type); 211 int cv_len(cg_var *cgv); 212 int cv2cbuf(cg_var *cv, cbuf *cb); 213 int cv2str(cg_var *cv, char *str, size_t size); 214 char *cv2str_dup(cg_var *cv); 215 216 int cv_print(FILE *f, cg_var *cgv); 217 int cvtype_max2str(enum cv_type type, char *str, size_t size); 218 char *cvtype_max2str_dup(enum cv_type type); 219 220 int cv_max_set(cg_var *cv); 221 int cv_min_set(cg_var *cv); 222 223 int cv_cmp(cg_var *cgv1, cg_var *cgv2); 224 int cv_cp(cg_var *n, cg_var *old); 225 cg_var *cv_dup(cg_var *old); 226 int cv_parse(char *str, cg_var *cgv); 227 int cv_parse1(char *str, cg_var *cgv, char **reason); /* better err-handling */ 228 229 int cv_validate(cligen_handle h, cg_var *cv, struct cg_varspec *cs, char **reason); 230 int cv_reset(cg_var *cgv); /* not free cgv itself */ /* XXX: free_only */ 231 int cv_free(cg_var *cv); /* free cgv itself */ 232 cg_var *cv_new(enum cv_type type); 233 234 size_t cv_size(cg_var *cv); 235 236 #endif /* _CLIGEN_CV_H_ */ 237 238