vlib: debug CLI macro expander, part deux

Deal with circular macro definitions instead of crashing due to stack
overflow.

Separate macro tables, per CLI session

Add documentation to the Sphinx docs

Type: improvement

Signed-off-by: Dave Barach <dave@barachs.net>
Change-Id: I55fc9152bd37ad0c15fa3959f38b07b63100e634
diff --git a/src/vppinfra/macros.c b/src/vppinfra/macros.c
index bc6df55..240cef0 100644
--- a/src/vppinfra/macros.c
+++ b/src/vppinfra/macros.c
@@ -92,12 +92,20 @@
  * looks up $foobar in the variable table.
  */
 i8 *
-clib_macro_eval (clib_macro_main_t * mm, i8 * s, i32 complain)
+clib_macro_eval (clib_macro_main_t * mm, i8 * s, i32 complain, u16 level,
+		 u16 max_level)
 {
   i8 *rv = 0;
   i8 *varname, *varvalue;
   i8 *ts;
 
+  if (level >= max_level)
+    {
+      if (complain)
+	clib_warning ("circular definition, level %d", level);
+      return (i8 *) format (0, " CIRCULAR ");
+    }
+
   while (*s)
     {
       switch (*s)
@@ -161,7 +169,8 @@
 	  if (varvalue)
 	    {
 	      /* recursively evaluate */
-	      ts = clib_macro_eval (mm, varvalue, complain);
+	      ts = clib_macro_eval (mm, varvalue, complain, level + 1,
+				    max_level);
 	      vec_free (varvalue);
 	      /* add results to answer */
 	      vec_append (rv, ts);
@@ -195,7 +204,7 @@
   i8 *rv;
 
   s2 = (i8 *) format (0, "$(%s)%c", s, 0);
-  rv = clib_macro_eval (mm, s2, complain);
+  rv = clib_macro_eval (mm, s2, complain, 0 /* level */ , 8 /* max_level */ );
   vec_free (s2);
   return (rv);
 }