/* $Id: snprintf.c,v 1.4 2006/12/04 23:45:07 craig Exp $ */
/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished proprietary information of BroadOn Communications Corp.,
 *  and are protected by federal copyright law. They may not be disclosed
 *  to third parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 *  Description:
 *      This file implements standard IO functions using semi-hosting
 *
 */


#include <stdarg.h>
#include <ioslibc.h>
#include "printf.h"

typedef struct {
    Context v;
    char *buf;
    unsigned int pos;
    int max;
} StrContext;
    

static void __strFlush(Context *ctx)
{
    StrContext *strctx = (StrContext *)ctx;
    if (strctx->pos < strctx->max) {
        strctx->buf[strctx->pos] = 0;
    } else {
        strctx->buf[strctx->max-1] = 0;
    }
}

static void __strWritec(Context *ctx, char c)
{
    StrContext *strctx = (StrContext *)ctx;
    if (strctx->pos < strctx->max) {
        strctx->buf[strctx->pos] = c;
    }
    strctx->pos++;
}

static void __writes(Context *ctx, char *s)
{
    while (*s) {
        ctx->writec(ctx, *s);
        ++s;
    }
}

static void PAD(Context *ctx, int n, char c) {
    while (n--) {
        ctx->writec(ctx, c); 
    } 
}

/*
 *  Function: printf
 *
 *  Description:
 *      Print a string to the console using semi-hosting.
 *      Currently %c, %s, %x, %X, and %d are supported
 *
 *  Inputs:
 *      fmt - Format string
 *      ... - Variable number of arguments
 *
 *  Outputs:
 *      A string printed to the console
 *
 *  Return values:
 *      0 - Successful completion
 *
 */
void
__printf(Context *ctx, const char *fmt, va_list ap)
{
    char                c = '\0';
    char                scratch[24], *str, *ptr;
    unsigned int        leading0 = 0, neg = 0;
    unsigned int        value = 0, value2 = 0;
    int                 svalue = 0;
    int                 count = 0;
    int                 showLeading0s = 0;
    int                 width = 0;
    int                 left = 0;
    int                 alternate = 0;
    int                 len;
    int                 sign = 0;
    char                t, base;
    int                 length = 0;

    while ((c = *fmt++)) {
        if (c == '%') {
            /* flags */
            width = alternate = left = sign = showLeading0s = 0;
            while ((c = *fmt++)) {
                switch (c) {
                case '#':
                    alternate = 1;
                    break;
                case '+':
                    sign = 1;
                    break;
                case '-':
                    left = 1;
                    break;
                case '0':
                    showLeading0s = 1;
                    break;
                case '*':
                    width = (int) va_arg(ap, int);
                    break;
                default:
                    goto end_flags;
                }
            }
end_flags:
            /* field width */
            while (c >= '0' && c <= '9') {
                width = width * 10 + (c - '0');
                c = *fmt++;
            }
        
            /* length modifier */
            length = 0;
            while (c == 'l') {
                ++length;
                c = *fmt++;
            }

            /* process format */
            switch (c) {
            case 'c':
                c = (char) va_arg(ap, int);
                if (width > 1 && !left) {
                    PAD(ctx, width-1, ' ');
                }   
                ctx->writec(ctx, c);
                if (width > 1 && left) {
                    PAD(ctx, width-1, ' ');
                }   
                break;

            case 's':
                str = va_arg(ap, char *);
                if (!str) {
                    str = "(null)";
                }
string:
                len = strnlen(str, 1024);
                if (width > len && !left) {
                    PAD(ctx, width-len, ' ');
                }   
                __writes(ctx, str);
                if (width > len && left) {
                    PAD(ctx, width-len, ' ');
                }   
                break;

            case 'p':
            case 'x':
            case 'X':
                if (length == 2) {
                    u64 ll = va_arg(ap, unsigned long long);
                    value = ll & 0xffffffff;
                    value2 = ll >> 32;
                } else {
                    value = va_arg(ap, unsigned int);
                }

                if (c == 'p') {
                    if (value) {
                        c = 'x';
                        alternate = 1;
                    } else {
                        str = "(nil)";
                        goto string;
                    }
                }

                if (c == 'x') {
                    base = 'a'-10;
                } else {
                    base = 'A'-10;
                }

                ptr = scratch;

                leading0 = 1;
                for (count = length == 2 ? 60 : 28; count >= 0; count -= 4) {
                    unsigned char b;
                    if (count > 28) {
                        b = (value2 >> (count-32)) & 0xf;
                    } else {
                        b = (value >> count) & 0xf;
                    }

                    if (!b && leading0) {
                        continue;
                    } else {
                        leading0 = 0;
                        *ptr++ = b < 10 ? '0' + b : base + b;
                    }
                }

                if (leading0) {
                    *ptr++ = '0';
                }
                *ptr = 0;
                len = strnlen(scratch, sizeof(scratch));
                if (value && alternate) len+=2;
                if (width > len && !left) {
                    if (showLeading0s) {
                        if (value && alternate) {
                            PAD(ctx, 1, '0');
                            PAD(ctx, 1, c);
                        }
                        PAD(ctx, width-len, '0');
                    } else {
                        PAD(ctx, width-len, ' ');
                        if (value && alternate) {
                            PAD(ctx, 1, '0');
                            PAD(ctx, 1, c);
                        }
                    }
                } else if (value && alternate) {
                    PAD(ctx, 1, '0');
                    PAD(ctx, 1, c);
                } 
                __writes(ctx, scratch);
                if (width > len && left) {
                    PAD(ctx, width-len, ' ');
                }
                break;

            case 'u':
            case 'd':
                svalue = va_arg(ap, int);

                ptr = scratch + sizeof(scratch);
                neg = 0;

                if (svalue < 0) {
                    neg = 1;
                    svalue = -svalue;   
                }

                *--ptr = '\0';
                do {
                    *--ptr = '0' + (svalue % 10);
                } while ((svalue /= 10) != 0);

                if (neg) {
                    t = '-';
                } else if (sign) {
                    t = '+';
                } else {
                    t = 0;
                }

                len = strnlen(ptr, sizeof(scratch));
                if (t) ++len;
                if (width > len && !left) {
                    if (showLeading0s) {
                        if (t) {
                            PAD(ctx, 1, t);
                        }
                        PAD(ctx, width-len, '0');
                    } else {
                        PAD(ctx, width-len, ' ');
                        if (t) {
                            PAD(ctx, 1, t);
                        }
                    }
                } else if (t) {
                    PAD(ctx, 1, t);
                }  
                __writes(ctx, ptr);
                if (width > len && left) {
                    PAD(ctx, width-len, ' ');
                }
                break;

            case '%':
            default:
                ctx->writec(ctx, c);
                break;
            }
        } else {
            ctx->writec(ctx, c);
        }
    }

    ctx->flush(ctx);
}

int
vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
{
    StrContext ctx;
    ctx.pos = 0;
    ctx.buf = str;
    ctx.max = size;
    ctx.v.writec = __strWritec;
    ctx.v.flush = __strFlush;
    __printf((Context *)&ctx, fmt, ap);
    return ctx.pos;
}

int
snprintf(char *str, size_t size, const char *fmt, ...)
{
    va_list ap;
    int rv;

    va_start(ap, fmt);
    rv = vsnprintf(str, size, fmt, ap);
    va_end(ap);

    return rv;
}
