Macros: convert to tree for speed of lookup
[exim.git] / src / src / macro_predef.c
index 47ba3fdd1c859cda43f2af1e8ab8d8688a5db3da..b594d5bfd23e4f4ce54ab5e9c337e7633b694b1c 100644 (file)
@@ -11,7 +11,8 @@ included in the main Exim build */
 #include "exim.h"
 #include "macro_predef.h"
 
-unsigned mp_index = 0;
+tree_node * tree_macros = NULL;
+unsigned m_number = 1;
 
 /* Global dummy variables */
 
@@ -23,16 +24,7 @@ uschar * syslog_facility_str;
 void
 builtin_macro_create_var(const uschar * name, const uschar * val)
 {
-printf ("static macro_item p%d = { ", mp_index);
-if (mp_index == 0)
-  printf(".next=NULL,");
-else
-  printf(".next=&p%d,", mp_index-1);
-
-printf(" .command_line=FALSE, .namelen=%d, .replen=%d,"
-       " .name=US\"%s\", .replacement=US\"%s\" };\n",
-       Ustrlen(name), Ustrlen(val), CS name, CS val);
-mp_index++;
+macro_create(name, val, FALSE);
 }
 
 
@@ -287,15 +279,59 @@ params_dkim();
 }
 
 
+static unsigned
+macro_dump(macro_item * m)
+{
+int left = 0, right = 0;
+tree_node * t;
+macro_item_64 * m64;
+
+/* fprintf(stderr, "%s %p\n", __FUNCTION__, m); */
+
+if (!m) return 0;
+/* fprintf(stderr, "%s '%s' l %p r %p\n", __FUNCTION__, m->tnode.name, m->tnode.left, m->tnode.left); */
+if ((t = m->tnode.left)) left = macro_dump(tnode_to_mitem(m->tnode.left));
+if ((t = m->tnode.right)) right = macro_dump(tnode_to_mitem(m->tnode.right));
+
+printf ("static macro_item_64 p%u = { ", m->m_number);
+printf(" .command_line=FALSE,"
+       " .namelen=%d,"
+       " .replen=%d,"
+       " .m_number=%u,"
+       " .tnode={",
+       Ustrlen(m->tnode.name), Ustrlen(m->tnode.data.ptr), m->m_number);
+printf(left  ? " .left=&p%d.tnode,"  : " .left=NULL,",  left);
+printf(right ? " .right=&p%d.tnode," : " .right=NULL,", right);
+printf(
+       " .data.ptr=\"%s\","
+       " .balance=%d,"
+       " .name=\"%s\"}};\n",
+       CS m->tnode.data.ptr,
+       m->tnode.balance,
+       CS m->tnode.name);
+
+if (Ustrlen(m->tnode.name) +1 > sizeof(m64->tnode.name))
+  {
+  printf("#error macro name too long for macro_item_64\n");
+  exit(1);
+  }
+return m->m_number;
+}
+
+
+
 int
 main(void)
 {
+unsigned idx;
+
 printf("#include \"exim.h\"\n");
 features();
 options();
 params();
 
-printf("macro_item * macros = &p%d;\n", mp_index-1);
-printf("macro_item * mlast = &p0;\n");
+idx = macro_dump(tnode_to_mitem(tree_macros));
+printf("tree_node * tree_macros = (tree_node *) &p%u.tnode;\n", idx);
+printf("unsigned m_number = %u;\n", m_number);
 exit(0);
 }