sn-show: Make it possible to display more than one network at once.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Mon, 10 May 2010 09:29:11 +0000 (11:29 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Mon, 10 May 2010 09:29:11 +0000 (11:29 +0200)
src/sn-show.c

index b96174b..21d1dee 100644 (file)
  *   Florian octo Forster <ff at octo.it>
  **/
 
  *   Florian octo Forster <ff at octo.it>
  **/
 
-#ifndef _ISOC99_SOURCE
-# define _ISOC99_SOURCE
-#endif
-#ifndef _POSIX_C_SOURCE
-# define _POSIX_C_SOURCE 200112L
-#endif
+#include "config.h"
 
 #include <stdlib.h>
 #include <stdio.h>
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
+#include <errno.h>
 
 #include "sn_network.h"
 
 
 #include "sn_network.h"
 
-int main (int argc, char **argv)
+static int show_fh (FILE *fh) /* {{{ */
 {
   sn_network_t *n;
 {
   sn_network_t *n;
-  FILE *fh = NULL;
-
-  if (argc == 1)
-    fh = stdin;
-  else if (argc == 2)
-    fh = fopen (argv[1], "r");
 
   if (fh == NULL)
 
   if (fh == NULL)
-  {
-    printf ("fh == NULL!\n");
-    return (1);
-  }
+    return (EINVAL);
 
   n = sn_network_read (fh);
 
   n = sn_network_read (fh);
-
   if (n == NULL)
   {
   if (n == NULL)
   {
-    printf ("n == NULL!\n");
-    return (1);
+    fprintf (stderr, "Parsing comparator network failed.\n");
+    return (EINVAL);
   }
 
   sn_network_show (n);
 
   }
 
   sn_network_show (n);
 
+  sn_network_destroy (n);
+
   return (0);
   return (0);
+} /* }}} int show_fh */
+
+static int show_file (const char *file) /* {{{ */
+{
+  FILE *fh;
+  int status;
+
+  if (file == NULL)
+    return (EINVAL);
+
+  fh = fopen (file, "r");
+  if (fh == NULL)
+  {
+    fprintf (stderr, "Opening file \"%s\" failed: %s\n",
+        file, strerror (errno));
+    return (errno);
+  }
+
+  status = show_fh (fh);
+
+  fclose (fh);
+  return (status);
+} /* }}} int show_file */
+
+int main (int argc, char **argv)
+{
+  if (argc == 1)
+  {
+    show_fh (stdin);
+  }
+  else
+  {
+    int i;
+    for (i = 1; i < argc; i++)
+    {
+      if (i > 1)
+        puts ("\n");
+
+      if (argc > 2)
+        printf ("=== %s ===\n\n", argv[i]);
+
+      show_file (argv[i]);
+    }
+  }
+
+  exit (EXIT_SUCCESS);
 } /* int main */
 
 /* vim: set shiftwidth=2 softtabstop=2 : */
 } /* int main */
 
 /* vim: set shiftwidth=2 softtabstop=2 : */