error-decode: Operate as a pipe and accept input from stdin

Useful for feeding in compressed files from bugzilla:

$ bzcat /tmp/i915_error_state.bz | intel_error_decode | less

Next step would be to use gzfopen or bzfopen to automagically handle
compressed files...

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
diff --git a/tools/intel_error_decode.c b/tools/intel_error_decode.c
index e6c3a76..e21b839 100644
--- a/tools/intel_error_decode.c
+++ b/tools/intel_error_decode.c
@@ -44,6 +44,7 @@
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
+#include <unistd.h>
 #include <inttypes.h>
 #include <errno.h>
 #include <sys/stat.h>
@@ -209,9 +210,8 @@
 }
 
 static void
-read_data_file (const char * filename)
+read_data_file (FILE *file)
 {
-    FILE *file;
     int devid = PCI_CHIP_I855_GM;
     uint32_t *data = NULL;
     int data_size = 0, count = 0, line_number = 0, matched;
@@ -222,13 +222,6 @@
     char *buffer_type[2] = {  "ringbuffer", "batchbuffer" };
     int is_batch = 1;
 
-    file = fopen (filename, "r");
-    if (file == NULL) {
-	fprintf (stderr, "Failed to open %s: %s\n",
-		 filename, strerror (errno));
-	exit (1);
-    }
-
     while (getline (&line, &line_size, file) > 0) {
 	line_number++;
 
@@ -306,14 +299,14 @@
 
     free (data);
     free (line);
-
-    fclose (file);
 }
 
 int
 main (int argc, char *argv[])
 {
+    FILE *file;
     const char *path;
+    char *filename;
     struct stat st;
     int err;
 
@@ -334,17 +327,22 @@
     }
 
     if (argc == 1) {
-	path = "/debug/dri/0";
-	err = stat (path, &st);
-	if (err != 0) {
-	    path = "/sys/kernel/debug/dri/0";
+	if (isatty(1)) {
+	    path = "/debug/dri/0";
 	    err = stat (path, &st);
 	    if (err != 0) {
-		errx(1,
-		     "Couldn't find i915 debugfs directory.\n\n"
-		     "Is debugfs mounted? You might try mounting it with a command such as:\n\n"
-		     "\tsudo mount -t debugfs debugfs /sys/kernel/debug\n");
+		path = "/sys/kernel/debug/dri/0";
+		err = stat (path, &st);
+		if (err != 0) {
+		    errx(1,
+			 "Couldn't find i915 debugfs directory.\n\n"
+			 "Is debugfs mounted? You might try mounting it with a command such as:\n\n"
+			 "\tsudo mount -t debugfs debugfs /sys/kernel/debug\n");
+		}
 	    }
+	} else {
+	    read_data_file(stdin);
+	    exit(0);
 	}
     } else {
 	path = argv[1];
@@ -356,15 +354,23 @@
 	}
     }
 
-    if (S_ISDIR (st.st_mode)) {
-	char *filename;
-
+    if (S_ISDIR (st.st_mode))
 	asprintf (&filename, "%s/i915_error_state", path);
-	read_data_file (filename);
-	free (filename);
+    else
+	filename = (char *) path;
+
+    file = fopen(filename, "r");
+    if (file) {
+	read_data_file (file);
+	fclose (file);
     } else {
-	read_data_file (path);
+	fprintf (stderr, "Failed to open %s: %s\n",
+		 filename, strerror (errno));
+	exit (1);
     }
 
+    if (filename != path)
+	free (filename);
+
     return 0;
 }