blob: 6649277d897659812d61fe5ee1638c4f447cddd4 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 macros.c - a simple macro expander
Dave Barachc3799992016-08-15 11:12:27 -04003
Ed Warnickecb9cada2015-12-08 15:45:58 -07004 Copyright (c) 2010, 2014 Cisco and/or its affiliates.
Dave Barachc3799992016-08-15 11:12:27 -04005
Ed Warnickecb9cada2015-12-08 15:45:58 -07006 * 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:
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 *
31builtin_eval (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;
34 i8 *(*fp) (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
44clib_macro_unset (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
64clib_macro_set_value (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 *
79clib_macro_get_value (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 *
95clib_macro_eval (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 *
192clib_macro_eval_dollar (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
204clib_macro_add_builtin (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 *
211eval_hostname (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
221clib_macro_init (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
238clib_macro_free (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
260/*
261 * fd.io coding-style-patch-verification: ON
262 *
263 * Local Variables:
264 * eval: (c-set-style "gnu")
265 * End:
266 */