diff options
author | Dave Barach <dave@barachs.net> | 2016-08-15 11:12:27 -0400 |
---|---|---|
committer | Dave Barach <dave@barachs.net> | 2016-08-15 11:12:40 -0400 |
commit | c379999665febd12ec55bfb3a7545224f2b39d3d (patch) | |
tree | 8bf0c11e52c2162e1239b6c3f4a5f74b62a07409 /vppinfra/vppinfra/macros.c | |
parent | b3d93dacfde8ab21bbce171fff2971b2ed7bce6a (diff) |
VPP-327 Coding standards cleanup for vppinfra
Fix additional a few additional deviations reported elsewhere by
checkstyle
Change-Id: I026a8ae1c5b1856bbe3c4a555e1b690e7501b045
Signed-off-by: Dave Barach <dave@barachs.net>
Diffstat (limited to 'vppinfra/vppinfra/macros.c')
-rw-r--r-- | vppinfra/vppinfra/macros.c | 266 |
1 files changed, 142 insertions, 124 deletions
diff --git a/vppinfra/vppinfra/macros.c b/vppinfra/vppinfra/macros.c index f16948ef0e4..ce4cc9bc81b 100644 --- a/vppinfra/vppinfra/macros.c +++ b/vppinfra/vppinfra/macros.c @@ -1,8 +1,8 @@ /* macros.c - a simple macro expander - + Copyright (c) 2010, 2014 Cisco and/or its affiliates. - + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -18,39 +18,40 @@ #include <vppinfra/macros.h> -static inline int macro_isalnum (i8 c) +static inline int +macro_isalnum (i8 c) { if ((c >= 'A' && c <= 'Z') - || (c >='a' && c <= 'z') - || (c >='0' && c <= '9') - || (c == '_')) + || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c == '_')) return 1; return 0; } -static i8 *builtin_eval(macro_main_t *mm, i8 *varname, i32 complain) +static i8 * +builtin_eval (macro_main_t * mm, i8 * varname, i32 complain) { - uword *p; - i8 *(*fp)(macro_main_t *, i32); - - p = hash_get_mem(mm->the_builtin_eval_hash, varname); - if (p == 0) - return 0; - fp = (void *)(p[0]); - return (*fp)(mm, complain); + uword *p; + i8 *(*fp) (macro_main_t *, i32); + + p = hash_get_mem (mm->the_builtin_eval_hash, varname); + if (p == 0) + return 0; + fp = (void *) (p[0]); + return (*fp) (mm, complain); } -int clib_macro_unset (macro_main_t * mm, char *name) +int +clib_macro_unset (macro_main_t * mm, char *name) { - hash_pair_t * p; - u8 * key, * value; + hash_pair_t *p; + u8 *key, *value; p = hash_get_pair (mm->the_value_table_hash, name); - + if (p == 0) return 1; - key = (u8 *)(p->key); + key = (u8 *) (p->key); value = (u8 *) (p->value[0]); hash_unset_mem (mm->the_value_table_hash, name); @@ -59,9 +60,10 @@ int clib_macro_unset (macro_main_t * mm, char *name) return 0; } -int clib_macro_set_value (macro_main_t * mm, char *name, char *value) +int +clib_macro_set_value (macro_main_t * mm, char *name, char *value) { - u8 * key_copy, *value_copy; + u8 *key_copy, *value_copy; int rv; rv = clib_macro_unset (mm, name); @@ -73,13 +75,14 @@ int clib_macro_set_value (macro_main_t * mm, char *name, char *value) return rv; } -i8 * clib_macro_get_value (macro_main_t * mm, char *name) +i8 * +clib_macro_get_value (macro_main_t * mm, char *name) { - uword * p; + uword *p; p = hash_get_mem (mm->the_value_table_hash, name); if (p) - return (i8 *)(p[0]); + return (i8 *) (p[0]); else return 0; } @@ -88,129 +91,134 @@ i8 * clib_macro_get_value (macro_main_t * mm, char *name) * eval: takes a string, returns a vector. * looks up $foobar in the variable table. */ -i8 * clib_macro_eval (macro_main_t *mm, i8 *s, i32 complain) +i8 * +clib_macro_eval (macro_main_t * mm, i8 * s, i32 complain) { - i8 *rv=0; + i8 *rv = 0; i8 *varname, *varvalue; i8 *ts; - while (*s) + while (*s) { - switch(*s) - { - case '\\': - s++; - /* fallthrough */ - - default: - vec_add1(rv, *s); - s++; - break; - - case '$': - s++; - varname = 0; - /* - * Make vector with variable name in it. - */ - while (*s && (macro_isalnum (*s) || (*s == '_') || (*s == '('))) - { - - /* handle $(foo) */ - if (*s == '(') - { - s++; /* skip '(' */ - while (*s && *s != ')') - { - vec_add1(varname, *s); - s++; - } - if (*s) - s++; /* skip ')' */ - break; - } - vec_add1(varname, *s); - s++; - } - /* null terminate */ - vec_add1(varname, 0); - /* Look for a builtin, e.g. $my_hostname */ - if (!(varvalue = builtin_eval(mm, varname, complain))) - { - /* Look in value table */ - if (! varvalue) - { - char * tmp = clib_macro_get_value (mm, varname); - if (tmp) - varvalue = (i8 *) format (0, "%s%c", tmp, 0); - } + switch (*s) + { + case '\\': + s++; + /* fallthrough */ + + default: + vec_add1 (rv, *s); + s++; + break; + + case '$': + s++; + varname = 0; + /* + * Make vector with variable name in it. + */ + while (*s && (macro_isalnum (*s) || (*s == '_') || (*s == '('))) + { + + /* handle $(foo) */ + if (*s == '(') + { + s++; /* skip '(' */ + while (*s && *s != ')') + { + vec_add1 (varname, *s); + s++; + } + if (*s) + s++; /* skip ')' */ + break; + } + vec_add1 (varname, *s); + s++; + } + /* null terminate */ + vec_add1 (varname, 0); + /* Look for a builtin, e.g. $my_hostname */ + if (!(varvalue = builtin_eval (mm, varname, complain))) + { + /* Look in value table */ + if (!varvalue) + { + char *tmp = clib_macro_get_value (mm, varname); + if (tmp) + varvalue = (i8 *) format (0, "%s%c", tmp, 0); + } #ifdef CLIB_UNIX - /* Look in environment. */ - if (! varvalue) - { - char * tmp = getenv (varname); - if (tmp) - varvalue = (i8 *) format (0, "%s%c", tmp, 0); - } + /* Look in environment. */ + if (!varvalue) + { + char *tmp = getenv (varname); + if (tmp) + varvalue = (i8 *) format (0, "%s%c", tmp, 0); + } #endif /* CLIB_UNIX */ - } - if (varvalue) - { - /* recursively evaluate */ - ts = clib_macro_eval(mm, varvalue, complain); - vec_free(varvalue); - /* add results to answer */ - vec_append(rv, ts); - /* Remove NULL termination or the results are sad */ - _vec_len(rv) = vec_len(rv)-1; - vec_free(ts); - } - else - { - if (complain) - clib_warning ("Undefined Variable Reference: %s\n", varname); - vec_append(rv, format(0, "UNSET ")); - _vec_len(rv) = vec_len(rv)-1; - - } - vec_free(varname); - } + } + if (varvalue) + { + /* recursively evaluate */ + ts = clib_macro_eval (mm, varvalue, complain); + vec_free (varvalue); + /* add results to answer */ + vec_append (rv, ts); + /* Remove NULL termination or the results are sad */ + _vec_len (rv) = vec_len (rv) - 1; + vec_free (ts); + } + else + { + if (complain) + clib_warning ("Undefined Variable Reference: %s\n", varname); + vec_append (rv, format (0, "UNSET ")); + _vec_len (rv) = vec_len (rv) - 1; + + } + vec_free (varname); + } } - vec_add1(rv, 0); - return(rv); + vec_add1 (rv, 0); + return (rv); } /* * eval: takes a string, returns a vector. * looks up $foobar in the variable table. */ -i8 *clib_macro_eval_dollar (macro_main_t *mm, i8 *s, i32 complain) +i8 * +clib_macro_eval_dollar (macro_main_t * mm, i8 * s, i32 complain) { i8 *s2; i8 *rv; - s2 = (i8 *)format (0, "$(%s)%c", s, 0); - rv = clib_macro_eval(mm, s2, complain); + s2 = (i8 *) format (0, "$(%s)%c", s, 0); + rv = clib_macro_eval (mm, s2, complain); vec_free (s2); return (rv); } -void clib_macro_add_builtin (macro_main_t *mm, char *name, void * eval_fn) +void +clib_macro_add_builtin (macro_main_t * mm, char *name, void *eval_fn) { - hash_set_mem(mm->the_builtin_eval_hash, name, (uword) eval_fn); + hash_set_mem (mm->the_builtin_eval_hash, name, (uword) eval_fn); } #ifdef CLIB_UNIX -static i8 *eval_hostname (macro_main_t *mm, i32 complain) +static i8 * +eval_hostname (macro_main_t * mm, i32 complain) { char tmp[128]; - if (gethostname (tmp, sizeof(tmp))) + if (gethostname (tmp, sizeof (tmp))) return ((i8 *) format (0, "gethostname-error%c", 0)); return ((i8 *) format (0, "%s%c", tmp, 0)); } #endif -void clib_macro_init(macro_main_t * mm) +void +clib_macro_init (macro_main_t * mm) { if (mm->the_builtin_eval_hash != 0) { @@ -218,31 +226,41 @@ void clib_macro_init(macro_main_t * mm) return; } - mm->the_builtin_eval_hash = hash_create_string(0, sizeof(uword)); - mm->the_value_table_hash = hash_create_string(0, sizeof(uword)); - + mm->the_builtin_eval_hash = hash_create_string (0, sizeof (uword)); + mm->the_value_table_hash = hash_create_string (0, sizeof (uword)); + #ifdef CLIB_UNIX - hash_set_mem(mm->the_builtin_eval_hash, "hostname", - (uword) eval_hostname); + hash_set_mem (mm->the_builtin_eval_hash, "hostname", (uword) eval_hostname); #endif } -void clib_macro_free(macro_main_t * mm) +void +clib_macro_free (macro_main_t * mm) { - hash_pair_t * p; - u8 ** strings_to_free = 0; + hash_pair_t *p; + u8 **strings_to_free = 0; int i; hash_free (mm->the_builtin_eval_hash); - hash_foreach_pair (p, mm->the_value_table_hash, + /* *INDENT-OFF* */ + hash_foreach_pair (p, mm->the_value_table_hash, ({ vec_add1 (strings_to_free, (u8 *) (p->key)); vec_add1 (strings_to_free, (u8 *) (p->value[0])); })); + /* *INDENT-ON* */ for (i = 0; i < vec_len (strings_to_free); i++) vec_free (strings_to_free[i]); vec_free (strings_to_free); hash_free (mm->the_value_table_hash); } + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ |