Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 2 | * macros.c - a simple macro expander |
| 3 | * |
| 4 | * Copyright (c) 2010-2020 Cisco and/or its affiliates. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include <vppinfra/macros.h> |
| 20 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 21 | static inline int |
| 22 | macro_isalnum (i8 c) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | { |
| 24 | if ((c >= 'A' && c <= 'Z') |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 25 | || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c == '_')) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | return 1; |
| 27 | return 0; |
| 28 | } |
| 29 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 30 | static i8 * |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 31 | builtin_eval (clib_macro_main_t * mm, i8 * varname, i32 complain) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 33 | uword *p; |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 34 | i8 *(*fp) (clib_macro_main_t *, i32); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 35 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 36 | p = hash_get_mem (mm->the_builtin_eval_hash, varname); |
| 37 | if (p == 0) |
| 38 | return 0; |
| 39 | fp = (void *) (p[0]); |
| 40 | return (*fp) (mm, complain); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame^] | 43 | __clib_export int |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 44 | clib_macro_unset (clib_macro_main_t * mm, char *name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 46 | hash_pair_t *p; |
| 47 | u8 *key, *value; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | |
| 49 | p = hash_get_pair (mm->the_value_table_hash, name); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 50 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | if (p == 0) |
| 52 | return 1; |
| 53 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 54 | key = (u8 *) (p->key); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | value = (u8 *) (p->value[0]); |
| 56 | hash_unset_mem (mm->the_value_table_hash, name); |
| 57 | |
| 58 | vec_free (value); |
| 59 | vec_free (key); |
| 60 | return 0; |
| 61 | } |
| 62 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame^] | 63 | __clib_export int |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 64 | clib_macro_set_value (clib_macro_main_t * mm, char *name, char *value) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 66 | u8 *key_copy, *value_copy; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | int rv; |
| 68 | |
| 69 | rv = clib_macro_unset (mm, name); |
| 70 | |
| 71 | key_copy = format (0, "%s%c", name, 0); |
| 72 | value_copy = format (0, "%s%c", value, 0); |
| 73 | |
| 74 | hash_set_mem (mm->the_value_table_hash, key_copy, value_copy); |
| 75 | return rv; |
| 76 | } |
| 77 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 78 | i8 * |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 79 | clib_macro_get_value (clib_macro_main_t * mm, char *name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 81 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | |
| 83 | p = hash_get_mem (mm->the_value_table_hash, name); |
| 84 | if (p) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 85 | return (i8 *) (p[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | else |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * eval: takes a string, returns a vector. |
| 92 | * looks up $foobar in the variable table. |
| 93 | */ |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame^] | 94 | __clib_export i8 * |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 95 | clib_macro_eval (clib_macro_main_t * mm, i8 * s, i32 complain, u16 level, |
| 96 | u16 max_level) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 98 | i8 *rv = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | i8 *varname, *varvalue; |
| 100 | i8 *ts; |
| 101 | |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 102 | if (level >= max_level) |
| 103 | { |
| 104 | if (complain) |
| 105 | clib_warning ("circular definition, level %d", level); |
| 106 | return (i8 *) format (0, " CIRCULAR "); |
| 107 | } |
| 108 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 109 | while (*s) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 111 | switch (*s) |
| 112 | { |
| 113 | case '\\': |
| 114 | s++; |
| 115 | /* fallthrough */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 117 | default: |
| 118 | vec_add1 (rv, *s); |
| 119 | s++; |
| 120 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 122 | case '$': |
| 123 | s++; |
| 124 | varname = 0; |
| 125 | /* |
| 126 | * Make vector with variable name in it. |
| 127 | */ |
| 128 | while (*s && (macro_isalnum (*s) || (*s == '_') || (*s == '('))) |
| 129 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 131 | /* handle $(foo) */ |
| 132 | if (*s == '(') |
| 133 | { |
| 134 | s++; /* skip '(' */ |
| 135 | while (*s && *s != ')') |
| 136 | { |
| 137 | vec_add1 (varname, *s); |
| 138 | s++; |
| 139 | } |
| 140 | if (*s) |
| 141 | s++; /* skip ')' */ |
| 142 | break; |
| 143 | } |
| 144 | vec_add1 (varname, *s); |
| 145 | s++; |
| 146 | } |
| 147 | /* null terminate */ |
| 148 | vec_add1 (varname, 0); |
| 149 | /* Look for a builtin, e.g. $my_hostname */ |
| 150 | if (!(varvalue = builtin_eval (mm, varname, complain))) |
| 151 | { |
| 152 | /* Look in value table */ |
| 153 | if (!varvalue) |
| 154 | { |
Damjan Marion | ffe9d21 | 2018-05-30 09:26:11 +0200 | [diff] [blame] | 155 | i8 *tmp = clib_macro_get_value (mm, (char *) varname); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 156 | if (tmp) |
| 157 | varvalue = (i8 *) format (0, "%s%c", tmp, 0); |
| 158 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | #ifdef CLIB_UNIX |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 160 | /* Look in environment. */ |
| 161 | if (!varvalue) |
| 162 | { |
Damjan Marion | ffe9d21 | 2018-05-30 09:26:11 +0200 | [diff] [blame] | 163 | char *tmp = getenv ((char *) varname); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 164 | if (tmp) |
| 165 | varvalue = (i8 *) format (0, "%s%c", tmp, 0); |
| 166 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | #endif /* CLIB_UNIX */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 168 | } |
| 169 | if (varvalue) |
| 170 | { |
| 171 | /* recursively evaluate */ |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 172 | ts = clib_macro_eval (mm, varvalue, complain, level + 1, |
| 173 | max_level); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 174 | vec_free (varvalue); |
| 175 | /* add results to answer */ |
| 176 | vec_append (rv, ts); |
| 177 | /* Remove NULL termination or the results are sad */ |
| 178 | _vec_len (rv) = vec_len (rv) - 1; |
| 179 | vec_free (ts); |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | if (complain) |
| 184 | clib_warning ("Undefined Variable Reference: %s\n", varname); |
| 185 | vec_append (rv, format (0, "UNSET ")); |
| 186 | _vec_len (rv) = vec_len (rv) - 1; |
| 187 | |
| 188 | } |
| 189 | vec_free (varname); |
| 190 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 192 | vec_add1 (rv, 0); |
| 193 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
| 197 | * eval: takes a string, returns a vector. |
| 198 | * looks up $foobar in the variable table. |
| 199 | */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 200 | i8 * |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 201 | clib_macro_eval_dollar (clib_macro_main_t * mm, i8 * s, i32 complain) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | { |
| 203 | i8 *s2; |
| 204 | i8 *rv; |
| 205 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 206 | s2 = (i8 *) format (0, "$(%s)%c", s, 0); |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 207 | rv = clib_macro_eval (mm, s2, complain, 0 /* level */ , 8 /* max_level */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | vec_free (s2); |
| 209 | return (rv); |
| 210 | } |
| 211 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame^] | 212 | __clib_export void |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 213 | clib_macro_add_builtin (clib_macro_main_t * mm, char *name, void *eval_fn) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 215 | hash_set_mem (mm->the_builtin_eval_hash, name, (uword) eval_fn); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | #ifdef CLIB_UNIX |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 219 | static i8 * |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 220 | eval_hostname (clib_macro_main_t * mm, i32 complain) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | { |
| 222 | char tmp[128]; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 223 | if (gethostname (tmp, sizeof (tmp))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | return ((i8 *) format (0, "gethostname-error%c", 0)); |
| 225 | return ((i8 *) format (0, "%s%c", tmp, 0)); |
| 226 | } |
| 227 | #endif |
| 228 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame^] | 229 | __clib_export void |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 230 | clib_macro_init (clib_macro_main_t * mm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | { |
| 232 | if (mm->the_builtin_eval_hash != 0) |
| 233 | { |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 234 | clib_warning ("mm %p already initialized", mm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 235 | return; |
| 236 | } |
| 237 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 238 | mm->the_builtin_eval_hash = hash_create_string (0, sizeof (uword)); |
| 239 | mm->the_value_table_hash = hash_create_string (0, sizeof (uword)); |
| 240 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | #ifdef CLIB_UNIX |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 242 | hash_set_mem (mm->the_builtin_eval_hash, "hostname", (uword) eval_hostname); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | #endif |
| 244 | } |
| 245 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame^] | 246 | __clib_export void |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 247 | clib_macro_free (clib_macro_main_t * mm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 248 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 249 | hash_pair_t *p; |
| 250 | u8 **strings_to_free = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | int i; |
| 252 | |
| 253 | hash_free (mm->the_builtin_eval_hash); |
| 254 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 255 | /* *INDENT-OFF* */ |
| 256 | hash_foreach_pair (p, mm->the_value_table_hash, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 257 | ({ |
| 258 | vec_add1 (strings_to_free, (u8 *) (p->key)); |
| 259 | vec_add1 (strings_to_free, (u8 *) (p->value[0])); |
| 260 | })); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 261 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | |
| 263 | for (i = 0; i < vec_len (strings_to_free); i++) |
| 264 | vec_free (strings_to_free[i]); |
| 265 | vec_free (strings_to_free); |
| 266 | hash_free (mm->the_value_table_hash); |
| 267 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 268 | |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 269 | typedef struct |
| 270 | { |
| 271 | u8 *name; |
| 272 | u8 *value; |
| 273 | } name_sort_t; |
| 274 | |
| 275 | static int |
| 276 | name_compare (void *a1, void *a2) |
| 277 | { |
| 278 | name_sort_t *ns1 = a1; |
| 279 | name_sort_t *ns2 = a2; |
| 280 | |
| 281 | return strcmp ((char *) ns1->name, (char *) ns2->name); |
| 282 | } |
| 283 | |
| 284 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame^] | 285 | __clib_export u8 * |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 286 | format_clib_macro_main (u8 * s, va_list * args) |
| 287 | { |
| 288 | clib_macro_main_t *mm = va_arg (*args, clib_macro_main_t *); |
| 289 | int evaluate = va_arg (*args, int); |
| 290 | hash_pair_t *p; |
| 291 | name_sort_t *nses = 0, *ns; |
| 292 | int i; |
| 293 | |
| 294 | /* *INDENT-OFF* */ |
| 295 | hash_foreach_pair (p, mm->the_value_table_hash, |
| 296 | ({ |
| 297 | vec_add2 (nses, ns, 1); |
| 298 | ns->name = (u8 *)(p->key); |
| 299 | ns->value = (u8 *)(p->value[0]); |
| 300 | })); |
| 301 | /* *INDENT-ON* */ |
| 302 | |
| 303 | if (vec_len (nses) == 0) |
| 304 | return s; |
| 305 | |
| 306 | vec_sort_with_function (nses, name_compare); |
| 307 | |
| 308 | for (i = 0; i < vec_len (nses); i++) |
| 309 | { |
| 310 | s = format (s, "%-20s", nses[i].name); |
| 311 | if (evaluate == 0) |
| 312 | s = format (s, "%s\n", nses[i].value); |
| 313 | else |
| 314 | { |
| 315 | u8 *rv = (u8 *) clib_macro_eval_dollar (mm, (i8 *) nses[i].name, |
| 316 | 0 /* no complain */ ); |
| 317 | s = format (s, "%s\n", rv); |
| 318 | vec_free (rv); |
| 319 | } |
| 320 | } |
| 321 | return s; |
| 322 | } |
| 323 | |
| 324 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 325 | /* |
| 326 | * fd.io coding-style-patch-verification: ON |
| 327 | * |
| 328 | * Local Variables: |
| 329 | * eval: (c-set-style "gnu") |
| 330 | * End: |
| 331 | */ |