Index: c/src/tests/support/ChangeLog
===================================================================
--- c/src/tests/support/ChangeLog	(revision d9ab6d3b31b9735b5143e6221b4b6d81d7ae77eb)
+++ c/src/tests/support/ChangeLog	(revision cd9396e7d084473bee312f6e1cda13174247e0cc)
@@ -1,2 +1,12 @@
+2002-08-01	Joel Sherrill <joel@OARcorp.com>
+
+	* Per PR47 add support for buffered test output.  This involved
+	adding defines to redirect output to a buffer and dump it when
+	full, at "test pause", and at exit.  To avoid problems when redefining
+	exit(), all tests were modified to call rtems_test_exit().
+	Some tests, notable psxtests, had to be modified to include 
+	the standard test macro .h file (pmacros.h or tmacros.h) to
+	enable this support.
+	* include/buffer_test_io.h: New file. 
 2002-03-27	Ralf Corsepius <corsepiu@faw.uni-ulm.de>
 
Index: c/src/tests/support/include/Makefile.am
===================================================================
--- c/src/tests/support/include/Makefile.am	(revision d9ab6d3b31b9735b5143e6221b4b6d81d7ae77eb)
+++ c/src/tests/support/include/Makefile.am	(revision cd9396e7d084473bee312f6e1cda13174247e0cc)
@@ -9,5 +9,6 @@
 
 TMPINSTALLFILES = $(project_bspdir)/lib/include \
-    $(project_bspdir)/lib/include/tmacros.h
+    $(project_bspdir)/lib/include/tmacros.h \
+    $(project_bspdir)/lib/include/buffer_test_io.h
 
 $(project_bspdir)/lib/include:
@@ -15,6 +16,9 @@
 $(project_bspdir)/lib/include/tmacros.h: tmacros.h
 	$(INSTALL_DATA) $< $@
+$(project_bspdir)/lib/include/buffer_test_io.h: buffer_test_io.h
+	$(INSTALL_DATA) $< $@
 
-CLEANFILES = $(project_bspdir)/lib/include/tmacros.h
+CLEANFILES = $(project_bspdir)/lib/include/tmacros.h \
+    $(project_bspdir)/lib/include/buffer_test_io.h
 
 all-local: $(TMPINSTALLFILES)
Index: c/src/tests/support/include/buffer_test_io.h
===================================================================
--- c/src/tests/support/include/buffer_test_io.h	(revision cd9396e7d084473bee312f6e1cda13174247e0cc)
+++ c/src/tests/support/include/buffer_test_io.h	(revision cd9396e7d084473bee312f6e1cda13174247e0cc)
@@ -0,0 +1,117 @@
+/*
+ *  Support for running the test output through a buffer
+ * 
+ *  $Id$
+ */
+
+#ifndef __BUFFER_TEST_IO_h
+#define __BUFFER_TEST_IO_h
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdlib.h>
+
+/*
+ *  Uncomment this to get buffered test output.  When commented out,
+ *  test output behaves as it always has and is printed ASAP.
+ */
+
+/* #define TESTS_BUFFER_OUTPUT */
+
+#if !defined(TESTS_BUFFER_OUTPUT)
+
+#define rtems_test_exit(_s) \
+  do { \
+    exit(_s); \
+  } while (0)
+
+#define FLUSH_OUTPUT() \
+  do { \
+    fflush(stdout); \
+  } while (0)
+
+#else  /* buffer test output */
+
+#define _TEST_OUTPUT_BUFFER_SIZE 2048
+extern char _test_output_buffer[_TEST_OUTPUT_BUFFER_SIZE];
+void _test_output_append(char *);
+void _test_output_flush(void);
+
+#define rtems_test_exit(_s) \
+  do { \
+    _test_output_flush(); \
+    exit(_s); \
+  } while (0)
+
+#undef printf
+#define printf(...) \
+  do { \
+     char _buffer[128]; \
+     sprintf( _buffer, __VA_ARGS__); \ 
+     _test_output_append( _buffer ); \
+  } while (0)
+
+#undef puts
+#define puts(_string) \
+  do { \
+     char _buffer[128]; \
+     sprintf( _buffer, "%s\n", _string ); \ 
+     _test_output_append( _buffer ); \
+  } while (0)
+
+#undef putchar
+#define putchar(_c) \
+  do { \
+     char _buffer[2]; \
+     _buffer[0] = _c; \
+     _buffer[1] = '\0'; \
+     _test_output_append( _buffer ); \
+  } while (0)
+
+/* we write to stderr when there is a pause() */
+#define FLUSH_OUTPUT() _test_output_flush()
+
+#if defined(TEST_INIT) || defined(CONFIGURE_INIT)
+
+char _test_output_buffer[_TEST_OUTPUT_BUFFER_SIZE];
+int _test_output_buffer_index = 0;
+
+void _test_output_append(char *_buffer)
+{
+  char *p;
+  
+  for ( p=_buffer ; *p ; p++ ) {
+    _test_output_buffer[_test_output_buffer_index++] = *p;
+    _test_output_buffer[_test_output_buffer_index]   = '\0';
+#if 0
+    if ( *p == '\n' ) {
+      fprintf( stderr, "BUFFER -- %s", _test_output_buffer );
+      _test_output_buffer_index = 0;
+     _test_output_buffer[0]   = '\0';
+    }
+#endif
+    if ( _test_output_buffer_index >= (_TEST_OUTPUT_BUFFER_SIZE - 80) ) 
+      _test_output_flush();
+  }
+}
+
+#include <termios.h>
+#include <unistd.h>
+
+void _test_output_flush(void)
+{
+  fprintf( stderr, "%s", _test_output_buffer );
+  _test_output_buffer_index = 0;
+  tcdrain( 2 );
+}
+
+#endif  /* TEST_INIT */
+#endif /* TESTS_BUFFER_OUTPUT */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
Index: c/src/tests/support/include/tmacros.h
===================================================================
--- c/src/tests/support/include/tmacros.h	(revision d9ab6d3b31b9735b5143e6221b4b6d81d7ae77eb)
+++ c/src/tests/support/include/tmacros.h	(revision cd9396e7d084473bee312f6e1cda13174247e0cc)
@@ -38,4 +38,11 @@
 #endif
 
+#include <buffer_test_io.h>
+
+/*
+ *  Check that that the dispatch disable level is proper for the
+ *  mode/state of the test.  Normally it should be 0 when in task space.
+ */
+
 #define check_dispatch_disable_level( _expect ) \
   do { \
@@ -44,6 +51,6 @@
       printf( "\n_Thread_Dispatch_disable_level is (%d) not %d\n", \
               _Thread_Dispatch_disable_level, (_expect) ); \
-      fflush(stdout); \
-      exit( 1 ); \
+      FLUSH_OUTPUT(); \
+      rtems_test_exit( 1 ); \
     } \
   } while ( 0 ) 
@@ -68,6 +75,6 @@
       printf( "\n%s FAILED -- expected (%s) got (%s)\n", \
               (_msg), rtems_status_text(_desired), rtems_status_text(_stat) ); \
-      fflush(stdout); \
-      exit( _stat ); \
+      FLUSH_OUTPUT(); \
+      rtems_test_exit( _stat ); \
     } \
   } while ( 0 )
@@ -101,6 +108,6 @@
       printf( "\n FAILED -- errno (%d - %s)\n", \
               errno, strerror(errno) ); \
-      fflush(stdout); \
-      exit( _stat ); \
+      FLUSH_OUTPUT(); \
+      rtems_test_exit( _stat ); \
     } \
   } while ( 0 )
@@ -126,6 +133,6 @@
       printf( "\n%s FAILED -- expected (%d) got (%d)\n", \
               (_msg), (_desired), (_stat) ); \
-      fflush(stdout); \
-      exit( _stat ); \
+      FLUSH_OUTPUT(); \
+      rtems_test_exit( _stat ); \
     } \
   } while ( 0 )
@@ -148,5 +155,4 @@
        _s1, (_tb)->hour, (_tb)->minute, (_tb)->second, \
        (_tb)->month, (_tb)->day, (_tb)->year, _s2 ); \
-    fflush(stdout); \
   } while ( 0 )
 
@@ -154,5 +160,5 @@
   do { \
     putchar( _c ); \
-    fflush( stdout ); \
+    FLUSH_OUTPUT(); \
   } while ( 0 )
 
@@ -164,10 +170,12 @@
 #define rtems_test_pause() \
     do { \
-      printf( "<pause>\n" ); fflush( stdout ); \
+      printf( "<pause>\n" ); \
+      FLUSH_OUTPUT(); \
   } while ( 0 )
 
 #define rtems_test_pause_and_screen_number( _screen ) \
   do { \
-    printf( "<pause - screen %d>\n", (_screen) ); fflush( stdout ); \
+    printf( "<pause - screen %d>\n", (_screen) ); \
+    FLUSH_OUTPUT(); \
   } while ( 0 )
 #else
@@ -175,5 +183,6 @@
   do { \
     char buffer[ 80 ]; \
-    printf( "<pause>" ); fflush( stdout ); \
+    printf( "<pause>" ); \
+    FLUSH_OUTPUT(); \
     gets( buffer ); \
     puts( "" ); \
@@ -183,5 +192,6 @@
   do { \
     char buffer[ 80 ]; \
-    printf( "<pause - screen %d>", (_screen) ); fflush( stdout ); \
+    printf( "<pause - screen %d>", (_screen) ); \
+    FLUSH_OUTPUT(); \
     gets( buffer ); \
     puts( "" ); \
Index: testsuites/support/ChangeLog
===================================================================
--- testsuites/support/ChangeLog	(revision d9ab6d3b31b9735b5143e6221b4b6d81d7ae77eb)
+++ testsuites/support/ChangeLog	(revision cd9396e7d084473bee312f6e1cda13174247e0cc)
@@ -1,2 +1,12 @@
+2002-08-01	Joel Sherrill <joel@OARcorp.com>
+
+	* Per PR47 add support for buffered test output.  This involved
+	adding defines to redirect output to a buffer and dump it when
+	full, at "test pause", and at exit.  To avoid problems when redefining
+	exit(), all tests were modified to call rtems_test_exit().
+	Some tests, notable psxtests, had to be modified to include 
+	the standard test macro .h file (pmacros.h or tmacros.h) to
+	enable this support.
+	* include/buffer_test_io.h: New file. 
 2002-03-27	Ralf Corsepius <corsepiu@faw.uni-ulm.de>
 
Index: testsuites/support/include/buffer_test_io.h
===================================================================
--- testsuites/support/include/buffer_test_io.h	(revision cd9396e7d084473bee312f6e1cda13174247e0cc)
+++ testsuites/support/include/buffer_test_io.h	(revision cd9396e7d084473bee312f6e1cda13174247e0cc)
@@ -0,0 +1,117 @@
+/*
+ *  Support for running the test output through a buffer
+ * 
+ *  $Id$
+ */
+
+#ifndef __BUFFER_TEST_IO_h
+#define __BUFFER_TEST_IO_h
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdlib.h>
+
+/*
+ *  Uncomment this to get buffered test output.  When commented out,
+ *  test output behaves as it always has and is printed ASAP.
+ */
+
+/* #define TESTS_BUFFER_OUTPUT */
+
+#if !defined(TESTS_BUFFER_OUTPUT)
+
+#define rtems_test_exit(_s) \
+  do { \
+    exit(_s); \
+  } while (0)
+
+#define FLUSH_OUTPUT() \
+  do { \
+    fflush(stdout); \
+  } while (0)
+
+#else  /* buffer test output */
+
+#define _TEST_OUTPUT_BUFFER_SIZE 2048
+extern char _test_output_buffer[_TEST_OUTPUT_BUFFER_SIZE];
+void _test_output_append(char *);
+void _test_output_flush(void);
+
+#define rtems_test_exit(_s) \
+  do { \
+    _test_output_flush(); \
+    exit(_s); \
+  } while (0)
+
+#undef printf
+#define printf(...) \
+  do { \
+     char _buffer[128]; \
+     sprintf( _buffer, __VA_ARGS__); \ 
+     _test_output_append( _buffer ); \
+  } while (0)
+
+#undef puts
+#define puts(_string) \
+  do { \
+     char _buffer[128]; \
+     sprintf( _buffer, "%s\n", _string ); \ 
+     _test_output_append( _buffer ); \
+  } while (0)
+
+#undef putchar
+#define putchar(_c) \
+  do { \
+     char _buffer[2]; \
+     _buffer[0] = _c; \
+     _buffer[1] = '\0'; \
+     _test_output_append( _buffer ); \
+  } while (0)
+
+/* we write to stderr when there is a pause() */
+#define FLUSH_OUTPUT() _test_output_flush()
+
+#if defined(TEST_INIT) || defined(CONFIGURE_INIT)
+
+char _test_output_buffer[_TEST_OUTPUT_BUFFER_SIZE];
+int _test_output_buffer_index = 0;
+
+void _test_output_append(char *_buffer)
+{
+  char *p;
+  
+  for ( p=_buffer ; *p ; p++ ) {
+    _test_output_buffer[_test_output_buffer_index++] = *p;
+    _test_output_buffer[_test_output_buffer_index]   = '\0';
+#if 0
+    if ( *p == '\n' ) {
+      fprintf( stderr, "BUFFER -- %s", _test_output_buffer );
+      _test_output_buffer_index = 0;
+     _test_output_buffer[0]   = '\0';
+    }
+#endif
+    if ( _test_output_buffer_index >= (_TEST_OUTPUT_BUFFER_SIZE - 80) ) 
+      _test_output_flush();
+  }
+}
+
+#include <termios.h>
+#include <unistd.h>
+
+void _test_output_flush(void)
+{
+  fprintf( stderr, "%s", _test_output_buffer );
+  _test_output_buffer_index = 0;
+  tcdrain( 2 );
+}
+
+#endif  /* TEST_INIT */
+#endif /* TESTS_BUFFER_OUTPUT */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
Index: testsuites/support/include/tmacros.h
===================================================================
--- testsuites/support/include/tmacros.h	(revision d9ab6d3b31b9735b5143e6221b4b6d81d7ae77eb)
+++ testsuites/support/include/tmacros.h	(revision cd9396e7d084473bee312f6e1cda13174247e0cc)
@@ -38,4 +38,11 @@
 #endif
 
+#include <buffer_test_io.h>
+
+/*
+ *  Check that that the dispatch disable level is proper for the
+ *  mode/state of the test.  Normally it should be 0 when in task space.
+ */
+
 #define check_dispatch_disable_level( _expect ) \
   do { \
@@ -44,6 +51,6 @@
       printf( "\n_Thread_Dispatch_disable_level is (%d) not %d\n", \
               _Thread_Dispatch_disable_level, (_expect) ); \
-      fflush(stdout); \
-      exit( 1 ); \
+      FLUSH_OUTPUT(); \
+      rtems_test_exit( 1 ); \
     } \
   } while ( 0 ) 
@@ -68,6 +75,6 @@
       printf( "\n%s FAILED -- expected (%s) got (%s)\n", \
               (_msg), rtems_status_text(_desired), rtems_status_text(_stat) ); \
-      fflush(stdout); \
-      exit( _stat ); \
+      FLUSH_OUTPUT(); \
+      rtems_test_exit( _stat ); \
     } \
   } while ( 0 )
@@ -101,6 +108,6 @@
       printf( "\n FAILED -- errno (%d - %s)\n", \
               errno, strerror(errno) ); \
-      fflush(stdout); \
-      exit( _stat ); \
+      FLUSH_OUTPUT(); \
+      rtems_test_exit( _stat ); \
     } \
   } while ( 0 )
@@ -126,6 +133,6 @@
       printf( "\n%s FAILED -- expected (%d) got (%d)\n", \
               (_msg), (_desired), (_stat) ); \
-      fflush(stdout); \
-      exit( _stat ); \
+      FLUSH_OUTPUT(); \
+      rtems_test_exit( _stat ); \
     } \
   } while ( 0 )
@@ -148,5 +155,4 @@
        _s1, (_tb)->hour, (_tb)->minute, (_tb)->second, \
        (_tb)->month, (_tb)->day, (_tb)->year, _s2 ); \
-    fflush(stdout); \
   } while ( 0 )
 
@@ -154,5 +160,5 @@
   do { \
     putchar( _c ); \
-    fflush( stdout ); \
+    FLUSH_OUTPUT(); \
   } while ( 0 )
 
@@ -164,10 +170,12 @@
 #define rtems_test_pause() \
     do { \
-      printf( "<pause>\n" ); fflush( stdout ); \
+      printf( "<pause>\n" ); \
+      FLUSH_OUTPUT(); \
   } while ( 0 )
 
 #define rtems_test_pause_and_screen_number( _screen ) \
   do { \
-    printf( "<pause - screen %d>\n", (_screen) ); fflush( stdout ); \
+    printf( "<pause - screen %d>\n", (_screen) ); \
+    FLUSH_OUTPUT(); \
   } while ( 0 )
 #else
@@ -175,5 +183,6 @@
   do { \
     char buffer[ 80 ]; \
-    printf( "<pause>" ); fflush( stdout ); \
+    printf( "<pause>" ); \
+    FLUSH_OUTPUT(); \
     gets( buffer ); \
     puts( "" ); \
@@ -183,5 +192,6 @@
   do { \
     char buffer[ 80 ]; \
-    printf( "<pause - screen %d>", (_screen) ); fflush( stdout ); \
+    printf( "<pause - screen %d>", (_screen) ); \
+    FLUSH_OUTPUT(); \
     gets( buffer ); \
     puts( "" ); \
