sn-evolution-cut: Implement the "-n" and "-r" options.
[sort-networks.git] / src / sn-count-markov.c
index 698ca4d..db010f5 100644 (file)
@@ -58,12 +58,35 @@ static GTree *all_networks = NULL;
 static uint64_t cycles_num = 0;
 
 static _Bool do_loop = 1;
+static _Bool do_output_network = 0;
 static uint64_t max_iterations = 0;
 
-static void sigint_handler (int signal __attribute__((unused)))
+static void sigint_handler (__attribute__((unused)) int signal) /* {{{ */
 {
   do_loop = 0;
-} /* void sigint_handler */
+} /* }}} void sigint_handler */
+
+static void sighup_handler (__attribute__((unused)) int signal) /* {{{ */
+{
+  do_output_network = 1;
+} /* }}} void sighup_handler */
+
+static int output_network (sn_network_t *n) /* {{{ */
+{
+  FILE *fh;
+
+  if (n == NULL)
+    return (EINVAL);
+
+  fh = fopen ("/dev/tty", "w");
+  if (fh == NULL)
+    return (errno);
+
+  sn_network_show_fh (n, fh);
+
+  fclose (fh);
+  return (0);
+} /* }}} int output_network */
 
 static gint compare_hashval (gconstpointer p0, gconstpointer p1) /* {{{ */
 {
@@ -88,7 +111,10 @@ static int account_network (const sn_network_t *n, uint64_t iteration) /* {{{ */
     return (EINVAL);
 
   if (iteration == 0)
-    printf ("# iteration cyclelength\n");
+  {
+    printf ("# prev_iter this_iter cyclelength hashval\n");
+    fflush (stdout);
+  }
 
   key = sn_network_get_hashval (n);
   key_ptr = &key;
@@ -97,10 +123,15 @@ static int account_network (const sn_network_t *n, uint64_t iteration) /* {{{ */
   if (value_ptr != NULL)
   {
     uint64_t cycle_length = iteration - (*value_ptr);
-    printf ("%"PRIu64" %"PRIu64"\n", iteration, cycle_length);
+    printf ("%"PRIu64" %"PRIu64" %"PRIu64" 0x%016"PRIx64"\n",
+       (*value_ptr), iteration, cycle_length, key);
+    fflush (stdout);
 
     cycles_num++;
     *value_ptr = iteration;
+
+    sn_random_init ();
+
     return (0);
   }
 
@@ -233,6 +264,12 @@ static void random_walk (sn_network_t *n)
 
     account_network (next, iteration_counter);
 
+    if (do_output_network)
+    {
+      output_network (next);
+      do_output_network = 0;
+    }
+
     sn_network_destroy (n);
     n = next;
     iteration_counter++;
@@ -252,12 +289,13 @@ int main (int argc, char **argv)
 
   struct sigaction sigint_action;
   struct sigaction sigterm_action;
+  struct sigaction sighup_action;
 
   read_options (argc, argv);
   if (initial_input_file == NULL)
     exit_usage (argv[0], EXIT_FAILURE);
 
-  memset (&sigint_action, '\0', sizeof (sigint_action));
+  memset (&sigint_action, 0, sizeof (sigint_action));
   sigint_action.sa_handler = sigint_handler;
   sigaction (SIGINT, &sigint_action, NULL);
 
@@ -265,6 +303,10 @@ int main (int argc, char **argv)
   sigterm_action.sa_handler = sigint_handler;
   sigaction (SIGTERM, &sigterm_action, NULL);
 
+  memset (&sighup_action, 0, sizeof (sighup_action));
+  sighup_action.sa_handler = sighup_handler;
+  sigaction (SIGHUP, &sighup_action, NULL);
+
   all_networks = g_tree_new (compare_hashval);
   if (all_networks == NULL)
   {