blob: bc6df55b39dff6f22d1fc50ddba54bb800f6ea4b [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
Dave Barach961e3c82020-06-18 17:04:18 -04002 * 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 Warnickecb9cada2015-12-08 15:45:58 -07007 * 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 Barachc3799992016-08-15 11:12:27 -040021static inline int
22macro_isalnum (i8 c)
Ed Warnickecb9cada2015-12-08 15:45:58 -070023{
24 if ((c >= 'A' && c <= 'Z')
Dave Barachc3799992016-08-15 11:12:27 -040025 || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c == '_'))
Ed Warnickecb9cada2015-12-08 15:45:58 -070026 return 1;
27 return 0;
28}
29
Dave Barachc3799992016-08-15 11:12:27 -040030static i8 *
Dave Barach961e3c82020-06-18 17:04:18 -040031builtin_eval (clib_macro_main_t * mm, i8 * varname, i32 complain)
Ed Warnickecb9cada2015-12-08 15:45:58 -070032{
Dave Barachc3799992016-08-15 11:12:27 -040033 uword *p;
Dave Barach961e3c82020-06-18 17:04:18 -040034 i8 *(*fp) (clib_macro_main_t *, i32);
Ed Warnickecb9cada2015-12-08 15:45:58 -070035
Dave Barachc3799992016-08-15 11:12:27 -040036 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 Warnickecb9cada2015-12-08 15:45:58 -070041}
42
Dave Barachc3799992016-08-15 11:12:27 -040043int
Dave Barach961e3c82020-06-18 17:04:18 -040044clib_macro_unset (clib_macro_main_t * mm, char *name)
Ed Warnickecb9cada2015-12-08 15:45:58 -070045{
Dave Barachc3799992016-08-15 11:12:27 -040046 hash_pair_t *p;
47 u8 *key, *value;
Ed Warnickecb9cada2015-12-08 15:45:58 -070048
49 p = hash_get_pair (mm->the_value_table_hash, name);
Dave Barachc3799992016-08-15 11:12:27 -040050
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 if (p == 0)
52 return 1;
53
Dave Barachc3799992016-08-15 11:12:27 -040054 key = (u8 *) (p->key);
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 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
Dave Barachc3799992016-08-15 11:12:27 -040063int
Dave Barach961e3c82020-06-18 17:04:18 -040064clib_macro_set_value (clib_macro_main_t * mm, char *name, char *value)
Ed Warnickecb9cada2015-12-08 15:45:58 -070065{
Dave Barachc3799992016-08-15 11:12:27 -040066 u8 *key_copy, *value_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -070067 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 Barachc3799992016-08-15 11:12:27 -040078i8 *
Dave Barach961e3c82020-06-18 17:04:18 -040079clib_macro_get_value (clib_macro_main_t * mm, char *name)
Ed Warnickecb9cada2015-12-08 15:45:58 -070080{
Dave Barachc3799992016-08-15 11:12:27 -040081 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83 p = hash_get_mem (mm->the_value_table_hash, name);
84 if (p)
Dave Barachc3799992016-08-15 11:12:27 -040085 return (i8 *) (p[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 else
87 return 0;
88}
89
90/*
91 * eval: takes a string, returns a vector.
92 * looks up $foobar in the variable table.
93 */
Dave Barachc3799992016-08-15 11:12:27 -040094i8 *
Dave Barach961e3c82020-06-18 17:04:18 -040095clib_macro_eval (clib_macro_main_t * mm, i8 * s, i32 complain)
Ed Warnickecb9cada2015-12-08 15:45:58 -070096{
Dave Barachc3799992016-08-15 11:12:27 -040097 i8 *rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070098 i8 *varname, *varvalue;
99 i8 *ts;
100
Dave Barachc3799992016-08-15 11:12:27 -0400101 while (*s)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102 {
Dave Barachc3799992016-08-15 11:12:27 -0400103 switch (*s)
104 {
105 case '\\':
106 s++;
107 /* fallthrough */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
Dave Barachc3799992016-08-15 11:12:27 -0400109 default:
110 vec_add1 (rv, *s);
111 s++;
112 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113
Dave Barachc3799992016-08-15 11:12:27 -0400114 case '$':
115 s++;
116 varname = 0;
117 /*
118 * Make vector with variable name in it.
119 */
120 while (*s && (macro_isalnum (*s) || (*s == '_') || (*s == '(')))
121 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122
Dave Barachc3799992016-08-15 11:12:27 -0400123 /* handle $(foo) */
124 if (*s == '(')
125 {
126 s++; /* skip '(' */
127 while (*s && *s != ')')
128 {
129 vec_add1 (varname, *s);
130 s++;
131 }
132 if (*s)
133 s++; /* skip ')' */
134 break;
135 }
136 vec_add1 (varname, *s);
137 s++;
138 }
139 /* null terminate */
140 vec_add1 (varname, 0);
141 /* Look for a builtin, e.g. $my_hostname */
142 if (!(varvalue = builtin_eval (mm, varname, complain)))
143 {
144 /* Look in value table */
145 if (!varvalue)
146 {
Damjan Marionffe9d212018-05-30 09:26:11 +0200147 i8 *tmp = clib_macro_get_value (mm, (char *) varname);
Dave Barachc3799992016-08-15 11:12:27 -0400148 if (tmp)
149 varvalue = (i8 *) format (0, "%s%c", tmp, 0);
150 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -0400152 /* Look in environment. */
153 if (!varvalue)
154 {
Damjan Marionffe9d212018-05-30 09:26:11 +0200155 char *tmp = getenv ((char *) varname);
Dave Barachc3799992016-08-15 11:12:27 -0400156 if (tmp)
157 varvalue = (i8 *) format (0, "%s%c", tmp, 0);
158 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159#endif /* CLIB_UNIX */
Dave Barachc3799992016-08-15 11:12:27 -0400160 }
161 if (varvalue)
162 {
163 /* recursively evaluate */
164 ts = clib_macro_eval (mm, varvalue, complain);
165 vec_free (varvalue);
166 /* add results to answer */
167 vec_append (rv, ts);
168 /* Remove NULL termination or the results are sad */
169 _vec_len (rv) = vec_len (rv) - 1;
170 vec_free (ts);
171 }
172 else
173 {
174 if (complain)
175 clib_warning ("Undefined Variable Reference: %s\n", varname);
176 vec_append (rv, format (0, "UNSET "));
177 _vec_len (rv) = vec_len (rv) - 1;
178
179 }
180 vec_free (varname);
181 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182 }
Dave Barachc3799992016-08-15 11:12:27 -0400183 vec_add1 (rv, 0);
184 return (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185}
186
187/*
188 * eval: takes a string, returns a vector.
189 * looks up $foobar in the variable table.
190 */
Dave Barachc3799992016-08-15 11:12:27 -0400191i8 *
Dave Barach961e3c82020-06-18 17:04:18 -0400192clib_macro_eval_dollar (clib_macro_main_t * mm, i8 * s, i32 complain)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193{
194 i8 *s2;
195 i8 *rv;
196
Dave Barachc3799992016-08-15 11:12:27 -0400197 s2 = (i8 *) format (0, "$(%s)%c", s, 0);
198 rv = clib_macro_eval (mm, s2, complain);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199 vec_free (s2);
200 return (rv);
201}
202
Dave Barachc3799992016-08-15 11:12:27 -0400203void
Dave Barach961e3c82020-06-18 17:04:18 -0400204clib_macro_add_builtin (clib_macro_main_t * mm, char *name, void *eval_fn)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205{
Dave Barachc3799992016-08-15 11:12:27 -0400206 hash_set_mem (mm->the_builtin_eval_hash, name, (uword) eval_fn);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207}
208
209#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -0400210static i8 *
Dave Barach961e3c82020-06-18 17:04:18 -0400211eval_hostname (clib_macro_main_t * mm, i32 complain)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212{
213 char tmp[128];
Dave Barachc3799992016-08-15 11:12:27 -0400214 if (gethostname (tmp, sizeof (tmp)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215 return ((i8 *) format (0, "gethostname-error%c", 0));
216 return ((i8 *) format (0, "%s%c", tmp, 0));
217}
218#endif
219
Dave Barachc3799992016-08-15 11:12:27 -0400220void
Dave Barach961e3c82020-06-18 17:04:18 -0400221clib_macro_init (clib_macro_main_t * mm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222{
223 if (mm->the_builtin_eval_hash != 0)
224 {
Christophe Fontainefef15b42016-04-09 12:38:49 +0900225 clib_warning ("mm %p already initialized", mm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226 return;
227 }
228
Dave Barachc3799992016-08-15 11:12:27 -0400229 mm->the_builtin_eval_hash = hash_create_string (0, sizeof (uword));
230 mm->the_value_table_hash = hash_create_string (0, sizeof (uword));
231
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -0400233 hash_set_mem (mm->the_builtin_eval_hash, "hostname", (uword) eval_hostname);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234#endif
235}
236
Dave Barachc3799992016-08-15 11:12:27 -0400237void
Dave Barach961e3c82020-06-18 17:04:18 -0400238clib_macro_free (clib_macro_main_t * mm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239{
Dave Barachc3799992016-08-15 11:12:27 -0400240 hash_pair_t *p;
241 u8 **strings_to_free = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242 int i;
243
244 hash_free (mm->the_builtin_eval_hash);
245
Dave Barachc3799992016-08-15 11:12:27 -0400246 /* *INDENT-OFF* */
247 hash_foreach_pair (p, mm->the_value_table_hash,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248 ({
249 vec_add1 (strings_to_free, (u8 *) (p->key));
250 vec_add1 (strings_to_free, (u8 *) (p->value[0]));
251 }));
Dave Barachc3799992016-08-15 11:12:27 -0400252 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253
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);
258}
Dave Barachc3799992016-08-15 11:12:27 -0400259
Dave Barach961e3c82020-06-18 17:04:18 -0400260typedef struct
261{
262 u8 *name;
263 u8 *value;
264} name_sort_t;
265
266static int
267name_compare (void *a1, void *a2)
268{
269 name_sort_t *ns1 = a1;
270 name_sort_t *ns2 = a2;
271
272 return strcmp ((char *) ns1->name, (char *) ns2->name);
273}
274
275
276u8 *
277format_clib_macro_main (u8 * s, va_list * args)
278{
279 clib_macro_main_t *mm = va_arg (*args, clib_macro_main_t *);
280 int evaluate = va_arg (*args, int);
281 hash_pair_t *p;
282 name_sort_t *nses = 0, *ns;
283 int i;
284
285 /* *INDENT-OFF* */
286 hash_foreach_pair (p, mm->the_value_table_hash,
287 ({
288 vec_add2 (nses, ns, 1);
289 ns->name = (u8 *)(p->key);
290 ns->value = (u8 *)(p->value[0]);
291 }));
292 /* *INDENT-ON* */
293
294 if (vec_len (nses) == 0)
295 return s;
296
297 vec_sort_with_function (nses, name_compare);
298
299 for (i = 0; i < vec_len (nses); i++)
300 {
301 s = format (s, "%-20s", nses[i].name);
302 if (evaluate == 0)
303 s = format (s, "%s\n", nses[i].value);
304 else
305 {
306 u8 *rv = (u8 *) clib_macro_eval_dollar (mm, (i8 *) nses[i].name,
307 0 /* no complain */ );
308 s = format (s, "%s\n", rv);
309 vec_free (rv);
310 }
311 }
312 return s;
313}
314
315
Dave Barachc3799992016-08-15 11:12:27 -0400316/*
317 * fd.io coding-style-patch-verification: ON
318 *
319 * Local Variables:
320 * eval: (c-set-style "gnu")
321 * End:
322 */