[PATCH 1/3] score: Add _IO_Printf() and _IO_Vprintf()

2017-11-03 Thread Sebastian Huber
The previous vprintk() implementation had a questionable licence header,
lacks support for the 'z' and 'j' format specifiers, is not robust
against invalid format specifiers, uses a global variable for output.
Replace it with a stripped down version of the FreeBSD kernel kvprintf()
function.

The new implementation allows a low overhead rtems_snprintf() if
necessary.

Update #3199.
Close #3216.
---
 cpukit/libcsupport/src/vprintk.c | 220 ++-
 cpukit/score/Makefile.am |   3 +
 cpukit/score/include/rtems/score/io.h|  46 
 cpukit/score/preinstall.am   |   4 +
 cpukit/score/src/ioprintf.c  |  31 +++
 cpukit/score/src/iovprintf.c | 365 +++
 testsuites/sptests/spprintk/init.c   | 161 +-
 testsuites/sptests/spprintk/spprintk.scn |  81 ---
 8 files changed, 613 insertions(+), 298 deletions(-)
 create mode 100644 cpukit/score/include/rtems/score/io.h
 create mode 100644 cpukit/score/src/ioprintf.c
 create mode 100644 cpukit/score/src/iovprintf.c

diff --git a/cpukit/libcsupport/src/vprintk.c b/cpukit/libcsupport/src/vprintk.c
index a254934a0d..09b6f0f006 100644
--- a/cpukit/libcsupport/src/vprintk.c
+++ b/cpukit/libcsupport/src/vprintk.c
@@ -6,224 +6,32 @@
  */
 
 /*
- * (C) Copyright 1997 -
- * - NavIST Group - Real-Time Distributed Systems and Industrial Automation
+ * Copyright (c) 2017 embedded brains GmbH.  All rights reserved.
  *
- * http://pandora.ist.utl.pt
+ *  embedded brains GmbH
+ *  Dornierstr. 4
+ *  82178 Puchheim
+ *  Germany
+ *  
  *
- * Instituto Superior Tecnico * Lisboa * PORTUGAL
- *
- * Disclaimer:
- *
- * This file is provided "AS IS" without warranty of any kind, either
- * expressed or implied.
- *
- * This code is based on code by: Jose Rufino - IST
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.org/license/LICENSE.
  */
 
 #if HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#include 
-#include 
-#include 
 #include 
+#include 
 
-static int printNum(
-  long long num,
-  unsigned base,
-  bool sign,
-  unsigned maxwidth,
-  char lead
-);
-
-/**
- *  A simplified version of printf intended for use when the
- *  console is not yet initialized or in ISR's.
- *
- * Arguments:
- *as in printf: fmt - format string, ... - unnamed arguments.
- */
-int vprintk(
-  const char *fmt,
-  va_list ap
-)
+static void vprintk_putchar( int c, void *arg )
 {
-  int len_out = 0;
-  for (; *fmt != '\0'; fmt++) {
-unsigned base = 0;
-unsigned width = 0;
-enum {
-  LFLAG_INT,
-  LFLAG_LONG,
-  LFLAG_LONG_LONG
-} lflag = LFLAG_INT;
-bool minus = false;
-bool sign = false;
-char lead = ' ';
-char c = *fmt;
-long long num;
-
-if (c != '%') {
-  rtems_putc(c);
-  ++len_out;
-  continue;
-}
-
-++fmt; c = *fmt;
-
-if (c == '0') {
-  lead = '0';
-  ++fmt; c = *fmt;
-}
-
-if (c == '-') {
-  minus = true;
-  ++fmt; c = *fmt;
-}
-
-while (c >= '0' && c <= '9' ) {
-  width *= 10;
-  width += ((unsigned) c - '0');
-  ++fmt; c = *fmt;
-}
-
-if (c == 'l') {
-  lflag = LFLAG_LONG;
-  ++fmt; c = *fmt;
-
-  if (c == 'l') {
-lflag = LFLAG_LONG_LONG;
-++fmt; c = *fmt;
-  }
-}
-
-if ( c == 'c' ) {
-  /* need a cast here since va_arg() only takes fully promoted types */
-  char chr = (char) va_arg(ap, int);
-  rtems_putc(chr);
-  ++len_out;
-  continue;
-}
-
-if ( c == 's' ) {
-  unsigned i, len;
-  char *s, *str;
-
-  str = va_arg(ap, char *);
-
-  if ( str == NULL ) {
-str = "";
-  }
-
-  /* calculate length of string */
-  for ( len=0, s=str ; *s ; len++, s++ )
-;
-
-  /* leading spaces */
-  if ( !minus )
-for ( i=len ; i 0) {
-toPrint[count++] = (char) (unsigned_num - (n * base));
-unsigned_num = n;
-  }
-  toPrint[count++] = (char) unsigned_num;
-
-  for (n=maxwidth ; n > count; n--, len_out++ )
-rtems_putc(lead);
-
-  for (n = 0; n < count; n++, len_out++) {
-rtems_putc("0123456789ABCDEF"[(int)(toPrint[count-(n+1)])]);
-  }
-
-  return len_out;
+  return _IO_Vprintf( vprintk_putchar, NULL, fmt, ap );
 }
diff --git a/cpukit/score/Makefile.am b/cpukit/score/Makefile.am
index 7ff1f41684..11bf59cca8 100644
--- a/cpukit/score/Makefile.am
+++ b/cpukit/score/Makefile.am
@@ -46,6 +46,7 @@ include_rtems_score_HEADERS += include/rtems/score/heap.h
 include_rtems_score_HEADERS += include/rtems/score/heapimpl.h
 include_rtems_score_HEADERS += include/rtems/score/protectedheap.h
 include_rtems_score_HEADERS += include/rtems/score/interr.h
+include_rtems_score_HEADERS += include/rtems/score/io.h
 include_rtems_score_HEADERS += include/rtems/score/isr.h
 include_rtems_score_HEADERS += include/rtems/score/isrlevel.h
 include_rtems_score_HEADERS +=

[PATCH 2/3] Add simple console driver

2017-11-03 Thread Sebastian Huber
Update #3170.
Update #3199.
---
 cpukit/include/rtems/console.h |  9 
 cpukit/libcsupport/Makefile.am |  1 +
 cpukit/libcsupport/src/consolesimple.c | 97 ++
 cpukit/sapi/include/confdefs.h | 17 ++
 4 files changed, 124 insertions(+)
 create mode 100644 cpukit/libcsupport/src/consolesimple.c

diff --git a/cpukit/include/rtems/console.h b/cpukit/include/rtems/console.h
index dbd749c60a..f9a7de186c 100644
--- a/cpukit/include/rtems/console.h
+++ b/cpukit/include/rtems/console.h
@@ -147,6 +147,15 @@ rtems_device_driver console_control(
   void  *arg
 );
 
+/**
+ * @brief Initializes a simple console device.
+ *
+ * This device writes via rtems_putc() and reads via getchark().  The Termios
+ * framework is not used.  There is no support to change device settings, e.g.
+ * baud, stop bits, parity, etc.
+ */
+void _Console_simple_Initialize( void );
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/cpukit/libcsupport/Makefile.am b/cpukit/libcsupport/Makefile.am
index 41985393af..756526ae8e 100644
--- a/cpukit/libcsupport/Makefile.am
+++ b/cpukit/libcsupport/Makefile.am
@@ -139,6 +139,7 @@ libcsupport_a_SOURCES = src/gxx_wrappers.c src/getchark.c 
src/printk.c \
 src/resource_snapshot.c \
 $(BSD_LIBC_C_FILES) $(BASE_FS_C_FILES) $(MALLOC_C_FILES) \
 $(ERROR_C_FILES) $(ASSOCIATION_C_FILES)
+libcsupport_a_SOURCES += src/consolesimple.c
 libcsupport_a_SOURCES += src/printertask.c
 libcsupport_a_SOURCES += src/printerfprintfputc.c
 
diff --git a/cpukit/libcsupport/src/consolesimple.c 
b/cpukit/libcsupport/src/consolesimple.c
new file mode 100644
index 00..5aa0f98528
--- /dev/null
+++ b/cpukit/libcsupport/src/consolesimple.c
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2017 embedded brains GmbH.  All rights reserved.
+ *
+ *  embedded brains GmbH
+ *  Dornierstr. 4
+ *  82178 Puchheim
+ *  Germany
+ *  
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.org/license/LICENSE.
+ */
+
+#include 
+#include 
+#include 
+
+static ssize_t _Console_simple_Read(
+  rtems_libio_t *iop,
+  void  *buffer,
+  size_t count
+)
+{
+  char*buf;
+  ssize_t  i;
+  ssize_t  n;
+
+  buf = buffer;
+  n = (ssize_t) count;
+
+  for ( i = 0; i < n; ++i ) {
+int c;
+
+do {
+  c = getchark();
+} while (c == -1);
+
+buf[ i ] = (char) c;
+  }
+
+  return n;
+}
+
+static ssize_t _Console_simple_Write(
+  rtems_libio_t *iop,
+  const void*buffer,
+  size_t count
+)
+{
+  const char *buf;
+  ssize_t i;
+  ssize_t n;
+
+  buf = buffer;
+  n = (ssize_t) count;
+
+  for ( i = 0; i < n; ++i ) {
+rtems_putc( buf[ i ] );
+  }
+
+  return n;
+}
+
+static const rtems_filesystem_file_handlers_r _Console_simple_Handlers = {
+  .open_h = rtems_filesystem_default_open,
+  .close_h = rtems_filesystem_default_close,
+  .read_h = _Console_simple_Read,
+  .write_h = _Console_simple_Write,
+  .ioctl_h = rtems_filesystem_default_ioctl,
+  .lseek_h = rtems_filesystem_default_lseek,
+  .fstat_h = IMFS_stat,
+  .ftruncate_h = rtems_filesystem_default_ftruncate,
+  .fsync_h = rtems_filesystem_default_fsync_or_fdatasync,
+  .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync,
+  .fcntl_h = rtems_filesystem_default_fcntl,
+  .readv_h = rtems_filesystem_default_readv,
+  .writev_h = rtems_filesystem_default_writev,
+  .mmap_h = rtems_filesystem_default_mmap
+};
+
+static const IMFS_node_control
+_Console_simple_Node_control = IMFS_GENERIC_INITIALIZER(
+  &_Console_simple_Handlers,
+  IMFS_node_initialize_default,
+  IMFS_node_destroy_default
+);
+
+void _Console_simple_Initialize( void )
+{
+  IMFS_make_generic_node(
+CONSOLE_DEVICE_NAME,
+S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO,
+&_Console_simple_Node_control,
+NULL
+  );
+}
diff --git a/cpukit/sapi/include/confdefs.h b/cpukit/sapi/include/confdefs.h
index 47b7d9aacb..ca4ee47b2b 100755
--- a/cpukit/sapi/include/confdefs.h
+++ b/cpukit/sapi/include/confdefs.h
@@ -1610,10 +1610,27 @@ extern rtems_initialization_tasks_table 
Initialization_tasks[];
 #define NULL_DRIVER_TABLE_ENTRY \
  { NULL, NULL, NULL, NULL, NULL, NULL }
 
+#if defined(CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER) && \
+  defined(CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER)
+#error "CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER and 
CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER are mutually exclusive"
+#endif
+
 #ifdef CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
   #include 
 #endif
 
+#ifdef CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
+  #include 
+
+  #ifdef CONFIGURE_INIT
+RTEMS_SYSINIT_ITEM(
+  _Console_simple_Initialize,
+  RTEMS_SYSINIT_DEVICE_DRIVERS,
+  RTEMS_SYSINIT_ORDER_SECOND
+);
+  #endif
+#endif
+
 #ifdef CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
   #include 
 #endif
-- 
2.12.3

___
devel mailing list
devel@rt