- Added coloring option to graph saving.
[oweals/gnunet.git] / src / regex / regex.c
index a17d5a57f68f54aca1d65b7a217f7bfc6b298344..c8b8ad3faedb680268ff3256c91ea8b868a9a449 100644 (file)
@@ -766,7 +766,8 @@ number_states (void *cls, unsigned int count, struct GNUNET_REGEX_State *s)
   struct GNUNET_REGEX_State **states = cls;
 
   s->proof_id = count;
-  states[count] = s;
+  if (NULL != states)
+    states[count] = s;
 }
 
 
@@ -1600,7 +1601,7 @@ nfa_fragment_create (struct GNUNET_REGEX_State *start,
   n->start = NULL;
   n->end = NULL;
 
-  if (NULL == start && NULL == end)
+  if (NULL == start || NULL == end)
     return n;
 
   automaton_add_state (n, end);
@@ -1813,7 +1814,7 @@ nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
   struct GNUNET_REGEX_Automaton *b;
-  struct GNUNET_REGEX_Automaton *new;
+  struct GNUNET_REGEX_Automaton *new_nfa;
 
   b = ctx->stack_tail;
   GNUNET_assert (NULL != b);
@@ -1826,15 +1827,15 @@ nfa_add_concatenation (struct GNUNET_REGEX_Context *ctx)
   a->end->accepting = 0;
   b->end->accepting = 1;
 
-  new = nfa_fragment_create (NULL, NULL);
-  nfa_add_states (new, a->states_head, a->states_tail);
-  nfa_add_states (new, b->states_head, b->states_tail);
-  new->start = a->start;
-  new->end = b->end;
+  new_nfa = nfa_fragment_create (NULL, NULL);
+  nfa_add_states (new_nfa, a->states_head, a->states_tail);
+  nfa_add_states (new_nfa, b->states_head, b->states_tail);
+  new_nfa->start = a->start;
+  new_nfa->end = b->end;
   automaton_fragment_clear (a);
   automaton_fragment_clear (b);
 
-  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
 }
 
 
@@ -1847,12 +1848,11 @@ static void
 nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
-  struct GNUNET_REGEX_Automaton *new;
+  struct GNUNET_REGEX_Automaton *new_nfa;
   struct GNUNET_REGEX_State *start;
   struct GNUNET_REGEX_State *end;
 
   a = ctx->stack_tail;
-  GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
 
   if (NULL == a)
   {
@@ -1861,6 +1861,8 @@ nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
     return;
   }
 
+  GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
+
   start = nfa_state_create (ctx, 0);
   end = nfa_state_create (ctx, 1);
 
@@ -1872,11 +1874,11 @@ nfa_add_star_op (struct GNUNET_REGEX_Context *ctx)
   a->end->accepting = 0;
   end->accepting = 1;
 
-  new = nfa_fragment_create (start, end);
-  nfa_add_states (new, a->states_head, a->states_tail);
+  new_nfa = nfa_fragment_create (start, end);
+  nfa_add_states (new_nfa, a->states_head, a->states_tail);
   automaton_fragment_clear (a);
 
-  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
 }
 
 
@@ -1908,12 +1910,11 @@ static void
 nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
-  struct GNUNET_REGEX_Automaton *new;
+  struct GNUNET_REGEX_Automaton *new_nfa;
   struct GNUNET_REGEX_State *start;
   struct GNUNET_REGEX_State *end;
 
   a = ctx->stack_tail;
-  GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
 
   if (NULL == a)
   {
@@ -1922,6 +1923,8 @@ nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
     return;
   }
 
+  GNUNET_CONTAINER_DLL_remove (ctx->stack_head, ctx->stack_tail, a);
+
   start = nfa_state_create (ctx, 0);
   end = nfa_state_create (ctx, 1);
 
@@ -1931,11 +1934,11 @@ nfa_add_question_op (struct GNUNET_REGEX_Context *ctx)
 
   a->end->accepting = 0;
 
-  new = nfa_fragment_create (start, end);
-  nfa_add_states (new, a->states_head, a->states_tail);
+  new_nfa = nfa_fragment_create (start, end);
+  nfa_add_states (new_nfa, a->states_head, a->states_tail);
   automaton_fragment_clear (a);
 
-  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
 }
 
 
@@ -1950,7 +1953,7 @@ nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
 {
   struct GNUNET_REGEX_Automaton *a;
   struct GNUNET_REGEX_Automaton *b;
-  struct GNUNET_REGEX_Automaton *new;
+  struct GNUNET_REGEX_Automaton *new_nfa;
   struct GNUNET_REGEX_State *start;
   struct GNUNET_REGEX_State *end;
 
@@ -1973,13 +1976,13 @@ nfa_add_alternation (struct GNUNET_REGEX_Context *ctx)
   b->end->accepting = 0;
   end->accepting = 1;
 
-  new = nfa_fragment_create (start, end);
-  nfa_add_states (new, a->states_head, a->states_tail);
-  nfa_add_states (new, b->states_head, b->states_tail);
+  new_nfa = nfa_fragment_create (start, end);
+  nfa_add_states (new_nfa, a->states_head, a->states_tail);
+  nfa_add_states (new_nfa, b->states_head, b->states_tail);
   automaton_fragment_clear (a);
   automaton_fragment_clear (b);
 
-  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new);
+  GNUNET_CONTAINER_DLL_insert_tail (ctx->stack_head, ctx->stack_tail, new_nfa);
 }
 
 
@@ -2137,6 +2140,7 @@ GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
     case 92:                   /* escape: \ */
       regexp++;
       count++;
+      /* fall through! */
     default:
       if (atomcount > 1)
       {
@@ -2171,6 +2175,9 @@ GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
 
   nfa->regex = GNUNET_strdup (regex);
 
+  /* create depth-first numbering of the states for pretty printing */
+  GNUNET_REGEX_automaton_traverse (nfa, &number_states, NULL);
+
   return nfa;
 
 error: