term: update module to work on linux and darwin

This commit is contained in:
2024-11-22 22:29:40 +00:00
parent 753d3ea9d3
commit d88b42f50e
6 changed files with 545 additions and 352 deletions

View File

@@ -122,8 +122,7 @@ struct out_tty_args {
};
// output function type
typedef void (*out_fct_type)(
char character, struct out_tty_args *args);
typedef void (*out_fct_type)(char character, struct out_tty_args *args);
// wrapper (used as buffer) for output function type
typedef struct {
@@ -149,17 +148,6 @@ static inline void _out_null(char character, void *buffer, size_t idx, size_t ma
(void)maxlen;
}
// internal _putchar wrapper
static inline void _out_char(char character, void *buffer, size_t idx, size_t maxlen)
{
(void)buffer;
(void)idx;
(void)maxlen;
if (character) {
_putchar(character);
}
}
// internal output function wrapper
static inline void _out_fct(char character, void *buffer, size_t idx, size_t maxlen)
{
@@ -201,8 +189,8 @@ static unsigned int _atoi(const char **str)
// output the specified string in reverse, taking care of any zero-padding
static size_t _out_rev(
out_fct_type out, struct out_tty_args *args,
const char *buf, size_t len, unsigned int width, unsigned int flags)
out_fct_type out, struct out_tty_args *args, const char *buf,
size_t len, unsigned int width, unsigned int flags)
{
size_t idx = 0;
@@ -231,9 +219,9 @@ static size_t _out_rev(
// internal itoa format
static size_t _ntoa_format(
out_fct_type out, struct out_tty_args *args, char *buf,
size_t len, bool negative, unsigned int base, unsigned int prec,
unsigned int width, unsigned int flags)
out_fct_type out, struct out_tty_args *args, char *buf, size_t len,
bool negative, unsigned int base, unsigned int prec, unsigned int width,
unsigned int flags)
{
// pad leading zeros
if (!(flags & FLAGS_LEFT)) {
@@ -289,9 +277,9 @@ static size_t _ntoa_format(
// internal itoa for 'long' type
static size_t _ntoa_long(
out_fct_type out, struct out_tty_args *args,
unsigned long value, bool negative, unsigned long base,
unsigned int prec, unsigned int width, unsigned int flags)
out_fct_type out, struct out_tty_args *args, unsigned long value,
bool negative, unsigned long base, unsigned int prec,
unsigned int width, unsigned int flags)
{
char buf[PRINTF_NTOA_BUFFER_SIZE];
size_t len = 0U;
@@ -314,16 +302,16 @@ static size_t _ntoa_long(
}
return _ntoa_format(
out, args, buf, len, negative,
(unsigned int)base, prec, width, flags);
out, args, buf, len, negative, (unsigned int)base, prec, width,
flags);
}
// internal itoa for 'long long' type
#if defined(PRINTF_SUPPORT_LONG_LONG)
static size_t _ntoa_long_long(
out_fct_type out, struct out_tty_args *args,
unsigned long long value, bool negative, unsigned long long base,
unsigned int prec, unsigned int width, unsigned int flags)
out_fct_type out, struct out_tty_args *args, unsigned long long value,
bool negative, unsigned long long base, unsigned int prec,
unsigned int width, unsigned int flags)
{
char buf[PRINTF_NTOA_BUFFER_SIZE];
size_t len = 0U;
@@ -346,8 +334,8 @@ static size_t _ntoa_long_long(
}
return _ntoa_format(
out, args, buf, len, negative,
(unsigned int)base, prec, width, flags);
out, args, buf, len, negative, (unsigned int)base, prec, width,
flags);
}
#endif // PRINTF_SUPPORT_LONG_LONG
@@ -381,8 +369,7 @@ static size_t _ftoa(
return _out_rev(out, args, "fni-", 4, width, flags);
if (value > DBL_MAX)
return _out_rev(
out, args,
(flags & FLAGS_PLUS) ? "fni+" : "fni",
out, args, (flags & FLAGS_PLUS) ? "fni+" : "fni",
(flags & FLAGS_PLUS) ? 4U : 3U, width, flags);
// test for very large values
@@ -582,7 +569,9 @@ static size_t _etoa(
}
// output the floating part
size_t idx = _ftoa(out, args, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP);
size_t idx
= _ftoa(out, args, negative ? -value : value, prec, fwidth,
flags & ~FLAGS_ADAPT_EXP);
// output the exponent part
if (minwidth) {
@@ -590,9 +579,8 @@ static size_t _etoa(
out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', args);
// output the exponent value
idx = _ntoa_long(
out, args,
(expval < 0) ? -expval : expval, expval < 0, 10, 0,
minwidth - 1, FLAGS_ZEROPAD | FLAGS_PLUS);
out, args, (expval < 0) ? -expval : expval, expval < 0,
10, 0, minwidth - 1, FLAGS_ZEROPAD | FLAGS_PLUS);
// might need to right-pad spaces
if (flags & FLAGS_LEFT) {
while (idx < width) {
@@ -606,13 +594,12 @@ static size_t _etoa(
#endif // PRINTF_SUPPORT_EXPONENTIAL
#endif // PRINTF_SUPPORT_FLOAT
#define set_format_ch(args) ((args)->format_ch = true)
#define set_format_ch(args) ((args)->format_ch = true)
#define unset_format_ch(args) ((args)->format_ch = false)
// internal vsnprintf
static int _vsnprintf(
out_fct_type out, struct out_tty_args *args, const char *format,
va_list va)
out_fct_type out, struct out_tty_args *args, const char *format, va_list va)
{
unsigned int flags, width, precision, n;
size_t idx = 0U;
@@ -831,15 +818,14 @@ static int _vsnprintf(
const unsigned int value
= (flags & FLAGS_CHAR)
? (unsigned char)va_arg(
va, unsigned int)
va, unsigned int)
: (flags & FLAGS_SHORT)
? (unsigned short int)va_arg(
va, unsigned int)
va, unsigned int)
: va_arg(va, unsigned int);
idx = _ntoa_long(
out, args, value,
false, base, precision, width,
flags);
out, args, value, false, base,
precision, width, flags);
}
}
format++;
@@ -850,9 +836,9 @@ static int _vsnprintf(
case 'F':
if (*format == 'F')
flags |= FLAGS_UPPERCASE;
idx = _ftoa(
out, args, va_arg(va, double),
precision, width, flags);
idx
= _ftoa(out, args, va_arg(va, double),
precision, width, flags);
format++;
break;
#if defined(PRINTF_SUPPORT_EXPONENTIAL)
@@ -864,9 +850,9 @@ static int _vsnprintf(
flags |= FLAGS_ADAPT_EXP;
if ((*format == 'E') || (*format == 'G'))
flags |= FLAGS_UPPERCASE;
idx = _etoa(
out, args, va_arg(va, double),
precision, width, flags);
idx
= _etoa(out, args, va_arg(va, double),
precision, width, flags);
format++;
break;
#endif // PRINTF_SUPPORT_EXPONENTIAL
@@ -926,9 +912,8 @@ static int _vsnprintf(
const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
if (is_ll) {
idx = _ntoa_long_long(
out, args,
(uintptr_t)va_arg(va, void *), false,
16U, precision, width, flags);
out, args, (uintptr_t)va_arg(va, void *),
false, 16U, precision, width, flags);
} else {
#endif
idx = _ntoa_long(
@@ -983,14 +968,15 @@ static void out_tty(char c, struct out_tty_args *args)
b_tty_putc(args->tty, flags, c);
}
int b_tty_vprintf(struct b_tty *tty, enum b_tty_print_flags flags, const char *format, va_list args)
int b_tty_vprintf(
struct b_tty *tty, enum b_tty_print_flags flags, const char *format,
va_list args)
{
struct out_tty_args tty_args = {
.flags = flags,
.tty = tty,
};
const int ret = _vsnprintf(
out_tty, &tty_args, format, args);
const int ret = _vsnprintf(out_tty, &tty_args, format, args);
return ret;
}