2 macros.c - a simple macro expander
4 Copyright (c) 2010, 2014 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 (macro_main_t * mm, i8 * varname, i32 complain)
34 i8 *(*fp) (macro_main_t *, i32);
36 p = hash_get_mem (mm->the_builtin_eval_hash, varname);
40 return (*fp) (mm, complain);
44 clib_macro_unset (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 (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 (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 (macro_main_t * mm, i8 * s, i32 complain)
98 i8 *varname, *varvalue;
118 * Make vector with variable name in it.
120 while (*s && (macro_isalnum (*s) || (*s == '_') || (*s == '(')))
127 while (*s && *s != ')')
129 vec_add1 (varname, *s);
136 vec_add1 (varname, *s);
140 vec_add1 (varname, 0);
141 /* Look for a builtin, e.g. $my_hostname */
142 if (!(varvalue = builtin_eval (mm, varname, complain)))
144 /* Look in value table */
147 i8 *tmp = clib_macro_get_value (mm, (char *) varname);
149 varvalue = (i8 *) format (0, "%s%c", tmp, 0);
152 /* Look in environment. */
155 char *tmp = getenv ((char *) varname);
157 varvalue = (i8 *) format (0, "%s%c", tmp, 0);
159 #endif /* CLIB_UNIX */
163 /* recursively evaluate */
164 ts = clib_macro_eval (mm, varvalue, complain);
166 /* add results to answer */
168 /* Remove NULL termination or the results are sad */
169 _vec_len (rv) = vec_len (rv) - 1;
175 clib_warning ("Undefined Variable Reference: %s\n", varname);
176 vec_append (rv, format (0, "UNSET "));
177 _vec_len (rv) = vec_len (rv) - 1;
188 * eval: takes a string, returns a vector.
189 * looks up $foobar in the variable table.
192 clib_macro_eval_dollar (macro_main_t * mm, i8 * s, i32 complain)
197 s2 = (i8 *) format (0, "$(%s)%c", s, 0);
198 rv = clib_macro_eval (mm, s2, complain);
204 clib_macro_add_builtin (macro_main_t * mm, char *name, void *eval_fn)
206 hash_set_mem (mm->the_builtin_eval_hash, name, (uword) eval_fn);
211 eval_hostname (macro_main_t * mm, i32 complain)
214 if (gethostname (tmp, sizeof (tmp)))
215 return ((i8 *) format (0, "gethostname-error%c", 0));
216 return ((i8 *) format (0, "%s%c", tmp, 0));
221 clib_macro_init (macro_main_t * mm)
223 if (mm->the_builtin_eval_hash != 0)
225 clib_warning ("mm %p already initialized", mm);
229 mm->the_builtin_eval_hash = hash_create_string (0, sizeof (uword));
230 mm->the_value_table_hash = hash_create_string (0, sizeof (uword));
233 hash_set_mem (mm->the_builtin_eval_hash, "hostname", (uword) eval_hostname);
238 clib_macro_free (macro_main_t * mm)
241 u8 **strings_to_free = 0;
244 hash_free (mm->the_builtin_eval_hash);
247 hash_foreach_pair (p, mm->the_value_table_hash,
249 vec_add1 (strings_to_free, (u8 *) (p->key));
250 vec_add1 (strings_to_free, (u8 *) (p->value[0]));
254 for (i = 0; i < vec_len (strings_to_free); i++)
255 vec_free (strings_to_free[i]);
256 vec_free (strings_to_free);
257 hash_free (mm->the_value_table_hash);
261 * fd.io coding-style-patch-verification: ON
264 * eval: (c-set-style "gnu")