2 * macros.c - a simple macro expander
4 * Copyright (c) 2010-2020 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <vppinfra/macros.h>
24 if ((c >= 'A' && c <= 'Z')
25 || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c == '_'))
31 builtin_eval (clib_macro_main_t * mm, i8 * varname, i32 complain)
34 i8 *(*fp) (clib_macro_main_t *, i32);
36 p = hash_get_mem (mm->the_builtin_eval_hash, varname);
40 return (*fp) (mm, complain);
44 clib_macro_unset (clib_macro_main_t * mm, char *name)
49 p = hash_get_pair (mm->the_value_table_hash, name);
54 key = (u8 *) (p->key);
55 value = (u8 *) (p->value[0]);
56 hash_unset_mem (mm->the_value_table_hash, name);
64 clib_macro_set_value (clib_macro_main_t * mm, char *name, char *value)
66 u8 *key_copy, *value_copy;
69 rv = clib_macro_unset (mm, name);
71 key_copy = format (0, "%s%c", name, 0);
72 value_copy = format (0, "%s%c", value, 0);
74 hash_set_mem (mm->the_value_table_hash, key_copy, value_copy);
79 clib_macro_get_value (clib_macro_main_t * mm, char *name)
83 p = hash_get_mem (mm->the_value_table_hash, name);
91 * eval: takes a string, returns a vector.
92 * looks up $foobar in the variable table.
95 clib_macro_eval (clib_macro_main_t * mm, i8 * s, i32 complain, u16 level,
99 i8 *varname, *varvalue;
102 if (level >= max_level)
105 clib_warning ("circular definition, level %d", level);
106 return (i8 *) format (0, " CIRCULAR ");
126 * Make vector with variable name in it.
128 while (*s && (macro_isalnum (*s) || (*s == '_') || (*s == '(')))
135 while (*s && *s != ')')
137 vec_add1 (varname, *s);
144 vec_add1 (varname, *s);
148 vec_add1 (varname, 0);
149 /* Look for a builtin, e.g. $my_hostname */
150 if (!(varvalue = builtin_eval (mm, varname, complain)))
152 /* Look in value table */
155 i8 *tmp = clib_macro_get_value (mm, (char *) varname);
157 varvalue = (i8 *) format (0, "%s%c", tmp, 0);
160 /* Look in environment. */
163 char *tmp = getenv ((char *) varname);
165 varvalue = (i8 *) format (0, "%s%c", tmp, 0);
167 #endif /* CLIB_UNIX */
171 /* recursively evaluate */
172 ts = clib_macro_eval (mm, varvalue, complain, level + 1,
175 /* add results to answer */
177 /* Remove NULL termination or the results are sad */
178 vec_set_len (rv, vec_len (rv) - 1);
184 clib_warning ("Undefined Variable Reference: %s\n", varname);
185 vec_append (rv, format (0, "UNSET "));
186 vec_set_len (rv, vec_len (rv) - 1);
196 * eval: takes a string, returns a vector.
197 * looks up $foobar in the variable table.
200 clib_macro_eval_dollar (clib_macro_main_t *mm, i8 *s, i32 complain)
205 s2 = (i8 *) format (0, "$(%s)%c", s, 0);
206 rv = clib_macro_eval (mm, s2, complain, 0 /* level */ , 8 /* max_level */ );
212 clib_macro_add_builtin (clib_macro_main_t * mm, char *name, void *eval_fn)
214 hash_set_mem (mm->the_builtin_eval_hash, name, (uword) eval_fn);
219 eval_hostname (clib_macro_main_t * mm, i32 complain)
222 if (gethostname (tmp, sizeof (tmp)))
223 return ((i8 *) format (0, "gethostname-error%c", 0));
224 return ((i8 *) format (0, "%s%c", tmp, 0));
229 clib_macro_init (clib_macro_main_t * mm)
231 if (mm->the_builtin_eval_hash != 0)
233 clib_warning ("mm %p already initialized", mm);
237 mm->the_builtin_eval_hash = hash_create_string (0, sizeof (uword));
238 mm->the_value_table_hash = hash_create_string (0, sizeof (uword));
241 hash_set_mem (mm->the_builtin_eval_hash, "hostname", (uword) eval_hostname);
246 clib_macro_free (clib_macro_main_t * mm)
249 u8 **strings_to_free = 0;
252 hash_free (mm->the_builtin_eval_hash);
255 hash_foreach_pair (p, mm->the_value_table_hash,
257 vec_add1 (strings_to_free, (u8 *) (p->key));
258 vec_add1 (strings_to_free, (u8 *) (p->value[0]));
262 for (i = 0; i < vec_len (strings_to_free); i++)
263 vec_free (strings_to_free[i]);
264 vec_free (strings_to_free);
265 hash_free (mm->the_value_table_hash);
275 name_compare (void *a1, void *a2)
277 name_sort_t *ns1 = a1;
278 name_sort_t *ns2 = a2;
280 return strcmp ((char *) ns1->name, (char *) ns2->name);
285 format_clib_macro_main (u8 * s, va_list * args)
287 clib_macro_main_t *mm = va_arg (*args, clib_macro_main_t *);
288 int evaluate = va_arg (*args, int);
290 name_sort_t *nses = 0, *ns;
294 hash_foreach_pair (p, mm->the_value_table_hash,
296 vec_add2 (nses, ns, 1);
297 ns->name = (u8 *)(p->key);
298 ns->value = (u8 *)(p->value[0]);
302 if (vec_len (nses) == 0)
305 vec_sort_with_function (nses, name_compare);
307 for (i = 0; i < vec_len (nses); i++)
309 s = format (s, "%-20s", nses[i].name);
311 s = format (s, "%s\n", nses[i].value);
314 u8 *rv = (u8 *) clib_macro_eval_dollar (mm, (i8 *) nses[i].name,
315 0 /* no complain */ );
316 s = format (s, "%s\n", rv);
325 * fd.io coding-style-patch-verification: ON
328 * eval: (c-set-style "gnu")