2011-06-27 15:00:05 +00:00
|
|
|
/*
|
2011-06-27 19:57:11 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
|
|
|
2011 Giovanni Di Sirio.
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2011-06-27 19:57:11 +00:00
|
|
|
This file is part of ChibiOS/RT.
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2011-06-27 19:57:11 +00:00
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2011-06-27 19:57:11 +00:00
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2011-06-27 15:00:05 +00:00
|
|
|
|
2011-06-27 19:57:11 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2011-06-27 15:00:05 +00:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
|
2011-06-27 19:57:11 +00:00
|
|
|
#define MAX_FILLER 11
|
2011-06-27 15:00:05 +00:00
|
|
|
|
|
|
|
static char *ltoa(char *p, long num, unsigned radix) {
|
|
|
|
int i;
|
|
|
|
char *q;
|
|
|
|
|
2011-06-27 19:57:11 +00:00
|
|
|
q = p + MAX_FILLER;
|
2011-06-27 15:00:05 +00:00
|
|
|
do {
|
|
|
|
i = (int)(num % radix);
|
|
|
|
i += '0';
|
|
|
|
if (i > '9')
|
2011-06-27 19:57:11 +00:00
|
|
|
i += 'A' - '0' - 10;
|
2011-06-27 15:00:05 +00:00
|
|
|
*--q = i;
|
|
|
|
} while ((num /= radix) != 0);
|
|
|
|
|
2011-06-27 19:57:11 +00:00
|
|
|
i = (int)(p + MAX_FILLER - q);
|
2011-06-27 15:00:05 +00:00
|
|
|
do
|
|
|
|
*p++ = *q++;
|
|
|
|
while (--i);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void chprintf(BaseChannel *chp, const char *fmt, ...) {
|
|
|
|
va_list ap;
|
2011-06-27 19:57:11 +00:00
|
|
|
char buf[MAX_FILLER + 1];
|
|
|
|
char *p, *s, c, filler;
|
|
|
|
int i, n, width;
|
|
|
|
bool_t lflag, ljust;
|
2011-06-27 15:00:05 +00:00
|
|
|
long l;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
while (TRUE) {
|
|
|
|
c = *fmt++;
|
|
|
|
if (c == 0) {
|
|
|
|
va_end(ap);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (c != '%') {
|
|
|
|
chIOPut(chp, (uint8_t)c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
p = buf;
|
|
|
|
s = buf;
|
2011-06-27 19:57:11 +00:00
|
|
|
ljust = FALSE;
|
2011-06-27 15:00:05 +00:00
|
|
|
if (*fmt == '-') {
|
|
|
|
fmt++;
|
2011-06-27 19:57:11 +00:00
|
|
|
ljust = TRUE;
|
2011-06-27 15:00:05 +00:00
|
|
|
}
|
2011-06-27 19:57:11 +00:00
|
|
|
filler = ' ';
|
|
|
|
if (*fmt == '.') {
|
2011-06-27 15:00:05 +00:00
|
|
|
fmt++;
|
2011-06-27 19:57:11 +00:00
|
|
|
filler = '0';
|
2011-06-27 15:00:05 +00:00
|
|
|
}
|
|
|
|
for (width = 0;;) {
|
|
|
|
c = *fmt++;
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
c -= '0';
|
|
|
|
else if (c == '*')
|
|
|
|
c = va_arg(ap, int);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
width *= 10;
|
|
|
|
width += c;
|
|
|
|
}
|
2011-06-27 19:57:11 +00:00
|
|
|
n = 0;
|
2011-06-27 15:00:05 +00:00
|
|
|
if (c == '.') {
|
|
|
|
for (;;) {
|
|
|
|
c = *fmt++;
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
c -= '0';
|
|
|
|
else if (c == '*')
|
|
|
|
c = va_arg(ap, int);
|
|
|
|
else
|
|
|
|
break;
|
2011-06-27 19:57:11 +00:00
|
|
|
n *= 10;
|
|
|
|
n += c;
|
2011-06-27 15:00:05 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-27 19:57:11 +00:00
|
|
|
lflag = FALSE;
|
2011-06-27 15:00:05 +00:00
|
|
|
if (c == 'l' || c == 'L') {
|
|
|
|
lflag++;
|
|
|
|
if (*fmt)
|
|
|
|
c = *fmt++;
|
|
|
|
}
|
|
|
|
switch (c) {
|
|
|
|
case 'X':
|
2011-06-27 19:57:11 +00:00
|
|
|
lflag = TRUE;
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'x':
|
|
|
|
c = 16;
|
|
|
|
goto oxu;
|
|
|
|
case 'U':
|
2011-06-27 19:57:11 +00:00
|
|
|
lflag = TRUE;
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'u':
|
|
|
|
c = 10;
|
|
|
|
goto oxu;
|
|
|
|
case 'O':
|
2011-06-27 19:57:11 +00:00
|
|
|
lflag = TRUE;
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'o':
|
|
|
|
c = 8;
|
2011-06-27 19:57:11 +00:00
|
|
|
oxu: if (lflag)
|
|
|
|
l = va_arg(ap, long);
|
|
|
|
else
|
|
|
|
l = va_arg(ap, int);
|
|
|
|
p = ltoa(p, l, c);
|
2011-06-27 15:00:05 +00:00
|
|
|
break;
|
|
|
|
case 'D':
|
2011-06-27 19:57:11 +00:00
|
|
|
lflag = TRUE;
|
2011-06-27 15:00:05 +00:00
|
|
|
case 'd':
|
2011-06-27 19:57:11 +00:00
|
|
|
if (lflag)
|
|
|
|
l = va_arg(ap, long);
|
|
|
|
else
|
|
|
|
l = va_arg(ap, int);
|
|
|
|
if (l < 0) {
|
2011-06-27 15:00:05 +00:00
|
|
|
*p++ = '-';
|
2011-06-27 19:57:11 +00:00
|
|
|
l = -l;
|
2011-06-27 15:00:05 +00:00
|
|
|
}
|
2011-06-27 19:57:11 +00:00
|
|
|
p = ltoa(p, l, 10);
|
2011-06-27 15:00:05 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
2011-06-27 19:57:11 +00:00
|
|
|
filler = ' ';
|
2011-06-27 15:00:05 +00:00
|
|
|
*p++ = va_arg(ap, int);
|
|
|
|
break;
|
|
|
|
case 's':
|
2011-06-27 19:57:11 +00:00
|
|
|
filler = ' ';
|
2011-06-27 15:00:05 +00:00
|
|
|
if ((s = va_arg(ap, char *)) == 0)
|
|
|
|
s = "(null)";
|
2011-06-27 19:57:11 +00:00
|
|
|
if (n == 0)
|
|
|
|
n = 32767;
|
|
|
|
for (p = s; *p && --n >= 0; p++)
|
2011-06-27 15:00:05 +00:00
|
|
|
;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*p++ = c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = (int)(p - s);
|
|
|
|
if ((width -= i) < 0)
|
|
|
|
width = 0;
|
2011-06-27 19:57:11 +00:00
|
|
|
if (ljust == FALSE)
|
2011-06-27 15:00:05 +00:00
|
|
|
width = -width;
|
|
|
|
if (width < 0) {
|
2011-06-27 19:57:11 +00:00
|
|
|
if (*s == '-' && filler == '0') {
|
2011-06-27 15:00:05 +00:00
|
|
|
chIOPut(chp, (uint8_t)*s++);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
do
|
2011-06-27 19:57:11 +00:00
|
|
|
chIOPut(chp, (uint8_t)filler);
|
2011-06-27 15:00:05 +00:00
|
|
|
while (++width != 0);
|
|
|
|
}
|
|
|
|
while (--i >= 0)
|
|
|
|
chIOPut(chp, (uint8_t)*s++);
|
|
|
|
|
|
|
|
while (width) {
|
2011-06-27 19:57:11 +00:00
|
|
|
chIOPut(chp, (uint8_t)filler);
|
2011-06-27 15:00:05 +00:00
|
|
|
width--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|