1 //####ECOSGPLCOPYRIGHTBEGIN####
2 // -------------------------------------------
3 // This file is part of eCos, the Embedded Configurable Operating System.
4 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
5 // Copyright (C) 2002 Gary Thomas
7 // eCos is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 2 or (at your option) any later version.
11 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 // You should have received a copy of the GNU General Public License along
17 // with eCos; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 // As a special exception, if other files instantiate templates or use macros
21 // or inline functions from this file, or you compile this file and link it
22 // with other works to produce a work based on this file, this file does not
23 // by itself cause the resulting work to be covered by the GNU General Public
24 // License. However the source code for this file must still be made available
25 // in accordance with section (3) of the GNU General Public License.
27 // This exception does not invalidate any other reasons why a work based on
28 // this file might be covered by the GNU General Public License.
30 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
31 // at http://sources.redhat.com/ecos/ecos-license/
32 // -------------------------------------------
33 //####ECOSGPLCOPYRIGHTEND####
38 #if SYSTEM_MODULE_PRINT
40 #include "athos_api.h"
42 #define is_digit(c) ((c >= '0') && (c <= '9'))
44 #if defined(__GNUC__) && defined(__mips__)
45 #define va_list __builtin_va_list
46 #define va_arg __builtin_va_arg
47 #define va_start __builtin_va_start
48 #define va_end __builtin_va_end
49 #define va_copy __builtin_va_copy
54 #define va_list __gnuc_va_list
58 cmnos_write_char(char c)
63 } else if (c == '\r') {
69 void (*_putc)(char c) = cmnos_write_char;
71 static int _cvt(unsigned long val, char *buf, long radix, char *digits)
82 *cp++ = digits[val % radix];
95 static int cmnos_vprintf(void (*putc)(char c), const char *fmt, va_list ap)
97 char buf[sizeof(long)*8];
98 char c, sign, *cp=buf;
99 int left_prec, right_prec, zero_fill, pad, pad_on_right,
100 i, islong, islonglong;
102 int res = 0, length = 0;
104 while ((c = *fmt++) != '\0') {
107 left_prec = right_prec = pad_on_right = islong = islonglong = 0;
118 while (is_digit(c)) {
119 left_prec = (left_prec * 10) + (c - '0');
125 while (is_digit(c)) {
126 right_prec = (right_prec * 10) + (c - '0');
130 right_prec = left_prec;
138 // long long qualifier
143 // Fetch value [numeric descriptors only]
156 val = va_arg(ap, long);
158 val = (long)va_arg(ap, long);
160 val = (long)va_arg(ap, int);
162 if ((c == 'd') || (c == 'D')) {
168 // Mask to unsigned, sized quantity
170 val &= (1ULL << (sizeof(long) * 8)) - 1;
172 val &= (1ULL << (sizeof(int) * 8)) - 1;
185 left_prec = sizeof(unsigned long)*2;
197 length = _cvt(val, buf, 10, "0123456789");
201 length = _cvt(val, buf, 16, "0123456789abcdef");
204 length = _cvt(val, buf, 16, "0123456789ABCDEF");
211 cp = va_arg(ap, char *);
216 while (cp[length] != '\0') length++;
220 c = va_arg(ap, int /*char*/);
227 if (left_prec == 0) {
229 length = sizeof(long)*8;
231 length = sizeof(long)*8;
233 length = sizeof(int)*8;
235 for (i = 0; i < length-1; i++) {
236 buf[i] = ((val & ((long)1<<i)) ? '1' : '.');
248 pad = left_prec - length;
272 while (length-- > 0) {
291 int cmnos_printf(const char *fmt, ...)
298 //if (A_SERIAL_ENABLED()) {
300 ret = cmnos_vprintf(_putc, fmt, ap);
311 cmnos_printf_init(void)
315 void cmnos_printf_module_install(struct printf_api *tbl)
317 tbl->_printf_init = cmnos_printf_init;
318 tbl->_printf = cmnos_printf;
321 #endif /* SYSTEM_MODULE_PRINT */